1 // SPDX-License-Identifier: GPL-2.0
3 * Test backward bit in event attribute, read ring buffer from end to
12 #include "parse-events.h"
13 #include "util/mmap.h"
15 #include <linux/string.h>
16 #include <perf/mmap.h>
20 static void testcase(void)
24 for (i
= 0; i
< NR_ITERS
; i
++) {
27 snprintf(proc_name
, sizeof(proc_name
), "p:%d\n", i
);
28 prctl(PR_SET_NAME
, proc_name
);
32 static int count_samples(struct evlist
*evlist
, int *sample_count
,
37 for (i
= 0; i
< evlist
->core
.nr_mmaps
; i
++) {
38 struct mmap
*map
= &evlist
->overwrite_mmap
[i
];
39 union perf_event
*event
;
41 perf_mmap__read_init(&map
->core
);
42 while ((event
= perf_mmap__read_event(&map
->core
)) != NULL
) {
43 const u32 type
= event
->header
.type
;
46 case PERF_RECORD_SAMPLE
:
49 case PERF_RECORD_COMM
:
53 pr_err("Unexpected record of type %d\n", type
);
57 perf_mmap__read_done(&map
->core
);
62 static int do_test(struct evlist
*evlist
, int mmap_pages
,
63 int *sample_count
, int *comm_count
)
66 char sbuf
[STRERR_BUFSIZE
];
68 err
= evlist__mmap(evlist
, mmap_pages
);
70 pr_debug("evlist__mmap: %s\n",
71 str_error_r(errno
, sbuf
, sizeof(sbuf
)));
75 evlist__enable(evlist
);
77 evlist__disable(evlist
);
79 err
= count_samples(evlist
, sample_count
, comm_count
);
80 evlist__munmap(evlist
);
85 int test__backward_ring_buffer(struct test
*test __maybe_unused
, int subtest __maybe_unused
)
87 int ret
= TEST_SKIP
, err
, sample_count
= 0, comm_count
= 0;
88 char pid
[16], sbuf
[STRERR_BUFSIZE
];
89 struct evlist
*evlist
;
90 struct evsel
*evsel __maybe_unused
;
91 struct parse_events_error parse_error
;
92 struct record_opts opts
= {
99 .default_interval
= 1,
102 snprintf(pid
, sizeof(pid
), "%d", getpid());
103 pid
[sizeof(pid
) - 1] = '\0';
104 opts
.target
.tid
= opts
.target
.pid
= pid
;
106 evlist
= evlist__new();
108 pr_debug("Not enough memory to create evlist\n");
112 err
= evlist__create_maps(evlist
, &opts
.target
);
114 pr_debug("Not enough memory to create thread/cpu maps\n");
115 goto out_delete_evlist
;
118 bzero(&parse_error
, sizeof(parse_error
));
120 * Set backward bit, ring buffer should be writing from end. Record
123 err
= parse_events(evlist
, "syscalls:sys_enter_prctl/overwrite/", &parse_error
);
125 pr_debug("Failed to parse tracepoint event, try use root\n");
127 goto out_delete_evlist
;
130 evlist__config(evlist
, &opts
, NULL
);
132 err
= evlist__open(evlist
);
134 pr_debug("perf_evlist__open: %s\n",
135 str_error_r(errno
, sbuf
, sizeof(sbuf
)));
136 goto out_delete_evlist
;
140 err
= do_test(evlist
, opts
.mmap_pages
, &sample_count
,
143 goto out_delete_evlist
;
145 if ((sample_count
!= NR_ITERS
) || (comm_count
!= NR_ITERS
)) {
146 pr_err("Unexpected counter: sample_count=%d, comm_count=%d\n",
147 sample_count
, comm_count
);
148 goto out_delete_evlist
;
151 evlist__close(evlist
);
153 err
= evlist__open(evlist
);
155 pr_debug("perf_evlist__open: %s\n",
156 str_error_r(errno
, sbuf
, sizeof(sbuf
)));
157 goto out_delete_evlist
;
160 err
= do_test(evlist
, 1, &sample_count
, &comm_count
);
162 goto out_delete_evlist
;
166 evlist__delete(evlist
);