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