From afefb9aafdcb6a6fd5a695e7e08f268a99a4d05d Mon Sep 17 00:00:00 2001 From: Chanwoo Choi Date: Tue, 9 Aug 2022 03:46:42 +0900 Subject: [PATCH] util: resource: Add resource flag to express the specific features Each resource are able to have the non-common features. The resource flag will be used to indicate the characteristics of resource. - RESOURCE_FLAG_COUNT_ONLY_ONE : Some resource's resource count is only one on all board such as system and memory resources. Add this flag when want to return 1 as resource count. - RESOURCE_FLAG_PROCESS : process and process-group are not physical resource like CPU/GPU/Memory. Sometimes, it should be handled by the different handling. In order to separate out them, use this flag. Change-Id: Ic7df2cae2105b14382c0f084c30fbbcde5efdac4 Signed-off-by: Chanwoo Choi --- include/util/resource.h | 14 ++++++++ src/resource/resource-memory.c | 1 + src/resource/resource-process-group.c | 1 + src/resource/resource-process.c | 1 + src/resource/resource-system.c | 1 + src/util/resource.c | 39 +++++++++++++++-------- tests/integration-test/resource-monitor-tests.cpp | 6 ++-- tools/resource-monitor/resource-monitor.c | 1 - 8 files changed, 47 insertions(+), 17 deletions(-) diff --git a/include/util/resource.h b/include/util/resource.h index 95cd10b..888943e 100644 --- a/include/util/resource.h +++ b/include/util/resource.h @@ -25,6 +25,19 @@ #include #include "common.h" +/* Resource flags */ +/* + * This flags indicates that the resource has the only one resource count + * such as system/memory resources. + */ +#define RESOURCE_FLAG_COUNT_ONLY_ONE BIT(0) + +/* + * This flag incidates that resource is either process or process-group + * and is not physical h/w resource. + */ +#define RESOURCE_FLAG_PROCESS BIT(1) + struct resource; struct resource_attribute; @@ -91,6 +104,7 @@ struct resource_driver_ops { struct resource_driver { const char *name; const int type; + const u_int64_t flag; const int num_attrs; const struct resource_attribute *attrs; const int num_ctrls; diff --git a/src/resource/resource-memory.c b/src/resource/resource-memory.c index a9218d2..c3e89d7 100644 --- a/src/resource/resource-memory.c +++ b/src/resource/resource-memory.c @@ -194,6 +194,7 @@ static const struct resource_attribute memory_attrs[] = { static const struct resource_driver memory_resource_driver = { .name = "MEMORY", .type = RESOURCE_TYPE_MEMORY, + .flag = RESOURCE_FLAG_COUNT_ONLY_ONE, .attrs = memory_attrs, .num_attrs = ARRAY_SIZE(memory_attrs), }; diff --git a/src/resource/resource-process-group.c b/src/resource/resource-process-group.c index a8a0f41..0ee9d90 100644 --- a/src/resource/resource-process-group.c +++ b/src/resource/resource-process-group.c @@ -693,6 +693,7 @@ static void process_group_exit(struct resource *res) static const struct resource_driver process_group_driver = { .name = "PROCESS_GROUP", .type = RESOURCE_TYPE_PROCESS_GROUP, + .flag = RESOURCE_FLAG_PROCESS, .attrs = process_group_attrs, .num_attrs = ARRAY_SIZE(process_group_attrs), .ctrls = process_group_ctrls, diff --git a/src/resource/resource-process.c b/src/resource/resource-process.c index 0c00e04..633fc78 100644 --- a/src/resource/resource-process.c +++ b/src/resource/resource-process.c @@ -486,6 +486,7 @@ static void process_exit(struct resource *res) static const struct resource_driver process_resource_driver = { .name = "PROCESS", .type = RESOURCE_TYPE_PROCESS, + .flag = RESOURCE_FLAG_PROCESS, .attrs = process_attrs, .num_attrs = ARRAY_SIZE(process_attrs), .ctrls = process_ctrls, diff --git a/src/resource/resource-system.c b/src/resource/resource-system.c index d45ccae..dcbb343 100644 --- a/src/resource/resource-system.c +++ b/src/resource/resource-system.c @@ -320,6 +320,7 @@ static int system_driver_prepare_update(struct resource *res) static const struct resource_driver system_resource_driver = { .name = "SYSTEM", .type = RESOURCE_TYPE_SYSTEM, + .flag = RESOURCE_FLAG_COUNT_ONLY_ONE, .attrs = system_attrs, .num_attrs = ARRAY_SIZE(system_attrs), .ops = { diff --git a/src/util/resource.c b/src/util/resource.c index b71e67f..59f22aa 100644 --- a/src/util/resource.c +++ b/src/util/resource.c @@ -63,6 +63,18 @@ static gint __compare_resource_type(gconstpointer data, gconstpointer input) return -1; } +static const struct resource_driver *find_resource_driver(int resource_type) +{ + GList *node; + + node = g_list_find_custom(g_resource_driver_head, + &resource_type, __compare_resource_type); + + if (!node) + return NULL; + return (struct resource_driver *)node->data; +} + void add_resource_driver(const struct resource_driver *driver) { if (!driver) @@ -90,6 +102,7 @@ int get_resource_device_count(int resource_type) { GList *node; struct resource_device *device; + const struct resource_driver *driver; int count = 0; for (node = g_resource_device_head; node != NULL; node = node->next) { @@ -98,7 +111,19 @@ int get_resource_device_count(int resource_type) count++; } - return count; + if (count > 0) + return count; + + driver = find_resource_driver(resource_type); + if (!driver) + return -EINVAL; + + if (driver->flag & RESOURCE_FLAG_COUNT_ONLY_ONE) + return 1; + else if (driver->flag & RESOURCE_FLAG_PROCESS) + return -EINVAL; + + return 0; } const struct resource_device *find_resource_device(int resource_type, @@ -142,18 +167,6 @@ void remove_resource_device(struct resource_device *device) g_list_remove(g_resource_device_head, (gpointer)device); } -static const struct resource_driver *find_resource_driver(int resource_type) -{ - GList *node; - - node = g_list_find_custom(g_resource_driver_head, - &resource_type, __compare_resource_type); - - if (!node) - return NULL; - return (struct resource_driver *)node->data; -} - static void do_delete_resource(struct resource *resource) { if (!resource->name) diff --git a/tests/integration-test/resource-monitor-tests.cpp b/tests/integration-test/resource-monitor-tests.cpp index 5bc4189..8c87b27 100644 --- a/tests/integration-test/resource-monitor-tests.cpp +++ b/tests/integration-test/resource-monitor-tests.cpp @@ -64,10 +64,8 @@ TEST_F(ResourceMonitorTest, pass_resource_monitor_get_resource_count_valid) RESOURCE_TYPE_GPU, RESOURCE_TYPE_MEMORY, RESOURCE_TYPE_BATTERY, - RESOURCE_TYPE_PROCESS, RESOURCE_TYPE_DISPLAY, RESOURCE_TYPE_SYSTEM, - RESOURCE_TYPE_PROCESS_GROUP, RESOURCE_TYPE_DISK, RESOURCE_TYPE_NETWORK, }; @@ -91,6 +89,8 @@ TEST_F(ResourceMonitorTest, pass_resource_monitor_get_resource_count_invalid) int res_types[] = { RESOURCE_TYPE_UNKNOWN, RESOURCE_TYPE_NONSTANDARD, + RESOURCE_TYPE_PROCESS, + RESOURCE_TYPE_PROCESS_GROUP, }; int id = pass_resource_monitor_init(); @@ -99,7 +99,7 @@ TEST_F(ResourceMonitorTest, pass_resource_monitor_get_resource_count_invalid) for (i = 0; i < (int)ARRAY_SIZE(res_types); i++) { int count; int ret = pass_resource_monitor_get_resource_count(id, res_types[i], &count); - EXPECT_EQ(ret, 0); + EXPECT_NE(ret, 0); } int ret = pass_resource_monitor_exit(id); diff --git a/tools/resource-monitor/resource-monitor.c b/tools/resource-monitor/resource-monitor.c index fe2675a..558b5e2 100644 --- a/tools/resource-monitor/resource-monitor.c +++ b/tools/resource-monitor/resource-monitor.c @@ -198,7 +198,6 @@ struct __resource_type { .ctrl_id = 0, .attrs = system_attrs, .num_attrs = ARRAY_SIZE(system_attrs), - .res_count = 1, }, { .type = RESOURCE_TYPE_MEMORY, .ctrl_id = 0, -- 2.7.4