Do not make backups for original policy, privilege 71/278571/2
authorUnsung Lee <unsung.lee@samsung.com>
Fri, 22 Jul 2022 06:01:34 +0000 (15:01 +0900)
committerUnsung Lee <unsung.lee@samsung.com>
Fri, 22 Jul 2022 07:47:29 +0000 (16:47 +0900)
Change-Id: Iba447a08455780d898718b709b8fb9dffba2a693
Signed-off-by: Unsung Lee <unsung.lee@samsung.com>
src/common/cpu-common.h
src/resource-optimizer/cpu/cpu-boosting.c

index f4518ff..3325300 100644 (file)
@@ -44,7 +44,6 @@ enum cpu_sched_type {
 struct cpu_boosting_info {
        pid_t tid;
        int level;                     /* current boosting level */
-       struct sched_attr origin_attr; /* cpu attribute before the first boosting */
        guint gsource_id;              /* timer id */
 };
 
index 2fdf3bf..2646b26 100644 (file)
@@ -237,7 +237,7 @@ static void cpu_boosting_send_reply(int sock, cpu_boosting_output_t output)
                return;
        }
 
-       if (output.level.tid_level && output.level.tid_count > 0) {
+       if (output.success) {
                /* Send a header first */
                byte = send(sock, (const void *)&output, sizeof(output), 0);
                if (byte != sizeof(output)) {
@@ -255,8 +255,6 @@ static void cpu_boosting_send_reply(int sock, cpu_boosting_output_t output)
                        return;
                }
 
-               if (output.level.tid_level)
-                       free(output.level.tid_level);
        }
        else {
                /* Send a header */
@@ -267,6 +265,9 @@ static void cpu_boosting_send_reply(int sock, cpu_boosting_output_t output)
                        return;
                }
        }
+
+       if (output.level.tid_level)
+               free(output.level.tid_level);
 }
 
 static int cpu_boosting_enqueue_by_socket(int sock, struct cpu_boosting_input **input_p)
@@ -434,13 +435,11 @@ static gboolean cpu_boosting_timeout(gpointer data)
                         * info is NULL when clearing boosting before timeout
                         * info->gsource_id != *(input->gsource_id) when setting boosting again before timeout
                         */
-                       if (info == NULL || info->gsource_id != *(input->gsource_id)) {
+                       if (info == NULL || info->gsource_id != *(input->gsource_id))
                                continue;
-                       }
 
-                       ret = sched_setattr(tid_list[i], &info->origin_attr, 0);
                        g_hash_table_remove(tid_table, &tid_list[i]);
-
+                       ret = sched_setattr(tid_list[i], &cpu_boosting_attr[CPU_BOOSTING_LEVEL_NONE], 0);
                        if (ret != RESOURCED_ERROR_NONE)
                                _E("[CPU-BOOSTING] Failed to clear boost cpu of (tid = %d)", tid_list[i]);
                }
@@ -454,40 +453,21 @@ timer_out:
        return G_SOURCE_REMOVE;
 }
 
-static struct cpu_boosting_info *cpu_boosting_find_and_insert_attr(pid_t tid, cpu_boosting_level_e level)
+static void cpu_boosting_find_and_insert_attr(pid_t tid, cpu_boosting_level_e level, guint id)
 {
-       int ret;
-
        struct cpu_boosting_info *info = g_hash_table_lookup(tid_table, &tid);
        if (info == NULL) {
                info = (struct cpu_boosting_info *)calloc(1, sizeof (struct cpu_boosting_info));
                if (info == NULL) {
                        _E("[CPU-BOOSTING] Failed to allocate memory");
-                       return NULL;
+                       return;
                }
 
-               ret = sched_getattr(tid, &info->origin_attr, 0);
-               if (ret != RESOURCED_ERROR_NONE) {
-                       free(info);
-                       return NULL;
-               }
                info->tid = tid;
                g_hash_table_insert(tid_table, &info->tid, info);
        }
 
        info->level = level;
-       info->gsource_id = 0;
-       return info;
-}
-
-static void cpu_boosting_set_gsource_id(pid_t tid, guint id)
-{
-       struct cpu_boosting_info *info = g_hash_table_lookup(tid_table, &tid);
-       if (info == NULL) {
-               _E("[CPU-BOOSTING] cpu boosting info cannot be NULL");
-               return;
-       }
-
        info->gsource_id = id;
 }
 
@@ -496,7 +476,6 @@ static void cpu_boosting_set(struct cpu_boosting_input *input)
        int fail_cnt = 0;
        int success_cnt = 0;
        struct sched_attr attr;
-       struct cpu_boosting_info *info;
        int tid_count = input->client_input.pid.tid_count;
        int *tid_list = input->client_input.pid.tid;
        int timeout_msec = input->client_input.timeout_msec;
@@ -510,14 +489,7 @@ static void cpu_boosting_set(struct cpu_boosting_input *input)
 
        for (int i = 0; i < tid_count; i++) {
                if (tid_list[i] > 0) {
-                       if ((info = cpu_boosting_find_and_insert_attr(tid_list[i], cpu_boosting_level)) == NULL) {
-                               fail_cnt++;
-                               continue;
-                       }
-
                        if (sched_setattr(tid_list[i], &attr, 0) < 0) {
-                               g_hash_table_remove(tid_table, &tid_list[i]);
-
                                _E("[CPU-BOOSTING] Failed to boost cpu of (tid = %d) with (level = %d)",
                                                tid_list[i], cpu_boosting_level);
                                fail_cnt++;
@@ -532,26 +504,31 @@ static void cpu_boosting_set(struct cpu_boosting_input *input)
        if (fail_cnt > 0)
                _E("[CPU-BOOSTING] Boosting success ratio = %d/%d", success_cnt, fail_cnt + success_cnt);
 
-       /*
-        * If timeout value is larger than 0 and at least one boosting succeed,
-        * then set the timer to restore
-        */
-       if (timeout_msec > 0 && success_cnt > 0) {
-               GSource *source;
-               input->gsource_id = g_new(guint, 1);
-
-               source = g_timeout_source_new(timeout_msec);
-               g_source_set_callback(source, cpu_boosting_timeout, input, NULL);
-               *(input->gsource_id) = g_source_attach(source, cpu_boosting_context);
-               g_source_unref(source);
+       if (success_cnt > 0) {
+               guint id;
+               /*
+                * If timeout value is larger than 0 and at least one boosting succeed,
+                * then set the timer to restore
+                */
+               if (timeout_msec > 0) {
+                       GSource *source;
+                       input->gsource_id = g_new(guint, 1);
+
+                       source = g_timeout_source_new(timeout_msec);
+                       g_source_set_callback(source, cpu_boosting_timeout, input, NULL);
+                       *(input->gsource_id) = g_source_attach(source, cpu_boosting_context);
+                       g_source_unref(source);
+
+                       id = *(input->gsource_id);
+               }
+               else
+                       id = 0; /* No timer source */
 
                for (int i = 0; i < tid_count; i++) {
                        if (tid_list[i] > 0)
-                               cpu_boosting_set_gsource_id(tid_list[i], *(input->gsource_id));
+                               cpu_boosting_find_and_insert_attr(tid_list[i], cpu_boosting_level, id);
                }
        }
-       else
-               cpu_boosting_destroy_request(input);
 }
 
 static void cpu_boosting_clear(struct cpu_boosting_input *input)
@@ -564,38 +541,14 @@ static void cpu_boosting_clear(struct cpu_boosting_input *input)
 
        for (int i = 0; i < tid_count; i++) {
                if (tid_list[i] > 0) {
-                       struct cpu_boosting_info *info = g_hash_table_lookup(tid_table, &tid_list[i]);
-                       if (info == NULL) {
-                               struct sched_attr attr;
-                               if (sched_getattr(tid_list[i], &attr, 0) < 0) {
-                                       _E("[CPU-BOOSTING] Failed to get boost cpu of (tid = %d)", tid_list[i]);
-                                       fail_cnt++;
-                                       continue;
-                               }
+                       g_hash_table_remove(tid_table, &tid_list[i]);
 
-                               cpu_boosting_level_e level = cpu_boosting_level_search(attr);
-                               if (level != CPU_BOOSTING_LEVEL_NONE) {
-                                       ret = sched_setattr(tid_list[i],
-                                                       &cpu_boosting_attr[CPU_BOOSTING_LEVEL_NONE], 0);
-                                       if (ret != RESOURCED_ERROR_NONE) {
-                                               _E("[CPU-BOOSTING] Failed to clear boost cpu of (tid = %d)", tid_list[i]);
-                                               fail_cnt++;
-                                               continue;
-                                       }
-                               }
-                               else {
-                                       _W("[CPU-BOOSTING] Try to clear non-boosted process (policy = %d, priority = %d, nice = %d", attr.sched_policy, attr.sched_priority, attr.sched_nice);
-                                       continue;
-                               }
-                       }
-                       else {
-                               ret = sched_setattr(tid_list[i], &info->origin_attr, 0);
-                               g_hash_table_remove(tid_table, &tid_list[i]);
-                               if (ret != RESOURCED_ERROR_NONE) {
-                                       _E("[CPU-BOOSTING] Failed to clear boost cpu of (tid = %d)", tid_list[i]);
-                                       fail_cnt++;
-                                       continue;
-                               }
+                       ret = sched_setattr(tid_list[i],
+                                       &cpu_boosting_attr[CPU_BOOSTING_LEVEL_NONE], 0);
+                       if (ret != RESOURCED_ERROR_NONE) {
+                               _E("[CPU-BOOSTING] Failed to clear boost cpu of (tid = %d)", tid_list[i]);
+                               fail_cnt++;
+                               continue;
                        }
 
                        success_cnt++;
@@ -647,17 +600,14 @@ static void cpu_boosting_get(struct cpu_boosting_input *input)
                }
        }
        output.level.tid_count = success_cnt;
-       if (success_cnt == 0)
-               free(output.level.tid_level);
 
 output_update:
        if (fail_cnt > 0) {
                _E("[CPU-BOOSTING] Get boosting success ratio = %d/%d", success_cnt, fail_cnt + success_cnt);
                output.success = false;
        }
-       else {
+       else
                output.success = true;
-       }
 
        cpu_boosting_send_reply(input->sock, output);
        cpu_boosting_destroy_request(input);