resourced-memory-lmk: Remove high working set size app from LMK 79/298379/2 accepted/tizen/unified/20230907.175324
authorUnsung Lee <unsung.lee@samsung.com>
Tue, 5 Sep 2023 12:03:54 +0000 (21:03 +0900)
committerUnsung Lee <unsung.lee@samsung.com>
Wed, 6 Sep 2023 08:34:08 +0000 (17:34 +0900)
App that satisfies criteria such as LMK try count and working set size threshold
will be removed from candidates list.

Change-Id: I393e1fadd035183cd17ca99acf31cfb63d637c1e
Signed-off-by: Unsung Lee <unsung.lee@samsung.com>
src/resourced-memory-lmk/resourced-memory-lmk.c

index bfed2a36fd3b7e74963fea37f4fa66c3d8f7fcd1..65179cb60f4293e96b6b5d4ce7d101f35aaccd72 100644 (file)
@@ -32,6 +32,9 @@
 #define INIT_PROCESS_ID                1
 #define MINIMUM_Z_VALUE                1
 
+#define LMK_TRY_COUNT_THRESHOLD                2
+#define WSS_THRESHOLD                  55
+
 /**
  * -900 is very low oom_score_adj (-1000 to 1000), so
  * focused app will not be choosen as victim regardless of memory size.
@@ -110,9 +113,81 @@ static int get_kill_candidates_post(GArray *candidates)
        return is_valid_candidates_list(candidates);
 }
 
-static int get_kill_candidates_post_with_wss(GArray *candidates)
+static int calculate_working_set_size(struct task_info task, int *wss)
 {
-       return is_valid_candidates_list(candidates);
+       unsigned long active_memory_kb = task.active_memory_kb;
+       unsigned long inactive_memory_kb = task.inactive_memory_kb;
+       unsigned long swap_memory_kb = task.swap_memory_kb;
+       unsigned long sum_memory_kb =
+               active_memory_kb + inactive_memory_kb + swap_memory_kb;
+
+       if (!wss)
+               return -EINVAL;
+
+       /* Check whether it is app. If not, do not calculate working set size */
+       if (active_memory_kb == 0 && inactive_memory_kb == 0) {
+               *wss = -1;
+               return 0;
+       }
+
+       /**
+        * If working set size is lower than working set size threshold,
+        * then do not consider working set size of this app.
+        */
+       *wss = (active_memory_kb * 100) / sum_memory_kb;
+       if (*wss < WSS_THRESHOLD)
+               *wss = -1;
+
+       return 0;
+}
+
+static int get_kill_candidates_post_with_wss(GArray *candidates, int lmk_try_count)
+{
+       int max_wss = 0;
+       int max_wss_app_memory_size_kb = 1024 * 1024 * 1024;
+       struct task_info *max_wss_app_task = NULL;
+
+       int ret = is_valid_candidates_list(candidates);
+       if (ret < 0)
+               return ret;
+
+       if (lmk_try_count <= 0)
+               return -EINVAL;
+
+       if (lmk_try_count >= LMK_TRY_COUNT_THRESHOLD)
+               return 0;
+
+       for (int i = 0; i < candidates->len; ++i) {
+               int wss = 0;
+               struct task_info *task = g_array_index(candidates,
+                               struct task_info *, i);
+
+               assert(task);
+
+               if (calculate_working_set_size(*task, &wss)  < 0)
+                       return -EINVAL;
+
+               if (wss < 0)
+                       continue;
+
+               if (max_wss < wss) {
+                       max_wss = wss;
+                       max_wss_app_memory_size_kb = task->size;
+                       max_wss_app_task = task;
+               } else if (max_wss == wss
+                               && max_wss_app_memory_size_kb > task->size) {
+                       max_wss_app_memory_size_kb = task->size;
+                       max_wss_app_task = task;
+               }
+       }
+
+       /* All apps in candidates list have lower wss than WSS_THRESHOLD */
+       if (max_wss_app_task == NULL)
+               return 0;
+
+       max_wss_app_task->is_free_from_kill = true;
+
+       return 0;
 }
 
 static int get_kill_candidates_post_with_foreground(GArray *candidates,