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