proc-oom-priority: Set low oom_score_adj for 'ForegroundAppList' 47/295747/10
authorUnsung Lee <unsung.lee@samsung.com>
Wed, 12 Jul 2023 05:57:04 +0000 (14:57 +0900)
committerUnsung Lee <unsung.lee@samsung.com>
Mon, 31 Jul 2023 02:35:06 +0000 (11:35 +0900)
Set low oom_score_adj of foreground app if the corresponding configuration sets.

oom_score_adj is an important indicator to choose victim
when available memory is too low.
Therefore, if specific foreground app(s) have low fixed oom_score_adj
compared to other apps, then they will be selected for victim with relatively
low probability.

Change-Id: Ic2752fb658da05d9d92a167e5a0f4d1dae411994
Signed-off-by: Unsung Lee <unsung.lee@samsung.com>
src/common/proc-common.h
src/process/proc-oom-priority.c

index 8880c76..833e2eb 100644 (file)
@@ -203,6 +203,7 @@ struct proc_memory_state {
        struct memcg_info *memcg_info;
        int memcg_idx;
        int oom_score_adj;
+       int foreground_oom_score_adj;
        bool oom_killed;
        bool use_mem_limit;
        bool memlimit_update_exclude;
index 1391d8a..20ac0a6 100644 (file)
 
 static GHashTable *oom_fixed_pid_list;
 
+#define is_fixed_oom_score(oom_score)  \
+       (oom_score <= FIXED_OOM_MAX && oom_score >= FIXED_OOM_MIN)
+
+static bool is_wrong_oom_score(int oom_score, const char *name)
+{
+       /**
+        * FIXME: Currently, resourced does not allow predefined oom_score_adj
+        * to be used as fixed oom_score_adj. This is because, resourced judges
+        * the current status of app using oom_score_adj of app (/proc/<pid>/oom_score_adj).
+        * However, it reduces resourced user experience considerably.
+        */
+       switch (oom_score) {
+       case OOMADJ_SERVICE_MIN:
+       case OOMADJ_SU:
+       case OOMADJ_INIT:
+       case OOMADJ_FOREGRD_LOCKED:
+       case OOMADJ_FOREGRD_UNLOCKED:
+       case OOMADJ_BACKGRD_PERCEPTIBLE:
+       case OOMADJ_BACKGRD_LOCKED:
+               _W("Do not set the fixed oom for %s with predefined value %d",
+                               name, oom_score);
+               return true;
+       default:
+               if (oom_score < FIXED_OOM_MIN || oom_score > FIXED_OOM_MAX) {
+                       _W("Do not set the fixed oom for %s with the out of range %d",
+                                       name, oom_score);
+                       return true;
+               }
+
+               return false;
+       }
+}
+
 static int proc_oom_priority_set_fixed_oom(void *data)
 {
-       int ret;
-       int *fixed_oom_score = NULL;
-       gint *pid, *oom_score;
        struct proc_status *ps = (struct proc_status *)data;
+       gint *oom_score;
+       gint *pid;
+       int ret;
 
        if (!ps)
                return RESOURCED_ERROR_INVALID_PARAMETER;
 
        struct proc_conf_info *pci = fixed_app_and_service_exist_check(ps->pai->appid, APP_TYPE);
-       /* This app is not interested in the fixed oom configuration */
-       if (!pci || pci->oom_score > OOMADJ_APP_MAX)
+       if (!pci)
                return RESOURCED_ERROR_NONE;
 
-       /* Make another hashtable for fast searching during proc_set_oom_score_adj */
-       fixed_oom_score = &pci->oom_score;
-       ret = proc_set_oom_score_adj(ps->pid, *fixed_oom_score, ps->pai);
+       if (!is_fixed_oom_score(pci->oom_score) && !(pci->is_app_in_foreground_app_list))
+               return RESOURCED_ERROR_NONE;
+
+       if (is_fixed_oom_score(pci->oom_score) && pci->is_app_in_foreground_app_list) {
+               _W("load_fixed_oom_config() should ignore OomScore before");
+               pci->oom_score = OOMADJ_APP_MAX + 10;
+       }
+
+       if (pci->is_app_in_foreground_app_list) {
+               /**
+                * TODO: Update /proc/<pid>/oom_score_adj with fixed-oom score
+                * when app status is changed to the foreground.
+                * Currently, resourced changes foreground app's oom_score_adj
+                * in the 'proc_app_info', so /proc/<pid>/oom_score_adj is not changed
+                * regardless of the configuration.
+                * This is because resourced judges the current app status by reading
+                * /proc/<pid>/oom_score_adj when process status is changed. Therefore,
+                * if /proc/<pid>/oom_score_adj is chagned with fixed oom_score_adj then,
+                * the resourced will misunderstand the process current status.
+                */
+               ps->pai->memory.foreground_oom_score_adj = OOMADJ_SERVICE_MIN;
+               return RESOURCED_ERROR_NONE;
+       }
+
+       ret = proc_set_oom_score_adj(ps->pid, pci->oom_score, ps->pai);
        if (ret != RESOURCED_ERROR_NONE) {
                _E("Failed to set the fixed oom for %s", ps->pai->appid);
                return ret;
        }
 
-       _D("Set the fixed oom of %s with %d", ps->pai->appid, *fixed_oom_score);
+       _D("Set the fixed oom of %s with %d", ps->pai->appid, pci->oom_score);
+
        pid = g_new(gint, 1);
-       oom_score = g_new(gint, 1);
        *pid = ps->pid;
-       *oom_score = *fixed_oom_score;
+       oom_score = g_new(gint, 1);
+       *oom_score = pci->oom_score;
        g_hash_table_insert(oom_fixed_pid_list, (gpointer)pid, (gpointer)oom_score);
 
        return RESOURCED_ERROR_NONE;
@@ -87,7 +142,6 @@ int proc_oom_priority_is_oom_fixed_process(int pid)
 
 static int load_fixed_oom_config(void)
 {
-       int score;
        gpointer app_name;
        gpointer proc_conf_ptr;
        GHashTableIter app_list_iter;
@@ -101,27 +155,19 @@ static int load_fixed_oom_config(void)
                        continue;
                }
 
-               score = pci->oom_score;
-               if (score > OOMADJ_APP_MAX)
-                       continue;
+               if (!is_fixed_oom_score(pci->oom_score) && !(pci->is_app_in_foreground_app_list))
+                       return RESOURCED_ERROR_NONE;
 
-               switch (score) {
-                       case OOMADJ_SERVICE_MIN:
-                       case OOMADJ_SU:
-                       case OOMADJ_INIT:
-                       case OOMADJ_FOREGRD_LOCKED:
-                       case OOMADJ_FOREGRD_UNLOCKED:
-                       case OOMADJ_BACKGRD_PERCEPTIBLE:
-                       case OOMADJ_BACKGRD_LOCKED:
-                               _W("You can't set the fixed oom for %s with predefined value %d", pci->name, score);
-                               pci->oom_score = OOMADJ_APP_MAX + 10;
-                               break;
-                       default:
-                               if (score < FIXED_OOM_MIN || score > FIXED_OOM_MAX) {
-                                       _W("You can't set the fixed oom for %s with the out of range %d", pci->name, score);
-                                       pci->oom_score = OOMADJ_APP_MAX + 10;
-                               }
+               if (is_fixed_oom_score(pci->oom_score) && pci->is_app_in_foreground_app_list) {
+                       _I("Ignore 'OomScore' configuration of app = %s from no on", pci->name);
+                       pci->oom_score = OOMADJ_APP_MAX + 10;
                }
+
+               if (!is_fixed_oom_score(pci->oom_score))
+                       continue;
+
+               if (is_wrong_oom_score(pci->oom_score, pci->name))
+                       pci->oom_score = OOMADJ_APP_MAX + 10;
        }
 
        return RESOURCED_ERROR_NONE;