From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1895) id 380F13858D38; Thu, 18 Apr 2024 13:31:02 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 380F13858D38 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1713447062; bh=zw+f242Bd4LLj5Fyy0PXajVLTx3nE6ZMCpCKRGF7oA0=; h=From:To:Subject:Date:From; b=fXba+U+fJFZtNu6a9rSxS6VvdgoSgfKJC9XjF6rlNLiXqdFu1qR3fddmBx4foWw1d AeEs0NA4a5H/ce3dSl9gt4+khEn4xZKGe+k+3ybTkPNImxXRXO9wKiL0YaPeH4su9V 6lsTK1Teu+kyAoRksQR0wRUB9VJJ77G7EJ26Q8RI= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Wilco Dijkstra To: glibc-cvs@sourceware.org Subject: [glibc] benchtests: Add random() benchmark X-Act-Checkin: glibc X-Git-Author: Wilco Dijkstra X-Git-Refname: refs/heads/master X-Git-Oldrev: 05c3495296bc38c3c46117c45ee6e663581e3370 X-Git-Newrev: 0997c3d0c87433ac8c78043aaa9b6b7e91df2882 Message-Id: <20240418133102.380F13858D38@sourceware.org> Date: Thu, 18 Apr 2024 13:31:02 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=0997c3d0c87433ac8c78043aaa9b6b7e91df2882 commit 0997c3d0c87433ac8c78043aaa9b6b7e91df2882 Author: Wilco Dijkstra Date: Tue Nov 28 13:33:56 2023 +0000 benchtests: Add random() benchmark 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). Reviewed-by: Adhemerval Zanella Diff: --- benchtests/Makefile | 1 + benchtests/bench-random-lock.c | 106 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+) diff --git a/benchtests/Makefile b/benchtests/Makefile index 05b8751c55..7e73b8504e 100644 --- a/benchtests/Makefile +++ b/benchtests/Makefile @@ -257,6 +257,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 0000000000..0fbe918137 --- /dev/null +++ b/benchtests/bench-random-lock.c @@ -0,0 +1,106 @@ +/* Benchmark internal libc locking functions used in random. + Copyright (C) 2022-2024 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" +#define TEST_FUNCTION test_main +#include +#include +#include +#include "bench-timing.h" +#include "json-lib.h" + +/* Modern cores run 20M iterations in about 1 second. */ +#define NUM_ITERS 50000000 + + +/* Measure the overhead of __libc_lock_lock and __libc_lock_unlock by + calling random (). */ +static void +bench_random_lock (json_ctx_t *json_ctx, 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 +test_main (void) +{ + json_ctx_t json_ctx; + + 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 (&json_ctx, 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 (&json_ctx, 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; +} + +#include "support/test-driver.c"