summaryrefslogtreecommitdiffstats
path: root/tools/perf/tests/evlist.c
blob: 99d7dfd4e20a634cd9bbf3cfdd8ab27e85d7c97c (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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include "util/evlist.h"
#include "util/debug.h"
#include "util/thread_map.h"
#include "util/cpumap.h"
#include "tests/tests.h"

static void perf_evlist__init_pollfd(struct perf_evlist *evlist,
				     int nr_fds_alloc, short revents)
{
	int fd;

	evlist->nr_fds = nr_fds_alloc;

	for (fd = 0; fd < nr_fds_alloc; ++fd) {
		evlist->pollfd[fd].fd	   = nr_fds_alloc - fd;
		evlist->pollfd[fd].revents = revents;
	}
}

static int perf_evlist__fprintf_pollfd(struct perf_evlist *evlist,
				       const char *prefix, FILE *fp)
{
	int printed = 0, fd;

	if (!verbose)
		return 0;

	printed += fprintf(fp, "\n%s: %3d [ ", prefix, evlist->nr_fds);
	for (fd = 0; fd < evlist->nr_fds; ++fd)
		printed += fprintf(fp, "%s%d", fd ? ", " : "", evlist->pollfd[fd].fd);
	printed += fprintf(fp, " ]");
	return printed;
}

int test__perf_evlist__filter_pollfd(void)
{
	const int nr_fds_alloc = 5;
	int nr_fds, expected_fd[2], fd;
	struct pollfd pollfd[nr_fds_alloc];
	struct perf_evlist evlist_alloc = {
		.pollfd	= pollfd,
	}, *evlist = &evlist_alloc;

	perf_evlist__init_pollfd(evlist, nr_fds_alloc, POLLIN);
	nr_fds = perf_evlist__filter_pollfd(evlist, POLLHUP);
	if (nr_fds != nr_fds_alloc) {
		pr_debug("\nperf_evlist__filter_pollfd()=%d != %d shouldn't have filtered anything",
			 nr_fds, nr_fds_alloc);
		return TEST_FAIL;
	}

	perf_evlist__init_pollfd(evlist, nr_fds_alloc, POLLHUP);
	nr_fds = perf_evlist__filter_pollfd(evlist, POLLHUP);
	if (nr_fds != 0) {
		pr_debug("\nperf_evlist__filter_pollfd()=%d != %d, should have filtered all fds",
			 nr_fds, nr_fds_alloc);
		return TEST_FAIL;
	}

	perf_evlist__init_pollfd(evlist, nr_fds_alloc, POLLHUP);
	pollfd[2].revents = POLLIN;
	expected_fd[0] = pollfd[2].fd;

	pr_debug("\nfiltering all but pollfd[2]:");
	perf_evlist__fprintf_pollfd(evlist, "before", stderr);
	nr_fds = perf_evlist__filter_pollfd(evlist, POLLHUP);
	perf_evlist__fprintf_pollfd(evlist, " after", stderr);
	if (nr_fds != 1) {
		pr_debug("\nperf_evlist__filter_pollfd()=%d != 1, should have left just one event",
			 nr_fds);
		return TEST_FAIL;
	}

	if (pollfd[0].fd != expected_fd[0]) {
		pr_debug("\npollfd[0].fd=%d != %d\n", pollfd[0].fd, expected_fd[0]);
		return TEST_FAIL;
	}

	perf_evlist__init_pollfd(evlist, nr_fds_alloc, POLLHUP);
	pollfd[0].revents = POLLIN;
	expected_fd[0] = pollfd[0].fd;
	pollfd[3].revents = POLLIN;
	expected_fd[1] = pollfd[3].fd;

	pr_debug("\nfiltering all but (pollfd[0], pollfd[3]):");
	perf_evlist__fprintf_pollfd(evlist, "before", stderr);
	nr_fds = perf_evlist__filter_pollfd(evlist, POLLHUP);
	perf_evlist__fprintf_pollfd(evlist, " after", stderr);
	if (nr_fds != 2) {
		pr_debug("\nperf_evlist__filter_pollfd()=%d != 2, should have left just two events",
			 nr_fds);
		return TEST_FAIL;
	}

	for (fd = 0; fd < 2; ++fd) {
		if (pollfd[fd].fd != expected_fd[fd]) {
			pr_debug("\npollfd[%d].fd=%d != %d\n", fd, pollfd[fd].fd, expected_fd[fd]);
			return TEST_FAIL;
		}
	}

	pr_debug("\n");

	return 0;
}

int test__perf_evlist__add_pollfd(void)
{
	struct perf_evsel evsel = {
		.system_wide = false,
	};
	struct thread_map threads = {
		.nr = 2,
	};
	struct perf_evlist evlist_alloc = {
		.pollfd	 = NULL,
		.threads = &threads,
	}, *evlist = &evlist_alloc;

	INIT_LIST_HEAD(&evlist->entries);
	list_add(&evsel.node, &evlist->entries);

	if (perf_evlist__alloc_pollfd(evlist) < 0) {
		pr_debug("\nperf_evlist__alloc_pollfd(evlist) failed!");
		return TEST_FAIL;
	}

	if (evlist->nr_fds_alloc != threads.nr) {
		pr_debug("\n_evlist__alloc_pollfd: nr_fds_alloc=%d != (threads->nr(%d) * cpu_map->nr(%d))=%d",
			 evlist->nr_fds_alloc, thread_map__nr(evlist->threads), cpu_map__nr(evlist->cpus),
			 thread_map__nr(evlist->threads) * cpu_map__nr(evlist->cpus));
		return TEST_FAIL;
	}

	if (perf_evlist__add_pollfd(evlist, 1) < 0) {
		pr_debug("\nperf_evlist__add_pollfd(evlist, 1) failed!");
		return TEST_FAIL;
	}

	if (evlist->nr_fds != 1) {
		pr_debug("\nperf_evlist__add_pollfd(evlist, 1)=%d != 1", evlist->nr_fds);
		return TEST_FAIL;
	}

	if (perf_evlist__add_pollfd(evlist, 2) < 0) {
		pr_debug("\nperf_evlist__add_pollfd(evlist, 2) failed!");
		return TEST_FAIL;
	}

	if (evlist->nr_fds != 2) {
		pr_debug("\nperf_evlist__add_pollfd(evlist, 2)=%d != 2", evlist->nr_fds);
		return TEST_FAIL;
	}

	perf_evlist__fprintf_pollfd(evlist, "before growing array", stderr);

	if (perf_evlist__add_pollfd(evlist, 35) < 0) {
		pr_debug("\nperf_evlist__add_pollfd(evlist, 35) failed!");
		return TEST_FAIL;
	}

	if (evlist->nr_fds != 3) {
		pr_debug("\nperf_evlist__add_pollfd(evlist, 35)=%d != 3", evlist->nr_fds);
		return TEST_FAIL;
	}

	if (evlist->pollfd == NULL) {
		pr_debug("\nperf_evlist__add_pollfd(evlist, 35) should have allocated evlist->pollfd!");
		return TEST_FAIL;
	}

	perf_evlist__fprintf_pollfd(evlist, "after 3rd add_pollfd", stderr);

	if (evlist->pollfd[2].fd != 35) {
		pr_debug("\nevlist->pollfd[2](%d) != 35!", evlist->pollfd[2].fd);
		return TEST_FAIL;
	}

	if (perf_evlist__add_pollfd(evlist, 88) < 0) {
		pr_debug("\nperf_evlist__add_pollfd(evlist, 88) failed!");
		return TEST_FAIL;
	}

	if (evlist->nr_fds != 4) {
		pr_debug("\nperf_evlist__add_pollfd(evlist, 88)=%d != 2", evlist->nr_fds);
		return TEST_FAIL;
	}

	perf_evlist__fprintf_pollfd(evlist, "after 4th add_pollfd", stderr);

	if (evlist->pollfd[0].fd != 1) {
		pr_debug("\nevlist->pollfd[0](%d) != 1!", evlist->pollfd[0].fd);
		return TEST_FAIL;
	}

	if (evlist->pollfd[1].fd != 2) {
		pr_debug("\nevlist->pollfd[1](%d) != 2!", evlist->pollfd[1].fd);
		return TEST_FAIL;
	}

	if (evlist->pollfd[2].fd != 35) {
		pr_debug("\nevlist->pollfd[2](%d) != 35!", evlist->pollfd[2].fd);
		return TEST_FAIL;
	}

	if (evlist->pollfd[3].fd != 88) {
		pr_debug("\nevlist->pollfd[3](%d) != 88!", evlist->pollfd[3].fd);
		return TEST_FAIL;
	}

	pr_debug("\n");

	return 0;
}
OpenPOWER on IntegriCloud