stats.c 11 KB

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