Add unit(in variable) & fix bugs
[platform/core/system/resourced.git] / src / common / cgroup / memory-cgroup.c
index 85c64ce..b894af4 100644 (file)
@@ -51,6 +51,8 @@
 
 static int default_swappiness = 0;
 
+static unsigned long long totalram_bytes = 0;
+
 /*
  * Special node that point's to /sys/fs/cgroup/memory - root of memcg group.
  * This is the same as memcg_tree[CGROUP_ROOT]->info.
@@ -67,6 +69,11 @@ static struct memcg_info gmemcg_info[CGROUP_END] = {
        {MEMCG_LOW_PATH,},
 };
 
+void register_totalram_bytes(unsigned long long ram_bytes)
+{
+       totalram_bytes = ram_bytes;
+}
+
 int set_mem_action_conf(struct mem_action *mem_action, const char *value)
 {
        char *ptr = strchr(value, ',');
@@ -88,19 +95,16 @@ int set_mem_action_conf(struct mem_action *mem_action, const char *value)
                *(ptr - 2) = '\0';
 
                if (temp == 'G') {
-                       mem_action->memory = GBYTE_TO_BYTE(atoi(value));
+                       mem_action->memory_bytes = GBYTE_TO_BYTE(atoi(value));
                }
                else if (temp == 'M') {
-                       mem_action->memory = MBYTE_TO_BYTE(atoi(value));
+                       mem_action->memory_bytes = MBYTE_TO_BYTE(atoi(value));
                }
                else if (temp == 'K') {
-                       mem_action->memory = KBYTE_TO_BYTE(atoi(value));
-               }
-               else if (temp == ' ') {
-                       mem_action->memory = atoi(value);
+                       mem_action->memory_bytes = KBYTE_TO_BYTE(atoi(value));
                }
                else {
-                       _E("Memory size unit should be GB or MB or KB or B");
+                       _E("Memory size unit should be GB or MB or KB");
                        return RESOURCED_ERROR_FAIL;
                }
 
@@ -144,12 +148,8 @@ int set_memcg_conf_threshold(bool percent, char size, int lvl, const char *value
                        memcg_conf->threshold[lvl].threshold =
                                KBYTE_TO_MBYTE(atoi(value));
                }
-               else if (size == ' ') {
-                       memcg_conf->threshold[lvl].threshold =
-                               BYTE_TO_MBYTE(atoi(value));
-               }
                else {
-                       _E("Memory size unit should be GB or MB or KB or B");
+                       _E("Memory size unit should be GB or MB or KB");
                        return RESOURCED_ERROR_FAIL;
                }
        }
@@ -180,31 +180,34 @@ void free_memcg_conf(void)
                free(memcg_conf);
 }
 
-static void set_limit_in_bytes(const char *dir, unsigned int limit)
+static void set_limit_in_bytes(const char *dir, unsigned long long limit_bytes)
 {
        int error;
-       unsigned int prev;
+       unsigned long long prev_bytes;
 
-       error = cgroup_read_node_uint32(dir, MEMCG_LIMIT_BYTE, &prev);
+       error = cgroup_read_node_ulonglong(dir, MEMCG_LIMIT_BYTE, &prev_bytes);
        if (error) {
                _E("[MEMORY-LIMIT] Failed to get %s from %s", MEMCG_LIMIT_BYTE, dir);
                return;
        }
 
-       if (limit == prev)
+       if (limit_bytes == prev_bytes)
                return;
 
-       if (prev > limit) {
-               cgroup_write_node_uint32(dir, MEMCG_LIMIT_BYTE, limit);
-               cgroup_write_node_uint32(dir, MEMCG_SWAP_LIMIT_BYTE, limit);
+       if (totalram_bytes > 0 && limit_bytes > totalram_bytes)
+               limit_bytes = totalram_bytes;
+
+       if (prev_bytes > limit_bytes) {
+               cgroup_write_node_ulonglong(dir, MEMCG_LIMIT_BYTE, limit_bytes);
+               cgroup_write_node_ulonglong(dir, MEMCG_SWAP_LIMIT_BYTE, limit_bytes);
        }
        else {
-               cgroup_write_node_uint32(dir, MEMCG_SWAP_LIMIT_BYTE, limit);
-               cgroup_write_node_uint32(dir, MEMCG_LIMIT_BYTE, limit);
+               cgroup_write_node_ulonglong(dir, MEMCG_SWAP_LIMIT_BYTE, limit_bytes);
+               cgroup_write_node_ulonglong(dir, MEMCG_LIMIT_BYTE, limit_bytes);
        }
 }
 
-int check_oom_and_set_limit(const char *dir, unsigned int limit)
+int check_oom_and_set_limit(const char *dir, unsigned long long limit_bytes)
 {
        int error;
        static unsigned int poo = -1;
@@ -213,7 +216,7 @@ int check_oom_and_set_limit(const char *dir, unsigned int limit)
                error = fread_uint("/proc/sys/vm/panic_on_oom", &poo);
                if (error) {
                        _E("[MEMORY-LIMIT] Failed to get %s from %s", "/proc/sys/vm/panic_on_oom", dir);
-                       return RESOURCED_ERROR_FAIL;
+                       poo = 0;
                }
        }
 
@@ -229,13 +232,13 @@ int check_oom_and_set_limit(const char *dir, unsigned int limit)
                }
        }
 
-       set_limit_in_bytes(dir, limit);
+       set_limit_in_bytes(dir, limit_bytes);
        return RESOURCED_ERROR_NONE;
 }
 
 static int memcg_write_limiter_info(struct memcg_info *mi)
 {
-       unsigned int limit = mi->limit;
+       unsigned long long limit_bytes = mi->limit_bytes;
        const char *name = mi->name;
        int ret = RESOURCED_ERROR_NONE;
        /* enable cgroup move */
@@ -247,8 +250,9 @@ static int memcg_write_limiter_info(struct memcg_info *mi)
        if (mi->limit_ratio == MEMCG_NO_LIMIT)
                return ret;
 
+       _I("[MEMORY-LIMIT] dir = %s, limit = %llu bytes", name, limit_bytes);
        /* write limit_in_bytes */
-       ret = check_oom_and_set_limit(name, limit);
+       ret = check_oom_and_set_limit(name, limit_bytes);
        return ret;
 }
 
@@ -282,16 +286,16 @@ static int memcg_write_optimizer_info(struct memcg_info *mi)
 int memcg_write_limiter_params(void)
 {
        unsigned int i;
-       unsigned int lower_group_limit = 0;
+       unsigned long long lower_group_limit_bytes = 0;
 
        for (i = CGROUP_LOW; i > CGROUP_ROOT; i--) {
                struct memcg_info *mi = get_memcg_info(i);
 
-               if (mi->limit < lower_group_limit)
-                       mi->limit = lower_group_limit;
+               if (mi->limit_bytes < lower_group_limit_bytes)
+                       mi->limit_bytes = lower_group_limit_bytes;
 
                memcg_write_limiter_info(mi);
-               lower_group_limit = mi->limit;
+               lower_group_limit_bytes = mi->limit_bytes;
        }
 
        return RESOURCED_ERROR_NONE;
@@ -315,7 +319,7 @@ void memcg_set_threshold(int type, int level, int value)
        if(!mi)
                _E("memory cgroup of %d is NULL", type);
        else
-               mi->threshold[level] = value;
+               mi->threshold_mb[level] = value;
 }
 
 void memcg_set_leave_threshold(int type, int value)
@@ -324,16 +328,16 @@ void memcg_set_leave_threshold(int type, int value)
        if(!mi)
                _E("memory cgroup of %d is NULL", type);
        else
-               mi->threshold_leave = value;
+               mi->threshold_leave_mb = value;
 }
 
 void memcg_info_set_limit(struct memcg_info *mi, float ratio,
-       unsigned int totalram)
+       unsigned long long totalram_bytes)
 {
        if (!mi)
                return;
 
-       mi->limit = (float)totalram * ratio;
+       mi->limit_bytes = (double)totalram_bytes * ratio;
        mi->limit_ratio = ratio;
 }
 
@@ -413,7 +417,7 @@ int memcg_get_memory_stat(const char *name, struct cgroup_memory_stat **mem_stat
        return 0;
 }
 
-int memcg_get_anon_usage(char *memcg, unsigned int *anon_usage)
+int memcg_get_anon_usage(char *memcg, unsigned long long *anon_usage_bytes)
 {
        int r;
        _cleanup_free_ struct cgroup_memory_stat *mem_stat = NULL;
@@ -424,12 +428,12 @@ int memcg_get_anon_usage(char *memcg, unsigned int *anon_usage)
                return r;
        }
 
-       *anon_usage = mem_stat->value[CGROUP_MEMORY_STAT_INACTIVE_ANON] +
+       *anon_usage_bytes = mem_stat->value[CGROUP_MEMORY_STAT_INACTIVE_ANON] +
                                mem_stat->value[CGROUP_MEMORY_STAT_ACTIVE_ANON];
        return 0;
 }
 
-int memcg_get_swap_usage(char *memcg, unsigned int *usage)
+int memcg_get_swap_usage(char *memcg, unsigned long long *usage_bytes)
 {
        int r;
        _cleanup_free_ struct cgroup_memory_stat *mem_stat = NULL;
@@ -440,7 +444,7 @@ int memcg_get_swap_usage(char *memcg, unsigned int *usage)
                return r;
        }
 
-       *usage = mem_stat->value[CGROUP_MEMORY_STAT_SWAP];
+       *usage_bytes = mem_stat->value[CGROUP_MEMORY_STAT_SWAP];
        return 0;
 }