From 45efc07414e3234c6bc4ef94ed4da68a44a471c7 Mon Sep 17 00:00:00 2001 From: Youngjae Cho Date: Tue, 21 Feb 2023 12:10:00 +0900 Subject: [PATCH] resource-manager: Reimplement attribute_ops attribute_ops is in charge of operations for resource instance type of INSTANCE_TYPE_NORMAL. As the is_resource_attr_supported() works based on instance type, API for NORMAL instance has been added. - syscommon_resman_is_resource_attr_supported() Change-Id: Ia9078f9d4639f6ce3812a4749aaf0ada87af22f2 Signed-off-by: Youngjae Cho --- src/resource-manager/resource-manager.c | 65 +++++++++++++++++++++---- src/resource-manager/resource-manager.h | 17 +++++++ 2 files changed, 73 insertions(+), 9 deletions(-) diff --git a/src/resource-manager/resource-manager.c b/src/resource-manager/resource-manager.c index 953ed85..f9eb0e3 100644 --- a/src/resource-manager/resource-manager.c +++ b/src/resource-manager/resource-manager.c @@ -62,6 +62,14 @@ struct syscommon_resman_resource { u_int64_t attr_supported; }; +struct attribute_is_supported_ops { + bool (*is_supported)(int resource_id, + const struct syscommon_resman_resource_attribute *attr); + int (*get)(int resource_id, + const struct syscommon_resman_resource_attribute *attr, + void *data); +}; + static int set_resource_attr_interest(struct syscommon_resman_resource *resource, u_int64_t interest_mask); static int unset_resource_attr_interest(struct syscommon_resman_resource *resource, u_int64_t interest_mask); static bool is_resource_attr_interested(struct syscommon_resman_resource *resource, u_int64_t interest_mask); @@ -444,12 +452,35 @@ syscommon_resman_get_resource_attr_value(int resource_id, u_int64_t attr_id) return get_resource_attr_value(resource, attr_id); } -static int monitor_is_resource_attr_supported(struct syscommon_resman_resource *resource, u_int64_t attr_id, bool *supported) +static int select_attribute_is_supported_ops(int instance_type, + const struct syscommon_resman_resource_attribute *attr, struct attribute_is_supported_ops *ops) +{ + assert(attr); + assert(ops); + + switch (instance_type) { + case INSTANCE_TYPE_NORMAL: + ops->is_supported = attr->ops.is_supported; + ops->get = attr->ops.get; + return 0; + case INSTANCE_TYPE_MONITOR: + ops->is_supported = attr->monitor_ops.is_supported; + ops->get = attr->monitor_ops.get; + return 0; + default: + assert(0); // unreachable + } + + return -EINVAL; +} + +static int is_resource_attr_supported(struct syscommon_resman_resource *resource, u_int64_t attr_id, bool *supported) { const struct syscommon_resman_resource_attribute *attr = NULL; int attr_index = RESOURCE_ATTR_INDEX(attr_id); int ret; bool is_supported = false; + struct attribute_is_supported_ops ops = { 0 , }; if (!resource || attr_index < 0 || attr_index >= resource->num_attrs) { *supported = false; @@ -458,11 +489,18 @@ static int monitor_is_resource_attr_supported(struct syscommon_resman_resource * attr = &resource->attrs[attr_index]; + /* Select ops for testing support by instance type */ + ret = select_attribute_is_supported_ops(resource->instance_type, attr, &ops); + if (ret < 0) { + *supported = false; + return ret; + } + if (attr->id & resource->attr_supported) { is_supported = true; - } else if (attr->monitor_ops.is_supported) { - is_supported = attr->monitor_ops.is_supported(resource->id, attr); - } else if (attr->monitor_ops.get) { + } else if (ops.is_supported) { + is_supported = ops.is_supported(resource->id, attr); + } else if (ops.get) { /* * Optionally, if .is_supported ops is not implemented, * use .get ops in order to check whether the resource attribute @@ -470,7 +508,7 @@ static int monitor_is_resource_attr_supported(struct syscommon_resman_resource * */ char data[SYSCOMMON_RESMAN_BUFF_MAX] = {0, }; - ret = attr->monitor_ops.get(resource->id, attr, (void *)data); + ret = ops.get(resource->id, attr, (void *)data); is_supported = (ret < 0) ? false : true; } @@ -482,12 +520,23 @@ static int monitor_is_resource_attr_supported(struct syscommon_resman_resource * return 0; } +int +syscommon_resman_is_resource_attr_supported(int resource_id, u_int64_t attr_id, bool *supported) +{ + struct syscommon_resman_resource *resource = find_resource(resource_id); + + if (!resource || resource->instance_type != INSTANCE_TYPE_NORMAL) + return -EINVAL; + + return is_resource_attr_supported(resource, attr_id, supported); +} + int syscommon_resman_monitor_is_resource_attr_supported(int resource_id, u_int64_t attr_id, bool *supported) { struct syscommon_resman_resource *resource = find_resource(resource_id); - return monitor_is_resource_attr_supported(resource, attr_id, supported); + return is_resource_attr_supported(resource, attr_id, supported); } static bool @@ -952,9 +1001,7 @@ set_resource_attr_interest(struct syscommon_resman_resource *resource, u_int64_t if (!(resource->attrs[i].id & interest_mask)) continue; - ret = monitor_is_resource_attr_supported(resource, - resource->attrs[i].id, - &supported); + ret = is_resource_attr_supported(resource, resource->attrs[i].id, &supported); if (ret < 0) { goto err; } else if (!supported) { diff --git a/src/resource-manager/resource-manager.h b/src/resource-manager/resource-manager.h index 5086cf4..cc6d886 100644 --- a/src/resource-manager/resource-manager.h +++ b/src/resource-manager/resource-manager.h @@ -48,6 +48,21 @@ struct syscommon_resman_resource_attribute_value { void *data; }; +struct syscommon_resman_resource_attribute_ops { + int (*set)(int resource_id, + const struct syscommon_resman_resource_attribute *attr, + const void *data, int count); + int (*get)(int resource_id, + const struct syscommon_resman_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)(int resource_id, + const struct syscommon_resman_resource_attribute *attr); +}; + struct syscommon_resman_resource_attribute_monitor_ops { int (*get)(int resource_id, const struct syscommon_resman_resource_attribute *attr, @@ -72,6 +87,7 @@ struct syscommon_resman_resource_attribute { const u_int64_t id; const int type; const u_int64_t flag; + const struct syscommon_resman_resource_attribute_ops ops; const struct syscommon_resman_resource_attribute_monitor_ops monitor_ops; const struct syscommon_resman_resource_attribute_listener_ops listener_ops; }; @@ -148,6 +164,7 @@ const struct syscommon_resman_resource_attribute * syscommon_resman_get_resource_attr(int resource_id, u_int64_t attr_id); struct syscommon_resman_resource_attribute_value * syscommon_resman_get_resource_attr_value(int resource_id, u_int64_t attr_id); +int syscommon_resman_is_resource_attr_supported(int resource_id, u_int64_t attr_id, bool *supported); int syscommon_resman_monitor_is_resource_attr_supported(int resource_id, u_int64_t attr_id, bool *supported); static inline bool -- 2.34.1