aboutsummaryrefslogtreecommitdiffstats
path: root/src/mutex.c
diff options
context:
space:
mode:
authorQi Wang <interwq@gwu.edu>2017-02-23 14:18:07 -0800
committerQi Wang <interwq@gmail.com>2017-03-23 00:03:28 -0700
commit6309df628fa4f11dce084dc53c77ea852408d347 (patch)
treeb04f7cae7e173cc3ec9fce75d4712fd200446d2b /src/mutex.c
parent32e7cf51cd879e4f2b0307bba544f913e2d77a7e (diff)
downloadplatform_external_jemalloc_new-6309df628fa4f11dce084dc53c77ea852408d347.tar.gz
platform_external_jemalloc_new-6309df628fa4f11dce084dc53c77ea852408d347.tar.bz2
platform_external_jemalloc_new-6309df628fa4f11dce084dc53c77ea852408d347.zip
First stage of mutex profiling.
Switched to trylock and update counters based on state.
Diffstat (limited to 'src/mutex.c')
-rw-r--r--src/mutex.c43
1 files changed, 43 insertions, 0 deletions
diff --git a/src/mutex.c b/src/mutex.c
index f1aa155e..2b80a9d9 100644
--- a/src/mutex.c
+++ b/src/mutex.c
@@ -65,6 +65,49 @@ JEMALLOC_EXPORT int _pthread_mutex_init_calloc_cb(pthread_mutex_t *mutex,
void *(calloc_cb)(size_t, size_t));
#endif
+void
+malloc_mutex_lock_slow(malloc_mutex_t *mutex) {
+ lock_prof_data_t *data = &mutex->prof_data;
+ bool spin_success = false;
+
+ {//TODO: a smart spin policy
+ if (!malloc_mutex_trylock(mutex)) {
+ spin_success = true;
+ goto label_locked;
+ }
+ }
+
+ nstime_t now, before;
+ uint32_t n_thds;
+ nstime_init(&now, 0);
+ nstime_update(&now);
+ n_thds = atomic_add_u32(&data->n_waiting_thds, 1);
+ /* One last try as above two calls may take quite some cycles. */
+ if (!malloc_mutex_trylock(mutex)) {
+ spin_success = true;
+ atomic_sub_u32(&data->n_waiting_thds, 1);
+ goto label_locked;
+ }
+ nstime_copy(&before, &now);
+ malloc_mutex_lock_final(mutex);
+ atomic_sub_u32(&data->n_waiting_thds, 1);
+ nstime_update(&now);
+ nstime_subtract(&now, &before);
+label_locked:
+ if (spin_success) {
+ data->n_spin_acquired++;
+ } else {
+ data->n_wait_times++;
+ nstime_add(&data->tot_wait_time, &now);
+ if (nstime_compare(&now, &data->max_wait_time)) {
+ nstime_copy(&data->max_wait_time, &now);
+ }
+ if (n_thds > data->max_n_thds) {
+ data->max_n_thds = n_thds;
+ }
+ }
+}
+
bool
malloc_mutex_init(malloc_mutex_t *mutex, const char *name,
witness_rank_t rank) {