conf: Add LmkThresholdLeave to set stop condition of LMK
[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 #define MEMCG_MAKE_NAME(name)    MEMCG_##name##_NAME
38
39 /* number of memory cgroups */
40 #define MEMCG_DEFAULT_EVENT_LEVEL       "low"
41
42 #define MEMCG_BACKGROUND_MRU_NAME       "BackgroundMru"
43 #define MEMCG_BACKGROUND_LRU_NAME       "BackgroundLru"
44 #define MEMCG_PRIVATE_NAME              "Private"
45
46 #define MEMCG_NAME                                              "memory"
47 #define MEMCG_PATH                      CGROUP_PATH "/" MEMCG_NAME
48 #define MEMCG_BACKGROUND_MRU_PATH       MEMCG_PATH "/" MEMCG_BACKGROUND_MRU_NAME
49 #define MEMCG_BACKGROUND_LRU_PATH       MEMCG_PATH "/" MEMCG_BACKGROUND_LRU_NAME
50 #define MEMCG_PRIVATE_PATH              MEMCG_PATH "/" MEMCG_PRIVATE_NAME
51
52 #define MEMCG_OOM_CONTROL               "memory.oom_control"
53 #define MEMCG_EVENTFD_CONTROL           "cgroup.event_control"
54 #define MEMCG_EVENTFD_MEMORY_PRESSURE   "memory.pressure_level"
55 #define MEMCG_USAGE                     "memory.usage_in_bytes"
56 #define MEMCG_SWAP_USAGE                "memory.memsw.usage_in_bytes"
57 #define MEMCG_LIMIT_BYTE                "memory.limit_in_bytes"
58 #define MEMCG_SWAP_LIMIT_BYTE           "memory.memsw.limit_in_bytes"
59 #define MEMCG_SWAPPINESS                "memory.swappiness"
60 #define MEMCG_FORCE_RECLAIM             "memory.force_reclaim"
61 #define MEMCG_MOVE_CHARGE               "memory.move_charge_at_immigrate"
62
63 #define DEFAULT_MEMLOG_PATH             "/var/log"
64 #define DEFAULT_MEMLOG_NR_MAX           50
65
66 #define MEMCG_NO_LIMIT                  0
67
68 enum {
69         MEM_LEVEL_HIGH,
70         MEM_LEVEL_MEDIUM,
71         MEM_LEVEL_LOW,
72         MEM_LEVEL_CRITICAL,
73         MEM_LEVEL_OOM,
74         MEM_LEVEL_MAX,
75 };
76
77 enum oom_score {
78         OOM_SCORE_HIGH,
79         OOM_SCORE_MEDIUM,
80         OOM_SCORE_LOW,
81         OOM_SCORE_MAX,
82 };
83
84 enum lowmem_control_type {
85         LOWMEM_MOVE_CGROUP,
86         LOWMEM_MANAGE_FOREGROUND,
87 };
88
89 enum mem_log {
90         MEMLOG_MEMPS,
91         MEMLOG_MEMPS_MEMLIMIT,
92         MEMLOG_MAX,
93 };
94
95 enum cgroup_memory_stat_id {
96         CGROUP_MEMORY_STAT_CACHE = 0,
97         CGROUP_MEMORY_STAT_RSS,
98         CGROUP_MEMORY_STAT_RSS_HUGE,
99         CGROUP_MEMORY_STAT_MAPPED_FILE,
100         CGROUP_MEMORY_STAT_DIRTY,
101         CGROUP_MEMORY_STAT_WRITEBACK,
102         CGROUP_MEMORY_STAT_SWAP,
103         CGROUP_MEMORY_STAT_PGPGIN,
104         CGROUP_MEMORY_STAT_PGPGOUT,
105         CGROUP_MEMORY_STAT_PGFAULT,
106         CGROUP_MEMORY_STAT_PGMAJFAULT,
107         CGROUP_MEMORY_STAT_INACTIVE_ANON,
108         CGROUP_MEMORY_STAT_ACTIVE_ANON,
109         CGROUP_MEMORY_STAT_INACTIVE_FILE,
110         CGROUP_MEMORY_STAT_ACTIVE_FILE,
111         CGROUP_MEMORY_STAT_UNEVICTABLE,
112         CGROUP_MEMORY_STAT_HIERARCHICAL_MEMORY_LIMIT,
113         CGROUP_MEMORY_STAT_HIERARCHICAL_MEMSW_LIMIT,
114         CGROUP_MEMORY_STAT_TOTAL_CACHE,
115         CGROUP_MEMORY_STAT_TOTAL_RSS,
116         CGROUP_MEMORY_STAT_TOTAL_RSS_HUGE,
117         CGROUP_MEMORY_STAT_TOTAL_MAPPED_FILE,
118         CGROUP_MEMORY_STAT_TOTAL_DIRTY,
119         CGROUP_MEMORY_STAT_TOTAL_WRITEBACK,
120         CGROUP_MEMORY_STAT_TOTAL_SWAP,
121         CGROUP_MEMORY_STAT_TOTAL_PGPGIN,
122         CGROUP_MEMORY_STAT_TOTAL_PGPGOUT,
123         CGROUP_MEMORY_STAT_TOTAL_PGFAULT,
124         CGROUP_MEMORY_STAT_TOTAL_PGMAJFAULT,
125         CGROUP_MEMORY_STAT_TOTAL_INACTIVE_ANON,
126         CGROUP_MEMORY_STAT_TOTAL_ACTIVE_ANON,
127         CGROUP_MEMORY_STAT_TOTAL_INACTIVE_FILE,
128         CGROUP_MEMORY_STAT_TOTAL_ACTIVE_FILE,
129         CGROUP_MEMORY_STAT_TOTAL_UNEVICTABLE,
130         CGROUP_MEMORY_STAT_RECENT_ROTATED_ANON,
131         CGROUP_MEMORY_STAT_RECENT_ROTATED_FILE,
132         CGROUP_MEMORY_STAT_RECENT_SCANNED_ANON,
133         CGROUP_MEMORY_STAT_RECENT_SCANNED_FILE,
134         CGROUP_MEMORY_STAT_MAX,
135         CGROUP_MEMORY_STAT_INVALID = -1,
136 };
137
138 /*
139  * [memory cgroup information]
140  * MEMCG_ROOT          : memory cgroup for root dir
141  * MEMCG_BackgroundMru : memory cgroup for most recently used background app
142  * MEMCG_BackgroundLru : memory cgroup for least recently used background app
143  *
144  * [memory cgroup hierarchy]
145  *  (normal mode)
146  *    root
147  *     └─BackgroundMru
148  *     └─BackgroundLru
149  *     └─system.slice/user.slice
150  */
151 enum cgroup_type {
152         MEMCG_TOP = -1,
153         MEMCG_ROOT,
154         MEMCG_BACKGROUND_MRU,
155         MEMCG_BACKGROUND_LRU,
156         MEMCG_END,
157 };
158
159 enum memcg_limit_trigger {
160         MEMCG_TRIGGER_NONE,
161         MEMCG_TRIGGER_OOM,
162         MEMCG_TRIGGER_THRESHOLD,
163         MEMCG_TRIGGER_MAX,
164 };
165
166 struct mem_threshold {
167         bool percent;
168         int threshold;                  /* MB or % */
169 };
170
171 struct mem_action {
172         unsigned long long memory_bytes;        /* Byte */
173         int action;
174 };
175
176 struct memcg_conf {
177         struct mem_threshold threshold[MEM_LEVEL_MAX];
178         struct mem_action service;
179         struct mem_action widget;
180         struct mem_action guiapp;
181         struct mem_action background;
182         float cgroup_limit[MEMCG_END];  /* % */
183         bool oom_popup;
184         enum memcg_limit_trigger limit_trigger;
185         struct mem_threshold threshold_leave;
186 };
187
188 struct memcg_info {
189         /* name of memory cgroup */
190         char name[MAX_PATH_LENGTH];
191         /* limit ratio, if don't want to set limit, use NO_LIMIT*/
192         float limit_ratio;
193         unsigned long long limit_bytes;
194         /* thresholds, normal, swap, low, medium, and leave */
195         unsigned int threshold_mb[MEM_LEVEL_MAX];       /* MB */
196         unsigned int threshold_leave_mb;                        /* MB */
197         int evfd;
198         int swappiness;
199 };
200
201 struct lowmem_control_data {
202         enum lowmem_control_type control_type;
203         int pid;
204         int oom_score_adj;
205         struct proc_app_info *pai;
206 };
207
208 struct cgroup_memory_stat {
209         long long value[CGROUP_MEMORY_STAT_MAX];
210 };
211
212 bool memcg_memsw_is_supported(void);
213
214 void register_totalram_bytes(unsigned long long ram_bytes);
215 int set_mem_action_conf(struct mem_action *mem_action, const char *value);
216 int set_memcg_conf_threshold(bool percent, char size, int lvl, const char *value);
217 int memcg_set_conf_threshold_leave(bool percent, char size, const char *value);
218 struct memcg_conf *get_memcg_conf(void);
219 void free_memcg_conf(void);
220
221 const char *cgroup_memory_stat_id_to_string(enum cgroup_memory_stat_id id);
222 enum cgroup_memory_stat_id cgroup_memory_stat_string_to_id(const char *str);
223
224 int memcg_write_limiter_params(void);
225 int memcg_write_optimizer_params(enum cgroup_type cgroup_type);
226
227 void memcg_set_threshold(int type, int level, int value);
228 void memcg_set_leave_threshold(int type, int value);
229
230 void memcg_info_set_limit(struct memcg_info *mi, float ratio,
231         unsigned long long totalram_bytes);
232 void memcg_set_default_swappiness(int swappiness);
233 void memcg_info_set_swappiness(struct memcg_info *mi, int swappiness);
234 int memcg_get_memory_stat(const char *name, struct cgroup_memory_stat **mem_stat);
235
236 /**
237  * @desc get anon memory usage of cgroup based on memory.stat
238  * @return 0 if the value was correctly read
239  */
240 int memcg_get_anon_usage(char *memcg, unsigned long long *anon_usage);
241
242 /**
243  * @desc get swapped memory usage of cgroup mi based on memory.stat
244  * @return 0 if the value was correctly read
245  */
246 int memcg_get_swap_usage(char *memcg, unsigned long long *usage);
247
248 /**
249  * @desc register eventfd to the memory cgroup with desired value
250  * @return eventfd if it was registered successfully or -1 on failure
251  */
252 int memcg_set_eventfd(const char *memcg, const char *event, const char *value);
253 int memcg_init_eventfd(int evfd, const char *memcg, const char *event, const char *value);
254
255 int check_oom_and_set_limit(const char *dir, unsigned long long limit);
256
257 struct memcg_info *get_root_memcg_info(void);
258 void memcg_params_init(void);
259
260 /**
261  * @desc make full cgroup,
262  * @param parentdir - parent cgroup path
263  * @return negative value if error
264  */
265 int memcg_make_full_subdir(const char* parentdir);
266 bool get_use_hierarchy(int idx);
267
268 /**
269  * @desc Get cgroup type according to oom_score_adj
270  * @param oom_score_adj - oom_score_adj
271  * @return cgroup type
272  */
273 int cgroup_get_type(int oom_score_adj);
274
275 /**
276  * @desc Get the highest oom_score_adj of the cgroup type
277  * @param type - cgroup type
278  * @return oom_score_adj
279  */
280 int cgroup_get_highest_oom_score_adj(int type);
281
282 /**
283  * @desc Get the lowest oom_score_adj of the cgroup type
284  * @param type - cgroup type
285  * @return oom_score_adj
286  */
287 int cgroup_get_lowest_oom_score_adj(int type);
288 struct memcg_info *get_memcg_info(int idx);
289 struct cgroup *get_cgroup_tree(int idx);
290
291 void set_memcg_limit_trigger(enum memcg_limit_trigger limit_trigger);
292 enum memcg_limit_trigger get_memcg_limit_trigger(void);
293
294 #ifdef __cplusplus
295 }
296 #endif /* __cplusplus */
297
298 #endif /*__MEMORY_CGROUP_H__*/