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 DC3E73858C98 for ; Mon, 4 Dec 2023 14:38:39 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org DC3E73858C98 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org DC3E73858C98 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701700721; cv=none; b=WCYIJpAbx2K09amk1+CIBo4Yiy1/t25KjqyCIF7IAkAUHOFG4RtFSrDqo6SGT+F4VtYkR+n6GSkX6j/NhYME+Ho9uEyg7Ziq7gUGCsufTF6OGRug6zdnBHt3d6gZxgo0s1pzYznoM49A15WdKEWq49J5T8sB7270VyA9tbWK6Jw= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701700721; c=relaxed/simple; bh=3NeIni0owb9kJRFGMGRy5mAK3ZxI40RqdmJBCed6QPk=; h=DKIM-Signature:Message-ID:Date:MIME-Version:Subject:To:From; b=uLTJ5af4MBgPl7j21iGdGn6z1nwwGAd9fL/tYDDYx+kVfrIPmxx4vusVXHb4CcTYZENkU9mTK2Riy9il0oRAC3WDDsfXsOPPRuOx4SCBSF5ocPea2hCbTgJysHKX1dp1bCdtBSOiq5Mbq87La39N0otY5uYC+czH7tGXc8BeKwE= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1701700719; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=i3QuoWq9/jTewpdpQXoIFQGWGCx8KctVIZkUDiogKr8=; b=A6RI+6LJ4uT4yqPfYI9c+5lfcTd9RQFynMtX16VFzfS1sKCgScZG0lBDgBsE8G7Y14Q/ZD ZpkU5GL1dxajyLIeE8nB4f1kLO/wv+WoAdUma2Jdo/0DVKTl7RQ4v+Uzur4km//7g5QlSy IPt0H8tLAJI4hAJ4frBMN7fZQjhismo= Received: from mail-yw1-f200.google.com (mail-yw1-f200.google.com [209.85.128.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-341-ByPrBXzZPOW66icaf9Me4w-1; Mon, 04 Dec 2023 09:38:38 -0500 X-MC-Unique: ByPrBXzZPOW66icaf9Me4w-1 Received: by mail-yw1-f200.google.com with SMTP id 00721157ae682-5ce16bc121aso75138057b3.1 for ; Mon, 04 Dec 2023 06:38:38 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701700717; x=1702305517; h=content-transfer-encoding:in-reply-to:organization:from:references :to:content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=i3QuoWq9/jTewpdpQXoIFQGWGCx8KctVIZkUDiogKr8=; b=jkGjpJxM+wb5KsMaaKLPhNfStqDmy6unk93HL/XnSqPY3V2up6+3Hw9klZBiF07Tqk jsXkqZL9QKWmaVx+hMK+MVPUI3xk0LhauTEwZRc3q+qApKzoSf+hT/r0PqOSVmDusyc7 GU+giJorcJJDO0PYyr4difn9rHzdjubduah+WSLNZpvNXxIzic4WyTvzDt4/jJKzDfKk Q5Jz8719NMDkPLFP5twEc216Ka72DccPC+NsgYIZ4a33pjGaiMMogdQGaEbqSLjYNZjc rwlzh3cEY8EmPyKu4nhlO8KC10Ah0jQ5ZlhcdjQEM5ZgpnEFpZjJVWzfmpRyGWznYuJV z7WA== X-Gm-Message-State: AOJu0YzU0P3RjTiWOe7SVPo2+10NZuMIrv7OmycoSwGEtlMSoiH9PvqH +NJQl2vkxrgvLs6tov9AE9RNlv7GsylnjRPbKbA5U4FBQgRrZHJoJA9XKjyq0J/cTyHpPoInQO5 073Cup/Qai7Oi4Z9iLXksVglRtinG4ZE= X-Received: by 2002:a81:ad54:0:b0:5d4:1242:29e5 with SMTP id l20-20020a81ad54000000b005d4124229e5mr3532303ywk.3.1701700717418; Mon, 04 Dec 2023 06:38:37 -0800 (PST) X-Google-Smtp-Source: AGHT+IF3GI0tiqqqzIqELlrc/zxQ5ZjN0r4HKAbepgG5KJ2hbSjqCHZrrE9NGOHj3FIKpWD31cbhYA== X-Received: by 2002:a81:ad54:0:b0:5d4:1242:29e5 with SMTP id l20-20020a81ad54000000b005d4124229e5mr3532293ywk.3.1701700717104; Mon, 04 Dec 2023 06:38:37 -0800 (PST) Received: from [192.168.0.241] ([198.48.244.52]) by smtp.gmail.com with ESMTPSA id x72-20020a81a04b000000b005a7f676f305sm2894631ywg.106.2023.12.04.06.38.36 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 04 Dec 2023 06:38:36 -0800 (PST) Message-ID: <28681765-c150-65a2-5f46-51172070263b@redhat.com> Date: Mon, 4 Dec 2023 09:38:35 -0500 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.15.1 Subject: Re: [PATCH 1/2] Add random benchmark To: Wilco Dijkstra , 'GNU C Library' References: From: Carlos O'Donell Organization: Red Hat In-Reply-To: X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-14.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,KAM_SHORT,NICE_REPLY_A,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE,TXREP,T_SCC_BODY_TEXT_LINE 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: On 11/28/23 12:35, Wilco Dijkstra wrote: > > 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..1415d20e25798871936f3e2b87f8690517239b21 100644 > --- a/benchtests/Makefile > +++ b/benchtests/Makefile > @@ -247,6 +247,7 @@ hash-benchset := \ > stdlib-benchset := \ > arc4random \ > strtod \ > + random-lock \ Fails pre-commit CI. https://patchwork.sourceware.org/project/glibc/patch/PAWPR08MB8982D7F540068200C32DA70983BCA@PAWPR08MB8982.eurprd08.prod.outlook.com/ This inserts a routine in a non-sorted order in the alpha-sorted list. lint-makefiles should fail for you? > # stdlib-benchset > > stdio-common-benchset := sprintf > diff --git a/benchtests/bench-random-lock.c b/benchtests/bench-random-lock.c > new file mode 100644 > index 0000000000000000000000000000000000000000..ff11f807359c05043366c855751a97a383b1b132 > --- /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 40000000 > + > +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" > > > -- Cheers, Carlos.