Skip to content

Commit 127ba0f

Browse files
committed
Split zstd into OS dependant files
This allows FreeBSD to use UMA, while Linux uses the custom mempool Also split private bits of sys/zstd/zstd.h 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. Sponsored-by: The FreeBSD Foundation Signed-off-by: Allan Jude <[email protected]>
1 parent 08cdb4d commit 127ba0f

File tree

13 files changed

+309
-1382
lines changed

13 files changed

+309
-1382
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
@@ -181,6 +181,7 @@ AC_CONFIG_FILES([
181181
module/nvpair/Makefile
182182
module/os/linux/spl/Makefile
183183
module/os/linux/zfs/Makefile
184+
module/os/linux/zstd/Makefile
184185
module/spl/Makefile
185186
module/unicode/Makefile
186187
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 & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -46,44 +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_ADD(stat, val) \
79-
atomic_add_64(&zstd_stats.stat.value.ui64, (val))
80-
#define ZSTDSTAT_SUB(stat, val) \
81-
atomic_sub_64(&zstd_stats.stat.value.ui64, (val))
82-
#define ZSTDSTAT_BUMP(stat) ZSTDSTAT_ADD(stat, 1)
83-
8449
/* (de)init for user space / kernel emulation */
8550
int zstd_init(void);
8651
void zstd_fini(void);
52+
extern int zstd_init_os(void);
53+
extern void zstd_fini_os(void);
8754

8855
size_t zfs_zstd_compress(void *s_start, void *d_start, size_t s_len,
8956
size_t d_len, int level);

include/sys/zstd/zstd_impl.h

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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+
kstat_named_t zstd_stat_buffers;
83+
kstat_named_t zstd_stat_size;
84+
} zstd_stats_t;
85+
86+
extern zstd_stats_t zstd_stats;
87+
88+
/*
89+
* ZSTD memory handlers
90+
*
91+
* For decompression we use a different handler which also provides fallback
92+
* memory allocation in case memory runs out.
93+
*
94+
* The ZSTD handlers were split up for the most simplified implementation.
95+
*/
96+
extern void *zstd_alloc(void *opaque, size_t size);
97+
extern void *zstd_dctx_alloc(void *opaque, size_t size);
98+
extern void zstd_free(void *opaque, void *ptr);
99+
100+
/*
101+
* kstat helper macros
102+
*/
103+
#define ZSTDSTAT(stat) (zstd_stats.stat.value.ui64)
104+
#define ZSTDSTAT_ADD(stat, val) \
105+
atomic_add_64(&zstd_stats.stat.value.ui64, (val))
106+
#define ZSTDSTAT_SUB(stat, val) \
107+
atomic_sub_64(&zstd_stats.stat.value.ui64, (val))
108+
#define ZSTDSTAT_BUMP(stat) ZSTDSTAT_ADD(stat, 1)
109+
110+
#ifdef __cplusplus
111+
}
112+
#endif
113+
114+
#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 \
@@ -27,6 +28,7 @@ CFLAGS+= -I${INCDIR}
2728
CFLAGS+= -I${INCDIR}/os/freebsd
2829
CFLAGS+= -I${INCDIR}/os/freebsd/spl
2930
CFLAGS+= -I${INCDIR}/os/freebsd/zfs
31+
CFLAGS+= -I${SRCDIR}/zstd
3032
CFLAGS+= -I${SRCDIR}/zstd/include
3133
CFLAGS+= -include ${INCDIR}/os/freebsd/spl/sys/ccompile.h
3234

@@ -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__

module/Makefile.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ include Kbuild
22

33
INSTALL_MOD_DIR ?= extra
44

5-
SUBDIR_TARGETS = icp lua zstd
5+
SUBDIR_TARGETS = icp lua
66

77
all: modules
88
distclean maintainer-clean: clean

0 commit comments

Comments
 (0)