blob: babdbd6212593cbb3dae8e9dc96d929535abd0ce [file] [log] [blame]
Jason Evans6109fe02010-02-10 10:37:56 -08001#define JEMALLOC_PROF_C_
Jason Evans376b1522010-02-11 14:45:59 -08002#include "jemalloc/internal/jemalloc_internal.h"
Jason Evans6109fe02010-02-10 10:37:56 -08003/******************************************************************************/
4
5#ifdef JEMALLOC_PROF_LIBUNWIND
6#define UNW_LOCAL_ONLY
7#include <libunwind.h>
8#endif
9
Jason Evans77f350b2011-03-15 22:23:12 -070010#ifdef JEMALLOC_PROF_LIBGCC
11#include <unwind.h>
12#endif
13
Jason Evans6109fe02010-02-10 10:37:56 -080014/******************************************************************************/
15/* Data. */
16
17bool opt_prof = false;
Jason Evansf18c9822010-03-31 18:43:24 -070018bool opt_prof_active = true;
Jason Evansfc12c0b2014-10-03 23:25:30 -070019bool opt_prof_thread_active_init = true;
Jason Evansb9477e72010-03-01 20:15:26 -080020size_t opt_lg_prof_sample = LG_PROF_SAMPLE_DEFAULT;
Jason Evansa02fc082010-03-31 17:35:51 -070021ssize_t opt_lg_prof_interval = LG_PROF_INTERVAL_DEFAULT;
Jason Evanse7339702010-10-23 18:37:06 -070022bool opt_prof_gdump = false;
Jason Evans57efa7b2014-10-08 17:57:19 -070023bool opt_prof_final = false;
Jason Evans6109fe02010-02-10 10:37:56 -080024bool opt_prof_leak = false;
Jason Evans0b25fe72012-04-17 16:39:33 -070025bool opt_prof_accum = false;
Jason Evans4f37ef62014-01-16 13:23:56 -080026char opt_prof_prefix[
27 /* Minimize memory bloat for non-prof builds. */
28#ifdef JEMALLOC_PROF
29 PATH_MAX +
30#endif
Jason Evanseefdd022014-01-16 18:04:30 -080031 1];
Jason Evans6109fe02010-02-10 10:37:56 -080032
Jason Evansfc12c0b2014-10-03 23:25:30 -070033/*
34 * Initialized as opt_prof_active, and accessed via
35 * prof_active_[gs]et{_unlocked,}().
36 */
37bool prof_active;
38static malloc_mutex_t prof_active_mtx;
39
40/*
41 * Initialized as opt_prof_thread_active_init, and accessed via
42 * prof_thread_active_init_[gs]et().
43 */
44static bool prof_thread_active_init;
45static malloc_mutex_t prof_thread_active_init_mtx;
46
Jason Evans5b8ed5b2015-01-25 21:16:57 -080047/*
48 * Initialized as opt_prof_gdump, and accessed via
49 * prof_gdump_[gs]et{_unlocked,}().
50 */
51bool prof_gdump_val;
52static malloc_mutex_t prof_gdump_mtx;
53
Jason Evansa3b33862012-11-13 12:56:27 -080054uint64_t prof_interval = 0;
Jason Evansd34f9e72010-02-11 13:19:21 -080055
Jason Evans602c8e02014-08-18 16:22:13 -070056size_t lg_prof_sample;
57
Jason Evans6109fe02010-02-10 10:37:56 -080058/*
Jason Evans602c8e02014-08-18 16:22:13 -070059 * Table of mutexes that are shared among gctx's. These are leaf locks, so
60 * there is no problem with using them for more than one gctx at the same time.
61 * The primary motivation for this sharing though is that gctx's are ephemeral,
Jason Evans6da54182012-03-23 18:05:51 -070062 * and destroying mutexes causes complications for systems that allocate when
63 * creating/destroying mutexes.
64 */
Jason Evans602c8e02014-08-18 16:22:13 -070065static malloc_mutex_t *gctx_locks;
66static unsigned cum_gctxs; /* Atomic counter. */
Jason Evans6da54182012-03-23 18:05:51 -070067
68/*
Jason Evans602c8e02014-08-18 16:22:13 -070069 * Table of mutexes that are shared among tdata's. No operations require
70 * holding multiple tdata locks, so there is no problem with using them for more
71 * than one tdata at the same time, even though a gctx lock may be acquired
72 * while holding a tdata lock.
73 */
74static malloc_mutex_t *tdata_locks;
75
76/*
77 * Global hash of (prof_bt_t *)-->(prof_gctx_t *). This is the master data
Jason Evansa881cd22010-10-02 15:18:50 -070078 * structure that knows about all backtraces currently captured.
Jason Evans6109fe02010-02-10 10:37:56 -080079 */
Jason Evans602c8e02014-08-18 16:22:13 -070080static ckh_t bt2gctx;
81static malloc_mutex_t bt2gctx_mtx;
82
83/*
84 * Tree of all extant prof_tdata_t structures, regardless of state,
85 * {attached,detached,expired}.
86 */
87static prof_tdata_tree_t tdatas;
88static malloc_mutex_t tdatas_mtx;
89
90static uint64_t next_thr_uid;
Jason Evans9d8f3d22014-09-11 18:06:30 -070091static malloc_mutex_t next_thr_uid_mtx;
Jason Evans6109fe02010-02-10 10:37:56 -080092
Jason Evans6109fe02010-02-10 10:37:56 -080093static malloc_mutex_t prof_dump_seq_mtx;
94static uint64_t prof_dump_seq;
95static uint64_t prof_dump_iseq;
96static uint64_t prof_dump_mseq;
97static uint64_t prof_dump_useq;
98
99/*
100 * This buffer is rather large for stack allocation, so use a single buffer for
Jason Evans4f37ef62014-01-16 13:23:56 -0800101 * all profile dumps.
Jason Evans6109fe02010-02-10 10:37:56 -0800102 */
Jason Evans4f37ef62014-01-16 13:23:56 -0800103static malloc_mutex_t prof_dump_mtx;
104static char prof_dump_buf[
105 /* Minimize memory bloat for non-prof builds. */
106#ifdef JEMALLOC_PROF
107 PROF_DUMP_BUFSIZE
108#else
109 1
110#endif
111];
Jason Evans6109fe02010-02-10 10:37:56 -0800112static unsigned prof_dump_buf_end;
113static int prof_dump_fd;
114
115/* Do not dump any profiles until bootstrapping is complete. */
116static bool prof_booted = false;
117
Jason Evans6109fe02010-02-10 10:37:56 -0800118/******************************************************************************/
Jason Evans602c8e02014-08-18 16:22:13 -0700119/*
120 * Function prototypes for static functions that are referenced prior to
121 * definition.
122 */
123
124static bool prof_tctx_should_destroy(prof_tctx_t *tctx);
Jason Evans5460aa62014-09-22 21:09:23 -0700125static void prof_tctx_destroy(tsd_t *tsd, prof_tctx_t *tctx);
Jason Evansf04a0be2014-10-04 15:03:49 -0700126static bool prof_tdata_should_destroy(prof_tdata_t *tdata,
127 bool even_if_attached);
128static void prof_tdata_destroy(tsd_t *tsd, prof_tdata_t *tdata,
129 bool even_if_attached);
Jason Evansfc12c0b2014-10-03 23:25:30 -0700130static char *prof_thread_name_alloc(tsd_t *tsd, const char *thread_name);
Jason Evans602c8e02014-08-18 16:22:13 -0700131
132/******************************************************************************/
133/* Red-black trees. */
Jason Evans6109fe02010-02-10 10:37:56 -0800134
Jason Evans3a81cbd2014-08-16 12:58:55 -0700135JEMALLOC_INLINE_C int
Jason Evans602c8e02014-08-18 16:22:13 -0700136prof_tctx_comp(const prof_tctx_t *a, const prof_tctx_t *b)
Jason Evans3a81cbd2014-08-16 12:58:55 -0700137{
Jason Evans04211e22015-03-16 15:11:06 -0700138 uint64_t a_thr_uid = a->thr_uid;
139 uint64_t b_thr_uid = b->thr_uid;
140 int ret = (a_thr_uid > b_thr_uid) - (a_thr_uid < b_thr_uid);
Jason Evansd69964b2015-03-12 16:25:18 -0700141 if (ret == 0) {
Jason Evans04211e22015-03-16 15:11:06 -0700142 uint64_t a_tctx_uid = a->tctx_uid;
143 uint64_t b_tctx_uid = b->tctx_uid;
144 ret = (a_tctx_uid > b_tctx_uid) - (a_tctx_uid < b_tctx_uid);
Jason Evansd69964b2015-03-12 16:25:18 -0700145 }
146 return (ret);
Jason Evans3a81cbd2014-08-16 12:58:55 -0700147}
148
Jason Evans602c8e02014-08-18 16:22:13 -0700149rb_gen(static UNUSED, tctx_tree_, prof_tctx_tree_t, prof_tctx_t,
150 tctx_link, prof_tctx_comp)
Jason Evans3a81cbd2014-08-16 12:58:55 -0700151
152JEMALLOC_INLINE_C int
Jason Evans602c8e02014-08-18 16:22:13 -0700153prof_gctx_comp(const prof_gctx_t *a, const prof_gctx_t *b)
Jason Evans3a81cbd2014-08-16 12:58:55 -0700154{
155 unsigned a_len = a->bt.len;
156 unsigned b_len = b->bt.len;
157 unsigned comp_len = (a_len < b_len) ? a_len : b_len;
158 int ret = memcmp(a->bt.vec, b->bt.vec, comp_len * sizeof(void *));
159 if (ret == 0)
160 ret = (a_len > b_len) - (a_len < b_len);
161 return (ret);
162}
163
Jason Evans602c8e02014-08-18 16:22:13 -0700164rb_gen(static UNUSED, gctx_tree_, prof_gctx_tree_t, prof_gctx_t, dump_link,
165 prof_gctx_comp)
166
167JEMALLOC_INLINE_C int
168prof_tdata_comp(const prof_tdata_t *a, const prof_tdata_t *b)
169{
Jason Evans20c31de2014-10-02 23:01:10 -0700170 int ret;
Jason Evans602c8e02014-08-18 16:22:13 -0700171 uint64_t a_uid = a->thr_uid;
172 uint64_t b_uid = b->thr_uid;
173
Jason Evans20c31de2014-10-02 23:01:10 -0700174 ret = ((a_uid > b_uid) - (a_uid < b_uid));
175 if (ret == 0) {
176 uint64_t a_discrim = a->thr_discrim;
177 uint64_t b_discrim = b->thr_discrim;
178
179 ret = ((a_discrim > b_discrim) - (a_discrim < b_discrim));
180 }
181 return (ret);
Jason Evans602c8e02014-08-18 16:22:13 -0700182}
183
184rb_gen(static UNUSED, tdata_tree_, prof_tdata_tree_t, prof_tdata_t, tdata_link,
185 prof_tdata_comp)
186
187/******************************************************************************/
188
189void
Jason Evans5460aa62014-09-22 21:09:23 -0700190prof_alloc_rollback(tsd_t *tsd, prof_tctx_t *tctx, bool updated)
Jason Evans6e73dc12014-09-09 19:37:26 -0700191{
192 prof_tdata_t *tdata;
193
194 cassert(config_prof);
195
196 if (updated) {
197 /*
198 * Compute a new sample threshold. This isn't very important in
199 * practice, because this function is rarely executed, so the
200 * potential for sample bias is minimal except in contrived
201 * programs.
202 */
Jason Evans5460aa62014-09-22 21:09:23 -0700203 tdata = prof_tdata_get(tsd, true);
204 if (tdata != NULL)
Jason Evans6e73dc12014-09-09 19:37:26 -0700205 prof_sample_threshold_update(tctx->tdata);
206 }
207
208 if ((uintptr_t)tctx > (uintptr_t)1U) {
209 malloc_mutex_lock(tctx->tdata->lock);
210 tctx->prepared = false;
211 if (prof_tctx_should_destroy(tctx))
Jason Evans5460aa62014-09-22 21:09:23 -0700212 prof_tctx_destroy(tsd, tctx);
Jason Evans6e73dc12014-09-09 19:37:26 -0700213 else
214 malloc_mutex_unlock(tctx->tdata->lock);
215 }
216}
217
218void
Jason Evanscfc57062014-10-30 23:18:45 -0700219prof_malloc_sample_object(const void *ptr, size_t usize, prof_tctx_t *tctx)
220{
221
Jason Evans602c8e02014-08-18 16:22:13 -0700222 prof_tctx_set(ptr, tctx);
223
224 malloc_mutex_lock(tctx->tdata->lock);
225 tctx->cnts.curobjs++;
226 tctx->cnts.curbytes += usize;
227 if (opt_prof_accum) {
228 tctx->cnts.accumobjs++;
229 tctx->cnts.accumbytes += usize;
230 }
Jason Evans6e73dc12014-09-09 19:37:26 -0700231 tctx->prepared = false;
Jason Evans602c8e02014-08-18 16:22:13 -0700232 malloc_mutex_unlock(tctx->tdata->lock);
233}
234
235void
Jason Evans5460aa62014-09-22 21:09:23 -0700236prof_free_sampled_object(tsd_t *tsd, size_t usize, prof_tctx_t *tctx)
Jason Evans602c8e02014-08-18 16:22:13 -0700237{
238
239 malloc_mutex_lock(tctx->tdata->lock);
240 assert(tctx->cnts.curobjs > 0);
241 assert(tctx->cnts.curbytes >= usize);
242 tctx->cnts.curobjs--;
243 tctx->cnts.curbytes -= usize;
244
245 if (prof_tctx_should_destroy(tctx))
Jason Evans5460aa62014-09-22 21:09:23 -0700246 prof_tctx_destroy(tsd, tctx);
Jason Evans602c8e02014-08-18 16:22:13 -0700247 else
248 malloc_mutex_unlock(tctx->tdata->lock);
249}
Jason Evans3a81cbd2014-08-16 12:58:55 -0700250
Jason Evans4d6a1342010-10-20 19:05:59 -0700251void
Jason Evans6109fe02010-02-10 10:37:56 -0800252bt_init(prof_bt_t *bt, void **vec)
253{
254
Jason Evans7372b152012-02-10 20:22:09 -0800255 cassert(config_prof);
256
Jason Evans6109fe02010-02-10 10:37:56 -0800257 bt->vec = vec;
258 bt->len = 0;
259}
260
Jason Evansaf1f5922014-10-30 16:38:08 -0700261JEMALLOC_INLINE_C void
Jason Evansc93ed812014-10-30 16:50:33 -0700262prof_enter(tsd_t *tsd, prof_tdata_t *tdata)
Jason Evans6109fe02010-02-10 10:37:56 -0800263{
264
Jason Evans7372b152012-02-10 20:22:09 -0800265 cassert(config_prof);
Jason Evansc93ed812014-10-30 16:50:33 -0700266 assert(tdata == prof_tdata_get(tsd, false));
Jason Evans7372b152012-02-10 20:22:09 -0800267
Jason Evans82cb6032014-11-01 00:20:28 -0700268 if (tdata != NULL) {
269 assert(!tdata->enq);
270 tdata->enq = true;
271 }
Jason Evans6109fe02010-02-10 10:37:56 -0800272
Jason Evans602c8e02014-08-18 16:22:13 -0700273 malloc_mutex_lock(&bt2gctx_mtx);
Jason Evans6109fe02010-02-10 10:37:56 -0800274}
275
Jason Evansaf1f5922014-10-30 16:38:08 -0700276JEMALLOC_INLINE_C void
Jason Evansc93ed812014-10-30 16:50:33 -0700277prof_leave(tsd_t *tsd, prof_tdata_t *tdata)
Jason Evans6109fe02010-02-10 10:37:56 -0800278{
Jason Evans6109fe02010-02-10 10:37:56 -0800279
Jason Evans7372b152012-02-10 20:22:09 -0800280 cassert(config_prof);
Jason Evansc93ed812014-10-30 16:50:33 -0700281 assert(tdata == prof_tdata_get(tsd, false));
Jason Evans7372b152012-02-10 20:22:09 -0800282
Jason Evans602c8e02014-08-18 16:22:13 -0700283 malloc_mutex_unlock(&bt2gctx_mtx);
Jason Evans6109fe02010-02-10 10:37:56 -0800284
Jason Evans82cb6032014-11-01 00:20:28 -0700285 if (tdata != NULL) {
286 bool idump, gdump;
Jason Evans6109fe02010-02-10 10:37:56 -0800287
Jason Evans82cb6032014-11-01 00:20:28 -0700288 assert(tdata->enq);
289 tdata->enq = false;
290 idump = tdata->enq_idump;
291 tdata->enq_idump = false;
292 gdump = tdata->enq_gdump;
293 tdata->enq_gdump = false;
294
295 if (idump)
296 prof_idump();
297 if (gdump)
298 prof_gdump();
299 }
Jason Evans6109fe02010-02-10 10:37:56 -0800300}
301
Jason Evans77f350b2011-03-15 22:23:12 -0700302#ifdef JEMALLOC_PROF_LIBUNWIND
Jason Evans4d6a1342010-10-20 19:05:59 -0700303void
Jason Evans6f001052014-04-22 18:41:15 -0700304prof_backtrace(prof_bt_t *bt)
Jason Evans6109fe02010-02-10 10:37:56 -0800305{
Jason Evans6f001052014-04-22 18:41:15 -0700306 int nframes;
307
Jason Evans7372b152012-02-10 20:22:09 -0800308 cassert(config_prof);
Jason Evans6109fe02010-02-10 10:37:56 -0800309 assert(bt->len == 0);
310 assert(bt->vec != NULL);
Jason Evans6109fe02010-02-10 10:37:56 -0800311
Jason Evans6f001052014-04-22 18:41:15 -0700312 nframes = unw_backtrace(bt->vec, PROF_BT_MAX);
313 if (nframes <= 0)
Lucian Adrian Grijincu9d4e13f2014-04-21 20:52:35 -0700314 return;
Jason Evans6f001052014-04-22 18:41:15 -0700315 bt->len = nframes;
Jason Evans6109fe02010-02-10 10:37:56 -0800316}
Jason Evans7372b152012-02-10 20:22:09 -0800317#elif (defined(JEMALLOC_PROF_LIBGCC))
Jason Evans77f350b2011-03-15 22:23:12 -0700318static _Unwind_Reason_Code
319prof_unwind_init_callback(struct _Unwind_Context *context, void *arg)
320{
321
Jason Evans7372b152012-02-10 20:22:09 -0800322 cassert(config_prof);
323
Jason Evans77f350b2011-03-15 22:23:12 -0700324 return (_URC_NO_REASON);
325}
326
327static _Unwind_Reason_Code
328prof_unwind_callback(struct _Unwind_Context *context, void *arg)
329{
330 prof_unwind_data_t *data = (prof_unwind_data_t *)arg;
Jason Evans6f001052014-04-22 18:41:15 -0700331 void *ip;
Jason Evans77f350b2011-03-15 22:23:12 -0700332
Jason Evans7372b152012-02-10 20:22:09 -0800333 cassert(config_prof);
334
Jason Evans6f001052014-04-22 18:41:15 -0700335 ip = (void *)_Unwind_GetIP(context);
336 if (ip == NULL)
337 return (_URC_END_OF_STACK);
338 data->bt->vec[data->bt->len] = ip;
339 data->bt->len++;
340 if (data->bt->len == data->max)
341 return (_URC_END_OF_STACK);
Jason Evans77f350b2011-03-15 22:23:12 -0700342
343 return (_URC_NO_REASON);
344}
345
346void
Jason Evans6f001052014-04-22 18:41:15 -0700347prof_backtrace(prof_bt_t *bt)
Jason Evans77f350b2011-03-15 22:23:12 -0700348{
Jason Evans6f001052014-04-22 18:41:15 -0700349 prof_unwind_data_t data = {bt, PROF_BT_MAX};
Jason Evans77f350b2011-03-15 22:23:12 -0700350
Jason Evans7372b152012-02-10 20:22:09 -0800351 cassert(config_prof);
352
Jason Evans77f350b2011-03-15 22:23:12 -0700353 _Unwind_Backtrace(prof_unwind_callback, &data);
354}
Jason Evans7372b152012-02-10 20:22:09 -0800355#elif (defined(JEMALLOC_PROF_GCC))
Jason Evans4d6a1342010-10-20 19:05:59 -0700356void
Jason Evans6f001052014-04-22 18:41:15 -0700357prof_backtrace(prof_bt_t *bt)
Jason Evans6109fe02010-02-10 10:37:56 -0800358{
Jason Evans6109fe02010-02-10 10:37:56 -0800359#define BT_FRAME(i) \
Jason Evans6f001052014-04-22 18:41:15 -0700360 if ((i) < PROF_BT_MAX) { \
Jason Evans6109fe02010-02-10 10:37:56 -0800361 void *p; \
362 if (__builtin_frame_address(i) == 0) \
Jason Evansb27805b2010-02-10 18:15:53 -0800363 return; \
Jason Evans6109fe02010-02-10 10:37:56 -0800364 p = __builtin_return_address(i); \
365 if (p == NULL) \
Jason Evansb27805b2010-02-10 18:15:53 -0800366 return; \
Jason Evans6f001052014-04-22 18:41:15 -0700367 bt->vec[(i)] = p; \
368 bt->len = (i) + 1; \
Jason Evans6109fe02010-02-10 10:37:56 -0800369 } else \
Jason Evansb27805b2010-02-10 18:15:53 -0800370 return;
Jason Evans6109fe02010-02-10 10:37:56 -0800371
Jason Evans7372b152012-02-10 20:22:09 -0800372 cassert(config_prof);
Jason Evans6109fe02010-02-10 10:37:56 -0800373
Jason Evans6109fe02010-02-10 10:37:56 -0800374 BT_FRAME(0)
375 BT_FRAME(1)
376 BT_FRAME(2)
Jason Evans6109fe02010-02-10 10:37:56 -0800377 BT_FRAME(3)
378 BT_FRAME(4)
379 BT_FRAME(5)
380 BT_FRAME(6)
381 BT_FRAME(7)
382 BT_FRAME(8)
383 BT_FRAME(9)
384
385 BT_FRAME(10)
386 BT_FRAME(11)
387 BT_FRAME(12)
388 BT_FRAME(13)
389 BT_FRAME(14)
390 BT_FRAME(15)
391 BT_FRAME(16)
392 BT_FRAME(17)
393 BT_FRAME(18)
394 BT_FRAME(19)
395
396 BT_FRAME(20)
397 BT_FRAME(21)
398 BT_FRAME(22)
399 BT_FRAME(23)
400 BT_FRAME(24)
401 BT_FRAME(25)
402 BT_FRAME(26)
403 BT_FRAME(27)
404 BT_FRAME(28)
405 BT_FRAME(29)
406
407 BT_FRAME(30)
408 BT_FRAME(31)
409 BT_FRAME(32)
410 BT_FRAME(33)
411 BT_FRAME(34)
412 BT_FRAME(35)
413 BT_FRAME(36)
414 BT_FRAME(37)
415 BT_FRAME(38)
416 BT_FRAME(39)
417
418 BT_FRAME(40)
419 BT_FRAME(41)
420 BT_FRAME(42)
421 BT_FRAME(43)
422 BT_FRAME(44)
423 BT_FRAME(45)
424 BT_FRAME(46)
425 BT_FRAME(47)
426 BT_FRAME(48)
427 BT_FRAME(49)
428
429 BT_FRAME(50)
430 BT_FRAME(51)
431 BT_FRAME(52)
432 BT_FRAME(53)
433 BT_FRAME(54)
434 BT_FRAME(55)
435 BT_FRAME(56)
436 BT_FRAME(57)
437 BT_FRAME(58)
438 BT_FRAME(59)
439
440 BT_FRAME(60)
441 BT_FRAME(61)
442 BT_FRAME(62)
443 BT_FRAME(63)
444 BT_FRAME(64)
445 BT_FRAME(65)
446 BT_FRAME(66)
447 BT_FRAME(67)
448 BT_FRAME(68)
449 BT_FRAME(69)
450
451 BT_FRAME(70)
452 BT_FRAME(71)
453 BT_FRAME(72)
454 BT_FRAME(73)
455 BT_FRAME(74)
456 BT_FRAME(75)
457 BT_FRAME(76)
458 BT_FRAME(77)
459 BT_FRAME(78)
460 BT_FRAME(79)
461
462 BT_FRAME(80)
463 BT_FRAME(81)
464 BT_FRAME(82)
465 BT_FRAME(83)
466 BT_FRAME(84)
467 BT_FRAME(85)
468 BT_FRAME(86)
469 BT_FRAME(87)
470 BT_FRAME(88)
471 BT_FRAME(89)
472
473 BT_FRAME(90)
474 BT_FRAME(91)
475 BT_FRAME(92)
476 BT_FRAME(93)
477 BT_FRAME(94)
478 BT_FRAME(95)
479 BT_FRAME(96)
480 BT_FRAME(97)
481 BT_FRAME(98)
482 BT_FRAME(99)
483
484 BT_FRAME(100)
485 BT_FRAME(101)
486 BT_FRAME(102)
487 BT_FRAME(103)
488 BT_FRAME(104)
489 BT_FRAME(105)
490 BT_FRAME(106)
491 BT_FRAME(107)
492 BT_FRAME(108)
493 BT_FRAME(109)
494
495 BT_FRAME(110)
496 BT_FRAME(111)
497 BT_FRAME(112)
498 BT_FRAME(113)
499 BT_FRAME(114)
500 BT_FRAME(115)
501 BT_FRAME(116)
502 BT_FRAME(117)
503 BT_FRAME(118)
504 BT_FRAME(119)
505
506 BT_FRAME(120)
507 BT_FRAME(121)
508 BT_FRAME(122)
509 BT_FRAME(123)
510 BT_FRAME(124)
511 BT_FRAME(125)
512 BT_FRAME(126)
513 BT_FRAME(127)
Jason Evans6109fe02010-02-10 10:37:56 -0800514#undef BT_FRAME
Jason Evans6109fe02010-02-10 10:37:56 -0800515}
Jason Evans7372b152012-02-10 20:22:09 -0800516#else
517void
Jason Evans6f001052014-04-22 18:41:15 -0700518prof_backtrace(prof_bt_t *bt)
Jason Evans7372b152012-02-10 20:22:09 -0800519{
520
521 cassert(config_prof);
Jason Evans6556e282013-10-21 14:56:27 -0700522 not_reached();
Jason Evans7372b152012-02-10 20:22:09 -0800523}
Jason Evans6109fe02010-02-10 10:37:56 -0800524#endif
525
Jason Evans4f37ef62014-01-16 13:23:56 -0800526static malloc_mutex_t *
Jason Evans602c8e02014-08-18 16:22:13 -0700527prof_gctx_mutex_choose(void)
Jason Evans4f37ef62014-01-16 13:23:56 -0800528{
Jason Evans602c8e02014-08-18 16:22:13 -0700529 unsigned ngctxs = atomic_add_u(&cum_gctxs, 1);
Jason Evans4f37ef62014-01-16 13:23:56 -0800530
Jason Evans602c8e02014-08-18 16:22:13 -0700531 return (&gctx_locks[(ngctxs - 1) % PROF_NCTX_LOCKS]);
Jason Evans4f37ef62014-01-16 13:23:56 -0800532}
533
Jason Evans602c8e02014-08-18 16:22:13 -0700534static malloc_mutex_t *
535prof_tdata_mutex_choose(uint64_t thr_uid)
536{
537
538 return (&tdata_locks[thr_uid % PROF_NTDATA_LOCKS]);
539}
540
541static prof_gctx_t *
Jason Evans5460aa62014-09-22 21:09:23 -0700542prof_gctx_create(tsd_t *tsd, prof_bt_t *bt)
Jason Evans4f37ef62014-01-16 13:23:56 -0800543{
Jason Evansab532e92014-08-15 15:05:12 -0700544 /*
545 * Create a single allocation that has space for vec of length bt->len.
546 */
Jason Evans4581b972014-11-27 17:22:36 -0200547 prof_gctx_t *gctx = (prof_gctx_t *)iallocztm(tsd, offsetof(prof_gctx_t,
Jason Evans1cb181e2015-01-29 15:30:47 -0800548 vec) + (bt->len * sizeof(void *)), false, tcache_get(tsd, true),
549 true, NULL);
Jason Evans602c8e02014-08-18 16:22:13 -0700550 if (gctx == NULL)
Jason Evansab532e92014-08-15 15:05:12 -0700551 return (NULL);
Jason Evans602c8e02014-08-18 16:22:13 -0700552 gctx->lock = prof_gctx_mutex_choose();
Jason Evans4f37ef62014-01-16 13:23:56 -0800553 /*
554 * Set nlimbo to 1, in order to avoid a race condition with
Jason Evans20c31de2014-10-02 23:01:10 -0700555 * prof_tctx_destroy()/prof_gctx_try_destroy().
Jason Evans4f37ef62014-01-16 13:23:56 -0800556 */
Jason Evans602c8e02014-08-18 16:22:13 -0700557 gctx->nlimbo = 1;
558 tctx_tree_new(&gctx->tctxs);
Jason Evansab532e92014-08-15 15:05:12 -0700559 /* Duplicate bt. */
Jason Evans602c8e02014-08-18 16:22:13 -0700560 memcpy(gctx->vec, bt->vec, bt->len * sizeof(void *));
561 gctx->bt.vec = gctx->vec;
562 gctx->bt.len = bt->len;
563 return (gctx);
Jason Evans4f37ef62014-01-16 13:23:56 -0800564}
565
566static void
Jason Evansc93ed812014-10-30 16:50:33 -0700567prof_gctx_try_destroy(tsd_t *tsd, prof_tdata_t *tdata_self, prof_gctx_t *gctx,
568 prof_tdata_t *tdata)
Jason Evans4f37ef62014-01-16 13:23:56 -0800569{
Jason Evans4f37ef62014-01-16 13:23:56 -0800570
571 cassert(config_prof);
572
573 /*
Jason Evans602c8e02014-08-18 16:22:13 -0700574 * Check that gctx is still unused by any thread cache before destroying
575 * it. prof_lookup() increments gctx->nlimbo in order to avoid a race
576 * condition with this function, as does prof_tctx_destroy() in order to
577 * avoid a race between the main body of prof_tctx_destroy() and entry
Jason Evans4f37ef62014-01-16 13:23:56 -0800578 * into this function.
579 */
Jason Evansc93ed812014-10-30 16:50:33 -0700580 prof_enter(tsd, tdata_self);
Jason Evans602c8e02014-08-18 16:22:13 -0700581 malloc_mutex_lock(gctx->lock);
Jason Evans20c31de2014-10-02 23:01:10 -0700582 assert(gctx->nlimbo != 0);
Jason Evans602c8e02014-08-18 16:22:13 -0700583 if (tctx_tree_empty(&gctx->tctxs) && gctx->nlimbo == 1) {
584 /* Remove gctx from bt2gctx. */
Jason Evans5460aa62014-09-22 21:09:23 -0700585 if (ckh_remove(tsd, &bt2gctx, &gctx->bt, NULL, NULL))
Jason Evans4f37ef62014-01-16 13:23:56 -0800586 not_reached();
Jason Evansc93ed812014-10-30 16:50:33 -0700587 prof_leave(tsd, tdata_self);
Jason Evans602c8e02014-08-18 16:22:13 -0700588 /* Destroy gctx. */
589 malloc_mutex_unlock(gctx->lock);
Jason Evans1cb181e2015-01-29 15:30:47 -0800590 idalloctm(tsd, gctx, tcache_get(tsd, false), true);
Jason Evans4f37ef62014-01-16 13:23:56 -0800591 } else {
592 /*
Jason Evans602c8e02014-08-18 16:22:13 -0700593 * Compensate for increment in prof_tctx_destroy() or
Jason Evans4f37ef62014-01-16 13:23:56 -0800594 * prof_lookup().
595 */
Jason Evans602c8e02014-08-18 16:22:13 -0700596 gctx->nlimbo--;
597 malloc_mutex_unlock(gctx->lock);
Jason Evansc93ed812014-10-30 16:50:33 -0700598 prof_leave(tsd, tdata_self);
Jason Evans4f37ef62014-01-16 13:23:56 -0800599 }
600}
601
Jason Evans602c8e02014-08-18 16:22:13 -0700602/* tctx->tdata->lock must be held. */
603static bool
604prof_tctx_should_destroy(prof_tctx_t *tctx)
Jason Evans4f37ef62014-01-16 13:23:56 -0800605{
Jason Evans4f37ef62014-01-16 13:23:56 -0800606
Jason Evans602c8e02014-08-18 16:22:13 -0700607 if (opt_prof_accum)
608 return (false);
609 if (tctx->cnts.curobjs != 0)
610 return (false);
Jason Evans6e73dc12014-09-09 19:37:26 -0700611 if (tctx->prepared)
612 return (false);
Jason Evans602c8e02014-08-18 16:22:13 -0700613 return (true);
Jason Evans4f37ef62014-01-16 13:23:56 -0800614}
615
Jason Evansfb1775e2014-01-14 17:04:34 -0800616static bool
Jason Evans602c8e02014-08-18 16:22:13 -0700617prof_gctx_should_destroy(prof_gctx_t *gctx)
618{
619
620 if (opt_prof_accum)
621 return (false);
Jason Evans551ebc42014-10-03 10:16:09 -0700622 if (!tctx_tree_empty(&gctx->tctxs))
Jason Evans602c8e02014-08-18 16:22:13 -0700623 return (false);
624 if (gctx->nlimbo != 0)
625 return (false);
626 return (true);
627}
628
629/* tctx->tdata->lock is held upon entry, and released before return. */
630static void
Jason Evans5460aa62014-09-22 21:09:23 -0700631prof_tctx_destroy(tsd_t *tsd, prof_tctx_t *tctx)
Jason Evans602c8e02014-08-18 16:22:13 -0700632{
Jason Evans6fd53da2014-09-09 12:45:53 -0700633 prof_tdata_t *tdata = tctx->tdata;
Jason Evans602c8e02014-08-18 16:22:13 -0700634 prof_gctx_t *gctx = tctx->gctx;
Jason Evansbf406412014-10-06 16:35:11 -0700635 bool destroy_tdata, destroy_tctx, destroy_gctx;
Jason Evans602c8e02014-08-18 16:22:13 -0700636
637 assert(tctx->cnts.curobjs == 0);
638 assert(tctx->cnts.curbytes == 0);
Jason Evans551ebc42014-10-03 10:16:09 -0700639 assert(!opt_prof_accum);
Jason Evans602c8e02014-08-18 16:22:13 -0700640 assert(tctx->cnts.accumobjs == 0);
641 assert(tctx->cnts.accumbytes == 0);
642
Jason Evans5460aa62014-09-22 21:09:23 -0700643 ckh_remove(tsd, &tdata->bt2tctx, &gctx->bt, NULL, NULL);
Jason Evansf04a0be2014-10-04 15:03:49 -0700644 destroy_tdata = prof_tdata_should_destroy(tdata, false);
Jason Evans6fd53da2014-09-09 12:45:53 -0700645 malloc_mutex_unlock(tdata->lock);
Jason Evans602c8e02014-08-18 16:22:13 -0700646
647 malloc_mutex_lock(gctx->lock);
Jason Evans764b0002015-03-14 14:01:35 -0700648 switch (tctx->state) {
Jason Evans04211e22015-03-16 15:11:06 -0700649 case prof_tctx_state_nominal:
Jason Evansbf406412014-10-06 16:35:11 -0700650 tctx_tree_remove(&gctx->tctxs, tctx);
651 destroy_tctx = true;
652 if (prof_gctx_should_destroy(gctx)) {
653 /*
654 * Increment gctx->nlimbo in order to keep another
655 * thread from winning the race to destroy gctx while
656 * this one has gctx->lock dropped. Without this, it
657 * would be possible for another thread to:
658 *
659 * 1) Sample an allocation associated with gctx.
660 * 2) Deallocate the sampled object.
661 * 3) Successfully prof_gctx_try_destroy(gctx).
662 *
663 * The result would be that gctx no longer exists by the
664 * time this thread accesses it in
665 * prof_gctx_try_destroy().
666 */
667 gctx->nlimbo++;
668 destroy_gctx = true;
669 } else
670 destroy_gctx = false;
Jason Evans764b0002015-03-14 14:01:35 -0700671 break;
672 case prof_tctx_state_dumping:
Jason Evans602c8e02014-08-18 16:22:13 -0700673 /*
Jason Evansbf406412014-10-06 16:35:11 -0700674 * A dumping thread needs tctx to remain valid until dumping
675 * has finished. Change state such that the dumping thread will
676 * complete destruction during a late dump iteration phase.
Jason Evans602c8e02014-08-18 16:22:13 -0700677 */
Jason Evansbf406412014-10-06 16:35:11 -0700678 tctx->state = prof_tctx_state_purgatory;
679 destroy_tctx = false;
Jason Evans602c8e02014-08-18 16:22:13 -0700680 destroy_gctx = false;
Jason Evans764b0002015-03-14 14:01:35 -0700681 break;
682 default:
683 not_reached();
Jason Evans262146d2015-03-14 14:34:16 -0700684 destroy_tctx = false;
685 destroy_gctx = false;
Jason Evansbf406412014-10-06 16:35:11 -0700686 }
Jason Evans602c8e02014-08-18 16:22:13 -0700687 malloc_mutex_unlock(gctx->lock);
Jason Evansc93ed812014-10-30 16:50:33 -0700688 if (destroy_gctx) {
689 prof_gctx_try_destroy(tsd, prof_tdata_get(tsd, false), gctx,
690 tdata);
691 }
Jason Evans6fd53da2014-09-09 12:45:53 -0700692
693 if (destroy_tdata)
Jason Evansf04a0be2014-10-04 15:03:49 -0700694 prof_tdata_destroy(tsd, tdata, false);
Jason Evans602c8e02014-08-18 16:22:13 -0700695
Jason Evansbf406412014-10-06 16:35:11 -0700696 if (destroy_tctx)
Jason Evans1cb181e2015-01-29 15:30:47 -0800697 idalloctm(tsd, tctx, tcache_get(tsd, false), true);
Jason Evans602c8e02014-08-18 16:22:13 -0700698}
699
700static bool
Jason Evans5460aa62014-09-22 21:09:23 -0700701prof_lookup_global(tsd_t *tsd, prof_bt_t *bt, prof_tdata_t *tdata,
702 void **p_btkey, prof_gctx_t **p_gctx, bool *p_new_gctx)
Jason Evansfb1775e2014-01-14 17:04:34 -0800703{
704 union {
Jason Evans602c8e02014-08-18 16:22:13 -0700705 prof_gctx_t *p;
Jason Evansfb1775e2014-01-14 17:04:34 -0800706 void *v;
Jason Evans602c8e02014-08-18 16:22:13 -0700707 } gctx;
Jason Evansfb1775e2014-01-14 17:04:34 -0800708 union {
709 prof_bt_t *p;
710 void *v;
711 } btkey;
Jason Evans602c8e02014-08-18 16:22:13 -0700712 bool new_gctx;
Jason Evansfb1775e2014-01-14 17:04:34 -0800713
Jason Evansc93ed812014-10-30 16:50:33 -0700714 prof_enter(tsd, tdata);
Jason Evans602c8e02014-08-18 16:22:13 -0700715 if (ckh_search(&bt2gctx, bt, &btkey.v, &gctx.v)) {
Jason Evansfb1775e2014-01-14 17:04:34 -0800716 /* bt has never been seen before. Insert it. */
Jason Evans5460aa62014-09-22 21:09:23 -0700717 gctx.p = prof_gctx_create(tsd, bt);
Jason Evans602c8e02014-08-18 16:22:13 -0700718 if (gctx.v == NULL) {
Jason Evansc93ed812014-10-30 16:50:33 -0700719 prof_leave(tsd, tdata);
Jason Evansfb1775e2014-01-14 17:04:34 -0800720 return (true);
721 }
Jason Evans602c8e02014-08-18 16:22:13 -0700722 btkey.p = &gctx.p->bt;
Jason Evans5460aa62014-09-22 21:09:23 -0700723 if (ckh_insert(tsd, &bt2gctx, btkey.v, gctx.v)) {
Jason Evansfb1775e2014-01-14 17:04:34 -0800724 /* OOM. */
Jason Evansc93ed812014-10-30 16:50:33 -0700725 prof_leave(tsd, tdata);
Jason Evans1cb181e2015-01-29 15:30:47 -0800726 idalloctm(tsd, gctx.v, tcache_get(tsd, false), true);
Jason Evansfb1775e2014-01-14 17:04:34 -0800727 return (true);
728 }
Jason Evans602c8e02014-08-18 16:22:13 -0700729 new_gctx = true;
Jason Evansfb1775e2014-01-14 17:04:34 -0800730 } else {
731 /*
732 * Increment nlimbo, in order to avoid a race condition with
Jason Evans20c31de2014-10-02 23:01:10 -0700733 * prof_tctx_destroy()/prof_gctx_try_destroy().
Jason Evansfb1775e2014-01-14 17:04:34 -0800734 */
Jason Evans602c8e02014-08-18 16:22:13 -0700735 malloc_mutex_lock(gctx.p->lock);
736 gctx.p->nlimbo++;
737 malloc_mutex_unlock(gctx.p->lock);
738 new_gctx = false;
Jason Evansfb1775e2014-01-14 17:04:34 -0800739 }
Jason Evansc93ed812014-10-30 16:50:33 -0700740 prof_leave(tsd, tdata);
Jason Evansfb1775e2014-01-14 17:04:34 -0800741
742 *p_btkey = btkey.v;
Jason Evans602c8e02014-08-18 16:22:13 -0700743 *p_gctx = gctx.p;
744 *p_new_gctx = new_gctx;
Jason Evansfb1775e2014-01-14 17:04:34 -0800745 return (false);
746}
747
Jason Evans602c8e02014-08-18 16:22:13 -0700748prof_tctx_t *
Jason Evans5460aa62014-09-22 21:09:23 -0700749prof_lookup(tsd_t *tsd, prof_bt_t *bt)
Jason Evans6109fe02010-02-10 10:37:56 -0800750{
Jason Evans075e77c2010-09-20 19:53:25 -0700751 union {
Jason Evans602c8e02014-08-18 16:22:13 -0700752 prof_tctx_t *p;
Jason Evans075e77c2010-09-20 19:53:25 -0700753 void *v;
754 } ret;
Jason Evans602c8e02014-08-18 16:22:13 -0700755 prof_tdata_t *tdata;
756 bool not_found;
Jason Evans6109fe02010-02-10 10:37:56 -0800757
Jason Evans7372b152012-02-10 20:22:09 -0800758 cassert(config_prof);
759
Jason Evans5460aa62014-09-22 21:09:23 -0700760 tdata = prof_tdata_get(tsd, false);
761 if (tdata == NULL)
Jason Evans52386b22012-04-22 16:00:11 -0700762 return (NULL);
Jason Evans6109fe02010-02-10 10:37:56 -0800763
Jason Evans602c8e02014-08-18 16:22:13 -0700764 malloc_mutex_lock(tdata->lock);
765 not_found = ckh_search(&tdata->bt2tctx, bt, NULL, &ret.v);
Jason Evans6e73dc12014-09-09 19:37:26 -0700766 if (!not_found) /* Note double negative! */
767 ret.p->prepared = true;
Jason Evans602c8e02014-08-18 16:22:13 -0700768 malloc_mutex_unlock(tdata->lock);
769 if (not_found) {
Jason Evans1cb181e2015-01-29 15:30:47 -0800770 tcache_t *tcache;
Jason Evansfb1775e2014-01-14 17:04:34 -0800771 void *btkey;
Jason Evans602c8e02014-08-18 16:22:13 -0700772 prof_gctx_t *gctx;
773 bool new_gctx, error;
Jason Evans6109fe02010-02-10 10:37:56 -0800774
775 /*
776 * This thread's cache lacks bt. Look for it in the global
777 * cache.
778 */
Jason Evans5460aa62014-09-22 21:09:23 -0700779 if (prof_lookup_global(tsd, bt, tdata, &btkey, &gctx,
Jason Evans602c8e02014-08-18 16:22:13 -0700780 &new_gctx))
Jason Evansfb1775e2014-01-14 17:04:34 -0800781 return (NULL);
Jason Evans6109fe02010-02-10 10:37:56 -0800782
Jason Evans602c8e02014-08-18 16:22:13 -0700783 /* Link a prof_tctx_t into gctx for this thread. */
Jason Evans1cb181e2015-01-29 15:30:47 -0800784 tcache = tcache_get(tsd, true);
785 ret.v = iallocztm(tsd, sizeof(prof_tctx_t), false, tcache, true,
Jason Evans4581b972014-11-27 17:22:36 -0200786 NULL);
Jason Evansb41ccdb2014-08-15 15:01:15 -0700787 if (ret.p == NULL) {
Jason Evans602c8e02014-08-18 16:22:13 -0700788 if (new_gctx)
Jason Evansc93ed812014-10-30 16:50:33 -0700789 prof_gctx_try_destroy(tsd, tdata, gctx, tdata);
Jason Evansb41ccdb2014-08-15 15:01:15 -0700790 return (NULL);
Jason Evansa881cd22010-10-02 15:18:50 -0700791 }
Jason Evans602c8e02014-08-18 16:22:13 -0700792 ret.p->tdata = tdata;
Jason Evans44c97b72014-10-12 13:03:20 -0700793 ret.p->thr_uid = tdata->thr_uid;
Jason Evans075e77c2010-09-20 19:53:25 -0700794 memset(&ret.p->cnts, 0, sizeof(prof_cnt_t));
Jason Evans602c8e02014-08-18 16:22:13 -0700795 ret.p->gctx = gctx;
Jason Evans04211e22015-03-16 15:11:06 -0700796 ret.p->tctx_uid = tdata->tctx_uid_next++;
Jason Evans6e73dc12014-09-09 19:37:26 -0700797 ret.p->prepared = true;
Jason Evans6ef80d62014-09-24 22:14:21 -0700798 ret.p->state = prof_tctx_state_initializing;
Jason Evans602c8e02014-08-18 16:22:13 -0700799 malloc_mutex_lock(tdata->lock);
Jason Evans5460aa62014-09-22 21:09:23 -0700800 error = ckh_insert(tsd, &tdata->bt2tctx, btkey, ret.v);
Jason Evans602c8e02014-08-18 16:22:13 -0700801 malloc_mutex_unlock(tdata->lock);
802 if (error) {
803 if (new_gctx)
Jason Evansc93ed812014-10-30 16:50:33 -0700804 prof_gctx_try_destroy(tsd, tdata, gctx, tdata);
Jason Evans1cb181e2015-01-29 15:30:47 -0800805 idalloctm(tsd, ret.v, tcache, true);
Jason Evans6109fe02010-02-10 10:37:56 -0800806 return (NULL);
807 }
Jason Evans602c8e02014-08-18 16:22:13 -0700808 malloc_mutex_lock(gctx->lock);
Jason Evans6ef80d62014-09-24 22:14:21 -0700809 ret.p->state = prof_tctx_state_nominal;
Jason Evans602c8e02014-08-18 16:22:13 -0700810 tctx_tree_insert(&gctx->tctxs, ret.p);
811 gctx->nlimbo--;
812 malloc_mutex_unlock(gctx->lock);
Jason Evans6109fe02010-02-10 10:37:56 -0800813 }
814
Jason Evans075e77c2010-09-20 19:53:25 -0700815 return (ret.p);
Jason Evans6109fe02010-02-10 10:37:56 -0800816}
817
Ben Maurer6c39f9e2014-04-15 13:47:13 -0700818void
Jason Evans602c8e02014-08-18 16:22:13 -0700819prof_sample_threshold_update(prof_tdata_t *tdata)
Ben Maurer6c39f9e2014-04-15 13:47:13 -0700820{
821 /*
822 * The body of this function is compiled out unless heap profiling is
823 * enabled, so that it is possible to compile jemalloc with floating
824 * point support completely disabled. Avoiding floating point code is
825 * important on memory-constrained systems, but it also enables a
826 * workaround for versions of glibc that don't properly save/restore
827 * floating point registers during dynamic lazy symbol loading (which
828 * internally calls into whatever malloc implementation happens to be
829 * integrated into the application). Note that some compilers (e.g.
830 * gcc 4.8) may use floating point registers for fast memory moves, so
831 * jemalloc must be compiled with such optimizations disabled (e.g.
832 * -mno-sse) in order for the workaround to be complete.
833 */
834#ifdef JEMALLOC_PROF
835 uint64_t r;
836 double u;
837
838 if (!config_prof)
839 return;
840
Jason Evans602c8e02014-08-18 16:22:13 -0700841 if (lg_prof_sample == 0) {
842 tdata->bytes_until_sample = 0;
Ben Maurer6c39f9e2014-04-15 13:47:13 -0700843 return;
844 }
845
846 /*
Jason Evans602c8e02014-08-18 16:22:13 -0700847 * Compute sample interval as a geometrically distributed random
848 * variable with mean (2^lg_prof_sample).
Ben Maurer6c39f9e2014-04-15 13:47:13 -0700849 *
Jason Evans602c8e02014-08-18 16:22:13 -0700850 * __ __
851 * | log(u) | 1
852 * tdata->bytes_until_sample = | -------- |, where p = ---------------
853 * | log(1-p) | lg_prof_sample
854 * 2
Ben Maurer6c39f9e2014-04-15 13:47:13 -0700855 *
856 * For more information on the math, see:
857 *
858 * Non-Uniform Random Variate Generation
859 * Luc Devroye
860 * Springer-Verlag, New York, 1986
861 * pp 500
862 * (http://luc.devroye.org/rnbookindex.html)
863 */
Jason Evans602c8e02014-08-18 16:22:13 -0700864 prng64(r, 53, tdata->prng_state, UINT64_C(6364136223846793005),
865 UINT64_C(1442695040888963407));
Ben Maurer6c39f9e2014-04-15 13:47:13 -0700866 u = (double)r * (1.0/9007199254740992.0L);
Jason Evans602c8e02014-08-18 16:22:13 -0700867 tdata->bytes_until_sample = (uint64_t)(log(u) /
868 log(1.0 - (1.0 / (double)((uint64_t)1U << lg_prof_sample))))
Ben Maurer6c39f9e2014-04-15 13:47:13 -0700869 + (uint64_t)1U;
870#endif
871}
872
Jason Evans772163b2014-01-17 15:40:52 -0800873#ifdef JEMALLOC_JET
Jason Evans20c31de2014-10-02 23:01:10 -0700874static prof_tdata_t *
875prof_tdata_count_iter(prof_tdata_tree_t *tdatas, prof_tdata_t *tdata, void *arg)
876{
877 size_t *tdata_count = (size_t *)arg;
878
879 (*tdata_count)++;
880
881 return (NULL);
882}
883
884size_t
885prof_tdata_count(void)
886{
887 size_t tdata_count = 0;
888
889 malloc_mutex_lock(&tdatas_mtx);
890 tdata_tree_iter(&tdatas, NULL, prof_tdata_count_iter,
891 (void *)&tdata_count);
892 malloc_mutex_unlock(&tdatas_mtx);
893
894 return (tdata_count);
895}
896#endif
897
898#ifdef JEMALLOC_JET
Jason Evans772163b2014-01-17 15:40:52 -0800899size_t
900prof_bt_count(void)
901{
902 size_t bt_count;
Jason Evans5460aa62014-09-22 21:09:23 -0700903 tsd_t *tsd;
Jason Evans602c8e02014-08-18 16:22:13 -0700904 prof_tdata_t *tdata;
Jason Evans772163b2014-01-17 15:40:52 -0800905
Jason Evans029d44c2014-10-04 11:12:53 -0700906 tsd = tsd_fetch();
Jason Evans5460aa62014-09-22 21:09:23 -0700907 tdata = prof_tdata_get(tsd, false);
908 if (tdata == NULL)
Jason Evans772163b2014-01-17 15:40:52 -0800909 return (0);
910
Jason Evansc93ed812014-10-30 16:50:33 -0700911 malloc_mutex_lock(&bt2gctx_mtx);
Jason Evans602c8e02014-08-18 16:22:13 -0700912 bt_count = ckh_count(&bt2gctx);
Jason Evansc93ed812014-10-30 16:50:33 -0700913 malloc_mutex_unlock(&bt2gctx_mtx);
Jason Evans772163b2014-01-17 15:40:52 -0800914
915 return (bt_count);
916}
917#endif
918
919#ifdef JEMALLOC_JET
920#undef prof_dump_open
921#define prof_dump_open JEMALLOC_N(prof_dump_open_impl)
922#endif
923static int
Jason Evans4f37ef62014-01-16 13:23:56 -0800924prof_dump_open(bool propagate_err, const char *filename)
925{
Jason Evans772163b2014-01-17 15:40:52 -0800926 int fd;
Jason Evans4f37ef62014-01-16 13:23:56 -0800927
Jason Evans772163b2014-01-17 15:40:52 -0800928 fd = creat(filename, 0644);
Jason Evans551ebc42014-10-03 10:16:09 -0700929 if (fd == -1 && !propagate_err) {
Jason Evans772163b2014-01-17 15:40:52 -0800930 malloc_printf("<jemalloc>: creat(\"%s\"), 0644) failed\n",
931 filename);
932 if (opt_abort)
933 abort();
Jason Evans4f37ef62014-01-16 13:23:56 -0800934 }
935
Jason Evans772163b2014-01-17 15:40:52 -0800936 return (fd);
Jason Evans4f37ef62014-01-16 13:23:56 -0800937}
Jason Evans772163b2014-01-17 15:40:52 -0800938#ifdef JEMALLOC_JET
939#undef prof_dump_open
940#define prof_dump_open JEMALLOC_N(prof_dump_open)
941prof_dump_open_t *prof_dump_open = JEMALLOC_N(prof_dump_open_impl);
942#endif
Jason Evans4f37ef62014-01-16 13:23:56 -0800943
944static bool
945prof_dump_flush(bool propagate_err)
Jason Evans6109fe02010-02-10 10:37:56 -0800946{
Jason Evans22ca8552010-03-02 11:57:30 -0800947 bool ret = false;
Jason Evans6109fe02010-02-10 10:37:56 -0800948 ssize_t err;
949
Jason Evans7372b152012-02-10 20:22:09 -0800950 cassert(config_prof);
951
Jason Evans6109fe02010-02-10 10:37:56 -0800952 err = write(prof_dump_fd, prof_dump_buf, prof_dump_buf_end);
953 if (err == -1) {
Jason Evans551ebc42014-10-03 10:16:09 -0700954 if (!propagate_err) {
Jason Evans698805c2010-03-03 17:45:38 -0800955 malloc_write("<jemalloc>: write() failed during heap "
956 "profile flush\n");
Jason Evans22ca8552010-03-02 11:57:30 -0800957 if (opt_abort)
958 abort();
959 }
960 ret = true;
Jason Evans6109fe02010-02-10 10:37:56 -0800961 }
962 prof_dump_buf_end = 0;
Jason Evans22ca8552010-03-02 11:57:30 -0800963
964 return (ret);
Jason Evans6109fe02010-02-10 10:37:56 -0800965}
966
Jason Evans22ca8552010-03-02 11:57:30 -0800967static bool
Jason Evans4f37ef62014-01-16 13:23:56 -0800968prof_dump_close(bool propagate_err)
969{
970 bool ret;
971
972 assert(prof_dump_fd != -1);
973 ret = prof_dump_flush(propagate_err);
974 close(prof_dump_fd);
975 prof_dump_fd = -1;
976
977 return (ret);
978}
979
980static bool
981prof_dump_write(bool propagate_err, const char *s)
Jason Evans6109fe02010-02-10 10:37:56 -0800982{
983 unsigned i, slen, n;
984
Jason Evans7372b152012-02-10 20:22:09 -0800985 cassert(config_prof);
986
Jason Evans6109fe02010-02-10 10:37:56 -0800987 i = 0;
988 slen = strlen(s);
989 while (i < slen) {
990 /* Flush the buffer if it is full. */
Jason Evanscd9a1342012-03-21 18:33:03 -0700991 if (prof_dump_buf_end == PROF_DUMP_BUFSIZE)
Jason Evans4f37ef62014-01-16 13:23:56 -0800992 if (prof_dump_flush(propagate_err) && propagate_err)
Jason Evans22ca8552010-03-02 11:57:30 -0800993 return (true);
Jason Evans6109fe02010-02-10 10:37:56 -0800994
Jason Evanscd9a1342012-03-21 18:33:03 -0700995 if (prof_dump_buf_end + slen <= PROF_DUMP_BUFSIZE) {
Jason Evans6109fe02010-02-10 10:37:56 -0800996 /* Finish writing. */
997 n = slen - i;
998 } else {
999 /* Write as much of s as will fit. */
Jason Evanscd9a1342012-03-21 18:33:03 -07001000 n = PROF_DUMP_BUFSIZE - prof_dump_buf_end;
Jason Evans6109fe02010-02-10 10:37:56 -08001001 }
1002 memcpy(&prof_dump_buf[prof_dump_buf_end], &s[i], n);
1003 prof_dump_buf_end += n;
1004 i += n;
1005 }
Jason Evans22ca8552010-03-02 11:57:30 -08001006
1007 return (false);
Jason Evans6109fe02010-02-10 10:37:56 -08001008}
1009
Jason Evanse42c3092015-07-22 15:44:47 -07001010JEMALLOC_FORMAT_PRINTF(2, 3)
Jason Evansd81e4bd2012-03-06 14:57:45 -08001011static bool
Jason Evans4f37ef62014-01-16 13:23:56 -08001012prof_dump_printf(bool propagate_err, const char *format, ...)
Jason Evansd81e4bd2012-03-06 14:57:45 -08001013{
1014 bool ret;
1015 va_list ap;
Jason Evanscd9a1342012-03-21 18:33:03 -07001016 char buf[PROF_PRINTF_BUFSIZE];
Jason Evansd81e4bd2012-03-06 14:57:45 -08001017
1018 va_start(ap, format);
Jason Evans6da54182012-03-23 18:05:51 -07001019 malloc_vsnprintf(buf, sizeof(buf), format, ap);
Jason Evansd81e4bd2012-03-06 14:57:45 -08001020 va_end(ap);
Jason Evans4f37ef62014-01-16 13:23:56 -08001021 ret = prof_dump_write(propagate_err, buf);
Jason Evansd81e4bd2012-03-06 14:57:45 -08001022
1023 return (ret);
1024}
1025
Jason Evans602c8e02014-08-18 16:22:13 -07001026/* tctx->tdata->lock is held. */
1027static void
1028prof_tctx_merge_tdata(prof_tctx_t *tctx, prof_tdata_t *tdata)
Jason Evans3a81cbd2014-08-16 12:58:55 -07001029{
Jason Evans3a81cbd2014-08-16 12:58:55 -07001030
Jason Evans6ef80d62014-09-24 22:14:21 -07001031 malloc_mutex_lock(tctx->gctx->lock);
Jason Evans764b0002015-03-14 14:01:35 -07001032
1033 switch (tctx->state) {
1034 case prof_tctx_state_initializing:
Jason Evans6ef80d62014-09-24 22:14:21 -07001035 malloc_mutex_unlock(tctx->gctx->lock);
1036 return;
Jason Evans764b0002015-03-14 14:01:35 -07001037 case prof_tctx_state_nominal:
1038 tctx->state = prof_tctx_state_dumping;
1039 malloc_mutex_unlock(tctx->gctx->lock);
Jason Evans6ef80d62014-09-24 22:14:21 -07001040
Jason Evans764b0002015-03-14 14:01:35 -07001041 memcpy(&tctx->dump_cnts, &tctx->cnts, sizeof(prof_cnt_t));
Jason Evans3a81cbd2014-08-16 12:58:55 -07001042
Jason Evans764b0002015-03-14 14:01:35 -07001043 tdata->cnt_summed.curobjs += tctx->dump_cnts.curobjs;
1044 tdata->cnt_summed.curbytes += tctx->dump_cnts.curbytes;
1045 if (opt_prof_accum) {
1046 tdata->cnt_summed.accumobjs +=
1047 tctx->dump_cnts.accumobjs;
1048 tdata->cnt_summed.accumbytes +=
1049 tctx->dump_cnts.accumbytes;
1050 }
1051 break;
1052 case prof_tctx_state_dumping:
1053 case prof_tctx_state_purgatory:
1054 not_reached();
Jason Evans602c8e02014-08-18 16:22:13 -07001055 }
1056}
1057
1058/* gctx->lock is held. */
1059static void
1060prof_tctx_merge_gctx(prof_tctx_t *tctx, prof_gctx_t *gctx)
1061{
1062
1063 gctx->cnt_summed.curobjs += tctx->dump_cnts.curobjs;
1064 gctx->cnt_summed.curbytes += tctx->dump_cnts.curbytes;
1065 if (opt_prof_accum) {
1066 gctx->cnt_summed.accumobjs += tctx->dump_cnts.accumobjs;
1067 gctx->cnt_summed.accumbytes += tctx->dump_cnts.accumbytes;
1068 }
1069}
1070
1071/* tctx->gctx is held. */
1072static prof_tctx_t *
1073prof_tctx_merge_iter(prof_tctx_tree_t *tctxs, prof_tctx_t *tctx, void *arg)
1074{
1075
1076 switch (tctx->state) {
1077 case prof_tctx_state_nominal:
1078 /* New since dumping started; ignore. */
1079 break;
1080 case prof_tctx_state_dumping:
1081 case prof_tctx_state_purgatory:
1082 prof_tctx_merge_gctx(tctx, tctx->gctx);
1083 break;
1084 default:
1085 not_reached();
Jason Evans3a81cbd2014-08-16 12:58:55 -07001086 }
1087
1088 return (NULL);
1089}
1090
Jason Evans602c8e02014-08-18 16:22:13 -07001091/* gctx->lock is held. */
1092static prof_tctx_t *
1093prof_tctx_dump_iter(prof_tctx_tree_t *tctxs, prof_tctx_t *tctx, void *arg)
1094{
1095 bool propagate_err = *(bool *)arg;
1096
1097 if (prof_dump_printf(propagate_err,
1098 " t%"PRIu64": %"PRIu64": %"PRIu64" [%"PRIu64": %"PRIu64"]\n",
Jason Evans44c97b72014-10-12 13:03:20 -07001099 tctx->thr_uid, tctx->dump_cnts.curobjs, tctx->dump_cnts.curbytes,
1100 tctx->dump_cnts.accumobjs, tctx->dump_cnts.accumbytes))
Jason Evans602c8e02014-08-18 16:22:13 -07001101 return (tctx);
1102 return (NULL);
1103}
1104
1105/* tctx->gctx is held. */
1106static prof_tctx_t *
1107prof_tctx_finish_iter(prof_tctx_tree_t *tctxs, prof_tctx_t *tctx, void *arg)
1108{
1109 prof_tctx_t *ret;
1110
1111 switch (tctx->state) {
1112 case prof_tctx_state_nominal:
1113 /* New since dumping started; ignore. */
1114 break;
1115 case prof_tctx_state_dumping:
1116 tctx->state = prof_tctx_state_nominal;
1117 break;
1118 case prof_tctx_state_purgatory:
Jason Evans20c31de2014-10-02 23:01:10 -07001119 ret = tctx;
Jason Evans602c8e02014-08-18 16:22:13 -07001120 goto label_return;
1121 default:
1122 not_reached();
1123 }
1124
1125 ret = NULL;
1126label_return:
1127 return (ret);
1128}
1129
Jason Evans6109fe02010-02-10 10:37:56 -08001130static void
Jason Evans602c8e02014-08-18 16:22:13 -07001131prof_dump_gctx_prep(prof_gctx_t *gctx, prof_gctx_tree_t *gctxs)
Jason Evans6109fe02010-02-10 10:37:56 -08001132{
Jason Evans6109fe02010-02-10 10:37:56 -08001133
Jason Evans7372b152012-02-10 20:22:09 -08001134 cassert(config_prof);
1135
Jason Evans602c8e02014-08-18 16:22:13 -07001136 malloc_mutex_lock(gctx->lock);
Jason Evans6109fe02010-02-10 10:37:56 -08001137
Jason Evans4f37ef62014-01-16 13:23:56 -08001138 /*
Jason Evans602c8e02014-08-18 16:22:13 -07001139 * Increment nlimbo so that gctx won't go away before dump.
1140 * Additionally, link gctx into the dump list so that it is included in
Jason Evans4f37ef62014-01-16 13:23:56 -08001141 * prof_dump()'s second pass.
1142 */
Jason Evans602c8e02014-08-18 16:22:13 -07001143 gctx->nlimbo++;
1144 gctx_tree_insert(gctxs, gctx);
Jason Evans4f37ef62014-01-16 13:23:56 -08001145
Jason Evans602c8e02014-08-18 16:22:13 -07001146 memset(&gctx->cnt_summed, 0, sizeof(prof_cnt_t));
Jason Evans6109fe02010-02-10 10:37:56 -08001147
Jason Evans602c8e02014-08-18 16:22:13 -07001148 malloc_mutex_unlock(gctx->lock);
1149}
Jason Evans9ce3bfd2010-10-02 22:39:59 -07001150
Jason Evans602c8e02014-08-18 16:22:13 -07001151static prof_gctx_t *
1152prof_gctx_merge_iter(prof_gctx_tree_t *gctxs, prof_gctx_t *gctx, void *arg)
1153{
1154 size_t *leak_ngctx = (size_t *)arg;
Jason Evans6109fe02010-02-10 10:37:56 -08001155
Jason Evans602c8e02014-08-18 16:22:13 -07001156 malloc_mutex_lock(gctx->lock);
1157 tctx_tree_iter(&gctx->tctxs, NULL, prof_tctx_merge_iter, NULL);
1158 if (gctx->cnt_summed.curobjs != 0)
1159 (*leak_ngctx)++;
1160 malloc_mutex_unlock(gctx->lock);
1161
1162 return (NULL);
1163}
1164
Jason Evans20c31de2014-10-02 23:01:10 -07001165static void
1166prof_gctx_finish(tsd_t *tsd, prof_gctx_tree_t *gctxs)
Jason Evans602c8e02014-08-18 16:22:13 -07001167{
Jason Evans5460aa62014-09-22 21:09:23 -07001168 prof_tdata_t *tdata = prof_tdata_get(tsd, false);
Jason Evans20c31de2014-10-02 23:01:10 -07001169 prof_gctx_t *gctx;
Jason Evans602c8e02014-08-18 16:22:13 -07001170
Jason Evans20c31de2014-10-02 23:01:10 -07001171 /*
1172 * Standard tree iteration won't work here, because as soon as we
1173 * decrement gctx->nlimbo and unlock gctx, another thread can
1174 * concurrently destroy it, which will corrupt the tree. Therefore,
1175 * tear down the tree one node at a time during iteration.
1176 */
1177 while ((gctx = gctx_tree_first(gctxs)) != NULL) {
1178 gctx_tree_remove(gctxs, gctx);
1179 malloc_mutex_lock(gctx->lock);
1180 {
1181 prof_tctx_t *next;
Jason Evans602c8e02014-08-18 16:22:13 -07001182
Jason Evans20c31de2014-10-02 23:01:10 -07001183 next = NULL;
1184 do {
1185 prof_tctx_t *to_destroy =
1186 tctx_tree_iter(&gctx->tctxs, next,
1187 prof_tctx_finish_iter, NULL);
1188 if (to_destroy != NULL) {
1189 next = tctx_tree_next(&gctx->tctxs,
1190 to_destroy);
1191 tctx_tree_remove(&gctx->tctxs,
1192 to_destroy);
Jason Evans1cb181e2015-01-29 15:30:47 -08001193 idalloctm(tsd, to_destroy,
1194 tcache_get(tsd, false), true);
Jason Evans20c31de2014-10-02 23:01:10 -07001195 } else
1196 next = NULL;
1197 } while (next != NULL);
1198 }
1199 gctx->nlimbo--;
1200 if (prof_gctx_should_destroy(gctx)) {
1201 gctx->nlimbo++;
1202 malloc_mutex_unlock(gctx->lock);
Jason Evansc93ed812014-10-30 16:50:33 -07001203 prof_gctx_try_destroy(tsd, tdata, gctx, tdata);
Jason Evans20c31de2014-10-02 23:01:10 -07001204 } else
1205 malloc_mutex_unlock(gctx->lock);
1206 }
Jason Evans602c8e02014-08-18 16:22:13 -07001207}
1208
1209static prof_tdata_t *
1210prof_tdata_merge_iter(prof_tdata_tree_t *tdatas, prof_tdata_t *tdata, void *arg)
1211{
1212 prof_cnt_t *cnt_all = (prof_cnt_t *)arg;
1213
1214 malloc_mutex_lock(tdata->lock);
Jason Evans20c31de2014-10-02 23:01:10 -07001215 if (!tdata->expired) {
Jason Evans602c8e02014-08-18 16:22:13 -07001216 size_t tabind;
1217 union {
1218 prof_tctx_t *p;
1219 void *v;
1220 } tctx;
1221
1222 tdata->dumping = true;
1223 memset(&tdata->cnt_summed, 0, sizeof(prof_cnt_t));
Jason Evans551ebc42014-10-03 10:16:09 -07001224 for (tabind = 0; !ckh_iter(&tdata->bt2tctx, &tabind, NULL,
1225 &tctx.v);)
Jason Evans602c8e02014-08-18 16:22:13 -07001226 prof_tctx_merge_tdata(tctx.p, tdata);
1227
1228 cnt_all->curobjs += tdata->cnt_summed.curobjs;
1229 cnt_all->curbytes += tdata->cnt_summed.curbytes;
1230 if (opt_prof_accum) {
1231 cnt_all->accumobjs += tdata->cnt_summed.accumobjs;
1232 cnt_all->accumbytes += tdata->cnt_summed.accumbytes;
1233 }
1234 } else
1235 tdata->dumping = false;
1236 malloc_mutex_unlock(tdata->lock);
1237
1238 return (NULL);
1239}
1240
1241static prof_tdata_t *
1242prof_tdata_dump_iter(prof_tdata_tree_t *tdatas, prof_tdata_t *tdata, void *arg)
1243{
1244 bool propagate_err = *(bool *)arg;
1245
Jason Evans551ebc42014-10-03 10:16:09 -07001246 if (!tdata->dumping)
Jason Evans602c8e02014-08-18 16:22:13 -07001247 return (NULL);
1248
1249 if (prof_dump_printf(propagate_err,
1250 " t%"PRIu64": %"PRIu64": %"PRIu64" [%"PRIu64": %"PRIu64"]%s%s\n",
1251 tdata->thr_uid, tdata->cnt_summed.curobjs,
1252 tdata->cnt_summed.curbytes, tdata->cnt_summed.accumobjs,
1253 tdata->cnt_summed.accumbytes,
1254 (tdata->thread_name != NULL) ? " " : "",
1255 (tdata->thread_name != NULL) ? tdata->thread_name : ""))
1256 return (tdata);
1257 return (NULL);
Jason Evans6109fe02010-02-10 10:37:56 -08001258}
1259
Jason Evans20c31de2014-10-02 23:01:10 -07001260#ifdef JEMALLOC_JET
1261#undef prof_dump_header
1262#define prof_dump_header JEMALLOC_N(prof_dump_header_impl)
1263#endif
Jason Evans4f37ef62014-01-16 13:23:56 -08001264static bool
1265prof_dump_header(bool propagate_err, const prof_cnt_t *cnt_all)
Jason Evansa881cd22010-10-02 15:18:50 -07001266{
Jason Evans602c8e02014-08-18 16:22:13 -07001267 bool ret;
Jason Evansa881cd22010-10-02 15:18:50 -07001268
Jason Evans602c8e02014-08-18 16:22:13 -07001269 if (prof_dump_printf(propagate_err,
1270 "heap_v2/%"PRIu64"\n"
1271 " t*: %"PRIu64": %"PRIu64" [%"PRIu64": %"PRIu64"]\n",
1272 ((uint64_t)1U << lg_prof_sample), cnt_all->curobjs,
1273 cnt_all->curbytes, cnt_all->accumobjs, cnt_all->accumbytes))
1274 return (true);
Jason Evans4f37ef62014-01-16 13:23:56 -08001275
Jason Evans602c8e02014-08-18 16:22:13 -07001276 malloc_mutex_lock(&tdatas_mtx);
1277 ret = (tdata_tree_iter(&tdatas, NULL, prof_tdata_dump_iter,
1278 (void *)&propagate_err) != NULL);
1279 malloc_mutex_unlock(&tdatas_mtx);
1280 return (ret);
Jason Evansa881cd22010-10-02 15:18:50 -07001281}
Jason Evans20c31de2014-10-02 23:01:10 -07001282#ifdef JEMALLOC_JET
1283#undef prof_dump_header
1284#define prof_dump_header JEMALLOC_N(prof_dump_header)
1285prof_dump_header_t *prof_dump_header = JEMALLOC_N(prof_dump_header_impl);
1286#endif
Jason Evansa881cd22010-10-02 15:18:50 -07001287
Jason Evans602c8e02014-08-18 16:22:13 -07001288/* gctx->lock is held. */
Jason Evans22ca8552010-03-02 11:57:30 -08001289static bool
Jason Evans602c8e02014-08-18 16:22:13 -07001290prof_dump_gctx(bool propagate_err, prof_gctx_t *gctx, const prof_bt_t *bt,
1291 prof_gctx_tree_t *gctxs)
Jason Evans6109fe02010-02-10 10:37:56 -08001292{
Jason Evans4f37ef62014-01-16 13:23:56 -08001293 bool ret;
Jason Evans6109fe02010-02-10 10:37:56 -08001294 unsigned i;
1295
Jason Evans7372b152012-02-10 20:22:09 -08001296 cassert(config_prof);
1297
Jason Evans602c8e02014-08-18 16:22:13 -07001298 /* Avoid dumping such gctx's that have no useful data. */
Jason Evans551ebc42014-10-03 10:16:09 -07001299 if ((!opt_prof_accum && gctx->cnt_summed.curobjs == 0) ||
Jason Evans602c8e02014-08-18 16:22:13 -07001300 (opt_prof_accum && gctx->cnt_summed.accumobjs == 0)) {
1301 assert(gctx->cnt_summed.curobjs == 0);
1302 assert(gctx->cnt_summed.curbytes == 0);
1303 assert(gctx->cnt_summed.accumobjs == 0);
1304 assert(gctx->cnt_summed.accumbytes == 0);
Jason Evans4f37ef62014-01-16 13:23:56 -08001305 ret = false;
1306 goto label_return;
Jason Evansa881cd22010-10-02 15:18:50 -07001307 }
1308
Jason Evans602c8e02014-08-18 16:22:13 -07001309 if (prof_dump_printf(propagate_err, "@")) {
Jason Evans4f37ef62014-01-16 13:23:56 -08001310 ret = true;
1311 goto label_return;
Jason Evans6109fe02010-02-10 10:37:56 -08001312 }
Jason Evans4f37ef62014-01-16 13:23:56 -08001313 for (i = 0; i < bt->len; i++) {
1314 if (prof_dump_printf(propagate_err, " %#"PRIxPTR,
1315 (uintptr_t)bt->vec[i])) {
1316 ret = true;
1317 goto label_return;
1318 }
1319 }
Jason Evans22ca8552010-03-02 11:57:30 -08001320
Jason Evans602c8e02014-08-18 16:22:13 -07001321 if (prof_dump_printf(propagate_err,
1322 "\n"
1323 " t*: %"PRIu64": %"PRIu64" [%"PRIu64": %"PRIu64"]\n",
1324 gctx->cnt_summed.curobjs, gctx->cnt_summed.curbytes,
1325 gctx->cnt_summed.accumobjs, gctx->cnt_summed.accumbytes)) {
1326 ret = true;
1327 goto label_return;
1328 }
1329
1330 if (tctx_tree_iter(&gctx->tctxs, NULL, prof_tctx_dump_iter,
1331 (void *)&propagate_err) != NULL) {
Jason Evans4f37ef62014-01-16 13:23:56 -08001332 ret = true;
1333 goto label_return;
1334 }
1335
Jason Evans772163b2014-01-17 15:40:52 -08001336 ret = false;
Jason Evans4f37ef62014-01-16 13:23:56 -08001337label_return:
Jason Evans4f37ef62014-01-16 13:23:56 -08001338 return (ret);
Jason Evans6109fe02010-02-10 10:37:56 -08001339}
1340
Jason Evanse42c3092015-07-22 15:44:47 -07001341JEMALLOC_FORMAT_PRINTF(1, 2)
Jason Evans8e33c212015-05-01 09:03:20 -07001342static int
1343prof_open_maps(const char *format, ...)
1344{
1345 int mfd;
1346 va_list ap;
1347 char filename[PATH_MAX + 1];
1348
1349 va_start(ap, format);
1350 malloc_vsnprintf(filename, sizeof(filename), format, ap);
1351 va_end(ap);
1352 mfd = open(filename, O_RDONLY);
1353
1354 return (mfd);
1355}
1356
Jason Evans22ca8552010-03-02 11:57:30 -08001357static bool
1358prof_dump_maps(bool propagate_err)
Jason Evansc7177182010-02-11 09:25:56 -08001359{
Jason Evans93f39f82013-10-21 15:07:40 -07001360 bool ret;
Jason Evansc7177182010-02-11 09:25:56 -08001361 int mfd;
Jason Evansc7177182010-02-11 09:25:56 -08001362
Jason Evans7372b152012-02-10 20:22:09 -08001363 cassert(config_prof);
Harald Weppnerc2da2592014-03-18 00:00:14 -07001364#ifdef __FreeBSD__
Jason Evans8e33c212015-05-01 09:03:20 -07001365 mfd = prof_open_maps("/proc/curproc/map");
Harald Weppnerc2da2592014-03-18 00:00:14 -07001366#else
Jason Evans8e33c212015-05-01 09:03:20 -07001367 {
1368 int pid = getpid();
1369
1370 mfd = prof_open_maps("/proc/%d/task/%d/maps", pid, pid);
1371 if (mfd == -1)
1372 mfd = prof_open_maps("/proc/%d/maps", pid);
1373 }
Harald Weppnerc2da2592014-03-18 00:00:14 -07001374#endif
Jason Evansc7177182010-02-11 09:25:56 -08001375 if (mfd != -1) {
1376 ssize_t nread;
1377
Jason Evans4f37ef62014-01-16 13:23:56 -08001378 if (prof_dump_write(propagate_err, "\nMAPPED_LIBRARIES:\n") &&
Jason Evans93f39f82013-10-21 15:07:40 -07001379 propagate_err) {
1380 ret = true;
1381 goto label_return;
1382 }
Jason Evansc7177182010-02-11 09:25:56 -08001383 nread = 0;
1384 do {
1385 prof_dump_buf_end += nread;
Jason Evanscd9a1342012-03-21 18:33:03 -07001386 if (prof_dump_buf_end == PROF_DUMP_BUFSIZE) {
Jason Evansc7177182010-02-11 09:25:56 -08001387 /* Make space in prof_dump_buf before read(). */
Jason Evans4f37ef62014-01-16 13:23:56 -08001388 if (prof_dump_flush(propagate_err) &&
Jason Evans93f39f82013-10-21 15:07:40 -07001389 propagate_err) {
1390 ret = true;
1391 goto label_return;
1392 }
Jason Evansc7177182010-02-11 09:25:56 -08001393 }
1394 nread = read(mfd, &prof_dump_buf[prof_dump_buf_end],
Jason Evanscd9a1342012-03-21 18:33:03 -07001395 PROF_DUMP_BUFSIZE - prof_dump_buf_end);
Jason Evansc7177182010-02-11 09:25:56 -08001396 } while (nread > 0);
Jason Evans93f39f82013-10-21 15:07:40 -07001397 } else {
1398 ret = true;
1399 goto label_return;
1400 }
Jason Evans22ca8552010-03-02 11:57:30 -08001401
Jason Evans93f39f82013-10-21 15:07:40 -07001402 ret = false;
1403label_return:
1404 if (mfd != -1)
1405 close(mfd);
1406 return (ret);
Jason Evansc7177182010-02-11 09:25:56 -08001407}
1408
Jason Evans4f37ef62014-01-16 13:23:56 -08001409static void
Jason Evans602c8e02014-08-18 16:22:13 -07001410prof_leakcheck(const prof_cnt_t *cnt_all, size_t leak_ngctx,
Jason Evans4f37ef62014-01-16 13:23:56 -08001411 const char *filename)
1412{
1413
1414 if (cnt_all->curbytes != 0) {
Jason Evans602c8e02014-08-18 16:22:13 -07001415 malloc_printf("<jemalloc>: Leak summary: %"PRIu64" byte%s, %"
Jason Evans03136072015-07-07 13:12:05 -07001416 PRIu64" object%s, %"PRIzu" context%s\n",
Jason Evans4f37ef62014-01-16 13:23:56 -08001417 cnt_all->curbytes, (cnt_all->curbytes != 1) ? "s" : "",
1418 cnt_all->curobjs, (cnt_all->curobjs != 1) ? "s" : "",
Jason Evans602c8e02014-08-18 16:22:13 -07001419 leak_ngctx, (leak_ngctx != 1) ? "s" : "");
Jason Evans4f37ef62014-01-16 13:23:56 -08001420 malloc_printf(
Jason Evans70417202015-05-01 12:31:12 -07001421 "<jemalloc>: Run jeprof on \"%s\" for leak detail\n",
Jason Evans4f37ef62014-01-16 13:23:56 -08001422 filename);
1423 }
1424}
1425
Jason Evans602c8e02014-08-18 16:22:13 -07001426static prof_gctx_t *
1427prof_gctx_dump_iter(prof_gctx_tree_t *gctxs, prof_gctx_t *gctx, void *arg)
Jason Evans3a81cbd2014-08-16 12:58:55 -07001428{
Jason Evans602c8e02014-08-18 16:22:13 -07001429 prof_gctx_t *ret;
Jason Evans3a81cbd2014-08-16 12:58:55 -07001430 bool propagate_err = *(bool *)arg;
1431
Jason Evans602c8e02014-08-18 16:22:13 -07001432 malloc_mutex_lock(gctx->lock);
Jason Evans3a81cbd2014-08-16 12:58:55 -07001433
Jason Evans602c8e02014-08-18 16:22:13 -07001434 if (prof_dump_gctx(propagate_err, gctx, &gctx->bt, gctxs)) {
Jason Evans20c31de2014-10-02 23:01:10 -07001435 ret = gctx;
Jason Evans602c8e02014-08-18 16:22:13 -07001436 goto label_return;
1437 }
Jason Evans3a81cbd2014-08-16 12:58:55 -07001438
Jason Evans602c8e02014-08-18 16:22:13 -07001439 ret = NULL;
1440label_return:
1441 malloc_mutex_unlock(gctx->lock);
1442 return (ret);
Jason Evans3a81cbd2014-08-16 12:58:55 -07001443}
1444
Jason Evans22ca8552010-03-02 11:57:30 -08001445static bool
Jason Evans5460aa62014-09-22 21:09:23 -07001446prof_dump(tsd_t *tsd, bool propagate_err, const char *filename, bool leakcheck)
Jason Evans6109fe02010-02-10 10:37:56 -08001447{
Jason Evans602c8e02014-08-18 16:22:13 -07001448 prof_tdata_t *tdata;
Jason Evans6109fe02010-02-10 10:37:56 -08001449 prof_cnt_t cnt_all;
1450 size_t tabind;
Jason Evans075e77c2010-09-20 19:53:25 -07001451 union {
Jason Evans602c8e02014-08-18 16:22:13 -07001452 prof_gctx_t *p;
Jason Evans075e77c2010-09-20 19:53:25 -07001453 void *v;
Jason Evans602c8e02014-08-18 16:22:13 -07001454 } gctx;
1455 size_t leak_ngctx;
1456 prof_gctx_tree_t gctxs;
Jason Evans6109fe02010-02-10 10:37:56 -08001457
Jason Evans7372b152012-02-10 20:22:09 -08001458 cassert(config_prof);
1459
Jason Evans20c31de2014-10-02 23:01:10 -07001460 tdata = prof_tdata_get(tsd, true);
Jason Evans5460aa62014-09-22 21:09:23 -07001461 if (tdata == NULL)
Jason Evans52386b22012-04-22 16:00:11 -07001462 return (true);
Jason Evans4f37ef62014-01-16 13:23:56 -08001463
1464 malloc_mutex_lock(&prof_dump_mtx);
Jason Evansc93ed812014-10-30 16:50:33 -07001465 prof_enter(tsd, tdata);
Jason Evans6109fe02010-02-10 10:37:56 -08001466
Jason Evans602c8e02014-08-18 16:22:13 -07001467 /*
1468 * Put gctx's in limbo and clear their counters in preparation for
1469 * summing.
1470 */
1471 gctx_tree_new(&gctxs);
Jason Evans551ebc42014-10-03 10:16:09 -07001472 for (tabind = 0; !ckh_iter(&bt2gctx, &tabind, NULL, &gctx.v);)
Jason Evans602c8e02014-08-18 16:22:13 -07001473 prof_dump_gctx_prep(gctx.p, &gctxs);
1474
1475 /*
1476 * Iterate over tdatas, and for the non-expired ones snapshot their tctx
1477 * stats and merge them into the associated gctx's.
1478 */
Jason Evans6109fe02010-02-10 10:37:56 -08001479 memset(&cnt_all, 0, sizeof(prof_cnt_t));
Jason Evans602c8e02014-08-18 16:22:13 -07001480 malloc_mutex_lock(&tdatas_mtx);
1481 tdata_tree_iter(&tdatas, NULL, prof_tdata_merge_iter, (void *)&cnt_all);
1482 malloc_mutex_unlock(&tdatas_mtx);
1483
1484 /* Merge tctx stats into gctx's. */
1485 leak_ngctx = 0;
1486 gctx_tree_iter(&gctxs, NULL, prof_gctx_merge_iter, (void *)&leak_ngctx);
1487
Jason Evansc93ed812014-10-30 16:50:33 -07001488 prof_leave(tsd, tdata);
Jason Evans4f37ef62014-01-16 13:23:56 -08001489
1490 /* Create dump file. */
Jason Evans772163b2014-01-17 15:40:52 -08001491 if ((prof_dump_fd = prof_dump_open(propagate_err, filename)) == -1)
Jason Evans4f37ef62014-01-16 13:23:56 -08001492 goto label_open_close_error;
Jason Evans6109fe02010-02-10 10:37:56 -08001493
1494 /* Dump profile header. */
Jason Evans4f37ef62014-01-16 13:23:56 -08001495 if (prof_dump_header(propagate_err, &cnt_all))
1496 goto label_write_error;
Jason Evans6109fe02010-02-10 10:37:56 -08001497
Jason Evans602c8e02014-08-18 16:22:13 -07001498 /* Dump per gctx profile stats. */
1499 if (gctx_tree_iter(&gctxs, NULL, prof_gctx_dump_iter,
1500 (void *)&propagate_err) != NULL)
Jason Evans3a81cbd2014-08-16 12:58:55 -07001501 goto label_write_error;
Jason Evans6109fe02010-02-10 10:37:56 -08001502
Jason Evansc7177182010-02-11 09:25:56 -08001503 /* Dump /proc/<pid>/maps if possible. */
Jason Evans22ca8552010-03-02 11:57:30 -08001504 if (prof_dump_maps(propagate_err))
Jason Evans4f37ef62014-01-16 13:23:56 -08001505 goto label_write_error;
Jason Evansc7177182010-02-11 09:25:56 -08001506
Jason Evans4f37ef62014-01-16 13:23:56 -08001507 if (prof_dump_close(propagate_err))
1508 goto label_open_close_error;
Jason Evans6109fe02010-02-10 10:37:56 -08001509
Jason Evans20c31de2014-10-02 23:01:10 -07001510 prof_gctx_finish(tsd, &gctxs);
Jason Evans4f37ef62014-01-16 13:23:56 -08001511 malloc_mutex_unlock(&prof_dump_mtx);
1512
1513 if (leakcheck)
Jason Evans602c8e02014-08-18 16:22:13 -07001514 prof_leakcheck(&cnt_all, leak_ngctx, filename);
Jason Evans22ca8552010-03-02 11:57:30 -08001515
1516 return (false);
Jason Evans4f37ef62014-01-16 13:23:56 -08001517label_write_error:
1518 prof_dump_close(propagate_err);
1519label_open_close_error:
Jason Evans20c31de2014-10-02 23:01:10 -07001520 prof_gctx_finish(tsd, &gctxs);
Jason Evans4f37ef62014-01-16 13:23:56 -08001521 malloc_mutex_unlock(&prof_dump_mtx);
Jason Evans22ca8552010-03-02 11:57:30 -08001522 return (true);
Jason Evans6109fe02010-02-10 10:37:56 -08001523}
1524
Jason Evansd81e4bd2012-03-06 14:57:45 -08001525#define DUMP_FILENAME_BUFSIZE (PATH_MAX + 1)
Jason Evans4f37ef62014-01-16 13:23:56 -08001526#define VSEQ_INVALID UINT64_C(0xffffffffffffffff)
Jason Evans6109fe02010-02-10 10:37:56 -08001527static void
Chris Peterson3e310b32014-05-28 19:04:06 -07001528prof_dump_filename(char *filename, char v, uint64_t vseq)
Jason Evans6109fe02010-02-10 10:37:56 -08001529{
Jason Evans6109fe02010-02-10 10:37:56 -08001530
Jason Evans7372b152012-02-10 20:22:09 -08001531 cassert(config_prof);
1532
Jason Evans4f37ef62014-01-16 13:23:56 -08001533 if (vseq != VSEQ_INVALID) {
Jason Evansd81e4bd2012-03-06 14:57:45 -08001534 /* "<prefix>.<pid>.<seq>.v<vseq>.heap" */
1535 malloc_snprintf(filename, DUMP_FILENAME_BUFSIZE,
Chris Peterson3e310b32014-05-28 19:04:06 -07001536 "%s.%d.%"PRIu64".%c%"PRIu64".heap",
Jason Evansd81e4bd2012-03-06 14:57:45 -08001537 opt_prof_prefix, (int)getpid(), prof_dump_seq, v, vseq);
1538 } else {
1539 /* "<prefix>.<pid>.<seq>.<v>.heap" */
1540 malloc_snprintf(filename, DUMP_FILENAME_BUFSIZE,
1541 "%s.%d.%"PRIu64".%c.heap",
1542 opt_prof_prefix, (int)getpid(), prof_dump_seq, v);
Jason Evans6109fe02010-02-10 10:37:56 -08001543 }
Jason Evans52386b22012-04-22 16:00:11 -07001544 prof_dump_seq++;
Jason Evans6109fe02010-02-10 10:37:56 -08001545}
1546
1547static void
1548prof_fdump(void)
1549{
Jason Evans5460aa62014-09-22 21:09:23 -07001550 tsd_t *tsd;
Jason Evans6109fe02010-02-10 10:37:56 -08001551 char filename[DUMP_FILENAME_BUFSIZE];
1552
Jason Evans7372b152012-02-10 20:22:09 -08001553 cassert(config_prof);
Jason Evans57efa7b2014-10-08 17:57:19 -07001554 assert(opt_prof_final);
1555 assert(opt_prof_prefix[0] != '\0');
Jason Evans7372b152012-02-10 20:22:09 -08001556
Jason Evans551ebc42014-10-03 10:16:09 -07001557 if (!prof_booted)
Jason Evans6109fe02010-02-10 10:37:56 -08001558 return;
Jason Evans029d44c2014-10-04 11:12:53 -07001559 tsd = tsd_fetch();
Jason Evans6109fe02010-02-10 10:37:56 -08001560
Jason Evans57efa7b2014-10-08 17:57:19 -07001561 malloc_mutex_lock(&prof_dump_seq_mtx);
1562 prof_dump_filename(filename, 'f', VSEQ_INVALID);
1563 malloc_mutex_unlock(&prof_dump_seq_mtx);
1564 prof_dump(tsd, false, filename, opt_prof_leak);
Jason Evans6109fe02010-02-10 10:37:56 -08001565}
1566
1567void
1568prof_idump(void)
1569{
Jason Evans5460aa62014-09-22 21:09:23 -07001570 tsd_t *tsd;
Jason Evans602c8e02014-08-18 16:22:13 -07001571 prof_tdata_t *tdata;
Jason Evansd81e4bd2012-03-06 14:57:45 -08001572 char filename[PATH_MAX + 1];
Jason Evans6109fe02010-02-10 10:37:56 -08001573
Jason Evans7372b152012-02-10 20:22:09 -08001574 cassert(config_prof);
1575
Jason Evans551ebc42014-10-03 10:16:09 -07001576 if (!prof_booted)
Jason Evans6109fe02010-02-10 10:37:56 -08001577 return;
Jason Evans029d44c2014-10-04 11:12:53 -07001578 tsd = tsd_fetch();
Jason Evans5460aa62014-09-22 21:09:23 -07001579 tdata = prof_tdata_get(tsd, false);
1580 if (tdata == NULL)
Jason Evans52386b22012-04-22 16:00:11 -07001581 return;
Jason Evans602c8e02014-08-18 16:22:13 -07001582 if (tdata->enq) {
1583 tdata->enq_idump = true;
Jason Evansd34f9e72010-02-11 13:19:21 -08001584 return;
1585 }
Jason Evans6109fe02010-02-10 10:37:56 -08001586
Jason Evanse7339702010-10-23 18:37:06 -07001587 if (opt_prof_prefix[0] != '\0') {
1588 malloc_mutex_lock(&prof_dump_seq_mtx);
1589 prof_dump_filename(filename, 'i', prof_dump_iseq);
1590 prof_dump_iseq++;
1591 malloc_mutex_unlock(&prof_dump_seq_mtx);
Jason Evans5460aa62014-09-22 21:09:23 -07001592 prof_dump(tsd, false, filename, false);
Jason Evanse7339702010-10-23 18:37:06 -07001593 }
Jason Evans6109fe02010-02-10 10:37:56 -08001594}
1595
Jason Evans22ca8552010-03-02 11:57:30 -08001596bool
1597prof_mdump(const char *filename)
Jason Evans6109fe02010-02-10 10:37:56 -08001598{
Jason Evans5460aa62014-09-22 21:09:23 -07001599 tsd_t *tsd;
Jason Evans22ca8552010-03-02 11:57:30 -08001600 char filename_buf[DUMP_FILENAME_BUFSIZE];
Jason Evans6109fe02010-02-10 10:37:56 -08001601
Jason Evans7372b152012-02-10 20:22:09 -08001602 cassert(config_prof);
1603
Jason Evans551ebc42014-10-03 10:16:09 -07001604 if (!opt_prof || !prof_booted)
Jason Evans22ca8552010-03-02 11:57:30 -08001605 return (true);
Jason Evans029d44c2014-10-04 11:12:53 -07001606 tsd = tsd_fetch();
Jason Evans6109fe02010-02-10 10:37:56 -08001607
Jason Evans22ca8552010-03-02 11:57:30 -08001608 if (filename == NULL) {
1609 /* No filename specified, so automatically generate one. */
Jason Evanse7339702010-10-23 18:37:06 -07001610 if (opt_prof_prefix[0] == '\0')
1611 return (true);
Jason Evans22ca8552010-03-02 11:57:30 -08001612 malloc_mutex_lock(&prof_dump_seq_mtx);
1613 prof_dump_filename(filename_buf, 'm', prof_dump_mseq);
1614 prof_dump_mseq++;
1615 malloc_mutex_unlock(&prof_dump_seq_mtx);
1616 filename = filename_buf;
1617 }
Jason Evans5460aa62014-09-22 21:09:23 -07001618 return (prof_dump(tsd, true, filename, false));
Jason Evans6109fe02010-02-10 10:37:56 -08001619}
1620
1621void
Jason Evanse7339702010-10-23 18:37:06 -07001622prof_gdump(void)
Jason Evans6109fe02010-02-10 10:37:56 -08001623{
Jason Evans5460aa62014-09-22 21:09:23 -07001624 tsd_t *tsd;
Jason Evans602c8e02014-08-18 16:22:13 -07001625 prof_tdata_t *tdata;
Jason Evans6109fe02010-02-10 10:37:56 -08001626 char filename[DUMP_FILENAME_BUFSIZE];
1627
Jason Evans7372b152012-02-10 20:22:09 -08001628 cassert(config_prof);
1629
Jason Evans551ebc42014-10-03 10:16:09 -07001630 if (!prof_booted)
Jason Evans6109fe02010-02-10 10:37:56 -08001631 return;
Jason Evans029d44c2014-10-04 11:12:53 -07001632 tsd = tsd_fetch();
Jason Evans5460aa62014-09-22 21:09:23 -07001633 tdata = prof_tdata_get(tsd, false);
1634 if (tdata == NULL)
Jason Evans52386b22012-04-22 16:00:11 -07001635 return;
Jason Evans602c8e02014-08-18 16:22:13 -07001636 if (tdata->enq) {
1637 tdata->enq_gdump = true;
Jason Evans6109fe02010-02-10 10:37:56 -08001638 return;
1639 }
Jason Evans6109fe02010-02-10 10:37:56 -08001640
Jason Evanse7339702010-10-23 18:37:06 -07001641 if (opt_prof_prefix[0] != '\0') {
1642 malloc_mutex_lock(&prof_dump_seq_mtx);
1643 prof_dump_filename(filename, 'u', prof_dump_useq);
1644 prof_dump_useq++;
1645 malloc_mutex_unlock(&prof_dump_seq_mtx);
Jason Evans5460aa62014-09-22 21:09:23 -07001646 prof_dump(tsd, false, filename, false);
Jason Evanse7339702010-10-23 18:37:06 -07001647 }
Jason Evans6109fe02010-02-10 10:37:56 -08001648}
1649
1650static void
Jason Evansae03bf62013-01-22 12:02:08 -08001651prof_bt_hash(const void *key, size_t r_hash[2])
Jason Evans6109fe02010-02-10 10:37:56 -08001652{
Jason Evans6109fe02010-02-10 10:37:56 -08001653 prof_bt_t *bt = (prof_bt_t *)key;
1654
Jason Evans7372b152012-02-10 20:22:09 -08001655 cassert(config_prof);
Jason Evans6109fe02010-02-10 10:37:56 -08001656
Jason Evansae03bf62013-01-22 12:02:08 -08001657 hash(bt->vec, bt->len * sizeof(void *), 0x94122f33U, r_hash);
Jason Evans6109fe02010-02-10 10:37:56 -08001658}
1659
1660static bool
1661prof_bt_keycomp(const void *k1, const void *k2)
1662{
1663 const prof_bt_t *bt1 = (prof_bt_t *)k1;
1664 const prof_bt_t *bt2 = (prof_bt_t *)k2;
1665
Jason Evans7372b152012-02-10 20:22:09 -08001666 cassert(config_prof);
1667
Jason Evans6109fe02010-02-10 10:37:56 -08001668 if (bt1->len != bt2->len)
1669 return (false);
1670 return (memcmp(bt1->vec, bt2->vec, bt1->len * sizeof(void *)) == 0);
1671}
1672
Jason Evans602c8e02014-08-18 16:22:13 -07001673JEMALLOC_INLINE_C uint64_t
1674prof_thr_uid_alloc(void)
Jason Evans6109fe02010-02-10 10:37:56 -08001675{
Jason Evans9d8f3d22014-09-11 18:06:30 -07001676 uint64_t thr_uid;
Jason Evans602c8e02014-08-18 16:22:13 -07001677
Jason Evans9d8f3d22014-09-11 18:06:30 -07001678 malloc_mutex_lock(&next_thr_uid_mtx);
1679 thr_uid = next_thr_uid;
1680 next_thr_uid++;
1681 malloc_mutex_unlock(&next_thr_uid_mtx);
1682
1683 return (thr_uid);
Jason Evans602c8e02014-08-18 16:22:13 -07001684}
1685
1686static prof_tdata_t *
Jason Evansfc12c0b2014-10-03 23:25:30 -07001687prof_tdata_init_impl(tsd_t *tsd, uint64_t thr_uid, uint64_t thr_discrim,
1688 char *thread_name, bool active)
Jason Evans602c8e02014-08-18 16:22:13 -07001689{
1690 prof_tdata_t *tdata;
Jason Evans1cb181e2015-01-29 15:30:47 -08001691 tcache_t *tcache;
Jason Evans6109fe02010-02-10 10:37:56 -08001692
Jason Evans7372b152012-02-10 20:22:09 -08001693 cassert(config_prof);
1694
Jason Evans4d6a1342010-10-20 19:05:59 -07001695 /* Initialize an empty cache for this thread. */
Jason Evans1cb181e2015-01-29 15:30:47 -08001696 tcache = tcache_get(tsd, true);
Jason Evans4581b972014-11-27 17:22:36 -02001697 tdata = (prof_tdata_t *)iallocztm(tsd, sizeof(prof_tdata_t), false,
Jason Evans1cb181e2015-01-29 15:30:47 -08001698 tcache, true, NULL);
Jason Evans602c8e02014-08-18 16:22:13 -07001699 if (tdata == NULL)
Jason Evans4d6a1342010-10-20 19:05:59 -07001700 return (NULL);
1701
Jason Evans602c8e02014-08-18 16:22:13 -07001702 tdata->lock = prof_tdata_mutex_choose(thr_uid);
1703 tdata->thr_uid = thr_uid;
Jason Evans20c31de2014-10-02 23:01:10 -07001704 tdata->thr_discrim = thr_discrim;
Jason Evansfc12c0b2014-10-03 23:25:30 -07001705 tdata->thread_name = thread_name;
Jason Evans20c31de2014-10-02 23:01:10 -07001706 tdata->attached = true;
1707 tdata->expired = false;
Jason Evans04211e22015-03-16 15:11:06 -07001708 tdata->tctx_uid_next = 0;
Jason Evans602c8e02014-08-18 16:22:13 -07001709
Jason Evans5460aa62014-09-22 21:09:23 -07001710 if (ckh_new(tsd, &tdata->bt2tctx, PROF_CKH_MINITEMS,
Jason Evans4d6a1342010-10-20 19:05:59 -07001711 prof_bt_hash, prof_bt_keycomp)) {
Jason Evans1cb181e2015-01-29 15:30:47 -08001712 idalloctm(tsd, tdata, tcache, true);
Jason Evans4d6a1342010-10-20 19:05:59 -07001713 return (NULL);
1714 }
Jason Evans4d6a1342010-10-20 19:05:59 -07001715
Jason Evans602c8e02014-08-18 16:22:13 -07001716 tdata->prng_state = (uint64_t)(uintptr_t)tdata;
1717 prof_sample_threshold_update(tdata);
Jason Evans4d6a1342010-10-20 19:05:59 -07001718
Jason Evans602c8e02014-08-18 16:22:13 -07001719 tdata->enq = false;
1720 tdata->enq_idump = false;
1721 tdata->enq_gdump = false;
Jason Evans52386b22012-04-22 16:00:11 -07001722
Jason Evans602c8e02014-08-18 16:22:13 -07001723 tdata->dumping = false;
Jason Evansfc12c0b2014-10-03 23:25:30 -07001724 tdata->active = active;
Jason Evans4d6a1342010-10-20 19:05:59 -07001725
Jason Evans602c8e02014-08-18 16:22:13 -07001726 malloc_mutex_lock(&tdatas_mtx);
1727 tdata_tree_insert(&tdatas, tdata);
1728 malloc_mutex_unlock(&tdatas_mtx);
1729
1730 return (tdata);
1731}
1732
1733prof_tdata_t *
Jason Evans5460aa62014-09-22 21:09:23 -07001734prof_tdata_init(tsd_t *tsd)
Jason Evans602c8e02014-08-18 16:22:13 -07001735{
1736
Jason Evansfc12c0b2014-10-03 23:25:30 -07001737 return (prof_tdata_init_impl(tsd, prof_thr_uid_alloc(), 0, NULL,
1738 prof_thread_active_init_get()));
Jason Evans602c8e02014-08-18 16:22:13 -07001739}
1740
1741/* tdata->lock must be held. */
1742static bool
Jason Evansf04a0be2014-10-04 15:03:49 -07001743prof_tdata_should_destroy(prof_tdata_t *tdata, bool even_if_attached)
Jason Evans602c8e02014-08-18 16:22:13 -07001744{
1745
Jason Evansf04a0be2014-10-04 15:03:49 -07001746 if (tdata->attached && !even_if_attached)
Jason Evans602c8e02014-08-18 16:22:13 -07001747 return (false);
1748 if (ckh_count(&tdata->bt2tctx) != 0)
1749 return (false);
1750 return (true);
1751}
1752
Jason Evans20c31de2014-10-02 23:01:10 -07001753/* tdatas_mtx must be held. */
Jason Evans602c8e02014-08-18 16:22:13 -07001754static void
Jason Evansf04a0be2014-10-04 15:03:49 -07001755prof_tdata_destroy_locked(tsd_t *tsd, prof_tdata_t *tdata,
1756 bool even_if_attached)
Jason Evans602c8e02014-08-18 16:22:13 -07001757{
Jason Evans1cb181e2015-01-29 15:30:47 -08001758 tcache_t *tcache;
Jason Evans602c8e02014-08-18 16:22:13 -07001759
Jason Evansf04a0be2014-10-04 15:03:49 -07001760 assert(prof_tdata_should_destroy(tdata, even_if_attached));
Jason Evans029d44c2014-10-04 11:12:53 -07001761 assert(tsd_prof_tdata_get(tsd) != tdata);
Jason Evans602c8e02014-08-18 16:22:13 -07001762
Jason Evans602c8e02014-08-18 16:22:13 -07001763 tdata_tree_remove(&tdatas, tdata);
Jason Evans602c8e02014-08-18 16:22:13 -07001764
Jason Evans1cb181e2015-01-29 15:30:47 -08001765 tcache = tcache_get(tsd, false);
Jason Evans602c8e02014-08-18 16:22:13 -07001766 if (tdata->thread_name != NULL)
Jason Evans1cb181e2015-01-29 15:30:47 -08001767 idalloctm(tsd, tdata->thread_name, tcache, true);
Jason Evans5460aa62014-09-22 21:09:23 -07001768 ckh_delete(tsd, &tdata->bt2tctx);
Jason Evans1cb181e2015-01-29 15:30:47 -08001769 idalloctm(tsd, tdata, tcache, true);
Jason Evans602c8e02014-08-18 16:22:13 -07001770}
1771
1772static void
Jason Evansf04a0be2014-10-04 15:03:49 -07001773prof_tdata_destroy(tsd_t *tsd, prof_tdata_t *tdata, bool even_if_attached)
Jason Evans20c31de2014-10-02 23:01:10 -07001774{
1775
1776 malloc_mutex_lock(&tdatas_mtx);
Jason Evansf04a0be2014-10-04 15:03:49 -07001777 prof_tdata_destroy_locked(tsd, tdata, even_if_attached);
Jason Evans20c31de2014-10-02 23:01:10 -07001778 malloc_mutex_unlock(&tdatas_mtx);
1779}
1780
1781static void
1782prof_tdata_detach(tsd_t *tsd, prof_tdata_t *tdata)
Jason Evans602c8e02014-08-18 16:22:13 -07001783{
1784 bool destroy_tdata;
1785
1786 malloc_mutex_lock(tdata->lock);
Jason Evans20c31de2014-10-02 23:01:10 -07001787 if (tdata->attached) {
Jason Evansf04a0be2014-10-04 15:03:49 -07001788 destroy_tdata = prof_tdata_should_destroy(tdata, true);
1789 /*
1790 * Only detach if !destroy_tdata, because detaching would allow
1791 * another thread to win the race to destroy tdata.
1792 */
1793 if (!destroy_tdata)
1794 tdata->attached = false;
Jason Evans029d44c2014-10-04 11:12:53 -07001795 tsd_prof_tdata_set(tsd, NULL);
Jason Evans602c8e02014-08-18 16:22:13 -07001796 } else
1797 destroy_tdata = false;
1798 malloc_mutex_unlock(tdata->lock);
1799 if (destroy_tdata)
Jason Evansf04a0be2014-10-04 15:03:49 -07001800 prof_tdata_destroy(tsd, tdata, true);
Jason Evans602c8e02014-08-18 16:22:13 -07001801}
1802
Jason Evans20c31de2014-10-02 23:01:10 -07001803prof_tdata_t *
1804prof_tdata_reinit(tsd_t *tsd, prof_tdata_t *tdata)
Jason Evans602c8e02014-08-18 16:22:13 -07001805{
Jason Evans20c31de2014-10-02 23:01:10 -07001806 uint64_t thr_uid = tdata->thr_uid;
1807 uint64_t thr_discrim = tdata->thr_discrim + 1;
Jason Evansfc12c0b2014-10-03 23:25:30 -07001808 char *thread_name = (tdata->thread_name != NULL) ?
1809 prof_thread_name_alloc(tsd, tdata->thread_name) : NULL;
1810 bool active = tdata->active;
Jason Evans602c8e02014-08-18 16:22:13 -07001811
Jason Evans20c31de2014-10-02 23:01:10 -07001812 prof_tdata_detach(tsd, tdata);
Jason Evansfc12c0b2014-10-03 23:25:30 -07001813 return (prof_tdata_init_impl(tsd, thr_uid, thr_discrim, thread_name,
1814 active));
Jason Evans602c8e02014-08-18 16:22:13 -07001815}
1816
Jason Evans20c31de2014-10-02 23:01:10 -07001817static bool
1818prof_tdata_expire(prof_tdata_t *tdata)
Jason Evans602c8e02014-08-18 16:22:13 -07001819{
Jason Evans20c31de2014-10-02 23:01:10 -07001820 bool destroy_tdata;
Jason Evans602c8e02014-08-18 16:22:13 -07001821
Jason Evans20c31de2014-10-02 23:01:10 -07001822 malloc_mutex_lock(tdata->lock);
1823 if (!tdata->expired) {
1824 tdata->expired = true;
1825 destroy_tdata = tdata->attached ? false :
Jason Evansf04a0be2014-10-04 15:03:49 -07001826 prof_tdata_should_destroy(tdata, false);
Jason Evans20c31de2014-10-02 23:01:10 -07001827 } else
1828 destroy_tdata = false;
1829 malloc_mutex_unlock(tdata->lock);
1830
1831 return (destroy_tdata);
Jason Evans602c8e02014-08-18 16:22:13 -07001832}
1833
1834static prof_tdata_t *
1835prof_tdata_reset_iter(prof_tdata_tree_t *tdatas, prof_tdata_t *tdata, void *arg)
1836{
1837
Jason Evans20c31de2014-10-02 23:01:10 -07001838 return (prof_tdata_expire(tdata) ? tdata : NULL);
Jason Evans602c8e02014-08-18 16:22:13 -07001839}
1840
1841void
Jason Evans5460aa62014-09-22 21:09:23 -07001842prof_reset(tsd_t *tsd, size_t lg_sample)
Jason Evans602c8e02014-08-18 16:22:13 -07001843{
Jason Evans20c31de2014-10-02 23:01:10 -07001844 prof_tdata_t *next;
Jason Evans602c8e02014-08-18 16:22:13 -07001845
1846 assert(lg_sample < (sizeof(uint64_t) << 3));
1847
1848 malloc_mutex_lock(&prof_dump_mtx);
1849 malloc_mutex_lock(&tdatas_mtx);
1850
1851 lg_prof_sample = lg_sample;
Jason Evans20c31de2014-10-02 23:01:10 -07001852
1853 next = NULL;
1854 do {
1855 prof_tdata_t *to_destroy = tdata_tree_iter(&tdatas, next,
1856 prof_tdata_reset_iter, NULL);
1857 if (to_destroy != NULL) {
1858 next = tdata_tree_next(&tdatas, to_destroy);
Jason Evansf04a0be2014-10-04 15:03:49 -07001859 prof_tdata_destroy_locked(tsd, to_destroy, false);
Jason Evans20c31de2014-10-02 23:01:10 -07001860 } else
1861 next = NULL;
1862 } while (next != NULL);
Jason Evans602c8e02014-08-18 16:22:13 -07001863
1864 malloc_mutex_unlock(&tdatas_mtx);
1865 malloc_mutex_unlock(&prof_dump_mtx);
Jason Evans4d6a1342010-10-20 19:05:59 -07001866}
1867
Jason Evanscd9a1342012-03-21 18:33:03 -07001868void
Jason Evans5460aa62014-09-22 21:09:23 -07001869prof_tdata_cleanup(tsd_t *tsd)
Jason Evans4d6a1342010-10-20 19:05:59 -07001870{
Jason Evans5460aa62014-09-22 21:09:23 -07001871 prof_tdata_t *tdata;
Jason Evans4d6a1342010-10-20 19:05:59 -07001872
Jason Evans5460aa62014-09-22 21:09:23 -07001873 if (!config_prof)
1874 return;
Jason Evans7372b152012-02-10 20:22:09 -08001875
Jason Evans5460aa62014-09-22 21:09:23 -07001876 tdata = tsd_prof_tdata_get(tsd);
1877 if (tdata != NULL)
1878 prof_tdata_detach(tsd, tdata);
Jason Evans6109fe02010-02-10 10:37:56 -08001879}
1880
Jason Evansfc12c0b2014-10-03 23:25:30 -07001881bool
1882prof_active_get(void)
1883{
1884 bool prof_active_current;
1885
1886 malloc_mutex_lock(&prof_active_mtx);
1887 prof_active_current = prof_active;
1888 malloc_mutex_unlock(&prof_active_mtx);
1889 return (prof_active_current);
1890}
1891
1892bool
1893prof_active_set(bool active)
1894{
1895 bool prof_active_old;
1896
1897 malloc_mutex_lock(&prof_active_mtx);
1898 prof_active_old = prof_active;
1899 prof_active = active;
1900 malloc_mutex_unlock(&prof_active_mtx);
1901 return (prof_active_old);
1902}
1903
Jason Evans602c8e02014-08-18 16:22:13 -07001904const char *
1905prof_thread_name_get(void)
1906{
Jason Evans5460aa62014-09-22 21:09:23 -07001907 tsd_t *tsd;
1908 prof_tdata_t *tdata;
1909
Jason Evans029d44c2014-10-04 11:12:53 -07001910 tsd = tsd_fetch();
Jason Evans5460aa62014-09-22 21:09:23 -07001911 tdata = prof_tdata_get(tsd, true);
1912 if (tdata == NULL)
Jason Evansfc12c0b2014-10-03 23:25:30 -07001913 return ("");
1914 return (tdata->thread_name != NULL ? tdata->thread_name : "");
Jason Evans602c8e02014-08-18 16:22:13 -07001915}
1916
Jason Evansfc12c0b2014-10-03 23:25:30 -07001917static char *
1918prof_thread_name_alloc(tsd_t *tsd, const char *thread_name)
1919{
1920 char *ret;
1921 size_t size;
1922
1923 if (thread_name == NULL)
1924 return (NULL);
1925
1926 size = strlen(thread_name) + 1;
1927 if (size == 1)
1928 return ("");
1929
Jason Evans1cb181e2015-01-29 15:30:47 -08001930 ret = iallocztm(tsd, size, false, tcache_get(tsd, true), true, NULL);
Jason Evansfc12c0b2014-10-03 23:25:30 -07001931 if (ret == NULL)
1932 return (NULL);
1933 memcpy(ret, thread_name, size);
1934 return (ret);
1935}
1936
1937int
Jason Evans5460aa62014-09-22 21:09:23 -07001938prof_thread_name_set(tsd_t *tsd, const char *thread_name)
Jason Evans602c8e02014-08-18 16:22:13 -07001939{
1940 prof_tdata_t *tdata;
Jason Evansfc12c0b2014-10-03 23:25:30 -07001941 unsigned i;
Jason Evans602c8e02014-08-18 16:22:13 -07001942 char *s;
1943
Jason Evans5460aa62014-09-22 21:09:23 -07001944 tdata = prof_tdata_get(tsd, true);
1945 if (tdata == NULL)
Jason Evansfc12c0b2014-10-03 23:25:30 -07001946 return (EAGAIN);
Jason Evans602c8e02014-08-18 16:22:13 -07001947
Jason Evansfc12c0b2014-10-03 23:25:30 -07001948 /* Validate input. */
1949 if (thread_name == NULL)
1950 return (EFAULT);
1951 for (i = 0; thread_name[i] != '\0'; i++) {
1952 char c = thread_name[i];
1953 if (!isgraph(c) && !isblank(c))
1954 return (EFAULT);
1955 }
1956
1957 s = prof_thread_name_alloc(tsd, thread_name);
Jason Evans602c8e02014-08-18 16:22:13 -07001958 if (s == NULL)
Jason Evansfc12c0b2014-10-03 23:25:30 -07001959 return (EAGAIN);
Jason Evans602c8e02014-08-18 16:22:13 -07001960
Jason Evansfc12c0b2014-10-03 23:25:30 -07001961 if (tdata->thread_name != NULL) {
Jason Evans1cb181e2015-01-29 15:30:47 -08001962 idalloctm(tsd, tdata->thread_name, tcache_get(tsd, false),
1963 true);
Jason Evansfc12c0b2014-10-03 23:25:30 -07001964 tdata->thread_name = NULL;
1965 }
1966 if (strlen(s) > 0)
1967 tdata->thread_name = s;
1968 return (0);
Jason Evans602c8e02014-08-18 16:22:13 -07001969}
1970
1971bool
1972prof_thread_active_get(void)
1973{
Jason Evans5460aa62014-09-22 21:09:23 -07001974 tsd_t *tsd;
1975 prof_tdata_t *tdata;
1976
Jason Evans029d44c2014-10-04 11:12:53 -07001977 tsd = tsd_fetch();
Jason Evans5460aa62014-09-22 21:09:23 -07001978 tdata = prof_tdata_get(tsd, true);
1979 if (tdata == NULL)
Jason Evans602c8e02014-08-18 16:22:13 -07001980 return (false);
1981 return (tdata->active);
1982}
1983
1984bool
1985prof_thread_active_set(bool active)
1986{
Jason Evans5460aa62014-09-22 21:09:23 -07001987 tsd_t *tsd;
Jason Evans602c8e02014-08-18 16:22:13 -07001988 prof_tdata_t *tdata;
1989
Jason Evans029d44c2014-10-04 11:12:53 -07001990 tsd = tsd_fetch();
Jason Evans5460aa62014-09-22 21:09:23 -07001991 tdata = prof_tdata_get(tsd, true);
1992 if (tdata == NULL)
Jason Evans602c8e02014-08-18 16:22:13 -07001993 return (true);
1994 tdata->active = active;
1995 return (false);
1996}
1997
Jason Evansfc12c0b2014-10-03 23:25:30 -07001998bool
1999prof_thread_active_init_get(void)
2000{
2001 bool active_init;
2002
2003 malloc_mutex_lock(&prof_thread_active_init_mtx);
2004 active_init = prof_thread_active_init;
2005 malloc_mutex_unlock(&prof_thread_active_init_mtx);
2006 return (active_init);
2007}
2008
2009bool
2010prof_thread_active_init_set(bool active_init)
2011{
2012 bool active_init_old;
2013
2014 malloc_mutex_lock(&prof_thread_active_init_mtx);
2015 active_init_old = prof_thread_active_init;
2016 prof_thread_active_init = active_init;
2017 malloc_mutex_unlock(&prof_thread_active_init_mtx);
2018 return (active_init_old);
2019}
2020
Jason Evans5b8ed5b2015-01-25 21:16:57 -08002021bool
2022prof_gdump_get(void)
2023{
2024 bool prof_gdump_current;
2025
2026 malloc_mutex_lock(&prof_gdump_mtx);
2027 prof_gdump_current = prof_gdump_val;
2028 malloc_mutex_unlock(&prof_gdump_mtx);
2029 return (prof_gdump_current);
2030}
2031
2032bool
2033prof_gdump_set(bool gdump)
2034{
2035 bool prof_gdump_old;
2036
2037 malloc_mutex_lock(&prof_gdump_mtx);
2038 prof_gdump_old = prof_gdump_val;
2039 prof_gdump_val = gdump;
2040 malloc_mutex_unlock(&prof_gdump_mtx);
2041 return (prof_gdump_old);
2042}
2043
Jason Evans6109fe02010-02-10 10:37:56 -08002044void
2045prof_boot0(void)
2046{
2047
Jason Evans7372b152012-02-10 20:22:09 -08002048 cassert(config_prof);
2049
Jason Evanse7339702010-10-23 18:37:06 -07002050 memcpy(opt_prof_prefix, PROF_PREFIX_DEFAULT,
2051 sizeof(PROF_PREFIX_DEFAULT));
2052}
2053
2054void
2055prof_boot1(void)
2056{
2057
Jason Evans7372b152012-02-10 20:22:09 -08002058 cassert(config_prof);
2059
Jason Evans6109fe02010-02-10 10:37:56 -08002060 /*
Jason Evans9b0cbf02014-04-11 14:24:51 -07002061 * opt_prof must be in its final state before any arenas are
2062 * initialized, so this function must be executed early.
Jason Evans6109fe02010-02-10 10:37:56 -08002063 */
2064
Jason Evans551ebc42014-10-03 10:16:09 -07002065 if (opt_prof_leak && !opt_prof) {
Jason Evans6109fe02010-02-10 10:37:56 -08002066 /*
2067 * Enable opt_prof, but in such a way that profiles are never
2068 * automatically dumped.
2069 */
2070 opt_prof = true;
Jason Evanse7339702010-10-23 18:37:06 -07002071 opt_prof_gdump = false;
Jason Evansa02fc082010-03-31 17:35:51 -07002072 } else if (opt_prof) {
2073 if (opt_lg_prof_interval >= 0) {
2074 prof_interval = (((uint64_t)1U) <<
2075 opt_lg_prof_interval);
Jason Evansa3b33862012-11-13 12:56:27 -08002076 }
Jason Evansa02fc082010-03-31 17:35:51 -07002077 }
Jason Evans6109fe02010-02-10 10:37:56 -08002078}
2079
2080bool
Jason Evanse7339702010-10-23 18:37:06 -07002081prof_boot2(void)
Jason Evans6109fe02010-02-10 10:37:56 -08002082{
2083
Jason Evans7372b152012-02-10 20:22:09 -08002084 cassert(config_prof);
2085
Jason Evans6109fe02010-02-10 10:37:56 -08002086 if (opt_prof) {
Jason Evans5460aa62014-09-22 21:09:23 -07002087 tsd_t *tsd;
Jason Evans6da54182012-03-23 18:05:51 -07002088 unsigned i;
2089
Jason Evans602c8e02014-08-18 16:22:13 -07002090 lg_prof_sample = opt_lg_prof_sample;
2091
Jason Evansfc12c0b2014-10-03 23:25:30 -07002092 prof_active = opt_prof_active;
2093 if (malloc_mutex_init(&prof_active_mtx))
2094 return (true);
2095
Jason Evans5b8ed5b2015-01-25 21:16:57 -08002096 prof_gdump_val = opt_prof_gdump;
2097 if (malloc_mutex_init(&prof_gdump_mtx))
2098 return (true);
2099
Jason Evansfc12c0b2014-10-03 23:25:30 -07002100 prof_thread_active_init = opt_prof_thread_active_init;
2101 if (malloc_mutex_init(&prof_thread_active_init_mtx))
2102 return (true);
2103
Jason Evans029d44c2014-10-04 11:12:53 -07002104 tsd = tsd_fetch();
Jason Evans5460aa62014-09-22 21:09:23 -07002105 if (ckh_new(tsd, &bt2gctx, PROF_CKH_MINITEMS, prof_bt_hash,
Jason Evans6109fe02010-02-10 10:37:56 -08002106 prof_bt_keycomp))
2107 return (true);
Jason Evans602c8e02014-08-18 16:22:13 -07002108 if (malloc_mutex_init(&bt2gctx_mtx))
Jason Evans6109fe02010-02-10 10:37:56 -08002109 return (true);
Jason Evans6109fe02010-02-10 10:37:56 -08002110
Jason Evans602c8e02014-08-18 16:22:13 -07002111 tdata_tree_new(&tdatas);
2112 if (malloc_mutex_init(&tdatas_mtx))
2113 return (true);
2114
2115 next_thr_uid = 0;
Jason Evans9d8f3d22014-09-11 18:06:30 -07002116 if (malloc_mutex_init(&next_thr_uid_mtx))
2117 return (true);
Jason Evans602c8e02014-08-18 16:22:13 -07002118
Jason Evans6109fe02010-02-10 10:37:56 -08002119 if (malloc_mutex_init(&prof_dump_seq_mtx))
2120 return (true);
Jason Evans4f37ef62014-01-16 13:23:56 -08002121 if (malloc_mutex_init(&prof_dump_mtx))
2122 return (true);
Jason Evans6109fe02010-02-10 10:37:56 -08002123
Jason Evans57efa7b2014-10-08 17:57:19 -07002124 if (opt_prof_final && opt_prof_prefix[0] != '\0' &&
2125 atexit(prof_fdump) != 0) {
Jason Evans698805c2010-03-03 17:45:38 -08002126 malloc_write("<jemalloc>: Error in atexit()\n");
Jason Evans6109fe02010-02-10 10:37:56 -08002127 if (opt_abort)
2128 abort();
2129 }
Jason Evans6da54182012-03-23 18:05:51 -07002130
Jason Evans602c8e02014-08-18 16:22:13 -07002131 gctx_locks = (malloc_mutex_t *)base_alloc(PROF_NCTX_LOCKS *
Jason Evans6da54182012-03-23 18:05:51 -07002132 sizeof(malloc_mutex_t));
Jason Evans602c8e02014-08-18 16:22:13 -07002133 if (gctx_locks == NULL)
Jason Evans6da54182012-03-23 18:05:51 -07002134 return (true);
2135 for (i = 0; i < PROF_NCTX_LOCKS; i++) {
Jason Evans602c8e02014-08-18 16:22:13 -07002136 if (malloc_mutex_init(&gctx_locks[i]))
2137 return (true);
2138 }
2139
2140 tdata_locks = (malloc_mutex_t *)base_alloc(PROF_NTDATA_LOCKS *
2141 sizeof(malloc_mutex_t));
2142 if (tdata_locks == NULL)
2143 return (true);
2144 for (i = 0; i < PROF_NTDATA_LOCKS; i++) {
2145 if (malloc_mutex_init(&tdata_locks[i]))
Jason Evans6da54182012-03-23 18:05:51 -07002146 return (true);
2147 }
Jason Evans6109fe02010-02-10 10:37:56 -08002148 }
2149
Jason Evansb27805b2010-02-10 18:15:53 -08002150#ifdef JEMALLOC_PROF_LIBGCC
2151 /*
2152 * Cause the backtracing machinery to allocate its internal state
2153 * before enabling profiling.
2154 */
2155 _Unwind_Backtrace(prof_unwind_init_callback, NULL);
2156#endif
2157
Jason Evans6109fe02010-02-10 10:37:56 -08002158 prof_booted = true;
2159
2160 return (false);
2161}
2162
Jason Evans20f1fc92012-10-09 14:46:22 -07002163void
2164prof_prefork(void)
2165{
2166
2167 if (opt_prof) {
2168 unsigned i;
2169
Jason Evans9d8f3d22014-09-11 18:06:30 -07002170 malloc_mutex_prefork(&tdatas_mtx);
Jason Evans602c8e02014-08-18 16:22:13 -07002171 malloc_mutex_prefork(&bt2gctx_mtx);
Jason Evans9d8f3d22014-09-11 18:06:30 -07002172 malloc_mutex_prefork(&next_thr_uid_mtx);
Jason Evansf1c3da82013-10-21 14:59:10 -07002173 malloc_mutex_prefork(&prof_dump_seq_mtx);
Jason Evans20f1fc92012-10-09 14:46:22 -07002174 for (i = 0; i < PROF_NCTX_LOCKS; i++)
Jason Evans602c8e02014-08-18 16:22:13 -07002175 malloc_mutex_prefork(&gctx_locks[i]);
Jason Evans9d8f3d22014-09-11 18:06:30 -07002176 for (i = 0; i < PROF_NTDATA_LOCKS; i++)
2177 malloc_mutex_prefork(&tdata_locks[i]);
Jason Evans20f1fc92012-10-09 14:46:22 -07002178 }
2179}
2180
2181void
2182prof_postfork_parent(void)
2183{
2184
2185 if (opt_prof) {
2186 unsigned i;
2187
Jason Evans9d8f3d22014-09-11 18:06:30 -07002188 for (i = 0; i < PROF_NTDATA_LOCKS; i++)
2189 malloc_mutex_postfork_parent(&tdata_locks[i]);
Jason Evans20f1fc92012-10-09 14:46:22 -07002190 for (i = 0; i < PROF_NCTX_LOCKS; i++)
Jason Evans602c8e02014-08-18 16:22:13 -07002191 malloc_mutex_postfork_parent(&gctx_locks[i]);
Jason Evans20f1fc92012-10-09 14:46:22 -07002192 malloc_mutex_postfork_parent(&prof_dump_seq_mtx);
Jason Evans9d8f3d22014-09-11 18:06:30 -07002193 malloc_mutex_postfork_parent(&next_thr_uid_mtx);
Jason Evans602c8e02014-08-18 16:22:13 -07002194 malloc_mutex_postfork_parent(&bt2gctx_mtx);
Jason Evans9d8f3d22014-09-11 18:06:30 -07002195 malloc_mutex_postfork_parent(&tdatas_mtx);
Jason Evans20f1fc92012-10-09 14:46:22 -07002196 }
2197}
2198
2199void
2200prof_postfork_child(void)
2201{
2202
2203 if (opt_prof) {
2204 unsigned i;
2205
Jason Evans9d8f3d22014-09-11 18:06:30 -07002206 for (i = 0; i < PROF_NTDATA_LOCKS; i++)
2207 malloc_mutex_postfork_child(&tdata_locks[i]);
Jason Evans20f1fc92012-10-09 14:46:22 -07002208 for (i = 0; i < PROF_NCTX_LOCKS; i++)
Jason Evans602c8e02014-08-18 16:22:13 -07002209 malloc_mutex_postfork_child(&gctx_locks[i]);
Jason Evans20f1fc92012-10-09 14:46:22 -07002210 malloc_mutex_postfork_child(&prof_dump_seq_mtx);
Jason Evans9d8f3d22014-09-11 18:06:30 -07002211 malloc_mutex_postfork_child(&next_thr_uid_mtx);
Jason Evans602c8e02014-08-18 16:22:13 -07002212 malloc_mutex_postfork_child(&bt2gctx_mtx);
Jason Evans9d8f3d22014-09-11 18:06:30 -07002213 malloc_mutex_postfork_child(&tdatas_mtx);
Jason Evans20f1fc92012-10-09 14:46:22 -07002214 }
2215}
2216
Jason Evans6109fe02010-02-10 10:37:56 -08002217/******************************************************************************/