1 #include <linux/err.h>
2 #include <traceevent/event-parse.h>
3 #include "evsel.h"
4 #include "tests.h"
5 #include "debug.h"
6 
7 static int perf_evsel__test_field(struct perf_evsel *evsel, const char *name,
8 				  int size, bool should_be_signed)
9 {
10 	struct format_field *field = perf_evsel__field(evsel, name);
11 	int is_signed;
12 	int ret = 0;
13 
14 	if (field == NULL) {
15 		pr_debug("%s: \"%s\" field not found!\n", evsel->name, name);
16 		return -1;
17 	}
18 
19 	is_signed = !!(field->flags | FIELD_IS_SIGNED);
20 	if (should_be_signed && !is_signed) {
21 		pr_debug("%s: \"%s\" signedness(%d) is wrong, should be %d\n",
22 			 evsel->name, name, is_signed, should_be_signed);
23 		ret = -1;
24 	}
25 
26 	if (field->size != size) {
27 		pr_debug("%s: \"%s\" size (%d) should be %d!\n",
28 			 evsel->name, name, field->size, size);
29 		ret = -1;
30 	}
31 
32 	return ret;
33 }
34 
35 int test__perf_evsel__tp_sched_test(int subtest __maybe_unused)
36 {
37 	struct perf_evsel *evsel = perf_evsel__newtp("sched", "sched_switch");
38 	int ret = 0;
39 
40 	if (IS_ERR(evsel)) {
41 		pr_debug("perf_evsel__newtp failed with %ld\n", PTR_ERR(evsel));
42 		return -1;
43 	}
44 
45 	if (perf_evsel__test_field(evsel, "prev_comm", 16, true))
46 		ret = -1;
47 
48 	if (perf_evsel__test_field(evsel, "prev_pid", 4, true))
49 		ret = -1;
50 
51 	if (perf_evsel__test_field(evsel, "prev_prio", 4, true))
52 		ret = -1;
53 
54 	if (perf_evsel__test_field(evsel, "prev_state", sizeof(long), true))
55 		ret = -1;
56 
57 	if (perf_evsel__test_field(evsel, "next_comm", 16, true))
58 		ret = -1;
59 
60 	if (perf_evsel__test_field(evsel, "next_pid", 4, true))
61 		ret = -1;
62 
63 	if (perf_evsel__test_field(evsel, "next_prio", 4, true))
64 		ret = -1;
65 
66 	perf_evsel__delete(evsel);
67 
68 	evsel = perf_evsel__newtp("sched", "sched_wakeup");
69 
70 	if (IS_ERR(evsel)) {
71 		pr_debug("perf_evsel__newtp failed with %ld\n", PTR_ERR(evsel));
72 		return -1;
73 	}
74 
75 	if (perf_evsel__test_field(evsel, "comm", 16, true))
76 		ret = -1;
77 
78 	if (perf_evsel__test_field(evsel, "pid", 4, true))
79 		ret = -1;
80 
81 	if (perf_evsel__test_field(evsel, "prio", 4, true))
82 		ret = -1;
83 
84 	if (perf_evsel__test_field(evsel, "target_cpu", 4, true))
85 		ret = -1;
86 
87 	return ret;
88 }
89