plugin: implement resource_set_cpu_boosting 82/276982/12 accepted/tizen/unified/20220630.212013 submit/tizen/20220630.054823
authorUnsung Lee <unsung.lee@samsung.com>
Tue, 28 Jun 2022 11:20:03 +0000 (20:20 +0900)
committerHyotaek Shim <hyotaek.shim@samsung.com>
Wed, 29 Jun 2022 06:41:18 +0000 (06:41 +0000)
Change-Id: Ibe5877898afa269653318a1fdfd28a8081753277
Signed-off-by: Unsung Lee <unsung.lee@samsung.com>
CMakeLists.txt
include/private/cpu-boosting-private.h [new file with mode: 0644]
packaging/capi-system-resource.spec
src/plugin/CMakeLists.txt
src/plugin/plugin.c
tests/main.c

index adeaadfa55a63e1b9d1dcf737fd4c3bcb23e1bb5..59e314769ead2462005e4b49776024d2f99fba61 100644 (file)
@@ -45,7 +45,7 @@ CONFIGURE_FILE(
 )
 INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_NAME}.pc DESTINATION ${LIB_INSTALL_DIR}/pkgconfig)
 INSTALL(
-               DIRECTORY ${INC_DIR}/ DESTINATION /usr/include/system
+               DIRECTORY ${INC_DIR}/ ${INC_DIR}/private/ DESTINATION /usr/include/system
                FILES_MATCHING PATTERN "cpu-boosting*.h")
 
 ADD_SUBDIRECTORY(src/plugin)
diff --git a/include/private/cpu-boosting-private.h b/include/private/cpu-boosting-private.h
new file mode 100644 (file)
index 0000000..dc3db61
--- /dev/null
@@ -0,0 +1,56 @@
+/* MIT License
+ *
+ * Copyright (c) 2022 Samsung Electronics Co., Ltd.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is furnished
+ * to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE. */
+
+#ifndef __TIZEN_SYSTEM_CPU_BOOSTING_PRIVATE_H__
+#define __TIZEN_SYSTEM_CPU_BOOSTING_PRIVATE_H__
+
+#include <stdbool.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+enum cpu_boosting_command {
+       CPU_BOOSTING_COMMAND_NONE = 0,
+       CPU_BOOSTING_COMMAND_SET,
+       CPU_BOOSTING_COMMAND_CLEAR,
+       CPU_BOOSTING_COMMAND_GET,
+};
+
+typedef struct resource_cpu_boosting_input {
+       int command;
+       int timeout_msec;
+       cpu_boosting_level_e level;
+       int body_size;
+       resource_pid_t pid;
+} cpu_boosting_input_t;
+
+typedef struct resource_cpu_boosting_output {
+       bool success;
+       cpu_boosting_level_info_t level;
+} cpu_boosting_output_t;
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif  // __TIZEN_SYSTEM_CPU_BOOSTING_PRIVATE_H__
index ec0cf616e50eaa58f7eb83752233a2d47dd0539f..c7e52d59dcf151fc3cea6267678d8589b6bc8f9f 100644 (file)
@@ -69,6 +69,7 @@ MAJORVER=`echo %{version} | awk 'BEGIN {FS="."}{print $1}'`
 %{_libdir}/pkgconfig/*.pc
 %{_libdir}/libcapi-system-resource.so
 %{_includedir}/system/*.h
+%{_includedir}/system/private/*.h
 
 %files plugin
 %manifest %{name}.manifest
index 94d688cb2f8bf9b0656713b8293965a8f42f2f3a..ef4820778909303e8da1e70680e30d21253d5335 100644 (file)
@@ -9,7 +9,7 @@ SET(PKG_MODULES
        dlog
        capi-base-common
 )
-INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include)
+INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include ${CMAKE_SOURCE_DIR}/include/private)
 
 INCLUDE(FindPkgConfig)
 pkg_check_modules(${PROJECT_NAME} REQUIRED ${PKG_MODULES})
index f0780c44fcaf6643aa474367ccd97ac9a013e70e..a3588fadd7356ac627c4a7aa0fba537db1aef9e8 100644 (file)
 
 #include "plugin.h"
 #include "cpu-boosting-type.h"
+#include "cpu-boosting-private.h"
+
+#include <errno.h>
+#include <string.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <sys/socket.h>
+#include <sys/types.h>
+#include <sys/un.h>
+#ifndef gettid
+#include <sys/syscall.h>
+
+#ifdef SYS_gettid
+#define gettid() (pid_t) syscall(SYS_gettid)
+#else
+#error "SYS_gettid unavailable on this system"
+#endif
+#endif
+
+#define SOCK_PATH "/run/.resourced.socket"
+
+static int resource_create_and_connect_sock(void)
+{
+       int sock;
+       socklen_t size;
+       struct sockaddr_un sockaddr;
+
+       sock = socket(AF_UNIX, SOCK_STREAM, 0);
+       if (sock < 0) {
+               _E("[CPU-BOOSTING-PLUGIN] Failed to allocate a socket");
+               return -1;
+       }
+
+       sockaddr.sun_family = AF_UNIX;
+       strncpy(sockaddr.sun_path, SOCK_PATH, strlen(SOCK_PATH) + 1);
+       size = sizeof(sockaddr);
+
+       if (connect(sock, (struct sockaddr *)&sockaddr, size) < 0) {
+               _E("[CPU-BOOSTING-PLUGIN] Failed to connect to the resourced module");
+               close(sock);
+               return -1;
+       }
+
+       return sock;
+}
+
+static inline bool resource_pid_input_is_valid(resource_pid_t pid)
+{
+       if (pid.pid < 0) {
+               _E("[CPU-BOOSTING-PLUGIN] pid should be euqal or larger than 0");
+               return false;
+       }
+
+       if (pid.pid == 0 && pid.tid != NULL && pid.tid_count <= 0) {
+               _E("[CPU-BOOSTING-PLUGIN] tid count should be larger than 0");
+               return false;
+       }
+
+       return true;
+}
+
+static inline bool resource_cpu_boosting_level_input_is_valid(cpu_boosting_level_e level)
+{
+       if (level < CPU_BOOSTING_LEVEL_STRONG || level > CPU_BOOSTING_LEVEL_WEAK) {
+               _E("[CPU-BOOSTING-PLUGIN] cpu boosting level should be located between %d and %d, but current level = %d", CPU_BOOSTING_LEVEL_STRONG, CPU_BOOSTING_LEVEL_WEAK, level);
+               return false;
+       }
+
+       return true;
+}
 
 API int resource_set_cpu_boosting (resource_pid_t pid,
                cpu_boosting_level_e level, int timeout_msec)
 {
-       _D("[CPU-BOOSTING-PLUGIN] %s called", __func__);
-
-       return 0;
+       int tid;
+       int byte;
+       int ret = 0;
+       int sock;
+       cpu_boosting_input_t input;
+//     cpu_boosting_output_t output;
+
+       if (!resource_pid_input_is_valid(pid))
+               return -1;
+
+       if (!resource_cpu_boosting_level_input_is_valid(level))
+               return -1;
+
+       if ((sock = resource_create_and_connect_sock()) < 0)
+               return -1;
+
+       input.command = CPU_BOOSTING_COMMAND_SET;
+       input.pid = pid;
+       input.level = level;
+       input.timeout_msec = timeout_msec;
+
+       if (input.pid.pid > 0)
+               input.body_size = 0;
+       else {
+               if (input.pid.tid == NULL) {
+                       tid = gettid();
+                       input.pid.tid = &tid;
+                       input.pid.tid_count = 1;
+               }
+
+               input.body_size = input.pid.tid_count * sizeof(int);
+       }
+
+       byte = send(sock, (const void *)&input, sizeof(input), 0);
+       if (byte != sizeof(input)) {
+               _E("[CPU-BOOSTING-PLUGIN] error is based on %s", strerror(errno));
+               _E("[CPU-BOOSTING-PLUGIN] client input size is %u, but sent size is %d",
+                               (unsigned int)sizeof(input), byte);
+               ret = -1;
+               goto close_sock;
+       }
+
+       if (input.body_size > 0) {
+               byte = send(sock, (const void *)input.pid.tid, input.body_size, 0);
+               if (byte != input.body_size) {
+                       _E("[CPU-BOOSTING-PLUGIN] error is based on %s", strerror(errno));
+                       _E("[CPU-BOOSTING-PLUGIN] client input size is %d, but sent size is %d",
+                                       input.body_size, byte);
+                       ret = -1;
+                       goto close_sock;
+               }
+       }
+
+/*     byte = recv(sock, (void *)&output, sizeof(bool), 0);
+       if (byte != sizeof(bool)) {
+               _E("[CPU-BOOSTING-PLUGIN] error is based on %s", strerror(errno));
+               _E("[CPU-BOOSTING-PLUGIN] client output size is %u, but received size is %d",
+                               (unsigned int)sizeof(bool), byte);
+               goto close_sock;
+       }
+
+
+       if (!output.success)
+               _E("[CPU-BOOSTING-PLUGIN] fail to communicate");
+       else {
+               _D("[CPU-BOOSTING-PLUGIN] success to communicate");
+               ret = 0;
+       }*/
+
+close_sock:
+       close(sock);
+
+       return ret;
 }
 
 API int resource_clear_cpu_boosting (resource_pid_t pid)
index 0757a18b3291b5502f31b4dc0cbaaba60cc99c32..1e7f0a487ecc96604ec2d912c0a27e1fe8338466 100644 (file)
@@ -50,6 +50,7 @@ static void *thread_worker(void *arg)
        if (tid)
                *tid = gettid();
 
+       sleep(5);
        pthread_exit(NULL);
 }
 
@@ -155,7 +156,7 @@ static void one_process_multi_thread_test(int tid_count)
                ret = pthread_create(&thread, NULL, thread_worker, &pid.tid[i]);
                if (ret == 0) {
                        real_tid_count++;
-                       ret = pthread_join(thread, NULL);
+                       ret = pthread_detach(thread);
                        if (ret)
                                _E("[CPU-BOOSTING-TEST] Failed to join a new thread");
                }
@@ -185,7 +186,7 @@ static void one_process_all_thread_test(int tid_count)
                pthread_t thread;
                ret = pthread_create(&thread, NULL, thread_worker, NULL);
                if (ret == 0) {
-                       ret = pthread_join(thread, NULL);
+                       ret = pthread_detach(thread);
                        if (ret)
                                _E("[CPU-BOOSTING-TEST] Failed to join a new thread");
                }