resource: process: Consolidate memory attribute functions 50/276450/3
authorDongwoo Lee <dwoo08.lee@samsung.com>
Thu, 16 Jun 2022 02:21:41 +0000 (11:21 +0900)
committerDongwoo Lee <dwoo08.lee@samsung.com>
Tue, 28 Jun 2022 10:56:29 +0000 (19:56 +0900)
To reduce the redundant code and make easy to add the new memory
attribute, this make use consolidate functions for memory attributes.

Change-Id: I551b8cf9dac43ae972ad9007c1c2791ed985367f
Signed-off-by: Dongwoo Lee <dwoo08.lee@samsung.com>
src/resource/resource-process.c

index 01ef3f9..052f43d 100644 (file)
@@ -72,13 +72,13 @@ static int process_get_cpu_util(const struct resource *res,
        return 0;
 }
 
-static int process_get_mem_virt(const struct resource *res,
-                               const struct resource_attribute *attr,
-                               void *data)
+static int process_get_mem_attrs(const struct resource *res,
+                                const struct resource_attribute *attr,
+                                void *data)
 {
        struct process_context *ctx;
        struct taskstats *curr;
-       u_int64_t *virt = (u_int64_t *)data;
+       u_int64_t *mem = (u_int64_t *)data;
 
        if (!res || !res->priv || !attr || !data)
                return -EINVAL;
@@ -92,46 +92,19 @@ static int process_get_mem_virt(const struct resource *res,
 
        curr = &ctx->curr;
 
-       *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)
-{
-       struct process_context *ctx;
-       struct taskstats *curr;
-       u_int64_t _rss;
-
-       if (!res || !res->priv || !attr || !data)
-               return -EINVAL;
-
-       ctx = res->priv;
-
-       if (!ctx->tgid) {
-               _E("resource %s is not yet initialized\n", res->name);
-               return -EINVAL;
-       }
-
-       curr = &ctx->curr;
-
-       _rss = (curr->coremem * 1024 * 1024) / curr->ac_stime;
-
        switch (attr->id) {
+       case PROCESS_ATTR_MEM_VIRT:
+               *mem = (curr->virtmem * 1024 * 1024) / curr->ac_stime;
+               break;
        case PROCESS_ATTR_MEM_RSS:
-       {
-               u_int64_t *rss = (u_int64_t *)data;
-
-               *rss = _rss;
+               *mem = (curr->coremem * 1024 * 1024) / curr->ac_stime;
                break;
-       }
        case PROCESS_ATTR_MEM_RSS_PERCENT:
        {
                double *percent = (double *)data;
 
-               *percent = ((double)_rss / (double)ctx->total_memory) * 100.0;
+               *percent = (curr->coremem * 1024 * 1024) / curr->ac_stime;
+               *percent /= ctx->total_memory * 100.0;
                break;
        }
        default:
@@ -236,7 +209,7 @@ static const struct resource_attribute process_attrs[] = {
                .id     = PROCESS_ATTR_MEM_VIRT,
                .type   = DATA_TYPE_UINT64,
                .ops    = {
-                       .get = process_get_mem_virt,
+                       .get = process_get_mem_attrs,
                        .is_supported = resource_attr_supported_always,
                },
        }, {
@@ -244,7 +217,7 @@ static const struct resource_attribute process_attrs[] = {
                .id     = PROCESS_ATTR_MEM_RSS,
                .type   = DATA_TYPE_UINT64,
                .ops    = {
-                       .get = process_get_mem_rss,
+                       .get = process_get_mem_attrs,
                        .is_supported = resource_attr_supported_always,
                },
        }, {
@@ -252,7 +225,7 @@ static const struct resource_attribute process_attrs[] = {
                .id     = PROCESS_ATTR_MEM_RSS_PERCENT,
                .type   = DATA_TYPE_DOUBLE,
                .ops    = {
-                       .get = process_get_mem_rss,
+                       .get = process_get_mem_attrs,
                        .is_supported = resource_attr_supported_always,
                },
        }, {