Skip to content

Commit

Permalink
More polished defaults, drop a couple debug tunables.
Browse files Browse the repository at this point in the history
Still have a few debug tunables in in case people want to play
with them, but they'll go away before a "final" version.

Signed-off-by: Rich Ercolani <rincebrain@gmail.com>
  • Loading branch information
rincebrain committed Mar 22, 2022
1 parent 52d9d68 commit 0594d03
Showing 1 changed file with 52 additions and 73 deletions.
125 changes: 52 additions & 73 deletions module/zstd/zfs_zstd.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@
#include "lib/zstd.h"
#include "lib/common/zstd_errors.h"

int zstd_lz4_pass = 3;
int zstd_lz4_shift = 3;
int zstd_lz4_pass = 1;
int zstd_firstpass_level = ZIO_ZSTD_LEVEL_1;
unsigned int zstd_abort_size = 131072;

kstat_t *zstd_ksp = NULL;

Expand Down Expand Up @@ -407,11 +407,13 @@ zfs_zstd_compress(void *s_start, void *d_start, size_t s_len, size_t d_len,
/*
* A zstd early abort heuristic.
*
* - Zeroth, if this is zstd-fast, don't try any of this, just go.
* - Zeroth, if this is <= zstd-3, or <= zstd_abort_size (currently 128k),
* don't try any of this, just go.
* (because experimentally that was a reasonable cutoff for a perf win
* with tiny ratio change)
* - First, we try LZ4 compression, and if it doesn't early abort, we
* jump directly to whatever compression level we intended to try.
* - Second, if this is > zstd-3 (because experimentally that was the
* cutoff), we try zstd-1 - if that errors out (usually, but not
* - Second, we try zstd-1 - if that errors out (usually, but not
* exclusively, if it would overflow), we give up early.
*
* If it works, instead we go on and compress anyway.
Expand All @@ -423,81 +425,58 @@ zfs_zstd_compress(void *s_start, void *d_start, size_t s_len, size_t d_len,
* pass noticably if stacked like this.
*/

int newsz = (s_len - (s_len >> zstd_lz4_shift));
if (zstd_lz4_pass > 0 && zstd_level > 0) {
/*
* zfs_dbgmsg("DBG: s_len %x d_len %x zstd_lz4_pass %x "
* "zstd_lz4_shift %x",
* s_len, d_len, zstd_lz4_pass, zstd_lz4_shift);
*/
if (zstd_lz4_pass == 1 || zstd_lz4_pass > 2) {
pass_len = lz4_compress_zfs(s_start, hdr, s_len,
newsz, 0);
if (pass_len < newsz)
if (zstd_lz4_pass > 0 && zstd_level > 3 && s_len >= zstd_abort_size) {
pass_len = lz4_compress_zfs(s_start, hdr, s_len, d_len, 0);
if (pass_len < d_len)
goto keep_trying;
cctx = ZSTD_createCCtx_advanced(zstd_malloc);
/*
* Out of kernel memory, gently fall through -
* this will disable compression in
* zio_compress_data
*/
if (!cctx) {
ZSTDSTAT_BUMP(zstd_stat_com_alloc_fail);
return (s_len);
}
if (zstd_lz4_pass > 1 && zstd_level > 3) {
cctx = ZSTD_createCCtx_advanced(zstd_malloc);

/*
* Out of kernel memory, gently fall through -
* this will disable compression in
* zio_compress_data
*/
if (!cctx) {
ZSTDSTAT_BUMP(zstd_stat_com_alloc_fail);
return (s_len);
}

/* Set the compression level */
ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel,
zstd_enum_to_level(zstd_firstpass_level,
&zstd_level));
/* Set the compression level */
ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel,
zstd_enum_to_level(zstd_firstpass_level,
&zstd_level));

/*
* Use the "magicless" zstd header which saves us 4
* header bytes
*/
ZSTD_CCtx_setParameter(cctx, ZSTD_c_format,
ZSTD_f_zstd1_magicless);
/*
* Use the "magicless" zstd header which saves us 4
* header bytes
*/
ZSTD_CCtx_setParameter(cctx, ZSTD_c_format,
ZSTD_f_zstd1_magicless);

/*
* Disable redundant checksum calculation and content
* size storage since this is already done by ZFS
* itself.
*/
ZSTD_CCtx_setParameter(cctx, ZSTD_c_checksumFlag, 0);
ZSTD_CCtx_setParameter(cctx, ZSTD_c_contentSizeFlag, 0);
/*
* Disable redundant checksum calculation and content
* size storage since this is already done by ZFS
* itself.
*/
ZSTD_CCtx_setParameter(cctx, ZSTD_c_checksumFlag, 0);
ZSTD_CCtx_setParameter(cctx, ZSTD_c_contentSizeFlag, 0);


pass_len = ZSTD_compress2(cctx,
hdr,
newsz,
s_start, s_len);
pass_len = ZSTD_compress2(cctx,
hdr,
d_len,
s_start, s_len);

ZSTD_freeCCtx(cctx);
ZSTD_freeCCtx(cctx);

/*
* Error in the compression routine, disable
* compression.
*/
if (ZSTD_isError(pass_len)) {
/*
* zfs_dbgmsg("zstd abort: level %x pass_len %x "
* "newsz %x", zstd_firstpass_level, pass_len,
* newsz);
*/
return (s_len);
}
}
if (pass_len <= 0 || pass_len >= newsz) {
/*
* zfs_dbgmsg("Length abort: pass_len %x newsz %x "
* "zstd_lz4_pass %x", pass_len, newsz,
* zstd_lz4_pass);
*/
/*
* Error in the compression routine, disable
* compression.
*/
if (ZSTD_isError(pass_len)) {
return (s_len);
}
if (pass_len <= 0 || pass_len >= d_len)
return (s_len);
}
keep_trying:
cctx = ZSTD_createCCtx_advanced(zstd_malloc);
Expand Down Expand Up @@ -527,7 +506,7 @@ zfs_zstd_compress(void *s_start, void *d_start, size_t s_len, size_t d_len,

c_len = ZSTD_compress2(cctx,
hdr->data,
newsz - sizeof (*hdr),
d_len - sizeof (*hdr),
s_start, s_len);

ZSTD_freeCCtx(cctx);
Expand Down Expand Up @@ -887,10 +866,10 @@ ZFS_MODULE_VERSION(ZSTD_VERSION_STRING "a");

ZFS_MODULE_PARAM(zfs, zstd_, lz4_pass, INT, ZMOD_RW,
"Try an LZ4 pass to determine whether we should early abort.");
ZFS_MODULE_PARAM(zfs, zstd_, lz4_shift, INT, ZMOD_RW,
"How much savings do you need from LZ4 to conclude it's compressible.");
ZFS_MODULE_PARAM(zfs, zstd_, firstpass_level, INT, ZMOD_RW,
"If trying zstd_fast firstpass, what level to try.");
"If trying zstd after LZ4, what level to try.");
ZFS_MODULE_PARAM(zfs, zstd_, abort_size, UINT, ZMOD_RW,
"Only >= this size should we try early abort at all");

EXPORT_SYMBOL(zfs_zstd_compress);
EXPORT_SYMBOL(zfs_zstd_decompress_level);
Expand Down

0 comments on commit 0594d03

Please sign in to comment.