Merge tag 'block-6.1-2022-10-20' of git://git.kernel.dk/linux
[linux-block.git] / tools / perf / tests / event_update.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
a6e52817 2#include <linux/compiler.h>
9c3516d1 3#include <perf/cpumap.h>
f2a39fe8 4#include <string.h>
87ffb6c6 5#include "cpumap.h"
a6e52817
JO
6#include "evlist.h"
7#include "evsel.h"
f2a39fe8 8#include "header.h"
a6e52817 9#include "machine.h"
ea49e01c 10#include "util/synthetic-events.h"
4a3cec84 11#include "tool.h"
a6e52817
JO
12#include "tests.h"
13#include "debug.h"
14
15static int process_event_unit(struct perf_tool *tool __maybe_unused,
16 union perf_event *event,
17 struct perf_sample *sample __maybe_unused,
18 struct machine *machine __maybe_unused)
19{
72932371 20 struct perf_record_event_update *ev = (struct perf_record_event_update *)event;
a6e52817
JO
21
22 TEST_ASSERT_VAL("wrong id", ev->id == 123);
23 TEST_ASSERT_VAL("wrong id", ev->type == PERF_EVENT_UPDATE__UNIT);
d773c999 24 TEST_ASSERT_VAL("wrong unit", !strcmp(ev->unit, "KRAVA"));
a6e52817
JO
25 return 0;
26}
27
daeecbc0
JO
28static int process_event_scale(struct perf_tool *tool __maybe_unused,
29 union perf_event *event,
30 struct perf_sample *sample __maybe_unused,
31 struct machine *machine __maybe_unused)
32{
72932371 33 struct perf_record_event_update *ev = (struct perf_record_event_update *)event;
daeecbc0
JO
34
35 TEST_ASSERT_VAL("wrong id", ev->id == 123);
36 TEST_ASSERT_VAL("wrong id", ev->type == PERF_EVENT_UPDATE__SCALE);
d773c999 37 TEST_ASSERT_VAL("wrong scale", ev->scale.scale == 0.123);
daeecbc0
JO
38 return 0;
39}
40
802c9048
JO
41struct event_name {
42 struct perf_tool tool;
43 const char *name;
44};
45
46static int process_event_name(struct perf_tool *tool,
47 union perf_event *event,
48 struct perf_sample *sample __maybe_unused,
49 struct machine *machine __maybe_unused)
50{
51 struct event_name *tmp = container_of(tool, struct event_name, tool);
72932371 52 struct perf_record_event_update *ev = (struct perf_record_event_update *)event;
802c9048
JO
53
54 TEST_ASSERT_VAL("wrong id", ev->id == 123);
55 TEST_ASSERT_VAL("wrong id", ev->type == PERF_EVENT_UPDATE__NAME);
d773c999 56 TEST_ASSERT_VAL("wrong name", !strcmp(ev->name, tmp->name));
802c9048
JO
57 return 0;
58}
59
86ebb09f
JO
60static int process_event_cpus(struct perf_tool *tool __maybe_unused,
61 union perf_event *event,
62 struct perf_sample *sample __maybe_unused,
63 struct machine *machine __maybe_unused)
64{
72932371 65 struct perf_record_event_update *ev = (struct perf_record_event_update *)event;
f854839b 66 struct perf_cpu_map *map;
86ebb09f 67
d773c999 68 map = cpu_map__new_data(&ev->cpus.cpus);
86ebb09f
JO
69
70 TEST_ASSERT_VAL("wrong id", ev->id == 123);
71 TEST_ASSERT_VAL("wrong type", ev->type == PERF_EVENT_UPDATE__CPUS);
44028699
IR
72 TEST_ASSERT_VAL("wrong cpus", perf_cpu_map__nr(map) == 3);
73 TEST_ASSERT_VAL("wrong cpus", perf_cpu_map__cpu(map, 0).cpu == 1);
74 TEST_ASSERT_VAL("wrong cpus", perf_cpu_map__cpu(map, 1).cpu == 2);
75 TEST_ASSERT_VAL("wrong cpus", perf_cpu_map__cpu(map, 2).cpu == 3);
38f01d8d 76 perf_cpu_map__put(map);
86ebb09f
JO
77 return 0;
78}
79
33f44bfd 80static int test__event_update(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
a6e52817 81{
32dcd021 82 struct evsel *evsel;
802c9048 83 struct event_name tmp;
606e2c29 84 struct evlist *evlist = evlist__new_default();
a6e52817 85
a6e52817
JO
86 TEST_ASSERT_VAL("failed to get evlist", evlist);
87
515dbe48 88 evsel = evlist__first(evlist);
a6e52817 89
ce095c9a 90 TEST_ASSERT_VAL("failed to allocate ids",
70c20369 91 !perf_evsel__alloc_id(&evsel->core, 1, 1));
a6e52817 92
b0031c22 93 perf_evlist__id_add(&evlist->core, &evsel->core, 0, 0, 123);
a6e52817 94
b194c9cd
IR
95 free((char *)evsel->unit);
96 evsel->unit = strdup("KRAVA");
a6e52817
JO
97
98 TEST_ASSERT_VAL("failed to synthesize attr update unit",
99 !perf_event__synthesize_event_update_unit(NULL, evsel, process_event_unit));
100
daeecbc0
JO
101 evsel->scale = 0.123;
102
103 TEST_ASSERT_VAL("failed to synthesize attr update scale",
104 !perf_event__synthesize_event_update_scale(NULL, evsel, process_event_scale));
105
8ab2e96d 106 tmp.name = evsel__name(evsel);
802c9048
JO
107
108 TEST_ASSERT_VAL("failed to synthesize attr update name",
109 !perf_event__synthesize_event_update_name(&tmp.tool, evsel, process_event_name));
110
fe1f61b3 111 evsel->core.own_cpus = perf_cpu_map__new("1,2,3");
86ebb09f
JO
112
113 TEST_ASSERT_VAL("failed to synthesize attr update cpus",
114 !perf_event__synthesize_event_update_cpus(&tmp.tool, evsel, process_event_cpus));
115
fc56f54f 116 evlist__delete(evlist);
a6e52817
JO
117 return 0;
118}
d68f0365
IR
119
120DEFINE_SUITE("Synthesize attr update", event_update);