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