summaryrefslogtreecommitdiffstats
path: root/usr.sbin/fifolog/lib/fifolog_write_poll.c
blob: 4fc52046557f32019e1d0cb91d37de05e489bb2c (plain)
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/*-
 * Copyright (c) 2005-2008 Poul-Henning Kamp
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $FreeBSD$
 */

#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <sys/endian.h>

#include <zlib.h>

#include "fifolog.h"
#include "libfifolog.h"
#include "libfifolog_int.h"
#include "fifolog_write.h"
#include "miniobj.h"

#define ALLOC(ptr, size) do {                   \
	(*(ptr)) = calloc(size, 1);             \
	assert(*(ptr) != NULL);                 \
} while (0)


const char *fifolog_write_statnames[] = {
[FIFOLOG_PT_BYTES_PRE] =	"Bytes before compression",
[FIFOLOG_PT_BYTES_POST] =	"Bytes after compression",
[FIFOLOG_PT_WRITES] =		"Writes",
[FIFOLOG_PT_FLUSH] =		"Flushes",
[FIFOLOG_PT_SYNC] =		"Syncs",
[FIFOLOG_PT_RUNTIME] =		"Runtime"
};

/*
 * Check that everything is all right
 */
static void
fifolog_write_assert(const struct fifolog_writer *f)
{

	CHECK_OBJ_NOTNULL(f, FIFOLOG_WRITER_MAGIC);
	assert(f->iptr == f->ff->zs->next_in + f->ff->zs->avail_in);
	assert(f->ff->zs->next_out + f->ff->zs->avail_out == \
	    f->ff->recbuf + f->ff->recsize);
}

struct fifolog_writer *
fifolog_write_new(void)
{
	struct fifolog_writer *f;

	ALLOC(&f, sizeof *f);
	f->magic = FIFOLOG_WRITER_MAGIC;
	return (f);
}

void
fifolog_write_destroy(struct fifolog_writer *f)
{
	CHECK_OBJ_NOTNULL(f, FIFOLOG_WRITER_MAGIC);
	free(f);
}

void
fifolog_write_close(struct fifolog_writer *f)
{

	CHECK_OBJ_NOTNULL(f, FIFOLOG_WRITER_MAGIC);
	fifolog_int_close(&f->ff);
	free(f->ff);
	if (f->ibuf != NULL)
		free(f->ibuf);
	free(f);
}

static void
fifo_prepobuf(struct fifolog_writer *f, time_t now, int flag)
{

	memset(f->ff->recbuf, 0, f->ff->recsize);
	f->ff->zs->next_out = f->ff->recbuf + 5;
	f->ff->zs->avail_out = f->ff->recsize - 5;
	if (f->recno == 0 && f->seq == 0) {
		srandomdev();
		do {
			f->seq = random();
		} while (f->seq == 0);
	}
	be32enc(f->ff->recbuf, f->seq++);
	f->ff->recbuf[4] = f->flag;
	f->flag = 0;
	if (flag) {
		f->ff->recbuf[4] |= FIFOLOG_FLG_SYNC;
		be32enc(f->ff->recbuf + 5, (u_int)now);
		f->ff->zs->next_out += 4;
		f->ff->zs->avail_out -= 4;
	}
	fifolog_write_assert(f);
}

const char *
fifolog_write_open(struct fifolog_writer *f, const char *fn, unsigned writerate, unsigned syncrate, int compression)
{
	const char *es;
	int i;
	time_t now;
	off_t o;

	CHECK_OBJ_NOTNULL(f, FIFOLOG_WRITER_MAGIC);

	/* Check for legal compression value */
	if (compression < Z_DEFAULT_COMPRESSION ||
	    compression > Z_BEST_COMPRESSION)
		return ("Illegal compression value");

	f->writerate = writerate;
	f->syncrate = syncrate;
	f->compression = compression;

	/* Reset statistics */
	memset(f->cnt, 0, sizeof f->cnt);

	es = fifolog_int_open(&f->ff, fn, 1);
	if (es != NULL)
		return (es);
	es = fifolog_int_findend(f->ff, &o);
	if (es != NULL)
		return (es);
	i = fifolog_int_read(f->ff, o);
	if (i)
		return ("Read error, looking for seq");
	f->seq = be32dec(f->ff->recbuf);
	if (f->seq == 0) {
		/* Empty fifolog */
		f->seq = random();
	} else {
		f->recno = o + 1;
		f->seq++;
	}

	f->ibufsize = 32768;
	ALLOC(&f->ibuf, f->ibufsize);
	f->iptr = f->ibuf;
	f->ff->zs->next_in = f->iptr;
	i = deflateInit(f->ff->zs, (int)f->compression);
	assert(i == Z_OK);

	f->flag |= FIFOLOG_FLG_RESTART;

	time(&now);
	fifo_prepobuf(f, now, 1);
	f->starttime = now;

	fifolog_write_assert(f);
	return (NULL);
}

static void
fifo_writerec(struct fifolog_writer *f)
{
	int i;
	time_t t;

	fifolog_write_assert(f);
	f->writes_since_sync++;

	assert(f->recno < f->ff->logsize);
	f->cnt[FIFOLOG_PT_BYTES_POST] += f->ff->recsize - f->ff->zs->avail_out;
	if (f->ff->zs->avail_out == 0) {
		/* nothing */
	} else if (f->ff->zs->avail_out <= 255) {
		f->ff->recbuf[f->ff->recsize - 1] = 
		    (u_char)f->ff->zs->avail_out;
		f->ff->recbuf[4] |= FIFOLOG_FLG_1BYTE;
	} else {
		be32enc(f->ff->recbuf + f->ff->recsize - 4,
		    f->ff->zs->avail_out);
		f->ff->recbuf[4] |= FIFOLOG_FLG_4BYTE;
	}
	i = pwrite(f->ff->fd, f->ff->recbuf, f->ff->recsize,
		(f->recno + 1) * f->ff->recsize);
	assert (i == (int)f->ff->recsize);
	if (++f->recno == f->ff->logsize)
		f->recno = 0;
	f->cnt[FIFOLOG_PT_WRITES]++;
	time(&t);
	f->cnt[FIFOLOG_PT_RUNTIME] = t - f->starttime; /*lint !e776 */
	fifolog_write_assert(f);
}

int
fifolog_write_poll(struct fifolog_writer *f, time_t now)
{
	int i, fl, bo, bf;

	if (now == 0)
		time(&now);

	fifolog_write_assert(f);
	if (f->cleanup || now >= (int)(f->lastsync + f->syncrate)) {
		/*
		 * We always check the sync timer, otherwise a flood of data
		 * would not get any sync records at all
		 */
		f->cleanup = 0;
		fl = Z_FINISH;
		f->lastsync = now;
		f->lastwrite = now;
		f->cnt[FIFOLOG_PT_SYNC]++;
	} else if (f->ff->zs->avail_in == 0 &&
	    now >= (int)(f->lastwrite + f->writerate)) {
		/*
		 * We only check for writerate timeouts when the input 
		 * buffer is empty.  It would be silly to force a write if
		 * pending input could cause it to happen on its own.
		 */
		fl = Z_SYNC_FLUSH;
		f->lastwrite = now;
		f->cnt[FIFOLOG_PT_FLUSH]++;
	} else if (f->ff->zs->avail_in == 0)
		return (0);			/* nothing to do */
	else
		fl = Z_NO_FLUSH;

	for (;;) {
		assert(f->ff->zs->avail_out > 0);

		bf = f->ff->zs->avail_out;

		i = deflate(f->ff->zs, fl);
		assert (i == Z_OK || i == Z_BUF_ERROR || i == Z_STREAM_END);

		bo = f->ff->zs->avail_out;

		/* If we have output space and not in a hurry.. */
		if (bo > 0 && fl == Z_NO_FLUSH)
			break;
	
		/* Write output buffer, if anything in it */
		if (bo != bf)
			fifo_writerec(f);

		/* If the buffer were full, we need to check again */
		if (bo == 0) {
			fifo_prepobuf(f, now, 0);
			continue;
		}

		if (fl == Z_FINISH) {
			/* Make next record a SYNC record */
			fifo_prepobuf(f, now, 1);
			/* And reset the zlib engine */
			i = deflateReset(f->ff->zs);
			assert(i == Z_OK);
			f->writes_since_sync = 0;
		} else {
			fifo_prepobuf(f, now, 0);
		}
		break;
	}

	if (f->ff->zs->avail_in == 0) {
		/* Reset input buffer when empty */
		f->iptr = f->ibuf;
		f->ff->zs->next_in = f->iptr;
	}

	fifolog_write_assert(f);
	return (1);
}

static void
fifolog_acct(struct fifolog_writer *f, unsigned bytes)
{

	f->ff->zs->avail_in += bytes;
	f->iptr += bytes;
	f->cnt[FIFOLOG_PT_BYTES_PRE] += bytes;
}

/*
 * Attempt to write an entry.
 * Return zero if there is no space, one otherwise
 */

int
fifolog_write_bytes(struct fifolog_writer *f, uint32_t id, time_t now, const void *ptr, unsigned len)
{
	u_int l;
	const unsigned char *p;

	fifolog_write_assert(f);
	assert(!(id & (FIFOLOG_TIMESTAMP|FIFOLOG_LENGTH)));
	assert(ptr != NULL);

	p = ptr;
	if (len == 0) {
		len = strlen(ptr) + 1;
		l = 4 + len;		/* id */
	} else {
		assert(len <= 255);
		id |= FIFOLOG_LENGTH;
		l = 5 + len;		/* id + len */
	}

	l += 4; 		/* A timestamp may be necessary */

	/* Now do timestamp, if needed */
	if (now == 0)
		time(&now);

	assert(l < f->ibufsize);

	/* Return if there is not enough space */
	if (f->iptr + l > f->ibuf + f->ibufsize)
		return (0);

	if (now != f->last) {
		id |= FIFOLOG_TIMESTAMP;
		f->last = now;
	} 

	/* Emit instance+flag and length */
	be32enc(f->iptr, id);
	fifolog_acct(f, 4);

	if (id & FIFOLOG_TIMESTAMP) {
		be32enc(f->iptr, (uint32_t)f->last);
		fifolog_acct(f, 4);
	}
	if (id & FIFOLOG_LENGTH) {
		f->iptr[0] = (u_char)len;
		fifolog_acct(f, 1);
	}

	assert (len > 0);
	memcpy(f->iptr, p, len);
	fifolog_acct(f, len);
	fifolog_write_assert(f);
	return (1);
}

/*
 * Write an entry, polling until success.
 * Long binary entries are broken into 255 byte chunks.
 */

void
fifolog_write_bytes_poll(struct fifolog_writer *f, uint32_t id, time_t now, const void *ptr, unsigned len)
{
	u_int l;
	const unsigned char *p;

	fifolog_write_assert(f);

	assert(!(id & (FIFOLOG_TIMESTAMP|FIFOLOG_LENGTH)));
	assert(ptr != NULL);

	if (len == 0) {
		while (!fifolog_write_bytes(f, id, now, ptr, len)) {
			(void)fifolog_write_poll(f, now);
			(void)usleep(10000);
		}
	} else {
		p = ptr;
		for (p = ptr; len > 0; len -= l, p += l) {
			l = len;
			if (l > 255)
				l = 255;
			while (!fifolog_write_bytes(f, id, now, p, l)) {
				(void)fifolog_write_poll(f, now);
				(void)usleep(10000);
			}
		}
	}
	fifolog_write_assert(f);
}

int
fifolog_write_flush(struct fifolog_writer *f)
{
	int i;

	fifolog_write_assert(f);

	f->cleanup = 1;
	for (i = 0; fifolog_write_poll(f, 0); i = 1)
		continue;
	fifolog_write_assert(f);
	return (i);
}
OpenPOWER on IntegriCloud