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/>. */
20 #define TEST_NAME "random-lock"
21 #define TEST_FUNCTION test_main
25 #include "bench-timing.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
35 bench_random_lock (json_ctx_t
*json_ctx
, size_t iters
)
37 timing_t start
, stop
, total
;
41 /* Warmup to reduce variations due to frequency scaling. */
42 for (int i
= 0; i
< iters
/ 4; i
++)
47 for (int i
= 0; i
< iters
; i
++)
52 TIMING_DIFF (total
, start
, stop
);
54 json_element_double (json_ctx
, (double) total
/ (double) iters
);
58 thread_start (void *p
)
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. */
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
);
106 #include "support/test-driver.c"