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 fprintf(tasks
, "%d\n", pid
);
75 cgroup_is_task_in_cgroup(const char *chier
, const char *cgroup
, pid_t pid
)
77 char tasksfile
[PATH_MAX
];
78 snprintf(tasksfile
, sizeof(tasksfile
), "%s%s/%s/tasks", cgroupfs
, chier
, cgroup
);
79 FILE *tasks
= fopen(tasksfile
, "r");
81 cgroup_perror(tasksfile
);
87 while (fgets(line
, sizeof(line
), tasks
)) {
88 if (atoi(line
) == pid
) {
98 cgroup_task_list(const char *chier
, const char *cgroup
, pid_t
**tasklist
)
100 char tasksfile
[PATH_MAX
];
101 snprintf(tasksfile
, sizeof(tasksfile
), "%s%s/%s/tasks", cgroupfs
, chier
, cgroup
);
102 FILE *tasks
= fopen(tasksfile
, "r");
104 cgroup_perror(tasksfile
);
111 while (fgets(line
, sizeof(line
), tasks
)) {
113 *tasklist
= realloc(*tasklist
, (ntasks
+ 32) * sizeof(*tasklist
));
114 (*tasklist
)[ntasks
++] = atoi(line
);
122 cgroup_get_mem_limit(const char *chier
, const char *cgroup
)
124 char limitfile
[PATH_MAX
];
125 snprintf(limitfile
, sizeof(limitfile
), "%s%s/%s/memory"MEMSW
".limit_in_bytes", cgroupfs
, chier
, cgroup
);
126 FILE *limit
= fopen(limitfile
, "r");
128 cgroup_perror(limitfile
);
132 fscanf(limit
, "%zu", &nlimit
);
138 cgroup_set_mem_limit_do(const char *chier
, const char *cgroup
, size_t nlimit
, char *memsw
)
140 char limitfile
[PATH_MAX
];
141 snprintf(limitfile
, sizeof(limitfile
), "%s%s/%s/memory%s.limit_in_bytes", cgroupfs
, chier
, cgroup
, memsw
);
142 FILE *limit
= fopen(limitfile
, "w");
144 cgroup_perror(limitfile
);
147 fprintf(limit
, "%zu\n", nlimit
);
153 cgroup_set_mem_limit(const char *chier
, const char *cgroup
, size_t nlimit
)
155 /* We need to set both the "normal" and memsw limits, normal first,
156 * since normal <= memsw must hold. */
157 int ret
= cgroup_set_mem_limit_do(chier
, cgroup
, nlimit
, "");
160 ret
|= cgroup_set_mem_limit_do(chier
, cgroup
, nlimit
, MEMSW
);
166 cgroup_get_mem_usage(const char *chier
, const char *cgroup
)
168 char usagefile
[PATH_MAX
];
169 snprintf(usagefile
, sizeof(usagefile
), "%s%s/%s/memory"MEMSW
".usage_in_bytes", cgroupfs
, chier
, cgroup
);
170 FILE *usage
= fopen(usagefile
, "r");
172 cgroup_perror(usagefile
);
176 fscanf(usage
, "%zu", &nusage
);