From: Unsung Lee Date: Wed, 10 Aug 2022 09:06:52 +0000 (+0900) Subject: plugin: add (un)register and get/set inheritance X-Git-Tag: submit/tizen/20220816.062425^0 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=e1682dbee452ff017957cd65430f56123cfa5ee9;p=platform%2Fcore%2Fapi%2Fresource.git plugin: add (un)register and get/set inheritance Change-Id: Ib1ca8bbc8eb775b0e5919c05420610dd340983b2 Signed-off-by: Unsung Lee --- diff --git a/include/private/cpu-boosting-private.h b/include/private/cpu-boosting-private.h index f6096e6..222861a 100644 --- a/include/private/cpu-boosting-private.h +++ b/include/private/cpu-boosting-private.h @@ -34,6 +34,10 @@ enum cpu_boosting_command { CPU_BOOSTING_COMMAND_SET, CPU_BOOSTING_COMMAND_CLEAR, CPU_BOOSTING_COMMAND_GET, + CPU_BOOSTING_COMMAND_SET_INHERITANCE, + CPU_BOOSTING_COMMAND_CLEAR_INHERITANCE, + CPU_BOOSTING_COMMAND_REGISTER_DESTINATION, + CPU_BOOSTING_COMMAND_UNREGISTER_DESTINATION, }; typedef struct resource_cpu_boosting_input { @@ -41,7 +45,9 @@ typedef struct resource_cpu_boosting_input { int timeout_msec; cpu_boosting_level_e level; cpu_boosting_flag_e flags; - int body_size; + int body_len; + int dest_len; + const char *dest; resource_pid_t pid; } cpu_boosting_input_t; diff --git a/src/plugin/plugin.c b/src/plugin/plugin.c index 7dcaac7..47de259 100644 --- a/src/plugin/plugin.c +++ b/src/plugin/plugin.c @@ -47,7 +47,7 @@ static int resource_create_and_connect_sock(void) { int sock; - socklen_t size; + socklen_t len; struct sockaddr_un sockaddr; sock = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0); @@ -58,9 +58,9 @@ static int resource_create_and_connect_sock(void) sockaddr.sun_family = AF_UNIX; strncpy(sockaddr.sun_path, SOCK_PATH, strlen(SOCK_PATH) + 1); - size = sizeof(sockaddr); + len = sizeof(sockaddr); - if (connect(sock, (struct sockaddr *)&sockaddr, size) < 0) { + if (connect(sock, (struct sockaddr *)&sockaddr, len) < 0) { _E("[CPU-BOOSTING-PLUGIN] Failed to connect to the resourced module"); close(sock); return -1; @@ -109,8 +109,8 @@ static int resource_cpu_boosting_send_command (cpu_boosting_input_t input, int s int byte; pid_t tid; - if (input.pid.pid > 0) - input.body_size = 0; + if (input.pid.pid != 0) + input.body_len = 0; else { if (input.pid.tid == NULL) { tid = gettid(); @@ -118,29 +118,40 @@ static int resource_cpu_boosting_send_command (cpu_boosting_input_t input, int s input.pid.tid_count = 1; } - input.body_size = input.pid.tid_count * sizeof(pid_t); + input.body_len = input.pid.tid_count * sizeof(pid_t); } byte = send(sock, (const void *)&input, sizeof(input), 0); if (byte != sizeof(input)) { - 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"); _E("[CPU-BOOSTING-PLUGIN] client input size is %u, but sent size is %d", (unsigned int)sizeof(input), byte); return -1; } - if (input.body_size > 0) { - byte = send(sock, (const void *)input.pid.tid, input.body_size, 0); - if (byte != input.body_size) { - char error_buf[1024]; - strerror_r(errno, error_buf, sizeof(error_buf)); + if (input.body_len > 0) { + switch (input.command) { + case CPU_BOOSTING_COMMAND_SET: + case CPU_BOOSTING_COMMAND_CLEAR: + case CPU_BOOSTING_COMMAND_GET: + case CPU_BOOSTING_COMMAND_REGISTER_DESTINATION: + byte = send(sock, (const void *)input.pid.tid, input.body_len, 0); + if (byte != input.body_len) { + _E("[CPU-BOOSTING-PLUGIN] error is based on %m"); + _E("[CPU-BOOSTING-PLUGIN] client input size is %d, but sent size is %d", + input.body_len, byte); + return -1; + } + break; + } + } - _E("[CPU-BOOSTING-PLUGIN] error is based on %s", error_buf); + if (input.dest && input.dest_len > 0) { + byte = send(sock, (const void *)input.dest, input.dest_len, 0); + if (byte != input.dest_len) { + _E("[CPU-BOOSTING-PLUGIN] error is based on %m"); _E("[CPU-BOOSTING-PLUGIN] client input size is %d, but sent size is %d", - input.body_size, byte); + input.dest_len, byte); return -1; } } @@ -164,6 +175,7 @@ API int resource_set_cpu_boosting (resource_pid_t pid, if ((sock = resource_create_and_connect_sock()) < 0) return -1; + memset(&input, 0, sizeof(input)); input.command = CPU_BOOSTING_COMMAND_SET; input.pid = pid; input.level = level; @@ -188,10 +200,10 @@ API int resource_clear_cpu_boosting (resource_pid_t pid) if ((sock = resource_create_and_connect_sock()) < 0) return -1; + memset(&input, 0, sizeof(input)); input.command = CPU_BOOSTING_COMMAND_CLEAR; input.pid = pid; input.level = CPU_BOOSTING_LEVEL_NONE; - input.timeout_msec = 0; ret = resource_cpu_boosting_send_command(input, sock); close(sock); @@ -215,10 +227,10 @@ API int resource_get_cpu_boosting_level (resource_pid_t pid, if ((sock = resource_create_and_connect_sock()) < 0) return -1; + memset(&input, 0, sizeof(input)); input.command = CPU_BOOSTING_COMMAND_GET; input.pid = pid; input.level = CPU_BOOSTING_LEVEL_NONE; - input.timeout_msec = 0; ret = resource_cpu_boosting_send_command(input, sock); if (ret < 0) @@ -303,30 +315,112 @@ close_sock: API int resource_set_cpu_inheritance (pid_t source_tid, const char *dest_process, int timeout_msec) { - _D("[CPU-BOOSTING-PLUGIN] %s called", __func__); + int ret; + int sock; + cpu_boosting_input_t input; - return 0; + if (source_tid < 0) + return -1; + else if (source_tid == 0) + source_tid = gettid(); + + if (dest_process == NULL) + return -1; + + if ((sock = resource_create_and_connect_sock()) < 0) + return -1; + + memset(&input, 0, sizeof(input)); + input.command = CPU_BOOSTING_COMMAND_SET_INHERITANCE; + input.pid.pid = source_tid; + input.timeout_msec = timeout_msec; + input.dest = dest_process; + input.dest_len = strlen(dest_process); + + ret = resource_cpu_boosting_send_command(input, sock); + close(sock); + + return ret; } API int resource_clear_cpu_inheritance (pid_t source_tid, const char *dest_process) { - _D("[CPU-BOOSTING-PLUGIN] %s called", __func__); + int ret; + int sock; + cpu_boosting_input_t input; - return 0; + if (source_tid < 0) + return -1; + else if (source_tid == 0) + source_tid = gettid(); + + if (dest_process == NULL) + return -1; + + if ((sock = resource_create_and_connect_sock()) < 0) + return -1; + + memset(&input, 0, sizeof(input)); + input.command = CPU_BOOSTING_COMMAND_CLEAR_INHERITANCE; + input.pid.pid = source_tid; + input.dest = dest_process; + input.dest_len = strlen(dest_process); + + ret = resource_cpu_boosting_send_command(input, sock); + close(sock); + + return ret; } API int resource_register_cpu_inheritance_destination (const char *dest_process, resource_pid_t pid) { - _D("[CPU-BOOSTING-PLUGIN] %s called", __func__); + int ret; + int sock; + cpu_boosting_input_t input; - return 0; + if (!resource_pid_input_is_valid(pid)) + return -1; + + if (dest_process == NULL) + return -1; + + if ((sock = resource_create_and_connect_sock()) < 0) + return -1; + + memset(&input, 0, sizeof(input)); + input.command = CPU_BOOSTING_COMMAND_REGISTER_DESTINATION; + input.pid = pid; + input.dest = dest_process; + input.dest_len = strlen(dest_process); + + ret = resource_cpu_boosting_send_command(input, sock); + close(sock); + + return ret; } API int resource_unregister_cpu_inheritance_destination (const char *dest_process) { - _D("[CPU-BOOSTING-PLUGIN] %s called", __func__); + int ret; + int sock; + cpu_boosting_input_t input; - return 0; + if (dest_process == NULL) + return -1; + + if ((sock = resource_create_and_connect_sock()) < 0) + return -1; + + memset(&input, 0, sizeof(input)); + input.command = CPU_BOOSTING_COMMAND_UNREGISTER_DESTINATION; + input.pid.pid = -1; + input.dest = dest_process; + input.dest_len = strlen(dest_process); + + ret = resource_cpu_boosting_send_command(input, sock); + close(sock); + + return ret; } void __CONSTRUCTOR__ cpu_boosting_plugin_init(void) diff --git a/tests/main.c b/tests/main.c index e00e571..ebca9b8 100644 --- a/tests/main.c +++ b/tests/main.c @@ -69,6 +69,26 @@ static void *thread_attr_check(void *arg) pthread_exit(NULL); } +static void test_register_cpu_inheritance(const char *dest_process, resource_pid_t pid) +{ + int ret; + + ret = resource_register_cpu_inheritance_destination(dest_process, pid); + if (ret) { + _E("[CPU-BOOSTING-TEST] error = %d", ret); + } +} + +static void test_unregister_cpu_inheritance(const char *dest_process) +{ + int ret; + + ret = resource_unregister_cpu_inheritance_destination(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) { int ret; @@ -131,7 +151,10 @@ static void test_cpu_boosting(resource_pid_t pid, cpu_boosting_flag_e flags) 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_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"); } static void one_process_one_thread_test(cpu_boosting_flag_e flags)