stats.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. #include "test/jemalloc_test.h"
  2. TEST_BEGIN(test_stats_summary) {
  3. size_t sz, allocated, active, resident, mapped;
  4. int expected = config_stats ? 0 : ENOENT;
  5. sz = sizeof(size_t);
  6. assert_d_eq(mallctl("stats.allocated", (void *)&allocated, &sz, NULL,
  7. 0), expected, "Unexpected mallctl() result");
  8. assert_d_eq(mallctl("stats.active", (void *)&active, &sz, NULL, 0),
  9. expected, "Unexpected mallctl() result");
  10. assert_d_eq(mallctl("stats.resident", (void *)&resident, &sz, NULL, 0),
  11. expected, "Unexpected mallctl() result");
  12. assert_d_eq(mallctl("stats.mapped", (void *)&mapped, &sz, NULL, 0),
  13. expected, "Unexpected mallctl() result");
  14. if (config_stats) {
  15. assert_zu_le(allocated, active,
  16. "allocated should be no larger than active");
  17. assert_zu_lt(active, resident,
  18. "active should be less than resident");
  19. assert_zu_lt(active, mapped,
  20. "active should be less than mapped");
  21. }
  22. }
  23. TEST_END
  24. TEST_BEGIN(test_stats_large) {
  25. void *p;
  26. uint64_t epoch;
  27. size_t allocated;
  28. uint64_t nmalloc, ndalloc, nrequests;
  29. size_t sz;
  30. int expected = config_stats ? 0 : ENOENT;
  31. p = mallocx(SMALL_MAXCLASS+1, MALLOCX_ARENA(0));
  32. assert_ptr_not_null(p, "Unexpected mallocx() failure");
  33. assert_d_eq(mallctl("epoch", NULL, NULL, (void *)&epoch, sizeof(epoch)),
  34. 0, "Unexpected mallctl() failure");
  35. sz = sizeof(size_t);
  36. assert_d_eq(mallctl("stats.arenas.0.large.allocated",
  37. (void *)&allocated, &sz, NULL, 0), expected,
  38. "Unexpected mallctl() result");
  39. sz = sizeof(uint64_t);
  40. assert_d_eq(mallctl("stats.arenas.0.large.nmalloc", (void *)&nmalloc,
  41. &sz, NULL, 0), expected, "Unexpected mallctl() result");
  42. assert_d_eq(mallctl("stats.arenas.0.large.ndalloc", (void *)&ndalloc,
  43. &sz, NULL, 0), expected, "Unexpected mallctl() result");
  44. assert_d_eq(mallctl("stats.arenas.0.large.nrequests",
  45. (void *)&nrequests, &sz, NULL, 0), expected,
  46. "Unexpected mallctl() result");
  47. if (config_stats) {
  48. assert_zu_gt(allocated, 0,
  49. "allocated should be greater than zero");
  50. assert_u64_ge(nmalloc, ndalloc,
  51. "nmalloc should be at least as large as ndalloc");
  52. assert_u64_le(nmalloc, nrequests,
  53. "nmalloc should no larger than nrequests");
  54. }
  55. dallocx(p, 0);
  56. }
  57. TEST_END
  58. TEST_BEGIN(test_stats_arenas_summary) {
  59. void *little, *large;
  60. uint64_t epoch;
  61. size_t sz;
  62. int expected = config_stats ? 0 : ENOENT;
  63. size_t mapped;
  64. uint64_t dirty_npurge, dirty_nmadvise, dirty_purged;
  65. uint64_t muzzy_npurge, muzzy_nmadvise, muzzy_purged;
  66. little = mallocx(SMALL_MAXCLASS, MALLOCX_ARENA(0));
  67. assert_ptr_not_null(little, "Unexpected mallocx() failure");
  68. large = mallocx((1U << LG_LARGE_MINCLASS), MALLOCX_ARENA(0));
  69. assert_ptr_not_null(large, "Unexpected mallocx() failure");
  70. dallocx(little, 0);
  71. dallocx(large, 0);
  72. assert_d_eq(mallctl("thread.tcache.flush", NULL, NULL, NULL, 0),
  73. opt_tcache ? 0 : EFAULT, "Unexpected mallctl() result");
  74. assert_d_eq(mallctl("arena.0.purge", NULL, NULL, NULL, 0), 0,
  75. "Unexpected mallctl() failure");
  76. assert_d_eq(mallctl("epoch", NULL, NULL, (void *)&epoch, sizeof(epoch)),
  77. 0, "Unexpected mallctl() failure");
  78. sz = sizeof(size_t);
  79. assert_d_eq(mallctl("stats.arenas.0.mapped", (void *)&mapped, &sz, NULL,
  80. 0), expected, "Unexepected mallctl() result");
  81. sz = sizeof(uint64_t);
  82. assert_d_eq(mallctl("stats.arenas.0.dirty_npurge",
  83. (void *)&dirty_npurge, &sz, NULL, 0), expected,
  84. "Unexepected mallctl() result");
  85. assert_d_eq(mallctl("stats.arenas.0.dirty_nmadvise",
  86. (void *)&dirty_nmadvise, &sz, NULL, 0), expected,
  87. "Unexepected mallctl() result");
  88. assert_d_eq(mallctl("stats.arenas.0.dirty_purged",
  89. (void *)&dirty_purged, &sz, NULL, 0), expected,
  90. "Unexepected mallctl() result");
  91. assert_d_eq(mallctl("stats.arenas.0.muzzy_npurge",
  92. (void *)&muzzy_npurge, &sz, NULL, 0), expected,
  93. "Unexepected mallctl() result");
  94. assert_d_eq(mallctl("stats.arenas.0.muzzy_nmadvise",
  95. (void *)&muzzy_nmadvise, &sz, NULL, 0), expected,
  96. "Unexepected mallctl() result");
  97. assert_d_eq(mallctl("stats.arenas.0.muzzy_purged",
  98. (void *)&muzzy_purged, &sz, NULL, 0), expected,
  99. "Unexepected mallctl() result");
  100. if (config_stats) {
  101. if (!background_thread_enabled()) {
  102. assert_u64_gt(dirty_npurge + muzzy_npurge, 0,
  103. "At least one purge should have occurred");
  104. }
  105. assert_u64_le(dirty_nmadvise, dirty_purged,
  106. "dirty_nmadvise should be no greater than dirty_purged");
  107. assert_u64_le(muzzy_nmadvise, muzzy_purged,
  108. "muzzy_nmadvise should be no greater than muzzy_purged");
  109. }
  110. }
  111. TEST_END
  112. void *
  113. thd_start(void *arg) {
  114. return NULL;
  115. }
  116. static void
  117. no_lazy_lock(void) {
  118. thd_t thd;
  119. thd_create(&thd, thd_start, NULL);
  120. thd_join(thd, NULL);
  121. }
  122. TEST_BEGIN(test_stats_arenas_small) {
  123. void *p;
  124. size_t sz, allocated;
  125. uint64_t epoch, nmalloc, ndalloc, nrequests;
  126. int expected = config_stats ? 0 : ENOENT;
  127. no_lazy_lock(); /* Lazy locking would dodge tcache testing. */
  128. p = mallocx(SMALL_MAXCLASS, MALLOCX_ARENA(0));
  129. assert_ptr_not_null(p, "Unexpected mallocx() failure");
  130. assert_d_eq(mallctl("thread.tcache.flush", NULL, NULL, NULL, 0),
  131. opt_tcache ? 0 : EFAULT, "Unexpected mallctl() result");
  132. assert_d_eq(mallctl("epoch", NULL, NULL, (void *)&epoch, sizeof(epoch)),
  133. 0, "Unexpected mallctl() failure");
  134. sz = sizeof(size_t);
  135. assert_d_eq(mallctl("stats.arenas.0.small.allocated",
  136. (void *)&allocated, &sz, NULL, 0), expected,
  137. "Unexpected mallctl() result");
  138. sz = sizeof(uint64_t);
  139. assert_d_eq(mallctl("stats.arenas.0.small.nmalloc", (void *)&nmalloc,
  140. &sz, NULL, 0), expected, "Unexpected mallctl() result");
  141. assert_d_eq(mallctl("stats.arenas.0.small.ndalloc", (void *)&ndalloc,
  142. &sz, NULL, 0), expected, "Unexpected mallctl() result");
  143. assert_d_eq(mallctl("stats.arenas.0.small.nrequests",
  144. (void *)&nrequests, &sz, NULL, 0), expected,
  145. "Unexpected mallctl() result");
  146. if (config_stats) {
  147. assert_zu_gt(allocated, 0,
  148. "allocated should be greater than zero");
  149. assert_u64_gt(nmalloc, 0,
  150. "nmalloc should be no greater than zero");
  151. assert_u64_ge(nmalloc, ndalloc,
  152. "nmalloc should be at least as large as ndalloc");
  153. assert_u64_gt(nrequests, 0,
  154. "nrequests should be greater than zero");
  155. }
  156. dallocx(p, 0);
  157. }
  158. TEST_END
  159. TEST_BEGIN(test_stats_arenas_large) {
  160. void *p;
  161. size_t sz, allocated;
  162. uint64_t epoch, nmalloc, ndalloc;
  163. int expected = config_stats ? 0 : ENOENT;
  164. p = mallocx((1U << LG_LARGE_MINCLASS), MALLOCX_ARENA(0));
  165. assert_ptr_not_null(p, "Unexpected mallocx() failure");
  166. assert_d_eq(mallctl("epoch", NULL, NULL, (void *)&epoch, sizeof(epoch)),
  167. 0, "Unexpected mallctl() failure");
  168. sz = sizeof(size_t);
  169. assert_d_eq(mallctl("stats.arenas.0.large.allocated",
  170. (void *)&allocated, &sz, NULL, 0), expected,
  171. "Unexpected mallctl() result");
  172. sz = sizeof(uint64_t);
  173. assert_d_eq(mallctl("stats.arenas.0.large.nmalloc", (void *)&nmalloc,
  174. &sz, NULL, 0), expected, "Unexpected mallctl() result");
  175. assert_d_eq(mallctl("stats.arenas.0.large.ndalloc", (void *)&ndalloc,
  176. &sz, NULL, 0), expected, "Unexpected mallctl() result");
  177. if (config_stats) {
  178. assert_zu_gt(allocated, 0,
  179. "allocated should be greater than zero");
  180. assert_u64_gt(nmalloc, 0,
  181. "nmalloc should be greater than zero");
  182. assert_u64_ge(nmalloc, ndalloc,
  183. "nmalloc should be at least as large as ndalloc");
  184. }
  185. dallocx(p, 0);
  186. }
  187. TEST_END
  188. static void
  189. gen_mallctl_str(char *cmd, char *name, unsigned arena_ind) {
  190. sprintf(cmd, "stats.arenas.%u.bins.0.%s", arena_ind, name);
  191. }
  192. TEST_BEGIN(test_stats_arenas_bins) {
  193. void *p;
  194. size_t sz, curslabs, curregs;
  195. uint64_t epoch, nmalloc, ndalloc, nrequests, nfills, nflushes;
  196. uint64_t nslabs, nreslabs;
  197. int expected = config_stats ? 0 : ENOENT;
  198. /* Make sure allocation below isn't satisfied by tcache. */
  199. assert_d_eq(mallctl("thread.tcache.flush", NULL, NULL, NULL, 0),
  200. opt_tcache ? 0 : EFAULT, "Unexpected mallctl() result");
  201. unsigned arena_ind, old_arena_ind;
  202. sz = sizeof(unsigned);
  203. assert_d_eq(mallctl("arenas.create", (void *)&arena_ind, &sz, NULL, 0),
  204. 0, "Arena creation failure");
  205. sz = sizeof(arena_ind);
  206. assert_d_eq(mallctl("thread.arena", (void *)&old_arena_ind, &sz,
  207. (void *)&arena_ind, sizeof(arena_ind)), 0,
  208. "Unexpected mallctl() failure");
  209. p = malloc(bin_infos[0].reg_size);
  210. assert_ptr_not_null(p, "Unexpected malloc() failure");
  211. assert_d_eq(mallctl("thread.tcache.flush", NULL, NULL, NULL, 0),
  212. opt_tcache ? 0 : EFAULT, "Unexpected mallctl() result");
  213. assert_d_eq(mallctl("epoch", NULL, NULL, (void *)&epoch, sizeof(epoch)),
  214. 0, "Unexpected mallctl() failure");
  215. char cmd[128];
  216. sz = sizeof(uint64_t);
  217. gen_mallctl_str(cmd, "nmalloc", arena_ind);
  218. assert_d_eq(mallctl(cmd, (void *)&nmalloc, &sz, NULL, 0), expected,
  219. "Unexpected mallctl() result");
  220. gen_mallctl_str(cmd, "ndalloc", arena_ind);
  221. assert_d_eq(mallctl(cmd, (void *)&ndalloc, &sz, NULL, 0), expected,
  222. "Unexpected mallctl() result");
  223. gen_mallctl_str(cmd, "nrequests", arena_ind);
  224. assert_d_eq(mallctl(cmd, (void *)&nrequests, &sz, NULL, 0), expected,
  225. "Unexpected mallctl() result");
  226. sz = sizeof(size_t);
  227. gen_mallctl_str(cmd, "curregs", arena_ind);
  228. assert_d_eq(mallctl(cmd, (void *)&curregs, &sz, NULL, 0), expected,
  229. "Unexpected mallctl() result");
  230. sz = sizeof(uint64_t);
  231. gen_mallctl_str(cmd, "nfills", arena_ind);
  232. assert_d_eq(mallctl(cmd, (void *)&nfills, &sz, NULL, 0), expected,
  233. "Unexpected mallctl() result");
  234. gen_mallctl_str(cmd, "nflushes", arena_ind);
  235. assert_d_eq(mallctl(cmd, (void *)&nflushes, &sz, NULL, 0), expected,
  236. "Unexpected mallctl() result");
  237. gen_mallctl_str(cmd, "nslabs", arena_ind);
  238. assert_d_eq(mallctl(cmd, (void *)&nslabs, &sz, NULL, 0), expected,
  239. "Unexpected mallctl() result");
  240. gen_mallctl_str(cmd, "nreslabs", arena_ind);
  241. assert_d_eq(mallctl(cmd, (void *)&nreslabs, &sz, NULL, 0), expected,
  242. "Unexpected mallctl() result");
  243. sz = sizeof(size_t);
  244. gen_mallctl_str(cmd, "curslabs", arena_ind);
  245. assert_d_eq(mallctl(cmd, (void *)&curslabs, &sz, NULL, 0), expected,
  246. "Unexpected mallctl() result");
  247. if (config_stats) {
  248. assert_u64_gt(nmalloc, 0,
  249. "nmalloc should be greater than zero");
  250. assert_u64_ge(nmalloc, ndalloc,
  251. "nmalloc should be at least as large as ndalloc");
  252. assert_u64_gt(nrequests, 0,
  253. "nrequests should be greater than zero");
  254. assert_zu_gt(curregs, 0,
  255. "allocated should be greater than zero");
  256. if (opt_tcache) {
  257. assert_u64_gt(nfills, 0,
  258. "At least one fill should have occurred");
  259. assert_u64_gt(nflushes, 0,
  260. "At least one flush should have occurred");
  261. }
  262. assert_u64_gt(nslabs, 0,
  263. "At least one slab should have been allocated");
  264. assert_zu_gt(curslabs, 0,
  265. "At least one slab should be currently allocated");
  266. }
  267. dallocx(p, 0);
  268. }
  269. TEST_END
  270. TEST_BEGIN(test_stats_arenas_lextents) {
  271. void *p;
  272. uint64_t epoch, nmalloc, ndalloc;
  273. size_t curlextents, sz, hsize;
  274. int expected = config_stats ? 0 : ENOENT;
  275. sz = sizeof(size_t);
  276. assert_d_eq(mallctl("arenas.lextent.0.size", (void *)&hsize, &sz, NULL,
  277. 0), 0, "Unexpected mallctl() failure");
  278. p = mallocx(hsize, MALLOCX_ARENA(0));
  279. assert_ptr_not_null(p, "Unexpected mallocx() failure");
  280. assert_d_eq(mallctl("epoch", NULL, NULL, (void *)&epoch, sizeof(epoch)),
  281. 0, "Unexpected mallctl() failure");
  282. sz = sizeof(uint64_t);
  283. assert_d_eq(mallctl("stats.arenas.0.lextents.0.nmalloc",
  284. (void *)&nmalloc, &sz, NULL, 0), expected,
  285. "Unexpected mallctl() result");
  286. assert_d_eq(mallctl("stats.arenas.0.lextents.0.ndalloc",
  287. (void *)&ndalloc, &sz, NULL, 0), expected,
  288. "Unexpected mallctl() result");
  289. sz = sizeof(size_t);
  290. assert_d_eq(mallctl("stats.arenas.0.lextents.0.curlextents",
  291. (void *)&curlextents, &sz, NULL, 0), expected,
  292. "Unexpected mallctl() result");
  293. if (config_stats) {
  294. assert_u64_gt(nmalloc, 0,
  295. "nmalloc should be greater than zero");
  296. assert_u64_ge(nmalloc, ndalloc,
  297. "nmalloc should be at least as large as ndalloc");
  298. assert_u64_gt(curlextents, 0,
  299. "At least one extent should be currently allocated");
  300. }
  301. dallocx(p, 0);
  302. }
  303. TEST_END
  304. int
  305. main(void) {
  306. return test_no_reentrancy(
  307. test_stats_summary,
  308. test_stats_large,
  309. test_stats_arenas_summary,
  310. test_stats_arenas_small,
  311. test_stats_arenas_large,
  312. test_stats_arenas_bins,
  313. test_stats_arenas_lextents);
  314. }