13 #define MEMSW ".memsw"
18 void (*cgroup_perror
)(const char *s
) = perror
;
19 static const char cgroupfs
[] = "/sys/fs/cgroup/";
22 cgroup_setup(const char *chier
, const char *controllers
)
24 char chierdir
[PATH_MAX
];
25 snprintf(chierdir
, sizeof(chierdir
), "%s%s", cgroupfs
, chier
);
26 if (access(chierdir
, F_OK
) >= 0)
29 if (mount("none", cgroupfs
, "tmpfs", 0, NULL
) < 0) {
30 cgroup_perror(cgroupfs
);
33 if (mkdir(chierdir
, 0777) < 0) {
34 cgroup_perror(chierdir
);
37 if (mount("none", chierdir
, "cgroup", 0, controllers
) < 0) {
38 cgroup_perror(chierdir
);
45 cgroup_create(const char *chier
, const char *cgroup
)
47 char cgroupdir
[PATH_MAX
];
48 snprintf(cgroupdir
, sizeof(cgroupdir
), "%s%s/%s", cgroupfs
, chier
, cgroup
);
49 if (access(cgroupdir
, F_OK
) >= 0)
51 if (mkdir(cgroupdir
, 0777) < 0) {
52 cgroup_perror(cgroupdir
);
60 cgroup_add_task(const char *chier
, const char *cgroup
, pid_t pid
)
62 char tasksfile
[PATH_MAX
];
63 snprintf(tasksfile
, sizeof(tasksfile
), "%s%s/%s/tasks", cgroupfs
, chier
, cgroup
);
64 FILE *tasks
= fopen(tasksfile
, "a");
66 cgroup_perror(tasksfile
);
69 if (fprintf(tasks
, "%d\n", pid
) < 0) {
70 cgroup_perror(tasksfile
);
74 if (fclose(tasks
) < 0) {
75 cgroup_perror(tasksfile
);
82 cgroup_is_task_in_cgroup(const char *chier
, const char *cgroup
, pid_t pid
)
84 char tasksfile
[PATH_MAX
];
85 snprintf(tasksfile
, sizeof(tasksfile
), "%s%s/%s/tasks", cgroupfs
, chier
, cgroup
);
86 FILE *tasks
= fopen(tasksfile
, "r");
88 cgroup_perror(tasksfile
);
94 while (fgets(line
, sizeof(line
), tasks
)) {
95 if (atoi(line
) == pid
) {
105 cgroup_task_list(const char *chier
, const char *cgroup
, pid_t
**tasklist
)
107 char tasksfile
[PATH_MAX
];
108 snprintf(tasksfile
, sizeof(tasksfile
), "%s%s/%s/tasks", cgroupfs
, chier
, cgroup
);
109 FILE *tasks
= fopen(tasksfile
, "r");
111 cgroup_perror(tasksfile
);
118 while (fgets(line
, sizeof(line
), tasks
)) {
120 *tasklist
= realloc(*tasklist
, (ntasks
+ 32) * sizeof(*tasklist
));
121 (*tasklist
)[ntasks
++] = atoi(line
);
129 cgroup_get_mem_limit(const char *chier
, const char *cgroup
)
131 char limitfile
[PATH_MAX
];
132 snprintf(limitfile
, sizeof(limitfile
), "%s%s/%s/memory"MEMSW
".limit_in_bytes", cgroupfs
, chier
, cgroup
);
133 FILE *limit
= fopen(limitfile
, "r");
135 cgroup_perror(limitfile
);
139 fscanf(limit
, "%zu", &nlimit
);
145 cgroup_set_mem_limit_do(const char *chier
, const char *cgroup
, size_t nlimit
, char *memsw
)
147 char limitfile
[PATH_MAX
];
148 snprintf(limitfile
, sizeof(limitfile
), "%s%s/%s/memory%s.limit_in_bytes", cgroupfs
, chier
, cgroup
, memsw
);
149 FILE *limit
= fopen(limitfile
, "w");
151 cgroup_perror(limitfile
);
154 if (fprintf(limit
, "%zu\n", nlimit
) < 0) {
155 cgroup_perror(limitfile
);
159 if (fclose(limit
) < 0) {
160 cgroup_perror(limitfile
);
167 cgroup_set_mem_limit_twice(const char *chier
, const char *cgroup
, size_t nlimit
, char *memsw1
, char *memsw2
)
169 int ret
= cgroup_set_mem_limit_do(chier
, cgroup
, nlimit
, memsw1
);
171 int ret2
= cgroup_set_mem_limit_do(chier
, cgroup
, nlimit
, memsw2
);
172 ret
= ret2
< 0 ? ret2
: (ret
|| ret2
);
178 cgroup_set_mem_limit(const char *chier
, const char *cgroup
, size_t nlimit
)
181 /* We need to set both the "normal" and memsw limits, but in such
182 * order that normal <= memsw always holds. */
183 size_t curlimit
= cgroup_get_mem_limit(chier
, cgroup
);
184 if (nlimit
< curlimit
)
185 return cgroup_set_mem_limit_twice(chier
, cgroup
, nlimit
, "", MEMSW
);
187 return cgroup_set_mem_limit_twice(chier
, cgroup
, nlimit
, MEMSW
, "");
189 return cgroup_set_mem_limit_do(chier
, cgroup
, nlimit
, "");
194 cgroup_get_mem_usage(const char *chier
, const char *cgroup
)
196 char usagefile
[PATH_MAX
];
197 snprintf(usagefile
, sizeof(usagefile
), "%s%s/%s/memory"MEMSW
".usage_in_bytes", cgroupfs
, chier
, cgroup
);
198 FILE *usage
= fopen(usagefile
, "r");
200 cgroup_perror(usagefile
);
204 fscanf(usage
, "%zu", &nusage
);