1 // SPDX-License-Identifier: GPL-2.0
4 #include <linux/kernel.h>
5 #include <linux/types.h>
10 #include <sys/resource.h>
11 #include <api/fs/fs.h>
18 static char *test_file(int size)
20 #define TEMPL "/tmp/perf-test-XXXXXX"
21 static char buf_templ[sizeof(TEMPL)];
22 char *templ = buf_templ;
26 strcpy(buf_templ, TEMPL);
31 perror("mkstemp failed");
41 for (i = 0; i < size; i++)
42 buf[i] = (unsigned char) ((int) i % 10);
44 if (size != write(fd, buf, size))
52 #define TEST_FILE_SIZE (DSO__DATA_CACHE_SIZE * 20)
54 struct test_data_offset {
60 struct test_data_offset offsets[] = {
61 /* Fill first cache page. */
64 .data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
67 /* Read first cache page. */
70 .data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
73 /* Fill cache boundary pages. */
75 .offset = DSO__DATA_CACHE_SIZE - DSO__DATA_CACHE_SIZE % 10,
76 .data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
79 /* Read cache boundary pages. */
81 .offset = DSO__DATA_CACHE_SIZE - DSO__DATA_CACHE_SIZE % 10,
82 .data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
85 /* Fill final cache page. */
87 .offset = TEST_FILE_SIZE - 10,
88 .data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
91 /* Read final cache page. */
93 .offset = TEST_FILE_SIZE - 10,
94 .data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
97 /* Read final cache page. */
99 .offset = TEST_FILE_SIZE - 3,
100 .data = { 7, 8, 9, 0, 0, 0, 0, 0, 0, 0 },
105 /* move it from util/dso.c for compatibility */
106 static int dso__data_fd(struct dso *dso, struct machine *machine)
108 int fd = dso__data_get_fd(dso, machine);
111 dso__data_put_fd(dso);
116 static int test__dso_data(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
118 struct machine machine;
120 char *file = test_file(TEST_FILE_SIZE);
123 TEST_ASSERT_VAL("No test file", file);
125 memset(&machine, 0, sizeof(machine));
127 dso = dso__new((const char *)file);
129 TEST_ASSERT_VAL("Failed to access to dso",
130 dso__data_fd(dso, &machine) >= 0);
132 /* Basic 10 bytes tests. */
133 for (i = 0; i < ARRAY_SIZE(offsets); i++) {
134 struct test_data_offset *data = &offsets[i];
139 size = dso__data_read_offset(dso, &machine, data->offset,
142 TEST_ASSERT_VAL("Wrong size", size == data->size);
143 TEST_ASSERT_VAL("Wrong data", !memcmp(buf, data->data, 10));
146 /* Read cross multiple cache pages. */
152 buf = malloc(TEST_FILE_SIZE);
153 TEST_ASSERT_VAL("ENOMEM\n", buf);
155 /* First iteration to fill caches, second one to read them. */
156 for (c = 0; c < 2; c++) {
157 memset(buf, 0, TEST_FILE_SIZE);
158 size = dso__data_read_offset(dso, &machine, 10,
159 buf, TEST_FILE_SIZE);
161 TEST_ASSERT_VAL("Wrong size",
162 size == (TEST_FILE_SIZE - 10));
164 for (i = 0; i < (size_t)size; i++)
165 TEST_ASSERT_VAL("Wrong data",
177 static long open_files_cnt(void)
184 scnprintf(path, PATH_MAX, "%s/self/fd", procfs__mountpoint());
185 pr_debug("fd path: %s\n", path);
188 TEST_ASSERT_VAL("failed to open fd directory", dir);
190 while ((dent = readdir(dir)) != NULL) {
191 if (!strcmp(dent->d_name, ".") ||
192 !strcmp(dent->d_name, ".."))
202 static struct dso **dsos;
204 static int dsos__create(int cnt, int size)
208 dsos = malloc(sizeof(*dsos) * cnt);
209 TEST_ASSERT_VAL("failed to alloc dsos array", dsos);
211 for (i = 0; i < cnt; i++) {
214 file = test_file(size);
215 TEST_ASSERT_VAL("failed to get dso file", file);
217 dsos[i] = dso__new(file);
218 TEST_ASSERT_VAL("failed to get dso", dsos[i]);
224 static void dsos__delete(int cnt)
228 for (i = 0; i < cnt; i++) {
229 struct dso *dso = dsos[i];
238 static int set_fd_limit(int n)
242 if (getrlimit(RLIMIT_NOFILE, &rlim))
245 pr_debug("file limit %ld, new %d\n", (long) rlim.rlim_cur, n);
248 return setrlimit(RLIMIT_NOFILE, &rlim);
251 static int test__dso_data_cache(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
253 struct machine machine;
254 long nr_end, nr = open_files_cnt();
255 int dso_cnt, limit, i, fd;
257 /* Rest the internal dso open counter limit. */
260 memset(&machine, 0, sizeof(machine));
262 /* set as system limit */
264 TEST_ASSERT_VAL("failed to set file limit", !set_fd_limit(limit));
266 /* and this is now our dso open FDs limit */
268 TEST_ASSERT_VAL("failed to create dsos\n",
269 !dsos__create(dso_cnt, TEST_FILE_SIZE));
271 for (i = 0; i < (dso_cnt - 1); i++) {
272 struct dso *dso = dsos[i];
275 * Open dsos via dso__data_fd(), it opens the data
276 * file and keep it open (unless open file limit).
278 fd = dso__data_fd(dso, &machine);
279 TEST_ASSERT_VAL("failed to get fd", fd > 0);
286 n = dso__data_read_offset(dso, &machine, 0, buf, BUFSIZE);
287 TEST_ASSERT_VAL("failed to read dso", n == BUFSIZE);
291 /* verify the first one is already open */
292 TEST_ASSERT_VAL("dsos[0] is not open", dsos[0]->data.fd != -1);
294 /* open +1 dso to reach the allowed limit */
295 fd = dso__data_fd(dsos[i], &machine);
296 TEST_ASSERT_VAL("failed to get fd", fd > 0);
298 /* should force the first one to be closed */
299 TEST_ASSERT_VAL("failed to close dsos[0]", dsos[0]->data.fd == -1);
301 /* cleanup everything */
302 dsos__delete(dso_cnt);
304 /* Make sure we did not leak any file descriptor. */
305 nr_end = open_files_cnt();
306 pr_debug("nr start %ld, nr stop %ld\n", nr, nr_end);
307 TEST_ASSERT_VAL("failed leaking files", nr == nr_end);
311 static long new_limit(int count)
313 int fd = open("/dev/null", O_RDONLY);
316 ret = new_limit(--count);
321 static int test__dso_data_reopen(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
323 struct machine machine;
324 long nr_end, nr = open_files_cnt(), lim = new_limit(3);
327 #define dso_0 (dsos[0])
328 #define dso_1 (dsos[1])
329 #define dso_2 (dsos[2])
331 /* Rest the internal dso open counter limit. */
334 memset(&machine, 0, sizeof(machine));
338 * - create 3 dso objects
339 * - set process file descriptor limit to current
341 * - test that the first dso gets closed when we
342 * reach the files count limit
345 /* Make sure we are able to open 3 fds anyway */
346 TEST_ASSERT_VAL("failed to set file limit",
347 !set_fd_limit((lim)));
349 TEST_ASSERT_VAL("failed to create dsos\n", !dsos__create(3, TEST_FILE_SIZE));
352 fd = dso__data_fd(dso_0, &machine);
353 TEST_ASSERT_VAL("failed to get fd", fd > 0);
356 fd = dso__data_fd(dso_1, &machine);
357 TEST_ASSERT_VAL("failed to get fd", fd > 0);
360 * open extra file descriptor and we just
361 * reached the files count limit
363 fd_extra = open("/dev/null", O_RDONLY);
364 TEST_ASSERT_VAL("failed to open extra fd", fd_extra > 0);
367 fd = dso__data_fd(dso_2, &machine);
368 TEST_ASSERT_VAL("failed to get fd", fd > 0);
371 * dso_0 should get closed, because we reached
372 * the file descriptor limit
374 TEST_ASSERT_VAL("failed to close dso_0", dso_0->data.fd == -1);
377 fd = dso__data_fd(dso_0, &machine);
378 TEST_ASSERT_VAL("failed to get fd", fd > 0);
381 * dso_1 should get closed, because we reached
382 * the file descriptor limit
384 TEST_ASSERT_VAL("failed to close dso_1", dso_1->data.fd == -1);
386 /* cleanup everything */
390 /* Make sure we did not leak any file descriptor. */
391 nr_end = open_files_cnt();
392 pr_debug("nr start %ld, nr stop %ld\n", nr, nr_end);
393 TEST_ASSERT_VAL("failed leaking files", nr == nr_end);
397 DEFINE_SUITE("DSO data read", dso_data);
398 DEFINE_SUITE("DSO data cache", dso_data_cache);
399 DEFINE_SUITE("DSO data reopen", dso_data_reopen);