-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathengine.c
240 lines (206 loc) · 6.11 KB
/
engine.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#include "engine.h"
#include "macro.h"
#include "stats.h"
#include "struct.h"
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/epoll.h>
#include <time.h>
#include <unistd.h>
static_assert(sizeof(uint64_t) >= sizeof(size_t),
"can't express sizes on this platform");
uint64_t min(uint64_t a, uint64_t b) {
return a < b ? a : b;
}
struct entry {
enum { P, C } type;
union {
struct producer *producer;
struct consumer *consumer;
};
uint64_t offset;
bool was_busy;
bool busy;
};
uint64_t min_offset(struct entry *index, size_t num_consumers) {
uint64_t rv = UINT64_MAX;
for (size_t i = 0; i != num_consumers; ++i)
rv = min(rv, index[1+i].offset);
return rv;
}
static bool adjust_wait(int epoll_fd, struct entry *entry) {
if (entry->was_busy == entry->busy)
return true;
int fd;
uint32_t events;
int op = entry->busy ? EPOLL_CTL_ADD : EPOLL_CTL_DEL;
switch (entry->type) {
case P:
fd = CALL0(*entry->producer, get_fd);
events = CALL0(*entry->producer, get_epoll_event);
break;
case C:
fd = CALL0(*entry->consumer, get_fd);
events = CALL0(*entry->consumer, get_epoll_event);
break;
default:
assert(0);
}
struct epoll_event ev = { .events = events, .data.ptr = entry };
CHECK(SYSCALL(epoll_ctl(epoll_fd, op, fd, &ev)),
perror("epoll_ctl() failed"), return false);
entry->was_busy = entry->busy;
return true;
}
static void prepare(struct state *const state, struct entry *const index) {
index[0] = (struct entry) {
.type = P,
.producer = &state->producer,
.offset = 0,
.was_busy = false,
.busy = false
};
for (size_t i = 0; i != state->num_consumers; ++i) {
index[1+i] = (struct entry) {
.type = C,
.consumer = &state->consumers[i],
.offset = 0,
.was_busy = false,
.busy = false
};
}
}
bool transfer(size_t buffer_size, size_t block_size,
struct state *const state) {
bool rv = true;
struct entry index[1+MAX_CONSUMERS];
struct epoll_event events[1+MAX_CONSUMERS];
char *buffer = NULL;
int epoll_fd = -1;
bool eof = false;
size_t waiting = 0;
#define FAIL_IF_NOT(cond, alert) \
CHECK(cond, alert, GOTO_WITH(cleanup, rv, false))
FAIL_IF_NOT(SYSCALL(epoll_fd = epoll_create(1)),
perror("failed to create epoll fd"));
FAIL_IF_NOT(buffer = malloc(buffer_size),
ERROR("can't allocate memory for buffer"));
prepare(state, index);
for (;;) {
INC(state->stats, total_cycles);
if (waiting) {
INC(state->stats, waited_cycles);
int num_events;
num_events = epoll_wait(epoll_fd, events, waiting, -1);
if (num_events == -1 && errno == EINTR)
continue;
FAIL_IF_NOT(SYSCALL(num_events), perror("epoll_wait failed"));
for (int i = 0; i != num_events; ++i) {
struct entry *entry = events[i].data.ptr;
assert(entry->busy);
ssize_t moved;
switch (entry->type) {
case P:
moved = CALL(*entry->producer, signal, &eof);
break;
case C:
moved = CALL0(*entry->consumer, signal);
break;
default:
assert(0);
break;
}
FAIL_IF_NOT(moved != -1, ;);
entry->offset += moved;
entry->busy = false;
waiting -= 1;
}
}
{
uint64_t begin = index[0].offset;
uint64_t end = min_offset(index, state->num_consumers);
assert(begin >= end);
if (begin == end && eof)
break;
uint64_t sbegin = begin % buffer_size;
uint64_t send = end % buffer_size;
if (!index[0].busy) {
uint64_t offset = 0, size = 0;
if (sbegin > send) {
offset = sbegin;
size = buffer_size - sbegin;
} else if (sbegin < send) {
offset = sbegin;
size = send - sbegin;
} else if (begin == end) {
offset = sbegin;
size = buffer_size - sbegin;
}
if (!eof) {
if (size) {
ssize_t produced;
FAIL_IF_NOT(
(produced = CALL(*index[0].producer, produce,
buffer+offset, min(block_size, size), &eof)) != -1, ;);
waiting += (index[0].busy = (produced == 0));
index[0].offset += produced;
} else {
INC(state->stats, buffer_overruns);
for (size_t i = 0; i != state->num_consumers; ++i) {
if (index[1+i].offset == end)
INC(state->stats, consumer_slowdowns[i]);
}
}
}
FAIL_IF_NOT(adjust_wait(epoll_fd, &index[0]), ;);
}
}
{
uint64_t begin = index[0].offset;
for (size_t i = 0; i != state->num_consumers; ++i) {
if (!index[1+i].busy) {
uint64_t end = index[1+i].offset;
assert(begin >= end);
uint64_t sbegin = begin % buffer_size;
uint64_t send = end % buffer_size;
uint64_t offset = 0, size = 0;
bool clip = true;
if (sbegin > send) {
clip = false;
offset = send;
size = sbegin - send;
} else if (sbegin < send) {
offset = send;
size = buffer_size - send;
} else if (begin > end) {
offset = send;
size = buffer_size - send;
}
if (size) {
if (eof || clip ||
size >= CALL0(*index[1+i].consumer, get_lo_watermark)) {
ssize_t consumed;
FAIL_IF_NOT(
(consumed = CALL(*index[1+i].consumer, consume,
buffer+offset,
min(block_size, size))) != -1, ;);
waiting += (index[1+i].busy = (consumed == 0));
index[1+i].offset += consumed;
}
} else {
INC(state->stats, buffer_underruns);
}
FAIL_IF_NOT(adjust_wait(epoll_fd, &index[1+i]), ;);
}
}
}
}
#undef FAIL_IF_NOT
cleanup:
free(buffer);
COND_CHECK(epoll_fd, -1, SYSCALL(close(epoll_fd)),
perror("failed to close epoll fd"));
return rv;
}