From d97656a8b99f4fa95a9c15abfbac777a94b84d55 Mon Sep 17 00:00:00 2001
From: jikai <jikai11@huawei.com>
Date: Mon, 20 May 2024 08:48:00 +0000
Subject: [PATCH 092/108] isolate oom monitor codes

Signed-off-by: jikai <jikai11@huawei.com>
---
 cmake/options.cmake                                |  7 +++++++
 src/daemon/common/cgroup/cgroup.c                  |  2 ++
 src/daemon/common/cgroup/cgroup.h                  |  2 ++
 src/daemon/common/cgroup/cgroup_common.h           |  4 ++++
 src/daemon/common/cgroup/cgroup_v1.c               |  8 ++++++++
 src/daemon/common/cgroup/cgroup_v2.c               |  8 ++++++++
 src/daemon/common/cri/v1/v1_cri_helpers.cc         |  2 ++
 src/daemon/modules/container/container_state.c     |  2 ++
 .../modules/container/supervisor/supervisor.c      | 14 ++++++++++++++
 9 files changed, 49 insertions(+)

diff --git a/cmake/options.cmake b/cmake/options.cmake
index a15b8194..5b17f631 100644
--- a/cmake/options.cmake
+++ b/cmake/options.cmake
@@ -58,6 +58,13 @@ if (ENABLE_SANDBOXER STREQUAL "ON")
     message("${Green}--  Enable sandbox API${ColourReset}")
 endif()
 
+option(ENABLE_OOM_MONITOR "Enable oom monitor" ON)
+IF (ENABLE_OOM_MONITOR STREQUAL "ON")
+    add_definitions(-DENABLE_OOM_MONITOR)
+    set(ENABLE_OOM_MONITOR 1)
+    message("${Green}--  Enable oom monitor${ColourReset}")
+endif()
+
 option(ENABLE_SYSTEMD_NOTIFY "Enable systemd notify" ON)
 if (ENABLE_SYSTEMD_NOTIFY STREQUAL "ON")
     add_definitions(-DSYSTEMD_NOTIFY)
diff --git a/src/daemon/common/cgroup/cgroup.c b/src/daemon/common/cgroup/cgroup.c
index 71bf9801..77fafdae 100644
--- a/src/daemon/common/cgroup/cgroup.c
+++ b/src/daemon/common/cgroup/cgroup.c
@@ -197,6 +197,7 @@ char *common_convert_cgroup_path(const char *cgroup_path)
     return util_strdup_s(result);
 }
 
+#ifdef ENABLE_OOM_MONITOR
 cgroup_oom_handler_info_t *common_get_cgroup_oom_handler(int fd, const char *name, const char *cgroup_path,
                                                          const char *exit_fifo)
 {
@@ -225,3 +226,4 @@ void common_free_cgroup_oom_handler_info(cgroup_oom_handler_info_t *info)
     free(info->cgroup_memory_event_path);
     free(info);
 }
+#endif
diff --git a/src/daemon/common/cgroup/cgroup.h b/src/daemon/common/cgroup/cgroup.h
index 0bbb70a0..1ebbfa98 100644
--- a/src/daemon/common/cgroup/cgroup.h
+++ b/src/daemon/common/cgroup/cgroup.h
@@ -43,9 +43,11 @@ char *common_get_own_cgroup_path(const char *subsystem);
 
 char *common_convert_cgroup_path(const char *cgroup_path);
 
+#ifdef ENABLE_OOM_MONITOR
 cgroup_oom_handler_info_t *common_get_cgroup_oom_handler(int fd, const char *name, const char *cgroup_path,
                                                          const char *exit_fifo);
 void common_free_cgroup_oom_handler_info(cgroup_oom_handler_info_t *info);
+#endif
 
 #ifdef __cplusplus
 }
diff --git a/src/daemon/common/cgroup/cgroup_common.h b/src/daemon/common/cgroup/cgroup_common.h
index 46a7de50..01fc669c 100644
--- a/src/daemon/common/cgroup/cgroup_common.h
+++ b/src/daemon/common/cgroup/cgroup_common.h
@@ -116,6 +116,7 @@ typedef struct {
     cgroup_pids_metrics_t cgpids_metrics;
 } cgroup_metrics_t;
 
+#ifdef ENABLE_OOM_MONITOR
 #define CGROUP_OOM_HANDLE_CONTINUE false
 #define CGROUP_OOM_HANDLE_CLOSE true
 
@@ -126,6 +127,7 @@ typedef struct _cgroup_oom_handler_info_t {
     char *cgroup_memory_event_path;
     bool (*oom_event_handler)(int, void *);
 } cgroup_oom_handler_info_t;
+#endif
 
 typedef struct {
     int (*get_cgroup_version)(void);
@@ -140,8 +142,10 @@ typedef struct {
     char *(*get_init_cgroup_path)(const char *subsystem);
     char *(*get_own_cgroup_path)(const char *subsystem);
 
+#ifdef ENABLE_OOM_MONITOR
     cgroup_oom_handler_info_t *(*get_cgroup_oom_handler)(int fd, const char *name, const char *cgroup_path,
                                                          const char *exit_fifo);
+#endif
 } cgroup_ops;
 
 #ifdef __cplusplus
diff --git a/src/daemon/common/cgroup/cgroup_v1.c b/src/daemon/common/cgroup/cgroup_v1.c
index 45b1d096..018336ea 100644
--- a/src/daemon/common/cgroup/cgroup_v1.c
+++ b/src/daemon/common/cgroup/cgroup_v1.c
@@ -20,12 +20,16 @@
 
 #include <stdio.h>
 #include <stdlib.h>
+#ifdef ENABLE_OOM_MONITOR
 #include <sys/eventfd.h>
+#endif
 
 #include "utils.h"
 #include "sysinfo.h"
 #include "err_msg.h"
+#ifdef ENABLE_OOM_MONITOR
 #include "events_sender_api.h"
+#endif
 
 #define CGROUP_HUGETLB_LIMIT "hugetlb.%s.limit_in_bytes"
 #define CGROUP_MOUNT_PATH_PREFIX "/sys/fs/cgroup/"
@@ -1052,6 +1056,7 @@ static char *common_get_cgroup_path(const char *path, const char *subsystem)
     return res;
 }
 
+#ifdef ENABLE_OOM_MONITOR
 static bool oom_cb_cgroup_v1(int fd, void *cbdata)
 {
     cgroup_oom_handler_info_t *info = (cgroup_oom_handler_info_t *)cbdata;
@@ -1205,6 +1210,7 @@ cleanup:
     common_free_cgroup_oom_handler_info(info);
     return NULL;
 }
+#endif
 
 char *get_init_cgroup_path_v1(const char *subsystem)
 {
@@ -1232,6 +1238,8 @@ int cgroup_v1_ops_init(cgroup_ops *ops)
     ops->get_cgroup_mnt_and_root_path = get_cgroup_mnt_and_root_path_v1;
     ops->get_init_cgroup_path = get_init_cgroup_path_v1;
     ops->get_own_cgroup_path = get_own_cgroup_v1;
+#ifdef ENABLE_OOM_MONITOR
     ops->get_cgroup_oom_handler = get_cgroup_oom_handler_v1;
+#endif
     return 0;
 }
\ No newline at end of file
diff --git a/src/daemon/common/cgroup/cgroup_v2.c b/src/daemon/common/cgroup/cgroup_v2.c
index 76754dc1..ce72e6c4 100644
--- a/src/daemon/common/cgroup/cgroup_v2.c
+++ b/src/daemon/common/cgroup/cgroup_v2.c
@@ -17,14 +17,18 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <sys/stat.h>
+#ifdef ENABLE_OOM_MONITOR
 #include <sys/inotify.h>
+#endif
 
 #include <isula_libutils/auto_cleanup.h>
 
 #include "utils.h"
 #include "path.h"
 #include "sysinfo.h"
+#ifdef ENABLE_OOM_MONITOR
 #include "events_sender_api.h"
+#endif
 
 // Cgroup V2 Item Definition
 #define CGROUP2_CPU_WEIGHT "cpu.weight"
@@ -416,6 +420,7 @@ static int get_cgroup_mnt_and_root_v2(const char *subsystem, char **mountpoint,
     return 0;
 }
 
+#ifdef ENABLE_OOM_MONITOR
 static bool oom_cb_cgroup_v2(int fd, void *cbdata)
 {
     const size_t events_size = sizeof(struct inotify_event) + NAME_MAX + 1;
@@ -547,6 +552,7 @@ cleanup:
     common_free_cgroup_oom_handler_info(info);
     return NULL;
 }
+#endif
 
 int get_cgroup_version_v2()
 {
@@ -562,6 +568,8 @@ int cgroup_v2_ops_init(cgroup_ops *ops)
     ops->get_cgroup_info = get_cgroup_info_v2;
     ops->get_cgroup_metrics = get_cgroup_metrics_v2;
     ops->get_cgroup_mnt_and_root_path = get_cgroup_mnt_and_root_v2;
+#ifdef ENABLE_OOM_MONITOR
     ops->get_cgroup_oom_handler = get_cgroup_oom_handler_v2;
+#endif
     return 0;
 }
\ No newline at end of file
diff --git a/src/daemon/common/cri/v1/v1_cri_helpers.cc b/src/daemon/common/cri/v1/v1_cri_helpers.cc
index 478dd105..31b6b137 100644
--- a/src/daemon/common/cri/v1/v1_cri_helpers.cc
+++ b/src/daemon/common/cri/v1/v1_cri_helpers.cc
@@ -517,9 +517,11 @@ void UpdateBaseStatusFromInspect(
         } else { // Case 3
             state = runtime::v1::CONTAINER_CREATED;
         }
+#ifdef ENABLE_OOM_MONITOR
         if (inspect->state->oom_killed == true) {
             reason = "OOMKilled";
         }
+#endif
         if (inspect->state->error != nullptr) {
             message = inspect->state->error;
         }
diff --git a/src/daemon/modules/container/container_state.c b/src/daemon/modules/container/container_state.c
index 452a2b26..f8ad0537 100644
--- a/src/daemon/modules/container/container_state.c
+++ b/src/daemon/modules/container/container_state.c
@@ -587,7 +587,9 @@ container_inspect_state *container_state_to_inspect_state(container_state_t *s)
     state->running = s->state->running;
     state->paused = s->state->paused;
     state->restarting = s->state->restarting;
+#ifdef ENABLE_OOM_MONITOR
     state->oom_killed = s->state->oom_killed;
+#endif
     state->pid = s->state->pid;
 
     state->exit_code = s->state->exit_code;
diff --git a/src/daemon/modules/container/supervisor/supervisor.c b/src/daemon/modules/container/supervisor/supervisor.c
index 39d9fdb8..294783eb 100644
--- a/src/daemon/modules/container/supervisor/supervisor.c
+++ b/src/daemon/modules/container/supervisor/supervisor.c
@@ -42,8 +42,10 @@
 #ifdef ENABLE_CRI_API_V1
 #include "sandbox_ops.h"
 #endif
+#ifdef ENABLE_OOM_MONITOR
 #include "cgroup.h"
 #include "specs_api.h"
+#endif
 
 pthread_mutex_t g_supervisor_lock = PTHREAD_MUTEX_INITIALIZER;
 struct epoll_descr g_supervisor_descr;
@@ -286,6 +288,7 @@ static int supervisor_exit_cb(int fd, uint32_t events, void *cbdata, struct epol
     return EPOLL_LOOP_HANDLE_CONTINUE;
 }
 
+#ifdef ENABLE_OOM_MONITOR
 static int oom_handle_cb(int fd, uint32_t events, void *cbdata, struct epoll_descr *descr)
 {
     cgroup_oom_handler_info_t *oom_handler_info = (cgroup_oom_handler_info_t *)cbdata;
@@ -305,6 +308,7 @@ static int oom_handle_cb(int fd, uint32_t events, void *cbdata, struct epoll_des
 
     return EPOLL_LOOP_HANDLE_CONTINUE;
 }
+#endif
 
 /* supervisor add exit monitor */
 int container_supervisor_add_exit_monitor(int fd, const char *exit_fifo, const pid_ppid_info_t *pid_info,
@@ -312,8 +316,10 @@ int container_supervisor_add_exit_monitor(int fd, const char *exit_fifo, const p
 {
     int ret = 0;
     struct supervisor_handler_data *data = NULL;
+#ifdef ENABLE_OOM_MONITOR
     cgroup_oom_handler_info_t *oom_handler_info = NULL;
     __isula_auto_free char *cgroup_path = NULL;
+#endif
 
     if (fd < 0) {
         ERROR("Invalid exit fifo fd");
@@ -326,12 +332,14 @@ int container_supervisor_add_exit_monitor(int fd, const char *exit_fifo, const p
         return -1;
     }
 
+#ifdef ENABLE_OOM_MONITOR
     cgroup_path = merge_container_cgroups_path(cont->common_config->id, cont->hostconfig);
     if (cgroup_path == NULL) {
         ERROR("Failed to get cgroup path");
         close(fd);
         return -1;
     }
+#endif
 
     data = util_common_calloc_s(sizeof(struct supervisor_handler_data));
     if (data == NULL) {
@@ -353,9 +361,12 @@ int container_supervisor_add_exit_monitor(int fd, const char *exit_fifo, const p
     data->pid_info.start_time = pid_info->start_time;
     data->pid_info.ppid = pid_info->ppid;
     data->pid_info.pstart_time = pid_info->pstart_time;
+#ifdef ENABLE_OOM_MONITOR
     oom_handler_info = common_get_cgroup_oom_handler(fd, cont->common_config->id, cgroup_path, exit_fifo);
+#endif
 
     supervisor_handler_lock();
+#ifdef ENABLE_OOM_MONITOR
     if (oom_handler_info != NULL) {
         ret = epoll_loop_add_handler(&g_supervisor_descr, oom_handler_info->oom_event_fd, oom_handle_cb, oom_handler_info);
         if (ret != 0) {
@@ -363,6 +374,7 @@ int container_supervisor_add_exit_monitor(int fd, const char *exit_fifo, const p
             goto err;
         }
     }
+#endif
 
     ret = epoll_loop_add_handler(&g_supervisor_descr, fd, supervisor_exit_cb, data);
     if (ret != 0) {
@@ -374,7 +386,9 @@ int container_supervisor_add_exit_monitor(int fd, const char *exit_fifo, const p
 
 err:
     supervisor_handler_data_free(data);
+#ifdef ENABLE_OOM_MONITOR
     common_free_cgroup_oom_handler_info(oom_handler_info);
+#endif
 out:
     supervisor_handler_unlock();
     return ret;
-- 
2.25.1

