From 132eb157bd7c6552c6cd2283014666740d37c8a5 Mon Sep 17 00:00:00 2001 From: ByungSoo Kim Date: Tue, 10 Jan 2017 16:57:48 +0900 Subject: [PATCH] proc: add new interface for getting ram size There are many requirements to get total ram size in such as smart manager or gear manager. Kernel and bootloader have already known ram size information and they wrote this to /proc/cmdline. 'mem=nn[KMG] [KNL,BOOT] Force usage of a specific amount of memory' Since Resourced can parse this information, it can avoid using the hard coded value in other modules. Change-Id: Ib1aca7c23414391b453b9e55c6cd3b393499f49b --- src/common/procfs.c | 39 +++++++++++++++++++++++++++++++++++++++ src/common/procfs.h | 28 ++++++++++++++++++++++++++++ src/memory/compaction.c | 10 ---------- src/proc-stat/proc-monitor.c | 17 +++++++++++++++++ 4 files changed, 84 insertions(+), 10 deletions(-) diff --git a/src/common/procfs.c b/src/common/procfs.c index 3e91146..c7916d7 100644 --- a/src/common/procfs.c +++ b/src/common/procfs.c @@ -689,6 +689,45 @@ int proc_get_meminfo(struct meminfo *mi, enum meminfo_mask mask) return RESOURCED_ERROR_NONE; } +static int parse_spanned_pages(const char *s, regmatch_t *match, + unsigned int parse_tag, void *data) +{ + char *e; + unsigned long v; + unsigned long *parse_v = (unsigned long *)data; + + v = strtoul(s + match[1].rm_so, &e, 0); + *parse_v += v; + + return 0; +} + +int proc_get_ram_total(unsigned int *total) +{ + unsigned long total_spanned = 0; + static unsigned int total_ram = 0; + int ret; + const struct parse_arg args[] = { + PARSE_TAG("spanned[[:blank:]]+([0-9]+)\n", + parse_spanned_pages, SPANNED), + PARSE_TAG_EMPTY(), + }; + + if (total_ram > 0) { + *total = total_ram; + return RESOURCED_ERROR_NONE; + } + + ret = proc_parse_zoneinfo(args, &total_spanned); + if (ret != RESOURCED_ERROR_NONE) + return RESOURCED_ERROR_NO_DATA; + + total_ram = (unsigned int)BYTE_TO_KBYTE(total_spanned << PAGE_SHIFT); + *total = total_ram; + + return RESOURCED_ERROR_NONE; +} + static inline char *proc_skip_blanks(const char *s) { while (s && (isblank(*s) || !isalnum(*s))) diff --git a/src/common/procfs.h b/src/common/procfs.h index e53be68..5f72ac1 100644 --- a/src/common/procfs.h +++ b/src/common/procfs.h @@ -214,6 +214,27 @@ struct parse_arg { unsigned int tag; }; +#define PARSE_TAG(exp, fn, id) \ +{ \ + .re_exp = exp, \ + .callback = fn, \ + .tag = PARSE_TAG_##id, \ +} + +#define PARSE_TAG_EMPTY() {0,} + +enum { + PARSE_TAG_ZONE = 1, + PARSE_TAG_PAGE_COUNT, + PARSE_TAG_WM_MIN, + PARSE_TAG_WM_LOW, + PARSE_TAG_WM_HIGH, + PARSE_TAG_MANAGED, + PARSE_TAG_SPANNED, + PARSE_TAG_MAX, +}; + + /** * @desc get command line from /proc/{pid}/cmdline * @return negative value if error @@ -350,6 +371,13 @@ int proc_sys_node_trigger(enum sys_node_id sys_node_id); int proc_get_uptime(unsigned long *uptime); /** + * @desc get total ram size including reserved memory + * @param[out] total Ram size in kilobytes + * @return negative value if error + */ +int proc_get_ram_total(unsigned int *total); + +/** * @brief print uss and memtotal for showing brief memory information */ void proc_print_meninfo(FILE *fp); diff --git a/src/memory/compaction.c b/src/memory/compaction.c index f42f53e..9749d02 100644 --- a/src/memory/compaction.c +++ b/src/memory/compaction.c @@ -74,16 +74,6 @@ #define PROC_COMPACT_ENTRY "/proc/sys/vm/compact_memory" #define MEM_CONF_FILE RD_CONFIG_FILE(memory) -enum { - PARSE_TAG_ZONE = 1, - PARSE_TAG_PAGE_COUNT, - PARSE_TAG_WM_MIN, - PARSE_TAG_WM_LOW, - PARSE_TAG_WM_HIGH, - PARSE_TAG_MANAGED, - PARSE_TAG_MAX, -}; - #define COMPACT_CONFIG_SECTION "Compaction" #define COMPACT_CONFIG_ENABLE "CompactEnable" #define COMPACT_CONFIG_FRAG "Fraglevel" diff --git a/src/proc-stat/proc-monitor.c b/src/proc-stat/proc-monitor.c index ba0149b..b6d6dab 100644 --- a/src/proc-stat/proc-monitor.c +++ b/src/proc-stat/proc-monitor.c @@ -53,6 +53,7 @@ #define TIZEN_DEBUG_MODE_FILE RD_SYS_ETC"/.debugmode" #define DEFAULT_APPID "NULL" +#define DEFAULT_RAMSIZE 512 #define INIT_PID 1 #define POWER_OFF_DIRECT 2 @@ -504,6 +505,18 @@ failure: D_BUS_REPLY_NULL(invocation); } +static void dbus_get_get_ram_size(GDBusMethodInvocation *invocation, GVariant *params) +{ + unsigned int total = MBYTE_TO_KBYTE(DEFAULT_RAMSIZE); + int ret; + + ret = proc_get_ram_total(&total); + if (ret) + _E("can't get ram total size. use default size"); + + g_dbus_method_invocation_return_value(invocation, g_variant_new("(u)", total)); +} + static void proc_dbus_exclude_signal_handler(GVariant *params) { const char *str = NULL; @@ -1062,6 +1075,9 @@ static const char dbus_methods_xml[] = " " " " " " +" " +" " +" " " " ""; @@ -1080,6 +1096,7 @@ static struct d_bus_method dbus_methods[] = { { "ProcMemoryUsage", dbus_proc_memory_usage }, { "ProcCpuUsage", dbus_proc_cpu_usage }, { "CheckAppStatus", dbus_get_checkappstatus }, + { "GetRamSize", dbus_get_get_ram_size }, /* Add methods here */ }; -- 2.7.4