stats.c 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973
  1. #include "jemalloc/internal/jemalloc_preamble.h"
  2. #include "jemalloc/internal/jemalloc_internal_includes.h"
  3. #include "jemalloc/internal/assert.h"
  4. #include "jemalloc/internal/ctl.h"
  5. #include "jemalloc/internal/emitter.h"
  6. #include "jemalloc/internal/fxp.h"
  7. #include "jemalloc/internal/mutex.h"
  8. #include "jemalloc/internal/mutex_prof.h"
  9. #include "jemalloc/internal/prof_stats.h"
  10. const char *global_mutex_names[mutex_prof_num_global_mutexes] = {
  11. #define OP(mtx) #mtx,
  12. MUTEX_PROF_GLOBAL_MUTEXES
  13. #undef OP
  14. };
  15. const char *arena_mutex_names[mutex_prof_num_arena_mutexes] = {
  16. #define OP(mtx) #mtx,
  17. MUTEX_PROF_ARENA_MUTEXES
  18. #undef OP
  19. };
  20. #define CTL_GET(n, v, t) do { \
  21. size_t sz = sizeof(t); \
  22. xmallctl(n, (void *)v, &sz, NULL, 0); \
  23. } while (0)
  24. #define CTL_LEAF_PREPARE(mib, miblen, name) do { \
  25. assert(miblen < CTL_MAX_DEPTH); \
  26. size_t miblen_new = CTL_MAX_DEPTH; \
  27. xmallctlmibnametomib(mib, miblen, name, &miblen_new); \
  28. assert(miblen_new > miblen); \
  29. } while (0)
  30. #define CTL_LEAF(mib, miblen, leaf, v, t) do { \
  31. assert(miblen < CTL_MAX_DEPTH); \
  32. size_t miblen_new = CTL_MAX_DEPTH; \
  33. size_t sz = sizeof(t); \
  34. xmallctlbymibname(mib, miblen, leaf, &miblen_new, (void *)v, \
  35. &sz, NULL, 0); \
  36. assert(miblen_new == miblen + 1); \
  37. } while (0)
  38. #define CTL_M2_GET(n, i, v, t) do { \
  39. size_t mib[CTL_MAX_DEPTH]; \
  40. size_t miblen = sizeof(mib) / sizeof(size_t); \
  41. size_t sz = sizeof(t); \
  42. xmallctlnametomib(n, mib, &miblen); \
  43. mib[2] = (i); \
  44. xmallctlbymib(mib, miblen, (void *)v, &sz, NULL, 0); \
  45. } while (0)
  46. /******************************************************************************/
  47. /* Data. */
  48. bool opt_stats_print = false;
  49. char opt_stats_print_opts[stats_print_tot_num_options+1] = "";
  50. int64_t opt_stats_interval = STATS_INTERVAL_DEFAULT;
  51. char opt_stats_interval_opts[stats_print_tot_num_options+1] = "";
  52. static counter_accum_t stats_interval_accumulated;
  53. /* Per thread batch accum size for stats_interval. */
  54. static uint64_t stats_interval_accum_batch;
  55. /******************************************************************************/
  56. static uint64_t
  57. rate_per_second(uint64_t value, uint64_t uptime_ns) {
  58. uint64_t billion = 1000000000;
  59. if (uptime_ns == 0 || value == 0) {
  60. return 0;
  61. }
  62. if (uptime_ns < billion) {
  63. return value;
  64. } else {
  65. uint64_t uptime_s = uptime_ns / billion;
  66. return value / uptime_s;
  67. }
  68. }
  69. /* Calculate x.yyy and output a string (takes a fixed sized char array). */
  70. static bool
  71. get_rate_str(uint64_t dividend, uint64_t divisor, char str[6]) {
  72. if (divisor == 0 || dividend > divisor) {
  73. /* The rate is not supposed to be greater than 1. */
  74. return true;
  75. }
  76. if (dividend > 0) {
  77. assert(UINT64_MAX / dividend >= 1000);
  78. }
  79. unsigned n = (unsigned)((dividend * 1000) / divisor);
  80. if (n < 10) {
  81. malloc_snprintf(str, 6, "0.00%u", n);
  82. } else if (n < 100) {
  83. malloc_snprintf(str, 6, "0.0%u", n);
  84. } else if (n < 1000) {
  85. malloc_snprintf(str, 6, "0.%u", n);
  86. } else {
  87. malloc_snprintf(str, 6, "1");
  88. }
  89. return false;
  90. }
  91. static void
  92. mutex_stats_init_cols(emitter_row_t *row, const char *table_name,
  93. emitter_col_t *name,
  94. emitter_col_t col_uint64_t[mutex_prof_num_uint64_t_counters],
  95. emitter_col_t col_uint32_t[mutex_prof_num_uint32_t_counters]) {
  96. mutex_prof_uint64_t_counter_ind_t k_uint64_t = 0;
  97. mutex_prof_uint32_t_counter_ind_t k_uint32_t = 0;
  98. emitter_col_t *col;
  99. if (name != NULL) {
  100. emitter_col_init(name, row);
  101. name->justify = emitter_justify_left;
  102. name->width = 21;
  103. name->type = emitter_type_title;
  104. name->str_val = table_name;
  105. }
  106. #define WIDTH_uint32_t 12
  107. #define WIDTH_uint64_t 16
  108. #define OP(counter, counter_type, human, derived, base_counter) \
  109. col = &col_##counter_type[k_##counter_type]; \
  110. ++k_##counter_type; \
  111. emitter_col_init(col, row); \
  112. col->justify = emitter_justify_right; \
  113. col->width = derived ? 8 : WIDTH_##counter_type; \
  114. col->type = emitter_type_title; \
  115. col->str_val = human;
  116. MUTEX_PROF_COUNTERS
  117. #undef OP
  118. #undef WIDTH_uint32_t
  119. #undef WIDTH_uint64_t
  120. col_uint64_t[mutex_counter_total_wait_time_ps].width = 10;
  121. }
  122. static void
  123. mutex_stats_read_global(size_t mib[], size_t miblen, const char *name,
  124. emitter_col_t *col_name,
  125. emitter_col_t col_uint64_t[mutex_prof_num_uint64_t_counters],
  126. emitter_col_t col_uint32_t[mutex_prof_num_uint32_t_counters],
  127. uint64_t uptime) {
  128. CTL_LEAF_PREPARE(mib, miblen, name);
  129. size_t miblen_name = miblen + 1;
  130. col_name->str_val = name;
  131. emitter_col_t *dst;
  132. #define EMITTER_TYPE_uint32_t emitter_type_uint32
  133. #define EMITTER_TYPE_uint64_t emitter_type_uint64
  134. #define OP(counter, counter_type, human, derived, base_counter) \
  135. dst = &col_##counter_type[mutex_counter_##counter]; \
  136. dst->type = EMITTER_TYPE_##counter_type; \
  137. if (!derived) { \
  138. CTL_LEAF(mib, miblen_name, #counter, \
  139. (counter_type *)&dst->bool_val, counter_type); \
  140. } else { \
  141. emitter_col_t *base = \
  142. &col_##counter_type[mutex_counter_##base_counter]; \
  143. dst->counter_type##_val = \
  144. (counter_type)rate_per_second( \
  145. base->counter_type##_val, uptime); \
  146. }
  147. MUTEX_PROF_COUNTERS
  148. #undef OP
  149. #undef EMITTER_TYPE_uint32_t
  150. #undef EMITTER_TYPE_uint64_t
  151. }
  152. static void
  153. mutex_stats_read_arena(size_t mib[], size_t miblen, const char *name,
  154. emitter_col_t *col_name,
  155. emitter_col_t col_uint64_t[mutex_prof_num_uint64_t_counters],
  156. emitter_col_t col_uint32_t[mutex_prof_num_uint32_t_counters],
  157. uint64_t uptime) {
  158. CTL_LEAF_PREPARE(mib, miblen, name);
  159. size_t miblen_name = miblen + 1;
  160. col_name->str_val = name;
  161. emitter_col_t *dst;
  162. #define EMITTER_TYPE_uint32_t emitter_type_uint32
  163. #define EMITTER_TYPE_uint64_t emitter_type_uint64
  164. #define OP(counter, counter_type, human, derived, base_counter) \
  165. dst = &col_##counter_type[mutex_counter_##counter]; \
  166. dst->type = EMITTER_TYPE_##counter_type; \
  167. if (!derived) { \
  168. CTL_LEAF(mib, miblen_name, #counter, \
  169. (counter_type *)&dst->bool_val, counter_type); \
  170. } else { \
  171. emitter_col_t *base = \
  172. &col_##counter_type[mutex_counter_##base_counter]; \
  173. dst->counter_type##_val = \
  174. (counter_type)rate_per_second( \
  175. base->counter_type##_val, uptime); \
  176. }
  177. MUTEX_PROF_COUNTERS
  178. #undef OP
  179. #undef EMITTER_TYPE_uint32_t
  180. #undef EMITTER_TYPE_uint64_t
  181. }
  182. static void
  183. mutex_stats_read_arena_bin(size_t mib[], size_t miblen,
  184. emitter_col_t col_uint64_t[mutex_prof_num_uint64_t_counters],
  185. emitter_col_t col_uint32_t[mutex_prof_num_uint32_t_counters],
  186. uint64_t uptime) {
  187. CTL_LEAF_PREPARE(mib, miblen, "mutex");
  188. size_t miblen_mutex = miblen + 1;
  189. emitter_col_t *dst;
  190. #define EMITTER_TYPE_uint32_t emitter_type_uint32
  191. #define EMITTER_TYPE_uint64_t emitter_type_uint64
  192. #define OP(counter, counter_type, human, derived, base_counter) \
  193. dst = &col_##counter_type[mutex_counter_##counter]; \
  194. dst->type = EMITTER_TYPE_##counter_type; \
  195. if (!derived) { \
  196. CTL_LEAF(mib, miblen_mutex, #counter, \
  197. (counter_type *)&dst->bool_val, counter_type); \
  198. } else { \
  199. emitter_col_t *base = \
  200. &col_##counter_type[mutex_counter_##base_counter]; \
  201. dst->counter_type##_val = \
  202. (counter_type)rate_per_second( \
  203. base->counter_type##_val, uptime); \
  204. }
  205. MUTEX_PROF_COUNTERS
  206. #undef OP
  207. #undef EMITTER_TYPE_uint32_t
  208. #undef EMITTER_TYPE_uint64_t
  209. }
  210. /* "row" can be NULL to avoid emitting in table mode. */
  211. static void
  212. mutex_stats_emit(emitter_t *emitter, emitter_row_t *row,
  213. emitter_col_t col_uint64_t[mutex_prof_num_uint64_t_counters],
  214. emitter_col_t col_uint32_t[mutex_prof_num_uint32_t_counters]) {
  215. if (row != NULL) {
  216. emitter_table_row(emitter, row);
  217. }
  218. mutex_prof_uint64_t_counter_ind_t k_uint64_t = 0;
  219. mutex_prof_uint32_t_counter_ind_t k_uint32_t = 0;
  220. emitter_col_t *col;
  221. #define EMITTER_TYPE_uint32_t emitter_type_uint32
  222. #define EMITTER_TYPE_uint64_t emitter_type_uint64
  223. #define OP(counter, type, human, derived, base_counter) \
  224. if (!derived) { \
  225. col = &col_##type[k_##type]; \
  226. ++k_##type; \
  227. emitter_json_kv(emitter, #counter, EMITTER_TYPE_##type, \
  228. (const void *)&col->bool_val); \
  229. }
  230. MUTEX_PROF_COUNTERS;
  231. #undef OP
  232. #undef EMITTER_TYPE_uint32_t
  233. #undef EMITTER_TYPE_uint64_t
  234. }
  235. #define COL_DECLARE(column_name) \
  236. emitter_col_t col_##column_name;
  237. #define COL_INIT(row_name, column_name, left_or_right, col_width, etype)\
  238. emitter_col_init(&col_##column_name, &row_name); \
  239. col_##column_name.justify = emitter_justify_##left_or_right; \
  240. col_##column_name.width = col_width; \
  241. col_##column_name.type = emitter_type_##etype;
  242. #define COL(row_name, column_name, left_or_right, col_width, etype) \
  243. COL_DECLARE(column_name); \
  244. COL_INIT(row_name, column_name, left_or_right, col_width, etype)
  245. #define COL_HDR_DECLARE(column_name) \
  246. COL_DECLARE(column_name); \
  247. emitter_col_t header_##column_name;
  248. #define COL_HDR_INIT(row_name, column_name, human, left_or_right, \
  249. col_width, etype) \
  250. COL_INIT(row_name, column_name, left_or_right, col_width, etype)\
  251. emitter_col_init(&header_##column_name, &header_##row_name); \
  252. header_##column_name.justify = emitter_justify_##left_or_right; \
  253. header_##column_name.width = col_width; \
  254. header_##column_name.type = emitter_type_title; \
  255. header_##column_name.str_val = human ? human : #column_name;
  256. #define COL_HDR(row_name, column_name, human, left_or_right, col_width, \
  257. etype) \
  258. COL_HDR_DECLARE(column_name) \
  259. COL_HDR_INIT(row_name, column_name, human, left_or_right, \
  260. col_width, etype)
  261. JEMALLOC_COLD
  262. static void
  263. stats_arena_bins_print(emitter_t *emitter, bool mutex, unsigned i,
  264. uint64_t uptime) {
  265. size_t page;
  266. bool in_gap, in_gap_prev;
  267. unsigned nbins, j;
  268. CTL_GET("arenas.page", &page, size_t);
  269. CTL_GET("arenas.nbins", &nbins, unsigned);
  270. emitter_row_t header_row;
  271. emitter_row_init(&header_row);
  272. emitter_row_t row;
  273. emitter_row_init(&row);
  274. bool prof_stats_on = config_prof && opt_prof && opt_prof_stats
  275. && i == MALLCTL_ARENAS_ALL;
  276. COL_HDR(row, size, NULL, right, 20, size)
  277. COL_HDR(row, ind, NULL, right, 4, unsigned)
  278. COL_HDR(row, allocated, NULL, right, 13, uint64)
  279. COL_HDR(row, nmalloc, NULL, right, 13, uint64)
  280. COL_HDR(row, nmalloc_ps, "(#/sec)", right, 8, uint64)
  281. COL_HDR(row, ndalloc, NULL, right, 13, uint64)
  282. COL_HDR(row, ndalloc_ps, "(#/sec)", right, 8, uint64)
  283. COL_HDR(row, nrequests, NULL, right, 13, uint64)
  284. COL_HDR(row, nrequests_ps, "(#/sec)", right, 10, uint64)
  285. COL_HDR_DECLARE(prof_live_requested);
  286. COL_HDR_DECLARE(prof_live_count);
  287. COL_HDR_DECLARE(prof_accum_requested);
  288. COL_HDR_DECLARE(prof_accum_count);
  289. if (prof_stats_on) {
  290. COL_HDR_INIT(row, prof_live_requested, NULL, right, 21, uint64)
  291. COL_HDR_INIT(row, prof_live_count, NULL, right, 17, uint64)
  292. COL_HDR_INIT(row, prof_accum_requested, NULL, right, 21, uint64)
  293. COL_HDR_INIT(row, prof_accum_count, NULL, right, 17, uint64)
  294. }
  295. COL_HDR(row, nshards, NULL, right, 9, unsigned)
  296. COL_HDR(row, curregs, NULL, right, 13, size)
  297. COL_HDR(row, curslabs, NULL, right, 13, size)
  298. COL_HDR(row, nonfull_slabs, NULL, right, 15, size)
  299. COL_HDR(row, regs, NULL, right, 5, unsigned)
  300. COL_HDR(row, pgs, NULL, right, 4, size)
  301. /* To buffer a right- and left-justified column. */
  302. COL_HDR(row, justify_spacer, NULL, right, 1, title)
  303. COL_HDR(row, util, NULL, right, 6, title)
  304. COL_HDR(row, nfills, NULL, right, 13, uint64)
  305. COL_HDR(row, nfills_ps, "(#/sec)", right, 8, uint64)
  306. COL_HDR(row, nflushes, NULL, right, 13, uint64)
  307. COL_HDR(row, nflushes_ps, "(#/sec)", right, 8, uint64)
  308. COL_HDR(row, nslabs, NULL, right, 13, uint64)
  309. COL_HDR(row, nreslabs, NULL, right, 13, uint64)
  310. COL_HDR(row, nreslabs_ps, "(#/sec)", right, 8, uint64)
  311. /* Don't want to actually print the name. */
  312. header_justify_spacer.str_val = " ";
  313. col_justify_spacer.str_val = " ";
  314. emitter_col_t col_mutex64[mutex_prof_num_uint64_t_counters];
  315. emitter_col_t col_mutex32[mutex_prof_num_uint32_t_counters];
  316. emitter_col_t header_mutex64[mutex_prof_num_uint64_t_counters];
  317. emitter_col_t header_mutex32[mutex_prof_num_uint32_t_counters];
  318. if (mutex) {
  319. mutex_stats_init_cols(&row, NULL, NULL, col_mutex64,
  320. col_mutex32);
  321. mutex_stats_init_cols(&header_row, NULL, NULL, header_mutex64,
  322. header_mutex32);
  323. }
  324. /*
  325. * We print a "bins:" header as part of the table row; we need to adjust
  326. * the header size column to compensate.
  327. */
  328. header_size.width -=5;
  329. emitter_table_printf(emitter, "bins:");
  330. emitter_table_row(emitter, &header_row);
  331. emitter_json_array_kv_begin(emitter, "bins");
  332. size_t stats_arenas_mib[CTL_MAX_DEPTH];
  333. CTL_LEAF_PREPARE(stats_arenas_mib, 0, "stats.arenas");
  334. stats_arenas_mib[2] = i;
  335. CTL_LEAF_PREPARE(stats_arenas_mib, 3, "bins");
  336. size_t arenas_bin_mib[CTL_MAX_DEPTH];
  337. CTL_LEAF_PREPARE(arenas_bin_mib, 0, "arenas.bin");
  338. size_t prof_stats_mib[CTL_MAX_DEPTH];
  339. if (prof_stats_on) {
  340. CTL_LEAF_PREPARE(prof_stats_mib, 0, "prof.stats.bins");
  341. }
  342. for (j = 0, in_gap = false; j < nbins; j++) {
  343. uint64_t nslabs;
  344. size_t reg_size, slab_size, curregs;
  345. size_t curslabs;
  346. size_t nonfull_slabs;
  347. uint32_t nregs, nshards;
  348. uint64_t nmalloc, ndalloc, nrequests, nfills, nflushes;
  349. uint64_t nreslabs;
  350. prof_stats_t prof_live;
  351. prof_stats_t prof_accum;
  352. stats_arenas_mib[4] = j;
  353. arenas_bin_mib[2] = j;
  354. CTL_LEAF(stats_arenas_mib, 5, "nslabs", &nslabs, uint64_t);
  355. if (prof_stats_on) {
  356. prof_stats_mib[3] = j;
  357. CTL_LEAF(prof_stats_mib, 4, "live", &prof_live,
  358. prof_stats_t);
  359. CTL_LEAF(prof_stats_mib, 4, "accum", &prof_accum,
  360. prof_stats_t);
  361. }
  362. in_gap_prev = in_gap;
  363. if (prof_stats_on) {
  364. in_gap = (nslabs == 0 && prof_accum.count == 0);
  365. } else {
  366. in_gap = (nslabs == 0);
  367. }
  368. if (in_gap_prev && !in_gap) {
  369. emitter_table_printf(emitter,
  370. " ---\n");
  371. }
  372. if (in_gap && !emitter_outputs_json(emitter)) {
  373. continue;
  374. }
  375. CTL_LEAF(arenas_bin_mib, 3, "size", &reg_size, size_t);
  376. CTL_LEAF(arenas_bin_mib, 3, "nregs", &nregs, uint32_t);
  377. CTL_LEAF(arenas_bin_mib, 3, "slab_size", &slab_size, size_t);
  378. CTL_LEAF(arenas_bin_mib, 3, "nshards", &nshards, uint32_t);
  379. CTL_LEAF(stats_arenas_mib, 5, "nmalloc", &nmalloc, uint64_t);
  380. CTL_LEAF(stats_arenas_mib, 5, "ndalloc", &ndalloc, uint64_t);
  381. CTL_LEAF(stats_arenas_mib, 5, "curregs", &curregs, size_t);
  382. CTL_LEAF(stats_arenas_mib, 5, "nrequests", &nrequests,
  383. uint64_t);
  384. CTL_LEAF(stats_arenas_mib, 5, "nfills", &nfills, uint64_t);
  385. CTL_LEAF(stats_arenas_mib, 5, "nflushes", &nflushes, uint64_t);
  386. CTL_LEAF(stats_arenas_mib, 5, "nreslabs", &nreslabs, uint64_t);
  387. CTL_LEAF(stats_arenas_mib, 5, "curslabs", &curslabs, size_t);
  388. CTL_LEAF(stats_arenas_mib, 5, "nonfull_slabs", &nonfull_slabs,
  389. size_t);
  390. if (mutex) {
  391. mutex_stats_read_arena_bin(stats_arenas_mib, 5,
  392. col_mutex64, col_mutex32, uptime);
  393. }
  394. emitter_json_object_begin(emitter);
  395. emitter_json_kv(emitter, "nmalloc", emitter_type_uint64,
  396. &nmalloc);
  397. emitter_json_kv(emitter, "ndalloc", emitter_type_uint64,
  398. &ndalloc);
  399. emitter_json_kv(emitter, "curregs", emitter_type_size,
  400. &curregs);
  401. emitter_json_kv(emitter, "nrequests", emitter_type_uint64,
  402. &nrequests);
  403. if (prof_stats_on) {
  404. emitter_json_kv(emitter, "prof_live_requested",
  405. emitter_type_uint64, &prof_live.req_sum);
  406. emitter_json_kv(emitter, "prof_live_count",
  407. emitter_type_uint64, &prof_live.count);
  408. emitter_json_kv(emitter, "prof_accum_requested",
  409. emitter_type_uint64, &prof_accum.req_sum);
  410. emitter_json_kv(emitter, "prof_accum_count",
  411. emitter_type_uint64, &prof_accum.count);
  412. }
  413. emitter_json_kv(emitter, "nfills", emitter_type_uint64,
  414. &nfills);
  415. emitter_json_kv(emitter, "nflushes", emitter_type_uint64,
  416. &nflushes);
  417. emitter_json_kv(emitter, "nreslabs", emitter_type_uint64,
  418. &nreslabs);
  419. emitter_json_kv(emitter, "curslabs", emitter_type_size,
  420. &curslabs);
  421. emitter_json_kv(emitter, "nonfull_slabs", emitter_type_size,
  422. &nonfull_slabs);
  423. if (mutex) {
  424. emitter_json_object_kv_begin(emitter, "mutex");
  425. mutex_stats_emit(emitter, NULL, col_mutex64,
  426. col_mutex32);
  427. emitter_json_object_end(emitter);
  428. }
  429. emitter_json_object_end(emitter);
  430. size_t availregs = nregs * curslabs;
  431. char util[6];
  432. if (get_rate_str((uint64_t)curregs, (uint64_t)availregs, util))
  433. {
  434. if (availregs == 0) {
  435. malloc_snprintf(util, sizeof(util), "1");
  436. } else if (curregs > availregs) {
  437. /*
  438. * Race detected: the counters were read in
  439. * separate mallctl calls and concurrent
  440. * operations happened in between. In this case
  441. * no meaningful utilization can be computed.
  442. */
  443. malloc_snprintf(util, sizeof(util), " race");
  444. } else {
  445. not_reached();
  446. }
  447. }
  448. col_size.size_val = reg_size;
  449. col_ind.unsigned_val = j;
  450. col_allocated.size_val = curregs * reg_size;
  451. col_nmalloc.uint64_val = nmalloc;
  452. col_nmalloc_ps.uint64_val = rate_per_second(nmalloc, uptime);
  453. col_ndalloc.uint64_val = ndalloc;
  454. col_ndalloc_ps.uint64_val = rate_per_second(ndalloc, uptime);
  455. col_nrequests.uint64_val = nrequests;
  456. col_nrequests_ps.uint64_val = rate_per_second(nrequests, uptime);
  457. if (prof_stats_on) {
  458. col_prof_live_requested.uint64_val = prof_live.req_sum;
  459. col_prof_live_count.uint64_val = prof_live.count;
  460. col_prof_accum_requested.uint64_val =
  461. prof_accum.req_sum;
  462. col_prof_accum_count.uint64_val = prof_accum.count;
  463. }
  464. col_nshards.unsigned_val = nshards;
  465. col_curregs.size_val = curregs;
  466. col_curslabs.size_val = curslabs;
  467. col_nonfull_slabs.size_val = nonfull_slabs;
  468. col_regs.unsigned_val = nregs;
  469. col_pgs.size_val = slab_size / page;
  470. col_util.str_val = util;
  471. col_nfills.uint64_val = nfills;
  472. col_nfills_ps.uint64_val = rate_per_second(nfills, uptime);
  473. col_nflushes.uint64_val = nflushes;
  474. col_nflushes_ps.uint64_val = rate_per_second(nflushes, uptime);
  475. col_nslabs.uint64_val = nslabs;
  476. col_nreslabs.uint64_val = nreslabs;
  477. col_nreslabs_ps.uint64_val = rate_per_second(nreslabs, uptime);
  478. /*
  479. * Note that mutex columns were initialized above, if mutex ==
  480. * true.
  481. */
  482. emitter_table_row(emitter, &row);
  483. }
  484. emitter_json_array_end(emitter); /* Close "bins". */
  485. if (in_gap) {
  486. emitter_table_printf(emitter, " ---\n");
  487. }
  488. }
  489. JEMALLOC_COLD
  490. static void
  491. stats_arena_lextents_print(emitter_t *emitter, unsigned i, uint64_t uptime) {
  492. unsigned nbins, nlextents, j;
  493. bool in_gap, in_gap_prev;
  494. CTL_GET("arenas.nbins", &nbins, unsigned);
  495. CTL_GET("arenas.nlextents", &nlextents, unsigned);
  496. emitter_row_t header_row;
  497. emitter_row_init(&header_row);
  498. emitter_row_t row;
  499. emitter_row_init(&row);
  500. bool prof_stats_on = config_prof && opt_prof && opt_prof_stats
  501. && i == MALLCTL_ARENAS_ALL;
  502. COL_HDR(row, size, NULL, right, 20, size)
  503. COL_HDR(row, ind, NULL, right, 4, unsigned)
  504. COL_HDR(row, allocated, NULL, right, 13, size)
  505. COL_HDR(row, nmalloc, NULL, right, 13, uint64)
  506. COL_HDR(row, nmalloc_ps, "(#/sec)", right, 8, uint64)
  507. COL_HDR(row, ndalloc, NULL, right, 13, uint64)
  508. COL_HDR(row, ndalloc_ps, "(#/sec)", right, 8, uint64)
  509. COL_HDR(row, nrequests, NULL, right, 13, uint64)
  510. COL_HDR(row, nrequests_ps, "(#/sec)", right, 8, uint64)
  511. COL_HDR_DECLARE(prof_live_requested)
  512. COL_HDR_DECLARE(prof_live_count)
  513. COL_HDR_DECLARE(prof_accum_requested)
  514. COL_HDR_DECLARE(prof_accum_count)
  515. if (prof_stats_on) {
  516. COL_HDR_INIT(row, prof_live_requested, NULL, right, 21, uint64)
  517. COL_HDR_INIT(row, prof_live_count, NULL, right, 17, uint64)
  518. COL_HDR_INIT(row, prof_accum_requested, NULL, right, 21, uint64)
  519. COL_HDR_INIT(row, prof_accum_count, NULL, right, 17, uint64)
  520. }
  521. COL_HDR(row, curlextents, NULL, right, 13, size)
  522. /* As with bins, we label the large extents table. */
  523. header_size.width -= 6;
  524. emitter_table_printf(emitter, "large:");
  525. emitter_table_row(emitter, &header_row);
  526. emitter_json_array_kv_begin(emitter, "lextents");
  527. size_t stats_arenas_mib[CTL_MAX_DEPTH];
  528. CTL_LEAF_PREPARE(stats_arenas_mib, 0, "stats.arenas");
  529. stats_arenas_mib[2] = i;
  530. CTL_LEAF_PREPARE(stats_arenas_mib, 3, "lextents");
  531. size_t arenas_lextent_mib[CTL_MAX_DEPTH];
  532. CTL_LEAF_PREPARE(arenas_lextent_mib, 0, "arenas.lextent");
  533. size_t prof_stats_mib[CTL_MAX_DEPTH];
  534. if (prof_stats_on) {
  535. CTL_LEAF_PREPARE(prof_stats_mib, 0, "prof.stats.lextents");
  536. }
  537. for (j = 0, in_gap = false; j < nlextents; j++) {
  538. uint64_t nmalloc, ndalloc, nrequests;
  539. size_t lextent_size, curlextents;
  540. prof_stats_t prof_live;
  541. prof_stats_t prof_accum;
  542. stats_arenas_mib[4] = j;
  543. arenas_lextent_mib[2] = j;
  544. CTL_LEAF(stats_arenas_mib, 5, "nmalloc", &nmalloc, uint64_t);
  545. CTL_LEAF(stats_arenas_mib, 5, "ndalloc", &ndalloc, uint64_t);
  546. CTL_LEAF(stats_arenas_mib, 5, "nrequests", &nrequests,
  547. uint64_t);
  548. in_gap_prev = in_gap;
  549. in_gap = (nrequests == 0);
  550. if (in_gap_prev && !in_gap) {
  551. emitter_table_printf(emitter,
  552. " ---\n");
  553. }
  554. CTL_LEAF(arenas_lextent_mib, 3, "size", &lextent_size, size_t);
  555. CTL_LEAF(stats_arenas_mib, 5, "curlextents", &curlextents,
  556. size_t);
  557. if (prof_stats_on) {
  558. prof_stats_mib[3] = j;
  559. CTL_LEAF(prof_stats_mib, 4, "live", &prof_live,
  560. prof_stats_t);
  561. CTL_LEAF(prof_stats_mib, 4, "accum", &prof_accum,
  562. prof_stats_t);
  563. }
  564. emitter_json_object_begin(emitter);
  565. if (prof_stats_on) {
  566. emitter_json_kv(emitter, "prof_live_requested",
  567. emitter_type_uint64, &prof_live.req_sum);
  568. emitter_json_kv(emitter, "prof_live_count",
  569. emitter_type_uint64, &prof_live.count);
  570. emitter_json_kv(emitter, "prof_accum_requested",
  571. emitter_type_uint64, &prof_accum.req_sum);
  572. emitter_json_kv(emitter, "prof_accum_count",
  573. emitter_type_uint64, &prof_accum.count);
  574. }
  575. emitter_json_kv(emitter, "curlextents", emitter_type_size,
  576. &curlextents);
  577. emitter_json_object_end(emitter);
  578. col_size.size_val = lextent_size;
  579. col_ind.unsigned_val = nbins + j;
  580. col_allocated.size_val = curlextents * lextent_size;
  581. col_nmalloc.uint64_val = nmalloc;
  582. col_nmalloc_ps.uint64_val = rate_per_second(nmalloc, uptime);
  583. col_ndalloc.uint64_val = ndalloc;
  584. col_ndalloc_ps.uint64_val = rate_per_second(ndalloc, uptime);
  585. col_nrequests.uint64_val = nrequests;
  586. col_nrequests_ps.uint64_val = rate_per_second(nrequests, uptime);
  587. if (prof_stats_on) {
  588. col_prof_live_requested.uint64_val = prof_live.req_sum;
  589. col_prof_live_count.uint64_val = prof_live.count;
  590. col_prof_accum_requested.uint64_val =
  591. prof_accum.req_sum;
  592. col_prof_accum_count.uint64_val = prof_accum.count;
  593. }
  594. col_curlextents.size_val = curlextents;
  595. if (!in_gap) {
  596. emitter_table_row(emitter, &row);
  597. }
  598. }
  599. emitter_json_array_end(emitter); /* Close "lextents". */
  600. if (in_gap) {
  601. emitter_table_printf(emitter, " ---\n");
  602. }
  603. }
  604. JEMALLOC_COLD
  605. static void
  606. stats_arena_extents_print(emitter_t *emitter, unsigned i) {
  607. unsigned j;
  608. bool in_gap, in_gap_prev;
  609. emitter_row_t header_row;
  610. emitter_row_init(&header_row);
  611. emitter_row_t row;
  612. emitter_row_init(&row);
  613. COL_HDR(row, size, NULL, right, 20, size)
  614. COL_HDR(row, ind, NULL, right, 4, unsigned)
  615. COL_HDR(row, ndirty, NULL, right, 13, size)
  616. COL_HDR(row, dirty, NULL, right, 13, size)
  617. COL_HDR(row, nmuzzy, NULL, right, 13, size)
  618. COL_HDR(row, muzzy, NULL, right, 13, size)
  619. COL_HDR(row, nretained, NULL, right, 13, size)
  620. COL_HDR(row, retained, NULL, right, 13, size)
  621. COL_HDR(row, ntotal, NULL, right, 13, size)
  622. COL_HDR(row, total, NULL, right, 13, size)
  623. /* Label this section. */
  624. header_size.width -= 8;
  625. emitter_table_printf(emitter, "extents:");
  626. emitter_table_row(emitter, &header_row);
  627. emitter_json_array_kv_begin(emitter, "extents");
  628. size_t stats_arenas_mib[CTL_MAX_DEPTH];
  629. CTL_LEAF_PREPARE(stats_arenas_mib, 0, "stats.arenas");
  630. stats_arenas_mib[2] = i;
  631. CTL_LEAF_PREPARE(stats_arenas_mib, 3, "extents");
  632. in_gap = false;
  633. for (j = 0; j < SC_NPSIZES; j++) {
  634. size_t ndirty, nmuzzy, nretained, total, dirty_bytes,
  635. muzzy_bytes, retained_bytes, total_bytes;
  636. stats_arenas_mib[4] = j;
  637. CTL_LEAF(stats_arenas_mib, 5, "ndirty", &ndirty, size_t);
  638. CTL_LEAF(stats_arenas_mib, 5, "nmuzzy", &nmuzzy, size_t);
  639. CTL_LEAF(stats_arenas_mib, 5, "nretained", &nretained, size_t);
  640. CTL_LEAF(stats_arenas_mib, 5, "dirty_bytes", &dirty_bytes,
  641. size_t);
  642. CTL_LEAF(stats_arenas_mib, 5, "muzzy_bytes", &muzzy_bytes,
  643. size_t);
  644. CTL_LEAF(stats_arenas_mib, 5, "retained_bytes",
  645. &retained_bytes, size_t);
  646. total = ndirty + nmuzzy + nretained;
  647. total_bytes = dirty_bytes + muzzy_bytes + retained_bytes;
  648. in_gap_prev = in_gap;
  649. in_gap = (total == 0);
  650. if (in_gap_prev && !in_gap) {
  651. emitter_table_printf(emitter,
  652. " ---\n");
  653. }
  654. emitter_json_object_begin(emitter);
  655. emitter_json_kv(emitter, "ndirty", emitter_type_size, &ndirty);
  656. emitter_json_kv(emitter, "nmuzzy", emitter_type_size, &nmuzzy);
  657. emitter_json_kv(emitter, "nretained", emitter_type_size,
  658. &nretained);
  659. emitter_json_kv(emitter, "dirty_bytes", emitter_type_size,
  660. &dirty_bytes);
  661. emitter_json_kv(emitter, "muzzy_bytes", emitter_type_size,
  662. &muzzy_bytes);
  663. emitter_json_kv(emitter, "retained_bytes", emitter_type_size,
  664. &retained_bytes);
  665. emitter_json_object_end(emitter);
  666. col_size.size_val = sz_pind2sz(j);
  667. col_ind.size_val = j;
  668. col_ndirty.size_val = ndirty;
  669. col_dirty.size_val = dirty_bytes;
  670. col_nmuzzy.size_val = nmuzzy;
  671. col_muzzy.size_val = muzzy_bytes;
  672. col_nretained.size_val = nretained;
  673. col_retained.size_val = retained_bytes;
  674. col_ntotal.size_val = total;
  675. col_total.size_val = total_bytes;
  676. if (!in_gap) {
  677. emitter_table_row(emitter, &row);
  678. }
  679. }
  680. emitter_json_array_end(emitter); /* Close "extents". */
  681. if (in_gap) {
  682. emitter_table_printf(emitter, " ---\n");
  683. }
  684. }
  685. static void
  686. stats_arena_hpa_shard_print(emitter_t *emitter, unsigned i, uint64_t uptime) {
  687. emitter_row_t header_row;
  688. emitter_row_init(&header_row);
  689. emitter_row_t row;
  690. emitter_row_init(&row);
  691. uint64_t npurge_passes;
  692. uint64_t npurges;
  693. uint64_t nhugifies;
  694. uint64_t ndehugifies;
  695. CTL_M2_GET("stats.arenas.0.hpa_shard.npurge_passes",
  696. i, &npurge_passes, uint64_t);
  697. CTL_M2_GET("stats.arenas.0.hpa_shard.npurges",
  698. i, &npurges, uint64_t);
  699. CTL_M2_GET("stats.arenas.0.hpa_shard.nhugifies",
  700. i, &nhugifies, uint64_t);
  701. CTL_M2_GET("stats.arenas.0.hpa_shard.ndehugifies",
  702. i, &ndehugifies, uint64_t);
  703. size_t npageslabs_huge;
  704. size_t nactive_huge;
  705. size_t ndirty_huge;
  706. size_t npageslabs_nonhuge;
  707. size_t nactive_nonhuge;
  708. size_t ndirty_nonhuge;
  709. size_t nretained_nonhuge;
  710. size_t sec_bytes;
  711. CTL_M2_GET("stats.arenas.0.hpa_sec_bytes", i, &sec_bytes, size_t);
  712. emitter_kv(emitter, "sec_bytes", "Bytes in small extent cache",
  713. emitter_type_size, &sec_bytes);
  714. /* First, global stats. */
  715. emitter_table_printf(emitter,
  716. "HPA shard stats:\n"
  717. " Purge passes: %" FMTu64 " (%" FMTu64 " / sec)\n"
  718. " Purges: %" FMTu64 " (%" FMTu64 " / sec)\n"
  719. " Hugeifies: %" FMTu64 " (%" FMTu64 " / sec)\n"
  720. " Dehugifies: %" FMTu64 " (%" FMTu64 " / sec)\n"
  721. "\n",
  722. npurge_passes, rate_per_second(npurge_passes, uptime),
  723. npurges, rate_per_second(npurges, uptime),
  724. nhugifies, rate_per_second(nhugifies, uptime),
  725. ndehugifies, rate_per_second(ndehugifies, uptime));
  726. emitter_json_object_kv_begin(emitter, "hpa_shard");
  727. emitter_json_kv(emitter, "npurge_passes", emitter_type_uint64,
  728. &npurge_passes);
  729. emitter_json_kv(emitter, "npurges", emitter_type_uint64,
  730. &npurges);
  731. emitter_json_kv(emitter, "nhugifies", emitter_type_uint64,
  732. &nhugifies);
  733. emitter_json_kv(emitter, "ndehugifies", emitter_type_uint64,
  734. &ndehugifies);
  735. /* Next, full slab stats. */
  736. CTL_M2_GET("stats.arenas.0.hpa_shard.full_slabs.npageslabs_huge",
  737. i, &npageslabs_huge, size_t);
  738. CTL_M2_GET("stats.arenas.0.hpa_shard.full_slabs.nactive_huge",
  739. i, &nactive_huge, size_t);
  740. CTL_M2_GET("stats.arenas.0.hpa_shard.full_slabs.ndirty_huge",
  741. i, &ndirty_huge, size_t);
  742. CTL_M2_GET("stats.arenas.0.hpa_shard.full_slabs.npageslabs_nonhuge",
  743. i, &npageslabs_nonhuge, size_t);
  744. CTL_M2_GET("stats.arenas.0.hpa_shard.full_slabs.nactive_nonhuge",
  745. i, &nactive_nonhuge, size_t);
  746. CTL_M2_GET("stats.arenas.0.hpa_shard.full_slabs.ndirty_nonhuge",
  747. i, &ndirty_nonhuge, size_t);
  748. nretained_nonhuge = npageslabs_nonhuge * HUGEPAGE_PAGES
  749. - nactive_nonhuge - ndirty_nonhuge;
  750. emitter_table_printf(emitter,
  751. " In full slabs:\n"
  752. " npageslabs: %zu huge, %zu nonhuge\n"
  753. " nactive: %zu huge, %zu nonhuge \n"
  754. " ndirty: %zu huge, %zu nonhuge \n"
  755. " nretained: 0 huge, %zu nonhuge \n",
  756. npageslabs_huge, npageslabs_nonhuge,
  757. nactive_huge, nactive_nonhuge,
  758. ndirty_huge, ndirty_nonhuge,
  759. nretained_nonhuge);
  760. emitter_json_object_kv_begin(emitter, "full_slabs");
  761. emitter_json_kv(emitter, "npageslabs_huge", emitter_type_size,
  762. &npageslabs_huge);
  763. emitter_json_kv(emitter, "nactive_huge", emitter_type_size,
  764. &nactive_huge);
  765. emitter_json_kv(emitter, "nactive_huge", emitter_type_size,
  766. &nactive_huge);
  767. emitter_json_kv(emitter, "npageslabs_nonhuge", emitter_type_size,
  768. &npageslabs_nonhuge);
  769. emitter_json_kv(emitter, "nactive_nonhuge", emitter_type_size,
  770. &nactive_nonhuge);
  771. emitter_json_kv(emitter, "ndirty_nonhuge", emitter_type_size,
  772. &ndirty_nonhuge);
  773. emitter_json_object_end(emitter); /* End "full_slabs" */
  774. /* Next, empty slab stats. */
  775. CTL_M2_GET("stats.arenas.0.hpa_shard.empty_slabs.npageslabs_huge",
  776. i, &npageslabs_huge, size_t);
  777. CTL_M2_GET("stats.arenas.0.hpa_shard.empty_slabs.nactive_huge",
  778. i, &nactive_huge, size_t);
  779. CTL_M2_GET("stats.arenas.0.hpa_shard.empty_slabs.ndirty_huge",
  780. i, &ndirty_huge, size_t);
  781. CTL_M2_GET("stats.arenas.0.hpa_shard.empty_slabs.npageslabs_nonhuge",
  782. i, &npageslabs_nonhuge, size_t);
  783. CTL_M2_GET("stats.arenas.0.hpa_shard.empty_slabs.nactive_nonhuge",
  784. i, &nactive_nonhuge, size_t);
  785. CTL_M2_GET("stats.arenas.0.hpa_shard.empty_slabs.ndirty_nonhuge",
  786. i, &ndirty_nonhuge, size_t);
  787. nretained_nonhuge = npageslabs_nonhuge * HUGEPAGE_PAGES
  788. - nactive_nonhuge - ndirty_nonhuge;
  789. emitter_table_printf(emitter,
  790. " In empty slabs:\n"
  791. " npageslabs: %zu huge, %zu nonhuge\n"
  792. " nactive: %zu huge, %zu nonhuge \n"
  793. " ndirty: %zu huge, %zu nonhuge \n"
  794. " nretained: 0 huge, %zu nonhuge \n"
  795. "\n",
  796. npageslabs_huge, npageslabs_nonhuge,
  797. nactive_huge, nactive_nonhuge,
  798. ndirty_huge, ndirty_nonhuge,
  799. nretained_nonhuge);
  800. emitter_json_object_kv_begin(emitter, "empty_slabs");
  801. emitter_json_kv(emitter, "npageslabs_huge", emitter_type_size,
  802. &npageslabs_huge);
  803. emitter_json_kv(emitter, "nactive_huge", emitter_type_size,
  804. &nactive_huge);
  805. emitter_json_kv(emitter, "nactive_huge", emitter_type_size,
  806. &nactive_huge);
  807. emitter_json_kv(emitter, "npageslabs_nonhuge", emitter_type_size,
  808. &npageslabs_nonhuge);
  809. emitter_json_kv(emitter, "nactive_nonhuge", emitter_type_size,
  810. &nactive_nonhuge);
  811. emitter_json_kv(emitter, "ndirty_nonhuge", emitter_type_size,
  812. &ndirty_nonhuge);
  813. emitter_json_object_end(emitter); /* End "empty_slabs" */
  814. COL_HDR(row, size, NULL, right, 20, size)
  815. COL_HDR(row, ind, NULL, right, 4, unsigned)
  816. COL_HDR(row, npageslabs_huge, NULL, right, 16, size)
  817. COL_HDR(row, nactive_huge, NULL, right, 16, size)
  818. COL_HDR(row, ndirty_huge, NULL, right, 16, size)
  819. COL_HDR(row, npageslabs_nonhuge, NULL, right, 20, size)
  820. COL_HDR(row, nactive_nonhuge, NULL, right, 20, size)
  821. COL_HDR(row, ndirty_nonhuge, NULL, right, 20, size)
  822. COL_HDR(row, nretained_nonhuge, NULL, right, 20, size)
  823. size_t stats_arenas_mib[CTL_MAX_DEPTH];
  824. CTL_LEAF_PREPARE(stats_arenas_mib, 0, "stats.arenas");
  825. stats_arenas_mib[2] = i;
  826. CTL_LEAF_PREPARE(stats_arenas_mib, 3, "hpa_shard.nonfull_slabs");
  827. emitter_table_row(emitter, &header_row);
  828. emitter_json_array_kv_begin(emitter, "nonfull_slabs");
  829. bool in_gap = false;
  830. for (pszind_t j = 0; j < PSSET_NPSIZES && j < SC_NPSIZES; j++) {
  831. stats_arenas_mib[5] = j;
  832. CTL_LEAF(stats_arenas_mib, 6, "npageslabs_huge",
  833. &npageslabs_huge, size_t);
  834. CTL_LEAF(stats_arenas_mib, 6, "nactive_huge",
  835. &nactive_huge, size_t);
  836. CTL_LEAF(stats_arenas_mib, 6, "ndirty_huge",
  837. &ndirty_huge, size_t);
  838. CTL_LEAF(stats_arenas_mib, 6, "npageslabs_nonhuge",
  839. &npageslabs_nonhuge, size_t);
  840. CTL_LEAF(stats_arenas_mib, 6, "nactive_nonhuge",
  841. &nactive_nonhuge, size_t);
  842. CTL_LEAF(stats_arenas_mib, 6, "ndirty_nonhuge",
  843. &ndirty_nonhuge, size_t);
  844. nretained_nonhuge = npageslabs_nonhuge * HUGEPAGE_PAGES
  845. - nactive_nonhuge - ndirty_nonhuge;
  846. bool in_gap_prev = in_gap;
  847. in_gap = (npageslabs_huge == 0 && npageslabs_nonhuge == 0);
  848. if (in_gap_prev && !in_gap) {
  849. emitter_table_printf(emitter,
  850. " ---\n");
  851. }
  852. col_size.size_val = sz_pind2sz(j);
  853. col_ind.size_val = j;
  854. col_npageslabs_huge.size_val = npageslabs_huge;
  855. col_nactive_huge.size_val = nactive_huge;
  856. col_ndirty_huge.size_val = ndirty_huge;
  857. col_npageslabs_nonhuge.size_val = npageslabs_nonhuge;
  858. col_nactive_nonhuge.size_val = nactive_nonhuge;
  859. col_ndirty_nonhuge.size_val = ndirty_nonhuge;
  860. col_nretained_nonhuge.size_val = nretained_nonhuge;
  861. if (!in_gap) {
  862. emitter_table_row(emitter, &row);
  863. }
  864. emitter_json_object_begin(emitter);
  865. emitter_json_kv(emitter, "npageslabs_huge", emitter_type_size,
  866. &npageslabs_huge);
  867. emitter_json_kv(emitter, "nactive_huge", emitter_type_size,
  868. &nactive_huge);
  869. emitter_json_kv(emitter, "ndirty_huge", emitter_type_size,
  870. &ndirty_huge);
  871. emitter_json_kv(emitter, "npageslabs_nonhuge", emitter_type_size,
  872. &npageslabs_nonhuge);
  873. emitter_json_kv(emitter, "nactive_nonhuge", emitter_type_size,
  874. &nactive_nonhuge);
  875. emitter_json_kv(emitter, "ndirty_nonhuge", emitter_type_size,
  876. &ndirty_nonhuge);
  877. emitter_json_object_end(emitter);
  878. }
  879. emitter_json_array_end(emitter); /* End "nonfull_slabs" */
  880. emitter_json_object_end(emitter); /* End "hpa_shard" */
  881. if (in_gap) {
  882. emitter_table_printf(emitter, " ---\n");
  883. }
  884. }
  885. static void
  886. stats_arena_mutexes_print(emitter_t *emitter, unsigned arena_ind, uint64_t uptime) {
  887. emitter_row_t row;
  888. emitter_col_t col_name;
  889. emitter_col_t col64[mutex_prof_num_uint64_t_counters];
  890. emitter_col_t col32[mutex_prof_num_uint32_t_counters];
  891. emitter_row_init(&row);
  892. mutex_stats_init_cols(&row, "", &col_name, col64, col32);
  893. emitter_json_object_kv_begin(emitter, "mutexes");
  894. emitter_table_row(emitter, &row);
  895. size_t stats_arenas_mib[CTL_MAX_DEPTH];
  896. CTL_LEAF_PREPARE(stats_arenas_mib, 0, "stats.arenas");
  897. stats_arenas_mib[2] = arena_ind;
  898. CTL_LEAF_PREPARE(stats_arenas_mib, 3, "mutexes");
  899. for (mutex_prof_arena_ind_t i = 0; i < mutex_prof_num_arena_mutexes;
  900. i++) {
  901. const char *name = arena_mutex_names[i];
  902. emitter_json_object_kv_begin(emitter, name);
  903. mutex_stats_read_arena(stats_arenas_mib, 4, name, &col_name,
  904. col64, col32, uptime);
  905. mutex_stats_emit(emitter, &row, col64, col32);
  906. emitter_json_object_end(emitter); /* Close the mutex dict. */
  907. }
  908. emitter_json_object_end(emitter); /* End "mutexes". */
  909. }
  910. JEMALLOC_COLD
  911. static void
  912. stats_arena_print(emitter_t *emitter, unsigned i, bool bins, bool large,
  913. bool mutex, bool extents, bool hpa) {
  914. unsigned nthreads;
  915. const char *dss;
  916. ssize_t dirty_decay_ms, muzzy_decay_ms;
  917. size_t page, pactive, pdirty, pmuzzy, mapped, retained;
  918. size_t base, internal, resident, metadata_thp, extent_avail;
  919. uint64_t dirty_npurge, dirty_nmadvise, dirty_purged;
  920. uint64_t muzzy_npurge, muzzy_nmadvise, muzzy_purged;
  921. size_t small_allocated;
  922. uint64_t small_nmalloc, small_ndalloc, small_nrequests, small_nfills,
  923. small_nflushes;
  924. size_t large_allocated;
  925. uint64_t large_nmalloc, large_ndalloc, large_nrequests, large_nfills,
  926. large_nflushes;
  927. size_t tcache_bytes, tcache_stashed_bytes, abandoned_vm;
  928. uint64_t uptime;
  929. CTL_GET("arenas.page", &page, size_t);
  930. CTL_M2_GET("stats.arenas.0.nthreads", i, &nthreads, unsigned);
  931. emitter_kv(emitter, "nthreads", "assigned threads",
  932. emitter_type_unsigned, &nthreads);
  933. CTL_M2_GET("stats.arenas.0.uptime", i, &uptime, uint64_t);
  934. emitter_kv(emitter, "uptime_ns", "uptime", emitter_type_uint64,
  935. &uptime);
  936. CTL_M2_GET("stats.arenas.0.dss", i, &dss, const char *);
  937. emitter_kv(emitter, "dss", "dss allocation precedence",
  938. emitter_type_string, &dss);
  939. CTL_M2_GET("stats.arenas.0.dirty_decay_ms", i, &dirty_decay_ms,
  940. ssize_t);
  941. CTL_M2_GET("stats.arenas.0.muzzy_decay_ms", i, &muzzy_decay_ms,
  942. ssize_t);
  943. CTL_M2_GET("stats.arenas.0.pactive", i, &pactive, size_t);
  944. CTL_M2_GET("stats.arenas.0.pdirty", i, &pdirty, size_t);
  945. CTL_M2_GET("stats.arenas.0.pmuzzy", i, &pmuzzy, size_t);
  946. CTL_M2_GET("stats.arenas.0.dirty_npurge", i, &dirty_npurge, uint64_t);
  947. CTL_M2_GET("stats.arenas.0.dirty_nmadvise", i, &dirty_nmadvise,
  948. uint64_t);
  949. CTL_M2_GET("stats.arenas.0.dirty_purged", i, &dirty_purged, uint64_t);
  950. CTL_M2_GET("stats.arenas.0.muzzy_npurge", i, &muzzy_npurge, uint64_t);
  951. CTL_M2_GET("stats.arenas.0.muzzy_nmadvise", i, &muzzy_nmadvise,
  952. uint64_t);
  953. CTL_M2_GET("stats.arenas.0.muzzy_purged", i, &muzzy_purged, uint64_t);
  954. emitter_row_t decay_row;
  955. emitter_row_init(&decay_row);
  956. /* JSON-style emission. */
  957. emitter_json_kv(emitter, "dirty_decay_ms", emitter_type_ssize,
  958. &dirty_decay_ms);
  959. emitter_json_kv(emitter, "muzzy_decay_ms", emitter_type_ssize,
  960. &muzzy_decay_ms);
  961. emitter_json_kv(emitter, "pactive", emitter_type_size, &pactive);
  962. emitter_json_kv(emitter, "pdirty", emitter_type_size, &pdirty);
  963. emitter_json_kv(emitter, "pmuzzy", emitter_type_size, &pmuzzy);
  964. emitter_json_kv(emitter, "dirty_npurge", emitter_type_uint64,
  965. &dirty_npurge);
  966. emitter_json_kv(emitter, "dirty_nmadvise", emitter_type_uint64,
  967. &dirty_nmadvise);
  968. emitter_json_kv(emitter, "dirty_purged", emitter_type_uint64,
  969. &dirty_purged);
  970. emitter_json_kv(emitter, "muzzy_npurge", emitter_type_uint64,
  971. &muzzy_npurge);
  972. emitter_json_kv(emitter, "muzzy_nmadvise", emitter_type_uint64,
  973. &muzzy_nmadvise);
  974. emitter_json_kv(emitter, "muzzy_purged", emitter_type_uint64,
  975. &muzzy_purged);
  976. /* Table-style emission. */
  977. COL(decay_row, decay_type, right, 9, title);
  978. col_decay_type.str_val = "decaying:";
  979. COL(decay_row, decay_time, right, 6, title);
  980. col_decay_time.str_val = "time";
  981. COL(decay_row, decay_npages, right, 13, title);
  982. col_decay_npages.str_val = "npages";
  983. COL(decay_row, decay_sweeps, right, 13, title);
  984. col_decay_sweeps.str_val = "sweeps";
  985. COL(decay_row, decay_madvises, right, 13, title);
  986. col_decay_madvises.str_val = "madvises";
  987. COL(decay_row, decay_purged, right, 13, title);
  988. col_decay_purged.str_val = "purged";
  989. /* Title row. */
  990. emitter_table_row(emitter, &decay_row);
  991. /* Dirty row. */
  992. col_decay_type.str_val = "dirty:";
  993. if (dirty_decay_ms >= 0) {
  994. col_decay_time.type = emitter_type_ssize;
  995. col_decay_time.ssize_val = dirty_decay_ms;
  996. } else {
  997. col_decay_time.type = emitter_type_title;
  998. col_decay_time.str_val = "N/A";
  999. }
  1000. col_decay_npages.type = emitter_type_size;
  1001. col_decay_npages.size_val = pdirty;
  1002. col_decay_sweeps.type = emitter_type_uint64;
  1003. col_decay_sweeps.uint64_val = dirty_npurge;
  1004. col_decay_madvises.type = emitter_type_uint64;
  1005. col_decay_madvises.uint64_val = dirty_nmadvise;
  1006. col_decay_purged.type = emitter_type_uint64;
  1007. col_decay_purged.uint64_val = dirty_purged;
  1008. emitter_table_row(emitter, &decay_row);
  1009. /* Muzzy row. */
  1010. col_decay_type.str_val = "muzzy:";
  1011. if (muzzy_decay_ms >= 0) {
  1012. col_decay_time.type = emitter_type_ssize;
  1013. col_decay_time.ssize_val = muzzy_decay_ms;
  1014. } else {
  1015. col_decay_time.type = emitter_type_title;
  1016. col_decay_time.str_val = "N/A";
  1017. }
  1018. col_decay_npages.type = emitter_type_size;
  1019. col_decay_npages.size_val = pmuzzy;
  1020. col_decay_sweeps.type = emitter_type_uint64;
  1021. col_decay_sweeps.uint64_val = muzzy_npurge;
  1022. col_decay_madvises.type = emitter_type_uint64;
  1023. col_decay_madvises.uint64_val = muzzy_nmadvise;
  1024. col_decay_purged.type = emitter_type_uint64;
  1025. col_decay_purged.uint64_val = muzzy_purged;
  1026. emitter_table_row(emitter, &decay_row);
  1027. /* Small / large / total allocation counts. */
  1028. emitter_row_t alloc_count_row;
  1029. emitter_row_init(&alloc_count_row);
  1030. COL(alloc_count_row, count_title, left, 21, title);
  1031. col_count_title.str_val = "";
  1032. COL(alloc_count_row, count_allocated, right, 16, title);
  1033. col_count_allocated.str_val = "allocated";
  1034. COL(alloc_count_row, count_nmalloc, right, 16, title);
  1035. col_count_nmalloc.str_val = "nmalloc";
  1036. COL(alloc_count_row, count_nmalloc_ps, right, 10, title);
  1037. col_count_nmalloc_ps.str_val = "(#/sec)";
  1038. COL(alloc_count_row, count_ndalloc, right, 16, title);
  1039. col_count_ndalloc.str_val = "ndalloc";
  1040. COL(alloc_count_row, count_ndalloc_ps, right, 10, title);
  1041. col_count_ndalloc_ps.str_val = "(#/sec)";
  1042. COL(alloc_count_row, count_nrequests, right, 16, title);
  1043. col_count_nrequests.str_val = "nrequests";
  1044. COL(alloc_count_row, count_nrequests_ps, right, 10, title);
  1045. col_count_nrequests_ps.str_val = "(#/sec)";
  1046. COL(alloc_count_row, count_nfills, right, 16, title);
  1047. col_count_nfills.str_val = "nfill";
  1048. COL(alloc_count_row, count_nfills_ps, right, 10, title);
  1049. col_count_nfills_ps.str_val = "(#/sec)";
  1050. COL(alloc_count_row, count_nflushes, right, 16, title);
  1051. col_count_nflushes.str_val = "nflush";
  1052. COL(alloc_count_row, count_nflushes_ps, right, 10, title);
  1053. col_count_nflushes_ps.str_val = "(#/sec)";
  1054. emitter_table_row(emitter, &alloc_count_row);
  1055. col_count_nmalloc_ps.type = emitter_type_uint64;
  1056. col_count_ndalloc_ps.type = emitter_type_uint64;
  1057. col_count_nrequests_ps.type = emitter_type_uint64;
  1058. col_count_nfills_ps.type = emitter_type_uint64;
  1059. col_count_nflushes_ps.type = emitter_type_uint64;
  1060. #define GET_AND_EMIT_ALLOC_STAT(small_or_large, name, valtype) \
  1061. CTL_M2_GET("stats.arenas.0." #small_or_large "." #name, i, \
  1062. &small_or_large##_##name, valtype##_t); \
  1063. emitter_json_kv(emitter, #name, emitter_type_##valtype, \
  1064. &small_or_large##_##name); \
  1065. col_count_##name.type = emitter_type_##valtype; \
  1066. col_count_##name.valtype##_val = small_or_large##_##name;
  1067. emitter_json_object_kv_begin(emitter, "small");
  1068. col_count_title.str_val = "small:";
  1069. GET_AND_EMIT_ALLOC_STAT(small, allocated, size)
  1070. GET_AND_EMIT_ALLOC_STAT(small, nmalloc, uint64)
  1071. col_count_nmalloc_ps.uint64_val =
  1072. rate_per_second(col_count_nmalloc.uint64_val, uptime);
  1073. GET_AND_EMIT_ALLOC_STAT(small, ndalloc, uint64)
  1074. col_count_ndalloc_ps.uint64_val =
  1075. rate_per_second(col_count_ndalloc.uint64_val, uptime);
  1076. GET_AND_EMIT_ALLOC_STAT(small, nrequests, uint64)
  1077. col_count_nrequests_ps.uint64_val =
  1078. rate_per_second(col_count_nrequests.uint64_val, uptime);
  1079. GET_AND_EMIT_ALLOC_STAT(small, nfills, uint64)
  1080. col_count_nfills_ps.uint64_val =
  1081. rate_per_second(col_count_nfills.uint64_val, uptime);
  1082. GET_AND_EMIT_ALLOC_STAT(small, nflushes, uint64)
  1083. col_count_nflushes_ps.uint64_val =
  1084. rate_per_second(col_count_nflushes.uint64_val, uptime);
  1085. emitter_table_row(emitter, &alloc_count_row);
  1086. emitter_json_object_end(emitter); /* Close "small". */
  1087. emitter_json_object_kv_begin(emitter, "large");
  1088. col_count_title.str_val = "large:";
  1089. GET_AND_EMIT_ALLOC_STAT(large, allocated, size)
  1090. GET_AND_EMIT_ALLOC_STAT(large, nmalloc, uint64)
  1091. col_count_nmalloc_ps.uint64_val =
  1092. rate_per_second(col_count_nmalloc.uint64_val, uptime);
  1093. GET_AND_EMIT_ALLOC_STAT(large, ndalloc, uint64)
  1094. col_count_ndalloc_ps.uint64_val =
  1095. rate_per_second(col_count_ndalloc.uint64_val, uptime);
  1096. GET_AND_EMIT_ALLOC_STAT(large, nrequests, uint64)
  1097. col_count_nrequests_ps.uint64_val =
  1098. rate_per_second(col_count_nrequests.uint64_val, uptime);
  1099. GET_AND_EMIT_ALLOC_STAT(large, nfills, uint64)
  1100. col_count_nfills_ps.uint64_val =
  1101. rate_per_second(col_count_nfills.uint64_val, uptime);
  1102. GET_AND_EMIT_ALLOC_STAT(large, nflushes, uint64)
  1103. col_count_nflushes_ps.uint64_val =
  1104. rate_per_second(col_count_nflushes.uint64_val, uptime);
  1105. emitter_table_row(emitter, &alloc_count_row);
  1106. emitter_json_object_end(emitter); /* Close "large". */
  1107. #undef GET_AND_EMIT_ALLOC_STAT
  1108. /* Aggregated small + large stats are emitter only in table mode. */
  1109. col_count_title.str_val = "total:";
  1110. col_count_allocated.size_val = small_allocated + large_allocated;
  1111. col_count_nmalloc.uint64_val = small_nmalloc + large_nmalloc;
  1112. col_count_ndalloc.uint64_val = small_ndalloc + large_ndalloc;
  1113. col_count_nrequests.uint64_val = small_nrequests + large_nrequests;
  1114. col_count_nfills.uint64_val = small_nfills + large_nfills;
  1115. col_count_nflushes.uint64_val = small_nflushes + large_nflushes;
  1116. col_count_nmalloc_ps.uint64_val =
  1117. rate_per_second(col_count_nmalloc.uint64_val, uptime);
  1118. col_count_ndalloc_ps.uint64_val =
  1119. rate_per_second(col_count_ndalloc.uint64_val, uptime);
  1120. col_count_nrequests_ps.uint64_val =
  1121. rate_per_second(col_count_nrequests.uint64_val, uptime);
  1122. col_count_nfills_ps.uint64_val =
  1123. rate_per_second(col_count_nfills.uint64_val, uptime);
  1124. col_count_nflushes_ps.uint64_val =
  1125. rate_per_second(col_count_nflushes.uint64_val, uptime);
  1126. emitter_table_row(emitter, &alloc_count_row);
  1127. emitter_row_t mem_count_row;
  1128. emitter_row_init(&mem_count_row);
  1129. emitter_col_t mem_count_title;
  1130. emitter_col_init(&mem_count_title, &mem_count_row);
  1131. mem_count_title.justify = emitter_justify_left;
  1132. mem_count_title.width = 21;
  1133. mem_count_title.type = emitter_type_title;
  1134. mem_count_title.str_val = "";
  1135. emitter_col_t mem_count_val;
  1136. emitter_col_init(&mem_count_val, &mem_count_row);
  1137. mem_count_val.justify = emitter_justify_right;
  1138. mem_count_val.width = 16;
  1139. mem_count_val.type = emitter_type_title;
  1140. mem_count_val.str_val = "";
  1141. emitter_table_row(emitter, &mem_count_row);
  1142. mem_count_val.type = emitter_type_size;
  1143. /* Active count in bytes is emitted only in table mode. */
  1144. mem_count_title.str_val = "active:";
  1145. mem_count_val.size_val = pactive * page;
  1146. emitter_table_row(emitter, &mem_count_row);
  1147. #define GET_AND_EMIT_MEM_STAT(stat) \
  1148. CTL_M2_GET("stats.arenas.0."#stat, i, &stat, size_t); \
  1149. emitter_json_kv(emitter, #stat, emitter_type_size, &stat); \
  1150. mem_count_title.str_val = #stat":"; \
  1151. mem_count_val.size_val = stat; \
  1152. emitter_table_row(emitter, &mem_count_row);
  1153. GET_AND_EMIT_MEM_STAT(mapped)
  1154. GET_AND_EMIT_MEM_STAT(retained)
  1155. GET_AND_EMIT_MEM_STAT(base)
  1156. GET_AND_EMIT_MEM_STAT(internal)
  1157. GET_AND_EMIT_MEM_STAT(metadata_thp)
  1158. GET_AND_EMIT_MEM_STAT(tcache_bytes)
  1159. GET_AND_EMIT_MEM_STAT(tcache_stashed_bytes)
  1160. GET_AND_EMIT_MEM_STAT(resident)
  1161. GET_AND_EMIT_MEM_STAT(abandoned_vm)
  1162. GET_AND_EMIT_MEM_STAT(extent_avail)
  1163. #undef GET_AND_EMIT_MEM_STAT
  1164. if (mutex) {
  1165. stats_arena_mutexes_print(emitter, i, uptime);
  1166. }
  1167. if (bins) {
  1168. stats_arena_bins_print(emitter, mutex, i, uptime);
  1169. }
  1170. if (large) {
  1171. stats_arena_lextents_print(emitter, i, uptime);
  1172. }
  1173. if (extents) {
  1174. stats_arena_extents_print(emitter, i);
  1175. }
  1176. if (hpa) {
  1177. stats_arena_hpa_shard_print(emitter, i, uptime);
  1178. }
  1179. }
  1180. JEMALLOC_COLD
  1181. static void
  1182. stats_general_print(emitter_t *emitter) {
  1183. const char *cpv;
  1184. bool bv, bv2;
  1185. unsigned uv;
  1186. uint32_t u32v;
  1187. uint64_t u64v;
  1188. int64_t i64v;
  1189. ssize_t ssv, ssv2;
  1190. size_t sv, bsz, usz, u32sz, u64sz, i64sz, ssz, sssz, cpsz;
  1191. bsz = sizeof(bool);
  1192. usz = sizeof(unsigned);
  1193. ssz = sizeof(size_t);
  1194. sssz = sizeof(ssize_t);
  1195. cpsz = sizeof(const char *);
  1196. u32sz = sizeof(uint32_t);
  1197. i64sz = sizeof(int64_t);
  1198. u64sz = sizeof(uint64_t);
  1199. CTL_GET("version", &cpv, const char *);
  1200. emitter_kv(emitter, "version", "Version", emitter_type_string, &cpv);
  1201. /* config. */
  1202. emitter_dict_begin(emitter, "config", "Build-time option settings");
  1203. #define CONFIG_WRITE_BOOL(name) \
  1204. do { \
  1205. CTL_GET("config."#name, &bv, bool); \
  1206. emitter_kv(emitter, #name, "config."#name, \
  1207. emitter_type_bool, &bv); \
  1208. } while (0)
  1209. CONFIG_WRITE_BOOL(cache_oblivious);
  1210. CONFIG_WRITE_BOOL(debug);
  1211. CONFIG_WRITE_BOOL(fill);
  1212. CONFIG_WRITE_BOOL(lazy_lock);
  1213. emitter_kv(emitter, "malloc_conf", "config.malloc_conf",
  1214. emitter_type_string, &config_malloc_conf);
  1215. CONFIG_WRITE_BOOL(opt_safety_checks);
  1216. CONFIG_WRITE_BOOL(prof);
  1217. CONFIG_WRITE_BOOL(prof_libgcc);
  1218. CONFIG_WRITE_BOOL(prof_libunwind);
  1219. CONFIG_WRITE_BOOL(stats);
  1220. CONFIG_WRITE_BOOL(utrace);
  1221. CONFIG_WRITE_BOOL(xmalloc);
  1222. #undef CONFIG_WRITE_BOOL
  1223. emitter_dict_end(emitter); /* Close "config" dict. */
  1224. /* opt. */
  1225. #define OPT_WRITE(name, var, size, emitter_type) \
  1226. if (je_mallctl("opt."name, (void *)&var, &size, NULL, 0) == \
  1227. 0) { \
  1228. emitter_kv(emitter, name, "opt."name, emitter_type, \
  1229. &var); \
  1230. }
  1231. #define OPT_WRITE_MUTABLE(name, var1, var2, size, emitter_type, \
  1232. altname) \
  1233. if (je_mallctl("opt."name, (void *)&var1, &size, NULL, 0) == \
  1234. 0 && je_mallctl(altname, (void *)&var2, &size, NULL, 0) \
  1235. == 0) { \
  1236. emitter_kv_note(emitter, name, "opt."name, \
  1237. emitter_type, &var1, altname, emitter_type, \
  1238. &var2); \
  1239. }
  1240. #define OPT_WRITE_BOOL(name) OPT_WRITE(name, bv, bsz, emitter_type_bool)
  1241. #define OPT_WRITE_BOOL_MUTABLE(name, altname) \
  1242. OPT_WRITE_MUTABLE(name, bv, bv2, bsz, emitter_type_bool, altname)
  1243. #define OPT_WRITE_UNSIGNED(name) \
  1244. OPT_WRITE(name, uv, usz, emitter_type_unsigned)
  1245. #define OPT_WRITE_INT64(name) \
  1246. OPT_WRITE(name, i64v, i64sz, emitter_type_int64)
  1247. #define OPT_WRITE_UINT64(name) \
  1248. OPT_WRITE(name, u64v, u64sz, emitter_type_uint64)
  1249. #define OPT_WRITE_SIZE_T(name) \
  1250. OPT_WRITE(name, sv, ssz, emitter_type_size)
  1251. #define OPT_WRITE_SSIZE_T(name) \
  1252. OPT_WRITE(name, ssv, sssz, emitter_type_ssize)
  1253. #define OPT_WRITE_SSIZE_T_MUTABLE(name, altname) \
  1254. OPT_WRITE_MUTABLE(name, ssv, ssv2, sssz, emitter_type_ssize, \
  1255. altname)
  1256. #define OPT_WRITE_CHAR_P(name) \
  1257. OPT_WRITE(name, cpv, cpsz, emitter_type_string)
  1258. emitter_dict_begin(emitter, "opt", "Run-time option settings");
  1259. OPT_WRITE_BOOL("abort")
  1260. OPT_WRITE_BOOL("abort_conf")
  1261. OPT_WRITE_BOOL("cache_oblivious")
  1262. OPT_WRITE_BOOL("confirm_conf")
  1263. OPT_WRITE_BOOL("retain")
  1264. OPT_WRITE_CHAR_P("dss")
  1265. OPT_WRITE_UNSIGNED("narenas")
  1266. OPT_WRITE_CHAR_P("percpu_arena")
  1267. OPT_WRITE_SIZE_T("oversize_threshold")
  1268. OPT_WRITE_BOOL("hpa")
  1269. OPT_WRITE_SIZE_T("hpa_slab_max_alloc")
  1270. OPT_WRITE_SIZE_T("hpa_hugification_threshold")
  1271. OPT_WRITE_UINT64("hpa_hugify_delay_ms")
  1272. OPT_WRITE_UINT64("hpa_min_purge_interval_ms")
  1273. if (je_mallctl("opt.hpa_dirty_mult", (void *)&u32v, &u32sz, NULL, 0)
  1274. == 0) {
  1275. /*
  1276. * We cheat a little and "know" the secret meaning of this
  1277. * representation.
  1278. */
  1279. if (u32v == (uint32_t)-1) {
  1280. const char *neg1 = "-1";
  1281. emitter_kv(emitter, "hpa_dirty_mult",
  1282. "opt.hpa_dirty_mult", emitter_type_string, &neg1);
  1283. } else {
  1284. char buf[FXP_BUF_SIZE];
  1285. fxp_print(u32v, buf);
  1286. const char *bufp = buf;
  1287. emitter_kv(emitter, "hpa_dirty_mult",
  1288. "opt.hpa_dirty_mult", emitter_type_string, &bufp);
  1289. }
  1290. }
  1291. OPT_WRITE_SIZE_T("hpa_sec_nshards")
  1292. OPT_WRITE_SIZE_T("hpa_sec_max_alloc")
  1293. OPT_WRITE_SIZE_T("hpa_sec_max_bytes")
  1294. OPT_WRITE_SIZE_T("hpa_sec_bytes_after_flush")
  1295. OPT_WRITE_SIZE_T("hpa_sec_batch_fill_extra")
  1296. OPT_WRITE_CHAR_P("metadata_thp")
  1297. OPT_WRITE_INT64("mutex_max_spin")
  1298. OPT_WRITE_BOOL_MUTABLE("background_thread", "background_thread")
  1299. OPT_WRITE_SSIZE_T_MUTABLE("dirty_decay_ms", "arenas.dirty_decay_ms")
  1300. OPT_WRITE_SSIZE_T_MUTABLE("muzzy_decay_ms", "arenas.muzzy_decay_ms")
  1301. OPT_WRITE_SIZE_T("lg_extent_max_active_fit")
  1302. OPT_WRITE_CHAR_P("junk")
  1303. OPT_WRITE_BOOL("zero")
  1304. OPT_WRITE_BOOL("utrace")
  1305. OPT_WRITE_BOOL("xmalloc")
  1306. OPT_WRITE_BOOL("experimental_infallible_new")
  1307. OPT_WRITE_BOOL("tcache")
  1308. OPT_WRITE_SIZE_T("tcache_max")
  1309. OPT_WRITE_UNSIGNED("tcache_nslots_small_min")
  1310. OPT_WRITE_UNSIGNED("tcache_nslots_small_max")
  1311. OPT_WRITE_UNSIGNED("tcache_nslots_large")
  1312. OPT_WRITE_SSIZE_T("lg_tcache_nslots_mul")
  1313. OPT_WRITE_SIZE_T("tcache_gc_incr_bytes")
  1314. OPT_WRITE_SIZE_T("tcache_gc_delay_bytes")
  1315. OPT_WRITE_UNSIGNED("lg_tcache_flush_small_div")
  1316. OPT_WRITE_UNSIGNED("lg_tcache_flush_large_div")
  1317. OPT_WRITE_CHAR_P("thp")
  1318. OPT_WRITE_BOOL("prof")
  1319. OPT_WRITE_CHAR_P("prof_prefix")
  1320. OPT_WRITE_BOOL_MUTABLE("prof_active", "prof.active")
  1321. OPT_WRITE_BOOL_MUTABLE("prof_thread_active_init",
  1322. "prof.thread_active_init")
  1323. OPT_WRITE_SSIZE_T_MUTABLE("lg_prof_sample", "prof.lg_sample")
  1324. OPT_WRITE_BOOL("prof_accum")
  1325. OPT_WRITE_SSIZE_T("lg_prof_interval")
  1326. OPT_WRITE_BOOL("prof_gdump")
  1327. OPT_WRITE_BOOL("prof_final")
  1328. OPT_WRITE_BOOL("prof_leak")
  1329. OPT_WRITE_BOOL("prof_leak_error")
  1330. OPT_WRITE_BOOL("stats_print")
  1331. OPT_WRITE_CHAR_P("stats_print_opts")
  1332. OPT_WRITE_BOOL("stats_print")
  1333. OPT_WRITE_CHAR_P("stats_print_opts")
  1334. OPT_WRITE_INT64("stats_interval")
  1335. OPT_WRITE_CHAR_P("stats_interval_opts")
  1336. OPT_WRITE_CHAR_P("zero_realloc")
  1337. emitter_dict_end(emitter);
  1338. #undef OPT_WRITE
  1339. #undef OPT_WRITE_MUTABLE
  1340. #undef OPT_WRITE_BOOL
  1341. #undef OPT_WRITE_BOOL_MUTABLE
  1342. #undef OPT_WRITE_UNSIGNED
  1343. #undef OPT_WRITE_SSIZE_T
  1344. #undef OPT_WRITE_SSIZE_T_MUTABLE
  1345. #undef OPT_WRITE_CHAR_P
  1346. /* prof. */
  1347. if (config_prof) {
  1348. emitter_dict_begin(emitter, "prof", "Profiling settings");
  1349. CTL_GET("prof.thread_active_init", &bv, bool);
  1350. emitter_kv(emitter, "thread_active_init",
  1351. "prof.thread_active_init", emitter_type_bool, &bv);
  1352. CTL_GET("prof.active", &bv, bool);
  1353. emitter_kv(emitter, "active", "prof.active", emitter_type_bool,
  1354. &bv);
  1355. CTL_GET("prof.gdump", &bv, bool);
  1356. emitter_kv(emitter, "gdump", "prof.gdump", emitter_type_bool,
  1357. &bv);
  1358. CTL_GET("prof.interval", &u64v, uint64_t);
  1359. emitter_kv(emitter, "interval", "prof.interval",
  1360. emitter_type_uint64, &u64v);
  1361. CTL_GET("prof.lg_sample", &ssv, ssize_t);
  1362. emitter_kv(emitter, "lg_sample", "prof.lg_sample",
  1363. emitter_type_ssize, &ssv);
  1364. emitter_dict_end(emitter); /* Close "prof". */
  1365. }
  1366. /* arenas. */
  1367. /*
  1368. * The json output sticks arena info into an "arenas" dict; the table
  1369. * output puts them at the top-level.
  1370. */
  1371. emitter_json_object_kv_begin(emitter, "arenas");
  1372. CTL_GET("arenas.narenas", &uv, unsigned);
  1373. emitter_kv(emitter, "narenas", "Arenas", emitter_type_unsigned, &uv);
  1374. /*
  1375. * Decay settings are emitted only in json mode; in table mode, they're
  1376. * emitted as notes with the opt output, above.
  1377. */
  1378. CTL_GET("arenas.dirty_decay_ms", &ssv, ssize_t);
  1379. emitter_json_kv(emitter, "dirty_decay_ms", emitter_type_ssize, &ssv);
  1380. CTL_GET("arenas.muzzy_decay_ms", &ssv, ssize_t);
  1381. emitter_json_kv(emitter, "muzzy_decay_ms", emitter_type_ssize, &ssv);
  1382. CTL_GET("arenas.quantum", &sv, size_t);
  1383. emitter_kv(emitter, "quantum", "Quantum size", emitter_type_size, &sv);
  1384. CTL_GET("arenas.page", &sv, size_t);
  1385. emitter_kv(emitter, "page", "Page size", emitter_type_size, &sv);
  1386. if (je_mallctl("arenas.tcache_max", (void *)&sv, &ssz, NULL, 0) == 0) {
  1387. emitter_kv(emitter, "tcache_max",
  1388. "Maximum thread-cached size class", emitter_type_size, &sv);
  1389. }
  1390. unsigned arenas_nbins;
  1391. CTL_GET("arenas.nbins", &arenas_nbins, unsigned);
  1392. emitter_kv(emitter, "nbins", "Number of bin size classes",
  1393. emitter_type_unsigned, &arenas_nbins);
  1394. unsigned arenas_nhbins;
  1395. CTL_GET("arenas.nhbins", &arenas_nhbins, unsigned);
  1396. emitter_kv(emitter, "nhbins", "Number of thread-cache bin size classes",
  1397. emitter_type_unsigned, &arenas_nhbins);
  1398. /*
  1399. * We do enough mallctls in a loop that we actually want to omit them
  1400. * (not just omit the printing).
  1401. */
  1402. if (emitter_outputs_json(emitter)) {
  1403. emitter_json_array_kv_begin(emitter, "bin");
  1404. size_t arenas_bin_mib[CTL_MAX_DEPTH];
  1405. CTL_LEAF_PREPARE(arenas_bin_mib, 0, "arenas.bin");
  1406. for (unsigned i = 0; i < arenas_nbins; i++) {
  1407. arenas_bin_mib[2] = i;
  1408. emitter_json_object_begin(emitter);
  1409. CTL_LEAF(arenas_bin_mib, 3, "size", &sv, size_t);
  1410. emitter_json_kv(emitter, "size", emitter_type_size,
  1411. &sv);
  1412. CTL_LEAF(arenas_bin_mib, 3, "nregs", &u32v, uint32_t);
  1413. emitter_json_kv(emitter, "nregs", emitter_type_uint32,
  1414. &u32v);
  1415. CTL_LEAF(arenas_bin_mib, 3, "slab_size", &sv, size_t);
  1416. emitter_json_kv(emitter, "slab_size", emitter_type_size,
  1417. &sv);
  1418. CTL_LEAF(arenas_bin_mib, 3, "nshards", &u32v, uint32_t);
  1419. emitter_json_kv(emitter, "nshards", emitter_type_uint32,
  1420. &u32v);
  1421. emitter_json_object_end(emitter);
  1422. }
  1423. emitter_json_array_end(emitter); /* Close "bin". */
  1424. }
  1425. unsigned nlextents;
  1426. CTL_GET("arenas.nlextents", &nlextents, unsigned);
  1427. emitter_kv(emitter, "nlextents", "Number of large size classes",
  1428. emitter_type_unsigned, &nlextents);
  1429. if (emitter_outputs_json(emitter)) {
  1430. emitter_json_array_kv_begin(emitter, "lextent");
  1431. size_t arenas_lextent_mib[CTL_MAX_DEPTH];
  1432. CTL_LEAF_PREPARE(arenas_lextent_mib, 0, "arenas.lextent");
  1433. for (unsigned i = 0; i < nlextents; i++) {
  1434. arenas_lextent_mib[2] = i;
  1435. emitter_json_object_begin(emitter);
  1436. CTL_LEAF(arenas_lextent_mib, 3, "size", &sv, size_t);
  1437. emitter_json_kv(emitter, "size", emitter_type_size,
  1438. &sv);
  1439. emitter_json_object_end(emitter);
  1440. }
  1441. emitter_json_array_end(emitter); /* Close "lextent". */
  1442. }
  1443. emitter_json_object_end(emitter); /* Close "arenas" */
  1444. }
  1445. JEMALLOC_COLD
  1446. static void
  1447. stats_print_helper(emitter_t *emitter, bool merged, bool destroyed,
  1448. bool unmerged, bool bins, bool large, bool mutex, bool extents, bool hpa) {
  1449. /*
  1450. * These should be deleted. We keep them around for a while, to aid in
  1451. * the transition to the emitter code.
  1452. */
  1453. size_t allocated, active, metadata, metadata_thp, resident, mapped,
  1454. retained;
  1455. size_t num_background_threads;
  1456. size_t zero_reallocs;
  1457. uint64_t background_thread_num_runs, background_thread_run_interval;
  1458. CTL_GET("stats.allocated", &allocated, size_t);
  1459. CTL_GET("stats.active", &active, size_t);
  1460. CTL_GET("stats.metadata", &metadata, size_t);
  1461. CTL_GET("stats.metadata_thp", &metadata_thp, size_t);
  1462. CTL_GET("stats.resident", &resident, size_t);
  1463. CTL_GET("stats.mapped", &mapped, size_t);
  1464. CTL_GET("stats.retained", &retained, size_t);
  1465. CTL_GET("stats.zero_reallocs", &zero_reallocs, size_t);
  1466. if (have_background_thread) {
  1467. CTL_GET("stats.background_thread.num_threads",
  1468. &num_background_threads, size_t);
  1469. CTL_GET("stats.background_thread.num_runs",
  1470. &background_thread_num_runs, uint64_t);
  1471. CTL_GET("stats.background_thread.run_interval",
  1472. &background_thread_run_interval, uint64_t);
  1473. } else {
  1474. num_background_threads = 0;
  1475. background_thread_num_runs = 0;
  1476. background_thread_run_interval = 0;
  1477. }
  1478. /* Generic global stats. */
  1479. emitter_json_object_kv_begin(emitter, "stats");
  1480. emitter_json_kv(emitter, "allocated", emitter_type_size, &allocated);
  1481. emitter_json_kv(emitter, "active", emitter_type_size, &active);
  1482. emitter_json_kv(emitter, "metadata", emitter_type_size, &metadata);
  1483. emitter_json_kv(emitter, "metadata_thp", emitter_type_size,
  1484. &metadata_thp);
  1485. emitter_json_kv(emitter, "resident", emitter_type_size, &resident);
  1486. emitter_json_kv(emitter, "mapped", emitter_type_size, &mapped);
  1487. emitter_json_kv(emitter, "retained", emitter_type_size, &retained);
  1488. emitter_json_kv(emitter, "zero_reallocs", emitter_type_size,
  1489. &zero_reallocs);
  1490. emitter_table_printf(emitter, "Allocated: %zu, active: %zu, "
  1491. "metadata: %zu (n_thp %zu), resident: %zu, mapped: %zu, "
  1492. "retained: %zu\n", allocated, active, metadata, metadata_thp,
  1493. resident, mapped, retained);
  1494. /* Strange behaviors */
  1495. emitter_table_printf(emitter,
  1496. "Count of realloc(non-null-ptr, 0) calls: %zu\n", zero_reallocs);
  1497. /* Background thread stats. */
  1498. emitter_json_object_kv_begin(emitter, "background_thread");
  1499. emitter_json_kv(emitter, "num_threads", emitter_type_size,
  1500. &num_background_threads);
  1501. emitter_json_kv(emitter, "num_runs", emitter_type_uint64,
  1502. &background_thread_num_runs);
  1503. emitter_json_kv(emitter, "run_interval", emitter_type_uint64,
  1504. &background_thread_run_interval);
  1505. emitter_json_object_end(emitter); /* Close "background_thread". */
  1506. emitter_table_printf(emitter, "Background threads: %zu, "
  1507. "num_runs: %"FMTu64", run_interval: %"FMTu64" ns\n",
  1508. num_background_threads, background_thread_num_runs,
  1509. background_thread_run_interval);
  1510. if (mutex) {
  1511. emitter_row_t row;
  1512. emitter_col_t name;
  1513. emitter_col_t col64[mutex_prof_num_uint64_t_counters];
  1514. emitter_col_t col32[mutex_prof_num_uint32_t_counters];
  1515. uint64_t uptime;
  1516. emitter_row_init(&row);
  1517. mutex_stats_init_cols(&row, "", &name, col64, col32);
  1518. emitter_table_row(emitter, &row);
  1519. emitter_json_object_kv_begin(emitter, "mutexes");
  1520. CTL_M2_GET("stats.arenas.0.uptime", 0, &uptime, uint64_t);
  1521. size_t stats_mutexes_mib[CTL_MAX_DEPTH];
  1522. CTL_LEAF_PREPARE(stats_mutexes_mib, 0, "stats.mutexes");
  1523. for (int i = 0; i < mutex_prof_num_global_mutexes; i++) {
  1524. mutex_stats_read_global(stats_mutexes_mib, 2,
  1525. global_mutex_names[i], &name, col64, col32, uptime);
  1526. emitter_json_object_kv_begin(emitter, global_mutex_names[i]);
  1527. mutex_stats_emit(emitter, &row, col64, col32);
  1528. emitter_json_object_end(emitter);
  1529. }
  1530. emitter_json_object_end(emitter); /* Close "mutexes". */
  1531. }
  1532. emitter_json_object_end(emitter); /* Close "stats". */
  1533. if (merged || destroyed || unmerged) {
  1534. unsigned narenas;
  1535. emitter_json_object_kv_begin(emitter, "stats.arenas");
  1536. CTL_GET("arenas.narenas", &narenas, unsigned);
  1537. size_t mib[3];
  1538. size_t miblen = sizeof(mib) / sizeof(size_t);
  1539. size_t sz;
  1540. VARIABLE_ARRAY(bool, initialized, narenas);
  1541. bool destroyed_initialized;
  1542. unsigned i, j, ninitialized;
  1543. xmallctlnametomib("arena.0.initialized", mib, &miblen);
  1544. for (i = ninitialized = 0; i < narenas; i++) {
  1545. mib[1] = i;
  1546. sz = sizeof(bool);
  1547. xmallctlbymib(mib, miblen, &initialized[i], &sz,
  1548. NULL, 0);
  1549. if (initialized[i]) {
  1550. ninitialized++;
  1551. }
  1552. }
  1553. mib[1] = MALLCTL_ARENAS_DESTROYED;
  1554. sz = sizeof(bool);
  1555. xmallctlbymib(mib, miblen, &destroyed_initialized, &sz,
  1556. NULL, 0);
  1557. /* Merged stats. */
  1558. if (merged && (ninitialized > 1 || !unmerged)) {
  1559. /* Print merged arena stats. */
  1560. emitter_table_printf(emitter, "Merged arenas stats:\n");
  1561. emitter_json_object_kv_begin(emitter, "merged");
  1562. stats_arena_print(emitter, MALLCTL_ARENAS_ALL, bins,
  1563. large, mutex, extents, hpa);
  1564. emitter_json_object_end(emitter); /* Close "merged". */
  1565. }
  1566. /* Destroyed stats. */
  1567. if (destroyed_initialized && destroyed) {
  1568. /* Print destroyed arena stats. */
  1569. emitter_table_printf(emitter,
  1570. "Destroyed arenas stats:\n");
  1571. emitter_json_object_kv_begin(emitter, "destroyed");
  1572. stats_arena_print(emitter, MALLCTL_ARENAS_DESTROYED,
  1573. bins, large, mutex, extents, hpa);
  1574. emitter_json_object_end(emitter); /* Close "destroyed". */
  1575. }
  1576. /* Unmerged stats. */
  1577. if (unmerged) {
  1578. for (i = j = 0; i < narenas; i++) {
  1579. if (initialized[i]) {
  1580. char arena_ind_str[20];
  1581. malloc_snprintf(arena_ind_str,
  1582. sizeof(arena_ind_str), "%u", i);
  1583. emitter_json_object_kv_begin(emitter,
  1584. arena_ind_str);
  1585. emitter_table_printf(emitter,
  1586. "arenas[%s]:\n", arena_ind_str);
  1587. stats_arena_print(emitter, i, bins,
  1588. large, mutex, extents, hpa);
  1589. /* Close "<arena-ind>". */
  1590. emitter_json_object_end(emitter);
  1591. }
  1592. }
  1593. }
  1594. emitter_json_object_end(emitter); /* Close "stats.arenas". */
  1595. }
  1596. }
  1597. void
  1598. stats_print(write_cb_t *write_cb, void *cbopaque, const char *opts) {
  1599. int err;
  1600. uint64_t epoch;
  1601. size_t u64sz;
  1602. #define OPTION(o, v, d, s) bool v = d;
  1603. STATS_PRINT_OPTIONS
  1604. #undef OPTION
  1605. /*
  1606. * Refresh stats, in case mallctl() was called by the application.
  1607. *
  1608. * Check for OOM here, since refreshing the ctl cache can trigger
  1609. * allocation. In practice, none of the subsequent mallctl()-related
  1610. * calls in this function will cause OOM if this one succeeds.
  1611. * */
  1612. epoch = 1;
  1613. u64sz = sizeof(uint64_t);
  1614. err = je_mallctl("epoch", (void *)&epoch, &u64sz, (void *)&epoch,
  1615. sizeof(uint64_t));
  1616. if (err != 0) {
  1617. if (err == EAGAIN) {
  1618. malloc_write("<jemalloc>: Memory allocation failure in "
  1619. "mallctl(\"epoch\", ...)\n");
  1620. return;
  1621. }
  1622. malloc_write("<jemalloc>: Failure in mallctl(\"epoch\", "
  1623. "...)\n");
  1624. abort();
  1625. }
  1626. if (opts != NULL) {
  1627. for (unsigned i = 0; opts[i] != '\0'; i++) {
  1628. switch (opts[i]) {
  1629. #define OPTION(o, v, d, s) case o: v = s; break;
  1630. STATS_PRINT_OPTIONS
  1631. #undef OPTION
  1632. default:;
  1633. }
  1634. }
  1635. }
  1636. emitter_t emitter;
  1637. emitter_init(&emitter,
  1638. json ? emitter_output_json_compact : emitter_output_table,
  1639. write_cb, cbopaque);
  1640. emitter_begin(&emitter);
  1641. emitter_table_printf(&emitter, "___ Begin jemalloc statistics ___\n");
  1642. emitter_json_object_kv_begin(&emitter, "jemalloc");
  1643. if (general) {
  1644. stats_general_print(&emitter);
  1645. }
  1646. if (config_stats) {
  1647. stats_print_helper(&emitter, merged, destroyed, unmerged,
  1648. bins, large, mutex, extents, hpa);
  1649. }
  1650. emitter_json_object_end(&emitter); /* Closes the "jemalloc" dict. */
  1651. emitter_table_printf(&emitter, "--- End jemalloc statistics ---\n");
  1652. emitter_end(&emitter);
  1653. }
  1654. uint64_t
  1655. stats_interval_new_event_wait(tsd_t *tsd) {
  1656. return stats_interval_accum_batch;
  1657. }
  1658. uint64_t
  1659. stats_interval_postponed_event_wait(tsd_t *tsd) {
  1660. return TE_MIN_START_WAIT;
  1661. }
  1662. void
  1663. stats_interval_event_handler(tsd_t *tsd, uint64_t elapsed) {
  1664. assert(elapsed > 0 && elapsed != TE_INVALID_ELAPSED);
  1665. if (counter_accum(tsd_tsdn(tsd), &stats_interval_accumulated,
  1666. elapsed)) {
  1667. je_malloc_stats_print(NULL, NULL, opt_stats_interval_opts);
  1668. }
  1669. }
  1670. bool
  1671. stats_boot(void) {
  1672. uint64_t stats_interval;
  1673. if (opt_stats_interval < 0) {
  1674. assert(opt_stats_interval == -1);
  1675. stats_interval = 0;
  1676. stats_interval_accum_batch = 0;
  1677. } else{
  1678. /* See comments in stats.h */
  1679. stats_interval = (opt_stats_interval > 0) ?
  1680. opt_stats_interval : 1;
  1681. uint64_t batch = stats_interval >>
  1682. STATS_INTERVAL_ACCUM_LG_BATCH_SIZE;
  1683. if (batch > STATS_INTERVAL_ACCUM_BATCH_MAX) {
  1684. batch = STATS_INTERVAL_ACCUM_BATCH_MAX;
  1685. } else if (batch == 0) {
  1686. batch = 1;
  1687. }
  1688. stats_interval_accum_batch = batch;
  1689. }
  1690. return counter_accum_init(&stats_interval_accumulated, stats_interval);
  1691. }
  1692. void
  1693. stats_prefork(tsdn_t *tsdn) {
  1694. counter_prefork(tsdn, &stats_interval_accumulated);
  1695. }
  1696. void
  1697. stats_postfork_parent(tsdn_t *tsdn) {
  1698. counter_postfork_parent(tsdn, &stats_interval_accumulated);
  1699. }
  1700. void
  1701. stats_postfork_child(tsdn_t *tsdn) {
  1702. counter_postfork_child(tsdn, &stats_interval_accumulated);
  1703. }