Debian Codegraph @

regressions/ck_brlock/benchmark/latency.c

raw
1 /*
2 * Copyright 2011-2015 Samy Al Bahra.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27 #include <ck_brlock.h>
28 #include <ck_rwlock.h>
29 #include <inttypes.h>
30 #include <stdio.h>
31
32 #include "../../common.h"
33
34 #ifndef STEPS
35 #define STEPS 1000000
36 #endif
37
38 int
39 main(void)
40 {
41 uint64_t s_b, e_b, i;
42 ck_brlock_t brlock = CK_BRLOCK_INITIALIZER;
43 ck_brlock_reader_t r[8];
44 ck_rwlock_t naive;
45
46 for (i = 0; i < sizeof(r) / sizeof(*r); i++)
47 ck_brlock_read_register(&brlock, &r[i]);
48
49 for (i = 0; i < STEPS; i++) {
50 ck_brlock_write_lock(&brlock);
51 ck_brlock_write_unlock(&brlock);
52 }
53
54 s_b = rdtsc();
55 for (i = 0; i < STEPS; i++) {
56 ck_brlock_write_lock(&brlock);
57 ck_brlock_write_unlock(&brlock);
58 }
59 e_b = rdtsc();
60 printf("WRITE: brlock %15" PRIu64 "\n", (e_b - s_b) / STEPS);
61
62 ck_rwlock_init(&naive);
63 for (i = 0; i < STEPS; i++) {
64 ck_rwlock_write_lock(&naive);
65 ck_rwlock_write_unlock(&naive);
66 }
67
68 s_b = rdtsc();
69 for (i = 0; i < STEPS; i++) {
70 ck_rwlock_write_lock(&naive);
71 ck_rwlock_write_unlock(&naive);
72 }
73 e_b = rdtsc();
74 printf("WRITE: naive %15" PRIu64 "\n", (e_b - s_b) / STEPS);
75
76 for (i = 0; i < STEPS; i++) {
77 ck_brlock_read_lock(&brlock, &r[0]);
78 ck_brlock_read_unlock(&r[0]);
79 }
80 s_b = rdtsc();
81 for (i = 0; i < STEPS; i++) {
82 ck_brlock_read_lock(&brlock, &r[0]);
83 ck_brlock_read_unlock(&r[0]);
84 }
85 e_b = rdtsc();
86 printf("READ: brlock %15" PRIu64 "\n", (e_b - s_b) / STEPS);
87
88 for (i = 0; i < STEPS; i++) {
89 ck_rwlock_read_lock(&naive);
90 ck_rwlock_read_unlock(&naive);
91 }
92
93 s_b = rdtsc();
94 for (i = 0; i < STEPS; i++) {
95 ck_rwlock_read_lock(&naive);
96 ck_rwlock_read_unlock(&naive);
97 }
98 e_b = rdtsc();
99 printf("READ: naive %15" PRIu64 "\n", (e_b - s_b) / STEPS);
100
101 return (0);
102 }
103