dso-data.c 7.8 KB

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