|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | +/* Copyright (c) 2024 Yafang Shao <[email protected]> */ |
| 3 | + |
| 4 | +#define _GNU_SOURCE |
| 5 | +#include <sched.h> |
| 6 | +#include <stdio.h> |
| 7 | +#include <unistd.h> |
| 8 | + |
| 9 | +#include <test_progs.h> |
| 10 | +#include "cgroup_helpers.h" |
| 11 | +#include "test_cpumask_iter.skel.h" |
| 12 | + |
| 13 | +static void verify_percpu_data(struct bpf_link *link, int nr_cpu_exp, int nr_running_exp) |
| 14 | +{ |
| 15 | + int iter_fd, len, item, nr_running, psi_running, nr_cpus; |
| 16 | + char buf[128]; |
| 17 | + size_t left; |
| 18 | + char *p; |
| 19 | + |
| 20 | + iter_fd = bpf_iter_create(bpf_link__fd(link)); |
| 21 | + if (!ASSERT_GE(iter_fd, 0, "iter_fd")) |
| 22 | + return; |
| 23 | + |
| 24 | + memset(buf, 0, sizeof(buf)); |
| 25 | + left = ARRAY_SIZE(buf); |
| 26 | + p = buf; |
| 27 | + while ((len = read(iter_fd, p, left)) > 0) { |
| 28 | + p += len; |
| 29 | + left -= len; |
| 30 | + } |
| 31 | + |
| 32 | + item = sscanf(buf, "nr_running %u nr_cpus %u psi_running %u\n", |
| 33 | + &nr_running, &nr_cpus, &psi_running); |
| 34 | + if (nr_cpu_exp == -1) { |
| 35 | + ASSERT_EQ(item, -1, "seq_format"); |
| 36 | + goto out; |
| 37 | + } |
| 38 | + |
| 39 | + ASSERT_EQ(item, 3, "seq_format"); |
| 40 | + ASSERT_GE(nr_running, nr_running_exp, "nr_running"); |
| 41 | + ASSERT_GE(psi_running, nr_running_exp, "psi_running"); |
| 42 | + ASSERT_EQ(nr_cpus, nr_cpu_exp, "nr_cpus"); |
| 43 | + |
| 44 | +out: |
| 45 | + close(iter_fd); |
| 46 | +} |
| 47 | + |
| 48 | +void test_cpumask_iter(void) |
| 49 | +{ |
| 50 | + DECLARE_LIBBPF_OPTS(bpf_iter_attach_opts, opts); |
| 51 | + int nr_possible, cgrp_fd, pid, err, cnt, i; |
| 52 | + struct test_cpumask_iter *skel; |
| 53 | + union bpf_iter_link_info linfo; |
| 54 | + int cpu_ids[] = {1, 3, 4, 5}; |
| 55 | + struct bpf_link *link; |
| 56 | + cpu_set_t set; |
| 57 | + |
| 58 | + skel = test_cpumask_iter__open_and_load(); |
| 59 | + if (!ASSERT_OK_PTR(skel, "test_for_each_cpu__open_and_load")) |
| 60 | + return; |
| 61 | + |
| 62 | + if (setup_cgroup_environment()) |
| 63 | + goto destroy; |
| 64 | + |
| 65 | + /* Utilize the cgroup iter */ |
| 66 | + cgrp_fd = get_root_cgroup(); |
| 67 | + if (!ASSERT_GE(cgrp_fd, 0, "create cgrp")) |
| 68 | + goto cleanup; |
| 69 | + |
| 70 | + memset(&linfo, 0, sizeof(linfo)); |
| 71 | + linfo.cgroup.cgroup_fd = cgrp_fd; |
| 72 | + linfo.cgroup.order = BPF_CGROUP_ITER_SELF_ONLY; |
| 73 | + opts.link_info = &linfo; |
| 74 | + opts.link_info_len = sizeof(linfo); |
| 75 | + |
| 76 | + link = bpf_program__attach_iter(skel->progs.cpu_cgroup, &opts); |
| 77 | + if (!ASSERT_OK_PTR(link, "attach_iter")) |
| 78 | + goto close_fd; |
| 79 | + |
| 80 | + skel->bss->target_pid = 1; |
| 81 | + /* In case init task is set CPU affinity */ |
| 82 | + err = sched_getaffinity(1, sizeof(set), &set); |
| 83 | + if (!ASSERT_OK(err, "setaffinity")) |
| 84 | + goto free_link; |
| 85 | + |
| 86 | + cnt = CPU_COUNT(&set); |
| 87 | + nr_possible = bpf_num_possible_cpus(); |
| 88 | + if (test__start_subtest("init_pid")) |
| 89 | + /* current task is running. */ |
| 90 | + verify_percpu_data(link, cnt, cnt == nr_possible ? 1 : 0); |
| 91 | + |
| 92 | + skel->bss->target_pid = -1; |
| 93 | + if (test__start_subtest("invalid_pid")) |
| 94 | + verify_percpu_data(link, -1, -1); |
| 95 | + |
| 96 | + pid = getpid(); |
| 97 | + skel->bss->target_pid = pid; |
| 98 | + CPU_ZERO(&set); |
| 99 | + CPU_SET(0, &set); |
| 100 | + err = sched_setaffinity(pid, sizeof(set), &set); |
| 101 | + if (!ASSERT_OK(err, "setaffinity")) |
| 102 | + goto free_link; |
| 103 | + |
| 104 | + if (test__start_subtest("self_pid_one_cpu")) |
| 105 | + verify_percpu_data(link, 1, 1); |
| 106 | + |
| 107 | + /* Assume there are at least 8 CPUs on the testbed */ |
| 108 | + if (nr_possible < 8) |
| 109 | + goto free_link; |
| 110 | + |
| 111 | + CPU_ZERO(&set); |
| 112 | + /* Set the CPU affinitiy: 1,3-5 */ |
| 113 | + for (i = 0; i < ARRAY_SIZE(cpu_ids); i++) |
| 114 | + CPU_SET(cpu_ids[i], &set); |
| 115 | + err = sched_setaffinity(pid, sizeof(set), &set); |
| 116 | + if (!ASSERT_OK(err, "setaffinity")) |
| 117 | + goto free_link; |
| 118 | + |
| 119 | + if (test__start_subtest("self_pid_multi_cpus")) |
| 120 | + verify_percpu_data(link, ARRAY_SIZE(cpu_ids), 1); |
| 121 | + |
| 122 | +free_link: |
| 123 | + bpf_link__destroy(link); |
| 124 | +close_fd: |
| 125 | + close(cgrp_fd); |
| 126 | +cleanup: |
| 127 | + cleanup_cgroup_environment(); |
| 128 | +destroy: |
| 129 | + test_cpumask_iter__destroy(skel); |
| 130 | +} |
0 commit comments