summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDongwoo Lee <dwoo08.lee@samsung.com>2022-10-12 18:41:39 +0900
committerYoungjae Cho <y0.cho@samsung.com>2022-10-31 11:21:11 +0900
commit1cb4ff9439c7d7d2bf7a16961b24ce0509f1d540 (patch)
treec18f21d37a193325855b8a4ae17c252d30e33483
parente0ba7790b336732ea303f8394726ab6f32c045b0 (diff)
downloadpass-1cb4ff9439c7d7d2bf7a16961b24ce0509f1d540.tar.gz
pass-1cb4ff9439c7d7d2bf7a16961b24ce0509f1d540.tar.bz2
pass-1cb4ff9439c7d7d2bf7a16961b24ce0509f1d540.zip
resource: extract resource core from monitor
To make resource-related features into library, this extracts core part of resource util from resource monitor. Change-Id: I69930d9b7a83e87b3f67fd507360464eea7d4b44 Signed-off-by: Dongwoo Lee <dwoo08.lee@samsung.com>
-rw-r--r--include/util/resource.h30
-rw-r--r--src/monitor/request-handler.c161
-rw-r--r--src/resource/resource-disk.c5
-rw-r--r--src/util/resource.c120
4 files changed, 161 insertions, 155 deletions
diff --git a/include/util/resource.h b/include/util/resource.h
index 0855c80..eb46610 100644
--- a/include/util/resource.h
+++ b/include/util/resource.h
@@ -119,6 +119,25 @@ struct resource_driver {
const struct resource_driver_ops ops;
};
+struct resource {
+ char *name;
+ const struct resource_driver *driver;
+ int type;
+
+ 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 flag;
+
+ void *priv;
+
+ u_int64_t attr_interest;
+ u_int64_t attr_supported;
+};
+
struct resource_device {
char *name;
int type;
@@ -196,21 +215,10 @@ 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);
-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);
-void init_resource_id(void);
int set_resource_privdata(struct resource *resource, void *priv);
void init_resource_drivers(void);
void exit_resource_drivers(void);
-
-inline __attribute__((always_inline)) int64_t get_time_now(void)
-{
- struct timeval tv;
-
- gettimeofday(&tv, NULL);
- return (int64_t)tv.tv_sec * 1000000 + (int64_t)tv.tv_usec;
-}
#endif
diff --git a/src/monitor/request-handler.c b/src/monitor/request-handler.c
index 3ffc258..4059ddc 100644
--- a/src/monitor/request-handler.c
+++ b/src/monitor/request-handler.c
@@ -53,27 +53,24 @@ struct request_client {
GHashTable *resource_table;
};
-static bool g_request_server_run;
-static struct thread *g_server_thread;
+struct resource_instance {
+ int id;
+ struct resource *res;
-static void update_resource(gpointer key, gpointer value, gpointer user_data)
-{
- struct resource *res = value;
- int ret;
+ int64_t ts_start;
+ int64_t ts_end;
+};
- ret = update_resource_attrs(res);
- if (ret < 0)
- _E("failed to update resource attributes (name:%s,id:%d)\n",
- get_resource_name(res),
- get_resource_id(res));
-}
+static int g_resource_id;
+static bool g_request_server_run;
+static struct thread *g_server_thread;
static void
-register_resource_to_client(struct request_client *client, struct resource *res)
+register_resource_to_client(struct request_client *client, struct resource_instance *res_inst)
{
g_hash_table_insert(client->resource_table,
- GINT_TO_POINTER(get_resource_id(res)),
- (gpointer)res);
+ GINT_TO_POINTER(res_inst->id),
+ (gpointer)res_inst);
}
static void
@@ -82,15 +79,32 @@ unregister_resource_from_client(struct request_client *client, int resource_id)
g_hash_table_remove(client->resource_table, GINT_TO_POINTER(resource_id));
}
+static struct resource_instance *
+get_resource_instance_by_id(struct request_client *client, int resource_id)
+{
+ return g_hash_table_lookup(client->resource_table, GINT_TO_POINTER(resource_id));
+}
+
static struct resource *
get_resource_by_id(struct request_client *client, int resource_id)
{
- return g_hash_table_lookup(client->resource_table, GINT_TO_POINTER(resource_id));
+ struct resource_instance *res_inst = get_resource_instance_by_id(client, resource_id);
+
+ res_inst = get_resource_instance_by_id(client, resource_id);
+ if (!res_inst)
+ return NULL;
+
+ return res_inst->res;
+}
+
+static int clear_sign_bit(unsigned int val)
+{
+ return (int)((val << 1) >> 1);
}
static int handle_request_create_resource(struct request_client *client, char *args)
{
- struct resource *res;
+ struct resource_instance *res_inst;
int resource_type, ret;
if (!client || !args) {
@@ -98,21 +112,29 @@ static int handle_request_create_resource(struct request_client *client, char *a
return -ENOENT;
}
+ res_inst = calloc(1, sizeof(struct resource_instance));
+ if (!res_inst)
+ return -ENOMEM;
+
/**
* Format of REQUEST_CREATE_RESOURCE args:
* - <RESOURCE_TYPE>
*/
resource_type = atoi(args);
- ret = create_resource(&res, resource_type);
+ ret = create_resource(&res_inst->res, resource_type);
if (ret < 0) {
_E("failed to create resource, res:type(%d)\n", resource_type);
+ free(res_inst);
return ret;
}
- register_resource_to_client(client, res);
+ res_inst->id = clear_sign_bit(
+ (unsigned int)__sync_fetch_and_add(&g_resource_id, 1));
- return get_resource_id(res);
+ register_resource_to_client(client, res_inst);
+
+ return res_inst->id;
}
static int handle_request_delete_resource(struct request_client *client, char *args)
@@ -135,8 +157,17 @@ static int handle_request_delete_resource(struct request_client *client, char *a
return 0;
}
+inline __attribute__((always_inline)) int64_t get_time_now(void)
+{
+ struct timeval tv;
+
+ gettimeofday(&tv, NULL);
+ return (int64_t)tv.tv_sec * 1000000 + (int64_t)tv.tv_usec;
+}
+
static int handle_request_update_resource(struct request_client *client, char *args)
{
+ struct resource_instance *res_inst;
struct resource *res;
int resource_id;
int ret;
@@ -152,14 +183,19 @@ static int handle_request_update_resource(struct request_client *client, char *a
*/
resource_id = atoi(args);
- res = get_resource_by_id(client, resource_id);
- if (!res) {
- _E("failed to get resource, res:id(%d)\n", resource_id);
+ res_inst = get_resource_instance_by_id(client, resource_id);
+ if (!res_inst) {
+ _E("failed to get resource instance, res:id(%d)\n", resource_id);
return -EINVAL;
}
+ res = res_inst->res;
+
+ res_inst->ts_start = get_time_now();
ret = update_resource_attrs(res);
+ res_inst->ts_end = get_time_now();
if (ret < 0) {
+ res_inst->ts_start = res_inst->ts_end = 0;
_E("failed to update resource attributes, res:name(%s)id(%d)\n",
get_resource_name(res), resource_id);
return ret;
@@ -194,6 +230,19 @@ static int handle_request_get_resource_count(struct request_client *client, char
return 0;
}
+static void update_resource(gpointer key, gpointer value, gpointer user_data)
+{
+ struct resource_instance *res_inst = value;
+ struct resource *res = res_inst->res;
+ int ret;
+
+ ret = update_resource_attrs(res);
+ if (ret < 0)
+ _E("failed to update resource attributes (name:%s,id:%d)\n",
+ get_resource_name(res), res_inst->id);
+}
+
+
static int handle_request_update_resource_all(struct request_client *client, char *args)
{
if (!client) {
@@ -234,9 +283,8 @@ static int handle_request_set_resource_attr(struct request_client *client, char
res = get_resource_by_id(client, resource_id);
if (!res) {
- _E("failed to get resource, client(%d) | res:name(%s)id(%d)\n",
- client->socket_fd,
- get_resource_name(res), resource_id);
+ _E("failed to get resource, client(%d) | res:id(%d)\n",
+ client->socket_fd, resource_id);
return -EINVAL;
}
@@ -288,9 +336,8 @@ static int handle_request_set_resource_flag(struct request_client *client, char
res = get_resource_by_id(client, resource_id);
if (!res) {
- _E("failed to get resource, client(%d) | res:name(%s)id(%d)\n",
- client->socket_fd,
- get_resource_name(res), resource_id);
+ _E("failed to get resource, client(%d) | res:id(%d)\n",
+ client->socket_fd, resource_id);
return -EINVAL;
}
@@ -713,11 +760,22 @@ handle_request_get_value_array(struct request_client *client, char *args, struct
return 0;
}
+static int get_resource_ts(struct resource_instance *inst, int64_t *ts_start, int64_t *ts_end)
+{
+ if (!inst)
+ return -EINVAL;
+
+ *ts_start = inst->ts_start;
+ *ts_end = inst->ts_end;
+
+ return 0;
+}
+
static int
handle_request_get_resource_ts(struct request_client *client, char *args,
int64_t *start, int64_t *end)
{
- struct resource *res;
+ struct resource_instance *res_inst;
int resource_id, ret;
if (!client || !args) {
@@ -731,18 +789,18 @@ handle_request_get_resource_ts(struct request_client *client, char *args,
*/
resource_id = atoi(args);
- res = get_resource_by_id(client, resource_id);
- if (!res) {
+ res_inst = get_resource_by_id(client, resource_id);
+ if (!res_inst) {
_E("failed to get resource, client(%d) | res:id(%d)\n",
client->socket_fd, resource_id);
return -EINVAL;
}
- ret = get_resource_ts(res, start, end);
+ ret = get_resource_ts(res_inst, start, end);
if (ret < 0) {
_E("failed to get timestamp value, client(%d) | res:name(%s)id(%d)\n",
client->socket_fd,
- get_resource_name(res), resource_id);
+ get_resource_name(res_inst->res), resource_id);
return ret;
}
@@ -1061,6 +1119,16 @@ static int request_handler_func(void *data, void **result)
return THREAD_RETURN_DONE;
}
+static void delete_resource_instance(struct resource_instance *res_inst)
+{
+ if (!res_inst)
+ return;
+
+ delete_resource(res_inst->res);
+
+ free(res_inst);
+}
+
static int create_request_client(int socket_fd)
{
struct request_client *client;
@@ -1073,7 +1141,7 @@ static int create_request_client(int socket_fd)
client->socket_fd = socket_fd;
client->resource_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
- NULL, (GDestroyNotify)delete_resource);
+ NULL, (GDestroyNotify)delete_resource_instance);
create_daemon_thread(&client->worker, request_handler_func, client);
@@ -1197,6 +1265,29 @@ error_out:
return -EIO;
}
+static void init_resource_id(void)
+{
+ int val;
+ struct timeval tv;
+
+ /*
+ * Initial g_resource_id is set by a combination of
+ * tv_sec and tv_usec values of gettimeofday().
+ * It ensures that PASS assigns a unique resource id
+ * for each create_resource request.
+ *
+ * Even PASS is restarted, a newly initialized value
+ * of g_resource_id reflects the current value of
+ * gettimeofday(). Thus, the uniqueness in resource
+ * id is kept unless the number of created resources
+ * crosses the time consumed (in msecs) before
+ * restarted.
+ */
+ gettimeofday(&tv, NULL);
+ val = tv.tv_sec * 1000 + tv.tv_usec / 1000;
+ g_resource_id = clear_sign_bit(val);
+}
+
static int request_server_func(void *ctx, void **result)
{
struct sockaddr_un unix_address;
diff --git a/src/resource/resource-disk.c b/src/resource/resource-disk.c
index 20fce27..7c26c9b 100644
--- a/src/resource/resource-disk.c
+++ b/src/resource/resource-disk.c
@@ -87,9 +87,8 @@ static int disk_get_value(struct resource *res,
return -EINVAL;
if (!ctx->device_name) {
- _E("DISK_CTRL_DEVICE_ID is not yet initialized, res:name(%s)id(%d) | attr:name(%s)id(%"PRId64")\n",
- get_resource_name(res), get_resource_id(res),
- get_resource_attr_name(res, attr->id), attr->id);
+ _E("DISK_CTRL_DEVICE_ID is not yet initialized, res:name(%s) | attr:name(%s)id(%"PRId64")\n",
+ get_resource_name(res), get_resource_attr_name(res, attr->id), attr->id);
return -EINVAL;
}
diff --git a/src/util/resource.c b/src/util/resource.c
index 39ccc68..963bb83 100644
--- a/src/util/resource.c
+++ b/src/util/resource.c
@@ -19,7 +19,6 @@
#include <glib.h>
#include <stdio.h>
#include <json.h>
-#include <sys/time.h>
#include <util/common.h>
#include <util/resource.h>
@@ -32,28 +31,6 @@
#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;
-
- u_int64_t flag;
-
- 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;
@@ -199,34 +176,6 @@ void delete_resource(struct resource *resource)
do_delete_resource(resource);
}
-static int clear_sign_bit(unsigned int val)
-{
- return (int)((val << 1) >> 1);
-}
-
-void init_resource_id(void)
-{
- int val;
- struct timeval tv;
-
- /*
- * Initial g_resource_id is set by a combination of
- * tv_sec and tv_usec values of gettimeofday().
- * It ensures that PASS assigns a unique resource id
- * for each create_resource request.
- *
- * Even PASS is restarted, a newly initialized value
- * of g_resource_id reflects the current value of
- * gettimeofday(). Thus, the uniqueness in resource
- * id is kept unless the number of created resources
- * crosses the time consumed (in msecs) before
- * restarted.
- */
- gettimeofday(&tv, NULL);
- val = tv.tv_sec * 1000 + tv.tv_usec / 1000;
- g_resource_id = clear_sign_bit(val);
-}
-
int create_resource(struct resource **res, int resource_type)
{
const struct resource_driver *driver = NULL;
@@ -247,8 +196,6 @@ int create_resource(struct resource **res, int resource_type)
if (!resource)
return -ENOMEM;
- resource->id = clear_sign_bit(
- (unsigned int)__sync_fetch_and_add(&g_resource_id, 1));
resource->type = resource_type;
resource->name = g_strdup(driver->name);
resource->driver = driver;
@@ -271,8 +218,8 @@ int create_resource(struct resource **res, int resource_type)
if (driver->ops.create) {
ret = driver->ops.create(resource);
if (ret < 0) {
- _E("failed to initialize resource driver, res:type(%s)id(%d)\n",
- resource->name, resource->id);
+ _E("failed to initialize resource driver, res:type(%s)\n",
+ resource->name);
do_delete_resource(resource);
return ret;
}
@@ -344,9 +291,8 @@ static int update_resource_attr(struct resource *resource, u_int64_t attr_id)
attr = &resource->attrs[attr_index];
if (!attr->ops.get) {
- _E("don't support get ops of resource attribute value, res:type(%s)id(%d) | attr:name(%s)id(%"PRId64")\n",
- resource->name, resource->id,
- attr->name, attr_id);
+ _E("don't support get ops of resource attribute value, res:type(%s) | attr:name(%s)id(%"PRId64")\n",
+ resource->name, attr->name, attr_id);
return -EINVAL;
}
@@ -354,9 +300,8 @@ static int update_resource_attr(struct resource *resource, u_int64_t attr_id)
ret = attr->ops.get(resource, attr, attr_value->data);
if (ret < 0) {
- _E("failed to get resource attribute value, res:type(%s)id(%d) | attr:name(%s)id(%"PRId64")\n",
- resource->name, resource->id,
- attr->name, attr_id);
+ _E("failed to get resource attribute value, res:type(%s) | attr:name(%s)id(%"PRId64")\n",
+ resource->name, attr->name, attr_id);
return ret;
}
@@ -370,13 +315,11 @@ int update_resource_attrs(struct resource *resource)
if (!resource || !resource->type)
return -EINVAL;
- resource->ts_start = get_time_now();
-
if (resource->driver && resource->driver->ops.prepare_update) {
ret = resource->driver->ops.prepare_update(resource);
if (ret < 0) {
- _E("failed to prepare_update resource driver, res:type(%s)id(%d)\n",
- resource->name, resource->id);
+ _E("failed to prepare_update resource driver, res:type(%s)\n",
+ resource->name);
return ret;
}
}
@@ -386,14 +329,11 @@ int update_resource_attrs(struct resource *resource)
continue;
ret = update_resource_attr(resource, resource->attrs[i].id);
if (ret < 0) {
- _E("failed to update resource attribute, res:type(%s)id(%d) | attr:name(%s)id(%"PRId64")\n",
- resource->name, resource->id,
- resource->attrs[i].name, resource->attrs[i].id);
+ _E("failed to update resource attribute, res:type(%s) | attr:name(%s)id(%"PRId64")\n",
+ resource->name, resource->attrs[i].name, resource->attrs[i].id);
}
}
- resource->ts_end = get_time_now();
-
return 0;
}
@@ -471,17 +411,14 @@ static bool check_attr_validate(struct resource *resource, u_int64_t attr_id, in
}
if (attr->type != type) {
- _E("Invalid data type(%d), res:type(%s)id(%d) | attr:name(%s)id(%"PRId64",%d)\n",
- type,
- resource->name, resource->id,
- attr->name, attr->id, attr->type);
+ _E("Invalid data type(%d), res:type(%s) | attr:name(%s)id(%"PRId64",%d)\n",
+ type, resource->name, attr->name, attr->id, attr->type);
return false;
}
if (!(attr->id & resource->attr_interest)) {
- _E("Invalid interest state, res:type(%s)id(%d) | attr:name(%s)id(%"PRId64")type(%d)\n",
- resource->name, resource->id,
- attr->name, attr->id, attr->type);
+ _E("Invalid interest state, res:type(%s) | attr:name(%s)id(%"PRId64")type(%d)\n",
+ resource->name, attr->name, attr->id, attr->type);
return false;
}
@@ -614,7 +551,6 @@ static void _put_resource_attr_json(json_object *jobj_attr)
int get_resource_attrs_json(struct resource *resource, char **json_string)
{
json_object *jobj_root, *jobj_res_name, *jobj_res_type, *jobj_res_attrs, *jobj_attr;
- json_object *jobj_res_ts, *jobj_ts_start, *jobj_ts_end;
const struct resource_attribute *attr;
const struct resource_attribute_value *attr_value;
int i;
@@ -640,30 +576,18 @@ int get_resource_attrs_json(struct resource *resource, char **json_string)
json_object_array_add(jobj_res_attrs, jobj_attr);
}
- jobj_res_ts = json_object_new_object();
- jobj_ts_start = json_object_new_int64(resource->ts_start);
- jobj_ts_end = json_object_new_int64(resource->ts_end);
-
- json_object_object_add(jobj_res_ts, "start", jobj_ts_start);
- json_object_object_add(jobj_res_ts, "end", jobj_ts_end);
-
json_object_object_add(jobj_root, "res_name", jobj_res_name);
json_object_object_add(jobj_root, "res_type", jobj_res_type);
json_object_object_add(jobj_root, "res_attrs", jobj_res_attrs);
- json_object_object_add(jobj_root, "res_ts", jobj_res_ts);
*json_string = strdup(json_object_to_json_string(jobj_root));
- json_object_object_del(jobj_res_ts, "end");
- json_object_object_del(jobj_res_ts, "start");
-
for (i = 0; i < json_object_array_length(jobj_res_attrs); i++) {
jobj_attr = json_object_array_get_idx(jobj_res_attrs, i);
_put_resource_attr_json(jobj_attr);
}
json_object_array_del_idx(jobj_res_attrs, 0, json_object_array_length(jobj_res_attrs));
- json_object_object_del(jobj_root, "res_ts");
json_object_object_del(jobj_root, "res_attrs");
json_object_object_del(jobj_root, "res_type");
json_object_object_del(jobj_root, "res_name");
@@ -1154,27 +1078,11 @@ 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)