|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | +/* Copyright (c) 2022 Google */ |
| 3 | +#define _GNU_SOURCE |
| 4 | +#include <sys/stat.h> /* mkdir */ |
| 5 | +#include <fcntl.h> /* name_to_handle_at */ |
| 6 | +#include <stdlib.h> |
| 7 | +#include <test_progs.h> |
| 8 | +#include "cgroup_monitor.skel.h" |
| 9 | +#include "cgroup_sched_lat.skel.h" |
| 10 | + |
| 11 | +static char mkdir_prog_path[64]; |
| 12 | +static char rmdir_prog_path[64]; |
| 13 | +static char dump_prog_path[64]; |
| 14 | + |
| 15 | +/* Get cgroup id from a full path to cgroup */ |
| 16 | +static int get_cgroup_id(const char *cgroup) |
| 17 | +{ |
| 18 | + int mount_id = 0; |
| 19 | + struct { |
| 20 | + struct file_handle fh; |
| 21 | + __u64 cgid; |
| 22 | + } fh = {}; |
| 23 | + |
| 24 | + fh.fh.handle_bytes = sizeof(fh.cgid); |
| 25 | + if (name_to_handle_at(AT_FDCWD, cgroup, &fh.fh, &mount_id, 0)) |
| 26 | + return -1; |
| 27 | + |
| 28 | + return fh.cgid; |
| 29 | +} |
| 30 | + |
| 31 | +static void spin_on_cpu(int seconds) |
| 32 | +{ |
| 33 | + time_t start, now; |
| 34 | + |
| 35 | + start = time(NULL); |
| 36 | + do { |
| 37 | + now = time(NULL); |
| 38 | + } while (now - start < seconds); |
| 39 | +} |
| 40 | + |
| 41 | +static void do_work(const char *cgroup) |
| 42 | +{ |
| 43 | + int i, cpu = 0, pid; |
| 44 | + char cmd[128]; |
| 45 | + |
| 46 | + /* make cgroup threaded */ |
| 47 | + snprintf(cmd, 128, "echo threaded > %s/cgroup.type", cgroup); |
| 48 | + system(cmd); |
| 49 | + |
| 50 | + /* try to enable cpu controller. this may fail if cpu controller is not |
| 51 | + * available in cgroup.controllers or there is a cgroup v1 already |
| 52 | + * mounted in the system. |
| 53 | + */ |
| 54 | + snprintf(cmd, 128, "echo \"+cpu\" > %s/cgroup.subtree_control", cgroup); |
| 55 | + system(cmd); |
| 56 | + |
| 57 | + /* launch two children, both running in child cgroup */ |
| 58 | + for (i = 0; i < 2; ++i) { |
| 59 | + pid = fork(); |
| 60 | + if (pid == 0) { |
| 61 | + /* attach to cgroup */ |
| 62 | + snprintf(cmd, 128, "echo %d > %s/cgroup.procs", getpid(), cgroup); |
| 63 | + system(cmd); |
| 64 | + |
| 65 | + /* pin process to target cpu */ |
| 66 | + snprintf(cmd, 128, "taskset -pc %d %d", cpu, getpid()); |
| 67 | + system(cmd); |
| 68 | + |
| 69 | + spin_on_cpu(3); /* spin on cpu for 3 seconds */ |
| 70 | + exit(0); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + /* pin parent process to target cpu */ |
| 75 | + snprintf(cmd, 128, "taskset -pc %d %d", cpu, getpid()); |
| 76 | + system(cmd); |
| 77 | + |
| 78 | + spin_on_cpu(3); /* spin on cpu for 3 seconds */ |
| 79 | + wait(NULL); |
| 80 | +} |
| 81 | + |
| 82 | +/* Check reading cgroup stats from auto pinned objects |
| 83 | + * @root: root directory in bpffs set up for this test |
| 84 | + * @cgroup: cgroup path |
| 85 | + */ |
| 86 | +static void check_cgroup_stats(const char *root, const char *cgroup) |
| 87 | +{ |
| 88 | + unsigned long queue_self, queue_other; |
| 89 | + char buf[64], path[64]; |
| 90 | + int id, cgroup_id; |
| 91 | + FILE *file; |
| 92 | + |
| 93 | + id = get_cgroup_id(cgroup); |
| 94 | + if (!ASSERT_GE(id, 0, "get_cgroup_id")) |
| 95 | + return; |
| 96 | + |
| 97 | + snprintf(path, sizeof(path), "%s/%d/stats", root, id); |
| 98 | + file = fopen(path, "r"); |
| 99 | + if (!ASSERT_OK_PTR(file, "open")) |
| 100 | + return; |
| 101 | + |
| 102 | + ASSERT_OK_PTR(fgets(buf, sizeof(buf), file), "cat"); |
| 103 | + ASSERT_EQ(sscanf(buf, "cgroup_id: %8d", &cgroup_id), 1, "output"); |
| 104 | + ASSERT_EQ(id, cgroup_id, "cgroup_id"); |
| 105 | + |
| 106 | + ASSERT_OK_PTR(fgets(buf, sizeof(buf), file), "cat"); |
| 107 | + ASSERT_EQ(sscanf(buf, "queue_self: %8lu", &queue_self), 1, "output"); |
| 108 | + |
| 109 | + ASSERT_OK_PTR(fgets(buf, sizeof(buf), file), "cat"); |
| 110 | + ASSERT_EQ(sscanf(buf, "queue_other: %8lu", &queue_other), 1, "output"); |
| 111 | + fclose(file); |
| 112 | +} |
| 113 | + |
| 114 | +/* Set up bpf progs for monitoring cgroup activities. */ |
| 115 | +static void setup_cgroup_monitor(const char *root) |
| 116 | +{ |
| 117 | + struct cgroup_monitor *skel = NULL; |
| 118 | + |
| 119 | + skel = cgroup_monitor__open_and_load(); |
| 120 | + if (!ASSERT_OK_PTR(skel, "cgroup_monitor_skel_load")) |
| 121 | + return; |
| 122 | + |
| 123 | + cgroup_monitor__attach(skel); |
| 124 | + |
| 125 | + snprintf(skel->bss->root, sizeof(skel->bss->root), "%s", root); |
| 126 | + |
| 127 | + snprintf(mkdir_prog_path, 64, "%s/mkdir_prog", root); |
| 128 | + bpf_obj_pin(bpf_link__fd(skel->links.mkdir_prog), mkdir_prog_path); |
| 129 | + |
| 130 | + snprintf(rmdir_prog_path, 64, "%s/rmdir_prog", root); |
| 131 | + bpf_obj_pin(bpf_link__fd(skel->links.rmdir_prog), rmdir_prog_path); |
| 132 | + |
| 133 | + cgroup_monitor__destroy(skel); |
| 134 | +} |
| 135 | + |
| 136 | +void test_cgroup_stats(void) |
| 137 | +{ |
| 138 | + char bpf_tmpl[] = "/sys/fs/bpf/XXXXXX"; |
| 139 | + char cgrp_tmpl[] = "/sys/fs/cgroup/XXXXXX"; |
| 140 | + struct cgroup_sched_lat *skel = NULL; |
| 141 | + char *root, *cgroup; |
| 142 | + |
| 143 | + /* prepare test directories */ |
| 144 | + system("mount -t cgroup2 none /sys/fs/cgroup"); |
| 145 | + system("mount -t bpf bpffs /sys/fs/bpf"); |
| 146 | + root = mkdtemp(bpf_tmpl); |
| 147 | + chmod(root, 0777); |
| 148 | + |
| 149 | + /* set up progs for monitoring cgroup events */ |
| 150 | + setup_cgroup_monitor(root); |
| 151 | + |
| 152 | + /* set up progs for profiling cgroup stats */ |
| 153 | + skel = cgroup_sched_lat__open_and_load(); |
| 154 | + if (!ASSERT_OK_PTR(skel, "cgroup_sched_lat_skel_load")) |
| 155 | + goto cleanup_root; |
| 156 | + |
| 157 | + snprintf(dump_prog_path, 64, "%s/prog", root); |
| 158 | + bpf_obj_pin(bpf_program__fd(skel->progs.dump_cgroup), dump_prog_path); |
| 159 | + chmod(dump_prog_path, 0644); |
| 160 | + |
| 161 | + cgroup_sched_lat__attach(skel); |
| 162 | + |
| 163 | + /* thanks to cgroup monitoring progs, a directory corresponding to the |
| 164 | + * cgroup is created in bpffs. |
| 165 | + */ |
| 166 | + cgroup = mkdtemp(cgrp_tmpl); |
| 167 | + |
| 168 | + /* collect some cgroup-level stats and check reading them from bpffs */ |
| 169 | + do_work(cgroup); |
| 170 | + check_cgroup_stats(root, cgroup); |
| 171 | + |
| 172 | + /* thanks to cgroup monitoring progs, removing cgroups also removes |
| 173 | + * the created directory in bpffs. |
| 174 | + */ |
| 175 | + rmdir(cgroup); |
| 176 | + |
| 177 | + /* clean up cgroup monitoring progs */ |
| 178 | + cgroup_sched_lat__detach(skel); |
| 179 | + cgroup_sched_lat__destroy(skel); |
| 180 | + unlink(dump_prog_path); |
| 181 | +cleanup_root: |
| 182 | + /* remove test directories in bpffs */ |
| 183 | + unlink(mkdir_prog_path); |
| 184 | + unlink(rmdir_prog_path); |
| 185 | + rmdir(root); |
| 186 | + return; |
| 187 | +} |
0 commit comments