util: kernel: Use 64-bit unsigned integer for memory information 65/271765/2
authorDongwoo Lee <dwoo08.lee@samsung.com>
Mon, 28 Feb 2022 05:29:14 +0000 (14:29 +0900)
committerDongwoo Lee <dwoo08.lee@samsung.com>
Mon, 28 Feb 2022 10:02:07 +0000 (19:02 +0900)
Since memory information node of kernel can be presented with 32-bit
signed variable as maximum 2GB, to overcome this limitation, this
changes related functions using 64-bit unsigned integer variable.

Change-Id: Id1b1da468ca5e7c98cd65318c854cd5a98ae2086
Signed-off-by: Dongwoo Lee <dwoo08.lee@samsung.com>
include/util/kernel.h
src/resource/resource-memory.c
src/resource/resource-process.c
src/util/kernel.c

index 4596ff6..f268e93 100644 (file)
@@ -41,6 +41,6 @@ int kernel_get_total_cpu_stat(struct cpu_stat *total);
 int kernel_get_per_cpu_stat(struct cpu_stat *cpus, int num_possible_cpus,
                            int *num_online_cpus);
 
-int kernel_get_memory_info(const char *key);
-int kernel_get_memory_total(void);
+int kernel_get_memory_info(const char *key, u_int64_t *val);
+int kernel_get_memory_total(u_int64_t *val);
 #endif
index 63b8c08..f376feb 100644 (file)
 #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)
+static inline int memory_read_val_from_proc_node(uint32_t val_id, u_int64_t *val)
 {
+       int ret;
+
        switch (val_id) {
        case MEMORY_TOTAL:
-               return kernel_get_memory_total();
+               ret = kernel_get_memory_total(val);
+               break;
        case MEMORY_AVAILABLE:
-               return kernel_get_memory_info(PROC_MEM_INFO_MEM_AVAILABLE);
+               ret = kernel_get_memory_info(PROC_MEM_INFO_MEM_AVAILABLE, val);
+               break;
        case MEMORY_FREE:
-               return kernel_get_memory_info(PROC_MEM_INFO_MEM_FREE);
+               ret = kernel_get_memory_info(PROC_MEM_INFO_MEM_FREE, val);
+               break;
+       default:
+               _E("wrong memory resource attribute\n");
+               ret = -EINVAL;
        }
-       return -EINVAL;
+
+       return ret;
 }
 
 static int memory_get_total_memory(const struct resource *res,
                                const struct resource_attribute *attr,
                                void **data)
 {
-       int val;
+       u_int64_t val;
+       int ret;
 
        if (!res || !attr || !data)
                return -EINVAL;
 
-       val = memory_read_val_from_proc_node(attr->id);
-       if (val < 0)
+       ret = memory_read_val_from_proc_node(attr->id, &val);
+       if (ret < 0)
                return -EINVAL;
 
        *data = (void *)(intptr_t)val;
@@ -71,13 +81,14 @@ static int memory_get_available_memory(const struct resource *res,
                                const struct resource_attribute *attr,
                                void **data)
 {
-       int val;
+       u_int64_t val;
+       int ret;
 
        if (!res || !attr || !data)
                return -EINVAL;
 
-       val = memory_read_val_from_proc_node(attr->id);
-       if (val < 0)
+       ret = memory_read_val_from_proc_node(attr->id, &val);
+       if (ret < 0)
                return -EINVAL;
 
        *data = (void *)(intptr_t)val;
@@ -89,13 +100,14 @@ static int memory_get_free_memory(const struct resource *res,
                                const struct resource_attribute *attr,
                                void **data)
 {
-       int val;
+       u_int64_t val;
+       int ret;
 
        if (!res || !attr || !data)
                return -EINVAL;
 
-       val = memory_read_val_from_proc_node(attr->id);
-       if (val < 0)
+       ret = memory_read_val_from_proc_node(attr->id, &val);
+       if (ret < 0)
                return -EINVAL;
 
        *data = (void *)(intptr_t)val;
@@ -107,21 +119,21 @@ static const struct resource_attribute memory_attrs[] = {
        {
                .name   = "MEMORY_TOTAL",
                .id     = MEMORY_TOTAL,
-               .type   = DATA_TYPE_INT,
+               .type   = DATA_TYPE_UINT64,
                .ops    = {
                        .get = memory_get_total_memory,
                },
        }, {
                .name   = "MEMORY_AVAILABLE",
                .id     = MEMORY_AVAILABLE,
-               .type   = DATA_TYPE_INT,
+               .type   = DATA_TYPE_UINT64,
                .ops    = {
                        .get = memory_get_available_memory,
                },
        }, {
                .name   = "MEMORY_FREE",
                .id     = MEMORY_FREE,
-               .type   = DATA_TYPE_INT,
+               .type   = DATA_TYPE_UINT64,
                .ops    = {
                        .get = memory_get_free_memory,
                }
index 2eb3cf9..4defc09 100644 (file)
@@ -467,6 +467,12 @@ static int process_init(struct resource *res)
        ctx->prev_total_time = get_total_cpu_time();
        ctx->pid = (pid_t)(intptr_t)res->user_data;
 
+       ret = kernel_get_memory_total(&ctx->total_memory);
+       if (ret < 0) {
+               free(ctx);
+               return -EINVAL;
+       }
+
        /* update initial status */
        ret = update_taskstats(ctx);
        if (ret < 0) {
@@ -481,8 +487,6 @@ static int process_init(struct resource *res)
        }
        memcpy(ctx->comm, stats.ac_comm, TS_COMM_LEN);
 
-       ctx->total_memory = kernel_get_memory_total() * 1024ULL;
-
        res->priv = ctx;
 
        return 0;
index a23c40e..a68e05b 100644 (file)
@@ -18,6 +18,7 @@
 
 #include <glib.h>
 #include <stdio.h>
+#include <string.h>
 
 #include <util/common.h>
 #include <util/log.h>
@@ -170,11 +171,9 @@ err:
        return ret;
 }
 
-int kernel_get_memory_info(const char *key)
+int kernel_get_memory_info(const char *key, u_int64_t *val)
 {
-       int ret = -EINVAL;
        char buf[BUFF_MAX];
-       char str[BUFF_MAX];
        FILE *fp = NULL;
 
        fp = fopen("/proc/meminfo", "r");
@@ -183,21 +182,31 @@ int kernel_get_memory_info(const char *key)
 
        while (fgets(buf, BUFF_MAX, fp)) {
                if (!strncmp(buf, key, strlen(key))) {
-                       sscanf(buf, "%s %d", str, &ret);
+                       sscanf(buf, "%*s %"PRIu64, val);
+                       if (strstr(buf, "kB"))
+                               *val *= 1024;
                        break;
                }
        }
        fclose(fp);
 
-       return ret;
+       return 0;
 }
 
-int kernel_get_memory_total(void)
+int kernel_get_memory_total(u_int64_t *val)
 {
        static u_int64_t mem_total = 0;
+       int ret;
 
-       if (!mem_total)
-               mem_total = kernel_get_memory_info("MemTotal");
+       if (!mem_total) {
+               ret = kernel_get_memory_info("MemTotal", &mem_total);
+               if (ret < 0) {
+                       _E("failed to get system total memory\n");
+                       return -EINVAL;
+               }
+       }
+
+       *val = mem_total;
 
-       return mem_total;
+       return 0;
 }