From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pf1-x42a.google.com (mail-pf1-x42a.google.com [IPv6:2607:f8b0:4864:20::42a]) by sourceware.org (Postfix) with ESMTPS id 445D33858D35 for ; Tue, 16 Apr 2024 16:55:15 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 445D33858D35 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 445D33858D35 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::42a ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1713286517; cv=none; b=pMsrOZZU1HC8rmJxO++VdplPsvPMcIoXs/ItST0wz2jVcck2t7yZndNTDHv0eOStTULcWcWu2CeCmaoPExgiUCOKzLLhzO2T7cjCt4coExgMeii9LsY58YjchKfNKTbNbRwnMNlReVGQyDWA+geZfgJfJG67X7b41ehwrtN2kV8= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1713286517; c=relaxed/simple; bh=YRh9KWU/ToErooGwY2NmZNDG1UFw5UB+1Z4MsYS1tkE=; h=DKIM-Signature:Message-ID:Date:MIME-Version:Subject:To:From; b=lRAFK5jKZQuG2l3z7B/iTmK9fZAnShVQyvmx16jWdvc5CZ4ln1Hwf0mN7D5owZTc8lzouVJWcqCnh/apIpop8pvfqE3O9Ua+inRT+vOP3EOFMhny/OPSqsUiMqnOXO+2qJ+jZbusrjmSzBL5csrXHQY4av6wlZD0vLOuuVg46rw= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pf1-x42a.google.com with SMTP id d2e1a72fcca58-6edc61d0ff6so4248996b3a.2 for ; Tue, 16 Apr 2024 09:55:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1713286513; x=1713891313; darn=sourceware.org; h=content-transfer-encoding:in-reply-to:organization:from :content-language:references:to:subject:user-agent:mime-version:date :message-id:from:to:cc:subject:date:message-id:reply-to; bh=A1vZ1Jq1NRatYaYs7wCdoQ2XGm8EBudTNZyHrqlGu/I=; b=AzRUkvGKsBm6K3Z1JKk30FoUtiMABJ+UjGXnGfogs1eHq2n1i+6pilOUkHYpfHS9sV 6LDUjkjoJhjjnO9VVhpeFcTnAfYTCChB9MlJtv8SxlAehkEznR64Su+YlR+lkLpzUwH8 f2wKaPVBKISjAQdcIt5HD5VG3QRXudwCduStwjBQJfXyTZs1OfHtoib+dtwi3Kd7AkEy aCxHHCR9xjPjuDERwdnHF+ITncjEJniAq08PMp2YFgHwLKlPpyrdhmhtndG0H6xP3M3A R3e3Xtc09/fyhKZRvwfQ8OHXXPdY+/k1SRRkEIuXdJeUwFCGghFOG6Siz05yLUeTEGLj gzaQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713286513; x=1713891313; h=content-transfer-encoding:in-reply-to:organization:from :content-language:references:to:subject:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=A1vZ1Jq1NRatYaYs7wCdoQ2XGm8EBudTNZyHrqlGu/I=; b=PevX6xWSU9tpppQvl0uOFKKVTWNTKZOC+JZHtJkZmlyr/kt6Aaz+jG+ppP43R3ZlQS BZ/JtymYcIKBPTWJ1b0LxK65/3iZ4UYUh0rTp5nGtmLyxz7vPXOZPSzizzjdUxnKyjmZ 7x4WPxGpvApzbYJR1GvvFp2EiY9LAY+v+6C8uf9Se6TFRCdb61bhiZ4U3HzYXw91ZHma NcBYLfo71puiI2zsdamiEjZMzJTezfx+dBOueb06BZYxwpASiowI3cq9oR4Si1MymdnB +anetJNZ9iQiDSqaITgyw9LTIWcIUmWWhL4XrWN7tw6DgHlIbmg5RPNjzCDu77kLbDgk tjHA== X-Gm-Message-State: AOJu0Yza40vQMJ3tDu8FTHrM2ef+p/K1Vjm4Aq/EwRoJ1B3uemz80D9t uwFyvpjs3Js9xB0AbbBn0rzUlt/f2p3J+Lxb8HVRLAEQmi57hvgLYIFtxoqpTY1I4wenDxgo5EJ I X-Google-Smtp-Source: AGHT+IHccrGMg90oSPi+FhHs/gCQUfQVXzOE6u6YacZCGCBfRA5Ltcdi9ThaAeiXclR1w+DzOxk9AA== X-Received: by 2002:a05:6a20:3943:b0:1a7:892b:5f89 with SMTP id r3-20020a056a20394300b001a7892b5f89mr19056969pzg.11.1713286513504; Tue, 16 Apr 2024 09:55:13 -0700 (PDT) Received: from ?IPV6:2804:1b3:a7c0:d8e5:34bc:e1c9:2b45:c06a? ([2804:1b3:a7c0:d8e5:34bc:e1c9:2b45:c06a]) by smtp.gmail.com with ESMTPSA id le3-20020a056a004fc300b006eac4297ed3sm9180075pfb.20.2024.04.16.09.55.11 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 16 Apr 2024 09:55:12 -0700 (PDT) Message-ID: <01232f1a-d1d6-48a5-8664-1bd36c3c39d3@linaro.org> Date: Tue, 16 Apr 2024 13:55:10 -0300 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH 1/2] Add random benchmark To: libc-alpha@sourceware.org, Wilco Dijkstra References: Content-Language: en-US From: Adhemerval Zanella Netto Organization: Linaro In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-12.2 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,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 18/03/24 12:17, 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. > 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? Ok with the changes below. > > --- > > 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. s/2023/s2024. > + 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; Maybe static here. > + > + > +/* 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" > Use support/test-driver.c here.