From f59d4b5f6785f808605cd6a72e06ba1d26886405 Mon Sep 17 00:00:00 2001 From: Dongwoo Lee Date: Tue, 22 Feb 2022 15:21:32 +0900 Subject: [PATCH] resource: process: Add new PROCESS_COMM and PROCESS_MEM_PERCENT attributes This introduces the new attributes as follows: PROCESS_COMM: shows the name of process. PROCESS_MEM_PERCENT: shows ratio of process rss against system memory. Change-Id: Ia50715204c4195deed3b469798f3626a2ea45d5c Signed-off-by: Dongwoo Lee --- lib/tmonitor/tmonitor.h | 6 +++-- src/resource/resource-process.c | 56 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 59 insertions(+), 3 deletions(-) diff --git a/lib/tmonitor/tmonitor.h b/lib/tmonitor/tmonitor.h index f06e54b..5bc637c 100644 --- a/lib/tmonitor/tmonitor.h +++ b/lib/tmonitor/tmonitor.h @@ -93,8 +93,10 @@ extern "C" { #define PROCESS_CPU_UTIL BIT(0) #define PROCESS_MEM_VIRT BIT(1) #define PROCESS_MEM_RSS BIT(2) -#define PROCESS_DISK_READ BIT(3) -#define PROCESS_DISK_WRITE BIT(4) +#define PROCESS_MEM_PERCENT BIT(3) +#define PROCESS_DISK_READ BIT(4) +#define PROCESS_DISK_WRITE BIT(5) +#define PROCESS_COMM BIT(6) /** * @brief Initialize the tizen monitor diff --git a/src/resource/resource-process.c b/src/resource/resource-process.c index aff58e1..2a2419d 100644 --- a/src/resource/resource-process.c +++ b/src/resource/resource-process.c @@ -30,12 +30,14 @@ struct process_context { pid_t pid; + char comm[TS_COMM_LEN]; struct taskstats prev; struct taskstats curr; u_int64_t prev_total_time; double cpu_period; int online_cpu; + u_int64_t total_memory; }; static long jiffy; @@ -112,7 +114,17 @@ static int process_get_mem_rss(const struct resource *res, rss = (double)curr->coremem / (double)curr->ac_stime; rss *= 1024.0 * 1000.0; - *data = (void *)(intptr_t)(int)rss; + switch (attr->id) { + case PROCESS_MEM_RSS: + *data = (void *)(intptr_t)(int)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); + break; + default: + return -EINVAL; + } return 0; } @@ -166,6 +178,21 @@ static int process_get_disk_write(const struct resource *res, return 0; } +static int process_get_comm(const struct resource *res, + const struct resource_attribute *attr, + void **data) +{ + struct process_context *ctx; + + if (!res || !res->priv || !attr || !data) + return -EINVAL; + + ctx = res->priv; + *data = g_strdup(ctx->comm); + + return 0; +} + static const struct resource_attribute process_attrs[] = { { .name = "PROCESS_CPU_UTIL", @@ -192,6 +219,14 @@ static const struct resource_attribute process_attrs[] = { }, }, { + .name = "PROCESS_MEM_PERCENT", + .id = PROCESS_MEM_PERCENT, + .type = DATA_TYPE_INT, + .ops = { + .get = process_get_mem_rss, + }, + }, + { .name = "PROCESS_DISK_READ", .id = PROCESS_DISK_READ, .type = DATA_TYPE_INT, @@ -207,6 +242,14 @@ static const struct resource_attribute process_attrs[] = { .get = process_get_disk_write, }, }, + { + .name = "PROCESS_COMM", + .id = PROCESS_COMM, + .type = DATA_TYPE_STRING, + .ops = { + .get = process_get_comm, + }, + }, }; #define saturatingSub(a, b) (a > b ? a - b : 0) @@ -406,6 +449,8 @@ static int process_prepare_update(struct resource *res) static int process_init(struct resource *res) { + struct resource *memory; + struct taskstats stats; struct process_context *ctx; int ret; @@ -430,6 +475,15 @@ static int process_init(struct resource *res) return -EINVAL; } + ret = query_taskstats(&stats, TASKSTATS_CMD_ATTR_PID, ctx->pid); + if (ret < 0) { + free(ctx); + return -EINVAL; + } + memcpy(ctx->comm, stats.ac_comm, TS_COMM_LEN); + + ctx->total_memory = kernel_get_memory_total() * 1024ULL; + res->priv = ctx; return 0; -- 2.7.4