lowmem-monitor: Add a module for PSI and vmpressure 08/297508/1 accepted/tizen/unified/20230822.162228
authorSangYoun Kwak <sy.kwak@samsung.com>
Mon, 21 Aug 2023 05:57:25 +0000 (14:57 +0900)
committerSangYoun Kwak <sy.kwak@samsung.com>
Mon, 21 Aug 2023 05:57:25 +0000 (14:57 +0900)
A new module "lowmem-monitor" is added:
 - Initialize monitor: PSI or vmpressure
    - Use PSI if the PSI initialization was successful.
    - Use vmpressure otherwise.

Change-Id: I37d9b223698d3742dd4a02b27c94f65852810fb4
Signed-off-by: SangYoun Kwak <sy.kwak@samsung.com>
src/resource-limiter/memory/lowmem-monitor-psi.c
src/resource-limiter/memory/lowmem-monitor-psi.h [new file with mode: 0644]
src/resource-limiter/memory/lowmem-monitor-vmpressure.c
src/resource-limiter/memory/lowmem-monitor-vmpressure.h [new file with mode: 0644]
src/resource-limiter/memory/lowmem-monitor.c [new file with mode: 0644]

index a18fb6e..696036a 100644 (file)
@@ -364,14 +364,12 @@ static void unregister_psi_events(void)
        g_psi_monitor_epoll_fd = -1;
 }
 
-static int lowmem_monitor_psi_initialize(void *data)
+/**
+ * FIXME: Modify lowmem-monitor-psi as a module and register functions to
+ * lowmem-monitor module.
+ */
+int lowmem_monitor_psi_initialize(void *data)
 {
-       /**
-        * FIXME: Disable PSI monitoring feature temporarily.
-        * Enable it when the PSI monitoring is ready to apply.
-        */
-       return RESOURCED_ERROR_NONE;
-
        if (register_psi_events() != RESOURCED_ERROR_NONE) {
                _E("Failed to register psi fds to epoll fd");
                return RESOURCED_ERROR_FAIL;
@@ -386,25 +384,10 @@ static int lowmem_monitor_psi_initialize(void *data)
        return RESOURCED_ERROR_NONE;
 }
 
-static int lowmem_monitor_psi_finalize(void *data)
+int lowmem_monitor_psi_finalize(void *data)
 {
-       /**
-        * FIXME: Disable PSI monitoring feature temporarily.
-        * Enable it when the PSI monitoring is ready to apply.
-        */
-       return RESOURCED_ERROR_NONE;
-
        destroy_psi_monitor_thread();
        unregister_psi_events();
 
        return RESOURCED_ERROR_NONE;
 }
-
-static struct module_ops g_lowmem_monitor_psi_modules_ops = {
-       .priority       = MODULE_PRIORITY_INITIAL,
-       .name           = "lowmem-monitor-psi",
-       .init           = lowmem_monitor_psi_initialize,
-       .exit           = lowmem_monitor_psi_finalize,
-};
-
-MODULE_REGISTER(&g_lowmem_monitor_psi_modules_ops)
diff --git a/src/resource-limiter/memory/lowmem-monitor-psi.h b/src/resource-limiter/memory/lowmem-monitor-psi.h
new file mode 100644 (file)
index 0000000..2db6049
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+ * 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-monitor-psi.h
+ * @desc Functions for memory monitoring using PSI.
+ **/
+
+/**
+ * FIXME: Modify lowmem-monitor-psi as a module and register functions to
+ * lowmem-monitor module.
+ */
+
+#ifndef __LOWMEM_MONITOR_PSI_H__
+#define __LOWMEM_MONITOR_PSI_H__
+
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
+int lowmem_monitor_psi_initialize(void *data);
+int lowmem_monitor_psi_finalize(void *data);
+
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+
+#endif /* __LOWMEM_MONITOR_PSI_H__ */
index 6d1edb4..8867ca1 100644 (file)
@@ -110,7 +110,11 @@ static int lowmem_monitor_pressure_register_eventfd(struct memcg_info *mi)
        return 0;
 }
 
-static int lowmem_monitor_initialize(void *data)
+/**
+ * FIXME: Modify lowmem-monitor-vmpressure as a module and register functions to
+ * lowmem-monitor module.
+ */
+int lowmem_monitor_vmpressure_initialize(void *data)
 {
        unsigned int i;
 
@@ -123,16 +127,7 @@ static int lowmem_monitor_initialize(void *data)
        return RESOURCED_ERROR_NONE;
 }
 
-static int lowmem_monitor_finalize(void *data)
+int lowmem_monitor_vmpressure_finalize(void *data)
 {
        return RESOURCED_ERROR_NONE;
 }
-
-static struct module_ops lowmem_monitor_vmpressure_modules_ops = {
-       .priority       = MODULE_PRIORITY_INITIAL,
-       .name           = "lowmem-monitor-vmpressure",
-       .init           = lowmem_monitor_initialize,
-       .exit           = lowmem_monitor_finalize,
-};
-
-MODULE_REGISTER(&lowmem_monitor_vmpressure_modules_ops)
diff --git a/src/resource-limiter/memory/lowmem-monitor-vmpressure.h b/src/resource-limiter/memory/lowmem-monitor-vmpressure.h
new file mode 100644 (file)
index 0000000..f46387d
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+ * 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-monitor-vmpressure.h
+ * @desc Functions for memory monitoring using vmpressure(cgroup).
+ **/
+
+/**
+ * FIXME: Modify lowmem-monitor-vmpressure as a module and register functions to
+ * lowmem-monitor module.
+ */
+
+#ifndef __LOWMEM_MONITOR_VMPRESSURE_H__
+#define __LOWMEM_MONITOR_VMPRESSURE_H__
+
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
+int lowmem_monitor_vmpressure_initialize(void *data);
+int lowmem_monitor_vmpressure_finalize(void *data);
+
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+
+#endif /* __LOWMEM_MONITOR_VMPRESSURE_H__ */
diff --git a/src/resource-limiter/memory/lowmem-monitor.c b/src/resource-limiter/memory/lowmem-monitor.c
new file mode 100644 (file)
index 0000000..eb10b54
--- /dev/null
@@ -0,0 +1,92 @@
+/**
+ * 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-monitor.c
+ * @desc Provides monitor functionalities to detect lowmem state
+ *       PSI is used as a default option. If PSI is not supported, then
+ *       vmpressure will be used.
+ */
+
+#include "resourced.h"
+#include "trace.h"
+#include "module.h"
+#include "lowmem-monitor-psi.h"
+#include "lowmem-monitor-vmpressure.h"
+
+enum lowmem_monitor_method {
+       LOWMEM_MONITOR_NONE,
+       LOWMEM_MONITOR_PSI,
+       LOWMEM_MONITOR_VMPRESSURE,
+};
+
+static enum lowmem_monitor_method g_monitor_method = LOWMEM_MONITOR_NONE;
+
+static int lowmem_monitor_initialize(void *data)
+{
+       int ret = 0;
+
+       g_monitor_method = LOWMEM_MONITOR_NONE;
+
+       ret = lowmem_monitor_psi_initialize(NULL);
+       if (ret == RESOURCED_ERROR_NONE) {
+               g_monitor_method = LOWMEM_MONITOR_PSI;
+               _I("lowmem monitor method is PSI");
+               return RESOURCED_ERROR_NONE;
+       }
+
+       _W("Failed to initialize PSI monitor, try vmpressure");
+
+       ret = lowmem_monitor_vmpressure_initialize(NULL);
+       if (ret == RESOURCED_ERROR_NONE) {
+               g_monitor_method = LOWMEM_MONITOR_VMPRESSURE;
+               _I("lowmem monitor method is vmpressure");
+               return RESOURCED_ERROR_NONE;
+       }
+
+       _E("Failed to initialize lowmem-monitor");
+
+       return RESOURCED_ERROR_FAIL;
+}
+
+static int lowmem_monitor_finalize(void *data)
+{
+       switch (g_monitor_method) {
+       case LOWMEM_MONITOR_NONE:
+               _E("Failed to finalize lowmem-monitor: no monitor");
+               return RESOURCED_ERROR_FAIL;
+       case LOWMEM_MONITOR_PSI:
+               return lowmem_monitor_psi_finalize(NULL);
+       case LOWMEM_MONITOR_VMPRESSURE:
+               return lowmem_monitor_vmpressure_finalize(NULL);
+       default:
+               _E("Unknown lowmem monitor method: %d", g_monitor_method);
+               break;
+       }
+
+       return RESOURCED_ERROR_FAIL;
+}
+
+static struct module_ops g_lowmem_monitor_module_ops = {
+       .priority       = MODULE_PRIORITY_INITIAL,
+       .name           = "lowmem-monitor",
+       .init           = lowmem_monitor_initialize,
+       .exit           = lowmem_monitor_finalize,
+};
+
+MODULE_REGISTER(&g_lowmem_monitor_module_ops)