Jason Evans | c0cc5db | 2017-01-19 21:41:41 -0800 | [diff] [blame] | 1 | #define JEMALLOC_PROF_C_ |
David Goldblatt | 743d940 | 2017-04-10 18:17:55 -0700 | [diff] [blame] | 2 | #include "jemalloc/internal/jemalloc_preamble.h" |
| 3 | #include "jemalloc/internal/jemalloc_internal_includes.h" |
| 4 | |
David Goldblatt | d9ec36e | 2017-04-11 14:43:12 -0700 | [diff] [blame] | 5 | #include "jemalloc/internal/assert.h" |
David Goldblatt | 68da236 | 2017-04-19 14:56:42 -0700 | [diff] [blame^] | 6 | #include "jemalloc/internal/ckh.h" |
David Goldblatt | 54373be | 2017-04-11 13:06:31 -0700 | [diff] [blame] | 7 | #include "jemalloc/internal/malloc_io.h" |
| 8 | |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 9 | /******************************************************************************/ |
| 10 | |
| 11 | #ifdef JEMALLOC_PROF_LIBUNWIND |
Jason Evans | c0cc5db | 2017-01-19 21:41:41 -0800 | [diff] [blame] | 12 | #define UNW_LOCAL_ONLY |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 13 | #include <libunwind.h> |
| 14 | #endif |
| 15 | |
Jason Evans | 77f350b | 2011-03-15 22:23:12 -0700 | [diff] [blame] | 16 | #ifdef JEMALLOC_PROF_LIBGCC |
David Goldblatt | 0a0fcd3 | 2017-03-28 17:30:54 -0700 | [diff] [blame] | 17 | /* |
| 18 | * We have a circular dependency -- jemalloc_internal.h tells us if we should |
| 19 | * use libgcc's unwinding functionality, but after we've included that, we've |
| 20 | * already hooked _Unwind_Backtrace. We'll temporarily disable hooking. |
| 21 | */ |
| 22 | #undef _Unwind_Backtrace |
Jason Evans | 77f350b | 2011-03-15 22:23:12 -0700 | [diff] [blame] | 23 | #include <unwind.h> |
David Goldblatt | 0a0fcd3 | 2017-03-28 17:30:54 -0700 | [diff] [blame] | 24 | #define _Unwind_Backtrace JEMALLOC_HOOK(_Unwind_Backtrace, hooks_libc_hook) |
Jason Evans | 77f350b | 2011-03-15 22:23:12 -0700 | [diff] [blame] | 25 | #endif |
| 26 | |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 27 | /******************************************************************************/ |
| 28 | /* Data. */ |
| 29 | |
| 30 | bool opt_prof = false; |
Jason Evans | f18c982 | 2010-03-31 18:43:24 -0700 | [diff] [blame] | 31 | bool opt_prof_active = true; |
Jason Evans | fc12c0b | 2014-10-03 23:25:30 -0700 | [diff] [blame] | 32 | bool opt_prof_thread_active_init = true; |
Jason Evans | b9477e7 | 2010-03-01 20:15:26 -0800 | [diff] [blame] | 33 | size_t opt_lg_prof_sample = LG_PROF_SAMPLE_DEFAULT; |
Jason Evans | a02fc08 | 2010-03-31 17:35:51 -0700 | [diff] [blame] | 34 | ssize_t opt_lg_prof_interval = LG_PROF_INTERVAL_DEFAULT; |
Jason Evans | e733970 | 2010-10-23 18:37:06 -0700 | [diff] [blame] | 35 | bool opt_prof_gdump = false; |
Jason Evans | 57efa7b | 2014-10-08 17:57:19 -0700 | [diff] [blame] | 36 | bool opt_prof_final = false; |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 37 | bool opt_prof_leak = false; |
Jason Evans | 0b25fe7 | 2012-04-17 16:39:33 -0700 | [diff] [blame] | 38 | bool opt_prof_accum = false; |
Jason Evans | 4f37ef6 | 2014-01-16 13:23:56 -0800 | [diff] [blame] | 39 | char opt_prof_prefix[ |
| 40 | /* Minimize memory bloat for non-prof builds. */ |
| 41 | #ifdef JEMALLOC_PROF |
| 42 | PATH_MAX + |
| 43 | #endif |
Jason Evans | eefdd02 | 2014-01-16 18:04:30 -0800 | [diff] [blame] | 44 | 1]; |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 45 | |
Jason Evans | fc12c0b | 2014-10-03 23:25:30 -0700 | [diff] [blame] | 46 | /* |
| 47 | * Initialized as opt_prof_active, and accessed via |
| 48 | * prof_active_[gs]et{_unlocked,}(). |
| 49 | */ |
| 50 | bool prof_active; |
| 51 | static malloc_mutex_t prof_active_mtx; |
| 52 | |
| 53 | /* |
| 54 | * Initialized as opt_prof_thread_active_init, and accessed via |
| 55 | * prof_thread_active_init_[gs]et(). |
| 56 | */ |
| 57 | static bool prof_thread_active_init; |
| 58 | static malloc_mutex_t prof_thread_active_init_mtx; |
| 59 | |
Jason Evans | 5b8ed5b | 2015-01-25 21:16:57 -0800 | [diff] [blame] | 60 | /* |
| 61 | * Initialized as opt_prof_gdump, and accessed via |
| 62 | * prof_gdump_[gs]et{_unlocked,}(). |
| 63 | */ |
| 64 | bool prof_gdump_val; |
| 65 | static malloc_mutex_t prof_gdump_mtx; |
| 66 | |
Jason Evans | a3b3386 | 2012-11-13 12:56:27 -0800 | [diff] [blame] | 67 | uint64_t prof_interval = 0; |
Jason Evans | d34f9e7 | 2010-02-11 13:19:21 -0800 | [diff] [blame] | 68 | |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 69 | size_t lg_prof_sample; |
| 70 | |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 71 | /* |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 72 | * Table of mutexes that are shared among gctx's. These are leaf locks, so |
| 73 | * there is no problem with using them for more than one gctx at the same time. |
| 74 | * The primary motivation for this sharing though is that gctx's are ephemeral, |
Jason Evans | 6da5418 | 2012-03-23 18:05:51 -0700 | [diff] [blame] | 75 | * and destroying mutexes causes complications for systems that allocate when |
| 76 | * creating/destroying mutexes. |
| 77 | */ |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 78 | static malloc_mutex_t *gctx_locks; |
David Goldblatt | 074f225 | 2017-04-04 18:36:45 -0700 | [diff] [blame] | 79 | static atomic_u_t cum_gctxs; /* Atomic counter. */ |
Jason Evans | 6da5418 | 2012-03-23 18:05:51 -0700 | [diff] [blame] | 80 | |
| 81 | /* |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 82 | * Table of mutexes that are shared among tdata's. No operations require |
| 83 | * holding multiple tdata locks, so there is no problem with using them for more |
| 84 | * than one tdata at the same time, even though a gctx lock may be acquired |
| 85 | * while holding a tdata lock. |
| 86 | */ |
| 87 | static malloc_mutex_t *tdata_locks; |
| 88 | |
| 89 | /* |
| 90 | * Global hash of (prof_bt_t *)-->(prof_gctx_t *). This is the master data |
Jason Evans | a881cd2 | 2010-10-02 15:18:50 -0700 | [diff] [blame] | 91 | * structure that knows about all backtraces currently captured. |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 92 | */ |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 93 | static ckh_t bt2gctx; |
Qi Wang | ca9074d | 2017-03-11 20:28:31 -0800 | [diff] [blame] | 94 | /* Non static to enable profiling. */ |
| 95 | malloc_mutex_t bt2gctx_mtx; |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 96 | |
| 97 | /* |
| 98 | * Tree of all extant prof_tdata_t structures, regardless of state, |
| 99 | * {attached,detached,expired}. |
| 100 | */ |
| 101 | static prof_tdata_tree_t tdatas; |
| 102 | static malloc_mutex_t tdatas_mtx; |
| 103 | |
| 104 | static uint64_t next_thr_uid; |
Jason Evans | 9d8f3d2 | 2014-09-11 18:06:30 -0700 | [diff] [blame] | 105 | static malloc_mutex_t next_thr_uid_mtx; |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 106 | |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 107 | static malloc_mutex_t prof_dump_seq_mtx; |
| 108 | static uint64_t prof_dump_seq; |
| 109 | static uint64_t prof_dump_iseq; |
| 110 | static uint64_t prof_dump_mseq; |
| 111 | static uint64_t prof_dump_useq; |
| 112 | |
| 113 | /* |
| 114 | * This buffer is rather large for stack allocation, so use a single buffer for |
Jason Evans | 4f37ef6 | 2014-01-16 13:23:56 -0800 | [diff] [blame] | 115 | * all profile dumps. |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 116 | */ |
Jason Evans | 4f37ef6 | 2014-01-16 13:23:56 -0800 | [diff] [blame] | 117 | static malloc_mutex_t prof_dump_mtx; |
| 118 | static char prof_dump_buf[ |
| 119 | /* Minimize memory bloat for non-prof builds. */ |
| 120 | #ifdef JEMALLOC_PROF |
| 121 | PROF_DUMP_BUFSIZE |
| 122 | #else |
| 123 | 1 |
| 124 | #endif |
| 125 | ]; |
Jason Evans | 42ce80e | 2016-02-25 20:51:00 -0800 | [diff] [blame] | 126 | static size_t prof_dump_buf_end; |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 127 | static int prof_dump_fd; |
| 128 | |
| 129 | /* Do not dump any profiles until bootstrapping is complete. */ |
| 130 | static bool prof_booted = false; |
| 131 | |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 132 | /******************************************************************************/ |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 133 | /* |
| 134 | * Function prototypes for static functions that are referenced prior to |
| 135 | * definition. |
| 136 | */ |
| 137 | |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 138 | static bool prof_tctx_should_destroy(tsdn_t *tsdn, prof_tctx_t *tctx); |
Jason Evans | 5460aa6 | 2014-09-22 21:09:23 -0700 | [diff] [blame] | 139 | static void prof_tctx_destroy(tsd_t *tsd, prof_tctx_t *tctx); |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 140 | static bool prof_tdata_should_destroy(tsdn_t *tsdn, prof_tdata_t *tdata, |
Jason Evans | f04a0be | 2014-10-04 15:03:49 -0700 | [diff] [blame] | 141 | bool even_if_attached); |
Jason Evans | b54d160 | 2016-10-20 23:59:12 -0700 | [diff] [blame] | 142 | static void prof_tdata_destroy(tsd_t *tsd, prof_tdata_t *tdata, |
Jason Evans | f04a0be | 2014-10-04 15:03:49 -0700 | [diff] [blame] | 143 | bool even_if_attached); |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 144 | static char *prof_thread_name_alloc(tsdn_t *tsdn, const char *thread_name); |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 145 | |
| 146 | /******************************************************************************/ |
| 147 | /* Red-black trees. */ |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 148 | |
David Goldblatt | 4d2e4bf | 2017-04-21 09:37:34 -0700 | [diff] [blame] | 149 | static int |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 150 | prof_tctx_comp(const prof_tctx_t *a, const prof_tctx_t *b) { |
Jason Evans | 04211e2 | 2015-03-16 15:11:06 -0700 | [diff] [blame] | 151 | uint64_t a_thr_uid = a->thr_uid; |
| 152 | uint64_t b_thr_uid = b->thr_uid; |
| 153 | int ret = (a_thr_uid > b_thr_uid) - (a_thr_uid < b_thr_uid); |
Jason Evans | d69964b | 2015-03-12 16:25:18 -0700 | [diff] [blame] | 154 | if (ret == 0) { |
Jason Evans | a00b107 | 2015-09-09 23:16:10 -0700 | [diff] [blame] | 155 | uint64_t a_thr_discrim = a->thr_discrim; |
| 156 | uint64_t b_thr_discrim = b->thr_discrim; |
| 157 | ret = (a_thr_discrim > b_thr_discrim) - (a_thr_discrim < |
| 158 | b_thr_discrim); |
| 159 | if (ret == 0) { |
| 160 | uint64_t a_tctx_uid = a->tctx_uid; |
| 161 | uint64_t b_tctx_uid = b->tctx_uid; |
| 162 | ret = (a_tctx_uid > b_tctx_uid) - (a_tctx_uid < |
| 163 | b_tctx_uid); |
| 164 | } |
Jason Evans | d69964b | 2015-03-12 16:25:18 -0700 | [diff] [blame] | 165 | } |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 166 | return ret; |
Jason Evans | 3a81cbd | 2014-08-16 12:58:55 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 169 | rb_gen(static UNUSED, tctx_tree_, prof_tctx_tree_t, prof_tctx_t, |
| 170 | tctx_link, prof_tctx_comp) |
Jason Evans | 3a81cbd | 2014-08-16 12:58:55 -0700 | [diff] [blame] | 171 | |
David Goldblatt | 4d2e4bf | 2017-04-21 09:37:34 -0700 | [diff] [blame] | 172 | static int |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 173 | prof_gctx_comp(const prof_gctx_t *a, const prof_gctx_t *b) { |
Jason Evans | 3a81cbd | 2014-08-16 12:58:55 -0700 | [diff] [blame] | 174 | unsigned a_len = a->bt.len; |
| 175 | unsigned b_len = b->bt.len; |
| 176 | unsigned comp_len = (a_len < b_len) ? a_len : b_len; |
| 177 | int ret = memcmp(a->bt.vec, b->bt.vec, comp_len * sizeof(void *)); |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 178 | if (ret == 0) { |
Jason Evans | 3a81cbd | 2014-08-16 12:58:55 -0700 | [diff] [blame] | 179 | ret = (a_len > b_len) - (a_len < b_len); |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 180 | } |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 181 | return ret; |
Jason Evans | 3a81cbd | 2014-08-16 12:58:55 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 184 | rb_gen(static UNUSED, gctx_tree_, prof_gctx_tree_t, prof_gctx_t, dump_link, |
| 185 | prof_gctx_comp) |
| 186 | |
David Goldblatt | 4d2e4bf | 2017-04-21 09:37:34 -0700 | [diff] [blame] | 187 | static int |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 188 | prof_tdata_comp(const prof_tdata_t *a, const prof_tdata_t *b) { |
Jason Evans | 20c31de | 2014-10-02 23:01:10 -0700 | [diff] [blame] | 189 | int ret; |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 190 | uint64_t a_uid = a->thr_uid; |
| 191 | uint64_t b_uid = b->thr_uid; |
| 192 | |
Jason Evans | 20c31de | 2014-10-02 23:01:10 -0700 | [diff] [blame] | 193 | ret = ((a_uid > b_uid) - (a_uid < b_uid)); |
| 194 | if (ret == 0) { |
| 195 | uint64_t a_discrim = a->thr_discrim; |
| 196 | uint64_t b_discrim = b->thr_discrim; |
| 197 | |
| 198 | ret = ((a_discrim > b_discrim) - (a_discrim < b_discrim)); |
| 199 | } |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 200 | return ret; |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | rb_gen(static UNUSED, tdata_tree_, prof_tdata_tree_t, prof_tdata_t, tdata_link, |
| 204 | prof_tdata_comp) |
| 205 | |
| 206 | /******************************************************************************/ |
| 207 | |
| 208 | void |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 209 | prof_alloc_rollback(tsd_t *tsd, prof_tctx_t *tctx, bool updated) { |
Jason Evans | 6e73dc1 | 2014-09-09 19:37:26 -0700 | [diff] [blame] | 210 | prof_tdata_t *tdata; |
| 211 | |
| 212 | cassert(config_prof); |
| 213 | |
| 214 | if (updated) { |
| 215 | /* |
| 216 | * Compute a new sample threshold. This isn't very important in |
| 217 | * practice, because this function is rarely executed, so the |
| 218 | * potential for sample bias is minimal except in contrived |
| 219 | * programs. |
| 220 | */ |
Jason Evans | 5460aa6 | 2014-09-22 21:09:23 -0700 | [diff] [blame] | 221 | tdata = prof_tdata_get(tsd, true); |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 222 | if (tdata != NULL) { |
Jason Evans | 3ca0cf6 | 2015-09-17 14:47:39 -0700 | [diff] [blame] | 223 | prof_sample_threshold_update(tdata); |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 224 | } |
Jason Evans | 6e73dc1 | 2014-09-09 19:37:26 -0700 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | if ((uintptr_t)tctx > (uintptr_t)1U) { |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 228 | malloc_mutex_lock(tsd_tsdn(tsd), tctx->tdata->lock); |
Jason Evans | 6e73dc1 | 2014-09-09 19:37:26 -0700 | [diff] [blame] | 229 | tctx->prepared = false; |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 230 | if (prof_tctx_should_destroy(tsd_tsdn(tsd), tctx)) { |
Jason Evans | 5460aa6 | 2014-09-22 21:09:23 -0700 | [diff] [blame] | 231 | prof_tctx_destroy(tsd, tctx); |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 232 | } else { |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 233 | malloc_mutex_unlock(tsd_tsdn(tsd), tctx->tdata->lock); |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 234 | } |
Jason Evans | 6e73dc1 | 2014-09-09 19:37:26 -0700 | [diff] [blame] | 235 | } |
| 236 | } |
| 237 | |
| 238 | void |
Jason Evans | 5e67fbc | 2017-03-20 11:00:07 -0700 | [diff] [blame] | 239 | prof_malloc_sample_object(tsdn_t *tsdn, const void *ptr, size_t usize, |
| 240 | prof_tctx_t *tctx) { |
Qi Wang | ccfe68a | 2017-04-11 18:13:10 -0700 | [diff] [blame] | 241 | prof_tctx_set(tsdn, ptr, usize, NULL, tctx); |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 242 | |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 243 | malloc_mutex_lock(tsdn, tctx->tdata->lock); |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 244 | tctx->cnts.curobjs++; |
| 245 | tctx->cnts.curbytes += usize; |
| 246 | if (opt_prof_accum) { |
| 247 | tctx->cnts.accumobjs++; |
| 248 | tctx->cnts.accumbytes += usize; |
| 249 | } |
Jason Evans | 6e73dc1 | 2014-09-09 19:37:26 -0700 | [diff] [blame] | 250 | tctx->prepared = false; |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 251 | malloc_mutex_unlock(tsdn, tctx->tdata->lock); |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | void |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 255 | prof_free_sampled_object(tsd_t *tsd, size_t usize, prof_tctx_t *tctx) { |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 256 | malloc_mutex_lock(tsd_tsdn(tsd), tctx->tdata->lock); |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 257 | assert(tctx->cnts.curobjs > 0); |
| 258 | assert(tctx->cnts.curbytes >= usize); |
| 259 | tctx->cnts.curobjs--; |
| 260 | tctx->cnts.curbytes -= usize; |
| 261 | |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 262 | if (prof_tctx_should_destroy(tsd_tsdn(tsd), tctx)) { |
Jason Evans | 5460aa6 | 2014-09-22 21:09:23 -0700 | [diff] [blame] | 263 | prof_tctx_destroy(tsd, tctx); |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 264 | } else { |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 265 | malloc_mutex_unlock(tsd_tsdn(tsd), tctx->tdata->lock); |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 266 | } |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 267 | } |
Jason Evans | 3a81cbd | 2014-08-16 12:58:55 -0700 | [diff] [blame] | 268 | |
Jason Evans | 4d6a134 | 2010-10-20 19:05:59 -0700 | [diff] [blame] | 269 | void |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 270 | bt_init(prof_bt_t *bt, void **vec) { |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 271 | cassert(config_prof); |
| 272 | |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 273 | bt->vec = vec; |
| 274 | bt->len = 0; |
| 275 | } |
| 276 | |
David Goldblatt | 4d2e4bf | 2017-04-21 09:37:34 -0700 | [diff] [blame] | 277 | static void |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 278 | prof_enter(tsd_t *tsd, prof_tdata_t *tdata) { |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 279 | cassert(config_prof); |
Jason Evans | c93ed81 | 2014-10-30 16:50:33 -0700 | [diff] [blame] | 280 | assert(tdata == prof_tdata_get(tsd, false)); |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 281 | |
Jason Evans | 82cb603 | 2014-11-01 00:20:28 -0700 | [diff] [blame] | 282 | if (tdata != NULL) { |
| 283 | assert(!tdata->enq); |
| 284 | tdata->enq = true; |
| 285 | } |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 286 | |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 287 | malloc_mutex_lock(tsd_tsdn(tsd), &bt2gctx_mtx); |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 288 | } |
| 289 | |
David Goldblatt | 4d2e4bf | 2017-04-21 09:37:34 -0700 | [diff] [blame] | 290 | static void |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 291 | prof_leave(tsd_t *tsd, prof_tdata_t *tdata) { |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 292 | cassert(config_prof); |
Jason Evans | c93ed81 | 2014-10-30 16:50:33 -0700 | [diff] [blame] | 293 | assert(tdata == prof_tdata_get(tsd, false)); |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 294 | |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 295 | malloc_mutex_unlock(tsd_tsdn(tsd), &bt2gctx_mtx); |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 296 | |
Jason Evans | 82cb603 | 2014-11-01 00:20:28 -0700 | [diff] [blame] | 297 | if (tdata != NULL) { |
| 298 | bool idump, gdump; |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 299 | |
Jason Evans | 82cb603 | 2014-11-01 00:20:28 -0700 | [diff] [blame] | 300 | assert(tdata->enq); |
| 301 | tdata->enq = false; |
| 302 | idump = tdata->enq_idump; |
| 303 | tdata->enq_idump = false; |
| 304 | gdump = tdata->enq_gdump; |
| 305 | tdata->enq_gdump = false; |
| 306 | |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 307 | if (idump) { |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 308 | prof_idump(tsd_tsdn(tsd)); |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 309 | } |
| 310 | if (gdump) { |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 311 | prof_gdump(tsd_tsdn(tsd)); |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 312 | } |
Jason Evans | 82cb603 | 2014-11-01 00:20:28 -0700 | [diff] [blame] | 313 | } |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 314 | } |
| 315 | |
Jason Evans | 77f350b | 2011-03-15 22:23:12 -0700 | [diff] [blame] | 316 | #ifdef JEMALLOC_PROF_LIBUNWIND |
Jason Evans | 4d6a134 | 2010-10-20 19:05:59 -0700 | [diff] [blame] | 317 | void |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 318 | prof_backtrace(prof_bt_t *bt) { |
Jason Evans | 6f00105 | 2014-04-22 18:41:15 -0700 | [diff] [blame] | 319 | int nframes; |
| 320 | |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 321 | cassert(config_prof); |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 322 | assert(bt->len == 0); |
| 323 | assert(bt->vec != NULL); |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 324 | |
Jason Evans | 6f00105 | 2014-04-22 18:41:15 -0700 | [diff] [blame] | 325 | nframes = unw_backtrace(bt->vec, PROF_BT_MAX); |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 326 | if (nframes <= 0) { |
Lucian Adrian Grijincu | 9d4e13f | 2014-04-21 20:52:35 -0700 | [diff] [blame] | 327 | return; |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 328 | } |
Jason Evans | 6f00105 | 2014-04-22 18:41:15 -0700 | [diff] [blame] | 329 | bt->len = nframes; |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 330 | } |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 331 | #elif (defined(JEMALLOC_PROF_LIBGCC)) |
Jason Evans | 77f350b | 2011-03-15 22:23:12 -0700 | [diff] [blame] | 332 | static _Unwind_Reason_Code |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 333 | prof_unwind_init_callback(struct _Unwind_Context *context, void *arg) { |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 334 | cassert(config_prof); |
| 335 | |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 336 | return _URC_NO_REASON; |
Jason Evans | 77f350b | 2011-03-15 22:23:12 -0700 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | static _Unwind_Reason_Code |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 340 | prof_unwind_callback(struct _Unwind_Context *context, void *arg) { |
Jason Evans | 77f350b | 2011-03-15 22:23:12 -0700 | [diff] [blame] | 341 | prof_unwind_data_t *data = (prof_unwind_data_t *)arg; |
Jason Evans | 6f00105 | 2014-04-22 18:41:15 -0700 | [diff] [blame] | 342 | void *ip; |
Jason Evans | 77f350b | 2011-03-15 22:23:12 -0700 | [diff] [blame] | 343 | |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 344 | cassert(config_prof); |
| 345 | |
Jason Evans | 6f00105 | 2014-04-22 18:41:15 -0700 | [diff] [blame] | 346 | ip = (void *)_Unwind_GetIP(context); |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 347 | if (ip == NULL) { |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 348 | return _URC_END_OF_STACK; |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 349 | } |
Jason Evans | 6f00105 | 2014-04-22 18:41:15 -0700 | [diff] [blame] | 350 | data->bt->vec[data->bt->len] = ip; |
| 351 | data->bt->len++; |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 352 | if (data->bt->len == data->max) { |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 353 | return _URC_END_OF_STACK; |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 354 | } |
Jason Evans | 77f350b | 2011-03-15 22:23:12 -0700 | [diff] [blame] | 355 | |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 356 | return _URC_NO_REASON; |
Jason Evans | 77f350b | 2011-03-15 22:23:12 -0700 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | void |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 360 | prof_backtrace(prof_bt_t *bt) { |
Jason Evans | 6f00105 | 2014-04-22 18:41:15 -0700 | [diff] [blame] | 361 | prof_unwind_data_t data = {bt, PROF_BT_MAX}; |
Jason Evans | 77f350b | 2011-03-15 22:23:12 -0700 | [diff] [blame] | 362 | |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 363 | cassert(config_prof); |
| 364 | |
Jason Evans | 77f350b | 2011-03-15 22:23:12 -0700 | [diff] [blame] | 365 | _Unwind_Backtrace(prof_unwind_callback, &data); |
| 366 | } |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 367 | #elif (defined(JEMALLOC_PROF_GCC)) |
Jason Evans | 4d6a134 | 2010-10-20 19:05:59 -0700 | [diff] [blame] | 368 | void |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 369 | prof_backtrace(prof_bt_t *bt) { |
Jason Evans | c0cc5db | 2017-01-19 21:41:41 -0800 | [diff] [blame] | 370 | #define BT_FRAME(i) \ |
Jason Evans | 6f00105 | 2014-04-22 18:41:15 -0700 | [diff] [blame] | 371 | if ((i) < PROF_BT_MAX) { \ |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 372 | void *p; \ |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 373 | if (__builtin_frame_address(i) == 0) { \ |
Jason Evans | b27805b | 2010-02-10 18:15:53 -0800 | [diff] [blame] | 374 | return; \ |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 375 | } \ |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 376 | p = __builtin_return_address(i); \ |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 377 | if (p == NULL) { \ |
Jason Evans | b27805b | 2010-02-10 18:15:53 -0800 | [diff] [blame] | 378 | return; \ |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 379 | } \ |
Jason Evans | 6f00105 | 2014-04-22 18:41:15 -0700 | [diff] [blame] | 380 | bt->vec[(i)] = p; \ |
| 381 | bt->len = (i) + 1; \ |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 382 | } else { \ |
| 383 | return; \ |
| 384 | } |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 385 | |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 386 | cassert(config_prof); |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 387 | |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 388 | BT_FRAME(0) |
| 389 | BT_FRAME(1) |
| 390 | BT_FRAME(2) |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 391 | BT_FRAME(3) |
| 392 | BT_FRAME(4) |
| 393 | BT_FRAME(5) |
| 394 | BT_FRAME(6) |
| 395 | BT_FRAME(7) |
| 396 | BT_FRAME(8) |
| 397 | BT_FRAME(9) |
| 398 | |
| 399 | BT_FRAME(10) |
| 400 | BT_FRAME(11) |
| 401 | BT_FRAME(12) |
| 402 | BT_FRAME(13) |
| 403 | BT_FRAME(14) |
| 404 | BT_FRAME(15) |
| 405 | BT_FRAME(16) |
| 406 | BT_FRAME(17) |
| 407 | BT_FRAME(18) |
| 408 | BT_FRAME(19) |
| 409 | |
| 410 | BT_FRAME(20) |
| 411 | BT_FRAME(21) |
| 412 | BT_FRAME(22) |
| 413 | BT_FRAME(23) |
| 414 | BT_FRAME(24) |
| 415 | BT_FRAME(25) |
| 416 | BT_FRAME(26) |
| 417 | BT_FRAME(27) |
| 418 | BT_FRAME(28) |
| 419 | BT_FRAME(29) |
| 420 | |
| 421 | BT_FRAME(30) |
| 422 | BT_FRAME(31) |
| 423 | BT_FRAME(32) |
| 424 | BT_FRAME(33) |
| 425 | BT_FRAME(34) |
| 426 | BT_FRAME(35) |
| 427 | BT_FRAME(36) |
| 428 | BT_FRAME(37) |
| 429 | BT_FRAME(38) |
| 430 | BT_FRAME(39) |
| 431 | |
| 432 | BT_FRAME(40) |
| 433 | BT_FRAME(41) |
| 434 | BT_FRAME(42) |
| 435 | BT_FRAME(43) |
| 436 | BT_FRAME(44) |
| 437 | BT_FRAME(45) |
| 438 | BT_FRAME(46) |
| 439 | BT_FRAME(47) |
| 440 | BT_FRAME(48) |
| 441 | BT_FRAME(49) |
| 442 | |
| 443 | BT_FRAME(50) |
| 444 | BT_FRAME(51) |
| 445 | BT_FRAME(52) |
| 446 | BT_FRAME(53) |
| 447 | BT_FRAME(54) |
| 448 | BT_FRAME(55) |
| 449 | BT_FRAME(56) |
| 450 | BT_FRAME(57) |
| 451 | BT_FRAME(58) |
| 452 | BT_FRAME(59) |
| 453 | |
| 454 | BT_FRAME(60) |
| 455 | BT_FRAME(61) |
| 456 | BT_FRAME(62) |
| 457 | BT_FRAME(63) |
| 458 | BT_FRAME(64) |
| 459 | BT_FRAME(65) |
| 460 | BT_FRAME(66) |
| 461 | BT_FRAME(67) |
| 462 | BT_FRAME(68) |
| 463 | BT_FRAME(69) |
| 464 | |
| 465 | BT_FRAME(70) |
| 466 | BT_FRAME(71) |
| 467 | BT_FRAME(72) |
| 468 | BT_FRAME(73) |
| 469 | BT_FRAME(74) |
| 470 | BT_FRAME(75) |
| 471 | BT_FRAME(76) |
| 472 | BT_FRAME(77) |
| 473 | BT_FRAME(78) |
| 474 | BT_FRAME(79) |
| 475 | |
| 476 | BT_FRAME(80) |
| 477 | BT_FRAME(81) |
| 478 | BT_FRAME(82) |
| 479 | BT_FRAME(83) |
| 480 | BT_FRAME(84) |
| 481 | BT_FRAME(85) |
| 482 | BT_FRAME(86) |
| 483 | BT_FRAME(87) |
| 484 | BT_FRAME(88) |
| 485 | BT_FRAME(89) |
| 486 | |
| 487 | BT_FRAME(90) |
| 488 | BT_FRAME(91) |
| 489 | BT_FRAME(92) |
| 490 | BT_FRAME(93) |
| 491 | BT_FRAME(94) |
| 492 | BT_FRAME(95) |
| 493 | BT_FRAME(96) |
| 494 | BT_FRAME(97) |
| 495 | BT_FRAME(98) |
| 496 | BT_FRAME(99) |
| 497 | |
| 498 | BT_FRAME(100) |
| 499 | BT_FRAME(101) |
| 500 | BT_FRAME(102) |
| 501 | BT_FRAME(103) |
| 502 | BT_FRAME(104) |
| 503 | BT_FRAME(105) |
| 504 | BT_FRAME(106) |
| 505 | BT_FRAME(107) |
| 506 | BT_FRAME(108) |
| 507 | BT_FRAME(109) |
| 508 | |
| 509 | BT_FRAME(110) |
| 510 | BT_FRAME(111) |
| 511 | BT_FRAME(112) |
| 512 | BT_FRAME(113) |
| 513 | BT_FRAME(114) |
| 514 | BT_FRAME(115) |
| 515 | BT_FRAME(116) |
| 516 | BT_FRAME(117) |
| 517 | BT_FRAME(118) |
| 518 | BT_FRAME(119) |
| 519 | |
| 520 | BT_FRAME(120) |
| 521 | BT_FRAME(121) |
| 522 | BT_FRAME(122) |
| 523 | BT_FRAME(123) |
| 524 | BT_FRAME(124) |
| 525 | BT_FRAME(125) |
| 526 | BT_FRAME(126) |
| 527 | BT_FRAME(127) |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 528 | #undef BT_FRAME |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 529 | } |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 530 | #else |
| 531 | void |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 532 | prof_backtrace(prof_bt_t *bt) { |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 533 | cassert(config_prof); |
Jason Evans | 6556e28 | 2013-10-21 14:56:27 -0700 | [diff] [blame] | 534 | not_reached(); |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 535 | } |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 536 | #endif |
| 537 | |
Jason Evans | 4f37ef6 | 2014-01-16 13:23:56 -0800 | [diff] [blame] | 538 | static malloc_mutex_t * |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 539 | prof_gctx_mutex_choose(void) { |
David Goldblatt | 074f225 | 2017-04-04 18:36:45 -0700 | [diff] [blame] | 540 | unsigned ngctxs = atomic_fetch_add_u(&cum_gctxs, 1, ATOMIC_RELAXED); |
Jason Evans | 4f37ef6 | 2014-01-16 13:23:56 -0800 | [diff] [blame] | 541 | |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 542 | return &gctx_locks[(ngctxs - 1) % PROF_NCTX_LOCKS]; |
Jason Evans | 4f37ef6 | 2014-01-16 13:23:56 -0800 | [diff] [blame] | 543 | } |
| 544 | |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 545 | static malloc_mutex_t * |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 546 | prof_tdata_mutex_choose(uint64_t thr_uid) { |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 547 | return &tdata_locks[thr_uid % PROF_NTDATA_LOCKS]; |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | static prof_gctx_t * |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 551 | prof_gctx_create(tsdn_t *tsdn, prof_bt_t *bt) { |
Jason Evans | ab532e9 | 2014-08-15 15:05:12 -0700 | [diff] [blame] | 552 | /* |
| 553 | * Create a single allocation that has space for vec of length bt->len. |
| 554 | */ |
Qi Wang | f4a0f32 | 2015-10-27 15:12:10 -0700 | [diff] [blame] | 555 | size_t size = offsetof(prof_gctx_t, vec) + (bt->len * sizeof(void *)); |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 556 | prof_gctx_t *gctx = (prof_gctx_t *)iallocztm(tsdn, size, |
| 557 | size2index(size), false, NULL, true, arena_get(TSDN_NULL, 0, true), |
Jason Evans | 66cd953 | 2016-04-22 14:34:14 -0700 | [diff] [blame] | 558 | true); |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 559 | if (gctx == NULL) { |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 560 | return NULL; |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 561 | } |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 562 | gctx->lock = prof_gctx_mutex_choose(); |
Jason Evans | 4f37ef6 | 2014-01-16 13:23:56 -0800 | [diff] [blame] | 563 | /* |
| 564 | * Set nlimbo to 1, in order to avoid a race condition with |
Jason Evans | 20c31de | 2014-10-02 23:01:10 -0700 | [diff] [blame] | 565 | * prof_tctx_destroy()/prof_gctx_try_destroy(). |
Jason Evans | 4f37ef6 | 2014-01-16 13:23:56 -0800 | [diff] [blame] | 566 | */ |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 567 | gctx->nlimbo = 1; |
| 568 | tctx_tree_new(&gctx->tctxs); |
Jason Evans | ab532e9 | 2014-08-15 15:05:12 -0700 | [diff] [blame] | 569 | /* Duplicate bt. */ |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 570 | memcpy(gctx->vec, bt->vec, bt->len * sizeof(void *)); |
| 571 | gctx->bt.vec = gctx->vec; |
| 572 | gctx->bt.len = bt->len; |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 573 | return gctx; |
Jason Evans | 4f37ef6 | 2014-01-16 13:23:56 -0800 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | static void |
Jason Evans | c93ed81 | 2014-10-30 16:50:33 -0700 | [diff] [blame] | 577 | prof_gctx_try_destroy(tsd_t *tsd, prof_tdata_t *tdata_self, prof_gctx_t *gctx, |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 578 | prof_tdata_t *tdata) { |
Jason Evans | 4f37ef6 | 2014-01-16 13:23:56 -0800 | [diff] [blame] | 579 | cassert(config_prof); |
| 580 | |
| 581 | /* |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 582 | * Check that gctx is still unused by any thread cache before destroying |
| 583 | * it. prof_lookup() increments gctx->nlimbo in order to avoid a race |
| 584 | * condition with this function, as does prof_tctx_destroy() in order to |
| 585 | * avoid a race between the main body of prof_tctx_destroy() and entry |
Jason Evans | 4f37ef6 | 2014-01-16 13:23:56 -0800 | [diff] [blame] | 586 | * into this function. |
| 587 | */ |
Jason Evans | c93ed81 | 2014-10-30 16:50:33 -0700 | [diff] [blame] | 588 | prof_enter(tsd, tdata_self); |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 589 | malloc_mutex_lock(tsd_tsdn(tsd), gctx->lock); |
Jason Evans | 20c31de | 2014-10-02 23:01:10 -0700 | [diff] [blame] | 590 | assert(gctx->nlimbo != 0); |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 591 | if (tctx_tree_empty(&gctx->tctxs) && gctx->nlimbo == 1) { |
| 592 | /* Remove gctx from bt2gctx. */ |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 593 | if (ckh_remove(tsd, &bt2gctx, &gctx->bt, NULL, NULL)) { |
Jason Evans | 4f37ef6 | 2014-01-16 13:23:56 -0800 | [diff] [blame] | 594 | not_reached(); |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 595 | } |
Jason Evans | c93ed81 | 2014-10-30 16:50:33 -0700 | [diff] [blame] | 596 | prof_leave(tsd, tdata_self); |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 597 | /* Destroy gctx. */ |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 598 | malloc_mutex_unlock(tsd_tsdn(tsd), gctx->lock); |
Qi Wang | bfa530b | 2017-04-07 14:12:30 -0700 | [diff] [blame] | 599 | idalloctm(tsd_tsdn(tsd), gctx, NULL, NULL, true, true); |
Jason Evans | 4f37ef6 | 2014-01-16 13:23:56 -0800 | [diff] [blame] | 600 | } else { |
| 601 | /* |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 602 | * Compensate for increment in prof_tctx_destroy() or |
Jason Evans | 4f37ef6 | 2014-01-16 13:23:56 -0800 | [diff] [blame] | 603 | * prof_lookup(). |
| 604 | */ |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 605 | gctx->nlimbo--; |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 606 | malloc_mutex_unlock(tsd_tsdn(tsd), gctx->lock); |
Jason Evans | c93ed81 | 2014-10-30 16:50:33 -0700 | [diff] [blame] | 607 | prof_leave(tsd, tdata_self); |
Jason Evans | 4f37ef6 | 2014-01-16 13:23:56 -0800 | [diff] [blame] | 608 | } |
| 609 | } |
| 610 | |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 611 | static bool |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 612 | prof_tctx_should_destroy(tsdn_t *tsdn, prof_tctx_t *tctx) { |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 613 | malloc_mutex_assert_owner(tsdn, tctx->tdata->lock); |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 614 | |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 615 | if (opt_prof_accum) { |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 616 | return false; |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 617 | } |
| 618 | if (tctx->cnts.curobjs != 0) { |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 619 | return false; |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 620 | } |
| 621 | if (tctx->prepared) { |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 622 | return false; |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 623 | } |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 624 | return true; |
Jason Evans | 4f37ef6 | 2014-01-16 13:23:56 -0800 | [diff] [blame] | 625 | } |
| 626 | |
Jason Evans | fb1775e | 2014-01-14 17:04:34 -0800 | [diff] [blame] | 627 | static bool |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 628 | prof_gctx_should_destroy(prof_gctx_t *gctx) { |
| 629 | if (opt_prof_accum) { |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 630 | return false; |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 631 | } |
| 632 | if (!tctx_tree_empty(&gctx->tctxs)) { |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 633 | return false; |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 634 | } |
| 635 | if (gctx->nlimbo != 0) { |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 636 | return false; |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 637 | } |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 638 | return true; |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 639 | } |
| 640 | |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 641 | static void |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 642 | prof_tctx_destroy(tsd_t *tsd, prof_tctx_t *tctx) { |
Jason Evans | 6fd53da | 2014-09-09 12:45:53 -0700 | [diff] [blame] | 643 | prof_tdata_t *tdata = tctx->tdata; |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 644 | prof_gctx_t *gctx = tctx->gctx; |
Jason Evans | bf40641 | 2014-10-06 16:35:11 -0700 | [diff] [blame] | 645 | bool destroy_tdata, destroy_tctx, destroy_gctx; |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 646 | |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 647 | malloc_mutex_assert_owner(tsd_tsdn(tsd), tctx->tdata->lock); |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 648 | |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 649 | assert(tctx->cnts.curobjs == 0); |
| 650 | assert(tctx->cnts.curbytes == 0); |
Jason Evans | 551ebc4 | 2014-10-03 10:16:09 -0700 | [diff] [blame] | 651 | assert(!opt_prof_accum); |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 652 | assert(tctx->cnts.accumobjs == 0); |
| 653 | assert(tctx->cnts.accumbytes == 0); |
| 654 | |
Jason Evans | b54d160 | 2016-10-20 23:59:12 -0700 | [diff] [blame] | 655 | ckh_remove(tsd, &tdata->bt2tctx, &gctx->bt, NULL, NULL); |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 656 | destroy_tdata = prof_tdata_should_destroy(tsd_tsdn(tsd), tdata, false); |
| 657 | malloc_mutex_unlock(tsd_tsdn(tsd), tdata->lock); |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 658 | |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 659 | malloc_mutex_lock(tsd_tsdn(tsd), gctx->lock); |
Jason Evans | 764b000 | 2015-03-14 14:01:35 -0700 | [diff] [blame] | 660 | switch (tctx->state) { |
Jason Evans | 04211e2 | 2015-03-16 15:11:06 -0700 | [diff] [blame] | 661 | case prof_tctx_state_nominal: |
Jason Evans | bf40641 | 2014-10-06 16:35:11 -0700 | [diff] [blame] | 662 | tctx_tree_remove(&gctx->tctxs, tctx); |
| 663 | destroy_tctx = true; |
| 664 | if (prof_gctx_should_destroy(gctx)) { |
| 665 | /* |
| 666 | * Increment gctx->nlimbo in order to keep another |
| 667 | * thread from winning the race to destroy gctx while |
| 668 | * this one has gctx->lock dropped. Without this, it |
| 669 | * would be possible for another thread to: |
| 670 | * |
| 671 | * 1) Sample an allocation associated with gctx. |
| 672 | * 2) Deallocate the sampled object. |
| 673 | * 3) Successfully prof_gctx_try_destroy(gctx). |
| 674 | * |
| 675 | * The result would be that gctx no longer exists by the |
| 676 | * time this thread accesses it in |
| 677 | * prof_gctx_try_destroy(). |
| 678 | */ |
| 679 | gctx->nlimbo++; |
| 680 | destroy_gctx = true; |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 681 | } else { |
Jason Evans | bf40641 | 2014-10-06 16:35:11 -0700 | [diff] [blame] | 682 | destroy_gctx = false; |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 683 | } |
Jason Evans | 764b000 | 2015-03-14 14:01:35 -0700 | [diff] [blame] | 684 | break; |
| 685 | case prof_tctx_state_dumping: |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 686 | /* |
Jason Evans | bf40641 | 2014-10-06 16:35:11 -0700 | [diff] [blame] | 687 | * A dumping thread needs tctx to remain valid until dumping |
| 688 | * has finished. Change state such that the dumping thread will |
| 689 | * complete destruction during a late dump iteration phase. |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 690 | */ |
Jason Evans | bf40641 | 2014-10-06 16:35:11 -0700 | [diff] [blame] | 691 | tctx->state = prof_tctx_state_purgatory; |
| 692 | destroy_tctx = false; |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 693 | destroy_gctx = false; |
Jason Evans | 764b000 | 2015-03-14 14:01:35 -0700 | [diff] [blame] | 694 | break; |
| 695 | default: |
| 696 | not_reached(); |
Jason Evans | 262146d | 2015-03-14 14:34:16 -0700 | [diff] [blame] | 697 | destroy_tctx = false; |
| 698 | destroy_gctx = false; |
Jason Evans | bf40641 | 2014-10-06 16:35:11 -0700 | [diff] [blame] | 699 | } |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 700 | malloc_mutex_unlock(tsd_tsdn(tsd), gctx->lock); |
Jason Evans | c93ed81 | 2014-10-30 16:50:33 -0700 | [diff] [blame] | 701 | if (destroy_gctx) { |
| 702 | prof_gctx_try_destroy(tsd, prof_tdata_get(tsd, false), gctx, |
| 703 | tdata); |
| 704 | } |
Jason Evans | 6fd53da | 2014-09-09 12:45:53 -0700 | [diff] [blame] | 705 | |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 706 | malloc_mutex_assert_not_owner(tsd_tsdn(tsd), tctx->tdata->lock); |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 707 | |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 708 | if (destroy_tdata) { |
Jason Evans | b54d160 | 2016-10-20 23:59:12 -0700 | [diff] [blame] | 709 | prof_tdata_destroy(tsd, tdata, false); |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 710 | } |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 711 | |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 712 | if (destroy_tctx) { |
Qi Wang | bfa530b | 2017-04-07 14:12:30 -0700 | [diff] [blame] | 713 | idalloctm(tsd_tsdn(tsd), tctx, NULL, NULL, true, true); |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 714 | } |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 715 | } |
| 716 | |
| 717 | static bool |
Jason Evans | 5460aa6 | 2014-09-22 21:09:23 -0700 | [diff] [blame] | 718 | prof_lookup_global(tsd_t *tsd, prof_bt_t *bt, prof_tdata_t *tdata, |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 719 | void **p_btkey, prof_gctx_t **p_gctx, bool *p_new_gctx) { |
Jason Evans | fb1775e | 2014-01-14 17:04:34 -0800 | [diff] [blame] | 720 | union { |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 721 | prof_gctx_t *p; |
Jason Evans | fb1775e | 2014-01-14 17:04:34 -0800 | [diff] [blame] | 722 | void *v; |
Jason Evans | 5033a91 | 2017-01-29 21:51:30 -0800 | [diff] [blame] | 723 | } gctx, tgctx; |
Jason Evans | fb1775e | 2014-01-14 17:04:34 -0800 | [diff] [blame] | 724 | union { |
| 725 | prof_bt_t *p; |
| 726 | void *v; |
| 727 | } btkey; |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 728 | bool new_gctx; |
Jason Evans | fb1775e | 2014-01-14 17:04:34 -0800 | [diff] [blame] | 729 | |
Jason Evans | c93ed81 | 2014-10-30 16:50:33 -0700 | [diff] [blame] | 730 | prof_enter(tsd, tdata); |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 731 | if (ckh_search(&bt2gctx, bt, &btkey.v, &gctx.v)) { |
Jason Evans | fb1775e | 2014-01-14 17:04:34 -0800 | [diff] [blame] | 732 | /* bt has never been seen before. Insert it. */ |
Jason Evans | 5033a91 | 2017-01-29 21:51:30 -0800 | [diff] [blame] | 733 | prof_leave(tsd, tdata); |
| 734 | tgctx.p = prof_gctx_create(tsd_tsdn(tsd), bt); |
| 735 | if (tgctx.v == NULL) { |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 736 | return true; |
Jason Evans | fb1775e | 2014-01-14 17:04:34 -0800 | [diff] [blame] | 737 | } |
Jason Evans | 5033a91 | 2017-01-29 21:51:30 -0800 | [diff] [blame] | 738 | prof_enter(tsd, tdata); |
| 739 | if (ckh_search(&bt2gctx, bt, &btkey.v, &gctx.v)) { |
| 740 | gctx.p = tgctx.p; |
| 741 | btkey.p = &gctx.p->bt; |
| 742 | if (ckh_insert(tsd, &bt2gctx, btkey.v, gctx.v)) { |
| 743 | /* OOM. */ |
| 744 | prof_leave(tsd, tdata); |
Qi Wang | bfa530b | 2017-04-07 14:12:30 -0700 | [diff] [blame] | 745 | idalloctm(tsd_tsdn(tsd), gctx.v, NULL, NULL, |
| 746 | true, true); |
Jason Evans | 5033a91 | 2017-01-29 21:51:30 -0800 | [diff] [blame] | 747 | return true; |
| 748 | } |
| 749 | new_gctx = true; |
| 750 | } else { |
| 751 | new_gctx = false; |
Jason Evans | fb1775e | 2014-01-14 17:04:34 -0800 | [diff] [blame] | 752 | } |
Jason Evans | fb1775e | 2014-01-14 17:04:34 -0800 | [diff] [blame] | 753 | } else { |
Jason Evans | 5033a91 | 2017-01-29 21:51:30 -0800 | [diff] [blame] | 754 | tgctx.v = NULL; |
| 755 | new_gctx = false; |
| 756 | } |
| 757 | |
| 758 | if (!new_gctx) { |
Jason Evans | fb1775e | 2014-01-14 17:04:34 -0800 | [diff] [blame] | 759 | /* |
| 760 | * Increment nlimbo, in order to avoid a race condition with |
Jason Evans | 20c31de | 2014-10-02 23:01:10 -0700 | [diff] [blame] | 761 | * prof_tctx_destroy()/prof_gctx_try_destroy(). |
Jason Evans | fb1775e | 2014-01-14 17:04:34 -0800 | [diff] [blame] | 762 | */ |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 763 | malloc_mutex_lock(tsd_tsdn(tsd), gctx.p->lock); |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 764 | gctx.p->nlimbo++; |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 765 | malloc_mutex_unlock(tsd_tsdn(tsd), gctx.p->lock); |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 766 | new_gctx = false; |
Jason Evans | 5033a91 | 2017-01-29 21:51:30 -0800 | [diff] [blame] | 767 | |
| 768 | if (tgctx.v != NULL) { |
| 769 | /* Lost race to insert. */ |
Qi Wang | bfa530b | 2017-04-07 14:12:30 -0700 | [diff] [blame] | 770 | idalloctm(tsd_tsdn(tsd), tgctx.v, NULL, NULL, true, |
| 771 | true); |
Jason Evans | 5033a91 | 2017-01-29 21:51:30 -0800 | [diff] [blame] | 772 | } |
Jason Evans | fb1775e | 2014-01-14 17:04:34 -0800 | [diff] [blame] | 773 | } |
Jason Evans | c93ed81 | 2014-10-30 16:50:33 -0700 | [diff] [blame] | 774 | prof_leave(tsd, tdata); |
Jason Evans | fb1775e | 2014-01-14 17:04:34 -0800 | [diff] [blame] | 775 | |
| 776 | *p_btkey = btkey.v; |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 777 | *p_gctx = gctx.p; |
| 778 | *p_new_gctx = new_gctx; |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 779 | return false; |
Jason Evans | fb1775e | 2014-01-14 17:04:34 -0800 | [diff] [blame] | 780 | } |
| 781 | |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 782 | prof_tctx_t * |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 783 | prof_lookup(tsd_t *tsd, prof_bt_t *bt) { |
Jason Evans | 075e77c | 2010-09-20 19:53:25 -0700 | [diff] [blame] | 784 | union { |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 785 | prof_tctx_t *p; |
Jason Evans | 075e77c | 2010-09-20 19:53:25 -0700 | [diff] [blame] | 786 | void *v; |
| 787 | } ret; |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 788 | prof_tdata_t *tdata; |
| 789 | bool not_found; |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 790 | |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 791 | cassert(config_prof); |
| 792 | |
Jason Evans | 5460aa6 | 2014-09-22 21:09:23 -0700 | [diff] [blame] | 793 | tdata = prof_tdata_get(tsd, false); |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 794 | if (tdata == NULL) { |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 795 | return NULL; |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 796 | } |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 797 | |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 798 | malloc_mutex_lock(tsd_tsdn(tsd), tdata->lock); |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 799 | not_found = ckh_search(&tdata->bt2tctx, bt, NULL, &ret.v); |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 800 | if (!not_found) { /* Note double negative! */ |
Jason Evans | 6e73dc1 | 2014-09-09 19:37:26 -0700 | [diff] [blame] | 801 | ret.p->prepared = true; |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 802 | } |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 803 | malloc_mutex_unlock(tsd_tsdn(tsd), tdata->lock); |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 804 | if (not_found) { |
Jason Evans | fb1775e | 2014-01-14 17:04:34 -0800 | [diff] [blame] | 805 | void *btkey; |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 806 | prof_gctx_t *gctx; |
| 807 | bool new_gctx, error; |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 808 | |
| 809 | /* |
| 810 | * This thread's cache lacks bt. Look for it in the global |
| 811 | * cache. |
| 812 | */ |
Jason Evans | 5460aa6 | 2014-09-22 21:09:23 -0700 | [diff] [blame] | 813 | if (prof_lookup_global(tsd, bt, tdata, &btkey, &gctx, |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 814 | &new_gctx)) { |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 815 | return NULL; |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 816 | } |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 817 | |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 818 | /* Link a prof_tctx_t into gctx for this thread. */ |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 819 | ret.v = iallocztm(tsd_tsdn(tsd), sizeof(prof_tctx_t), |
Jason Evans | 66cd953 | 2016-04-22 14:34:14 -0700 | [diff] [blame] | 820 | size2index(sizeof(prof_tctx_t)), false, NULL, true, |
Jason Evans | b54d160 | 2016-10-20 23:59:12 -0700 | [diff] [blame] | 821 | arena_ichoose(tsd, NULL), true); |
Jason Evans | b41ccdb | 2014-08-15 15:01:15 -0700 | [diff] [blame] | 822 | if (ret.p == NULL) { |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 823 | if (new_gctx) { |
Jason Evans | c93ed81 | 2014-10-30 16:50:33 -0700 | [diff] [blame] | 824 | prof_gctx_try_destroy(tsd, tdata, gctx, tdata); |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 825 | } |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 826 | return NULL; |
Jason Evans | a881cd2 | 2010-10-02 15:18:50 -0700 | [diff] [blame] | 827 | } |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 828 | ret.p->tdata = tdata; |
Jason Evans | 44c97b7 | 2014-10-12 13:03:20 -0700 | [diff] [blame] | 829 | ret.p->thr_uid = tdata->thr_uid; |
Jason Evans | a00b107 | 2015-09-09 23:16:10 -0700 | [diff] [blame] | 830 | ret.p->thr_discrim = tdata->thr_discrim; |
Jason Evans | 075e77c | 2010-09-20 19:53:25 -0700 | [diff] [blame] | 831 | memset(&ret.p->cnts, 0, sizeof(prof_cnt_t)); |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 832 | ret.p->gctx = gctx; |
Jason Evans | 04211e2 | 2015-03-16 15:11:06 -0700 | [diff] [blame] | 833 | ret.p->tctx_uid = tdata->tctx_uid_next++; |
Jason Evans | 6e73dc1 | 2014-09-09 19:37:26 -0700 | [diff] [blame] | 834 | ret.p->prepared = true; |
Jason Evans | 6ef80d6 | 2014-09-24 22:14:21 -0700 | [diff] [blame] | 835 | ret.p->state = prof_tctx_state_initializing; |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 836 | malloc_mutex_lock(tsd_tsdn(tsd), tdata->lock); |
Jason Evans | b54d160 | 2016-10-20 23:59:12 -0700 | [diff] [blame] | 837 | error = ckh_insert(tsd, &tdata->bt2tctx, btkey, ret.v); |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 838 | malloc_mutex_unlock(tsd_tsdn(tsd), tdata->lock); |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 839 | if (error) { |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 840 | if (new_gctx) { |
Jason Evans | c93ed81 | 2014-10-30 16:50:33 -0700 | [diff] [blame] | 841 | prof_gctx_try_destroy(tsd, tdata, gctx, tdata); |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 842 | } |
Qi Wang | bfa530b | 2017-04-07 14:12:30 -0700 | [diff] [blame] | 843 | idalloctm(tsd_tsdn(tsd), ret.v, NULL, NULL, true, true); |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 844 | return NULL; |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 845 | } |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 846 | malloc_mutex_lock(tsd_tsdn(tsd), gctx->lock); |
Jason Evans | 6ef80d6 | 2014-09-24 22:14:21 -0700 | [diff] [blame] | 847 | ret.p->state = prof_tctx_state_nominal; |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 848 | tctx_tree_insert(&gctx->tctxs, ret.p); |
| 849 | gctx->nlimbo--; |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 850 | malloc_mutex_unlock(tsd_tsdn(tsd), gctx->lock); |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 851 | } |
| 852 | |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 853 | return ret.p; |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 854 | } |
| 855 | |
Jason Evans | dc391ad | 2016-05-04 12:14:36 -0700 | [diff] [blame] | 856 | /* |
| 857 | * The bodies of this function and prof_leakcheck() are compiled out unless heap |
| 858 | * profiling is enabled, so that it is possible to compile jemalloc with |
| 859 | * floating point support completely disabled. Avoiding floating point code is |
| 860 | * important on memory-constrained systems, but it also enables a workaround for |
| 861 | * versions of glibc that don't properly save/restore floating point registers |
| 862 | * during dynamic lazy symbol loading (which internally calls into whatever |
| 863 | * malloc implementation happens to be integrated into the application). Note |
| 864 | * that some compilers (e.g. gcc 4.8) may use floating point registers for fast |
| 865 | * memory moves, so jemalloc must be compiled with such optimizations disabled |
| 866 | * (e.g. |
| 867 | * -mno-sse) in order for the workaround to be complete. |
| 868 | */ |
Ben Maurer | 6c39f9e | 2014-04-15 13:47:13 -0700 | [diff] [blame] | 869 | void |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 870 | prof_sample_threshold_update(prof_tdata_t *tdata) { |
Ben Maurer | 6c39f9e | 2014-04-15 13:47:13 -0700 | [diff] [blame] | 871 | #ifdef JEMALLOC_PROF |
| 872 | uint64_t r; |
| 873 | double u; |
| 874 | |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 875 | if (!config_prof) { |
Ben Maurer | 6c39f9e | 2014-04-15 13:47:13 -0700 | [diff] [blame] | 876 | return; |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 877 | } |
Ben Maurer | 6c39f9e | 2014-04-15 13:47:13 -0700 | [diff] [blame] | 878 | |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 879 | if (lg_prof_sample == 0) { |
| 880 | tdata->bytes_until_sample = 0; |
Ben Maurer | 6c39f9e | 2014-04-15 13:47:13 -0700 | [diff] [blame] | 881 | return; |
| 882 | } |
| 883 | |
| 884 | /* |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 885 | * Compute sample interval as a geometrically distributed random |
| 886 | * variable with mean (2^lg_prof_sample). |
Ben Maurer | 6c39f9e | 2014-04-15 13:47:13 -0700 | [diff] [blame] | 887 | * |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 888 | * __ __ |
| 889 | * | log(u) | 1 |
| 890 | * tdata->bytes_until_sample = | -------- |, where p = --------------- |
| 891 | * | log(1-p) | lg_prof_sample |
| 892 | * 2 |
Ben Maurer | 6c39f9e | 2014-04-15 13:47:13 -0700 | [diff] [blame] | 893 | * |
| 894 | * For more information on the math, see: |
| 895 | * |
| 896 | * Non-Uniform Random Variate Generation |
| 897 | * Luc Devroye |
| 898 | * Springer-Verlag, New York, 1986 |
| 899 | * pp 500 |
| 900 | * (http://luc.devroye.org/rnbookindex.html) |
| 901 | */ |
Jason Evans | 04b4635 | 2016-11-07 10:52:44 -0800 | [diff] [blame] | 902 | r = prng_lg_range_u64(&tdata->prng_state, 53); |
Ben Maurer | 6c39f9e | 2014-04-15 13:47:13 -0700 | [diff] [blame] | 903 | u = (double)r * (1.0/9007199254740992.0L); |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 904 | tdata->bytes_until_sample = (uint64_t)(log(u) / |
| 905 | log(1.0 - (1.0 / (double)((uint64_t)1U << lg_prof_sample)))) |
Ben Maurer | 6c39f9e | 2014-04-15 13:47:13 -0700 | [diff] [blame] | 906 | + (uint64_t)1U; |
| 907 | #endif |
| 908 | } |
| 909 | |
Jason Evans | 772163b | 2014-01-17 15:40:52 -0800 | [diff] [blame] | 910 | #ifdef JEMALLOC_JET |
Jason Evans | 20c31de | 2014-10-02 23:01:10 -0700 | [diff] [blame] | 911 | static prof_tdata_t * |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 912 | prof_tdata_count_iter(prof_tdata_tree_t *tdatas, prof_tdata_t *tdata, |
| 913 | void *arg) { |
Jason Evans | 20c31de | 2014-10-02 23:01:10 -0700 | [diff] [blame] | 914 | size_t *tdata_count = (size_t *)arg; |
| 915 | |
| 916 | (*tdata_count)++; |
| 917 | |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 918 | return NULL; |
Jason Evans | 20c31de | 2014-10-02 23:01:10 -0700 | [diff] [blame] | 919 | } |
| 920 | |
| 921 | size_t |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 922 | prof_tdata_count(void) { |
Jason Evans | 20c31de | 2014-10-02 23:01:10 -0700 | [diff] [blame] | 923 | size_t tdata_count = 0; |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 924 | tsdn_t *tsdn; |
Jason Evans | 20c31de | 2014-10-02 23:01:10 -0700 | [diff] [blame] | 925 | |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 926 | tsdn = tsdn_fetch(); |
| 927 | malloc_mutex_lock(tsdn, &tdatas_mtx); |
Jason Evans | 20c31de | 2014-10-02 23:01:10 -0700 | [diff] [blame] | 928 | tdata_tree_iter(&tdatas, NULL, prof_tdata_count_iter, |
| 929 | (void *)&tdata_count); |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 930 | malloc_mutex_unlock(tsdn, &tdatas_mtx); |
Jason Evans | 20c31de | 2014-10-02 23:01:10 -0700 | [diff] [blame] | 931 | |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 932 | return tdata_count; |
Jason Evans | 20c31de | 2014-10-02 23:01:10 -0700 | [diff] [blame] | 933 | } |
| 934 | #endif |
| 935 | |
| 936 | #ifdef JEMALLOC_JET |
Jason Evans | 772163b | 2014-01-17 15:40:52 -0800 | [diff] [blame] | 937 | size_t |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 938 | prof_bt_count(void) { |
Jason Evans | 772163b | 2014-01-17 15:40:52 -0800 | [diff] [blame] | 939 | size_t bt_count; |
Jason Evans | 5460aa6 | 2014-09-22 21:09:23 -0700 | [diff] [blame] | 940 | tsd_t *tsd; |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 941 | prof_tdata_t *tdata; |
Jason Evans | 772163b | 2014-01-17 15:40:52 -0800 | [diff] [blame] | 942 | |
Jason Evans | 029d44c | 2014-10-04 11:12:53 -0700 | [diff] [blame] | 943 | tsd = tsd_fetch(); |
Jason Evans | 5460aa6 | 2014-09-22 21:09:23 -0700 | [diff] [blame] | 944 | tdata = prof_tdata_get(tsd, false); |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 945 | if (tdata == NULL) { |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 946 | return 0; |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 947 | } |
Jason Evans | 772163b | 2014-01-17 15:40:52 -0800 | [diff] [blame] | 948 | |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 949 | malloc_mutex_lock(tsd_tsdn(tsd), &bt2gctx_mtx); |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 950 | bt_count = ckh_count(&bt2gctx); |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 951 | malloc_mutex_unlock(tsd_tsdn(tsd), &bt2gctx_mtx); |
Jason Evans | 772163b | 2014-01-17 15:40:52 -0800 | [diff] [blame] | 952 | |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 953 | return bt_count; |
Jason Evans | 772163b | 2014-01-17 15:40:52 -0800 | [diff] [blame] | 954 | } |
| 955 | #endif |
| 956 | |
| 957 | #ifdef JEMALLOC_JET |
| 958 | #undef prof_dump_open |
Jason Evans | c0cc5db | 2017-01-19 21:41:41 -0800 | [diff] [blame] | 959 | #define prof_dump_open JEMALLOC_N(prof_dump_open_impl) |
Jason Evans | 772163b | 2014-01-17 15:40:52 -0800 | [diff] [blame] | 960 | #endif |
| 961 | static int |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 962 | prof_dump_open(bool propagate_err, const char *filename) { |
Jason Evans | 772163b | 2014-01-17 15:40:52 -0800 | [diff] [blame] | 963 | int fd; |
Jason Evans | 4f37ef6 | 2014-01-16 13:23:56 -0800 | [diff] [blame] | 964 | |
Jason Evans | 772163b | 2014-01-17 15:40:52 -0800 | [diff] [blame] | 965 | fd = creat(filename, 0644); |
Jason Evans | 551ebc4 | 2014-10-03 10:16:09 -0700 | [diff] [blame] | 966 | if (fd == -1 && !propagate_err) { |
Jason Evans | 772163b | 2014-01-17 15:40:52 -0800 | [diff] [blame] | 967 | malloc_printf("<jemalloc>: creat(\"%s\"), 0644) failed\n", |
| 968 | filename); |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 969 | if (opt_abort) { |
Jason Evans | 772163b | 2014-01-17 15:40:52 -0800 | [diff] [blame] | 970 | abort(); |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 971 | } |
Jason Evans | 4f37ef6 | 2014-01-16 13:23:56 -0800 | [diff] [blame] | 972 | } |
| 973 | |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 974 | return fd; |
Jason Evans | 4f37ef6 | 2014-01-16 13:23:56 -0800 | [diff] [blame] | 975 | } |
Jason Evans | 772163b | 2014-01-17 15:40:52 -0800 | [diff] [blame] | 976 | #ifdef JEMALLOC_JET |
| 977 | #undef prof_dump_open |
Jason Evans | c0cc5db | 2017-01-19 21:41:41 -0800 | [diff] [blame] | 978 | #define prof_dump_open JEMALLOC_N(prof_dump_open) |
Jason Evans | 772163b | 2014-01-17 15:40:52 -0800 | [diff] [blame] | 979 | prof_dump_open_t *prof_dump_open = JEMALLOC_N(prof_dump_open_impl); |
| 980 | #endif |
Jason Evans | 4f37ef6 | 2014-01-16 13:23:56 -0800 | [diff] [blame] | 981 | |
| 982 | static bool |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 983 | prof_dump_flush(bool propagate_err) { |
Jason Evans | 22ca855 | 2010-03-02 11:57:30 -0800 | [diff] [blame] | 984 | bool ret = false; |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 985 | ssize_t err; |
| 986 | |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 987 | cassert(config_prof); |
| 988 | |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 989 | err = write(prof_dump_fd, prof_dump_buf, prof_dump_buf_end); |
| 990 | if (err == -1) { |
Jason Evans | 551ebc4 | 2014-10-03 10:16:09 -0700 | [diff] [blame] | 991 | if (!propagate_err) { |
Jason Evans | 698805c | 2010-03-03 17:45:38 -0800 | [diff] [blame] | 992 | malloc_write("<jemalloc>: write() failed during heap " |
| 993 | "profile flush\n"); |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 994 | if (opt_abort) { |
Jason Evans | 22ca855 | 2010-03-02 11:57:30 -0800 | [diff] [blame] | 995 | abort(); |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 996 | } |
Jason Evans | 22ca855 | 2010-03-02 11:57:30 -0800 | [diff] [blame] | 997 | } |
| 998 | ret = true; |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 999 | } |
| 1000 | prof_dump_buf_end = 0; |
Jason Evans | 22ca855 | 2010-03-02 11:57:30 -0800 | [diff] [blame] | 1001 | |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 1002 | return ret; |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 1003 | } |
| 1004 | |
Jason Evans | 22ca855 | 2010-03-02 11:57:30 -0800 | [diff] [blame] | 1005 | static bool |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 1006 | prof_dump_close(bool propagate_err) { |
Jason Evans | 4f37ef6 | 2014-01-16 13:23:56 -0800 | [diff] [blame] | 1007 | bool ret; |
| 1008 | |
| 1009 | assert(prof_dump_fd != -1); |
| 1010 | ret = prof_dump_flush(propagate_err); |
| 1011 | close(prof_dump_fd); |
| 1012 | prof_dump_fd = -1; |
| 1013 | |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 1014 | return ret; |
Jason Evans | 4f37ef6 | 2014-01-16 13:23:56 -0800 | [diff] [blame] | 1015 | } |
| 1016 | |
| 1017 | static bool |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 1018 | prof_dump_write(bool propagate_err, const char *s) { |
Jason Evans | ca8fffb | 2016-02-24 13:16:51 -0800 | [diff] [blame] | 1019 | size_t i, slen, n; |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 1020 | |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1021 | cassert(config_prof); |
| 1022 | |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 1023 | i = 0; |
| 1024 | slen = strlen(s); |
| 1025 | while (i < slen) { |
| 1026 | /* Flush the buffer if it is full. */ |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 1027 | if (prof_dump_buf_end == PROF_DUMP_BUFSIZE) { |
| 1028 | if (prof_dump_flush(propagate_err) && propagate_err) { |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 1029 | return true; |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 1030 | } |
| 1031 | } |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 1032 | |
Jason Evans | cd9a134 | 2012-03-21 18:33:03 -0700 | [diff] [blame] | 1033 | if (prof_dump_buf_end + slen <= PROF_DUMP_BUFSIZE) { |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 1034 | /* Finish writing. */ |
| 1035 | n = slen - i; |
| 1036 | } else { |
| 1037 | /* Write as much of s as will fit. */ |
Jason Evans | cd9a134 | 2012-03-21 18:33:03 -0700 | [diff] [blame] | 1038 | n = PROF_DUMP_BUFSIZE - prof_dump_buf_end; |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 1039 | } |
| 1040 | memcpy(&prof_dump_buf[prof_dump_buf_end], &s[i], n); |
| 1041 | prof_dump_buf_end += n; |
| 1042 | i += n; |
| 1043 | } |
Jason Evans | 22ca855 | 2010-03-02 11:57:30 -0800 | [diff] [blame] | 1044 | |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 1045 | return false; |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 1046 | } |
| 1047 | |
Jason Evans | e42c309 | 2015-07-22 15:44:47 -0700 | [diff] [blame] | 1048 | JEMALLOC_FORMAT_PRINTF(2, 3) |
Jason Evans | d81e4bd | 2012-03-06 14:57:45 -0800 | [diff] [blame] | 1049 | static bool |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 1050 | prof_dump_printf(bool propagate_err, const char *format, ...) { |
Jason Evans | d81e4bd | 2012-03-06 14:57:45 -0800 | [diff] [blame] | 1051 | bool ret; |
| 1052 | va_list ap; |
Jason Evans | cd9a134 | 2012-03-21 18:33:03 -0700 | [diff] [blame] | 1053 | char buf[PROF_PRINTF_BUFSIZE]; |
Jason Evans | d81e4bd | 2012-03-06 14:57:45 -0800 | [diff] [blame] | 1054 | |
| 1055 | va_start(ap, format); |
Jason Evans | 6da5418 | 2012-03-23 18:05:51 -0700 | [diff] [blame] | 1056 | malloc_vsnprintf(buf, sizeof(buf), format, ap); |
Jason Evans | d81e4bd | 2012-03-06 14:57:45 -0800 | [diff] [blame] | 1057 | va_end(ap); |
Jason Evans | 4f37ef6 | 2014-01-16 13:23:56 -0800 | [diff] [blame] | 1058 | ret = prof_dump_write(propagate_err, buf); |
Jason Evans | d81e4bd | 2012-03-06 14:57:45 -0800 | [diff] [blame] | 1059 | |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 1060 | return ret; |
Jason Evans | d81e4bd | 2012-03-06 14:57:45 -0800 | [diff] [blame] | 1061 | } |
| 1062 | |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 1063 | static void |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 1064 | prof_tctx_merge_tdata(tsdn_t *tsdn, prof_tctx_t *tctx, prof_tdata_t *tdata) { |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 1065 | malloc_mutex_assert_owner(tsdn, tctx->tdata->lock); |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1066 | |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 1067 | malloc_mutex_lock(tsdn, tctx->gctx->lock); |
Jason Evans | 764b000 | 2015-03-14 14:01:35 -0700 | [diff] [blame] | 1068 | |
| 1069 | switch (tctx->state) { |
| 1070 | case prof_tctx_state_initializing: |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 1071 | malloc_mutex_unlock(tsdn, tctx->gctx->lock); |
Jason Evans | 6ef80d6 | 2014-09-24 22:14:21 -0700 | [diff] [blame] | 1072 | return; |
Jason Evans | 764b000 | 2015-03-14 14:01:35 -0700 | [diff] [blame] | 1073 | case prof_tctx_state_nominal: |
| 1074 | tctx->state = prof_tctx_state_dumping; |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 1075 | malloc_mutex_unlock(tsdn, tctx->gctx->lock); |
Jason Evans | 6ef80d6 | 2014-09-24 22:14:21 -0700 | [diff] [blame] | 1076 | |
Jason Evans | 764b000 | 2015-03-14 14:01:35 -0700 | [diff] [blame] | 1077 | memcpy(&tctx->dump_cnts, &tctx->cnts, sizeof(prof_cnt_t)); |
Jason Evans | 3a81cbd | 2014-08-16 12:58:55 -0700 | [diff] [blame] | 1078 | |
Jason Evans | 764b000 | 2015-03-14 14:01:35 -0700 | [diff] [blame] | 1079 | tdata->cnt_summed.curobjs += tctx->dump_cnts.curobjs; |
| 1080 | tdata->cnt_summed.curbytes += tctx->dump_cnts.curbytes; |
| 1081 | if (opt_prof_accum) { |
| 1082 | tdata->cnt_summed.accumobjs += |
| 1083 | tctx->dump_cnts.accumobjs; |
| 1084 | tdata->cnt_summed.accumbytes += |
| 1085 | tctx->dump_cnts.accumbytes; |
| 1086 | } |
| 1087 | break; |
| 1088 | case prof_tctx_state_dumping: |
| 1089 | case prof_tctx_state_purgatory: |
| 1090 | not_reached(); |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 1091 | } |
| 1092 | } |
| 1093 | |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 1094 | static void |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 1095 | prof_tctx_merge_gctx(tsdn_t *tsdn, prof_tctx_t *tctx, prof_gctx_t *gctx) { |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 1096 | malloc_mutex_assert_owner(tsdn, gctx->lock); |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1097 | |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 1098 | gctx->cnt_summed.curobjs += tctx->dump_cnts.curobjs; |
| 1099 | gctx->cnt_summed.curbytes += tctx->dump_cnts.curbytes; |
| 1100 | if (opt_prof_accum) { |
| 1101 | gctx->cnt_summed.accumobjs += tctx->dump_cnts.accumobjs; |
| 1102 | gctx->cnt_summed.accumbytes += tctx->dump_cnts.accumbytes; |
| 1103 | } |
| 1104 | } |
| 1105 | |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 1106 | static prof_tctx_t * |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 1107 | prof_tctx_merge_iter(prof_tctx_tree_t *tctxs, prof_tctx_t *tctx, void *arg) { |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 1108 | tsdn_t *tsdn = (tsdn_t *)arg; |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1109 | |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 1110 | malloc_mutex_assert_owner(tsdn, tctx->gctx->lock); |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 1111 | |
| 1112 | switch (tctx->state) { |
| 1113 | case prof_tctx_state_nominal: |
| 1114 | /* New since dumping started; ignore. */ |
| 1115 | break; |
| 1116 | case prof_tctx_state_dumping: |
| 1117 | case prof_tctx_state_purgatory: |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 1118 | prof_tctx_merge_gctx(tsdn, tctx, tctx->gctx); |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 1119 | break; |
| 1120 | default: |
| 1121 | not_reached(); |
Jason Evans | 3a81cbd | 2014-08-16 12:58:55 -0700 | [diff] [blame] | 1122 | } |
| 1123 | |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 1124 | return NULL; |
Jason Evans | 3a81cbd | 2014-08-16 12:58:55 -0700 | [diff] [blame] | 1125 | } |
| 1126 | |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1127 | struct prof_tctx_dump_iter_arg_s { |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 1128 | tsdn_t *tsdn; |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1129 | bool propagate_err; |
| 1130 | }; |
| 1131 | |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 1132 | static prof_tctx_t * |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 1133 | prof_tctx_dump_iter(prof_tctx_tree_t *tctxs, prof_tctx_t *tctx, void *opaque) { |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1134 | struct prof_tctx_dump_iter_arg_s *arg = |
| 1135 | (struct prof_tctx_dump_iter_arg_s *)opaque; |
| 1136 | |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 1137 | malloc_mutex_assert_owner(arg->tsdn, tctx->gctx->lock); |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 1138 | |
Jason Evans | fb64ec2 | 2015-09-21 18:37:18 -0700 | [diff] [blame] | 1139 | switch (tctx->state) { |
| 1140 | case prof_tctx_state_initializing: |
| 1141 | case prof_tctx_state_nominal: |
| 1142 | /* Not captured by this dump. */ |
| 1143 | break; |
| 1144 | case prof_tctx_state_dumping: |
| 1145 | case prof_tctx_state_purgatory: |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1146 | if (prof_dump_printf(arg->propagate_err, |
Jason Evans | fb64ec2 | 2015-09-21 18:37:18 -0700 | [diff] [blame] | 1147 | " t%"FMTu64": %"FMTu64": %"FMTu64" [%"FMTu64": " |
| 1148 | "%"FMTu64"]\n", tctx->thr_uid, tctx->dump_cnts.curobjs, |
| 1149 | tctx->dump_cnts.curbytes, tctx->dump_cnts.accumobjs, |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 1150 | tctx->dump_cnts.accumbytes)) { |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 1151 | return tctx; |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 1152 | } |
Jason Evans | fb64ec2 | 2015-09-21 18:37:18 -0700 | [diff] [blame] | 1153 | break; |
| 1154 | default: |
| 1155 | not_reached(); |
| 1156 | } |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 1157 | return NULL; |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 1158 | } |
| 1159 | |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 1160 | static prof_tctx_t * |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 1161 | prof_tctx_finish_iter(prof_tctx_tree_t *tctxs, prof_tctx_t *tctx, void *arg) { |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 1162 | tsdn_t *tsdn = (tsdn_t *)arg; |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 1163 | prof_tctx_t *ret; |
| 1164 | |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 1165 | malloc_mutex_assert_owner(tsdn, tctx->gctx->lock); |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1166 | |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 1167 | switch (tctx->state) { |
| 1168 | case prof_tctx_state_nominal: |
| 1169 | /* New since dumping started; ignore. */ |
| 1170 | break; |
| 1171 | case prof_tctx_state_dumping: |
| 1172 | tctx->state = prof_tctx_state_nominal; |
| 1173 | break; |
| 1174 | case prof_tctx_state_purgatory: |
Jason Evans | 20c31de | 2014-10-02 23:01:10 -0700 | [diff] [blame] | 1175 | ret = tctx; |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 1176 | goto label_return; |
| 1177 | default: |
| 1178 | not_reached(); |
| 1179 | } |
| 1180 | |
| 1181 | ret = NULL; |
| 1182 | label_return: |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 1183 | return ret; |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 1184 | } |
| 1185 | |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 1186 | static void |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 1187 | prof_dump_gctx_prep(tsdn_t *tsdn, prof_gctx_t *gctx, prof_gctx_tree_t *gctxs) { |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1188 | cassert(config_prof); |
| 1189 | |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 1190 | malloc_mutex_lock(tsdn, gctx->lock); |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 1191 | |
Jason Evans | 4f37ef6 | 2014-01-16 13:23:56 -0800 | [diff] [blame] | 1192 | /* |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 1193 | * Increment nlimbo so that gctx won't go away before dump. |
| 1194 | * Additionally, link gctx into the dump list so that it is included in |
Jason Evans | 4f37ef6 | 2014-01-16 13:23:56 -0800 | [diff] [blame] | 1195 | * prof_dump()'s second pass. |
| 1196 | */ |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 1197 | gctx->nlimbo++; |
| 1198 | gctx_tree_insert(gctxs, gctx); |
Jason Evans | 4f37ef6 | 2014-01-16 13:23:56 -0800 | [diff] [blame] | 1199 | |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 1200 | memset(&gctx->cnt_summed, 0, sizeof(prof_cnt_t)); |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 1201 | |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 1202 | malloc_mutex_unlock(tsdn, gctx->lock); |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 1203 | } |
Jason Evans | 9ce3bfd | 2010-10-02 22:39:59 -0700 | [diff] [blame] | 1204 | |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1205 | struct prof_gctx_merge_iter_arg_s { |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 1206 | tsdn_t *tsdn; |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1207 | size_t leak_ngctx; |
| 1208 | }; |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 1209 | |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1210 | static prof_gctx_t * |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 1211 | prof_gctx_merge_iter(prof_gctx_tree_t *gctxs, prof_gctx_t *gctx, void *opaque) { |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1212 | struct prof_gctx_merge_iter_arg_s *arg = |
| 1213 | (struct prof_gctx_merge_iter_arg_s *)opaque; |
| 1214 | |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 1215 | malloc_mutex_lock(arg->tsdn, gctx->lock); |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1216 | tctx_tree_iter(&gctx->tctxs, NULL, prof_tctx_merge_iter, |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 1217 | (void *)arg->tsdn); |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 1218 | if (gctx->cnt_summed.curobjs != 0) { |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1219 | arg->leak_ngctx++; |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 1220 | } |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 1221 | malloc_mutex_unlock(arg->tsdn, gctx->lock); |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 1222 | |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 1223 | return NULL; |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 1224 | } |
| 1225 | |
Jason Evans | 20c31de | 2014-10-02 23:01:10 -0700 | [diff] [blame] | 1226 | static void |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 1227 | prof_gctx_finish(tsd_t *tsd, prof_gctx_tree_t *gctxs) { |
Jason Evans | 5460aa6 | 2014-09-22 21:09:23 -0700 | [diff] [blame] | 1228 | prof_tdata_t *tdata = prof_tdata_get(tsd, false); |
Jason Evans | 20c31de | 2014-10-02 23:01:10 -0700 | [diff] [blame] | 1229 | prof_gctx_t *gctx; |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 1230 | |
Jason Evans | 20c31de | 2014-10-02 23:01:10 -0700 | [diff] [blame] | 1231 | /* |
| 1232 | * Standard tree iteration won't work here, because as soon as we |
| 1233 | * decrement gctx->nlimbo and unlock gctx, another thread can |
| 1234 | * concurrently destroy it, which will corrupt the tree. Therefore, |
| 1235 | * tear down the tree one node at a time during iteration. |
| 1236 | */ |
| 1237 | while ((gctx = gctx_tree_first(gctxs)) != NULL) { |
| 1238 | gctx_tree_remove(gctxs, gctx); |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 1239 | malloc_mutex_lock(tsd_tsdn(tsd), gctx->lock); |
Jason Evans | 20c31de | 2014-10-02 23:01:10 -0700 | [diff] [blame] | 1240 | { |
| 1241 | prof_tctx_t *next; |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 1242 | |
Jason Evans | 20c31de | 2014-10-02 23:01:10 -0700 | [diff] [blame] | 1243 | next = NULL; |
| 1244 | do { |
| 1245 | prof_tctx_t *to_destroy = |
| 1246 | tctx_tree_iter(&gctx->tctxs, next, |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 1247 | prof_tctx_finish_iter, |
| 1248 | (void *)tsd_tsdn(tsd)); |
Jason Evans | 20c31de | 2014-10-02 23:01:10 -0700 | [diff] [blame] | 1249 | if (to_destroy != NULL) { |
| 1250 | next = tctx_tree_next(&gctx->tctxs, |
| 1251 | to_destroy); |
| 1252 | tctx_tree_remove(&gctx->tctxs, |
| 1253 | to_destroy); |
Jason Evans | 51a2ec9 | 2017-03-17 02:45:12 -0700 | [diff] [blame] | 1254 | idalloctm(tsd_tsdn(tsd), to_destroy, |
Qi Wang | bfa530b | 2017-04-07 14:12:30 -0700 | [diff] [blame] | 1255 | NULL, NULL, true, true); |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 1256 | } else { |
Jason Evans | 20c31de | 2014-10-02 23:01:10 -0700 | [diff] [blame] | 1257 | next = NULL; |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 1258 | } |
Jason Evans | 20c31de | 2014-10-02 23:01:10 -0700 | [diff] [blame] | 1259 | } while (next != NULL); |
| 1260 | } |
| 1261 | gctx->nlimbo--; |
| 1262 | if (prof_gctx_should_destroy(gctx)) { |
| 1263 | gctx->nlimbo++; |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 1264 | malloc_mutex_unlock(tsd_tsdn(tsd), gctx->lock); |
Jason Evans | c93ed81 | 2014-10-30 16:50:33 -0700 | [diff] [blame] | 1265 | prof_gctx_try_destroy(tsd, tdata, gctx, tdata); |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 1266 | } else { |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 1267 | malloc_mutex_unlock(tsd_tsdn(tsd), gctx->lock); |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 1268 | } |
Jason Evans | 20c31de | 2014-10-02 23:01:10 -0700 | [diff] [blame] | 1269 | } |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 1270 | } |
| 1271 | |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1272 | struct prof_tdata_merge_iter_arg_s { |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 1273 | tsdn_t *tsdn; |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1274 | prof_cnt_t cnt_all; |
| 1275 | }; |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 1276 | |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1277 | static prof_tdata_t * |
| 1278 | prof_tdata_merge_iter(prof_tdata_tree_t *tdatas, prof_tdata_t *tdata, |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 1279 | void *opaque) { |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1280 | struct prof_tdata_merge_iter_arg_s *arg = |
| 1281 | (struct prof_tdata_merge_iter_arg_s *)opaque; |
| 1282 | |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 1283 | malloc_mutex_lock(arg->tsdn, tdata->lock); |
Jason Evans | 20c31de | 2014-10-02 23:01:10 -0700 | [diff] [blame] | 1284 | if (!tdata->expired) { |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 1285 | size_t tabind; |
| 1286 | union { |
| 1287 | prof_tctx_t *p; |
| 1288 | void *v; |
| 1289 | } tctx; |
| 1290 | |
| 1291 | tdata->dumping = true; |
| 1292 | memset(&tdata->cnt_summed, 0, sizeof(prof_cnt_t)); |
Jason Evans | 551ebc4 | 2014-10-03 10:16:09 -0700 | [diff] [blame] | 1293 | for (tabind = 0; !ckh_iter(&tdata->bt2tctx, &tabind, NULL, |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 1294 | &tctx.v);) { |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 1295 | prof_tctx_merge_tdata(arg->tsdn, tctx.p, tdata); |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 1296 | } |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 1297 | |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1298 | arg->cnt_all.curobjs += tdata->cnt_summed.curobjs; |
| 1299 | arg->cnt_all.curbytes += tdata->cnt_summed.curbytes; |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 1300 | if (opt_prof_accum) { |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1301 | arg->cnt_all.accumobjs += tdata->cnt_summed.accumobjs; |
| 1302 | arg->cnt_all.accumbytes += tdata->cnt_summed.accumbytes; |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 1303 | } |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 1304 | } else { |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 1305 | tdata->dumping = false; |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 1306 | } |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 1307 | malloc_mutex_unlock(arg->tsdn, tdata->lock); |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 1308 | |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 1309 | return NULL; |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 1310 | } |
| 1311 | |
| 1312 | static prof_tdata_t * |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 1313 | prof_tdata_dump_iter(prof_tdata_tree_t *tdatas, prof_tdata_t *tdata, |
| 1314 | void *arg) { |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 1315 | bool propagate_err = *(bool *)arg; |
| 1316 | |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 1317 | if (!tdata->dumping) { |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 1318 | return NULL; |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 1319 | } |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 1320 | |
| 1321 | if (prof_dump_printf(propagate_err, |
Jason Evans | 5fae7dc | 2015-07-23 13:56:25 -0700 | [diff] [blame] | 1322 | " t%"FMTu64": %"FMTu64": %"FMTu64" [%"FMTu64": %"FMTu64"]%s%s\n", |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 1323 | tdata->thr_uid, tdata->cnt_summed.curobjs, |
| 1324 | tdata->cnt_summed.curbytes, tdata->cnt_summed.accumobjs, |
| 1325 | tdata->cnt_summed.accumbytes, |
| 1326 | (tdata->thread_name != NULL) ? " " : "", |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 1327 | (tdata->thread_name != NULL) ? tdata->thread_name : "")) { |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 1328 | return tdata; |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 1329 | } |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 1330 | return NULL; |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 1331 | } |
| 1332 | |
Jason Evans | 20c31de | 2014-10-02 23:01:10 -0700 | [diff] [blame] | 1333 | #ifdef JEMALLOC_JET |
| 1334 | #undef prof_dump_header |
Jason Evans | c0cc5db | 2017-01-19 21:41:41 -0800 | [diff] [blame] | 1335 | #define prof_dump_header JEMALLOC_N(prof_dump_header_impl) |
Jason Evans | 20c31de | 2014-10-02 23:01:10 -0700 | [diff] [blame] | 1336 | #endif |
Jason Evans | 4f37ef6 | 2014-01-16 13:23:56 -0800 | [diff] [blame] | 1337 | static bool |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 1338 | prof_dump_header(tsdn_t *tsdn, bool propagate_err, const prof_cnt_t *cnt_all) { |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 1339 | bool ret; |
Jason Evans | a881cd2 | 2010-10-02 15:18:50 -0700 | [diff] [blame] | 1340 | |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 1341 | if (prof_dump_printf(propagate_err, |
Jason Evans | 5fae7dc | 2015-07-23 13:56:25 -0700 | [diff] [blame] | 1342 | "heap_v2/%"FMTu64"\n" |
| 1343 | " t*: %"FMTu64": %"FMTu64" [%"FMTu64": %"FMTu64"]\n", |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 1344 | ((uint64_t)1U << lg_prof_sample), cnt_all->curobjs, |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 1345 | cnt_all->curbytes, cnt_all->accumobjs, cnt_all->accumbytes)) { |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 1346 | return true; |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 1347 | } |
Jason Evans | 4f37ef6 | 2014-01-16 13:23:56 -0800 | [diff] [blame] | 1348 | |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 1349 | malloc_mutex_lock(tsdn, &tdatas_mtx); |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 1350 | ret = (tdata_tree_iter(&tdatas, NULL, prof_tdata_dump_iter, |
| 1351 | (void *)&propagate_err) != NULL); |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 1352 | malloc_mutex_unlock(tsdn, &tdatas_mtx); |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 1353 | return ret; |
Jason Evans | a881cd2 | 2010-10-02 15:18:50 -0700 | [diff] [blame] | 1354 | } |
Jason Evans | 20c31de | 2014-10-02 23:01:10 -0700 | [diff] [blame] | 1355 | #ifdef JEMALLOC_JET |
| 1356 | #undef prof_dump_header |
Jason Evans | c0cc5db | 2017-01-19 21:41:41 -0800 | [diff] [blame] | 1357 | #define prof_dump_header JEMALLOC_N(prof_dump_header) |
Jason Evans | 20c31de | 2014-10-02 23:01:10 -0700 | [diff] [blame] | 1358 | prof_dump_header_t *prof_dump_header = JEMALLOC_N(prof_dump_header_impl); |
| 1359 | #endif |
Jason Evans | a881cd2 | 2010-10-02 15:18:50 -0700 | [diff] [blame] | 1360 | |
Jason Evans | 22ca855 | 2010-03-02 11:57:30 -0800 | [diff] [blame] | 1361 | static bool |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 1362 | prof_dump_gctx(tsdn_t *tsdn, bool propagate_err, prof_gctx_t *gctx, |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 1363 | const prof_bt_t *bt, prof_gctx_tree_t *gctxs) { |
Jason Evans | 4f37ef6 | 2014-01-16 13:23:56 -0800 | [diff] [blame] | 1364 | bool ret; |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 1365 | unsigned i; |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1366 | struct prof_tctx_dump_iter_arg_s prof_tctx_dump_iter_arg; |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 1367 | |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1368 | cassert(config_prof); |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 1369 | malloc_mutex_assert_owner(tsdn, gctx->lock); |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1370 | |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 1371 | /* Avoid dumping such gctx's that have no useful data. */ |
Jason Evans | 551ebc4 | 2014-10-03 10:16:09 -0700 | [diff] [blame] | 1372 | if ((!opt_prof_accum && gctx->cnt_summed.curobjs == 0) || |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 1373 | (opt_prof_accum && gctx->cnt_summed.accumobjs == 0)) { |
| 1374 | assert(gctx->cnt_summed.curobjs == 0); |
| 1375 | assert(gctx->cnt_summed.curbytes == 0); |
| 1376 | assert(gctx->cnt_summed.accumobjs == 0); |
| 1377 | assert(gctx->cnt_summed.accumbytes == 0); |
Jason Evans | 4f37ef6 | 2014-01-16 13:23:56 -0800 | [diff] [blame] | 1378 | ret = false; |
| 1379 | goto label_return; |
Jason Evans | a881cd2 | 2010-10-02 15:18:50 -0700 | [diff] [blame] | 1380 | } |
| 1381 | |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 1382 | if (prof_dump_printf(propagate_err, "@")) { |
Jason Evans | 4f37ef6 | 2014-01-16 13:23:56 -0800 | [diff] [blame] | 1383 | ret = true; |
| 1384 | goto label_return; |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 1385 | } |
Jason Evans | 4f37ef6 | 2014-01-16 13:23:56 -0800 | [diff] [blame] | 1386 | for (i = 0; i < bt->len; i++) { |
Jason Evans | 5fae7dc | 2015-07-23 13:56:25 -0700 | [diff] [blame] | 1387 | if (prof_dump_printf(propagate_err, " %#"FMTxPTR, |
Jason Evans | 4f37ef6 | 2014-01-16 13:23:56 -0800 | [diff] [blame] | 1388 | (uintptr_t)bt->vec[i])) { |
| 1389 | ret = true; |
| 1390 | goto label_return; |
| 1391 | } |
| 1392 | } |
Jason Evans | 22ca855 | 2010-03-02 11:57:30 -0800 | [diff] [blame] | 1393 | |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 1394 | if (prof_dump_printf(propagate_err, |
| 1395 | "\n" |
Jason Evans | 5fae7dc | 2015-07-23 13:56:25 -0700 | [diff] [blame] | 1396 | " t*: %"FMTu64": %"FMTu64" [%"FMTu64": %"FMTu64"]\n", |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 1397 | gctx->cnt_summed.curobjs, gctx->cnt_summed.curbytes, |
| 1398 | gctx->cnt_summed.accumobjs, gctx->cnt_summed.accumbytes)) { |
| 1399 | ret = true; |
| 1400 | goto label_return; |
| 1401 | } |
| 1402 | |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 1403 | prof_tctx_dump_iter_arg.tsdn = tsdn; |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1404 | prof_tctx_dump_iter_arg.propagate_err = propagate_err; |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 1405 | if (tctx_tree_iter(&gctx->tctxs, NULL, prof_tctx_dump_iter, |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1406 | (void *)&prof_tctx_dump_iter_arg) != NULL) { |
Jason Evans | 4f37ef6 | 2014-01-16 13:23:56 -0800 | [diff] [blame] | 1407 | ret = true; |
| 1408 | goto label_return; |
| 1409 | } |
| 1410 | |
Jason Evans | 772163b | 2014-01-17 15:40:52 -0800 | [diff] [blame] | 1411 | ret = false; |
Jason Evans | 4f37ef6 | 2014-01-16 13:23:56 -0800 | [diff] [blame] | 1412 | label_return: |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 1413 | return ret; |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 1414 | } |
| 1415 | |
Jason Evans | 788d29d | 2016-02-20 23:46:14 -0800 | [diff] [blame] | 1416 | #ifndef _WIN32 |
Jason Evans | e42c309 | 2015-07-22 15:44:47 -0700 | [diff] [blame] | 1417 | JEMALLOC_FORMAT_PRINTF(1, 2) |
Jason Evans | 8e33c21 | 2015-05-01 09:03:20 -0700 | [diff] [blame] | 1418 | static int |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 1419 | prof_open_maps(const char *format, ...) { |
Jason Evans | 8e33c21 | 2015-05-01 09:03:20 -0700 | [diff] [blame] | 1420 | int mfd; |
| 1421 | va_list ap; |
| 1422 | char filename[PATH_MAX + 1]; |
| 1423 | |
| 1424 | va_start(ap, format); |
| 1425 | malloc_vsnprintf(filename, sizeof(filename), format, ap); |
| 1426 | va_end(ap); |
| 1427 | mfd = open(filename, O_RDONLY); |
| 1428 | |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 1429 | return mfd; |
Jason Evans | 8e33c21 | 2015-05-01 09:03:20 -0700 | [diff] [blame] | 1430 | } |
Jason Evans | 788d29d | 2016-02-20 23:46:14 -0800 | [diff] [blame] | 1431 | #endif |
| 1432 | |
| 1433 | static int |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 1434 | prof_getpid(void) { |
Jason Evans | 788d29d | 2016-02-20 23:46:14 -0800 | [diff] [blame] | 1435 | #ifdef _WIN32 |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 1436 | return GetCurrentProcessId(); |
Jason Evans | 788d29d | 2016-02-20 23:46:14 -0800 | [diff] [blame] | 1437 | #else |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 1438 | return getpid(); |
Jason Evans | 788d29d | 2016-02-20 23:46:14 -0800 | [diff] [blame] | 1439 | #endif |
| 1440 | } |
Jason Evans | 8e33c21 | 2015-05-01 09:03:20 -0700 | [diff] [blame] | 1441 | |
Jason Evans | 22ca855 | 2010-03-02 11:57:30 -0800 | [diff] [blame] | 1442 | static bool |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 1443 | prof_dump_maps(bool propagate_err) { |
Jason Evans | 93f39f8 | 2013-10-21 15:07:40 -0700 | [diff] [blame] | 1444 | bool ret; |
Jason Evans | c717718 | 2010-02-11 09:25:56 -0800 | [diff] [blame] | 1445 | int mfd; |
Jason Evans | c717718 | 2010-02-11 09:25:56 -0800 | [diff] [blame] | 1446 | |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1447 | cassert(config_prof); |
Harald Weppner | c2da259 | 2014-03-18 00:00:14 -0700 | [diff] [blame] | 1448 | #ifdef __FreeBSD__ |
Jason Evans | 8e33c21 | 2015-05-01 09:03:20 -0700 | [diff] [blame] | 1449 | mfd = prof_open_maps("/proc/curproc/map"); |
rustyx | 7f28398 | 2016-01-30 14:51:16 +0100 | [diff] [blame] | 1450 | #elif defined(_WIN32) |
| 1451 | mfd = -1; // Not implemented |
Harald Weppner | c2da259 | 2014-03-18 00:00:14 -0700 | [diff] [blame] | 1452 | #else |
Jason Evans | 8e33c21 | 2015-05-01 09:03:20 -0700 | [diff] [blame] | 1453 | { |
Jason Evans | 788d29d | 2016-02-20 23:46:14 -0800 | [diff] [blame] | 1454 | int pid = prof_getpid(); |
Jason Evans | 8e33c21 | 2015-05-01 09:03:20 -0700 | [diff] [blame] | 1455 | |
| 1456 | mfd = prof_open_maps("/proc/%d/task/%d/maps", pid, pid); |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 1457 | if (mfd == -1) { |
Jason Evans | 8e33c21 | 2015-05-01 09:03:20 -0700 | [diff] [blame] | 1458 | mfd = prof_open_maps("/proc/%d/maps", pid); |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 1459 | } |
Jason Evans | 8e33c21 | 2015-05-01 09:03:20 -0700 | [diff] [blame] | 1460 | } |
Harald Weppner | c2da259 | 2014-03-18 00:00:14 -0700 | [diff] [blame] | 1461 | #endif |
Jason Evans | c717718 | 2010-02-11 09:25:56 -0800 | [diff] [blame] | 1462 | if (mfd != -1) { |
| 1463 | ssize_t nread; |
| 1464 | |
Jason Evans | 4f37ef6 | 2014-01-16 13:23:56 -0800 | [diff] [blame] | 1465 | if (prof_dump_write(propagate_err, "\nMAPPED_LIBRARIES:\n") && |
Jason Evans | 93f39f8 | 2013-10-21 15:07:40 -0700 | [diff] [blame] | 1466 | propagate_err) { |
| 1467 | ret = true; |
| 1468 | goto label_return; |
| 1469 | } |
Jason Evans | c717718 | 2010-02-11 09:25:56 -0800 | [diff] [blame] | 1470 | nread = 0; |
| 1471 | do { |
| 1472 | prof_dump_buf_end += nread; |
Jason Evans | cd9a134 | 2012-03-21 18:33:03 -0700 | [diff] [blame] | 1473 | if (prof_dump_buf_end == PROF_DUMP_BUFSIZE) { |
Jason Evans | c717718 | 2010-02-11 09:25:56 -0800 | [diff] [blame] | 1474 | /* Make space in prof_dump_buf before read(). */ |
Jason Evans | 4f37ef6 | 2014-01-16 13:23:56 -0800 | [diff] [blame] | 1475 | if (prof_dump_flush(propagate_err) && |
Jason Evans | 93f39f8 | 2013-10-21 15:07:40 -0700 | [diff] [blame] | 1476 | propagate_err) { |
| 1477 | ret = true; |
| 1478 | goto label_return; |
| 1479 | } |
Jason Evans | c717718 | 2010-02-11 09:25:56 -0800 | [diff] [blame] | 1480 | } |
| 1481 | nread = read(mfd, &prof_dump_buf[prof_dump_buf_end], |
Jason Evans | cd9a134 | 2012-03-21 18:33:03 -0700 | [diff] [blame] | 1482 | PROF_DUMP_BUFSIZE - prof_dump_buf_end); |
Jason Evans | c717718 | 2010-02-11 09:25:56 -0800 | [diff] [blame] | 1483 | } while (nread > 0); |
Jason Evans | 93f39f8 | 2013-10-21 15:07:40 -0700 | [diff] [blame] | 1484 | } else { |
| 1485 | ret = true; |
| 1486 | goto label_return; |
| 1487 | } |
Jason Evans | 22ca855 | 2010-03-02 11:57:30 -0800 | [diff] [blame] | 1488 | |
Jason Evans | 93f39f8 | 2013-10-21 15:07:40 -0700 | [diff] [blame] | 1489 | ret = false; |
| 1490 | label_return: |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 1491 | if (mfd != -1) { |
Jason Evans | 93f39f8 | 2013-10-21 15:07:40 -0700 | [diff] [blame] | 1492 | close(mfd); |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 1493 | } |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 1494 | return ret; |
Jason Evans | c717718 | 2010-02-11 09:25:56 -0800 | [diff] [blame] | 1495 | } |
| 1496 | |
Jason Evans | dc391ad | 2016-05-04 12:14:36 -0700 | [diff] [blame] | 1497 | /* |
| 1498 | * See prof_sample_threshold_update() comment for why the body of this function |
| 1499 | * is conditionally compiled. |
| 1500 | */ |
Jason Evans | 4f37ef6 | 2014-01-16 13:23:56 -0800 | [diff] [blame] | 1501 | static void |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 1502 | prof_leakcheck(const prof_cnt_t *cnt_all, size_t leak_ngctx, |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 1503 | const char *filename) { |
Jason Evans | dc391ad | 2016-05-04 12:14:36 -0700 | [diff] [blame] | 1504 | #ifdef JEMALLOC_PROF |
| 1505 | /* |
| 1506 | * Scaling is equivalent AdjustSamples() in jeprof, but the result may |
| 1507 | * differ slightly from what jeprof reports, because here we scale the |
| 1508 | * summary values, whereas jeprof scales each context individually and |
| 1509 | * reports the sums of the scaled values. |
| 1510 | */ |
Jason Evans | 4f37ef6 | 2014-01-16 13:23:56 -0800 | [diff] [blame] | 1511 | if (cnt_all->curbytes != 0) { |
Jason Evans | dc391ad | 2016-05-04 12:14:36 -0700 | [diff] [blame] | 1512 | double sample_period = (double)((uint64_t)1 << lg_prof_sample); |
| 1513 | double ratio = (((double)cnt_all->curbytes) / |
| 1514 | (double)cnt_all->curobjs) / sample_period; |
| 1515 | double scale_factor = 1.0 / (1.0 - exp(-ratio)); |
| 1516 | uint64_t curbytes = (uint64_t)round(((double)cnt_all->curbytes) |
| 1517 | * scale_factor); |
| 1518 | uint64_t curobjs = (uint64_t)round(((double)cnt_all->curobjs) * |
| 1519 | scale_factor); |
| 1520 | |
| 1521 | malloc_printf("<jemalloc>: Leak approximation summary: ~%"FMTu64 |
| 1522 | " byte%s, ~%"FMTu64" object%s, >= %zu context%s\n", |
| 1523 | curbytes, (curbytes != 1) ? "s" : "", curobjs, (curobjs != |
| 1524 | 1) ? "s" : "", leak_ngctx, (leak_ngctx != 1) ? "s" : ""); |
Jason Evans | 4f37ef6 | 2014-01-16 13:23:56 -0800 | [diff] [blame] | 1525 | malloc_printf( |
Jason Evans | 7041720 | 2015-05-01 12:31:12 -0700 | [diff] [blame] | 1526 | "<jemalloc>: Run jeprof on \"%s\" for leak detail\n", |
Jason Evans | 4f37ef6 | 2014-01-16 13:23:56 -0800 | [diff] [blame] | 1527 | filename); |
| 1528 | } |
Jason Evans | dc391ad | 2016-05-04 12:14:36 -0700 | [diff] [blame] | 1529 | #endif |
Jason Evans | 4f37ef6 | 2014-01-16 13:23:56 -0800 | [diff] [blame] | 1530 | } |
| 1531 | |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1532 | struct prof_gctx_dump_iter_arg_s { |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 1533 | tsdn_t *tsdn; |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1534 | bool propagate_err; |
| 1535 | }; |
| 1536 | |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 1537 | static prof_gctx_t * |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 1538 | prof_gctx_dump_iter(prof_gctx_tree_t *gctxs, prof_gctx_t *gctx, void *opaque) { |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 1539 | prof_gctx_t *ret; |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1540 | struct prof_gctx_dump_iter_arg_s *arg = |
| 1541 | (struct prof_gctx_dump_iter_arg_s *)opaque; |
Jason Evans | 3a81cbd | 2014-08-16 12:58:55 -0700 | [diff] [blame] | 1542 | |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 1543 | malloc_mutex_lock(arg->tsdn, gctx->lock); |
Jason Evans | 3a81cbd | 2014-08-16 12:58:55 -0700 | [diff] [blame] | 1544 | |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 1545 | if (prof_dump_gctx(arg->tsdn, arg->propagate_err, gctx, &gctx->bt, |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1546 | gctxs)) { |
Jason Evans | 20c31de | 2014-10-02 23:01:10 -0700 | [diff] [blame] | 1547 | ret = gctx; |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 1548 | goto label_return; |
| 1549 | } |
Jason Evans | 3a81cbd | 2014-08-16 12:58:55 -0700 | [diff] [blame] | 1550 | |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 1551 | ret = NULL; |
| 1552 | label_return: |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 1553 | malloc_mutex_unlock(arg->tsdn, gctx->lock); |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 1554 | return ret; |
Jason Evans | 3a81cbd | 2014-08-16 12:58:55 -0700 | [diff] [blame] | 1555 | } |
| 1556 | |
Jason Evans | 1ff0953 | 2017-01-16 11:09:24 -0800 | [diff] [blame] | 1557 | static void |
| 1558 | prof_dump_prep(tsd_t *tsd, prof_tdata_t *tdata, |
| 1559 | struct prof_tdata_merge_iter_arg_s *prof_tdata_merge_iter_arg, |
| 1560 | struct prof_gctx_merge_iter_arg_s *prof_gctx_merge_iter_arg, |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 1561 | prof_gctx_tree_t *gctxs) { |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 1562 | size_t tabind; |
Jason Evans | 075e77c | 2010-09-20 19:53:25 -0700 | [diff] [blame] | 1563 | union { |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 1564 | prof_gctx_t *p; |
Jason Evans | 075e77c | 2010-09-20 19:53:25 -0700 | [diff] [blame] | 1565 | void *v; |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 1566 | } gctx; |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 1567 | |
Jason Evans | c93ed81 | 2014-10-30 16:50:33 -0700 | [diff] [blame] | 1568 | prof_enter(tsd, tdata); |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 1569 | |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 1570 | /* |
| 1571 | * Put gctx's in limbo and clear their counters in preparation for |
| 1572 | * summing. |
| 1573 | */ |
Jason Evans | 1ff0953 | 2017-01-16 11:09:24 -0800 | [diff] [blame] | 1574 | gctx_tree_new(gctxs); |
| 1575 | for (tabind = 0; !ckh_iter(&bt2gctx, &tabind, NULL, &gctx.v);) { |
| 1576 | prof_dump_gctx_prep(tsd_tsdn(tsd), gctx.p, gctxs); |
| 1577 | } |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 1578 | |
| 1579 | /* |
| 1580 | * Iterate over tdatas, and for the non-expired ones snapshot their tctx |
| 1581 | * stats and merge them into the associated gctx's. |
| 1582 | */ |
Jason Evans | 1ff0953 | 2017-01-16 11:09:24 -0800 | [diff] [blame] | 1583 | prof_tdata_merge_iter_arg->tsdn = tsd_tsdn(tsd); |
| 1584 | memset(&prof_tdata_merge_iter_arg->cnt_all, 0, sizeof(prof_cnt_t)); |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 1585 | malloc_mutex_lock(tsd_tsdn(tsd), &tdatas_mtx); |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1586 | tdata_tree_iter(&tdatas, NULL, prof_tdata_merge_iter, |
Jason Evans | 1ff0953 | 2017-01-16 11:09:24 -0800 | [diff] [blame] | 1587 | (void *)prof_tdata_merge_iter_arg); |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 1588 | malloc_mutex_unlock(tsd_tsdn(tsd), &tdatas_mtx); |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 1589 | |
| 1590 | /* Merge tctx stats into gctx's. */ |
Jason Evans | 1ff0953 | 2017-01-16 11:09:24 -0800 | [diff] [blame] | 1591 | prof_gctx_merge_iter_arg->tsdn = tsd_tsdn(tsd); |
| 1592 | prof_gctx_merge_iter_arg->leak_ngctx = 0; |
| 1593 | gctx_tree_iter(gctxs, NULL, prof_gctx_merge_iter, |
| 1594 | (void *)prof_gctx_merge_iter_arg); |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 1595 | |
Jason Evans | c93ed81 | 2014-10-30 16:50:33 -0700 | [diff] [blame] | 1596 | prof_leave(tsd, tdata); |
Jason Evans | 1ff0953 | 2017-01-16 11:09:24 -0800 | [diff] [blame] | 1597 | } |
Jason Evans | 4f37ef6 | 2014-01-16 13:23:56 -0800 | [diff] [blame] | 1598 | |
Jason Evans | 1ff0953 | 2017-01-16 11:09:24 -0800 | [diff] [blame] | 1599 | static bool |
| 1600 | prof_dump_file(tsd_t *tsd, bool propagate_err, const char *filename, |
| 1601 | bool leakcheck, prof_tdata_t *tdata, |
| 1602 | struct prof_tdata_merge_iter_arg_s *prof_tdata_merge_iter_arg, |
| 1603 | struct prof_gctx_merge_iter_arg_s *prof_gctx_merge_iter_arg, |
| 1604 | struct prof_gctx_dump_iter_arg_s *prof_gctx_dump_iter_arg, |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 1605 | prof_gctx_tree_t *gctxs) { |
Jason Evans | 4f37ef6 | 2014-01-16 13:23:56 -0800 | [diff] [blame] | 1606 | /* Create dump file. */ |
Jason Evans | 1ff0953 | 2017-01-16 11:09:24 -0800 | [diff] [blame] | 1607 | if ((prof_dump_fd = prof_dump_open(propagate_err, filename)) == -1) { |
| 1608 | return true; |
| 1609 | } |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 1610 | |
| 1611 | /* Dump profile header. */ |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 1612 | if (prof_dump_header(tsd_tsdn(tsd), propagate_err, |
Jason Evans | 1ff0953 | 2017-01-16 11:09:24 -0800 | [diff] [blame] | 1613 | &prof_tdata_merge_iter_arg->cnt_all)) { |
Jason Evans | 4f37ef6 | 2014-01-16 13:23:56 -0800 | [diff] [blame] | 1614 | goto label_write_error; |
Jason Evans | 1ff0953 | 2017-01-16 11:09:24 -0800 | [diff] [blame] | 1615 | } |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 1616 | |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 1617 | /* Dump per gctx profile stats. */ |
Jason Evans | 1ff0953 | 2017-01-16 11:09:24 -0800 | [diff] [blame] | 1618 | prof_gctx_dump_iter_arg->tsdn = tsd_tsdn(tsd); |
| 1619 | prof_gctx_dump_iter_arg->propagate_err = propagate_err; |
| 1620 | if (gctx_tree_iter(gctxs, NULL, prof_gctx_dump_iter, |
| 1621 | (void *)prof_gctx_dump_iter_arg) != NULL) { |
Jason Evans | 3a81cbd | 2014-08-16 12:58:55 -0700 | [diff] [blame] | 1622 | goto label_write_error; |
Jason Evans | 1ff0953 | 2017-01-16 11:09:24 -0800 | [diff] [blame] | 1623 | } |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 1624 | |
Jason Evans | c717718 | 2010-02-11 09:25:56 -0800 | [diff] [blame] | 1625 | /* Dump /proc/<pid>/maps if possible. */ |
Jason Evans | 1ff0953 | 2017-01-16 11:09:24 -0800 | [diff] [blame] | 1626 | if (prof_dump_maps(propagate_err)) { |
Jason Evans | 4f37ef6 | 2014-01-16 13:23:56 -0800 | [diff] [blame] | 1627 | goto label_write_error; |
Jason Evans | 1ff0953 | 2017-01-16 11:09:24 -0800 | [diff] [blame] | 1628 | } |
Jason Evans | c717718 | 2010-02-11 09:25:56 -0800 | [diff] [blame] | 1629 | |
Jason Evans | 1ff0953 | 2017-01-16 11:09:24 -0800 | [diff] [blame] | 1630 | if (prof_dump_close(propagate_err)) { |
| 1631 | return true; |
| 1632 | } |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 1633 | |
Jason Evans | 1ff0953 | 2017-01-16 11:09:24 -0800 | [diff] [blame] | 1634 | return false; |
| 1635 | label_write_error: |
| 1636 | prof_dump_close(propagate_err); |
| 1637 | return true; |
| 1638 | } |
| 1639 | |
| 1640 | static bool |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 1641 | prof_dump(tsd_t *tsd, bool propagate_err, const char *filename, |
| 1642 | bool leakcheck) { |
Jason Evans | 1ff0953 | 2017-01-16 11:09:24 -0800 | [diff] [blame] | 1643 | prof_tdata_t *tdata; |
| 1644 | struct prof_tdata_merge_iter_arg_s prof_tdata_merge_iter_arg; |
| 1645 | struct prof_gctx_merge_iter_arg_s prof_gctx_merge_iter_arg; |
| 1646 | struct prof_gctx_dump_iter_arg_s prof_gctx_dump_iter_arg; |
| 1647 | prof_gctx_tree_t gctxs; |
| 1648 | bool err; |
| 1649 | |
| 1650 | cassert(config_prof); |
| 1651 | |
| 1652 | tdata = prof_tdata_get(tsd, true); |
| 1653 | if (tdata == NULL) { |
| 1654 | return true; |
| 1655 | } |
| 1656 | |
| 1657 | malloc_mutex_lock(tsd_tsdn(tsd), &prof_dump_mtx); |
| 1658 | |
| 1659 | prof_dump_prep(tsd, tdata, &prof_tdata_merge_iter_arg, |
| 1660 | &prof_gctx_merge_iter_arg, &gctxs); |
| 1661 | err = prof_dump_file(tsd, propagate_err, filename, leakcheck, tdata, |
| 1662 | &prof_tdata_merge_iter_arg, &prof_gctx_merge_iter_arg, |
| 1663 | &prof_gctx_dump_iter_arg, &gctxs); |
Jason Evans | 20c31de | 2014-10-02 23:01:10 -0700 | [diff] [blame] | 1664 | prof_gctx_finish(tsd, &gctxs); |
Jason Evans | 1ff0953 | 2017-01-16 11:09:24 -0800 | [diff] [blame] | 1665 | |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 1666 | malloc_mutex_unlock(tsd_tsdn(tsd), &prof_dump_mtx); |
Jason Evans | 4f37ef6 | 2014-01-16 13:23:56 -0800 | [diff] [blame] | 1667 | |
Jason Evans | 1ff0953 | 2017-01-16 11:09:24 -0800 | [diff] [blame] | 1668 | if (err) { |
| 1669 | return true; |
| 1670 | } |
| 1671 | |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1672 | if (leakcheck) { |
| 1673 | prof_leakcheck(&prof_tdata_merge_iter_arg.cnt_all, |
| 1674 | prof_gctx_merge_iter_arg.leak_ngctx, filename); |
| 1675 | } |
Jason Evans | 1ff0953 | 2017-01-16 11:09:24 -0800 | [diff] [blame] | 1676 | return false; |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 1677 | } |
| 1678 | |
Jason Evans | 1ff0953 | 2017-01-16 11:09:24 -0800 | [diff] [blame] | 1679 | #ifdef JEMALLOC_JET |
| 1680 | void |
| 1681 | prof_cnt_all(uint64_t *curobjs, uint64_t *curbytes, uint64_t *accumobjs, |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 1682 | uint64_t *accumbytes) { |
Jason Evans | 1ff0953 | 2017-01-16 11:09:24 -0800 | [diff] [blame] | 1683 | tsd_t *tsd; |
| 1684 | prof_tdata_t *tdata; |
| 1685 | struct prof_tdata_merge_iter_arg_s prof_tdata_merge_iter_arg; |
| 1686 | struct prof_gctx_merge_iter_arg_s prof_gctx_merge_iter_arg; |
| 1687 | prof_gctx_tree_t gctxs; |
| 1688 | |
| 1689 | tsd = tsd_fetch(); |
| 1690 | tdata = prof_tdata_get(tsd, false); |
| 1691 | if (tdata == NULL) { |
| 1692 | if (curobjs != NULL) { |
| 1693 | *curobjs = 0; |
| 1694 | } |
| 1695 | if (curbytes != NULL) { |
| 1696 | *curbytes = 0; |
| 1697 | } |
| 1698 | if (accumobjs != NULL) { |
| 1699 | *accumobjs = 0; |
| 1700 | } |
| 1701 | if (accumbytes != NULL) { |
| 1702 | *accumbytes = 0; |
| 1703 | } |
| 1704 | return; |
| 1705 | } |
| 1706 | |
| 1707 | prof_dump_prep(tsd, tdata, &prof_tdata_merge_iter_arg, |
| 1708 | &prof_gctx_merge_iter_arg, &gctxs); |
| 1709 | prof_gctx_finish(tsd, &gctxs); |
| 1710 | |
| 1711 | if (curobjs != NULL) { |
| 1712 | *curobjs = prof_tdata_merge_iter_arg.cnt_all.curobjs; |
| 1713 | } |
| 1714 | if (curbytes != NULL) { |
| 1715 | *curbytes = prof_tdata_merge_iter_arg.cnt_all.curbytes; |
| 1716 | } |
| 1717 | if (accumobjs != NULL) { |
| 1718 | *accumobjs = prof_tdata_merge_iter_arg.cnt_all.accumobjs; |
| 1719 | } |
| 1720 | if (accumbytes != NULL) { |
| 1721 | *accumbytes = prof_tdata_merge_iter_arg.cnt_all.accumbytes; |
| 1722 | } |
| 1723 | } |
| 1724 | #endif |
| 1725 | |
Jason Evans | c0cc5db | 2017-01-19 21:41:41 -0800 | [diff] [blame] | 1726 | #define DUMP_FILENAME_BUFSIZE (PATH_MAX + 1) |
| 1727 | #define VSEQ_INVALID UINT64_C(0xffffffffffffffff) |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 1728 | static void |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 1729 | prof_dump_filename(char *filename, char v, uint64_t vseq) { |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1730 | cassert(config_prof); |
| 1731 | |
Jason Evans | 4f37ef6 | 2014-01-16 13:23:56 -0800 | [diff] [blame] | 1732 | if (vseq != VSEQ_INVALID) { |
Jason Evans | d81e4bd | 2012-03-06 14:57:45 -0800 | [diff] [blame] | 1733 | /* "<prefix>.<pid>.<seq>.v<vseq>.heap" */ |
| 1734 | malloc_snprintf(filename, DUMP_FILENAME_BUFSIZE, |
Jason Evans | 5fae7dc | 2015-07-23 13:56:25 -0700 | [diff] [blame] | 1735 | "%s.%d.%"FMTu64".%c%"FMTu64".heap", |
Jason Evans | 788d29d | 2016-02-20 23:46:14 -0800 | [diff] [blame] | 1736 | opt_prof_prefix, prof_getpid(), prof_dump_seq, v, vseq); |
Jason Evans | d81e4bd | 2012-03-06 14:57:45 -0800 | [diff] [blame] | 1737 | } else { |
| 1738 | /* "<prefix>.<pid>.<seq>.<v>.heap" */ |
| 1739 | malloc_snprintf(filename, DUMP_FILENAME_BUFSIZE, |
Jason Evans | 5fae7dc | 2015-07-23 13:56:25 -0700 | [diff] [blame] | 1740 | "%s.%d.%"FMTu64".%c.heap", |
Jason Evans | 788d29d | 2016-02-20 23:46:14 -0800 | [diff] [blame] | 1741 | opt_prof_prefix, prof_getpid(), prof_dump_seq, v); |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 1742 | } |
Jason Evans | 52386b2 | 2012-04-22 16:00:11 -0700 | [diff] [blame] | 1743 | prof_dump_seq++; |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 1744 | } |
| 1745 | |
| 1746 | static void |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 1747 | prof_fdump(void) { |
Jason Evans | 5460aa6 | 2014-09-22 21:09:23 -0700 | [diff] [blame] | 1748 | tsd_t *tsd; |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 1749 | char filename[DUMP_FILENAME_BUFSIZE]; |
| 1750 | |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1751 | cassert(config_prof); |
Jason Evans | 57efa7b | 2014-10-08 17:57:19 -0700 | [diff] [blame] | 1752 | assert(opt_prof_final); |
| 1753 | assert(opt_prof_prefix[0] != '\0'); |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1754 | |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 1755 | if (!prof_booted) { |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 1756 | return; |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 1757 | } |
Jason Evans | 029d44c | 2014-10-04 11:12:53 -0700 | [diff] [blame] | 1758 | tsd = tsd_fetch(); |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 1759 | |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 1760 | malloc_mutex_lock(tsd_tsdn(tsd), &prof_dump_seq_mtx); |
Jason Evans | 57efa7b | 2014-10-08 17:57:19 -0700 | [diff] [blame] | 1761 | prof_dump_filename(filename, 'f', VSEQ_INVALID); |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 1762 | malloc_mutex_unlock(tsd_tsdn(tsd), &prof_dump_seq_mtx); |
Jason Evans | 57efa7b | 2014-10-08 17:57:19 -0700 | [diff] [blame] | 1763 | prof_dump(tsd, false, filename, opt_prof_leak); |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 1764 | } |
| 1765 | |
Jason Evans | fa2d64c | 2017-02-12 17:03:46 -0800 | [diff] [blame] | 1766 | bool |
| 1767 | prof_accum_init(tsdn_t *tsdn, prof_accum_t *prof_accum) { |
| 1768 | cassert(config_prof); |
| 1769 | |
| 1770 | #ifndef JEMALLOC_ATOMIC_U64 |
| 1771 | if (malloc_mutex_init(&prof_accum->mtx, "prof_accum", |
| 1772 | WITNESS_RANK_PROF_ACCUM)) { |
| 1773 | return true; |
| 1774 | } |
Jason Evans | fa2d64c | 2017-02-12 17:03:46 -0800 | [diff] [blame] | 1775 | prof_accum->accumbytes = 0; |
David Goldblatt | 30d74db | 2017-04-04 18:08:58 -0700 | [diff] [blame] | 1776 | #else |
| 1777 | atomic_store_u64(&prof_accum->accumbytes, 0, ATOMIC_RELAXED); |
| 1778 | #endif |
Jason Evans | fa2d64c | 2017-02-12 17:03:46 -0800 | [diff] [blame] | 1779 | return false; |
| 1780 | } |
| 1781 | |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 1782 | void |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 1783 | prof_idump(tsdn_t *tsdn) { |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 1784 | tsd_t *tsd; |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 1785 | prof_tdata_t *tdata; |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 1786 | |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1787 | cassert(config_prof); |
| 1788 | |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 1789 | if (!prof_booted || tsdn_null(tsdn)) { |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 1790 | return; |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 1791 | } |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 1792 | tsd = tsdn_tsd(tsdn); |
Jason Evans | 5460aa6 | 2014-09-22 21:09:23 -0700 | [diff] [blame] | 1793 | tdata = prof_tdata_get(tsd, false); |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 1794 | if (tdata == NULL) { |
Jason Evans | 52386b2 | 2012-04-22 16:00:11 -0700 | [diff] [blame] | 1795 | return; |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 1796 | } |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 1797 | if (tdata->enq) { |
| 1798 | tdata->enq_idump = true; |
Jason Evans | d34f9e7 | 2010-02-11 13:19:21 -0800 | [diff] [blame] | 1799 | return; |
| 1800 | } |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 1801 | |
Jason Evans | e733970 | 2010-10-23 18:37:06 -0700 | [diff] [blame] | 1802 | if (opt_prof_prefix[0] != '\0') { |
Dmitry-Me | 78ae1ac | 2015-09-08 15:09:20 +0300 | [diff] [blame] | 1803 | char filename[PATH_MAX + 1]; |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 1804 | malloc_mutex_lock(tsd_tsdn(tsd), &prof_dump_seq_mtx); |
Jason Evans | e733970 | 2010-10-23 18:37:06 -0700 | [diff] [blame] | 1805 | prof_dump_filename(filename, 'i', prof_dump_iseq); |
| 1806 | prof_dump_iseq++; |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 1807 | malloc_mutex_unlock(tsd_tsdn(tsd), &prof_dump_seq_mtx); |
Jason Evans | 5460aa6 | 2014-09-22 21:09:23 -0700 | [diff] [blame] | 1808 | prof_dump(tsd, false, filename, false); |
Jason Evans | e733970 | 2010-10-23 18:37:06 -0700 | [diff] [blame] | 1809 | } |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 1810 | } |
| 1811 | |
Jason Evans | 22ca855 | 2010-03-02 11:57:30 -0800 | [diff] [blame] | 1812 | bool |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 1813 | prof_mdump(tsd_t *tsd, const char *filename) { |
Jason Evans | 22ca855 | 2010-03-02 11:57:30 -0800 | [diff] [blame] | 1814 | char filename_buf[DUMP_FILENAME_BUFSIZE]; |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 1815 | |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1816 | cassert(config_prof); |
| 1817 | |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 1818 | if (!opt_prof || !prof_booted) { |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 1819 | return true; |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 1820 | } |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 1821 | |
Jason Evans | 22ca855 | 2010-03-02 11:57:30 -0800 | [diff] [blame] | 1822 | if (filename == NULL) { |
| 1823 | /* No filename specified, so automatically generate one. */ |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 1824 | if (opt_prof_prefix[0] == '\0') { |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 1825 | return true; |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 1826 | } |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 1827 | malloc_mutex_lock(tsd_tsdn(tsd), &prof_dump_seq_mtx); |
Jason Evans | 22ca855 | 2010-03-02 11:57:30 -0800 | [diff] [blame] | 1828 | prof_dump_filename(filename_buf, 'm', prof_dump_mseq); |
| 1829 | prof_dump_mseq++; |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 1830 | malloc_mutex_unlock(tsd_tsdn(tsd), &prof_dump_seq_mtx); |
Jason Evans | 22ca855 | 2010-03-02 11:57:30 -0800 | [diff] [blame] | 1831 | filename = filename_buf; |
| 1832 | } |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 1833 | return prof_dump(tsd, true, filename, false); |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 1834 | } |
| 1835 | |
| 1836 | void |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 1837 | prof_gdump(tsdn_t *tsdn) { |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 1838 | tsd_t *tsd; |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 1839 | prof_tdata_t *tdata; |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 1840 | |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1841 | cassert(config_prof); |
| 1842 | |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 1843 | if (!prof_booted || tsdn_null(tsdn)) { |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 1844 | return; |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 1845 | } |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 1846 | tsd = tsdn_tsd(tsdn); |
Jason Evans | 5460aa6 | 2014-09-22 21:09:23 -0700 | [diff] [blame] | 1847 | tdata = prof_tdata_get(tsd, false); |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 1848 | if (tdata == NULL) { |
Jason Evans | 52386b2 | 2012-04-22 16:00:11 -0700 | [diff] [blame] | 1849 | return; |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 1850 | } |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 1851 | if (tdata->enq) { |
| 1852 | tdata->enq_gdump = true; |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 1853 | return; |
| 1854 | } |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 1855 | |
Jason Evans | e733970 | 2010-10-23 18:37:06 -0700 | [diff] [blame] | 1856 | if (opt_prof_prefix[0] != '\0') { |
Dmitry-Me | 78ae1ac | 2015-09-08 15:09:20 +0300 | [diff] [blame] | 1857 | char filename[DUMP_FILENAME_BUFSIZE]; |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 1858 | malloc_mutex_lock(tsdn, &prof_dump_seq_mtx); |
Jason Evans | e733970 | 2010-10-23 18:37:06 -0700 | [diff] [blame] | 1859 | prof_dump_filename(filename, 'u', prof_dump_useq); |
| 1860 | prof_dump_useq++; |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 1861 | malloc_mutex_unlock(tsdn, &prof_dump_seq_mtx); |
Jason Evans | 5460aa6 | 2014-09-22 21:09:23 -0700 | [diff] [blame] | 1862 | prof_dump(tsd, false, filename, false); |
Jason Evans | e733970 | 2010-10-23 18:37:06 -0700 | [diff] [blame] | 1863 | } |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 1864 | } |
| 1865 | |
| 1866 | static void |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 1867 | prof_bt_hash(const void *key, size_t r_hash[2]) { |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 1868 | prof_bt_t *bt = (prof_bt_t *)key; |
| 1869 | |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1870 | cassert(config_prof); |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 1871 | |
Jason Evans | ae03bf6 | 2013-01-22 12:02:08 -0800 | [diff] [blame] | 1872 | hash(bt->vec, bt->len * sizeof(void *), 0x94122f33U, r_hash); |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 1873 | } |
| 1874 | |
| 1875 | static bool |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 1876 | prof_bt_keycomp(const void *k1, const void *k2) { |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 1877 | const prof_bt_t *bt1 = (prof_bt_t *)k1; |
| 1878 | const prof_bt_t *bt2 = (prof_bt_t *)k2; |
| 1879 | |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1880 | cassert(config_prof); |
| 1881 | |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 1882 | if (bt1->len != bt2->len) { |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 1883 | return false; |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 1884 | } |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 1885 | return (memcmp(bt1->vec, bt2->vec, bt1->len * sizeof(void *)) == 0); |
| 1886 | } |
| 1887 | |
David Goldblatt | 4d2e4bf | 2017-04-21 09:37:34 -0700 | [diff] [blame] | 1888 | static uint64_t |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 1889 | prof_thr_uid_alloc(tsdn_t *tsdn) { |
Jason Evans | 9d8f3d2 | 2014-09-11 18:06:30 -0700 | [diff] [blame] | 1890 | uint64_t thr_uid; |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 1891 | |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 1892 | malloc_mutex_lock(tsdn, &next_thr_uid_mtx); |
Jason Evans | 9d8f3d2 | 2014-09-11 18:06:30 -0700 | [diff] [blame] | 1893 | thr_uid = next_thr_uid; |
| 1894 | next_thr_uid++; |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 1895 | malloc_mutex_unlock(tsdn, &next_thr_uid_mtx); |
Jason Evans | 9d8f3d2 | 2014-09-11 18:06:30 -0700 | [diff] [blame] | 1896 | |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 1897 | return thr_uid; |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 1898 | } |
| 1899 | |
| 1900 | static prof_tdata_t * |
Jason Evans | b54d160 | 2016-10-20 23:59:12 -0700 | [diff] [blame] | 1901 | prof_tdata_init_impl(tsd_t *tsd, uint64_t thr_uid, uint64_t thr_discrim, |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 1902 | char *thread_name, bool active) { |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 1903 | prof_tdata_t *tdata; |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 1904 | |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1905 | cassert(config_prof); |
| 1906 | |
Jason Evans | 4d6a134 | 2010-10-20 19:05:59 -0700 | [diff] [blame] | 1907 | /* Initialize an empty cache for this thread. */ |
Jason Evans | b54d160 | 2016-10-20 23:59:12 -0700 | [diff] [blame] | 1908 | tdata = (prof_tdata_t *)iallocztm(tsd_tsdn(tsd), sizeof(prof_tdata_t), |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 1909 | size2index(sizeof(prof_tdata_t)), false, NULL, true, |
| 1910 | arena_get(TSDN_NULL, 0, true), true); |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 1911 | if (tdata == NULL) { |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 1912 | return NULL; |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 1913 | } |
Jason Evans | 4d6a134 | 2010-10-20 19:05:59 -0700 | [diff] [blame] | 1914 | |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 1915 | tdata->lock = prof_tdata_mutex_choose(thr_uid); |
| 1916 | tdata->thr_uid = thr_uid; |
Jason Evans | 20c31de | 2014-10-02 23:01:10 -0700 | [diff] [blame] | 1917 | tdata->thr_discrim = thr_discrim; |
Jason Evans | fc12c0b | 2014-10-03 23:25:30 -0700 | [diff] [blame] | 1918 | tdata->thread_name = thread_name; |
Jason Evans | 20c31de | 2014-10-02 23:01:10 -0700 | [diff] [blame] | 1919 | tdata->attached = true; |
| 1920 | tdata->expired = false; |
Jason Evans | 04211e2 | 2015-03-16 15:11:06 -0700 | [diff] [blame] | 1921 | tdata->tctx_uid_next = 0; |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 1922 | |
Jason Evans | b54d160 | 2016-10-20 23:59:12 -0700 | [diff] [blame] | 1923 | if (ckh_new(tsd, &tdata->bt2tctx, PROF_CKH_MINITEMS, prof_bt_hash, |
| 1924 | prof_bt_keycomp)) { |
Qi Wang | bfa530b | 2017-04-07 14:12:30 -0700 | [diff] [blame] | 1925 | idalloctm(tsd_tsdn(tsd), tdata, NULL, NULL, true, true); |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 1926 | return NULL; |
Jason Evans | 4d6a134 | 2010-10-20 19:05:59 -0700 | [diff] [blame] | 1927 | } |
Jason Evans | 4d6a134 | 2010-10-20 19:05:59 -0700 | [diff] [blame] | 1928 | |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 1929 | tdata->prng_state = (uint64_t)(uintptr_t)tdata; |
| 1930 | prof_sample_threshold_update(tdata); |
Jason Evans | 4d6a134 | 2010-10-20 19:05:59 -0700 | [diff] [blame] | 1931 | |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 1932 | tdata->enq = false; |
| 1933 | tdata->enq_idump = false; |
| 1934 | tdata->enq_gdump = false; |
Jason Evans | 52386b2 | 2012-04-22 16:00:11 -0700 | [diff] [blame] | 1935 | |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 1936 | tdata->dumping = false; |
Jason Evans | fc12c0b | 2014-10-03 23:25:30 -0700 | [diff] [blame] | 1937 | tdata->active = active; |
Jason Evans | 4d6a134 | 2010-10-20 19:05:59 -0700 | [diff] [blame] | 1938 | |
Jason Evans | b54d160 | 2016-10-20 23:59:12 -0700 | [diff] [blame] | 1939 | malloc_mutex_lock(tsd_tsdn(tsd), &tdatas_mtx); |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 1940 | tdata_tree_insert(&tdatas, tdata); |
Jason Evans | b54d160 | 2016-10-20 23:59:12 -0700 | [diff] [blame] | 1941 | malloc_mutex_unlock(tsd_tsdn(tsd), &tdatas_mtx); |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 1942 | |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 1943 | return tdata; |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 1944 | } |
| 1945 | |
| 1946 | prof_tdata_t * |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 1947 | prof_tdata_init(tsd_t *tsd) { |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 1948 | return prof_tdata_init_impl(tsd, prof_thr_uid_alloc(tsd_tsdn(tsd)), 0, |
| 1949 | NULL, prof_thread_active_init_get(tsd_tsdn(tsd))); |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 1950 | } |
| 1951 | |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 1952 | static bool |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 1953 | prof_tdata_should_destroy_unlocked(prof_tdata_t *tdata, bool even_if_attached) { |
| 1954 | if (tdata->attached && !even_if_attached) { |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 1955 | return false; |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 1956 | } |
| 1957 | if (ckh_count(&tdata->bt2tctx) != 0) { |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 1958 | return false; |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 1959 | } |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 1960 | return true; |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 1961 | } |
| 1962 | |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1963 | static bool |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 1964 | prof_tdata_should_destroy(tsdn_t *tsdn, prof_tdata_t *tdata, |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 1965 | bool even_if_attached) { |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 1966 | malloc_mutex_assert_owner(tsdn, tdata->lock); |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1967 | |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 1968 | return prof_tdata_should_destroy_unlocked(tdata, even_if_attached); |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1969 | } |
| 1970 | |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 1971 | static void |
Jason Evans | b54d160 | 2016-10-20 23:59:12 -0700 | [diff] [blame] | 1972 | prof_tdata_destroy_locked(tsd_t *tsd, prof_tdata_t *tdata, |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 1973 | bool even_if_attached) { |
Jason Evans | b54d160 | 2016-10-20 23:59:12 -0700 | [diff] [blame] | 1974 | malloc_mutex_assert_owner(tsd_tsdn(tsd), &tdatas_mtx); |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 1975 | |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 1976 | tdata_tree_remove(&tdatas, tdata); |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 1977 | |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 1978 | assert(prof_tdata_should_destroy_unlocked(tdata, even_if_attached)); |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1979 | |
Jason Evans | db72272 | 2016-03-23 20:29:33 -0700 | [diff] [blame] | 1980 | if (tdata->thread_name != NULL) { |
Qi Wang | bfa530b | 2017-04-07 14:12:30 -0700 | [diff] [blame] | 1981 | idalloctm(tsd_tsdn(tsd), tdata->thread_name, NULL, NULL, true, |
| 1982 | true); |
Jason Evans | db72272 | 2016-03-23 20:29:33 -0700 | [diff] [blame] | 1983 | } |
Jason Evans | b54d160 | 2016-10-20 23:59:12 -0700 | [diff] [blame] | 1984 | ckh_delete(tsd, &tdata->bt2tctx); |
Qi Wang | bfa530b | 2017-04-07 14:12:30 -0700 | [diff] [blame] | 1985 | idalloctm(tsd_tsdn(tsd), tdata, NULL, NULL, true, true); |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 1986 | } |
| 1987 | |
| 1988 | static void |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 1989 | prof_tdata_destroy(tsd_t *tsd, prof_tdata_t *tdata, bool even_if_attached) { |
Jason Evans | b54d160 | 2016-10-20 23:59:12 -0700 | [diff] [blame] | 1990 | malloc_mutex_lock(tsd_tsdn(tsd), &tdatas_mtx); |
| 1991 | prof_tdata_destroy_locked(tsd, tdata, even_if_attached); |
| 1992 | malloc_mutex_unlock(tsd_tsdn(tsd), &tdatas_mtx); |
Jason Evans | 20c31de | 2014-10-02 23:01:10 -0700 | [diff] [blame] | 1993 | } |
| 1994 | |
| 1995 | static void |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 1996 | prof_tdata_detach(tsd_t *tsd, prof_tdata_t *tdata) { |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 1997 | bool destroy_tdata; |
| 1998 | |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 1999 | malloc_mutex_lock(tsd_tsdn(tsd), tdata->lock); |
Jason Evans | 20c31de | 2014-10-02 23:01:10 -0700 | [diff] [blame] | 2000 | if (tdata->attached) { |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 2001 | destroy_tdata = prof_tdata_should_destroy(tsd_tsdn(tsd), tdata, |
| 2002 | true); |
Jason Evans | f04a0be | 2014-10-04 15:03:49 -0700 | [diff] [blame] | 2003 | /* |
| 2004 | * Only detach if !destroy_tdata, because detaching would allow |
| 2005 | * another thread to win the race to destroy tdata. |
| 2006 | */ |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2007 | if (!destroy_tdata) { |
Jason Evans | f04a0be | 2014-10-04 15:03:49 -0700 | [diff] [blame] | 2008 | tdata->attached = false; |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2009 | } |
Jason Evans | 029d44c | 2014-10-04 11:12:53 -0700 | [diff] [blame] | 2010 | tsd_prof_tdata_set(tsd, NULL); |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2011 | } else { |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 2012 | destroy_tdata = false; |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2013 | } |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 2014 | malloc_mutex_unlock(tsd_tsdn(tsd), tdata->lock); |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2015 | if (destroy_tdata) { |
Jason Evans | b54d160 | 2016-10-20 23:59:12 -0700 | [diff] [blame] | 2016 | prof_tdata_destroy(tsd, tdata, true); |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2017 | } |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 2018 | } |
| 2019 | |
Jason Evans | 20c31de | 2014-10-02 23:01:10 -0700 | [diff] [blame] | 2020 | prof_tdata_t * |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2021 | prof_tdata_reinit(tsd_t *tsd, prof_tdata_t *tdata) { |
Jason Evans | 20c31de | 2014-10-02 23:01:10 -0700 | [diff] [blame] | 2022 | uint64_t thr_uid = tdata->thr_uid; |
| 2023 | uint64_t thr_discrim = tdata->thr_discrim + 1; |
Jason Evans | fc12c0b | 2014-10-03 23:25:30 -0700 | [diff] [blame] | 2024 | char *thread_name = (tdata->thread_name != NULL) ? |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 2025 | prof_thread_name_alloc(tsd_tsdn(tsd), tdata->thread_name) : NULL; |
Jason Evans | fc12c0b | 2014-10-03 23:25:30 -0700 | [diff] [blame] | 2026 | bool active = tdata->active; |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 2027 | |
Jason Evans | 20c31de | 2014-10-02 23:01:10 -0700 | [diff] [blame] | 2028 | prof_tdata_detach(tsd, tdata); |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 2029 | return prof_tdata_init_impl(tsd, thr_uid, thr_discrim, thread_name, |
| 2030 | active); |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 2031 | } |
| 2032 | |
Jason Evans | 20c31de | 2014-10-02 23:01:10 -0700 | [diff] [blame] | 2033 | static bool |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2034 | prof_tdata_expire(tsdn_t *tsdn, prof_tdata_t *tdata) { |
Jason Evans | 20c31de | 2014-10-02 23:01:10 -0700 | [diff] [blame] | 2035 | bool destroy_tdata; |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 2036 | |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 2037 | malloc_mutex_lock(tsdn, tdata->lock); |
Jason Evans | 20c31de | 2014-10-02 23:01:10 -0700 | [diff] [blame] | 2038 | if (!tdata->expired) { |
| 2039 | tdata->expired = true; |
| 2040 | destroy_tdata = tdata->attached ? false : |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 2041 | prof_tdata_should_destroy(tsdn, tdata, false); |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2042 | } else { |
Jason Evans | 20c31de | 2014-10-02 23:01:10 -0700 | [diff] [blame] | 2043 | destroy_tdata = false; |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2044 | } |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 2045 | malloc_mutex_unlock(tsdn, tdata->lock); |
Jason Evans | 20c31de | 2014-10-02 23:01:10 -0700 | [diff] [blame] | 2046 | |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 2047 | return destroy_tdata; |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 2048 | } |
| 2049 | |
| 2050 | static prof_tdata_t * |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2051 | prof_tdata_reset_iter(prof_tdata_tree_t *tdatas, prof_tdata_t *tdata, |
| 2052 | void *arg) { |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 2053 | tsdn_t *tsdn = (tsdn_t *)arg; |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 2054 | |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 2055 | return (prof_tdata_expire(tsdn, tdata) ? tdata : NULL); |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 2056 | } |
| 2057 | |
| 2058 | void |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2059 | prof_reset(tsd_t *tsd, size_t lg_sample) { |
Jason Evans | 20c31de | 2014-10-02 23:01:10 -0700 | [diff] [blame] | 2060 | prof_tdata_t *next; |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 2061 | |
| 2062 | assert(lg_sample < (sizeof(uint64_t) << 3)); |
| 2063 | |
Jason Evans | b54d160 | 2016-10-20 23:59:12 -0700 | [diff] [blame] | 2064 | malloc_mutex_lock(tsd_tsdn(tsd), &prof_dump_mtx); |
| 2065 | malloc_mutex_lock(tsd_tsdn(tsd), &tdatas_mtx); |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 2066 | |
| 2067 | lg_prof_sample = lg_sample; |
Jason Evans | 20c31de | 2014-10-02 23:01:10 -0700 | [diff] [blame] | 2068 | |
| 2069 | next = NULL; |
| 2070 | do { |
| 2071 | prof_tdata_t *to_destroy = tdata_tree_iter(&tdatas, next, |
Jason Evans | b54d160 | 2016-10-20 23:59:12 -0700 | [diff] [blame] | 2072 | prof_tdata_reset_iter, (void *)tsd); |
Jason Evans | 20c31de | 2014-10-02 23:01:10 -0700 | [diff] [blame] | 2073 | if (to_destroy != NULL) { |
| 2074 | next = tdata_tree_next(&tdatas, to_destroy); |
Jason Evans | b54d160 | 2016-10-20 23:59:12 -0700 | [diff] [blame] | 2075 | prof_tdata_destroy_locked(tsd, to_destroy, false); |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2076 | } else { |
Jason Evans | 20c31de | 2014-10-02 23:01:10 -0700 | [diff] [blame] | 2077 | next = NULL; |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2078 | } |
Jason Evans | 20c31de | 2014-10-02 23:01:10 -0700 | [diff] [blame] | 2079 | } while (next != NULL); |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 2080 | |
Jason Evans | b54d160 | 2016-10-20 23:59:12 -0700 | [diff] [blame] | 2081 | malloc_mutex_unlock(tsd_tsdn(tsd), &tdatas_mtx); |
| 2082 | malloc_mutex_unlock(tsd_tsdn(tsd), &prof_dump_mtx); |
Jason Evans | 4d6a134 | 2010-10-20 19:05:59 -0700 | [diff] [blame] | 2083 | } |
| 2084 | |
Jason Evans | cd9a134 | 2012-03-21 18:33:03 -0700 | [diff] [blame] | 2085 | void |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2086 | prof_tdata_cleanup(tsd_t *tsd) { |
Jason Evans | 5460aa6 | 2014-09-22 21:09:23 -0700 | [diff] [blame] | 2087 | prof_tdata_t *tdata; |
Jason Evans | 4d6a134 | 2010-10-20 19:05:59 -0700 | [diff] [blame] | 2088 | |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2089 | if (!config_prof) { |
Jason Evans | 5460aa6 | 2014-09-22 21:09:23 -0700 | [diff] [blame] | 2090 | return; |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2091 | } |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 2092 | |
Jason Evans | 5460aa6 | 2014-09-22 21:09:23 -0700 | [diff] [blame] | 2093 | tdata = tsd_prof_tdata_get(tsd); |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2094 | if (tdata != NULL) { |
Jason Evans | 5460aa6 | 2014-09-22 21:09:23 -0700 | [diff] [blame] | 2095 | prof_tdata_detach(tsd, tdata); |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2096 | } |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 2097 | } |
| 2098 | |
Jason Evans | fc12c0b | 2014-10-03 23:25:30 -0700 | [diff] [blame] | 2099 | bool |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2100 | prof_active_get(tsdn_t *tsdn) { |
Jason Evans | fc12c0b | 2014-10-03 23:25:30 -0700 | [diff] [blame] | 2101 | bool prof_active_current; |
| 2102 | |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 2103 | malloc_mutex_lock(tsdn, &prof_active_mtx); |
Jason Evans | fc12c0b | 2014-10-03 23:25:30 -0700 | [diff] [blame] | 2104 | prof_active_current = prof_active; |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 2105 | malloc_mutex_unlock(tsdn, &prof_active_mtx); |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 2106 | return prof_active_current; |
Jason Evans | fc12c0b | 2014-10-03 23:25:30 -0700 | [diff] [blame] | 2107 | } |
| 2108 | |
| 2109 | bool |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2110 | prof_active_set(tsdn_t *tsdn, bool active) { |
Jason Evans | fc12c0b | 2014-10-03 23:25:30 -0700 | [diff] [blame] | 2111 | bool prof_active_old; |
| 2112 | |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 2113 | malloc_mutex_lock(tsdn, &prof_active_mtx); |
Jason Evans | fc12c0b | 2014-10-03 23:25:30 -0700 | [diff] [blame] | 2114 | prof_active_old = prof_active; |
| 2115 | prof_active = active; |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 2116 | malloc_mutex_unlock(tsdn, &prof_active_mtx); |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 2117 | return prof_active_old; |
Jason Evans | fc12c0b | 2014-10-03 23:25:30 -0700 | [diff] [blame] | 2118 | } |
| 2119 | |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 2120 | const char * |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2121 | prof_thread_name_get(tsd_t *tsd) { |
Jason Evans | 5460aa6 | 2014-09-22 21:09:23 -0700 | [diff] [blame] | 2122 | prof_tdata_t *tdata; |
| 2123 | |
Jason Evans | 5460aa6 | 2014-09-22 21:09:23 -0700 | [diff] [blame] | 2124 | tdata = prof_tdata_get(tsd, true); |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2125 | if (tdata == NULL) { |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 2126 | return ""; |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2127 | } |
Jason Evans | fc12c0b | 2014-10-03 23:25:30 -0700 | [diff] [blame] | 2128 | return (tdata->thread_name != NULL ? tdata->thread_name : ""); |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 2129 | } |
| 2130 | |
Jason Evans | fc12c0b | 2014-10-03 23:25:30 -0700 | [diff] [blame] | 2131 | static char * |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2132 | prof_thread_name_alloc(tsdn_t *tsdn, const char *thread_name) { |
Jason Evans | fc12c0b | 2014-10-03 23:25:30 -0700 | [diff] [blame] | 2133 | char *ret; |
| 2134 | size_t size; |
| 2135 | |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2136 | if (thread_name == NULL) { |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 2137 | return NULL; |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2138 | } |
Jason Evans | fc12c0b | 2014-10-03 23:25:30 -0700 | [diff] [blame] | 2139 | |
| 2140 | size = strlen(thread_name) + 1; |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2141 | if (size == 1) { |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 2142 | return ""; |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2143 | } |
Jason Evans | fc12c0b | 2014-10-03 23:25:30 -0700 | [diff] [blame] | 2144 | |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 2145 | ret = iallocztm(tsdn, size, size2index(size), false, NULL, true, |
| 2146 | arena_get(TSDN_NULL, 0, true), true); |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2147 | if (ret == NULL) { |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 2148 | return NULL; |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2149 | } |
Jason Evans | fc12c0b | 2014-10-03 23:25:30 -0700 | [diff] [blame] | 2150 | memcpy(ret, thread_name, size); |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 2151 | return ret; |
Jason Evans | fc12c0b | 2014-10-03 23:25:30 -0700 | [diff] [blame] | 2152 | } |
| 2153 | |
| 2154 | int |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2155 | prof_thread_name_set(tsd_t *tsd, const char *thread_name) { |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 2156 | prof_tdata_t *tdata; |
Jason Evans | fc12c0b | 2014-10-03 23:25:30 -0700 | [diff] [blame] | 2157 | unsigned i; |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 2158 | char *s; |
| 2159 | |
Jason Evans | 5460aa6 | 2014-09-22 21:09:23 -0700 | [diff] [blame] | 2160 | tdata = prof_tdata_get(tsd, true); |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2161 | if (tdata == NULL) { |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 2162 | return EAGAIN; |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2163 | } |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 2164 | |
Jason Evans | fc12c0b | 2014-10-03 23:25:30 -0700 | [diff] [blame] | 2165 | /* Validate input. */ |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2166 | if (thread_name == NULL) { |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 2167 | return EFAULT; |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2168 | } |
Jason Evans | fc12c0b | 2014-10-03 23:25:30 -0700 | [diff] [blame] | 2169 | for (i = 0; thread_name[i] != '\0'; i++) { |
| 2170 | char c = thread_name[i]; |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2171 | if (!isgraph(c) && !isblank(c)) { |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 2172 | return EFAULT; |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2173 | } |
Jason Evans | fc12c0b | 2014-10-03 23:25:30 -0700 | [diff] [blame] | 2174 | } |
| 2175 | |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 2176 | s = prof_thread_name_alloc(tsd_tsdn(tsd), thread_name); |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2177 | if (s == NULL) { |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 2178 | return EAGAIN; |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2179 | } |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 2180 | |
Jason Evans | fc12c0b | 2014-10-03 23:25:30 -0700 | [diff] [blame] | 2181 | if (tdata->thread_name != NULL) { |
Qi Wang | bfa530b | 2017-04-07 14:12:30 -0700 | [diff] [blame] | 2182 | idalloctm(tsd_tsdn(tsd), tdata->thread_name, NULL, NULL, true, |
| 2183 | true); |
Jason Evans | fc12c0b | 2014-10-03 23:25:30 -0700 | [diff] [blame] | 2184 | tdata->thread_name = NULL; |
| 2185 | } |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2186 | if (strlen(s) > 0) { |
Jason Evans | fc12c0b | 2014-10-03 23:25:30 -0700 | [diff] [blame] | 2187 | tdata->thread_name = s; |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2188 | } |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 2189 | return 0; |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 2190 | } |
| 2191 | |
| 2192 | bool |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2193 | prof_thread_active_get(tsd_t *tsd) { |
Jason Evans | 5460aa6 | 2014-09-22 21:09:23 -0700 | [diff] [blame] | 2194 | prof_tdata_t *tdata; |
| 2195 | |
Jason Evans | 5460aa6 | 2014-09-22 21:09:23 -0700 | [diff] [blame] | 2196 | tdata = prof_tdata_get(tsd, true); |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2197 | if (tdata == NULL) { |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 2198 | return false; |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2199 | } |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 2200 | return tdata->active; |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 2201 | } |
| 2202 | |
| 2203 | bool |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2204 | prof_thread_active_set(tsd_t *tsd, bool active) { |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 2205 | prof_tdata_t *tdata; |
| 2206 | |
Jason Evans | 5460aa6 | 2014-09-22 21:09:23 -0700 | [diff] [blame] | 2207 | tdata = prof_tdata_get(tsd, true); |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2208 | if (tdata == NULL) { |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 2209 | return true; |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2210 | } |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 2211 | tdata->active = active; |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 2212 | return false; |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 2213 | } |
| 2214 | |
Jason Evans | fc12c0b | 2014-10-03 23:25:30 -0700 | [diff] [blame] | 2215 | bool |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2216 | prof_thread_active_init_get(tsdn_t *tsdn) { |
Jason Evans | fc12c0b | 2014-10-03 23:25:30 -0700 | [diff] [blame] | 2217 | bool active_init; |
| 2218 | |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 2219 | malloc_mutex_lock(tsdn, &prof_thread_active_init_mtx); |
Jason Evans | fc12c0b | 2014-10-03 23:25:30 -0700 | [diff] [blame] | 2220 | active_init = prof_thread_active_init; |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 2221 | malloc_mutex_unlock(tsdn, &prof_thread_active_init_mtx); |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 2222 | return active_init; |
Jason Evans | fc12c0b | 2014-10-03 23:25:30 -0700 | [diff] [blame] | 2223 | } |
| 2224 | |
| 2225 | bool |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2226 | prof_thread_active_init_set(tsdn_t *tsdn, bool active_init) { |
Jason Evans | fc12c0b | 2014-10-03 23:25:30 -0700 | [diff] [blame] | 2227 | bool active_init_old; |
| 2228 | |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 2229 | malloc_mutex_lock(tsdn, &prof_thread_active_init_mtx); |
Jason Evans | fc12c0b | 2014-10-03 23:25:30 -0700 | [diff] [blame] | 2230 | active_init_old = prof_thread_active_init; |
| 2231 | prof_thread_active_init = active_init; |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 2232 | malloc_mutex_unlock(tsdn, &prof_thread_active_init_mtx); |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 2233 | return active_init_old; |
Jason Evans | fc12c0b | 2014-10-03 23:25:30 -0700 | [diff] [blame] | 2234 | } |
| 2235 | |
Jason Evans | 5b8ed5b | 2015-01-25 21:16:57 -0800 | [diff] [blame] | 2236 | bool |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2237 | prof_gdump_get(tsdn_t *tsdn) { |
Jason Evans | 5b8ed5b | 2015-01-25 21:16:57 -0800 | [diff] [blame] | 2238 | bool prof_gdump_current; |
| 2239 | |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 2240 | malloc_mutex_lock(tsdn, &prof_gdump_mtx); |
Jason Evans | 5b8ed5b | 2015-01-25 21:16:57 -0800 | [diff] [blame] | 2241 | prof_gdump_current = prof_gdump_val; |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 2242 | malloc_mutex_unlock(tsdn, &prof_gdump_mtx); |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 2243 | return prof_gdump_current; |
Jason Evans | 5b8ed5b | 2015-01-25 21:16:57 -0800 | [diff] [blame] | 2244 | } |
| 2245 | |
| 2246 | bool |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2247 | prof_gdump_set(tsdn_t *tsdn, bool gdump) { |
Jason Evans | 5b8ed5b | 2015-01-25 21:16:57 -0800 | [diff] [blame] | 2248 | bool prof_gdump_old; |
| 2249 | |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 2250 | malloc_mutex_lock(tsdn, &prof_gdump_mtx); |
Jason Evans | 5b8ed5b | 2015-01-25 21:16:57 -0800 | [diff] [blame] | 2251 | prof_gdump_old = prof_gdump_val; |
| 2252 | prof_gdump_val = gdump; |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 2253 | malloc_mutex_unlock(tsdn, &prof_gdump_mtx); |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 2254 | return prof_gdump_old; |
Jason Evans | 5b8ed5b | 2015-01-25 21:16:57 -0800 | [diff] [blame] | 2255 | } |
| 2256 | |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 2257 | void |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2258 | prof_boot0(void) { |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 2259 | cassert(config_prof); |
| 2260 | |
Jason Evans | e733970 | 2010-10-23 18:37:06 -0700 | [diff] [blame] | 2261 | memcpy(opt_prof_prefix, PROF_PREFIX_DEFAULT, |
| 2262 | sizeof(PROF_PREFIX_DEFAULT)); |
| 2263 | } |
| 2264 | |
| 2265 | void |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2266 | prof_boot1(void) { |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 2267 | cassert(config_prof); |
| 2268 | |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 2269 | /* |
Jason Evans | 9b0cbf0 | 2014-04-11 14:24:51 -0700 | [diff] [blame] | 2270 | * opt_prof must be in its final state before any arenas are |
| 2271 | * initialized, so this function must be executed early. |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 2272 | */ |
| 2273 | |
Jason Evans | 551ebc4 | 2014-10-03 10:16:09 -0700 | [diff] [blame] | 2274 | if (opt_prof_leak && !opt_prof) { |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 2275 | /* |
| 2276 | * Enable opt_prof, but in such a way that profiles are never |
| 2277 | * automatically dumped. |
| 2278 | */ |
| 2279 | opt_prof = true; |
Jason Evans | e733970 | 2010-10-23 18:37:06 -0700 | [diff] [blame] | 2280 | opt_prof_gdump = false; |
Jason Evans | a02fc08 | 2010-03-31 17:35:51 -0700 | [diff] [blame] | 2281 | } else if (opt_prof) { |
| 2282 | if (opt_lg_prof_interval >= 0) { |
| 2283 | prof_interval = (((uint64_t)1U) << |
| 2284 | opt_lg_prof_interval); |
Jason Evans | a3b3386 | 2012-11-13 12:56:27 -0800 | [diff] [blame] | 2285 | } |
Jason Evans | a02fc08 | 2010-03-31 17:35:51 -0700 | [diff] [blame] | 2286 | } |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 2287 | } |
| 2288 | |
| 2289 | bool |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2290 | prof_boot2(tsd_t *tsd) { |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 2291 | cassert(config_prof); |
| 2292 | |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 2293 | if (opt_prof) { |
Jason Evans | 6da5418 | 2012-03-23 18:05:51 -0700 | [diff] [blame] | 2294 | unsigned i; |
| 2295 | |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 2296 | lg_prof_sample = opt_lg_prof_sample; |
| 2297 | |
Jason Evans | fc12c0b | 2014-10-03 23:25:30 -0700 | [diff] [blame] | 2298 | prof_active = opt_prof_active; |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 2299 | if (malloc_mutex_init(&prof_active_mtx, "prof_active", |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2300 | WITNESS_RANK_PROF_ACTIVE)) { |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 2301 | return true; |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2302 | } |
Jason Evans | fc12c0b | 2014-10-03 23:25:30 -0700 | [diff] [blame] | 2303 | |
Jason Evans | 5b8ed5b | 2015-01-25 21:16:57 -0800 | [diff] [blame] | 2304 | prof_gdump_val = opt_prof_gdump; |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 2305 | if (malloc_mutex_init(&prof_gdump_mtx, "prof_gdump", |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2306 | WITNESS_RANK_PROF_GDUMP)) { |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 2307 | return true; |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2308 | } |
Jason Evans | 5b8ed5b | 2015-01-25 21:16:57 -0800 | [diff] [blame] | 2309 | |
Jason Evans | fc12c0b | 2014-10-03 23:25:30 -0700 | [diff] [blame] | 2310 | prof_thread_active_init = opt_prof_thread_active_init; |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 2311 | if (malloc_mutex_init(&prof_thread_active_init_mtx, |
| 2312 | "prof_thread_active_init", |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2313 | WITNESS_RANK_PROF_THREAD_ACTIVE_INIT)) { |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 2314 | return true; |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2315 | } |
Jason Evans | fc12c0b | 2014-10-03 23:25:30 -0700 | [diff] [blame] | 2316 | |
Jason Evans | b54d160 | 2016-10-20 23:59:12 -0700 | [diff] [blame] | 2317 | if (ckh_new(tsd, &bt2gctx, PROF_CKH_MINITEMS, prof_bt_hash, |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2318 | prof_bt_keycomp)) { |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 2319 | return true; |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2320 | } |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 2321 | if (malloc_mutex_init(&bt2gctx_mtx, "prof_bt2gctx", |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2322 | WITNESS_RANK_PROF_BT2GCTX)) { |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 2323 | return true; |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2324 | } |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 2325 | |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 2326 | tdata_tree_new(&tdatas); |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 2327 | if (malloc_mutex_init(&tdatas_mtx, "prof_tdatas", |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2328 | WITNESS_RANK_PROF_TDATAS)) { |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 2329 | return true; |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2330 | } |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 2331 | |
| 2332 | next_thr_uid = 0; |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 2333 | if (malloc_mutex_init(&next_thr_uid_mtx, "prof_next_thr_uid", |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2334 | WITNESS_RANK_PROF_NEXT_THR_UID)) { |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 2335 | return true; |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2336 | } |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 2337 | |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 2338 | if (malloc_mutex_init(&prof_dump_seq_mtx, "prof_dump_seq", |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2339 | WITNESS_RANK_PROF_DUMP_SEQ)) { |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 2340 | return true; |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2341 | } |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 2342 | if (malloc_mutex_init(&prof_dump_mtx, "prof_dump", |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2343 | WITNESS_RANK_PROF_DUMP)) { |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 2344 | return true; |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2345 | } |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 2346 | |
Jason Evans | 57efa7b | 2014-10-08 17:57:19 -0700 | [diff] [blame] | 2347 | if (opt_prof_final && opt_prof_prefix[0] != '\0' && |
| 2348 | atexit(prof_fdump) != 0) { |
Jason Evans | 698805c | 2010-03-03 17:45:38 -0800 | [diff] [blame] | 2349 | malloc_write("<jemalloc>: Error in atexit()\n"); |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2350 | if (opt_abort) { |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 2351 | abort(); |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2352 | } |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 2353 | } |
Jason Evans | 6da5418 | 2012-03-23 18:05:51 -0700 | [diff] [blame] | 2354 | |
Jason Evans | b54d160 | 2016-10-20 23:59:12 -0700 | [diff] [blame] | 2355 | gctx_locks = (malloc_mutex_t *)base_alloc(tsd_tsdn(tsd), |
Jason Evans | a0dd3a4 | 2016-12-22 16:39:10 -0600 | [diff] [blame] | 2356 | b0get(), PROF_NCTX_LOCKS * sizeof(malloc_mutex_t), |
| 2357 | CACHELINE); |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2358 | if (gctx_locks == NULL) { |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 2359 | return true; |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2360 | } |
Jason Evans | 6da5418 | 2012-03-23 18:05:51 -0700 | [diff] [blame] | 2361 | for (i = 0; i < PROF_NCTX_LOCKS; i++) { |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 2362 | if (malloc_mutex_init(&gctx_locks[i], "prof_gctx", |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2363 | WITNESS_RANK_PROF_GCTX)) { |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 2364 | return true; |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2365 | } |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 2366 | } |
| 2367 | |
Jason Evans | b54d160 | 2016-10-20 23:59:12 -0700 | [diff] [blame] | 2368 | tdata_locks = (malloc_mutex_t *)base_alloc(tsd_tsdn(tsd), |
Jason Evans | a0dd3a4 | 2016-12-22 16:39:10 -0600 | [diff] [blame] | 2369 | b0get(), PROF_NTDATA_LOCKS * sizeof(malloc_mutex_t), |
| 2370 | CACHELINE); |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2371 | if (tdata_locks == NULL) { |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 2372 | return true; |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2373 | } |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 2374 | for (i = 0; i < PROF_NTDATA_LOCKS; i++) { |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 2375 | if (malloc_mutex_init(&tdata_locks[i], "prof_tdata", |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2376 | WITNESS_RANK_PROF_TDATA)) { |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 2377 | return true; |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2378 | } |
Jason Evans | 6da5418 | 2012-03-23 18:05:51 -0700 | [diff] [blame] | 2379 | } |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 2380 | } |
| 2381 | |
Jason Evans | b27805b | 2010-02-10 18:15:53 -0800 | [diff] [blame] | 2382 | #ifdef JEMALLOC_PROF_LIBGCC |
| 2383 | /* |
| 2384 | * Cause the backtracing machinery to allocate its internal state |
| 2385 | * before enabling profiling. |
| 2386 | */ |
| 2387 | _Unwind_Backtrace(prof_unwind_init_callback, NULL); |
| 2388 | #endif |
| 2389 | |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 2390 | prof_booted = true; |
| 2391 | |
Jason Evans | f408643 | 2017-01-19 18:15:45 -0800 | [diff] [blame] | 2392 | return false; |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 2393 | } |
| 2394 | |
Jason Evans | 20f1fc9 | 2012-10-09 14:46:22 -0700 | [diff] [blame] | 2395 | void |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2396 | prof_prefork0(tsdn_t *tsdn) { |
Jason Evans | 397f54a | 2017-01-29 17:35:57 -0800 | [diff] [blame] | 2397 | if (config_prof && opt_prof) { |
Jason Evans | 20f1fc9 | 2012-10-09 14:46:22 -0700 | [diff] [blame] | 2398 | unsigned i; |
| 2399 | |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 2400 | malloc_mutex_prefork(tsdn, &prof_dump_mtx); |
| 2401 | malloc_mutex_prefork(tsdn, &bt2gctx_mtx); |
| 2402 | malloc_mutex_prefork(tsdn, &tdatas_mtx); |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2403 | for (i = 0; i < PROF_NTDATA_LOCKS; i++) { |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 2404 | malloc_mutex_prefork(tsdn, &tdata_locks[i]); |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2405 | } |
| 2406 | for (i = 0; i < PROF_NCTX_LOCKS; i++) { |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 2407 | malloc_mutex_prefork(tsdn, &gctx_locks[i]); |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2408 | } |
Jason Evans | 174c0c3 | 2016-04-25 23:14:40 -0700 | [diff] [blame] | 2409 | } |
| 2410 | } |
| 2411 | |
| 2412 | void |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2413 | prof_prefork1(tsdn_t *tsdn) { |
Jason Evans | 397f54a | 2017-01-29 17:35:57 -0800 | [diff] [blame] | 2414 | if (config_prof && opt_prof) { |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 2415 | malloc_mutex_prefork(tsdn, &prof_active_mtx); |
| 2416 | malloc_mutex_prefork(tsdn, &prof_dump_seq_mtx); |
| 2417 | malloc_mutex_prefork(tsdn, &prof_gdump_mtx); |
| 2418 | malloc_mutex_prefork(tsdn, &next_thr_uid_mtx); |
| 2419 | malloc_mutex_prefork(tsdn, &prof_thread_active_init_mtx); |
Jason Evans | 20f1fc9 | 2012-10-09 14:46:22 -0700 | [diff] [blame] | 2420 | } |
| 2421 | } |
| 2422 | |
| 2423 | void |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2424 | prof_postfork_parent(tsdn_t *tsdn) { |
Jason Evans | 397f54a | 2017-01-29 17:35:57 -0800 | [diff] [blame] | 2425 | if (config_prof && opt_prof) { |
Jason Evans | 20f1fc9 | 2012-10-09 14:46:22 -0700 | [diff] [blame] | 2426 | unsigned i; |
| 2427 | |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 2428 | malloc_mutex_postfork_parent(tsdn, |
| 2429 | &prof_thread_active_init_mtx); |
| 2430 | malloc_mutex_postfork_parent(tsdn, &next_thr_uid_mtx); |
| 2431 | malloc_mutex_postfork_parent(tsdn, &prof_gdump_mtx); |
| 2432 | malloc_mutex_postfork_parent(tsdn, &prof_dump_seq_mtx); |
| 2433 | malloc_mutex_postfork_parent(tsdn, &prof_active_mtx); |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2434 | for (i = 0; i < PROF_NCTX_LOCKS; i++) { |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 2435 | malloc_mutex_postfork_parent(tsdn, &gctx_locks[i]); |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2436 | } |
| 2437 | for (i = 0; i < PROF_NTDATA_LOCKS; i++) { |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 2438 | malloc_mutex_postfork_parent(tsdn, &tdata_locks[i]); |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2439 | } |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 2440 | malloc_mutex_postfork_parent(tsdn, &tdatas_mtx); |
| 2441 | malloc_mutex_postfork_parent(tsdn, &bt2gctx_mtx); |
| 2442 | malloc_mutex_postfork_parent(tsdn, &prof_dump_mtx); |
Jason Evans | 20f1fc9 | 2012-10-09 14:46:22 -0700 | [diff] [blame] | 2443 | } |
| 2444 | } |
| 2445 | |
| 2446 | void |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2447 | prof_postfork_child(tsdn_t *tsdn) { |
Jason Evans | 397f54a | 2017-01-29 17:35:57 -0800 | [diff] [blame] | 2448 | if (config_prof && opt_prof) { |
Jason Evans | 20f1fc9 | 2012-10-09 14:46:22 -0700 | [diff] [blame] | 2449 | unsigned i; |
| 2450 | |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 2451 | malloc_mutex_postfork_child(tsdn, &prof_thread_active_init_mtx); |
| 2452 | malloc_mutex_postfork_child(tsdn, &next_thr_uid_mtx); |
| 2453 | malloc_mutex_postfork_child(tsdn, &prof_gdump_mtx); |
| 2454 | malloc_mutex_postfork_child(tsdn, &prof_dump_seq_mtx); |
| 2455 | malloc_mutex_postfork_child(tsdn, &prof_active_mtx); |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2456 | for (i = 0; i < PROF_NCTX_LOCKS; i++) { |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 2457 | malloc_mutex_postfork_child(tsdn, &gctx_locks[i]); |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2458 | } |
| 2459 | for (i = 0; i < PROF_NTDATA_LOCKS; i++) { |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 2460 | malloc_mutex_postfork_child(tsdn, &tdata_locks[i]); |
Jason Evans | c4c2592 | 2017-01-15 16:56:30 -0800 | [diff] [blame] | 2461 | } |
Jason Evans | c1e00ef | 2016-05-10 22:21:10 -0700 | [diff] [blame] | 2462 | malloc_mutex_postfork_child(tsdn, &tdatas_mtx); |
| 2463 | malloc_mutex_postfork_child(tsdn, &bt2gctx_mtx); |
| 2464 | malloc_mutex_postfork_child(tsdn, &prof_dump_mtx); |
Jason Evans | 20f1fc9 | 2012-10-09 14:46:22 -0700 | [diff] [blame] | 2465 | } |
| 2466 | } |
| 2467 | |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 2468 | /******************************************************************************/ |