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