From 42a071fe0797df815dc22f0218398b92534c4591 Mon Sep 17 00:00:00 2001 From: Kichan Kwon Date: Wed, 12 Jul 2017 09:35:23 +0900 Subject: [PATCH] Fix the size of KiB - KiB = KB = 2^10 bytes - kB = 10^2 bytes - However, kernel consider kB as 2^10 bytes Change-Id: I2291a1daa097d1838ac2955316a19b17259d6c2f Signed-off-by: Kichan Kwon --- src/runtime_info_usage.c | 44 ++++++++++++++++++++++++++++++++------------ 1 file changed, 32 insertions(+), 12 deletions(-) diff --git a/src/runtime_info_usage.c b/src/runtime_info_usage.c index ee172bd..dba663d 100644 --- a/src/runtime_info_usage.c +++ b/src/runtime_info_usage.c @@ -53,8 +53,10 @@ static const runtime_info_dbus_info_s dbus_info[] = { { "GetCpuList", "all apps cpu" }, }; -#define kBtoKiB(val) ((val) <= 0) ? 0 : (int)MIN((((long long)(val) << 10) / 1000), INT_MAX) -#define pagetoKiB(val) ((val) <= 0) ? 0 : (int)MIN((((long long)(val) << 12) / 1000), INT_MAX) +#define ULONGtoINT(ulong) (int)(MIN((ulong), INT_MAX)) + +/* ((val << 12) >> 10) = (val << 2) */ +#define pagetoKiB(val) ((val) <= 0) ? 0 : (int)MIN(((long long)(val) << 2), INT_MAX) /* Convert int array to GVariant("ai") */ static GVariant *runtime_info_append_args(int *args, int size) @@ -255,7 +257,13 @@ API int runtime_info_get_system_memory_info(runtime_memory_info_s *info) { FILE *fp; char buf[256]; - unsigned long swap_total, swap_free, value, mem_available; + unsigned long value; + unsigned long mem_total = 0; + unsigned long mem_free = 0; + unsigned long cached = 0; + unsigned long mem_available = 0; + unsigned long swap_total = 0; + unsigned long swap_free = 0; if (info == NULL) { _E("INVALID_PARAMETER(0x%08x) : invalid output param", @@ -272,15 +280,16 @@ API int runtime_info_get_system_memory_info(runtime_memory_info_s *info) return RUNTIME_INFO_ERROR_IO_ERROR; } + info->total = info->free = info->cache = 0; while (fgets(buf, sizeof(buf), fp) != NULL) { - if (sscanf(buf, "MemTotal: %lu", &value) == 1) - info->total = kBtoKiB(value); - else if (sscanf(buf, "MemFree: %lu", &value) == 1) - info->free = kBtoKiB(value); - else if (sscanf(buf, "Cached: %lu", &value) == 1) - info->cache = kBtoKiB(value); + if (sscanf(buf, "MemTotal: %lu", &mem_total) == 1) + info->total = ULONGtoINT(mem_total); + else if (sscanf(buf, "MemFree: %lu", &mem_free) == 1) + info->free = ULONGtoINT(mem_free); + else if (sscanf(buf, "Cached: %lu", &cached) == 1) + info->cache = ULONGtoINT(cached); else if (sscanf(buf, "MemAvailable: %lu", &value) == 1) - mem_available = kBtoKiB(value); + mem_available = value; else if (sscanf(buf, "SwapTotal: %lu", &value) == 1) swap_total = value; else if (sscanf(buf, "SwapFree: %lu", &value) == 1) @@ -288,8 +297,19 @@ API int runtime_info_get_system_memory_info(runtime_memory_info_s *info) } fclose(fp); - info->used = mem_available ? (info->total - mem_available) : (info->total - info->free - info->cache); - info->swap = kBtoKiB(((swap_total > swap_free) ? (int)(swap_total - swap_free) : 0)); + if (mem_available > 0) { + if (mem_total > mem_available) + info->used = ULONGtoINT(mem_total - mem_available); + else + info->used = 0; + } else { + if (mem_total > mem_free && mem_total - mem_free > cached) + info->used = ULONGtoINT(mem_total - mem_free - cached); + else + info->used = 0; + } + + info->swap = (swap_total > swap_free) ? ULONGtoINT(swap_total - swap_free) : 0; return RUNTIME_INFO_ERROR_NONE; } -- 2.7.4