xref: /linux/tools/perf/tests/event_update.c (revision 5ea5880764cbb164afb17a62e76ca75dc371409d)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/compiler.h>
3 #include <perf/cpumap.h>
4 #include <string.h>
5 #include "cpumap.h"
6 #include "evlist.h"
7 #include "evsel.h"
8 #include "header.h"
9 #include "machine.h"
10 #include "util/synthetic-events.h"
11 #include "target.h"
12 #include "tool.h"
13 #include "tests.h"
14 #include "debug.h"
15 
16 static int process_event_unit(const struct perf_tool *tool __maybe_unused,
17 			      union perf_event *event,
18 			      struct perf_sample *sample __maybe_unused,
19 			      struct machine *machine __maybe_unused)
20 {
21 	struct perf_record_event_update *ev = (struct perf_record_event_update *)event;
22 
23 	TEST_ASSERT_VAL("wrong id", ev->id == 123);
24 	TEST_ASSERT_VAL("wrong id", ev->type == PERF_EVENT_UPDATE__UNIT);
25 	TEST_ASSERT_VAL("wrong unit", !strcmp(ev->unit, "KRAVA"));
26 	return 0;
27 }
28 
29 static int process_event_scale(const struct perf_tool *tool __maybe_unused,
30 			       union perf_event *event,
31 			       struct perf_sample *sample __maybe_unused,
32 			       struct machine *machine __maybe_unused)
33 {
34 	struct perf_record_event_update *ev = (struct perf_record_event_update *)event;
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->scale.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(const 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->name, tmp->name));
58 	return 0;
59 }
60 
61 static int process_event_cpus(const 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_cpu_map *map;
68 
69 	map = cpu_map__new_data(&ev->cpus.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", perf_cpu_map__nr(map) == 3);
74 	TEST_ASSERT_VAL("wrong cpus", perf_cpu_map__cpu(map, 0).cpu == 1);
75 	TEST_ASSERT_VAL("wrong cpus", perf_cpu_map__cpu(map, 1).cpu == 2);
76 	TEST_ASSERT_VAL("wrong cpus", perf_cpu_map__cpu(map, 2).cpu == 3);
77 	perf_cpu_map__put(map);
78 	return 0;
79 }
80 
81 static int test__event_update(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
82 {
83 	struct evsel *evsel;
84 	struct event_name tmp;
85 	struct target target = {};
86 	struct evlist *evlist = evlist__new_default(&target, /*sample_callchains=*/false);
87 
88 	TEST_ASSERT_VAL("failed to get evlist", evlist);
89 
90 	evsel = evlist__first(evlist);
91 
92 	TEST_ASSERT_VAL("failed to allocate ids",
93 			!perf_evsel__alloc_id(&evsel->core, 1, 1));
94 
95 	perf_evlist__id_add(&evlist->core, &evsel->core, 0, 0, 123);
96 
97 	free((char *)evsel->unit);
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 	perf_tool__init(&tmp.tool, /*ordered_events=*/false);
109 	tmp.name = evsel__name(evsel);
110 
111 	TEST_ASSERT_VAL("failed to synthesize attr update name",
112 			!perf_event__synthesize_event_update_name(&tmp.tool, evsel, process_event_name));
113 
114 	perf_cpu_map__put(evsel->core.pmu_cpus);
115 	evsel->core.pmu_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 	evlist__delete(evlist);
121 	return 0;
122 }
123 
124 DEFINE_SUITE("Synthesize attr update", event_update);
125