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