lowmem-governor: Modify governor as a module 13/290613/10
authorSangYoun Kwak <sy.kwak@samsung.com>
Wed, 29 Mar 2023 10:23:37 +0000 (19:23 +0900)
committerSangYoun Kwak <sy.kwak@samsung.com>
Fri, 7 Apr 2023 07:59:04 +0000 (16:59 +0900)
Since the lowmem-governor is separated from lowmem.c in functional
manner, it is better to modulize the lowmem-governor to reduce
dependency.

lowmem.c uses lowmem_governor_ops to use governor and
lowmem_governor_ops is initialized by the lowmem-governor.
lowmem-governor.h is removed since it is not used directly.

Change-Id: I177d847dd8010a9c66f970f2b7aec53f06210740
Signed-off-by: SangYoun Kwak <sy.kwak@samsung.com>
src/resource-limiter/memory/lowmem-governor.c
src/resource-limiter/memory/lowmem-governor.h [deleted file]
src/resource-limiter/memory/lowmem.c
src/resource-limiter/memory/lowmem.h

index aaa4b9a..1409a91 100644 (file)
@@ -26,8 +26,9 @@
 #include <assert.h>
 #include <stdbool.h>
 
+#include "lowmem.h"
 #include "trace.h"
-#include "lowmem-governor.h"
+#include "module.h"
 
 static int compare_victims(const struct task_info **ta,
                        const struct task_info **tb,
@@ -82,3 +83,23 @@ int lowmem_governor_get_kill_candidates(GArray *candidates,
 
        return candidates->len;
 }
+
+static int lowmem_governor_initialize(void *data)
+{
+       lowmem_initialize_governor_ops(lowmem_governor_get_kill_candidates);
+       return RESOURCED_ERROR_NONE;
+}
+
+static int lowmem_governor_finalize(void *data)
+{
+       return RESOURCED_ERROR_NONE;
+}
+
+static struct module_ops lowmem_governor_module_ops = {
+       .priority       = MODULE_PRIORITY_INITIAL,
+       .name           = "lowmem-governor",
+       .init           = lowmem_governor_initialize,
+       .exit           = lowmem_governor_finalize,
+};
+
+MODULE_REGISTER(&lowmem_governor_module_ops)
diff --git a/src/resource-limiter/memory/lowmem-governor.h b/src/resource-limiter/memory/lowmem-governor.h
deleted file mode 100644 (file)
index 86882c0..0000000
+++ /dev/null
@@ -1,63 +0,0 @@
-/*
- * resourced
- *
- * 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.
- *
- */
-
-/**
- * @file lowmem-governor.h
- * @desc Function and structure for governor feature.
- **/
-
-#ifndef __LOWMEM_GOVERNOR_H__
-#define __LOWMEM_GOVERNOR_H__
-
-#include <stdbool.h>
-
-struct task_info {
-       /**
-        * Mostly, there are not multiple processes with the same pgid.
-        * So, for the frequent case, we use pid variable to avoid
-        * allocating arrays.
-        */
-       pid_t pid;
-       GArray *pids;
-       pid_t pgid;
-       int oom_score_adj; /* equal to /proc/<pid>/oom_score_adj */
-       /**
-        * oom_score_lru is equal to oom_score_adj
-        * or adjusted by proc_app_info's lru_state
-        * for apps that are marked as favourite.
-        * It can be used to compare between apps/procs for LMK.
-        */
-       int oom_score_lru;
-       int size;
-       /**
-        * proc_app_info_oom_killed and proc_app_info_flags
-        * are not used if task is not an app.
-        * Especially, proc_app_info_oom_killed is NULL when this task
-        * is not an app
-        */
-       bool *proc_app_info_oom_killed;
-       int proc_app_info_flags;
-};
-
-int lowmem_governor_get_kill_candidates(GArray *candidates,
-                                       GArray *task_info_app_array,
-                                       GArray *task_info_proc_array,
-                                       unsigned long totalram_kb);
-
-#endif /* __LOWMEM_GOVERNOR_H__ */
index dd12fa5..b1d7bd5 100644 (file)
@@ -630,6 +630,17 @@ static GArray *lowmem_get_task_info_proc()
        return lowmem_task_info_proc_array;
 }
 
+struct lowmem_governor_ops {
+       int(*get_kill_candidates)(GArray *, GArray *, GArray *, unsigned long);
+};
+
+static struct lowmem_governor_ops governor_ops = { NULL };
+void lowmem_initialize_governor_ops(int(*get_kill_candidates)(GArray *,
+                                       GArray *, GArray *, unsigned long))
+{
+       governor_ops.get_kill_candidates = get_kill_candidates;
+}
+
 /**
  * @brief Terminate up to max_victims processes after finding them from pai.
        It depends on proc_app_info lists
@@ -670,10 +681,12 @@ static int lowmem_kill_victims(int max_victims,
        if (!lowmem_kill_candidates)
                lowmem_kill_candidates = g_array_new(false, false, sizeof(struct task_info *));
 
-       candidates_cnt = lowmem_governor_get_kill_candidates(lowmem_kill_candidates,
-                                                       task_info_app_array,
-                                                       task_info_proc_array,
-                                                       totalram_kb);
+       assert(governor_ops.get_kill_candidates != NULL);
+       candidates_cnt = governor_ops.get_kill_candidates(
+                                               lowmem_kill_candidates,
+                                               task_info_app_array,
+                                               task_info_proc_array,
+                                               totalram_kb);
 
        _D("[LMK] candidates_cnt=%d", candidates_cnt);
        if (candidates_cnt <= 0) {
index 33ec0a8..e365c2f 100644 (file)
@@ -30,7 +30,6 @@
 #include <memory-cgroup.h>
 
 #include "fd-handler.h"
-#include "lowmem-governor.h"
 
 #ifdef __cplusplus
 extern "C" {
@@ -38,6 +37,34 @@ extern "C" {
 
 #define MAX_MEMORY_CGROUP_VICTIMS      10
 
+struct task_info {
+       /**
+        * Mostly, there are not multiple processes with the same pgid.
+        * So, for the frequent case, we use pid variable to avoid
+        * allocating arrays.
+        */
+       pid_t pid;
+       GArray *pids;
+       pid_t pgid;
+       int oom_score_adj; /* equal to /proc/<pid>/oom_score_adj */
+       /**
+        * oom_score_lru is equal to oom_score_adj
+        * or adjusted by proc_app_info's lru_state
+        * for apps that are marked as favourite.
+        * It can be used to compare between apps/procs for LMK.
+        */
+       int oom_score_lru;
+       int size;
+       /**
+        * proc_app_info_oom_killed and proc_app_info_flags
+        * are not used if task is not an app.
+        * Especially, proc_app_info_oom_killed is NULL when this task
+        * is not an app
+        */
+       bool *proc_app_info_oom_killed;
+       int proc_app_info_flags;
+};
+
 unsigned int lowmem_get_task_mem_usage_rss(const struct task_info *tsk);
 void lowmem_trigger_swap(pid_t pid, char *path, bool move);
 int lowmem_trigger_reclaim(int flags, int victims, enum oom_score score, int threshold);
@@ -90,6 +117,9 @@ enum {
        LOWMEM_RECLAIM_NEXT_TYPE
 };
 
+void lowmem_initialize_governor_ops(int(*)(GArray *, GArray *, GArray *,
+                                       unsigned long));
+
 #ifdef __cplusplus
 }
 #endif /* __cplusplus */