From 99870d8f63e9794f933bfb1d566ec7d27a982538 Mon Sep 17 00:00:00 2001 From: Dongwoo Lee Date: Thu, 10 Mar 2022 20:32:21 +0900 Subject: [PATCH] util: resource: Allocate storage for attribute data at set interest This introduces the new way to using attribute value. Instead of allocate memory for each attribute value by driver-self, memory is allocated when client sets interest for each attribute. This can prevent from unnecessary memory allocation by deferring until it is really required. Change-Id: Ie6d47db4c6735510a0e784f8c254705c2c176b25 Signed-off-by: Dongwoo Lee --- include/util/resource.h | 10 +-- lib/tmonitor/tmonitor.h | 6 +- src/resource/resource-battery.c | 17 ++-- src/resource/resource-bus.c | 148 +++++++------------------------ src/resource/resource-cpu.c | 152 +++++++------------------------- src/resource/resource-display.c | 11 +-- src/resource/resource-gpu.c | 150 +++++++------------------------- src/resource/resource-memory.c | 71 +++------------ src/resource/resource-process.c | 130 +++++++++++----------------- src/resource/resource-system.c | 39 ++++----- src/util/resource.c | 187 ++++++++++++++++++++++------------------ 11 files changed, 286 insertions(+), 635 deletions(-) diff --git a/include/util/resource.h b/include/util/resource.h index 527e466..9290a29 100644 --- a/include/util/resource.h +++ b/include/util/resource.h @@ -58,7 +58,7 @@ struct resource_attribute_ops { const void *data, int count); int (*get)(const struct resource *res, const struct resource_attribute *attr, - void **data); + void *data); }; struct resource_attribute { @@ -168,15 +168,11 @@ int get_resource_attr_int(struct resource *resource, u_int64_t attr_id, int32_t int get_resource_attr_int64(struct resource *resource, u_int64_t attr_id, int64_t *data); int get_resource_attr_uint(struct resource *resource, u_int64_t attr_id, u_int32_t *data); int get_resource_attr_uint64(struct resource *resource, u_int64_t attr_id, u_int64_t *data); -int get_resource_attr_double(struct resource *resource, u_int64_t attr_id, double **data); -int put_resource_attr_double(struct resource *resource, u_int64_t attr_id); -int get_resource_attr_string(struct resource *resource, u_int64_t attr_id, char **data); -int put_resource_attr_string(struct resource *resource, u_int64_t attr_id); +int get_resource_attr_double(struct resource *resource, u_int64_t attr_id, double *data); +int get_resource_attr_string(struct resource *resource, u_int64_t attr_id, char *data); int get_resource_attr_array(struct resource *resource, u_int64_t attr_id, struct array_value **data); -int put_resource_attr_array(struct resource *resource, u_int64_t attr_id); int get_resource_attr_ptr(struct resource *resource, u_int64_t attr_id, void **data); -int put_resource_attr_ptr(struct resource *resource, u_int64_t attr_id); void set_resource_attr_interest(struct resource *resource, u_int64_t interest_mask); void unset_resource_attr_interest(struct resource *resource, u_int64_t interest_mask); diff --git a/lib/tmonitor/tmonitor.h b/lib/tmonitor/tmonitor.h index d661db2..b3bbeca 100644 --- a/lib/tmonitor/tmonitor.h +++ b/lib/tmonitor/tmonitor.h @@ -101,9 +101,9 @@ extern "C" { #define PROCESS_CPU_UTIL BIT(0) #define PROCESS_MEM_VIRT BIT(1) #define PROCESS_MEM_RSS BIT(2) -#define PROCESS_MEM_PERCENT BIT(3) -#define PROCESS_DISK_READ BIT(4) -#define PROCESS_DISK_WRITE BIT(5) +#define PROCESS_MEM_RSS_PERCENT BIT(3) +#define PROCESS_DISK_READ_BPS BIT(4) +#define PROCESS_DISK_WRITE_BPS BIT(5) #define PROCESS_COMM BIT(6) #define PROCESS_CTRL_TGID BIT(0) diff --git a/src/resource/resource-battery.c b/src/resource/resource-battery.c index d621c37..ded8667 100644 --- a/src/resource/resource-battery.c +++ b/src/resource/resource-battery.c @@ -32,10 +32,11 @@ static int battery_get_info(const struct resource *res, const struct resource_attribute *attr, - void **data) + void *data) { char *path = NULL; - int ret, val = 0; + int ret; + int *val = (int *)data; if (!res || !attr || !data) return -EINVAL; @@ -63,20 +64,18 @@ static int battery_get_info(const struct resource *res, return -EINVAL; } - ret = sysfs_get_int(path, &val); + ret = sysfs_get_int(path, val); if (ret < 0) return ret; - *data = (void *)(intptr_t)val; - return 0; } static int battery_get_status(const struct resource *res, const struct resource_attribute *attr, - void **data) + void *data) { - char buf[BUFF_MAX]; + char *buf = (char *)data; int ret; if (!res || !attr || !data) @@ -86,10 +85,6 @@ static int battery_get_status(const struct resource *res, if (ret < 0) return ret; - *data = strdup(buf); - if (!*data) - return -ENOMEM; - return 0; } diff --git a/src/resource/resource-bus.c b/src/resource/resource-bus.c index 9ca48d9..1fc5fe3 100644 --- a/src/resource/resource-bus.c +++ b/src/resource/resource-bus.c @@ -37,12 +37,13 @@ struct bus_context { int index; }; -static int bus_get_cur_freq(const struct resource *res, +static int bus_get_value_from_hal_power(const struct resource *res, const struct resource_attribute *attr, - void **data) + void *data) { struct bus_context *ctx; - int val; + int *val = (int *)data; + int _val; if (!res || !res->priv || !attr || !data) return -EINVAL; @@ -54,125 +55,40 @@ static int bus_get_cur_freq(const struct resource *res, return -EINVAL; } - val = hal_power_dvfs_get_curr_freq(res->type, ctx->device_name); - if (val < 0) - return -EINVAL; - - *data = (void *)(intptr_t)val; - - return 0; -} - -static int bus_get_min_freq(const struct resource *res, - const struct resource_attribute *attr, - void **data) -{ - struct bus_context *ctx; - int val; - - if (!res || !res->priv || !attr || !data) - return -EINVAL; - - ctx = res->priv; - - if (!ctx->device_name) { - _E("%s: BUS_CTRL_DEVICE_ID is not yet initialized\n", res->name); + switch (attr->id) { + case BUS_CUR_FREQ: + _val = hal_power_dvfs_get_curr_freq(res->type, ctx->device_name); + break; + case BUS_MIN_FREQ: + _val = hal_power_dvfs_get_min_freq(res->type, ctx->device_name); + break; + case BUS_MAX_FREQ: + _val = hal_power_dvfs_get_max_freq(res->type, ctx->device_name); + break; + case BUS_AVAILABLE_MIN_FREQ: + _val = hal_power_dvfs_get_available_min_freq(res->type, ctx->device_name); + break; + case BUS_AVAILABLE_MAX_FREQ: + _val = hal_power_dvfs_get_available_max_freq(res->type, ctx->device_name); + break; + default: return -EINVAL; } - val = hal_power_dvfs_get_min_freq(res->type, ctx->device_name); - if (val < 0) + if (_val < 0) return -EINVAL; - *data = (void *)(intptr_t)val; - - return 0; -} - -static int bus_get_max_freq(const struct resource *res, - const struct resource_attribute *attr, - void **data) -{ - struct bus_context *ctx; - int val; - - if (!res || !res->priv || !attr || !data) - return -EINVAL; - - ctx = res->priv; - - if (!ctx->device_name) { - _E("%s: BUS_CTRL_DEVICE_ID is not yet initialized\n", res->name); - return -EINVAL; - } - - val = hal_power_dvfs_get_max_freq(res->type, ctx->device_name); - if (val < 0) - return -EINVAL; - - *data = (void *)(intptr_t)val; - - return 0; -} - -static int bus_get_available_min_freq(const struct resource *res, - const struct resource_attribute *attr, - void **data) -{ - struct bus_context *ctx; - int val; - - if (!res || !res->priv || !attr || !data) - return -EINVAL; - - ctx = res->priv; - - if (!ctx->device_name) { - _E("%s: BUS_CTRL_DEVICE_ID is not yet initialized\n", res->name); - return -EINVAL; - } - - val = hal_power_dvfs_get_available_min_freq(res->type, ctx->device_name); - if (val < 0) - return -EINVAL; - - *data = (void *)(intptr_t)val; - - return 0; -} - -static int bus_get_available_max_freq(const struct resource *res, - const struct resource_attribute *attr, - void **data) -{ - struct bus_context *ctx; - int val; - - if (!res || !res->priv || !attr || !data) - return -EINVAL; - - ctx = res->priv; - - if (!ctx->device_name) { - _E("%s: BUS_CTRL_DEVICE_ID is not yet initialized\n", res->name); - return -EINVAL; - } - - val = hal_power_dvfs_get_available_max_freq(res->type, ctx->device_name); - if (val < 0) - return -EINVAL; - - *data = (void *)(intptr_t)val; + *val = _val; return 0; } static int bus_get_curr_governor(const struct resource *res, const struct resource_attribute *attr, - void **data) + void *data) { struct bus_context *ctx; - char buf[BUFF_MAX]; + char *buf = (char *)data; int val; if (!res || !res->priv || !attr || !data) @@ -189,10 +105,6 @@ static int bus_get_curr_governor(const struct resource *res, if (val < 0) return -EINVAL; - *data = strdup(buf); - if (!*data) - return -ENOMEM; - return 0; } @@ -202,35 +114,35 @@ static const struct resource_attribute bus_attrs[] = { .id = BUS_CUR_FREQ, .type = DATA_TYPE_INT, .ops = { - .get = bus_get_cur_freq, + .get = bus_get_value_from_hal_power, }, }, { .name = "BUS_MIN_FREQ", .id = BUS_MIN_FREQ, .type = DATA_TYPE_INT, .ops = { - .get = bus_get_min_freq, + .get = bus_get_value_from_hal_power, }, }, { .name = "BUS_MAX_FREQ", .id = BUS_MAX_FREQ, .type = DATA_TYPE_INT, .ops = { - .get = bus_get_max_freq, + .get = bus_get_value_from_hal_power, } }, { .name = "BUS_AVAILABLE_MIN_FREQ", .id = BUS_AVAILABLE_MIN_FREQ, .type = DATA_TYPE_INT, .ops = { - .get = bus_get_available_min_freq, + .get = bus_get_value_from_hal_power, } }, { .name = "BUS_AVAILABLE_MAX_FREQ", .id = BUS_AVAILABLE_MAX_FREQ, .type = DATA_TYPE_INT, .ops = { - .get = bus_get_available_max_freq, + .get = bus_get_value_from_hal_power, } }, { .name = "BUS_CUR_GOVERNOR", diff --git a/src/resource/resource-cpu.c b/src/resource/resource-cpu.c index 9930016..4527f2c 100644 --- a/src/resource/resource-cpu.c +++ b/src/resource/resource-cpu.c @@ -37,12 +37,13 @@ struct cpu_context { int index; }; -static int cpu_get_cur_freq(const struct resource *res, +static int cpu_get_value_from_hal_power(const struct resource *res, const struct resource_attribute *attr, - void **data) + void *data) { struct cpu_context *ctx; - int val; + int *val = (int *)data; + int _val; if (!res || !res->priv || !attr || !data) return -EINVAL; @@ -54,125 +55,40 @@ static int cpu_get_cur_freq(const struct resource *res, return -EINVAL; } - val = hal_power_dvfs_get_curr_freq(res->type, ctx->device_name); - if (val < 0) - return -EINVAL; - - *data = (void *)(intptr_t)val; - - return 0; -} - -static int cpu_get_min_freq(const struct resource *res, - const struct resource_attribute *attr, - void **data) -{ - struct cpu_context *ctx; - int val; - - if (!res || !res->priv || !attr || !data) - return -EINVAL; - - ctx = res->priv; - - if (!ctx->device_name) { - _E("%s: CPU_CTRL_CLUSTER_ID is not yet initialized\n", res->name); + switch (attr->id) { + case CPU_CUR_FREQ: + _val = hal_power_dvfs_get_curr_freq(res->type, ctx->device_name); + break; + case CPU_MIN_FREQ: + _val = hal_power_dvfs_get_min_freq(res->type, ctx->device_name); + break; + case CPU_MAX_FREQ: + _val = hal_power_dvfs_get_max_freq(res->type, ctx->device_name); + break; + case CPU_AVAILABLE_MIN_FREQ: + _val = hal_power_dvfs_get_available_min_freq(res->type, ctx->device_name); + break; + case CPU_AVAILABLE_MAX_FREQ: + _val = hal_power_dvfs_get_available_max_freq(res->type, ctx->device_name); + break; + default: return -EINVAL; } - val = hal_power_dvfs_get_min_freq(res->type, ctx->device_name); - if (val < 0) + if (_val < 0) return -EINVAL; - *data = (void *)(intptr_t)val; - - return 0; -} - -static int cpu_get_max_freq(const struct resource *res, - const struct resource_attribute *attr, - void **data) -{ - struct cpu_context *ctx; - int val; - - if (!res || !res->priv || !attr || !data) - return -EINVAL; - - ctx = res->priv; - - if (!ctx->device_name) { - _E("%s: CPU_CTRL_CLUSTER_ID is not yet initialized\n", res->name); - return -EINVAL; - } - - val = hal_power_dvfs_get_max_freq(res->type, ctx->device_name); - if (val < 0) - return -EINVAL; - - *data = (void *)(intptr_t)val; - - return 0; -} - -static int cpu_get_available_min_freq(const struct resource *res, - const struct resource_attribute *attr, - void **data) -{ - struct cpu_context *ctx; - int val; - - if (!res || !res->priv || !attr || !data) - return -EINVAL; - - ctx = res->priv; - - if (!ctx->device_name) { - _E("%s: CPU_CTRL_CLUSTER_ID is not yet initialized\n", res->name); - return -EINVAL; - } - - val = hal_power_dvfs_get_available_min_freq(res->type, ctx->device_name); - if (val < 0) - return -EINVAL; - - *data = (void *)(intptr_t)val; - - return 0; -} - -static int cpu_get_available_max_freq(const struct resource *res, - const struct resource_attribute *attr, - void **data) -{ - struct cpu_context *ctx; - int val; - - if (!res || !res->priv || !attr || !data) - return -EINVAL; - - ctx = res->priv; - - if (!ctx->device_name) { - _E("%s: CPU_CTRL_CLUSTER_ID is not yet initialized\n", res->name); - return -EINVAL; - } - - val = hal_power_dvfs_get_available_max_freq(res->type, ctx->device_name); - if (val < 0) - return -EINVAL; - - *data = (void *)(intptr_t)val; + *val = _val; return 0; } static int cpu_get_curr_governor(const struct resource *res, const struct resource_attribute *attr, - void **data) + void *data) { struct cpu_context *ctx; - char buf[BUFF_MAX]; + char *buf = (char *)data; int val; if (!res || !res->priv || !attr || !data) @@ -189,23 +105,19 @@ static int cpu_get_curr_governor(const struct resource *res, if (val < 0) return -EINVAL; - *data = strdup(buf); - if (!*data) - return -ENOMEM; - return 0; } static int cpu_get_online_cpu(const struct resource *res, const struct resource_attribute *attr, - void **data) + void *data) { return 0; } static int cpu_get_temperature(const struct resource *res, const struct resource_attribute *attr, - void **data) + void *data) { return 0; } @@ -216,35 +128,35 @@ static const struct resource_attribute cpu_attrs[] = { .id = CPU_CUR_FREQ, .type = DATA_TYPE_INT, .ops = { - .get = cpu_get_cur_freq, + .get = cpu_get_value_from_hal_power, }, }, { .name = "CPU_MIN_FREQ", .id = CPU_MIN_FREQ, .type = DATA_TYPE_INT, .ops = { - .get = cpu_get_min_freq, + .get = cpu_get_value_from_hal_power, }, }, { .name = "CPU_MAX_FREQ", .id = CPU_MAX_FREQ, .type = DATA_TYPE_INT, .ops = { - .get = cpu_get_max_freq, + .get = cpu_get_value_from_hal_power, } }, { .name = "CPU_AVAILABLE_MIN_FREQ", .id = CPU_AVAILABLE_MIN_FREQ, .type = DATA_TYPE_INT, .ops = { - .get = cpu_get_available_min_freq, + .get = cpu_get_value_from_hal_power, } }, { .name = "CPU_AVAILABLE_MAX_FREQ", .id = CPU_AVAILABLE_MAX_FREQ, .type = DATA_TYPE_INT, .ops = { - .get = cpu_get_available_max_freq, + .get = cpu_get_value_from_hal_power, } }, { .name = "CPU_CUR_GOVERNOR", diff --git a/src/resource/resource-display.c b/src/resource/resource-display.c index 2b95337..cbce0c9 100644 --- a/src/resource/resource-display.c +++ b/src/resource/resource-display.c @@ -48,7 +48,7 @@ struct display_fps_data { static int display_get_fps(const struct resource *res, const struct resource_attribute *attr, - void **data) + void *data) { GDBusConnection *conn; GDBusMessage *msg, *reply; @@ -56,7 +56,7 @@ static int display_get_fps(const struct resource *res, GError *err = NULL; struct display_fps_data fps_data; int *disp_id; - double *fps; + double *fps = (double *)data; int ret = 0; if (!res || !res->priv || !attr || !data) @@ -113,14 +113,7 @@ static int display_get_fps(const struct resource *res, &fps_data.window, &fps_data.fps); out: - fps = malloc(sizeof(double)); - if (!fps) { - ret = -ENOMEM; - goto err_reply; - } - *fps = fps_data.fps; - *data = (void *)fps; err_reply: g_object_unref(reply); diff --git a/src/resource/resource-gpu.c b/src/resource/resource-gpu.c index 1c27599..561e745 100644 --- a/src/resource/resource-gpu.c +++ b/src/resource/resource-gpu.c @@ -37,12 +37,13 @@ struct gpu_context { int index; }; -static int gpu_get_cur_freq(const struct resource *res, +static int gpu_get_value_from_hal_power(const struct resource *res, const struct resource_attribute *attr, - void **data) + void *data) { struct gpu_context *ctx; - int val; + int *val = (int *)data; + int _val; if (!res || !res->priv || !attr || !data) return -EINVAL; @@ -54,125 +55,40 @@ static int gpu_get_cur_freq(const struct resource *res, return -EINVAL; } - val = hal_power_dvfs_get_curr_freq(res->type, ctx->device_name); - if (val < 0) - return -EINVAL; - - *data = (void *)(intptr_t)val; - - return 0; -} - -static int gpu_get_min_freq(const struct resource *res, - const struct resource_attribute *attr, - void **data) -{ - struct gpu_context *ctx; - int val; - - if (!res || !res->priv || !attr || !data) - return -EINVAL; - - ctx = res->priv; - - if (!ctx->device_name) { - _E("%s: GPU_CTRL_DEVICE_ID is not yet initialized\n", res->name); + switch (attr->id) { + case GPU_CUR_FREQ: + _val = hal_power_dvfs_get_curr_freq(res->type, ctx->device_name); + break; + case GPU_MIN_FREQ: + _val = hal_power_dvfs_get_min_freq(res->type, ctx->device_name); + break; + case GPU_MAX_FREQ: + _val = hal_power_dvfs_get_max_freq(res->type, ctx->device_name); + break; + case GPU_AVAILABLE_MIN_FREQ: + _val = hal_power_dvfs_get_available_min_freq(res->type, ctx->device_name); + break; + case GPU_AVAILABLE_MAX_FREQ: + _val = hal_power_dvfs_get_available_max_freq(res->type, ctx->device_name); + break; + default: return -EINVAL; } - val = hal_power_dvfs_get_min_freq(res->type, ctx->device_name); - if (val < 0) + if (_val < 0) return -EINVAL; - *data = (void *)(intptr_t)val; - - return 0; -} - -static int gpu_get_max_freq(const struct resource *res, - const struct resource_attribute *attr, - void **data) -{ - struct gpu_context *ctx; - int val; - - if (!res || !res->priv || !attr || !data) - return -EINVAL; - - ctx = res->priv; - - if (!ctx->device_name) { - _E("%s: GPU_CTRL_DEVICE_ID is not yet initialized\n", res->name); - return -EINVAL; - } - - val = hal_power_dvfs_get_available_max_freq(res->type, ctx->device_name); - if (val < 0) - return -EINVAL; - - *data = (void *)(intptr_t)val; - - return 0; -} - -static int gpu_get_available_min_freq(const struct resource *res, - const struct resource_attribute *attr, - void **data) -{ - struct gpu_context *ctx; - int val; - - if (!res || !res->priv || !attr || !data) - return -EINVAL; - - ctx = res->priv; - - if (!ctx->device_name) { - _E("%s: GPU_CTRL_DEVICE_ID is not yet initialized\n", res->name); - return -EINVAL; - } - - val = hal_power_dvfs_get_available_min_freq(res->type, ctx->device_name); - if (val < 0) - return -EINVAL; - - *data = (void *)(intptr_t)val; - - return 0; -} - -static int gpu_get_available_max_freq(const struct resource *res, - const struct resource_attribute *attr, - void **data) -{ - struct gpu_context *ctx; - int val; - - if (!res || !res->priv || !attr || !data) - return -EINVAL; - - ctx = res->priv; - - if (!ctx->device_name) { - _E("%s: GPU_CTRL_DEVICE_ID is not yet initialized\n", res->name); - return -EINVAL; - } - - val = hal_power_dvfs_get_available_max_freq(res->type, ctx->device_name); - if (val < 0) - return -EINVAL; - - *data = (void *)(intptr_t)val; + *val = _val; return 0; } static int gpu_get_curr_governor(const struct resource *res, const struct resource_attribute *attr, - void **data) + void *data) { struct gpu_context *ctx; - char buf[BUFF_MAX]; + char *buf = (char *)data; int val; if (!res || !res->priv || !attr || !data) @@ -189,16 +105,12 @@ static int gpu_get_curr_governor(const struct resource *res, if (val < 0) return -EINVAL; - *data = strdup(buf); - if (!*data) - return -ENOMEM; - return 0; } static int gpu_get_temperature(const struct resource *res, const struct resource_attribute *attr, - void **data) + void *data) { return 0; } @@ -209,35 +121,35 @@ static const struct resource_attribute gpu_attrs[] = { .id = GPU_CUR_FREQ, .type = DATA_TYPE_INT, .ops = { - .get = gpu_get_cur_freq, + .get = gpu_get_value_from_hal_power, }, }, { .name = "GPU_MIN_FREQ", .id = GPU_MIN_FREQ, .type = DATA_TYPE_INT, .ops = { - .get = gpu_get_min_freq, + .get = gpu_get_value_from_hal_power, }, }, { .name = "GPU_MAX_FREQ", .id = GPU_MAX_FREQ, .type = DATA_TYPE_INT, .ops = { - .get = gpu_get_max_freq, + .get = gpu_get_value_from_hal_power, } }, { .name = "GPU_AVAILABLE_MIN_FREQ", .id = GPU_AVAILABLE_MIN_FREQ, .type = DATA_TYPE_INT, .ops = { - .get = gpu_get_available_min_freq, + .get = gpu_get_value_from_hal_power, } }, { .name = "GPU_AVAILABLE_MAX_FREQ", .id = GPU_AVAILABLE_MAX_FREQ, .type = DATA_TYPE_INT, .ops = { - .get = gpu_get_available_max_freq, + .get = gpu_get_value_from_hal_power, } }, { .name = "GPU_CUR_GOVERNOR", diff --git a/src/resource/resource-memory.c b/src/resource/resource-memory.c index 74c8fd4..f86eb93 100644 --- a/src/resource/resource-memory.c +++ b/src/resource/resource-memory.c @@ -36,11 +36,17 @@ #define PROC_MEM_INFO_MEM_AVAILABLE "MemAvailable" #define PROC_MEM_INFO_MEM_FREE "MemFree" -static inline int memory_read_val_from_proc_node(uint32_t val_id, u_int64_t *val) +static int memory_get_memory_info(const struct resource *res, + const struct resource_attribute *attr, + void *data) { + u_int64_t *val = (u_int64_t *)data; int ret; - switch (val_id) { + if (!res || !attr || !data) + return -EINVAL; + + switch (attr->id) { case MEMORY_TOTAL: ret = kernel_get_memory_total(val); break; @@ -54,64 +60,9 @@ static inline int memory_read_val_from_proc_node(uint32_t val_id, u_int64_t *val _E("wrong memory resource attribute\n"); ret = -EINVAL; } - - return ret; -} - -static int memory_get_total_memory(const struct resource *res, - const struct resource_attribute *attr, - void **data) -{ - u_int64_t val; - int ret; - - if (!res || !attr || !data) - return -EINVAL; - - ret = memory_read_val_from_proc_node(attr->id, &val); if (ret < 0) return -EINVAL; - *data = (void *)(intptr_t)val; - - return 0; -} - -static int memory_get_available_memory(const struct resource *res, - const struct resource_attribute *attr, - void **data) -{ - u_int64_t val; - int ret; - - if (!res || !attr || !data) - return -EINVAL; - - ret = memory_read_val_from_proc_node(attr->id, &val); - if (ret < 0) - return -EINVAL; - - *data = (void *)(intptr_t)val; - - return 0; -} - -static int memory_get_free_memory(const struct resource *res, - const struct resource_attribute *attr, - void **data) -{ - u_int64_t val; - int ret; - - if (!res || !attr || !data) - return -EINVAL; - - ret = memory_read_val_from_proc_node(attr->id, &val); - if (ret < 0) - return -EINVAL; - - *data = (void *)(intptr_t)val; - return 0; } @@ -121,21 +72,21 @@ static const struct resource_attribute memory_attrs[] = { .id = MEMORY_TOTAL, .type = DATA_TYPE_UINT64, .ops = { - .get = memory_get_total_memory, + .get = memory_get_memory_info, }, }, { .name = "MEMORY_AVAILABLE", .id = MEMORY_AVAILABLE, .type = DATA_TYPE_UINT64, .ops = { - .get = memory_get_available_memory, + .get = memory_get_memory_info, }, }, { .name = "MEMORY_FREE", .id = MEMORY_FREE, .type = DATA_TYPE_UINT64, .ops = { - .get = memory_get_free_memory, + .get = memory_get_memory_info, } }, }; diff --git a/src/resource/resource-process.c b/src/resource/resource-process.c index 1c28294..728d8be 100644 --- a/src/resource/resource-process.c +++ b/src/resource/resource-process.c @@ -44,11 +44,11 @@ static long jiffy; static int process_get_cpu_util(const struct resource *res, const struct resource_attribute *attr, - void **data) + void *data) { struct process_context *ctx; struct taskstats *prev, *curr; - double util = 0.0; + double *util = (double *)data; if (!res || !res->priv || !attr || !data) return -EINVAL; @@ -64,30 +64,22 @@ static int process_get_cpu_util(const struct resource *res, curr = &ctx->curr; if (ctx->cpu_period >= 1E-6) { - util = (double)(curr->ac_utime + curr->ac_stime); - util -= (double)(prev->ac_utime + prev->ac_stime); - util *= (jiffy / (ctx->cpu_period * 10.0)); - - /* - * To obtain precision after converting types between double and - * void pointer, utilization increased 1000 times (0~100000), so - * value should be divided by 1000 at user-side. - */ - util = min(util, 100000.0); + *util = (double)(curr->ac_utime + curr->ac_stime); + *util -= (double)(prev->ac_utime + prev->ac_stime); + *util *= (jiffy / (ctx->cpu_period * 10000.0)); + *util = min(*util, 100.0); } - *data = (void *)(intptr_t)(int)util; - return 0; } static int process_get_mem_virt(const struct resource *res, const struct resource_attribute *attr, - void **data) + void *data) { struct process_context *ctx; struct taskstats *curr; - double virt; + u_int64_t *virt = (u_int64_t *)data; if (!res || !res->priv || !attr || !data) return -EINVAL; @@ -101,21 +93,18 @@ static int process_get_mem_virt(const struct resource *res, curr = &ctx->curr; - virt = (double)curr->virtmem / (double)curr->ac_stime; - virt *= 1024.0 * 1000.0; - - *data = (void *)(uintptr_t)(int)virt; + *virt = (curr->virtmem * 1024 * 1024) / curr->ac_stime; return 0; } static int process_get_mem_rss(const struct resource *res, const struct resource_attribute *attr, - void **data) + void *data) { struct process_context *ctx; struct taskstats *curr; - double rss; + u_int64_t _rss; if (!res || !res->priv || !attr || !data) return -EINVAL; @@ -129,17 +118,23 @@ static int process_get_mem_rss(const struct resource *res, curr = &ctx->curr; - rss = (double)curr->coremem / (double)curr->ac_stime; - rss *= 1024.0 * 1000.0; + _rss = (curr->coremem * 1024 * 1024) / curr->ac_stime; switch (attr->id) { case PROCESS_MEM_RSS: - *data = (void *)(intptr_t)(int)rss; + { + u_int64_t *rss = (u_int64_t *)data; + + *rss = _rss; break; - case PROCESS_MEM_PERCENT: - /* percentage value should be divided by 1000 at user-side */ - *data = (void *)(intptr_t)(int)((rss / ctx->total_memory) * 100000.0); + } + case PROCESS_MEM_RSS_PERCENT: + { + double *percent = (double *)data; + + *percent = ((double)_rss / (double)ctx->total_memory) * 100.0; break; + } default: return -EINVAL; } @@ -147,14 +142,14 @@ static int process_get_mem_rss(const struct resource *res, return 0; } -static int process_get_disk_read(const struct resource *res, +static int process_get_disk_bps(const struct resource *res, const struct resource_attribute *attr, - void **data) + void *data) { struct process_context *ctx; struct taskstats *prev, *curr; u_int64_t period; - double bps = 0.0; + u_int32_t *bps = (u_int32_t *)data; if (!res || !res->priv || !attr || !data) return -EINVAL; @@ -170,49 +165,26 @@ static int process_get_disk_read(const struct resource *res, curr = &ctx->curr; period = curr->ac_etime - prev->ac_etime; - bps = (double)(curr->read_bytes - prev->read_bytes) * 1000000 / period; - *data = (void *)(intptr_t)(int)bps; - - return 0; -} - -static int process_get_disk_write(const struct resource *res, - const struct resource_attribute *attr, - void **data) -{ - struct process_context *ctx; - struct taskstats *prev, *curr; - u_int64_t period; - double bps = 0.0; - - if (!res || !res->priv || !attr || !data) - return -EINVAL; - - ctx = res->priv; - - if (!ctx->tgid) { - _E("resource %s is not yet initialized\n", res->name); + switch (attr->id) { + case PROCESS_DISK_READ_BPS: + *bps = (curr->read_bytes - prev->read_bytes) * 1000000 / period; + break; + case PROCESS_DISK_WRITE_BPS: + *bps = (curr->write_bytes - prev->write_bytes) * 1000000 / period; + break; + default: return -EINVAL; } - - prev = &ctx->prev; - curr = &ctx->curr; - - period = curr->ac_etime - prev->ac_etime; - - bps = (double)(curr->write_bytes - prev->write_bytes) * 1000000 / period; - - *data = (void *)(intptr_t)(int)bps; - return 0; } static int process_get_comm(const struct resource *res, const struct resource_attribute *attr, - void **data) + void *data) { struct process_context *ctx; + char *buf = (char *)data; if (!res || !res->priv || !attr || !data) return -EINVAL; @@ -224,7 +196,7 @@ static int process_get_comm(const struct resource *res, return -EINVAL; } - *data = g_strdup(ctx->comm); + strncpy(buf, ctx->comm, TS_COMM_LEN); return 0; } @@ -233,7 +205,7 @@ static const struct resource_attribute process_attrs[] = { { .name = "PROCESS_CPU_UTIL", .id = PROCESS_CPU_UTIL, - .type = DATA_TYPE_INT, + .type = DATA_TYPE_DOUBLE, .ops = { .get = process_get_cpu_util, }, @@ -241,7 +213,7 @@ static const struct resource_attribute process_attrs[] = { { .name = "PROCESS_MEM_VIRT", .id = PROCESS_MEM_VIRT, - .type = DATA_TYPE_INT, + .type = DATA_TYPE_UINT64, .ops = { .get = process_get_mem_virt, }, @@ -249,33 +221,33 @@ static const struct resource_attribute process_attrs[] = { { .name = "PROCESS_MEM_RSS", .id = PROCESS_MEM_RSS, - .type = DATA_TYPE_INT, + .type = DATA_TYPE_UINT64, .ops = { .get = process_get_mem_rss, }, }, { - .name = "PROCESS_MEM_PERCENT", - .id = PROCESS_MEM_PERCENT, - .type = DATA_TYPE_INT, + .name = "PROCESS_MEM_RSS_PERCENT", + .id = PROCESS_MEM_RSS_PERCENT, + .type = DATA_TYPE_DOUBLE, .ops = { .get = process_get_mem_rss, }, }, { - .name = "PROCESS_DISK_READ", - .id = PROCESS_DISK_READ, - .type = DATA_TYPE_INT, + .name = "PROCESS_DISK_READ_BPS", + .id = PROCESS_DISK_READ_BPS, + .type = DATA_TYPE_UINT, .ops = { - .get = process_get_disk_read, + .get = process_get_disk_bps, }, }, { - .name = "PROCESS_DISK_WRITE", - .id = PROCESS_DISK_WRITE, - .type = DATA_TYPE_INT, + .name = "PROCESS_DISK_WRITE_BPS", + .id = PROCESS_DISK_WRITE_BPS, + .type = DATA_TYPE_UINT, .ops = { - .get = process_get_disk_write, + .get = process_get_disk_bps, }, }, { diff --git a/src/resource/resource-system.c b/src/resource/resource-system.c index 46bd145..8375fe9 100644 --- a/src/resource/resource-system.c +++ b/src/resource/resource-system.c @@ -79,36 +79,31 @@ static double __calculate_cpu_util(int64_t id, struct cpu_stat *prev, static int system_get_avg_cpu_util(const struct resource *res, const struct resource_attribute *attr, - void **data) + void *data) { struct system_resouce_data *sysdata; - double *util; + double *util = (double *)data; if (!res || !res->priv ||!attr || !data) return -EINVAL; - util = malloc(sizeof(double)); - if (!util) - return -ENOMEM; - sysdata = (struct system_resouce_data *)res->priv; *util = __calculate_cpu_util(attr->id, &sysdata->prev_avg, &sysdata->curr_avg); - if (util < 0) { + if (*util < 0) { _W("failed to calculate average cpu util (%s: %s)\n", res->name, attr->name); *util = 0.0; } - *data = (void *)util; return 0; } static int system_get_per_cpu_util(const struct resource *res, const struct resource_attribute *attr, - void **data) + void *data) { struct system_resouce_data *sysdata; - struct array_value *array; + struct array_value *array = (struct array_value *)data; double *utils; int i; @@ -117,17 +112,16 @@ static int system_get_per_cpu_util(const struct resource *res, sysdata = (struct system_resouce_data *)res->priv; - array = calloc(1, sizeof(*array)); - if (!array) - return -ENOMEM; - array->type = DATA_TYPE_DOUBLE; array->length = sysdata->num_possible_cpus; + + if (array->data) + free(array->data); + array->data = calloc(sysdata->num_possible_cpus, sizeof(double)); - if (!array->data) { - free(array); + if (!array->data) return -ENOMEM; - } + utils = (double *)array->data; for (i = 0; i < sysdata->num_possible_cpus; i++) { @@ -139,33 +133,30 @@ static int system_get_per_cpu_util(const struct resource *res, utils[i] = 0; } } - *data = (void *)array; return 0; } static int system_get_cpu_num(const struct resource *res, const struct resource_attribute *attr, - void **data) + void *data) { - int cpu_num; + int *cpu_num = (int *)data; if (!res || !attr || !data) return -EINVAL; switch (attr->id) { case SYSTEM_POSSIBLE_CPU: - cpu_num = kernel_get_online_cpu_num(); + *cpu_num = kernel_get_online_cpu_num(); break; case SYSTEM_ONLINE_CPU: - cpu_num = kernel_get_possible_cpu_num(); + *cpu_num = kernel_get_possible_cpu_num(); break; default: return -EINVAL; } - *data = (void *)(intptr_t)cpu_num; - return 0; } diff --git a/src/util/resource.c b/src/util/resource.c index 4128e97..9e25238 100644 --- a/src/util/resource.c +++ b/src/util/resource.c @@ -231,12 +231,13 @@ static int update_resource_attr(struct resource *resource, u_int64_t attr_id) return -EINVAL; attr = &resource->attrs[attr_index]; - attr_value = &resource->attrs_value[attr_index]; if (!attr->ops.get) return -EINVAL; - ret = attr->ops.get(resource, attr, &(attr_value->data)); + attr_value = &resource->attrs_value[attr_index]; + + ret = attr->ops.get(resource, attr, attr_value->data); if (ret < 0) return ret; @@ -315,7 +316,7 @@ int get_resource_attr_int(struct resource *resource, u_int64_t attr_id, int32_t if (!attr_value) return -EINVAL; - *data = (int32_t)(intptr_t)attr_value->data; + *data = *((int32_t *)attr_value->data); return 0; } @@ -331,7 +332,7 @@ int get_resource_attr_int64(struct resource *resource, u_int64_t attr_id, int64_ if (!attr_value) return -EINVAL; - *data = (int64_t)(intptr_t)attr_value->data; + *data = *((int64_t *)attr_value->data); return 0; } @@ -347,7 +348,7 @@ int get_resource_attr_uint(struct resource *resource, u_int64_t attr_id, u_int32 if (!attr_value) return -EINVAL; - *data = (u_int32_t)(intptr_t)attr_value->data; + *data = *((u_int32_t *)attr_value->data); return 0; } @@ -363,12 +364,12 @@ int get_resource_attr_uint64(struct resource *resource, u_int64_t attr_id, u_int if (!attr_value) return -EINVAL; - *data = (u_int64_t)(intptr_t)attr_value->data; + *data = *((u_int64_t *)attr_value->data); return 0; } -int get_resource_attr_double(struct resource *resource, u_int64_t attr_id, double **data) +int get_resource_attr_double(struct resource *resource, u_int64_t attr_id, double *data) { struct resource_attribute_value *attr_value = NULL; @@ -379,29 +380,12 @@ int get_resource_attr_double(struct resource *resource, u_int64_t attr_id, doubl if (!attr_value) return -EINVAL; - *data = (double *)attr_value->data; + *data = *((double *)attr_value->data); return 0; } -int put_resource_attr_double(struct resource *resource, u_int64_t attr_id) -{ - struct resource_attribute_value *attr_value = NULL; - - if (!check_attr_validate(resource, attr_id, DATA_TYPE_DOUBLE)) - return -EINVAL; - - attr_value = get_resource_attr_value(resource, attr_id); - if (!attr_value) - return -EINVAL; - - free(attr_value->data); - attr_value->data = NULL; - - return 0; -} - -int get_resource_attr_string(struct resource *resource, u_int64_t attr_id, char **data) +int get_resource_attr_string(struct resource *resource, u_int64_t attr_id, char *data) { struct resource_attribute_value *attr_value = NULL; @@ -412,24 +396,7 @@ int get_resource_attr_string(struct resource *resource, u_int64_t attr_id, char if (!attr_value) return -EINVAL; - *data = (char *)attr_value->data; - - return 0; -} - -int put_resource_attr_string(struct resource *resource, u_int64_t attr_id) -{ - struct resource_attribute_value *attr_value = NULL; - - if (!check_attr_validate(resource, attr_id, DATA_TYPE_STRING)) - return -EINVAL; - - attr_value = get_resource_attr_value(resource, attr_id); - if (!attr_value || !attr_value->data) - return -EINVAL; - - free(attr_value->data); - attr_value->data = NULL; + strncpy(data, (char *)attr_value->data, BUFF_MAX); return 0; } @@ -450,31 +417,6 @@ int get_resource_attr_array(struct resource *resource, u_int64_t attr_id, struct return 0; } -int put_resource_attr_array(struct resource *resource, u_int64_t attr_id) -{ - struct resource_attribute_value *attr_value = NULL; - struct array_value *array; - - if (!check_attr_validate(resource, attr_id, DATA_TYPE_ARRAY)) - return -EINVAL; - - attr_value = get_resource_attr_value(resource, attr_id); - if (!attr_value || !attr_value->data) - return -EINVAL; - - array = (struct array_value *)attr_value->data; - if (array) { - array->type = 0; - array->length = 0; - free(array->data); - array->data = NULL; - } - free(attr_value->data); - attr_value->data = NULL; - - return 0; -} - int get_resource_attr_ptr(struct resource *resource, u_int64_t attr_id, void **data) { struct resource_attribute_value *attr_value = NULL; @@ -491,35 +433,110 @@ int get_resource_attr_ptr(struct resource *resource, u_int64_t attr_id, void **d return 0; } -int put_resource_attr_ptr(struct resource *resource, u_int64_t attr_id) +void set_resource_attr_interest(struct resource *resource, u_int64_t interest_mask) { - struct resource_attribute_value *attr_value = NULL; + struct resource_attribute_value *attr_value; + int i; - if (!check_attr_validate(resource, attr_id, DATA_TYPE_PTR)) - return -EINVAL; - - attr_value = get_resource_attr_value(resource, attr_id); - if (!attr_value || !attr_value->data) - return -EINVAL; + if (!resource) + return; - free(attr_value->data); - attr_value->data = NULL; + for (i = 0; i < resource->num_attrs; i++) { + if (!(resource->attrs[i].id & interest_mask)) + continue; - return 0; -} + attr_value = get_resource_attr_value(resource, resource->attrs[i].id); + if (!attr_value) { + _E("failed to get attribute value: resource: %s, attribute: %s", + resource->name, resource->attrs[i].name); + return; + } -void set_resource_attr_interest(struct resource *resource, u_int64_t interest_mask) -{ - if (!resource) - return; + /* + * In resource monitor, each resource has a lot of attributes, but + * only updated attributes are selected by clients on demand. So, + * instead of allocating memory at the resource creation, allocate + * at the set interest. + */ + if (!attr_value->data) { + switch (attr_value->type) { + case DATA_TYPE_INT: + attr_value->data = calloc(1, sizeof(int32_t)); + break; + case DATA_TYPE_INT64: + attr_value->data = calloc(1, sizeof(int64_t)); + break; + case DATA_TYPE_UINT: + attr_value->data = calloc(1, sizeof(u_int32_t)); + break; + case DATA_TYPE_UINT64: + attr_value->data = calloc(1, sizeof(u_int64_t)); + break; + case DATA_TYPE_DOUBLE: + attr_value->data = calloc(1, sizeof(double)); + break; + case DATA_TYPE_STRING: + attr_value->data = calloc(BUFF_MAX, sizeof(char)); + break; + case DATA_TYPE_ARRAY: + attr_value->data = calloc(1, sizeof(struct array_value)); + break; + default: + _E("Not supported data type: %d", attr_value->type); + return; + } + } + } resource->attr_interest |= interest_mask; } void unset_resource_attr_interest(struct resource *resource, u_int64_t interest_mask) { + struct resource_attribute_value *attr_value; + int i; + if (!resource) return; + for (i = 0; i < resource->num_attrs; i++) { + if (!(resource->attrs[i].id & interest_mask)) + continue; + + attr_value = get_resource_attr_value(resource, resource->attrs[i].id); + if (!attr_value) { + _E("failed to get attribute value: resource: %s, attribute: %s", + resource->name, resource->attrs[i].name); + return; + } + + if (attr_value->data) { + switch (attr_value->type) { + case DATA_TYPE_ARRAY: + { + struct array_value *array = attr_value->data; + + if (array->data) { + free(array->data); + array->data = NULL; + } + /* fall through */ + } + case DATA_TYPE_INT: + case DATA_TYPE_INT64: + case DATA_TYPE_UINT: + case DATA_TYPE_UINT64: + case DATA_TYPE_DOUBLE: + case DATA_TYPE_STRING: + free(attr_value->data); + attr_value->data = NULL; + break; + default: + _E("Not supported data type: %d", attr_value->type); + return; + } + } + } + resource->attr_interest &= ~interest_mask; } -- 2.7.4