|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | +#include <test_progs.h> |
| 3 | +#include "wq_iter.skel.h" |
| 4 | +#include "wq_iter_fail.skel.h" |
| 5 | + |
| 6 | +static void subtest_open_coded(struct wq_iter *skel) |
| 7 | +{ |
| 8 | + LIBBPF_OPTS(bpf_test_run_opts, opts); |
| 9 | + int err; |
| 10 | + |
| 11 | + err = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.count_workqueues), |
| 12 | + &opts); |
| 13 | + if (!ASSERT_OK(err, "run count_workqueues")) |
| 14 | + return; |
| 15 | + /* There are always several system workqueues (events, ...). */ |
| 16 | + ASSERT_GT(skel->bss->nr_workqueues, 0, "nr_workqueues"); |
| 17 | + |
| 18 | + err = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.count_worker_pools), |
| 19 | + &opts); |
| 20 | + if (!ASSERT_OK(err, "run count_worker_pools")) |
| 21 | + return; |
| 22 | + /* At least the per-CPU normal/highpri pools exist. */ |
| 23 | + ASSERT_GT(skel->bss->nr_worker_pools, 0, "nr_worker_pools"); |
| 24 | + ASSERT_GT(skel->bss->nr_percpu_pools, 0, "nr_percpu_pools"); |
| 25 | +} |
| 26 | + |
| 27 | +static void drain_iter(struct bpf_program *prog, const char *name) |
| 28 | +{ |
| 29 | + struct bpf_link *link; |
| 30 | + char buf[512]; |
| 31 | + int iter_fd; |
| 32 | + ssize_t n; |
| 33 | + |
| 34 | + link = bpf_program__attach_iter(prog, NULL); |
| 35 | + if (!ASSERT_OK_PTR(link, name)) |
| 36 | + return; |
| 37 | + iter_fd = bpf_iter_create(bpf_link__fd(link)); |
| 38 | + if (!ASSERT_GE(iter_fd, 0, "iter_create")) |
| 39 | + goto out; |
| 40 | + while ((n = read(iter_fd, buf, sizeof(buf))) > 0) |
| 41 | + ; |
| 42 | + ASSERT_GE(n, 0, "read iter"); |
| 43 | + close(iter_fd); |
| 44 | +out: |
| 45 | + bpf_link__destroy(link); |
| 46 | +} |
| 47 | + |
| 48 | +static void subtest_seq(struct wq_iter *skel) |
| 49 | +{ |
| 50 | + /* seq forms are backed by the same open-coded next(). */ |
| 51 | + drain_iter(skel->progs.dump_workqueues, "attach workqueue iter"); |
| 52 | + ASSERT_GT(skel->bss->nr_wq_seq, 0, "nr_wq_seq"); |
| 53 | + |
| 54 | + drain_iter(skel->progs.dump_worker_pools, "attach worker_pool iter"); |
| 55 | + ASSERT_GT(skel->bss->nr_pool_seq, 0, "nr_pool_seq"); |
| 56 | + |
| 57 | + /* |
| 58 | + * worklists are usually empty on an idle system; this drives the whole |
| 59 | + * per-pool snapshot path and verifies it drains cleanly. |
| 60 | + */ |
| 61 | + drain_iter(skel->progs.dump_pending, "attach pending_work iter"); |
| 62 | +} |
| 63 | + |
| 64 | +void test_wq_iter(void) |
| 65 | +{ |
| 66 | + struct wq_iter *skel; |
| 67 | + |
| 68 | + skel = wq_iter__open_and_load(); |
| 69 | + if (!ASSERT_OK_PTR(skel, "wq_iter__open_and_load")) |
| 70 | + return; |
| 71 | + |
| 72 | + if (test__start_subtest("open_coded")) |
| 73 | + subtest_open_coded(skel); |
| 74 | + if (test__start_subtest("seq")) |
| 75 | + subtest_seq(skel); |
| 76 | + |
| 77 | + wq_iter__destroy(skel); |
| 78 | + |
| 79 | + RUN_TESTS(wq_iter_fail); |
| 80 | +} |
0 commit comments