From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-oa1-x30.google.com (mail-oa1-x30.google.com [IPv6:2001:4860:4864:20::30]) by sourceware.org (Postfix) with ESMTPS id D17B83858D33 for ; Tue, 14 Feb 2023 18:47:44 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D17B83858D33 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org Received: by mail-oa1-x30.google.com with SMTP id 586e51a60fabf-1442977d77dso20224832fac.6 for ; Tue, 14 Feb 2023 10:47:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=content-transfer-encoding:in-reply-to:organization:references:to :from:content-language:subject:user-agent:mime-version:date :message-id:from:to:cc:subject:date:message-id:reply-to; bh=qvYhrIbNhAB7HjN3DAykK1U/S1F5ZxVxgkSO8HQsJvA=; b=LD689nhdCMEi8UhVfeK6Ig0UN2wmB8Rc8R4iMjJ0Bgb2S/Bvaj8vhH2U+r8UoKUmPe cygIpYkCzd2RjlBY9fFOUASSQ79SPNo13XTW38Jpv8s0kGLh2LFvCRBMQ87MwuaZoS9p oj3m96jom/mJtcn2tt1aH8mUhoSFTZs7UaYEwo3Y3in74DtcZKl4IvOQQBgtQ3Xs2F5r MsOLpaHj7XL/FmjZUiYZ3eckKpA5QjthaJ582DUcYaHwhQoXMP/673ZZwuOJ8W/9Hx5Q wu8PROH+OnlVET6POelZt8INguF+pA210K8G2ydM4+uC/oNzxop6HWA6HGdQsFky74OC Pquw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:organization:references:to :from:content-language:subject:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=qvYhrIbNhAB7HjN3DAykK1U/S1F5ZxVxgkSO8HQsJvA=; b=GO6Z88lsP0oOlZWZC4PjxU0Edg3k3RdlMjqpDPkoUjE2592LiqCVl3i/V2hE+w3u3Z jQ2QLGGY8PzKdyqVmTjlf7jpkp7IPcffbKvpwFZCfcRnumskdHQFxCyc1sHYRNNhvyI6 GF6j5k6MNTlfhuh5swxUa+Y2jVxFOY4So2xrh8zEkzKEnF4vDbc3i/YLc0u7gYOwYu7o MOvj5bjCND+M23N0pvmcWWuwuXFxgLDFlVzqjxCx323Lw8z+o+HaV0pM40q3h0US89Sj DAFbeJWLBGL8fAkNiX36URxGJtozsOsMtRG4uojML4DVwRXrq0OiXjp4j+DJ1lp+BVGw NZ7w== X-Gm-Message-State: AO0yUKUfBdrUKAdOxGxxiNqhw/6qgUZb84OXizg786+SHLMPapSe4kq+ OsWDYK5poLigCoLutQ62bPuc2pPGATSOZG9mBYM= X-Google-Smtp-Source: AK7set+N1ePo100W6aJyfhE9SFSNshQXvXCuSNnCyZzgvteh8wWeFtU7ZzVVAIcp+O/7RiMjSgU/3A== X-Received: by 2002:a05:6870:ac0c:b0:16d:ea08:cd96 with SMTP id kw12-20020a056870ac0c00b0016dea08cd96mr1615521oab.43.1676400463223; Tue, 14 Feb 2023 10:47:43 -0800 (PST) Received: from ?IPV6:2804:1b3:a7c3:3a5:44ad:5aad:f16a:f333? ([2804:1b3:a7c3:3a5:44ad:5aad:f16a:f333]) by smtp.gmail.com with ESMTPSA id xl6-20020a0568709f0600b00163263f84dasm6175737oab.12.2023.02.14.10.47.41 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 14 Feb 2023 10:47:42 -0800 (PST) Message-ID: <9febb5f0-2544-f8dd-50a5-df312572e89c@linaro.org> Date: Tue, 14 Feb 2023 15:47:39 -0300 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0) Gecko/20100101 Thunderbird/102.7.2 Subject: Re: [PATCH] malloc: Use C11 like atomics on memusage Content-Language: en-US From: Adhemerval Zanella Netto To: libc-alpha@sourceware.org References: <20220831181435.3875859-1-adhemerval.zanella@linaro.org> Organization: Linaro In-Reply-To: <20220831181435.3875859-1-adhemerval.zanella@linaro.org> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-12.0 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,NICE_REPLY_A,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: Ping. On 31/08/22 15:14, Adhemerval Zanella wrote: > Checked on x86_64-linux-gnu. > --- > malloc/memusage.c | 132 +++++++++++++++++++++++++--------------------- > 1 file changed, 73 insertions(+), 59 deletions(-) > > diff --git a/malloc/memusage.c b/malloc/memusage.c > index f30906dffb..ddc487422c 100644 > --- a/malloc/memusage.c > +++ b/malloc/memusage.c > @@ -134,6 +134,19 @@ gettime (struct entry *e) > #endif > } > > +static inline void > +peak_atomic_max (size_t *peak, size_t val) > +{ > + size_t v; > + do > + { > + v = atomic_load_relaxed (peak); > + if (v >= val) > + break; > + } > + while (! atomic_compare_exchange_weak_acquire (peak, &v, val)); > +} > + > /* Update the global data after a successful function call. */ > static void > update_data (struct header *result, size_t len, size_t old_len) > @@ -148,8 +161,8 @@ update_data (struct header *result, size_t len, size_t old_len) > > /* Compute current heap usage and compare it with the maximum value. */ > size_t heap > - = catomic_exchange_and_add (¤t_heap, len - old_len) + len - old_len; > - catomic_max (&peak_heap, heap); > + = atomic_fetch_add_acquire (¤t_heap, len - old_len) + len - old_len; > + peak_atomic_max (&peak_heap, heap); > > /* Compute current stack usage and compare it with the maximum > value. The base stack pointer might not be set if this is not > @@ -172,15 +185,15 @@ update_data (struct header *result, size_t len, size_t old_len) > start_sp = sp; > size_t current_stack = start_sp - sp; > #endif > - catomic_max (&peak_stack, current_stack); > + peak_atomic_max (&peak_stack, current_stack); > > /* Add up heap and stack usage and compare it with the maximum value. */ > - catomic_max (&peak_total, heap + current_stack); > + peak_atomic_max (&peak_total, heap + current_stack); > > /* Store the value only if we are writing to a file. */ > if (fd != -1) > { > - uint32_t idx = catomic_exchange_and_add (&buffer_cnt, 1); > + uint32_t idx = atomic_fetch_add_acquire (&buffer_cnt, 1); > if (idx + 1 >= 2 * buffer_size) > { > /* We try to reset the counter to the correct range. If > @@ -188,7 +201,8 @@ update_data (struct header *result, size_t len, size_t old_len) > counter it does not matter since that thread will take > care of the correction. */ > uint32_t reset = (idx + 1) % (2 * buffer_size); > - catomic_compare_and_exchange_val_acq (&buffer_cnt, reset, idx + 1); > + uint32_t expected = idx + 1; > + atomic_compare_exchange_weak_acquire (&buffer_cnt, &expected, reset); > if (idx >= 2 * buffer_size) > idx = reset - 1; > } > @@ -362,24 +376,24 @@ malloc (size_t len) > return (*mallocp)(len); > > /* Keep track of number of calls. */ > - catomic_increment (&calls[idx_malloc]); > + atomic_fetch_add_acquire (&calls[idx_malloc], 1); > /* Keep track of total memory consumption for `malloc'. */ > - catomic_add (&total[idx_malloc], len); > + atomic_fetch_add_acquire (&total[idx_malloc], len); > /* Keep track of total memory requirement. */ > - catomic_add (&grand_total, len); > + atomic_fetch_add_acquire (&grand_total, len); > /* Remember the size of the request. */ > if (len < 65536) > - catomic_increment (&histogram[len / 16]); > + atomic_fetch_add_acquire (&histogram[len / 16], 1); > else > - catomic_increment (&large); > + atomic_fetch_add_acquire (&large, 1); > /* Total number of calls of any of the functions. */ > - catomic_increment (&calls_total); > + atomic_fetch_add_acquire (&calls_total, 1); > > /* Do the real work. */ > result = (struct header *) (*mallocp)(len + sizeof (struct header)); > if (result == NULL) > { > - catomic_increment (&failed[idx_malloc]); > + atomic_fetch_add_acquire (&failed[idx_malloc], 1); > return NULL; > } > > @@ -430,21 +444,21 @@ realloc (void *old, size_t len) > } > > /* Keep track of number of calls. */ > - catomic_increment (&calls[idx_realloc]); > + atomic_fetch_add_acquire (&calls[idx_realloc], 1); > if (len > old_len) > { > /* Keep track of total memory consumption for `realloc'. */ > - catomic_add (&total[idx_realloc], len - old_len); > + atomic_fetch_add_acquire (&total[idx_realloc], len - old_len); > /* Keep track of total memory requirement. */ > - catomic_add (&grand_total, len - old_len); > + atomic_fetch_add_acquire (&grand_total, len - old_len); > } > > if (len == 0 && old != NULL) > { > /* Special case. */ > - catomic_increment (&realloc_free); > + atomic_fetch_add_acquire (&realloc_free, 1); > /* Keep track of total memory freed using `free'. */ > - catomic_add (&total[idx_free], real->length); > + atomic_fetch_add_acquire (&total[idx_free], real->length); > > /* Update the allocation data and write out the records if necessary. */ > update_data (NULL, 0, old_len); > @@ -457,26 +471,26 @@ realloc (void *old, size_t len) > > /* Remember the size of the request. */ > if (len < 65536) > - catomic_increment (&histogram[len / 16]); > + atomic_fetch_add_acquire (&histogram[len / 16], 1); > else > - catomic_increment (&large); > + atomic_fetch_add_acquire (&large, 1); > /* Total number of calls of any of the functions. */ > - catomic_increment (&calls_total); > + atomic_fetch_add_acquire (&calls_total, 1); > > /* Do the real work. */ > result = (struct header *) (*reallocp)(real, len + sizeof (struct header)); > if (result == NULL) > { > - catomic_increment (&failed[idx_realloc]); > + atomic_fetch_add_acquire (&failed[idx_realloc], 1); > return NULL; > } > > /* Record whether the reduction/increase happened in place. */ > if (real == result) > - catomic_increment (&inplace); > + atomic_fetch_add_acquire (&inplace, 1); > /* Was the buffer increased? */ > if (old_len > len) > - catomic_increment (&decreasing); > + atomic_fetch_add_acquire (&decreasing, 1); > > /* Update the allocation data and write out the records if necessary. */ > update_data (result, len, old_len); > @@ -508,16 +522,16 @@ calloc (size_t n, size_t len) > return (*callocp)(n, len); > > /* Keep track of number of calls. */ > - catomic_increment (&calls[idx_calloc]); > + atomic_fetch_add_acquire (&calls[idx_calloc], 1); > /* Keep track of total memory consumption for `calloc'. */ > - catomic_add (&total[idx_calloc], size); > + atomic_fetch_add_acquire (&total[idx_calloc], size); > /* Keep track of total memory requirement. */ > - catomic_add (&grand_total, size); > + atomic_fetch_add_acquire (&grand_total, size); > /* Remember the size of the request. */ > if (size < 65536) > - catomic_increment (&histogram[size / 16]); > + atomic_fetch_add_acquire (&histogram[size / 16], 1); > else > - catomic_increment (&large); > + atomic_fetch_add_acquire (&large, 1); > /* Total number of calls of any of the functions. */ > ++calls_total; > > @@ -525,7 +539,7 @@ calloc (size_t n, size_t len) > result = (struct header *) (*mallocp)(size + sizeof (struct header)); > if (result == NULL) > { > - catomic_increment (&failed[idx_calloc]); > + atomic_fetch_add_acquire (&failed[idx_calloc], 1); > return NULL; > } > > @@ -563,7 +577,7 @@ free (void *ptr) > /* `free (NULL)' has no effect. */ > if (ptr == NULL) > { > - catomic_increment (&calls[idx_free]); > + atomic_fetch_add_acquire (&calls[idx_free], 1); > return; > } > > @@ -577,9 +591,9 @@ free (void *ptr) > } > > /* Keep track of number of calls. */ > - catomic_increment (&calls[idx_free]); > + atomic_fetch_add_acquire (&calls[idx_free], 1); > /* Keep track of total memory freed using `free'. */ > - catomic_add (&total[idx_free], real->length); > + atomic_fetch_add_acquire (&total[idx_free], real->length); > > /* Update the allocation data and write out the records if necessary. */ > update_data (NULL, 0, real->length); > @@ -614,22 +628,22 @@ mmap (void *start, size_t len, int prot, int flags, int fd, off_t offset) > ? idx_mmap_a : prot & PROT_WRITE ? idx_mmap_w : idx_mmap_r); > > /* Keep track of number of calls. */ > - catomic_increment (&calls[idx]); > + atomic_fetch_add_acquire (&calls[idx], 1); > /* Keep track of total memory consumption for `malloc'. */ > - catomic_add (&total[idx], len); > + atomic_fetch_add_acquire (&total[idx], len); > /* Keep track of total memory requirement. */ > - catomic_add (&grand_total, len); > + atomic_fetch_add_acquire (&grand_total, len); > /* Remember the size of the request. */ > if (len < 65536) > - catomic_increment (&histogram[len / 16]); > + atomic_fetch_add_acquire (&histogram[len / 16], 1); > else > - catomic_increment (&large); > + atomic_fetch_add_acquire (&large, 1); > /* Total number of calls of any of the functions. */ > - catomic_increment (&calls_total); > + atomic_fetch_add_acquire (&calls_total, 1); > > /* Check for failures. */ > if (result == NULL) > - catomic_increment (&failed[idx]); > + atomic_fetch_add_acquire (&failed[idx], 1); > else if (idx == idx_mmap_w) > /* Update the allocation data and write out the records if > necessary. Note the first parameter is NULL which means > @@ -667,22 +681,22 @@ mmap64 (void *start, size_t len, int prot, int flags, int fd, off64_t offset) > ? idx_mmap_a : prot & PROT_WRITE ? idx_mmap_w : idx_mmap_r); > > /* Keep track of number of calls. */ > - catomic_increment (&calls[idx]); > + atomic_fetch_add_acquire (&calls[idx], 1); > /* Keep track of total memory consumption for `malloc'. */ > - catomic_add (&total[idx], len); > + atomic_fetch_add_acquire (&total[idx], len); > /* Keep track of total memory requirement. */ > - catomic_add (&grand_total, len); > + atomic_fetch_add_acquire (&grand_total, len); > /* Remember the size of the request. */ > if (len < 65536) > - catomic_increment (&histogram[len / 16]); > + atomic_fetch_add_acquire (&histogram[len / 16], 1); > else > - catomic_increment (&large); > + atomic_fetch_add_acquire (&large, 1); > /* Total number of calls of any of the functions. */ > - catomic_increment (&calls_total); > + atomic_fetch_add_acquire (&calls_total, 1); > > /* Check for failures. */ > if (result == NULL) > - catomic_increment (&failed[idx]); > + atomic_fetch_add_acquire (&failed[idx], 1); > else if (idx == idx_mmap_w) > /* Update the allocation data and write out the records if > necessary. Note the first parameter is NULL which means > @@ -722,33 +736,33 @@ mremap (void *start, size_t old_len, size_t len, int flags, ...) > if (!not_me && trace_mmap) > { > /* Keep track of number of calls. */ > - catomic_increment (&calls[idx_mremap]); > + atomic_fetch_add_acquire (&calls[idx_mremap], 1); > if (len > old_len) > { > /* Keep track of total memory consumption for `malloc'. */ > - catomic_add (&total[idx_mremap], len - old_len); > + atomic_fetch_add_acquire (&total[idx_mremap], len - old_len); > /* Keep track of total memory requirement. */ > - catomic_add (&grand_total, len - old_len); > + atomic_fetch_add_acquire (&grand_total, len - old_len); > } > /* Remember the size of the request. */ > if (len < 65536) > - catomic_increment (&histogram[len / 16]); > + atomic_fetch_add_acquire (&histogram[len / 16], 1); > else > - catomic_increment (&large); > + atomic_fetch_add_acquire (&large, 1); > /* Total number of calls of any of the functions. */ > - catomic_increment (&calls_total); > + atomic_fetch_add_acquire (&calls_total, 1); > > /* Check for failures. */ > if (result == NULL) > - catomic_increment (&failed[idx_mremap]); > + atomic_fetch_add_acquire (&failed[idx_mremap], 1); > else > { > /* Record whether the reduction/increase happened in place. */ > if (start == result) > - catomic_increment (&inplace_mremap); > + atomic_fetch_add_acquire (&inplace_mremap, 1); > /* Was the buffer increased? */ > if (old_len > len) > - catomic_increment (&decreasing_mremap); > + atomic_fetch_add_acquire (&decreasing_mremap, 1); > > /* Update the allocation data and write out the records if > necessary. Note the first parameter is NULL which means > @@ -783,19 +797,19 @@ munmap (void *start, size_t len) > if (!not_me && trace_mmap) > { > /* Keep track of number of calls. */ > - catomic_increment (&calls[idx_munmap]); > + atomic_fetch_add_acquire (&calls[idx_munmap], 1); > > if (__glibc_likely (result == 0)) > { > /* Keep track of total memory freed using `free'. */ > - catomic_add (&total[idx_munmap], len); > + atomic_fetch_add_acquire (&total[idx_munmap], len); > > /* Update the allocation data and write out the records if > necessary. */ > update_data (NULL, 0, len); > } > else > - catomic_increment (&failed[idx_munmap]); > + atomic_fetch_add_acquire (&failed[idx_munmap], 1); > } > > return result;