resourced-memory-lmk: Add governor function
authorSangYoun Kwak <sy.kwak@samsung.com>
Wed, 15 Feb 2023 08:59:04 +0000 (17:59 +0900)
committerSangYoun Kwak <sy.kwak@samsung.com>
Fri, 21 Apr 2023 08:17:42 +0000 (17:17 +0900)
The "get_kill_candidates" function has been added to the
resourced-memory-lmk module.
This function has been separated from lowmem module of resourced.

Signed-off-by: SangYoun Kwak <sy.kwak@samsung.com>
packaging/plugin-backend-resourced-rpi.spec
src/resourced-memory-lmk/resourced-memory-lmk-governor.c [deleted file]
src/resourced-memory-lmk/resourced-memory-lmk-governor.h [deleted file]
src/resourced-memory-lmk/resourced-memory-lmk.c

index b7cca9503965de84bf21c172a9a94bf6e87a959e..645d139d37fd2f27a2926467513970af291f5284 100644 (file)
@@ -26,6 +26,7 @@ cp %{SOURCE1} .
 
 %build
 %cmake . -DENABLE_DLOG=1 \
+       -DPLUGIN_BACKEND_RESOURCED_MEMORY_LMK_ENABLE_DLOG=1 \
        -DPLUGIN_NAME=%{name} \
        -DPLUGIN_LICENSE_DIR=%{PLUGIN_LICENSEDIR} \
        -DPLUGIN_LIB_DIR=%{PLUGIN_LIBDIR}
diff --git a/src/resourced-memory-lmk/resourced-memory-lmk-governor.c b/src/resourced-memory-lmk/resourced-memory-lmk-governor.c
deleted file mode 100644 (file)
index 2251e58..0000000
+++ /dev/null
@@ -1,29 +0,0 @@
-/**
- * plugin-backend-resourced-rpi
- *
- * Copyright (c) 2023 Samsung Electronics Co., Ltd. All rights reserved.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include <glib.h>
-
-#include "common.h"
-#include "resourced-memory-lmk-governor.h"
-
-GArray *lowmem_governor_get_kill_candidates(GSList *proc_app_list, int start_oom, int end_oom, int killer_flags)
-{
-       _D("lowmem_governor_get_kill_candidates called");
-
-       return NULL;
-}
diff --git a/src/resourced-memory-lmk/resourced-memory-lmk-governor.h b/src/resourced-memory-lmk/resourced-memory-lmk-governor.h
deleted file mode 100644 (file)
index 9328607..0000000
+++ /dev/null
@@ -1,24 +0,0 @@
-/**
- * plugin-backend-resourced-rpi
- *
- * Copyright (c) 2023 Samsung Electronics Co., Ltd. All rights reserved.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef __RESOURCED_MEMORY_LMK_GOVERNOR_H__
-#define __RESOURCED_MEMORY_LMK_GOVERNOR_H__
-
-GArray *lowmem_governor_get_kill_candidates(GSList *proc_app_list, int start_oom, int end_oom, int killer_flags);
-
-#endif /* __RESOURCED_MEMORY_LMK_GOVERNOR_H__ */
index 39b0b4ad672db3946103bf87782d6c58703e8542..24af6257309dfd700e4e2109f518cc4f08e645a4 100644 (file)
  */
 
 #include <stdlib.h>
+#include <glib.h>
+#include <assert.h>
+#include <stdbool.h>
 
 #include <plugin/plugin-common-interface.h>
+#include <plugin/plugin-resourced-memory-lmk.h>
 #include <plugin/plugin-resourced-memory-lmk-interface.h>
 
 #include "common.h"
-#include "resourced-memory-lmk-governor.h" /* TODO: Remove this line */
 
 #define EXPORT __attribute__ ((visibility("default")))
 
-/*
-GArray *get_kill_candidates(GSList *proc_app_list, int start_oom, int end_oom, int killer_flags)
+static int compare_victims(const struct task_info **ta,
+                       const struct task_info **tb,
+                       const unsigned long *totalram_kibi_bytes)
 {
-       return NULL;
+        unsigned int pa, pb;
+
+       assert(ta != NULL);
+       assert(tb != NULL);
+       assert(*ta != NULL);
+       assert(*tb != NULL);
+       /**
+        * followed by kernel badness point calculation using heuristic.
+        * oom_score_adj is normalized by its unit, which varies -1000 ~ 1000.
+        */
+       pa = (*ta)->oom_score_lru * (*totalram_kibi_bytes / 2000) + (*ta)->size;
+       pb = (*tb)->oom_score_lru * (*totalram_kibi_bytes / 2000) + (*tb)->size;
+
+       return pb - pa;
+}
+
+static GArray *get_kill_candidates(GArray *task_info_app_array,
+                                       GArray *task_info_proc_array,
+                                       int start_oom, int end_oom,
+                                       unsigned long totalram_kibi_bytes)
+{
+       GArray *candidates =
+               g_array_new(false, false, sizeof(struct task_info *));
+
+       for (int i = 0; i < task_info_app_array->len; ++i) {
+               struct task_info *task = &g_array_index(task_info_app_array,
+                                                       struct task_info, i);
+
+               if (!task->pid)
+                       continue;
+
+               if (task->oom_score_adj > end_oom
+                       || task->oom_score_adj < start_oom)
+                       continue;
+
+               g_array_append_val(candidates, task);
+       }
+
+       _D("Apps candidate ratio=%d/%d",
+               candidates->len, task_info_app_array->len);
+
+       if (!candidates->len) {
+               return candidates;
+       }
+
+       if (task_info_proc_array) {
+               for (int i = 0; i < task_info_proc_array->len; ++i) {
+                       struct task_info *task =
+                               &g_array_index(task_info_proc_array,
+                                               struct task_info, i);
+                       g_array_append_val(candidates, task);
+               }
+               _D("%d processes were added to candidate", task_info_proc_array->len);
+       }
+
+       g_array_sort_with_data(candidates, (GCompareDataFunc)compare_victims,
+                                                       &totalram_kibi_bytes);
+
+       return candidates;
 }
-*/
 
-int resourced_memory_lmk_init(void **data)
+static int resourced_memory_lmk_init(void **data)
 {
        plugin_backend_resourced_memory_lmk_funcs *funcs = NULL;
 
@@ -41,15 +102,14 @@ int resourced_memory_lmk_init(void **data)
        if (!funcs)
                return -ENOMEM;
 
-       /* funcs->get_kill_candidates = get_kill_candidates; */ /* TODO: Use this line instead of a line below */
-       funcs->get_kill_candidates = lowmem_governor_get_kill_candidates; /* TODO: Remove this line */
+       funcs->get_kill_candidates = get_kill_candidates;
 
        *data = (void *)funcs;
 
        return 0;
 }
 
-int resourced_memory_lmk_exit(void *data)
+static int resourced_memory_lmk_exit(void *data)
 {
        free(data);
        return 0;