4 * Copyright (c) 2014 Samsung Electronics Co., Ltd. All rights reserved.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * @file memory-cgroup.h
22 * @desc header file for handling memory cgroups
25 #ifndef __MEMORY_CGROUP_H__
26 #define __MEMORY_CGROUP_H__
35 #endif /* __cplusplus */
37 #define MEMCG_MAKE_NAME(name) MEMCG_##name##_NAME
39 /* number of memory cgroups */
40 #define MEMCG_DEFAULT_EVENT_LEVEL "low"
42 #define MEMCG_BACKGROUND_MRU_NAME "BackgroundMru"
43 #define MEMCG_BACKGROUND_LRU_NAME "BackgroundLru"
44 #define MEMCG_PRIVATE_NAME "Private"
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
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"
63 #define DEFAULT_MEMLOG_PATH "/var/log"
64 #define DEFAULT_MEMLOG_NR_MAX 50
66 #define MEMCG_NO_LIMIT 0
84 enum lowmem_control_type {
86 LOWMEM_MANAGE_FOREGROUND,
91 MEMLOG_MEMPS_MEMLIMIT,
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,
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
144 * [memory cgroup hierarchy]
149 * └─system.slice/user.slice
154 MEMCG_BACKGROUND_MRU,
155 MEMCG_BACKGROUND_LRU,
159 enum memcg_limit_trigger {
162 MEMCG_TRIGGER_THRESHOLD,
166 struct mem_threshold {
168 int threshold; /* MB or % */
172 unsigned long long memory_bytes; /* Byte */
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]; /* % */
184 enum memcg_limit_trigger limit_trigger;
188 /* name of memory cgroup */
189 char name[MAX_PATH_LENGTH];
190 /* limit ratio, if don't want to set limit, use NO_LIMIT*/
192 unsigned long long limit_bytes;
193 /* thresholds, normal, swap, low, medium, and leave */
194 unsigned int threshold_mb[MEM_LEVEL_MAX]; /* MB */
195 unsigned int threshold_leave_mb; /* MB */
200 struct lowmem_control_data {
201 enum lowmem_control_type control_type;
204 struct proc_app_info *pai;
207 struct cgroup_memory_stat {
208 long long value[CGROUP_MEMORY_STAT_MAX];
211 bool memcg_memsw_is_supported(void);
213 void register_totalram_bytes(unsigned long long ram_bytes);
214 int set_mem_action_conf(struct mem_action *mem_action, const char *value);
215 int set_memcg_conf_threshold(bool percent, char size, int lvl, const char *value);
216 struct memcg_conf *get_memcg_conf(void);
217 void free_memcg_conf(void);
219 const char *cgroup_memory_stat_id_to_string(enum cgroup_memory_stat_id id);
220 enum cgroup_memory_stat_id cgroup_memory_stat_string_to_id(const char *str);
222 int memcg_write_limiter_params(void);
223 int memcg_write_optimizer_params(enum cgroup_type cgroup_type);
225 void memcg_set_threshold(int type, int level, int value);
226 void memcg_set_leave_threshold(int type, int value);
228 void memcg_info_set_limit(struct memcg_info *mi, float ratio,
229 unsigned long long totalram_bytes);
230 void memcg_set_default_swappiness(int swappiness);
231 void memcg_info_set_swappiness(struct memcg_info *mi, int swappiness);
232 int memcg_get_memory_stat(const char *name, struct cgroup_memory_stat **mem_stat);
235 * @desc get anon memory usage of cgroup based on memory.stat
236 * @return 0 if the value was correctly read
238 int memcg_get_anon_usage(char *memcg, unsigned long long *anon_usage);
241 * @desc get swapped memory usage of cgroup mi based on memory.stat
242 * @return 0 if the value was correctly read
244 int memcg_get_swap_usage(char *memcg, unsigned long long *usage);
247 * @desc register eventfd to the memory cgroup with desired value
248 * @return eventfd if it was registered successfully or -1 on failure
250 int memcg_set_eventfd(const char *memcg, const char *event, const char *value);
251 int memcg_init_eventfd(int evfd, const char *memcg, const char *event, const char *value);
253 int check_oom_and_set_limit(const char *dir, unsigned long long limit);
255 struct memcg_info *get_root_memcg_info(void);
256 void memcg_params_init(void);
259 * @desc make full cgroup,
260 * @param parentdir - parent cgroup path
261 * @return negative value if error
263 int memcg_make_full_subdir(const char* parentdir);
264 bool get_use_hierarchy(int idx);
267 * @desc Get cgroup type according to oom_score_adj
268 * @param oom_score_adj - oom_score_adj
269 * @return cgroup type
271 int cgroup_get_type(int oom_score_adj);
274 * @desc Get the highest oom_score_adj of the cgroup type
275 * @param type - cgroup type
276 * @return oom_score_adj
278 int cgroup_get_highest_oom_score_adj(int type);
281 * @desc Get the lowest oom_score_adj of the cgroup type
282 * @param type - cgroup type
283 * @return oom_score_adj
285 int cgroup_get_lowest_oom_score_adj(int type);
286 struct memcg_info *get_memcg_info(int idx);
287 struct cgroup *get_cgroup_tree(int idx);
289 void set_memcg_limit_trigger(enum memcg_limit_trigger limit_trigger);
290 enum memcg_limit_trigger get_memcg_limit_trigger(void);
294 #endif /* __cplusplus */
296 #endif /*__MEMORY_CGROUP_H__*/