dso-data.c 8.1 KB

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