blob: d2532ec17dced229183a6dc76107e28ff59ce868 [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
Jason Evanscd9a1342012-03-21 18:33:03 -070017malloc_tsd_data(, prof_tdata, prof_tdata_t *, NULL)
18
Jason Evans6109fe02010-02-10 10:37:56 -080019bool opt_prof = false;
Jason Evansf18c9822010-03-31 18:43:24 -070020bool opt_prof_active = true;
Jason Evansb9477e72010-03-01 20:15:26 -080021size_t opt_lg_prof_sample = LG_PROF_SAMPLE_DEFAULT;
Jason Evansa02fc082010-03-31 17:35:51 -070022ssize_t opt_lg_prof_interval = LG_PROF_INTERVAL_DEFAULT;
Jason Evanse7339702010-10-23 18:37:06 -070023bool opt_prof_gdump = false;
Jason Evans6109fe02010-02-10 10:37:56 -080024bool opt_prof_leak = false;
Jason Evansa881cd22010-10-02 15:18:50 -070025bool opt_prof_accum = true;
Jason Evanse7339702010-10-23 18:37:06 -070026char opt_prof_prefix[PATH_MAX + 1];
Jason Evans6109fe02010-02-10 10:37:56 -080027
Jason Evansd34f9e72010-02-11 13:19:21 -080028uint64_t prof_interval;
Jason Evans0b270a92010-03-31 16:45:04 -070029bool prof_promote;
Jason Evansd34f9e72010-02-11 13:19:21 -080030
Jason Evans6109fe02010-02-10 10:37:56 -080031/*
Jason Evans6da54182012-03-23 18:05:51 -070032 * Table of mutexes that are shared among ctx's. These are leaf locks, so
33 * there is no problem with using them for more than one ctx at the same time.
34 * The primary motivation for this sharing though is that ctx's are ephemeral,
35 * and destroying mutexes causes complications for systems that allocate when
36 * creating/destroying mutexes.
37 */
38static malloc_mutex_t *ctx_locks;
39static unsigned cum_ctxs; /* Atomic counter. */
40
41/*
Jason Evans6109fe02010-02-10 10:37:56 -080042 * Global hash of (prof_bt_t *)-->(prof_ctx_t *). This is the master data
Jason Evansa881cd22010-10-02 15:18:50 -070043 * structure that knows about all backtraces currently captured.
Jason Evans6109fe02010-02-10 10:37:56 -080044 */
45static ckh_t bt2ctx;
46static malloc_mutex_t bt2ctx_mtx;
47
Jason Evans6109fe02010-02-10 10:37:56 -080048static malloc_mutex_t prof_dump_seq_mtx;
49static uint64_t prof_dump_seq;
50static uint64_t prof_dump_iseq;
51static uint64_t prof_dump_mseq;
52static uint64_t prof_dump_useq;
53
54/*
55 * This buffer is rather large for stack allocation, so use a single buffer for
56 * all profile dumps. The buffer is implicitly protected by bt2ctx_mtx, since
57 * it must be locked anyway during dumping.
58 */
Jason Evanscd9a1342012-03-21 18:33:03 -070059static char prof_dump_buf[PROF_DUMP_BUFSIZE];
Jason Evans6109fe02010-02-10 10:37:56 -080060static unsigned prof_dump_buf_end;
61static int prof_dump_fd;
62
63/* Do not dump any profiles until bootstrapping is complete. */
64static bool prof_booted = false;
65
66static malloc_mutex_t enq_mtx;
67static bool enq;
Jason Evansd34f9e72010-02-11 13:19:21 -080068static bool enq_idump;
Jason Evanse7339702010-10-23 18:37:06 -070069static bool enq_gdump;
Jason Evans6109fe02010-02-10 10:37:56 -080070
71/******************************************************************************/
72/* Function prototypes for non-inline static functions. */
73
74static prof_bt_t *bt_dup(prof_bt_t *bt);
Jason Evansa881cd22010-10-02 15:18:50 -070075static void bt_destroy(prof_bt_t *bt);
Jason Evansb27805b2010-02-10 18:15:53 -080076#ifdef JEMALLOC_PROF_LIBGCC
77static _Unwind_Reason_Code prof_unwind_init_callback(
78 struct _Unwind_Context *context, void *arg);
79static _Unwind_Reason_Code prof_unwind_callback(
80 struct _Unwind_Context *context, void *arg);
81#endif
Jason Evans22ca8552010-03-02 11:57:30 -080082static bool prof_flush(bool propagate_err);
Jason Evansd81e4bd2012-03-06 14:57:45 -080083static bool prof_write(bool propagate_err, const char *s);
84static bool prof_printf(bool propagate_err, const char *format, ...)
85 JEMALLOC_ATTR(format(printf, 2, 3));
Jason Evansa881cd22010-10-02 15:18:50 -070086static void prof_ctx_sum(prof_ctx_t *ctx, prof_cnt_t *cnt_all,
Jason Evans6109fe02010-02-10 10:37:56 -080087 size_t *leak_nctx);
Jason Evansa881cd22010-10-02 15:18:50 -070088static void prof_ctx_destroy(prof_ctx_t *ctx);
89static void prof_ctx_merge(prof_ctx_t *ctx, prof_thr_cnt_t *cnt);
Jason Evansd81e4bd2012-03-06 14:57:45 -080090static bool prof_dump_ctx(bool propagate_err, prof_ctx_t *ctx,
91 prof_bt_t *bt);
Jason Evans22ca8552010-03-02 11:57:30 -080092static bool prof_dump_maps(bool propagate_err);
Jason Evansd81e4bd2012-03-06 14:57:45 -080093static bool prof_dump(bool propagate_err, const char *filename,
94 bool leakcheck);
Jason Evans6109fe02010-02-10 10:37:56 -080095static void prof_dump_filename(char *filename, char v, int64_t vseq);
96static void prof_fdump(void);
97static void prof_bt_hash(const void *key, unsigned minbits, size_t *hash1,
98 size_t *hash2);
99static bool prof_bt_keycomp(const void *k1, const void *k2);
Jason Evans6da54182012-03-23 18:05:51 -0700100static malloc_mutex_t *prof_ctx_mutex_choose(void);
Jason Evans6109fe02010-02-10 10:37:56 -0800101
102/******************************************************************************/
103
Jason Evans4d6a1342010-10-20 19:05:59 -0700104void
Jason Evans6109fe02010-02-10 10:37:56 -0800105bt_init(prof_bt_t *bt, void **vec)
106{
107
Jason Evans7372b152012-02-10 20:22:09 -0800108 cassert(config_prof);
109
Jason Evans6109fe02010-02-10 10:37:56 -0800110 bt->vec = vec;
111 bt->len = 0;
112}
113
Jason Evansa881cd22010-10-02 15:18:50 -0700114static void
115bt_destroy(prof_bt_t *bt)
116{
117
Jason Evans7372b152012-02-10 20:22:09 -0800118 cassert(config_prof);
119
Jason Evansa881cd22010-10-02 15:18:50 -0700120 idalloc(bt);
121}
122
Jason Evans6109fe02010-02-10 10:37:56 -0800123static prof_bt_t *
124bt_dup(prof_bt_t *bt)
125{
126 prof_bt_t *ret;
127
Jason Evans7372b152012-02-10 20:22:09 -0800128 cassert(config_prof);
129
Jason Evans6109fe02010-02-10 10:37:56 -0800130 /*
131 * Create a single allocation that has space for vec immediately
132 * following the prof_bt_t structure. The backtraces that get
133 * stored in the backtrace caches are copied from stack-allocated
134 * temporary variables, so size is known at creation time. Making this
135 * a contiguous object improves cache locality.
136 */
137 ret = (prof_bt_t *)imalloc(QUANTUM_CEILING(sizeof(prof_bt_t)) +
138 (bt->len * sizeof(void *)));
139 if (ret == NULL)
140 return (NULL);
141 ret->vec = (void **)((uintptr_t)ret +
142 QUANTUM_CEILING(sizeof(prof_bt_t)));
143 memcpy(ret->vec, bt->vec, bt->len * sizeof(void *));
144 ret->len = bt->len;
145
146 return (ret);
147}
148
149static inline void
150prof_enter(void)
151{
152
Jason Evans7372b152012-02-10 20:22:09 -0800153 cassert(config_prof);
154
Jason Evans6109fe02010-02-10 10:37:56 -0800155 malloc_mutex_lock(&enq_mtx);
156 enq = true;
157 malloc_mutex_unlock(&enq_mtx);
158
159 malloc_mutex_lock(&bt2ctx_mtx);
160}
161
162static inline void
163prof_leave(void)
164{
Jason Evanse7339702010-10-23 18:37:06 -0700165 bool idump, gdump;
Jason Evans6109fe02010-02-10 10:37:56 -0800166
Jason Evans7372b152012-02-10 20:22:09 -0800167 cassert(config_prof);
168
Jason Evans6109fe02010-02-10 10:37:56 -0800169 malloc_mutex_unlock(&bt2ctx_mtx);
170
171 malloc_mutex_lock(&enq_mtx);
172 enq = false;
Jason Evansd34f9e72010-02-11 13:19:21 -0800173 idump = enq_idump;
174 enq_idump = false;
Jason Evanse7339702010-10-23 18:37:06 -0700175 gdump = enq_gdump;
176 enq_gdump = false;
Jason Evans6109fe02010-02-10 10:37:56 -0800177 malloc_mutex_unlock(&enq_mtx);
178
Jason Evansd34f9e72010-02-11 13:19:21 -0800179 if (idump)
180 prof_idump();
Jason Evanse7339702010-10-23 18:37:06 -0700181 if (gdump)
182 prof_gdump();
Jason Evans6109fe02010-02-10 10:37:56 -0800183}
184
Jason Evans77f350b2011-03-15 22:23:12 -0700185#ifdef JEMALLOC_PROF_LIBUNWIND
Jason Evans4d6a1342010-10-20 19:05:59 -0700186void
Jason Evans53891462012-02-13 18:23:41 -0800187prof_backtrace(prof_bt_t *bt, unsigned nignore)
Jason Evans6109fe02010-02-10 10:37:56 -0800188{
189 unw_context_t uc;
190 unw_cursor_t cursor;
191 unsigned i;
192 int err;
193
Jason Evans7372b152012-02-10 20:22:09 -0800194 cassert(config_prof);
Jason Evans6109fe02010-02-10 10:37:56 -0800195 assert(bt->len == 0);
196 assert(bt->vec != NULL);
Jason Evans6109fe02010-02-10 10:37:56 -0800197
198 unw_getcontext(&uc);
199 unw_init_local(&cursor, &uc);
200
Jason Evans9f949f92011-03-22 20:44:40 -0700201 /* Throw away (nignore+1) stack frames, if that many exist. */
202 for (i = 0; i < nignore + 1; i++) {
203 err = unw_step(&cursor);
204 if (err <= 0)
205 return;
Jason Evans6109fe02010-02-10 10:37:56 -0800206 }
207
Jason Evans9f949f92011-03-22 20:44:40 -0700208 /*
209 * Iterate over stack frames until there are no more, or until no space
210 * remains in bt.
211 */
Jason Evans53891462012-02-13 18:23:41 -0800212 for (i = 0; i < PROF_BT_MAX; i++) {
Jason Evans9f949f92011-03-22 20:44:40 -0700213 unw_get_reg(&cursor, UNW_REG_IP, (unw_word_t *)&bt->vec[i]);
214 bt->len++;
215 err = unw_step(&cursor);
216 if (err <= 0)
217 break;
Jason Evans6109fe02010-02-10 10:37:56 -0800218 }
Jason Evans6109fe02010-02-10 10:37:56 -0800219}
Jason Evans7372b152012-02-10 20:22:09 -0800220#elif (defined(JEMALLOC_PROF_LIBGCC))
Jason Evans77f350b2011-03-15 22:23:12 -0700221static _Unwind_Reason_Code
222prof_unwind_init_callback(struct _Unwind_Context *context, void *arg)
223{
224
Jason Evans7372b152012-02-10 20:22:09 -0800225 cassert(config_prof);
226
Jason Evans77f350b2011-03-15 22:23:12 -0700227 return (_URC_NO_REASON);
228}
229
230static _Unwind_Reason_Code
231prof_unwind_callback(struct _Unwind_Context *context, void *arg)
232{
233 prof_unwind_data_t *data = (prof_unwind_data_t *)arg;
234
Jason Evans7372b152012-02-10 20:22:09 -0800235 cassert(config_prof);
236
Jason Evans77f350b2011-03-15 22:23:12 -0700237 if (data->nignore > 0)
238 data->nignore--;
239 else {
240 data->bt->vec[data->bt->len] = (void *)_Unwind_GetIP(context);
241 data->bt->len++;
242 if (data->bt->len == data->max)
243 return (_URC_END_OF_STACK);
244 }
245
246 return (_URC_NO_REASON);
247}
248
249void
Jason Evans53891462012-02-13 18:23:41 -0800250prof_backtrace(prof_bt_t *bt, unsigned nignore)
Jason Evans77f350b2011-03-15 22:23:12 -0700251{
Jason Evans53891462012-02-13 18:23:41 -0800252 prof_unwind_data_t data = {bt, nignore, PROF_BT_MAX};
Jason Evans77f350b2011-03-15 22:23:12 -0700253
Jason Evans7372b152012-02-10 20:22:09 -0800254 cassert(config_prof);
255
Jason Evans77f350b2011-03-15 22:23:12 -0700256 _Unwind_Backtrace(prof_unwind_callback, &data);
257}
Jason Evans7372b152012-02-10 20:22:09 -0800258#elif (defined(JEMALLOC_PROF_GCC))
Jason Evans4d6a1342010-10-20 19:05:59 -0700259void
Jason Evans53891462012-02-13 18:23:41 -0800260prof_backtrace(prof_bt_t *bt, unsigned nignore)
Jason Evans6109fe02010-02-10 10:37:56 -0800261{
Jason Evans6109fe02010-02-10 10:37:56 -0800262#define BT_FRAME(i) \
Jason Evans53891462012-02-13 18:23:41 -0800263 if ((i) < nignore + PROF_BT_MAX) { \
Jason Evans6109fe02010-02-10 10:37:56 -0800264 void *p; \
265 if (__builtin_frame_address(i) == 0) \
Jason Evansb27805b2010-02-10 18:15:53 -0800266 return; \
Jason Evans6109fe02010-02-10 10:37:56 -0800267 p = __builtin_return_address(i); \
268 if (p == NULL) \
Jason Evansb27805b2010-02-10 18:15:53 -0800269 return; \
Jason Evanse4f78462010-10-22 10:45:59 -0700270 if (i >= nignore) { \
271 bt->vec[(i) - nignore] = p; \
272 bt->len = (i) - nignore + 1; \
Jason Evans6109fe02010-02-10 10:37:56 -0800273 } \
274 } else \
Jason Evansb27805b2010-02-10 18:15:53 -0800275 return;
Jason Evans6109fe02010-02-10 10:37:56 -0800276
Jason Evans7372b152012-02-10 20:22:09 -0800277 cassert(config_prof);
Jason Evansb04a9402010-10-27 19:47:40 -0700278 assert(nignore <= 3);
Jason Evans6109fe02010-02-10 10:37:56 -0800279
Jason Evans6109fe02010-02-10 10:37:56 -0800280 BT_FRAME(0)
281 BT_FRAME(1)
282 BT_FRAME(2)
Jason Evans6109fe02010-02-10 10:37:56 -0800283 BT_FRAME(3)
284 BT_FRAME(4)
285 BT_FRAME(5)
286 BT_FRAME(6)
287 BT_FRAME(7)
288 BT_FRAME(8)
289 BT_FRAME(9)
290
291 BT_FRAME(10)
292 BT_FRAME(11)
293 BT_FRAME(12)
294 BT_FRAME(13)
295 BT_FRAME(14)
296 BT_FRAME(15)
297 BT_FRAME(16)
298 BT_FRAME(17)
299 BT_FRAME(18)
300 BT_FRAME(19)
301
302 BT_FRAME(20)
303 BT_FRAME(21)
304 BT_FRAME(22)
305 BT_FRAME(23)
306 BT_FRAME(24)
307 BT_FRAME(25)
308 BT_FRAME(26)
309 BT_FRAME(27)
310 BT_FRAME(28)
311 BT_FRAME(29)
312
313 BT_FRAME(30)
314 BT_FRAME(31)
315 BT_FRAME(32)
316 BT_FRAME(33)
317 BT_FRAME(34)
318 BT_FRAME(35)
319 BT_FRAME(36)
320 BT_FRAME(37)
321 BT_FRAME(38)
322 BT_FRAME(39)
323
324 BT_FRAME(40)
325 BT_FRAME(41)
326 BT_FRAME(42)
327 BT_FRAME(43)
328 BT_FRAME(44)
329 BT_FRAME(45)
330 BT_FRAME(46)
331 BT_FRAME(47)
332 BT_FRAME(48)
333 BT_FRAME(49)
334
335 BT_FRAME(50)
336 BT_FRAME(51)
337 BT_FRAME(52)
338 BT_FRAME(53)
339 BT_FRAME(54)
340 BT_FRAME(55)
341 BT_FRAME(56)
342 BT_FRAME(57)
343 BT_FRAME(58)
344 BT_FRAME(59)
345
346 BT_FRAME(60)
347 BT_FRAME(61)
348 BT_FRAME(62)
349 BT_FRAME(63)
350 BT_FRAME(64)
351 BT_FRAME(65)
352 BT_FRAME(66)
353 BT_FRAME(67)
354 BT_FRAME(68)
355 BT_FRAME(69)
356
357 BT_FRAME(70)
358 BT_FRAME(71)
359 BT_FRAME(72)
360 BT_FRAME(73)
361 BT_FRAME(74)
362 BT_FRAME(75)
363 BT_FRAME(76)
364 BT_FRAME(77)
365 BT_FRAME(78)
366 BT_FRAME(79)
367
368 BT_FRAME(80)
369 BT_FRAME(81)
370 BT_FRAME(82)
371 BT_FRAME(83)
372 BT_FRAME(84)
373 BT_FRAME(85)
374 BT_FRAME(86)
375 BT_FRAME(87)
376 BT_FRAME(88)
377 BT_FRAME(89)
378
379 BT_FRAME(90)
380 BT_FRAME(91)
381 BT_FRAME(92)
382 BT_FRAME(93)
383 BT_FRAME(94)
384 BT_FRAME(95)
385 BT_FRAME(96)
386 BT_FRAME(97)
387 BT_FRAME(98)
388 BT_FRAME(99)
389
390 BT_FRAME(100)
391 BT_FRAME(101)
392 BT_FRAME(102)
393 BT_FRAME(103)
394 BT_FRAME(104)
395 BT_FRAME(105)
396 BT_FRAME(106)
397 BT_FRAME(107)
398 BT_FRAME(108)
399 BT_FRAME(109)
400
401 BT_FRAME(110)
402 BT_FRAME(111)
403 BT_FRAME(112)
404 BT_FRAME(113)
405 BT_FRAME(114)
406 BT_FRAME(115)
407 BT_FRAME(116)
408 BT_FRAME(117)
409 BT_FRAME(118)
410 BT_FRAME(119)
411
412 BT_FRAME(120)
413 BT_FRAME(121)
414 BT_FRAME(122)
415 BT_FRAME(123)
416 BT_FRAME(124)
417 BT_FRAME(125)
418 BT_FRAME(126)
419 BT_FRAME(127)
420
Jason Evansb04a9402010-10-27 19:47:40 -0700421 /* Extras to compensate for nignore. */
Jason Evans6109fe02010-02-10 10:37:56 -0800422 BT_FRAME(128)
423 BT_FRAME(129)
424 BT_FRAME(130)
Jason Evans6109fe02010-02-10 10:37:56 -0800425#undef BT_FRAME
Jason Evans6109fe02010-02-10 10:37:56 -0800426}
Jason Evans7372b152012-02-10 20:22:09 -0800427#else
428void
Jason Evans53891462012-02-13 18:23:41 -0800429prof_backtrace(prof_bt_t *bt, unsigned nignore)
Jason Evans7372b152012-02-10 20:22:09 -0800430{
431
432 cassert(config_prof);
433 assert(false);
434}
Jason Evans6109fe02010-02-10 10:37:56 -0800435#endif
436
Jason Evans4d6a1342010-10-20 19:05:59 -0700437prof_thr_cnt_t *
Jason Evans6109fe02010-02-10 10:37:56 -0800438prof_lookup(prof_bt_t *bt)
439{
Jason Evans075e77c2010-09-20 19:53:25 -0700440 union {
441 prof_thr_cnt_t *p;
442 void *v;
443 } ret;
Jason Evans4d6a1342010-10-20 19:05:59 -0700444 prof_tdata_t *prof_tdata;
Jason Evans6109fe02010-02-10 10:37:56 -0800445
Jason Evans7372b152012-02-10 20:22:09 -0800446 cassert(config_prof);
447
Jason Evanscd9a1342012-03-21 18:33:03 -0700448 prof_tdata = *prof_tdata_tsd_get();
Jason Evans4d6a1342010-10-20 19:05:59 -0700449 if (prof_tdata == NULL) {
450 prof_tdata = prof_tdata_init();
451 if (prof_tdata == NULL)
Jason Evans6109fe02010-02-10 10:37:56 -0800452 return (NULL);
Jason Evans6109fe02010-02-10 10:37:56 -0800453 }
454
Jason Evans4d6a1342010-10-20 19:05:59 -0700455 if (ckh_search(&prof_tdata->bt2cnt, bt, NULL, &ret.v)) {
Jason Evans075e77c2010-09-20 19:53:25 -0700456 union {
457 prof_bt_t *p;
458 void *v;
459 } btkey;
460 union {
461 prof_ctx_t *p;
462 void *v;
463 } ctx;
Jason Evans10e45232011-01-14 17:27:44 -0800464 bool new_ctx;
Jason Evans6109fe02010-02-10 10:37:56 -0800465
466 /*
467 * This thread's cache lacks bt. Look for it in the global
468 * cache.
469 */
470 prof_enter();
Jason Evans075e77c2010-09-20 19:53:25 -0700471 if (ckh_search(&bt2ctx, bt, &btkey.v, &ctx.v)) {
Jason Evans6109fe02010-02-10 10:37:56 -0800472 /* bt has never been seen before. Insert it. */
Jason Evans075e77c2010-09-20 19:53:25 -0700473 ctx.v = imalloc(sizeof(prof_ctx_t));
474 if (ctx.v == NULL) {
Jason Evans6109fe02010-02-10 10:37:56 -0800475 prof_leave();
476 return (NULL);
477 }
Jason Evans075e77c2010-09-20 19:53:25 -0700478 btkey.p = bt_dup(bt);
479 if (btkey.v == NULL) {
Jason Evans6109fe02010-02-10 10:37:56 -0800480 prof_leave();
Jason Evans075e77c2010-09-20 19:53:25 -0700481 idalloc(ctx.v);
Jason Evans6109fe02010-02-10 10:37:56 -0800482 return (NULL);
483 }
Jason Evans075e77c2010-09-20 19:53:25 -0700484 ctx.p->bt = btkey.p;
Jason Evans6da54182012-03-23 18:05:51 -0700485 ctx.p->lock = prof_ctx_mutex_choose();
Jason Evans075e77c2010-09-20 19:53:25 -0700486 memset(&ctx.p->cnt_merged, 0, sizeof(prof_cnt_t));
487 ql_new(&ctx.p->cnts_ql);
488 if (ckh_insert(&bt2ctx, btkey.v, ctx.v)) {
Jason Evans6109fe02010-02-10 10:37:56 -0800489 /* OOM. */
490 prof_leave();
Jason Evans075e77c2010-09-20 19:53:25 -0700491 idalloc(btkey.v);
492 idalloc(ctx.v);
Jason Evans6109fe02010-02-10 10:37:56 -0800493 return (NULL);
494 }
Jason Evans10e45232011-01-14 17:27:44 -0800495 /*
496 * Artificially raise curobjs, in order to avoid a race
497 * condition with prof_ctx_merge()/prof_ctx_destroy().
Jason Evansa9076c92011-08-30 23:40:11 -0700498 *
499 * No locking is necessary for ctx here because no other
500 * threads have had the opportunity to fetch it from
501 * bt2ctx yet.
Jason Evans10e45232011-01-14 17:27:44 -0800502 */
503 ctx.p->cnt_merged.curobjs++;
504 new_ctx = true;
Jason Evansa9076c92011-08-30 23:40:11 -0700505 } else {
506 /*
507 * Artificially raise curobjs, in order to avoid a race
508 * condition with prof_ctx_merge()/prof_ctx_destroy().
509 */
Jason Evans6da54182012-03-23 18:05:51 -0700510 malloc_mutex_lock(ctx.p->lock);
Jason Evansa9076c92011-08-30 23:40:11 -0700511 ctx.p->cnt_merged.curobjs++;
Jason Evans6da54182012-03-23 18:05:51 -0700512 malloc_mutex_unlock(ctx.p->lock);
Jason Evans10e45232011-01-14 17:27:44 -0800513 new_ctx = false;
Jason Evansa9076c92011-08-30 23:40:11 -0700514 }
Jason Evans6109fe02010-02-10 10:37:56 -0800515 prof_leave();
516
517 /* Link a prof_thd_cnt_t into ctx for this thread. */
Jason Evans0b526ff2012-02-13 18:04:26 -0800518 if (ckh_count(&prof_tdata->bt2cnt) == PROF_TCMAX) {
Jason Evans4d6a1342010-10-20 19:05:59 -0700519 assert(ckh_count(&prof_tdata->bt2cnt) > 0);
Jason Evansa881cd22010-10-02 15:18:50 -0700520 /*
Jason Evanse4f78462010-10-22 10:45:59 -0700521 * Flush the least recently used cnt in order to keep
522 * bt2cnt from becoming too large.
Jason Evansa881cd22010-10-02 15:18:50 -0700523 */
Jason Evans4d6a1342010-10-20 19:05:59 -0700524 ret.p = ql_last(&prof_tdata->lru_ql, lru_link);
Jason Evansa881cd22010-10-02 15:18:50 -0700525 assert(ret.v != NULL);
Jason Evansa9076c92011-08-30 23:40:11 -0700526 if (ckh_remove(&prof_tdata->bt2cnt, ret.p->ctx->bt,
527 NULL, NULL))
528 assert(false);
Jason Evans4d6a1342010-10-20 19:05:59 -0700529 ql_remove(&prof_tdata->lru_ql, ret.p, lru_link);
Jason Evansa881cd22010-10-02 15:18:50 -0700530 prof_ctx_merge(ret.p->ctx, ret.p);
531 /* ret can now be re-used. */
532 } else {
Jason Evans0b526ff2012-02-13 18:04:26 -0800533 assert(ckh_count(&prof_tdata->bt2cnt) < PROF_TCMAX);
Jason Evansa881cd22010-10-02 15:18:50 -0700534 /* Allocate and partially initialize a new cnt. */
535 ret.v = imalloc(sizeof(prof_thr_cnt_t));
Jason Evansb04a9402010-10-27 19:47:40 -0700536 if (ret.p == NULL) {
Jason Evans0cdd42e2011-08-09 19:06:06 -0700537 if (new_ctx)
538 prof_ctx_destroy(ctx.p);
Jason Evansa881cd22010-10-02 15:18:50 -0700539 return (NULL);
Jason Evansb04a9402010-10-27 19:47:40 -0700540 }
Jason Evansa881cd22010-10-02 15:18:50 -0700541 ql_elm_new(ret.p, cnts_link);
542 ql_elm_new(ret.p, lru_link);
543 }
544 /* Finish initializing ret. */
Jason Evans075e77c2010-09-20 19:53:25 -0700545 ret.p->ctx = ctx.p;
546 ret.p->epoch = 0;
547 memset(&ret.p->cnts, 0, sizeof(prof_cnt_t));
Jason Evans4d6a1342010-10-20 19:05:59 -0700548 if (ckh_insert(&prof_tdata->bt2cnt, btkey.v, ret.v)) {
Jason Evans0cdd42e2011-08-09 19:06:06 -0700549 if (new_ctx)
550 prof_ctx_destroy(ctx.p);
Jason Evans075e77c2010-09-20 19:53:25 -0700551 idalloc(ret.v);
Jason Evans6109fe02010-02-10 10:37:56 -0800552 return (NULL);
553 }
Jason Evans4d6a1342010-10-20 19:05:59 -0700554 ql_head_insert(&prof_tdata->lru_ql, ret.p, lru_link);
Jason Evans6da54182012-03-23 18:05:51 -0700555 malloc_mutex_lock(ctx.p->lock);
Jason Evansa881cd22010-10-02 15:18:50 -0700556 ql_tail_insert(&ctx.p->cnts_ql, ret.p, cnts_link);
Jason Evansa9076c92011-08-30 23:40:11 -0700557 ctx.p->cnt_merged.curobjs--;
Jason Evans6da54182012-03-23 18:05:51 -0700558 malloc_mutex_unlock(ctx.p->lock);
Jason Evansa881cd22010-10-02 15:18:50 -0700559 } else {
560 /* Move ret to the front of the LRU. */
Jason Evans4d6a1342010-10-20 19:05:59 -0700561 ql_remove(&prof_tdata->lru_ql, ret.p, lru_link);
562 ql_head_insert(&prof_tdata->lru_ql, ret.p, lru_link);
Jason Evans6109fe02010-02-10 10:37:56 -0800563 }
564
Jason Evans075e77c2010-09-20 19:53:25 -0700565 return (ret.p);
Jason Evans6109fe02010-02-10 10:37:56 -0800566}
567
Jason Evans22ca8552010-03-02 11:57:30 -0800568static bool
569prof_flush(bool propagate_err)
Jason Evans6109fe02010-02-10 10:37:56 -0800570{
Jason Evans22ca8552010-03-02 11:57:30 -0800571 bool ret = false;
Jason Evans6109fe02010-02-10 10:37:56 -0800572 ssize_t err;
573
Jason Evans7372b152012-02-10 20:22:09 -0800574 cassert(config_prof);
575
Jason Evans6109fe02010-02-10 10:37:56 -0800576 err = write(prof_dump_fd, prof_dump_buf, prof_dump_buf_end);
577 if (err == -1) {
Jason Evans22ca8552010-03-02 11:57:30 -0800578 if (propagate_err == false) {
Jason Evans698805c2010-03-03 17:45:38 -0800579 malloc_write("<jemalloc>: write() failed during heap "
580 "profile flush\n");
Jason Evans22ca8552010-03-02 11:57:30 -0800581 if (opt_abort)
582 abort();
583 }
584 ret = true;
Jason Evans6109fe02010-02-10 10:37:56 -0800585 }
586 prof_dump_buf_end = 0;
Jason Evans22ca8552010-03-02 11:57:30 -0800587
588 return (ret);
Jason Evans6109fe02010-02-10 10:37:56 -0800589}
590
Jason Evans22ca8552010-03-02 11:57:30 -0800591static bool
Jason Evansd81e4bd2012-03-06 14:57:45 -0800592prof_write(bool propagate_err, const char *s)
Jason Evans6109fe02010-02-10 10:37:56 -0800593{
594 unsigned i, slen, n;
595
Jason Evans7372b152012-02-10 20:22:09 -0800596 cassert(config_prof);
597
Jason Evans6109fe02010-02-10 10:37:56 -0800598 i = 0;
599 slen = strlen(s);
600 while (i < slen) {
601 /* Flush the buffer if it is full. */
Jason Evanscd9a1342012-03-21 18:33:03 -0700602 if (prof_dump_buf_end == PROF_DUMP_BUFSIZE)
Jason Evans22ca8552010-03-02 11:57:30 -0800603 if (prof_flush(propagate_err) && propagate_err)
604 return (true);
Jason Evans6109fe02010-02-10 10:37:56 -0800605
Jason Evanscd9a1342012-03-21 18:33:03 -0700606 if (prof_dump_buf_end + slen <= PROF_DUMP_BUFSIZE) {
Jason Evans6109fe02010-02-10 10:37:56 -0800607 /* Finish writing. */
608 n = slen - i;
609 } else {
610 /* Write as much of s as will fit. */
Jason Evanscd9a1342012-03-21 18:33:03 -0700611 n = PROF_DUMP_BUFSIZE - prof_dump_buf_end;
Jason Evans6109fe02010-02-10 10:37:56 -0800612 }
613 memcpy(&prof_dump_buf[prof_dump_buf_end], &s[i], n);
614 prof_dump_buf_end += n;
615 i += n;
616 }
Jason Evans22ca8552010-03-02 11:57:30 -0800617
618 return (false);
Jason Evans6109fe02010-02-10 10:37:56 -0800619}
620
Jason Evansd81e4bd2012-03-06 14:57:45 -0800621JEMALLOC_ATTR(format(printf, 2, 3))
622static bool
623prof_printf(bool propagate_err, const char *format, ...)
624{
625 bool ret;
626 va_list ap;
Jason Evanscd9a1342012-03-21 18:33:03 -0700627 char buf[PROF_PRINTF_BUFSIZE];
Jason Evansd81e4bd2012-03-06 14:57:45 -0800628
629 va_start(ap, format);
Jason Evans6da54182012-03-23 18:05:51 -0700630 malloc_vsnprintf(buf, sizeof(buf), format, ap);
Jason Evansd81e4bd2012-03-06 14:57:45 -0800631 va_end(ap);
Jason Evanscd9a1342012-03-21 18:33:03 -0700632 ret = prof_write(propagate_err, buf);
Jason Evansd81e4bd2012-03-06 14:57:45 -0800633
634 return (ret);
635}
636
Jason Evans6109fe02010-02-10 10:37:56 -0800637static void
Jason Evansa881cd22010-10-02 15:18:50 -0700638prof_ctx_sum(prof_ctx_t *ctx, prof_cnt_t *cnt_all, size_t *leak_nctx)
Jason Evans6109fe02010-02-10 10:37:56 -0800639{
640 prof_thr_cnt_t *thr_cnt;
641 prof_cnt_t tcnt;
642
Jason Evans7372b152012-02-10 20:22:09 -0800643 cassert(config_prof);
644
Jason Evans6da54182012-03-23 18:05:51 -0700645 malloc_mutex_lock(ctx->lock);
Jason Evans6109fe02010-02-10 10:37:56 -0800646
Jason Evansa881cd22010-10-02 15:18:50 -0700647 memcpy(&ctx->cnt_summed, &ctx->cnt_merged, sizeof(prof_cnt_t));
648 ql_foreach(thr_cnt, &ctx->cnts_ql, cnts_link) {
Jason Evans6109fe02010-02-10 10:37:56 -0800649 volatile unsigned *epoch = &thr_cnt->epoch;
650
651 while (true) {
652 unsigned epoch0 = *epoch;
653
654 /* Make sure epoch is even. */
655 if (epoch0 & 1U)
656 continue;
657
658 memcpy(&tcnt, &thr_cnt->cnts, sizeof(prof_cnt_t));
659
660 /* Terminate if epoch didn't change while reading. */
661 if (*epoch == epoch0)
662 break;
663 }
664
Jason Evansa881cd22010-10-02 15:18:50 -0700665 ctx->cnt_summed.curobjs += tcnt.curobjs;
666 ctx->cnt_summed.curbytes += tcnt.curbytes;
667 if (opt_prof_accum) {
668 ctx->cnt_summed.accumobjs += tcnt.accumobjs;
669 ctx->cnt_summed.accumbytes += tcnt.accumbytes;
670 }
Jason Evans6109fe02010-02-10 10:37:56 -0800671 }
672
Jason Evans9ce3bfd2010-10-02 22:39:59 -0700673 if (ctx->cnt_summed.curobjs != 0)
674 (*leak_nctx)++;
675
Jason Evansa881cd22010-10-02 15:18:50 -0700676 /* Add to cnt_all. */
677 cnt_all->curobjs += ctx->cnt_summed.curobjs;
678 cnt_all->curbytes += ctx->cnt_summed.curbytes;
679 if (opt_prof_accum) {
680 cnt_all->accumobjs += ctx->cnt_summed.accumobjs;
681 cnt_all->accumbytes += ctx->cnt_summed.accumbytes;
682 }
Jason Evans6109fe02010-02-10 10:37:56 -0800683
Jason Evans6da54182012-03-23 18:05:51 -0700684 malloc_mutex_unlock(ctx->lock);
Jason Evans6109fe02010-02-10 10:37:56 -0800685}
686
Jason Evansa881cd22010-10-02 15:18:50 -0700687static void
688prof_ctx_destroy(prof_ctx_t *ctx)
689{
690
Jason Evans7372b152012-02-10 20:22:09 -0800691 cassert(config_prof);
692
Jason Evansa881cd22010-10-02 15:18:50 -0700693 /*
694 * Check that ctx is still unused by any thread cache before destroying
Jason Evans0cdd42e2011-08-09 19:06:06 -0700695 * it. prof_lookup() artificially raises ctx->cnt_merge.curobjs in
696 * order to avoid a race condition with this function, as does
697 * prof_ctx_merge() in order to avoid a race between the main body of
698 * prof_ctx_merge() and entry into this function.
Jason Evansa881cd22010-10-02 15:18:50 -0700699 */
700 prof_enter();
Jason Evans6da54182012-03-23 18:05:51 -0700701 malloc_mutex_lock(ctx->lock);
Jason Evansb04a9402010-10-27 19:47:40 -0700702 if (ql_first(&ctx->cnts_ql) == NULL && ctx->cnt_merged.curobjs == 1) {
Jason Evansa881cd22010-10-02 15:18:50 -0700703 assert(ctx->cnt_merged.curbytes == 0);
704 assert(ctx->cnt_merged.accumobjs == 0);
705 assert(ctx->cnt_merged.accumbytes == 0);
706 /* Remove ctx from bt2ctx. */
Jason Evansa9076c92011-08-30 23:40:11 -0700707 if (ckh_remove(&bt2ctx, ctx->bt, NULL, NULL))
708 assert(false);
Jason Evansa881cd22010-10-02 15:18:50 -0700709 prof_leave();
710 /* Destroy ctx. */
Jason Evans6da54182012-03-23 18:05:51 -0700711 malloc_mutex_unlock(ctx->lock);
Jason Evansa881cd22010-10-02 15:18:50 -0700712 bt_destroy(ctx->bt);
Jason Evansa881cd22010-10-02 15:18:50 -0700713 idalloc(ctx);
714 } else {
Jason Evans0cdd42e2011-08-09 19:06:06 -0700715 /*
716 * Compensate for increment in prof_ctx_merge() or
717 * prof_lookup().
718 */
Jason Evansb04a9402010-10-27 19:47:40 -0700719 ctx->cnt_merged.curobjs--;
Jason Evans6da54182012-03-23 18:05:51 -0700720 malloc_mutex_unlock(ctx->lock);
Jason Evansa881cd22010-10-02 15:18:50 -0700721 prof_leave();
722 }
723}
724
725static void
726prof_ctx_merge(prof_ctx_t *ctx, prof_thr_cnt_t *cnt)
727{
728 bool destroy;
729
Jason Evans7372b152012-02-10 20:22:09 -0800730 cassert(config_prof);
731
Jason Evansa881cd22010-10-02 15:18:50 -0700732 /* Merge cnt stats and detach from ctx. */
Jason Evans6da54182012-03-23 18:05:51 -0700733 malloc_mutex_lock(ctx->lock);
Jason Evansa881cd22010-10-02 15:18:50 -0700734 ctx->cnt_merged.curobjs += cnt->cnts.curobjs;
735 ctx->cnt_merged.curbytes += cnt->cnts.curbytes;
736 ctx->cnt_merged.accumobjs += cnt->cnts.accumobjs;
737 ctx->cnt_merged.accumbytes += cnt->cnts.accumbytes;
738 ql_remove(&ctx->cnts_ql, cnt, cnts_link);
739 if (opt_prof_accum == false && ql_first(&ctx->cnts_ql) == NULL &&
Jason Evansb04a9402010-10-27 19:47:40 -0700740 ctx->cnt_merged.curobjs == 0) {
741 /*
742 * Artificially raise ctx->cnt_merged.curobjs in order to keep
743 * another thread from winning the race to destroy ctx while
744 * this one has ctx->lock dropped. Without this, it would be
745 * possible for another thread to:
746 *
747 * 1) Sample an allocation associated with ctx.
748 * 2) Deallocate the sampled object.
749 * 3) Successfully prof_ctx_destroy(ctx).
750 *
751 * The result would be that ctx no longer exists by the time
752 * this thread accesses it in prof_ctx_destroy().
753 */
754 ctx->cnt_merged.curobjs++;
Jason Evansa881cd22010-10-02 15:18:50 -0700755 destroy = true;
Jason Evansb04a9402010-10-27 19:47:40 -0700756 } else
Jason Evansa881cd22010-10-02 15:18:50 -0700757 destroy = false;
Jason Evans6da54182012-03-23 18:05:51 -0700758 malloc_mutex_unlock(ctx->lock);
Jason Evansa881cd22010-10-02 15:18:50 -0700759 if (destroy)
760 prof_ctx_destroy(ctx);
761}
762
Jason Evans22ca8552010-03-02 11:57:30 -0800763static bool
Jason Evansd81e4bd2012-03-06 14:57:45 -0800764prof_dump_ctx(bool propagate_err, prof_ctx_t *ctx, prof_bt_t *bt)
Jason Evans6109fe02010-02-10 10:37:56 -0800765{
Jason Evans6109fe02010-02-10 10:37:56 -0800766 unsigned i;
767
Jason Evans7372b152012-02-10 20:22:09 -0800768 cassert(config_prof);
769
Jason Evansa881cd22010-10-02 15:18:50 -0700770 if (opt_prof_accum == false && ctx->cnt_summed.curobjs == 0) {
771 assert(ctx->cnt_summed.curbytes == 0);
772 assert(ctx->cnt_summed.accumobjs == 0);
773 assert(ctx->cnt_summed.accumbytes == 0);
774 return (false);
775 }
776
Jason Evansd81e4bd2012-03-06 14:57:45 -0800777 if (prof_printf(propagate_err, "%"PRId64": %"PRId64
778 " [%"PRIu64": %"PRIu64"] @",
779 ctx->cnt_summed.curobjs, ctx->cnt_summed.curbytes,
780 ctx->cnt_summed.accumobjs, ctx->cnt_summed.accumbytes))
Jason Evans22ca8552010-03-02 11:57:30 -0800781 return (true);
Jason Evans6109fe02010-02-10 10:37:56 -0800782
783 for (i = 0; i < bt->len; i++) {
Jason Evans2bb6c7a2012-03-12 13:38:00 -0700784 if (prof_printf(propagate_err, " %#"PRIxPTR,
Jason Evansd81e4bd2012-03-06 14:57:45 -0800785 (uintptr_t)bt->vec[i]))
Jason Evans22ca8552010-03-02 11:57:30 -0800786 return (true);
Jason Evans6109fe02010-02-10 10:37:56 -0800787 }
788
Jason Evansd81e4bd2012-03-06 14:57:45 -0800789 if (prof_write(propagate_err, "\n"))
Jason Evans22ca8552010-03-02 11:57:30 -0800790 return (true);
791
792 return (false);
Jason Evans6109fe02010-02-10 10:37:56 -0800793}
794
Jason Evans22ca8552010-03-02 11:57:30 -0800795static bool
796prof_dump_maps(bool propagate_err)
Jason Evansc7177182010-02-11 09:25:56 -0800797{
798 int mfd;
Jason Evanscd9a1342012-03-21 18:33:03 -0700799 char filename[PATH_MAX + 1];
Jason Evansc7177182010-02-11 09:25:56 -0800800
Jason Evans7372b152012-02-10 20:22:09 -0800801 cassert(config_prof);
802
Jason Evanscd9a1342012-03-21 18:33:03 -0700803 malloc_snprintf(filename, sizeof(filename), "/proc/%d/maps",
804 (int)getpid());
805 mfd = open(filename, O_RDONLY);
Jason Evansc7177182010-02-11 09:25:56 -0800806 if (mfd != -1) {
807 ssize_t nread;
808
Jason Evansd81e4bd2012-03-06 14:57:45 -0800809 if (prof_write(propagate_err, "\nMAPPED_LIBRARIES:\n") &&
Jason Evans22ca8552010-03-02 11:57:30 -0800810 propagate_err)
811 return (true);
Jason Evansc7177182010-02-11 09:25:56 -0800812 nread = 0;
813 do {
814 prof_dump_buf_end += nread;
Jason Evanscd9a1342012-03-21 18:33:03 -0700815 if (prof_dump_buf_end == PROF_DUMP_BUFSIZE) {
Jason Evansc7177182010-02-11 09:25:56 -0800816 /* Make space in prof_dump_buf before read(). */
Jason Evans22ca8552010-03-02 11:57:30 -0800817 if (prof_flush(propagate_err) && propagate_err)
818 return (true);
Jason Evansc7177182010-02-11 09:25:56 -0800819 }
820 nread = read(mfd, &prof_dump_buf[prof_dump_buf_end],
Jason Evanscd9a1342012-03-21 18:33:03 -0700821 PROF_DUMP_BUFSIZE - prof_dump_buf_end);
Jason Evansc7177182010-02-11 09:25:56 -0800822 } while (nread > 0);
Jason Evansd34f9e72010-02-11 13:19:21 -0800823 close(mfd);
Jason Evans22ca8552010-03-02 11:57:30 -0800824 } else
825 return (true);
826
827 return (false);
Jason Evansc7177182010-02-11 09:25:56 -0800828}
829
Jason Evans22ca8552010-03-02 11:57:30 -0800830static bool
Jason Evansd81e4bd2012-03-06 14:57:45 -0800831prof_dump(bool propagate_err, const char *filename, bool leakcheck)
Jason Evans6109fe02010-02-10 10:37:56 -0800832{
833 prof_cnt_t cnt_all;
834 size_t tabind;
Jason Evans075e77c2010-09-20 19:53:25 -0700835 union {
836 prof_bt_t *p;
837 void *v;
838 } bt;
839 union {
840 prof_ctx_t *p;
841 void *v;
842 } ctx;
Jason Evans6109fe02010-02-10 10:37:56 -0800843 size_t leak_nctx;
844
Jason Evans7372b152012-02-10 20:22:09 -0800845 cassert(config_prof);
846
Jason Evans6109fe02010-02-10 10:37:56 -0800847 prof_enter();
848 prof_dump_fd = creat(filename, 0644);
849 if (prof_dump_fd == -1) {
Jason Evans22ca8552010-03-02 11:57:30 -0800850 if (propagate_err == false) {
Jason Evansd81e4bd2012-03-06 14:57:45 -0800851 malloc_printf(
852 "<jemalloc>: creat(\"%s\"), 0644) failed\n",
853 filename);
Jason Evans22ca8552010-03-02 11:57:30 -0800854 if (opt_abort)
855 abort();
856 }
Jason Evans22ca8552010-03-02 11:57:30 -0800857 goto ERROR;
Jason Evans6109fe02010-02-10 10:37:56 -0800858 }
859
860 /* Merge per thread profile stats, and sum them in cnt_all. */
861 memset(&cnt_all, 0, sizeof(prof_cnt_t));
862 leak_nctx = 0;
Jason Evans588a32c2010-10-02 22:38:14 -0700863 for (tabind = 0; ckh_iter(&bt2ctx, &tabind, NULL, &ctx.v) == false;)
Jason Evansa881cd22010-10-02 15:18:50 -0700864 prof_ctx_sum(ctx.p, &cnt_all, &leak_nctx);
Jason Evans6109fe02010-02-10 10:37:56 -0800865
866 /* Dump profile header. */
Jason Evans22ca8552010-03-02 11:57:30 -0800867 if (opt_lg_prof_sample == 0) {
Jason Evansd81e4bd2012-03-06 14:57:45 -0800868 if (prof_printf(propagate_err,
869 "heap profile: %"PRId64": %"PRId64
870 " [%"PRIu64": %"PRIu64"] @ heapprofile\n",
871 cnt_all.curobjs, cnt_all.curbytes,
872 cnt_all.accumobjs, cnt_all.accumbytes))
Jason Evans22ca8552010-03-02 11:57:30 -0800873 goto ERROR;
874 } else {
Jason Evansd81e4bd2012-03-06 14:57:45 -0800875 if (prof_printf(propagate_err,
876 "heap profile: %"PRId64": %"PRId64
877 " [%"PRIu64": %"PRIu64"] @ heap_v2/%"PRIu64"\n",
878 cnt_all.curobjs, cnt_all.curbytes,
879 cnt_all.accumobjs, cnt_all.accumbytes,
880 ((uint64_t)1U << opt_lg_prof_sample)))
Jason Evans22ca8552010-03-02 11:57:30 -0800881 goto ERROR;
Jason Evansb9477e72010-03-01 20:15:26 -0800882 }
Jason Evans6109fe02010-02-10 10:37:56 -0800883
884 /* Dump per ctx profile stats. */
Jason Evans075e77c2010-09-20 19:53:25 -0700885 for (tabind = 0; ckh_iter(&bt2ctx, &tabind, &bt.v, &ctx.v)
Jason Evans6109fe02010-02-10 10:37:56 -0800886 == false;) {
Jason Evansd81e4bd2012-03-06 14:57:45 -0800887 if (prof_dump_ctx(propagate_err, ctx.p, bt.p))
Jason Evans22ca8552010-03-02 11:57:30 -0800888 goto ERROR;
Jason Evans6109fe02010-02-10 10:37:56 -0800889 }
890
Jason Evansc7177182010-02-11 09:25:56 -0800891 /* Dump /proc/<pid>/maps if possible. */
Jason Evans22ca8552010-03-02 11:57:30 -0800892 if (prof_dump_maps(propagate_err))
893 goto ERROR;
Jason Evansc7177182010-02-11 09:25:56 -0800894
Jason Evans22ca8552010-03-02 11:57:30 -0800895 if (prof_flush(propagate_err))
896 goto ERROR;
Jason Evans6109fe02010-02-10 10:37:56 -0800897 close(prof_dump_fd);
898 prof_leave();
899
900 if (leakcheck && cnt_all.curbytes != 0) {
Jason Evansd81e4bd2012-03-06 14:57:45 -0800901 malloc_printf("<jemalloc>: Leak summary: %"PRId64" byte%s, %"
902 PRId64" object%s, %zu context%s\n",
903 cnt_all.curbytes, (cnt_all.curbytes != 1) ? "s" : "",
904 cnt_all.curobjs, (cnt_all.curobjs != 1) ? "s" : "",
905 leak_nctx, (leak_nctx != 1) ? "s" : "");
906 malloc_printf(
907 "<jemalloc>: Run pprof on \"%s\" for leak detail\n",
908 filename);
Jason Evans6109fe02010-02-10 10:37:56 -0800909 }
Jason Evans22ca8552010-03-02 11:57:30 -0800910
911 return (false);
912ERROR:
913 prof_leave();
914 return (true);
Jason Evans6109fe02010-02-10 10:37:56 -0800915}
916
Jason Evansd81e4bd2012-03-06 14:57:45 -0800917#define DUMP_FILENAME_BUFSIZE (PATH_MAX + 1)
Jason Evans6109fe02010-02-10 10:37:56 -0800918static void
919prof_dump_filename(char *filename, char v, int64_t vseq)
920{
Jason Evans6109fe02010-02-10 10:37:56 -0800921
Jason Evans7372b152012-02-10 20:22:09 -0800922 cassert(config_prof);
923
Jason Evansb8c8be72012-03-05 12:26:26 -0800924 if (vseq != UINT64_C(0xffffffffffffffff)) {
Jason Evansd81e4bd2012-03-06 14:57:45 -0800925 /* "<prefix>.<pid>.<seq>.v<vseq>.heap" */
926 malloc_snprintf(filename, DUMP_FILENAME_BUFSIZE,
927 "%s.%d.%"PRIu64".%c%"PRId64".heap",
928 opt_prof_prefix, (int)getpid(), prof_dump_seq, v, vseq);
929 } else {
930 /* "<prefix>.<pid>.<seq>.<v>.heap" */
931 malloc_snprintf(filename, DUMP_FILENAME_BUFSIZE,
932 "%s.%d.%"PRIu64".%c.heap",
933 opt_prof_prefix, (int)getpid(), prof_dump_seq, v);
Jason Evans6109fe02010-02-10 10:37:56 -0800934 }
Jason Evans6109fe02010-02-10 10:37:56 -0800935}
936
937static void
938prof_fdump(void)
939{
940 char filename[DUMP_FILENAME_BUFSIZE];
941
Jason Evans7372b152012-02-10 20:22:09 -0800942 cassert(config_prof);
943
Jason Evans6109fe02010-02-10 10:37:56 -0800944 if (prof_booted == false)
945 return;
946
Jason Evanse7339702010-10-23 18:37:06 -0700947 if (opt_prof_prefix[0] != '\0') {
948 malloc_mutex_lock(&prof_dump_seq_mtx);
Jason Evansb8c8be72012-03-05 12:26:26 -0800949 prof_dump_filename(filename, 'f', UINT64_C(0xffffffffffffffff));
Jason Evanse7339702010-10-23 18:37:06 -0700950 malloc_mutex_unlock(&prof_dump_seq_mtx);
Jason Evansd81e4bd2012-03-06 14:57:45 -0800951 prof_dump(false, filename, opt_prof_leak);
Jason Evanse7339702010-10-23 18:37:06 -0700952 }
Jason Evans6109fe02010-02-10 10:37:56 -0800953}
954
955void
956prof_idump(void)
957{
Jason Evansd81e4bd2012-03-06 14:57:45 -0800958 char filename[PATH_MAX + 1];
Jason Evans6109fe02010-02-10 10:37:56 -0800959
Jason Evans7372b152012-02-10 20:22:09 -0800960 cassert(config_prof);
961
Jason Evans6109fe02010-02-10 10:37:56 -0800962 if (prof_booted == false)
963 return;
Jason Evansd34f9e72010-02-11 13:19:21 -0800964 malloc_mutex_lock(&enq_mtx);
965 if (enq) {
966 enq_idump = true;
967 malloc_mutex_unlock(&enq_mtx);
968 return;
969 }
970 malloc_mutex_unlock(&enq_mtx);
Jason Evans6109fe02010-02-10 10:37:56 -0800971
Jason Evanse7339702010-10-23 18:37:06 -0700972 if (opt_prof_prefix[0] != '\0') {
973 malloc_mutex_lock(&prof_dump_seq_mtx);
974 prof_dump_filename(filename, 'i', prof_dump_iseq);
975 prof_dump_iseq++;
976 malloc_mutex_unlock(&prof_dump_seq_mtx);
Jason Evansd81e4bd2012-03-06 14:57:45 -0800977 prof_dump(false, filename, false);
Jason Evanse7339702010-10-23 18:37:06 -0700978 }
Jason Evans6109fe02010-02-10 10:37:56 -0800979}
980
Jason Evans22ca8552010-03-02 11:57:30 -0800981bool
982prof_mdump(const char *filename)
Jason Evans6109fe02010-02-10 10:37:56 -0800983{
Jason Evans22ca8552010-03-02 11:57:30 -0800984 char filename_buf[DUMP_FILENAME_BUFSIZE];
Jason Evans6109fe02010-02-10 10:37:56 -0800985
Jason Evans7372b152012-02-10 20:22:09 -0800986 cassert(config_prof);
987
Jason Evans22ca8552010-03-02 11:57:30 -0800988 if (opt_prof == false || prof_booted == false)
989 return (true);
Jason Evans6109fe02010-02-10 10:37:56 -0800990
Jason Evans22ca8552010-03-02 11:57:30 -0800991 if (filename == NULL) {
992 /* No filename specified, so automatically generate one. */
Jason Evanse7339702010-10-23 18:37:06 -0700993 if (opt_prof_prefix[0] == '\0')
994 return (true);
Jason Evans22ca8552010-03-02 11:57:30 -0800995 malloc_mutex_lock(&prof_dump_seq_mtx);
996 prof_dump_filename(filename_buf, 'm', prof_dump_mseq);
997 prof_dump_mseq++;
998 malloc_mutex_unlock(&prof_dump_seq_mtx);
999 filename = filename_buf;
1000 }
Jason Evansd81e4bd2012-03-06 14:57:45 -08001001 return (prof_dump(true, filename, false));
Jason Evans6109fe02010-02-10 10:37:56 -08001002}
1003
1004void
Jason Evanse7339702010-10-23 18:37:06 -07001005prof_gdump(void)
Jason Evans6109fe02010-02-10 10:37:56 -08001006{
1007 char filename[DUMP_FILENAME_BUFSIZE];
1008
Jason Evans7372b152012-02-10 20:22:09 -08001009 cassert(config_prof);
1010
Jason Evans6109fe02010-02-10 10:37:56 -08001011 if (prof_booted == false)
1012 return;
1013 malloc_mutex_lock(&enq_mtx);
1014 if (enq) {
Jason Evanse7339702010-10-23 18:37:06 -07001015 enq_gdump = true;
Jason Evans6109fe02010-02-10 10:37:56 -08001016 malloc_mutex_unlock(&enq_mtx);
1017 return;
1018 }
1019 malloc_mutex_unlock(&enq_mtx);
1020
Jason Evanse7339702010-10-23 18:37:06 -07001021 if (opt_prof_prefix[0] != '\0') {
1022 malloc_mutex_lock(&prof_dump_seq_mtx);
1023 prof_dump_filename(filename, 'u', prof_dump_useq);
1024 prof_dump_useq++;
1025 malloc_mutex_unlock(&prof_dump_seq_mtx);
Jason Evansd81e4bd2012-03-06 14:57:45 -08001026 prof_dump(false, filename, false);
Jason Evanse7339702010-10-23 18:37:06 -07001027 }
Jason Evans6109fe02010-02-10 10:37:56 -08001028}
1029
1030static void
1031prof_bt_hash(const void *key, unsigned minbits, size_t *hash1, size_t *hash2)
1032{
1033 size_t ret1, ret2;
1034 uint64_t h;
1035 prof_bt_t *bt = (prof_bt_t *)key;
1036
Jason Evans7372b152012-02-10 20:22:09 -08001037 cassert(config_prof);
Jason Evans6109fe02010-02-10 10:37:56 -08001038 assert(minbits <= 32 || (SIZEOF_PTR == 8 && minbits <= 64));
1039 assert(hash1 != NULL);
1040 assert(hash2 != NULL);
1041
Jason Evansb8c8be72012-03-05 12:26:26 -08001042 h = hash(bt->vec, bt->len * sizeof(void *),
1043 UINT64_C(0x94122f335b332aea));
Jason Evans6109fe02010-02-10 10:37:56 -08001044 if (minbits <= 32) {
1045 /*
1046 * Avoid doing multiple hashes, since a single hash provides
1047 * enough bits.
1048 */
1049 ret1 = h & ZU(0xffffffffU);
1050 ret2 = h >> 32;
1051 } else {
1052 ret1 = h;
1053 ret2 = hash(bt->vec, bt->len * sizeof(void *),
Jason Evansb8c8be72012-03-05 12:26:26 -08001054 UINT64_C(0x8432a476666bbc13));
Jason Evans6109fe02010-02-10 10:37:56 -08001055 }
1056
1057 *hash1 = ret1;
1058 *hash2 = ret2;
1059}
1060
1061static bool
1062prof_bt_keycomp(const void *k1, const void *k2)
1063{
1064 const prof_bt_t *bt1 = (prof_bt_t *)k1;
1065 const prof_bt_t *bt2 = (prof_bt_t *)k2;
1066
Jason Evans7372b152012-02-10 20:22:09 -08001067 cassert(config_prof);
1068
Jason Evans6109fe02010-02-10 10:37:56 -08001069 if (bt1->len != bt2->len)
1070 return (false);
1071 return (memcmp(bt1->vec, bt2->vec, bt1->len * sizeof(void *)) == 0);
1072}
1073
Jason Evans6da54182012-03-23 18:05:51 -07001074static malloc_mutex_t *
1075prof_ctx_mutex_choose(void)
1076{
1077 unsigned nctxs = atomic_add_u(&cum_ctxs, 1);
1078
1079 return (&ctx_locks[(nctxs - 1) % PROF_NCTX_LOCKS]);
1080}
1081
Jason Evans4d6a1342010-10-20 19:05:59 -07001082prof_tdata_t *
1083prof_tdata_init(void)
Jason Evans6109fe02010-02-10 10:37:56 -08001084{
Jason Evans4d6a1342010-10-20 19:05:59 -07001085 prof_tdata_t *prof_tdata;
Jason Evans6109fe02010-02-10 10:37:56 -08001086
Jason Evans7372b152012-02-10 20:22:09 -08001087 cassert(config_prof);
1088
Jason Evans4d6a1342010-10-20 19:05:59 -07001089 /* Initialize an empty cache for this thread. */
1090 prof_tdata = (prof_tdata_t *)imalloc(sizeof(prof_tdata_t));
1091 if (prof_tdata == NULL)
1092 return (NULL);
1093
1094 if (ckh_new(&prof_tdata->bt2cnt, PROF_CKH_MINITEMS,
1095 prof_bt_hash, prof_bt_keycomp)) {
1096 idalloc(prof_tdata);
1097 return (NULL);
1098 }
1099 ql_new(&prof_tdata->lru_ql);
1100
Jason Evans53891462012-02-13 18:23:41 -08001101 prof_tdata->vec = imalloc(sizeof(void *) * PROF_BT_MAX);
Jason Evans4d6a1342010-10-20 19:05:59 -07001102 if (prof_tdata->vec == NULL) {
Jason Evans4d6a1342010-10-20 19:05:59 -07001103 ckh_delete(&prof_tdata->bt2cnt);
1104 idalloc(prof_tdata);
1105 return (NULL);
1106 }
1107
Jason Evans84f7cdb2012-03-02 15:59:45 -08001108 prof_tdata->prng_state = 0;
Jason Evans4d6a1342010-10-20 19:05:59 -07001109 prof_tdata->threshold = 0;
1110 prof_tdata->accum = 0;
1111
Jason Evanscd9a1342012-03-21 18:33:03 -07001112 prof_tdata_tsd_set(&prof_tdata);
Jason Evans4d6a1342010-10-20 19:05:59 -07001113
1114 return (prof_tdata);
1115}
1116
Jason Evanscd9a1342012-03-21 18:33:03 -07001117void
Jason Evans4d6a1342010-10-20 19:05:59 -07001118prof_tdata_cleanup(void *arg)
1119{
Jason Evans41b954e2011-08-08 17:10:07 -07001120 prof_thr_cnt_t *cnt;
Jason Evanscd9a1342012-03-21 18:33:03 -07001121 prof_tdata_t *prof_tdata = *(prof_tdata_t **)arg;
Jason Evans4d6a1342010-10-20 19:05:59 -07001122
Jason Evans7372b152012-02-10 20:22:09 -08001123 cassert(config_prof);
1124
Jason Evans41b954e2011-08-08 17:10:07 -07001125 /*
Jason Evans0cdd42e2011-08-09 19:06:06 -07001126 * Delete the hash table. All of its contents can still be iterated
1127 * over via the LRU.
Jason Evans41b954e2011-08-08 17:10:07 -07001128 */
1129 ckh_delete(&prof_tdata->bt2cnt);
Jason Evans6109fe02010-02-10 10:37:56 -08001130
Jason Evans0cdd42e2011-08-09 19:06:06 -07001131 /* Iteratively merge cnt's into the global stats and delete them. */
Jason Evans41b954e2011-08-08 17:10:07 -07001132 while ((cnt = ql_last(&prof_tdata->lru_ql, lru_link)) != NULL) {
Jason Evans41b954e2011-08-08 17:10:07 -07001133 ql_remove(&prof_tdata->lru_ql, cnt, lru_link);
Jason Evans0cdd42e2011-08-09 19:06:06 -07001134 prof_ctx_merge(cnt->ctx, cnt);
Jason Evans41b954e2011-08-08 17:10:07 -07001135 idalloc(cnt);
Jason Evans6109fe02010-02-10 10:37:56 -08001136 }
Jason Evans41b954e2011-08-08 17:10:07 -07001137
1138 idalloc(prof_tdata->vec);
1139
1140 idalloc(prof_tdata);
Jason Evanscd9a1342012-03-21 18:33:03 -07001141 prof_tdata = NULL;
1142 prof_tdata_tsd_set(&prof_tdata);
Jason Evans6109fe02010-02-10 10:37:56 -08001143}
1144
1145void
1146prof_boot0(void)
1147{
1148
Jason Evans7372b152012-02-10 20:22:09 -08001149 cassert(config_prof);
1150
Jason Evanse7339702010-10-23 18:37:06 -07001151 memcpy(opt_prof_prefix, PROF_PREFIX_DEFAULT,
1152 sizeof(PROF_PREFIX_DEFAULT));
1153}
1154
1155void
1156prof_boot1(void)
1157{
1158
Jason Evans7372b152012-02-10 20:22:09 -08001159 cassert(config_prof);
1160
Jason Evans6109fe02010-02-10 10:37:56 -08001161 /*
Jason Evans0b270a92010-03-31 16:45:04 -07001162 * opt_prof and prof_promote must be in their final state before any
1163 * arenas are initialized, so this function must be executed early.
Jason Evans6109fe02010-02-10 10:37:56 -08001164 */
1165
1166 if (opt_prof_leak && opt_prof == false) {
1167 /*
1168 * Enable opt_prof, but in such a way that profiles are never
1169 * automatically dumped.
1170 */
1171 opt_prof = true;
Jason Evanse7339702010-10-23 18:37:06 -07001172 opt_prof_gdump = false;
Jason Evans6109fe02010-02-10 10:37:56 -08001173 prof_interval = 0;
Jason Evansa02fc082010-03-31 17:35:51 -07001174 } else if (opt_prof) {
1175 if (opt_lg_prof_interval >= 0) {
1176 prof_interval = (((uint64_t)1U) <<
1177 opt_lg_prof_interval);
1178 } else
1179 prof_interval = 0;
1180 }
Jason Evans0b270a92010-03-31 16:45:04 -07001181
Jason Evansae4c7b42012-04-02 07:04:34 -07001182 prof_promote = (opt_prof && opt_lg_prof_sample > LG_PAGE);
Jason Evans6109fe02010-02-10 10:37:56 -08001183}
1184
1185bool
Jason Evanse7339702010-10-23 18:37:06 -07001186prof_boot2(void)
Jason Evans6109fe02010-02-10 10:37:56 -08001187{
1188
Jason Evans7372b152012-02-10 20:22:09 -08001189 cassert(config_prof);
1190
Jason Evans6109fe02010-02-10 10:37:56 -08001191 if (opt_prof) {
Jason Evans6da54182012-03-23 18:05:51 -07001192 unsigned i;
1193
Jason Evans6109fe02010-02-10 10:37:56 -08001194 if (ckh_new(&bt2ctx, PROF_CKH_MINITEMS, prof_bt_hash,
1195 prof_bt_keycomp))
1196 return (true);
1197 if (malloc_mutex_init(&bt2ctx_mtx))
1198 return (true);
Jason Evanscd9a1342012-03-21 18:33:03 -07001199 if (prof_tdata_tsd_boot()) {
Jason Evans698805c2010-03-03 17:45:38 -08001200 malloc_write(
1201 "<jemalloc>: Error in pthread_key_create()\n");
Jason Evans6109fe02010-02-10 10:37:56 -08001202 abort();
1203 }
1204
Jason Evans6109fe02010-02-10 10:37:56 -08001205 if (malloc_mutex_init(&prof_dump_seq_mtx))
1206 return (true);
1207
1208 if (malloc_mutex_init(&enq_mtx))
1209 return (true);
1210 enq = false;
Jason Evansd34f9e72010-02-11 13:19:21 -08001211 enq_idump = false;
Jason Evanse7339702010-10-23 18:37:06 -07001212 enq_gdump = false;
Jason Evans6109fe02010-02-10 10:37:56 -08001213
1214 if (atexit(prof_fdump) != 0) {
Jason Evans698805c2010-03-03 17:45:38 -08001215 malloc_write("<jemalloc>: Error in atexit()\n");
Jason Evans6109fe02010-02-10 10:37:56 -08001216 if (opt_abort)
1217 abort();
1218 }
Jason Evans6da54182012-03-23 18:05:51 -07001219
1220 ctx_locks = (malloc_mutex_t *)base_alloc(PROF_NCTX_LOCKS *
1221 sizeof(malloc_mutex_t));
1222 if (ctx_locks == NULL)
1223 return (true);
1224 for (i = 0; i < PROF_NCTX_LOCKS; i++) {
1225 if (malloc_mutex_init(&ctx_locks[i]))
1226 return (true);
1227 }
Jason Evans6109fe02010-02-10 10:37:56 -08001228 }
1229
Jason Evansb27805b2010-02-10 18:15:53 -08001230#ifdef JEMALLOC_PROF_LIBGCC
1231 /*
1232 * Cause the backtracing machinery to allocate its internal state
1233 * before enabling profiling.
1234 */
1235 _Unwind_Backtrace(prof_unwind_init_callback, NULL);
1236#endif
1237
Jason Evans6109fe02010-02-10 10:37:56 -08001238 prof_booted = true;
1239
1240 return (false);
1241}
1242
Jason Evans6109fe02010-02-10 10:37:56 -08001243/******************************************************************************/