xref: /linux/tools/perf/tests/event_update.c (revision fe1f61b37ffada9fc7ec2c9d4ca5376b5a797dbc)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2a6e52817SJiri Olsa #include <linux/compiler.h>
39c3516d1SJiri Olsa #include <perf/cpumap.h>
4a6e52817SJiri Olsa #include "evlist.h"
5a6e52817SJiri Olsa #include "evsel.h"
6a6e52817SJiri Olsa #include "machine.h"
7a6e52817SJiri Olsa #include "tests.h"
8a6e52817SJiri Olsa #include "debug.h"
9a6e52817SJiri Olsa 
10a6e52817SJiri Olsa static int process_event_unit(struct perf_tool *tool __maybe_unused,
11a6e52817SJiri Olsa 			      union perf_event *event,
12a6e52817SJiri Olsa 			      struct perf_sample *sample __maybe_unused,
13a6e52817SJiri Olsa 			      struct machine *machine __maybe_unused)
14a6e52817SJiri Olsa {
15a6e52817SJiri Olsa 	struct event_update_event *ev = (struct event_update_event *) event;
16a6e52817SJiri Olsa 
17a6e52817SJiri Olsa 	TEST_ASSERT_VAL("wrong id", ev->id == 123);
18a6e52817SJiri Olsa 	TEST_ASSERT_VAL("wrong id", ev->type == PERF_EVENT_UPDATE__UNIT);
19a6e52817SJiri Olsa 	TEST_ASSERT_VAL("wrong unit", !strcmp(ev->data, "KRAVA"));
20a6e52817SJiri Olsa 	return 0;
21a6e52817SJiri Olsa }
22a6e52817SJiri Olsa 
23daeecbc0SJiri Olsa static int process_event_scale(struct perf_tool *tool __maybe_unused,
24daeecbc0SJiri Olsa 			       union perf_event *event,
25daeecbc0SJiri Olsa 			       struct perf_sample *sample __maybe_unused,
26daeecbc0SJiri Olsa 			       struct machine *machine __maybe_unused)
27daeecbc0SJiri Olsa {
28daeecbc0SJiri Olsa 	struct event_update_event *ev = (struct event_update_event *) event;
29daeecbc0SJiri Olsa 	struct event_update_event_scale *ev_data;
30daeecbc0SJiri Olsa 
31daeecbc0SJiri Olsa 	ev_data = (struct event_update_event_scale *) ev->data;
32daeecbc0SJiri Olsa 
33daeecbc0SJiri Olsa 	TEST_ASSERT_VAL("wrong id", ev->id == 123);
34daeecbc0SJiri Olsa 	TEST_ASSERT_VAL("wrong id", ev->type == PERF_EVENT_UPDATE__SCALE);
358daef508SColin Ian King 	TEST_ASSERT_VAL("wrong scale", ev_data->scale == 0.123);
36daeecbc0SJiri Olsa 	return 0;
37daeecbc0SJiri Olsa }
38daeecbc0SJiri Olsa 
39802c9048SJiri Olsa struct event_name {
40802c9048SJiri Olsa 	struct perf_tool tool;
41802c9048SJiri Olsa 	const char *name;
42802c9048SJiri Olsa };
43802c9048SJiri Olsa 
44802c9048SJiri Olsa static int process_event_name(struct perf_tool *tool,
45802c9048SJiri Olsa 			      union perf_event *event,
46802c9048SJiri Olsa 			      struct perf_sample *sample __maybe_unused,
47802c9048SJiri Olsa 			      struct machine *machine __maybe_unused)
48802c9048SJiri Olsa {
49802c9048SJiri Olsa 	struct event_name *tmp = container_of(tool, struct event_name, tool);
50802c9048SJiri Olsa 	struct event_update_event *ev = (struct event_update_event*) event;
51802c9048SJiri Olsa 
52802c9048SJiri Olsa 	TEST_ASSERT_VAL("wrong id", ev->id == 123);
53802c9048SJiri Olsa 	TEST_ASSERT_VAL("wrong id", ev->type == PERF_EVENT_UPDATE__NAME);
54802c9048SJiri Olsa 	TEST_ASSERT_VAL("wrong name", !strcmp(ev->data, tmp->name));
55802c9048SJiri Olsa 	return 0;
56802c9048SJiri Olsa }
57802c9048SJiri Olsa 
5886ebb09fSJiri Olsa static int process_event_cpus(struct perf_tool *tool __maybe_unused,
5986ebb09fSJiri Olsa 			      union perf_event *event,
6086ebb09fSJiri Olsa 			      struct perf_sample *sample __maybe_unused,
6186ebb09fSJiri Olsa 			      struct machine *machine __maybe_unused)
6286ebb09fSJiri Olsa {
6386ebb09fSJiri Olsa 	struct event_update_event *ev = (struct event_update_event*) event;
6486ebb09fSJiri Olsa 	struct event_update_event_cpus *ev_data;
65f854839bSJiri Olsa 	struct perf_cpu_map *map;
6686ebb09fSJiri Olsa 
6786ebb09fSJiri Olsa 	ev_data = (struct event_update_event_cpus*) ev->data;
6886ebb09fSJiri Olsa 
6986ebb09fSJiri Olsa 	map = cpu_map__new_data(&ev_data->cpus);
7086ebb09fSJiri Olsa 
7186ebb09fSJiri Olsa 	TEST_ASSERT_VAL("wrong id", ev->id == 123);
7286ebb09fSJiri Olsa 	TEST_ASSERT_VAL("wrong type", ev->type == PERF_EVENT_UPDATE__CPUS);
7386ebb09fSJiri Olsa 	TEST_ASSERT_VAL("wrong cpus", map->nr == 3);
7486ebb09fSJiri Olsa 	TEST_ASSERT_VAL("wrong cpus", map->map[0] == 1);
7586ebb09fSJiri Olsa 	TEST_ASSERT_VAL("wrong cpus", map->map[1] == 2);
7686ebb09fSJiri Olsa 	TEST_ASSERT_VAL("wrong cpus", map->map[2] == 3);
7738f01d8dSJiri Olsa 	perf_cpu_map__put(map);
7886ebb09fSJiri Olsa 	return 0;
7986ebb09fSJiri Olsa }
8086ebb09fSJiri Olsa 
8181f17c90SArnaldo Carvalho de Melo int test__event_update(struct test *test __maybe_unused, int subtest __maybe_unused)
82a6e52817SJiri Olsa {
8363503dbaSJiri Olsa 	struct evlist *evlist;
8432dcd021SJiri Olsa 	struct evsel *evsel;
85802c9048SJiri Olsa 	struct event_name tmp;
86a6e52817SJiri Olsa 
87a6e52817SJiri Olsa 	evlist = perf_evlist__new_default();
88a6e52817SJiri Olsa 	TEST_ASSERT_VAL("failed to get evlist", evlist);
89a6e52817SJiri Olsa 
90a6e52817SJiri Olsa 	evsel = perf_evlist__first(evlist);
91a6e52817SJiri Olsa 
92a6e52817SJiri Olsa 	TEST_ASSERT_VAL("failed to allos ids",
93a6e52817SJiri Olsa 			!perf_evsel__alloc_id(evsel, 1, 1));
94a6e52817SJiri Olsa 
95a6e52817SJiri Olsa 	perf_evlist__id_add(evlist, evsel, 0, 0, 123);
96a6e52817SJiri Olsa 
97a6e52817SJiri Olsa 	evsel->unit = strdup("KRAVA");
98a6e52817SJiri Olsa 
99a6e52817SJiri Olsa 	TEST_ASSERT_VAL("failed to synthesize attr update unit",
100a6e52817SJiri Olsa 			!perf_event__synthesize_event_update_unit(NULL, evsel, process_event_unit));
101a6e52817SJiri Olsa 
102daeecbc0SJiri Olsa 	evsel->scale = 0.123;
103daeecbc0SJiri Olsa 
104daeecbc0SJiri Olsa 	TEST_ASSERT_VAL("failed to synthesize attr update scale",
105daeecbc0SJiri Olsa 			!perf_event__synthesize_event_update_scale(NULL, evsel, process_event_scale));
106daeecbc0SJiri Olsa 
107802c9048SJiri Olsa 	tmp.name = perf_evsel__name(evsel);
108802c9048SJiri Olsa 
109802c9048SJiri Olsa 	TEST_ASSERT_VAL("failed to synthesize attr update name",
110802c9048SJiri Olsa 			!perf_event__synthesize_event_update_name(&tmp.tool, evsel, process_event_name));
111802c9048SJiri Olsa 
112*fe1f61b3SJiri Olsa 	evsel->core.own_cpus = perf_cpu_map__new("1,2,3");
11386ebb09fSJiri Olsa 
11486ebb09fSJiri Olsa 	TEST_ASSERT_VAL("failed to synthesize attr update cpus",
11586ebb09fSJiri Olsa 			!perf_event__synthesize_event_update_cpus(&tmp.tool, evsel, process_event_cpus));
11686ebb09fSJiri Olsa 
117*fe1f61b3SJiri Olsa 	perf_cpu_map__put(evsel->core.own_cpus);
118a6e52817SJiri Olsa 	return 0;
119a6e52817SJiri Olsa }
120