4 * Copyright (c) 2013 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 * Cgroup creation interface
24 #ifndef _CGROUP_LIBRARY_CGROUP_H_
25 #define _CGROUP_LIBRARY_CGROUP_H_
29 #include <resourced.h>
30 #include <sys/types.h>
38 #endif /* __cplusplus */
40 #define TASK_FILE_NAME "tasks"
41 #define CGROUP_FILE_NAME "cgroup.procs"
43 #define CGROUP_PATH "/sys/fs/cgroup"
45 #define CGROUP_VIP_NAME ""
46 #define CGROUP_HIGH_NAME "High"
47 #define CGROUP_MEDIUM_NAME "Medium"
48 #define CGROUP_LOW_NAME "Lowest"
50 #define CGROUP_PER_PROCESS_NAME ""
51 #define CGROUP_GROUP_NAME ""
54 #define CGROUP_DEFAULT_USE_HIERARCHY false
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
66 * ├─high─(tizendocker)
69 * └─system.slice/user.slice (not controlled by resourced)
75 * ├─high─(tizendocker)
78 * └─system.slice/user.slice (not controlled by resourced)
91 /* hashname of memory cgroup for restoring memcg info*/
92 char hashname[MAX_NAME_LENGTH];
93 /* parent cgroup index */
95 /* set when using multiple sub cgroups */
97 /* memory cgroup information */
98 struct memcg_info *memcg_info;
99 /* list of child cgroups when using multi groups */
100 GSList *child_cgroups;
104 * @desc Get cgroup type according to oom_score_adj
105 * @param oom_score_adj - oom_score_adj
106 * @return cgroup type
108 int cgroup_get_type(int oom_score_adj);
111 * @desc Get the highest oom_score_adj of the cgroup type
112 * @param type - cgroup type
113 * @return oom_score_adj
115 int cgroup_get_highest_oom_score_adj(int type);
118 * @desc Get the lowest oom_score_adj of the cgroup type
119 * @param type - cgroup type
120 * @return oom_score_adj
122 int cgroup_get_lowest_oom_score_adj(int type);
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
131 int cgroup_read_node_uint32(const char *cgroup_name,
132 const char *file_name, uint32_t *value);
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
141 int cgroup_read_node_int32(const char *cgroup_name,
142 const char *file_name, int32_t *value);
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
151 int cgroup_read_node_ulonglong(const char *cgroup_name,
152 const char *file_name, unsigned long long *value);
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
161 int cgroup_write_node_uint32(const char *cgroup_name, const char *file_name, uint32_t value);
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
170 int cgroup_write_node_int32(const char *cgroup_name, const char *file_name, int32_t value);
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
179 int cgroup_write_node_ulonglong(const char *cgroup_name, const char *file_name, unsigned long long value);
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
188 int cgroup_write_node_str(const char *cgroup_name,
189 const char *file_name, const char *string);
192 * @desc make full cgroup,
193 * @param parentdir - parent cgroup path
194 * @return negative value if error
196 int cgroup_make_full_subdir(const char* parentdir);
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
206 int cgroup_make_subdir(const char* parentdir, const char* cgroup_name, bool *already);
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
215 int cgroup_mount_subsystem(char* source, char* mount_point, char* opts);
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
223 resourced_ret_c cgroup_write_tid_fullpath(const char *cgroup_full_path,
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
232 resourced_ret_c cgroup_write_pid_fullpath(const char *cgroup_full_path,
235 resourced_ret_c cgroup_write_pid(const char *cgroup_subsystem,
236 const char *cgroup_name, const int pid);
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
246 int cgroup_set_release_agent(const char *cgroup_subsys, const char *release_agent);
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
259 int cgroup_pid_get_path(const char *controller, pid_t pid, char **path);
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
265 int cgroup_get_pids(const char *name, GArray **pids);
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);
276 //void cgroup_params_exit(void);
277 void cgroup_params_init(void);
281 #endif /* __cplusplus */
283 #endif /*_CGROUP_LIBRARY_CGROUP_H_*/