Move cpu hotplug 68/275068/1
authorUnsung Lee <unsung.lee@samsung.com>
Mon, 16 May 2022 08:08:47 +0000 (17:08 +0900)
committerUnsung Lee <unsung.lee@samsung.com>
Mon, 16 May 2022 08:09:38 +0000 (17:09 +0900)
from limiter to optimizer

Change-Id: I4d816c6890bb418fe0532e34f2fa3a88de9d5d33
Signed-off-by: Unsung Lee <unsung.lee@samsung.com>
src/CMakeLists.txt
src/resource-limiter/cpu/cpu-hotplug.c [deleted file]
src/resource-limiter/cpu/cpu-hotplug.h [deleted file]
src/resource-optimizer/cpu/cpu-hotplug.c [new file with mode: 0644]
src/resource-optimizer/cpu/cpu-hotplug.h [new file with mode: 0644]

index 47a3ce4d404df4a97b58422afb9ab2893eb3b234..032be941ff0d6e94a1ebfce30d8d975bbd31bd18 100644 (file)
@@ -154,6 +154,7 @@ TARGET_LINK_LIBRARIES(compaction resourced-private-api ${RESOURCED_REQUIRE_PKGS_
 INSTALL(TARGETS compaction DESTINATION ${MAKE_INSTALL_PREFIX}${RD_PLUGIN_PATH})
 
 ADD_LIBRARY(cpu-sched MODULE
+       ${CPU_RESOURCE_OPTIMIZER_SOURCE_DIR}/cpu-hotplug.c
        ${CPU_RESOURCE_OPTIMIZER_SOURCE_DIR}/cpu-sched.c)
 TARGET_LINK_LIBRARIES(cpu-sched resourced-private-api ${RESOURCED_REQUIRE_PKGS_LDFLAGS})
 INSTALL(TARGETS cpu-sched DESTINATION ${MAKE_INSTALL_PREFIX}${RD_PLUGIN_PATH})
diff --git a/src/resource-limiter/cpu/cpu-hotplug.c b/src/resource-limiter/cpu/cpu-hotplug.c
deleted file mode 100644 (file)
index b9c7db6..0000000
+++ /dev/null
@@ -1,232 +0,0 @@
-#include <cpu-hotplug.h>
-#include <libudev.h>
-#include <stdbool.h>
-#include "trace.h"
-#include "fd-handler.h"
-#include "notifier.h"
-#include "file-helper.h"
-
-#define UDEV_MONITOR_BUFFER_SIZE (512 * 1024)
-
-struct cpu_hotplug {
-       struct udev *udev;
-       struct udev_monitor *mon;
-       int fd;
-       fd_handler_h io_handler;
-       bool is_initialized;
-};
-
-static struct cpu_hotplug ch;
-
-static int get_core_status(int core_id)
-{
-       char buf[128];
-       int32_t status = 0;
-
-       if (snprintf(buf, sizeof buf, "/sys/devices/system/cpu/cpu%d/online", core_id) < 0)
-               return -1;
-
-       if (fread_int(buf, &status) != RESOURCED_ERROR_NONE)
-               return -1;
-
-       return status;
-}
-
-static void handle_event(struct udev_device *dev)
-{
-       int cpu_id;
-       const char *action = NULL;
-       int status = 0;
-
-       if (NULL == dev)
-               return;
-
-       /* get cpu id from syspath */
-       if (sscanf(udev_device_get_syspath(dev), "/sys/devices/system/cpu/cpu%d", &cpu_id) != 1)
-               return;
-
-       action = udev_device_get_action(dev);
-
-       /* action == NULL means that a cpu has been enumerated on startup, check if it's on or off */
-       if (NULL == action) {
-               status = get_core_status(cpu_id);
-               if (status < 0) {
-                       _E("cpu hotplug: could not determine core status (%d)", cpu_id);
-                       return;
-               }
-       } else if (!strcmp(action, "online")) {
-               status = 1;
-       } else if (!strcmp(action, "offline")) {
-               status = 0;
-       }
-
-       resourced_notify((status == 1) ? RESOURCED_NOTIFIER_CPU_ON : RESOURCED_NOTIFIER_CPU_OFF, &cpu_id);
-}
-
-static bool udev_io_handler(int fd, void *user_data)
-{
-       struct udev_monitor *mon = (struct udev_monitor *)user_data;
-       struct udev_device *dev;
-
-       assert(mon);
-       assert(fd >= 0);
-
-       dev = udev_monitor_receive_device(mon);
-       handle_event(dev);
-       udev_device_unref(dev);
-       return true;
-}
-
-static int monitor_cpus()
-{
-       int ret = 0;
-
-       if (false == ch.is_initialized)
-               return 0;
-
-       ch.mon = udev_monitor_new_from_netlink(ch.udev, "kernel");
-       if (NULL == ch.mon) {
-               _E("cpu-hotplug: creating device monitor failed");
-               return -1;
-       }
-
-       if (udev_monitor_set_receive_buffer_size(ch.mon, UDEV_MONITOR_BUFFER_SIZE) < 0) {
-               _E("cpu-hotplug: change monitor buffer size failed");
-               ret = -1;
-               goto monitor_failed;
-       }
-
-       if (udev_monitor_filter_add_match_subsystem_devtype(ch.mon, "cpu", NULL) < 0) {
-               _E("cpu-hotplug: creating monitor add match subsystem failed");
-               ret = -1;
-               goto monitor_failed;
-       }
-
-       if (udev_monitor_enable_receiving(ch.mon) < 0) {
-               _E("cpu-hotplug: monitor: enable receiving failed");
-               ret = -1;
-               goto monitor_failed;
-       }
-
-       ch.fd = udev_monitor_get_fd(ch.mon);
-       if (ch.fd < 0) {
-               _E("cpu-hotplug: monitor: could not setup fd");
-               ret = -1;
-               goto monitor_failed;
-       }
-
-       if (add_fd_read_handler(ch.fd, udev_io_handler, ch.mon, NULL, &(ch.io_handler)) < 0) {
-               _E("cpu-hotplug: could not add fd read handler");
-               ret = -1;
-               goto monitor_failed;
-       }
-       return ret;
-
-monitor_failed:
-       ch.io_handler = NULL;
-       ch.mon = udev_monitor_unref(ch.mon);
-
-       /* udev_monitor_unref() should take care of closing fd */
-       ch.fd = -1;
-       return ret;
-}
-
-static int enumerate_cpus()
-{
-       struct udev_enumerate *enumerate;
-       struct udev_device *dev;
-       struct udev_list_entry *devices, *entry;
-       int ret = 0;
-
-       if (false == ch.is_initialized)
-               return 0;
-
-       enumerate = udev_enumerate_new(ch.udev);
-
-       if (NULL == enumerate) {
-               _E("cpu-hotplug: could not create enumerate context");
-               return -1;
-       }
-
-       if (udev_enumerate_add_match_subsystem(enumerate, "cpu") < 0) {
-               _E("cpu-hotplug: could not add match subsystem");
-               ret = -1;
-               goto enumerate_quit;
-       }
-
-       if (udev_enumerate_scan_devices(enumerate) < 0) {
-               _E("cpu-hotplug: could not scan devices");
-               ret = -1;
-               goto enumerate_quit;
-       }
-
-       devices = udev_enumerate_get_list_entry(enumerate);
-       if (NULL == devices) {
-               _E("cpu-hotplug: could not enumerate devices");
-               ret = -1;
-               goto enumerate_quit;
-       }
-
-       udev_list_entry_foreach(entry, devices) {
-               dev = udev_device_new_from_syspath(ch.udev, udev_list_entry_get_name(entry));
-               handle_event(dev);
-               udev_device_unref(dev);
-       }
-
-enumerate_quit:
-       udev_enumerate_unref(enumerate);
-       return ret;
-}
-
-int cpu_hotplug_init()
-{
-       if (true == ch.is_initialized) {
-               _W("cpu hotplug monitor: already initialized");
-               return -1;
-       }
-
-       ch.udev = udev_new();
-       if (NULL == ch.udev) {
-               _E("cpu hotplug monitor: error creating udev handle");
-               return -1;
-       }
-
-       ch.is_initialized = true;
-
-       if (enumerate_cpus() < 0) {
-               _E("cpu-hotplug: enumerate failed");
-               goto init_failed;
-       }
-
-       if (monitor_cpus() < 0) {
-               _E("cpu-hotplug: monitoring start failed");
-               goto init_failed;
-       }
-
-       return 0;
-
-init_failed:
-       if (NULL != ch.mon)
-               ch.mon = udev_monitor_unref(ch.mon);
-
-       ch.is_initialized = false;
-       if (NULL != ch.udev)
-               ch.udev = udev_unref(ch.udev);
-       return -1;
-}
-
-void cpu_hotplug_finalize()
-{
-       if (false == ch.is_initialized)
-               return;
-
-       if (NULL != ch.io_handler)
-               remove_fd_read_handler(&(ch.io_handler));
-
-       if (NULL != ch.mon)
-               ch.mon = udev_monitor_unref(ch.mon);
-
-       if (NULL != ch.udev)
-               ch.udev = udev_unref(ch.udev);
-       ch.is_initialized = false;
-}
diff --git a/src/resource-limiter/cpu/cpu-hotplug.h b/src/resource-limiter/cpu/cpu-hotplug.h
deleted file mode 100644 (file)
index 9879088..0000000
+++ /dev/null
@@ -1,15 +0,0 @@
-#ifndef CPU_HOTPLUG_H
-#define CPU_HOTPLUG_H
-
-#ifdef __cplusplus
-extern "C" {
-#endif /* __cplusplus */
-
-int cpu_hotplug_init();
-void cpu_hotplug_finalize();
-
-#ifdef __cplusplus
-}
-#endif /* __cplusplus */
-
-#endif /* CPU_HOTPLUG_H */
diff --git a/src/resource-optimizer/cpu/cpu-hotplug.c b/src/resource-optimizer/cpu/cpu-hotplug.c
new file mode 100644 (file)
index 0000000..cf344cc
--- /dev/null
@@ -0,0 +1,232 @@
+#include <libudev.h>
+#include <stdbool.h>
+#include "trace.h"
+#include "fd-handler.h"
+#include "notifier.h"
+#include "file-helper.h"
+#include "cpu-hotplug.h"
+
+#define UDEV_MONITOR_BUFFER_SIZE (512 * 1024)
+
+struct cpu_hotplug {
+       struct udev *udev;
+       struct udev_monitor *mon;
+       int fd;
+       fd_handler_h io_handler;
+       bool is_initialized;
+};
+
+static struct cpu_hotplug ch;
+
+static int get_core_status(int core_id)
+{
+       char buf[128];
+       int32_t status = 0;
+
+       if (snprintf(buf, sizeof buf, "/sys/devices/system/cpu/cpu%d/online", core_id) < 0)
+               return -1;
+
+       if (fread_int(buf, &status) != RESOURCED_ERROR_NONE)
+               return -1;
+
+       return status;
+}
+
+static void handle_event(struct udev_device *dev)
+{
+       int cpu_id;
+       const char *action = NULL;
+       int status = 0;
+
+       if (NULL == dev)
+               return;
+
+       /* get cpu id from syspath */
+       if (sscanf(udev_device_get_syspath(dev), "/sys/devices/system/cpu/cpu%d", &cpu_id) != 1)
+               return;
+
+       action = udev_device_get_action(dev);
+
+       /* action == NULL means that a cpu has been enumerated on startup, check if it's on or off */
+       if (NULL == action) {
+               status = get_core_status(cpu_id);
+               if (status < 0) {
+                       _E("cpu hotplug: could not determine core status (%d)", cpu_id);
+                       return;
+               }
+       } else if (!strcmp(action, "online")) {
+               status = 1;
+       } else if (!strcmp(action, "offline")) {
+               status = 0;
+       }
+
+       resourced_notify((status == 1) ? RESOURCED_NOTIFIER_CPU_ON : RESOURCED_NOTIFIER_CPU_OFF, &cpu_id);
+}
+
+static bool udev_io_handler(int fd, void *user_data)
+{
+       struct udev_monitor *mon = (struct udev_monitor *)user_data;
+       struct udev_device *dev;
+
+       assert(mon);
+       assert(fd >= 0);
+
+       dev = udev_monitor_receive_device(mon);
+       handle_event(dev);
+       udev_device_unref(dev);
+       return true;
+}
+
+static int monitor_cpus()
+{
+       int ret = 0;
+
+       if (false == ch.is_initialized)
+               return 0;
+
+       ch.mon = udev_monitor_new_from_netlink(ch.udev, "kernel");
+       if (NULL == ch.mon) {
+               _E("cpu-hotplug: creating device monitor failed");
+               return -1;
+       }
+
+       if (udev_monitor_set_receive_buffer_size(ch.mon, UDEV_MONITOR_BUFFER_SIZE) < 0) {
+               _E("cpu-hotplug: change monitor buffer size failed");
+               ret = -1;
+               goto monitor_failed;
+       }
+
+       if (udev_monitor_filter_add_match_subsystem_devtype(ch.mon, "cpu", NULL) < 0) {
+               _E("cpu-hotplug: creating monitor add match subsystem failed");
+               ret = -1;
+               goto monitor_failed;
+       }
+
+       if (udev_monitor_enable_receiving(ch.mon) < 0) {
+               _E("cpu-hotplug: monitor: enable receiving failed");
+               ret = -1;
+               goto monitor_failed;
+       }
+
+       ch.fd = udev_monitor_get_fd(ch.mon);
+       if (ch.fd < 0) {
+               _E("cpu-hotplug: monitor: could not setup fd");
+               ret = -1;
+               goto monitor_failed;
+       }
+
+       if (add_fd_read_handler(ch.fd, udev_io_handler, ch.mon, NULL, &(ch.io_handler)) < 0) {
+               _E("cpu-hotplug: could not add fd read handler");
+               ret = -1;
+               goto monitor_failed;
+       }
+       return ret;
+
+monitor_failed:
+       ch.io_handler = NULL;
+       ch.mon = udev_monitor_unref(ch.mon);
+
+       /* udev_monitor_unref() should take care of closing fd */
+       ch.fd = -1;
+       return ret;
+}
+
+static int enumerate_cpus()
+{
+       struct udev_enumerate *enumerate;
+       struct udev_device *dev;
+       struct udev_list_entry *devices, *entry;
+       int ret = 0;
+
+       if (false == ch.is_initialized)
+               return 0;
+
+       enumerate = udev_enumerate_new(ch.udev);
+
+       if (NULL == enumerate) {
+               _E("cpu-hotplug: could not create enumerate context");
+               return -1;
+       }
+
+       if (udev_enumerate_add_match_subsystem(enumerate, "cpu") < 0) {
+               _E("cpu-hotplug: could not add match subsystem");
+               ret = -1;
+               goto enumerate_quit;
+       }
+
+       if (udev_enumerate_scan_devices(enumerate) < 0) {
+               _E("cpu-hotplug: could not scan devices");
+               ret = -1;
+               goto enumerate_quit;
+       }
+
+       devices = udev_enumerate_get_list_entry(enumerate);
+       if (NULL == devices) {
+               _E("cpu-hotplug: could not enumerate devices");
+               ret = -1;
+               goto enumerate_quit;
+       }
+
+       udev_list_entry_foreach(entry, devices) {
+               dev = udev_device_new_from_syspath(ch.udev, udev_list_entry_get_name(entry));
+               handle_event(dev);
+               udev_device_unref(dev);
+       }
+
+enumerate_quit:
+       udev_enumerate_unref(enumerate);
+       return ret;
+}
+
+int cpu_hotplug_init()
+{
+       if (true == ch.is_initialized) {
+               _W("cpu hotplug monitor: already initialized");
+               return -1;
+       }
+
+       ch.udev = udev_new();
+       if (NULL == ch.udev) {
+               _E("cpu hotplug monitor: error creating udev handle");
+               return -1;
+       }
+
+       ch.is_initialized = true;
+
+       if (enumerate_cpus() < 0) {
+               _E("cpu-hotplug: enumerate failed");
+               goto init_failed;
+       }
+
+       if (monitor_cpus() < 0) {
+               _E("cpu-hotplug: monitoring start failed");
+               goto init_failed;
+       }
+
+       return 0;
+
+init_failed:
+       if (NULL != ch.mon)
+               ch.mon = udev_monitor_unref(ch.mon);
+
+       ch.is_initialized = false;
+       if (NULL != ch.udev)
+               ch.udev = udev_unref(ch.udev);
+       return -1;
+}
+
+void cpu_hotplug_finalize()
+{
+       if (false == ch.is_initialized)
+               return;
+
+       if (NULL != ch.io_handler)
+               remove_fd_read_handler(&(ch.io_handler));
+
+       if (NULL != ch.mon)
+               ch.mon = udev_monitor_unref(ch.mon);
+
+       if (NULL != ch.udev)
+               ch.udev = udev_unref(ch.udev);
+       ch.is_initialized = false;
+}
diff --git a/src/resource-optimizer/cpu/cpu-hotplug.h b/src/resource-optimizer/cpu/cpu-hotplug.h
new file mode 100644 (file)
index 0000000..9879088
--- /dev/null
@@ -0,0 +1,15 @@
+#ifndef CPU_HOTPLUG_H
+#define CPU_HOTPLUG_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
+int cpu_hotplug_init();
+void cpu_hotplug_finalize();
+
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+
+#endif /* CPU_HOTPLUG_H */