From: Unsung Lee Date: Tue, 28 Jun 2022 11:20:03 +0000 (+0900) Subject: plugin: implement resource_set_cpu_boosting X-Git-Tag: submit/tizen_6.5/20220630.054955^0 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=27aeed44a8e86d8b5c5b8ea569b5efe6242c5e28;p=platform%2Fcore%2Fapi%2Fresource.git plugin: implement resource_set_cpu_boosting Change-Id: Ibe5877898afa269653318a1fdfd28a8081753277 Signed-off-by: Unsung Lee (cherry picked from commit c235ebf186f540eaf79188fc4edc13b3d6ca4e63) --- diff --git a/CMakeLists.txt b/CMakeLists.txt index adeaadf..59e3147 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 index 0000000..dc3db61 --- /dev/null +++ b/include/private/cpu-boosting-private.h @@ -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 + +#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__ diff --git a/packaging/capi-system-resource.spec b/packaging/capi-system-resource.spec index ec0cf61..c7e52d5 100644 --- a/packaging/capi-system-resource.spec +++ b/packaging/capi-system-resource.spec @@ -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 diff --git a/src/plugin/CMakeLists.txt b/src/plugin/CMakeLists.txt index 94d688c..ef48207 100644 --- a/src/plugin/CMakeLists.txt +++ b/src/plugin/CMakeLists.txt @@ -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}) diff --git a/src/plugin/plugin.c b/src/plugin/plugin.c index f0780c4..a3588fa 100644 --- a/src/plugin/plugin.c +++ b/src/plugin/plugin.c @@ -22,13 +22,153 @@ #include "plugin.h" #include "cpu-boosting-type.h" +#include "cpu-boosting-private.h" + +#include +#include +#include +#include +#include +#include +#include +#ifndef gettid +#include + +#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) diff --git a/tests/main.c b/tests/main.c index 0757a18..1e7f0a4 100644 --- a/tests/main.c +++ b/tests/main.c @@ -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"); }