sched-attr: Create a setter function for sched_attr 46/319346/4
authorUnsung Lee <unsung.lee@samsung.com>
Sat, 8 Feb 2025 10:50:04 +0000 (19:50 +0900)
committerUnsung Lee <unsung.lee@samsung.com>
Sun, 9 Feb 2025 13:21:50 +0000 (22:21 +0900)
Create a setter function called sched_attr_set_sched_attr to update sched_attr structure.
In addition, replace the existing code which updates sched_attr members to this setter.

Change-Id: I1ab23fad14f4579f459fdb47517d4c778450a553
Signed-off-by: Unsung Lee <unsung.lee@samsung.com>
src/common/sched-attr.c
src/common/sched-attr.h
src/process/proc-main.c
src/resource-limiter/cpu-throttling.c
src/resource-optimizer/cpu/cpu-boosting.c
src/resourced/init.c

index 0c9abc26faa7a7ed34a3c8caf358df3e17ac3250..f09bc946e638ad5eb495d01e41340a2109015f30 100644 (file)
@@ -149,6 +149,44 @@ static int sched_attr_set_scheduling_of_thread(pid_t pid, struct sched_attr sche
        return RESOURCED_ERROR_NONE;
 }
 
+int sched_attr_set_sched_attr(uint32_t sched_attr_set_flag,
+                struct sched_attr *sched_attr,
+                uint32_t size, uint32_t sched_policy, uint64_t sched_flags,
+                int32_t sched_nice, uint32_t sched_priority,
+                uint64_t sched_runtime, uint64_t sched_deadline, uint64_t sched_period)
+{
+        if (sched_attr == NULL) {
+                _E("Failed to set sched_attr structure due to null argument");
+                return RESOURCED_ERROR_INVALID_PARAMETER;
+        }
+
+        if (sched_attr_set_flag & SCHED_ATTR_SET_SCHED_ATTR_SIZE)
+                sched_attr->size = size;
+
+        if (sched_attr_set_flag & SCHED_ATTR_SET_SCHED_ATTR_SCHED_POLICY)
+                sched_attr->sched_policy = sched_policy;
+
+        if (sched_attr_set_flag & SCHED_ATTR_SET_SCHED_ATTR_SCHED_FLAGS)
+                sched_attr->sched_flags = sched_flags;
+
+        if (sched_attr_set_flag & SCHED_ATTR_SET_SCHED_ATTR_SCHED_NICE)
+                sched_attr->sched_nice = sched_nice;
+
+        if (sched_attr_set_flag & SCHED_ATTR_SET_SCHED_ATTR_SCHED_PRIORITY)
+                sched_attr->sched_priority = sched_priority;
+
+        if (sched_attr_set_flag & SCHED_ATTR_SET_SCHED_ATTR_SCHED_RUNTIME)
+                sched_attr->sched_runtime = sched_runtime;
+
+        if (sched_attr_set_flag & SCHED_ATTR_SET_SCHED_ATTR_SCHED_DEADLINE)
+                sched_attr->sched_deadline = sched_deadline;
+
+        if (sched_attr_set_flag & SCHED_ATTR_SET_SCHED_ATTR_SCHED_PERIOD)
+                sched_attr->sched_period = sched_period;
+
+        return RESOURCED_ERROR_NONE;
+}
+
 int sched_attr_set_scheduling(pid_t pid, bool is_process,
                struct sched_attr sched_attr, unsigned int flags)
 {
@@ -210,6 +248,7 @@ int sched_attr_get_scheduling(pid_t pid, struct sched_attr *sched_attr,
        int ret = 0;
        struct sched_param sp;
        int sched_policy;
+       uint32_t sched_attr_set_flag;
 
        if (is_sched_getattr_supported())
                return syscall(SYS_sched_getattr, pid, sched_attr, sizeof(struct sched_attr), flags);
@@ -220,16 +259,18 @@ int sched_attr_get_scheduling(pid_t pid, struct sched_attr *sched_attr,
                return RESOURCED_ERROR_FAIL;
        }
 
-       sched_attr->sched_policy = sched_policy;
-       sched_attr->sched_nice = getpriority(PRIO_PROCESS, pid);
-
        ret = sched_getparam(pid, &sp);
        if (ret < 0) {
                _E("Failed to call sched_sched_getparam by %m");
                return RESOURCED_ERROR_FAIL;
        }
 
-       sched_attr->sched_priority = sp.sched_priority;
+       sched_attr_set_flag = SCHED_ATTR_SET_SCHED_ATTR_SCHED_POLICY |
+               SCHED_ATTR_SET_SCHED_ATTR_SCHED_NICE |
+               SCHED_ATTR_SET_SCHED_ATTR_SCHED_PRIORITY;
+       sched_attr_set_sched_attr(sched_attr_set_flag, sched_attr,
+                       0, sched_policy, 0, getpriority(PRIO_PROCESS, pid),
+                       sp.sched_priority, 0, 0, 0);
 
        return RESOURCED_ERROR_NONE;
 }
index eb44c417dea35df3ee3c5c69138fcc4969e43c20..494df84e242c7d605a1ff9a59085139e7166679c 100644 (file)
 extern "C" {
 #endif /* __cplusplus */
 
+#define SCHED_ATTR_SET_SCHED_ATTR_SIZE                  (1 << 0)
+#define SCHED_ATTR_SET_SCHED_ATTR_SCHED_POLICY          (1 << 1)
+#define SCHED_ATTR_SET_SCHED_ATTR_SCHED_FLAGS           (1 << 2)
+#define SCHED_ATTR_SET_SCHED_ATTR_SCHED_NICE            (1 << 3)
+#define SCHED_ATTR_SET_SCHED_ATTR_SCHED_PRIORITY        (1 << 4)
+#define SCHED_ATTR_SET_SCHED_ATTR_SCHED_RUNTIME         (1 << 5)
+#define SCHED_ATTR_SET_SCHED_ATTR_SCHED_DEADLINE        (1 << 6)
+#define SCHED_ATTR_SET_SCHED_ATTR_SCHED_PERIOD          (1 << 7)
+
 struct sched_attr {
        uint32_t size;                  /* Size of this structure */
        uint32_t sched_policy;          /* Policy (SCHED_*) */
@@ -46,6 +55,11 @@ struct sched_attr {
        uint64_t sched_period;
 };
 
+int sched_attr_set_sched_attr(uint32_t sched_attr_set_flag,
+                struct sched_attr *sched_attr,
+                uint32_t size, uint32_t sched_policy, uint64_t sched_flags,
+                int32_t sched_nice, uint32_t sched_priority,
+                uint64_t sched_runtime, uint64_t sched_deadline, uint64_t sched_period);
 int sched_attr_set_scheduling(pid_t pid, bool is_process,
                struct sched_attr sched_attr, unsigned int flags);
 int sched_attr_get_scheduling(pid_t pid, struct sched_attr *sched_attr,
index dd73f2933b3fc945a7a8d72a4375c5c203223a60..b68cb2e90165b40c6b564ad625d7f7e63a614b83 100644 (file)
@@ -869,6 +869,10 @@ __attribute__((weak)) struct proc_app_info *proc_create_app_info(const char *app
        struct proc_app_info *pai;
        unsigned long uptime = 0;
        int ret;
+       uint32_t sched_attr_set_flag;
+       uint32_t sched_policy = SCHED_OTHER;
+       int32_t sched_nice = 0;
+       uint32_t sched_priority = 0;
 
        if (!appid || !pkgid) {
                _E("[PROCESS] Each app should have appid and pkgid");
@@ -962,51 +966,59 @@ __attribute__((weak)) struct proc_app_info *proc_create_app_info(const char *app
 
                        if (pci) {
                                memset(&attr, 0, sizeof(struct sched_attr));
-                               attr.size = sizeof(struct sched_attr);
                                if (pci->cpu_sched_info.cpu_nice >= CPU_MIN_NICE &&
                                        pci->cpu_sched_info.cpu_nice <= CPU_MAX_NICE) {
-                                       attr.sched_nice = pci->cpu_sched_info.cpu_nice;
+                                       sched_nice = pci->cpu_sched_info.cpu_nice;
                                        pai->app_cpu_nice_update_exclude = true;
                                }
                                else {
-                                       attr.sched_nice = CPU_INIT_NICE;
+                                       sched_nice = CPU_INIT_NICE;
                                }
 
                                if (pci->cpu_sched_info.cpu_rt_priority >= CPU_MIN_PRIO &&
                                                pci->cpu_sched_info.cpu_rt_priority <= CPU_MAX_PRIO) {
-                                       attr.sched_priority = pci->cpu_sched_info.cpu_rt_priority;
+                                       sched_priority = pci->cpu_sched_info.cpu_rt_priority;
                                }
                                else {
-                                       attr.sched_priority = CPU_INIT_PRIO;
+                                       sched_priority = CPU_INIT_PRIO;
                                }
 
                                switch (pci->cpu_sched_info.cpu_sched_type) {
                                case CPU_SCHED_NONE:
-                                       if (attr.sched_nice == CPU_INIT_NICE)
+                                       if (sched_nice == CPU_INIT_NICE)
                                                goto skip_scheduler_update;
-                                       attr.sched_policy = SCHED_OTHER;
+                                       sched_policy = SCHED_OTHER;
                                        break;
                                case CPU_SCHED_OTHER:
-                                       attr.sched_policy = SCHED_OTHER;
+                                       sched_policy = SCHED_OTHER;
                                        break;
                                case CPU_SCHED_IDLE:
-                                       attr.sched_policy = SCHED_IDLE;
+                                       sched_policy = SCHED_IDLE;
                                        break;
                                case CPU_SCHED_BATCH:
-                                       attr.sched_policy = SCHED_BATCH;
+                                       sched_policy = SCHED_BATCH;
                                        break;
                                case CPU_SCHED_FIFO:
-                                       attr.sched_policy = SCHED_FIFO;
+                                       sched_policy = SCHED_FIFO;
                                        break;
                                case CPU_SCHED_RR:
-                                       attr.sched_policy = SCHED_RR;
+                                       sched_policy = SCHED_RR;
                                        break;
                                case CPU_SCHED_DEADLINE:
-                                       attr.sched_policy = SCHED_DEADLINE;
+                                       sched_policy = SCHED_DEADLINE;
                                        break;
                                default:
                                        _E("[PROCESS] Unknown CPU sched type");
                                }
+
+                               sched_attr_set_flag = SCHED_ATTR_SET_SCHED_ATTR_SIZE |
+                                       SCHED_ATTR_SET_SCHED_ATTR_SCHED_POLICY |
+                                       SCHED_ATTR_SET_SCHED_ATTR_SCHED_NICE |
+                                       SCHED_ATTR_SET_SCHED_ATTR_SCHED_PRIORITY;
+                               sched_attr_set_sched_attr(sched_attr_set_flag, &attr,
+                                               sizeof(struct sched_attr), sched_policy, 0,
+                                               sched_nice, sched_priority, 0, 0, 0);
+
                                error = sched_attr_set_scheduling(pid, true, attr, 0);
                                if (error)
                                        _E("[PROCESS] Failed to set sched attributes");
index f9f32cf93462216ec66e18ce8a865caf926b629f..204b4db7c694abca8eb55632031168197d099c76 100644 (file)
@@ -163,6 +163,8 @@ static void load_cpu_throttling_config(void)
        unsigned long long cpu_period_us;
        unsigned long long cpu_share;
        enum cpu_sched_type cpu_sched_type;
+       uint32_t sched_attr_set_flag;
+       uint32_t sched_policy = SCHED_OTHER;
 
        struct cpu_throttling_conf *cpu_throttling_conf = get_cpu_throttling_conf();
        if (cpu_throttling_conf == NULL) {
@@ -223,22 +225,24 @@ static void load_cpu_throttling_config(void)
 
        /* Initialize type and nice of cpu throttling scheduler */
        memset(&normal_attr, 0, sizeof(struct sched_attr));
-       normal_attr.size = sizeof(struct sched_attr);
-       normal_attr.sched_nice = CPU_DEFAULT_NICE;
-       normal_attr.sched_policy = SCHED_OTHER;
+
+       sched_attr_set_flag = SCHED_ATTR_SET_SCHED_ATTR_SIZE |
+                       SCHED_ATTR_SET_SCHED_ATTR_SCHED_POLICY |
+                       SCHED_ATTR_SET_SCHED_ATTR_SCHED_NICE;
+       sched_attr_set_sched_attr(sched_attr_set_flag, &normal_attr,
+                       sizeof(struct sched_attr), SCHED_OTHER, 0,
+                       CPU_DEFAULT_NICE, 0, 0, 0, 0);
 
        memset(&throttling_attr, 0, sizeof(struct sched_attr));
-       throttling_attr.size = sizeof(struct sched_attr);
-       throttling_attr.sched_nice = cpu_nice;
        switch (cpu_sched_type) {
                case CPU_SCHED_IDLE:
-                       throttling_attr.sched_policy = SCHED_IDLE;
+                       sched_policy = SCHED_IDLE;
                        break;
                case CPU_SCHED_BATCH:
-                       throttling_attr.sched_policy = SCHED_BATCH;
+                       sched_policy = SCHED_BATCH;
                        break;
                case CPU_SCHED_OTHER:
-                       throttling_attr.sched_policy = SCHED_OTHER;
+                       sched_policy = SCHED_OTHER;
                        break;
                default:
                        if (!skip_share || !skip_bandwidth)
@@ -246,6 +250,13 @@ static void load_cpu_throttling_config(void)
                        goto free_cpu_throttling_conf;
        }
 
+       sched_attr_set_flag = SCHED_ATTR_SET_SCHED_ATTR_SIZE |
+                       SCHED_ATTR_SET_SCHED_ATTR_SCHED_POLICY |
+                       SCHED_ATTR_SET_SCHED_ATTR_SCHED_NICE;
+       sched_attr_set_sched_attr(sched_attr_set_flag, &throttling_attr,
+                       sizeof(struct sched_attr), sched_policy, 0,
+                       cpu_nice, 0, 0, 0, 0);
+
        throttling_success = true;
 
        _I("[CPU-THROTTLING] throttling policy = %s",
index 6f5218a68856b1dfa432742ae001342b0975055e..99ac94e6174cd89870df49d0a35625b39d14a5b2 100644 (file)
@@ -455,8 +455,6 @@ void cpu_boosting_wakeup_cpu_boosting_thread(void)
 
 static cpu_boosting_level_e cpu_boosting_level_search(struct sched_attr attr)
 {
-       attr.sched_policy = attr.sched_policy & ~SCHED_RESET_ON_FORK;
-
        for (int level = CPU_BOOSTING_LEVEL_STRONG; level < CPU_BOOSTING_LEVEL_END; level++) {
                if (cpu_boosting_attr[level].sched_policy == SCHED_RR ||
                        cpu_boosting_attr[level].sched_policy == SCHED_FIFO) {
@@ -569,6 +567,7 @@ static bool load_cpu_boosting_config(cpu_boosting_level_e level)
        int cpu_nice, cpu_rt_priority, cpu_policy;
 
        enum cpu_sched_type cpu_sched_type;
+       uint32_t sched_attr_set_flag;
 
        struct cpu_boosting_conf *cpu_boosting_conf = get_cpu_boosting_conf(level);
        if (cpu_boosting_conf == NULL) {
@@ -608,8 +607,6 @@ static bool load_cpu_boosting_config(cpu_boosting_level_e level)
                        cpu_policy = SCHED_BATCH;
                else
                        cpu_policy = SCHED_OTHER;
-
-               cpu_boosting_attr[level].sched_nice = cpu_nice;
        }
        else {
                if (cpu_rt_priority < CPU_MIN_PRIO || cpu_rt_priority > CPU_MAX_PRIO) {
@@ -621,11 +618,17 @@ static bool load_cpu_boosting_config(cpu_boosting_level_e level)
                        cpu_policy = SCHED_RR;
                else
                        cpu_policy = SCHED_FIFO;
+
+               cpu_nice = 0;
        }
 
-       cpu_boosting_attr[level].size = sizeof(struct sched_attr);
-       cpu_boosting_attr[level].sched_policy = cpu_policy;
-       cpu_boosting_attr[level].sched_priority = cpu_rt_priority;
+       sched_attr_set_flag = SCHED_ATTR_SET_SCHED_ATTR_SIZE |
+                       SCHED_ATTR_SET_SCHED_ATTR_SCHED_POLICY |
+                       SCHED_ATTR_SET_SCHED_ATTR_SCHED_NICE |
+                       SCHED_ATTR_SET_SCHED_ATTR_SCHED_PRIORITY;
+       sched_attr_set_sched_attr(sched_attr_set_flag, &cpu_boosting_attr[level],
+                       sizeof(struct sched_attr), cpu_policy, 0,
+                       cpu_nice, cpu_rt_priority, 0, 0, 0);
 
        _I("[CPU-BOOSTING] Boosting (level = %d) policy = %s", level,
                cpu_boosting_attr[level].sched_policy == SCHED_BATCH ? "SCHED_BATCH" :
@@ -640,12 +643,18 @@ static bool load_cpu_boosting_config(cpu_boosting_level_e level)
 static void load_cpu_boosting_configs(void)
 {
        int level = CPU_BOOSTING_LEVEL_NONE;
+       uint32_t sched_attr_set_flag;
 
        /* Initialize type and nice of cpu throttling scheduler */
        memset(&cpu_boosting_attr[level], 0, sizeof(struct sched_attr));
-       cpu_boosting_attr[level].size = sizeof(struct sched_attr);
-       cpu_boosting_attr[level].sched_nice = CPU_DEFAULT_NICE;
-       cpu_boosting_attr[level].sched_policy = SCHED_OTHER;
+
+       sched_attr_set_flag = SCHED_ATTR_SET_SCHED_ATTR_SIZE |
+                       SCHED_ATTR_SET_SCHED_ATTR_SCHED_POLICY |
+                       SCHED_ATTR_SET_SCHED_ATTR_SCHED_NICE;
+       sched_attr_set_sched_attr(sched_attr_set_flag, &cpu_boosting_attr[level],
+                       sizeof(struct sched_attr), SCHED_OTHER, 0,
+                       CPU_DEFAULT_NICE, 0, 0, 0, 0);
+
        cpu_boosting_success[level] = true;
 
        for (level = CPU_BOOSTING_LEVEL_STRONG; level < CPU_BOOSTING_LEVEL_END; level++) {
@@ -1066,10 +1075,14 @@ static int update_cpu_sched_of_thread_list(int tid_count, int *tid_list,
        struct sched_attr attr = cpu_boosting_attr[cpu_boosting_level];
        int fail_cnt = 0;
        int success_cnt = 0;
+       uint32_t sched_attr_set_flag;
 
        if (cpu_boosting_flags & CPU_BOOSTING_RESET_ON_FORK) {
                _I("[CPU-BOOSTING] Turn on SCHED_RESET_ON_FORK flag");
-               attr.sched_policy |= SCHED_RESET_ON_FORK;
+
+               sched_attr_set_flag = SCHED_ATTR_SET_SCHED_ATTR_SCHED_POLICY;
+               sched_attr_set_sched_attr(sched_attr_set_flag, &attr,
+                               0, attr.sched_policy | SCHED_RESET_ON_FORK, 0, 0, 0, 0, 0, 0);
        }
 
        for (int i = 0; i < tid_count; i++) {
index ea1969fa9bcd5b116bbfc1eaa83a512804d4243b..45f1810258efbdbae8db3918c779081a595da2d0 100644 (file)
@@ -231,6 +231,10 @@ int fixed_list_init(enum proc_type proc_type)
        gpointer value;
        GHashTableIter iter;
        struct sched_attr attr;
+       uint32_t sched_attr_set_flag;
+       uint32_t sched_policy = SCHED_OTHER;
+       int32_t sched_nice = 0;
+       uint32_t sched_priority = 0;
 
        if (proc_type == SERVICE_TYPE)
                g_hash_table_iter_init(&iter, fixed_service_list_get());
@@ -281,48 +285,56 @@ int fixed_list_init(enum proc_type proc_type)
                }
 
                memset(&attr, 0, sizeof(struct sched_attr));
-               attr.size = sizeof(struct sched_attr);
                if (pci->cpu_sched_info.cpu_nice >= CPU_MIN_NICE &&
                        pci->cpu_sched_info.cpu_nice <= CPU_MAX_NICE) {
-                       attr.sched_nice = pci->cpu_sched_info.cpu_nice;
+                       sched_nice = pci->cpu_sched_info.cpu_nice;
                }
                else
-                       attr.sched_nice = CPU_INIT_NICE;
+                       sched_nice = CPU_INIT_NICE;
 
                if (pci->cpu_sched_info.cpu_rt_priority >= CPU_MIN_PRIO &&
                    pci->cpu_sched_info.cpu_rt_priority <= CPU_MAX_PRIO) {
-                       attr.sched_priority = pci->cpu_sched_info.cpu_rt_priority;
+                       sched_priority = pci->cpu_sched_info.cpu_rt_priority;
                }
                else
-                       attr.sched_priority = CPU_INIT_PRIO;
+                       sched_priority = CPU_INIT_PRIO;
 
                switch (pci->cpu_sched_info.cpu_sched_type) {
                case CPU_SCHED_NONE:
-                       if (attr.sched_nice == CPU_INIT_NICE)
+                       if (sched_nice == CPU_INIT_NICE)
                                goto skip_scheduler_update;
-                       attr.sched_policy = SCHED_OTHER;
+                       sched_policy = SCHED_OTHER;
                        break;
                case CPU_SCHED_OTHER:
-                       attr.sched_policy = SCHED_OTHER;
+                       sched_policy = SCHED_OTHER;
                        break;
                case CPU_SCHED_IDLE:
-                       attr.sched_policy = SCHED_IDLE;
+                       sched_policy = SCHED_IDLE;
                        break;
                case CPU_SCHED_BATCH:
-                       attr.sched_policy = SCHED_BATCH;
+                       sched_policy = SCHED_BATCH;
                        break;
                case CPU_SCHED_FIFO:
-                       attr.sched_policy = SCHED_FIFO;
+                       sched_policy = SCHED_FIFO;
                        break;
                case CPU_SCHED_RR:
-                       attr.sched_policy = SCHED_RR;
+                       sched_policy = SCHED_RR;
                        break;
                case CPU_SCHED_DEADLINE:
-                       attr.sched_policy = SCHED_DEADLINE;
+                       sched_policy = SCHED_DEADLINE;
                        break;
                default:
                        _E("[CPU-SCHED] Unknown CPU sched type");
                }
+
+               sched_attr_set_flag = SCHED_ATTR_SET_SCHED_ATTR_SIZE |
+                       SCHED_ATTR_SET_SCHED_ATTR_SCHED_POLICY |
+                       SCHED_ATTR_SET_SCHED_ATTR_SCHED_NICE |
+                       SCHED_ATTR_SET_SCHED_ATTR_SCHED_PRIORITY;
+               sched_attr_set_sched_attr(sched_attr_set_flag, &attr,
+                               sizeof(struct sched_attr), sched_policy, 0,
+                               sched_nice, sched_priority, 0, 0, 0);
+
                error = sched_attr_set_scheduling(pid, true, attr, 0);
                if (error)
                        _E("[CPU-SCHED] Failed to set sched attributes");