cpu-sched: Do not depend on foreground= being set in configuration 82/242182/5
authorKarol Lewandowski <k.lewandowsk@samsung.com>
Mon, 24 Aug 2020 23:35:52 +0000 (01:35 +0200)
committerKarol Lewandowski <k.lewandowsk@samsung.com>
Fri, 28 Aug 2020 10:19:26 +0000 (12:19 +0200)
Change-Id: I48330dfe807ab4c581ae2d8a17a0019eead18dd8

src/cpu/cpu-sched.c

index 08b63f9..ed28754 100644 (file)
@@ -28,7 +28,7 @@ struct coreset {
 };
 
 struct cpu_sched {
-       struct coreset fg;
+       struct coreset *fg;
        GSList *apps;
        bool is_initalized;
 };
@@ -139,7 +139,8 @@ static int cpu_sched_init_cgroup(struct cpu_sched *cs)
                        return r;
                }
        }
-       return cpu_sched_init_cgroup_set(&cs->fg);
+
+       return cs->fg ? cpu_sched_init_cgroup_set(cs->fg) : 0;
 }
 
 /* create single core struct representation */
@@ -232,33 +233,29 @@ parse_cpuset_fail:
 
 static int load_config(struct parse_result *result, void *user_data)
 {
-       int r;
        struct cpu_sched *data = (struct cpu_sched *)user_data;
 
        assert(data);
 
-       if (strcmp(result->name, CPU_SCHED_FG_NAME)) { /* not a 'foreground' group */
-               struct coreset *c = (struct coreset *)calloc(1, sizeof *c);
-               if (NULL == c)
-                       return RESOURCED_ERROR_OUT_OF_MEMORY;
-
-               c->name = strdup(result->name);
-
-               r = cpu_sched_parse_cpuset(c, result->value);
-
-               if (RESOURCED_ERROR_NONE == r)
-                       data->apps = g_slist_append(data->apps, c);
-       } else {
-               data->fg.name = strdup(CPU_SCHED_FG_NAME);
-               r = cpu_sched_parse_cpuset(&data->fg, result->value);
-       }
+       struct coreset *c = (struct coreset *)calloc(1, sizeof *c);
+       if (NULL == c)
+               return RESOURCED_ERROR_OUT_OF_MEMORY;
 
+       int r = cpu_sched_parse_cpuset(c, result->value);
        if (r < 0) {
-               _E("cpu-sched parse fg coreset: could not parse");
+               _E("cpu-sched parse %s coreset: could not parse", result->name);
                return RESOURCED_ERROR_FAIL;
        }
 
-       return RESOURCED_ERROR_NONE;
+       if (strcmp(result->name, CPU_SCHED_FG_NAME)) { /* not a 'foreground' group */
+               c->name = strdup(result->name);
+               data->apps = g_slist_append(data->apps, c);
+       } else {
+               c->name = strdup(CPU_SCHED_FG_NAME);
+               data->fg = c;
+       }
+
+       return c->name ? RESOURCED_ERROR_NONE : RESOURCED_ERROR_OUT_OF_MEMORY;
 }
 
 static int cpu_sched_parse_config(struct cpu_sched *data)
@@ -279,7 +276,6 @@ static int cpu_sched_write_coreset(struct coreset *set)
 
        assert(set);
 
-
        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);
@@ -372,7 +368,8 @@ static int cpu_sched_cpu_on(void *data)
        gslist_for_each_item(i, cs.apps) {
                cpu_sched_cpu_on_for_coreset((struct coreset *)i->data, id);
        }
-       cpu_sched_cpu_on_for_coreset(&cs.fg, id);
+       if (cs.fg)
+               cpu_sched_cpu_on_for_coreset(cs.fg, id);
 
        return RESOURCED_ERROR_NONE;
 }
@@ -391,7 +388,8 @@ static int cpu_sched_cpu_off(void *data)
                cpu_sched_cpu_off_for_coreset((struct coreset *)i->data, id);
        }
 
-       cpu_sched_cpu_off_for_coreset(&cs.fg, id);
+       if (cs.fg)
+               cpu_sched_cpu_off_for_coreset(cs.fg, id);
 
        return RESOURCED_ERROR_NONE;
 }
@@ -471,6 +469,8 @@ static int cpu_sched_add_pid_to_cpuset(struct coreset *set, pid_t pid, bool move
 
 static int cpu_sched_app_foreground(void *data)
 {
+       assert(cs.fg);
+
        struct proc_status *ps = (struct proc_status *)data;
 
        assert(ps);
@@ -485,10 +485,9 @@ static int cpu_sched_app_foreground(void *data)
 
        _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, true);
+       return cpu_sched_add_pid_to_cpuset(cs.fg, ps->pid, true);
 }
 
-
 static int cpu_sched_app_launch(void *data)
 {
        struct proc_status *ps = (struct proc_status *)data;
@@ -508,9 +507,11 @@ static int cpu_sched_app_launch(void *data)
 
 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);
+       if (cs.fg) {
+               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);
@@ -520,9 +521,11 @@ static void register_notifiers()
 
 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);
+       if (cs.fg) {
+               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);
@@ -533,7 +536,11 @@ static void unregister_notifiers()
 static void cpu_sched_free_cpusets()
 {
        g_slist_free_full(g_steal_pointer(&cs.apps), cpu_sched_free_cpuset_full);
-       cpu_sched_free_cpuset(&cs.fg);
+       if (cs.fg) {
+               cpu_sched_free_cpuset(cs.fg);
+               free(cs.fg);
+               cs.fg = NULL;
+       }
 }
 
 static void cpu_sched_check_apps()
@@ -555,8 +562,8 @@ static void cpu_sched_check_apps()
                        continue;
                }
 
-               if (pai->state == PROC_STATE_FOREGROUND)
-                       cpu_sched_add_pid_to_cpuset(&cs.fg, pai->main_pid, true);
+               if (cs.fg && pai->state == PROC_STATE_FOREGROUND)
+                       cpu_sched_add_pid_to_cpuset(cs.fg, pai->main_pid);
        }
 }