From db5ec6c73c99ce3cc79c938ecaa7acc8bf2b3750 Mon Sep 17 00:00:00 2001 From: SangYoun Kwak Date: Wed, 29 Mar 2023 19:23:37 +0900 Subject: [PATCH] lowmem-governor: Modify governor as a module 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 --- src/resource-limiter/memory/lowmem-governor.c | 23 +++++++++- src/resource-limiter/memory/lowmem-governor.h | 63 --------------------------- src/resource-limiter/memory/lowmem.c | 21 +++++++-- src/resource-limiter/memory/lowmem.h | 32 +++++++++++++- 4 files changed, 70 insertions(+), 69 deletions(-) delete mode 100644 src/resource-limiter/memory/lowmem-governor.h diff --git a/src/resource-limiter/memory/lowmem-governor.c b/src/resource-limiter/memory/lowmem-governor.c index aaa4b9a..1409a91 100644 --- a/src/resource-limiter/memory/lowmem-governor.c +++ b/src/resource-limiter/memory/lowmem-governor.c @@ -26,8 +26,9 @@ #include #include +#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 index 86882c0..0000000 --- a/src/resource-limiter/memory/lowmem-governor.h +++ /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 - -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//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__ */ diff --git a/src/resource-limiter/memory/lowmem.c b/src/resource-limiter/memory/lowmem.c index dd12fa5..b1d7bd5 100644 --- a/src/resource-limiter/memory/lowmem.c +++ b/src/resource-limiter/memory/lowmem.c @@ -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) { diff --git a/src/resource-limiter/memory/lowmem.h b/src/resource-limiter/memory/lowmem.h index 33ec0a8..e365c2f 100644 --- a/src/resource-limiter/memory/lowmem.h +++ b/src/resource-limiter/memory/lowmem.h @@ -30,7 +30,6 @@ #include #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//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 */ -- 2.7.4