forked from siriobalmelli/memorywell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwell_test.c
More file actions
327 lines (272 loc) · 8.14 KB
/
well_test.c
File metadata and controls
327 lines (272 loc) · 8.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/* well_test.c
Test single- and multi-threaded use of memorywell.
Used to test that data in and out of buffer is correct.
Not good for benchmarking speed: uses a fixed number of runs
(which may run terribly slowly depending on settings)
to verify data correctness.
*/
#include <well.h>
#include <well_fail.h>
#include <zed_dbg.h>
#include <stdlib.h>
#include <pthread.h>
#include <getopt.h>
#include <nonlibc.h> /* timing */
static size_t numiter = 1000000;
static size_t blk_cnt = 256; /* how many blocks in the cbuf */
const static size_t blk_size = sizeof(size_t); /* in Bytes */
static size_t tx_thread_cnt = 1;
static pthread_t *tx = NULL;
static size_t rx_thread_cnt = 1;
static pthread_t *rx = NULL;
static size_t reservation = 1; /* how many blocks to reserve at once */
static size_t waits = 0; /* how many times did threads wait? */
/* tx_thread()
*/
void *tx_single(void* arg)
{
struct well *buf = arg;
size_t tally = 0;
size_t num = numiter;
/* loop on TX */
for (size_t i=0, res=0; i < num; i += res) {
size_t ask = i + reservation < num ? reservation : num - i;
size_t pos;
while (!(res = well_reserve(&buf->tx, &pos, ask)))
FAIL_DO();
for (size_t j=0; j < res; j++)
tally += WELL_DEREF(size_t, pos, j, buf) = i + j;
well_release_single(&buf->rx, res);
}
__atomic_fetch_add(&waits, wait_count, __ATOMIC_RELAXED);
return (void *)tally;
}
void *tx_multi(void* arg)
{
struct well *buf = arg;
size_t tally = 0;
size_t num = numiter / tx_thread_cnt;
/* loop on TX */
for (size_t i=0, res=0; i < num; i += res) {
size_t ask = i + reservation < num ? reservation : num - i;
size_t pos;
while (!(res = well_reserve(&buf->tx, &pos, ask)))
FAIL_DO();
for (size_t j=0; j < res; j++)
tally += WELL_DEREF(size_t, pos, j, buf) = i + j;
while (!well_release_multi(&buf->rx, res, pos))
FAIL_DO();
}
__atomic_fetch_add(&waits, wait_count, __ATOMIC_RELAXED);
return (void *)tally;
}
/* rx_thread()
*/
void *rx_single(void* arg)
{
struct well *buf = arg;
size_t tally = 0;
size_t num = numiter;
/* loop on RX */
for (size_t i=0, res=0; i < num; i += res) {
size_t ask = i + reservation < num ? reservation : num - i;
size_t pos;
while (!(res = well_reserve(&buf->rx, &pos, ask)))
FAIL_DO();
for (size_t j=0; j < res; j++) {
size_t temp = WELL_DEREF(size_t, pos, j, buf);
tally += temp;
}
well_release_single(&buf->tx, res);
}
__atomic_fetch_add(&waits, wait_count, __ATOMIC_RELAXED);
return (void *)tally;
}
void *rx_multi(void* arg)
{
struct well *buf = arg;
size_t tally = 0;
size_t num = numiter / rx_thread_cnt;
/* loop on RX */
for (size_t i=0, res=0; i < num; i += res) {
size_t ask = i + reservation < num ? reservation : num - i;
size_t pos;
while (!(res = well_reserve(&buf->rx, &pos, ask)))
FAIL_DO();
for (size_t j=0; j < res; j++) {
size_t temp = WELL_DEREF(size_t, pos, j, buf);
tally += temp;
}
while (!well_release_multi(&buf->tx, res, pos))
FAIL_DO();
}
__atomic_fetch_add(&waits, wait_count, __ATOMIC_RELAXED);
return (void *)tally;
}
/* usage()
*/
void usage(const char *pgm_name)
{
fprintf(stderr, "Usage: %s [OPTIONS]\n\
Test MemoryWell correctness/performance.\n\
\n\
Notes:\n\
- block size is fixed at sizeof(size_t)\n\
\n\
Options:\n\
-n, --numiter <iter> : Push <iter> blocks through the buffer.\n\
-c, --count <blk_count> : How many blocks in the circular buffer.\n\
-r, --reservation <res> : (Attempt to) reserve <res> blocks at once.\n\
-t, --tx-threads : Number of TX threads.\n\
-x, --rx-threads : Number of RX threads.\n\
-h, --help : Print this message and exit.\n",
pgm_name);
}
/* main()
*/
int main(int argc, char **argv)
{
/* Use global error count so that threads can error
and directly affect the return code of main()
int err_cnt = 0;
*/
/*
options
*/
int opt = 0;
static struct option long_options[] = {
{ "numiter", required_argument, 0, 'n'},
{ "count", required_argument, 0, 'c'},
{ "reservation",required_argument, 0, 'r'},
{ "tx-threads", required_argument, 0, 't'},
{ "rx-threads", required_argument, 0, 'x'},
{ "help", no_argument, 0, 'h'}
};
while ((opt = getopt_long(argc, argv, "n:c:r:t:x:h", long_options, NULL)) != -1) {
switch(opt)
{
case 'n':
opt = sscanf(optarg, "%zu", &numiter);
Z_die_if(opt != 1, "invalid numiter '%s'", optarg);
break;
case 'c':
opt = sscanf(optarg, "%zu", &blk_cnt);
Z_die_if(opt != 1, "invalid blk_cnt '%s'", optarg);
Z_die_if(blk_cnt < 2,
"blk_cnt %zu impossible", blk_cnt);
break;
case 'r':
opt = sscanf(optarg, "%zu", &reservation);
Z_die_if(opt != 1, "invalid reservation '%s'", optarg);
Z_die_if(!reservation || reservation > blk_cnt,
"reservation %zu; blk_cnt %zu", reservation, blk_cnt);
break;
case 't':
opt = sscanf(optarg, "%zu", &tx_thread_cnt);
Z_die_if(opt != 1, "invalid tx_thread_cnt '%s'", optarg);
break;
case 'x':
opt = sscanf(optarg, "%zu", &rx_thread_cnt);
Z_die_if(opt != 1, "invalid rx_thread_cnt '%s'", optarg);
break;
case 'h':
usage(argv[0]);
goto out;
default:
usage(argv[0]);
Z_die("option '%c' invalid", opt);
}
}
/* sanity check thread counts */
Z_die_if(numiter != nm_next_mult64(numiter, tx_thread_cnt),
"numiter %zu doesn't evenly divide into %zu tx threads",
numiter, tx_thread_cnt);
Z_die_if(numiter != nm_next_mult64(numiter, rx_thread_cnt),
"numiter %zu doesn't evenly divide into %zu rx threads",
numiter, rx_thread_cnt);
/* sanity check reservation sizes */
size_t num = numiter / tx_thread_cnt;
Z_die_if(num != nm_next_mult64(num, reservation),
"TX: num %zu doesn't evenly divide into %zu reservation blocks",
num, reservation);
num = numiter / rx_thread_cnt;
Z_die_if(num != nm_next_mult64(num, reservation),
"RX: num %zu doesn't evenly divide into %zu reservation blocks",
num, reservation);
/* ... for finite values tho */
Z_die_if(numiter > 1000000000, "one billion is plenty thanks");
/* do MANY less iterations if running under Valgrind! */
const static size_t valgrind_max = 100000;
if (getenv("VALGRIND") && numiter >valgrind_max)
numiter = valgrind_max;
/* create buffer */
struct well buf = { {0} };
Z_die_if(
well_params(blk_size, blk_cnt, &buf)
, "");
Z_die_if(
well_init(&buf, malloc(well_size(&buf)))
, "size %zu", well_size(&buf));
void *(*tx_t)(void *) = tx_single;
if (tx_thread_cnt > 1)
tx_t = tx_multi;
Z_die_if(!(
tx = malloc(sizeof(pthread_t) * tx_thread_cnt)
), "");
void *(*rx_t)(void *) = rx_single;
if (rx_thread_cnt > 1)
rx_t = rx_multi;
Z_die_if(!(
rx = malloc(sizeof(pthread_t) * rx_thread_cnt)
), "");
nlc_timing_start(t);
/* fire reader-writer threads */
for (size_t i=0; i < tx_thread_cnt; i++)
pthread_create(&tx[i], NULL, tx_t, &buf);
for (size_t i=0; i < rx_thread_cnt; i++)
pthread_create(&rx[i], NULL, rx_t, &buf);
/* wait for threads to finish */
size_t tx_i_sum = 0, rx_i_sum = 0;
for (size_t i=0; i < tx_thread_cnt; i++) {
void *tmp;
pthread_join(tx[i], &tmp);
tx_i_sum += (size_t)tmp;
}
for (size_t i=0; i < rx_thread_cnt; i++) {
void *tmp;
pthread_join(rx[i], &tmp);
rx_i_sum += (size_t)tmp;
}
nlc_timing_stop(t);
/* verify sums */
Z_die_if(tx_i_sum != rx_i_sum, "%zu != %zu", tx_i_sum, rx_i_sum);
/* This is the expected sum of all 'i' loop iterators for all threads
The logic to arrive at this was:
@1 : i = 0 0/1 = 0
@2 : i = 0+1 2/1 = 0.5
@3 : i = 0+1+2 3/3 = 1
@4 : i = 0+1+2+3 6/4 = 1.5
@5 : i = 0+1+2+3+4 10/5 = 2
...
@8 : i = 0+1+2+3+4+5+6+7 28/8 = 3.5
@8 : (8-1)*0.5 = 3.5
@8 : 0+1+2+3+4+5+6+7 = (8-1)*0.5*8 = 28
*/
numiter /= tx_thread_cnt;
size_t verif_i_sum = (numiter -1) * 0.5 * numiter * tx_thread_cnt;
Z_die_if(verif_i_sum != tx_i_sum, "%zu != %zu", verif_i_sum, tx_i_sum);
/* print stats */
printf("numiter %zu; blk_size %zu; blk_count %zu; reservation %zu\n",
numiter, blk_size, blk_cnt, reservation);
printf("TX threads %zu; RX threads %zu\n",
tx_thread_cnt, rx_thread_cnt);
printf("waits: %zu\n", waits);
printf("cpu time %.4lfs; wall time %.4lfs\n",
nlc_timing_cpu(t), nlc_timing_wall(t));
out:
well_deinit(&buf);
free(well_mem(&buf));
free(tx);
free(rx);
return err_cnt;
}