stats.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. #include "test/jemalloc_test.h"
  2. TEST_BEGIN(test_stats_summary)
  3. {
  4. size_t *cactive;
  5. size_t sz, allocated, active, resident, mapped;
  6. int expected = config_stats ? 0 : ENOENT;
  7. sz = sizeof(cactive);
  8. assert_d_eq(mallctl("stats.cactive", &cactive, &sz, NULL, 0), expected,
  9. "Unexpected mallctl() result");
  10. sz = sizeof(size_t);
  11. assert_d_eq(mallctl("stats.allocated", &allocated, &sz, NULL, 0),
  12. expected, "Unexpected mallctl() result");
  13. assert_d_eq(mallctl("stats.active", &active, &sz, NULL, 0), expected,
  14. "Unexpected mallctl() result");
  15. assert_d_eq(mallctl("stats.resident", &resident, &sz, NULL, 0),
  16. expected, "Unexpected mallctl() result");
  17. assert_d_eq(mallctl("stats.mapped", &mapped, &sz, NULL, 0), expected,
  18. "Unexpected mallctl() result");
  19. if (config_stats) {
  20. assert_zu_le(active, *cactive,
  21. "active should be no larger than cactive");
  22. assert_zu_le(allocated, active,
  23. "allocated should be no larger than active");
  24. assert_zu_lt(active, resident,
  25. "active should be less than resident");
  26. assert_zu_lt(active, mapped,
  27. "active should be less than mapped");
  28. }
  29. }
  30. TEST_END
  31. TEST_BEGIN(test_stats_huge)
  32. {
  33. void *p;
  34. uint64_t epoch;
  35. size_t allocated;
  36. uint64_t nmalloc, ndalloc, nrequests;
  37. size_t sz;
  38. int expected = config_stats ? 0 : ENOENT;
  39. p = mallocx(large_maxclass+1, 0);
  40. assert_ptr_not_null(p, "Unexpected mallocx() failure");
  41. assert_d_eq(mallctl("epoch", NULL, NULL, &epoch, sizeof(epoch)), 0,
  42. "Unexpected mallctl() failure");
  43. sz = sizeof(size_t);
  44. assert_d_eq(mallctl("stats.arenas.0.huge.allocated", &allocated, &sz,
  45. NULL, 0), expected, "Unexpected mallctl() result");
  46. sz = sizeof(uint64_t);
  47. assert_d_eq(mallctl("stats.arenas.0.huge.nmalloc", &nmalloc, &sz, NULL,
  48. 0), expected, "Unexpected mallctl() result");
  49. assert_d_eq(mallctl("stats.arenas.0.huge.ndalloc", &ndalloc, &sz, NULL,
  50. 0), expected, "Unexpected mallctl() result");
  51. assert_d_eq(mallctl("stats.arenas.0.huge.nrequests", &nrequests, &sz,
  52. NULL, 0), expected, "Unexpected mallctl() result");
  53. if (config_stats) {
  54. assert_zu_gt(allocated, 0,
  55. "allocated should be greater than zero");
  56. assert_u64_ge(nmalloc, ndalloc,
  57. "nmalloc should be at least as large as ndalloc");
  58. assert_u64_le(nmalloc, nrequests,
  59. "nmalloc should no larger than nrequests");
  60. }
  61. dallocx(p, 0);
  62. }
  63. TEST_END
  64. TEST_BEGIN(test_stats_arenas_summary)
  65. {
  66. unsigned arena;
  67. void *little, *large, *huge;
  68. uint64_t epoch;
  69. size_t sz;
  70. int expected = config_stats ? 0 : ENOENT;
  71. size_t mapped;
  72. uint64_t npurge, nmadvise, purged;
  73. arena = 0;
  74. assert_d_eq(mallctl("thread.arena", NULL, NULL, &arena, sizeof(arena)),
  75. 0, "Unexpected mallctl() failure");
  76. little = mallocx(SMALL_MAXCLASS, 0);
  77. assert_ptr_not_null(little, "Unexpected mallocx() failure");
  78. large = mallocx(large_maxclass, 0);
  79. assert_ptr_not_null(large, "Unexpected mallocx() failure");
  80. huge = mallocx(chunksize, 0);
  81. assert_ptr_not_null(huge, "Unexpected mallocx() failure");
  82. assert_d_eq(mallctl("arena.0.purge", NULL, NULL, NULL, 0), 0,
  83. "Unexpected mallctl() failure");
  84. assert_d_eq(mallctl("epoch", NULL, NULL, &epoch, sizeof(epoch)), 0,
  85. "Unexpected mallctl() failure");
  86. sz = sizeof(size_t);
  87. assert_d_eq(mallctl("stats.arenas.0.mapped", &mapped, &sz, NULL, 0),
  88. expected, "Unexepected mallctl() result");
  89. sz = sizeof(uint64_t);
  90. assert_d_eq(mallctl("stats.arenas.0.npurge", &npurge, &sz, NULL, 0),
  91. expected, "Unexepected mallctl() result");
  92. assert_d_eq(mallctl("stats.arenas.0.nmadvise", &nmadvise, &sz, NULL, 0),
  93. expected, "Unexepected mallctl() result");
  94. assert_d_eq(mallctl("stats.arenas.0.purged", &purged, &sz, NULL, 0),
  95. expected, "Unexepected mallctl() result");
  96. if (config_stats) {
  97. assert_u64_gt(npurge, 0,
  98. "At least one purge should have occurred");
  99. assert_u64_le(nmadvise, purged,
  100. "nmadvise should be no greater than purged");
  101. }
  102. dallocx(little, 0);
  103. dallocx(large, 0);
  104. dallocx(huge, 0);
  105. }
  106. TEST_END
  107. void *
  108. thd_start(void *arg)
  109. {
  110. return (NULL);
  111. }
  112. static void
  113. no_lazy_lock(void)
  114. {
  115. thd_t thd;
  116. thd_create(&thd, thd_start, NULL);
  117. thd_join(thd, NULL);
  118. }
  119. TEST_BEGIN(test_stats_arenas_small)
  120. {
  121. unsigned arena;
  122. void *p;
  123. size_t sz, allocated;
  124. uint64_t epoch, nmalloc, ndalloc, nrequests;
  125. int expected = config_stats ? 0 : ENOENT;
  126. no_lazy_lock(); /* Lazy locking would dodge tcache testing. */
  127. arena = 0;
  128. assert_d_eq(mallctl("thread.arena", NULL, NULL, &arena, sizeof(arena)),
  129. 0, "Unexpected mallctl() failure");
  130. p = mallocx(SMALL_MAXCLASS, 0);
  131. assert_ptr_not_null(p, "Unexpected mallocx() failure");
  132. assert_d_eq(mallctl("thread.tcache.flush", NULL, NULL, NULL, 0),
  133. config_tcache ? 0 : ENOENT, "Unexpected mallctl() result");
  134. assert_d_eq(mallctl("epoch", NULL, NULL, &epoch, sizeof(epoch)), 0,
  135. "Unexpected mallctl() failure");
  136. sz = sizeof(size_t);
  137. assert_d_eq(mallctl("stats.arenas.0.small.allocated", &allocated, &sz,
  138. NULL, 0), expected, "Unexpected mallctl() result");
  139. sz = sizeof(uint64_t);
  140. assert_d_eq(mallctl("stats.arenas.0.small.nmalloc", &nmalloc, &sz,
  141. NULL, 0), expected, "Unexpected mallctl() result");
  142. assert_d_eq(mallctl("stats.arenas.0.small.ndalloc", &ndalloc, &sz,
  143. NULL, 0), expected, "Unexpected mallctl() result");
  144. assert_d_eq(mallctl("stats.arenas.0.small.nrequests", &nrequests, &sz,
  145. NULL, 0), expected, "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. {
  161. unsigned arena;
  162. void *p;
  163. size_t sz, allocated;
  164. uint64_t epoch, nmalloc, ndalloc, nrequests;
  165. int expected = config_stats ? 0 : ENOENT;
  166. arena = 0;
  167. assert_d_eq(mallctl("thread.arena", NULL, NULL, &arena, sizeof(arena)),
  168. 0, "Unexpected mallctl() failure");
  169. p = mallocx(large_maxclass, 0);
  170. assert_ptr_not_null(p, "Unexpected mallocx() failure");
  171. assert_d_eq(mallctl("epoch", NULL, NULL, &epoch, sizeof(epoch)), 0,
  172. "Unexpected mallctl() failure");
  173. sz = sizeof(size_t);
  174. assert_d_eq(mallctl("stats.arenas.0.large.allocated", &allocated, &sz,
  175. NULL, 0), expected, "Unexpected mallctl() result");
  176. sz = sizeof(uint64_t);
  177. assert_d_eq(mallctl("stats.arenas.0.large.nmalloc", &nmalloc, &sz,
  178. NULL, 0), expected, "Unexpected mallctl() result");
  179. assert_d_eq(mallctl("stats.arenas.0.large.ndalloc", &ndalloc, &sz,
  180. NULL, 0), expected, "Unexpected mallctl() result");
  181. assert_d_eq(mallctl("stats.arenas.0.large.nrequests", &nrequests, &sz,
  182. NULL, 0), expected, "Unexpected mallctl() result");
  183. if (config_stats) {
  184. assert_zu_gt(allocated, 0,
  185. "allocated should be greater than zero");
  186. assert_zu_gt(nmalloc, 0,
  187. "nmalloc should be greater than zero");
  188. assert_zu_ge(nmalloc, ndalloc,
  189. "nmalloc should be at least as large as ndalloc");
  190. assert_zu_gt(nrequests, 0,
  191. "nrequests should be greater than zero");
  192. }
  193. dallocx(p, 0);
  194. }
  195. TEST_END
  196. TEST_BEGIN(test_stats_arenas_huge)
  197. {
  198. unsigned arena;
  199. void *p;
  200. size_t sz, allocated;
  201. uint64_t epoch, nmalloc, ndalloc;
  202. int expected = config_stats ? 0 : ENOENT;
  203. arena = 0;
  204. assert_d_eq(mallctl("thread.arena", NULL, NULL, &arena, sizeof(arena)),
  205. 0, "Unexpected mallctl() failure");
  206. p = mallocx(chunksize, 0);
  207. assert_ptr_not_null(p, "Unexpected mallocx() failure");
  208. assert_d_eq(mallctl("epoch", NULL, NULL, &epoch, sizeof(epoch)), 0,
  209. "Unexpected mallctl() failure");
  210. sz = sizeof(size_t);
  211. assert_d_eq(mallctl("stats.arenas.0.huge.allocated", &allocated, &sz,
  212. NULL, 0), expected, "Unexpected mallctl() result");
  213. sz = sizeof(uint64_t);
  214. assert_d_eq(mallctl("stats.arenas.0.huge.nmalloc", &nmalloc, &sz,
  215. NULL, 0), expected, "Unexpected mallctl() result");
  216. assert_d_eq(mallctl("stats.arenas.0.huge.ndalloc", &ndalloc, &sz,
  217. NULL, 0), expected, "Unexpected mallctl() result");
  218. if (config_stats) {
  219. assert_zu_gt(allocated, 0,
  220. "allocated should be greater than zero");
  221. assert_zu_gt(nmalloc, 0,
  222. "nmalloc should be greater than zero");
  223. assert_zu_ge(nmalloc, ndalloc,
  224. "nmalloc should be at least as large as ndalloc");
  225. }
  226. dallocx(p, 0);
  227. }
  228. TEST_END
  229. TEST_BEGIN(test_stats_arenas_bins)
  230. {
  231. unsigned arena;
  232. void *p;
  233. size_t sz, curruns, curregs;
  234. uint64_t epoch, nmalloc, ndalloc, nrequests, nfills, nflushes;
  235. uint64_t nruns, nreruns;
  236. int expected = config_stats ? 0 : ENOENT;
  237. arena = 0;
  238. assert_d_eq(mallctl("thread.arena", NULL, NULL, &arena, sizeof(arena)),
  239. 0, "Unexpected mallctl() failure");
  240. p = mallocx(arena_bin_info[0].reg_size, 0);
  241. assert_ptr_not_null(p, "Unexpected mallocx() failure");
  242. assert_d_eq(mallctl("thread.tcache.flush", NULL, NULL, NULL, 0),
  243. config_tcache ? 0 : ENOENT, "Unexpected mallctl() result");
  244. assert_d_eq(mallctl("epoch", NULL, NULL, &epoch, sizeof(epoch)), 0,
  245. "Unexpected mallctl() failure");
  246. sz = sizeof(uint64_t);
  247. assert_d_eq(mallctl("stats.arenas.0.bins.0.nmalloc", &nmalloc, &sz,
  248. NULL, 0), expected, "Unexpected mallctl() result");
  249. assert_d_eq(mallctl("stats.arenas.0.bins.0.ndalloc", &ndalloc, &sz,
  250. NULL, 0), expected, "Unexpected mallctl() result");
  251. assert_d_eq(mallctl("stats.arenas.0.bins.0.nrequests", &nrequests, &sz,
  252. NULL, 0), expected, "Unexpected mallctl() result");
  253. sz = sizeof(size_t);
  254. assert_d_eq(mallctl("stats.arenas.0.bins.0.curregs", &curregs, &sz,
  255. NULL, 0), expected, "Unexpected mallctl() result");
  256. sz = sizeof(uint64_t);
  257. assert_d_eq(mallctl("stats.arenas.0.bins.0.nfills", &nfills, &sz,
  258. NULL, 0), config_tcache ? expected : ENOENT,
  259. "Unexpected mallctl() result");
  260. assert_d_eq(mallctl("stats.arenas.0.bins.0.nflushes", &nflushes, &sz,
  261. NULL, 0), config_tcache ? expected : ENOENT,
  262. "Unexpected mallctl() result");
  263. assert_d_eq(mallctl("stats.arenas.0.bins.0.nruns", &nruns, &sz,
  264. NULL, 0), expected, "Unexpected mallctl() result");
  265. assert_d_eq(mallctl("stats.arenas.0.bins.0.nreruns", &nreruns, &sz,
  266. NULL, 0), expected, "Unexpected mallctl() result");
  267. sz = sizeof(size_t);
  268. assert_d_eq(mallctl("stats.arenas.0.bins.0.curruns", &curruns, &sz,
  269. NULL, 0), expected, "Unexpected mallctl() result");
  270. if (config_stats) {
  271. assert_u64_gt(nmalloc, 0,
  272. "nmalloc should be greater than zero");
  273. assert_u64_ge(nmalloc, ndalloc,
  274. "nmalloc should be at least as large as ndalloc");
  275. assert_u64_gt(nrequests, 0,
  276. "nrequests should be greater than zero");
  277. assert_zu_gt(curregs, 0,
  278. "allocated should be greater than zero");
  279. if (config_tcache) {
  280. assert_u64_gt(nfills, 0,
  281. "At least one fill should have occurred");
  282. assert_u64_gt(nflushes, 0,
  283. "At least one flush should have occurred");
  284. }
  285. assert_u64_gt(nruns, 0,
  286. "At least one run should have been allocated");
  287. assert_zu_gt(curruns, 0,
  288. "At least one run should be currently allocated");
  289. }
  290. dallocx(p, 0);
  291. }
  292. TEST_END
  293. TEST_BEGIN(test_stats_arenas_lruns)
  294. {
  295. unsigned arena;
  296. void *p;
  297. uint64_t epoch, nmalloc, ndalloc, nrequests;
  298. size_t curruns, sz;
  299. int expected = config_stats ? 0 : ENOENT;
  300. arena = 0;
  301. assert_d_eq(mallctl("thread.arena", NULL, NULL, &arena, sizeof(arena)),
  302. 0, "Unexpected mallctl() failure");
  303. p = mallocx(LARGE_MINCLASS, 0);
  304. assert_ptr_not_null(p, "Unexpected mallocx() failure");
  305. assert_d_eq(mallctl("epoch", NULL, NULL, &epoch, sizeof(epoch)), 0,
  306. "Unexpected mallctl() failure");
  307. sz = sizeof(uint64_t);
  308. assert_d_eq(mallctl("stats.arenas.0.lruns.0.nmalloc", &nmalloc, &sz,
  309. NULL, 0), expected, "Unexpected mallctl() result");
  310. assert_d_eq(mallctl("stats.arenas.0.lruns.0.ndalloc", &ndalloc, &sz,
  311. NULL, 0), expected, "Unexpected mallctl() result");
  312. assert_d_eq(mallctl("stats.arenas.0.lruns.0.nrequests", &nrequests, &sz,
  313. NULL, 0), expected, "Unexpected mallctl() result");
  314. sz = sizeof(size_t);
  315. assert_d_eq(mallctl("stats.arenas.0.lruns.0.curruns", &curruns, &sz,
  316. NULL, 0), expected, "Unexpected mallctl() result");
  317. if (config_stats) {
  318. assert_u64_gt(nmalloc, 0,
  319. "nmalloc should be greater than zero");
  320. assert_u64_ge(nmalloc, ndalloc,
  321. "nmalloc should be at least as large as ndalloc");
  322. assert_u64_gt(nrequests, 0,
  323. "nrequests should be greater than zero");
  324. assert_u64_gt(curruns, 0,
  325. "At least one run should be currently allocated");
  326. }
  327. dallocx(p, 0);
  328. }
  329. TEST_END
  330. TEST_BEGIN(test_stats_arenas_hchunks)
  331. {
  332. unsigned arena;
  333. void *p;
  334. uint64_t epoch, nmalloc, ndalloc;
  335. size_t curhchunks, sz;
  336. int expected = config_stats ? 0 : ENOENT;
  337. arena = 0;
  338. assert_d_eq(mallctl("thread.arena", NULL, NULL, &arena, sizeof(arena)),
  339. 0, "Unexpected mallctl() failure");
  340. p = mallocx(chunksize, 0);
  341. assert_ptr_not_null(p, "Unexpected mallocx() failure");
  342. assert_d_eq(mallctl("epoch", NULL, NULL, &epoch, sizeof(epoch)), 0,
  343. "Unexpected mallctl() failure");
  344. sz = sizeof(uint64_t);
  345. assert_d_eq(mallctl("stats.arenas.0.hchunks.0.nmalloc", &nmalloc, &sz,
  346. NULL, 0), expected, "Unexpected mallctl() result");
  347. assert_d_eq(mallctl("stats.arenas.0.hchunks.0.ndalloc", &ndalloc, &sz,
  348. NULL, 0), expected, "Unexpected mallctl() result");
  349. sz = sizeof(size_t);
  350. assert_d_eq(mallctl("stats.arenas.0.hchunks.0.curhchunks", &curhchunks,
  351. &sz, NULL, 0), expected, "Unexpected mallctl() result");
  352. if (config_stats) {
  353. assert_u64_gt(nmalloc, 0,
  354. "nmalloc should be greater than zero");
  355. assert_u64_ge(nmalloc, ndalloc,
  356. "nmalloc should be at least as large as ndalloc");
  357. assert_u64_gt(curhchunks, 0,
  358. "At least one chunk should be currently allocated");
  359. }
  360. dallocx(p, 0);
  361. }
  362. TEST_END
  363. int
  364. main(void)
  365. {
  366. return (test(
  367. test_stats_summary,
  368. test_stats_huge,
  369. test_stats_arenas_summary,
  370. test_stats_arenas_small,
  371. test_stats_arenas_large,
  372. test_stats_arenas_huge,
  373. test_stats_arenas_bins,
  374. test_stats_arenas_lruns,
  375. test_stats_arenas_hchunks));
  376. }