diff mbox series

[v2] Add random locking benchmark

Message ID AM5PR0801MB1668AC4129F20190BE84C37D83719@AM5PR0801MB1668.eurprd08.prod.outlook.com
State New
Headers show
Series [v2] Add random locking benchmark | expand

Commit Message

Wilco Dijkstra Aug. 22, 2022, 3:14 p.m. UTC
v2: rename as suggested, use random throughout

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.

---

Comments

Carlos O'Donell Aug. 22, 2022, 9:13 p.m. UTC | #1
On 8/22/22 11:14, Wilco Dijkstra via Libc-alpha wrote:
> v2: rename as suggested, use random throughout
> 
> 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.

Suggest:
                         ... This relies on the implementation of random
using internal locks to access shared global data, and that the runtime
enables locking once a single thread has been created.

~~~

I think this needs a few more explanatory comments. A lot of other people
look at our benchmarks and might scratch their heads at this one.

Post a v3 and I'll ACK that.

> 
> ---
> 
> 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 \

OK.

>    # 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..13c8e77cbd3538bb1a15f81def0a2c41a3763729
> --- /dev/null
> +++ b/benchtests/bench-random-lock.c
> @@ -0,0 +1,102 @@
> +/* Benchmark internal libc locking functions used in random.

OK.

> +   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"
> +

We could run the benchmark for a fixed amount of time, rather than a
fixed number of iterations, see bench-skeleton.c and DURATION.

Suggest:

/* Arbitrarily pick 20M such that we can measure the time taken.  */

> +#define NUM_ITERS 20000000

OK.

> +
> +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);

OK. Seed to 0.


Suggest:

/* Warmup. Arbitrarily pick 1/4 of iterations.  */

> +  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);

OK.

> +
> +  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");
> +
> +  pthread_t t;

Suggest:

/* We assume the runtimes decision to keep the library multi-threaded
   once a single thread has launched, and not change back to
   single-threaded.  */

Provides a little more context to this section, even though
you mention 'SINGLE_THREAD_P == false' later.

> +  pthread_create (&t, NULL, thread_start, NULL);
> +  pthread_join (t, NULL);

OK. We are now in multi-threaded and will never switch back to the
single threaded case.

> +
> +  /* Repeat benchmark now SINGLE_THREAD_P == false.  */
> +  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"
> +
> 
> 
> 
>
Wilco Dijkstra Aug. 24, 2022, 2:43 p.m. UTC | #2
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"
+
Wilco Dijkstra Dec. 9, 2022, 1:59 p.m. UTC | #3
ping

From: Wilco Dijkstra <Wilco.Dijkstra@arm.com>
Sent: 24 August 2022 15:43
To: Carlos O'Donell <carlos@redhat.com>; 'GNU C Library' <libc-alpha@sourceware.org>
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
+   <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"
+
Noah Goldstein Dec. 9, 2022, 5:23 p.m. UTC | #4
On Fri, Dec 9, 2022 at 5:59 AM Wilco Dijkstra via Libc-alpha
<libc-alpha@sourceware.org> wrote:
>
>
> ping
>
> From: Wilco Dijkstra <Wilco.Dijkstra@arm.com>
> Sent: 24 August 2022 15:43
> To: Carlos O'Donell <carlos@redhat.com>; 'GNU C Library' <libc-alpha@sourceware.org>
> 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
> +   <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.  */

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.
```
Wilco Dijkstra Dec. 9, 2022, 6:14 p.m. UTC | #5
Hi Noah,

> +  /* 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?

The latter, the goal is just to test singlethreaded is faster and multithreaded
is not slower. We could add a benchmark for the internal locks (which might
test contention cases), but trying to use the internal headers in benchtests
seems non-trivial.

> +
> Can you remove this extra line.

I don't remember ever seeing that, another oddity of git apply...
I'll remove it in the next version.

Cheers,
Wilco
Noah Goldstein Dec. 9, 2022, 6:49 p.m. UTC | #6
On Fri, Dec 9, 2022 at 10:15 AM Wilco Dijkstra <Wilco.Dijkstra@arm.com> wrote:
>
> Hi Noah,
>
> > +  /* 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?
>
> The latter, the goal is just to test singlethreaded is faster and multithreaded
> is not slower. We could add a benchmark for the internal locks (which might
> test contention cases), but trying to use the internal headers in benchtests
> seems non-trivial.
>
Fair enough (reviewed this patch w.o seeing your next one initially so
was missing the context).
> > +
> > Can you remove this extra line.
>
> I don't remember ever seeing that, another oddity of git apply...
> I'll remove it in the next version.
>
> Cheers,
> Wilco
diff mbox series

Patch

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..13c8e77cbd3538bb1a15f81def0a2c41a3763729
--- /dev/null
+++ b/benchtests/bench-random-lock.c
@@ -0,0 +1,102 @@ 
+/* 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"
+
+#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);
+  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");
+
+  pthread_t t;
+  pthread_create (&t, NULL, thread_start, NULL);
+  pthread_join (t, NULL);
+
+  /* Repeat benchmark now SINGLE_THREAD_P == false.  */
+  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"
+