From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id D11D33858D1E for ; Wed, 29 Mar 2023 19:16:43 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D11D33858D1E Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1680117403; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to; bh=zsIHl/vwx/wWA8ixYf2HBSge8Ep7GCnHRoRP+oaadZI=; b=cjxytqRt6FK7FbB33z56pNInY5DQe741RwfFhNMGco/OctV2xlUrJ5oSnNlY/ZsW5NaOgg IhyiMMsJOwaWj9ImDZs5Ne06/FREUKBEqKyDukBSM7ubCofUDOHD0IlFb98AggVfmc3dr+ dbNFCeR/3k87EHFe1r190NA1DZVvTC4= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-567-R3EqlvoGPZiSqE-kRUv03A-1; Wed, 29 Mar 2023 15:16:39 -0400 X-MC-Unique: R3EqlvoGPZiSqE-kRUv03A-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 67B931C02D2D; Wed, 29 Mar 2023 19:16:39 +0000 (UTC) Received: from greed.delorie.com (unknown [10.22.8.104]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 41208202701E; Wed, 29 Mar 2023 19:16:39 +0000 (UTC) Received: from greed.delorie.com.redhat.com (localhost [127.0.0.1]) by greed.delorie.com (8.15.2/8.15.2) with ESMTP id 32TJGc1s1747296; Wed, 29 Mar 2023 15:16:38 -0400 From: DJ Delorie To: Adhemerval Zanella Cc: libc-alpha@sourceware.org, Wilco.Dijkstra@arm.com Subject: Re: [PATCH v3] malloc: Use C11 atomics on memusage In-Reply-To: <20230323161737.2592579-1-adhemerval.zanella@linaro.org> Date: Wed, 29 Mar 2023 15:16:38 -0400 Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain X-Spam-Status: No, score=-4.8 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE,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: LGTM Reviewed-by: DJ Delorie Adhemerval Zanella writes: > -#include > -#include > -#include > -#include > -#include > -#include > -#include > -#include > +#include > +#include > +#include Ok. I admit I'm also the kind of programmer that block-pastes a #include list from existing source ;-) > @@ -73,20 +68,20 @@ struct header > #define MAGIC 0xfeedbeaf > > > -static unsigned long int calls[idx_last]; > -static unsigned long int failed[idx_last]; > -static size_t total[idx_last]; > -static size_t grand_total; > -static unsigned long int histogram[65536 / 16]; > -static unsigned long int large; > -static unsigned long int calls_total; > -static unsigned long int inplace; > -static unsigned long int decreasing; > -static unsigned long int realloc_free; > -static unsigned long int inplace_mremap; > -static unsigned long int decreasing_mremap; > -static size_t current_heap; > -static size_t peak_use[3]; > +static _Atomic unsigned long int calls[idx_last]; > +static _Atomic unsigned long int failed[idx_last]; > +static _Atomic size_t total[idx_last]; > +static _Atomic size_t grand_total; > +static _Atomic unsigned long int histogram[65536 / 16]; > +static _Atomic unsigned long int large; > +static _Atomic unsigned long int calls_total; > +static _Atomic unsigned long int inplace; > +static _Atomic unsigned long int decreasing; > +static _Atomic unsigned long int realloc_free; > +static _Atomic unsigned long int inplace_mremap; > +static _Atomic unsigned long int decreasing_mremap; > +static _Atomic size_t current_heap; > +static _Atomic size_t peak_use[3]; > static __thread uintptr_t start_sp; Ok. > -static uint32_t buffer_cnt; > +static _Atomic uint32_t buffer_cnt; Ok. > +static inline void > +peak_atomic_max (_Atomic size_t *peak, size_t val) > +{ > + size_t v; > + do > + { > + v = atomic_load_explicit (peak, memory_order_relaxed); > + if (v >= val) > + break; > + } > + while (! atomic_compare_exchange_weak (peak, &v, val)); > +} Ok. > /* 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_explicit (¤t_heap, len - old_len, > + memory_order_relaxed) + len - old_len; > + peak_atomic_max (&peak_heap, heap); > Ok. > - catomic_max (&peak_stack, current_stack); > + peak_atomic_max (&peak_stack, current_stack); Ok. > /* 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); Ok. > /* 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_explicit (&buffer_cnt, 1, > + memory_order_relaxed); Ok. > - catomic_compare_and_exchange_val_acq (&buffer_cnt, reset, idx + 1); > + uint32_t expected = idx + 1; > + atomic_compare_exchange_weak (&buffer_cnt, &expected, reset); Ok. > /* Keep track of number of calls. */ > - catomic_increment (&calls[idx_malloc]); > + atomic_fetch_add_explicit (&calls[idx_malloc], 1, memory_order_relaxed); Ok. > /* Keep track of total memory consumption for `malloc'. */ > - catomic_add (&total[idx_malloc], len); > + atomic_fetch_add_explicit (&total[idx_malloc], len, memory_order_relaxed); Ok. > /* Keep track of total memory requirement. */ > - catomic_add (&grand_total, len); > + atomic_fetch_add_explicit (&grand_total, len, memory_order_relaxed); Ok. > /* Remember the size of the request. */ > if (len < 65536) > - catomic_increment (&histogram[len / 16]); > + atomic_fetch_add_explicit (&histogram[len / 16], 1, memory_order_relaxed); > else > - catomic_increment (&large); > + atomic_fetch_add_explicit (&large, 1, memory_order_relaxed); Ok. > /* Total number of calls of any of the functions. */ > - catomic_increment (&calls_total); > + atomic_fetch_add_explicit (&calls_total, 1, memory_order_relaxed); Ok. > /* Do the real work. */ > result = (struct header *) (*mallocp)(len + sizeof (struct header)); > if (result == NULL) > { > - catomic_increment (&failed[idx_malloc]); > + atomic_fetch_add_explicit (&failed[idx_malloc], 1, > + memory_order_relaxed); > return NULL; > } Ok. > /* Keep track of number of calls. */ > - catomic_increment (&calls[idx_realloc]); > + atomic_fetch_add_explicit (&calls[idx_realloc], 1, memory_order_relaxed); Ok. > /* Keep track of total memory consumption for `realloc'. */ > - catomic_add (&total[idx_realloc], len - old_len); > + atomic_fetch_add_explicit (&total[idx_realloc], len - old_len, > + memory_order_relaxed); Ok. > /* Keep track of total memory requirement. */ > - catomic_add (&grand_total, len - old_len); > + atomic_fetch_add_explicit (&grand_total, len - old_len, > + memory_order_relaxed); Ok. > /* Special case. */ > - catomic_increment (&realloc_free); > + atomic_fetch_add_explicit (&realloc_free, 1, memory_order_relaxed); > /* Keep track of total memory freed using `free'. */ > - catomic_add (&total[idx_free], real->length); > + atomic_fetch_add_explicit (&total[idx_free], real->length, > + memory_order_relaxed); Ok. > /* Remember the size of the request. */ > if (len < 65536) > - catomic_increment (&histogram[len / 16]); > + atomic_fetch_add_explicit (&histogram[len / 16], 1, memory_order_relaxed); > else > - catomic_increment (&large); > + atomic_fetch_add_explicit (&large, 1, memory_order_relaxed); Ok. > /* Total number of calls of any of the functions. */ > - catomic_increment (&calls_total); > + atomic_fetch_add_explicit (&calls_total, 1, memory_order_relaxed); Ok. > - catomic_increment (&failed[idx_realloc]); > + atomic_fetch_add_explicit (&failed[idx_realloc], 1, > + memory_order_relaxed); Ok. > if (real == result) > - catomic_increment (&inplace); > + atomic_fetch_add_explicit (&inplace, 1, memory_order_relaxed); Ok. > /* Was the buffer increased? */ > if (old_len > len) > - catomic_increment (&decreasing); > + atomic_fetch_add_explicit (&decreasing, 1, memory_order_relaxed); Ok. > /* Keep track of number of calls. */ > - catomic_increment (&calls[idx_calloc]); > + atomic_fetch_add_explicit (&calls[idx_calloc], 1, memory_order_relaxed); Ok. > /* Keep track of total memory consumption for `calloc'. */ > - catomic_add (&total[idx_calloc], size); > + atomic_fetch_add_explicit (&total[idx_calloc], size, memory_order_relaxed); Ok. > /* Keep track of total memory requirement. */ > - catomic_add (&grand_total, size); > + atomic_fetch_add_explicit (&grand_total, size, memory_order_relaxed); Ok. > /* Remember the size of the request. */ > if (size < 65536) > - catomic_increment (&histogram[size / 16]); > + atomic_fetch_add_explicit (&histogram[size / 16], 1, > + memory_order_relaxed); Ok. > else > - catomic_increment (&large); > + atomic_fetch_add_explicit (&large, 1, memory_order_relaxed); Ok. > - catomic_increment (&failed[idx_calloc]); > + atomic_fetch_add_explicit (&failed[idx_calloc], 1, > + memory_order_relaxed); Ok. > - catomic_increment (&calls[idx_free]); > + atomic_fetch_add_explicit (&calls[idx_free], 1, memory_order_relaxed); Ok. > /* Keep track of number of calls. */ > - catomic_increment (&calls[idx_free]); > + atomic_fetch_add_explicit (&calls[idx_free], 1, memory_order_relaxed); Ok. > /* Keep track of total memory freed using `free'. */ > - catomic_add (&total[idx_free], real->length); > + atomic_fetch_add_explicit (&total[idx_free], real->length, > + memory_order_relaxed); Ok. > /* Keep track of number of calls. */ > - catomic_increment (&calls[idx]); > + atomic_fetch_add_explicit (&calls[idx], 1, memory_order_relaxed); Ok. > /* Keep track of total memory consumption for `malloc'. */ > - catomic_add (&total[idx], len); > + atomic_fetch_add_explicit (&total[idx], len, memory_order_relaxed); Ok. > /* Keep track of total memory requirement. */ > - catomic_add (&grand_total, len); > + atomic_fetch_add_explicit (&grand_total, len, memory_order_relaxed); Ok. > /* Remember the size of the request. */ > if (len < 65536) > - catomic_increment (&histogram[len / 16]); > + atomic_fetch_add_explicit (&histogram[len / 16], 1, > + memory_order_relaxed); Ok. > else > - catomic_increment (&large); > + atomic_fetch_add_explicit (&large, 1, memory_order_relaxed); Ok. > /* Total number of calls of any of the functions. */ > - catomic_increment (&calls_total); > + atomic_fetch_add_explicit (&calls_total, 1, memory_order_relaxed); Ok. > /* Check for failures. */ > if (result == NULL) > - catomic_increment (&failed[idx]); > + atomic_fetch_add_explicit (&failed[idx], 1, memory_order_relaxed); Ok. > /* Keep track of number of calls. */ > - catomic_increment (&calls[idx]); > + atomic_fetch_add_explicit (&calls[idx], 1, memory_order_relaxed); Ok. > /* Keep track of total memory consumption for `malloc'. */ > - catomic_add (&total[idx], len); > + atomic_fetch_add_explicit (&total[idx], len, memory_order_relaxed); Ok. > /* Keep track of total memory requirement. */ > - catomic_add (&grand_total, len); > + atomic_fetch_add_explicit (&grand_total, len, memory_order_relaxed); Ok. > /* Remember the size of the request. */ > if (len < 65536) > - catomic_increment (&histogram[len / 16]); > + atomic_fetch_add_explicit (&histogram[len / 16], 1, > + memory_order_relaxed); Ok. > else > - catomic_increment (&large); > + atomic_fetch_add_explicit (&large, 1, memory_order_relaxed); Ok. > /* Total number of calls of any of the functions. */ > - catomic_increment (&calls_total); > + atomic_fetch_add_explicit (&calls_total, 1, memory_order_relaxed); Ok. > /* Check for failures. */ > if (result == NULL) > - catomic_increment (&failed[idx]); > + atomic_fetch_add_explicit (&failed[idx], 1, memory_order_relaxed); Ok. > /* Keep track of number of calls. */ > - catomic_increment (&calls[idx_mremap]); > + atomic_fetch_add_explicit (&calls[idx_mremap], 1, memory_order_relaxed); Ok. > /* Keep track of total memory consumption for `malloc'. */ > - catomic_add (&total[idx_mremap], len - old_len); > + atomic_fetch_add_explicit (&total[idx_mremap], len - old_len, > + memory_order_relaxed); Ok. > /* Keep track of total memory requirement. */ > - catomic_add (&grand_total, len - old_len); > + atomic_fetch_add_explicit (&grand_total, len - old_len, > + memory_order_relaxed); Ok. > if (len < 65536) > - catomic_increment (&histogram[len / 16]); > + atomic_fetch_add_explicit (&histogram[len / 16], 1, > + memory_order_relaxed); Ok. > else > - catomic_increment (&large); > + atomic_fetch_add_explicit (&large, 1, memory_order_relaxed); Ok. > /* Total number of calls of any of the functions. */ > - catomic_increment (&calls_total); > + atomic_fetch_add_explicit (&calls_total, 1, memory_order_relaxed); Ok. > /* Check for failures. */ > if (result == NULL) > - catomic_increment (&failed[idx_mremap]); > + atomic_fetch_add_explicit (&failed[idx_mremap], 1, > + memory_order_relaxed); Ok. > /* Record whether the reduction/increase happened in place. */ > if (start == result) > - catomic_increment (&inplace_mremap); > + atomic_fetch_add_explicit (&inplace_mremap, 1, > + memory_order_relaxed); Ok. > /* Was the buffer increased? */ > if (old_len > len) > - catomic_increment (&decreasing_mremap); > + atomic_fetch_add_explicit (&decreasing_mremap, 1, > + memory_order_relaxed); Ok. > /* Keep track of number of calls. */ > - catomic_increment (&calls[idx_munmap]); > + atomic_fetch_add_explicit (&calls[idx_munmap], 1, memory_order_relaxed); Ok. > /* Keep track of total memory freed using `free'. */ > - catomic_add (&total[idx_munmap], len); > + atomic_fetch_add_explicit (&total[idx_munmap], len, > + memory_order_relaxed); Ok. > - catomic_increment (&failed[idx_munmap]); > + atomic_fetch_add_explicit (&failed[idx_munmap], 1, > + memory_order_relaxed); Ok.