From patchwork Mon Mar 18 15:17:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wilco Dijkstra X-Patchwork-Id: 1913167 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=armh.onmicrosoft.com header.i=@armh.onmicrosoft.com header.a=rsa-sha256 header.s=selector2-armh-onmicrosoft-com header.b=BWH7Aq5k; dkim=pass (1024-bit key) header.d=armh.onmicrosoft.com header.i=@armh.onmicrosoft.com header.a=rsa-sha256 header.s=selector2-armh-onmicrosoft-com header.b=BWH7Aq5k; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Tyz4L5Yf0z23sH for ; Tue, 19 Mar 2024 02:17:38 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 5BD913858408 for ; Mon, 18 Mar 2024 15:17:36 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from EUR03-DBA-obe.outbound.protection.outlook.com (mail-dbaeur03on2042.outbound.protection.outlook.com [40.107.104.42]) by sourceware.org (Postfix) with ESMTPS id 017DC3858D37 for ; Mon, 18 Mar 2024 15:17:13 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 017DC3858D37 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 017DC3858D37 Authentication-Results: server2.sourceware.org; arc=pass smtp.remote-ip=40.107.104.42 ARC-Seal: i=3; a=rsa-sha256; d=sourceware.org; s=key; t=1710775039; cv=pass; b=CTb2HdwEN+Rfnd+BID47oiKC7xh5AQOhDm6W20ii+OfyOSI9tm3Wyrqv90QyqGF+SwL3u0PUR8b0dJuBTEfoQkBJMNfPv8YdLCczFZLM9XcealR4vmV6gbFpOvEtB+0VFW0UlzVBTL5BwSwqrFOIIdufbJRgJ0+SzlDysBGL2eY= ARC-Message-Signature: i=3; a=rsa-sha256; d=sourceware.org; s=key; t=1710775039; c=relaxed/simple; bh=AJzDWhn0TVxEQhNGiHTAVFv+mfNLa1gmf58i4eQ/pKk=; h=DKIM-Signature:DKIM-Signature:From:To:Subject:Date:Message-ID: MIME-Version; b=fwnwRkkMqDGYKOInwOjvPUgaVACLXSjurbZAKJYu2ffKg6I4D2kbblc/6SWua/vV1ucNxnZ9hfkT/YpVCbPzZ03L+14he2lMDt+IzJYcduUy0LHDHA+wNsPeUh/Yknr4GcSnS39APHISMiMo0Y/0bBJCa++2rklaL4N5o+KgAuA= ARC-Authentication-Results: i=3; server2.sourceware.org ARC-Seal: i=2; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=pass; b=WtUTdkW201Nyp8PKO/orf8bk9PpmXdghE2JPrNVU/jOSL1tCynW/0o1xELIAxQKJDjt6HJZBTTIOUHJT5gBhsErdqLWSPa/viRZ+TPRl1u4bnXMNkwV6hnR60A2qFLdXpaLgeIvEJ2O6SLhuDKDJzy2MwwfgicI+3KAh+zZA03v8lPYbKuMtmaqs3kN6XRJpQp2AFKm+S9OPXRbVWd2LuSQ4ETwCdK6p8VgeJM6Z4nts7S5if1j6SuS3mBiaqqd4SD8bA0KWHb8SMYDcBBjJukbm/6TvyRq68zGzCX8ObmMKqdxIxEw2RWBdWITv2klU3K9bfMgMlXIHNQY8+/x+CA== 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=zO01Q+XG4qGhSXBKDmkt9MrNry8i5n5eSfASSym1C78=; b=RTRKhej8BfeWCoxtgJUU4esCMhaYFnbSiQHdbM2/LXcZrdeFMcFvumYgVlt/6P3GGZnbUl2XE/1KyrK1d5Bkh33AS1uhglI1N1pwa3QZifoS0F0HItUwBGIF3Ct8jQCgOjj6cU1ZzIfkthVUHDOH1WPOI8BG4yCe5SHbpP662fDR8Is65dD83M4i2GLx5ly3l61cOhR7Nx6XNf/Q2lBr/V1Hs1/s/U818QHoYk0kWZ5481Z9IgBD0x5FWqT9skc9yq+/+M6fJn/EforjCZKmhSzsulLAccU6b8gaAI7SQtSnebyuIBEdcnxECUnzUT75zfwYiLh7OhI53bbJkmNt2A== 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=zO01Q+XG4qGhSXBKDmkt9MrNry8i5n5eSfASSym1C78=; b=BWH7Aq5k6RAH7JzYdwk9uT1+04ayBv1FovIm5poXzV0tu89kDet6MhufozGl+xfKzk7f8M925D6hqFySPnq0Io2Xf1GcSeUCuJC6XN0d9fIiyEF+YkmL967qDEL+Rz5lEAaDLxQ2nkiCVH24CuS9D8ZuoFGAc1Q5XGcjP9FNxAE= Received: from DUZPR01CA0233.eurprd01.prod.exchangelabs.com (2603:10a6:10:4b4::17) by GV1PR08MB8059.eurprd08.prod.outlook.com (2603:10a6:150:95::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7386.25; Mon, 18 Mar 2024 15:17:09 +0000 Received: from DB5PEPF00014B91.eurprd02.prod.outlook.com (2603:10a6:10:4b4:cafe::3e) by DUZPR01CA0233.outlook.office365.com (2603:10a6:10:4b4::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7362.39 via Frontend Transport; Mon, 18 Mar 2024 15:17:09 +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 DB5PEPF00014B91.mail.protection.outlook.com (10.167.8.229) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7409.10 via Frontend Transport; Mon, 18 Mar 2024 15:17:09 +0000 Received: ("Tessian outbound ff4e98f65004:v300"); Mon, 18 Mar 2024 15:17:08 +0000 X-CheckRecipientChecked: true X-CR-MTA-CID: c0e30e05088fc676 X-CR-MTA-TID: 64aa7808 Received: from 881bda044310.1 by 64aa7808-outbound-1.mta.getcheckrecipient.com id 81F252CF-3FA9-4B71-8907-EC10731169F9.1; Mon, 18 Mar 2024 15:17:02 +0000 Received: from EUR05-DB8-obe.outbound.protection.outlook.com by 64aa7808-outbound-1.mta.getcheckrecipient.com with ESMTPS id 881bda044310.1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384); Mon, 18 Mar 2024 15:17:02 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=bDIBP0c/oMYv0YF4MaHGAICfCyiFyJDHEZ8CoUdROHo1x/arjqZlp0mnFeHkwcuMgXEl20K4UfCbWqwvsirfpaCame2PlvaNQnxqaIrP9z5CM9xjXLyGqZfWmfINmDqhofBxlCI4EJ1RKPWfezPmGZ/dbh7I4c4uriioeNZmyuyoO2d8I5/C78ZZ64MDg9uSJrc4P+IlhESNvhJcTm7wKuQvdXliu1pf4+7XliBwyfMxjd+x3qx+PtoBWZUJa7NGsK9xUTNHIN3qG0VXMyacxU92+574SvWEppi54vP27gJAVqy4iTSjHi5l/v2xaauHtVPeq+CRB87YTIpDQRokQQ== 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=zO01Q+XG4qGhSXBKDmkt9MrNry8i5n5eSfASSym1C78=; b=Tf0/hcEiBIrhLWCa77oQkuMYYjOb45NfwnKjC6b+m85/ez8FepZZlphbaH1XjLkYqiVapZ6EbeSMbjeV0TpEzX8x+IZFuONBoVj6vta7x+DMzag/DxdX/+nxNUVTU+lu1q0G/GjVceOGGZE/vJUC8ocEXTtg5LGj6rqKhOFE+/+fNMRbZ0OTV6AqXKLj8Xv4UubY8jk45BaNxLWz/mlIRc+OCTA9uryM41MmlBQPNo5Pdf0kiqcAqLtB+UFa/THcus5sZbHadZjFBXsOYmdHdmCTau/PXdVGf+2+9awXQTNXoo0JsFD5AsPGd2yyVQg6OYbPJ5mk/vsPL0WU/BbOAw== 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=zO01Q+XG4qGhSXBKDmkt9MrNry8i5n5eSfASSym1C78=; b=BWH7Aq5k6RAH7JzYdwk9uT1+04ayBv1FovIm5poXzV0tu89kDet6MhufozGl+xfKzk7f8M925D6hqFySPnq0Io2Xf1GcSeUCuJC6XN0d9fIiyEF+YkmL967qDEL+Rz5lEAaDLxQ2nkiCVH24CuS9D8ZuoFGAc1Q5XGcjP9FNxAE= Received: from PAWPR08MB8982.eurprd08.prod.outlook.com (2603:10a6:102:33f::20) by AM9PR08MB6035.eurprd08.prod.outlook.com (2603:10a6:20b:2d9::24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7386.26; Mon, 18 Mar 2024 15:17:01 +0000 Received: from PAWPR08MB8982.eurprd08.prod.outlook.com ([fe80::8b1b:5f28:5006:ac18]) by PAWPR08MB8982.eurprd08.prod.outlook.com ([fe80::8b1b:5f28:5006:ac18%4]) with mapi id 15.20.7386.023; Mon, 18 Mar 2024 15:17:01 +0000 From: Wilco Dijkstra To: 'GNU C Library' Subject: [PATCH 1/2] Add random benchmark Thread-Topic: [PATCH 1/2] Add random benchmark Thread-Index: AQHaeUa7SB0YBnP+/0KfpQ11Qxb7yg== Date: Mon, 18 Mar 2024 15:17:01 +0000 Message-ID: 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_|AM9PR08MB6035:EE_|DB5PEPF00014B91:EE_|GV1PR08MB8059:EE_ X-MS-Office365-Filtering-Correlation-Id: d1a286d8-740e-46c0-0e62-08dc475e7ab1 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: eGNruZYHAK1Z0ZD0Nwn1O4CsLOa0YyySZwMvSwV/SrWaNmU1YxbDdwpjmuZ+MT/I/G6/C3yxkQ6GdpttGDR1KaYrzF8+802qqG8p0HyDC9qQkJdQSSCnEtrmLCTotvDc8nV8Xa6ZCJrKVL5Ur6f14iHmeiiIBtJjWZpXo4IfBpjcvBP3ldBSTyV4IPuWr1T7hNtJ7wQBGcX5pbe7o5UjmIu5mD72+gJvHhDq7C2IZmi8WEDrts1QE2lGqyM4zMHjpPQQsiErrJn6VSYh0z3mZueNv3q8L0Uic3zQoOkRYpd8y/qHqJ0v9CNL+VRNi3TF78HiCbVOhxy1Eh7N8A/kNKcDDuex+LGEDumeWreDBzEyNkzXduOznPnyghwy9EYCxcei9HsQl9UGbYkWkeSD+CsUSbLz3N/IGQp9ELqyuFSGkm4bp9feP9P/Ecz5U7GCDRfr35Q7PK7ed/IqCfBMzhbtWfQO03cRCvNP+8U5O9gIRyKj26jLiAHmJ81zx18xPlpntZaecS3NnX2fK9BAkBdNt9rmlkAcKhR3lTAnZI4q6AoiW5qBx15fby3xStsO0G97Bc9F5+pQPeRO/pbkhEX0/QOxggVrWv8v3mar8VNa12auoxvYq0INjpiTc63xlRVIQtgqAbg7qT9MK2HXIqnxldMRaS9zje9bzFzyX9o= 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)(366007)(376005)(1800799015)(38070700009); DIR:OUT; SFP:1101; MIME-Version: 1.0 X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM9PR08MB6035 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: DB5PEPF00014B91.eurprd02.prod.outlook.com X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id-Prvs: 723e9e5d-23e8-414c-e1e7-08dc475e7612 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: tZRVa+NExJUeuZGSFg3Zab2Y/LUgHtC1HofOPRBcbLhPEXLLcpu+2vo7STbGHJymutpaQ/lwzK+K9m3S9UsfpMqsFgObje53yeX6hfCUeBr15Jyqm4tKr3w54pRn4OpVNYZG0BDHbgmPXbbNiPGIpBay6/cS32XQTb2emM6LtPwhJhto2AmWr17TL2i2WZzqW2bBdaEJiZ/0imfJxMvg9NSfDmQ90Lhal05QxBXDZJ4mBvOhnU6zkOhpIMkoijsoSQS13O5C7fjoBQguawebIUWrfWclSPOvH/F0Mnr2/hi/AybwWpFDW+fWWYAMFzLsInfm/dhUu1PrPLT16Ux5E6XlX6x5CsLHaGerh8GtDylYuNvIaQ3SGAW4wL2bgE4A8X9jO+OYzCADuJ9sHFWF6R9vtKlJwpnOcIGG0iWaBZET9WsXvvg1OxswVQddOaiRxqqPQZgOSiOowVqLz4kwZe1GR2TPcu1pVAqU8jJX0NvVm07u6xg55XxnStGisBmwTVyk2MvDJE4li2cECdR0/+qRk5UgjKbYcppuDQfDkAyRiKrWHHMUJk6GyA2j7pCF5zN0dgbSJu4SEn1uTKcqVoY/2fJ9GGv/j+ZAkMnL3QH1ftnP8iHXyK/ekXk11cZloXFcNL9DPxSglXby+9fuTcxGstYhXACeB5ejSVcmVhGQBi6zmeS7Q+L4id+6kFi29rxLMeb52A4QairmnPBcVw== 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)(36860700004)(376005)(82310400014)(1800799015); DIR:OUT; SFP:1101; X-OriginatorOrg: arm.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 18 Mar 2024 15:17:09.0377 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: d1a286d8-740e-46c0-0e62-08dc475e7ab1 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: DB5PEPF00014B91.eurprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: GV1PR08MB8059 X-Spam-Status: No, score=-11.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, GIT_PATCH_0, KAM_DMARC_NONE, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, 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+incoming=patchwork.ozlabs.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. This relies on the implementation of random using internal locks to access shared global data, and that the runtime uses multi-threaded locking once a thread has been created (even after it finishes). OK for commit? 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 Mon Mar 18 15:20:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wilco Dijkstra X-Patchwork-Id: 1913170 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=armh.onmicrosoft.com header.i=@armh.onmicrosoft.com header.a=rsa-sha256 header.s=selector2-armh-onmicrosoft-com header.b=G+8rzd2Y; dkim=pass (1024-bit key) header.d=armh.onmicrosoft.com header.i=@armh.onmicrosoft.com header.a=rsa-sha256 header.s=selector2-armh-onmicrosoft-com header.b=G+8rzd2Y; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Tyz8N5Dhhz1yWy for ; Tue, 19 Mar 2024 02:21:08 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B4EEF3858401 for ; Mon, 18 Mar 2024 15:21:06 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from EUR05-AM6-obe.outbound.protection.outlook.com (mail-am6eur05on2060.outbound.protection.outlook.com [40.107.22.60]) by sourceware.org (Postfix) with ESMTPS id 0039B3858D37 for ; Mon, 18 Mar 2024 15:20:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 0039B3858D37 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 0039B3858D37 Authentication-Results: server2.sourceware.org; arc=pass smtp.remote-ip=40.107.22.60 ARC-Seal: i=3; a=rsa-sha256; d=sourceware.org; s=key; t=1710775252; cv=pass; b=Y7wm8/Ea65L06xk1fYKHP3IXPIrgEu1gfSnZ0/ZL12FzTbprV3XDA8HbHBm5Da3XtfkGoyph0uVGZIV5lYtD6dlVmxICxeRHEMstL8kFYBkqxSrARSvMk/mw3IRQWDSa61f0W4AwTN0kDaX3Zcq4Ed9VK/MOyzIsM3oMRMQO/XA= ARC-Message-Signature: i=3; a=rsa-sha256; d=sourceware.org; s=key; t=1710775252; c=relaxed/simple; bh=cdShbtg8H9bmIjLOsdsFOcdqya2Y7/JrqamkCs7TiA0=; h=DKIM-Signature:DKIM-Signature:From:To:Subject:Date:Message-ID: MIME-Version; b=iBUEqeYOWbShfhscPNxdSMYr0KwoVno0wWpm9AxfsgKA7K1zZdq3W+UVJHLi1HPD2f2V79EpL22Mn6hCLPIeYRQ1XjeikzDeZbpYXPqy6XIK9a5vbZy8qzRxkn4oo/LGpxCNEblsxSCHB5YlYddpT9dvo9G8wvnd+pPvSALxG7Y= ARC-Authentication-Results: i=3; server2.sourceware.org ARC-Seal: i=2; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=pass; b=XrIkNOaZzMVBi8eKmIDaoFFmGsf57nFdIuf35wABpY8CotMS5X8RkNce9j9yU3gJmINAZebu79Z7bxB/ndcAbAwXXmQcRh0r69FuoAVgpPMcfMgMgIs2pLPiXhpitNlI6eZv5E2FyW5zwDgb34wdp3M+S1yz2MQ40lY5cNMcLKu/CKRD+EbLxkkrIPp4MA9Hov588g+D620zvw/56cMBko1MhMdJ8usd5d7kXY8SiMMngrKVCPEzJ8aZ74qsiI2/ZY/rBcpP8c+Z8zc0ztni7/9oqtLmmmHDRq78lou/I6vS/UeXrArrVufnkHgtVg17hYP5Baq8pwSBJpv4zc2YNg== 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=btKD+9l64Dm5C0jGR73EDTonH+mSGph8ttXh8fFwgw8=; b=Kn+DfJfwBUl+Wslv9mqc4cmyXIyGX5j/jrp8FsmPpVhePMzAIqt7rrdPDsupq+AwCBbU8ikRlkhyaQS6mxIuFfAyG61y0qeBK41BfmnWWcQ7AB6oIsaZkeVbdgVHgyzFH1HXnsFshCQVfmfuCUx0wQ8n8ZClBMEACEJLO7k4V6knSXN8UcVXHQDNaPrKkP3D/smK72neWeIH5Y7tXFI+2oCbOhr/a20Kx5gLmIr0AenYM1HzXqbcKOVR1DhSoTa048GSMQZ3f/XCSzbtNIzQmTRU02oOtK5zMcye+vddHGoI0ersyZZeb47fVC+EFlkXLXfyf3FlYrzr/SxRIZnVvw== 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=btKD+9l64Dm5C0jGR73EDTonH+mSGph8ttXh8fFwgw8=; b=G+8rzd2YzmEpkhjfwe99yJM2cTUyWn2GQ03Bf6O18x+wN2GklfybVFg4thz+a0cRhem3lfE8Jf/avQXbnX59PcgguDemOjanhjWzuOWJ2EBJ5HnPAxWwwB1sOWxMkutztP4qwgxMhqgwxlLzlZ9fWergxG6PKZZZQDLn2GBH9gI= Received: from AS9PR04CA0100.eurprd04.prod.outlook.com (2603:10a6:20b:50e::16) by DU0PR08MB9821.eurprd08.prod.outlook.com (2603:10a6:10:446::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7386.26; Mon, 18 Mar 2024 15:20:45 +0000 Received: from AMS0EPF000001A8.eurprd05.prod.outlook.com (2603:10a6:20b:50e:cafe::76) by AS9PR04CA0100.outlook.office365.com (2603:10a6:20b:50e::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7386.26 via Frontend Transport; Mon, 18 Mar 2024 15:20:45 +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 AMS0EPF000001A8.mail.protection.outlook.com (10.167.16.148) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7409.10 via Frontend Transport; Mon, 18 Mar 2024 15:20:44 +0000 Received: ("Tessian outbound e26069fc76b9:v300"); Mon, 18 Mar 2024 15:20:43 +0000 X-CheckRecipientChecked: true X-CR-MTA-CID: 032f4d4c2d129847 X-CR-MTA-TID: 64aa7808 Received: from b300d399d13f.1 by 64aa7808-outbound-1.mta.getcheckrecipient.com id 7E4D39CD-DAC7-4738-B142-B0F3438995ED.1; Mon, 18 Mar 2024 15:20:37 +0000 Received: from EUR05-VI1-obe.outbound.protection.outlook.com by 64aa7808-outbound-1.mta.getcheckrecipient.com with ESMTPS id b300d399d13f.1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384); Mon, 18 Mar 2024 15:20:37 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=AACRCkgzQba4VDBiUjhE8Tha1GuGkDMlyrSMrMs2tV3rtEty+bVm+ch8vvGJDmGFDsUAVlh09naFSDF9spWey0IxhvddXd2jKXBEwLWIbEGTzVlRFSrj7JIDHPZqdEGLKT1KVL4f8wpRbbNbxbJsuBEfhLzq1LafcDKFxKd2kKukwbRHfmhz02nixFuPk5KeiR4ckgLLX0iBw1ZoOA3MROrrfQNMvTpA0nNCIg+rHtCVAs2Dslu9fixlfyilNp8g5RDFesgv5AkkhWtFXx5juxbVn1tkPlhDeAZ97Po2RZBgjnbFjnlYgU8k0P2l84xwHlRg1U7g0v6kuyyZcy2fXA== 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=btKD+9l64Dm5C0jGR73EDTonH+mSGph8ttXh8fFwgw8=; b=L+a6BdWgt/PU8uswtzZnWbHFeFF+d8gxUN6xumxQainkIU7SAu6hWDniJ2V9EvZfKED06kja24aDeNRZ/MQo3mGh3AzC05ChpcZCCQc4j3D0jHYDXXgfWP07B4M9Te8Tf/SfMsA3vFlPoZBhr8fdeU+q0ypmKoZBELOPKLGNGvDLqVLZndAu1POaEZk6FEha1Hv2ZQXCCnzRq96OO13WvEo+RRUY1eWF+/aF/eFfl/hp4HV1VFYN0tbPtl4pRWw5oBSJrXhoZKmLf6KqDuzFWImpZ0FGA2QrFzIQRl+n+e027QRetsaYLt9Unhy/XlwpWX9Jj349zxcDormVWc4rFg== 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=btKD+9l64Dm5C0jGR73EDTonH+mSGph8ttXh8fFwgw8=; b=G+8rzd2YzmEpkhjfwe99yJM2cTUyWn2GQ03Bf6O18x+wN2GklfybVFg4thz+a0cRhem3lfE8Jf/avQXbnX59PcgguDemOjanhjWzuOWJ2EBJ5HnPAxWwwB1sOWxMkutztP4qwgxMhqgwxlLzlZ9fWergxG6PKZZZQDLn2GBH9gI= Received: from PAWPR08MB8982.eurprd08.prod.outlook.com (2603:10a6:102:33f::20) by GV2PR08MB9301.eurprd08.prod.outlook.com (2603:10a6:150:d4::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7386.26; Mon, 18 Mar 2024 15:20:33 +0000 Received: from PAWPR08MB8982.eurprd08.prod.outlook.com ([fe80::8b1b:5f28:5006:ac18]) by PAWPR08MB8982.eurprd08.prod.outlook.com ([fe80::8b1b:5f28:5006:ac18%4]) with mapi id 15.20.7386.023; Mon, 18 Mar 2024 15:20:33 +0000 From: Wilco Dijkstra To: 'GNU C Library' Subject: [PATCH 2/2] Add single-threaded fast path to rand() Thread-Topic: [PATCH 2/2] Add single-threaded fast path to rand() Thread-Index: AQHaeUdlLczthbtZN0yQRvy3br3L0A== Date: Mon, 18 Mar 2024 15:20:33 +0000 Message-ID: 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_|GV2PR08MB9301:EE_|AMS0EPF000001A8:EE_|DU0PR08MB9821:EE_ X-MS-Office365-Filtering-Correlation-Id: e163ebcf-033c-48df-88ca-08dc475efb02 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: tcGO2RRzf45I3qzP1+8ieSQeKOoF3I29sgieosAJIGd/yX8hrjktJpSkxlLXqa9/QB+s9wglq+gQtrnDYhn6XXoz7qju6zyFFHe95LTKSjeRQt9jKZQZPAPNaTW+FE07BYWxq049VevcqSBFbp1Oo+NpsYu85q5+3IAgsGb6hQ7/QRF5TWDX+VJLLT77JGTZofMFAI/6Q8QLo/YELafQ6jxl/k90MAWgvJ2F30/13yVPxZApmQTHdjprGZI9KE4mUOt8NAJfBvRq1KVIgATcBz0Z1hpf01Y8OEiISiLuuS3d0/fBGW8Oj9FU/1dAdUJu/L3uvWdFwiimejcfl4EYSB0fawgSnMgB6BsiuzdQjKw/18BRlE0Vh6MQfMRicMQ/ijNGSd/hpIG7Nedp+rRY3Vywj1prwGscjCG77oht9G0OCn7PVv+lQav+orUAaS3TO89WQ5StjW6RoS3FzqFuitC5jBqWGxIr25i3PL/FmaSBY9xBJC4I70MqGenSiHxRAxj+6eOdyWy+63pfhGTLtLUOVu1zxgruxwSgDiHzLUC9N2lOHKYjqtaeTKEB6KwH8DmrM19qZ+aeENB1CkdxYOTptP1sNbtbypxMeSP/p1yhCo8q3ztgNnFowNnHkvGIOd83oM2DuG24qGdEtIbEjuJyP0cOIpblpAXunbm/SjANendYqJqcFB0Jdmz82mY2NyKFoCfQ5W6hr85Zh64rcA== 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)(366007)(1800799015)(376005)(38070700009); DIR:OUT; SFP:1101; MIME-Version: 1.0 X-MS-Exchange-Transport-CrossTenantHeadersStamped: GV2PR08MB9301 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: AMS0EPF000001A8.eurprd05.prod.outlook.com X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id-Prvs: 0ca2e880-fd37-44cd-9ba2-08dc475ef476 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: PjIGCnlYLPij9ygLrj5JM2U12YTtX/N4M9CzsaPS0wP8PDY/s6yAYrDnYhINkiOosM+m6Vo4M3Iyu9uZ7H+NZ/ZlRDjjYAvdBOMchypIvW9V5S0+NXyDNnptpGVaHfneiujSsugcjFP7JtJcc/BkZ1ltUajUCTAzyTems1IUBmnzKx/85Dj0GtBS7GKw1p/xFZLVAZ0B/+lh4m+Cx5IX6RhLQ2gX3AV9KI/t9Q2ANug9gg/OQdIVrIp2ru35M76+JDjXJUaAzK0M5anTi1mtqV6r+nJJzvwaGRl/5RlIgFCvRNQ9AFlYER0gUJUYMVMju+4OfGi1n3f7+8bWW5y8tkZDzI/o824vZkIQKDS4AmvCTfBD4vXW9mGJcfR+44IV9x9v63IkVP9aXmO7Sd84FAlf+33MnvqLe+MivP3Wpol7rkeu9K6jM/tBMsK+g7ZoJN4ecqNNaWXs2dL+yQK/mBWmKdJytP/91HJV34y1j4Ti3hror4H+gEj2Ij30kcHl5FeuMHeUHtFWJRfVDOQabaovZniN/wU510AAHlQs8/HVS3YRvFfuhRtTTjUNKFuo1vziJiHOnFlcF82B+8TFaS0jCOOMuN6FNlAncp4rtWeXwVYkaqudR4u+0J3IXFeac9eVA4zRja3WGSCGozlTMnEj3hTqUHWR7hv+B4hk9YIIgJdV3bGWWR+FvAfMvF6W3TGzkHIr5W3431TpH+Xqe02ORdqYSoy2Nmu7iKRAqOcWnWYRXzjHr7dHvEgdN4LN 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)(1800799015)(376005)(36860700004)(82310400014); DIR:OUT; SFP:1101; X-OriginatorOrg: arm.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 18 Mar 2024 15:20:44.2419 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: e163ebcf-033c-48df-88ca-08dc475efb02 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: AMS0EPF000001A8.eurprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DU0PR08MB9821 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+incoming=patchwork.ozlabs.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. OK for commit? 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);