Add unit(in variable) & fix bugs
[platform/core/system/resourced.git] / src / common / cgroup / cgroup.h
1 /*
2  * resourced
3  *
4  * Copyright (c) 2013 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  * Cgroup creation interface
22  */
23
24 #ifndef _CGROUP_LIBRARY_CGROUP_H_
25 #define _CGROUP_LIBRARY_CGROUP_H_
26
27 #include <assert.h>
28 #include <string.h>
29 #include <resourced.h>
30 #include <sys/types.h>
31 #include <stdint.h>
32 #include <glib.h>
33 #include "const.h"
34 #include "macro.h"
35
36 #ifdef __cplusplus
37 extern "C" {
38 #endif /* __cplusplus */
39
40 #define TASK_FILE_NAME          "tasks"
41 #define CGROUP_FILE_NAME        "cgroup.procs"
42
43 #define CGROUP_PATH             "/sys/fs/cgroup"
44
45 #define CGROUP_VIP_NAME         ""
46 #define CGROUP_HIGH_NAME        "High"
47 #define CGROUP_MEDIUM_NAME      "Medium"
48 #define CGROUP_LOW_NAME         "Lowest"
49
50 #define CGROUP_PER_PROCESS_NAME ""
51 #define CGROUP_GROUP_NAME       ""
52
53
54 #define CGROUP_DEFAULT_USE_HIERARCHY     false
55 /*
56  * [cgroup information]
57  * CGROUP_ROOT   : root cgroup
58  * CGROUP_VIP    : cgroup for vip apps(or daemons)
59  * CGROUP_HIGH   : cgroup for foreground apps
60  * CGROUP_MEDIUM : cgroup for background apps
61  * CGROUP_LOW    : cgroup for apps of the lowest privilege
62  *
63  * [cgroup hierarchy]
64  *  (normal mode)
65      root(cpu, memory, io)
66  *         ├─high─(tizendocker)
67  *     │   └─medium
68  *     │       └─low
69  *     └─system.slice/user.slice (not controlled by resourced)
70  *
71  *  (vip mode)
72      root(cpu, memory, io)
73  *     │
74  *    vip
75  *         ├─high─(tizendocker)
76  *     │   └─medium
77  *     │       └─low
78  *     └─system.slice/user.slice (not controlled by resourced)
79  */
80 enum cgroup_type {
81         CGROUP_TOP = -1,
82         CGROUP_ROOT,
83         CGROUP_VIP,
84         CGROUP_HIGH,
85         CGROUP_MEDIUM,
86         CGROUP_LOW,
87         CGROUP_END,
88 };
89
90 struct cgroup {
91         /* hashname of memory cgroup for restoring memcg info*/
92         char hashname[MAX_NAME_LENGTH];
93         /* parent cgroup index */
94         int parent_cgroup;
95         /* set when using multiple sub cgroups */
96         bool use_hierarchy;
97         /* memory cgroup information */
98         struct memcg_info *memcg_info;
99         /* list of child cgroups when using multi groups */
100         GSList *child_cgroups;
101 };
102
103 /**
104  * @desc Get cgroup type according to oom_score_adj
105  * @param oom_score_adj - oom_score_adj
106  * @return cgroup type
107  */
108 int cgroup_get_type(int oom_score_adj);
109
110 /**
111  * @desc Get the highest oom_score_adj of the cgroup type
112  * @param type - cgroup type
113  * @return oom_score_adj
114  */
115 int cgroup_get_highest_oom_score_adj(int type);
116
117 /**
118  * @desc Get the lowest oom_score_adj of the cgroup type
119  * @param type - cgroup type
120  * @return oom_score_adj
121  */
122 int cgroup_get_lowest_oom_score_adj(int type);
123
124 /**
125  * @desc Get one unsigned int32 value from cgroup
126  * @param cgroup_name - cgroup path
127  * @param file_name - cgroup content to write
128  * @param value - out parameter, value to fill
129  * @return negative value if error
130 */
131 int cgroup_read_node_uint32(const char *cgroup_name,
132                 const char *file_name, uint32_t *value);
133
134 /**
135  * @desc Get one signed int32 value from cgroup
136  * @param cgroup_name - cgroup path
137  * @param file_name - cgroup content to write
138  * @param value - out parameter, value to fill
139  * @return negative value if error
140  */
141 int cgroup_read_node_int32(const char *cgroup_name,
142                 const char *file_name, int32_t *value);
143
144 /**
145  * @desc Get one unsigned long long value from cgroup
146  * @param cgroup_name - cgroup path
147  * @param file_name - cgroup content to write
148  * @param value - out parameter, value to fill
149  * @return negative value if error
150  */
151 int cgroup_read_node_ulonglong(const char *cgroup_name,
152                 const char *file_name, unsigned long long *value);
153
154 /**
155  * @desc Put unsigned int32 value to cgroup,
156  * @param cgroup_name - cgroup path
157  * @param file_name - cgroup content to write
158  * @param value - unsigned int32 data to write
159  * @return negative value if error
160  */
161 int cgroup_write_node_uint32(const char *cgroup_name,  const char *file_name, uint32_t value);
162
163 /**
164  * @desc Put signed int32 value to cgroup,
165  * @param cgroup_name - cgroup path
166  * @param file_name - cgroup content to write
167  * @param value - signed int32 data to write
168  * @return negative value if error
169  */
170 int cgroup_write_node_int32(const char *cgroup_name,  const char *file_name, int32_t value);
171
172 /**
173  * @desc Put unsigned long long value to cgroup,
174  * @param cgroup_name - cgroup path
175  * @param file_name - cgroup content to write
176  * @param value - unsigned long data to write
177  * @return negative value if error
178  */
179 int cgroup_write_node_ulonglong(const char *cgroup_name, const char *file_name, unsigned long long value);
180
181 /**
182  * @desc Put value to cgroup,
183  * @param cgroup_name - cgroup path
184  * @param file_name - cgroup content to write
185  * @param string -string to write
186  * @return negative value if error
187  */
188 int cgroup_write_node_str(const char *cgroup_name,
189                 const char *file_name, const char *string);
190
191 /**
192  * @desc make full cgroup,
193  * @param parentdir - parent cgroup path
194  * @return negative value if error
195  */
196 int cgroup_make_full_subdir(const char* parentdir);
197
198 /**
199  * @desc make cgroup,
200  * @param parentdir - parent cgroup path
201  * @param cgroup_name - cgroup subdirectory to write
202  * @param already - true if subdir already exists, NULL pointer is possible
203  * as formal argument, in this case it will not be filled
204  * @return negative value if error
205  */
206 int cgroup_make_subdir(const char* parentdir, const char* cgroup_name, bool *already);
207
208 /**
209  * @desc mount cgroup,
210  * @param source -cgroup name
211  * @param mount_point - cgroup path
212  * @param opts - mount options
213  * @return negative value if error
214  */
215 int cgroup_mount_subsystem(char* source, char* mount_point, char* opts);
216
217 /**
218  * @desc write tid into cgroup_subsystem/cgroup_name file,
219  * @param cgroup_full_path - name in /sys/fs/cgroup/subsystem/
220  * @param tid - tid to write to cgroup file
221  * @return negative value if error
222  */
223 resourced_ret_c cgroup_write_tid_fullpath(const char *cgroup_full_path,
224         const int tid);
225
226 /**
227  * @desc write pid into cgroup_subsystem/cgroup_name file,
228  * @param cgroup_full_path - name in /sys/fs/cgroup/subsystem/
229  * @param pid - pid to write to cgroup file
230  * @return negative value if error
231  */
232 resourced_ret_c cgroup_write_pid_fullpath(const char *cgroup_full_path,
233         const int pid);
234
235 resourced_ret_c cgroup_write_pid(const char *cgroup_subsystem,
236         const char *cgroup_name, const int pid);
237
238 /**
239  * @desc this function sets release agent path into cgroup subsystem
240  * and enables this mechanism
241  * @param cgroup_sussys - cgroup subsystem name, it's relative path to cgroup,
242  * relativelly default cgroup path (DEFAULT_CGROUP)
243  * @param release_agent full path to release agent executable
244  * @return negative value if error
245  */
246 int cgroup_set_release_agent(const char *cgroup_subsys, const char *release_agent);
247
248 /**
249  * @desc get controller path of pid
250  * @param controller the name of control group such like
251  * "cpuacct,cpu", memory, freezer or debug. Especially supporting
252  * "name=" prefix. For example, if "systemd" is given as controller
253  * then finding also "name=systemd".
254  * @param pid pid to get the controller path
255  * @param path contoller path is filled. This value has to be free-ed
256  * by caller function.
257  * @return negative value if error
258  */
259 int cgroup_pid_get_path(const char *controller, pid_t pid, char **path);
260
261 /**
262  * @desc get PIDs of processes in a certain cgroup, an allocated array must be provided
263  * @return 0 if pids were read and array filled
264  */
265 int cgroup_get_pids(const char *name, GArray **pids);
266
267
268 struct cgroup *get_cgroup_tree(int idx);
269 void set_memcg_info(int idx, struct memcg_info *mi);
270 struct memcg_info *get_memcg_info(int idx);
271 GSList *get_child_cgroups(int idx);
272 int get_parent_cgroup(int idx);
273 void set_use_hierarchy(int idx, bool use_hierarchy);
274 bool get_use_hierarchy(int idx);
275
276 //void cgroup_params_exit(void);
277 void cgroup_params_init(void);
278
279 #ifdef __cplusplus
280 }
281 #endif /* __cplusplus */
282
283 #endif /*_CGROUP_LIBRARY_CGROUP_H_*/