-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy patht_stress.c
217 lines (190 loc) · 4.51 KB
/
t_stress.c
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
/*
* Copyright (c) 2017 Mindaugas Rasiukevicius <rmind at netbsd org>
* All rights reserved.
*
* Use is subject to license terms, as specified in the LICENSE file.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <inttypes.h>
#include <string.h>
#include <unistd.h>
#include <limits.h>
#include <signal.h>
#include <pthread.h>
#include <assert.h>
#include <errno.h>
#include <err.h>
#include "ringbuf.h"
#include "utils.h"
static unsigned nsec = 10; /* seconds */
static pthread_barrier_t barrier;
static unsigned nworkers;
static volatile bool stop;
static ringbuf_t * ringbuf;
static size_t ringbuf_obj_size;
__thread uint32_t fast_random_seed = 5381;
#define RBUF_SIZE (512)
#define MAGIC_BYTE (0x5a)
/* Note: leave one byte for the magic byte. */
static uint8_t rbuf[RBUF_SIZE + 1];
/*
* Simple xorshift; random() causes huge lock contention on Linux/glibc,
* which would "hide" the possible race conditions.
*/
static unsigned long
fast_random(void)
{
uint32_t x = fast_random_seed;
x ^= x << 13;
x ^= x >> 17;
x ^= x << 5;
fast_random_seed = x;
return x;
}
/*
* Generate a random message of a random length (up to the given size)
* and simple XOR based checksum. The first byte is reserved for the
* message length and the last byte is reserved for a checksum.
*/
static size_t
generate_message(unsigned char *buf, size_t buflen)
{
const unsigned len = fast_random() % (buflen - 2);
unsigned i = 1, n = len;
unsigned char cksum = 0;
while (n--) {
buf[i] = '!' + (fast_random() % ('~' - '!'));
cksum ^= buf[i];
i++;
}
/*
* Write the length and checksum last, trying to exploit a
* possibility of a race condition. NOTE: depending on an
* architecture, might want to try a memory barrier here.
*/
buf[i++] = cksum;
buf[0] = len;
return i;
}
/*
* Take an arbitrary message of a variable length and verify its checksum.
*/
static ssize_t
verify_message(const unsigned char *buf)
{
unsigned i = 1, len = (unsigned char)buf[0];
unsigned char cksum = 0;
while (len--) {
cksum ^= buf[i++];
}
if (buf[i] != cksum) {
return -1;
}
return (unsigned)buf[0] + 2;
}
static void *
ringbuf_stress(void *arg)
{
const unsigned id = (uintptr_t)arg;
ringbuf_worker_t *w;
w = ringbuf_register(ringbuf, id);
assert(w != NULL);
/*
* There are NCPU threads concurrently generating and producing
* random messages and a single consumer thread (ID 0) verifying
* and releasing the messages.
*/
pthread_barrier_wait(&barrier);
while (!stop) {
unsigned char buf[MIN((1 << CHAR_BIT), RBUF_SIZE)];
size_t len, off;
ssize_t ret;
/* Check that the buffer is never overrun. */
assert(rbuf[RBUF_SIZE] == MAGIC_BYTE);
if (id == 0) {
if ((len = ringbuf_consume(ringbuf, &off)) != 0) {
size_t rem = len;
assert(off < RBUF_SIZE);
while (rem) {
ret = verify_message(&rbuf[off]);
assert(ret > 0);
assert(ret <= (ssize_t)rem);
off += ret, rem -= ret;
}
ringbuf_release(ringbuf, len);
}
continue;
}
len = generate_message(buf, sizeof(buf) - 1);
if ((ret = ringbuf_acquire(ringbuf, w, len)) != -1) {
off = (size_t)ret;
assert(off < RBUF_SIZE);
memcpy(&rbuf[off], buf, len);
ringbuf_produce(ringbuf, w);
}
}
pthread_barrier_wait(&barrier);
pthread_exit(NULL);
return NULL;
}
static void
ding(int sig)
{
(void)sig;
stop = true;
}
static void
run_test(void *func(void *))
{
struct sigaction sigalarm;
pthread_t *thr;
int ret;
/*
* Setup the threads.
*/
nworkers = sysconf(_SC_NPROCESSORS_CONF) + 1;
thr = calloc(nworkers, sizeof(pthread_t));
pthread_barrier_init(&barrier, NULL, nworkers);
stop = false;
memset(&sigalarm, 0, sizeof(struct sigaction));
sigalarm.sa_handler = ding;
ret = sigaction(SIGALRM, &sigalarm, NULL);
assert(ret == 0); (void)ret;
/*
* Create a ring buffer.
*/
ringbuf_get_sizes(nworkers, &ringbuf_obj_size, NULL);
ringbuf = malloc(ringbuf_obj_size);
assert(ringbuf != NULL);
ringbuf_setup(ringbuf, nworkers, RBUF_SIZE);
memset(rbuf, MAGIC_BYTE, sizeof(rbuf));
/*
* Spin the test.
*/
alarm(nsec);
for (unsigned i = 0; i < nworkers; i++) {
if ((errno = pthread_create(&thr[i], NULL,
func, (void *)(uintptr_t)i)) != 0) {
err(EXIT_FAILURE, "pthread_create");
}
}
for (unsigned i = 0; i < nworkers; i++) {
pthread_join(thr[i], NULL);
}
pthread_barrier_destroy(&barrier);
free(ringbuf);
free(thr);
}
int
main(int argc, char **argv)
{
if (argc >= 2) {
nsec = (unsigned)atoi(argv[1]);
}
puts("stress test");
run_test(ringbuf_stress);
puts("ok");
return 0;
}