Skip to content

Commit ec23f10

Browse files
gmelikovmcmilk
authored andcommitted
zio_compress: introduce max size threshold
Now default compression is lz4, which can stop compression process by itself on incompressible data. If there are additional size checks - we will only make our compressratio worse. New usable compression thresholds are: - less than BPE_PAYLOAD_SIZE (embedded_data feature); - at least one saved sector. Old 12.5% threshold is left to minimize affect on existing user expectations of CPU utilization. If data wasn't compressed - it will be saved as ZIO_COMPRESS_OFF, so if we really need to recompress data without ashift info and check anything - we can just compress it with zero threshold. So, we don't need a new feature flag here! Reviewed-by: Brian Behlendorf <[email protected]> Reviewed-by: Tony Hutter <[email protected]> Reviewed-by: Alexander Motin <[email protected]> Signed-off-by: George Melikov <[email protected]> Closes openzfs#9416
1 parent e8ede2b commit ec23f10

File tree

8 files changed

+57
-28
lines changed

8 files changed

+57
-28
lines changed

cmd/zstream/zstream_recompress.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,8 @@ zstream_do_recompress(int argc, char *argv[])
288288
abd_t *pabd =
289289
abd_get_from_buf_struct(&abd, buf, bufsz);
290290
size_t csize = zio_compress_data(ctype, &dabd,
291-
&pabd, drrw->drr_logical_size, level);
291+
&pabd, drrw->drr_logical_size,
292+
drrw->drr_logical_size, level);
292293
size_t rounded =
293294
P2ROUNDUP(csize, SPA_MINBLOCKSIZE);
294295
if (rounded >= drrw->drr_logical_size) {

include/sys/zio.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
* Copyright (c) 2019, Allan Jude
3030
* Copyright (c) 2019, 2023, 2024, Klara Inc.
3131
* Copyright (c) 2019-2020, Michael Niewöhner
32+
* Copyright (c) 2024 by George Melikov. All rights reserved.
3233
*/
3334

3435
#ifndef _ZIO_H
@@ -603,6 +604,8 @@ extern int zio_alloc_zil(spa_t *spa, objset_t *os, uint64_t txg,
603604
extern void zio_flush(zio_t *zio, vdev_t *vd);
604605
extern void zio_shrink(zio_t *zio, uint64_t size);
605606

607+
extern size_t zio_get_compression_max_size(uint64_t gcd_alloc,
608+
uint64_t min_alloc, size_t s_len);
606609
extern int zio_wait(zio_t *zio);
607610
extern void zio_nowait(zio_t *zio);
608611
extern void zio_execute(void *zio);

include/sys/zio_compress.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
* Copyright (c) 2019, 2024, Klara, Inc.
2626
* Use is subject to license terms.
2727
* Copyright (c) 2015, 2016 by Delphix. All rights reserved.
28+
* Copyright (c) 2021, 2024 by George Melikov. All rights reserved.
2829
*/
2930

3031
#ifndef _SYS_ZIO_COMPRESS_H
@@ -174,7 +175,7 @@ extern int zfs_lz4_decompress(abd_t *src, abd_t *dst, size_t s_len,
174175
* Compress and decompress data if necessary.
175176
*/
176177
extern size_t zio_compress_data(enum zio_compress c, abd_t *src, abd_t **dst,
177-
size_t s_len, uint8_t level);
178+
size_t s_len, size_t d_len, uint8_t level);
178179
extern int zio_decompress_data(enum zio_compress c, abd_t *src, abd_t *abd,
179180
size_t s_len, size_t d_len, uint8_t *level);
180181
extern int zio_compress_to_feature(enum zio_compress comp);

man/man7/zfsprops.7

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -917,14 +917,24 @@ zeroes (the NUL byte).
917917
When a zero-filled block is detected, it is stored as
918918
a hole and not compressed using the indicated compression algorithm.
919919
.Pp
920-
Any block being compressed must be no larger than 7/8 of its original size
921-
after compression, otherwise the compression will not be considered worthwhile
922-
and the block saved uncompressed.
923-
Note that when the logical block is less than
924-
8 times the disk sector size this effectively reduces the necessary compression
925-
ratio; for example, 8 KiB blocks on disks with 4 KiB disk sectors must compress
926-
to 1/2
927-
or less of their original size.
920+
All blocks are allocated as a whole number of sectors
921+
.Pq chunks of 2^ Ns Sy ashift No bytes , e.g . Sy 512B No or Sy 4KB .
922+
Compression may result in a non-sector-aligned size, which will be rounded up
923+
to a whole number of sectors.
924+
If compression saves less than one whole sector,
925+
the block will be stored uncompressed.
926+
Therefore, blocks whose logical size is a small number of sectors will
927+
experience less compression
928+
(e.g. for
929+
.Sy recordsize Ns = Ns Sy 16K
930+
with
931+
.Sy 4K
932+
sectors, which have 4 sectors per block,
933+
compression needs to save at least 25% to actually save space on disk).
934+
.Pp
935+
There is
936+
.Sy 12.5%
937+
default compression threshold in addition to sector rounding.
928938
.It Xo
929939
.Sy context Ns = Ns Sy none Ns | Ns
930940
.Ar SELinux-User : Ns Ar SELinux-Role : Ns Ar SELinux-Type : Ns Ar Sensitivity-Level

module/zfs/arc.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
* Copyright (c) 2019, 2023, Klara Inc.
3030
* Copyright (c) 2019, Allan Jude
3131
* Copyright (c) 2020, The FreeBSD Foundation [1]
32+
* Copyright (c) 2021, 2024 by George Melikov. All rights reserved.
3233
*
3334
* [1] Portions of this software were developed by Allan Jude
3435
* under sponsorship from the FreeBSD Foundation.
@@ -1786,7 +1787,7 @@ arc_hdr_authenticate(arc_buf_hdr_t *hdr, spa_t *spa, uint64_t dsobj)
17861787
!HDR_COMPRESSION_ENABLED(hdr)) {
17871788
abd = NULL;
17881789
csize = zio_compress_data(HDR_GET_COMPRESS(hdr),
1789-
hdr->b_l1hdr.b_pabd, &abd, lsize, hdr->b_complevel);
1790+
hdr->b_l1hdr.b_pabd, &abd, lsize, lsize, hdr->b_complevel);
17901791
ASSERT3P(abd, !=, NULL);
17911792
ASSERT3U(csize, <=, psize);
17921793
abd_zero_off(abd, csize, psize - csize);
@@ -9029,8 +9030,8 @@ l2arc_apply_transforms(spa_t *spa, arc_buf_hdr_t *hdr, uint64_t asize,
90299030
if (compress != ZIO_COMPRESS_OFF && !HDR_COMPRESSION_ENABLED(hdr)) {
90309031
cabd = abd_alloc_for_io(MAX(size, asize), ismd);
90319032
uint64_t csize = zio_compress_data(compress, to_write, &cabd,
9032-
size, hdr->b_complevel);
9033-
if (csize > psize) {
9033+
size, MIN(size, psize), hdr->b_complevel);
9034+
if (csize >= size || csize > psize) {
90349035
/*
90359036
* We can't re-compress the block into the original
90369037
* psize. Even if it fits into asize, it does not
@@ -10521,9 +10522,11 @@ l2arc_log_blk_commit(l2arc_dev_t *dev, zio_t *pio, l2arc_write_callback_t *cb)
1052110522
*/
1052210523
list_insert_tail(&cb->l2wcb_abd_list, abd_buf);
1052310524

10524-
/* try to compress the buffer */
10525+
/* try to compress the buffer, at least one sector to save */
1052510526
psize = zio_compress_data(ZIO_COMPRESS_LZ4,
10526-
abd_buf->abd, &abd, sizeof (*lb), 0);
10527+
abd_buf->abd, &abd, sizeof (*lb),
10528+
zio_get_compression_max_size(dev->l2ad_vdev->vdev_ashift,
10529+
dev->l2ad_vdev->vdev_ashift, sizeof (*lb)), 0);
1052710530

1052810531
/* a log block is never entirely zero */
1052910532
ASSERT(psize != 0);

module/zfs/dmu_recv.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1408,7 +1408,7 @@ do_corrective_recv(struct receive_writer_arg *rwa, struct drr_write *drrw,
14081408
abd_t *cabd = abd_alloc_linear(BP_GET_PSIZE(bp),
14091409
B_FALSE);
14101410
uint64_t csize = zio_compress_data(BP_GET_COMPRESS(bp),
1411-
abd, &cabd, abd_get_size(abd),
1411+
abd, &cabd, abd_get_size(abd), BP_GET_PSIZE(bp),
14121412
rwa->os->os_complevel);
14131413
abd_zero_off(cabd, csize, BP_GET_PSIZE(bp) - csize);
14141414
/* Swap in newly compressed data into the abd */

module/zfs/zio.c

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
* Copyright (c) 2019, 2023, 2024, Klara Inc.
2727
* Copyright (c) 2019, Allan Jude
2828
* Copyright (c) 2021, Datto, Inc.
29+
* Copyright (c) 2021, 2024 by George Melikov. All rights reserved.
2930
*/
3031

3132
#include <sys/sysmacros.h>
@@ -1704,6 +1705,21 @@ zio_roundup_alloc_size(spa_t *spa, uint64_t size)
17041705
return (spa->spa_min_alloc);
17051706
}
17061707

1708+
size_t
1709+
zio_get_compression_max_size(uint64_t gcd_alloc, uint64_t min_alloc,
1710+
size_t s_len)
1711+
{
1712+
size_t d_len;
1713+
1714+
/* minimum 12.5% must be saved (legacy value, may be changed later) */
1715+
d_len = s_len - (s_len >> 3);
1716+
1717+
d_len = d_len - d_len % gcd_alloc;
1718+
if (d_len < min_alloc)
1719+
return (BPE_PAYLOAD_SIZE);
1720+
return (d_len);
1721+
}
1722+
17071723
/*
17081724
* ==========================================================================
17091725
* Prepare to read and write logical blocks
@@ -1885,7 +1901,10 @@ zio_write_compress(zio_t *zio)
18851901
psize = lsize;
18861902
else
18871903
psize = zio_compress_data(compress, zio->io_abd, &cabd,
1888-
lsize, zp->zp_complevel);
1904+
lsize,
1905+
zio_get_compression_max_size(spa->spa_gcd_alloc,
1906+
spa->spa_min_alloc, lsize),
1907+
zp->zp_complevel);
18891908
if (psize == 0) {
18901909
compress = ZIO_COMPRESS_OFF;
18911910
} else if (psize >= lsize) {

module/zfs/zio_compress.c

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,11 @@
2222
/*
2323
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
2424
* Use is subject to license terms.
25-
*/
26-
/*
2725
* Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
28-
*/
29-
30-
/*
3126
* Copyright (c) 2013, 2018 by Delphix. All rights reserved.
3227
* Copyright (c) 2019, 2024, Klara, Inc.
3328
* Copyright (c) 2019, Allan Jude
29+
* Copyright (c) 2021, 2024 by George Melikov. All rights reserved.
3430
*/
3531

3632
#include <sys/zfs_context.h>
@@ -129,9 +125,9 @@ zio_compress_select(spa_t *spa, enum zio_compress child,
129125

130126
size_t
131127
zio_compress_data(enum zio_compress c, abd_t *src, abd_t **dst, size_t s_len,
132-
uint8_t level)
128+
size_t d_len, uint8_t level)
133129
{
134-
size_t c_len, d_len;
130+
size_t c_len;
135131
uint8_t complevel;
136132
zio_compress_info_t *ci = &zio_compress_table[c];
137133

@@ -156,15 +152,11 @@ zio_compress_data(enum zio_compress c, abd_t *src, abd_t **dst, size_t s_len,
156152
if (*dst == NULL)
157153
*dst = abd_alloc_sametype(src, s_len);
158154

159-
/* Compress at least 12.5%, but limit to the size of the dest abd. */
160-
d_len = MIN(s_len - (s_len >> 3), abd_get_size(*dst));
161-
162155
c_len = ci->ci_compress(src, *dst, s_len, d_len, complevel);
163156

164157
if (c_len > d_len)
165158
return (s_len);
166159

167-
ASSERT3U(c_len, <=, d_len);
168160
return (c_len);
169161
}
170162

0 commit comments

Comments
 (0)