#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"
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);
}