cpu-sched: use apps' foreground events to pin to proper cpuset 65/236965/7
authorMaciej Słodczyk <m.slodczyk2@partner.samsung.com>
Tue, 23 Jun 2020 20:24:08 +0000 (22:24 +0200)
committerHyotaek Shim <hyotaek.shim@samsung.com>
Tue, 21 Jul 2020 11:56:36 +0000 (11:56 +0000)
Change-Id: Icb19ef5628f1576aa87f9cc86ed3db59b24f18ef
Signed-off-by: Maciej Słodczyk <m.slodczyk2@partner.samsung.com>
src/cpu/cpu-sched.c

index a40d92b..728396b 100644 (file)
@@ -7,6 +7,7 @@
 #include "cgroup.h"
 #include "notifier.h"
 #include "cpu-hotplug.h"
+#include "proc-common.h"
 
 #define CPU_SCHED_CONF_FILE     RD_CONFIG_FILE(cpu-sched)
 #define CPU_SCHED_FG_NAME      "foreground"
@@ -339,14 +340,73 @@ static int cpu_sched_cpu_off(void *data)
        return RESOURCED_ERROR_NONE;
 }
 
+static int cpu_sched_add_pid_to_cpuset(struct coreset *set, pid_t pid)
+{
+       pid_t old_pid;
+       char path[128];
+       int r;
+
+       assert(set);
+
+       _D("cpu-sched: add pid %d to cpuset %s", pid, set->name);
+       r = snprintf(path, sizeof path, "%s/%s", CPUSET_CGROUP, set->name);
+       if (r < 0) {
+               _E("cpu-sched: failed to setup path for cpuset (%s)", set->name);
+               return r;
+       }
+
+       /* read pid of previous foreground app */
+       if (RESOURCED_ERROR_NONE != cgroup_read_node_int32(path, "tasks", &old_pid)) {
+               _E("cpu-sched: could not read old value of fg app's pid");
+               return RESOURCED_ERROR_FAIL;
+       }
+
+       /* and write it to default cpuset */
+       r = cgroup_write_node_int32(CPUSET_CGROUP, "tasks", (u_int32_t)old_pid);
+       ret_value_msg_if(r < 0, RESOURCED_ERROR_FAIL,
+               "Failed to attach pid %d to cgroup %s : %m",
+                        old_pid, CPUSET_CGROUP);
+
+       /* write current foreground app to proper cpuset */
+       r = cgroup_write_node_int32(path, "tasks", (u_int32_t)pid);
+       ret_value_msg_if(r < 0, RESOURCED_ERROR_FAIL,
+               "Failed to attach pid %d to cgroup %s : %m",
+                        pid, path);
+
+       return RESOURCED_ERROR_NONE;
+}
+
+static int cpu_sched_app_foreground(void *data)
+{
+       struct proc_status *ps = (struct proc_status *)data;
+
+       assert(ps);
+       assert(ps->pai);
+
+       if (false == cs.is_initalized)
+               return RESOURCED_ERROR_NONE;
+
+       _D("cpu-sched: app %s moved to foreground; pid=%d", ps->pai->appid, ps->pid);
+
+       return cpu_sched_add_pid_to_cpuset(&cs.fg, ps->pid);
+}
+
 static void register_notifiers()
 {
+       register_notifier(RESOURCED_NOTIFIER_APP_RESUME, cpu_sched_app_foreground);
+       register_notifier(RESOURCED_NOTIFIER_APP_FOREGRD, cpu_sched_app_foreground);
+       register_notifier(RESOURCED_NOTIFIER_WIDGET_FOREGRD, cpu_sched_app_foreground);
+
        register_notifier(RESOURCED_NOTIFIER_CPU_ON, cpu_sched_cpu_on);
        register_notifier(RESOURCED_NOTIFIER_CPU_OFF, cpu_sched_cpu_off);
 }
 
 static void unregister_notifiers()
 {
+       unregister_notifier(RESOURCED_NOTIFIER_APP_RESUME, cpu_sched_app_foreground);
+       unregister_notifier(RESOURCED_NOTIFIER_APP_FOREGRD, cpu_sched_app_foreground);
+       unregister_notifier(RESOURCED_NOTIFIER_WIDGET_FOREGRD, cpu_sched_app_foreground);
+
        unregister_notifier(RESOURCED_NOTIFIER_CPU_ON, cpu_sched_cpu_on);
        unregister_notifier(RESOURCED_NOTIFIER_CPU_OFF, cpu_sched_cpu_off);
 }