dso-data.c 9.0 KB

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