lowmem-controller: Move medium memory action from lowmem to controller 75/290575/18
authorSangYoun Kwak <sy.kwak@samsung.com>
Wed, 29 Mar 2023 04:47:42 +0000 (13:47 +0900)
committerSangYoun Kwak <sy.kwak@samsung.com>
Mon, 10 Apr 2023 09:04:30 +0000 (18:04 +0900)
For modulization, medium 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.

The dedup_act is moved from lowmem.c to controller since it is an action
of medium memory. But this function is also an action of critical
memory. Thus, a part of critical memory actions was replaced with
lowmem_controller_ops.

Change-Id: I04663b904cfd8c1a18008bc45be74abb2cb6fb47
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

index db2c9e3..0afaff9 100644 (file)
@@ -39,6 +39,7 @@
 #include "notifier.h"
 #include "freezer.h"
 #include "swap-common.h"
+#include "dedup-common.h"
 
 #define MAX_VICTIMS_BETWEEN_CHECK  3
 
@@ -199,8 +200,19 @@ static int lowmem_controller_kill_candidates(GArray *candidates,
        return victim_cnt;
 }
 
-/* lowmem actions */
+static int dedup_prepare(void)
+{
+       if (dedup_get_state() != DEDUP_ONE_SHOT)
+               return RESOURCED_ERROR_FAIL;
+
+       if (proc_get_freezer_status() == CGROUP_FREEZER_PAUSED)
+               resourced_notify(RESOURCED_NOTIFIER_FREEZER_CGROUP_STATE,
+                               (void *)CGROUP_FREEZER_ENABLED);
+
+       return RESOURCED_ERROR_NONE;
+}
 
+/* lowmem actions */
 static int high_mem_act(void *data)
 {
        int ret, status;
@@ -227,6 +239,41 @@ static int high_mem_act(void *data)
        return RESOURCED_ERROR_NONE;
 }
 
+static int medium_mem_act(void *data)
+{
+       int status = 0;
+       int scan_mode = KSM_SCAN_PARTIAL;
+
+       if (dedup_prepare() < 0)
+               return RESOURCED_ERROR_FAIL;
+
+       if (vconf_get_int(VCONFKEY_SYSMAN_LOW_MEMORY, &status) != 0)
+               _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_MEDIUM);
+       }
+       lowmem_change_lowmem_state(MEM_LEVEL_MEDIUM);
+
+       resourced_notify(RESOURCED_NOTIFIER_DEDUP_SCAN, &scan_mode);
+
+       return RESOURCED_ERROR_NONE;
+}
+static int critical_mem_act(void *data)
+{
+       int scan_mode = KSM_SCAN_FULL;
+
+       if (dedup_prepare() < 0)
+               return RESOURCED_ERROR_FAIL;
+
+       resourced_notify(RESOURCED_NOTIFIER_DEDUP_SCAN, &scan_mode);
+
+       return RESOURCED_ERROR_NONE;
+}
+
+
 static int lowmem_controller_initialize(void *data)
 {
        /**
@@ -239,6 +286,8 @@ 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_CRITICAL, critical_mem_act);
 
        return RESOURCED_ERROR_NONE;
 }
index c5e9e76..e8a0989 100644 (file)
@@ -26,6 +26,7 @@
 #include "lowmem.h"
 #include "module.h"
 #include "memory-cgroup.h"
+#include "dedup-common.h"
 
 static int mem_state_dummy_governor(void *data)
 {
@@ -44,6 +45,8 @@ 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_CRITICAL, mem_state_dummy_governor);
 
        return RESOURCED_ERROR_NONE;
 }
index da729a6..0ed6637 100644 (file)
@@ -289,6 +289,8 @@ 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_CRITICAL:
                lowmem_actions[mem_state].governor = governor;
                return;
        default:
@@ -299,6 +301,8 @@ void lowmem_initialize_controller_ops_action(int mem_state, int (*action)(void *
 {
        switch (mem_state) {
        case MEM_LEVEL_HIGH:
+       case MEM_LEVEL_MEDIUM:
+       case MEM_LEVEL_CRITICAL:
                lowmem_actions[mem_state].action = action;
                return;
        default:
@@ -1065,38 +1069,6 @@ static void swap_activate_act(void)
                resourced_notify(RESOURCED_NOTIFIER_SWAP_ACTIVATE, NULL);
 }
 
-static void dedup_act(enum ksm_scan_mode mode)
-{
-       int ret, status;
-       int data;
-
-       if (dedup_get_state() != DEDUP_ONE_SHOT)
-               return;
-
-       if (proc_get_freezer_status() == CGROUP_FREEZER_PAUSED)
-               resourced_notify(RESOURCED_NOTIFIER_FREEZER_CGROUP_STATE,
-                               (void *)CGROUP_FREEZER_ENABLED);
-
-       if (mode == KSM_SCAN_PARTIAL) {
-               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_MEDIUM);
-               }
-               lowmem_change_lowmem_state(MEM_LEVEL_MEDIUM);
-
-               data = KSM_SCAN_PARTIAL;
-               resourced_notify(RESOURCED_NOTIFIER_DEDUP_SCAN, &data);
-       } else if (mode == KSM_SCAN_FULL) {
-               data = KSM_SCAN_FULL;
-               resourced_notify(RESOURCED_NOTIFIER_DEDUP_SCAN, &data);
-       }
-}
-
 static void swap_compact_act(void)
 {
        lowmem_change_lowmem_state(MEM_LEVEL_CRITICAL);
@@ -1175,20 +1147,22 @@ void lowmem_trigger_memory_state_action(int mem_state)
 
        switch (mem_state) {
        case MEM_LEVEL_HIGH:
+       case MEM_LEVEL_MEDIUM:
                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_MEDIUM:
-               dedup_act(KSM_SCAN_PARTIAL);
-               break;
        case MEM_LEVEL_LOW:
                swap_activate_act();
                break;
        case MEM_LEVEL_CRITICAL:
-               dedup_act(KSM_SCAN_FULL);
+               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);
                swap_compact_act();
                break;
        case MEM_LEVEL_OOM: