build-many-glibcs.py: Add openrisc hard float glibc variant
[glibc.git] / benchtests / bench-random-lock.c
blob0fbe9181375170d32abd7fa2df7deb7e1830b277
1 /* Benchmark internal libc locking functions used in random.
2 Copyright (C) 2022-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 #define TEST_MAIN
20 #define TEST_NAME "random-lock"
21 #define TEST_FUNCTION test_main
22 #include <pthread.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include "bench-timing.h"
26 #include "json-lib.h"
28 /* Modern cores run 20M iterations in about 1 second. */
29 #define NUM_ITERS 50000000
32 /* Measure the overhead of __libc_lock_lock and __libc_lock_unlock by
33 calling random (). */
34 static void
35 bench_random_lock (json_ctx_t *json_ctx, size_t iters)
37 timing_t start, stop, total;
39 srandom (0);
41 /* Warmup to reduce variations due to frequency scaling. */
42 for (int i = 0; i < iters / 4; i++)
43 (void) random ();
45 TIMING_NOW (start);
47 for (int i = 0; i < iters; i++)
48 (void) random ();
50 TIMING_NOW (stop);
52 TIMING_DIFF (total, start, stop);
54 json_element_double (json_ctx, (double) total / (double) iters);
57 static void *
58 thread_start (void *p)
60 return p;
63 int
64 test_main (void)
66 json_ctx_t json_ctx;
68 json_init (&json_ctx, 0, stdout);
70 json_document_begin (&json_ctx);
72 json_attr_string (&json_ctx, "timing_type", TIMING_TYPE);
73 json_attr_object_begin (&json_ctx, "functions");
74 json_attr_object_begin (&json_ctx, "random");
75 json_attr_string (&json_ctx, "bench-variant", "single-threaded");
76 json_array_begin (&json_ctx, "results");
78 /* Run benchmark single threaded. */
79 bench_random_lock (&json_ctx, NUM_ITERS);
81 json_array_end (&json_ctx);
82 json_attr_object_end (&json_ctx);
84 json_attr_object_begin (&json_ctx, "random");
85 json_attr_string (&json_ctx, "bench-variant", "multi-threaded");
86 json_array_begin (&json_ctx, "results");
88 /* Start a short thread to force SINGLE_THREAD_P == false. This relies on
89 the runtime disabling single-threaded optimizations when multiple
90 threads are used, even after they finish. */
92 pthread_t t;
93 pthread_create (&t, NULL, thread_start, NULL);
94 pthread_join (t, NULL);
96 /* Repeat benchmark with single-threaded optimizations disabled. */
97 bench_random_lock (&json_ctx, NUM_ITERS);
99 json_array_end (&json_ctx);
100 json_attr_object_end (&json_ctx);
101 json_attr_object_end (&json_ctx);
102 json_document_end (&json_ctx);
103 return 0;
106 #include "support/test-driver.c"