2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2005-2008 Poul-Henning Kamp
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 #include <sys/endian.h>
43 #include "libfifolog_int.h"
44 #include "fifolog_write.h"
47 static int fifolog_write_gzip(struct fifolog_writer *f, time_t now);
49 #define ALLOC(ptr, size) do { \
50 (*(ptr)) = calloc(1, size); \
51 assert(*(ptr) != NULL); \
55 const char *fifolog_write_statnames[] = {
56 [FIFOLOG_PT_BYTES_PRE] = "Bytes before compression",
57 [FIFOLOG_PT_BYTES_POST] = "Bytes after compression",
58 [FIFOLOG_PT_WRITES] = "Writes",
59 [FIFOLOG_PT_FLUSH] = "Flushes",
60 [FIFOLOG_PT_SYNC] = "Syncs",
61 [FIFOLOG_PT_RUNTIME] = "Runtime"
64 /**********************************************************************
65 * Check that everything is all right
68 fifolog_write_assert(const struct fifolog_writer *f)
71 CHECK_OBJ_NOTNULL(f, FIFOLOG_WRITER_MAGIC);
72 assert(f->ff->zs->next_out + f->ff->zs->avail_out == \
73 f->obuf + f->obufsize);
76 /**********************************************************************
77 * Allocate/Destroy a new fifolog writer instance
80 struct fifolog_writer *
81 fifolog_write_new(void)
83 struct fifolog_writer *f;
85 ALLOC_OBJ(f, FIFOLOG_WRITER_MAGIC);
91 fifolog_write_destroy(struct fifolog_writer *f)
99 /**********************************************************************
100 * Open/Close the fifolog
104 fifolog_write_close(struct fifolog_writer *f)
108 CHECK_OBJ_NOTNULL(f, FIFOLOG_WRITER_MAGIC);
109 fifolog_write_assert(f);
113 fifolog_write_gzip(f, now);
114 fifolog_write_assert(f);
115 fifolog_int_close(&f->ff);
120 fifolog_write_open(struct fifolog_writer *f, const char *fn,
121 unsigned writerate, unsigned syncrate, unsigned compression)
128 CHECK_OBJ_NOTNULL(f, FIFOLOG_WRITER_MAGIC);
130 /* Check for legal compression value */
131 if (compression > Z_BEST_COMPRESSION)
132 return ("Illegal compression value");
134 f->writerate = writerate;
135 f->syncrate = syncrate;
136 f->compression = compression;
138 /* Reset statistics */
139 memset(f->cnt, 0, sizeof f->cnt);
141 es = fifolog_int_open(&f->ff, fn, 1);
144 es = fifolog_int_findend(f->ff, &o);
147 i = fifolog_int_read(f->ff, o);
149 return ("Read error, looking for seq");
150 f->seq = be32dec(f->ff->recbuf);
159 f->obufsize = f->ff->recsize;
160 ALLOC(&f->obuf, f->obufsize);
162 f->ibufsize = f->obufsize * 10;
163 ALLOC(&f->ibuf, f->ibufsize);
166 i = deflateInit(f->ff->zs, (int)f->compression);
169 f->flag |= FIFOLOG_FLG_RESTART;
170 f->flag |= FIFOLOG_FLG_SYNC;
171 f->ff->zs->next_out = f->obuf + 9;
172 f->ff->zs->avail_out = f->obufsize - 9;
179 fifolog_write_assert(f);
183 /**********************************************************************
184 * Write an output record
185 * Returns -1 if there are trouble writing data
189 fifolog_write_output(struct fifolog_writer *f, int fl, time_t now)
191 long h, l = f->ff->zs->next_out - f->obuf;
196 be32enc(f->obuf, f->seq);
197 f->obuf[h] = f->flag;
199 if (f->flag & FIFOLOG_FLG_SYNC) {
200 be32enc(f->obuf + h, now);
201 h += 4; /* timestamp */
204 assert(l <= (long)f->ff->recsize); /* NB: l includes h */
207 /* We will never write an entirely empty buffer */
211 if (l < (long)f->ff->recsize && fl == Z_NO_FLUSH)
214 w = f->ff->recsize - l;
216 be32enc(f->obuf + f->ff->recsize - 4, w);
217 f->obuf[4] |= FIFOLOG_FLG_4BYTE;
219 f->obuf[f->ff->recsize - 1] = (uint8_t)w;
220 f->obuf[4] |= FIFOLOG_FLG_1BYTE;
223 f->cnt[FIFOLOG_PT_BYTES_POST] += l - h;
225 i = pwrite(f->ff->fd, f->obuf, f->ff->recsize,
226 (f->recno + 1) * f->ff->recsize);
227 if (i != f->ff->recsize)
232 f->cnt[FIFOLOG_PT_WRITES]++;
233 f->cnt[FIFOLOG_PT_RUNTIME] = now - f->starttime;
237 * We increment these even on error, so as to properly skip bad,
238 * sectors or other light trouble.
244 * Ensure we wrap recno once we hit the file size (in records.)
246 if (f->recno >= f->ff->logsize)
247 /* recno 0 is header; skip */
252 memset(f->obuf, 0, f->obufsize);
253 f->ff->zs->next_out = f->obuf + 5;
254 f->ff->zs->avail_out = f->obufsize - 5;
258 /**********************************************************************
259 * Run the compression engine
260 * Returns -1 if there are trouble writing data
264 fifolog_write_gzip(struct fifolog_writer *f, time_t now)
266 int i, fl, retval = 0;
269 if (f->cleanup || now >= (int)(f->lastsync + f->syncrate)) {
272 f->cnt[FIFOLOG_PT_SYNC]++;
273 } else if (now >= (int)(f->lastwrite + f->writerate)) {
275 f->cnt[FIFOLOG_PT_FLUSH]++;
276 } else if (f->ibufptr == 0)
281 f->ff->zs->avail_in = f->ibufptr;
282 f->ff->zs->next_in = f->ibuf;
285 i = deflate(f->ff->zs, fl);
286 assert(i == Z_OK || i == Z_BUF_ERROR || i == Z_STREAM_END);
288 i = fifolog_write_output(f, fl, now);
294 assert(f->ff->zs->avail_in == 0);
296 if (fl == Z_FINISH) {
297 f->flag |= FIFOLOG_FLG_SYNC;
298 f->ff->zs->next_out = f->obuf + 9;
299 f->ff->zs->avail_out = f->obufsize - 9;
301 assert(Z_OK == deflateReset(f->ff->zs));
306 /**********************************************************************
307 * Poll to see if we need to flush out a record
308 * Returns -1 if there are trouble writing data
312 fifolog_write_poll(struct fifolog_writer *f, time_t now)
317 return (fifolog_write_gzip(f, now));
320 /**********************************************************************
321 * Attempt to write an entry into the ibuf.
322 * Return zero if there is no space, one otherwise
326 fifolog_write_record(struct fifolog_writer *f, uint32_t id, time_t now,
327 const void *ptr, ssize_t len)
329 const unsigned char *p;
333 fifolog_write_assert(f);
334 assert(!(id & (FIFOLOG_TIMESTAMP|FIFOLOG_LENGTH)));
343 id |= FIFOLOG_LENGTH;
347 /* Do a timestamp, if needed */
352 id |= FIFOLOG_TIMESTAMP;
354 /* Emit instance+flag */
358 if (id & FIFOLOG_TIMESTAMP) {
359 be32enc(buf + bufl, (uint32_t)now);
362 if (id & FIFOLOG_LENGTH)
363 buf[bufl++] = (u_char)len;
365 if (bufl + len + f->ibufptr > f->ibufsize)
368 memcpy(f->ibuf + f->ibufptr, buf, bufl);
370 memcpy(f->ibuf + f->ibufptr, p, len);
372 f->cnt[FIFOLOG_PT_BYTES_PRE] += bufl + len;
374 if (id & FIFOLOG_TIMESTAMP)
379 /**********************************************************************
380 * Write an entry, polling the gzip/writer until success.
381 * Long binary entries are broken into 255 byte chunks.
382 * Returns -1 if there are problems writing data
386 fifolog_write_record_poll(struct fifolog_writer *f, uint32_t id, time_t now,
387 const void *ptr, ssize_t len)
390 const unsigned char *p;
395 fifolog_write_assert(f);
397 assert(!(id & (FIFOLOG_TIMESTAMP|FIFOLOG_LENGTH)));
401 if (!fifolog_write_record(f, id, now, ptr, len)) {
402 if (fifolog_write_gzip(f, now) < 0)
404 /* The string could be too long for the ibuf, so... */
405 if (!fifolog_write_record(f, id, now, ptr, len))
409 for (p = ptr; len > 0; len -= l, p += l) {
413 while (!fifolog_write_record(f, id, now, p, l))
414 if (fifolog_write_gzip(f, now) < 0)
418 if (fifolog_write_gzip(f, now) < 0)
420 fifolog_write_assert(f);