-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuffer.c
316 lines (286 loc) · 7.85 KB
/
buffer.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
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
/*
* Copyright 2012+ Michal Soltys <[email protected]>
*
* This file is part of Yancat.
*
* Yancat is free software: you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* Yancat is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* Yancat. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <limits.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <stdio.h>
#include "common.h"
#include "buffer.h"
#include "crc.h"
/*
* we require sharp inequalities here, as otherwise we could end (after
* reading) with got == did, and that implies empty buffer
*/
size_t buf_can_r(struct buf_s *restrict buf)
{
size_t emp;
emp = (buf->did - buf->got) & buf->mask;
if unlikely(!emp)
emp = buf->size;
if unlikely(buf->rstall) {
/* hav = (~emp + 1) & mask; equivalent of hav > buf->rsp */
if likely(emp < buf->rsp_inv)
return 0;
DEB2("read resumed: fill %zu\n", (buf->got - buf->did) & buf->mask);
buf->rstall = 0;
/* min. free space is guaranteed after options' parsing */
return buf->rblk;
}
if likely(emp > buf->rblk)
return buf->rblk;
if likely(emp > buf->rmin)
return emp;
if unlikely(buf->rsp) {
/* overrun, back off */
buf->rstall = 1;
DEB2("read stalled: fill %zu\n", (buf->got - buf->did) & buf->mask);
}
return 0;
}
size_t buf_can_w(struct buf_s * restrict buf)
{
size_t hav;
hav = (buf->got - buf->did) & buf->mask;
if unlikely(buf->wstall) {
if likely(hav < buf->wsp)
return 0;
DEB2("write resumed: fill %zu\n", hav);
buf->wstall = 0;
/* min. free space is guaranteed after options' parsing */
return buf->wblk;
}
if likely(hav >= buf->wblk)
return buf->wblk;
if likely(hav >= buf->wmin)
return hav;
if unlikely(buf->wsp) {
/* underrun, back off */
buf->wstall = 1;
DEB2("write stalled: fill %zu\n", hav);
}
return 0;
}
void buf_dt(struct buf_s *buf)
{
if (!buf)
return;
shmw_dt(&buf->buf);
shmw_dt(&buf->scr);
}
void buf_dtor(struct buf_s *buf)
{
if (!buf)
return;
shmw_dtor(&buf->buf);
shmw_dtor(&buf->scr);
memset(buf, 0, sizeof *buf);
}
int buf_ctor(struct buf_s *buf, size_t bsiz, size_t rblk, size_t wblk, size_t hpage)
{
int ret, idx;
size_t csiz, page = get_page(), blk;
if (!buf) {
fputs("buf: no buf ?\n", stderr);
return -1;
}
memset(buf, 0, sizeof *buf);
if ((ret = shmw_ctor(&buf->buf, "/yancat-buf-area", &bsiz, hpage, 1, 0)) < 0) {
fputs("buf: failed to allocate [shared] memory buffer area\n", stderr);
return -1;
}
if ((idx = is_pow2(bsiz)) < 0) {
fprintf(stderr, "buf: aligned buffer size must be power of 2, but is: 0x%zX\n", bsiz);
goto out;
}
buf->ptr = shmw_ptr(&buf->buf);
buf->size = bsiz;
buf->mask = (((size_t)1 << idx) - 1);
if (ret != 1)
buf->flags |= M_SHM;
if (hpage > page)
buf->flags |= M_HUGE;
if (shmw_cir(&buf->buf) >= 0) {
buf->flags |= M_CIR;
buf->iscir = 1;
}
// TODO sharp should be enough ... verify this (carefully)
blk = Y_MAX(rblk, wblk);
if (bsiz <= 2*blk) {
fputs("Buffer size must be greater than 2*max(rblk, wblk),\n"
" to avoid corner cases.\n", stderr);
goto out;
}
csiz = Y_ALIGN(rblk, page) + Y_ALIGN(wblk, page);
if (shmw_ctor(&buf->scr, "/yancat-bounce-area", &csiz, 0, 0, 0) < 0) {
fputs("buf: failed to allocate shared memory bounce area\n", stderr);
goto out;
}
buf->rchunk = shmw_ptr(&buf->scr);
buf->wchunk = buf->rchunk + Y_ALIGN(rblk, page);
buf->rblk = rblk;
buf->wblk = wblk;
return ret;
out:
shmw_dtor(&buf->buf);
return -1;
}
void ibuf_commit_rbounce(struct buf_s *restrict buf, size_t chunk)
{
size_t siz1, siz2;
/* we can't assume we're at the edge, as read may return less (even if buf_fetch_r() checked for it) */
siz1 = Y_MIN(buf->size - buf->got, chunk);
siz2 = chunk - siz1;
memcpy(buf->ptr + buf->got, buf->rchunk, siz1);
if likely(siz2)
memcpy(buf->ptr, buf->rchunk + siz1, siz2);
if unlikely(buf->dorcrc)
buf->rcrc = crc_calc(buf->rcrc, buf->rchunk, chunk);
}
/* fetch can be forced, we can't assume that it's at the edge */
uint8_t *ibuf_fetch_wbounce(struct buf_s *restrict buf, size_t chunk)
{
size_t siz1, siz2;
siz1 = Y_MIN(buf->size - buf->did, chunk);
siz2 = chunk - siz1;
memcpy(buf->wchunk, buf->ptr + buf->did, siz1);
if likely(siz2)
memcpy(buf->wchunk + siz1, buf->ptr, siz2);
buf->fastw = 0;
return buf->wchunk;
}
void buf_setlinew(struct buf_s *buf)
{
buf->flags |= M_LINEW;
buf->wmin = 1;
buf->wsp = 0;
#if DEBUG == 2
if (buf->wstall)
DEB2("write forcibly (epilogue) resumed: fill %zu\n", (buf->got - buf->did) & buf->mask);
#endif
buf->wstall = 0;
}
int buf_setextra(struct buf_s *buf, int rline, int wline, int rcrc, int wcrc, double rs, double ws)
{
size_t rsp, wsp;
if (rline) {
buf->flags |= M_LINER;
buf->rmin = 1;
} else
buf->rmin = buf->rblk;
if (wline) {
buf->flags |= M_LINEW;
buf->wmin = 1;
} else
buf->wmin = buf->wblk;
if (rcrc) {
buf->rcrc = crc_beg();
buf->dorcrc = 1;
}
if (wcrc) {
buf->wcrc = crc_beg();
buf->dowcrc = 1;
}
rsp = (size_t)(0.5 + rs*(double)buf->size);
if (rsp) {
/*
* write will stop at nodata when wblk-1 or less data is ready;
* we must restart reads then; rsp as below does that at
* wblk+1;
* at the same time we need at least rblk+1 (+1 due to
* ambiguity) space to read
* basic condition: hav(dec) <= rsp to resume reads
*/
if (rsp <= buf->wblk || rsp >= buf->size - buf->rblk) {
fputs("Read resume point is too small or leaves not enough space.\n", stderr);
return -1;
}
buf->rsp_inv = buf->size - rsp;
buf->rsp = rsp;
}
wsp = (size_t)(0.5 + ws*(double)buf->size);
if (wsp) {
/*
* read will stop at nospace when rblk-1 or less space is
* available; as buffer will never be fully filled (due to hav
* == got ambiguity) nospace will happen at rblk;
* we must restarts writes then; wsp as below does that at
* rblk+1;
* at the same time we need at least wblk data to write (wblk+1
* below)
* basic condition: hav(inc) >= wsp to resume writes
*/
if (wsp >= buf->size - buf->rblk || wsp <= buf->wblk) {
fputs("Write resume point is too big or leaves.\n", stderr);
return -1;
}
buf->wsp = wsp;
buf->wstall = 1;
DEB2("write forcibly (pre-run) stalled: fill 0\n");
}
return 0;
}
static void rep_crc(int c, CRCINT _crc, unsigned long long cnt)
{
unsigned long long int crc, cks;
crc = crc_end(_crc);
cks = crc_end(crc_cksum(_crc, cnt));
fprintf (stderr,
" %ccrc: 0x%llx; %ccksum: %llu (0x%llx)\n",
c, crc, c, cks, cks
);
}
void buf_report_stats(struct buf_s * restrict buf)
{
fprintf (stderr,
"Buffer stats:\n"
" read: %llu (%llu%c blocks)\n"
" wrote: %llu (%llu%c blocks)\n",
buf->allin, buf->allin / buf->rblk, buf->allin % buf->rblk ? '+':' ',
buf->allout, buf->allout / buf->wblk, buf->allout % buf->wblk ? '+':' '
);
if (buf->dorcrc)
rep_crc('r', buf->rcrc, buf->allin);
if (buf->dowcrc)
rep_crc('w', buf->wcrc, buf->allout);
}
void buf_report_init(struct buf_s * restrict buf)
{
fprintf (stderr,
"Buffer setup:\n"
" size: %zu\n"
" re. block: %zu%s\n"
" wr. block: %zu%s\n"
" re. resume @: %zd\n"
" wr. resume @: %zd\n"
" shared: %s\n"
" wrapped: %s\n"
" huge page: %s\n",
buf->size,
buf->rblk, buf->flags & M_LINER ? " (byte/line mode)" : "",
buf->wblk, buf->flags & M_LINEW ? " (byte/line mode)" : "",
buf->rsp ? (ssize_t)buf->rsp : -1,
buf->wsp ? (ssize_t)buf->wsp : -1,
buf->flags & M_SHM ? "yes" : "no",
buf->flags & M_CIR ? "yes" : "no",
buf->flags & M_HUGE ? "yes" : "no"
);
}