Skip to content

Commit 6dfef3f

Browse files
committed
Split zstd into OS dependant files
This allows FreeBSD to use kmem_cache, while Linux uses the custom mempool Also split private bits of sys/zstd/zstd.h out into sys/zstd/zstd_impl.h This new FreeBSD version uses the zio_data_buf_cache's for all sizes less than 16 MB, and uses its own dedicated kmem_caches for the decompression context, and for higher compression levels at larger block sizes that require the larger compression workspace. Signed-off-by: Allan Jude <[email protected]>
1 parent d82d228 commit 6dfef3f

File tree

11 files changed

+301
-1347
lines changed

11 files changed

+301
-1347
lines changed

cmd/zdb/zdb.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
#include <sys/btree.h>
7777
#include <zfs_comutil.h>
7878
#include <sys/zstd/zstd.h>
79+
#include <sys/zstd/zstd_impl.h>
7980

8081
#include <libnvpair.h>
8182
#include <libzutil.h>

configure.ac

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ AC_CONFIG_FILES([
180180
module/nvpair/Makefile
181181
module/os/linux/spl/Makefile
182182
module/os/linux/zfs/Makefile
183+
module/os/linux/zstd/Makefile
183184
module/spl/Makefile
184185
module/unicode/Makefile
185186
module/zcommon/Makefile

include/sys/zstd/Makefile.am

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
COMMON_H = \
2-
$(top_srcdir)/include/sys/zstd/zstd.h
2+
$(top_srcdir)/include/sys/zstd/zstd.h \
3+
$(top_srcdir)/include/sys/zstd/zstd_impl.h
34

45
KERNEL_H =
56

include/sys/zstd/zstd.h

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -46,42 +46,11 @@
4646
extern "C" {
4747
#endif
4848

49-
/*
50-
* ZSTD block header
51-
* NOTE: all fields in this header are in big endian order.
52-
*/
53-
typedef struct zfs_zstd_header {
54-
/* Compressed size of data */
55-
uint32_t c_len;
56-
57-
/*
58-
* Version and compression level
59-
* We use a union to be able to big endian encode a single 32 bit
60-
* unsigned integer, but still access the individual bitmasked
61-
* components easily.
62-
*/
63-
union {
64-
uint32_t raw_version_level;
65-
struct {
66-
uint32_t version : 24;
67-
uint8_t level;
68-
};
69-
};
70-
71-
char data[];
72-
} zfs_zstdhdr_t;
73-
74-
/*
75-
* kstat helper macros
76-
*/
77-
#define ZSTDSTAT(stat) (zstd_stats.stat.value.ui64)
78-
#define ZSTDSTAT_INCR(stat, val) \
79-
atomic_add_64(&zstd_stats.stat.value.ui64, (val))
80-
#define ZSTDSTAT_BUMP(stat) ZSTDSTAT_INCR(stat, 1)
81-
8249
/* (de)init for user space / kernel emulation */
8350
int zstd_init(void);
8451
void zstd_fini(void);
52+
extern int zstd_init_os(void);
53+
extern void zstd_fini_os(void);
8554

8655
size_t zfs_zstd_compress(void *s_start, void *d_start, size_t s_len,
8756
size_t d_len, int level);

include/sys/zstd/zstd_impl.h

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*
2+
* BSD 3-Clause New License (https://spdx.org/licenses/BSD-3-Clause.html)
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions are met:
6+
*
7+
* 1. Redistributions of source code must retain the above copyright notice,
8+
* this list of conditions and the following disclaimer.
9+
*
10+
* 2. Redistributions in binary form must reproduce the above copyright notice,
11+
* this list of conditions and the following disclaimer in the documentation
12+
* and/or other materials provided with the distribution.
13+
*
14+
* 3. Neither the name of the copyright holder nor the names of its
15+
* contributors may be used to endorse or promote products derived from this
16+
* software without specific prior written permission.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
*/
30+
31+
/*
32+
* Copyright (c) 2020, The FreeBSD Foundation [1]
33+
*
34+
* [1] Portions of this software were developed by Allan Jude
35+
* under sponsorship from the FreeBSD Foundation.
36+
*/
37+
38+
#ifndef _ZFS_ZSTD_IMPL_H
39+
#define _ZFS_ZSTD_IMPL_H
40+
41+
#include <sys/zfs_context.h>
42+
43+
#ifdef __cplusplus
44+
extern "C" {
45+
#endif
46+
47+
/*
48+
* ZSTD block header
49+
* NOTE: all fields in this header are in big endian order.
50+
*/
51+
typedef struct zfs_zstd_header {
52+
/* Compressed size of data */
53+
uint32_t c_len;
54+
55+
/*
56+
* Version and compression level
57+
* We use a union to be able to big endian encode a single 32 bit
58+
* unsigned integer, but still access the individual bitmasked
59+
* components easily.
60+
*/
61+
union {
62+
uint32_t raw_version_level;
63+
struct {
64+
uint32_t version : 24;
65+
uint8_t level;
66+
};
67+
};
68+
69+
char data[];
70+
} zfs_zstdhdr_t;
71+
72+
typedef struct zstd_stats {
73+
kstat_named_t zstd_stat_alloc_fail;
74+
kstat_named_t zstd_stat_alloc_fallback;
75+
kstat_named_t zstd_stat_com_alloc_fail;
76+
kstat_named_t zstd_stat_dec_alloc_fail;
77+
kstat_named_t zstd_stat_com_inval;
78+
kstat_named_t zstd_stat_dec_inval;
79+
kstat_named_t zstd_stat_dec_header_inval;
80+
kstat_named_t zstd_stat_com_fail;
81+
kstat_named_t zstd_stat_dec_fail;
82+
} zstd_stats_t;
83+
84+
static zstd_stats_t zstd_stats __maybe_unused = {
85+
{ "alloc_fail", KSTAT_DATA_UINT64 },
86+
{ "alloc_fallback", KSTAT_DATA_UINT64 },
87+
{ "compress_alloc_fail", KSTAT_DATA_UINT64 },
88+
{ "decompress_alloc_fail", KSTAT_DATA_UINT64 },
89+
{ "compress_level_invalid", KSTAT_DATA_UINT64 },
90+
{ "decompress_level_invalid", KSTAT_DATA_UINT64 },
91+
{ "decompress_header_invalid", KSTAT_DATA_UINT64 },
92+
{ "compress_failed", KSTAT_DATA_UINT64 },
93+
{ "decompress_failed", KSTAT_DATA_UINT64 },
94+
};
95+
96+
/*
97+
* ZSTD memory handlers
98+
*
99+
* For decompression we use a different handler which also provides fallback
100+
* memory allocation in case memory runs out.
101+
*
102+
* The ZSTD handlers were split up for the most simplified implementation.
103+
*/
104+
extern void *zstd_alloc(void *opaque, size_t size);
105+
extern void *zstd_dctx_alloc(void *opaque, size_t size);
106+
extern void zstd_free(void *opaque, void *ptr);
107+
108+
/*
109+
* kstat helper macros
110+
*/
111+
#define ZSTDSTAT(stat) (zstd_stats.stat.value.ui64)
112+
#define ZSTDSTAT_INCR(stat, val) \
113+
atomic_add_64(&zstd_stats.stat.value.ui64, (val))
114+
#define ZSTDSTAT_BUMP(stat) ZSTDSTAT_INCR(stat, 1)
115+
116+
#ifdef __cplusplus
117+
}
118+
#endif
119+
120+
#endif /* _ZFS_ZSTD_IMPL_H */

lib/libzstd/Makefile.am

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
include $(top_srcdir)/config/Rules.am
22

33
VPATH = $(top_srcdir)/module/zstd
4+
VPATH += $(top_srcdir)/module/os/linux/zstd
5+
6+
DEFAULT_INCLUDES += -I$(top_srcdir)/module/zstd
47

58
# -fno-tree-vectorize is set for gcc in zstd/common/compiler.h
69
# Set it for other compilers, too.
@@ -10,7 +13,8 @@ noinst_LTLIBRARIES = libzstd.la
1013

1114
KERNEL_C = \
1215
lib/zstd.c \
13-
zfs_zstd.c
16+
zfs_zstd.c \
17+
zfs_zstd_os.c
1418

1519
nodist_libzstd_la_SOURCES = $(KERNEL_C)
1620

@@ -19,3 +23,5 @@ lib/zstd.l$(OBJEXT): CFLAGS += -fno-tree-vectorize -include $(top_srcdir)/module
1923

2024
zfs_zstd.$(OBJEXT): CFLAGS += -include $(top_srcdir)/module/zstd/include/zstd_compat_wrapper.h
2125
zfs_zstd.l$(OBJEXT): CFLAGS += -include $(top_srcdir)/module/zstd/include/zstd_compat_wrapper.h
26+
zfs_zstd_os.$(OBJEXT): CFLAGS += -include $(top_srcdir)/module/zstd/include/zstd_compat_wrapper.h
27+
zfs_zstd_os.l$(OBJEXT): CFLAGS += -include $(top_srcdir)/module/zstd/include/zstd_compat_wrapper.h

module/Makefile.bsd

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ KMOD= openzfs
1414
${SRCDIR}/nvpair \
1515
${SRCDIR}/os/freebsd/spl \
1616
${SRCDIR}/os/freebsd/zfs \
17+
${SRCDIR}/os/freebsd/zstd \
1718
${SRCDIR}/unicode \
1819
${SRCDIR}/zcommon \
1920
${SRCDIR}/zfs \
@@ -28,6 +29,7 @@ CFLAGS+= -I${INCDIR}/spl
2829
CFLAGS+= -I${INCDIR}/os/freebsd
2930
CFLAGS+= -I${INCDIR}/os/freebsd/spl
3031
CFLAGS+= -I${INCDIR}/os/freebsd/zfs
32+
CFLAGS+= -I${SRCDIR}/zstd
3133
CFLAGS+= -I${SRCDIR}/zstd/include
3234
CFLAGS+= -include ${INCDIR}/os/freebsd/spl/sys/ccompile.h
3335

@@ -298,6 +300,7 @@ SRCS+= abd.c \
298300

299301
#zstd
300302
SRCS+= zfs_zstd.c \
303+
zfs_zstd_os.c \
301304
zstd.c
302305

303306
beforeinstall:
@@ -356,4 +359,5 @@ CFLAGS.zil.c= -Wno-cast-qual
356359
CFLAGS.zio.c= -Wno-cast-qual
357360
CFLAGS.zrlock.c= -Wno-cast-qual
358361
CFLAGS.zfs_zstd.c= -Wno-cast-qual -Wno-pointer-arith
362+
CFLAGS.zfs_zstd_os.c= -Wno-cast-qual -Wno-pointer-arith
359363
CFLAGS.zstd.c= -fno-tree-vectorize -U__BMI__

0 commit comments

Comments
 (0)