2 #include <linux/types.h>
7 #include <sys/resource.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;
22 strcpy(buf_templ, TEMPL);
27 perror("mkstemp failed");
37 for (i = 0; i < size; i++)
38 buf[i] = (unsigned char) ((int) i % 10);
40 if (size != write(fd, buf, size))
48 #define TEST_FILE_SIZE (DSO__DATA_CACHE_SIZE * 20)
50 struct test_data_offset {
56 struct test_data_offset offsets[] = {
57 /* Fill first cache page. */
60 .data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
63 /* Read first cache page. */
66 .data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
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 },
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 },
81 /* Fill final cache page. */
83 .offset = TEST_FILE_SIZE - 10,
84 .data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
87 /* Read final cache page. */
89 .offset = TEST_FILE_SIZE - 10,
90 .data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
93 /* Read final cache page. */
95 .offset = TEST_FILE_SIZE - 3,
96 .data = { 7, 8, 9, 0, 0, 0, 0, 0, 0, 0 },
101 int test__dso_data(void)
103 struct machine machine;
105 char *file = test_file(TEST_FILE_SIZE);
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];
121 size = dso__data_read_offset(dso, &machine, data->offset,
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. */
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",
159 static long open_files_cnt(void)
166 scnprintf(path, PATH_MAX, "%s/self/fd", procfs__mountpoint());
167 pr_debug("fd path: %s\n", 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, ".."))
184 static struct dso **dsos;
186 static int dsos__create(int cnt, int size)
190 dsos = malloc(sizeof(dsos) * cnt);
191 TEST_ASSERT_VAL("failed to alloc dsos array", dsos);
193 for (i = 0; i < cnt; i++) {
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]);
206 static void dsos__delete(int cnt)
210 for (i = 0; i < cnt; i++) {
211 struct dso *dso = dsos[i];
220 static int set_fd_limit(int n)
224 if (getrlimit(RLIMIT_NOFILE, &rlim))
227 pr_debug("file limit %ld, new %d\n", (long) 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 */
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.
258 fd = dso__data_fd(dso, &machine);
259 TEST_ASSERT_VAL("failed to get fd", fd > 0);
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);