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