Debian Codegraph @

regressions/ck_swlock/benchmark/throughput.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_swlock.h>
28 #include <inttypes.h>
29 #include <pthread.h>
30 #include <stdio.h>
31 #include <stdint.h>
32 #include <stdlib.h>
33 #include <unistd.h>
34
35 #include "../../common.h"
36
37 #ifndef STEPS
38 #define STEPS 1000000
39 #endif
40
41 static int barrier;
42 static int threads;
43 static unsigned int flag CK_CC_CACHELINE;
44 static struct {
45 ck_swlock_t lock;
46 } rw CK_CC_CACHELINE = {
47 .lock = CK_SWLOCK_INITIALIZER
48 };
49
50 static struct affinity affinity;
51
52 static void *
53 thread_lock(void *pun)
54 {
55 uint64_t s_b, e_b, a, i;
56 uint64_t *value = pun;
57
58 if (aff_iterate(&affinity) != 0) {
59 perror("ERROR: Could not affine thread");
60 exit(EXIT_FAILURE);
61 }
62
63 ck_pr_inc_int(&barrier);
64 while (ck_pr_load_int(&barrier) != threads)
65 ck_pr_stall();
66
67 for (i = 1, a = 0;; i++) {
68 s_b = rdtsc();
69 ck_swlock_read_lock(&rw.lock);
70 ck_swlock_read_unlock(&rw.lock);
71 ck_swlock_read_lock(&rw.lock);
72 ck_swlock_read_unlock(&rw.lock);
73 ck_swlock_read_lock(&rw.lock);
74 ck_swlock_read_unlock(&rw.lock);
75 ck_swlock_read_lock(&rw.lock);
76 ck_swlock_read_unlock(&rw.lock);
77 ck_swlock_read_lock(&rw.lock);
78 ck_swlock_read_unlock(&rw.lock);
79 ck_swlock_read_lock(&rw.lock);
80 ck_swlock_read_unlock(&rw.lock);
81 ck_swlock_read_lock(&rw.lock);
82 ck_swlock_read_unlock(&rw.lock);
83 ck_swlock_read_lock(&rw.lock);
84 ck_swlock_read_unlock(&rw.lock);
85 ck_swlock_read_lock(&rw.lock);
86 ck_swlock_read_unlock(&rw.lock);
87 ck_swlock_read_lock(&rw.lock);
88 ck_swlock_read_unlock(&rw.lock);
89 ck_swlock_read_lock(&rw.lock);
90 ck_swlock_read_unlock(&rw.lock);
91 ck_swlock_read_lock(&rw.lock);
92 ck_swlock_read_unlock(&rw.lock);
93 ck_swlock_read_lock(&rw.lock);
94 ck_swlock_read_unlock(&rw.lock);
95 ck_swlock_read_lock(&rw.lock);
96 ck_swlock_read_unlock(&rw.lock);
97 ck_swlock_read_lock(&rw.lock);
98 ck_swlock_read_unlock(&rw.lock);
99 ck_swlock_read_lock(&rw.lock);
100 ck_swlock_read_unlock(&rw.lock);
101 e_b = rdtsc();
102
103 a += (e_b - s_b) >> 4;
104
105 if (ck_pr_load_uint(&flag) == 1)
106 break;
107 }
108
109 ck_pr_inc_int(&barrier);
110 while (ck_pr_load_int(&barrier) != threads * 2)
111 ck_pr_stall();
112
113 *value = (a / i);
114 return NULL;
115 }
116
117 static void
118 swlock_test(pthread_t *p, int d, uint64_t *latency, void *(*f)(void *), const char *label)
119 {
120 int t;
121
122 ck_pr_store_int(&barrier, 0);
123 ck_pr_store_uint(&flag, 0);
124
125 affinity.delta = d;
126 affinity.request = 0;
127
128 fprintf(stderr, "Creating threads (%s)...", label);
129 for (t = 0; t < threads; t++) {
130 if (pthread_create(&p[t], NULL, f, latency + t) != 0) {
131 ck_error("ERROR: Could not create thread %d\n", t);
132 }
133 }
134 fprintf(stderr, "done\n");
135
136 common_sleep(10);
137 ck_pr_store_uint(&flag, 1);
138
139 fprintf(stderr, "Waiting for threads to finish acquisition regression...");
140 for (t = 0; t < threads; t++)
141 pthread_join(p[t], NULL);
142 fprintf(stderr, "done\n\n");
143
144 for (t = 1; t <= threads; t++)
145 printf("%10u %20" PRIu64 "\n", t, latency[t - 1]);
146
147 fprintf(stderr, "\n");
148 return;
149 }
150
151
152 int
153 main(int argc, char *argv[])
154 {
155 int d;
156 pthread_t *p;
157 uint64_t *latency;
158
159 if (argc != 3) {
160 ck_error("Usage: throughput <delta> <threads>\n");
161 }
162
163 threads = atoi(argv[2]);
164 if (threads <= 0) {
165 ck_error("ERROR: Threads must be a value > 0.\n");
166 }
167
168 p = malloc(sizeof(pthread_t) * threads);
169 if (p == NULL) {
170 ck_error("ERROR: Failed to initialize thread.\n");
171 }
172
173 latency = malloc(sizeof(uint64_t) * threads);
174 if (latency == NULL) {
175 ck_error("ERROR: Failed to create latency buffer.\n");
176 }
177
178 d = atoi(argv[1]);
179 swlock_test(p, d, latency, thread_lock, "swlock");
180
181 return 0;
182 }
183