From fe5e0dd127e30fe312166fa82adfe9111278fb99 Mon Sep 17 00:00:00 2001 From: Dongwoo Lee Date: Thu, 31 Mar 2022 13:16:49 +0900 Subject: [PATCH] resource: process: Get initial stats of target process with taskstats Since open and read procfs task stat often cause performance degradation in the case that many process drivers are frequentyl set control. To reduce this overhead, now initial stats are retrieved by taskstats and 'pgid' which cannot be retrieved by taskstats is get lazily once at the first reading. Change-Id: I9bae9965a17192fbc0ab5789df45a782ab97ca9d Signed-off-by: Dongwoo Lee --- src/resource/resource-process.c | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/src/resource/resource-process.c b/src/resource/resource-process.c index 00fb14c..01ef3f9 100644 --- a/src/resource/resource-process.c +++ b/src/resource/resource-process.c @@ -199,6 +199,19 @@ static int process_get_context_data(const struct resource *res, strncpy((char *)data, ctx->comm, TS_COMM_LEN); break; case PROCESS_ATTR_PGID: + if (ctx->pgid < 0) { + char *stat_fields[PROCESS_STAT_FIELD_MAX]; + char buffer[BUFF_MAX]; + int ret; + + ret = kernel_get_process_stat_fields(ctx->tgid, buffer, + BUFF_MAX, stat_fields); + if (ret < 0) + break; + + ctx->pgid = atoi(stat_fields[PROCESS_STAT_FIELD_PGID]); + } + *((int *)data) = ctx->pgid; break; case PROCESS_ATTR_PPID: @@ -309,8 +322,6 @@ static int process_setup_tgid(const struct resource *res, const struct resource_control *ctrl, const void *data) { - char buf[BUFF_MAX]; - char *stat_fields[PROCESS_STAT_FIELD_MAX]; struct process_context *ctx; u_int64_t total_memory; int ret; @@ -328,20 +339,14 @@ static int process_setup_tgid(const struct resource *res, ctx->prev_total_time = get_total_cpu_time(); /* update initial status */ - ret = kernel_get_process_taskstats(&ctx->curr, ctx->tgid, false); - if (ret < 0) - return ret; - - ret = kernel_get_process_stat_fields(ctx->tgid, buf, BUFF_MAX, stat_fields); + ret = kernel_get_thread_group_taskstats(&ctx->curr, ctx->tgid, true); if (ret < 0) return ret; - /* save comm with removing parentheses */ - strncpy(ctx->comm, stat_fields[PROCESS_STAT_FIELD_COMM] + 1, - strlen(stat_fields[PROCESS_STAT_FIELD_COMM]) - 2 /* '(', ')' */); + memcpy(ctx->comm, ctx->curr.ac_comm, strlen(ctx->curr.ac_comm) + 1); - ctx->pgid = atoi(stat_fields[PROCESS_STAT_FIELD_PGID]); - ctx->ppid = atoi(stat_fields[PROCESS_STAT_FIELD_PPID]); + ctx->pgid = -1; + ctx->ppid = ctx->curr.ac_ppid; return 0; } -- 2.7.4