From patchwork Tue Oct 4 20:04:49 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: DJ Delorie X-Patchwork-Id: 16255 Received: (qmail 77824 invoked by alias); 4 Oct 2016 20:04:53 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 77808 invoked by uid 89); 4 Oct 2016 20:04:53 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-4.5 required=5.0 tests=BAYES_00, RP_MATCHES_RCVD, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=counters, Care, Hx-languages-length:2240, arena X-HELO: mx1.redhat.com From: DJ Delorie To: "Carlos O'Donell" Cc: libc-alpha@sourceware.org Subject: Re: [PATCH] [BZ 20628] make mallinfo saturating References: Date: Tue, 04 Oct 2016 16:04:49 -0400 In-Reply-To: (Carlos O'Donell's message of "Wed, 28 Sep 2016 16:26:13 -0400") Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) MIME-Version: 1.0 "Carlos O'Donell" writes: > DJ, Care to make a version 2 of the patch? Version 2. Changed some of the counters to larger types and tested those too, which took about 130Gb of virtual memory and lots of waiting... [BZ #20628] * malloc/malloc.c (int_mallinfo): Use saturating add instead of overflow to collect statistics into a fixed "int" container. diff --git a/malloc/malloc.c b/malloc/malloc.c index ef04360..e3f4693 100644 --- a/malloc/malloc.c +++ b/malloc/malloc.c @@ -224,6 +224,7 @@ #include #include /* needed for malloc_stats */ #include +#include #include @@ -4594,8 +4595,8 @@ int_mallinfo (mstate av, struct mallinfo *m) mchunkptr p; INTERNAL_SIZE_T avail; INTERNAL_SIZE_T fastavail; - int nblocks; - int nfastblocks; + INTERNAL_SIZE_T nblocks; + INTERNAL_SIZE_T nfastblocks; /* Ensure initialization */ if (av->top == 0) @@ -4633,18 +4634,29 @@ int_mallinfo (mstate av, struct mallinfo *m) } } - m->smblks += nfastblocks; - m->ordblks += nblocks; - m->fordblks += avail; - m->uordblks += av->system_mem - avail; - m->arena += av->system_mem; - m->fsmblks += fastavail; +/* Saturated add - add ADD to SUM. If the result exceeds the range of + "int", set SUM to UINT_MAX instead ((int)-1). Assumes ADD and SUM + are positive. The published ABI prevents us from bumping "int" to + a larger type. */ +#define SAT_ADD(SUM, ADD) \ + ({ INTERNAL_SIZE_T tmp = (INTERNAL_SIZE_T)(SUM) + (INTERNAL_SIZE_T)(ADD); SUM = (tmp > UINT_MAX) ? -1 : tmp; }) + +/* Likewise, but assign ADD to SUM. */ +#define SAT_SET(SUM, ADD) \ + ({ SUM = ((INTERNAL_SIZE_T)(ADD) > UINT_MAX) ? -1 : (ADD); }) + + SAT_ADD (m->smblks, nfastblocks); + SAT_ADD (m->ordblks, nblocks); + SAT_ADD (m->fordblks, avail); + SAT_ADD (m->uordblks, av->system_mem - avail); + SAT_ADD (m->arena, av->system_mem); + SAT_ADD (m->fsmblks, fastavail); if (av == &main_arena) { - m->hblks = mp_.n_mmaps; - m->hblkhd = mp_.mmapped_mem; - m->usmblks = 0; - m->keepcost = chunksize (av->top); + SAT_SET (m->hblks, mp_.n_mmaps); + SAT_SET (m->hblkhd, mp_.mmapped_mem); + SAT_SET (m->usmblks, 0); + SAT_SET (m->keepcost, chunksize (av->top)); } }