1 // SPDX-License-Identifier: GPL-2.0-only
3 * This test makes sure BPF stats collection using rstat works correctly.
4 * The test uses 3 BPF progs:
5 * (a) counter: This BPF prog is invoked every time we attach a process to a
6 * cgroup and locklessly increments a percpu counter.
7 * The program then calls cgroup_rstat_updated() to inform rstat
8 * of an update on the (cpu, cgroup) pair.
10 * (b) flusher: This BPF prog is invoked when an rstat flush is ongoing, it
11 * aggregates all percpu counters to a total counter, and also
12 * propagates the changes to the ancestor cgroups.
14 * (c) dumper: This BPF prog is a cgroup_iter. It is used to output the total
15 * counter of a cgroup through reading a file in userspace.
17 * The test sets up a cgroup hierarchy, and the above programs. It spawns a few
18 * processes in the leaf cgroups and makes sure all the counters are aggregated
21 * Copyright 2022 Google LLC.
23 #include <asm-generic/errno.h>
25 #include <sys/types.h>
26 #include <sys/mount.h>
30 #include <test_progs.h>
31 #include <bpf/libbpf.h>
34 #include "cgroup_helpers.h"
35 #include "cgroup_hierarchical_stats.skel.h"
37 #define PAGE_SIZE 4096
38 #define MB(x) (x << 20)
40 #define PROCESSES_PER_CGROUP 3
42 #define BPFFS_ROOT "/sys/fs/bpf/"
43 #define BPFFS_ATTACH_COUNTERS BPFFS_ROOT "attach_counters/"
45 #define CG_ROOT_NAME "root"
48 #define CGROUP_PATH(p, n) {.path = p"/"n, .name = n}
51 const char *path, *name;
52 unsigned long long id;
55 CGROUP_PATH("/", "test"),
56 CGROUP_PATH("/test", "child1"),
57 CGROUP_PATH("/test", "child2"),
58 CGROUP_PATH("/test/child1", "child1_1"),
59 CGROUP_PATH("/test/child1", "child1_2"),
60 CGROUP_PATH("/test/child2", "child2_1"),
61 CGROUP_PATH("/test/child2", "child2_2"),
64 #define N_CGROUPS ARRAY_SIZE(cgroups)
65 #define N_NON_LEAF_CGROUPS 3
67 static int root_cgroup_fd;
68 static bool mounted_bpffs;
70 /* reads file at 'path' to 'buf', returns 0 on success. */
71 static int read_from_file(const char *path, char *buf, size_t size)
75 fd = open(path, O_RDONLY);
79 len = read(fd, buf, size);
88 /* mounts bpffs and mkdir for reading stats, returns 0 on success. */
89 static int setup_bpffs(void)
94 err = mount("bpf", BPFFS_ROOT, "bpf", 0, NULL);
96 if (ASSERT_FALSE(err && errno != EBUSY, "mount"))
99 /* Create a directory to contain stat files in bpffs */
100 err = mkdir(BPFFS_ATTACH_COUNTERS, 0755);
101 if (!ASSERT_OK(err, "mkdir"))
107 static void cleanup_bpffs(void)
109 /* Remove created directory in bpffs */
110 ASSERT_OK(rmdir(BPFFS_ATTACH_COUNTERS), "rmdir "BPFFS_ATTACH_COUNTERS);
112 /* Unmount bpffs, if it wasn't already mounted when we started */
116 ASSERT_OK(umount(BPFFS_ROOT), "unmount bpffs");
119 /* sets up cgroups, returns 0 on success. */
120 static int setup_cgroups(void)
124 err = setup_cgroup_environment();
125 if (!ASSERT_OK(err, "setup_cgroup_environment"))
128 root_cgroup_fd = get_root_cgroup();
129 if (!ASSERT_GE(root_cgroup_fd, 0, "get_root_cgroup"))
130 return root_cgroup_fd;
132 for (i = 0; i < N_CGROUPS; i++) {
133 fd = create_and_get_cgroup(cgroups[i].path);
134 if (!ASSERT_GE(fd, 0, "create_and_get_cgroup"))
138 cgroups[i].id = get_cgroup_id(cgroups[i].path);
143 static void cleanup_cgroups(void)
145 close(root_cgroup_fd);
146 for (int i = 0; i < N_CGROUPS; i++)
147 close(cgroups[i].fd);
148 cleanup_cgroup_environment();
151 /* Sets up cgroup hiearchary, returns 0 on success. */
152 static int setup_hierarchy(void)
154 return setup_bpffs() || setup_cgroups();
157 static void destroy_hierarchy(void)
163 static int attach_processes(void)
167 /* In every leaf cgroup, attach 3 processes */
168 for (i = N_NON_LEAF_CGROUPS; i < N_CGROUPS; i++) {
169 for (j = 0; j < PROCESSES_PER_CGROUP; j++) {
172 /* Create child and attach to cgroup */
175 if (join_parent_cgroup(cgroups[i].path))
181 waitpid(pid, &status, 0);
182 if (!ASSERT_TRUE(WIFEXITED(status), "child process exited"))
184 if (!ASSERT_EQ(WEXITSTATUS(status), 0,
185 "child process exit code"))
192 static unsigned long long
193 get_attach_counter(unsigned long long cgroup_id, const char *file_name)
195 unsigned long long attach_counter = 0, id = 0;
196 static char buf[128], path[128];
198 /* For every cgroup, read the file generated by cgroup_iter */
199 snprintf(path, 128, "%s%s", BPFFS_ATTACH_COUNTERS, file_name);
200 if (!ASSERT_OK(read_from_file(path, buf, 128), "read cgroup_iter"))
203 /* Check the output file formatting */
204 ASSERT_EQ(sscanf(buf, "cg_id: %llu, attach_counter: %llu\n",
205 &id, &attach_counter), 2, "output format");
207 /* Check that the cgroup_id is displayed correctly */
208 ASSERT_EQ(id, cgroup_id, "cgroup_id");
209 /* Check that the counter is non-zero */
210 ASSERT_GT(attach_counter, 0, "attach counter non-zero");
211 return attach_counter;
214 static void check_attach_counters(void)
216 unsigned long long attach_counters[N_CGROUPS], root_attach_counter;
219 for (i = 0; i < N_CGROUPS; i++)
220 attach_counters[i] = get_attach_counter(cgroups[i].id,
223 /* Read stats for root too */
224 root_attach_counter = get_attach_counter(CG_ROOT_ID, CG_ROOT_NAME);
226 /* Check that all leafs cgroups have an attach counter of 3 */
227 for (i = N_NON_LEAF_CGROUPS; i < N_CGROUPS; i++)
228 ASSERT_EQ(attach_counters[i], PROCESSES_PER_CGROUP,
229 "leaf cgroup attach counter");
231 /* Check that child1 == child1_1 + child1_2 */
232 ASSERT_EQ(attach_counters[1], attach_counters[3] + attach_counters[4],
234 /* Check that child2 == child2_1 + child2_2 */
235 ASSERT_EQ(attach_counters[2], attach_counters[5] + attach_counters[6],
237 /* Check that test == child1 + child2 */
238 ASSERT_EQ(attach_counters[0], attach_counters[1] + attach_counters[2],
240 /* Check that root >= test */
241 ASSERT_GE(root_attach_counter, attach_counters[1], "root_counter");
244 /* Creates iter link and pins in bpffs, returns 0 on success, -errno on failure.
246 static int setup_cgroup_iter(struct cgroup_hierarchical_stats *obj,
247 int cgroup_fd, const char *file_name)
249 DECLARE_LIBBPF_OPTS(bpf_iter_attach_opts, opts);
250 union bpf_iter_link_info linfo = {};
251 struct bpf_link *link;
252 static char path[128];
256 * Create an iter link, parameterized by cgroup_fd. We only want to
257 * traverse one cgroup, so set the traversal order to "self".
259 linfo.cgroup.cgroup_fd = cgroup_fd;
260 linfo.cgroup.order = BPF_CGROUP_ITER_SELF_ONLY;
261 opts.link_info = &linfo;
262 opts.link_info_len = sizeof(linfo);
263 link = bpf_program__attach_iter(obj->progs.dumper, &opts);
264 if (!ASSERT_OK_PTR(link, "attach_iter"))
267 /* Pin the link to a bpffs file */
268 snprintf(path, 128, "%s%s", BPFFS_ATTACH_COUNTERS, file_name);
269 err = bpf_link__pin(link, path);
270 ASSERT_OK(err, "pin cgroup_iter");
272 /* Remove the link, leaving only the ref held by the pinned file */
273 bpf_link__destroy(link);
277 /* Sets up programs for collecting stats, returns 0 on success. */
278 static int setup_progs(struct cgroup_hierarchical_stats **skel)
282 *skel = cgroup_hierarchical_stats__open_and_load();
283 if (!ASSERT_OK_PTR(*skel, "open_and_load"))
286 /* Attach cgroup_iter program that will dump the stats to cgroups */
287 for (i = 0; i < N_CGROUPS; i++) {
288 err = setup_cgroup_iter(*skel, cgroups[i].fd, cgroups[i].name);
289 if (!ASSERT_OK(err, "setup_cgroup_iter"))
293 /* Also dump stats for root */
294 err = setup_cgroup_iter(*skel, root_cgroup_fd, CG_ROOT_NAME);
295 if (!ASSERT_OK(err, "setup_cgroup_iter"))
298 bpf_program__set_autoattach((*skel)->progs.dumper, false);
299 err = cgroup_hierarchical_stats__attach(*skel);
300 if (!ASSERT_OK(err, "attach"))
306 static void destroy_progs(struct cgroup_hierarchical_stats *skel)
308 static char path[128];
311 for (i = 0; i < N_CGROUPS; i++) {
312 /* Delete files in bpffs that cgroup_iters are pinned in */
313 snprintf(path, 128, "%s%s", BPFFS_ATTACH_COUNTERS,
315 ASSERT_OK(remove(path), "remove cgroup_iter pin");
318 /* Delete root file in bpffs */
319 snprintf(path, 128, "%s%s", BPFFS_ATTACH_COUNTERS, CG_ROOT_NAME);
320 ASSERT_OK(remove(path), "remove cgroup_iter root pin");
321 cgroup_hierarchical_stats__destroy(skel);
324 void test_cgroup_hierarchical_stats(void)
326 struct cgroup_hierarchical_stats *skel = NULL;
328 if (setup_hierarchy())
329 goto hierarchy_cleanup;
330 if (setup_progs(&skel))
332 if (attach_processes())
334 check_attach_counters();