Merge tag 'locks-v3.16-2' of git://git.samba.org/jlayton/linux
[linux/fpc-iii.git] / tools / perf / tests / dso-data.c
blob630808cd7cc2fedabc1fb79e3c5c7e0cfb3b9bdb
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"
14 static char *test_file(int size)
16 #define TEMPL "/tmp/perf-test-XXXXXX"
17 static char buf_templ[sizeof(TEMPL)];
18 char *templ = buf_templ;
19 int fd, i;
20 unsigned char *buf;
22 strcpy(buf_templ, TEMPL);
23 #undef TEMPL
25 fd = mkstemp(templ);
26 if (fd < 0) {
27 perror("mkstemp failed");
28 return NULL;
31 buf = malloc(size);
32 if (!buf) {
33 close(fd);
34 return NULL;
37 for (i = 0; i < size; i++)
38 buf[i] = (unsigned char) ((int) i % 10);
40 if (size != write(fd, buf, size))
41 templ = NULL;
43 free(buf);
44 close(fd);
45 return templ;
48 #define TEST_FILE_SIZE (DSO__DATA_CACHE_SIZE * 20)
50 struct test_data_offset {
51 off_t offset;
52 u8 data[10];
53 int size;
56 struct test_data_offset offsets[] = {
57 /* Fill first cache page. */
59 .offset = 10,
60 .data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
61 .size = 10,
63 /* Read first cache page. */
65 .offset = 10,
66 .data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
67 .size = 10,
69 /* Fill cache boundary pages. */
71 .offset = DSO__DATA_CACHE_SIZE - DSO__DATA_CACHE_SIZE % 10,
72 .data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
73 .size = 10,
75 /* Read cache boundary pages. */
77 .offset = DSO__DATA_CACHE_SIZE - DSO__DATA_CACHE_SIZE % 10,
78 .data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
79 .size = 10,
81 /* Fill final cache page. */
83 .offset = TEST_FILE_SIZE - 10,
84 .data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
85 .size = 10,
87 /* Read final cache page. */
89 .offset = TEST_FILE_SIZE - 10,
90 .data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
91 .size = 10,
93 /* Read final cache page. */
95 .offset = TEST_FILE_SIZE - 3,
96 .data = { 7, 8, 9, 0, 0, 0, 0, 0, 0, 0 },
97 .size = 3,
101 int test__dso_data(void)
103 struct machine machine;
104 struct dso *dso;
105 char *file = test_file(TEST_FILE_SIZE);
106 size_t i;
108 TEST_ASSERT_VAL("No test file", file);
110 memset(&machine, 0, sizeof(machine));
112 dso = dso__new((const char *)file);
114 /* Basic 10 bytes tests. */
115 for (i = 0; i < ARRAY_SIZE(offsets); i++) {
116 struct test_data_offset *data = &offsets[i];
117 ssize_t size;
118 u8 buf[10];
120 memset(buf, 0, 10);
121 size = dso__data_read_offset(dso, &machine, data->offset,
122 buf, 10);
124 TEST_ASSERT_VAL("Wrong size", size == data->size);
125 TEST_ASSERT_VAL("Wrong data", !memcmp(buf, data->data, 10));
128 /* Read cross multiple cache pages. */
130 ssize_t size;
131 int c;
132 u8 *buf;
134 buf = malloc(TEST_FILE_SIZE);
135 TEST_ASSERT_VAL("ENOMEM\n", buf);
137 /* First iteration to fill caches, second one to read them. */
138 for (c = 0; c < 2; c++) {
139 memset(buf, 0, TEST_FILE_SIZE);
140 size = dso__data_read_offset(dso, &machine, 10,
141 buf, TEST_FILE_SIZE);
143 TEST_ASSERT_VAL("Wrong size",
144 size == (TEST_FILE_SIZE - 10));
146 for (i = 0; i < (size_t)size; i++)
147 TEST_ASSERT_VAL("Wrong data",
148 buf[i] == (i % 10));
151 free(buf);
154 dso__delete(dso);
155 unlink(file);
156 return 0;
159 static long open_files_cnt(void)
161 char path[PATH_MAX];
162 struct dirent *dent;
163 DIR *dir;
164 long nr = 0;
166 scnprintf(path, PATH_MAX, "%s/self/fd", procfs__mountpoint());
167 pr_debug("fd path: %s\n", path);
169 dir = opendir(path);
170 TEST_ASSERT_VAL("failed to open fd directory", dir);
172 while ((dent = readdir(dir)) != NULL) {
173 if (!strcmp(dent->d_name, ".") ||
174 !strcmp(dent->d_name, ".."))
175 continue;
177 nr++;
180 closedir(dir);
181 return nr - 1;
184 static struct dso **dsos;
186 static int dsos__create(int cnt, int size)
188 int i;
190 dsos = malloc(sizeof(dsos) * cnt);
191 TEST_ASSERT_VAL("failed to alloc dsos array", dsos);
193 for (i = 0; i < cnt; i++) {
194 char *file;
196 file = test_file(size);
197 TEST_ASSERT_VAL("failed to get dso file", file);
199 dsos[i] = dso__new(file);
200 TEST_ASSERT_VAL("failed to get dso", dsos[i]);
203 return 0;
206 static void dsos__delete(int cnt)
208 int i;
210 for (i = 0; i < cnt; i++) {
211 struct dso *dso = dsos[i];
213 unlink(dso->name);
214 dso__delete(dso);
217 free(dsos);
220 static int set_fd_limit(int n)
222 struct rlimit rlim;
224 if (getrlimit(RLIMIT_NOFILE, &rlim))
225 return -1;
227 pr_debug("file limit %ld, new %d\n", (long) rlim.rlim_cur, n);
229 rlim.rlim_cur = n;
230 return setrlimit(RLIMIT_NOFILE, &rlim);
233 int test__dso_data_cache(void)
235 struct machine machine;
236 long nr_end, nr = open_files_cnt();
237 int dso_cnt, limit, i, fd;
239 memset(&machine, 0, sizeof(machine));
241 /* set as system limit */
242 limit = nr * 4;
243 TEST_ASSERT_VAL("failed to set file limit", !set_fd_limit(limit));
245 /* and this is now our dso open FDs limit + 1 extra */
246 dso_cnt = limit / 2 + 1;
247 TEST_ASSERT_VAL("failed to create dsos\n",
248 !dsos__create(dso_cnt, TEST_FILE_SIZE));
250 for (i = 0; i < (dso_cnt - 1); i++) {
251 struct dso *dso = dsos[i];
254 * Open dsos via dso__data_fd or dso__data_read_offset.
255 * Both opens the data file and keep it open.
257 if (i % 2) {
258 fd = dso__data_fd(dso, &machine);
259 TEST_ASSERT_VAL("failed to get fd", fd > 0);
260 } else {
261 #define BUFSIZE 10
262 u8 buf[BUFSIZE];
263 ssize_t n;
265 n = dso__data_read_offset(dso, &machine, 0, buf, BUFSIZE);
266 TEST_ASSERT_VAL("failed to read dso", n == BUFSIZE);
270 /* open +1 dso over the allowed limit */
271 fd = dso__data_fd(dsos[i], &machine);
272 TEST_ASSERT_VAL("failed to get fd", fd > 0);
274 /* should force the first one to be closed */
275 TEST_ASSERT_VAL("failed to close dsos[0]", dsos[0]->data.fd == -1);
277 /* cleanup everything */
278 dsos__delete(dso_cnt);
280 /* Make sure we did not leak any file descriptor. */
281 nr_end = open_files_cnt();
282 pr_debug("nr start %ld, nr stop %ld\n", nr, nr_end);
283 TEST_ASSERT_VAL("failed leadking files", nr == nr_end);
284 return 0;
287 int test__dso_data_reopen(void)
289 struct machine machine;
290 long nr_end, nr = open_files_cnt();
291 int fd, fd_extra;
293 #define dso_0 (dsos[0])
294 #define dso_1 (dsos[1])
295 #define dso_2 (dsos[2])
297 memset(&machine, 0, sizeof(machine));
300 * Test scenario:
301 * - create 3 dso objects
302 * - set process file descriptor limit to current
303 * files count + 3
304 * - test that the first dso gets closed when we
305 * reach the files count limit
308 /* Make sure we are able to open 3 fds anyway */
309 TEST_ASSERT_VAL("failed to set file limit",
310 !set_fd_limit((nr + 3)));
312 TEST_ASSERT_VAL("failed to create dsos\n", !dsos__create(3, TEST_FILE_SIZE));
314 /* open dso_0 */
315 fd = dso__data_fd(dso_0, &machine);
316 TEST_ASSERT_VAL("failed to get fd", fd > 0);
318 /* open dso_1 */
319 fd = dso__data_fd(dso_1, &machine);
320 TEST_ASSERT_VAL("failed to get fd", fd > 0);
323 * open extra file descriptor and we just
324 * reached the files count limit
326 fd_extra = open("/dev/null", O_RDONLY);
327 TEST_ASSERT_VAL("failed to open extra fd", fd_extra > 0);
329 /* open dso_2 */
330 fd = dso__data_fd(dso_2, &machine);
331 TEST_ASSERT_VAL("failed to get fd", fd > 0);
334 * dso_0 should get closed, because we reached
335 * the file descriptor limit
337 TEST_ASSERT_VAL("failed to close dso_0", dso_0->data.fd == -1);
339 /* open dso_0 */
340 fd = dso__data_fd(dso_0, &machine);
341 TEST_ASSERT_VAL("failed to get fd", fd > 0);
344 * dso_1 should get closed, because we reached
345 * the file descriptor limit
347 TEST_ASSERT_VAL("failed to close dso_1", dso_1->data.fd == -1);
349 /* cleanup everything */
350 close(fd_extra);
351 dsos__delete(3);
353 /* Make sure we did not leak any file descriptor. */
354 nr_end = open_files_cnt();
355 pr_debug("nr start %ld, nr stop %ld\n", nr, nr_end);
356 TEST_ASSERT_VAL("failed leadking files", nr == nr_end);
357 return 0;