From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-4.0 required=3.0 tests=ALL_TRUSTED,AWL,BAYES_00, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 4AD101F953 for ; Mon, 19 Dec 2022 11:19:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=80x24.org; s=selector1; t=1671448763; bh=2+n9JoYz0iOsOpJue0p1oNpFqDD6hiVyJn9cP85spbQ=; h=From:To:Subject:Date:In-Reply-To:References:From; b=oksNRILzMcvLgmUQfKU+K/k75HYVAPthLs/ZMkfaOvW/NzfzSKPHolLJPy6QVnSl5 AMtXZRxE3Q9xLvqnMTEa1vr4pBI2eBe2Yv8srqH9DfY90DpsD8eUq2J8ZJmcqSgheq Gzc3b1ogM6IeCiqigrLDBUKMq4VpSpmNp2nbHELU= From: Eric Wong To: mwrap-perl@80x24.org Subject: [PATCH 1/7] core: shrink src_loc 8 bytes on x86-64 Date: Mon, 19 Dec 2022 11:19:16 +0000 Message-Id: <20221219111922.1079128-2-e@80x24.org> In-Reply-To: <20221219111922.1079128-1-e@80x24.org> References: <20221219111922.1079128-1-e@80x24.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: We can rely on the stable value of ->loc_hash to assign the mutex at when it requires locking rather than relying on a monotonically increasing counter. --- mwrap_core.h | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/mwrap_core.h b/mwrap_core.h index 7681ca5..7be0a7a 100644 --- a/mwrap_core.h +++ b/mwrap_core.h @@ -93,7 +93,6 @@ union padded_mutex { #else /* only tested on Linux + glibc */ # define STATIC_MTX_INIT_OK (1) #endif -static size_t mutex_i; static union padded_mutex mutexes[MUTEX_NR] = { #if STATIC_MTX_INIT_OK [0 ... (MUTEX_NR-1)].mtx = PTHREAD_MUTEX_INITIALIZER @@ -105,11 +104,6 @@ static union padded_mutex mutexes[MUTEX_NR] = { static_assert(UINT32_MAX > PATH_MAX, "UINT32_MAX > PATH_MAX"); #endif -static pthread_mutex_t *mutex_assign(void) -{ - return &mutexes[uatomic_add_return(&mutex_i, 1) & MUTEX_MASK].mtx; -} - static struct cds_lfht *lfht_new(size_t size) { return cds_lfht_new(size, 1, 0, CDS_LFHT_AUTO_RESIZE, 0); @@ -163,7 +157,6 @@ struct src_file { /* allocated via real_malloc, immortal for safety reasons */ struct src_loc { - pthread_mutex_t *mtx; size_t total; size_t freed_bytes; size_t allocations; @@ -309,7 +302,6 @@ again: if (!l) return l; memcpy(l, k, n); l->freed_bytes = 0; - l->mtx = mutex_assign(); l->age_total = 0; l->max_lifespan = 0; l->freed_bytes = 0; @@ -470,6 +462,13 @@ static void free_hdr_rcu(struct rcu_head *dead) real_free(h->real); } +static pthread_mutex_t *src_loc_mutex_lock(const struct src_loc *l) +{ + pthread_mutex_t *mtx = &mutexes[l->loc_hash & MUTEX_MASK].mtx; + CHECK(int, 0, pthread_mutex_lock(mtx)); + return mtx; +} + void free(void *p) { if (p) { @@ -485,11 +484,11 @@ void free(void *p) uatomic_inc(&l->frees); uatomic_add(&l->age_total, age); - CHECK(int, 0, pthread_mutex_lock(l->mtx)); + pthread_mutex_t *mtx = src_loc_mutex_lock(l); cds_list_del_rcu(&h->anode); if (age > l->max_lifespan) l->max_lifespan = age; - CHECK(int, 0, pthread_mutex_unlock(l->mtx)); + CHECK(int, 0, pthread_mutex_unlock(mtx)); call_rcu(&h->as.dead, free_hdr_rcu); } else { @@ -509,9 +508,9 @@ alloc_insert_rcu(struct src_loc *l, struct alloc_hdr *h, size_t size, h->as.live.loc = l; h->as.live.gen = generation; if (l) { - CHECK(int, 0, pthread_mutex_lock(l->mtx)); + pthread_mutex_t *mtx = src_loc_mutex_lock(l); cds_list_add_rcu(&h->anode, &l->allocs); - CHECK(int, 0, pthread_mutex_unlock(l->mtx)); + CHECK(int, 0, pthread_mutex_unlock(mtx)); } }