From patchwork Tue Sep 19 16:32:50 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wilco Dijkstra X-Patchwork-Id: 22960 Received: (qmail 98408 invoked by alias); 19 Sep 2017 16:32:58 -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 98399 invoked by uid 89); 19 Sep 2017 16:32:58 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-25.1 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_HELO_PASS, SPF_PASS autolearn=ham version=3.3.2 spammy=HAccept-Language:en-GB, HTo:U*dj, simplifying X-HELO: EUR03-AM5-obe.outbound.protection.outlook.com From: Wilco Dijkstra To: "libc-alpha@sourceware.org" , "dj@redhat.com" CC: nd Subject: [PATCH][malloc] Avoid atomics in have_fastchunks Date: Tue, 19 Sep 2017 16:32:50 +0000 Message-ID: authentication-results: spf=none (sender IP is ) smtp.mailfrom=Wilco.Dijkstra@arm.com; x-ms-publictraffictype: Email x-microsoft-exchange-diagnostics: 1; DB6PR0801MB1671; 6:VMghLalcTfWppioFSbHHZHLZHQ1lm4IS5V7DbHJvkttG7YQhjIgPx6IEk5QGkNFxAk0CFPkq4NUex9SQTRYbly09GOTInr6O37CHBHhnk91i1NwNtaqAkriU2rnj9DUcv4gTXmrbbT2XJaAaLjxWN2UjMT2KkqmgUKZKShNFLQE65e+SwmPbeFLj7qbYnXYVHx+qUvIxz9htNwmplUS/OFyvsbrnN35wjIM/clNZbrvLQxQ/bjUG0v/hXqNmH4plgBaTRp+k8Cc8fetVcr14ozM1Mb8A0L3Nh4zxJOa8zIoOtySaX6VFC3ytzW0VqWWFfrD9CxKwgnLq/QJOi+/2Iw==; 5:zgwonuzidSKHPPHkTTKQtbxlY7AdWCyfpwFKcV8xlCyHuF+txEk3s03EhMTmKVApNl4qgwDsxySre4XJ1wk1saSZfDilc1xEQNggsYWnorVYi9Te3yluAjEujb3SZ0J8h4Uw33QMr90ssfAduvJQjA==; 24:Xf8azgKZ2WufVp/B6dmeWJt8Qe9tlinM+XXhKRBToiAgAdnCvmYE6mHyaZKKVcwdjkJfJvf4aoSUiYT2xIPdrbdfNyCCt5lUhwf8/ZaQ6jE=; 7:cleHCrlrjl4Zgs/qB0toxTwNS0mSitWUhXFCdFu4ESqp7p4rnqRzAl7OE6Y27jMkflWlUwACjdQMmh6EAxAjbq8+YTMoJIHSt+yKh/OI8cwSzZZsUDxf1B+fNFU+7AWSrF1a1uhXhIUOv+F0UOFEcyIf1UzE3A+0Hj5Iv279rQtGhND1GjA5kykfSLYv9nfUvpZTP6+lZZTaZfl5Owjq2pxpVC4q/O899hup/oTNkA8= x-ms-exchange-antispam-srfa-diagnostics: SSOS; x-ms-office365-filtering-correlation-id: 62e501c6-e9b7-487b-a20e-08d4ff7c1213 x-ms-office365-filtering-ht: Tenant x-microsoft-antispam: UriScan:; BCL:0; PCL:0; RULEID:(300000500095)(300135000095)(300000501095)(300135300095)(300000502095)(300135100095)(22001)(2017030254152)(48565401081)(300000503095)(300135400095)(2017052603199)(201703131423075)(201703031133081)(201702281549075)(300000504095)(300135200095)(300000505095)(300135600095)(300000506095)(300135500095); SRVR:DB6PR0801MB1671; x-ms-traffictypediagnostic: DB6PR0801MB1671: nodisclaimer: True x-exchange-antispam-report-test: UriScan:(180628864354917); x-microsoft-antispam-prvs: x-exchange-antispam-report-cfa-test: BCL:0; PCL:0; RULEID:(100000700101)(100105000095)(100000701101)(100105300095)(100000702101)(100105100095)(6040450)(2401047)(5005006)(8121501046)(10201501046)(93006095)(93001095)(100000703101)(100105400095)(3002001)(6055026)(6041248)(20161123562025)(20161123558100)(20161123564025)(20161123555025)(20161123560025)(201703131423075)(201702281528075)(201703061421075)(201703061406153)(6072148)(201708071742011)(100000704101)(100105200095)(100000705101)(100105500095); SRVR:DB6PR0801MB1671; BCL:0; PCL:0; RULEID:(100000800101)(100110000095)(100000801101)(100110300095)(100000802101)(100110100095)(100000803101)(100110400095)(100000804101)(100110200095)(100000805101)(100110500095); SRVR:DB6PR0801MB1671; x-forefront-prvs: 04359FAD81 x-forefront-antispam-report: SFV:NSPM; SFS:(10009020)(6009001)(39860400002)(376002)(346002)(199003)(54534003)(377424004)(189002)(7736002)(189998001)(5660300001)(316002)(3846002)(102836003)(6116002)(33656002)(4326008)(74316002)(110136005)(72206003)(66066001)(3660700001)(106356001)(6436002)(7696004)(6506006)(478600001)(305945005)(2501003)(50986999)(5250100002)(68736007)(86362001)(9686003)(575784001)(3280700002)(54356999)(8936002)(14454004)(53936002)(101416001)(2900100001)(2906002)(105586002)(25786009)(55016002)(97736004)(99286003)(81156014)(8676002)(81166006); DIR:OUT; SFP:1101; SCL:1; SRVR:DB6PR0801MB1671; H:DB6PR0801MB2053.eurprd08.prod.outlook.com; FPR:; SPF:None; PTR:InfoNoRecords; A:1; MX:3; LANG:en; received-spf: None (protection.outlook.com: arm.com does not designate permitted sender hosts) spamdiagnosticoutput: 1:99 spamdiagnosticmetadata: NSPM MIME-Version: 1.0 X-OriginatorOrg: arm.com X-MS-Exchange-CrossTenant-originalarrivaltime: 19 Sep 2017 16:32:50.6792 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: f34e5979-57d9-4aaa-ad4d-b122a662184d X-MS-Exchange-Transport-CrossTenantHeadersStamped: DB6PR0801MB1671 Currently free typically uses 2 atomic operations per call. The have_fastchunks flag indicates whether there are recently freed blocks in the fastbins. This is purely an optimization to avoid calling malloc_consolidate too often and avoiding the overhead of walking all fast bins even if all are empty during a sequence of allocations. However using atomics to update the flag is completely unnecessary since it can be changed into a simple boolean. There is no change in multi-threaded behaviour given the flag is already approximate (it may be set when there are no blocks in any fast bins, or it may be clear when there are free blocks that could be consolidated). Performance of malloc/free improves by 27% on a simple benchmark on AArch64 (both single and multithreaded). The number of load/store exclusive instructions is reduced by 33%. Bench-malloc-thread speeds up by ~3% in all cases. Passes GLIBC tests. OK to commit? ChangeLog: 2017-09-19 Wilco Dijkstra * malloc/malloc.c (FASTCHUNKS_BIT): Remove. (have_fastchunks): Remove. (clear_fastchunks): Remove. (set_fastchunks): Remove. (malloc_state): Add have_fastchunks. (malloc_init_state): Use have_fastchunks. (do_check_malloc_state): Remove incorrect invariant checks. (_int_malloc): Use have_fastchunks. (_int_free): Likewise. (malloc_consolidate): Likewise. diff --git a/malloc/malloc.c b/malloc/malloc.c index 1c2a0b05b78c84cea60ee998108180d51b1f1ddf..61bea1eb321f4abbc6a843abc49b2e5b1fef9f9c 100644 --- a/malloc/malloc.c +++ b/malloc/malloc.c @@ -1604,27 +1604,6 @@ typedef struct malloc_chunk *mfastbinptr; #define FASTBIN_CONSOLIDATION_THRESHOLD (65536UL) /* - Since the lowest 2 bits in max_fast don't matter in size comparisons, - they are used as flags. - */ - -/* - FASTCHUNKS_BIT held in max_fast indicates that there are probably - some fastbin chunks. It is set true on entering a chunk into any - fastbin, and cleared only in malloc_consolidate. - - The truth value is inverted so that have_fastchunks will be true - upon startup (since statics are zero-filled), simplifying - initialization checks. - */ - -#define FASTCHUNKS_BIT (1U) - -#define have_fastchunks(M) (((M)->flags & FASTCHUNKS_BIT) == 0) -#define clear_fastchunks(M) catomic_or (&(M)->flags, FASTCHUNKS_BIT) -#define set_fastchunks(M) catomic_and (&(M)->flags, ~FASTCHUNKS_BIT) - -/* NONCONTIGUOUS_BIT indicates that MORECORE does not return contiguous regions. Otherwise, contiguity is exploited in merging together, when possible, results from consecutive MORECORE calls. @@ -1672,6 +1651,16 @@ get_max_fast (void) ----------- Internal state representation and initialization ----------- */ +/* + have_fastchunks indicates that there are probably some fastbin chunks. + It is set true on entering a chunk into any fastbin, and cleared early in + malloc_consolidate. The value is approximate since it may be set when there + are no fastbin chunks, or it may be clear even if there are fastbin chunks + available. Given its sole purpose is to reduce number of redundant calls to + malloc_consolidate, it does not affect correctness. + */ + + struct malloc_state { /* Serialize access. */ @@ -1680,6 +1669,9 @@ struct malloc_state /* Flags (formerly in max_fast). */ int flags; + /* Set if the fastbin chunks contain recently inserted free blocks. */ + bool have_fastchunks; + /* Fastbins */ mfastbinptr fastbinsY[NFASTBINS]; @@ -1823,7 +1815,7 @@ malloc_init_state (mstate av) set_noncontiguous (av); if (av == &main_arena) set_max_fast (DEFAULT_MXFAST); - av->flags |= FASTCHUNKS_BIT; + av->have_fastchunks = false; av->top = initial_top (av); } @@ -2179,11 +2171,6 @@ do_check_malloc_state (mstate av) } } - if (total != 0) - assert (have_fastchunks (av)); - else if (!have_fastchunks (av)) - assert (total == 0); - /* check normal bins */ for (i = 1; i < NBINS; ++i) { @@ -3650,7 +3637,7 @@ _int_malloc (mstate av, size_t bytes) else { idx = largebin_index (nb); - if (have_fastchunks (av)) + if (av->have_fastchunks) malloc_consolidate (av); } @@ -4058,7 +4045,7 @@ _int_malloc (mstate av, size_t bytes) /* When we are using atomic ops to free fast chunks we can get here for all block sizes. */ - else if (have_fastchunks (av)) + else if (av->have_fastchunks) { malloc_consolidate (av); /* restore original bin index */ @@ -4163,7 +4150,7 @@ _int_free (mstate av, mchunkptr p, int have_lock) free_perturb (chunk2mem(p), size - 2 * SIZE_SZ); - set_fastchunks(av); + av->have_fastchunks = true; unsigned int idx = fastbin_index(size); fb = &fastbin (av, idx); @@ -4291,7 +4278,7 @@ _int_free (mstate av, mchunkptr p, int have_lock) */ if ((unsigned long)(size) >= FASTBIN_CONSOLIDATION_THRESHOLD) { - if (have_fastchunks(av)) + if (av->have_fastchunks) malloc_consolidate(av); if (av == &main_arena) { @@ -4360,7 +4347,7 @@ static void malloc_consolidate(mstate av) */ if (get_max_fast () != 0) { - clear_fastchunks(av); + av->have_fastchunks = false; unsorted_bin = unsorted_chunks(av);