resource: process: Add PGID and PPID attributes 21/272421/2
authorDongwoo Lee <dwoo08.lee@samsung.com>
Fri, 11 Mar 2022 10:11:48 +0000 (19:11 +0900)
committerDongwoo Lee <dwoo08.lee@samsung.com>
Thu, 17 Mar 2022 03:39:51 +0000 (12:39 +0900)
Change-Id: I6516546b9e1603095070e02b299f8e904e1aa63f
Signed-off-by: Dongwoo Lee <dwoo08.lee@samsung.com>
lib/tmonitor/tmonitor.h
src/resource/resource-process.c

index 9252ba5..ae859c6 100644 (file)
@@ -117,6 +117,8 @@ extern "C" {
 #define PROCESS_ATTR_DISK_READ_BPS             BIT(4)  /* DATA_TYPE_UINT */
 #define PROCESS_ATTR_DISK_WRITE_BPS            BIT(5)  /* DATA_TYPE_UINT */
 #define PROCESS_ATTR_COMM                      BIT(6)  /* DATA_TYPE_STRING */
+#define PROCESS_ATTR_PGID                      BIT(7)  /* DATA_TYPE_INT */
+#define PROCESS_ATTR_PPID                      BIT(8)  /* DATA_TYPE_INT */
 
 #define PROCESS_CTRL_TGID                      BIT(0)
 
index 831f3b5..fbfbd6c 100644 (file)
@@ -30,6 +30,8 @@
 
 struct process_context {
        pid_t tgid;
+       pid_t ppid;
+       pid_t pgid;
        char comm[TS_COMM_LEN];
        struct taskstats prev;
        struct taskstats curr;
@@ -179,12 +181,11 @@ static int process_get_disk_bps(const struct resource *res,
        return 0;
 }
 
-static int process_get_comm(const struct resource *res,
+static int process_get_context_data(const struct resource *res,
                                const struct resource_attribute *attr,
                                void *data)
 {
        struct process_context *ctx;
-       char *buf = (char *)data;
 
        if (!res || !res->priv || !attr || !data)
                return -EINVAL;
@@ -196,7 +197,17 @@ static int process_get_comm(const struct resource *res,
                return -EINVAL;
        }
 
-       strncpy(buf, ctx->comm, TS_COMM_LEN);
+       switch (attr->id) {
+       case PROCESS_ATTR_COMM:
+               strncpy((char *)data, ctx->comm, TS_COMM_LEN);
+               break;
+       case PROCESS_ATTR_PGID:
+               *((int *)data) = ctx->pgid;
+               break;
+       case PROCESS_ATTR_PPID:
+               *((int *)data) = ctx->ppid;
+               break;
+       }
 
        return 0;
 }
@@ -249,7 +260,21 @@ static const struct resource_attribute process_attrs[] = {
                .id     = PROCESS_ATTR_COMM,
                .type   = DATA_TYPE_STRING,
                .ops    = {
-                       .get = process_get_comm,
+                       .get = process_get_context_data,
+               },
+       }, {
+               .name   = "PROCESS_ATTR_PGID",
+               .id     = PROCESS_ATTR_PGID,
+               .type   = DATA_TYPE_INT,
+               .ops    = {
+                       .get = process_get_context_data,
+               },
+       }, {
+               .name   = "PROCESS_ATTR_PPID",
+               .id     = PROCESS_ATTR_PPID,
+               .type   = DATA_TYPE_INT,
+               .ops    = {
+                       .get = process_get_context_data,
                },
        },
 };
@@ -433,7 +458,8 @@ static int process_setup_tgid(const struct resource *res,
                                const struct resource_control *ctrl,
                                const void *data)
 {
-       struct taskstats stats;
+       char buf[BUFF_MAX];
+       char *stat_fields[PROCESS_STAT_FIELD_MAX];
        struct process_context *ctx;
        u_int64_t total_memory;
        int ret;
@@ -453,13 +479,18 @@ static int process_setup_tgid(const struct resource *res,
        /* update initial status */
        ret = update_taskstats(ctx);
        if (ret < 0)
-               return -EINVAL;
+               return ret;
 
-       ret = query_taskstats(&stats, TASKSTATS_CMD_ATTR_PID, ctx->tgid);
+       ret = kernel_get_process_stat_fields(ctx->tgid, buf, BUFF_MAX, stat_fields);
        if (ret < 0)
-               return -EINVAL;
+               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, stats.ac_comm, TS_COMM_LEN);
+       ctx->pgid = atoi(stat_fields[PROCESS_STAT_FIELD_PGID]);
+       ctx->ppid = atoi(stat_fields[PROCESS_STAT_FIELD_PPID]);
 
        return 0;
 }