Debian Codegraph @

regressions/ck_brlock/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_brlock.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 ck_brlock_t brlock = CK_BRLOCK_INITIALIZER;
45 static struct affinity affinity;
46
47 static void *
48 thread_brlock(void *pun)
49 {
50 uint64_t s_b, e_b, a, i;
51 ck_brlock_reader_t r;
52 uint64_t *value = pun;
53
54 if (aff_iterate(&affinity) != 0) {
55 perror("ERROR: Could not affine thread");
56 exit(EXIT_FAILURE);
57 }
58
59 ck_brlock_read_register(&brlock, &r);
60 ck_pr_inc_int(&barrier);
61 while (ck_pr_load_int(&barrier) != threads)
62 ck_pr_stall();
63
64 for (i = 1, a = 0;; i++) {
65 s_b = rdtsc();
66 ck_brlock_read_lock(&brlock, &r);
67 ck_brlock_read_unlock(&r);
68 ck_brlock_read_lock(&brlock, &r);
69 ck_brlock_read_unlock(&r);
70 ck_brlock_read_lock(&brlock, &r);
71 ck_brlock_read_unlock(&r);
72 ck_brlock_read_lock(&brlock, &r);
73 ck_brlock_read_unlock(&r);
74 ck_brlock_read_lock(&brlock, &r);
75 ck_brlock_read_unlock(&r);
76 ck_brlock_read_lock(&brlock, &r);
77 ck_brlock_read_unlock(&r);
78 ck_brlock_read_lock(&brlock, &r);
79 ck_brlock_read_unlock(&r);
80 ck_brlock_read_lock(&brlock, &r);
81 ck_brlock_read_unlock(&r);
82 ck_brlock_read_lock(&brlock, &r);
83 ck_brlock_read_unlock(&r);
84 ck_brlock_read_lock(&brlock, &r);
85 ck_brlock_read_unlock(&r);
86 ck_brlock_read_lock(&brlock, &r);
87 ck_brlock_read_unlock(&r);
88 ck_brlock_read_lock(&brlock, &r);
89 ck_brlock_read_unlock(&r);
90 ck_brlock_read_lock(&brlock, &r);
91 ck_brlock_read_unlock(&r);
92 ck_brlock_read_lock(&brlock, &r);
93 ck_brlock_read_unlock(&r);
94 ck_brlock_read_lock(&brlock, &r);
95 ck_brlock_read_unlock(&r);
96 ck_brlock_read_lock(&brlock, &r);
97 ck_brlock_read_unlock(&r);
98 e_b = rdtsc();
99
100 a += (e_b - s_b) >> 4;
101
102 if (ck_pr_load_uint(&flag) == 1)
103 break;
104 }
105
106 ck_pr_inc_int(&barrier);
107 while (ck_pr_load_int(&barrier) != threads * 2)
108 ck_pr_stall();
109
110 *value = (a / i);
111 return NULL;
112 }
113
114 int
115 main(int argc, char *argv[])
116 {
117 int t;
118 pthread_t *p;
119 uint64_t *latency;
120
121 if (argc != 3) {
122 ck_error("Usage: throughput <delta> <threads>\n");
123 }
124
125 threads = atoi(argv[2]);
126 if (threads <= 0) {
127 ck_error("ERROR: Threads must be a value > 0.\n");
128 }
129
130 p = malloc(sizeof(pthread_t) * threads);
131 if (p == NULL) {
132 ck_error("ERROR: Failed to initialize thread.\n");
133 }
134
135 latency = malloc(sizeof(uint64_t) * threads);
136 if (latency == NULL) {
137 ck_error("ERROR: Failed to create latency buffer.\n");
138 }
139
140 affinity.delta = atoi(argv[1]);
141 affinity.request = 0;
142
143 fprintf(stderr, "Creating threads (brlock)...");
144 for (t = 0; t < threads; t++) {
145 if (pthread_create(&p[t], NULL, thread_brlock, latency + t) != 0) {
146 ck_error("ERROR: Could not create thread %d\n", t);
147 }
148 }
149 fprintf(stderr, "done\n");
150
151 common_sleep(10);
152 ck_pr_store_uint(&flag, 1);
153
154 fprintf(stderr, "Waiting for threads to finish acquisition regression...");
155 for (t = 0; t < threads; t++)
156 pthread_join(p[t], NULL);
157 fprintf(stderr, "done\n\n");
158
159 for (t = 1; t <= threads; t++)
160 printf("%10u %20" PRIu64 "\n", t, latency[t - 1]);
161
162 return (0);
163 }
164