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