From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ed1-x52d.google.com (mail-ed1-x52d.google.com [IPv6:2a00:1450:4864:20::52d]) by sourceware.org (Postfix) with ESMTPS id B9697383A0E4 for ; Fri, 9 Dec 2022 17:23:13 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org B9697383A0E4 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-ed1-x52d.google.com with SMTP id i15so4009672edf.2 for ; Fri, 09 Dec 2022 09:23:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=U1g5xSZf8V3BKJIexiB1A8zgvCM0nL3i8nEirOVbCd0=; b=jwepKIiFBrkBgHhK+FBm4P/JBFq/c1xf5D/ekRmYjkK+BT28RlJrqpZ0zB4WzQHFHu MnyGdCiZvLFRmouKHjoef5w2/AomLW/dUzmMUwcyhL7c5Oxvellx0Gwd+xGNb05ppfkk Xtv/Zt1LrObJtsrrXv+/TINUPMutODlrhGONnRWjpp5bQIY6GhjC8H84wEggwI2/A9Ti w7/m9PB2UbPlxnCPzrHCnZ15QzYVDdsEEvZMFVixwJCDP/eCIHKwn2l7NrPxxvHKCRod ulumvp917leMrLK5ps2oq4HWLBxl4d+JFCD0ZlhuWzhjyTRpY7mlMO+HjLxf1fRa/3Lp XC1Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=U1g5xSZf8V3BKJIexiB1A8zgvCM0nL3i8nEirOVbCd0=; b=YIUmcHR8MiJfaVOo8UYvlneR0uHYxLA9lxarhSOu5TbXr6Ds9EQFI9FsBB2mCMzeoQ GmGmmQ4SSXmNQorS4evBmnKTEULijFJlp3j7H7i6CK9tt27ULfU9CSpdvEnQPJwSGqa9 P42SUzEvzxna9aDOtBym9dzueilltDcLYZA6/yyjqlx5lUhg0mzpFvf/wntsommruqTO FU+YiM96FMiWbZAqGC2rDdZ9cHoZnWHTDgB7rCfgDWIKxe9ug3un4C/7omN8jv8mXNkt VH97d9M5f+NxMk1bdxLgI5GDuPXxubtwXipakjR1MmLC6V39yGI7xLSYq2fT8LvgtYr7 5tYQ== X-Gm-Message-State: ANoB5plnIXzdGImBkmqYa1WtfzsJtOJ6Mf/FVIjjbZviAsS/PKSy87aS YmpCgJNaDFpkBb+mB4DeLIwcNd8qWTi6GSW3LmU= X-Google-Smtp-Source: AA0mqf4quasbgR5qIOS/JNYMHO9Lw9spuNhh+SpInjRU6Bho18N0w+lnqHMmNWjUOdgMxBE+8gqkUskk85E5kMlIelc= X-Received: by 2002:a05:6402:e8c:b0:468:89dd:aa0b with SMTP id h12-20020a0564020e8c00b0046889ddaa0bmr86441299eda.258.1670606592589; Fri, 09 Dec 2022 09:23:12 -0800 (PST) MIME-Version: 1.0 References: <6a3d9283-3866-e837-c348-6d2a4a701c4f@redhat.com> In-Reply-To: From: Noah Goldstein Date: Fri, 9 Dec 2022 09:23:15 -0800 Message-ID: Subject: Re: [PATCH v2] Add random locking benchmark To: Wilco Dijkstra Cc: "Carlos O'Donell" , GNU C Library Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.5 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,GIT_PATCH_0,KAM_SHORT,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP 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 Fri, Dec 9, 2022 at 5:59 AM Wilco Dijkstra via Libc-alpha wrote: > > > ping > > From: Wilco Dijkstra > Sent: 24 August 2022 15:43 > To: Carlos O'Donell ; 'GNU C Library' > Subject: Re: [PATCH v2] Add random locking benchmark > > Hi Carlos, > > > We could run the benchmark for a fixed amount of time, rather than a > > fixed number of iterations, see bench-skeleton.c and DURATION. > > Unfortunately bench-skeleton is way too difficult to use - I'd just want to > give it a few functions to benchmark and let it do the rest. So it needs > cleaning up before benchmarks can use it without a magic python script. > > I've updated the comments, see v3 below. > > Cheers, > Wilco > > v3: Improve comments > > 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). > > --- > diff --git a/benchtests/Makefile b/benchtests/Makefile > index d99771be74b40f8afa3953f61c0721b19658d4b7..c413eac1d23568cb88bf22c6e50303e24ec10ea0 100644 > --- a/benchtests/Makefile > +++ b/benchtests/Makefile > @@ -236,6 +236,7 @@ hash-benchset := \ > stdlib-benchset := \ > arc4random \ > strtod \ > + random-lock \ > # 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..c0aecb7f95b9f185df3bba7a75e3ef0884dbf6ce > --- /dev/null > +++ b/benchtests/bench-random-lock.c > @@ -0,0 +1,109 @@ > +/* Benchmark internal libc locking functions used in random. > + Copyright (C) 2022 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 20000000 > + > +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. */ Is it worth benchmarking with some contention or is the goal just SINGLE_THREAD_P true/false? > + > + 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" > + Can you remove this extra line. ``` Applying: Add random locking benchmark .git/rebase-apply/patch:120: new blank line at EOF. + warning: 1 line adds whitespace errors. ```