lowmem-controller: Move low memory action from lowmem to controller 78/290578/18
authorSangYoun Kwak <sy.kwak@samsung.com>
Wed, 29 Mar 2023 06:25:01 +0000 (15:25 +0900)
committerSangYoun Kwak <sy.kwak@samsung.com>
Mon, 10 Apr 2023 09:04:30 +0000 (18:04 +0900)
For modulization, low memory action is moved from lowmem.c(core) to
lowmem-controller.c(controller) and this action will be assigned to
lowmem.c by the initializer of controller.

Functions which are related to swap_activate_act are moved to controller
as well.
register_notifier and unregister_notifier of
RESOURCED_NOTIFIER_MEM_CONTROL are moved to controller.

Change-Id: Icd0a0c78d660e773221426c585e41ee6c4449ed8
Signed-off-by: SangYoun Kwak <sy.kwak@samsung.com>
src/resource-limiter/memory/lowmem-controller.c
src/resource-limiter/memory/lowmem-governor.c
src/resource-limiter/memory/lowmem.c
src/resource-limiter/memory/lowmem.h

index 0afaff9..8066072 100644 (file)
@@ -28,6 +28,7 @@
 #include <eventsystem.h>
 
 #include "lowmem.h"
+#include "lowmem-limit.h"
 #include "proc-common.h"
 #include "proc-main.h"
 #include "resourced.h"
@@ -212,6 +213,118 @@ static int dedup_prepare(void)
        return RESOURCED_ERROR_NONE;
 }
 
+static void swap_activate_act(void)
+{
+       int ret, status;
+
+       ret = vconf_get_int(VCONFKEY_SYSMAN_LOW_MEMORY, &status);
+       if (ret)
+               _E("vconf get failed %s", VCONFKEY_SYSMAN_LOW_MEMORY);
+
+       if (status != VCONFKEY_SYSMAN_LOW_MEMORY_NORMAL) {
+               vconf_set_int(VCONFKEY_SYSMAN_LOW_MEMORY,
+                               VCONFKEY_SYSMAN_LOW_MEMORY_NORMAL);
+               lowmem_memory_level_send_system_event(MEM_LEVEL_LOW);
+       }
+       lowmem_change_lowmem_state(MEM_LEVEL_LOW);
+       if (proc_get_freezer_status() == CGROUP_FREEZER_PAUSED)
+               resourced_notify(RESOURCED_NOTIFIER_FREEZER_CGROUP_STATE,
+                       (void *)CGROUP_FREEZER_ENABLED);
+
+       if (swap_get_state() != SWAP_ON)
+               resourced_notify(RESOURCED_NOTIFIER_SWAP_ACTIVATE, NULL);
+}
+
+static void lowmem_swap_memory(char *path)
+{
+       /* The services cannot call this function but the apps can. */
+       unsigned int available_mb;
+       unsigned int cur_mem_state;
+
+       cur_mem_state = lowmem_get_lowmem_state();
+       if (cur_mem_state == MEM_LEVEL_HIGH)
+               return;
+
+       if (swap_get_state() != SWAP_ON)
+               return;
+
+       available_mb = proc_get_mem_available();
+       if (cur_mem_state != MEM_LEVEL_LOW &&
+           available_mb <= get_root_memcg_info()->threshold_mb[MEM_LEVEL_LOW])
+               swap_activate_act();
+
+       resourced_notify(RESOURCED_NOTIFIER_SWAP_START, path);
+       lowmem_set_memcg_swap_status(true);
+}
+
+static void lowmem_move_memcgroup(int pid, int next_oom_score_adj, struct proc_app_info *pai)
+{
+       int cur_oom_score_adj;
+       int cur_memcg_idx;
+       struct memcg_info *mi;
+       int next_memcg_idx = cgroup_get_type(next_oom_score_adj);
+
+       mi = get_memcg_info(next_memcg_idx);
+
+       if (!mi)
+               return;
+
+       if (!pai) {
+               cgroup_write_pid_fullpath(mi->name, pid);
+               return;
+       }
+
+       if (pai->main_pid == pid) {
+               /* parent pid */
+               cur_oom_score_adj = pai->memory.oom_score_adj;
+               cur_memcg_idx = cgroup_get_type(cur_oom_score_adj);
+
+               if (cur_oom_score_adj == next_oom_score_adj) {
+                       _D("next oom_score_adj (%d) is same with current one", next_oom_score_adj);
+                       return;
+               }
+
+               proc_set_process_memory_state(pai, next_memcg_idx, mi, next_oom_score_adj);
+
+               if (!lowmem_limit_move_cgroup(pai))
+                       return;
+
+               if(cur_memcg_idx == next_memcg_idx)
+                       return;
+
+               _I("app (%s) memory cgroup move from %s to %s", pai->appid,
+                       lowmem_convert_cgroup_type_to_str(cur_memcg_idx),
+                       lowmem_convert_cgroup_type_to_str(next_memcg_idx));
+
+               cgroup_write_pid_fullpath(mi->name, pid);
+               if (next_memcg_idx == MEMCG_THROTTLING)
+                       lowmem_swap_memory(get_memcg_info(MEMCG_THROTTLING)->name);
+       } else {
+               /* child pid */
+               if (pai->memory.use_mem_limit)
+                       return;
+
+               cgroup_write_pid_fullpath(mi->name, pid);
+       }
+}
+
+int lowmem_control_handler(void *data)
+{
+       struct lowmem_control_data *lowmem_data = (struct lowmem_control_data *)data;
+
+       switch (lowmem_data->control_type) {
+       case LOWMEM_MOVE_CGROUP:
+               lowmem_move_memcgroup((pid_t)lowmem_data->pid,
+                                       lowmem_data->oom_score_adj,
+                                       lowmem_data->pai);
+               break;
+       default:
+               break;
+       }
+
+       return RESOURCED_ERROR_NONE;
+}
+
 /* lowmem actions */
 static int high_mem_act(void *data)
 {
@@ -261,6 +374,12 @@ static int medium_mem_act(void *data)
 
        return RESOURCED_ERROR_NONE;
 }
+static int low_mem_act(void *data)
+{
+       swap_activate_act();
+
+       return RESOURCED_ERROR_NONE;
+}
 static int critical_mem_act(void *data)
 {
        int scan_mode = KSM_SCAN_FULL;
@@ -287,13 +406,18 @@ static int lowmem_controller_initialize(void *data)
        /* Initialize actions for the memory levels */
        lowmem_initialize_controller_ops_action(MEM_LEVEL_HIGH, high_mem_act);
        lowmem_initialize_controller_ops_action(MEM_LEVEL_MEDIUM, medium_mem_act);
+       lowmem_initialize_controller_ops_action(MEM_LEVEL_LOW, low_mem_act);
        lowmem_initialize_controller_ops_action(MEM_LEVEL_CRITICAL, critical_mem_act);
 
+       register_notifier(RESOURCED_NOTIFIER_MEM_CONTROL, lowmem_control_handler);
+
        return RESOURCED_ERROR_NONE;
 }
 
 static int lowmem_controller_finalize(void *data)
 {
+       unregister_notifier(RESOURCED_NOTIFIER_MEM_CONTROL, lowmem_control_handler);
+
        return RESOURCED_ERROR_NONE;
 }
 
index e8a0989..2803fdc 100644 (file)
@@ -46,6 +46,7 @@ static int lowmem_governor_initialize(void *data)
        /* Initialize governors for the memory levels */
        lowmem_initialize_controller_ops_governor(MEM_LEVEL_HIGH, mem_state_dummy_governor);
        lowmem_initialize_controller_ops_governor(MEM_LEVEL_MEDIUM, mem_state_dummy_governor);
+       lowmem_initialize_controller_ops_governor(MEM_LEVEL_LOW, mem_state_dummy_governor);
        lowmem_initialize_controller_ops_governor(MEM_LEVEL_CRITICAL, mem_state_dummy_governor);
 
        return RESOURCED_ERROR_NONE;
index 0ed6637..3bf9bea 100644 (file)
@@ -276,7 +276,6 @@ static void lowmem_request_destroy(gpointer data)
 
 /* low memory action function for cgroup */
 /* low memory action function */
-static void swap_activate_act(void);
 static void swap_compact_act(void);
 static void lmk_act(void);
 
@@ -290,6 +289,7 @@ void lowmem_initialize_controller_ops_governor(int mem_state, int (*governor)(vo
        switch (mem_state) {
        case MEM_LEVEL_HIGH:
        case MEM_LEVEL_MEDIUM:
+       case MEM_LEVEL_LOW:
        case MEM_LEVEL_CRITICAL:
                lowmem_actions[mem_state].governor = governor;
                return;
@@ -947,6 +947,10 @@ process_again:
        pthread_exit(NULL);
 }
 
+unsigned int lowmem_get_lowmem_state()
+{
+       return cur_mem_state;
+}
 void lowmem_change_lowmem_state(unsigned int mem_state)
 {
        cur_mem_state = mem_state;
@@ -956,28 +960,6 @@ void lowmem_change_lowmem_state(unsigned int mem_state)
                (void *)&cur_mem_state);
 }
 
-/* only app can call this function
- * that is, service cannot call the function
- */
-static void lowmem_swap_memory(char *path)
-{
-       unsigned int available_mb;
-
-       if (cur_mem_state == MEM_LEVEL_HIGH)
-               return;
-
-       if (swap_get_state() != SWAP_ON)
-               return;
-
-       available_mb = proc_get_mem_available();
-       if (cur_mem_state != MEM_LEVEL_LOW &&
-           available_mb <= get_root_memcg_info()->threshold_mb[MEM_LEVEL_LOW])
-               swap_activate_act();
-
-       resourced_notify(RESOURCED_NOTIFIER_SWAP_START, path);
-       memcg_swap_status = true;
-}
-
 void lowmem_trigger_swap(pid_t pid, char *path, bool move)
 {
        int error;
@@ -1047,28 +1029,6 @@ void lowmem_memory_level_send_system_event(int lv)
        bundle_free(b);
 }
 
-static void swap_activate_act(void)
-{
-       int ret, status;
-
-       ret = vconf_get_int(VCONFKEY_SYSMAN_LOW_MEMORY, &status);
-       if (ret)
-               _E("vconf get failed %s", VCONFKEY_SYSMAN_LOW_MEMORY);
-
-       if (status != VCONFKEY_SYSMAN_LOW_MEMORY_NORMAL) {
-               vconf_set_int(VCONFKEY_SYSMAN_LOW_MEMORY,
-                               VCONFKEY_SYSMAN_LOW_MEMORY_NORMAL);
-               lowmem_memory_level_send_system_event(MEM_LEVEL_LOW);
-       }
-       lowmem_change_lowmem_state(MEM_LEVEL_LOW);
-       if (proc_get_freezer_status() == CGROUP_FREEZER_PAUSED)
-               resourced_notify(RESOURCED_NOTIFIER_FREEZER_CGROUP_STATE,
-                       (void *)CGROUP_FREEZER_ENABLED);
-
-       if (swap_get_state() != SWAP_ON)
-               resourced_notify(RESOURCED_NOTIFIER_SWAP_ACTIVATE, NULL);
-}
-
 static void swap_compact_act(void)
 {
        lowmem_change_lowmem_state(MEM_LEVEL_CRITICAL);
@@ -1148,15 +1108,13 @@ void lowmem_trigger_memory_state_action(int mem_state)
        switch (mem_state) {
        case MEM_LEVEL_HIGH:
        case MEM_LEVEL_MEDIUM:
+       case MEM_LEVEL_LOW:
                assert(lowmem_actions[mem_state].governor != NULL);
                assert(lowmem_actions[mem_state].action != NULL);
                if (lowmem_actions[mem_state].governor(NULL) < 0)
                        break;
                lowmem_actions[mem_state].action(NULL);
                break;
-       case MEM_LEVEL_LOW:
-               swap_activate_act();
-               break;
        case MEM_LEVEL_CRITICAL:
                assert(lowmem_actions[mem_state].governor != NULL);
                assert(lowmem_actions[mem_state].action != NULL);
@@ -1277,56 +1235,6 @@ static void setup_memcg_params(void)
        }
 }
 
-static void lowmem_move_memcgroup(int pid, int next_oom_score_adj, struct proc_app_info *pai)
-{
-       int cur_oom_score_adj;
-       int cur_memcg_idx;
-       struct memcg_info *mi;
-       int next_memcg_idx = cgroup_get_type(next_oom_score_adj);
-
-       mi = get_memcg_info(next_memcg_idx);
-
-       if (!mi) {
-               return;
-       }
-
-       if (!pai) {
-               cgroup_write_pid_fullpath(mi->name, pid);
-               return;
-       }
-
-       /* parent pid */
-       if (pai->main_pid == pid) {
-               cur_oom_score_adj = pai->memory.oom_score_adj;
-               cur_memcg_idx = cgroup_get_type(cur_oom_score_adj);
-
-               if (cur_oom_score_adj == next_oom_score_adj) {
-                       _D("next oom_score_adj (%d) is same with current one", next_oom_score_adj);
-                       return;
-               }
-
-               proc_set_process_memory_state(pai, next_memcg_idx, mi, next_oom_score_adj);
-
-               if (!lowmem_limit_move_cgroup(pai))
-                       return;
-
-               if(cur_memcg_idx == next_memcg_idx)
-                       return;
-
-               _I("app (%s) memory cgroup move from %s to %s", pai->appid, lowmem_convert_cgroup_type_to_str(cur_memcg_idx), lowmem_convert_cgroup_type_to_str(next_memcg_idx));
-               cgroup_write_pid_fullpath(mi->name, pid);
-               if (next_memcg_idx == MEMCG_THROTTLING)
-                       lowmem_swap_memory(get_memcg_info(MEMCG_THROTTLING)->name);
-       }
-       /* child pid */
-       else {
-               if (pai->memory.use_mem_limit)
-                       return;
-
-               cgroup_write_pid_fullpath(mi->name, pid);
-       }
-}
-
 static int lowmem_activate_worker(void)
 {
        int ret = RESOURCED_ERROR_NONE;
@@ -1528,22 +1436,6 @@ static int lowmem_prelaunch_handler(void *data)
        return RESOURCED_ERROR_NONE;
 }
 
-int lowmem_control_handler(void *data)
-{
-       struct lowmem_control_data *lowmem_data;
-
-       lowmem_data = (struct lowmem_control_data *)data;
-       switch (lowmem_data->control_type) {
-       case LOWMEM_MOVE_CGROUP:
-               lowmem_move_memcgroup((pid_t)lowmem_data->pid,
-                                       lowmem_data->oom_score_adj, lowmem_data->pai);
-               break;
-       default:
-               break;
-       }
-       return RESOURCED_ERROR_NONE;
-}
-
 static inline int calculate_threshold_size(double ratio)
 {
        unsigned long long size_bytes = (double)totalram_bytes * ratio / 100.0;
@@ -1656,7 +1548,6 @@ static int lowmem_init(void)
        lowmem_system_init();
 
        register_notifier(RESOURCED_NOTIFIER_APP_PRELAUNCH, lowmem_prelaunch_handler);
-       register_notifier(RESOURCED_NOTIFIER_MEM_CONTROL, lowmem_control_handler);
 
        return ret;
 }
@@ -1668,7 +1559,6 @@ static int lowmem_exit(void)
        lowmem_system_exit();
 
        unregister_notifier(RESOURCED_NOTIFIER_APP_PRELAUNCH, lowmem_prelaunch_handler);
-       unregister_notifier(RESOURCED_NOTIFIER_MEM_CONTROL, lowmem_control_handler);
 
        return RESOURCED_ERROR_NONE;
 }
index 4003e25..136a511 100644 (file)
@@ -102,6 +102,7 @@ void lowmem_initialize_kill_candidates(int(*kill_candidates)(GArray *, unsigned,
                                                void(*)(void)));
 void lowmem_initialize_controller_ops_governor(int mem_state, int (*governor)(void *data));
 void lowmem_initialize_controller_ops_action(int mem_state, int (*action)(void *data));
+unsigned int lowmem_get_lowmem_state();
 void lowmem_change_lowmem_state(unsigned int mem_state);
 void lowmem_memory_level_send_system_event(int lv);
 bool lowmem_get_memcg_swap_status();