ARM: pmu: add support for interrupt-affinity property
[linux/fpc-iii.git] / tools / perf / tests / dso-data.c
blobcaaf37f079b1c60fb987ca98445b12159135b3d5
1 #include <stdlib.h>
2 #include <linux/types.h>
3 #include <sys/stat.h>
4 #include <fcntl.h>
5 #include <string.h>
6 #include <sys/time.h>
7 #include <sys/resource.h>
8 #include <api/fs/fs.h>
9 #include "util.h"
10 #include "machine.h"
11 #include "symbol.h"
12 #include "tests.h"
13 #include "debug.h"
15 static char *test_file(int size)
17 #define TEMPL "/tmp/perf-test-XXXXXX"
18 static char buf_templ[sizeof(TEMPL)];
19 char *templ = buf_templ;
20 int fd, i;
21 unsigned char *buf;
23 strcpy(buf_templ, TEMPL);
24 #undef TEMPL
26 fd = mkstemp(templ);
27 if (fd < 0) {
28 perror("mkstemp failed");
29 return NULL;
32 buf = malloc(size);
33 if (!buf) {
34 close(fd);
35 return NULL;
38 for (i = 0; i < size; i++)
39 buf[i] = (unsigned char) ((int) i % 10);
41 if (size != write(fd, buf, size))
42 templ = NULL;
44 free(buf);
45 close(fd);
46 return templ;
49 #define TEST_FILE_SIZE (DSO__DATA_CACHE_SIZE * 20)
51 struct test_data_offset {
52 off_t offset;
53 u8 data[10];
54 int size;
57 struct test_data_offset offsets[] = {
58 /* Fill first cache page. */
60 .offset = 10,
61 .data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
62 .size = 10,
64 /* Read first cache page. */
66 .offset = 10,
67 .data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
68 .size = 10,
70 /* Fill cache boundary pages. */
72 .offset = DSO__DATA_CACHE_SIZE - DSO__DATA_CACHE_SIZE % 10,
73 .data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
74 .size = 10,
76 /* Read cache boundary pages. */
78 .offset = DSO__DATA_CACHE_SIZE - DSO__DATA_CACHE_SIZE % 10,
79 .data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
80 .size = 10,
82 /* Fill final cache page. */
84 .offset = TEST_FILE_SIZE - 10,
85 .data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
86 .size = 10,
88 /* Read final cache page. */
90 .offset = TEST_FILE_SIZE - 10,
91 .data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
92 .size = 10,
94 /* Read final cache page. */
96 .offset = TEST_FILE_SIZE - 3,
97 .data = { 7, 8, 9, 0, 0, 0, 0, 0, 0, 0 },
98 .size = 3,
102 int test__dso_data(void)
104 struct machine machine;
105 struct dso *dso;
106 char *file = test_file(TEST_FILE_SIZE);
107 size_t i;
109 TEST_ASSERT_VAL("No test file", file);
111 memset(&machine, 0, sizeof(machine));
113 dso = dso__new((const char *)file);
115 /* Basic 10 bytes tests. */
116 for (i = 0; i < ARRAY_SIZE(offsets); i++) {
117 struct test_data_offset *data = &offsets[i];
118 ssize_t size;
119 u8 buf[10];
121 memset(buf, 0, 10);
122 size = dso__data_read_offset(dso, &machine, data->offset,
123 buf, 10);
125 TEST_ASSERT_VAL("Wrong size", size == data->size);
126 TEST_ASSERT_VAL("Wrong data", !memcmp(buf, data->data, 10));
129 /* Read cross multiple cache pages. */
131 ssize_t size;
132 int c;
133 u8 *buf;
135 buf = malloc(TEST_FILE_SIZE);
136 TEST_ASSERT_VAL("ENOMEM\n", buf);
138 /* First iteration to fill caches, second one to read them. */
139 for (c = 0; c < 2; c++) {
140 memset(buf, 0, TEST_FILE_SIZE);
141 size = dso__data_read_offset(dso, &machine, 10,
142 buf, TEST_FILE_SIZE);
144 TEST_ASSERT_VAL("Wrong size",
145 size == (TEST_FILE_SIZE - 10));
147 for (i = 0; i < (size_t)size; i++)
148 TEST_ASSERT_VAL("Wrong data",
149 buf[i] == (i % 10));
152 free(buf);
155 dso__delete(dso);
156 unlink(file);
157 return 0;
160 static long open_files_cnt(void)
162 char path[PATH_MAX];
163 struct dirent *dent;
164 DIR *dir;
165 long nr = 0;
167 scnprintf(path, PATH_MAX, "%s/self/fd", procfs__mountpoint());
168 pr_debug("fd path: %s\n", path);
170 dir = opendir(path);
171 TEST_ASSERT_VAL("failed to open fd directory", dir);
173 while ((dent = readdir(dir)) != NULL) {
174 if (!strcmp(dent->d_name, ".") ||
175 !strcmp(dent->d_name, ".."))
176 continue;
178 nr++;
181 closedir(dir);
182 return nr - 1;
185 static struct dso **dsos;
187 static int dsos__create(int cnt, int size)
189 int i;
191 dsos = malloc(sizeof(dsos) * cnt);
192 TEST_ASSERT_VAL("failed to alloc dsos array", dsos);
194 for (i = 0; i < cnt; i++) {
195 char *file;
197 file = test_file(size);
198 TEST_ASSERT_VAL("failed to get dso file", file);
200 dsos[i] = dso__new(file);
201 TEST_ASSERT_VAL("failed to get dso", dsos[i]);
204 return 0;
207 static void dsos__delete(int cnt)
209 int i;
211 for (i = 0; i < cnt; i++) {
212 struct dso *dso = dsos[i];
214 unlink(dso->name);
215 dso__delete(dso);
218 free(dsos);
221 static int set_fd_limit(int n)
223 struct rlimit rlim;
225 if (getrlimit(RLIMIT_NOFILE, &rlim))
226 return -1;
228 pr_debug("file limit %ld, new %d\n", (long) rlim.rlim_cur, n);
230 rlim.rlim_cur = n;
231 return setrlimit(RLIMIT_NOFILE, &rlim);
234 int test__dso_data_cache(void)
236 struct machine machine;
237 long nr_end, nr = open_files_cnt();
238 int dso_cnt, limit, i, fd;
240 memset(&machine, 0, sizeof(machine));
242 /* set as system limit */
243 limit = nr * 4;
244 TEST_ASSERT_VAL("failed to set file limit", !set_fd_limit(limit));
246 /* and this is now our dso open FDs limit + 1 extra */
247 dso_cnt = limit / 2 + 1;
248 TEST_ASSERT_VAL("failed to create dsos\n",
249 !dsos__create(dso_cnt, TEST_FILE_SIZE));
251 for (i = 0; i < (dso_cnt - 1); i++) {
252 struct dso *dso = dsos[i];
255 * Open dsos via dso__data_fd or dso__data_read_offset.
256 * Both opens the data file and keep it open.
258 if (i % 2) {
259 fd = dso__data_fd(dso, &machine);
260 TEST_ASSERT_VAL("failed to get fd", fd > 0);
261 } else {
262 #define BUFSIZE 10
263 u8 buf[BUFSIZE];
264 ssize_t n;
266 n = dso__data_read_offset(dso, &machine, 0, buf, BUFSIZE);
267 TEST_ASSERT_VAL("failed to read dso", n == BUFSIZE);
271 /* open +1 dso over the allowed limit */
272 fd = dso__data_fd(dsos[i], &machine);
273 TEST_ASSERT_VAL("failed to get fd", fd > 0);
275 /* should force the first one to be closed */
276 TEST_ASSERT_VAL("failed to close dsos[0]", dsos[0]->data.fd == -1);
278 /* cleanup everything */
279 dsos__delete(dso_cnt);
281 /* Make sure we did not leak any file descriptor. */
282 nr_end = open_files_cnt();
283 pr_debug("nr start %ld, nr stop %ld\n", nr, nr_end);
284 TEST_ASSERT_VAL("failed leadking files", nr == nr_end);
285 return 0;
288 int test__dso_data_reopen(void)
290 struct machine machine;
291 long nr_end, nr = open_files_cnt();
292 int fd, fd_extra;
294 #define dso_0 (dsos[0])
295 #define dso_1 (dsos[1])
296 #define dso_2 (dsos[2])
298 memset(&machine, 0, sizeof(machine));
301 * Test scenario:
302 * - create 3 dso objects
303 * - set process file descriptor limit to current
304 * files count + 3
305 * - test that the first dso gets closed when we
306 * reach the files count limit
309 /* Make sure we are able to open 3 fds anyway */
310 TEST_ASSERT_VAL("failed to set file limit",
311 !set_fd_limit((nr + 3)));
313 TEST_ASSERT_VAL("failed to create dsos\n", !dsos__create(3, TEST_FILE_SIZE));
315 /* open dso_0 */
316 fd = dso__data_fd(dso_0, &machine);
317 TEST_ASSERT_VAL("failed to get fd", fd > 0);
319 /* open dso_1 */
320 fd = dso__data_fd(dso_1, &machine);
321 TEST_ASSERT_VAL("failed to get fd", fd > 0);
324 * open extra file descriptor and we just
325 * reached the files count limit
327 fd_extra = open("/dev/null", O_RDONLY);
328 TEST_ASSERT_VAL("failed to open extra fd", fd_extra > 0);
330 /* open dso_2 */
331 fd = dso__data_fd(dso_2, &machine);
332 TEST_ASSERT_VAL("failed to get fd", fd > 0);
335 * dso_0 should get closed, because we reached
336 * the file descriptor limit
338 TEST_ASSERT_VAL("failed to close dso_0", dso_0->data.fd == -1);
340 /* open dso_0 */
341 fd = dso__data_fd(dso_0, &machine);
342 TEST_ASSERT_VAL("failed to get fd", fd > 0);
345 * dso_1 should get closed, because we reached
346 * the file descriptor limit
348 TEST_ASSERT_VAL("failed to close dso_1", dso_1->data.fd == -1);
350 /* cleanup everything */
351 close(fd_extra);
352 dsos__delete(3);
354 /* Make sure we did not leak any file descriptor. */
355 nr_end = open_files_cnt();
356 pr_debug("nr start %ld, nr stop %ld\n", nr, nr_end);
357 TEST_ASSERT_VAL("failed leadking files", nr == nr_end);
358 return 0;