dso-data.c 7.4 KB

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