cpu-sched: Refactor common cpu affinity code 58/287858/8
authorUnsung Lee <unsung.lee@samsung.com>
Tue, 7 Feb 2023 07:00:18 +0000 (16:00 +0900)
committerUnsung Lee <unsung.lee@samsung.com>
Wed, 15 Feb 2023 05:46:27 +0000 (14:46 +0900)
This patch includes
  - Refactoring of cpu-sched related code
    * 'cpu_sched_parse_cpuset' and 'cpu_shced_new_core' functions are moved
  from cpu-sched.c to config-parser.c/h
    * Cpu common data structure (i.e., 'coreset' and 'core') is moved
  from cpu-sched.c to cpu-common.h
    * Comments are modified
  - Renaming variable
    * Data structure: 'core' -> 'cpu_info' and 'coreset' -> 'cpuset_info'
    * Element: 'id' -> 'cpu_id' and 'on' -> 'online'
    * Other: minor change (such as local variable)

Change-Id: I1d04ee9c33b93f5f82504815c4344c2d472e25b8
Signed-off-by: Unsung Lee <unsung.lee@samsung.com>
src/common/conf/config-parser.c
src/common/conf/config-parser.h
src/common/conf/cpu-common.h
src/resource-optimizer/cpu/cpu-sched.c

index 25da66b..684ecc5 100644 (file)
@@ -205,6 +205,87 @@ static cpu_boosting_level_e config_parse_boosting_level(const char *value)
                return CPU_BOOSTING_LEVEL_NONE;
 }
 
+/* Create a single cpu_info struct representation */
+static int cpu_sched_new_cpu_info(struct cpuset_info *set, int cpu_id)
+{
+       struct cpu_info *cpu_info;
+
+       assert(set);
+
+       if (cpu_id < 0)
+               return RESOURCED_ERROR_FAIL;
+
+       cpu_info = (struct cpu_info *)calloc(1, sizeof *cpu_info);
+       if (cpu_info == NULL) {
+               _E("cpu-sched: could not allocate memory for cpu_info struct");
+               return RESOURCED_ERROR_FAIL;
+       }
+       cpu_info->cpu_id = cpu_id;
+       cpu_info->online = false;
+
+       set->cpu_info = g_slist_append(set->cpu_info, cpu_info);
+       return RESOURCED_ERROR_NONE;
+}
+
+static int cpu_sched_new_cpu_info_range(struct cpuset_info *set, int min_cpu, int max_cpu)
+{
+       assert(set);
+
+       if (min_cpu < 0 || max_cpu < 0 || min_cpu >= max_cpu) {
+               _E("cpu range (%d - %d) is wrong", min_cpu, max_cpu);
+               return RESOURCED_ERROR_INVALID_PARAMETER;
+       }
+
+       for (int cpu = min_cpu; cpu <= max_cpu; cpu++) {
+               if (cpu_sched_new_cpu_info(set, cpu) != RESOURCED_ERROR_NONE)
+                       return RESOURCED_ERROR_FAIL;
+       }
+
+       return RESOURCED_ERROR_NONE;
+}
+
+/* Parse config for specific cpuset_info line */
+int cpu_sched_parse_cpuset(void *data, char *value)
+{
+       int count;
+       int min, max;
+       struct cpuset_info *set = (struct cpuset_info *)data;
+       char *ptr, *saveptr;
+
+       assert(set);
+       assert(value);
+       assert(!set->cpu_info);
+
+       ptr = strtok_r(value, ",", &saveptr);
+       while (ptr) {
+               /* single value */
+               if (strstr(ptr, "-") == NULL) {
+                       count = sscanf(ptr, "%d", &min);
+                       if (count != 1)
+                               goto parse_cpuset_fail;
+
+                       if (cpu_sched_new_cpu_info(set, min) != RESOURCED_ERROR_NONE)
+                               goto parse_cpuset_fail;
+               /* range */
+               } else {
+                       count = sscanf(ptr, "%d-%d", &min, &max);
+                       if (count != 2)
+                               goto parse_cpuset_fail;
+
+                       if (cpu_sched_new_cpu_info_range(set, min, max) != RESOURCED_ERROR_NONE)
+                               goto parse_cpuset_fail;
+               }
+
+               ptr = strtok_r(NULL, ",", &saveptr);
+       }
+
+       return RESOURCED_ERROR_NONE;
+
+parse_cpuset_fail:
+       _E("cpu-sched: error parsing cpuset (%s)", ptr);
+       return RESOURCED_ERROR_FAIL;
+}
+
 static int optimizer_config(struct parse_result *result, void *user_data)
 {
        if (!result)
index 66787d7..064fb72 100644 (file)
@@ -198,6 +198,7 @@ GSList *get_app_conf_info_list(void);
 GSList *get_service_conf_info_list(void);
 void resourced_parse_vendor_configs(void);
 void resourced_free_vendor_configs(void);
+int cpu_sched_parse_cpuset(void *data, char *value);
 #ifdef __cplusplus
 }
 #endif /* __cplusplus */
index bd549b8..34e5ff8 100644 (file)
@@ -65,6 +65,17 @@ struct cpuset_cgroup_info {
        char value[64];
 };
 
+struct cpu_info {
+       int cpu_id;
+       bool online;
+};
+
+struct cpuset_info {
+       GSList *cpu_info;
+       char *name;
+       bool disclaimer_shown;
+};
+
 struct cpu_cgroup_info {
        long long cfs_runtime_us;
        long long rt_runtime_us;
index 847e0cd..0486b02 100644 (file)
 #define GLOBAL_RT_RUNTIME_US_PATH      "/proc/sys/kernel/sched_rt_runtime_us"
 #define CPU_SCHED_FEATURE_PATH         "/sys/kernel/debug/sched_features"
 
-struct core {
-       int id;
-       bool on;
-};
-
-struct coreset {
-       GSList *cores;
-       char *name;
-       bool disclaimer_shown;
-};
-
 struct cpu_sched {
-       struct coreset *fg;
+       struct cpuset_info *fg;
        GSList *apps;
        bool is_initialized;
 };
 
 static struct cpu_sched cs;
 
-struct coreset *cpu_sched_find_coreset(const char *name)
+struct cpuset_info *cpu_sched_find_coreset(const char *name)
 {
        GSList *i;
-       struct coreset *c;
+       struct cpuset_info *cpuset_info;
 
        gslist_for_each_item(i, cs.apps) {
-               c = (struct coreset *)i->data;
-               if (!strcmp(name, c->name))
-                       return c;
+               cpuset_info = (struct cpuset_info *)i->data;
+               if (!strcmp(name, cpuset_info->name))
+                       return cpuset_info;
        }
        return NULL;
 }
@@ -84,7 +73,7 @@ static bool cpu_sched_is_cpuset_mounted()
        return ret;
 }
 
-static int cpu_sched_init_cgroup_set(const struct coreset *set)
+static int cpu_sched_init_cgroup_set(const struct cpuset_info *set)
 {
        int r;
        char buf[512];
@@ -117,7 +106,7 @@ static int cpu_sched_init_cgroup(struct cpu_sched *cs)
 {
        int r;
        GSList *i;
-       struct coreset *c;
+       struct cpuset_info *cpuset_info;
 
        if (cpu_sched_is_cpuset_mounted() == false) {
                r = cgroup_make_subdir(CGROUP_PATH, "cpuset", NULL);
@@ -134,10 +123,10 @@ static int cpu_sched_init_cgroup(struct cpu_sched *cs)
        }
 
        gslist_for_each_item(i, cs->apps) {
-               c = (struct coreset *)i->data;
-               r = cpu_sched_init_cgroup_set(c);
+               cpuset_info = (struct cpuset_info *)i->data;
+               r = cpu_sched_init_cgroup_set(cpuset_info);
                if (r < 0) {
-                       _E("cpu-set: error setting up cpuset (%s)", c->name);
+                       _E("cpu-set: error setting up cpuset (%s)", cpuset_info->name);
                        return r;
                }
        }
@@ -145,95 +134,32 @@ static int cpu_sched_init_cgroup(struct cpu_sched *cs)
        return cs->fg ? cpu_sched_init_cgroup_set(cs->fg) : 0;
 }
 
-/* create single core struct representation */
-static int cpu_sched_new_core(struct coreset *set, int core_id)
+/* free memory allocated in cpuset_info structure */
+static void cpu_sched_free_cpuset(struct cpuset_info **cpuset_info)
 {
-       struct core *c;
+       struct cpuset_info *set = *cpuset_info;
 
-       assert(set);
-       assert(core_id >= 0);
-
-       c = (struct core *)calloc(1, sizeof *c);
-       if (c == NULL) {
-               _E("cpu-sched: could not allocate memory for core struct");
-               return RESOURCED_ERROR_FAIL;
-       }
-       c->id = core_id;
-       c->on = false;
-
-       set->cores = g_slist_append(set->cores, c);
-       return RESOURCED_ERROR_NONE;
-}
-
-/* free memory allocated in coreset structure */
-static void cpu_sched_free_cpuset(struct coreset **c)
-{
-       struct coreset *set = *c;
-
-       if (set->cores != NULL)
-               g_slist_free_full(set->cores, free);
-       set->cores = NULL;
+       if (set->cpu_info != NULL)
+               g_slist_free_full(set->cpu_info, free);
+       set->cpu_info = NULL;
 
        free(set->name);
        set->name = NULL;
 
        free(set);
-       *c = NULL;
+       *cpuset_info = NULL;
 }
 
-/* free memory allocated in coreset structure and the structure */
+/* free memory allocated in cpuset_info structure and the structure */
 static void cpu_sched_free_cpuset_full(void *data)
 {
-       struct coreset *set = (struct coreset *)data;
+       struct cpuset_info *set = (struct cpuset_info *)data;
 
        assert(set);
 
        cpu_sched_free_cpuset(&set);
 }
 
-/* parse config for specific coreset line */
-static int cpu_sched_parse_cpuset(struct coreset *set, char *value)
-{
-       char *ptr, *saveptr;
-       int min, max;
-       int r;
-
-       assert(set);
-       assert(value);
-       assert(!set->cores);
-
-       ptr = strtok_r(value, ",", &saveptr);
-       while (ptr) {
-               if (strstr(ptr, "-") == NULL) { /* single value */
-                       r = sscanf(ptr, "%d", &min);
-                       if (r == 1 && min >= 0) {
-                               if (cpu_sched_new_core(set, min) != RESOURCED_ERROR_NONE)
-                                       goto parse_cpuset_fail;
-                       } else {
-                               _E("cpu-sched: error parsing cpuset (%s)", ptr);
-                               goto parse_cpuset_fail;
-                       }
-               } else { /* range */
-                       r = sscanf(ptr, "%d-%d", &min, &max);
-                       if (r == 2 && min >= 0 && max >= 0 && max > min) {
-                               for (int i = min; i <= max; i++) {
-                                       if (cpu_sched_new_core(set, i) != RESOURCED_ERROR_NONE)
-                                               goto parse_cpuset_fail;
-                               }
-                       } else {
-                               _E("cpu-sched: error parsing cpuset (%s)", ptr);
-                               goto parse_cpuset_fail;
-                       }
-
-               }
-
-               ptr = strtok_r(NULL, ",", &saveptr);
-       }
-       return RESOURCED_ERROR_NONE;
-parse_cpuset_fail:
-       cpu_sched_free_cpuset(&set);
-       return RESOURCED_ERROR_FAIL;
-}
 
 static int load_cpu_affinity_config(struct cpu_sched *data)
 {
@@ -248,22 +174,23 @@ static int load_cpu_affinity_config(struct cpu_sched *data)
        name = strdup(get_cpu_affinity_conf_name());
        is_fg = !strcmp(name, FOREGROUND_APPS_NAME_CONF);
 
-       struct coreset *c = (struct coreset *)calloc(1, sizeof *c);
-       if (c == NULL) {
+       struct cpuset_info *cpuset_info = (struct cpuset_info *)calloc(1, sizeof *cpuset_info);
+       if (cpuset_info == NULL) {
                free(name);
                return RESOURCED_ERROR_OUT_OF_MEMORY;
        }
 
-       c->name = name;
+       cpuset_info->name = name;
 
-       if (cpu_sched_parse_cpuset(c, get_cpu_affinity_conf_value()) < 0) {
+       if (cpu_sched_parse_cpuset(cpuset_info, get_cpu_affinity_conf_value()) < 0) {
+               cpu_sched_free_cpuset(&cpuset_info);
                return RESOURCED_ERROR_FAIL;
        }
 
        if (is_fg)
-               data->fg = c;
+               data->fg = cpuset_info;
        else
-               data->apps = g_slist_append(data->apps, c);
+               data->apps = g_slist_append(data->apps, cpuset_info);
 
        free_cpu_affinity_conf();
        return RESOURCED_ERROR_NONE;
@@ -322,10 +249,10 @@ static int cpu_sched_parse_config(struct cpu_sched *data)
        return RESOURCED_ERROR_NONE;
 }
 
-static int cpu_sched_write_coreset(struct coreset *set)
+static int cpu_sched_write_coreset(struct cpuset_info *set)
 {
        GSList *i;
-       struct core *c;
+       struct cpu_info *cpu_info;
        char path[128], coreset[128];
        int r;
 
@@ -338,12 +265,12 @@ static int cpu_sched_write_coreset(struct coreset *set)
        }
 
        r = 0;
-       gslist_for_each_item(i, set->cores) {
-               c = (struct core *)i->data;
-               if (c == NULL || c->on == false)
+       gslist_for_each_item(i, set->cpu_info) {
+               cpu_info = (struct cpu_info *)i->data;
+               if (cpu_info == NULL || cpu_info->online == false)
                        continue;
 
-               r += snprintf(coreset + r, sizeof coreset - r, "%d,", c->id);
+               r += snprintf(coreset + r, sizeof coreset - r, "%d,", cpu_info->cpu_id);
        }
 
        if (r > 0)
@@ -352,25 +279,25 @@ static int cpu_sched_write_coreset(struct coreset *set)
        return cgroup_write_node_str(path, "cpuset.cpus", coreset);
 }
 
-static int cpu_sched_cpu_on_for_coreset(struct coreset *set, int core_id)
+static int cpu_sched_cpu_on_for_coreset(struct cpuset_info *set, int core_id)
 {
        GSList *i;
-       struct core *c;
+       struct cpu_info *cpu_info;
        bool refresh = false;
 
        assert(set);
        assert(core_id >= 0);
 
-       if (set->cores == NULL)
+       if (set->cpu_info == NULL)
                return 0;
 
-       gslist_for_each_item(i, set->cores) {
-               c = (struct core *)i->data;
-               if (c == NULL || c->id != core_id)
+       gslist_for_each_item(i, set->cpu_info) {
+               cpu_info = (struct cpu_info *)i->data;
+               if (cpu_info == NULL || cpu_info->cpu_id != core_id)
                        continue;
 
-               _D("cpu-sched: core %d on for coreset %s", core_id, set->name);
-               c->on = true;
+               _D("cpu-sched: core %d on for cpuset_info %s", core_id, set->name);
+               cpu_info->online = true;
                refresh = true;
                break;
        }
@@ -381,25 +308,25 @@ static int cpu_sched_cpu_on_for_coreset(struct coreset *set, int core_id)
        return cpu_sched_write_coreset(set);
 }
 
-static int cpu_sched_cpu_off_for_coreset(struct coreset *set, int core_id)
+static int cpu_sched_cpu_off_for_coreset(struct cpuset_info *set, int core_id)
 {
        GSList *i;
-       struct core *c;
+       struct cpu_info *cpu_info;
        bool refresh = false;
 
        assert(set);
        assert(core_id >= 0);
 
-       if (set->cores == NULL)
+       if (set->cpu_info == NULL)
                return 0;
 
-       gslist_for_each_item(i, set->cores) {
-               c = (struct core *)i->data;
-               if (c == NULL || c->id != core_id)
+       gslist_for_each_item(i, set->cpu_info) {
+               cpu_info = (struct cpu_info *)i->data;
+               if (cpu_info == NULL || cpu_info->cpu_id != core_id)
                        continue;
 
-               _D("cpu-sched: core %d off for coreset %s", core_id, set->name);
-               c->on = false;
+               _D("cpu-sched: core %d off for cpuset_info %s", core_id, set->name);
+               cpu_info->online = false;
                refresh = true;
                break;
        }
@@ -421,7 +348,7 @@ static int cpu_sched_cpu_on(void *data)
 
        _D("cpu-sched: core %d plugged in", id);
        gslist_for_each_item(i, cs.apps) {
-               cpu_sched_cpu_on_for_coreset((struct coreset *)i->data, id);
+               cpu_sched_cpu_on_for_coreset((struct cpuset_info *)i->data, id);
        }
        if (cs.fg)
                cpu_sched_cpu_on_for_coreset(cs.fg, id);
@@ -440,7 +367,7 @@ static int cpu_sched_cpu_off(void *data)
 
        _D("cpu-sched: core %d plugged out", id);
        gslist_for_each_item(i, cs.apps) {
-               cpu_sched_cpu_off_for_coreset((struct coreset *)i->data, id);
+               cpu_sched_cpu_off_for_coreset((struct cpuset_info *)i->data, id);
        }
 
        if (cs.fg)
@@ -449,7 +376,7 @@ 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)
+static int cpu_sched_add_pid_to_cpuset(struct cpuset_info *set, pid_t pid)
 {
        assert(set);
 
@@ -460,7 +387,7 @@ static int cpu_sched_add_pid_to_cpuset(struct coreset *set, pid_t pid)
        return RESOURCED_ERROR_NONE;
 }
 
-static int cpu_sched_remove_pid_from_cpuset(struct coreset *set, pid_t pid)
+static int cpu_sched_remove_pid_from_cpuset(struct cpuset_info *set, pid_t pid)
 {
        assert(set);
 
@@ -471,18 +398,18 @@ static int cpu_sched_remove_pid_from_cpuset(struct coreset *set, pid_t pid)
        return RESOURCED_ERROR_NONE;
 }
 
-/* if app is subject to static coreset config its coreset should not be changed */
+/* if app is subject to static cpuset_info config its cpuset_info should not be changed */
 static bool cpu_sched_is_static_app(const char *appid)
 {
        assert (appid);
 
-       struct coreset *c = cpu_sched_find_coreset(appid);
-       if (!c)
+       struct cpuset_info *cpuset_info = cpu_sched_find_coreset(appid);
+       if (!cpuset_info)
                return false;
 
-       if (c->disclaimer_shown == false) {
+       if (cpuset_info->disclaimer_shown == false) {
                _D("cpu-sched: appid %s is statically configured - not subject to cpuset change", appid);
-               c->disclaimer_shown = true;
+               cpuset_info->disclaimer_shown = true;
        }
 
        return true;
@@ -523,18 +450,18 @@ static int cpu_sched_app_background(void *data)
 static int cpu_sched_app_launch(void *data)
 {
        struct proc_status *ps = (struct proc_status *)data;
-       struct coreset *c;
+       struct cpuset_info *cpuset_info;
 
        assert(ps);
        assert(ps->pai);
 
        _D("cpu-sched: app launch: %s", ps->pai->appid);
 
-       c = cpu_sched_find_coreset(ps->pai->appid);
-       if (c == NULL)
+       cpuset_info = cpu_sched_find_coreset(ps->pai->appid);
+       if (cpuset_info == NULL)
                return 0;
 
-       return cpu_sched_add_pid_to_cpuset(c, ps->pid);
+       return cpu_sched_add_pid_to_cpuset(cpuset_info, ps->pid);
 }
 
 /*static int cpu_sched_rt_scheduler(void *data)
@@ -631,7 +558,7 @@ static void cpu_sched_check_apps()
        _cleanup_app_list_close_ GSList *proc_app_list = PAL_INIT_VALUE;
        GSList *giter;
        struct proc_app_info *pai;
-       struct coreset *c;
+       struct cpuset_info *cpuset_info;
 
        proc_app_list = proc_app_list_open();
        gslist_for_each_item(giter, proc_app_list) {
@@ -639,9 +566,9 @@ static void cpu_sched_check_apps()
                if (!pai || !pai->main_pid)
                        continue;
 
-               c = cpu_sched_find_coreset(pai->appid);
-               if (c != NULL) {
-                       cpu_sched_add_pid_to_cpuset(c, pai->main_pid);
+               cpuset_info = cpu_sched_find_coreset(pai->appid);
+               if (cpuset_info != NULL) {
+                       cpu_sched_add_pid_to_cpuset(cpuset_info, pai->main_pid);
                        continue;
                }