blob: 44a50527f9d95170f176d4e5abbe70fdadd53a0a [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Jiri Olsaa6e52812015-10-25 15:51:37 +01002#include <linux/compiler.h>
Jiri Olsa9c3516d2019-07-21 13:24:30 +02003#include <perf/cpumap.h>
Arnaldo Carvalho de Melof2a39fe2019-08-30 14:45:20 -03004#include <string.h>
Arnaldo Carvalho de Melo87ffb6c2019-09-10 16:29:02 +01005#include "cpumap.h"
Jiri Olsaa6e52812015-10-25 15:51:37 +01006#include "evlist.h"
7#include "evsel.h"
Arnaldo Carvalho de Melof2a39fe2019-08-30 14:45:20 -03008#include "header.h"
Jiri Olsaa6e52812015-10-25 15:51:37 +01009#include "machine.h"
Arnaldo Carvalho de Meloea49e012019-09-18 11:36:13 -030010#include "util/synthetic-events.h"
Arnaldo Carvalho de Melo4a3cec82019-08-30 11:11:01 -030011#include "tool.h"
Jiri Olsaa6e52812015-10-25 15:51:37 +010012#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{
Jiri Olsa72932372019-08-28 15:57:16 +020020 struct perf_record_event_update *ev = (struct perf_record_event_update *)event;
Jiri Olsaa6e52812015-10-25 15:51:37 +010021
22 TEST_ASSERT_VAL("wrong id", ev->id == 123);
23 TEST_ASSERT_VAL("wrong id", ev->type == PERF_EVENT_UPDATE__UNIT);
24 TEST_ASSERT_VAL("wrong unit", !strcmp(ev->data, "KRAVA"));
25 return 0;
26}
27
Jiri Olsadaeecbc2015-10-25 15:51:38 +010028static 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{
Jiri Olsa72932372019-08-28 15:57:16 +020033 struct perf_record_event_update *ev = (struct perf_record_event_update *)event;
34 struct perf_record_event_update_scale *ev_data;
Jiri Olsadaeecbc2015-10-25 15:51:38 +010035
Jiri Olsa72932372019-08-28 15:57:16 +020036 ev_data = (struct perf_record_event_update_scale *)ev->data;
Jiri Olsadaeecbc2015-10-25 15:51:38 +010037
38 TEST_ASSERT_VAL("wrong id", ev->id == 123);
39 TEST_ASSERT_VAL("wrong id", ev->type == PERF_EVENT_UPDATE__SCALE);
Colin Ian King8daef502016-04-23 14:45:54 +010040 TEST_ASSERT_VAL("wrong scale", ev_data->scale == 0.123);
Jiri Olsadaeecbc2015-10-25 15:51:38 +010041 return 0;
42}
43
Jiri Olsa802c9042015-10-25 15:51:39 +010044struct event_name {
45 struct perf_tool tool;
46 const char *name;
47};
48
49static int process_event_name(struct perf_tool *tool,
50 union perf_event *event,
51 struct perf_sample *sample __maybe_unused,
52 struct machine *machine __maybe_unused)
53{
54 struct event_name *tmp = container_of(tool, struct event_name, tool);
Jiri Olsa72932372019-08-28 15:57:16 +020055 struct perf_record_event_update *ev = (struct perf_record_event_update *)event;
Jiri Olsa802c9042015-10-25 15:51:39 +010056
57 TEST_ASSERT_VAL("wrong id", ev->id == 123);
58 TEST_ASSERT_VAL("wrong id", ev->type == PERF_EVENT_UPDATE__NAME);
59 TEST_ASSERT_VAL("wrong name", !strcmp(ev->data, tmp->name));
60 return 0;
61}
62
Jiri Olsa86ebb092015-10-25 15:51:40 +010063static int process_event_cpus(struct perf_tool *tool __maybe_unused,
64 union perf_event *event,
65 struct perf_sample *sample __maybe_unused,
66 struct machine *machine __maybe_unused)
67{
Jiri Olsa72932372019-08-28 15:57:16 +020068 struct perf_record_event_update *ev = (struct perf_record_event_update *)event;
69 struct perf_record_event_update_cpus *ev_data;
Jiri Olsaf8548392019-07-21 13:23:49 +020070 struct perf_cpu_map *map;
Jiri Olsa86ebb092015-10-25 15:51:40 +010071
Jiri Olsa72932372019-08-28 15:57:16 +020072 ev_data = (struct perf_record_event_update_cpus *) ev->data;
Jiri Olsa86ebb092015-10-25 15:51:40 +010073
74 map = cpu_map__new_data(&ev_data->cpus);
75
76 TEST_ASSERT_VAL("wrong id", ev->id == 123);
77 TEST_ASSERT_VAL("wrong type", ev->type == PERF_EVENT_UPDATE__CPUS);
78 TEST_ASSERT_VAL("wrong cpus", map->nr == 3);
79 TEST_ASSERT_VAL("wrong cpus", map->map[0] == 1);
80 TEST_ASSERT_VAL("wrong cpus", map->map[1] == 2);
81 TEST_ASSERT_VAL("wrong cpus", map->map[2] == 3);
Jiri Olsa38f01d82019-07-21 13:24:17 +020082 perf_cpu_map__put(map);
Jiri Olsa86ebb092015-10-25 15:51:40 +010083 return 0;
84}
85
Arnaldo Carvalho de Melo81f17c92017-08-03 15:16:31 -030086int test__event_update(struct test *test __maybe_unused, int subtest __maybe_unused)
Jiri Olsaa6e52812015-10-25 15:51:37 +010087{
Jiri Olsa32dcd022019-07-21 13:23:51 +020088 struct evsel *evsel;
Jiri Olsa802c9042015-10-25 15:51:39 +010089 struct event_name tmp;
Arnaldo Carvalho de Melo606e2c22020-11-30 15:04:05 -030090 struct evlist *evlist = evlist__new_default();
Riccardo Mancinidccfca92021-07-15 18:07:10 +020091 char *unit = strdup("KRAVA");
Jiri Olsaa6e52812015-10-25 15:51:37 +010092
Jiri Olsaa6e52812015-10-25 15:51:37 +010093 TEST_ASSERT_VAL("failed to get evlist", evlist);
94
Jiri Olsa515dbe42019-09-03 10:39:52 +020095 evsel = evlist__first(evlist);
Jiri Olsaa6e52812015-10-25 15:51:37 +010096
Colin Ian Kingce095c92019-09-11 16:21:48 +010097 TEST_ASSERT_VAL("failed to allocate ids",
Jiri Olsa70c20362019-09-03 10:34:29 +020098 !perf_evsel__alloc_id(&evsel->core, 1, 1));
Jiri Olsaa6e52812015-10-25 15:51:37 +010099
Jiri Olsab0031c22019-09-03 11:01:04 +0200100 perf_evlist__id_add(&evlist->core, &evsel->core, 0, 0, 123);
Jiri Olsaa6e52812015-10-25 15:51:37 +0100101
Riccardo Mancinidccfca92021-07-15 18:07:10 +0200102 evsel->unit = unit;
Jiri Olsaa6e52812015-10-25 15:51:37 +0100103
104 TEST_ASSERT_VAL("failed to synthesize attr update unit",
105 !perf_event__synthesize_event_update_unit(NULL, evsel, process_event_unit));
106
Jiri Olsadaeecbc2015-10-25 15:51:38 +0100107 evsel->scale = 0.123;
108
109 TEST_ASSERT_VAL("failed to synthesize attr update scale",
110 !perf_event__synthesize_event_update_scale(NULL, evsel, process_event_scale));
111
Arnaldo Carvalho de Melo8ab2e962020-04-29 16:07:09 -0300112 tmp.name = evsel__name(evsel);
Jiri Olsa802c9042015-10-25 15:51:39 +0100113
114 TEST_ASSERT_VAL("failed to synthesize attr update name",
115 !perf_event__synthesize_event_update_name(&tmp.tool, evsel, process_event_name));
116
Jiri Olsafe1f61b2019-07-21 13:24:38 +0200117 evsel->core.own_cpus = perf_cpu_map__new("1,2,3");
Jiri Olsa86ebb092015-10-25 15:51:40 +0100118
119 TEST_ASSERT_VAL("failed to synthesize attr update cpus",
120 !perf_event__synthesize_event_update_cpus(&tmp.tool, evsel, process_event_cpus));
121
Riccardo Mancinidccfca92021-07-15 18:07:10 +0200122 free(unit);
Riccardo Mancinifc56f542021-07-15 18:07:09 +0200123 evlist__delete(evlist);
Jiri Olsaa6e52812015-10-25 15:51:37 +0100124 return 0;
125}