From 3dc33c273d0fdfa88e226b9aac1a00a912183c04 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Maciej=20S=C5=82odczyk?= Date: Tue, 23 Jun 2020 22:24:08 +0200 Subject: [PATCH] cpu-sched: use apps' foreground events to pin to proper cpuset MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Change-Id: Icb19ef5628f1576aa87f9cc86ed3db59b24f18ef Signed-off-by: Maciej Słodczyk --- src/cpu/cpu-sched.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/src/cpu/cpu-sched.c b/src/cpu/cpu-sched.c index a40d92b..728396b 100644 --- a/src/cpu/cpu-sched.c +++ b/src/cpu/cpu-sched.c @@ -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); } -- 2.7.4