summaryrefslogtreecommitdiff
path: root/tools/perf/tests/event_groups.c
blob: c119ff114948394e4400a093a06d32c61a90a29f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// SPDX-License-Identifier: GPL-2.0
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include "linux/perf_event.h"
#include "tests.h"
#include "debug.h"
#include "pmu.h"
#include "pmus.h"
#include "header.h"
#include "../perf-sys.h"

/* hw: cycles,instructions sw: context-switch, uncore: [arch dependent] */
static int types[] = {0, 1, -1};
static unsigned long configs[] = {0, 3, 0};
static unsigned long configs_hw[] = {1};

#define NR_UNCORE_PMUS 5

/* Uncore pmus that support more than 3 counters */
static struct uncore_pmus {
	const char *name;
	__u64 config;
} uncore_pmus[NR_UNCORE_PMUS] = {
	{ "amd_l3", 0x0 },
	{ "amd_df", 0x0 },
	{ "uncore_imc_0", 0x1 },         /* Intel */
	{ "core_imc", 0x318 },           /* PowerPC: core_imc/CPM_STCX_FIN/ */
	{ "hv_24x7", 0x22000000003 },    /* PowerPC: hv_24x7/CPM_STCX_FIN/ */
};

static int event_open(int type, unsigned long config, int group_fd)
{
	struct perf_event_attr attr;

	memset(&attr, 0, sizeof(struct perf_event_attr));
	attr.type = type;
	attr.size = sizeof(struct perf_event_attr);
	attr.config = config;
	/*
	 * When creating an event group, typically the group leader is
	 * initialized with disabled set to 1 and any child events are
	 * initialized with disabled set to 0. Despite disabled being 0,
	 * the child events will not start until the group leader is
	 * enabled.
	 */
	attr.disabled = group_fd == -1 ? 1 : 0;

	return sys_perf_event_open(&attr, -1, 0, group_fd, 0);
}

static int setup_uncore_event(void)
{
	struct perf_pmu *pmu = NULL;
	int i, fd;

	while ((pmu = perf_pmus__scan(pmu)) != NULL) {
		for (i = 0; i < NR_UNCORE_PMUS; i++) {
			if (!strcmp(uncore_pmus[i].name, pmu->name)) {
				pr_debug("Using %s for uncore pmu event\n", pmu->name);
				types[2] = pmu->type;
				configs[2] = uncore_pmus[i].config;
				/*
				 * Check if the chosen uncore pmu event can be
				 * used in the test. For example, incase of accessing
				 * hv_24x7 pmu counters, partition should have
				 * additional permissions. If not, event open will
				 * fail. So check if the event open succeeds
				 * before proceeding.
				 */
				fd = event_open(types[2], configs[2], -1);
				if (fd < 0)
					return -1;
				close(fd);
				return 0;
			}
		}
	}
	return -1;
}

static int run_test(int i, int j, int k)
{
	int erroneous = ((((1 << i) | (1 << j) | (1 << k)) & 5) == 5);
	int group_fd, sibling_fd1, sibling_fd2;

	group_fd = event_open(types[i], configs[i], -1);
	if (group_fd == -1)
		return -1;

	sibling_fd1 = event_open(types[j], configs[j], group_fd);
	if (sibling_fd1 == -1) {
		close(group_fd);
		return erroneous ? 0 : -1;
	}

	/*
	 * if all three events (leader and two sibling events)
	 * are hardware events, use instructions as one of the
	 * sibling event. There is event constraint in powerpc that
	 * events using same counter cannot be programmed in a group.
	 * Since PERF_COUNT_HW_INSTRUCTIONS is a generic hardware
	 * event and present in all platforms, lets use that.
	 */
	if (!i && !j && !k)
		sibling_fd2 = event_open(types[k], configs_hw[k], group_fd);
	else
		sibling_fd2 = event_open(types[k], configs[k], group_fd);
	if (sibling_fd2 == -1) {
		close(sibling_fd1);
		close(group_fd);
		return erroneous ? 0 : -1;
	}

	close(sibling_fd2);
	close(sibling_fd1);
	close(group_fd);
	return erroneous ? -1 : 0;
}

static int test__event_groups(struct test_suite *text __maybe_unused, int subtest __maybe_unused)
{
	int i, j, k;
	int ret;
	int r;

	ret = setup_uncore_event();
	if (ret || types[2] == -1)
		return TEST_SKIP;

	ret = TEST_OK;
	for (i = 0; i < 3; i++) {
		for (j = 0; j < 3; j++) {
			for (k = 0; k < 3; k++) {
				r = run_test(i, j, k);
				if (r)
					ret = TEST_FAIL;

				/*
				 * For all three events as HW events, second sibling
				 * event is picked from configs_hw. So print accordingly
				 */
				if (!i && !j && !k)
					pr_debug("0x%x 0x%lx, 0x%x 0x%lx, 0x%x 0x%lx: %s\n",
						 types[i], configs[i], types[j], configs[j],
						 types[k], configs_hw[k], r ? "Fail" : "Pass");
				else
					pr_debug("0x%x 0x%lx, 0x%x 0x%lx, 0x%x 0x%lx: %s\n",
						 types[i], configs[i], types[j], configs[j],
						 types[k], configs[k], r ? "Fail" : "Pass");
			}
		}
	}
	return ret;
}

DEFINE_SUITE("Event groups", event_groups);