2 #include <linux/types.h>
7 #include <sys/resource.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;
23 strcpy(buf_templ, TEMPL);
28 perror("mkstemp failed");
38 for (i = 0; i < size; i++)
39 buf[i] = (unsigned char) ((int) i % 10);
41 if (size != write(fd, buf, size))
49 #define TEST_FILE_SIZE (DSO__DATA_CACHE_SIZE * 20)
51 struct test_data_offset {
57 struct test_data_offset offsets[] = {
58 /* Fill first cache page. */
61 .data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
64 /* Read first cache page. */
67 .data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
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 },
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 },
82 /* Fill final cache page. */
84 .offset = TEST_FILE_SIZE - 10,
85 .data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
88 /* Read final cache page. */
90 .offset = TEST_FILE_SIZE - 10,
91 .data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
94 /* Read final cache page. */
96 .offset = TEST_FILE_SIZE - 3,
97 .data = { 7, 8, 9, 0, 0, 0, 0, 0, 0, 0 },
102 /* move it from util/dso.c for compatibility */
103 static int dso__data_fd(struct dso *dso, struct machine *machine)
105 int fd = dso__data_get_fd(dso, machine);
108 dso__data_put_fd(dso);
113 int test__dso_data(int subtest __maybe_unused)
115 struct machine machine;
117 char *file = test_file(TEST_FILE_SIZE);
120 TEST_ASSERT_VAL("No test file", file);
122 memset(&machine, 0, sizeof(machine));
124 dso = dso__new((const char *)file);
126 TEST_ASSERT_VAL("Failed to access to dso",
127 dso__data_fd(dso, &machine) >= 0);
129 /* Basic 10 bytes tests. */
130 for (i = 0; i < ARRAY_SIZE(offsets); i++) {
131 struct test_data_offset *data = &offsets[i];
136 size = dso__data_read_offset(dso, &machine, data->offset,
139 TEST_ASSERT_VAL("Wrong size", size == data->size);
140 TEST_ASSERT_VAL("Wrong data", !memcmp(buf, data->data, 10));
143 /* Read cross multiple cache pages. */
149 buf = malloc(TEST_FILE_SIZE);
150 TEST_ASSERT_VAL("ENOMEM\n", buf);
152 /* First iteration to fill caches, second one to read them. */
153 for (c = 0; c < 2; c++) {
154 memset(buf, 0, TEST_FILE_SIZE);
155 size = dso__data_read_offset(dso, &machine, 10,
156 buf, TEST_FILE_SIZE);
158 TEST_ASSERT_VAL("Wrong size",
159 size == (TEST_FILE_SIZE - 10));
161 for (i = 0; i < (size_t)size; i++)
162 TEST_ASSERT_VAL("Wrong data",
174 static long open_files_cnt(void)
181 scnprintf(path, PATH_MAX, "%s/self/fd", procfs__mountpoint());
182 pr_debug("fd path: %s\n", path);
185 TEST_ASSERT_VAL("failed to open fd directory", dir);
187 while ((dent = readdir(dir)) != NULL) {
188 if (!strcmp(dent->d_name, ".") ||
189 !strcmp(dent->d_name, ".."))
199 static struct dso **dsos;
201 static int dsos__create(int cnt, int size)
205 dsos = malloc(sizeof(dsos) * cnt);
206 TEST_ASSERT_VAL("failed to alloc dsos array", dsos);
208 for (i = 0; i < cnt; i++) {
211 file = test_file(size);
212 TEST_ASSERT_VAL("failed to get dso file", file);
214 dsos[i] = dso__new(file);
215 TEST_ASSERT_VAL("failed to get dso", dsos[i]);
221 static void dsos__delete(int cnt)
225 for (i = 0; i < cnt; i++) {
226 struct dso *dso = dsos[i];
235 static int set_fd_limit(int n)
239 if (getrlimit(RLIMIT_NOFILE, &rlim))
242 pr_debug("file limit %ld, new %d\n", (long) rlim.rlim_cur, n);
245 return setrlimit(RLIMIT_NOFILE, &rlim);
248 int test__dso_data_cache(int subtest __maybe_unused)
250 struct machine machine;
251 long nr_end, nr = open_files_cnt();
252 int dso_cnt, limit, i, fd;
254 memset(&machine, 0, sizeof(machine));
256 /* set as system limit */
258 TEST_ASSERT_VAL("failed to set file limit", !set_fd_limit(limit));
260 /* and this is now our dso open FDs limit */
262 TEST_ASSERT_VAL("failed to create dsos\n",
263 !dsos__create(dso_cnt, TEST_FILE_SIZE));
265 for (i = 0; i < (dso_cnt - 1); i++) {
266 struct dso *dso = dsos[i];
269 * Open dsos via dso__data_fd(), it opens the data
270 * file and keep it open (unless open file limit).
272 fd = dso__data_fd(dso, &machine);
273 TEST_ASSERT_VAL("failed to get fd", fd > 0);
280 n = dso__data_read_offset(dso, &machine, 0, buf, BUFSIZE);
281 TEST_ASSERT_VAL("failed to read dso", n == BUFSIZE);
285 /* verify the first one is already open */
286 TEST_ASSERT_VAL("dsos[0] is not open", dsos[0]->data.fd != -1);
288 /* open +1 dso to reach the allowed limit */
289 fd = dso__data_fd(dsos[i], &machine);
290 TEST_ASSERT_VAL("failed to get fd", fd > 0);
292 /* should force the first one to be closed */
293 TEST_ASSERT_VAL("failed to close dsos[0]", dsos[0]->data.fd == -1);
295 /* cleanup everything */
296 dsos__delete(dso_cnt);
298 /* Make sure we did not leak any file descriptor. */
299 nr_end = open_files_cnt();
300 pr_debug("nr start %ld, nr stop %ld\n", nr, nr_end);
301 TEST_ASSERT_VAL("failed leadking files", nr == nr_end);
305 int test__dso_data_reopen(int subtest __maybe_unused)
307 struct machine machine;
308 long nr_end, nr = open_files_cnt();
311 #define dso_0 (dsos[0])
312 #define dso_1 (dsos[1])
313 #define dso_2 (dsos[2])
315 memset(&machine, 0, sizeof(machine));
319 * - create 3 dso objects
320 * - set process file descriptor limit to current
322 * - test that the first dso gets closed when we
323 * reach the files count limit
326 /* Make sure we are able to open 3 fds anyway */
327 TEST_ASSERT_VAL("failed to set file limit",
328 !set_fd_limit((nr + 3)));
330 TEST_ASSERT_VAL("failed to create dsos\n", !dsos__create(3, TEST_FILE_SIZE));
333 fd = dso__data_fd(dso_0, &machine);
334 TEST_ASSERT_VAL("failed to get fd", fd > 0);
337 fd = dso__data_fd(dso_1, &machine);
338 TEST_ASSERT_VAL("failed to get fd", fd > 0);
341 * open extra file descriptor and we just
342 * reached the files count limit
344 fd_extra = open("/dev/null", O_RDONLY);
345 TEST_ASSERT_VAL("failed to open extra fd", fd_extra > 0);
348 fd = dso__data_fd(dso_2, &machine);
349 TEST_ASSERT_VAL("failed to get fd", fd > 0);
352 * dso_0 should get closed, because we reached
353 * the file descriptor limit
355 TEST_ASSERT_VAL("failed to close dso_0", dso_0->data.fd == -1);
358 fd = dso__data_fd(dso_0, &machine);
359 TEST_ASSERT_VAL("failed to get fd", fd > 0);
362 * dso_1 should get closed, because we reached
363 * the file descriptor limit
365 TEST_ASSERT_VAL("failed to close dso_1", dso_1->data.fd == -1);
367 /* cleanup everything */
371 /* Make sure we did not leak any file descriptor. */
372 nr_end = open_files_cnt();
373 pr_debug("nr start %ld, nr stop %ld\n", nr, nr_end);
374 TEST_ASSERT_VAL("failed leadking files", nr == nr_end);