Skip to content

Commit 84d4544

Browse files
rincebrainandrewc12
authored andcommitted
Tiered early abort, zstd edition
It turns out that "do LZ4 and zstd-1 both fail" is a great heuristic for "don't even bother trying higher zstd tiers". By way of illustration: $ cat /incompress | mbuffer | zfs recv -o compression=zstd-12 evenfaster/lowcomp_1M_zstd12_normal summary: 39.8 GiByte in 3min 40.2sec - average of 185 MiB/s $ echo 3 | sudo tee /sys/module/zzstd/parameters/zstd_lz4_pass 3 $ cat /incompress | mbuffer -m 4G | zfs recv -o compression=zstd-12 evenfaster/lowcomp_1M_zstd12_patched summary: 39.8 GiByte in 48.6sec - average of 839 MiB/s $ sudo zfs list -p -o name,used,lused,ratio evenfaster/lowcomp_1M_zstd12_normal evenfaster/lowcomp_1M_zstd12_patched NAME USED LUSED RATIO evenfaster/lowcomp_1M_zstd12_normal 39549931520 42721221632 1.08 evenfaster/lowcomp_1M_zstd12_patched 39626399744 42721217536 1.07 $ python3 -c "print(39626399744 - 39549931520)" 76468224 $ I'll take 76 MB out of 42 GB for > 4x speedup. Reviewed-by: Allan Jude <[email protected]> Reviewed-by: Brian Behlendorf <[email protected]> Reviewed-by: George Melikov <[email protected]> Reviewed-by: Kjeld Schouten <[email protected]> Reviewed-by: Ahelenia Ziemiańska <[email protected]> Signed-off-by: Rich Ercolani <[email protected]> Closes openzfs#13244
1 parent b27b8f4 commit 84d4544

File tree

4 files changed

+134
-6
lines changed

4 files changed

+134
-6
lines changed

include/sys/zstd/zstd.h

+4
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ typedef struct zfs_zstd_meta {
7878
* kstat helper macros
7979
*/
8080
#define ZSTDSTAT(stat) (zstd_stats.stat.value.ui64)
81+
#define ZSTDSTAT_ZERO(stat) \
82+
(atomic_store_64(&zstd_stats.stat.value.ui64, 0))
8183
#define ZSTDSTAT_ADD(stat, val) \
8284
atomic_add_64(&zstd_stats.stat.value.ui64, (val))
8385
#define ZSTDSTAT_SUB(stat, val) \
@@ -90,6 +92,8 @@ void zstd_fini(void);
9092

9193
size_t zfs_zstd_compress(void *s_start, void *d_start, size_t s_len,
9294
size_t d_len, int level);
95+
size_t zfs_zstd_compress_wrap(void *s_start, void *d_start, size_t s_len,
96+
size_t d_len, int level);
9397
int zfs_zstd_get_level(void *s_start, size_t s_len, uint8_t *level);
9498
int zfs_zstd_decompress_level(void *s_start, void *d_start, size_t s_len,
9599
size_t d_len, uint8_t *level);

man/man4/zfs.4

+8
Original file line numberDiff line numberDiff line change
@@ -2129,6 +2129,14 @@ However, if there are fewer than
21292129
metaslabs in the vdev, this functionality is disabled.
21302130
This ensures that we don't set aside an unreasonable amount of space for the ZIL.
21312131
.
2132+
.It Sy zfs_zstd_earlyabort_pass Ns = Ns Sy 1 Pq int
2133+
Whether heuristic for detection of incompressible data with zstd levels >= 3
2134+
using LZ4 and zstd-1 passes is enabled.
2135+
.
2136+
.It Sy zfs_zstd_abort_size Ns = Ns Sy 131072 Pq int
2137+
Minimal uncompressed size (inclusive) of a record before the early abort
2138+
heuristic will be attempted.
2139+
.
21322140
.It Sy zio_deadman_log_all Ns = Ns Sy 0 Ns | Ns 1 Pq int
21332141
If non-zero, the zio deadman will produce debugging messages
21342142
.Pq see Sy zfs_dbgmsg_enable

module/zfs/zio_compress.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ zio_compress_info_t zio_compress_table[ZIO_COMPRESS_FUNCTIONS] = {
6666
{"gzip-9", 9, gzip_compress, gzip_decompress, NULL},
6767
{"zle", 64, zle_compress, zle_decompress, NULL},
6868
{"lz4", 0, lz4_compress_zfs, lz4_decompress_zfs, NULL},
69-
{"zstd", ZIO_ZSTD_LEVEL_DEFAULT, zfs_zstd_compress,
69+
{"zstd", ZIO_ZSTD_LEVEL_DEFAULT, zfs_zstd_compress_wrap,
7070
zfs_zstd_decompress, zfs_zstd_decompress_level},
7171
};
7272

module/zstd/zfs_zstd.c

+121-5
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@
5050
#include "lib/zstd.h"
5151
#include "lib/common/zstd_errors.h"
5252

53+
static int zstd_earlyabort_pass = 1;
54+
static int zstd_cutoff_level = ZIO_ZSTD_LEVEL_3;
55+
static unsigned int zstd_abort_size = (128 * 1024);
56+
5357
static kstat_t *zstd_ksp = NULL;
5458

5559
typedef struct zstd_stats {
@@ -62,6 +66,21 @@ typedef struct zstd_stats {
6266
kstat_named_t zstd_stat_dec_header_inval;
6367
kstat_named_t zstd_stat_com_fail;
6468
kstat_named_t zstd_stat_dec_fail;
69+
/*
70+
* LZ4 first-pass early abort verdict
71+
*/
72+
kstat_named_t zstd_stat_lz4pass_allowed;
73+
kstat_named_t zstd_stat_lz4pass_rejected;
74+
/*
75+
* zstd-1 second-pass early abort verdict
76+
*/
77+
kstat_named_t zstd_stat_zstdpass_allowed;
78+
kstat_named_t zstd_stat_zstdpass_rejected;
79+
/*
80+
* We excluded this from early abort for some reason
81+
*/
82+
kstat_named_t zstd_stat_passignored;
83+
kstat_named_t zstd_stat_passignored_size;
6584
kstat_named_t zstd_stat_buffers;
6685
kstat_named_t zstd_stat_size;
6786
} zstd_stats_t;
@@ -76,10 +95,44 @@ static zstd_stats_t zstd_stats = {
7695
{ "decompress_header_invalid", KSTAT_DATA_UINT64 },
7796
{ "compress_failed", KSTAT_DATA_UINT64 },
7897
{ "decompress_failed", KSTAT_DATA_UINT64 },
98+
{ "lz4pass_allowed", KSTAT_DATA_UINT64 },
99+
{ "lz4pass_rejected", KSTAT_DATA_UINT64 },
100+
{ "zstdpass_allowed", KSTAT_DATA_UINT64 },
101+
{ "zstdpass_rejected", KSTAT_DATA_UINT64 },
102+
{ "passignored", KSTAT_DATA_UINT64 },
103+
{ "passignored_size", KSTAT_DATA_UINT64 },
79104
{ "buffers", KSTAT_DATA_UINT64 },
80105
{ "size", KSTAT_DATA_UINT64 },
81106
};
82107

108+
#ifdef _KERNEL
109+
static int
110+
kstat_zstd_update(kstat_t *ksp, int rw)
111+
{
112+
ASSERT(ksp != NULL);
113+
114+
if (rw == KSTAT_WRITE && ksp == zstd_ksp) {
115+
ZSTDSTAT_ZERO(zstd_stat_alloc_fail);
116+
ZSTDSTAT_ZERO(zstd_stat_alloc_fallback);
117+
ZSTDSTAT_ZERO(zstd_stat_com_alloc_fail);
118+
ZSTDSTAT_ZERO(zstd_stat_dec_alloc_fail);
119+
ZSTDSTAT_ZERO(zstd_stat_com_inval);
120+
ZSTDSTAT_ZERO(zstd_stat_dec_inval);
121+
ZSTDSTAT_ZERO(zstd_stat_dec_header_inval);
122+
ZSTDSTAT_ZERO(zstd_stat_com_fail);
123+
ZSTDSTAT_ZERO(zstd_stat_dec_fail);
124+
ZSTDSTAT_ZERO(zstd_stat_lz4pass_allowed);
125+
ZSTDSTAT_ZERO(zstd_stat_lz4pass_rejected);
126+
ZSTDSTAT_ZERO(zstd_stat_zstdpass_allowed);
127+
ZSTDSTAT_ZERO(zstd_stat_zstdpass_rejected);
128+
ZSTDSTAT_ZERO(zstd_stat_passignored);
129+
ZSTDSTAT_ZERO(zstd_stat_passignored_size);
130+
}
131+
132+
return (0);
133+
}
134+
#endif
135+
83136
/* Enums describing the allocator type specified by kmem_type in zstd_kmem */
84137
enum zstd_kmem_type {
85138
ZSTD_KMEM_UNKNOWN = 0,
@@ -377,6 +430,64 @@ zstd_enum_to_level(enum zio_zstd_levels level, int16_t *zstd_level)
377430
}
378431

379432

433+
size_t
434+
zfs_zstd_compress_wrap(void *s_start, void *d_start, size_t s_len, size_t d_len,
435+
int level)
436+
{
437+
int16_t zstd_level;
438+
if (zstd_enum_to_level(level, &zstd_level)) {
439+
ZSTDSTAT_BUMP(zstd_stat_com_inval);
440+
return (s_len);
441+
}
442+
/*
443+
* A zstd early abort heuristic.
444+
*
445+
* - Zeroth, if this is <= zstd-3, or < zstd_abort_size (currently
446+
* 128k), don't try any of this, just go.
447+
* (because experimentally that was a reasonable cutoff for a perf win
448+
* with tiny ratio change)
449+
* - First, we try LZ4 compression, and if it doesn't early abort, we
450+
* jump directly to whatever compression level we intended to try.
451+
* - Second, we try zstd-1 - if that errors out (usually, but not
452+
* exclusively, if it would overflow), we give up early.
453+
*
454+
* If it works, instead we go on and compress anyway.
455+
*
456+
* Why two passes? LZ4 alone gets you a lot of the way, but on highly
457+
* compressible data, it was losing up to 8.5% of the compressed
458+
* savings versus no early abort, and all the zstd-fast levels are
459+
* worse indications on their own than LZ4, and don't improve the LZ4
460+
* pass noticably if stacked like this.
461+
*/
462+
size_t actual_abort_size = zstd_abort_size;
463+
if (zstd_earlyabort_pass > 0 && zstd_level >= zstd_cutoff_level &&
464+
s_len >= actual_abort_size) {
465+
int pass_len = 1;
466+
pass_len = lz4_compress_zfs(s_start, d_start, s_len, d_len, 0);
467+
if (pass_len < d_len) {
468+
ZSTDSTAT_BUMP(zstd_stat_lz4pass_allowed);
469+
goto keep_trying;
470+
}
471+
ZSTDSTAT_BUMP(zstd_stat_lz4pass_rejected);
472+
473+
pass_len = zfs_zstd_compress(s_start, d_start, s_len, d_len,
474+
ZIO_ZSTD_LEVEL_1);
475+
if (pass_len == s_len || pass_len <= 0 || pass_len > d_len) {
476+
ZSTDSTAT_BUMP(zstd_stat_zstdpass_rejected);
477+
return (s_len);
478+
}
479+
ZSTDSTAT_BUMP(zstd_stat_zstdpass_allowed);
480+
} else {
481+
ZSTDSTAT_BUMP(zstd_stat_passignored);
482+
if (s_len < actual_abort_size) {
483+
ZSTDSTAT_BUMP(zstd_stat_passignored_size);
484+
}
485+
}
486+
keep_trying:
487+
return (zfs_zstd_compress(s_start, d_start, s_len, d_len, level));
488+
489+
}
490+
380491
/* Compress block using zstd */
381492
size_t
382493
zfs_zstd_compress(void *s_start, void *d_start, size_t s_len, size_t d_len,
@@ -437,8 +548,10 @@ zfs_zstd_compress(void *s_start, void *d_start, size_t s_len, size_t d_len,
437548
* too small, that is not a failure. Everything else is a
438549
* failure, so increment the compression failure counter.
439550
*/
440-
if (ZSTD_getErrorCode(c_len) != ZSTD_error_dstSize_tooSmall) {
551+
int err = ZSTD_getErrorCode(c_len);
552+
if (err != ZSTD_error_dstSize_tooSmall) {
441553
ZSTDSTAT_BUMP(zstd_stat_com_fail);
554+
dprintf("Error: %s", ZSTD_getErrorString(err));
442555
}
443556
return (s_len);
444557
}
@@ -753,6 +866,9 @@ zstd_init(void)
753866
if (zstd_ksp != NULL) {
754867
zstd_ksp->ks_data = &zstd_stats;
755868
kstat_install(zstd_ksp);
869+
#ifdef _KERNEL
870+
zstd_ksp->ks_update = kstat_zstd_update;
871+
#endif
756872
}
757873

758874
return (0);
@@ -781,8 +897,8 @@ module_init(zstd_init);
781897
module_exit(zstd_fini);
782898
#endif
783899

784-
EXPORT_SYMBOL(zfs_zstd_compress);
785-
EXPORT_SYMBOL(zfs_zstd_decompress_level);
786-
EXPORT_SYMBOL(zfs_zstd_decompress);
787-
EXPORT_SYMBOL(zfs_zstd_cache_reap_now);
900+
ZFS_MODULE_PARAM(zfs, zstd_, earlyabort_pass, INT, ZMOD_RW,
901+
"Enable early abort attempts when using zstd");
902+
ZFS_MODULE_PARAM(zfs, zstd_, abort_size, UINT, ZMOD_RW,
903+
"Minimal size of block to attempt early abort");
788904
#endif

0 commit comments

Comments
 (0)