Debian Codegraph @

regressions/ck_pflock/benchmark/throughput.c

raw
1 /*
2 * Copyright 2011-2015 Samy Al Bahra.
3 * Copyright 2013 John Wittrock.
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 OTHEPFISE) 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_pflock.h>
29 #include <inttypes.h>
30 #include <pthread.h>
31 #include <stdio.h>
32 #include <stdint.h>
33 #include <stdlib.h>
34 #include <unistd.h>
35
36 #include "../../common.h"
37
38 #ifndef STEPS
39 #define STEPS 1000000
40 #endif
41
42 static int barrier;
43 static int threads;
44 static unsigned int flag CK_CC_CACHELINE;
45 static ck_pflock_t pflock = CK_PFLOCK_INITIALIZER;
46 static struct affinity affinity;
47
48 static void *
49 thread_pflock(void *pun)
50 {
51 uint64_t s_b, e_b, a, i;
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_pr_inc_int(&barrier);
60 while (ck_pr_load_int(&barrier) != threads)
61 ck_pr_stall();
62
63 for (i = 1, a = 0;; i++) {
64 s_b = rdtsc();
65 ck_pflock_read_lock(&pflock);
66 ck_pflock_read_unlock(&pflock);
67 ck_pflock_read_lock(&pflock);
68 ck_pflock_read_unlock(&pflock);
69 ck_pflock_read_lock(&pflock);
70 ck_pflock_read_unlock(&pflock);
71 ck_pflock_read_lock(&pflock);
72 ck_pflock_read_unlock(&pflock);
73 ck_pflock_read_lock(&pflock);
74 ck_pflock_read_unlock(&pflock);
75 ck_pflock_read_lock(&pflock);
76 ck_pflock_read_unlock(&pflock);
77 ck_pflock_read_lock(&pflock);
78 ck_pflock_read_unlock(&pflock);
79 ck_pflock_read_lock(&pflock);
80 ck_pflock_read_unlock(&pflock);
81 ck_pflock_read_lock(&pflock);
82 ck_pflock_read_unlock(&pflock);
83 ck_pflock_read_lock(&pflock);
84 ck_pflock_read_unlock(&pflock);
85 ck_pflock_read_lock(&pflock);
86 ck_pflock_read_unlock(&pflock);
87 ck_pflock_read_lock(&pflock);
88 ck_pflock_read_unlock(&pflock);
89 ck_pflock_read_lock(&pflock);
90 ck_pflock_read_unlock(&pflock);
91 ck_pflock_read_lock(&pflock);
92 ck_pflock_read_unlock(&pflock);
93 ck_pflock_read_lock(&pflock);
94 ck_pflock_read_unlock(&pflock);
95 ck_pflock_read_lock(&pflock);
96 ck_pflock_read_unlock(&pflock);
97 e_b = rdtsc();
98
99 a += (e_b - s_b) >> 4;
100
101 if (ck_pr_load_uint(&flag) == 1)
102 break;
103 }
104
105 ck_pr_inc_int(&barrier);
106 while (ck_pr_load_int(&barrier) != threads * 2)
107 ck_pr_stall();
108
109 *value = (a / i);
110 return NULL;
111 }
112
113 int
114 main(int argc, char *argv[])
115 {
116 int t;
117 pthread_t *p;
118 uint64_t *latency;
119
120 if (argc != 3) {
121 ck_error("Usage: throughput <delta> <threads>\n");
122 }
123
124 threads = atoi(argv[2]);
125 if (threads <= 0) {
126 ck_error("ERROR: Threads must be a value > 0.\n");
127 }
128
129 p = malloc(sizeof(pthread_t) * threads);
130 if (p == NULL) {
131 ck_error("ERROR: Failed to initialize thread.\n");
132 }
133
134 latency = malloc(sizeof(uint64_t) * threads);
135 if (latency == NULL) {
136 ck_error("ERROR: Failed to create latency buffer.\n");
137 }
138
139 affinity.delta = atoi(argv[1]);
140 affinity.request = 0;
141
142 fprintf(stderr, "Creating threads (pflock)...");
143 for (t = 0; t < threads; t++) {
144 if (pthread_create(&p[t], NULL, thread_pflock, latency + t) != 0) {
145 ck_error("ERROR: Could not create thread %d\n", t);
146 }
147 }
148 fprintf(stderr, "done\n");
149
150 common_sleep(10);
151 ck_pr_store_uint(&flag, 1);
152
153 fprintf(stderr, "Waiting for threads to finish acquisition regression...");
154 for (t = 0; t < threads; t++)
155 pthread_join(p[t], NULL);
156 fprintf(stderr, "done\n\n");
157
158 for (t = 1; t <= threads; t++)
159 printf("%10u %20" PRIu64 "\n", t, latency[t - 1]);
160
161 return 0;
162 }
163