0f3d5aae790b2f67f98b581835783eee2f0cb0b4
[platform/core/system/resourced.git] / src / common / cgroup / memory-cgroup.h
1 /*
2  * resourced
3  *
4  * Copyright (c) 2014 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 /**
21  * @file memory-cgroup.h
22  * @desc header file for handling memory cgroups
23  **/
24
25 #ifndef __MEMORY_CGROUP_H__
26 #define __MEMORY_CGROUP_H__
27
28 #include <glib.h>
29 #include <stdbool.h>
30 #include "cgroup.h"
31 #include "const.h"
32
33 #ifdef __cplusplus
34 extern "C" {
35 #endif /* __cplusplus */
36
37 /* number of memory cgroups */
38 #define MEMCG_DEFAULT_EVENT_LEVEL       "low"
39
40 #define MEMCG_LOW_RATIO                 0.8
41 #define MEMCG_MEDIUM_RATIO              0.96
42 #define MEMCG_FOREGROUND_LEAVE_RATIO    0.25
43
44 #define MEMCG_NAME                                              "memory"
45 #define MEMCG_PATH                      CGROUP_PATH "/" MEMCG_NAME
46 #define MEMCG_VIP_PATH                  MEMCG_PATH "/" CGROUP_VIP_NAME
47 #define MEMCG_HIGH_PATH                 MEMCG_PATH "/" CGROUP_VIP_NAME "/" CGROUP_HIGH_NAME
48 #define MEMCG_MEDIUM_PATH               MEMCG_PATH "/" CGROUP_VIP_NAME "/" CGROUP_HIGH_NAME "/" CGROUP_MEDIUM_NAME
49 #define MEMCG_LOW_PATH                  MEMCG_PATH "/" CGROUP_VIP_NAME "/" CGROUP_HIGH_NAME "/" CGROUP_MEDIUM_NAME "/" CGROUP_LOW_NAME
50
51 #define MEMCG_VIP_PP_PATH               MEMCG_VIP_PATH "/" CGROUP_PER_PROCESS_NAME
52 #define MEMCG_VIP_GROUP_PATH            MEMCG_VIP_PATH "/" CGROUP_GROUP_NAME
53
54 #define MEMCG_HIGH_PP_PATH              MEMCG_HIGH_PATH "/" CGROUP_PER_PROCESS_NAME
55 #define MEMCG_HIGH_GROUP_PATH           MEMCG_HIGH_PATH "/" CGROUP_GROUP_NAME
56
57 #define MEMCG_MEDIUM_PP_PATH            MEMCG_MEDIUM_PATH "/" CGROUP_PER_PROCESS_NAME
58 #define MEMCG_MEDIUM_GROUP_PATH         MEMCG_MEDIUM_PATH "/" CGROUP_GROUP_NAME
59
60 #define MEMCG_LOW_PP_PATH               MEMCG_LOW_PATH "/" CGROUP_PER_PROCESS_NAME
61 #define MEMCG_LOW_GROUP_PATH            MEMCG_LOW_PATH "/" CGROUP_GROUP_NAME
62
63 #define MEMCG_OOM_CONTROL               "memory.oom_control"
64 #define MEMCG_EVENTFD_CONTROL           "cgroup.event_control"
65 #define MEMCG_EVENTFD_MEMORY_PRESSURE   "memory.pressure_level"
66 #define MEMCG_USAGE                     "memory.usage_in_bytes"
67 #define MEMCG_SWAP_USAGE                "memory.memsw.usage_in_bytes"
68 #define MEMCG_LIMIT_BYTE                "memory.limit_in_bytes"
69 #define MEMCG_SWAP_LIMIT_BYTE           "memory.memsw.limit_in_bytes"
70 #define MEMCG_SWAPPINESS                "memory.swappiness"
71 #define MEMCG_FORCE_RECLAIM             "memory.force_reclaim"
72 #define MEMCG_MOVE_CHARGE               "memory.move_charge_at_immigrate"
73
74 #define DEFAULT_MEMLOG_PATH             "/var/log"
75 #define DEFAULT_MEMLOG_NR_MAX           50
76
77 enum {
78         MEM_LEVEL_HIGH,
79         MEM_LEVEL_MEDIUM,
80         MEM_LEVEL_LOW,
81         MEM_LEVEL_CRITICAL,
82         MEM_LEVEL_OOM,
83         MEM_LEVEL_MAX,
84 };
85
86 enum lowmem_control_type {
87         LOWMEM_MOVE_CGROUP,
88         LOWMEM_MANAGE_FOREGROUND,
89 };
90
91 enum mem_log {
92         MEMLOG_MEMPS,
93         MEMLOG_MEMPS_MEMLIMIT,
94         MEMLOG_MAX,
95 };
96
97 enum cgroup_memory_stat_id {
98         CGROUP_MEMORY_STAT_CACHE = 0,
99         CGROUP_MEMORY_STAT_RSS,
100         CGROUP_MEMORY_STAT_RSS_HUGE,
101         CGROUP_MEMORY_STAT_MAPPED_FILE,
102         CGROUP_MEMORY_STAT_DIRTY,
103         CGROUP_MEMORY_STAT_WRITEBACK,
104         CGROUP_MEMORY_STAT_SWAP,
105         CGROUP_MEMORY_STAT_PGPGIN,
106         CGROUP_MEMORY_STAT_PGPGOUT,
107         CGROUP_MEMORY_STAT_PGFAULT,
108         CGROUP_MEMORY_STAT_PGMAJFAULT,
109         CGROUP_MEMORY_STAT_INACTIVE_ANON,
110         CGROUP_MEMORY_STAT_ACTIVE_ANON,
111         CGROUP_MEMORY_STAT_INACTIVE_FILE,
112         CGROUP_MEMORY_STAT_ACTIVE_FILE,
113         CGROUP_MEMORY_STAT_UNEVICTABLE,
114         CGROUP_MEMORY_STAT_HIERARCHICAL_MEMORY_LIMIT,
115         CGROUP_MEMORY_STAT_HIERARCHICAL_MEMSW_LIMIT,
116         CGROUP_MEMORY_STAT_TOTAL_CACHE,
117         CGROUP_MEMORY_STAT_TOTAL_RSS,
118         CGROUP_MEMORY_STAT_TOTAL_RSS_HUGE,
119         CGROUP_MEMORY_STAT_TOTAL_MAPPED_FILE,
120         CGROUP_MEMORY_STAT_TOTAL_DIRTY,
121         CGROUP_MEMORY_STAT_TOTAL_WRITEBACK,
122         CGROUP_MEMORY_STAT_TOTAL_SWAP,
123         CGROUP_MEMORY_STAT_TOTAL_PGPGIN,
124         CGROUP_MEMORY_STAT_TOTAL_PGPGOUT,
125         CGROUP_MEMORY_STAT_TOTAL_PGFAULT,
126         CGROUP_MEMORY_STAT_TOTAL_PGMAJFAULT,
127         CGROUP_MEMORY_STAT_TOTAL_INACTIVE_ANON,
128         CGROUP_MEMORY_STAT_TOTAL_ACTIVE_ANON,
129         CGROUP_MEMORY_STAT_TOTAL_INACTIVE_FILE,
130         CGROUP_MEMORY_STAT_TOTAL_ACTIVE_FILE,
131         CGROUP_MEMORY_STAT_TOTAL_UNEVICTABLE,
132         CGROUP_MEMORY_STAT_RECENT_ROTATED_ANON,
133         CGROUP_MEMORY_STAT_RECENT_ROTATED_FILE,
134         CGROUP_MEMORY_STAT_RECENT_SCANNED_ANON,
135         CGROUP_MEMORY_STAT_RECENT_SCANNED_FILE,
136         CGROUP_MEMORY_STAT_MAX,
137         CGROUP_MEMORY_STAT_INVALID = -1,
138 };
139
140 struct mem_threshold {
141         bool percent;
142         int threshold;                  /* MB or % */
143 };
144
145 struct mem_action {
146         unsigned int memory;    /* Byte */
147         int action;
148 };
149
150 struct memcg_conf {
151         struct mem_threshold threshold[MEM_LEVEL_MAX];
152         struct mem_action service;
153         struct mem_action widget;
154         struct mem_action guiapp;
155         struct mem_action background;
156         float cgroup_limit[CGROUP_END]; /* % */
157         bool oom_popup;
158 };
159
160 struct memcg_info {
161         /* name of memory cgroup */
162         char name[MAX_PATH_LENGTH];
163         /* hashname of memory cgroup for restoring memcg info*/
164         /* parent id */
165         /* limit ratio, if don't want to set limit, use NO_LIMIT*/
166         float limit_ratio;
167         unsigned int limit;
168         /* leave memory usage */
169         unsigned int oomleave;
170         /* thresholds, normal, swap, low, medium, and leave */
171         unsigned int threshold[MEM_LEVEL_MAX];  /* MB */
172         unsigned int threshold_leave;                   /* MB */
173         int evfd;
174         int swappiness;
175 };
176
177 struct lowmem_control_data {
178         enum lowmem_control_type control_type;
179         int pid;
180         int oom_score_adj;
181         struct proc_app_info *pai;
182 };
183
184 struct cgroup_memory_stat {
185         long long value[CGROUP_MEMORY_STAT_MAX];
186 };
187
188 int set_mem_action_conf(struct mem_action *mem_action, const char *value);
189 int set_memcg_conf_threshold(bool percent, char size, int lvl, const char *value); 
190 struct memcg_conf *get_memcg_conf(void);
191 void free_memcg_conf(void);
192
193 const char *cgroup_memory_stat_id_to_string(enum cgroup_memory_stat_id id);
194 enum cgroup_memory_stat_id cgroup_memory_stat_string_to_id(const char *str);
195
196 int memcg_write_limiter_params(void);
197 int memcg_write_optimizer_params(void);
198
199 void memcg_set_threshold(int type, int level, int value);
200 void memcg_set_leave_threshold(int type, int value);
201
202 void memcg_info_set_limit(struct memcg_info *memcg_info, float ratio,
203         unsigned int totalram);
204 void memcg_set_default_swappiness(int swappiness);
205 void memcg_info_set_swappiness(struct memcg_info *mi, int swappiness);
206 int memcg_get_memory_stat(const char *name, struct cgroup_memory_stat **mem_stat);
207
208 /**
209  * @desc get anon memory usage of cgroup based on memory.stat
210  * @return 0 if the value was correctly read
211  */
212 int memcg_get_anon_usage(char *memcg, unsigned int *anon_usage);
213
214 /**
215  * @desc get swapped memory usage of cgroup mi based on memory.stat
216  * @return 0 if the value was correctly read
217  */
218 int memcg_get_swap_usage(char *memcg, unsigned int *usage);
219
220 /**
221  * @desc register eventfd to the memory cgroup with desired value
222  * @return eventfd if it was registered successfully or -1 on failure
223  */
224 int memcg_set_eventfd(const char *memcg, const char *event, const char *value);
225 int memcg_init_eventfd(int evfd, const char *memcg, const char *event, const char *value);
226
227 int check_oom_and_set_limit(const char *dir, unsigned int limit);
228
229 struct memcg_info *get_root_memcg_info(void);
230 void memcg_params_init(void);
231
232 #ifdef __cplusplus
233 }
234 #endif /* __cplusplus */
235
236 #endif /*__MEMORY_CGROUP_H__*/