34 #if defined(ENABLE_LZO)
50 lzo_adaptive_compress_test(
struct lzo_adaptive_compress *ac)
52 const bool save = ac->compress_state;
53 const time_t local_now =
now;
55 if (!ac->compress_state)
57 if (local_now >= ac->next)
59 if (ac->n_total > AC_MIN_BYTES
60 && (ac->n_total - ac->n_comp) < (ac->n_total / (100 / AC_SAVE_PCT)))
62 ac->compress_state =
true;
63 ac->next = local_now + AC_OFF_SEC;
67 ac->next = local_now + AC_SAMP_SEC;
69 dmsg(
D_COMP,
"lzo_adaptive_compress_test: comp=%d total=%d", ac->n_comp, ac->n_total);
70 ac->n_total = ac->n_comp = 0;
75 if (local_now >= ac->next)
77 ac->next = local_now + AC_SAMP_SEC;
78 ac->n_total = ac->n_comp = 0;
79 ac->compress_state =
false;
83 if (ac->compress_state != save)
85 dmsg(
D_COMP_LOW,
"Adaptive compression state %s", (ac->compress_state ?
"OFF" :
"ON"));
88 return !ac->compress_state;
92 lzo_adaptive_compress_data(
struct lzo_adaptive_compress *ac,
int n_total,
int n_comp)
94 ac->n_total += n_total;
99 lzo_compress_init(
struct compress_context *compctx)
103 compctx->wu.lzo.wmem_size = LZO_WORKSPACE;
105 int lzo_status = lzo_init();
106 if (lzo_status != LZO_E_OK)
108 msg(
M_FATAL,
"Cannot initialize LZO compression library (lzo_init() returns %d)", lzo_status);
110 compctx->wu.lzo.wmem = (lzo_voidp) malloc(compctx->wu.lzo.wmem_size);
115 lzo_compress_uninit(
struct compress_context *compctx)
117 free(compctx->wu.lzo.wmem);
118 compctx->wu.lzo.wmem = NULL;
122 lzo_compression_enabled(
struct compress_context *compctx)
132 return lzo_adaptive_compress_test(&compctx->wu.lzo.ac);
143 struct compress_context *compctx,
148 bool compressed =
false;
159 if (buf->
len >= COMPRESS_THRESHOLD && lzo_compression_enabled(compctx))
172 err = LZO_COMPRESS(
BPTR(buf),
BLEN(buf),
BPTR(&work), &zlen, compctx->wu.lzo.wmem);
185 compctx->pre_compress += buf->
len;
186 compctx->post_compress += work.
len;
191 lzo_adaptive_compress_data(&compctx->wu.lzo.ac, buf->
len, work.
len);
196 if (compressed && work.
len < buf->
len)
199 *header = LZO_COMPRESS_BYTE;
205 *header = NO_COMPRESS_BYTE;
211 struct compress_context *compctx,
228 if (c == LZO_COMPRESS_BYTE)
231 err = LZO_DECOMPRESS(
BPTR(buf),
BLEN(buf),
BPTR(&work), &zlen,
232 compctx->wu.lzo.wmem);
244 compctx->pre_decompress += buf->
len;
245 compctx->post_decompress += work.
len;
249 else if (c == NO_COMPRESS_BYTE)
260 const struct compress_alg lzo_alg = {