Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #include <dirent.h>
0003 #include <stdlib.h>
0004 #include <linux/kernel.h>
0005 #include <linux/types.h>
0006 #include <sys/stat.h>
0007 #include <fcntl.h>
0008 #include <string.h>
0009 #include <sys/time.h>
0010 #include <sys/resource.h>
0011 #include <api/fs/fs.h>
0012 #include "dso.h"
0013 #include "machine.h"
0014 #include "symbol.h"
0015 #include "tests.h"
0016 #include "debug.h"
0017 
0018 static char *test_file(int size)
0019 {
0020 #define TEMPL "/tmp/perf-test-XXXXXX"
0021     static char buf_templ[sizeof(TEMPL)];
0022     char *templ = buf_templ;
0023     int fd, i;
0024     unsigned char *buf;
0025 
0026     strcpy(buf_templ, TEMPL);
0027 #undef TEMPL
0028 
0029     fd = mkstemp(templ);
0030     if (fd < 0) {
0031         perror("mkstemp failed");
0032         return NULL;
0033     }
0034 
0035     buf = malloc(size);
0036     if (!buf) {
0037         close(fd);
0038         return NULL;
0039     }
0040 
0041     for (i = 0; i < size; i++)
0042         buf[i] = (unsigned char) ((int) i % 10);
0043 
0044     if (size != write(fd, buf, size))
0045         templ = NULL;
0046 
0047     free(buf);
0048     close(fd);
0049     return templ;
0050 }
0051 
0052 #define TEST_FILE_SIZE (DSO__DATA_CACHE_SIZE * 20)
0053 
0054 struct test_data_offset {
0055     off_t offset;
0056     u8 data[10];
0057     int size;
0058 };
0059 
0060 struct test_data_offset offsets[] = {
0061     /* Fill first cache page. */
0062     {
0063         .offset = 10,
0064         .data   = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
0065         .size   = 10,
0066     },
0067     /* Read first cache page. */
0068     {
0069         .offset = 10,
0070         .data   = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
0071         .size   = 10,
0072     },
0073     /* Fill cache boundary pages. */
0074     {
0075         .offset = DSO__DATA_CACHE_SIZE - DSO__DATA_CACHE_SIZE % 10,
0076         .data   = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
0077         .size   = 10,
0078     },
0079     /* Read cache boundary pages. */
0080     {
0081         .offset = DSO__DATA_CACHE_SIZE - DSO__DATA_CACHE_SIZE % 10,
0082         .data   = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
0083         .size   = 10,
0084     },
0085     /* Fill final cache page. */
0086     {
0087         .offset = TEST_FILE_SIZE - 10,
0088         .data   = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
0089         .size   = 10,
0090     },
0091     /* Read final cache page. */
0092     {
0093         .offset = TEST_FILE_SIZE - 10,
0094         .data   = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
0095         .size   = 10,
0096     },
0097     /* Read final cache page. */
0098     {
0099         .offset = TEST_FILE_SIZE - 3,
0100         .data   = { 7, 8, 9, 0, 0, 0, 0, 0, 0, 0 },
0101         .size   = 3,
0102     },
0103 };
0104 
0105 /* move it from util/dso.c for compatibility */
0106 static int dso__data_fd(struct dso *dso, struct machine *machine)
0107 {
0108     int fd = dso__data_get_fd(dso, machine);
0109 
0110     if (fd >= 0)
0111         dso__data_put_fd(dso);
0112 
0113     return fd;
0114 }
0115 
0116 static int test__dso_data(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
0117 {
0118     struct machine machine;
0119     struct dso *dso;
0120     char *file = test_file(TEST_FILE_SIZE);
0121     size_t i;
0122 
0123     TEST_ASSERT_VAL("No test file", file);
0124 
0125     memset(&machine, 0, sizeof(machine));
0126 
0127     dso = dso__new((const char *)file);
0128 
0129     TEST_ASSERT_VAL("Failed to access to dso",
0130             dso__data_fd(dso, &machine) >= 0);
0131 
0132     /* Basic 10 bytes tests. */
0133     for (i = 0; i < ARRAY_SIZE(offsets); i++) {
0134         struct test_data_offset *data = &offsets[i];
0135         ssize_t size;
0136         u8 buf[10];
0137 
0138         memset(buf, 0, 10);
0139         size = dso__data_read_offset(dso, &machine, data->offset,
0140                      buf, 10);
0141 
0142         TEST_ASSERT_VAL("Wrong size", size == data->size);
0143         TEST_ASSERT_VAL("Wrong data", !memcmp(buf, data->data, 10));
0144     }
0145 
0146     /* Read cross multiple cache pages. */
0147     {
0148         ssize_t size;
0149         int c;
0150         u8 *buf;
0151 
0152         buf = malloc(TEST_FILE_SIZE);
0153         TEST_ASSERT_VAL("ENOMEM\n", buf);
0154 
0155         /* First iteration to fill caches, second one to read them. */
0156         for (c = 0; c < 2; c++) {
0157             memset(buf, 0, TEST_FILE_SIZE);
0158             size = dso__data_read_offset(dso, &machine, 10,
0159                              buf, TEST_FILE_SIZE);
0160 
0161             TEST_ASSERT_VAL("Wrong size",
0162                 size == (TEST_FILE_SIZE - 10));
0163 
0164             for (i = 0; i < (size_t)size; i++)
0165                 TEST_ASSERT_VAL("Wrong data",
0166                     buf[i] == (i % 10));
0167         }
0168 
0169         free(buf);
0170     }
0171 
0172     dso__put(dso);
0173     unlink(file);
0174     return 0;
0175 }
0176 
0177 static long open_files_cnt(void)
0178 {
0179     char path[PATH_MAX];
0180     struct dirent *dent;
0181     DIR *dir;
0182     long nr = 0;
0183 
0184     scnprintf(path, PATH_MAX, "%s/self/fd", procfs__mountpoint());
0185     pr_debug("fd path: %s\n", path);
0186 
0187     dir = opendir(path);
0188     TEST_ASSERT_VAL("failed to open fd directory", dir);
0189 
0190     while ((dent = readdir(dir)) != NULL) {
0191         if (!strcmp(dent->d_name, ".") ||
0192             !strcmp(dent->d_name, ".."))
0193             continue;
0194 
0195         nr++;
0196     }
0197 
0198     closedir(dir);
0199     return nr - 1;
0200 }
0201 
0202 static struct dso **dsos;
0203 
0204 static int dsos__create(int cnt, int size)
0205 {
0206     int i;
0207 
0208     dsos = malloc(sizeof(*dsos) * cnt);
0209     TEST_ASSERT_VAL("failed to alloc dsos array", dsos);
0210 
0211     for (i = 0; i < cnt; i++) {
0212         char *file;
0213 
0214         file = test_file(size);
0215         TEST_ASSERT_VAL("failed to get dso file", file);
0216 
0217         dsos[i] = dso__new(file);
0218         TEST_ASSERT_VAL("failed to get dso", dsos[i]);
0219     }
0220 
0221     return 0;
0222 }
0223 
0224 static void dsos__delete(int cnt)
0225 {
0226     int i;
0227 
0228     for (i = 0; i < cnt; i++) {
0229         struct dso *dso = dsos[i];
0230 
0231         unlink(dso->name);
0232         dso__put(dso);
0233     }
0234 
0235     free(dsos);
0236 }
0237 
0238 static int set_fd_limit(int n)
0239 {
0240     struct rlimit rlim;
0241 
0242     if (getrlimit(RLIMIT_NOFILE, &rlim))
0243         return -1;
0244 
0245     pr_debug("file limit %ld, new %d\n", (long) rlim.rlim_cur, n);
0246 
0247     rlim.rlim_cur = n;
0248     return setrlimit(RLIMIT_NOFILE, &rlim);
0249 }
0250 
0251 static int test__dso_data_cache(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
0252 {
0253     struct machine machine;
0254     long nr_end, nr = open_files_cnt();
0255     int dso_cnt, limit, i, fd;
0256 
0257     /* Rest the internal dso open counter limit. */
0258     reset_fd_limit();
0259 
0260     memset(&machine, 0, sizeof(machine));
0261 
0262     /* set as system limit */
0263     limit = nr * 4;
0264     TEST_ASSERT_VAL("failed to set file limit", !set_fd_limit(limit));
0265 
0266     /* and this is now our dso open FDs limit */
0267     dso_cnt = limit / 2;
0268     TEST_ASSERT_VAL("failed to create dsos\n",
0269         !dsos__create(dso_cnt, TEST_FILE_SIZE));
0270 
0271     for (i = 0; i < (dso_cnt - 1); i++) {
0272         struct dso *dso = dsos[i];
0273 
0274         /*
0275          * Open dsos via dso__data_fd(), it opens the data
0276          * file and keep it open (unless open file limit).
0277          */
0278         fd = dso__data_fd(dso, &machine);
0279         TEST_ASSERT_VAL("failed to get fd", fd > 0);
0280 
0281         if (i % 2) {
0282             #define BUFSIZE 10
0283             u8 buf[BUFSIZE];
0284             ssize_t n;
0285 
0286             n = dso__data_read_offset(dso, &machine, 0, buf, BUFSIZE);
0287             TEST_ASSERT_VAL("failed to read dso", n == BUFSIZE);
0288         }
0289     }
0290 
0291     /* verify the first one is already open */
0292     TEST_ASSERT_VAL("dsos[0] is not open", dsos[0]->data.fd != -1);
0293 
0294     /* open +1 dso to reach the allowed limit */
0295     fd = dso__data_fd(dsos[i], &machine);
0296     TEST_ASSERT_VAL("failed to get fd", fd > 0);
0297 
0298     /* should force the first one to be closed */
0299     TEST_ASSERT_VAL("failed to close dsos[0]", dsos[0]->data.fd == -1);
0300 
0301     /* cleanup everything */
0302     dsos__delete(dso_cnt);
0303 
0304     /* Make sure we did not leak any file descriptor. */
0305     nr_end = open_files_cnt();
0306     pr_debug("nr start %ld, nr stop %ld\n", nr, nr_end);
0307     TEST_ASSERT_VAL("failed leaking files", nr == nr_end);
0308     return 0;
0309 }
0310 
0311 static long new_limit(int count)
0312 {
0313     int fd = open("/dev/null", O_RDONLY);
0314     long ret = fd;
0315     if (count > 0)
0316         ret = new_limit(--count);
0317     close(fd);
0318     return ret;
0319 }
0320 
0321 static int test__dso_data_reopen(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
0322 {
0323     struct machine machine;
0324     long nr_end, nr = open_files_cnt(), lim = new_limit(3);
0325     int fd, fd_extra;
0326 
0327 #define dso_0 (dsos[0])
0328 #define dso_1 (dsos[1])
0329 #define dso_2 (dsos[2])
0330 
0331     /* Rest the internal dso open counter limit. */
0332     reset_fd_limit();
0333 
0334     memset(&machine, 0, sizeof(machine));
0335 
0336     /*
0337      * Test scenario:
0338      * - create 3 dso objects
0339      * - set process file descriptor limit to current
0340      *   files count + 3
0341      * - test that the first dso gets closed when we
0342      *   reach the files count limit
0343      */
0344 
0345     /* Make sure we are able to open 3 fds anyway */
0346     TEST_ASSERT_VAL("failed to set file limit",
0347             !set_fd_limit((lim)));
0348 
0349     TEST_ASSERT_VAL("failed to create dsos\n", !dsos__create(3, TEST_FILE_SIZE));
0350 
0351     /* open dso_0 */
0352     fd = dso__data_fd(dso_0, &machine);
0353     TEST_ASSERT_VAL("failed to get fd", fd > 0);
0354 
0355     /* open dso_1 */
0356     fd = dso__data_fd(dso_1, &machine);
0357     TEST_ASSERT_VAL("failed to get fd", fd > 0);
0358 
0359     /*
0360      * open extra file descriptor and we just
0361      * reached the files count limit
0362      */
0363     fd_extra = open("/dev/null", O_RDONLY);
0364     TEST_ASSERT_VAL("failed to open extra fd", fd_extra > 0);
0365 
0366     /* open dso_2 */
0367     fd = dso__data_fd(dso_2, &machine);
0368     TEST_ASSERT_VAL("failed to get fd", fd > 0);
0369 
0370     /*
0371      * dso_0 should get closed, because we reached
0372      * the file descriptor limit
0373      */
0374     TEST_ASSERT_VAL("failed to close dso_0", dso_0->data.fd == -1);
0375 
0376     /* open dso_0 */
0377     fd = dso__data_fd(dso_0, &machine);
0378     TEST_ASSERT_VAL("failed to get fd", fd > 0);
0379 
0380     /*
0381      * dso_1 should get closed, because we reached
0382      * the file descriptor limit
0383      */
0384     TEST_ASSERT_VAL("failed to close dso_1", dso_1->data.fd == -1);
0385 
0386     /* cleanup everything */
0387     close(fd_extra);
0388     dsos__delete(3);
0389 
0390     /* Make sure we did not leak any file descriptor. */
0391     nr_end = open_files_cnt();
0392     pr_debug("nr start %ld, nr stop %ld\n", nr, nr_end);
0393     TEST_ASSERT_VAL("failed leaking files", nr == nr_end);
0394     return 0;
0395 }
0396 
0397 DEFINE_SUITE("DSO data read", dso_data);
0398 DEFINE_SUITE("DSO data cache", dso_data_cache);
0399 DEFINE_SUITE("DSO data reopen", dso_data_reopen);