From 5c87d6626e38af6511e576dfb6e18b8ea66f439e Mon Sep 17 00:00:00 2001 From: Unsung Lee Date: Mon, 11 Jul 2022 16:48:44 +0900 Subject: [PATCH] plugin: add clear and get funcs Change-Id: I0d6ef27e209f4dbb13fdad5153db2b962217a938 Signed-off-by: Unsung Lee --- src/plugin/plugin.c | 146 ++++++++++++++++++++++++++++++++++++---------------- tests/main.c | 2 +- 2 files changed, 104 insertions(+), 44 deletions(-) diff --git a/src/plugin/plugin.c b/src/plugin/plugin.c index 598c1b3..67f19e0 100644 --- a/src/plugin/plugin.c +++ b/src/plugin/plugin.c @@ -93,29 +93,10 @@ static inline bool resource_cpu_boosting_level_input_is_valid(cpu_boosting_level return true; } -API int resource_set_cpu_boosting (resource_pid_t pid, - cpu_boosting_level_e level, int timeout_msec) +static int resource_cpu_boosting_send_command (cpu_boosting_input_t input, int sock) { - pid_t 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; + pid_t tid; if (input.pid.pid > 0) input.body_size = 0; @@ -134,8 +115,7 @@ API int resource_set_cpu_boosting (resource_pid_t pid, _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; + return -1; } if (input.body_size > 0) { @@ -144,28 +124,35 @@ API int resource_set_cpu_boosting (resource_pid_t pid, _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; + return -1; } } -/* 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; - } + return 0; +} +API int resource_set_cpu_boosting (resource_pid_t pid, + cpu_boosting_level_e level, int timeout_msec) +{ + int ret; + int sock; + cpu_boosting_input_t input; - if (!output.success) - _E("[CPU-BOOSTING-PLUGIN] fail to communicate"); - else { - _D("[CPU-BOOSTING-PLUGIN] success to communicate"); - ret = 0; - }*/ + if (!resource_pid_input_is_valid(input.pid)) + return -1; -close_sock: + 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; + + ret = resource_cpu_boosting_send_command(input, sock); close(sock); return ret; @@ -173,17 +160,90 @@ close_sock: API int resource_clear_cpu_boosting (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(input.pid)) + return -1; + + if ((sock = resource_create_and_connect_sock()) < 0) + return -1; + + 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); + + return ret; } API int resource_get_cpu_boosting_level (resource_pid_t pid, cpu_boosting_level_info_t *level) { - _D("[CPU-BOOSTING-PLUGIN] %s called", __func__); + int ret; + int byte; + int sock; + cpu_boosting_output_t output; + cpu_boosting_input_t input; - return 0; + if (!resource_pid_input_is_valid(pid)) + return -1; + + if ((sock = resource_create_and_connect_sock()) < 0) + return -1; + + input.command = CPU_BOOSTING_COMMAND_GET; + input.pid = pid; + + ret = resource_cpu_boosting_send_command(input, sock); + if (ret < 0) + goto close_sock; + + byte = recv(sock, (void *)&output, sizeof(output), 0); + if (byte != sizeof(output)) { + _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(output), byte); + ret = -1; + goto close_sock; + } + + if (output.level.tid_count > 0) { + level->tid_level = (int *)calloc(output.level.tid_count, sizeof(int)); + if (level->tid_level == NULL) { + _E("[CPU-BOOSTING-PLUGIN] Failed to allocate memory"); + ret = -1; + goto close_sock; + } + else + level->tid_count = output.level.tid_count; + + byte = recv(sock, (void *)level->tid_level, level->tid_count * sizeof(int), 0); + if (byte != level->tid_count * sizeof(int)) { + _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", + level->tid_count * (unsigned int)sizeof(int), byte); + free(level->tid_level); + ret = -1; + goto close_sock; + } + } + + if (!output.success) { + _E("[CPU-BOOSTING-PLUGIN] Failed to get boosting"); + ret = -1; + } + else + _D("[CPU-BOOSTING-PLUGIN] Success to get boosting"); + +close_sock: + close(sock); + + return ret; } API int resource_set_cpu_inheritance (pid_t source_tid, const char *dest_process, int timeout_msec) diff --git a/tests/main.c b/tests/main.c index fd4d8e0..1009a8b 100644 --- a/tests/main.c +++ b/tests/main.c @@ -89,7 +89,7 @@ static void test_cpu_boosting(resource_pid_t pid) cpu_boosting_level_info_t cur_level; for (int level = CPU_BOOSTING_LEVEL_WEAK; level > CPU_BOOSTING_LEVEL_NONE; level--) - test_set_cpu_boosting(pid, level, -1); + test_set_cpu_boosting(pid, level, 3000); test_get_cpu_boosting(pid, &cur_level); /* Expect CPU_BOOSTING_LEVEL_STRRONG */ test_clear_cpu_boosting(pid); -- 2.7.4