xref: /OK3568_Linux_fs/kernel/tools/perf/tests/parse-no-sample-id-all.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun #include <linux/kernel.h>
2*4882a593Smuzhiyun #include <linux/types.h>
3*4882a593Smuzhiyun #include <stddef.h>
4*4882a593Smuzhiyun 
5*4882a593Smuzhiyun #include "tests.h"
6*4882a593Smuzhiyun 
7*4882a593Smuzhiyun #include "event.h"
8*4882a593Smuzhiyun #include "evlist.h"
9*4882a593Smuzhiyun #include "header.h"
10*4882a593Smuzhiyun #include "debug.h"
11*4882a593Smuzhiyun 
process_event(struct evlist ** pevlist,union perf_event * event)12*4882a593Smuzhiyun static int process_event(struct evlist **pevlist, union perf_event *event)
13*4882a593Smuzhiyun {
14*4882a593Smuzhiyun 	struct perf_sample sample;
15*4882a593Smuzhiyun 
16*4882a593Smuzhiyun 	if (event->header.type == PERF_RECORD_HEADER_ATTR) {
17*4882a593Smuzhiyun 		if (perf_event__process_attr(NULL, event, pevlist)) {
18*4882a593Smuzhiyun 			pr_debug("perf_event__process_attr failed\n");
19*4882a593Smuzhiyun 			return -1;
20*4882a593Smuzhiyun 		}
21*4882a593Smuzhiyun 		return 0;
22*4882a593Smuzhiyun 	}
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun 	if (event->header.type >= PERF_RECORD_USER_TYPE_START)
25*4882a593Smuzhiyun 		return -1;
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun 	if (!*pevlist)
28*4882a593Smuzhiyun 		return -1;
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun 	if (perf_evlist__parse_sample(*pevlist, event, &sample)) {
31*4882a593Smuzhiyun 		pr_debug("perf_evlist__parse_sample failed\n");
32*4882a593Smuzhiyun 		return -1;
33*4882a593Smuzhiyun 	}
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun 	return 0;
36*4882a593Smuzhiyun }
37*4882a593Smuzhiyun 
process_events(union perf_event ** events,size_t count)38*4882a593Smuzhiyun static int process_events(union perf_event **events, size_t count)
39*4882a593Smuzhiyun {
40*4882a593Smuzhiyun 	struct evlist *evlist = NULL;
41*4882a593Smuzhiyun 	int err = 0;
42*4882a593Smuzhiyun 	size_t i;
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun 	for (i = 0; i < count && !err; i++)
45*4882a593Smuzhiyun 		err = process_event(&evlist, events[i]);
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun 	evlist__delete(evlist);
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun 	return err;
50*4882a593Smuzhiyun }
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun struct test_attr_event {
53*4882a593Smuzhiyun 	struct perf_event_header header;
54*4882a593Smuzhiyun 	struct perf_event_attr	 attr;
55*4882a593Smuzhiyun 	u64 id;
56*4882a593Smuzhiyun };
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun /**
59*4882a593Smuzhiyun  * test__parse_no_sample_id_all - test parsing with no sample_id_all bit set.
60*4882a593Smuzhiyun  *
61*4882a593Smuzhiyun  * This function tests parsing data produced on kernel's that do not support the
62*4882a593Smuzhiyun  * sample_id_all bit.  Without the sample_id_all bit, non-sample events (such as
63*4882a593Smuzhiyun  * mmap events) do not have an id sample appended, and consequently logic
64*4882a593Smuzhiyun  * designed to determine the id will not work.  That case happens when there is
65*4882a593Smuzhiyun  * more than one selected event, so this test processes three events: 2
66*4882a593Smuzhiyun  * attributes representing the selected events and one mmap event.
67*4882a593Smuzhiyun  *
68*4882a593Smuzhiyun  * Return: %0 on success, %-1 if the test fails.
69*4882a593Smuzhiyun  */
test__parse_no_sample_id_all(struct test * test __maybe_unused,int subtest __maybe_unused)70*4882a593Smuzhiyun int test__parse_no_sample_id_all(struct test *test __maybe_unused, int subtest __maybe_unused)
71*4882a593Smuzhiyun {
72*4882a593Smuzhiyun 	int err;
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun 	struct test_attr_event event1 = {
75*4882a593Smuzhiyun 		.header = {
76*4882a593Smuzhiyun 			.type = PERF_RECORD_HEADER_ATTR,
77*4882a593Smuzhiyun 			.size = sizeof(struct test_attr_event),
78*4882a593Smuzhiyun 		},
79*4882a593Smuzhiyun 		.id = 1,
80*4882a593Smuzhiyun 	};
81*4882a593Smuzhiyun 	struct test_attr_event event2 = {
82*4882a593Smuzhiyun 		.header = {
83*4882a593Smuzhiyun 			.type = PERF_RECORD_HEADER_ATTR,
84*4882a593Smuzhiyun 			.size = sizeof(struct test_attr_event),
85*4882a593Smuzhiyun 		},
86*4882a593Smuzhiyun 		.id = 2,
87*4882a593Smuzhiyun 	};
88*4882a593Smuzhiyun 	struct perf_record_mmap event3 = {
89*4882a593Smuzhiyun 		.header = {
90*4882a593Smuzhiyun 			.type = PERF_RECORD_MMAP,
91*4882a593Smuzhiyun 			.size = sizeof(struct perf_record_mmap),
92*4882a593Smuzhiyun 		},
93*4882a593Smuzhiyun 	};
94*4882a593Smuzhiyun 	union perf_event *events[] = {
95*4882a593Smuzhiyun 		(union perf_event *)&event1,
96*4882a593Smuzhiyun 		(union perf_event *)&event2,
97*4882a593Smuzhiyun 		(union perf_event *)&event3,
98*4882a593Smuzhiyun 	};
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun 	err = process_events(events, ARRAY_SIZE(events));
101*4882a593Smuzhiyun 	if (err)
102*4882a593Smuzhiyun 		return -1;
103*4882a593Smuzhiyun 
104*4882a593Smuzhiyun 	return 0;
105*4882a593Smuzhiyun }
106