From e64636438edeb8b842d8ea56cd2cc1d18ebcca97 Mon Sep 17 00:00:00 2001 From: Chanwoo Choi Date: Mon, 4 Jul 2022 12:32:06 +0900 Subject: [PATCH 01/16] util: common: Replace prototype of json helper functions to get correct value The get_int_from_object() returns '-EINVAL' when json object doesn't contain ' key'. But it isn't correct because 'int' value is able to get the minus value. It means that when get_int_from_object() return -EINVAL, it is not error on side of user. To fix them, return the error and then get the value as the parameter. Also, get_double_from_object() has the same issue. Also, in order that keep the consistent prototype of json helper functions, changes the functiosn as following: - int get_int_from_object(json_object *obj, const char *key, int *data); - int get_double_from_object(json_object *obj, const char *key, double *data); - int get_boolean_from_object(json_object *obj, const char *key, bool *data); Change-Id: I0a9b9da36312a0d86920f259b1f702095f104da6 Signed-off-by: Chanwoo Choi --- include/util/common.h | 6 +++--- src/util/common.c | 46 +++++++++++++++++++++++----------------------- 2 files changed, 26 insertions(+), 26 deletions(-) diff --git a/include/util/common.h b/include/util/common.h index f361886..a11475e 100644 --- a/include/util/common.h +++ b/include/util/common.h @@ -127,9 +127,9 @@ int sys_get_str(const char *fname, char *str); int sysfs_get_int(char *path, int *val); const char *get_string_from_object(json_object *obj, const char *key); -const int get_int_from_object(json_object *obj, const char *key); -const double get_double_from_object(json_object *obj, const char *key); -const int get_boolean_from_object(json_object *obj, const char *key); +int get_int_from_object(json_object *obj, const char *key, int *data); +int get_double_from_object(json_object *obj, const char *key, double *data); +int get_boolean_from_object(json_object *obj, const char *key, bool *data); json_object *get_object_from_object(json_object *obj, const char *key); int get_property(json_object *obj, const char *key, int type, bool mandatory, diff --git a/src/util/common.c b/src/util/common.c index aae3582..b9368d5 100644 --- a/src/util/common.c +++ b/src/util/common.c @@ -99,34 +99,40 @@ const char *get_string_from_object(json_object *obj, const char *key) return json_object_get_string(tmp); } -const int get_int_from_object(json_object *obj, const char *key) +int get_int_from_object(json_object *obj, const char *key, int *data) { json_object *tmp = NULL; if (!json_object_object_get_ex(obj, key, &tmp)) return -EINVAL; - return json_object_get_int(tmp); + *data = json_object_get_int(tmp); + + return 0; } -const double get_double_from_object(json_object *obj, const char *key) +int get_double_from_object(json_object *obj, const char *key, double *data) { json_object *tmp = NULL; if (!json_object_object_get_ex(obj, key, &tmp)) return -EINVAL; - return json_object_get_double(tmp); + *data = json_object_get_double(tmp); + + return 0; } -const int get_boolean_from_object(json_object *obj, const char *key) +int get_boolean_from_object(json_object *obj, const char *key, bool *data) { json_object *tmp = NULL; if (!json_object_object_get_ex(obj, key, &tmp)) return -EINVAL; - return (int)json_object_get_boolean(tmp); + *data = json_object_get_boolean(tmp); + + return 0; } json_object *get_object_from_object(json_object *obj, const char *key) @@ -143,25 +149,19 @@ int get_property(json_object *obj, const char *key, int type, bool mandatory, void *data) { int ret = 0; - int val_int; - double val_double; const char *val_str; + bool val_bool; switch (type) { case DATA_TYPE_INT: - val_int = get_int_from_object(obj, key); - if (val_int < 0) - ret = val_int; - else - *(int *)data = val_int; + ret = get_int_from_object(obj, key, (int *)data); + if (ret < 0) + *(int *)data = ret; break; case DATA_TYPE_DOUBLE: - val_double = get_double_from_object(obj, key); - if (val_double < 0) - ret = -EINVAL; - else - *(double *)data = val_double; - break; + ret = get_double_from_object(obj, key, (double *)data); + if (ret < 0) + *(double *)data = (double)ret; case DATA_TYPE_STRING: val_str = get_string_from_object(obj, key); if (!val_str) @@ -170,11 +170,11 @@ int get_property(json_object *obj, const char *key, snprintf((char *)data, BUFF_MAX, "%s", val_str); break; case DATA_TYPE_BOOLEAN: - val_int = get_int_from_object(obj, key); - if (val_int < 0) - ret = val_int; + ret = get_boolean_from_object(obj, key, (bool *)&val_bool); + if (ret < 0) + *(bool *)data = true; else - *(int *)data = val_int; + *(bool *)data = val_bool; break; default: ret = -EINVAL; -- 2.7.4 From e96eeca5255064761ee920da0359189d0af36f54 Mon Sep 17 00:00:00 2001 From: Chanwoo Choi Date: Tue, 5 Jul 2022 19:11:15 +0900 Subject: [PATCH 02/16] pass: parser: Replace with bool data type to prevent mismatched type casting Until now, even if use DATA_TYPE_BOOLEN, use 'int' variable type. It might cause the unknown issue by mismatched type casting on get_property() fucntion. In order to prevent the wrong type casting, replace with bool data type instead of integer type. Change-Id: I51791d72579afc97b73b6a67d9a9711c21c033f4 Signed-off-by: Chanwoo Choi --- src/pass/pass-parser.c | 10 +++++----- src/pmqos/pmqos-parser.c | 8 ++++---- src/thermal/thermal-parser.c | 8 ++++---- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/pass/pass-parser.c b/src/pass/pass-parser.c index 5a9001a..d268050 100644 --- a/src/pass/pass-parser.c +++ b/src/pass/pass-parser.c @@ -108,7 +108,7 @@ static int parse_scenario(struct pass_resource *res, json_object *obj, struct pass_scenario *scenario) { char name[BUFF_MAX] = {}; - int support; + bool support; int target_level; int cpuhp_min_level; int cpuhp_max_level; @@ -117,7 +117,7 @@ static int parse_scenario(struct pass_resource *res, json_object *obj, int temp_end; int threshold; int timer_interval_ms; - int overridable; + bool overridable; int ret = 0; /* Get property values */ @@ -174,7 +174,7 @@ static int parse_scenario(struct pass_resource *res, json_object *obj, /* Initialize config_data from property values of confiugartion file */ snprintf(scenario->name, NAME_MAX, "%s", name); - scenario->state = (support < 0) ? 1 : support; + scenario->state = support; scenario->level = target_level; /* - property for only pmqos module */ @@ -186,7 +186,7 @@ static int parse_scenario(struct pass_resource *res, json_object *obj, scenario->thermal.temp_end = temp_end; scenario->thermal.threshold = threshold; scenario->thermal.timer_interval = timer_interval_ms; - scenario->thermal.overridable = (overridable < 0) ? 1 : overridable; + scenario->thermal.overridable = overridable; return 0; } @@ -685,7 +685,7 @@ static int parse_cpuhp(struct pass_resource *res, json_object *obj) */ static int parse_header(struct pass_resource *res, json_object *obj) { - int support; + bool support; int init_level; json_object *level_list = NULL; int num_levels = 0, ret = 0, i; diff --git a/src/pmqos/pmqos-parser.c b/src/pmqos/pmqos-parser.c index 7cc5a80..845f441 100644 --- a/src/pmqos/pmqos-parser.c +++ b/src/pmqos/pmqos-parser.c @@ -53,7 +53,7 @@ static int pmqos_parse_scenario(json_object *obj, struct scenario *scenario) { char name[BUFF_MAX] = {}; int max_duration_ms; - int support; + bool support; int ret = 0; /* Get property values */ @@ -73,7 +73,7 @@ static int pmqos_parse_scenario(json_object *obj, struct scenario *scenario) /* Initialize config_data from property values of confiugartion file */ snprintf(scenario->name, strlen(name) + 1, "%s", name); scenario->max_duration_ms = (max_duration_ms < 0) ? 0 : max_duration_ms; - scenario->support = (support < 0) ? 1 : support; + scenario->support = support; return 0; } @@ -88,7 +88,7 @@ static int pmqos_parse_scenario(json_object *obj, struct scenario *scenario) */ static int pmqos_load_config(json_object *obj, struct pmqos_scenario *scenarios) { - int pmqos_support; + bool pmqos_support; json_object *pmqos_scenario_list = NULL; int num_scenarios = 0, ret, i; @@ -106,7 +106,7 @@ static int pmqos_load_config(json_object *obj, struct pmqos_scenario *scenarios) } /* Check the validation of property value */ - if (pmqos_support <= 0 || num_scenarios <= 0) { + if (!pmqos_support || num_scenarios <= 0) { _I("Disable PMQOS module\n"); return 0; } diff --git a/src/thermal/thermal-parser.c b/src/thermal/thermal-parser.c index c1660b4..09edc5b 100644 --- a/src/thermal/thermal-parser.c +++ b/src/thermal/thermal-parser.c @@ -50,7 +50,7 @@ static int thermal_parse_scenario(json_object *obj, struct scenario *scenario) { char name[BUFF_MAX] = {}; - int support; + bool support; int ret = 0; /* Get property values */ @@ -67,7 +67,7 @@ static int thermal_parse_scenario(json_object *obj, struct scenario *scenario) /* Initialize config_data from property values of confiugartion file */ snprintf(scenario->name, strlen(name) + 1, "%s", name); - scenario->support = (support < 0) ? 1 : support; + scenario->support = support; return 0; } @@ -81,7 +81,7 @@ static int thermal_parse_scenario(json_object *obj, struct scenario *scenario) */ static int thermal_load_config(json_object *obj, struct thermal_scenario *scenarios) { - int thermal_support; + bool thermal_support; json_object *thermal_scenario_list = NULL; int num_scenarios = 0, ret = 0, i; @@ -99,7 +99,7 @@ static int thermal_load_config(json_object *obj, struct thermal_scenario *scenar } /* Check the validation of property value */ - if (thermal_support <= 0 || num_scenarios <= 0) { + if (!thermal_support || num_scenarios <= 0) { _I("Disable thermal module\n"); return 0; } -- 2.7.4 From 3393781e7dd5e19df1205bc8512fe84a8c1dbcf8 Mon Sep 17 00:00:00 2001 From: Chanwoo Choi Date: Tue, 5 Jul 2022 03:53:39 +0900 Subject: [PATCH 03/16] util: kernel: Remove unused variable to fix build warning Fix the following warning. /home/abuild/rpmbuild/BUILD/pass-1.2.0/src/util/kernel.c: In function 'get_gpu_mem_info': /home/abuild/rpmbuild/BUILD/pass-1.2.0/src/util/kernel.c:435:6: warning: variable 'n' set but not used [-Wunused-but-set-variable] | int n; Change-Id: Ie763f9bf4f2aa61aa22535ca0209871359306419 Signed-off-by: Chanwoo Choi --- src/util/kernel.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/util/kernel.c b/src/util/kernel.c index 2b60f3e..e714c6a 100644 --- a/src/util/kernel.c +++ b/src/util/kernel.c @@ -432,10 +432,9 @@ get_gpu_mem_info(struct proc_map_info *map_info, FILE *smaps_fd, char *entry) struct gpu_mem_node *gnode = NULL; char name[BUFF_MAX]; char buffer[BUFF_MAX]; - int n; new_entry: - n = sscanf(entry, "%*s %*s %*s %*s %*s %[^\n]", name); + sscanf(entry, "%*s %*s %*s %*s %*s %[^\n]", name); gnode = get_gpu_mem_node(name); if (gnode) { -- 2.7.4 From df48eead3b4382eade57857dac6b054d441b8a05 Mon Sep 17 00:00:00 2001 From: Dongwoo Lee Date: Fri, 1 Jul 2022 14:39:58 +0900 Subject: [PATCH 04/16] util: kernel: gpu-mem: Fix missing header file Change-Id: Iec6278b4a5cab50315b3f42f28cc12205d5d2ee7 Signed-off-by: Dongwoo Lee --- include/util/gpu-mem.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/include/util/gpu-mem.h b/include/util/gpu-mem.h index bda0426..cdf865e 100644 --- a/include/util/gpu-mem.h +++ b/include/util/gpu-mem.h @@ -19,6 +19,8 @@ #ifndef __GPU_MEM_H__ #define __GPU_MEM_H__ +#include + struct gpu_mem_node { char *name; char *path; -- 2.7.4 From 92ec572910fa6bfca7199ee3cd75989c29f61b94 Mon Sep 17 00:00:00 2001 From: Dongwoo Lee Date: Mon, 4 Jul 2022 11:14:38 +0900 Subject: [PATCH 05/16] util: kernel: gpu-mem: Improve process for finding gpu node To get rid of redundant gpu node checking at every update, finding gpu node will be proceeded at once as constructor. Change-Id: I30c18784f578e224e812c9ee9e0d9a4704cf901a Signed-off-by: Dongwoo Lee --- include/util/gpu-mem.h | 33 ++++++++++++++++++++++++--------- include/util/kernel.h | 1 + src/util/kernel.c | 28 ++++++++++++++++++++-------- 3 files changed, 45 insertions(+), 17 deletions(-) diff --git a/include/util/gpu-mem.h b/include/util/gpu-mem.h index cdf865e..ce69b1c 100644 --- a/include/util/gpu-mem.h +++ b/include/util/gpu-mem.h @@ -19,6 +19,7 @@ #ifndef __GPU_MEM_H__ #define __GPU_MEM_H__ +#include #include struct gpu_mem_node { @@ -32,20 +33,34 @@ struct gpu_mem_node { * if pass-hal provides gpu information. */ static struct gpu_mem_node gpu_mem_nodes[] = { - { .name = "SGX", .path = "/dev/pvrsrvkm", .node = "Pss:" }, - { .name = "MALI", .path = "/dev/mali", .node = "Size:"}, - { .name = "KGSL", .path = "/dev/kgsl-3d0", .node = "Size:"}, + { .name = "SGX", .path = "pvrsrvkm", .node = "Pss:" }, + { .name = "MALI", .path = "mali", .node = "Size:"}, + { .name = "KGSL", .path = "kgsl-3d0", .node = "Size:"}, }; -static inline struct gpu_mem_node *get_gpu_mem_node(const char *name) +static inline struct gpu_mem_node *get_system_gpu_mem_node(void) { + struct gpu_mem_node *node = NULL; + struct dirent *dev_entry; + DIR *dev_dir; int i; - for (i = 0; i < ARRAY_SIZE(gpu_mem_nodes); i++) - if (strstr(name, gpu_mem_nodes[i].path)) - return &gpu_mem_nodes[i]; + dev_dir = opendir("/dev/"); - return NULL; -} + while ((dev_entry = readdir(dev_dir)) != NULL) { + if (dev_entry->d_type == DT_DIR && dev_entry->d_type != DT_UNKNOWN) + continue; + + for (i = 0; i < ARRAY_SIZE(gpu_mem_nodes); i++) { + if (strstr(dev_entry->d_name, gpu_mem_nodes[i].path)) { + node = &gpu_mem_nodes[i]; + break; + } + } + } + closedir(dev_dir); + + return node; +} #endif diff --git a/include/util/kernel.h b/include/util/kernel.h index b3b9a3d..12ae6e9 100644 --- a/include/util/kernel.h +++ b/include/util/kernel.h @@ -68,4 +68,5 @@ int kernel_get_process_taskstats(struct taskstats *stats, int cmd_type, pid_t pi int kernel_get_thread_group_taskstats(struct taskstats *stats, pid_t tgid, bool get_proc_info); int kernel_get_thread_group_map_info(struct proc_map_info *map_info, pid_t tgid, bool include_gpu_mem); +bool kernel_check_gpu_support(void); #endif diff --git a/src/util/kernel.c b/src/util/kernel.c index e714c6a..e53e2fb 100644 --- a/src/util/kernel.c +++ b/src/util/kernel.c @@ -29,6 +29,7 @@ #include static int have_smaps_rollup; +static struct gpu_mem_node *gpu_mem_node; static int __get_cpu_num(char *path) { @@ -426,21 +427,22 @@ static inline bool is_new_entry(const char *str) return ((*str >= '0' && *str <= '9') || (*str >= 'a' && *str <= 'f')); } -static void -get_gpu_mem_info(struct proc_map_info *map_info, FILE *smaps_fd, char *entry) +static unsigned long get_gpu_mem_size(FILE *smaps_fd, char *entry) { - struct gpu_mem_node *gnode = NULL; + unsigned long mem_size = 0; char name[BUFF_MAX]; char buffer[BUFF_MAX]; + if (!gpu_mem_node) + return 0; + new_entry: sscanf(entry, "%*s %*s %*s %*s %*s %[^\n]", name); - gnode = get_gpu_mem_node(name); - if (gnode) { + if (strstr(name, gpu_mem_node->path)) { while (fgets(buffer, sizeof(buffer), smaps_fd)) { - if (strstr(buffer, gnode->node)) - map_info->gpu_mem += strtol(buffer + strlen(gnode->node), NULL, 10); + if (strstr(buffer, gpu_mem_node->node)) + mem_size += strtol(buffer + strlen(gpu_mem_node->node), NULL, 10); if (is_new_entry(buffer)) { entry = buffer; @@ -448,6 +450,8 @@ new_entry: } } } + + return mem_size; } int kernel_get_thread_group_map_info(struct proc_map_info *map_info, @@ -466,9 +470,11 @@ int kernel_get_thread_group_map_info(struct proc_map_info *map_info, return -ENOENT; } + memset(map_info, 0, sizeof(struct proc_map_info)); + while (fgets(buffer, sizeof(buffer), smaps_fd)) { if (include_gpu_mem && is_new_entry(buffer)) - get_gpu_mem_info(map_info, smaps_fd, buffer); + map_info->gpu_mem += get_gpu_mem_size(smaps_fd, buffer); if (strstr(buffer, "Rss:")) map_info->rss += strtol(buffer + 4, NULL, 10); @@ -485,7 +491,13 @@ int kernel_get_thread_group_map_info(struct proc_map_info *map_info, return 0; } +bool kernel_check_gpu_support(void) +{ + return !!gpu_mem_node; +} + static void __CONSTRUCTOR__ kernel_init(void) { have_smaps_rollup = !access("/proc/self/smaps_rollup", R_OK); + gpu_mem_node = get_system_gpu_mem_node(); } -- 2.7.4 From 5484efb0fd69b10b0ddea85f00a22173b1cc1498 Mon Sep 17 00:00:00 2001 From: Dongwoo Lee Date: Mon, 4 Jul 2022 11:41:38 +0900 Subject: [PATCH 06/16] resource: process-group: Add checking support for PROCESS_GROUP_ATTR_GPU_MEM Change-Id: Iefbaa9526f62fe1af5fb054f8cbacbe81863bb9b Signed-off-by: Dongwoo Lee --- src/resource/resource-process-group.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/resource/resource-process-group.c b/src/resource/resource-process-group.c index f126d60..b0b2ade 100644 --- a/src/resource/resource-process-group.c +++ b/src/resource/resource-process-group.c @@ -230,6 +230,12 @@ static int process_group_get_disk_bps(const struct resource *res, return 0; } +static bool process_group_check_gpu_support(const struct resource *resource, + const struct resource_attribute *attr) +{ + return kernel_check_gpu_support(); +} + static const struct resource_attribute process_group_attrs[] = { { .name = "PROCESS_GROUP_ATTR_PID_LIST", @@ -317,7 +323,7 @@ static const struct resource_attribute process_group_attrs[] = { .type = DATA_TYPE_UINT64, .ops = { .get = process_group_get_mem, - .is_supported = resource_attr_supported_always, + .is_supported = process_group_check_gpu_support, }, }, }; -- 2.7.4 From 5b88ee60d6f60e0997b3c036911ad3432adcc88b Mon Sep 17 00:00:00 2001 From: Dongwoo Lee Date: Mon, 4 Jul 2022 15:19:07 +0900 Subject: [PATCH 07/16] resource: process: Add checking support for PROCESS_ATTR_GPU_MEM Change-Id: Icc7f49ee785f668f8e6afc68c6960a4d4c81c438 Signed-off-by: Dongwoo Lee --- src/resource/resource-process.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/resource/resource-process.c b/src/resource/resource-process.c index b59c06b..1d9b39a 100644 --- a/src/resource/resource-process.c +++ b/src/resource/resource-process.c @@ -209,6 +209,12 @@ static int process_get_context_data(const struct resource *res, return 0; } +static bool process_check_gpu_support(const struct resource *resource, + const struct resource_attribute *attr) +{ + return kernel_check_gpu_support(); +} + static const struct resource_attribute process_attrs[] = { { .name = "PROCESS_ATTR_CPU_UTIL", @@ -312,7 +318,7 @@ static const struct resource_attribute process_attrs[] = { .type = DATA_TYPE_UINT64, .ops = { .get = process_get_mem_attrs, - .is_supported = resource_attr_supported_always, + .is_supported = process_check_gpu_support, }, }, }; -- 2.7.4 From dc0876d6aaa583fb46792b4412fcbe72b413b9b5 Mon Sep 17 00:00:00 2001 From: Dongwoo Lee Date: Tue, 5 Jul 2022 19:09:51 +0900 Subject: [PATCH 08/16] util: kernel: Fix to check memory map info precisely Since both "Pss:" and "SwapPss:" nodes has "Pss:" as substring, so just strstr() cannot distinguish them. To fix it, not just check section including node name, but check section starting with node name. Change-Id: Ia113d57b84414b9483b098920634c7f9f4b7461d Signed-off-by: Dongwoo Lee --- src/util/kernel.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/util/kernel.c b/src/util/kernel.c index e53e2fb..0a64734 100644 --- a/src/util/kernel.c +++ b/src/util/kernel.c @@ -454,6 +454,8 @@ new_entry: return mem_size; } +#define strstart(str1, str2) (strstr(str1, str2) == str1) + int kernel_get_thread_group_map_info(struct proc_map_info *map_info, pid_t tgid, bool include_gpu_mem) { @@ -476,13 +478,13 @@ int kernel_get_thread_group_map_info(struct proc_map_info *map_info, if (include_gpu_mem && is_new_entry(buffer)) map_info->gpu_mem += get_gpu_mem_size(smaps_fd, buffer); - if (strstr(buffer, "Rss:")) + if (strstart(buffer, "Rss:")) map_info->rss += strtol(buffer + 4, NULL, 10); - else if (strstr(buffer, "Pss:")) + else if (strstart(buffer, "Pss:")) map_info->pss += strtol(buffer + 4, NULL, 10); - else if (strstr(buffer, "Swap:")) + else if (strstart(buffer, "Swap:")) map_info->swap += strtol(buffer + 5, NULL, 10); - else if (strstr(buffer, "SwapPss:")) + else if (strstart(buffer, "SwapPss:")) map_info->swap_pss += strtol(buffer + 8, NULL, 10); } -- 2.7.4 From e45c248c8ddd067b9de838af54fc682f19b48a34 Mon Sep 17 00:00:00 2001 From: Chanwoo Choi Date: Tue, 5 Jul 2022 07:37:38 +0900 Subject: [PATCH 09/16] test: intergration-test: Add resource-monitor-tests In order that test the resource-monitor library and internal logic, add resource-monitor-tests. [Example, the result of resource-monitor-tests] root:~> resource-monitor-tests [==========] Running 16 tests from 2 test suites. [----------] Global test environment set-up. [----------] 4 tests from ResourceMonitorTest [ RUN ] ResourceMonitorTest.pass_resource_monitor_init_and_exit [ OK ] ResourceMonitorTest.pass_resource_monitor_init_and_exit (0 ms) [ RUN ] ResourceMonitorTest.pass_resource_monitor_get_resource_count_valid [ OK ] ResourceMonitorTest.pass_resource_monitor_get_resource_count_valid (2 ms) [ RUN ] ResourceMonitorTest.pass_resource_monitor_get_resource_count_invalid [ OK ] ResourceMonitorTest.pass_resource_monitor_get_resource_count_invalid (0 ms) [ RUN ] ResourceMonitorTest.pass_resource_monitor_is_resource_attr_supported_invalid [ OK ] ResourceMonitorTest.pass_resource_monitor_is_resource_attr_supported_invalid (1 ms) [----------] 4 tests from ResourceMonitorTest (6 ms total) [----------] 12 tests from ResourceMonitorTest/EachResourceMonitorTest [ RUN ] ResourceMonitorTest/EachResourceMonitorTest.pass_resource_monitor_create_resource_and_delete/0 [ OK ] ResourceMonitorTest/EachResourceMonitorTest.pass_resource_monitor_create_resource_and_delete/0 (0 ms) [ RUN ] ResourceMonitorTest/EachResourceMonitorTest.pass_resource_monitor_create_resource_and_delete/1 [ OK ] ResourceMonitorTest/EachResourceMonitorTest.pass_resource_monitor_create_resource_and_delete/1 (0 ms) [ RUN ] ResourceMonitorTest/EachResourceMonitorTest.pass_resource_monitor_create_resource_and_delete/2 [ OK ] ResourceMonitorTest/EachResourceMonitorTest.pass_resource_monitor_create_resource_and_delete/2 (0 ms) [ RUN ] ResourceMonitorTest/EachResourceMonitorTest.pass_resource_monitor_set_resource_ctrl/0 [ OK ] ResourceMonitorTest/EachResourceMonitorTest.pass_resource_monitor_set_resource_ctrl/0 (1 ms) [ RUN ] ResourceMonitorTest/EachResourceMonitorTest.pass_resource_monitor_set_resource_ctrl/1 [ OK ] ResourceMonitorTest/EachResourceMonitorTest.pass_resource_monitor_set_resource_ctrl/1 (0 ms) [ RUN ] ResourceMonitorTest/EachResourceMonitorTest.pass_resource_monitor_set_resource_ctrl/2 [ OK ] ResourceMonitorTest/EachResourceMonitorTest.pass_resource_monitor_set_resource_ctrl/2 (0 ms) [ RUN ] ResourceMonitorTest/EachResourceMonitorTest.pass_resource_monitor_set_resource_attr/0 [ OK ] ResourceMonitorTest/EachResourceMonitorTest.pass_resource_monitor_set_resource_attr/0 (2 ms) [ RUN ] ResourceMonitorTest/EachResourceMonitorTest.pass_resource_monitor_set_resource_attr/1 [ OK ] ResourceMonitorTest/EachResourceMonitorTest.pass_resource_monitor_set_resource_attr/1 (0 ms) [ RUN ] ResourceMonitorTest/EachResourceMonitorTest.pass_resource_monitor_set_resource_attr/2 [ OK ] ResourceMonitorTest/EachResourceMonitorTest.pass_resource_monitor_set_resource_attr/2 (0 ms) [ RUN ] ResourceMonitorTest/EachResourceMonitorTest.pass_resource_monitor_get_value/0 [ OK ] ResourceMonitorTest/EachResourceMonitorTest.pass_resource_monitor_get_value/0 (3 ms) [ RUN ] ResourceMonitorTest/EachResourceMonitorTest.pass_resource_monitor_get_value/1 [ OK ] ResourceMonitorTest/EachResourceMonitorTest.pass_resource_monitor_get_value/1 (0 ms) [ RUN ] ResourceMonitorTest/EachResourceMonitorTest.pass_resource_monitor_get_value/2 [ OK ] ResourceMonitorTest/EachResourceMonitorTest.pass_resource_monitor_get_value/2 (0 ms) [----------] 12 tests from ResourceMonitorTest/EachResourceMonitorTest (15 ms total) [----------] Global test environment tear-down [==========] 16 tests from 2 test suites ran. (21 ms total) [ PASSED ] 16 tests. Change-Id: Id15d6948849decedcbe0f48bee083667afdc996b Signed-off-by: Chanwoo Choi --- packaging/pass.spec | 1 + tests/integration-test/CMakeLists.txt | 9 + tests/integration-test/resource-monitor-tests.cpp | 461 ++++++++++++++++++++++ 3 files changed, 471 insertions(+) create mode 100644 tests/integration-test/resource-monitor-tests.cpp diff --git a/packaging/pass.spec b/packaging/pass.spec index 74c735a..0168238 100644 --- a/packaging/pass.spec +++ b/packaging/pass.spec @@ -127,6 +127,7 @@ systemctl daemon-reload %files -n %{unittest_name} %defattr(-,root,root,-) %{_bindir}/pass-tests +%{_bindir}/resource-monitor-tests %files -n %{libpass_resource_monitor_name} %license LICENSE diff --git a/tests/integration-test/CMakeLists.txt b/tests/integration-test/CMakeLists.txt index 2f12af5..69ec8ff 100644 --- a/tests/integration-test/CMakeLists.txt +++ b/tests/integration-test/CMakeLists.txt @@ -3,12 +3,14 @@ PROJECT(pass C CXX) SET(SRCS ${CMAKE_SOURCE_DIR}/src/pass/pass-hal.c ${CMAKE_SOURCE_DIR}/src/pass/pass-parser.c ${CMAKE_SOURCE_DIR}/src/util/common.c + ${CMAKE_SOURCE_DIR}/lib/resource-monitor/resource-monitor.c ) INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}) INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/src) INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/src/pass) INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include) +INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/lib/resource-monitor) INCLUDE(FindPkgConfig) pkg_check_modules(gtest_pkgs REQUIRED @@ -35,3 +37,10 @@ MESSAGE("${src_name}") ADD_EXECUTABLE(${src_name} ${SRCS} ${src}) TARGET_LINK_LIBRARIES(${src_name} ${gtest_LDFLAGS} ${gtest_pkgs_LDFLAGS} -ldl -L${LIBDIR}/hal) INSTALL(TARGETS ${src_name} DESTINATION /usr/bin/) + +SET(src ${CMAKE_SOURCE_DIR}/tests/integration-test/resource-monitor-tests.cpp) +GET_FILENAME_COMPONENT(src_name ${src} NAME_WE) +MESSAGE("${src_name}") +ADD_EXECUTABLE(${src_name} ${SRCS} ${src}) +TARGET_LINK_LIBRARIES(${src_name} ${gtest_LDFLAGS} ${gtest_pkgs_LDFLAGS} -ldl -L${LIBDIR}/hal) +INSTALL(TARGETS ${src_name} DESTINATION /usr/bin/) diff --git a/tests/integration-test/resource-monitor-tests.cpp b/tests/integration-test/resource-monitor-tests.cpp new file mode 100644 index 0000000..71fd72f --- /dev/null +++ b/tests/integration-test/resource-monitor-tests.cpp @@ -0,0 +1,461 @@ +/* + * Copyright (C) 2022 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include + +#include +#include +#include + +#include + +#include +#include + +#include +#include +#include +#include + +#include "resource-monitor.h" + +#define UNKNOWN_ID 9999 + +using namespace std; + +class ResourceMonitorTest : public testing::Test { +public: + void SetUp() override {} + void TearDown() override {} +}; + +TEST_F(ResourceMonitorTest, pass_resource_monitor_init_and_exit) +{ + int id = pass_resource_monitor_init(); + EXPECT_TRUE(id > 0); + + pass_resource_monitor_exit(id); +} + +TEST_F(ResourceMonitorTest, pass_resource_monitor_get_resource_count_valid) +{ + int i; + int res_types[] = { + RESOURCE_TYPE_CPU, + RESOURCE_TYPE_BUS, + RESOURCE_TYPE_GPU, + RESOURCE_TYPE_MEMORY, + RESOURCE_TYPE_BATTERY, + RESOURCE_TYPE_PROCESS, + RESOURCE_TYPE_DISPLAY, + RESOURCE_TYPE_SYSTEM, + RESOURCE_TYPE_PROCESS_GROUP, + }; + + int id = pass_resource_monitor_init(); + EXPECT_TRUE(id > 0); + + for (i = 0; i < (int)ARRAY_SIZE(res_types); i++) { + int ret = pass_resource_monitor_get_resource_count(id, res_types[i]); + EXPECT_TRUE(ret >= 0); + } + + pass_resource_monitor_exit(id); +} + +TEST_F(ResourceMonitorTest, pass_resource_monitor_get_resource_count_invalid) +{ + int i; + int res_types[] = { + RESOURCE_TYPE_UNKNOWN, + RESOURCE_TYPE_NONSTANDARD, + }; + + int id = pass_resource_monitor_init(); + EXPECT_TRUE(id > 0); + + for (i = 0; i < (int)ARRAY_SIZE(res_types); i++) { + int ret = pass_resource_monitor_get_resource_count(id, res_types[i]); + EXPECT_EQ(ret, 0); + } + + pass_resource_monitor_exit(id); +} + +TEST_F(ResourceMonitorTest, + pass_resource_monitor_is_resource_attr_supported_invalid) +{ + int i, j; + int res_type = RESOURCE_TYPE_CPU; + u_int64_t res_attr_ids[] = { + BIT(50), + BIT(51), + BIT(52), + }; + + int id = pass_resource_monitor_init(); + EXPECT_TRUE(id > 0); + + int count = pass_resource_monitor_get_resource_count(id, res_type); + EXPECT_TRUE(count >= 0); + + for (i = 0; i < count; i++) { + int res_id = pass_resource_monitor_create_resource(id, res_type); + EXPECT_TRUE(res_id >= 0); + + int ret = pass_resource_monitor_set_resource_ctrl(id, res_id, + CPU_CTRL_CLUSTER_ID, i); + EXPECT_EQ(ret, 0); + + for (j = 0; j < (int)ARRAY_SIZE(res_attr_ids); j++) { + bool supported = pass_resource_monitor_is_resource_attr_supported( + id, res_id, + res_attr_ids[j]); + EXPECT_EQ(supported, false); + + supported = pass_resource_monitor_is_resource_attr_supported( + UNKNOWN_ID, res_id, + res_attr_ids[j]); + EXPECT_EQ(supported, false); + + supported = pass_resource_monitor_is_resource_attr_supported( + id, UNKNOWN_ID, + res_attr_ids[j]); + EXPECT_EQ(supported, false); + } + + ret = pass_resource_monitor_delete_resource(id, res_id); + EXPECT_EQ(ret, 0); + } + + pass_resource_monitor_exit(id); +} + +struct resource_attr_info { + u_int64_t attr_id; + int attr_type; +}; + +class ResourceInfo { +public: + const int type; + const char *name; + int mon_id; + int res_id; + int count; + const u_int64_t ctrl_id; + const int num_attrs; + vector attrs; + + ResourceInfo( + const int type_, + const char *name_, + int mon_id_, + int res_id_, + int count_, + const u_int64_t ctrl_id_, + const int num_attrs_, + vector attrs_) : + type(type_), + name(name_), + mon_id(mon_id_), + res_id(res_id_), + count(count_), + ctrl_id(ctrl_id_), + num_attrs(num_attrs_), + attrs(attrs_) {} +}; + +class EachResourceMonitorTest : public ::testing::TestWithParam {}; + +INSTANTIATE_TEST_CASE_P (ResourceMonitorTest, EachResourceMonitorTest, + ::testing::Values ( + ResourceInfo ( + RESOURCE_TYPE_CPU, + "RESOURCE_TYPE_CPU", + -1, + -1, + 0, + CPU_CTRL_CLUSTER_ID, + 5, + vector { + { .attr_id = CPU_ATTR_CUR_FREQ, .attr_type = DATA_TYPE_INT, }, + { .attr_id = CPU_ATTR_MIN_FREQ, .attr_type = DATA_TYPE_INT, }, + { .attr_id = CPU_ATTR_MAX_FREQ, .attr_type = DATA_TYPE_INT, }, + { .attr_id = CPU_ATTR_AVAILABLE_MIN_FREQ, .attr_type = DATA_TYPE_INT, }, + { .attr_id = CPU_ATTR_AVAILABLE_MAX_FREQ, .attr_type = DATA_TYPE_INT, }, + } + ), + + ResourceInfo ( + RESOURCE_TYPE_BUS, + "RESOURCE_TYPE_BUS", + -1, + -1, + 0, + BUS_CTRL_DEVICE_ID, + 5, + vector { + { .attr_id = BUS_ATTR_CUR_FREQ, .attr_type = DATA_TYPE_INT, }, + { .attr_id = BUS_ATTR_MIN_FREQ, .attr_type = DATA_TYPE_INT, }, + { .attr_id = BUS_ATTR_MAX_FREQ, .attr_type = DATA_TYPE_INT, }, + { .attr_id = BUS_ATTR_AVAILABLE_MIN_FREQ, .attr_type = DATA_TYPE_INT, }, + { .attr_id = BUS_ATTR_AVAILABLE_MAX_FREQ, .attr_type = DATA_TYPE_INT, }, + } + ), + + ResourceInfo ( + RESOURCE_TYPE_GPU, + "RESOURCE_TYPE_GPU", + -1, + -1, + 0, + GPU_CTRL_DEVICE_ID, + 6, + vector { + { .attr_id = GPU_ATTR_CUR_FREQ, .attr_type = DATA_TYPE_INT, }, + { .attr_id = GPU_ATTR_MIN_FREQ, .attr_type = DATA_TYPE_INT, }, + { .attr_id = GPU_ATTR_MAX_FREQ, .attr_type = DATA_TYPE_INT, }, + { .attr_id = GPU_ATTR_AVAILABLE_MIN_FREQ, .attr_type = DATA_TYPE_INT, }, + { .attr_id = GPU_ATTR_AVAILABLE_MAX_FREQ, .attr_type = DATA_TYPE_INT, }, + { .attr_id = GPU_ATTR_CUR_GOVERNOR, .attr_type = DATA_TYPE_STRING, }, + } + ) +)); + +TEST_P(EachResourceMonitorTest, pass_resource_monitor_create_resource_and_delete) +{ + auto res = GetParam(); + int i; + + res.mon_id = pass_resource_monitor_init(); + ASSERT_TRUE(res.mon_id > 0); + + res.count = pass_resource_monitor_get_resource_count(res.mon_id, res.type); + EXPECT_TRUE(res.count >= 0); + + for (i = 0; i < res.count; i++) { + /* Test pass_resource_monitor_create_resource */ + res.res_id = pass_resource_monitor_create_resource(res.mon_id, res.type); + EXPECT_TRUE(res.res_id >= 0); + + /* Test pass_resource_monitor_delete_resource */ + int ret = pass_resource_monitor_delete_resource(res.mon_id, res.res_id); + EXPECT_EQ(ret, 0); + } + + pass_resource_monitor_exit(res.mon_id); +} + +TEST_P(EachResourceMonitorTest, pass_resource_monitor_set_resource_ctrl) +{ + auto res = GetParam(); + int i; + + res.mon_id = pass_resource_monitor_init(); + ASSERT_TRUE(res.mon_id > 0); + + res.count = pass_resource_monitor_get_resource_count(res.mon_id, res.type); + EXPECT_TRUE(res.count >= 0); + + for (i = 0; i < res.count; i++) { + res.res_id = pass_resource_monitor_create_resource(res.mon_id, res.type); + EXPECT_TRUE(res.res_id >= 0); + + /* Test pass_resource_monitor_set_resource_ctrl */ + int ret = pass_resource_monitor_set_resource_ctrl(res.mon_id, res.res_id, + res.ctrl_id, i); + if (res.ctrl_id > 0 ) + EXPECT_EQ(ret, 0); + else + EXPECT_NE(ret, 0); + + ret = pass_resource_monitor_delete_resource(res.mon_id, res.res_id); + EXPECT_EQ(ret, 0); + } + + pass_resource_monitor_exit(res.mon_id); +} + +static int __pass_resource_monitor_set_resource_attr(int mon_id, int res_id, int num_attrs, + std::vector attrs) +{ + int i, ret; + u_int64_t mask = 0; + + for (i = 0; i < num_attrs; i++) { + bool supported = pass_resource_monitor_is_resource_attr_supported( + mon_id, res_id, attrs[i].attr_id); + if (supported) + mask |= attrs[i].attr_id; + } + + ret = pass_resource_monitor_set_resource_attr(mon_id, res_id, mask); + EXPECT_EQ(ret, 0); + + return ret; +} + +TEST_P(EachResourceMonitorTest, pass_resource_monitor_set_resource_attr) +{ + auto res = GetParam(); + int i; + + res.mon_id = pass_resource_monitor_init(); + ASSERT_TRUE(res.mon_id > 0); + + res.count = pass_resource_monitor_get_resource_count(res.mon_id, res.type); + EXPECT_TRUE(res.count >= 0); + + for (i = 0; i < res.count; i++) { + res.res_id = pass_resource_monitor_create_resource(res.mon_id, res.type); + EXPECT_TRUE(res.res_id >= 0); + + /* + * Test both pass_resource_monitor_set_resource_attr + * and pass_resource_monitor_is_resource_attr_supported + * */ + int ret = pass_resource_monitor_set_resource_ctrl(res.mon_id, res.res_id, + res.ctrl_id, i); + if (res.ctrl_id > 0 ) { + ret = __pass_resource_monitor_set_resource_attr(res.mon_id, + res.res_id, + res.num_attrs, + res.attrs); + EXPECT_EQ(ret, 0); + } else { + EXPECT_NE(ret, 0); + } + + ret = pass_resource_monitor_delete_resource(res.mon_id, res.res_id); + EXPECT_EQ(ret, 0); + } + + pass_resource_monitor_exit(res.mon_id); +} + +static int __pass_resource_monitor_get_value(int mon_id, int res_id, int num_attrs, + std::vector attrs) +{ + int i, ret, err_count = 0; + + for (i = 0; i < num_attrs; i++) { + bool supported = pass_resource_monitor_is_resource_attr_supported( + mon_id, res_id, attrs[i].attr_id); + if (!supported) + continue; + + switch (attrs[i].attr_type) { + case DATA_TYPE_INT: + int32_t value_int32; + ret = pass_resource_monitor_get_value_int(mon_id, res_id, + attrs[i].attr_id, &value_int32); + EXPECT_EQ(ret, 0); + break; + case DATA_TYPE_INT64: + int64_t value_int64; + ret = pass_resource_monitor_get_value_int64(mon_id, res_id, + attrs[i].attr_id, &value_int64); + EXPECT_EQ(ret, 0); + break; + case DATA_TYPE_UINT: + u_int32_t value_uint32; + ret = pass_resource_monitor_get_value_uint(mon_id, res_id, + attrs[i].attr_id, &value_uint32); + EXPECT_EQ(ret, 0); + break; + case DATA_TYPE_UINT64: + u_int64_t value_uint64; + ret = pass_resource_monitor_get_value_uint64(mon_id, res_id, + attrs[i].attr_id, &value_uint64); + EXPECT_EQ(ret, 0); + break; + case DATA_TYPE_DOUBLE: + double value_double; + ret = pass_resource_monitor_get_value_double(mon_id, res_id, + attrs[i].attr_id, &value_double); + EXPECT_EQ(ret, 0); + break; + case DATA_TYPE_STRING: + char value_str[BUFF_MAX]; + ret = pass_resource_monitor_get_value_string(mon_id, res_id, + attrs[i].attr_id, value_str); + EXPECT_EQ(ret, 0); + break; + default: + ret = -EINVAL; + EXPECT_EQ(ret, 0); + break; + } + + if (ret < 0) + err_count++; + } + + return err_count ? -EINVAL : 0; +} + +TEST_P(EachResourceMonitorTest, pass_resource_monitor_get_value) +{ + auto res = GetParam(); + int i; + + res.mon_id = pass_resource_monitor_init(); + ASSERT_TRUE(res.mon_id > 0); + + res.count = pass_resource_monitor_get_resource_count(res.mon_id, res.type); + EXPECT_TRUE(res.count >= 0); + + for (i = 0; i < res.count; i++) { + res.res_id = pass_resource_monitor_create_resource(res.mon_id, res.type); + EXPECT_TRUE(res.res_id >= 0); + + int ret = pass_resource_monitor_set_resource_ctrl(res.mon_id, res.res_id, + res.ctrl_id, i); + if (res.ctrl_id > 0 ) { + ret = __pass_resource_monitor_set_resource_attr(res.mon_id, + res.res_id, + res.num_attrs, + res.attrs); + EXPECT_EQ(ret, 0); + } else { + EXPECT_NE(ret, 0); + } + + /* Test both pass_resource_monitor_get_value_[data type] */ + ret = __pass_resource_monitor_get_value(res.mon_id, + res.res_id, + res.num_attrs, + res.attrs); + EXPECT_EQ(ret, 0); + + ret = pass_resource_monitor_delete_resource(res.mon_id, res.res_id); + EXPECT_EQ(ret, 0); + } + + pass_resource_monitor_exit(res.mon_id); +} + +int main(int argc, char *argv[]) +{ + try { + testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); + } catch (...) { + return EXIT_FAILURE; + } +} -- 2.7.4 From af1f87f100cfafb316285c5a8349530fe379ed62 Mon Sep 17 00:00:00 2001 From: Chanwoo Choi Date: Wed, 6 Jul 2022 18:16:29 +0900 Subject: [PATCH 10/16] monitor: request-handler: Fix wrong hash function when using integer key The glib hash supports the various hash type. The resource-monitor uses the integer value of resource id as the key of hash. If want to use the integer key value, have to use 'g_direct_hash'[1] function and 'g_direct_equal' function to compare the integer key instead of pointer key for finding the inserted data. [1] https://docs.gtk.org/glib/func.direct_hash.html [2] https://docs.gtk.org/glib/func.direct_equal.html Fix the following issues by this patch: [ FAILED ] ResourceMonitorTest/EachResourceMonitorTest.pass_resource_monitor_set_resource_ctrl/0 [ FAILED ] ResourceMonitorTest/EachResourceMonitorTest.pass_resource_monitor_set_resource_attr/0 [ FAILED ] ResourceMonitorTest/EachResourceMonitorTest.pass_resource_monitor_get_value/0 Change-Id: I128fd800d44e5709d55d0c7a44a80db94e6d83d8 Signed-off-by: Chanwoo Choi --- src/monitor/request-handler.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/monitor/request-handler.c b/src/monitor/request-handler.c index 184631d..482132a 100644 --- a/src/monitor/request-handler.c +++ b/src/monitor/request-handler.c @@ -55,13 +55,13 @@ static void update_resource(gpointer key, gpointer value, gpointer user_data) static void register_resource_to_client(struct request_client *client, struct resource *res) { - g_hash_table_insert(client->resource_table, (gpointer)&res->id, (gpointer)res); -} + g_hash_table_insert(client->resource_table, GINT_TO_POINTER(res->id), + (gpointer)res); static void unregister_resource_from_client(struct request_client *client, int resource_id) { - g_hash_table_remove(client->resource_table, (gpointer)&resource_id); + g_hash_table_remove(client->resource_table, GINT_TO_POINTER(resource_id)); } static struct resource * @@ -69,7 +69,7 @@ get_resource_by_id(struct request_client *client, int resource_id) { struct resource *res; - res = g_hash_table_lookup(client->resource_table, (gpointer)&resource_id); + res = g_hash_table_lookup(client->resource_table, GINT_TO_POINTER(resource_id)); return res; } @@ -830,7 +830,7 @@ int create_request_client(int socket_fd) } client->socket_fd = socket_fd; - client->resource_table = g_hash_table_new_full(g_int_hash, g_int_equal, + client->resource_table = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, (GDestroyNotify)delete_resource); create_daemon_thread(&client->worker, request_handler_func, client); -- 2.7.4 From 992b6df22b83f005be20d857c0fa1ab8b7b7e71e Mon Sep 17 00:00:00 2001 From: Chanwoo Choi Date: Fri, 8 Jul 2022 16:49:57 +0900 Subject: [PATCH 11/16] lib: resource-monitor: Fix return value of pass_resource_monitor_is_resource_attr_supported When return minus error value, it is converted to 1. It means that return 'true' when error happen. So that change the return value from minus integer value to false. Fix following issue by this patch: [ FAILED ] ResourceMonitorTest.pass_resource_monitor_is_resource_attr_supported_invalid Change-Id: I437644ffbc6b55f9ff0e539ab3af1ceda6878872 Signed-off-by: Chanwoo Choi --- lib/resource-monitor/resource-monitor.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/lib/resource-monitor/resource-monitor.c b/lib/resource-monitor/resource-monitor.c index fc70700..2a4b637 100644 --- a/lib/resource-monitor/resource-monitor.c +++ b/lib/resource-monitor/resource-monitor.c @@ -348,23 +348,26 @@ bool pass_resource_monitor_is_resource_attr_supported(int id, int resource_id, u REQUEST_IS_RESOURCE_ATTR_SUPPORTED, resource_id, attr_id); if (send(id, buffer, buffer_len, 0) < 0) { _E("[libpass] error occurred while sending buffer"); - return -EIO; + return false; } /* wait for response */ buffer_len = recv(id, buffer, GENERIC_BUFF_MAX, 0); if (buffer_len <= 0) { _E("[libpass] socket disconnected"); - return -EIO; + return false; } buffer[buffer_len] = '\0'; if (sscanf(buffer, "%d$%d", &response_req, &ret) < 2) - return -EINVAL; + return false; + + if (ret < 0) + return false; if (response_req != REQUEST_IS_RESOURCE_ATTR_SUPPORTED) { _E("[libpass] wrong response"); - return -EINVAL; + return false; } return (bool)ret; -- 2.7.4 From 196216a31323cc759b10896608575a230d6a8a5c Mon Sep 17 00:00:00 2001 From: Chanwoo Choi Date: Tue, 5 Jul 2022 06:16:35 +0900 Subject: [PATCH 12/16] util: resource: Encapsulate struct resource by hiding from user Move 'struct resource' into src/util/resource.c for hiding it and add missing getter/setter for encapsulation to access the fiels of 'struct resource'. Change-Id: Ib20b3de6f5ac68f20c14da82dedef38bbe26d2e8 Signed-off-by: Chanwoo Choi --- include/util/resource.h | 27 +++++++--------------- src/util/resource.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 19 deletions(-) diff --git a/include/util/resource.h b/include/util/resource.h index 79f9bbd..d9b62f7 100644 --- a/include/util/resource.h +++ b/include/util/resource.h @@ -109,25 +109,6 @@ struct resource_device { int index; }; -struct resource { - int id; - char *name; - const struct resource_driver *driver; - int type; - void *priv; - - int num_attrs; - const struct resource_attribute *attrs; - struct resource_attribute_value *attrs_value; - int num_ctrls; - const struct resource_control *ctrls; - u_int64_t attr_interest; - u_int64_t attr_supported; - - int64_t ts_start; - int64_t ts_end; -}; - #define RESOURCE_DRIVER_REGISTER(resource_driver) \ static void __CONSTRUCTOR__ module_init(void) \ { \ @@ -187,6 +168,14 @@ int set_resource_attr_interest(struct resource *resource, u_int64_t interest_mas int unset_resource_attr_interest(struct resource *resource, u_int64_t interest_mask); bool is_resource_attr_interested(const struct resource *resource, u_int64_t interest_mask); +const char *get_resource_name(struct resource *resource); +void *get_resource_privdata(struct resource *resource); +int get_resource_id(struct resource *resource); +int get_resource_type(struct resource *resource); +int get_resource_ts(struct resource *resource, int64_t *ts_start, int64_t *ts_end); + +int set_resource_privdata(struct resource *resource, void *priv); + inline __attribute__((always_inline)) int64_t get_time_now(void) { struct timeval tv; diff --git a/src/util/resource.c b/src/util/resource.c index 207eb6b..feea4d1 100644 --- a/src/util/resource.c +++ b/src/util/resource.c @@ -29,6 +29,25 @@ #define RESOURCE_ATTR_INDEX(id) BIT64_INDEX(id) #define RESOURCE_CTRL_INDEX(id) BIT64_INDEX(id) +struct resource { + int id; + char *name; + const struct resource_driver *driver; + int type; + void *priv; + + int num_attrs; + const struct resource_attribute *attrs; + struct resource_attribute_value *attrs_value; + int num_ctrls; + const struct resource_control *ctrls; + u_int64_t attr_interest; + u_int64_t attr_supported; + + int64_t ts_start; + int64_t ts_end; +}; + static int g_resource_id; static GList *g_resource_driver_head; static GList *g_resource_device_head; @@ -954,3 +973,44 @@ bool is_resource_attr_interested(const struct resource *resource, u_int64_t inte return true; } + +const char *get_resource_name(struct resource *resource) +{ + return resource ? resource->name : NULL; +} + +void *get_resource_privdata(struct resource *resource) +{ + return resource ? resource->priv : NULL; +} + +int get_resource_id(struct resource *resource) +{ + return resource ? resource->id : -EINVAL; +} + +int get_resource_type(struct resource *resource) +{ + return resource ? resource->type : -EINVAL; +} + +int get_resource_ts(struct resource *resource, int64_t *ts_start, int64_t *ts_end) +{ + if (!resource) + return -EINVAL; + + *ts_start = resource->ts_start; + *ts_end = resource->ts_end; + + return 0; +} + +int set_resource_privdata(struct resource *resource, void *priv) +{ + if (!resource) + return -EINVAL; + + resource->priv = priv; + + return 0; +} -- 2.7.4 From a502594b7f7e2911ddfdd42d55e35c58017d006a Mon Sep 17 00:00:00 2001 From: Chanwoo Choi Date: Tue, 5 Jul 2022 06:15:56 +0900 Subject: [PATCH 13/16] resource: Use getter/setter of struct resource for encapsulation Prevent the direct access of the fields of struct resource by using getter/setter functiosn of struct resource. By this change, keep the encapsulation of struct resource and only communicate via getter/setter functions. Change-Id: I4c7b92b79197ef67688773a5ef6b7db5e044bc09 Signed-off-by: Chanwoo Choi --- src/monitor/request-handler.c | 14 ++++--- src/resource/resource-bus.c | 71 ++++++++++++++++++++------------- src/resource/resource-cpu.c | 72 +++++++++++++++++++++------------- src/resource/resource-display.c | 35 ++++++++++++----- src/resource/resource-gpu.c | 68 ++++++++++++++++++++------------ src/resource/resource-process-group.c | 62 ++++++++++++++++++----------- src/resource/resource-process.c | 74 ++++++++++++++++++++++++----------- src/resource/resource-system.c | 47 ++++++++++++++-------- 8 files changed, 284 insertions(+), 159 deletions(-) diff --git a/src/monitor/request-handler.c b/src/monitor/request-handler.c index 482132a..1cf1a69 100644 --- a/src/monitor/request-handler.c +++ b/src/monitor/request-handler.c @@ -49,14 +49,17 @@ static void update_resource(gpointer key, gpointer value, gpointer user_data) ret = update_resource_attrs(res); if (ret < 0) _E("failed to update resource attributes (name:%s,id:%d)\n", - res->name, res->id); + get_resource_name(res), + get_resource_id(res)); } static void register_resource_to_client(struct request_client *client, struct resource *res) { - g_hash_table_insert(client->resource_table, GINT_TO_POINTER(res->id), - (gpointer)res); + g_hash_table_insert(client->resource_table, + GINT_TO_POINTER(get_resource_id(res)), + (gpointer)res); +} static void unregister_resource_from_client(struct request_client *client, int resource_id) @@ -94,7 +97,7 @@ static int handle_request_create_resource(struct request_client *client, char *a register_resource_to_client(client, res); - return res->id; + return get_resource_id(res); } static int handle_request_delete_resource(struct request_client *client, char *args) @@ -481,8 +484,7 @@ handle_request_get_resource_ts(struct request_client *client, char *args, if (!res) return -EINVAL; - *start = res->ts_start; - *end = res->ts_end; + get_resource_ts(res, start, end); return 0; } diff --git a/src/resource/resource-bus.c b/src/resource/resource-bus.c index c7e9ab7..a1f2a9a 100644 --- a/src/resource/resource-bus.c +++ b/src/resource/resource-bus.c @@ -42,34 +42,40 @@ static int bus_get_value_from_hal_power(const struct resource *res, void *data) { struct bus_context *ctx; + int type; int *val = (int *)data; int _val; - if (!res || !res->priv || !attr || !data) + if (!res || !attr || !data) return -EINVAL; - ctx = res->priv; + ctx = get_resource_privdata(res); + if (!ctx) + return -EINVAL; if (!ctx->device_name) { - _E("%s: BUS_CTRL_DEVICE_ID is not yet initialized\n", res->name); + _E("%s: BUS_CTRL_DEVICE_ID is not yet initialized\n", + get_resource_name(res)); return -EINVAL; } + type = get_resource_type(res); + switch (attr->id) { case BUS_ATTR_CUR_FREQ: - _val = hal_power_dvfs_get_curr_freq(res->type, ctx->device_name); + _val = hal_power_dvfs_get_curr_freq(type, ctx->device_name); break; case BUS_ATTR_MIN_FREQ: - _val = hal_power_dvfs_get_min_freq(res->type, ctx->device_name); + _val = hal_power_dvfs_get_min_freq(type, ctx->device_name); break; case BUS_ATTR_MAX_FREQ: - _val = hal_power_dvfs_get_max_freq(res->type, ctx->device_name); + _val = hal_power_dvfs_get_max_freq(type, ctx->device_name); break; case BUS_ATTR_AVAILABLE_MIN_FREQ: - _val = hal_power_dvfs_get_available_min_freq(res->type, ctx->device_name); + _val = hal_power_dvfs_get_available_min_freq(type, ctx->device_name); break; case BUS_ATTR_AVAILABLE_MAX_FREQ: - _val = hal_power_dvfs_get_available_max_freq(res->type, ctx->device_name); + _val = hal_power_dvfs_get_available_max_freq(type, ctx->device_name); break; default: return -EINVAL; @@ -87,21 +93,25 @@ static int bus_get_curr_governor(const struct resource *res, const struct resource_attribute *attr, void *data) { - struct bus_context *ctx; + struct bus_context *ctx;; char *buf = (char *)data; int val; - if (!res || !res->priv || !attr || !data) + if (!res || !attr || !data) return -EINVAL; - ctx = res->priv; + ctx = get_resource_privdata(res); + if (!ctx) + return -EINVAL; if (!ctx->device_name) { - _E("%s: BUS_CTRL_DEVICE_ID is not yet initialized\n", res->name); + _E("%s: BUS_CTRL_DEVICE_ID is not yet initialized\n", + get_resource_name(res)); return -EINVAL; } - val = hal_power_dvfs_get_curr_governor(res->type, ctx->device_name, buf); + val = hal_power_dvfs_get_curr_governor(get_resource_type(res), + ctx->device_name, buf); if (val < 0) return -EINVAL; @@ -162,18 +172,20 @@ static int bus_setup_device_id(const struct resource *res, const struct resource_device *device; int device_id = (int)(intptr_t)data; - if (!res || !res->priv || !ctrl) + if (!res || !ctrl) + return -EINVAL; + + ctx = get_resource_privdata(res); + if (!ctx) return -EINVAL; - device = find_resource_device(res->type, device_id); + device = find_resource_device(get_resource_type(res), device_id); if (!device) { _E("Not available resource: type: %s, index: %d\n", - res->name, device_id); + get_resource_name(res), device_id); return -EINVAL; } - ctx = res->priv; - if (ctx->device_name) free(ctx->device_name); @@ -201,7 +213,7 @@ static int bus_init(struct resource *res) if (!ctx) return -ENOMEM; - res->priv = ctx; + set_resource_privdata(res, ctx); return 0; } @@ -210,15 +222,19 @@ static void bus_exit(struct resource *res) { struct bus_context *ctx; - if (res && res->priv) { - ctx = res->priv; - if (ctx->device_name) { - free(ctx->device_name); - ctx->device_name = NULL; - } - free(ctx); - res->priv = NULL; + if (!res) + return; + + ctx = get_resource_privdata(res); + if (!ctx) + return; + + if (ctx->device_name) { + free(ctx->device_name); + ctx->device_name = NULL; } + free(ctx); + set_resource_privdata(res, NULL); } static const struct resource_driver bus_resource_driver = { @@ -234,4 +250,3 @@ static const struct resource_driver bus_resource_driver = { }, }; RESOURCE_DRIVER_REGISTER(&bus_resource_driver) - diff --git a/src/resource/resource-cpu.c b/src/resource/resource-cpu.c index c25a6c9..1651e30 100644 --- a/src/resource/resource-cpu.c +++ b/src/resource/resource-cpu.c @@ -43,33 +43,38 @@ static int cpu_get_value_from_hal_power(const struct resource *res, { struct cpu_context *ctx; int *val = (int *)data; - int _val; + int _val, type; - if (!res || !res->priv || !attr || !data) + if (!res || !attr || !data) return -EINVAL; - ctx = res->priv; + ctx = get_resource_privdata(res); + if (!ctx) + return -EINVAL; if (!ctx->device_name) { - _E("%s: CPU_CTRL_CLUSTER_ID is not yet initialized\n", res->name); + _E("%s: CPU_CTRL_CLUSTER_ID is not yet initialized\n", + get_resource_name(res)); return -EINVAL; } + type = get_resource_type(res); + switch (attr->id) { case CPU_ATTR_CUR_FREQ: - _val = hal_power_dvfs_get_curr_freq(res->type, ctx->device_name); + _val = hal_power_dvfs_get_curr_freq(type, ctx->device_name); break; case CPU_ATTR_MIN_FREQ: - _val = hal_power_dvfs_get_min_freq(res->type, ctx->device_name); + _val = hal_power_dvfs_get_min_freq(type, ctx->device_name); break; case CPU_ATTR_MAX_FREQ: - _val = hal_power_dvfs_get_max_freq(res->type, ctx->device_name); + _val = hal_power_dvfs_get_max_freq(type, ctx->device_name); break; case CPU_ATTR_AVAILABLE_MIN_FREQ: - _val = hal_power_dvfs_get_available_min_freq(res->type, ctx->device_name); + _val = hal_power_dvfs_get_available_min_freq(type, ctx->device_name); break; case CPU_ATTR_AVAILABLE_MAX_FREQ: - _val = hal_power_dvfs_get_available_max_freq(res->type, ctx->device_name); + _val = hal_power_dvfs_get_available_max_freq(type, ctx->device_name); break; default: return -EINVAL; @@ -91,17 +96,21 @@ static int cpu_get_curr_governor(const struct resource *res, char *buf = (char *)data; int val; - if (!res || !res->priv || !attr || !data) + if (!res || !attr || !data) return -EINVAL; - ctx = res->priv; + ctx = get_resource_privdata(res); + if (!ctx) + return -EINVAL; if (!ctx->device_name) { - _E("%s: CPU_CTRL_CLUSTER_ID is not yet initialized\n", res->name); + _E("%s: CPU_CTRL_CLUSTER_ID is not yet initialized\n", + get_resource_name(res)); return -EINVAL; } - val = hal_power_dvfs_get_curr_governor(res->type, ctx->device_name, buf); + val = hal_power_dvfs_get_curr_governor(get_resource_type(res), + ctx->device_name, buf); if (val < 0) return -EINVAL; @@ -162,18 +171,20 @@ static int cpu_setup_cluster_id(const struct resource *res, const struct resource_device *device; int resource_index = (int)(intptr_t)data; - if (!res || !res->priv || !ctrl) + if (!res || !ctrl) + return -EINVAL; + + ctx = get_resource_privdata(res); + if (!ctx) return -EINVAL; - device = find_resource_device(res->type, resource_index); + device = find_resource_device(get_resource_type(res), resource_index); if (!device) { _E("Not available resource: type: %s, index: %d\n", - res->name, resource_index); + get_resource_name(res), resource_index); return -EINVAL; } - ctx = res->priv; - if (ctx->device_name) free(ctx->device_name); @@ -195,13 +206,13 @@ static const struct resource_control cpu_ctrls[] = { static int cpu_init(struct resource *res) { - struct cpu_context *ctx; + struct cpu_context *ctx = get_resource_privdata(res); ctx = calloc(1, sizeof(struct cpu_context)); if (!ctx) return -ENOMEM; - res->priv = ctx; + set_resource_privdata(res, ctx); return 0; } @@ -210,15 +221,20 @@ static void cpu_exit(struct resource *res) { struct cpu_context *ctx; - if (res && res->priv) { - ctx = res->priv; - if (ctx->device_name) { - free(ctx->device_name); - ctx->device_name = NULL; - } - free(ctx); - res->priv = NULL; + if (!res) + return; + + ctx = get_resource_privdata(res); + if (!ctx) + return; + + if (ctx->device_name) { + free(ctx->device_name); + ctx->device_name = NULL; } + + free(ctx); + set_resource_privdata(res, NULL); } static const struct resource_driver cpu_resource_driver = { diff --git a/src/resource/resource-display.c b/src/resource/resource-display.c index 33b8aee..ea05b32 100644 --- a/src/resource/resource-display.c +++ b/src/resource/resource-display.c @@ -59,13 +59,16 @@ static int display_get_fps(const struct resource *res, double *fps = (double *)data; int ret = 0; - if (!res || !res->priv || !attr || !data) + if (!res || !attr || !data) return -EINVAL; - disp_id = res->priv; + disp_id = get_resource_privdata(res); + if (!disp_id) + return -EINVAL; if (*disp_id < 0) { - _E("%s: DISPLAY_CTRL_DEVICE_ID is not yet initialized\n", res->name); + _E("%s: DISPLAY_CTRL_DEVICE_ID is not yet initialized\n", + get_resource_name(res)); return -EINVAL; } @@ -140,15 +143,19 @@ static int display_setup_device_id(const struct resource *res, const struct resource_control *ctrl, const void *data) { - int *disp_id; + int *disp_id = NULL; - if (!res || !res->priv || !ctrl) + if (!res || !ctrl) return -EINVAL; - disp_id = res->priv; + disp_id = get_resource_privdata(res); + if (!disp_id) + return -EINVAL; *disp_id = (int)(intptr_t)data; + set_resource_privdata(res, disp_id); + return 0; } @@ -172,16 +179,26 @@ static int display_init(struct resource *res) *disp_id = -1; - res->priv = disp_id; + set_resource_privdata(res, disp_id); return 0; } static void display_exit(struct resource *res) { - if (res && res->priv) - free(res->priv); + int *disp_id; + + if (!res) + return; + + disp_id = get_resource_privdata(res); + if (!disp_id) + return; + + free(disp_id); + set_resource_privdata(res, NULL); } + static const struct resource_driver display_resource_driver = { .name = "DISPLAY", .type = RESOURCE_TYPE_DISPLAY, diff --git a/src/resource/resource-gpu.c b/src/resource/resource-gpu.c index b736650..c9cc0b3 100644 --- a/src/resource/resource-gpu.c +++ b/src/resource/resource-gpu.c @@ -42,34 +42,40 @@ static int gpu_get_value_from_hal_power(const struct resource *res, void *data) { struct gpu_context *ctx; + int type; int *val = (int *)data; int _val; - if (!res || !res->priv || !attr || !data) + if (!res || !attr || !data) return -EINVAL; - ctx = res->priv; + ctx = get_resource_privdata(res); + if (!ctx) + return -EINVAL; if (!ctx->device_name) { - _E("%s: GPU_CTRL_DEVICE_ID is not yet initialized\n", res->name); + _E("%s: GPU_CTRL_DEVICE_ID is not yet initialized\n", + get_resource_name(res)); return -EINVAL; } + type = get_resource_type(res); + switch (attr->id) { case GPU_ATTR_CUR_FREQ: - _val = hal_power_dvfs_get_curr_freq(res->type, ctx->device_name); + _val = hal_power_dvfs_get_curr_freq(type, ctx->device_name); break; case GPU_ATTR_MIN_FREQ: - _val = hal_power_dvfs_get_min_freq(res->type, ctx->device_name); + _val = hal_power_dvfs_get_min_freq(type, ctx->device_name); break; case GPU_ATTR_MAX_FREQ: - _val = hal_power_dvfs_get_max_freq(res->type, ctx->device_name); + _val = hal_power_dvfs_get_max_freq(type, ctx->device_name); break; case GPU_ATTR_AVAILABLE_MIN_FREQ: - _val = hal_power_dvfs_get_available_min_freq(res->type, ctx->device_name); + _val = hal_power_dvfs_get_available_min_freq(type, ctx->device_name); break; case GPU_ATTR_AVAILABLE_MAX_FREQ: - _val = hal_power_dvfs_get_available_max_freq(res->type, ctx->device_name); + _val = hal_power_dvfs_get_available_max_freq(type, ctx->device_name); break; default: return -EINVAL; @@ -91,17 +97,21 @@ static int gpu_get_curr_governor(const struct resource *res, char *buf = (char *)data; int val; - if (!res || !res->priv || !attr || !data) + if (!res || !attr || !data) return -EINVAL; - ctx = res->priv; + ctx = get_resource_privdata(res); + if (!ctx) + return -EINVAL; if (!ctx->device_name) { - _E("%s: GPU_CTRL_DEVICE_ID is not yet initialized\n", res->name); + _E("%s: GPU_CTRL_DEVICE_ID is not yet initialized\n", + get_resource_name(res)); return -EINVAL; } - val = hal_power_dvfs_get_curr_governor(res->type, ctx->device_name, buf); + val = hal_power_dvfs_get_curr_governor(get_resource_type(res), + ctx->device_name, buf); if (val < 0) return -EINVAL; @@ -162,18 +172,20 @@ static int gpu_setup_device_id(const struct resource *res, const struct resource_device *device; int resource_index = (int)(intptr_t)data; - if (!res || !res->priv || !ctrl) + if (!res || !ctrl) + return -EINVAL; + + ctx = get_resource_privdata(res); + if (!ctx) return -EINVAL; - device = find_resource_device(res->type, resource_index); + device = find_resource_device(get_resource_type(res), resource_index); if (!device) { _E("Not available resource: type: %s, index: %d\n", - res->name, resource_index); + get_resource_name(res), resource_index); return -EINVAL; } - ctx = res->priv; - if (ctx->device_name) free(ctx->device_name); @@ -201,7 +213,7 @@ static int gpu_init(struct resource *res) if (!ctx) return -ENOMEM; - res->priv = ctx; + set_resource_privdata(res, ctx); return 0; } @@ -210,15 +222,19 @@ static void gpu_exit(struct resource *res) { struct gpu_context *ctx; - if (res && res->priv) { - ctx = res->priv; - if (ctx->device_name) { - free(ctx->device_name); - ctx->device_name = NULL; - } - free(ctx); - res->priv = NULL; + if (!res) + return; + + ctx = get_resource_privdata(res); + if (!ctx) + return; + + if (ctx->device_name) { + free(ctx->device_name); + ctx->device_name = NULL; } + free(ctx); + set_resource_privdata(res, NULL); } static const struct resource_driver gpu_resource_driver = { diff --git a/src/resource/resource-process-group.c b/src/resource/resource-process-group.c index b0b2ade..5d056ed 100644 --- a/src/resource/resource-process-group.c +++ b/src/resource/resource-process-group.c @@ -64,10 +64,12 @@ static int process_group_get_pid_list(const struct resource *res, int *data_array; int i = 0; - if (!res || !res->priv || !attr || !data) + if (!res || !attr || !data) return -EINVAL; - ctx = res->priv; + ctx = get_resource_privdata(res); + if (!ctx) + return -EINVAL; if (!ctx->pi_map) return -EINVAL; @@ -102,10 +104,12 @@ static int process_group_get_comm_list(const struct resource *res, char **data_array; int i = 0; - if (!res || !res->priv || !attr || !data) + if (!res || !attr || !data) return -EINVAL; - ctx = res->priv; + ctx = get_resource_privdata(res); + if (!ctx) + return -EINVAL; if (!ctx->pi_map) return -EINVAL; @@ -148,10 +152,12 @@ static int process_group_get_cpu_util(const struct resource *res, struct process_group_context *ctx; double *util = (double *)data; - if (!res || !res->priv || !attr || !data) + if (!res || !attr || !data) return -EINVAL; - ctx = res->priv; + ctx = get_resource_privdata(res); + if (!ctx) + return -EINVAL; if (!ctx->pi_map || ctx->pid < 0) return -EINVAL; @@ -168,10 +174,12 @@ static int process_group_get_mem(const struct resource *res, struct process_group_context *ctx; u_int64_t *mem = (u_int64_t *)data; - if (!res || !res->priv || !attr || !data) + if (!res || !attr || !data) return -EINVAL; - ctx = res->priv; + ctx = get_resource_privdata(res); + if (!ctx) + return -EINVAL; if (!ctx->pi_map || ctx->pid < 0) return -EINVAL; @@ -209,10 +217,12 @@ static int process_group_get_disk_bps(const struct resource *res, struct process_group_context *ctx; u_int32_t *bps = (u_int32_t *)data; - if (!res || !res->priv || !attr || !data) + if (!res || !attr || !data) return -EINVAL; - ctx = res->priv; + ctx = get_resource_privdata(res); + if (!ctx) + return -EINVAL; if (!ctx->pi_map || ctx->pid < 0) return -EINVAL; @@ -337,10 +347,12 @@ static int process_group_setup_root_pid(const struct resource *res, int target_pid; int ret; - if (!res || !res->priv || !ctrl) + if (!res || !ctrl) return -EINVAL; - ctx = res->priv; + ctx = get_resource_privdata(res); + if (!ctx) + return -EINVAL; target_pid = (int)(intptr_t)data; @@ -501,10 +513,12 @@ static int process_group_prepare_update(struct resource *res) int ret = 0; pid_t tgid; - if (!res || !res->priv) + if (!res) return -EINVAL; - ctx = res->priv; + ctx = get_resource_privdata(res); + if (!ctx) + return -EINVAL; task_dir = opendir(PROC_DIR_PATH); if (!task_dir) @@ -652,7 +666,7 @@ static int process_group_init(struct resource *res) ctx->pid = -1; ctx->pi_map = g_hash_table_new_full(g_int_hash, g_int_equal, NULL, free_node); - res->priv = ctx; + set_resource_privdata(res, ctx); return 0; } @@ -661,16 +675,20 @@ static void process_group_exit(struct resource *res) { struct process_group_context *ctx; - if (res && res->priv) { - ctx = res->priv; + if (!res) + return; - if (ctx->pi_map) { - g_hash_table_destroy(ctx->pi_map); - ctx->pi_map = NULL; - } + ctx = get_resource_privdata(res); + if (!ctx) + return; - free(res->priv); + if (ctx->pi_map) { + g_hash_table_destroy(ctx->pi_map); + ctx->pi_map = NULL; } + + free(ctx); + set_resource_privdata(res, NULL); } static const struct resource_driver process_group_driver = { diff --git a/src/resource/resource-process.c b/src/resource/resource-process.c index 1d9b39a..5d99b3e 100644 --- a/src/resource/resource-process.c +++ b/src/resource/resource-process.c @@ -50,13 +50,16 @@ static int process_get_cpu_util(const struct resource *res, struct taskstats *prev, *curr; double *util = (double *)data; - if (!res || !res->priv || !attr || !data) + if (!res || !attr || !data) return -EINVAL; - ctx = res->priv; + ctx = get_resource_privdata(res); + if (!ctx) + return -EINVAL; if (!ctx->tgid) { - _E("resource %s is not yet initialized\n", res->name); + _E("resource %s is not yet initialized\n", + get_resource_name(res)); return -EINVAL; } @@ -81,13 +84,16 @@ static int process_get_mem_attrs(const struct resource *res, struct taskstats *curr; u_int64_t *mem = (u_int64_t *)data; - if (!res || !res->priv || !attr || !data) + if (!res || !attr || !data) return -EINVAL; - ctx = res->priv; + ctx = get_resource_privdata(res); + if (!ctx) + return -EINVAL; if (!ctx->tgid) { - _E("resource %s is not yet initialized\n", res->name); + _E("resource %s is not yet initialized\n", + get_resource_name(res)); return -EINVAL; } @@ -137,13 +143,16 @@ static int process_get_disk_bps(const struct resource *res, u_int64_t period; u_int32_t *bps = (u_int32_t *)data; - if (!res || !res->priv || !attr || !data) + if (!res || !attr || !data) return -EINVAL; - ctx = res->priv; + ctx = get_resource_privdata(res); + if (!ctx) + return -EINVAL; if (!ctx->tgid) { - _E("resource %s is not yet initialized\n", res->name); + _E("resource %s is not yet initialized\n", + get_resource_name(res)); return -EINVAL; } @@ -171,13 +180,16 @@ static int process_get_context_data(const struct resource *res, { struct process_context *ctx; - if (!res || !res->priv || !attr || !data) + if (!res || !attr || !data) return -EINVAL; - ctx = res->priv; + ctx = get_resource_privdata(res); + if (!ctx) + return -EINVAL; if (!ctx->tgid) { - _E("resource %s is not yet initialized\n", res->name); + _E("resource %s is not yet initialized\n", + get_resource_name(res)); return -EINVAL; } @@ -350,13 +362,16 @@ static int process_setup_tgid(const struct resource *res, struct process_context *ctx; u_int64_t total_memory; int ret; - bool include_gpu_mem = is_resource_attr_interested(res, PROCESS_GROUP_ATTR_MEM_GPU); + bool include_gpu_mem; - if (!res || !res->priv || !ctrl) + if (!res || !ctrl) return -EINVAL; - ctx = res->priv; + ctx = get_resource_privdata(res); + if (!ctx) + return -EINVAL; + include_gpu_mem = is_resource_attr_interested(res, PROCESS_GROUP_ATTR_MEM_GPU); total_memory = ctx->total_memory; memset(ctx, 0, sizeof(*ctx)); @@ -393,10 +408,19 @@ static const struct resource_control process_ctrls[] = { static int process_prepare_update(struct resource *res) { - struct process_context *ctx = res->priv; + struct process_context *ctx; u_int64_t total_time; int ret, online; - bool include_gpu_mem = is_resource_attr_interested(res, PROCESS_GROUP_ATTR_MEM_GPU); + bool include_gpu_mem; + + if (!res) + return -EINVAL; + + ctx = get_resource_privdata(res); + if (!ctx) + return -EINVAL; + + include_gpu_mem = is_resource_attr_interested(res, PROCESS_GROUP_ATTR_MEM_GPU); memcpy(&ctx->prev, &ctx->curr, sizeof(struct taskstats)); @@ -440,7 +464,7 @@ static int process_init(struct resource *res) return -EINVAL; } - res->priv = ctx; + set_resource_privdata(res, ctx); return 0; } @@ -449,11 +473,15 @@ static void process_exit(struct resource *res) { struct process_context *ctx; - if (res->priv) { - ctx = res->priv; - free(ctx); - res->priv = NULL; - } + if (!res) + return; + + ctx = get_resource_privdata(res); + if (!ctx) + return; + + free(ctx); + set_resource_privdata(res, NULL); } static const struct resource_driver process_resource_driver = { diff --git a/src/resource/resource-system.c b/src/resource/resource-system.c index 6f9f2a8..99a22f6 100644 --- a/src/resource/resource-system.c +++ b/src/resource/resource-system.c @@ -84,14 +84,17 @@ static int system_get_avg_cpu_util(const struct resource *res, struct system_resouce_data *sysdata; double *util = (double *)data; - if (!res || !res->priv ||!attr || !data) + if (!res || !attr || !data) return -EINVAL; - sysdata = (struct system_resouce_data *)res->priv; + sysdata = get_resource_privdata(res); + if (!sysdata) + return -EINVAL; *util = __calculate_cpu_util(attr->id, &sysdata->prev_avg, &sysdata->curr_avg); if (*util < 0) { - _W("failed to calculate average cpu util (%s: %s)\n", res->name, attr->name); + _W("failed to calculate average cpu util (%s: %s)\n", + get_resource_name(res), attr->name); *util = 0.0; } @@ -107,10 +110,12 @@ static int system_get_per_cpu_util(const struct resource *res, double *utils; int i; - if (!res || !res->priv ||!attr || !data) + if (!res || !attr || !data) return -EINVAL; - sysdata = (struct system_resouce_data *)res->priv; + sysdata = get_resource_privdata(res); + if (!sysdata) + return -EINVAL; array->type = DATA_TYPE_DOUBLE; array->length = sysdata->num_possible_cpus; @@ -129,7 +134,8 @@ static int system_get_per_cpu_util(const struct resource *res, &sysdata->prev_cpus[i], &sysdata->curr_cpus[i]); if (utils[i] < 0) { - _W("failed to calculate per-cpu util (%s: %s)\n", res->name, attr->name); + _W("failed to calculate per-cpu util (%s: %s)\n", + get_resource_name(res), attr->name); utils[i] = 0; } } @@ -223,6 +229,7 @@ static const struct resource_attribute system_attrs[] = { static int system_driver_init(struct resource *res) { struct system_resouce_data *sysdata; + const char *res_name = get_resource_name(res); int ret; sysdata = calloc(1, sizeof(struct system_resouce_data)); @@ -231,7 +238,7 @@ static int system_driver_init(struct resource *res) ret = kernel_get_possible_cpu_num(); if (ret < 0) { - _I("failed to get possible cpu on system driver (%s)\n", res->name); + _I("failed to get possible cpu on system driver (%s)\n", res_name); goto err; } sysdata->num_possible_cpus = ret; @@ -239,7 +246,7 @@ static int system_driver_init(struct resource *res) sysdata->prev_cpus = calloc(sysdata->num_possible_cpus, sizeof(struct cpu_stat)); if (!sysdata->prev_cpus) { - _I("failed to allocate memory of prev_cpus (%s)\n", res->name); + _I("failed to allocate memory of prev_cpus (%s)\n", res_name); ret = -ENOMEM; goto err; } @@ -247,12 +254,12 @@ static int system_driver_init(struct resource *res) sysdata->curr_cpus = calloc(sysdata->num_possible_cpus, sizeof(struct cpu_stat)); if (!sysdata->curr_cpus) { - _I("failed to allocate memory of curr_cpus (%s)\n", res->name); + _I("failed to allocate memory of curr_cpus (%s)\n", res_name); ret = -ENOMEM; goto err_prev_cpus; } - res->priv = (void *)sysdata; + set_resource_privdata(res, (void *)sysdata); return 0; @@ -267,26 +274,32 @@ err: static void system_driver_exit(struct resource *res) { - struct system_resouce_data *sysdata - = (struct system_resouce_data *)res->priv; + struct system_resouce_data *sysdata; + + if (!res) + return; + + sysdata = get_resource_privdata(res); + if (!sysdata) + return; free(sysdata->prev_cpus); free(sysdata->curr_cpus); free(sysdata); - res->priv = NULL; + set_resource_privdata(res, NULL); } static int system_driver_prepare_update(struct resource *res) { - struct system_resouce_data *sysdata - = (struct system_resouce_data *)res->priv; + struct system_resouce_data *sysdata = get_resource_privdata(res); + const char *res_name = get_resource_name(res); int ret; /* Get the average cpu utilization of all cpus */ memcpy(&sysdata->prev_avg, &sysdata->curr_avg, sizeof(sysdata->prev_avg)); ret = kernel_get_total_cpu_stat(&sysdata->curr_avg); if (ret < 0) { - _I("failed to calculate average cpu util (%s)\n", res->name); + _I("failed to calculate average cpu util (%s)\n", res_name); return ret; } @@ -297,7 +310,7 @@ static int system_driver_prepare_update(struct resource *res) sysdata->num_possible_cpus, &sysdata->num_online_cpus); if (ret < 0) { - _I("failed to calculate per-cpu util (%s)\n", res->name); + _I("failed to calculate per-cpu util (%s)\n", res_name); return ret; } -- 2.7.4 From 205e26e4e52237610789178c872723b024de2df5 Mon Sep 17 00:00:00 2001 From: Chanwoo Choi Date: Tue, 5 Jul 2022 06:51:41 +0900 Subject: [PATCH 14/16] util: resource: Discard const keyword of struct resource Discard const keyword when accessing struct resource by hiding the 'struct resource' from use of 'struct resource'. Except for 'void *priv' field of struct resource, the fields of struct resource should be accessed by getter/setter functions. Change-Id: I80fe2a7c9df8f6a5c273da9f222668a03ba8e62e Signed-off-by: Chanwoo Choi --- include/util/resource.h | 14 +++++++------- src/resource/resource-battery.c | 4 ++-- src/resource/resource-bus.c | 6 +++--- src/resource/resource-cpu.c | 6 +++--- src/resource/resource-display.c | 4 ++-- src/resource/resource-gpu.c | 6 +++--- src/resource/resource-memory.c | 2 +- src/resource/resource-process-group.c | 14 +++++++------- src/resource/resource-process.c | 12 ++++++------ src/resource/resource-system.c | 6 +++--- src/util/resource.c | 2 +- 11 files changed, 38 insertions(+), 38 deletions(-) diff --git a/include/util/resource.h b/include/util/resource.h index d9b62f7..e96d3ba 100644 --- a/include/util/resource.h +++ b/include/util/resource.h @@ -40,17 +40,17 @@ struct resource_attribute_value { }; struct resource_attribute_ops { - int (*set)(const struct resource *res, + int (*set)(struct resource *res, const struct resource_attribute *attr, const void *data, int count); - int (*get)(const struct resource *res, + int (*get)(struct resource *res, const struct resource_attribute *attr, void *data); /* * If .is_supported ops is not implemented, use .get ops in order to * check whether the resource attribute is supported or not. */ - bool (*is_supported)(const struct resource *res, + bool (*is_supported)(struct resource *res, const struct resource_attribute *attr); }; @@ -64,10 +64,10 @@ struct resource_attribute { struct resource_control; struct resource_control_ops { - const int (*set)(const struct resource *res, + const int (*set)(struct resource *res, const struct resource_control *ctrl, const void *data); - const int (*get)(const struct resource *res, + const int (*get)(struct resource *res, const struct resource_control *ctrl, void **data); }; @@ -144,7 +144,7 @@ get_resource_attr_value(struct resource *resource, u_int64_t attr_id); bool is_resource_attr_supported(struct resource *resource, u_int64_t attr_id); static inline bool -resource_attr_supported_always(const struct resource *resource, +resource_attr_supported_always(struct resource *resource, const struct resource_attribute *attr) { return true; @@ -166,7 +166,7 @@ int get_resource_attr_ptr(struct resource *resource, u_int64_t attr_id, void **d int set_resource_attr_interest(struct resource *resource, u_int64_t interest_mask); int unset_resource_attr_interest(struct resource *resource, u_int64_t interest_mask); -bool is_resource_attr_interested(const struct resource *resource, u_int64_t interest_mask); +bool is_resource_attr_interested(struct resource *resource, u_int64_t interest_mask); const char *get_resource_name(struct resource *resource); void *get_resource_privdata(struct resource *resource); diff --git a/src/resource/resource-battery.c b/src/resource/resource-battery.c index 584fe2f..ed51d39 100644 --- a/src/resource/resource-battery.c +++ b/src/resource/resource-battery.c @@ -30,7 +30,7 @@ #include -static int battery_get_info(const struct resource *res, +static int battery_get_info(struct resource *res, const struct resource_attribute *attr, void *data) { @@ -71,7 +71,7 @@ static int battery_get_info(const struct resource *res, return 0; } -static int battery_get_status(const struct resource *res, +static int battery_get_status(struct resource *res, const struct resource_attribute *attr, void *data) { diff --git a/src/resource/resource-bus.c b/src/resource/resource-bus.c index a1f2a9a..7366111 100644 --- a/src/resource/resource-bus.c +++ b/src/resource/resource-bus.c @@ -37,7 +37,7 @@ struct bus_context { int index; }; -static int bus_get_value_from_hal_power(const struct resource *res, +static int bus_get_value_from_hal_power(struct resource *res, const struct resource_attribute *attr, void *data) { @@ -89,7 +89,7 @@ static int bus_get_value_from_hal_power(const struct resource *res, return 0; } -static int bus_get_curr_governor(const struct resource *res, +static int bus_get_curr_governor(struct resource *res, const struct resource_attribute *attr, void *data) { @@ -164,7 +164,7 @@ static const struct resource_attribute bus_attrs[] = { }, }; -static int bus_setup_device_id(const struct resource *res, +static int bus_setup_device_id(struct resource *res, const struct resource_control *ctrl, const void *data) { diff --git a/src/resource/resource-cpu.c b/src/resource/resource-cpu.c index 1651e30..4613705 100644 --- a/src/resource/resource-cpu.c +++ b/src/resource/resource-cpu.c @@ -37,7 +37,7 @@ struct cpu_context { int index; }; -static int cpu_get_value_from_hal_power(const struct resource *res, +static int cpu_get_value_from_hal_power(struct resource *res, const struct resource_attribute *attr, void *data) { @@ -88,7 +88,7 @@ static int cpu_get_value_from_hal_power(const struct resource *res, return 0; } -static int cpu_get_curr_governor(const struct resource *res, +static int cpu_get_curr_governor(struct resource *res, const struct resource_attribute *attr, void *data) { @@ -163,7 +163,7 @@ static const struct resource_attribute cpu_attrs[] = { }, }; -static int cpu_setup_cluster_id(const struct resource *res, +static int cpu_setup_cluster_id(struct resource *res, const struct resource_control *ctrl, const void *data) { diff --git a/src/resource/resource-display.c b/src/resource/resource-display.c index ea05b32..7cfebdb 100644 --- a/src/resource/resource-display.c +++ b/src/resource/resource-display.c @@ -46,7 +46,7 @@ struct display_fps_data { double fps; }; -static int display_get_fps(const struct resource *res, +static int display_get_fps(struct resource *res, const struct resource_attribute *attr, void *data) { @@ -139,7 +139,7 @@ static const struct resource_attribute display_attrs[] = { }, }; -static int display_setup_device_id(const struct resource *res, +static int display_setup_device_id(struct resource *res, const struct resource_control *ctrl, const void *data) { diff --git a/src/resource/resource-gpu.c b/src/resource/resource-gpu.c index c9cc0b3..4fdce6c 100644 --- a/src/resource/resource-gpu.c +++ b/src/resource/resource-gpu.c @@ -37,7 +37,7 @@ struct gpu_context { int index; }; -static int gpu_get_value_from_hal_power(const struct resource *res, +static int gpu_get_value_from_hal_power(struct resource *res, const struct resource_attribute *attr, void *data) { @@ -89,7 +89,7 @@ static int gpu_get_value_from_hal_power(const struct resource *res, return 0; } -static int gpu_get_curr_governor(const struct resource *res, +static int gpu_get_curr_governor(struct resource *res, const struct resource_attribute *attr, void *data) { @@ -164,7 +164,7 @@ static const struct resource_attribute gpu_attrs[] = { }, }; -static int gpu_setup_device_id(const struct resource *res, +static int gpu_setup_device_id(struct resource *res, const struct resource_control *ctrl, const void *data) { diff --git a/src/resource/resource-memory.c b/src/resource/resource-memory.c index 48c627d..a9218d2 100644 --- a/src/resource/resource-memory.c +++ b/src/resource/resource-memory.c @@ -76,7 +76,7 @@ static int memory_get_swap_total(u_int64_t *val) return 0; } -static int memory_get_memory_info(const struct resource *res, +static int memory_get_memory_info(struct resource *res, const struct resource_attribute *attr, void *data) { diff --git a/src/resource/resource-process-group.c b/src/resource/resource-process-group.c index 5d056ed..e6a2812 100644 --- a/src/resource/resource-process-group.c +++ b/src/resource/resource-process-group.c @@ -52,7 +52,7 @@ struct process_group_context { static u_int64_t total_memory; static long jiffy; -static int process_group_get_pid_list(const struct resource *res, +static int process_group_get_pid_list(struct resource *res, const struct resource_attribute *attr, void *data) { @@ -92,7 +92,7 @@ static int process_group_get_pid_list(const struct resource *res, return 0; } -static int process_group_get_comm_list(const struct resource *res, +static int process_group_get_comm_list(struct resource *res, const struct resource_attribute *attr, void *data) { @@ -145,7 +145,7 @@ static int process_group_get_comm_list(const struct resource *res, return 0; } -static int process_group_get_cpu_util(const struct resource *res, +static int process_group_get_cpu_util(struct resource *res, const struct resource_attribute *attr, void *data) { @@ -167,7 +167,7 @@ static int process_group_get_cpu_util(const struct resource *res, return 0; } -static int process_group_get_mem(const struct resource *res, +static int process_group_get_mem(struct resource *res, const struct resource_attribute *attr, void *data) { @@ -210,7 +210,7 @@ static int process_group_get_mem(const struct resource *res, return 0; } -static int process_group_get_disk_bps(const struct resource *res, +static int process_group_get_disk_bps(struct resource *res, const struct resource_attribute *attr, void *data) { @@ -240,7 +240,7 @@ static int process_group_get_disk_bps(const struct resource *res, return 0; } -static bool process_group_check_gpu_support(const struct resource *resource, +static bool process_group_check_gpu_support(struct resource *resource, const struct resource_attribute *attr) { return kernel_check_gpu_support(); @@ -338,7 +338,7 @@ static const struct resource_attribute process_group_attrs[] = { }, }; -static int process_group_setup_root_pid(const struct resource *res, +static int process_group_setup_root_pid(struct resource *res, const struct resource_control *ctrl, const void *data) { diff --git a/src/resource/resource-process.c b/src/resource/resource-process.c index 5d99b3e..600d3d9 100644 --- a/src/resource/resource-process.c +++ b/src/resource/resource-process.c @@ -42,7 +42,7 @@ struct process_context { static long jiffy; -static int process_get_cpu_util(const struct resource *res, +static int process_get_cpu_util(struct resource *res, const struct resource_attribute *attr, void *data) { @@ -76,7 +76,7 @@ static int process_get_cpu_util(const struct resource *res, return 0; } -static int process_get_mem_attrs(const struct resource *res, +static int process_get_mem_attrs(struct resource *res, const struct resource_attribute *attr, void *data) { @@ -134,7 +134,7 @@ static int process_get_mem_attrs(const struct resource *res, return 0; } -static int process_get_disk_bps(const struct resource *res, +static int process_get_disk_bps(struct resource *res, const struct resource_attribute *attr, void *data) { @@ -174,7 +174,7 @@ static int process_get_disk_bps(const struct resource *res, return 0; } -static int process_get_context_data(const struct resource *res, +static int process_get_context_data(struct resource *res, const struct resource_attribute *attr, void *data) { @@ -221,7 +221,7 @@ static int process_get_context_data(const struct resource *res, return 0; } -static bool process_check_gpu_support(const struct resource *resource, +static bool process_check_gpu_support(struct resource *resource, const struct resource_attribute *attr) { return kernel_check_gpu_support(); @@ -355,7 +355,7 @@ static u_int64_t get_total_cpu_time(void) return total_time; } -static int process_setup_tgid(const struct resource *res, +static int process_setup_tgid(struct resource *res, const struct resource_control *ctrl, const void *data) { diff --git a/src/resource/resource-system.c b/src/resource/resource-system.c index 99a22f6..72d5835 100644 --- a/src/resource/resource-system.c +++ b/src/resource/resource-system.c @@ -77,7 +77,7 @@ static double __calculate_cpu_util(int64_t id, struct cpu_stat *prev, return util; } -static int system_get_avg_cpu_util(const struct resource *res, +static int system_get_avg_cpu_util(struct resource *res, const struct resource_attribute *attr, void *data) { @@ -101,7 +101,7 @@ static int system_get_avg_cpu_util(const struct resource *res, return 0; } -static int system_get_per_cpu_util(const struct resource *res, +static int system_get_per_cpu_util(struct resource *res, const struct resource_attribute *attr, void *data) { @@ -143,7 +143,7 @@ static int system_get_per_cpu_util(const struct resource *res, return 0; } -static int system_get_cpu_num(const struct resource *res, +static int system_get_cpu_num(struct resource *res, const struct resource_attribute *attr, void *data) { diff --git a/src/util/resource.c b/src/util/resource.c index feea4d1..4f74cc0 100644 --- a/src/util/resource.c +++ b/src/util/resource.c @@ -963,7 +963,7 @@ int unset_resource_attr_interest(struct resource *resource, u_int64_t interest_m return 0; } -bool is_resource_attr_interested(const struct resource *resource, u_int64_t interest_mask) +bool is_resource_attr_interested(struct resource *resource, u_int64_t interest_mask) { if (!resource) return false; -- 2.7.4 From 8dadde95406c67ccbe49cfe321bc95879a0b5239 Mon Sep 17 00:00:00 2001 From: Chanwoo Choi Date: Tue, 5 Jul 2022 20:17:48 +0900 Subject: [PATCH 15/16] monitor: request-handler: Remove unneeded local variable Change-Id: Iff69aa14a7aa97b4db8efa782dc845ba513f2648 Signed-off-by: Chanwoo Choi --- src/monitor/request-handler.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/monitor/request-handler.c b/src/monitor/request-handler.c index 1cf1a69..ee21593 100644 --- a/src/monitor/request-handler.c +++ b/src/monitor/request-handler.c @@ -70,11 +70,7 @@ unregister_resource_from_client(struct request_client *client, int resource_id) static struct resource * get_resource_by_id(struct request_client *client, int resource_id) { - struct resource *res; - - res = g_hash_table_lookup(client->resource_table, GINT_TO_POINTER(resource_id)); - - return res; + return g_hash_table_lookup(client->resource_table, GINT_TO_POINTER(resource_id)); } static int handle_request_create_resource(struct request_client *client, char *args) -- 2.7.4 From 51e9db0bcf2b7c81305bd22702868769d30e0240 Mon Sep 17 00:00:00 2001 From: Chanwoo Choi Date: Tue, 19 Jul 2022 12:27:22 +0900 Subject: [PATCH 16/16] util: resource: Add get_resource_attr_name and get_resource_controL_name function Add get_resource_attr_name and get_resource_controL_name function to get the attribute name and control name. Function prototype as following: - const char *get_resource_attr_name(struct resource *resource, u_int64_t attr_id) - const char *get_resource_control_name(struct resource *resource, u_int64_t ctrl_id) Change-Id: Id77a7f07f8129fc14d693b32f35f94e8b4c93f3a Signed-off-by: Chanwoo Choi --- include/util/resource.h | 2 ++ src/util/resource.c | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/include/util/resource.h b/include/util/resource.h index e96d3ba..2569ccb 100644 --- a/include/util/resource.h +++ b/include/util/resource.h @@ -135,6 +135,7 @@ void delete_resource(struct resource *resource); /* Handle resource control */ int set_resource_control(struct resource *resource, u_int64_t ctrl_id, const void *data); +const char *get_resource_control_name(struct resource *resource, u_int64_t ctrl_id); /* Handle resource attribute */ int update_resource_attrs(struct resource *resource); @@ -167,6 +168,7 @@ int get_resource_attr_ptr(struct resource *resource, u_int64_t attr_id, void **d int set_resource_attr_interest(struct resource *resource, u_int64_t interest_mask); int unset_resource_attr_interest(struct resource *resource, u_int64_t interest_mask); bool is_resource_attr_interested(struct resource *resource, u_int64_t interest_mask); +const char *get_resource_attr_name(struct resource *resource, u_int64_t attr_id); const char *get_resource_name(struct resource *resource); void *get_resource_privdata(struct resource *resource); diff --git a/src/util/resource.c b/src/util/resource.c index 4f74cc0..a07cace 100644 --- a/src/util/resource.c +++ b/src/util/resource.c @@ -242,6 +242,18 @@ int set_resource_control(struct resource *resource, u_int64_t ctrl_id, const voi return 0; } +const char *get_resource_control_name(struct resource *resource, u_int64_t ctrl_id) +{ + const struct resource_control *ctrl; + int ctrl_index = RESOURCE_CTRL_INDEX(ctrl_id); + + if (!resource) + return NULL; + ctrl = &resource->ctrls[ctrl_index]; + + return ctrl->name; +} + static int update_resource_attr(struct resource *resource, u_int64_t attr_id) { int attr_index = RESOURCE_ATTR_INDEX(attr_id); @@ -974,6 +986,17 @@ bool is_resource_attr_interested(struct resource *resource, u_int64_t interest_m return true; } +const char *get_resource_attr_name(struct resource *resource, u_int64_t attr_id) +{ + const struct resource_attribute *attr; + + attr = get_resource_attr(resource, attr_id); + if (!attr) + return NULL; + + return attr->name; +} + const char *get_resource_name(struct resource *resource) { return resource ? resource->name : NULL; -- 2.7.4