util: resource: Add resource flag to express the specific features 26/279426/4
authorChanwoo Choi <cw00.choi@samsung.com>
Mon, 8 Aug 2022 18:46:42 +0000 (03:46 +0900)
committerChanwoo Choi <cw00.choi@samsung.com>
Thu, 11 Aug 2022 08:54:40 +0000 (17:54 +0900)
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 <cw00.choi@samsung.com>
include/util/resource.h
src/resource/resource-memory.c
src/resource/resource-process-group.c
src/resource/resource-process.c
src/resource/resource-system.c
src/util/resource.c
tests/integration-test/resource-monitor-tests.cpp
tools/resource-monitor/resource-monitor.c

index 95cd10b..888943e 100644 (file)
 #include <glib.h>
 #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;
index a9218d2..c3e89d7 100644 (file)
@@ -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),
 };
index a8a0f41..0ee9d90 100644 (file)
@@ -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,
index 0c00e04..633fc78 100644 (file)
@@ -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,
index d45ccae..dcbb343 100644 (file)
@@ -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 = {
index b71e67f..59f22aa 100644 (file)
@@ -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)
index 5bc4189..8c87b27 100644 (file)
@@ -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);
index fe2675a..558b5e2 100644 (file)
@@ -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,