about summary refs log tree commit diff
path: root/benchtests/bench-random-lock.c
blob: 0fbe9181375170d32abd7fa2df7deb7e1830b277 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/* Benchmark internal libc locking functions used in random.
   Copyright (C) 2022-2024 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"
#define TEST_FUNCTION test_main
#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


/* Measure the overhead of __libc_lock_lock and __libc_lock_unlock by
   calling random ().  */
static void
bench_random_lock (json_ctx_t *json_ctx, 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
test_main (void)
{
  json_ctx_t json_ctx;

  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 (&json_ctx, 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 (&json_ctx, 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;
}

#include "support/test-driver.c"