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