~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/perf/tests/dso-data.c

Version: ~ [ linux-5.15-rc5 ] ~ [ linux-5.14.11 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.72 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.152 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.210 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.250 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.286 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.288 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 #include <dirent.h>
  2 #include <stdlib.h>
  3 #include <linux/kernel.h>
  4 #include <linux/types.h>
  5 #include <sys/stat.h>
  6 #include <fcntl.h>
  7 #include <string.h>
  8 #include <sys/time.h>
  9 #include <sys/resource.h>
 10 #include <api/fs/fs.h>
 11 #include "util.h"
 12 #include "machine.h"
 13 #include "symbol.h"
 14 #include "tests.h"
 15 #include "debug.h"
 16 
 17 static char *test_file(int size)
 18 {
 19 #define TEMPL "/tmp/perf-test-XXXXXX"
 20         static char buf_templ[sizeof(TEMPL)];
 21         char *templ = buf_templ;
 22         int fd, i;
 23         unsigned char *buf;
 24 
 25         strcpy(buf_templ, TEMPL);
 26 #undef TEMPL
 27 
 28         fd = mkstemp(templ);
 29         if (fd < 0) {
 30                 perror("mkstemp failed");
 31                 return NULL;
 32         }
 33 
 34         buf = malloc(size);
 35         if (!buf) {
 36                 close(fd);
 37                 return NULL;
 38         }
 39 
 40         for (i = 0; i < size; i++)
 41                 buf[i] = (unsigned char) ((int) i % 10);
 42 
 43         if (size != write(fd, buf, size))
 44                 templ = NULL;
 45 
 46         free(buf);
 47         close(fd);
 48         return templ;
 49 }
 50 
 51 #define TEST_FILE_SIZE (DSO__DATA_CACHE_SIZE * 20)
 52 
 53 struct test_data_offset {
 54         off_t offset;
 55         u8 data[10];
 56         int size;
 57 };
 58 
 59 struct test_data_offset offsets[] = {
 60         /* Fill first cache page. */
 61         {
 62                 .offset = 10,
 63                 .data   = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
 64                 .size   = 10,
 65         },
 66         /* Read first cache page. */
 67         {
 68                 .offset = 10,
 69                 .data   = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
 70                 .size   = 10,
 71         },
 72         /* Fill cache boundary pages. */
 73         {
 74                 .offset = DSO__DATA_CACHE_SIZE - DSO__DATA_CACHE_SIZE % 10,
 75                 .data   = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
 76                 .size   = 10,
 77         },
 78         /* Read cache boundary pages. */
 79         {
 80                 .offset = DSO__DATA_CACHE_SIZE - DSO__DATA_CACHE_SIZE % 10,
 81                 .data   = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
 82                 .size   = 10,
 83         },
 84         /* Fill final cache page. */
 85         {
 86                 .offset = TEST_FILE_SIZE - 10,
 87                 .data   = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
 88                 .size   = 10,
 89         },
 90         /* Read final cache page. */
 91         {
 92                 .offset = TEST_FILE_SIZE - 10,
 93                 .data   = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
 94                 .size   = 10,
 95         },
 96         /* Read final cache page. */
 97         {
 98                 .offset = TEST_FILE_SIZE - 3,
 99                 .data   = { 7, 8, 9, 0, 0, 0, 0, 0, 0, 0 },
100                 .size   = 3,
101         },
102 };
103 
104 /* move it from util/dso.c for compatibility */
105 static int dso__data_fd(struct dso *dso, struct machine *machine)
106 {
107         int fd = dso__data_get_fd(dso, machine);
108 
109         if (fd >= 0)
110                 dso__data_put_fd(dso);
111 
112         return fd;
113 }
114 
115 int test__dso_data(int subtest __maybe_unused)
116 {
117         struct machine machine;
118         struct dso *dso;
119         char *file = test_file(TEST_FILE_SIZE);
120         size_t i;
121 
122         TEST_ASSERT_VAL("No test file", file);
123 
124         memset(&machine, 0, sizeof(machine));
125 
126         dso = dso__new((const char *)file);
127 
128         TEST_ASSERT_VAL("Failed to access to dso",
129                         dso__data_fd(dso, &machine) >= 0);
130 
131         /* Basic 10 bytes tests. */
132         for (i = 0; i < ARRAY_SIZE(offsets); i++) {
133                 struct test_data_offset *data = &offsets[i];
134                 ssize_t size;
135                 u8 buf[10];
136 
137                 memset(buf, 0, 10);
138                 size = dso__data_read_offset(dso, &machine, data->offset,
139                                      buf, 10);
140 
141                 TEST_ASSERT_VAL("Wrong size", size == data->size);
142                 TEST_ASSERT_VAL("Wrong data", !memcmp(buf, data->data, 10));
143         }
144 
145         /* Read cross multiple cache pages. */
146         {
147                 ssize_t size;
148                 int c;
149                 u8 *buf;
150 
151                 buf = malloc(TEST_FILE_SIZE);
152                 TEST_ASSERT_VAL("ENOMEM\n", buf);
153 
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);
159 
160                         TEST_ASSERT_VAL("Wrong size",
161                                 size == (TEST_FILE_SIZE - 10));
162 
163                         for (i = 0; i < (size_t)size; i++)
164                                 TEST_ASSERT_VAL("Wrong data",
165                                         buf[i] == (i % 10));
166                 }
167 
168                 free(buf);
169         }
170 
171         dso__put(dso);
172         unlink(file);
173         return 0;
174 }
175 
176 static long open_files_cnt(void)
177 {
178         char path[PATH_MAX];
179         struct dirent *dent;
180         DIR *dir;
181         long nr = 0;
182 
183         scnprintf(path, PATH_MAX, "%s/self/fd", procfs__mountpoint());
184         pr_debug("fd path: %s\n", path);
185 
186         dir = opendir(path);
187         TEST_ASSERT_VAL("failed to open fd directory", dir);
188 
189         while ((dent = readdir(dir)) != NULL) {
190                 if (!strcmp(dent->d_name, ".") ||
191                     !strcmp(dent->d_name, ".."))
192                         continue;
193 
194                 nr++;
195         }
196 
197         closedir(dir);
198         return nr - 1;
199 }
200 
201 static struct dso **dsos;
202 
203 static int dsos__create(int cnt, int size)
204 {
205         int i;
206 
207         dsos = malloc(sizeof(*dsos) * cnt);
208         TEST_ASSERT_VAL("failed to alloc dsos array", dsos);
209 
210         for (i = 0; i < cnt; i++) {
211                 char *file;
212 
213                 file = test_file(size);
214                 TEST_ASSERT_VAL("failed to get dso file", file);
215 
216                 dsos[i] = dso__new(file);
217                 TEST_ASSERT_VAL("failed to get dso", dsos[i]);
218         }
219 
220         return 0;
221 }
222 
223 static void dsos__delete(int cnt)
224 {
225         int i;
226 
227         for (i = 0; i < cnt; i++) {
228                 struct dso *dso = dsos[i];
229 
230                 unlink(dso->name);
231                 dso__put(dso);
232         }
233 
234         free(dsos);
235 }
236 
237 static int set_fd_limit(int n)
238 {
239         struct rlimit rlim;
240 
241         if (getrlimit(RLIMIT_NOFILE, &rlim))
242                 return -1;
243 
244         pr_debug("file limit %ld, new %d\n", (long) rlim.rlim_cur, n);
245 
246         rlim.rlim_cur = n;
247         return setrlimit(RLIMIT_NOFILE, &rlim);
248 }
249 
250 int test__dso_data_cache(int subtest __maybe_unused)
251 {
252         struct machine machine;
253         long nr_end, nr = open_files_cnt();
254         int dso_cnt, limit, i, fd;
255 
256         /* Rest the internal dso open counter limit. */
257         reset_fd_limit();
258 
259         memset(&machine, 0, sizeof(machine));
260 
261         /* set as system limit */
262         limit = nr * 4;
263         TEST_ASSERT_VAL("failed to set file limit", !set_fd_limit(limit));
264 
265         /* and this is now our dso open FDs limit */
266         dso_cnt = limit / 2;
267         TEST_ASSERT_VAL("failed to create dsos\n",
268                 !dsos__create(dso_cnt, TEST_FILE_SIZE));
269 
270         for (i = 0; i < (dso_cnt - 1); i++) {
271                 struct dso *dso = dsos[i];
272 
273                 /*
274                  * Open dsos via dso__data_fd(), it opens the data
275                  * file and keep it open (unless open file limit).
276                  */
277                 fd = dso__data_fd(dso, &machine);
278                 TEST_ASSERT_VAL("failed to get fd", fd > 0);
279 
280                 if (i % 2) {
281                         #define BUFSIZE 10
282                         u8 buf[BUFSIZE];
283                         ssize_t n;
284 
285                         n = dso__data_read_offset(dso, &machine, 0, buf, BUFSIZE);
286                         TEST_ASSERT_VAL("failed to read dso", n == BUFSIZE);
287                 }
288         }
289 
290         /* verify the first one is already open */
291         TEST_ASSERT_VAL("dsos[0] is not open", dsos[0]->data.fd != -1);
292 
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);
296 
297         /* should force the first one to be closed */
298         TEST_ASSERT_VAL("failed to close dsos[0]", dsos[0]->data.fd == -1);
299 
300         /* cleanup everything */
301         dsos__delete(dso_cnt);
302 
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);
307         return 0;
308 }
309 
310 int test__dso_data_reopen(int subtest __maybe_unused)
311 {
312         struct machine machine;
313         long nr_end, nr = open_files_cnt();
314         int fd, fd_extra;
315 
316 #define dso_0 (dsos[0])
317 #define dso_1 (dsos[1])
318 #define dso_2 (dsos[2])
319 
320         /* Rest the internal dso open counter limit. */
321         reset_fd_limit();
322 
323         memset(&machine, 0, sizeof(machine));
324 
325         /*
326          * Test scenario:
327          * - create 3 dso objects
328          * - set process file descriptor limit to current
329          *   files count + 3
330          * - test that the first dso gets closed when we
331          *   reach the files count limit
332          */
333 
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)));
337 
338         TEST_ASSERT_VAL("failed to create dsos\n", !dsos__create(3, TEST_FILE_SIZE));
339 
340         /* open dso_0 */
341         fd = dso__data_fd(dso_0, &machine);
342         TEST_ASSERT_VAL("failed to get fd", fd > 0);
343 
344         /* open dso_1 */
345         fd = dso__data_fd(dso_1, &machine);
346         TEST_ASSERT_VAL("failed to get fd", fd > 0);
347 
348         /*
349          * open extra file descriptor and we just
350          * reached the files count limit
351          */
352         fd_extra = open("/dev/null", O_RDONLY);
353         TEST_ASSERT_VAL("failed to open extra fd", fd_extra > 0);
354 
355         /* open dso_2 */
356         fd = dso__data_fd(dso_2, &machine);
357         TEST_ASSERT_VAL("failed to get fd", fd > 0);
358 
359         /*
360          * dso_0 should get closed, because we reached
361          * the file descriptor limit
362          */
363         TEST_ASSERT_VAL("failed to close dso_0", dso_0->data.fd == -1);
364 
365         /* open dso_0 */
366         fd = dso__data_fd(dso_0, &machine);
367         TEST_ASSERT_VAL("failed to get fd", fd > 0);
368 
369         /*
370          * dso_1 should get closed, because we reached
371          * the file descriptor limit
372          */
373         TEST_ASSERT_VAL("failed to close dso_1", dso_1->data.fd == -1);
374 
375         /* cleanup everything */
376         close(fd_extra);
377         dsos__delete(3);
378 
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);
383         return 0;
384 }
385 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp