xref: /openbmc/linux/tools/perf/tests/event_update.c (revision 6355592e)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/compiler.h>
3 #include <perf/cpumap.h>
4 #include <string.h>
5 #include "evlist.h"
6 #include "evsel.h"
7 #include "header.h"
8 #include "machine.h"
9 #include "tool.h"
10 #include "tests.h"
11 #include "debug.h"
12 
13 static int process_event_unit(struct perf_tool *tool __maybe_unused,
14 			      union perf_event *event,
15 			      struct perf_sample *sample __maybe_unused,
16 			      struct machine *machine __maybe_unused)
17 {
18 	struct perf_record_event_update *ev = (struct perf_record_event_update *)event;
19 
20 	TEST_ASSERT_VAL("wrong id", ev->id == 123);
21 	TEST_ASSERT_VAL("wrong id", ev->type == PERF_EVENT_UPDATE__UNIT);
22 	TEST_ASSERT_VAL("wrong unit", !strcmp(ev->data, "KRAVA"));
23 	return 0;
24 }
25 
26 static int process_event_scale(struct perf_tool *tool __maybe_unused,
27 			       union perf_event *event,
28 			       struct perf_sample *sample __maybe_unused,
29 			       struct machine *machine __maybe_unused)
30 {
31 	struct perf_record_event_update *ev = (struct perf_record_event_update *)event;
32 	struct perf_record_event_update_scale *ev_data;
33 
34 	ev_data = (struct perf_record_event_update_scale *)ev->data;
35 
36 	TEST_ASSERT_VAL("wrong id", ev->id == 123);
37 	TEST_ASSERT_VAL("wrong id", ev->type == PERF_EVENT_UPDATE__SCALE);
38 	TEST_ASSERT_VAL("wrong scale", ev_data->scale == 0.123);
39 	return 0;
40 }
41 
42 struct event_name {
43 	struct perf_tool tool;
44 	const char *name;
45 };
46 
47 static int process_event_name(struct perf_tool *tool,
48 			      union perf_event *event,
49 			      struct perf_sample *sample __maybe_unused,
50 			      struct machine *machine __maybe_unused)
51 {
52 	struct event_name *tmp = container_of(tool, struct event_name, tool);
53 	struct perf_record_event_update *ev = (struct perf_record_event_update *)event;
54 
55 	TEST_ASSERT_VAL("wrong id", ev->id == 123);
56 	TEST_ASSERT_VAL("wrong id", ev->type == PERF_EVENT_UPDATE__NAME);
57 	TEST_ASSERT_VAL("wrong name", !strcmp(ev->data, tmp->name));
58 	return 0;
59 }
60 
61 static int process_event_cpus(struct perf_tool *tool __maybe_unused,
62 			      union perf_event *event,
63 			      struct perf_sample *sample __maybe_unused,
64 			      struct machine *machine __maybe_unused)
65 {
66 	struct perf_record_event_update *ev = (struct perf_record_event_update *)event;
67 	struct perf_record_event_update_cpus *ev_data;
68 	struct perf_cpu_map *map;
69 
70 	ev_data = (struct perf_record_event_update_cpus *) ev->data;
71 
72 	map = cpu_map__new_data(&ev_data->cpus);
73 
74 	TEST_ASSERT_VAL("wrong id", ev->id == 123);
75 	TEST_ASSERT_VAL("wrong type", ev->type == PERF_EVENT_UPDATE__CPUS);
76 	TEST_ASSERT_VAL("wrong cpus", map->nr == 3);
77 	TEST_ASSERT_VAL("wrong cpus", map->map[0] == 1);
78 	TEST_ASSERT_VAL("wrong cpus", map->map[1] == 2);
79 	TEST_ASSERT_VAL("wrong cpus", map->map[2] == 3);
80 	perf_cpu_map__put(map);
81 	return 0;
82 }
83 
84 int test__event_update(struct test *test __maybe_unused, int subtest __maybe_unused)
85 {
86 	struct evlist *evlist;
87 	struct evsel *evsel;
88 	struct event_name tmp;
89 
90 	evlist = perf_evlist__new_default();
91 	TEST_ASSERT_VAL("failed to get evlist", evlist);
92 
93 	evsel = perf_evlist__first(evlist);
94 
95 	TEST_ASSERT_VAL("failed to allos ids",
96 			!perf_evsel__alloc_id(evsel, 1, 1));
97 
98 	perf_evlist__id_add(evlist, evsel, 0, 0, 123);
99 
100 	evsel->unit = strdup("KRAVA");
101 
102 	TEST_ASSERT_VAL("failed to synthesize attr update unit",
103 			!perf_event__synthesize_event_update_unit(NULL, evsel, process_event_unit));
104 
105 	evsel->scale = 0.123;
106 
107 	TEST_ASSERT_VAL("failed to synthesize attr update scale",
108 			!perf_event__synthesize_event_update_scale(NULL, evsel, process_event_scale));
109 
110 	tmp.name = perf_evsel__name(evsel);
111 
112 	TEST_ASSERT_VAL("failed to synthesize attr update name",
113 			!perf_event__synthesize_event_update_name(&tmp.tool, evsel, process_event_name));
114 
115 	evsel->core.own_cpus = perf_cpu_map__new("1,2,3");
116 
117 	TEST_ASSERT_VAL("failed to synthesize attr update cpus",
118 			!perf_event__synthesize_event_update_cpus(&tmp.tool, evsel, process_event_cpus));
119 
120 	perf_cpu_map__put(evsel->core.own_cpus);
121 	return 0;
122 }
123