diff options
author | Kichan Kwon <k_c.kwon@samsung.com> | 2016-11-21 14:41:19 +0900 |
---|---|---|
committer | Kichan Kwon <k_c.kwon@samsung.com> | 2016-12-12 13:42:58 +0900 |
commit | 5efcbaa348752136df436df9be85adb4726cff17 (patch) | |
tree | 0e26870fc110b7646274a2810b8f107567a16c1c | |
parent | 73b906e4714db967fa6438260c3fa020e67d71a7 (diff) | |
download | resourced-5efcbaa348752136df436df9be85adb4726cff17.tar.gz resourced-5efcbaa348752136df436df9be85adb4726cff17.tar.bz2 resourced-5efcbaa348752136df436df9be85adb4726cff17.zip |
common : separate cgroup integer function with uint and int
- Some numbers like pid should be written to unsigned int
Change-Id: Id34e1395974afd7526e1d92bb2dcfef4a2fd4f84
Signed-off-by: Kichan Kwon <k_c.kwon@samsung.com>
-rw-r--r-- | src/common/cgroup.c | 102 | ||||
-rw-r--r-- | src/common/cgroup.h | 35 | ||||
-rw-r--r-- | src/cpu/cpu.c | 16 | ||||
-rw-r--r-- | src/memory/memcontrol.c | 2 | ||||
-rw-r--r-- | src/memory/vmpressure-lowmem-handler.c | 10 | ||||
-rw-r--r-- | src/swap/swap.c | 6 | ||||
-rw-r--r-- | src/timer-slack/timer-slack.c | 4 | ||||
-rw-r--r-- | src/vip-agent/vip-process.c | 2 |
8 files changed, 113 insertions, 64 deletions
diff --git a/src/common/cgroup.c b/src/common/cgroup.c index 7a466fb8..20702c74 100644 --- a/src/common/cgroup.c +++ b/src/common/cgroup.c @@ -68,8 +68,15 @@ static int cgroup_create(const char *cgroup_full_path) resourced_ret_c cgroup_write_pid_fullpath(const char *cgroup_full_path, const int pid) { + int ret; char buf[256]; - int ret = cgroup_write_node(cgroup_full_path, CGROUP_FILE_NAME, + + if (pid <=0) { + _E("try to write empty pid to %s", cgroup_full_path); + return RESOURCED_ERROR_NO_DATA; + } + + ret = cgroup_write_node_uint32(cgroup_full_path, CGROUP_FILE_NAME, (u_int32_t)pid); ret_value_msg_if(ret < 0, RESOURCED_ERROR_FAIL, @@ -126,8 +133,17 @@ resourced_ret_c cgroup_write_pidtree(const char *cgroup_subsystem, return RESOURCED_ERROR_NONE; } -int cgroup_write_node(const char *cgroup_name, - const char *file_name, unsigned int value) +int cgroup_write_node_uint32(const char *cgroup_name, + const char *file_name, uint32_t value) +{ + char buf[MAX_PATH_LENGTH]; + snprintf(buf, sizeof(buf), "%s/%s", cgroup_name, file_name); + _SD("cgroup_buf %s, value %d\n", buf, value); + return fwrite_uint(buf, value); +} + +int cgroup_write_node_int32(const char *cgroup_name, + const char *file_name, int32_t value) { char buf[MAX_PATH_LENGTH]; snprintf(buf, sizeof(buf), "%s/%s", cgroup_name, file_name); @@ -144,8 +160,8 @@ int cgroup_write_node_str(const char *cgroup_name, return fwrite_str(buf, string); } -int cgroup_read_node(const char *cgroup_name, - const char *file_name, unsigned int *value) +int cgroup_read_node_uint32(const char *cgroup_name, + const char *file_name, uint32_t *value) { char buf[MAX_PATH_LENGTH]; int ret; @@ -155,6 +171,17 @@ int cgroup_read_node(const char *cgroup_name, return ret; } +int cgroup_read_node_int32(const char *cgroup_name, + const char *file_name, int32_t *value) +{ + char buf[MAX_PATH_LENGTH]; + int ret; + snprintf(buf, sizeof(buf), "%s/%s", cgroup_name, file_name); + ret = fread_int(buf, value); + _SD("cgroup_buf %s, value %d\n", buf, *value); + return ret; +} + int cgroup_make_subdir(const char* parentdir, const char* cgroup_name, bool *already) { char buf[MAX_PATH_LENGTH]; @@ -274,39 +301,6 @@ int cgroup_pid_get_path(const char *controller, pid_t pid, char **path) return 0; } -int cgroup_get_pids(const char *name, GArray **pids) -{ - _cleanup_free_ char *cg_procs = NULL; - _cleanup_fclose_ FILE *f = NULL; - char buf[MAX_PATH_LENGTH] = ""; - GArray *array; - pid_t pid; - - assert(name); - assert(pids); - assert(!pids); - - if (asprintf(&cg_procs, "%s/cgroup.procs", name) < 0) - return -ENOMEM; - - f = fopen(cg_procs, "r"); - if (!f) { - _E("Failed to open '%s': %m", name); - return -errno; - } - - array = g_array_new(false, false, sizeof(pid_t)); - - while (fgets(buf, 32, f)) { - pid = atoi(buf); - g_array_append_val(array, pid); - } - - *pids = array; - - return 0; -} - static void cgroup_memory_stat_init(struct cgroup_memory_stat *mem_stat, long long val) { enum cgroup_memory_stat_id id; @@ -399,3 +393,35 @@ int cgroup_get_memory_stat(const char *name, struct cgroup_memory_stat **mem_sta * g_array_free(pids_array, TRUE); * */ +int cgroup_get_pids(const char *name, GArray **pids) +{ + _cleanup_free_ char *cg_procs = NULL; + _cleanup_fclose_ FILE *f = NULL; + char buf[MAX_PATH_LENGTH] = ""; + GArray *array; + pid_t pid; + + assert(name); + assert(pids); + assert(!*pids); + + if (asprintf(&cg_procs, "%s/cgroup.procs", name) < 0) + return -ENOMEM; + + f = fopen(cg_procs, "r"); + if (!f) { + _E("Failed to open '%s': %m", name); + return -errno; + } + + array = g_array_new(false, false, sizeof(pid_t)); + + while (fgets(buf, 32, f)) { + pid = atoi(buf); + g_array_append_val(array, pid); + } + + *pids = array; + + return 0; +} diff --git a/src/common/cgroup.h b/src/common/cgroup.h index 2cb67e4b..9a2f10eb 100644 --- a/src/common/cgroup.h +++ b/src/common/cgroup.h @@ -17,9 +17,13 @@ * */ +#include <assert.h> +#include <string.h> #include <resourced.h> #include <sys/types.h> +#include <stdint.h> #include <glib.h> +#include "macro.h" /* * Cgroup creation interface @@ -34,23 +38,42 @@ #define PROC_TASK_CHILDREN "/proc/%d/task/%d/children" /** - * @desc Get one unsigned int value from cgroup + * @desc Get one unsigned int32 value from cgroup * @param cgroup_name - cgroup path * @param file_name - cgroup content to write * @param value - out parameter, value to fill * @return negative value if error */ -int cgroup_read_node(const char *cgroup_name, - const char *file_name, unsigned int *value); +int cgroup_read_node_uint32(const char *cgroup_name, + const char *file_name, uint32_t *value); /** - * @desc Put value to cgroup, + * @desc Get one signed int32 value from cgroup + * @param cgroup_name - cgroup path + * @param file_name - cgroup content to write + * @param value - out parameter, value to fill + * @return negative value if error + */ +int cgroup_read_node_int32(const char *cgroup_name, + const char *file_name, int32_t *value); + +/** + * @desc Put unsigned int32 value to cgroup, + * @param cgroup_name - cgroup path + * @param file_name - cgroup content to write + * @param value - unsigned int32 data to write + * @return negative value if error + */ +int cgroup_write_node_uint32(const char *cgroup_name, const char *file_name, uint32_t value); + +/** + * @desc Put signed int32 value to cgroup, * @param cgroup_name - cgroup path * @param file_name - cgroup content to write - * @param value - data to write + * @param value - signed int32 data to write * @return negative value if error */ -int cgroup_write_node(const char *cgroup_name, const char *file_name, unsigned int value); +int cgroup_write_node_int32(const char *cgroup_name, const char *file_name, int32_t value); /** * @desc Put value to cgroup, diff --git a/src/cpu/cpu.c b/src/cpu/cpu.c index fb0cae16..98bf08cd 100644 --- a/src/cpu/cpu.c +++ b/src/cpu/cpu.c @@ -122,7 +122,7 @@ static int check_predefined(const pid_t pid) static int cpu_move_cgroup(pid_t pid, char *path) { - return cgroup_write_node(path, CGROUP_FILE_NAME, pid); + return cgroup_write_node_uint32(path, CGROUP_FILE_NAME, pid); } static int cpu_move_cgroup_foreach(pid_t pid, struct proc_app_info *pai, char *path) @@ -131,13 +131,13 @@ static int cpu_move_cgroup_foreach(pid_t pid, struct proc_app_info *pai, char *p pid_t child_pid; if (!pai) - return cgroup_write_node(path, CGROUP_FILE_NAME, pid); + return cgroup_write_node_uint32(path, CGROUP_FILE_NAME, pid); - cgroup_write_node(path, CGROUP_FILE_NAME, pai->main_pid); + cgroup_write_node_uint32(path, CGROUP_FILE_NAME, pai->main_pid); if (pai->childs) { gslist_for_each_item(iter, pai->childs) { child_pid = GPOINTER_TO_PID(iter->data); - cgroup_write_node(path, CGROUP_FILE_NAME, child_pid); + cgroup_write_node_uint32(path, CGROUP_FILE_NAME, child_pid); } } return RESOURCED_ERROR_NONE; @@ -164,7 +164,7 @@ static int get_relative_value(const char *cgroup_name, { unsigned int val; - if (cgroup_read_node(cgroup_name, file_name, &val) != RESOURCED_ERROR_NONE) { + if (cgroup_read_node_uint32(cgroup_name, file_name, &val) != RESOURCED_ERROR_NONE) { _E("Can't read %s%s. value is set to 1000", cgroup_name, file_name); val = 1000; } @@ -215,17 +215,17 @@ static int load_cpu_config(struct parse_result *result, void *user_data) } else if (!strncmp(result->name, "BACKGROUND_CPU_SHARE", strlen("BACKGROUND_CPU_SHARE")+1)) { value = atoi(result->value); if (value) - cgroup_write_node(CPU_BACKGROUND_GROUP, CPU_SHARE, + cgroup_write_node_uint32(CPU_BACKGROUND_GROUP, CPU_SHARE, get_relative_value(CPU_DEFAULT_CGROUP, CPU_SHARE, value)); } else if (!strncmp(result->name, "QUOTA_CPU_SHARE", strlen("QUOTA_CPU_SHARE")+1)) { value = atoi(result->value); if (value && cpu_quota_enabled()) - cgroup_write_node(CPU_CPUQUOTA_GROUP, CPU_SHARE, + cgroup_write_node_uint32(CPU_CPUQUOTA_GROUP, CPU_SHARE, get_relative_value(CPU_DEFAULT_CGROUP, CPU_SHARE, value)); } else if (!strncmp(result->name, "QUOTA_MAX_BANDWIDTH", strlen("QUOTA_MAX_BANDWIDTH")+1)) { value = atoi(result->value); if (value && cpu_quota_enabled()) - cgroup_write_node(CPU_CPUQUOTA_GROUP, CPU_CONTROL_BANDWIDTH, + cgroup_write_node_uint32(CPU_CPUQUOTA_GROUP, CPU_CONTROL_BANDWIDTH, get_relative_value(CPU_CPUQUOTA_GROUP, CPU_CONTROL_FULL_BANDWIDTH, value)); } diff --git a/src/memory/memcontrol.c b/src/memory/memcontrol.c index 50acf4f3..28d982c5 100644 --- a/src/memory/memcontrol.c +++ b/src/memory/memcontrol.c @@ -188,7 +188,7 @@ int memcg_get_anon_usage(struct memcg_info *mi, unsigned long long *anon_usage) int memcg_get_usage(struct memcg_info *mi, unsigned int *usage_bytes) { - return cgroup_read_node(mi->name, "memory.usage_in_bytes", usage_bytes); + return cgroup_read_node_uint32(mi->name, "memory.usage_in_bytes", usage_bytes); } /* diff --git a/src/memory/vmpressure-lowmem-handler.c b/src/memory/vmpressure-lowmem-handler.c index 504eb689..040d1ca8 100644 --- a/src/memory/vmpressure-lowmem-handler.c +++ b/src/memory/vmpressure-lowmem-handler.c @@ -1029,7 +1029,7 @@ void lowmem_trigger_swap(pid_t pid, int memcg_idx) mi = memcg_tree[memcg_idx]->info; _D("name : %s, pid : %d", mi->name, pid); - cgroup_write_node(mi->name, CGROUP_FILE_NAME, pid); + cgroup_write_node_uint32(mi->name, CGROUP_FILE_NAME, pid); msg.type = memcg_idx; msg.info = mi; resourced_notify(RESOURCED_NOTIFIER_SWAP_START, &msg); @@ -1525,7 +1525,7 @@ static int write_params_memcg_info(struct memcg_info *mi, int ret = RESOURCED_ERROR_NONE; _I("write memcg param for %s", name); /* enable cgroup move */ - ret = cgroup_write_node(name, + ret = cgroup_write_node_uint32(name, MEMCG_MOVE_CHARGE_PATH, 3); if (ret) return ret; @@ -1539,13 +1539,13 @@ static int write_params_memcg_info(struct memcg_info *mi, return ret; /* disable memcg OOM-killer */ - ret = cgroup_write_node(name, + ret = cgroup_write_node_uint32(name, MEMCG_OOM_CONTROL_PATH, 1); if (ret) return ret; /* write limit_in_bytes */ - ret = cgroup_write_node(name, + ret = cgroup_write_node_uint32(name, MEMCG_LIMIT_PATH, limit); _I("set %s's limit to %u", name, limit); return ret; @@ -1668,7 +1668,7 @@ static void lowmem_move_memcgroup(int pid, int oom_score_adj) _D("pid: %d, proc_name: %s, cg_name: %s, oom_score_adj: %d", pid, pai ? pai->appid : "---", memcg_name[memcg_idx], oom_score_adj); - cgroup_write_node(mi->name, CGROUP_FILE_NAME, pid); + cgroup_write_node_uint32(mi->name, CGROUP_FILE_NAME, pid); proc_set_process_memory_state(pai, memcg_idx, mi, oom_score_adj); /* diff --git a/src/swap/swap.c b/src/swap/swap.c index 947a9600..d03a1d49 100644 --- a/src/swap/swap.c +++ b/src/swap/swap.c @@ -386,7 +386,7 @@ static int swap_reclaim_memcg(struct swap_status_msg msg) memcg_limit = swap_calculate_hard_limit_in_bytes(usage); _D("Swap request: %s cgroup usage is %lu, hard limit set to %lu (hard limit fraction %f)", msg.info->name, usage, memcg_limit, swap_hard_limit_fraction); - ret = cgroup_write_node(msg.info->name, MEMCG_SIZE_LIMIT, memcg_limit); + ret = cgroup_write_node_uint32(msg.info->name, MEMCG_SIZE_LIMIT, memcg_limit); if (ret != RESOURCED_ERROR_NONE) _E("Not able to set hard limit of %s memory cgroup", msg.info->name); @@ -752,7 +752,7 @@ static int swap_cgroup_reset_limit(void *data) struct swap_status_msg *msg = data; limit = -1; - ret = cgroup_write_node(msg->info->name, MEMCG_SIZE_LIMIT, limit); + ret = cgroup_write_node_int32(msg->info->name, MEMCG_SIZE_LIMIT, limit); if (ret != RESOURCED_ERROR_NONE) _E("Failed to change hard limit of %s cgroup to -1", msg->info->name); else @@ -1015,7 +1015,7 @@ static void resourced_swap_change_memcg_settings(enum memcg_type type) if (ret != RESOURCED_ERROR_NONE) return; - cgroup_write_node(memcg_swap->info->name, MOVE_CHARGE, 1); + cgroup_write_node_uint32(memcg_swap->info->name, MOVE_CHARGE, 1); } static int resourced_swap_init(void *data) diff --git a/src/timer-slack/timer-slack.c b/src/timer-slack/timer-slack.c index 43518a10..07d8d07e 100644 --- a/src/timer-slack/timer-slack.c +++ b/src/timer-slack/timer-slack.c @@ -93,9 +93,9 @@ static int timer_slack_write(char *sub_cgroup, char *node, int val) int ret; if (sub_cgroup) { snprintf(path_buf, sizeof(path_buf), "%s/%s", TIMER_CGROUP_PATH, sub_cgroup); - ret = cgroup_write_node(path_buf, node, val); + ret = cgroup_write_node_uint32(path_buf, node, val); } else - ret = cgroup_write_node(TIMER_CGROUP_PATH, node, val); + ret = cgroup_write_node_uint32(TIMER_CGROUP_PATH, node, val); return ret; } diff --git a/src/vip-agent/vip-process.c b/src/vip-agent/vip-process.c index f300c46a..13b39fb8 100644 --- a/src/vip-agent/vip-process.c +++ b/src/vip-agent/vip-process.c @@ -116,7 +116,7 @@ static int vip_create_sub_cgroup(const char *name, pid_t pid) return -ENOMEM; } - r = cgroup_write_node(cgroup_name, TASK_FILE_NAME, pid); + r = cgroup_write_node_uint32(cgroup_name, TASK_FILE_NAME, pid); if (r < 0) { _E("failed to write pid '%d' to '%s': %s", pid, cgroup_name, strerror_r(-r, buf, sizeof(buf))); |