^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Test backward bit in event attribute, read ring buffer from end to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * beginning
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <evlist.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <sys/prctl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include "record.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include "tests.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include "debug.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include "parse-events.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include "util/mmap.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <perf/mmap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #define NR_ITERS 111
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) static void testcase(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) for (i = 0; i < NR_ITERS; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) char proc_name[15];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) snprintf(proc_name, sizeof(proc_name), "p:%d\n", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) prctl(PR_SET_NAME, proc_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) static int count_samples(struct evlist *evlist, int *sample_count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) int *comm_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) for (i = 0; i < evlist->core.nr_mmaps; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) struct mmap *map = &evlist->overwrite_mmap[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) union perf_event *event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) perf_mmap__read_init(&map->core);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) while ((event = perf_mmap__read_event(&map->core)) != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) const u32 type = event->header.type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) switch (type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) case PERF_RECORD_SAMPLE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) (*sample_count)++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) case PERF_RECORD_COMM:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) (*comm_count)++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) pr_err("Unexpected record of type %d\n", type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) return TEST_FAIL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) perf_mmap__read_done(&map->core);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) return TEST_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) static int do_test(struct evlist *evlist, int mmap_pages,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) int *sample_count, int *comm_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) char sbuf[STRERR_BUFSIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) err = evlist__mmap(evlist, mmap_pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) pr_debug("evlist__mmap: %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) str_error_r(errno, sbuf, sizeof(sbuf)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) return TEST_FAIL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) evlist__enable(evlist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) testcase();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) evlist__disable(evlist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) err = count_samples(evlist, sample_count, comm_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) evlist__munmap(evlist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) int test__backward_ring_buffer(struct test *test __maybe_unused, int subtest __maybe_unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) int ret = TEST_SKIP, err, sample_count = 0, comm_count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) char pid[16], sbuf[STRERR_BUFSIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) struct evlist *evlist;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) struct evsel *evsel __maybe_unused;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) struct parse_events_error parse_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) struct record_opts opts = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) .target = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) .uid = UINT_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) .uses_mmap = true,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) .freq = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) .mmap_pages = 256,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) .default_interval = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) snprintf(pid, sizeof(pid), "%d", getpid());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) pid[sizeof(pid) - 1] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) opts.target.tid = opts.target.pid = pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) evlist = evlist__new();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) if (!evlist) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) pr_debug("Not enough memory to create evlist\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) return TEST_FAIL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) err = perf_evlist__create_maps(evlist, &opts.target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) pr_debug("Not enough memory to create thread/cpu maps\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) goto out_delete_evlist;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) bzero(&parse_error, sizeof(parse_error));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) * Set backward bit, ring buffer should be writing from end. Record
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) * it in aux evlist
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) err = parse_events(evlist, "syscalls:sys_enter_prctl/overwrite/", &parse_error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) pr_debug("Failed to parse tracepoint event, try use root\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) ret = TEST_SKIP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) goto out_delete_evlist;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) perf_evlist__config(evlist, &opts, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) err = evlist__open(evlist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) pr_debug("perf_evlist__open: %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) str_error_r(errno, sbuf, sizeof(sbuf)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) goto out_delete_evlist;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) ret = TEST_FAIL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) err = do_test(evlist, opts.mmap_pages, &sample_count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) &comm_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) if (err != TEST_OK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) goto out_delete_evlist;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) if ((sample_count != NR_ITERS) || (comm_count != NR_ITERS)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) pr_err("Unexpected counter: sample_count=%d, comm_count=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) sample_count, comm_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) goto out_delete_evlist;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) evlist__close(evlist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) err = evlist__open(evlist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) pr_debug("perf_evlist__open: %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) str_error_r(errno, sbuf, sizeof(sbuf)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) goto out_delete_evlist;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) err = do_test(evlist, 1, &sample_count, &comm_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) if (err != TEST_OK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) goto out_delete_evlist;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) ret = TEST_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) out_delete_evlist:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) evlist__delete(evlist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) }