proc-stat : block modifying the OOM-fixed app's score 42/159742/6
authorKichan Kwon <k_c.kwon@samsung.com>
Mon, 13 Nov 2017 01:45:42 +0000 (10:45 +0900)
committerKichan Kwon <k_c.kwon@samsung.com>
Tue, 28 Nov 2017 07:41:05 +0000 (16:41 +0900)
- We can't control widget's OOM score with existing mechanism
  because widget's score is modified not via set_foregrd(backgrd)
- For performance, make another hashtable using PID as the key

Change-Id: Ib89c84cf6062c0f4d73438b2425234422d2eb2b3
Signed-off-by: Kichan Kwon <k_c.kwon@samsung.com>
src/common/proc-common.h
src/common/procfs.c
src/proc-stat/proc-priority.c

index 29752b9b208a03e775b44fa361c0e6c2a007a7c1..e8bc6f79761bf6f43e43f404436cd9efee371350 100644 (file)
@@ -222,4 +222,11 @@ static inline void cleanup_proc_app_list_close_func(GSList **l)
 
 #define _cleanup_app_list_close_ __attribute__((cleanup(cleanup_proc_app_list_close_func)))
 
+/**
+ * @desc  Check whether this process is OOM-fixed app or not
+ * @param[in] pid  Process ID
+ * @return  1 if it is OOM-fixed app. Else, 0
+ */
+int proc_priority_is_oom_fixed_process(int pid);
+
 #endif /* __PROC_COMMON_H__ */
index eca0404f06e5f3bda2b040dd44e21def86ee3bf7..4d381d73a3bffe6db98f6f1f8dd6d4ffa41ef2b1 100644 (file)
@@ -155,6 +155,10 @@ int proc_set_oom_score_adj(int pid, int oom_score_adj)
        struct lowmem_control_data lowmem_data;
        char buf[sizeof(PROC_OOM_SCORE_ADJ_PATH) + MAX_DEC_SIZE(int)] = {0};
 
+       /* Don't touch OOM-fixed process' score */
+       if (proc_priority_is_oom_fixed_process(pid))
+               return RESOURCED_ERROR_NONE;
+
        snprintf(buf, sizeof(buf), PROC_OOM_SCORE_ADJ_PATH, pid);
        fp = fopen(buf, "r+");
        if (fp == NULL)
index 1f3822127de951cb897bdbdc0b05c03d8b8db759..e2dec9d68789a0c0481b3d6a7811ef43ef233538 100644 (file)
 #define FIXED_OOM_MAX  OOMADJ_BACKGRD_LOCKED
 
 static GHashTable *oom_fixed_app_list;
+static GHashTable *oom_fixed_pid_list;
 
 static int proc_priority_set_fixed_oom(void *data)
 {
        int ret;
        int *fixed_oom = NULL;
+       gint *key, *val;
        struct proc_status *ps = (struct proc_status *)data;
 
+       if (!ps)
+               return RESOURCED_ERROR_INVALID_PARAMETER;
+
        fixed_oom = (int *)g_hash_table_lookup(oom_fixed_app_list, ps->pai->appid);
+
+       /* Make another hashtable for fast searching during proc_set_oom_score_adj */
        if (fixed_oom) {
                ret = proc_set_oom_score_adj(ps->pid, *fixed_oom);
-               if (ret != RESOURCED_ERROR_NONE)
+               if (ret != RESOURCED_ERROR_NONE) {
                        _E("Failed to set the fixed oom for %s", ps->pai->appid);
-               else
-                       _D("Set the fixed oom of %s with %d", ps->pai->appid, *fixed_oom);
+                       return ret;
+               }
+
+               _D("Set the fixed oom of %s with %d", ps->pai->appid, fixed_oom);
+               key = g_new(gint, 1);
+               val = g_new(gint, 1);
+               *key = ps->pid;
+               *val = *fixed_oom;
+               g_hash_table_insert(oom_fixed_pid_list, (gpointer)key, (gpointer)val);
        }
 
        return RESOURCED_ERROR_NONE;
 }
 
+static int proc_priority_remove_pid(void *data)
+{
+       struct proc_status *ps = (struct proc_status *)data;
+       int pid = ps->pid;
+
+       if (g_hash_table_contains(oom_fixed_pid_list, &pid))
+               g_hash_table_remove(oom_fixed_pid_list, &pid);
+
+       return RESOURCED_ERROR_NONE;
+}
+
+int proc_priority_is_oom_fixed_process(int pid)
+{
+       return g_hash_table_contains(oom_fixed_pid_list, &pid);
+}
+
 static int load_fixed_oom_config(struct parse_result *result, void *user_data)
 {
+       int score;
        gint *fixed_oom;
 
        if (!result)
                return RESOURCED_ERROR_INVALID_PARAMETER;
 
        if (!strncmp(result->section, FIXED_OOM_CONF_SECTION, strlen(FIXED_OOM_CONF_SECTION) + 1)) {
-               fixed_oom = g_new(gint, 1);
-               *fixed_oom = atoi(result->value);
+               /* Set predefined OOM score */
+               score = atoi(result->value);
 
-               switch (*fixed_oom) {
+               switch (score) {
                case OOMADJ_SERVICE_MIN:
                case OOMADJ_SU:
                case OOMADJ_INIT:
@@ -82,19 +113,17 @@ static int load_fixed_oom_config(struct parse_result *result, void *user_data)
                case OOMADJ_FOREGRD_UNLOCKED:
                case OOMADJ_BACKGRD_PERCEPTIBLE:
                case OOMADJ_BACKGRD_LOCKED:
-                       _E("You can't set the fixed oom for %s with predefined value %d", result->name, *fixed_oom);
-                       g_free(fixed_oom);
+                       _E("You can't set the fixed oom for %s with predefined value %d", result->name, score);
                        return RESOURCED_ERROR_INVALID_PARAMETER;
-                       break;
                default:
-                       if (*fixed_oom < FIXED_OOM_MIN || *fixed_oom > FIXED_OOM_MAX) {
-                               _E("You can't set the fixed oom for %s with the out of range %d", result->name, *fixed_oom);
-                               g_free(fixed_oom);
-                       return RESOURCED_ERROR_INVALID_PARAMETER;
+                       if (score < FIXED_OOM_MIN || score > FIXED_OOM_MAX) {
+                               _E("You can't set the fixed oom for %s with the out of range %d", result->name, score);
+                               return RESOURCED_ERROR_INVALID_PARAMETER;
                        }
-                       break;
                }
 
+               fixed_oom = g_new(gint, 1);
+               *fixed_oom = score;
                g_hash_table_insert(oom_fixed_app_list,
                                g_strndup(result->name, strlen(result->name)),
                                (gpointer)fixed_oom);
@@ -106,9 +135,13 @@ static int load_fixed_oom_config(struct parse_result *result, void *user_data)
 static int proc_priority_init(void *data)
 {
        oom_fixed_app_list = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
+       oom_fixed_pid_list = g_hash_table_new_full(g_int_hash, g_int_equal, g_free, g_free);
+       g_assert(oom_fixed_app_list && oom_fixed_pid_list);
+
        config_parse(PRIORITY_CONF_FILE, load_fixed_oom_config, NULL);
 
        register_notifier(RESOURCED_NOTIFIER_APP_LAUNCH, proc_priority_set_fixed_oom);
+       register_notifier(RESOURCED_NOTIFIER_APP_TERMINATED, proc_priority_remove_pid);
        return RESOURCED_ERROR_NONE;
 }
 
@@ -116,7 +149,10 @@ static int proc_priority_exit(void *data)
 {
        if (oom_fixed_app_list)
                g_hash_table_destroy(oom_fixed_app_list);
+       if (oom_fixed_pid_list)
+               g_hash_table_destroy(oom_fixed_pid_list);
        unregister_notifier(RESOURCED_NOTIFIER_APP_LAUNCH, proc_priority_set_fixed_oom);
+       unregister_notifier(RESOURCED_NOTIFIER_APP_TERMINATED, proc_priority_remove_pid);
        return RESOURCED_ERROR_NONE;
 }