Debian Codegraph @

regressions/ck_rwcohort/benchmark/latency.h

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