Add a service to test inheritance cases 76/280276/1
authorUnsung Lee <unsung.lee@samsung.com>
Tue, 23 Aug 2022 04:41:36 +0000 (13:41 +0900)
committerHyotaek Shim <hyotaek.shim@samsung.com>
Fri, 26 Aug 2022 05:32:53 +0000 (05:32 +0000)
Change-Id: I918af83806f04e86c6abe71f3e179996c131b5d1
Signed-off-by: Unsung Lee <unsung.lee@samsung.com>
(cherry picked from commit 467f2cdf3075c0f495a55675ed10326b6d0add4d)

packaging/capi-system-resource.spec
src/plugin/plugin.c
tests/CMakeLists.txt
tests/main.c
tests/service/CMakeLists.txt [new file with mode: 0644]
tests/service/main.c [new file with mode: 0644]
tests/service/system-resource-test-service.service [new file with mode: 0644]

index c7e52d5..6e420c6 100644 (file)
@@ -9,6 +9,7 @@ Source1:    capi-system-resource.manifest
 BuildRequires:  cmake
 BuildRequires:  pkgconfig(capi-base-common)
 BuildRequires:  pkgconfig(dlog)
+BuildRequires:  pkgconfig(glib-2.0)
 
 %description
 API library for system resource management and optimization
@@ -80,3 +81,6 @@ MAJORVER=`echo %{version} | awk 'BEGIN {FS="."}{print $1}'`
 %manifest %{name}.manifest
 %license LICENSE.MIT
 %{_bindir}/capi-system-resource-test
+%{_bindir}/system-resource-test-service
+%{_unitdir}/system-resource-test-service.service
+%{_unitdir}/multi-user.target.wants/system-resource-test-service.service
index 47de259..587fbd2 100644 (file)
@@ -251,9 +251,7 @@ retry_header:
                                goto retry_header;
                        }
 
-                       char error_buf[1024];
-                       strerror_r(errno, error_buf, sizeof(error_buf));
-                       _E("[CPU-BOOSTING-PLUGIN] error is based on %s", error_buf);
+                       _E("[CPU-BOOSTING-PLUGIN] error is based on %m");
                }
                else
                        _E("[CPU-BOOSTING-PLUGIN] client output size is %u, but received size is %d",
@@ -282,9 +280,7 @@ retry_body:
                                        goto retry_body;
                                }
 
-                               char error_buf[1024];
-                               strerror_r(errno, error_buf, sizeof(error_buf));
-                               _E("[CPU-BOOSTING-PLUGIN] error is based on %s", error_buf);
+                               _E("[CPU-BOOSTING-PLUGIN] error is based on %m");
                        }
                        else
                                _E("[CPU-BOOSTING-PLUGIN] client output size is %u, but received size is %d",
index 4d991a7..4088a97 100644 (file)
@@ -1,8 +1,7 @@
 SET(test "capi-system-resource-test")
 
 SET(SOURCES
-       main.c
-)
+       main.c)
 
 SET(PKG_MODULES
        dlog
@@ -10,6 +9,16 @@ SET(PKG_MODULES
 )
 INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include)
 
+MACRO(INSTALL_SYMLINK _filepath _sympath)
+  GET_FILENAME_COMPONENT(_symname ${_sympath} NAME)
+  GET_FILENAME_COMPONENT(_installdir ${_sympath} PATH)
+  EXECUTE_PROCESS(COMMAND "${CMAKE_COMMAND}" -E create_symlink
+    ${_filepath}
+    ${CMAKE_CURRENT_BINARY_DIR}/${_symname})
+  INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/${_symname}
+    DESTINATION ${_installdir})
+ENDMACRO(INSTALL_SYMLINK)
+
 INCLUDE(FindPkgConfig)
 pkg_check_modules(${test} REQUIRED ${PKG_MODULES})
 FOREACH(flag ${${test}_CFLAGS})
@@ -20,5 +29,7 @@ SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS} -Werror -Wall -pthread")
 SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed -pie")
 
 ADD_EXECUTABLE(${test} ${SOURCES})
-TARGET_LINK_LIBRARIES(${test} ${PROJECT_NAME} ${test_LDFLAGS} "-ldl")
+TARGET_LINK_LIBRARIES(${test} ${PROJECT_NAME} ${test_LDFLAGS})
 INSTALL(TARGETS ${test} DESTINATION bin)
+
+ADD_SUBDIRECTORY(service)
index ebca9b8..4d59ae6 100644 (file)
@@ -69,7 +69,22 @@ static void *thread_attr_check(void *arg)
        pthread_exit(NULL);
 }
 
-static void test_register_cpu_inheritance(const char *dest_process, resource_pid_t pid)
+static void test_set_cpu_inheritance(pid_t source_tid, const char *dest_process,
+               int timeout_msec)
+{
+       int ret = resource_set_cpu_inheritance(source_tid, dest_process, timeout_msec);
+       if (ret)
+               _E("[CPU-BOOSTING-TEST] error = %d", ret);
+}
+
+static void test_clear_cpu_inheritance(pid_t source_tid, const char *dest_process)
+{
+       int ret = resource_clear_cpu_inheritance(source_tid, dest_process);
+       if (ret)
+               _E("[CPU-BOOSTING-TEST] error = %d", ret);
+}
+
+/*static void test_register_cpu_inheritance(const char *dest_process, resource_pid_t pid)
 {
        int ret;
 
@@ -87,7 +102,7 @@ static void test_unregister_cpu_inheritance(const char *dest_process)
        if (ret) {
                _E("[CPU-BOOSTING-TEST] error = %d", ret);
        }
-}
+}*/
 
 static void test_set_cpu_boosting(resource_pid_t pid, cpu_boosting_level_e level, cpu_boosting_flag_e flags, int timeout_msec)
 {
@@ -134,8 +149,10 @@ static void test_cpu_boosting(resource_pid_t pid, cpu_boosting_flag_e flags)
 {
        cpu_boosting_level_info_t cur_level;
 
-       for (int level = CPU_BOOSTING_LEVEL_WEAK; level > CPU_BOOSTING_LEVEL_NONE; level--)
+       for (int level = CPU_BOOSTING_LEVEL_WEAK; level > CPU_BOOSTING_LEVEL_NONE; level--) {
+               test_set_cpu_boosting(pid, level, flags, -1);
                test_set_cpu_boosting(pid, level, flags, 3000);
+       }
 
        if (flags & CPU_BOOSTING_RESET_ON_FORK) {
                pthread_t thread;
@@ -149,12 +166,13 @@ static void test_cpu_boosting(resource_pid_t pid, cpu_boosting_flag_e flags)
                        _E("[CPU-BOOSTING-TEST] Failed to create a new thread");
        }
 
+       test_set_cpu_inheritance(gettid(), "system-resource-test-service", -1);
        test_get_cpu_boosting(pid, &cur_level, false); /* Expect CPU_BOOSTING_LEVEL_STRONG */
        test_clear_cpu_boosting(pid);
        test_get_cpu_boosting(pid, &cur_level, false); /* Expect CPU_BOOSTING_LEVEL_NONE */
-
-       test_register_cpu_inheritance("test-service", pid);
-       test_unregister_cpu_inheritance("test-service");
+       test_clear_cpu_inheritance(gettid(), "system-resource-test-service");
+//     test_register_cpu_inheritance("system-resource-test-service", pid);
+//     test_unregister_cpu_inheritance("system-resource-test-service");
 }
 
 static void one_process_one_thread_test(cpu_boosting_flag_e flags)
diff --git a/tests/service/CMakeLists.txt b/tests/service/CMakeLists.txt
new file mode 100644 (file)
index 0000000..a3dbce2
--- /dev/null
@@ -0,0 +1,40 @@
+SET(test-service "system-resource-test-service")
+SET(SERVICE_SOURCE_DIR ${CMAKE_SOURCE_DIR}/tests/service)
+
+SET(SERVICE_SOURCES
+       main.c)
+
+SET(PKG_MODULES
+       dlog
+       capi-base-common
+       glib-2.0
+)
+INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include)
+
+MACRO(INSTALL_SYMLINK _filepath _sympath)
+  GET_FILENAME_COMPONENT(_symname ${_sympath} NAME)
+  GET_FILENAME_COMPONENT(_installdir ${_sympath} PATH)
+  EXECUTE_PROCESS(COMMAND "${CMAKE_COMMAND}" -E create_symlink
+    ${_filepath}
+    ${CMAKE_CURRENT_BINARY_DIR}/${_symname})
+  INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/${_symname}
+    DESTINATION ${_installdir})
+ENDMACRO(INSTALL_SYMLINK)
+
+INCLUDE(FindPkgConfig)
+pkg_check_modules(${test-service} REQUIRED ${PKG_MODULES})
+FOREACH(flag ${${test-service}_CFLAGS})
+    SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}")
+ENDFOREACH(flag)
+
+SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS} -Werror -Wall -pthread")
+SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed -pie")
+
+ADD_EXECUTABLE(${test-service} ${SERVICE_SOURCES})
+TARGET_LINK_LIBRARIES(${test-service} ${PROJECT_NAME} ${${test-service}_LDFLAGS})
+INSTALL(TARGETS ${test-service} DESTINATION bin)
+
+INSTALL(FILES
+  ${SERVICE_SOURCE_DIR}/${test-service}.service
+  DESTINATION lib/systemd/system)
+INSTALL_SYMLINK(../${test-service}.service lib/systemd/system/multi-user.target.wants/${test-service}.service)
diff --git a/tests/service/main.c b/tests/service/main.c
new file mode 100644 (file)
index 0000000..5d5424f
--- /dev/null
@@ -0,0 +1,85 @@
+#include <glib.h>
+#include <dlog.h>
+#include <assert.h>
+#include <signal.h>
+#include <unistd.h>
+#include <sys/types.h>
+
+#include "common.h"
+#include "cpu-boosting.h"
+
+GMainLoop *mainloop;
+resource_pid_t pid;
+
+static void sig_int_handler(int sig)
+{
+    _E("[CPU-BOOSTING-SERVICE-TEST] signal = %d is received", sig);
+       cpu_boosting_level_info_t level;
+       int ret = resource_get_cpu_boosting_level(pid, &level);
+       if (ret < 0) {
+               _E("[CPU-BOOSTING-SERVICE-TEST] error = %d", ret);
+       }
+       else {
+               for(int i = 0; i < level.tid_count; i++)
+                       _I("[CPU-BOOSTING-SERVICE-TEST] boosting level is %s",
+                         level.tid_level[i] == CPU_BOOSTING_LEVEL_STRONG ? "strong" :
+                         level.tid_level[i] == CPU_BOOSTING_LEVEL_MEDIUM ? "medium" :
+                         level.tid_level[i] == CPU_BOOSTING_LEVEL_WEAK ? "weak" : "none");
+
+               if (level.tid_level)
+                       free(level.tid_level);
+       }
+
+       resource_unregister_cpu_inheritance_destination("system-resource-test-service");
+       g_main_loop_quit(mainloop);
+}
+
+static void sig_term_handler(int sig, siginfo_t *siginfo, void *context)
+{
+    _E("[CPU-BOOSTING-SERVICE-TEST] signal = %d is received", sig);
+       cpu_boosting_level_info_t level;
+       int ret = resource_get_cpu_boosting_level(pid, &level);
+       if (ret < 0) {
+               _E("[CPU-BOOSTING-SERVICE-TEST] error = %d", ret);
+       }
+       else {
+               for(int i = 0; i < level.tid_count; i++)
+                       _I("[CPU-BOOSTING-SERVICE-TEST] boosting level is %s",
+                         level.tid_level[i] == CPU_BOOSTING_LEVEL_STRONG ? "strong" :
+                         level.tid_level[i] == CPU_BOOSTING_LEVEL_MEDIUM ? "medium" :
+                         level.tid_level[i] == CPU_BOOSTING_LEVEL_WEAK ? "weak" : "none");
+
+               if (level.tid_level)
+                       free(level.tid_level);
+       }
+
+       resource_unregister_cpu_inheritance_destination("system-resource-test-service");
+       g_main_loop_quit(mainloop);
+}
+
+static void add_signal_handler(void)
+{
+    struct sigaction sig_act;
+
+    sig_act.sa_sigaction = sig_term_handler;
+    sig_act.sa_flags = 0;
+    sig_act.sa_flags |= SA_SIGINFO;
+    sigaction(SIGTERM, &sig_act, NULL);
+    signal(SIGINT, sig_int_handler);
+}
+
+int main(int argc, char *argv[])
+{
+       mainloop = g_main_loop_new(NULL, false);
+       assert(mainloop);
+
+       add_signal_handler();
+
+       pid.pid = getpid();
+       resource_register_cpu_inheritance_destination("system-resource-test-service", pid);
+
+       g_main_loop_run(mainloop);
+       g_main_loop_unref(mainloop);
+
+    return 0;
+}
diff --git a/tests/service/system-resource-test-service.service b/tests/service/system-resource-test-service.service
new file mode 100644 (file)
index 0000000..b2eb0a5
--- /dev/null
@@ -0,0 +1,10 @@
+[Unit]
+Description=test cpu inheritance (un)register
+DefaultDependencies=no
+
+[Service]
+Type=simple
+ExecStart=/usr/bin/system-resource-test-service
+
+[Install]
+WantedBy=multi-user.target