: lib/lzo: run-length encoding support
Subsystems (4)
v5 cover : lib/lzo: run-length encoding support
Hi,
Following on from the previous lzo-rle patchset:
https://lkml.org/lkml/2018/11/30/972
This patchset contains only the RLE patches, and should be applied on top of the non-RLE patches ( https://lkml.org/lkml/2019/2/5/366 ).
Previously, some questions were raised around the RLE patches. I've done some additional benchmarking to answer these questions. In short:
- RLE offers significant additional performance (data-dependent)
- I didn't measure any regressions that were clearly outside the noise
One concern with this patchset was around performance - specifically, measuring RLE impact separately from Matt Sealey's patches (CTZ & fast copy). I have done some additional benchmarking which I hope clarifies the benefits of each part of the patchset.
Firstly, I've captured some memory via /dev/fmem from a Chromebook with many tabs open which is starting to swap, and then split this into 4178 4k pages. I've excluded the all-zero pages (as zram does), and also the no-zero pages (which won't tell us anything about RLE performance). This should give a realistic test dataset for zram. What I found was that the data is VERY
bimodal: 44% of pages in this dataset contain 5% or fewer zeros, and 44% contain over 90% zeros (30% if you include the no-zero pages). This supports the idea of special-casing zeros in zram.
Next, I've benchmarked four variants of lzo on these pages (on 64-bit Arm at max frequency): baseline LZO; baseline + Matt Sealey's patches (aka MS); baseline + RLE only; baseline + MS + RLE. Numbers are for weighted roundtrip throughput (the weighting reflects that zram does more compression than decompression).
https://drive.google.com/file/d/1VLtLjRVxgUNuWFOxaGPwJYhl_hMQXpHe/view?usp=sharing
Matt's patches help in all cases for Arm (and no effect on Intel), as expected.
RLE also behaves as expected: with few zeros present, it makes no difference; above ~75%, it gives a good improvement (50 - 300 MB/s on top of the benefit from Matt's patches).
Best performance is seen with both MS and RLE patches.
Finally, I have benchmarked the same dataset on an x86-64 device. Here, the MS patches make no difference (as expected); RLE helps, similarly as on Arm. There were no definite regressions; allowing for observational error, 0.1% (3/4178) of cases had a regression > 1 standard deviation, of which the largest was 4.6% (1.2 standard deviations). I think this is probably within the noise.
https://drive.google.com/file/d/1xCUVwmiGD0heEMx5gcVEmLBI4eLaageV/view?usp=sharing
One point to note is that the graphs show RLE appears to help very slightly with no zeros present! This is because the extra code causes the clang optimiser to change code layout in a way that happens to have a significant benefit. Taking baseline LZO and adding a do-nothing line like "__builtin_prefetch(out_len);" immediately before the "goto next" has the same effect. So this is a real, but basically spurious effect - it's small enough not to upset the overall findings.
Dave
PATCH v5 1/3 lib/lzo: implement run-length encoding
When using zram, we frequently encounter long runs of zero bytes. This adds a special case which identifies runs of zeros and encodes them using run-length encoding.
This is faster for both compression and decompresion. For high-entropy data which doesn't hit this case, impact is minimal.
Compression ratio is within a few percent in all cases.
This modifies the bitstream in a way which is backwards compatible (i.e., we can decompress old bitstreams, but old versions of lzo cannot decompress new bitstreams).
--- Documentation/lzo.txt | 35 ++++++++--- include/linux/lzo.h | 2 +- lib/lzo/lzo1x_compress.c | 100 ++++++++++++++++++++++++++++---- lib/lzo/lzo1x_decompress_safe.c | 75 ++++++++++++++++-------- lib/lzo/lzodefs.h | 12 +++- 5 files changed, 181 insertions(+), 43 deletions(-)
Documentation/lzo.txt +28 -7
@@ -78,16 +78,30 @@ Description is an implementation design choice independent on the algorithm or encoding. +Versions + +0: Original version +1: LZO-RLE + +Version 1 of LZO implements an extension to encode runs of zeros using run +length encoding. This improves speed for data with many zeros, which is a +common case for zram. This modifies the bitstream in a backwards compatible way +(v1 can correctly decompress v0 compressed data, but v0 cannot read v1 data). + Byte sequences ============== First byte encoding:: - 0..17 : follow regular instruction encoding, see below. It is worth - noting that codes 16 and 17 will represent a block copy from - the dictionary which is empty, and that they will always be + 0..16 : follow regular instruction encoding, see below. It is worth + noting that code 16 will represent a block copy from the + dictionary which is empty, and that it will always be invalid at this place. + 17 : bitstream version. If the first byte is 17, the next byte + gives the bitstream version. If the first byte is not 17, + the bitstream version is 0. + 18..21 : copy 0..3 literals state = (byte - 17) = 0..3 [ copy <state> literals ] skip byte
@@ -140,6 +154,11 @@ Byte sequences state = S (copy S literals after this block) End of stream is reached if distance == 16384 + In version 1, this instruction is also used to encode a run of zeros if + distance = 0xbfff, i.e. H = 1 and the D bits are all 1. + In this case, it is followed by a fourth byte, X. + run length = ((X << 3) | (0 0 0 0 0 L L L)) + 4. + 0 0 1 L L L L L (32..63) Copy of small block within 16kB distance (preferably less than 34B) length = 2 + (L ?: 31 + (zero_bytes * 255) + non_zero_byte)
@@ -165,7 +184,9 @@ Authors ======= This document was written by Willy Tarreau <[email protected]> on 2014/07/19 during an - analysis of the decompression code available in Linux 3.16-rc5. The code is - tricky, it is possible that this document contains mistakes or that a few - corner cases were overlooked. In any case, please report any doubt, fix, or - proposed updates to the author(s) so that the document can be updated. + analysis of the decompression code available in Linux 3.16-rc5, and updated + by Dave Rodgman <[email protected]> on 2018/10/30 to introduce run-length + encoding. The code is tricky, it is possible that this document contains + mistakes or that a few corner cases were overlooked. In any case, please + report any doubt, fix, or proposed updates to the author(s) so that the + document can be updated.
include/linux/lzo.h +1 -1
@@ -18,7 +18,7 @@#define LZO1X_1_MEM_COMPRESS (8192 * sizeof(unsigned short))#define LZO1X_MEM_COMPRESS LZO1X_1_MEM_COMPRESS -#define lzo1x_worst_compress (x) ((x) + ((x) / 16) + 64 + 3) +#define lzo1x_worst_compress (x) ((x) + ((x) / 16) + 64 + 3 + 2)/* This requires 'wrkmem' of size LZO1X_1_MEM_COMPRESS */ int lzo1x_1_compress(const unsigned char *src, size_t src_len,
lib/lzo/lzo1x_compress.c +89 -11
162 lines not on this page · raw message
lib/lzo/lzo1x_decompress_safe.c +52 -23
95 lines not on this page · raw message
lib/lzo/lzodefs.h +11 -1
@@ -13,6 +13,12 @@*/ +/* Version +* 0: original lzo version +* 1: lzo with support for RLE +*/ +#define LZO_VERSION 1 +#define COPY4 (dst, src) \ put_unaligned(get_unaligned((const u32 *)(src)), (u32 *)(dst))#if defined(CONFIG_X86_64 ) || defined(CONFIG_ARM64 )
@@ -28,6 +34,7 @@#elif defined(CONFIG_X86_64 ) || defined(CONFIG_ARM64 )#define LZO_USE_CTZ64 1#define LZO_USE_CTZ32 1 +#define LZO_FAST_64BIT_MEMORY_ACCESS #elif defined(CONFIG_X86 ) || defined(CONFIG_PPC )#define LZO_USE_CTZ32 1#elif defined(CONFIG_ARM ) && (__LINUX_ARM_ARCH__ >=5 )
@@ -37,7 +44,7 @@#define M1_MAX_OFFSET 0x0400#define M2_MAX_OFFSET 0x0800#define M3_MAX_OFFSET 0x4000 -#define M4_MAX_OFFSET 0xbfff +#define M4_MAX_OFFSET 0xbffe#define M1_MIN_LEN 2#define M1_MAX_LEN 2
@@ -53,6 +60,9 @@#define M3_MARKER 32#define M4_MARKER 16 +#define MIN_ZERO_RUN_LENGTH 4 +#define MAX_ZERO_RUN_LENGTH (2047 + MIN_ZERO_RUN_LENGTH) +#define lzo_dict_t unsigned short#define D_BITS 13#define D_SIZE (1u << D_BITS)
Signature · 1 line
2.17.1
PATCH v5 2/3 lib/lzo: separate lzo-rle from lzo
In reply to Dave RodgmanTo prevent any issues with persistent data, separate lzo-rle from lzo so that it is treated as a separate algorithm, and lzo is still available.
--- Documentation/lzo.txt | 12 ++- crypto/Makefile | 2 +- crypto/lzo-rle.c | 175 +++++++++++++++++++++++++++++++++++++ crypto/tcrypt.c | 4 +- drivers/block/zram/zcomp.c | 1 + include/linux/lzo.h | 4 + lib/lzo/lzo1x_compress.c | 42 +++++++-- lib/lzo/lzodefs.h | 3 +- 8 files changed, 226 insertions(+), 17 deletions(-) create mode 100644 crypto/lzo-rle.c
Documentation/lzo.txt +8 -4
@@ -88,6 +88,10 @@ length encoding. This improves speed for data with many zeros, which is a common case for zram. This modifies the bitstream in a backwards compatible way (v1 can correctly decompress v0 compressed data, but v0 cannot read v1 data). +For maximum compatibility, both versions are available under different names +(lzo and lzo-rle). Differences in the encoding are noted in this document with +e.g.: version 1 only. + Byte sequences ==============
@@ -99,8 +103,8 @@ Byte sequences invalid at this place. 17 : bitstream version. If the first byte is 17, the next byte - gives the bitstream version. If the first byte is not 17, - the bitstream version is 0. + gives the bitstream version (version 1 only). If the first byte + is not 17, the bitstream version is 0. 18..21 : copy 0..3 literals state = (byte - 17) = 0..3 [ copy <state> literals ]
@@ -154,8 +158,8 @@ Byte sequences state = S (copy S literals after this block) End of stream is reached if distance == 16384 - In version 1, this instruction is also used to encode a run of zeros if - distance = 0xbfff, i.e. H = 1 and the D bits are all 1. + In version 1 only, this instruction is also used to encode a run of + zeros if distance = 0xbfff, i.e. H = 1 and the D bits are all 1. In this case, it is followed by a fourth byte, X. run length = ((X << 3) | (0 0 0 0 0 L L L)) + 4.
crypto/Makefile +1 -1
@@ -128,7 +128,7 @@ obj-$(CONFIG_CRYPTO_CRC32C) += crc32c_generic.o obj-$(CONFIG_CRYPTO_CRC32) += crc32_generic.o obj-$(CONFIG_CRYPTO_CRCT10DIF) += crct10dif_common.o crct10dif_generic.o obj-$(CONFIG_CRYPTO_AUTHENC) += authenc.o authencesn.o -obj-$(CONFIG_CRYPTO_LZO) += lzo.o +obj-$(CONFIG_CRYPTO_LZO) += lzo.o lzo-rle.o obj-$(CONFIG_CRYPTO_LZ4) += lz4.o obj-$(CONFIG_CRYPTO_LZ4HC) += lz4hc.o obj-$(CONFIG_CRYPTO_842) += 842.o
crypto/lzo-rle.c new file +175 -0
175 lines not on this page · raw message
crypto/tcrypt.c +2 -2
@@ -76,8 +76,8 @@ static char *check[] = {"cast6" ,"arc4" ,"michael_mic" ,"deflate" ,"crc32c" ,"tea" ,"xtea" ,"khazad" ,"wp512" ,"wp384" ,"wp256" ,"tnepres" ,"xeta" ,"fcrypt" ,"camellia" ,"seed" ,"salsa20" ,"rmd128" ,"rmd160" ,"rmd256" ,"rmd320" , -"lzo" ,"cts" ,"sha3-224" ,"sha3-256" ,"sha3-384" ,"sha3-512" , -"streebog256" ,"streebog512" , +"lzo" ,"lzo-rle" ,"cts" ,"sha3-224" ,"sha3-256" ,"sha3-384" , +"sha3-512" ,"streebog256" ,"streebog512" ,NULL };
drivers/block/zram/zcomp.c +1 -0
@@ -20,6 +20,7 @@static const char *const backends[] = {"lzo" , +"lzo-rle" , #if IS_ENABLED(CONFIG_CRYPTO_LZ4)"lz4" , #endif
include/linux/lzo.h +4 -0
@@ -24,6 +24,10 @@int lzo1x_1_compress (const unsigned char *src,size_t src_len,unsigned char *dst,size_t *dst_len,void *wrkmem); +/* This requires 'wrkmem' of size LZO1X_1_MEM_COMPRESS */ +int lzorle1x_1_compress (const unsigned char *src,size_t src_len, +unsigned char *dst,size_t *dst_len,void *wrkmem); +/* safe decompression with overrun testing */ int lzo1x_decompress_safe (const unsigned char *src,size_t src_len,unsigned char *dst,size_t *dst_len);
lib/lzo/lzo1x_compress.c +33 -9
84 lines not on this page · raw message
lib/lzo/lzodefs.h +2 -1
@@ -44,7 +44,8 @@#define M1_MAX_OFFSET 0x0400#define M2_MAX_OFFSET 0x0800#define M3_MAX_OFFSET 0x4000 -#define M4_MAX_OFFSET 0xbffe +#define M4_MAX_OFFSET_V0 0xbfff +#define M4_MAX_OFFSET_V1 0xbffe#define M1_MIN_LEN 2#define M1_MAX_LEN 2
Signature · 1 line
2.17.1
PATCH v5 3/3 zram: default to lzo-rle instead of lzo
In reply to Dave Rodgmanlzo-rle gives higher performance and similar compression ratios to lzo.
--- drivers/block/zram/zram_drv.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/block/zram/zram_drv.c +1 -1
@@ -41,7 +41,7 @@ static DEFINE_IDR(zram_index_idr);static DEFINE_MUTEX (zram_index_mutex);static int zram_major; -static const char *default_compressor ="lzo" ; +static const char *default_compressor ="lzo-rle" ;/* Module params (documentation at end) */ static unsigned int num_devices =1 ;
Signature · 1 line
2.17.1
Hi Dave,
Quoted 9 lines · Dave Rodgman
Hi,
Following on from the previous lzo-rle patchset:
https://lkml.org/lkml/2018/11/30/972
This patchset contains only the RLE patches, and should be applied on top of the non-RLE patches ( https://lkml.org/lkml/2019/2/5/366 ).
Sorry for the interruption, since it is an old patchset and discussion. I have a few questions on lzo-rle support, hope you can give me some directions, thanks in advance!
1) Is lzo-rle suitable for userspace library? I've checked the current userspace lzo library lzo-2.10, it seems no lzo-rle support (Please correct me if I'm wrong). If lzo-rle have better performance in kernel, then is it possible to implement one in userspace and gain better performance as well?
2) Currently Yulong TANG have encountered problem that, crash utility cannot decompress a lzo-rle compressed zram since kernel 5.1 [1], since there is no lzo-rle support for current lzo library, crash have to import the kernel source code directly into crash, which is not good for crash utility code maintainance. It will be better if we can update lzo library with lzo-rle support. I guess not only crash, but also other kernel debugging tools running in userspace such as drgn may also need this feature.
Do you have any suggestions on for these?
[1]: https://www.mail-archive.com/[email protected]/msg00475.html
Thanks,
Tao Liu
Quoted 57 lines
Previously, some questions were raised around the RLE patches. I've done some additional benchmarking to answer these questions. In short:
- RLE offers significant additional performance (data-dependent)
- I didn't measure any regressions that were clearly outside the noiseOne concern with this patchset was around performance - specifically, measuring RLE impact separately from Matt Sealey's patches (CTZ & fast copy). I have done some additional benchmarking which I hope clarifies the benefits of each part of the patchset.
Firstly, I've captured some memory via
/dev/fmemfrom a Chromebook with many tabs open which is starting to swap, and then split this into 4178 4k pages. I've excluded the all-zero pages (as zram does), and also the no-zero pages (which won't tell us anything about RLE performance). This should give a realistic test dataset for zram. What I found was that the data is VERY
bimodal: 44% of pages in this dataset contain 5% or fewer zeros, and 44% contain over 90% zeros (30% if you include the no-zero pages). This supports the idea of special-casing zeros in zram.Next, I've benchmarked four variants of lzo on these pages (on 64-bit Arm at max frequency): baseline LZO; baseline + Matt Sealey's patches (aka MS); baseline + RLE only; baseline + MS + RLE. Numbers are for weighted roundtrip throughput (the weighting reflects that zram does more compression than decompression).
https://drive.google.com/file/d/1VLtLjRVxgUNuWFOxaGPwJYhl_hMQXpHe/view?usp=sharing
Matt's patches help in all cases for Arm (and no effect on Intel), as expected.
RLE also behaves as expected: with few zeros present, it makes no difference; above ~75%, it gives a good improvement (50 - 300 MB/s on top of the benefit from Matt's patches).
Best performance is seen with both MS and RLE patches.
Finally, I have benchmarked the same dataset on an x86-64 device. Here, the MS patches make no difference (as expected); RLE helps, similarly as on Arm. There were no definite regressions; allowing for observational error, 0.1% (3/4178) of cases had a regression > 1 standard deviation, of which the largest was 4.6% (1.2 standard deviations). I think this is probably within the noise.
https://drive.google.com/file/d/1xCUVwmiGD0heEMx5gcVEmLBI4eLaageV/view?usp=sharing
One point to note is that the graphs show RLE appears to help very slightly with no zeros present! This is because the extra code causes the clang optimiser to change code layout in a way that happens to have a significant benefit. Taking baseline LZO and adding a do-nothing line like "
__builtin_prefetch(out_len);" immediately before the "goto next" has the same effect. So this is a real, but basically spurious effect - it's small enough not to upset the overall findings.Dave
A message that is not in the archive (sent off-list or not yet received): AS8PR08MB102898FB26D627E790FE8D4638F272@AS8PR08MB10289.eurprd08.prod.outlook.com
Quoted 6 lines · Dave Rodgman
Hi Tao,
I don’t see any reason for the upstream LZO library not to pick up the lzo-rle algorithm from the kernel, and I would expect the same performance benefit in userspace. This is really a question for Markus (the owner/maintainer of that library).
Hi Markus,
Is it possible to port the lzo-rle algorithm to the lzo library, so userspace programs such as crash-utility or drgn can use it to decompress the kernel data? Thanks in advance!
Quoted 2 lines
I think the simplest short-term option would be to pull in the lzo library as source into crash-utility, and carry a patch against it to add support for lzo-rle.
Hi Dave,
Thanks for the suggestion! I agree with your short-term option, this is what we are planning to do for now. If lzo-rle has been integrated into the lzo library, we can then delete the patch from crash-utility code.
Thanks,
Tao Liu
Quoted 109 lines
Dave
From: Tao Liu <[email protected]>
Date: Friday, 8 March 2024 at 03:26
To: Dave Rodgman <[email protected]>
Cc: [email protected] <[email protected]>, Matt Sealey <[email protected]>, [email protected] <[email protected]>, [email protected] <[email protected]>, [email protected] <[email protected]>, [email protected] <[email protected]>, [email protected] <[email protected]>, [email protected] <[email protected]>, [email protected] <[email protected]>, [email protected] <[email protected]>, [email protected] <[email protected]>, [email protected] <[email protected]>, [email protected] <[email protected]>, nd <[email protected]>
Subject: Re: [PATCH v5 0/3]: lib/lzo: run-length encoding supportHi Dave,
Quoted 9 lines · Dave Rodgman
Hi,
Following on from the previous lzo-rle patchset:
https://lkml.org/lkml/2018/11/30/972
This patchset contains only the RLE patches, and should be applied on top of the non-RLE patches ( https://lkml.org/lkml/2019/2/5/366 ).
Sorry for the interruption, since it is an old patchset and discussion. I have a few questions on lzo-rle support, hope you can give me some directions, thanks in advance!
1) Is lzo-rle suitable for userspace library? I've checked the current userspace lzo library lzo-2.10, it seems no lzo-rle support (Please correct me if I'm wrong). If lzo-rle have better performance in kernel, then is it possible to implement one in userspace and gain better performance as well?
2) Currently Yulong TANG have encountered problem that, crash utility cannot decompress a lzo-rle compressed zram since kernel 5.1 [1], since there is no lzo-rle support for current lzo library, crash have to import the kernel source code directly into crash, which is not good for crash utility code maintainance. It will be better if we can update lzo library with lzo-rle support. I guess not only crash, but also other kernel debugging tools running in userspace such as drgn may also need this feature.
Do you have any suggestions on for these?
[1]: https://www.mail-archive.com/[email protected]/msg00475.html
Thanks,
Tao LiuQuoted 57 lines
Previously, some questions were raised around the RLE patches. I've done some additional benchmarking to answer these questions. In short:
- RLE offers significant additional performance (data-dependent)
- I didn't measure any regressions that were clearly outside the noiseOne concern with this patchset was around performance - specifically, measuring RLE impact separately from Matt Sealey's patches (CTZ & fast copy). I have done some additional benchmarking which I hope clarifies the benefits of each part of the patchset.
Firstly, I've captured some memory via
/dev/fmemfrom a Chromebook with many tabs open which is starting to swap, and then split this into 4178 4k pages. I've excluded the all-zero pages (as zram does), and also the no-zero pages (which won't tell us anything about RLE performance). This should give a realistic test dataset for zram. What I found was that the data is VERY
bimodal: 44% of pages in this dataset contain 5% or fewer zeros, and 44% contain over 90% zeros (30% if you include the no-zero pages). This supports the idea of special-casing zeros in zram.Next, I've benchmarked four variants of lzo on these pages (on 64-bit Arm at max frequency): baseline LZO; baseline + Matt Sealey's patches (aka MS); baseline + RLE only; baseline + MS + RLE. Numbers are for weighted roundtrip throughput (the weighting reflects that zram does more compression than decompression).
https://drive.google.com/file/d/1VLtLjRVxgUNuWFOxaGPwJYhl_hMQXpHe/view?usp=sharing
Matt's patches help in all cases for Arm (and no effect on Intel), as expected.
RLE also behaves as expected: with few zeros present, it makes no difference; above ~75%, it gives a good improvement (50 - 300 MB/s on top of the benefit from Matt's patches).
Best performance is seen with both MS and RLE patches.
Finally, I have benchmarked the same dataset on an x86-64 device. Here, the MS patches make no difference (as expected); RLE helps, similarly as on Arm. There were no definite regressions; allowing for observational error, 0.1% (3/4178) of cases had a regression > 1 standard deviation, of which the largest was 4.6% (1.2 standard deviations). I think this is probably within the noise.
https://drive.google.com/file/d/1xCUVwmiGD0heEMx5gcVEmLBI4eLaageV/view?usp=sharing
One point to note is that the graphs show RLE appears to help very slightly with no zeros present! This is because the extra code causes the clang optimiser to change code layout in a way that happens to have a significant benefit. Taking baseline LZO and adding a do-nothing line like "
__builtin_prefetch(out_len);" immediately before the "goto next" has the same effect. So this is a real, but basically spurious effect - it's small enough not to upset the overall findings.Dave