event_update.c 3.9 KB
Newer Older
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 7
#include "evlist.h"
#include "evsel.h"
8
#include "header.h"
9
#include "machine.h"
10
#include "util/synthetic-events.h"
11
#include "tool.h"
12 13 14 15 16 17 18 19
#include "tests.h"
#include "debug.h"

static int process_event_unit(struct perf_tool *tool __maybe_unused,
			      union perf_event *event,
			      struct perf_sample *sample __maybe_unused,
			      struct machine *machine __maybe_unused)
{
20
	struct perf_record_event_update *ev = (struct perf_record_event_update *)event;
21 22 23 24 25 26 27

	TEST_ASSERT_VAL("wrong id", ev->id == 123);
	TEST_ASSERT_VAL("wrong id", ev->type == PERF_EVENT_UPDATE__UNIT);
	TEST_ASSERT_VAL("wrong unit", !strcmp(ev->data, "KRAVA"));
	return 0;
}

28 29 30 31 32
static int process_event_scale(struct perf_tool *tool __maybe_unused,
			       union perf_event *event,
			       struct perf_sample *sample __maybe_unused,
			       struct machine *machine __maybe_unused)
{
33 34
	struct perf_record_event_update *ev = (struct perf_record_event_update *)event;
	struct perf_record_event_update_scale *ev_data;
35

36
	ev_data = (struct perf_record_event_update_scale *)ev->data;
37 38 39

	TEST_ASSERT_VAL("wrong id", ev->id == 123);
	TEST_ASSERT_VAL("wrong id", ev->type == PERF_EVENT_UPDATE__SCALE);
40
	TEST_ASSERT_VAL("wrong scale", ev_data->scale == 0.123);
41 42 43
	return 0;
}

44 45 46 47 48 49 50 51 52 53 54
struct event_name {
	struct perf_tool tool;
	const char *name;
};

static int process_event_name(struct perf_tool *tool,
			      union perf_event *event,
			      struct perf_sample *sample __maybe_unused,
			      struct machine *machine __maybe_unused)
{
	struct event_name *tmp = container_of(tool, struct event_name, tool);
55
	struct perf_record_event_update *ev = (struct perf_record_event_update *)event;
56 57 58 59 60 61 62

	TEST_ASSERT_VAL("wrong id", ev->id == 123);
	TEST_ASSERT_VAL("wrong id", ev->type == PERF_EVENT_UPDATE__NAME);
	TEST_ASSERT_VAL("wrong name", !strcmp(ev->data, tmp->name));
	return 0;
}

63 64 65 66 67
static int process_event_cpus(struct perf_tool *tool __maybe_unused,
			      union perf_event *event,
			      struct perf_sample *sample __maybe_unused,
			      struct machine *machine __maybe_unused)
{
68 69
	struct perf_record_event_update *ev = (struct perf_record_event_update *)event;
	struct perf_record_event_update_cpus *ev_data;
70
	struct perf_cpu_map *map;
71

72
	ev_data = (struct perf_record_event_update_cpus *) ev->data;
73 74 75 76 77 78 79 80 81

	map = cpu_map__new_data(&ev_data->cpus);

	TEST_ASSERT_VAL("wrong id", ev->id == 123);
	TEST_ASSERT_VAL("wrong type", ev->type == PERF_EVENT_UPDATE__CPUS);
	TEST_ASSERT_VAL("wrong cpus", map->nr == 3);
	TEST_ASSERT_VAL("wrong cpus", map->map[0] == 1);
	TEST_ASSERT_VAL("wrong cpus", map->map[1] == 2);
	TEST_ASSERT_VAL("wrong cpus", map->map[2] == 3);
82
	perf_cpu_map__put(map);
83 84 85
	return 0;
}

86
int test__event_update(struct test *test __maybe_unused, int subtest __maybe_unused)
87
{
88
	struct evlist *evlist;
89
	struct evsel *evsel;
90
	struct event_name tmp;
91 92 93 94 95 96

	evlist = perf_evlist__new_default();
	TEST_ASSERT_VAL("failed to get evlist", evlist);

	evsel = perf_evlist__first(evlist);

97
	TEST_ASSERT_VAL("failed to allocate ids",
98 99 100 101 102 103 104 105 106
			!perf_evsel__alloc_id(evsel, 1, 1));

	perf_evlist__id_add(evlist, evsel, 0, 0, 123);

	evsel->unit = strdup("KRAVA");

	TEST_ASSERT_VAL("failed to synthesize attr update unit",
			!perf_event__synthesize_event_update_unit(NULL, evsel, process_event_unit));

107 108 109 110 111
	evsel->scale = 0.123;

	TEST_ASSERT_VAL("failed to synthesize attr update scale",
			!perf_event__synthesize_event_update_scale(NULL, evsel, process_event_scale));

112 113 114 115 116
	tmp.name = perf_evsel__name(evsel);

	TEST_ASSERT_VAL("failed to synthesize attr update name",
			!perf_event__synthesize_event_update_name(&tmp.tool, evsel, process_event_name));

117
	evsel->core.own_cpus = perf_cpu_map__new("1,2,3");
118 119 120 121

	TEST_ASSERT_VAL("failed to synthesize attr update cpus",
			!perf_event__synthesize_event_update_cpus(&tmp.tool, evsel, process_event_cpus));

122
	perf_cpu_map__put(evsel->core.own_cpus);
123 124
	return 0;
}