3 #include <linux/kernel.h>
4 #include <linux/types.h>
9 #include <sys/resource.h>
10 #include <api/fs/fs.h>
17 static char *test_file(int size)
19 #define TEMPL "/tmp/perf-test-XXXXXX"
20 static char buf_templ[sizeof(TEMPL)];
21 char *templ = buf_templ;
25 strcpy(buf_templ, TEMPL);
30 perror("mkstemp failed");
40 for (i = 0; i < size; i++)
41 buf[i] = (unsigned char) ((int) i % 10);
43 if (size != write(fd, buf, size))
51 #define TEST_FILE_SIZE (DSO__DATA_CACHE_SIZE * 20)
53 struct test_data_offset {
59 struct test_data_offset offsets[] = {
60 /* Fill first cache page. */
63 .data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
66 /* Read first cache page. */
69 .data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
72 /* Fill cache boundary pages. */
74 .offset = DSO__DATA_CACHE_SIZE - DSO__DATA_CACHE_SIZE % 10,
75 .data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
78 /* Read cache boundary pages. */
80 .offset = DSO__DATA_CACHE_SIZE - DSO__DATA_CACHE_SIZE % 10,
81 .data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
84 /* Fill final cache page. */
86 .offset = TEST_FILE_SIZE - 10,
87 .data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
90 /* Read final cache page. */
92 .offset = TEST_FILE_SIZE - 10,
93 .data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
96 /* Read final cache page. */
98 .offset = TEST_FILE_SIZE - 3,
99 .data = { 7, 8, 9, 0, 0, 0, 0, 0, 0, 0 },
104 /* move it from util/dso.c for compatibility */
105 static int dso__data_fd(struct dso *dso, struct machine *machine)
107 int fd = dso__data_get_fd(dso, machine);
110 dso__data_put_fd(dso);
115 int test__dso_data(int subtest __maybe_unused)
117 struct machine machine;
119 char *file = test_file(TEST_FILE_SIZE);
122 TEST_ASSERT_VAL("No test file", file);
124 memset(&machine, 0, sizeof(machine));
126 dso = dso__new((const char *)file);
128 TEST_ASSERT_VAL("Failed to access to dso",
129 dso__data_fd(dso, &machine) >= 0);
131 /* Basic 10 bytes tests. */
132 for (i = 0; i < ARRAY_SIZE(offsets); i++) {
133 struct test_data_offset *data = &offsets[i];
138 size = dso__data_read_offset(dso, &machine, data->offset,
141 TEST_ASSERT_VAL("Wrong size", size == data->size);
142 TEST_ASSERT_VAL("Wrong data", !memcmp(buf, data->data, 10));
145 /* Read cross multiple cache pages. */
151 buf = malloc(TEST_FILE_SIZE);
152 TEST_ASSERT_VAL("ENOMEM\n", buf);
154 /* First iteration to fill caches, second one to read them. */
155 for (c = 0; c < 2; c++) {
156 memset(buf, 0, TEST_FILE_SIZE);
157 size = dso__data_read_offset(dso, &machine, 10,
158 buf, TEST_FILE_SIZE);
160 TEST_ASSERT_VAL("Wrong size",
161 size == (TEST_FILE_SIZE - 10));
163 for (i = 0; i < (size_t)size; i++)
164 TEST_ASSERT_VAL("Wrong data",
176 static long open_files_cnt(void)
183 scnprintf(path, PATH_MAX, "%s/self/fd", procfs__mountpoint());
184 pr_debug("fd path: %s\n", path);
187 TEST_ASSERT_VAL("failed to open fd directory", dir);
189 while ((dent = readdir(dir)) != NULL) {
190 if (!strcmp(dent->d_name, ".") ||
191 !strcmp(dent->d_name, ".."))
201 static struct dso **dsos;
203 static int dsos__create(int cnt, int size)
207 dsos = malloc(sizeof(*dsos) * cnt);
208 TEST_ASSERT_VAL("failed to alloc dsos array", dsos);
210 for (i = 0; i < cnt; i++) {
213 file = test_file(size);
214 TEST_ASSERT_VAL("failed to get dso file", file);
216 dsos[i] = dso__new(file);
217 TEST_ASSERT_VAL("failed to get dso", dsos[i]);
223 static void dsos__delete(int cnt)
227 for (i = 0; i < cnt; i++) {
228 struct dso *dso = dsos[i];
237 static int set_fd_limit(int n)
241 if (getrlimit(RLIMIT_NOFILE, &rlim))
244 pr_debug("file limit %ld, new %d\n", (long) rlim.rlim_cur, n);
247 return setrlimit(RLIMIT_NOFILE, &rlim);
250 int test__dso_data_cache(int subtest __maybe_unused)
252 struct machine machine;
253 long nr_end, nr = open_files_cnt();
254 int dso_cnt, limit, i, fd;
256 /* Rest the internal dso open counter limit. */
259 memset(&machine, 0, sizeof(machine));
261 /* set as system limit */
263 TEST_ASSERT_VAL("failed to set file limit", !set_fd_limit(limit));
265 /* and this is now our dso open FDs limit */
267 TEST_ASSERT_VAL("failed to create dsos\n",
268 !dsos__create(dso_cnt, TEST_FILE_SIZE));
270 for (i = 0; i < (dso_cnt - 1); i++) {
271 struct dso *dso = dsos[i];
274 * Open dsos via dso__data_fd(), it opens the data
275 * file and keep it open (unless open file limit).
277 fd = dso__data_fd(dso, &machine);
278 TEST_ASSERT_VAL("failed to get fd", fd > 0);
285 n = dso__data_read_offset(dso, &machine, 0, buf, BUFSIZE);
286 TEST_ASSERT_VAL("failed to read dso", n == BUFSIZE);
290 /* verify the first one is already open */
291 TEST_ASSERT_VAL("dsos[0] is not open", dsos[0]->data.fd != -1);
293 /* open +1 dso to reach the allowed limit */
294 fd = dso__data_fd(dsos[i], &machine);
295 TEST_ASSERT_VAL("failed to get fd", fd > 0);
297 /* should force the first one to be closed */
298 TEST_ASSERT_VAL("failed to close dsos[0]", dsos[0]->data.fd == -1);
300 /* cleanup everything */
301 dsos__delete(dso_cnt);
303 /* Make sure we did not leak any file descriptor. */
304 nr_end = open_files_cnt();
305 pr_debug("nr start %ld, nr stop %ld\n", nr, nr_end);
306 TEST_ASSERT_VAL("failed leadking files", nr == nr_end);
310 int test__dso_data_reopen(int subtest __maybe_unused)
312 struct machine machine;
313 long nr_end, nr = open_files_cnt();
316 #define dso_0 (dsos[0])
317 #define dso_1 (dsos[1])
318 #define dso_2 (dsos[2])
320 /* Rest the internal dso open counter limit. */
323 memset(&machine, 0, sizeof(machine));
327 * - create 3 dso objects
328 * - set process file descriptor limit to current
330 * - test that the first dso gets closed when we
331 * reach the files count limit
334 /* Make sure we are able to open 3 fds anyway */
335 TEST_ASSERT_VAL("failed to set file limit",
336 !set_fd_limit((nr + 3)));
338 TEST_ASSERT_VAL("failed to create dsos\n", !dsos__create(3, TEST_FILE_SIZE));
341 fd = dso__data_fd(dso_0, &machine);
342 TEST_ASSERT_VAL("failed to get fd", fd > 0);
345 fd = dso__data_fd(dso_1, &machine);
346 TEST_ASSERT_VAL("failed to get fd", fd > 0);
349 * open extra file descriptor and we just
350 * reached the files count limit
352 fd_extra = open("/dev/null", O_RDONLY);
353 TEST_ASSERT_VAL("failed to open extra fd", fd_extra > 0);
356 fd = dso__data_fd(dso_2, &machine);
357 TEST_ASSERT_VAL("failed to get fd", fd > 0);
360 * dso_0 should get closed, because we reached
361 * the file descriptor limit
363 TEST_ASSERT_VAL("failed to close dso_0", dso_0->data.fd == -1);
366 fd = dso__data_fd(dso_0, &machine);
367 TEST_ASSERT_VAL("failed to get fd", fd > 0);
370 * dso_1 should get closed, because we reached
371 * the file descriptor limit
373 TEST_ASSERT_VAL("failed to close dso_1", dso_1->data.fd == -1);
375 /* cleanup everything */
379 /* Make sure we did not leak any file descriptor. */
380 nr_end = open_files_cnt();
381 pr_debug("nr start %ld, nr stop %ld\n", nr, nr_end);
382 TEST_ASSERT_VAL("failed leadking files", nr == nr_end);