* [PATCH 1/2] Add random benchmark
@ 2024-03-18 15:17 Wilco Dijkstra
2024-04-16 16:55 ` Adhemerval Zanella Netto
0 siblings, 1 reply; 4+ messages in thread
From: Wilco Dijkstra @ 2024-03-18 15:17 UTC (permalink / raw)
To: 'GNU C Library'
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?
---
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.
+ 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 50000000
+
+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"
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH 1/2] Add random benchmark
2024-03-18 15:17 [PATCH 1/2] Add random benchmark Wilco Dijkstra
@ 2024-04-16 16:55 ` Adhemerval Zanella Netto
0 siblings, 0 replies; 4+ messages in thread
From: Adhemerval Zanella Netto @ 2024-04-16 16:55 UTC (permalink / raw)
To: libc-alpha, Wilco Dijkstra
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
> + <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 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.
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH 1/2] Add random benchmark
2023-11-28 17:35 Wilco Dijkstra
@ 2023-12-04 14:38 ` Carlos O'Donell
0 siblings, 0 replies; 4+ messages in thread
From: Carlos O'Donell @ 2023-12-04 14:38 UTC (permalink / raw)
To: Wilco Dijkstra, 'GNU C Library'
On 11/28/23 12:35, 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.
>
> ---
>
> diff --git a/benchtests/Makefile b/benchtests/Makefile
> index e1346bbda125be9fc2b216f9e8be3f2ee7cb0c4d..1415d20e25798871936f3e2b87f8690517239b21 100644
> --- a/benchtests/Makefile
> +++ b/benchtests/Makefile
> @@ -247,6 +247,7 @@ hash-benchset := \
> stdlib-benchset := \
> arc4random \
> strtod \
> + random-lock \
Fails pre-commit CI.
https://patchwork.sourceware.org/project/glibc/patch/PAWPR08MB8982D7F540068200C32DA70983BCA@PAWPR08MB8982.eurprd08.prod.outlook.com/
This inserts a routine in a non-sorted order in the alpha-sorted list.
lint-makefiles should fail for you?
> # 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..ff11f807359c05043366c855751a97a383b1b132
> --- /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.
> + 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 40000000
> +
> +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"
>
>
>
--
Cheers,
Carlos.
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH 1/2] Add random benchmark
@ 2023-11-28 17:35 Wilco Dijkstra
2023-12-04 14:38 ` Carlos O'Donell
0 siblings, 1 reply; 4+ messages in thread
From: Wilco Dijkstra @ 2023-11-28 17:35 UTC (permalink / raw)
To: 'GNU C Library'
Add a simple benchmark to measure the overhead of internal libc locks in
the random() implementation on both single- and multi-threaded cases.
---
diff --git a/benchtests/Makefile b/benchtests/Makefile
index e1346bbda125be9fc2b216f9e8be3f2ee7cb0c4d..1415d20e25798871936f3e2b87f8690517239b21 100644
--- a/benchtests/Makefile
+++ b/benchtests/Makefile
@@ -247,6 +247,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..ff11f807359c05043366c855751a97a383b1b132
--- /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.
+ 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 40000000
+
+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"
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2024-04-16 16:55 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-03-18 15:17 [PATCH 1/2] Add random benchmark Wilco Dijkstra
2024-04-16 16:55 ` Adhemerval Zanella Netto
-- strict thread matches above, loose matches on Subject: below --
2023-11-28 17:35 Wilco Dijkstra
2023-12-04 14:38 ` Carlos O'Donell
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).