From patchwork Wed Dec 13 14:08:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Wilco Dijkstra X-Patchwork-Id: 82060 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C3DEC3857C72 for ; Wed, 13 Dec 2023 14:09:17 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from EUR02-VI1-obe.outbound.protection.outlook.com (mail-vi1eur02on2054.outbound.protection.outlook.com [40.107.241.54]) by sourceware.org (Postfix) with ESMTPS id 9B9693858C2F for ; Wed, 13 Dec 2023 14:08:53 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 9B9693858C2F Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=arm.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 9B9693858C2F Authentication-Results: server2.sourceware.org; arc=pass smtp.remote-ip=40.107.241.54 ARC-Seal: i=3; a=rsa-sha256; d=sourceware.org; s=key; t=1702476544; cv=pass; b=mwqQD15Z7FiYhnGwaoAi+dzWJuAU3MRtDc8tXNIJ2IdwiXL9RhXQo3t/jc/FQITjrUJSADpUHgso6UUGUsaFD9EOsoanyIeEW/kKaCbusCFMavjD0/8H6BsxRrLLVNXJcMKFwmY2csZ4h4TfkWQXhItnTvjFy8uc1qFK8rUCiUg= ARC-Message-Signature: i=3; a=rsa-sha256; d=sourceware.org; s=key; t=1702476544; c=relaxed/simple; bh=icpgXOYmCTgytcUVx7ePh3FLy7uSgM5uizRgCXv23QY=; h=DKIM-Signature:DKIM-Signature:From:To:Subject:Date:Message-ID: MIME-Version; b=M5xVRsweBjFAPQnDpZcgJprwHmBzF0qXMEltYfpkjHGtFnCMNFMHRKRuhJl/F9O4ff7DzhX2rmnoCd8s4VG382uhWuiARJgbbJ4qwWkLN04TfYAiEm1OI0lRVUfQSzpAaFOBGN24TPOhChxA7FAlTqpNMYfxE782IShANTT2glY= ARC-Authentication-Results: i=3; server2.sourceware.org ARC-Seal: i=2; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=pass; b=F2oh4EAnL1phNFSZxxyZrsTfBQsYGOt4vqto+QERZ1IJSTB4Z2bfCQNtlVS5yiOKPCmfwrZDWYxrwhaG8YrGs400PFUmh4lurvz9i9fofAiS+KSqNm5gh8984VQgK8ZBNeVtqH2uH14QrXhGR76nc7GFupVLgYw/cHgqns+adi89fEc84KrYharHx05i25JcpEpkJuhtChZDYulsIRzrtIuZLufsbEfj/uqrHfnscODrHXXwPF7/fAD0JJgtob+WSdjYdxs2KScpDijQ1ecKkoA3NaUhFOhvMQutiV/wJb8pG9IDf7UOSJhQcyD/UWMXciTNedb4/sZgwdtyyHk6SQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=icpgXOYmCTgytcUVx7ePh3FLy7uSgM5uizRgCXv23QY=; b=R8vT3+0PxW3OgdB619lhcHFlIYF4JFF7NPy3TPXY6imoKJKwwHwGdtc6AP1tq/8rgXxZTLmyzBJo+t9LLigh4P+ZamwTxIymN2xJROd6iw/wwfEhOvVnEd176YY5tRx1o3994vZqdDhvKDlylKeg3XbvvwoAyBITvF3Cg2lrulmM0jMttmSRARb4Tdd03sWx2U0QzYZd3Ea9g1Mn5XWeqPRQAw7O9dTf1G8AEPJbE6wSXdjXwzrim3viN80DlIXUkZoISJv7LQ4dpC4Bj5D/9Yx+2uZRDNLF+cv/c+irEDLeeomshwGjhlJqX6ap3buo2tWGp3toBOvsnCeg/qe0Kw== ARC-Authentication-Results: i=2; mx.microsoft.com 1; spf=pass (sender ip is 63.35.35.123) smtp.rcpttodomain=sourceware.org smtp.mailfrom=arm.com; dmarc=pass (p=none sp=none pct=100) action=none header.from=arm.com; dkim=pass (signature was verified) header.d=armh.onmicrosoft.com; arc=pass (0 oda=1 ltdi=1 spf=[1,1,smtp.mailfrom=arm.com] dkim=[1,1,header.d=arm.com] dmarc=[1,1,header.from=arm.com]) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=armh.onmicrosoft.com; s=selector2-armh-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=icpgXOYmCTgytcUVx7ePh3FLy7uSgM5uizRgCXv23QY=; b=Uj710irO0aZkOXN0cFhtzK5705b0qXh0YySZW6INxCdHtEthbol+nNty6B8KvDpaBGjHj3HFRKJuiDBsU0uL05j5QqGDlXfhtUBU6mhfm2qu/oCW9f3P84fs1Gxlztf89ROst8eREfSZ+m2nxL3/nQn1i5+KuYi7qGgZwbk7slY= Received: from DU7PR01CA0047.eurprd01.prod.exchangelabs.com (2603:10a6:10:50e::24) by AS2PR08MB10226.eurprd08.prod.outlook.com (2603:10a6:20b:62f::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7091.26; Wed, 13 Dec 2023 14:08:50 +0000 Received: from DB5PEPF00014B99.eurprd02.prod.outlook.com (2603:10a6:10:50e:cafe::5b) by DU7PR01CA0047.outlook.office365.com (2603:10a6:10:50e::24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7091.26 via Frontend Transport; Wed, 13 Dec 2023 14:08:50 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 63.35.35.123) smtp.mailfrom=arm.com; dkim=pass (signature was verified) header.d=armh.onmicrosoft.com;dmarc=pass action=none header.from=arm.com; Received-SPF: Pass (protection.outlook.com: domain of arm.com designates 63.35.35.123 as permitted sender) receiver=protection.outlook.com; client-ip=63.35.35.123; helo=64aa7808-outbound-1.mta.getcheckrecipient.com; pr=C Received: from 64aa7808-outbound-1.mta.getcheckrecipient.com (63.35.35.123) by DB5PEPF00014B99.mail.protection.outlook.com (10.167.8.166) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7091.26 via Frontend Transport; Wed, 13 Dec 2023 14:08:49 +0000 Received: ("Tessian outbound 26ee1d40577c:v228"); Wed, 13 Dec 2023 14:08:49 +0000 X-CheckRecipientChecked: true X-CR-MTA-CID: 742f66f3b2f391d5 X-CR-MTA-TID: 64aa7808 Received: from 507788ded535.1 by 64aa7808-outbound-1.mta.getcheckrecipient.com id 7C40D386-3923-41DE-B1E6-0C9DDF8AEFAB.1; Wed, 13 Dec 2023 14:08:43 +0000 Received: from EUR04-DB3-obe.outbound.protection.outlook.com by 64aa7808-outbound-1.mta.getcheckrecipient.com with ESMTPS id 507788ded535.1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384); Wed, 13 Dec 2023 14:08:43 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Z0P/9jB+wXlMbfjMCfJi+e0cOtQSlgwzrpAU70lmKor7BMF5iM55xG+caIqt3GsI/bz9zVMtotA7hgnXWFcVU65VSkyxtXoBDpwyRu4FG86h7/fKD38CxMdKD0PMJBhxCK867zLP6h+LEbFUoPY+8AwZ0cy1zsDk3DiCOuKSAgT0CTjH3IMU54Nu885Q0ojivx8K9kmJtttECRm/T090D8IL0AwL+NiI+VZaFgTgzbmVgOhi/Ip+NrlObKzWrOIKAwK/Y3SPFIDnBPziCyOfBSsTXM2OE7fEZyU20Vt2DUhgmoeh6o4CHf4BGnVlqnhSPOw0MKidKaTPxRdRP3i6Rw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=icpgXOYmCTgytcUVx7ePh3FLy7uSgM5uizRgCXv23QY=; b=ZWzTOKxBBACj3C6hqJV189SNq9QR8tnDzEeA55Qv4oMfD4vFobmbdo+TIHt4QhD1s7wxdTeE8SMDICrxMnqqCesxzILYnfUucRKNpjdr4wMoSR3yWOyFrizPtT07OA9ZAHKefPfSkr8DMEBvaGayvfg8Kxa/dnC9DAogTlVlcF/cKx4nYSB1WKRHfBEgcCibPcVf2KwEQggqfaRFu0OdWLNThJKaIja6f3grM7WDsihXzbrDM74xIodd4nayv2Hcf/aEYSP8wg4i7ZzeVTemHG7RoCi8CQubPIR03dRdGOiCKpSpkP4Kcmqn3O1jWejlKctkh741PL04cPUPuY3Sag== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=arm.com; dmarc=pass action=none header.from=arm.com; dkim=pass header.d=arm.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=armh.onmicrosoft.com; s=selector2-armh-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=icpgXOYmCTgytcUVx7ePh3FLy7uSgM5uizRgCXv23QY=; b=Uj710irO0aZkOXN0cFhtzK5705b0qXh0YySZW6INxCdHtEthbol+nNty6B8KvDpaBGjHj3HFRKJuiDBsU0uL05j5QqGDlXfhtUBU6mhfm2qu/oCW9f3P84fs1Gxlztf89ROst8eREfSZ+m2nxL3/nQn1i5+KuYi7qGgZwbk7slY= Received: from PAWPR08MB8982.eurprd08.prod.outlook.com (2603:10a6:102:33f::20) by VE1PR08MB5792.eurprd08.prod.outlook.com (2603:10a6:800:1a6::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7091.26; Wed, 13 Dec 2023 14:08:36 +0000 Received: from PAWPR08MB8982.eurprd08.prod.outlook.com ([fe80::2ed5:dc23:2624:df0a]) by PAWPR08MB8982.eurprd08.prod.outlook.com ([fe80::2ed5:dc23:2624:df0a%7]) with mapi id 15.20.7091.022; Wed, 13 Dec 2023 14:08:36 +0000 From: Wilco Dijkstra To: 'GNU C Library' Subject: [PATCH v3 1/2] Add random benchmark Thread-Topic: [PATCH v3 1/2] Add random benchmark Thread-Index: AQHaLc3d7+GX4rulT0mwTpeK+Kmy+A== Date: Wed, 13 Dec 2023 14:08:36 +0000 Message-ID: References: In-Reply-To: Accept-Language: en-GB, en-US Content-Language: en-GB X-MS-Has-Attach: X-MS-TNEF-Correlator: msip_labels: Authentication-Results-Original: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=arm.com; x-ms-traffictypediagnostic: PAWPR08MB8982:EE_|VE1PR08MB5792:EE_|DB5PEPF00014B99:EE_|AS2PR08MB10226:EE_ X-MS-Office365-Filtering-Correlation-Id: c6236f50-d77a-4894-e68f-08dbfbe507b5 x-checkrecipientrouted: true nodisclaimer: true X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam-Untrusted: BCL:0; X-Microsoft-Antispam-Message-Info-Original: B9WKr5VeWGqZvvS1s6fC7OINGikpCurKx3SBfoXVIEAJM9H6kClkkuvP4b3Dvo/zWfMvsL/zxjwygdsp1pVq4nNZrI+UfmDHMloJ8xwzzGN2Z6uizFsam/B0xerv0i1ZCKSGRjKZ0Hlb1HSbuHqwFxWZ8MkGuscCKJaW+Xy3iYJ8P0LngDqkpjnwutc81t9izrRL/fhWKTGJxGJuNrmrk3shRwMuaZLQ++lGqOrVEJ1F40mVeuX0IB8pPYfFENFhXXwYVo78FGY8Fq3J7+EwaUymVJ1ZINnWBE6nbZ5grt2USXnSl2uU5J65Y0m/HtxGyETIT0o6DFbm8Xuox3prn08kD72vKqZFQ/h6Oy2nfd/FpzYFEEb9R0ErD5CWInbEJwTTI5tAQd1JEa3sK0MrBvMaVd2W0wvhptwO0xwy6I0lclWCCzHS5mIGDT7jgtDW35l4bL5VT19/bqSRMylMCXyhHFSl5gUVsQNYBFiyWuWYMLqaNxYreewFlgzzsj0vlS6wNCEterApooFlLTRP2npNciBeJOpX6CPR394vufhsmiq7lBcNQg6hz8qdCrJRF0qbgrQdqMQ74jlFUFDMor8mUHUpgD2RJ6e84kCLdmOB0MLSq4bt1tP+q9LBUSL/ X-Forefront-Antispam-Report-Untrusted: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:PAWPR08MB8982.eurprd08.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230031)(39850400004)(396003)(376002)(346002)(366004)(136003)(230922051799003)(451199024)(1800799012)(64100799003)(186009)(26005)(83380400001)(6506007)(9686003)(122000001)(7696005)(8676002)(38100700002)(5660300002)(8936002)(52536014)(41300700001)(2906002)(316002)(71200400001)(478600001)(66476007)(91956017)(64756008)(6916009)(66946007)(76116006)(66446008)(66556008)(33656002)(86362001)(38070700009)(55016003)(2004002); DIR:OUT; SFP:1101; MIME-Version: 1.0 X-MS-Exchange-Transport-CrossTenantHeadersStamped: VE1PR08MB5792 Original-Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=arm.com; X-EOPAttributedMessage: 0 X-MS-Exchange-Transport-CrossTenantHeadersStripped: DB5PEPF00014B99.eurprd02.prod.outlook.com X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id-Prvs: 59318464-2f87-449d-5696-08dbfbe50003 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: ZzMRW8GTAdbtKCUaN6qafj1jYHw+ZTA7jy3sf1eGXBHDhHN0rAq/hUy+qUA80l6Redte3MoUtQbQF/Bm4gK3EvNUpk3adBpF3kKzW+3PTmSjsZ5Sx2Dmq3bfm5Yh/lH8xyO8Me3y8Wp1+4ch3iDmNi3mUyywhswZJ//uNsMCH3eZNbSHHwDRBuffCS3lz/1jwwmXW9IzBtIZyWgdyMFltVg/9435+JuBQ37ejY2xknr2LRnHN+9Ek+ko/gSOMzQz9ZWD2JSHokQAoTpbXcDe5qENIRMp5OSWuTp/fZoxBjAV4JlHJn1KTnWAiwIfDJR4Xc0J8ZGx0RFhs74JUVMw5QhKUJBMMi03x8ZoFowyiJi81rRJdlNhsXHb40v/T2USkWVQ+Yzy7A3yHpbEgZthXvwvD7bUHu2TpHGAhB2c2QDaXeF6gF0GJx4awmyFfAqxGkdDAecmb8THq0YoxVn/g/dCgidzMOc0ccgiB2XW3UpjZjJsfQFYGQe9UirL1JiXnsXtbmldgkekSD2oI2zX2MW6g+h8VrpLyRTNwHBEj6pncbyS392hKLcaYrnajHIQnTawlGgr5EFU+nRrBW97lIctk4I3NjRWBd0AiYgoQz+8ShrihBkU8/w2Gp+HsaffWx9sFUzaHJH9yO83TLEqcNcG2aBb5ziXwi1BhO496uC9C3hvxsXPBshHApe7kp5wWtFYDAV3VrHKRpm0JJPuoLJ8J88bhQq5Dx7lbEt0tWdqDMreIJNG1xxExI5wm6r3 X-Forefront-Antispam-Report: CIP:63.35.35.123; CTRY:IE; LANG:en; SCL:1; SRV:; IPV:CAL; SFV:NSPM; H:64aa7808-outbound-1.mta.getcheckrecipient.com; PTR:ec2-63-35-35-123.eu-west-1.compute.amazonaws.com; CAT:NONE; SFS:(13230031)(4636009)(39860400002)(346002)(136003)(396003)(376002)(230922051799003)(64100799003)(1800799012)(186009)(451199024)(82310400011)(40470700004)(36840700001)(46966006)(40480700001)(36860700001)(52536014)(5660300002)(7696005)(40460700003)(41300700001)(47076005)(8936002)(81166007)(82740400003)(55016003)(356005)(2906002)(33656002)(86362001)(83380400001)(336012)(8676002)(70586007)(70206006)(6916009)(26005)(9686003)(478600001)(6506007)(316002)(2004002); DIR:OUT; SFP:1101; X-OriginatorOrg: arm.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 13 Dec 2023 14:08:49.8345 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: c6236f50-d77a-4894-e68f-08dbfbe507b5 X-MS-Exchange-CrossTenant-Id: f34e5979-57d9-4aaa-ad4d-b122a662184d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=f34e5979-57d9-4aaa-ad4d-b122a662184d; Ip=[63.35.35.123]; Helo=[64aa7808-outbound-1.mta.getcheckrecipient.com] X-MS-Exchange-CrossTenant-AuthSource: DB5PEPF00014B99.eurprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: AS2PR08MB10226 X-Spam-Status: No, score=-10.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, FORGED_SPF_HELO, GIT_PATCH_0, KAM_DMARC_NONE, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_PASS, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE, UNPARSEABLE_RELAY autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Add a simple benchmark to measure the overhead of internal libc locks in the random() implementation on both single- and multi-threaded cases. diff --git a/benchtests/Makefile b/benchtests/Makefile index e1346bbda125be9fc2b216f9e8be3f2ee7cb0c4d..1ec14078ab73d7c1c0fa1d4d870a075a66543a5c 100644 --- a/benchtests/Makefile +++ b/benchtests/Makefile @@ -246,6 +246,7 @@ hash-benchset := \    stdlib-benchset := \    arc4random \ +  random-lock \    strtod \    # stdlib-benchset   diff --git a/benchtests/bench-random-lock.c b/benchtests/bench-random-lock.c new file mode 100644 index 0000000000000000000000000000000000000000..29a02ae9ff3a81114e8dd7e1dddcb3309b92df6c --- /dev/null +++ b/benchtests/bench-random-lock.c @@ -0,0 +1,108 @@ +/* Benchmark internal libc locking functions used in random. +   Copyright (C) 2022-2023 Free Software Foundation, Inc. +   This file is part of the GNU C Library. + +   The GNU C Library is free software; you can redistribute it and/or +   modify it under the terms of the GNU Lesser General Public +   License as published by the Free Software Foundation; either +   version 2.1 of the License, or (at your option) any later version. + +   The GNU C Library is distributed in the hope that it will be useful, +   but WITHOUT ANY WARRANTY; without even the implied warranty of +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU +   Lesser General Public License for more details. + +   You should have received a copy of the GNU Lesser General Public +   License along with the GNU C Library; if not, see +   .  */ + +#define TEST_MAIN +#define TEST_NAME "random-lock" + +#include +#include +#include +#include "bench-timing.h" +#include "json-lib.h" + +/* Modern cores run 20M iterations in about 1 second.  */ +#define NUM_ITERS 50000000 + +json_ctx_t json_ctx; + + +/* Measure the overhead of __libc_lock_lock and __libc_lock_unlock by +   calling random ().  */ +static void +bench_random_lock (size_t iters) +{ +  timing_t start, stop, total; + +  srandom (0); + +  /* Warmup to reduce variations due to frequency scaling.  */ +  for (int i = 0; i < iters / 4; i++) +    (void) random (); + +  TIMING_NOW (start); + +  for (int i = 0; i < iters; i++) +    (void) random (); + +  TIMING_NOW (stop); + +  TIMING_DIFF (total, start, stop); + +  json_element_double (&json_ctx, (double) total / (double) iters); +} + +static void * +thread_start (void *p) +{ +  return p; +} + +int +do_bench (void) +{ +  json_init (&json_ctx, 0, stdout); + +  json_document_begin (&json_ctx); + +  json_attr_string (&json_ctx, "timing_type", TIMING_TYPE); +  json_attr_object_begin (&json_ctx, "functions"); +  json_attr_object_begin (&json_ctx, "random"); +  json_attr_string (&json_ctx, "bench-variant", "single-threaded"); +  json_array_begin (&json_ctx, "results"); + +  /* Run benchmark single threaded.  */ +  bench_random_lock (NUM_ITERS); + +  json_array_end (&json_ctx); +  json_attr_object_end (&json_ctx); + +  json_attr_object_begin (&json_ctx, "random"); +  json_attr_string (&json_ctx, "bench-variant", "multi-threaded"); +  json_array_begin (&json_ctx, "results"); + +  /* Start a short thread to force SINGLE_THREAD_P == false.  This relies on +     the runtime disabling single-threaded optimizations when multiple +     threads are used, even after they finish.  */ + +  pthread_t t; +  pthread_create (&t, NULL, thread_start, NULL); +  pthread_join (t, NULL); + +  /* Repeat benchmark with single-threaded optimizations disabled.  */ +  bench_random_lock (NUM_ITERS); + +  json_array_end (&json_ctx); +  json_attr_object_end (&json_ctx); +  json_attr_object_end (&json_ctx); +  json_document_end (&json_ctx); +  return 0; +} + +#define TEST_FUNCTION do_bench () + +#include "../test-skeleton.c" From patchwork Wed Dec 13 14:10:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wilco Dijkstra X-Patchwork-Id: 82061 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 9AF42385C41F for ; Wed, 13 Dec 2023 14:10:40 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from EUR05-VI1-obe.outbound.protection.outlook.com (mail-vi1eur05on2048.outbound.protection.outlook.com [40.107.21.48]) by sourceware.org (Postfix) with ESMTPS id 92225385C40E for ; Wed, 13 Dec 2023 14:10:26 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 92225385C40E Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=arm.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 92225385C40E Authentication-Results: server2.sourceware.org; arc=pass smtp.remote-ip=40.107.21.48 ARC-Seal: i=3; a=rsa-sha256; d=sourceware.org; s=key; t=1702476628; cv=pass; b=IAAvPllZVZDDJvrbD+AIhp6zVwawHDRYVZr5kK+psA3u1vWbTgYCLyCQw6Of3fbsQnWprjU7qK43IiYI0SBvNq0KADsneeVo/Nf71FC+q23RVGgM3BvjJ39PRtsfrXMbYmpv9l345FhEHscwWHSMAA1W10MVs4xQw9+Gm0njMIU= ARC-Message-Signature: i=3; a=rsa-sha256; d=sourceware.org; s=key; t=1702476628; c=relaxed/simple; bh=Y01+QWelp9uUaYy3NKtliVGtIODTbmPLCpAEupgKvqE=; h=DKIM-Signature:DKIM-Signature:From:To:Subject:Date:Message-ID: MIME-Version; b=cFmZ0PhWLlMQJIJftQdbT0Fp9YbkHq6yo8sfExb65d22TB2G77/IJ0uVLf0KdDXZZOlYRhe/tbP83uM8dwqTf2Ca3um0eCNNOi8VTE0rbevgALUmLw8IUIlInfCO6+yWG8eQuNn6R2OIUylfo9u5ZtiXEzuOAFWv9/hD/MnkP3g= ARC-Authentication-Results: i=3; server2.sourceware.org ARC-Seal: i=2; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=pass; b=FVNw6ohtyz9HPE8dqKH7i6aNhURB6EUR03OEVlygatBD31Mn6cyGA6wfSWttGtCFzcXjZ0u9koQ+jAvsJ8P0Bg3bkxOAZlGNLtqLeJD9+kDIgjJYnNHSzqzAnmeA2NPK8cckFeh/STa8hDdjYXzUXpRju+s8UnVuUOXWPo0FI8+Dhfab0VNuq+w7kGOQHZiKwCWObUA5gD5TsAM+GCtgFj9SonpSLwIR5wJQTqLcCMs8grpXcXjf+6wrWf5QyKGP7OeYSkyCrlO6csLg6P1GP8L6N/wQHiigrbSD1dkixkTEwDbUOaXjEnER/dBNqyuRYRafzDNYJ82GOE2M/K6Xzw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=ZDne19+aGr0eYmoc1Rj1txwZtiApeY7Bhh9VS8Xf9PM=; b=BTPFT5ZAZG0xszaAJannWsSAytH+wO6KozUSkWRPEa8XurmAFPdZXTDqqsIo5geLahJWHafAz/0vIWSmLmRvSyxhJ+NHRmh6OM2el9bT/Gn2Bo/Ze26NWMmoQZF2xjFagDJ7o6h7nDyvPSy/KPAAkpkD+H4kTcLvb6uc/3DKbV2UQxYvuU64KDgLMy8B98n9jEi2x/23pA0hwvRJMJ/Jj8dF+z1OOnTdJJteW1c0+nweh7po5x/IjvGk7VvVV9Y+j2WEBrF12+4X9Ajo0tzxs10ncxpYZJfxM5AQlt6E004gsZ1/6kxXDZQXrm30G462Na5ITQ09PfBPXevYF4yepw== ARC-Authentication-Results: i=2; mx.microsoft.com 1; spf=pass (sender ip is 63.35.35.123) smtp.rcpttodomain=sourceware.org smtp.mailfrom=arm.com; dmarc=pass (p=none sp=none pct=100) action=none header.from=arm.com; dkim=pass (signature was verified) header.d=armh.onmicrosoft.com; arc=pass (0 oda=1 ltdi=1 spf=[1,1,smtp.mailfrom=arm.com] dkim=[1,1,header.d=arm.com] dmarc=[1,1,header.from=arm.com]) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=armh.onmicrosoft.com; s=selector2-armh-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=ZDne19+aGr0eYmoc1Rj1txwZtiApeY7Bhh9VS8Xf9PM=; b=EOz9OEs4oTJaL8Wg4e0Mx0uyBT/aF1NoWRejO2KyxyW/UXu7JfvC0BKolOceidaGv8dzhNcXjBLTa7U44Vux0SGp2RDJXQcvRdOXX9kBMJ9ca+3jjpTU+h97Ki0as+yt51zmExSen+rDX+ak/e6wQciiLDWPqLw/aglVkFAKGZI= Received: from AM9P195CA0001.EURP195.PROD.OUTLOOK.COM (2603:10a6:20b:21f::6) by DB9PR08MB6393.eurprd08.prod.outlook.com (2603:10a6:10:25a::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7091.26; Wed, 13 Dec 2023 14:10:23 +0000 Received: from AM3PEPF00009B9F.eurprd04.prod.outlook.com (2603:10a6:20b:21f::4) by AM9P195CA0001.outlook.office365.com (2603:10a6:20b:21f::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7091.26 via Frontend Transport; Wed, 13 Dec 2023 14:10:23 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 63.35.35.123) smtp.mailfrom=arm.com; dkim=pass (signature was verified) header.d=armh.onmicrosoft.com;dmarc=pass action=none header.from=arm.com; Received-SPF: Pass (protection.outlook.com: domain of arm.com designates 63.35.35.123 as permitted sender) receiver=protection.outlook.com; client-ip=63.35.35.123; helo=64aa7808-outbound-1.mta.getcheckrecipient.com; pr=C Received: from 64aa7808-outbound-1.mta.getcheckrecipient.com (63.35.35.123) by AM3PEPF00009B9F.mail.protection.outlook.com (10.167.16.24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7091.26 via Frontend Transport; Wed, 13 Dec 2023 14:10:23 +0000 Received: ("Tessian outbound 5d213238733f:v228"); Wed, 13 Dec 2023 14:10:23 +0000 X-CheckRecipientChecked: true X-CR-MTA-CID: 0f99cc7af7023f5c X-CR-MTA-TID: 64aa7808 Received: from 5921cbf537b3.1 by 64aa7808-outbound-1.mta.getcheckrecipient.com id E741CF89-9F97-4B95-8F67-D4C13DC2C089.1; Wed, 13 Dec 2023 14:10:16 +0000 Received: from EUR04-DB3-obe.outbound.protection.outlook.com by 64aa7808-outbound-1.mta.getcheckrecipient.com with ESMTPS id 5921cbf537b3.1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384); Wed, 13 Dec 2023 14:10:16 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=bUS8bCGL1U9tR6NL5Y71KIhTjAn8qi+0CjlLpn7ML6rouzmYO5VVe8pGg5bcLiGgTFPqJs7quZVlJO0cJK5WLcuEDojH4CKbrDzKjIr0LMaZgoUz3mfSbjOpDbuBg0Z9Pv8zp36K/5y+OrsCrdyeXxyHd5e6NRpgDngNj/roPvvFgwC4TiqsooGtlkQlzH8LzC38HuJucnc1CTtMfC6D19UHKX4p2HPcsD6TG3JuxZfLG7EA1A+mYPnP1HZqQsU05liMyIbf6qPWFTeleBw3O18qog9RE2VH1AGEsGMIVXgOsVGndYv7o0t6faGOWcswLpv2U/Np/RLECcNEuJks/g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=ZDne19+aGr0eYmoc1Rj1txwZtiApeY7Bhh9VS8Xf9PM=; b=f8I1U7NdrBE8adpN6UEeuD4mfG1L8ClTOAbi6YfYzc6chH/jO9EpYxUjxtY8RgDyF2KlZmO/y9pe7RlyrJ/B77TPm/0xLJcV1etPM1DeG0zjtvQff/6Z5GnJV44FaEVUnSvdfLB/9gZUMfY8VzUeT8vjyO7cahMQl53muX2sgdDhF2gbEepa2N84teSoqNjOvcNohCI5vk1UVRBc2lfkDFhQdpHOr4EirTXXXZOn3nnA/JRmDAYTwH0GzN4tbFF6Ne/DHl+xYb/zt08j9fkiQ+7UUl7fLoKUBq0TqkFq+41tHDKnUEfiMsim9H5I0qmjZD4lyXOJ6e+i8Xk7J++55Q== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=arm.com; dmarc=pass action=none header.from=arm.com; dkim=pass header.d=arm.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=armh.onmicrosoft.com; s=selector2-armh-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=ZDne19+aGr0eYmoc1Rj1txwZtiApeY7Bhh9VS8Xf9PM=; b=EOz9OEs4oTJaL8Wg4e0Mx0uyBT/aF1NoWRejO2KyxyW/UXu7JfvC0BKolOceidaGv8dzhNcXjBLTa7U44Vux0SGp2RDJXQcvRdOXX9kBMJ9ca+3jjpTU+h97Ki0as+yt51zmExSen+rDX+ak/e6wQciiLDWPqLw/aglVkFAKGZI= Received: from PAWPR08MB8982.eurprd08.prod.outlook.com (2603:10a6:102:33f::20) by VE1PR08MB5792.eurprd08.prod.outlook.com (2603:10a6:800:1a6::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7091.26; Wed, 13 Dec 2023 14:10:14 +0000 Received: from PAWPR08MB8982.eurprd08.prod.outlook.com ([fe80::2ed5:dc23:2624:df0a]) by PAWPR08MB8982.eurprd08.prod.outlook.com ([fe80::2ed5:dc23:2624:df0a%7]) with mapi id 15.20.7091.022; Wed, 13 Dec 2023 14:10:14 +0000 From: Wilco Dijkstra To: 'GNU C Library' Subject: [PATCH v3 2/2] Add single-threaded fast path to rand() Thread-Topic: [PATCH v3 2/2] Add single-threaded fast path to rand() Thread-Index: AQHaLc4XbeAP2hVUVUen27IfZxhrXQ== Date: Wed, 13 Dec 2023 14:10:14 +0000 Message-ID: References: In-Reply-To: Accept-Language: en-GB, en-US Content-Language: en-GB X-MS-Has-Attach: X-MS-TNEF-Correlator: msip_labels: Authentication-Results-Original: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=arm.com; x-ms-traffictypediagnostic: PAWPR08MB8982:EE_|VE1PR08MB5792:EE_|AM3PEPF00009B9F:EE_|DB9PR08MB6393:EE_ X-MS-Office365-Filtering-Correlation-Id: 803d3809-7c5c-494a-65ee-08dbfbe53f7f x-checkrecipientrouted: true nodisclaimer: true X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam-Untrusted: BCL:0; X-Microsoft-Antispam-Message-Info-Original: 47gCvY8G4kmvixN0nV46JqUhbjfZM3NvBv2dU6zdUJjC0WfIB/jKkdsNQHnFD2JJooBpXT/TMzDDZzKsRuMtaMA+/1FpiMj3RXzzrbthpzAs/PukO3ikqW5g7PuE8zvQVVLugn5B82ih0BrUttg0/OCZKuJoWRbU50aOejlanL4u2ay/bqL17RxLrd1FmhxynnAQxGVsTsxIxQbTPCUNgqgvsCEsikc0Ps6eridW4u1l4knTozkIeyfQUVpsasEi9O12qwGfzNFe/QsJWyOWx8picjoKEH6r2cA4ziTQN4CbFxw2u1UG49RptqV2gZE8zL5J9cIi7aHi/CmPeN2d1dNHoFof4kcfHqetuxXFr+RCaMBUWgLOXVcvKmRpuCFvqwfqLcK28YHzOMs2CpU/DoUJh+/4k28Q6jNy6Bla0ilP80GnvkHrLG0H09lKZ47adfyUA9UAjO98DOTA60S4BW/69XWcmptKItnLGrBnGYqeUtH8ghSTvYWb2WlMNEqiVQef3j0I7SU0gyzA4NjIlhylqk1nvZ2BKSnY59e0jg5zSLXe/SBze6UswChKw+WCVHKn02/t+8kZpMlnbPZUyaTp1GsTXkOk2OCAIGsK22BeZn7EIWT3TO/HiHP0EOD2 X-Forefront-Antispam-Report-Untrusted: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:PAWPR08MB8982.eurprd08.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230031)(136003)(366004)(346002)(376002)(396003)(39850400004)(230922051799003)(64100799003)(186009)(451199024)(1800799012)(55016003)(38070700009)(26005)(76116006)(66946007)(66446008)(91956017)(66476007)(64756008)(6916009)(66556008)(86362001)(33656002)(122000001)(7696005)(83380400001)(6506007)(2940100002)(9686003)(316002)(4744005)(2906002)(71200400001)(478600001)(5660300002)(8676002)(38100700002)(8936002)(41300700001)(52536014); DIR:OUT; SFP:1101; MIME-Version: 1.0 X-MS-Exchange-Transport-CrossTenantHeadersStamped: VE1PR08MB5792 Original-Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=arm.com; X-EOPAttributedMessage: 0 X-MS-Exchange-Transport-CrossTenantHeadersStripped: AM3PEPF00009B9F.eurprd04.prod.outlook.com X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id-Prvs: 7ab798cc-3eaf-422c-4c02-08dbfbe53a5c X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: JIcCTePSOOj3WUscz8kc8XnvxJ/GyC2WzyfaVVVJnQMlHvbYAZKIq90nbezuL2Hrf/lumtnFjg+5VlOOp/Nm0fwCjMEBadx8450L5r2tSFSRsElm4tJA1IeRINDqQyBSG0qD8WlsCplZwq1LZq28SlbCTMUBuY6X7rO++o+IgdPj45ZFUFX1yZEmB7Q73FMTTndFtNDUaDNhIYuRq/MGEVl87XBnDsRxtZ/GGJ6mfV2TlbN4bPd3br+nkEtFaTcXWfaHepaYL2XtVVUenVLNVHY/+HsByjQ53UsUaBfvUoihpFXy1CKvnxMdKczDZph3NjI7ne5XWAduV0JcFgSm9hbadjgS5G/8WWm66slOrTDU+DRBlAQs+OD+I0X2am7ADYp6XY6P3oYa7WigJMNdutgc3FEua/wh6cQ87/pIBlHAqwgBN1oldOX4pWSIWVcedJgEu4/eL08YsRSQp/JY0iX6mSSe1cq+geFs/AgPiZxyMPvOfZWUubzzFmiK0nSH3EwVoqgM3OWmTC50LDvKFb17IjljmvunP9PG8shzCizSMDoWai2ubp+eigtsup+n71L3foVBsRKIAM+hB0xT0VrY9t8229QtWkNgCvQKyP2DN9X6PzEParoEHwCGUIMz7NHMl1FJCqFNxQTydBNY6P645rFJV2HcWpt/ZRuvJrzenfRVl6uXtuFx1G1uMaFT1z9PTmtptudoI3lb4iP+Op8s6TZ6hHAKew/R01iYytQ= X-Forefront-Antispam-Report: CIP:63.35.35.123; CTRY:IE; LANG:en; SCL:1; SRV:; IPV:CAL; SFV:NSPM; H:64aa7808-outbound-1.mta.getcheckrecipient.com; PTR:ec2-63-35-35-123.eu-west-1.compute.amazonaws.com; CAT:NONE; SFS:(13230031)(4636009)(396003)(39860400002)(136003)(346002)(376002)(230922051799003)(1800799012)(64100799003)(82310400011)(186009)(451199024)(36840700001)(46966006)(40470700004)(40480700001)(86362001)(33656002)(40460700003)(55016003)(7696005)(9686003)(70206006)(70586007)(82740400003)(6506007)(41300700001)(336012)(26005)(356005)(81166007)(83380400001)(47076005)(4744005)(2906002)(8676002)(2940100002)(478600001)(36860700001)(8936002)(6916009)(316002)(52536014)(5660300002); DIR:OUT; SFP:1101; X-OriginatorOrg: arm.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 13 Dec 2023 14:10:23.3984 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 803d3809-7c5c-494a-65ee-08dbfbe53f7f X-MS-Exchange-CrossTenant-Id: f34e5979-57d9-4aaa-ad4d-b122a662184d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=f34e5979-57d9-4aaa-ad4d-b122a662184d; Ip=[63.35.35.123]; Helo=[64aa7808-outbound-1.mta.getcheckrecipient.com] X-MS-Exchange-CrossTenant-AuthSource: AM3PEPF00009B9F.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DB9PR08MB6393 X-Spam-Status: No, score=-10.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, FORGED_SPF_HELO, GIT_PATCH_0, KAM_DMARC_NONE, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_PASS, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE, UNPARSEABLE_RELAY autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Improve performance of rand() and __random() by adding a single-threaded fast path. Bench-random-lock shows about 5x speedup on Neoverse V1. diff --git a/stdlib/random.c b/stdlib/random.c index 62f22fac8d58c7977f09c134bf80a797750da645..174603a8915fd8aa4b3ae64d023003c9e2c038f2 100644 --- a/stdlib/random.c +++ b/stdlib/random.c @@ -51,6 +51,7 @@ SUCH DAMAGE.*/ #include +#include #include #include #include @@ -288,6 +289,12 @@ __random (void) { int32_t retval; + if (SINGLE_THREAD_P) + { + (void) __random_r (&unsafe_state, &retval); + return retval; + } + __libc_lock_lock (lock); (void) __random_r (&unsafe_state, &retval);