public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Wilco Dijkstra <Wilco.Dijkstra@arm.com>
To: Carlos O'Donell <carlos@redhat.com>,
	'GNU C Library' <libc-alpha@sourceware.org>
Subject: Re: [PATCH v2] Add random locking benchmark
Date: Wed, 24 Aug 2022 14:43:46 +0000	[thread overview]
Message-ID: <AM5PR0801MB1668FE131D3C50C53AD08CAD83739@AM5PR0801MB1668.eurprd08.prod.outlook.com> (raw)
In-Reply-To: <6a3d9283-3866-e837-c348-6d2a4a701c4f@redhat.com>

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
+   <https://www.gnu.org/licenses/>.  */
+
+#define TEST_MAIN
+#define TEST_NAME "random-lock"
+
+#include <pthread.h>
+#include <stdio.h>
+#include <stdlib.h>
+#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.  */
+
+  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"
+


  reply	other threads:[~2022-08-24 14:44 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-22 15:14 Wilco Dijkstra
2022-08-22 21:13 ` Carlos O'Donell
2022-08-24 14:43   ` Wilco Dijkstra [this message]
2022-12-09 13:59     ` Wilco Dijkstra
2022-12-09 17:23       ` Noah Goldstein
2022-12-09 18:14         ` Wilco Dijkstra
2022-12-09 18:49           ` Noah Goldstein

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=AM5PR0801MB1668FE131D3C50C53AD08CAD83739@AM5PR0801MB1668.eurprd08.prod.outlook.com \
    --to=wilco.dijkstra@arm.com \
    --cc=carlos@redhat.com \
    --cc=libc-alpha@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).