From 16afd4f811379bf89faf1c4f3072b0420d9661c3 Mon Sep 17 00:00:00 2001 From: Unsung Lee Date: Tue, 5 Sep 2023 21:03:54 +0900 Subject: [PATCH] resourced-memory-lmk: Remove high working set size app from LMK 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 --- .../resourced-memory-lmk.c | 79 ++++++++++++++++++- 1 file changed, 77 insertions(+), 2 deletions(-) diff --git a/src/resourced-memory-lmk/resourced-memory-lmk.c b/src/resourced-memory-lmk/resourced-memory-lmk.c index bfed2a3..65179cb 100644 --- a/src/resourced-memory-lmk/resourced-memory-lmk.c +++ b/src/resourced-memory-lmk/resourced-memory-lmk.c @@ -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, -- 2.34.1