Skip to content

Commit

Permalink
Default compress level use
Browse files Browse the repository at this point in the history
Discuss with Fabio, invalid compression level not the knet
responsible for, so error logged. But if compress success but
dstLen larger than srcLen, defualt compression level will be
used, because the request level is not effective.

Signed-off-by: yuan ren <yren@suse.com>
  • Loading branch information
yuanren10 authored and fabbione committed Aug 21, 2019
1 parent 4f9904c commit 62da86b
Show file tree
Hide file tree
Showing 10 changed files with 142 additions and 8 deletions.
27 changes: 27 additions & 0 deletions libknet/compress.c
Expand Up @@ -195,6 +195,7 @@ static int compress_lib_test(knet_handle_t knet_h)
unsigned char dst[KNET_DATABUFSIZE_COMPRESS];
ssize_t dst_comp_len = KNET_DATABUFSIZE_COMPRESS, dst_decomp_len = KNET_DATABUFSIZE;
unsigned int i;
int request_level;

memset(src, 0, KNET_DATABUFSIZE);
memset(dst, 0, KNET_DATABUFSIZE_COMPRESS);
Expand All @@ -209,6 +210,32 @@ static int compress_lib_test(knet_handle_t knet_h)
log_err(knet_h, KNET_SUB_COMPRESS, "Unable to compress test buffer. Please check your compression settings: %s", strerror(savederrno));
errno = savederrno;
return -1;
} else if ((long unsigned int)dst_comp_len >= KNET_DATABUFSIZE) {
/*
* compress not effective, try again using default compression level when available
*/
request_level = knet_h->compress_level;
log_warn(knet_h, KNET_SUB_COMPRESS, "Requested compression level (%d) did not generate any compressed data (source: %zu destination: %zu)",
request_level, sizeof(src), dst_comp_len);

if ((!compress_modules_cmds[knet_h->compress_model].ops->get_default_level()) ||
((knet_h->compress_level = compress_modules_cmds[knet_h->compress_model].ops->get_default_level()) == KNET_COMPRESS_UNKNOWN_DEFAULT)) {
log_err(knet_h, KNET_SUB_COMPRESS, "compression %s does not provide a default value",
compress_modules_cmds[knet_h->compress_model].model_name);
return -1;
} else {
memset(src, 0, KNET_DATABUFSIZE);
memset(dst, 0, KNET_DATABUFSIZE_COMPRESS);
dst_comp_len = KNET_DATABUFSIZE_COMPRESS;
if (compress_modules_cmds[knet_h->compress_model].ops->compress(knet_h, src, KNET_DATABUFSIZE, dst, &dst_comp_len) < 0) {
savederrno = errno;
log_err(knet_h, KNET_SUB_COMPRESS, "Unable to compress with default compression level: %s", strerror(savederrno));
errno = savederrno;
return -1;
}
log_warn(knet_h, KNET_SUB_COMPRESS, "Requested compression level (%d) did not work, switching to default (%d)",
request_level, knet_h->compress_level);
}
}

if (compress_modules_cmds[knet_h->compress_model].ops->decompress(knet_h, dst, dst_comp_len, src, &dst_decomp_len) < 0) {
Expand Down
14 changes: 13 additions & 1 deletion libknet/compress_bzip2.c
Expand Up @@ -15,6 +15,12 @@
#include "logging.h"
#include "compress_model.h"

#ifdef BZIP2_COMPRESS_LEVEL
#define KNET_COMPRESS_DEFAULT BZIP2_COMPRESS_LEVEL
#else
#define KNET_COMPRESS_DEFAULT KNET_COMPRESS_UNKNOWN_DEFAULT
#endif

static int bzip2_compress(
knet_handle_t knet_h,
const unsigned char *buf_in,
Expand Down Expand Up @@ -103,12 +109,18 @@ static int bzip2_decompress(
return err;
}

static int bzip2_get_default_level()
{
return KNET_COMPRESS_DEFAULT;
}

compress_ops_t compress_model = {
KNET_COMPRESS_MODEL_ABI,
NULL,
NULL,
NULL,
NULL,
bzip2_compress,
bzip2_decompress
bzip2_decompress,
bzip2_get_default_level
};
15 changes: 14 additions & 1 deletion libknet/compress_lz4.c
Expand Up @@ -6,6 +6,7 @@
* This software licensed under LGPL-2.0+
*/
#define KNET_MODULE
#define ACCELERATION_DEFAULT 1 /* lz4 default compression level from lz4.c */

#include "config.h"

Expand All @@ -15,6 +16,12 @@
#include "logging.h"
#include "compress_model.h"

#ifdef LZ4_COMPRESS_DEFAULT
#define KNET_COMPRESS_DEFAULT LZ4_COMPRESS_DEFAULT
#else
#define KNET_COMPRESS_DEFAULT KNET_COMPRESS_UNKNOWN_DEFAULT
#endif

static int lz4_compress(
knet_handle_t knet_h,
const unsigned char *buf_in,
Expand Down Expand Up @@ -80,12 +87,18 @@ static int lz4_decompress(
return err;
}

static int lz4_get_default_level()
{
return KNET_COMPRESS_DEFAULT;
}

compress_ops_t compress_model = {
KNET_COMPRESS_MODEL_ABI,
NULL,
NULL,
NULL,
NULL,
lz4_compress,
lz4_decompress
lz4_decompress,
lz4_get_default_level
};
13 changes: 12 additions & 1 deletion libknet/compress_lz4hc.c
Expand Up @@ -16,6 +16,11 @@
#include "logging.h"
#include "compress_model.h"

#ifdef LZ4HC_CLEVEL_DEFAULT
#define KNET_COMPRESS_DEFAULT LZ4HC_CLEVEL_DEFAULT /* lz4hc default compression level from lz4hc.h */
#else
#define KNET_COMPRESS_DEFAULT KNET_COMPRESS_UNKNOWN_DEFAULT
#endif
#ifdef LZ4HC_CLEVEL_MAX
#define KNET_LZ4HC_MAX LZ4HC_CLEVEL_MAX
#endif
Expand Down Expand Up @@ -91,12 +96,18 @@ static int lz4_decompress(
return err;
}

static int lz4hc_get_default_level()
{
return KNET_COMPRESS_DEFAULT;
}

compress_ops_t compress_model = {
KNET_COMPRESS_MODEL_ABI,
NULL,
NULL,
NULL,
NULL,
lz4hc_compress,
lz4_decompress
lz4_decompress,
lz4hc_get_default_level
};
14 changes: 13 additions & 1 deletion libknet/compress_lzma.c
Expand Up @@ -15,6 +15,12 @@
#include "logging.h"
#include "compress_model.h"

#ifdef LZMA_PRESET_DEFAULT
#define KNET_COMPRESS_DEFAULT LZMA_PRESET_DEFAULT /* lzma default compression level from lzma.h */
#else
#define KNET_COMPRESS_DEFAULT KNET_COMPRESS_UNKNOWN_DEFAULT
#endif

static int lzma_compress(
knet_handle_t knet_h,
const unsigned char *buf_in,
Expand Down Expand Up @@ -114,12 +120,18 @@ static int lzma_decompress(
return err;
}

static int lzma_get_default_level()
{
return KNET_COMPRESS_DEFAULT;
}

compress_ops_t compress_model = {
KNET_COMPRESS_MODEL_ABI,
NULL,
NULL,
NULL,
NULL,
lzma_compress,
lzma_decompress
lzma_decompress,
lzma_get_default_level
};
15 changes: 14 additions & 1 deletion libknet/compress_lzo2.c
Expand Up @@ -6,6 +6,7 @@
* This software licensed under LGPL-2.0+
*/
#define KNET_MODULE
#define LZO2_COMPRESS_DEFAULT 1

#include "config.h"

Expand All @@ -17,6 +18,12 @@
#include "logging.h"
#include "compress_model.h"

#ifdef LZO2_COMPRESS_DEFAULT
#define KNET_COMPRESS_DEFAULT LZO2_COMPRESS_DEFAULT
#else
#define KNET_COMPRESS_DEFAULT KNET_COMPRESS_UNKNOWN_DEFAULT
#endif

static int lzo2_is_init(
knet_handle_t knet_h,
int method_idx)
Expand Down Expand Up @@ -154,12 +161,18 @@ static int lzo2_decompress(
return err;
}

static int lzo2_get_default_level()
{
return KNET_COMPRESS_DEFAULT;
}

compress_ops_t compress_model = {
KNET_COMPRESS_MODEL_ABI,
lzo2_is_init,
lzo2_init,
lzo2_fini,
lzo2_val_level,
lzo2_compress,
lzo2_decompress
lzo2_decompress,
lzo2_get_default_level
};
8 changes: 7 additions & 1 deletion libknet/compress_model.h
Expand Up @@ -11,7 +11,8 @@

#include "internals.h"

#define KNET_COMPRESS_MODEL_ABI 1
#define KNET_COMPRESS_MODEL_ABI 2
#define KNET_COMPRESS_UNKNOWN_DEFAULT (-2)

typedef struct {
uint8_t abi_ver;
Expand Down Expand Up @@ -70,6 +71,11 @@ typedef struct {
const ssize_t buf_in_len,
unsigned char *buf_out,
ssize_t *buf_out_len);

/*
* Get default compression level
*/
int (*get_default_level) (void);
} compress_ops_t;

typedef struct {
Expand Down
14 changes: 13 additions & 1 deletion libknet/compress_zlib.c
Expand Up @@ -15,6 +15,12 @@
#include "logging.h"
#include "compress_model.h"

#ifdef Z_DEFAULT_COMPRESSION
#define KNET_COMPRESS_DEFAULT Z_DEFAULT_COMPRESSION /* zlib default compression level from zlib.h */
#else
#define KNET_COMPRESS_DEFAULT KNET_COMPRESS_UNKNOWN_DEFAULT
#endif

static int zlib_compress(
knet_handle_t knet_h,
const unsigned char *buf_in,
Expand Down Expand Up @@ -106,12 +112,18 @@ static int zlib_decompress(
return err;
}

static int zlib_get_default_level()
{
return KNET_COMPRESS_DEFAULT;
}

compress_ops_t compress_model = {
KNET_COMPRESS_MODEL_ABI,
NULL,
NULL,
NULL,
NULL,
zlib_compress,
zlib_decompress
zlib_decompress,
zlib_get_default_level
};
14 changes: 13 additions & 1 deletion libknet/compress_zstd.c
Expand Up @@ -17,6 +17,12 @@
#include "logging.h"
#include "compress_model.h"

#ifdef ZSTD_CLEVEL_DEFAULT
#define KNET_COMPRESS_DEFAULT ZSTD_CLEVEL_DEFAULT /* zstd default compression level from zstd.h */
#else
#define KNET_COMPRESS_DEFAULT KNET_COMPRESS_UNKNOWN_DEFAULT
#endif

struct zstd_ctx {
ZSTD_CCtx* cctx;
ZSTD_DCtx* dctx;
Expand Down Expand Up @@ -149,12 +155,18 @@ static int zstd_decompress(
return 0;
}

static int zstd_get_default_level()
{
return KNET_COMPRESS_DEFAULT;
}

compress_ops_t compress_model = {
KNET_COMPRESS_MODEL_ABI,
zstd_is_init,
zstd_init,
zstd_fini,
NULL,
zstd_compress,
zstd_decompress
zstd_decompress,
zstd_get_default_level
};
16 changes: 16 additions & 0 deletions libknet/tests/api_knet_handle_compress.c
Expand Up @@ -81,6 +81,22 @@ static void test(void)

flush_logs(logfds[0], stdout);

printf("Test knet_handle_compress with zlib compress and not effective compression level (0)\n");

memset(&knet_handle_compress_cfg, 0, sizeof(struct knet_handle_compress_cfg));
strncpy(knet_handle_compress_cfg.compress_model, "zlib", sizeof(knet_handle_compress_cfg.compress_model) - 1);
knet_handle_compress_cfg.compress_level = 0;

if((knet_handle_compress(knet_h, &knet_handle_compress_cfg)) || (errno == EINVAL)) {
printf("knet_handle_compress failed to compress with default compression level\n");
knet_handle_free(knet_h);
flush_logs(logfds[0], stdout);
close_logpipes(logfds);
exit(FAIL);
}

flush_logs(logfds[0], stdout);

printf("Test knet_handle_compress with zlib compress and negative level (-2)\n");

memset(&knet_handle_compress_cfg, 0, sizeof(struct knet_handle_compress_cfg));
Expand Down

0 comments on commit 62da86b

Please sign in to comment.