4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
31 * Copyright (c) 2013 by Delphix. All rights reserved.
34 #include <sys/zfs_context.h>
35 #include <sys/compress.h>
36 #include <sys/kstat.h>
39 #include <sys/zio_compress.h>
41 typedef struct zcomp_stats {
42 kstat_named_t zcompstat_attempts;
43 kstat_named_t zcompstat_empty;
44 kstat_named_t zcompstat_skipped_insufficient_gain;
47 static zcomp_stats_t zcomp_stats = {
48 { "attempts", KSTAT_DATA_UINT64 },
49 { "empty", KSTAT_DATA_UINT64 },
50 { "skipped_insufficient_gain", KSTAT_DATA_UINT64 }
53 #define ZCOMPSTAT_INCR(stat, val) \
54 atomic_add_64(&zcomp_stats.stat.value.ui64, (val));
56 #define ZCOMPSTAT_BUMP(stat) ZCOMPSTAT_INCR(stat, 1);
61 * Compression vectors.
64 zio_compress_info_t zio_compress_table[ZIO_COMPRESS_FUNCTIONS] = {
65 {NULL, NULL, 0, "inherit"},
66 {NULL, NULL, 0, "on"},
67 {NULL, NULL, 0, "uncompressed"},
68 {lzjb_compress, lzjb_decompress, 0, "lzjb"},
69 {NULL, NULL, 0, "empty"},
70 {gzip_compress, gzip_decompress, 1, "gzip-1"},
71 {gzip_compress, gzip_decompress, 2, "gzip-2"},
72 {gzip_compress, gzip_decompress, 3, "gzip-3"},
73 {gzip_compress, gzip_decompress, 4, "gzip-4"},
74 {gzip_compress, gzip_decompress, 5, "gzip-5"},
75 {gzip_compress, gzip_decompress, 6, "gzip-6"},
76 {gzip_compress, gzip_decompress, 7, "gzip-7"},
77 {gzip_compress, gzip_decompress, 8, "gzip-8"},
78 {gzip_compress, gzip_decompress, 9, "gzip-9"},
79 {zle_compress, zle_decompress, 64, "zle"},
80 {lz4_compress, lz4_decompress, 0, "lz4"},
84 zio_compress_select(enum zio_compress child, enum zio_compress parent)
86 ASSERT(child < ZIO_COMPRESS_FUNCTIONS);
87 ASSERT(parent < ZIO_COMPRESS_FUNCTIONS);
88 ASSERT(parent != ZIO_COMPRESS_INHERIT && parent != ZIO_COMPRESS_ON);
90 if (child == ZIO_COMPRESS_INHERIT)
93 if (child == ZIO_COMPRESS_ON)
94 return (ZIO_COMPRESS_ON_VALUE);
100 zio_compress_data(enum zio_compress c, void *src, void *dst, size_t s_len)
102 uint64_t *word, *word_end;
104 zio_compress_info_t *ci = &zio_compress_table[c];
106 ASSERT((uint_t)c < ZIO_COMPRESS_FUNCTIONS);
107 ASSERT((uint_t)c == ZIO_COMPRESS_EMPTY || ci->ci_compress != NULL);
109 ZCOMPSTAT_BUMP(zcompstat_attempts);
112 * If the data is all zeroes, we don't even need to allocate
113 * a block for it. We indicate this by returning zero size.
115 word_end = (uint64_t *)((char *)src + s_len);
116 for (word = src; word < word_end; word++)
120 if (word == word_end) {
121 ZCOMPSTAT_BUMP(zcompstat_empty);
125 if (c == ZIO_COMPRESS_EMPTY)
128 /* Compress at least 12.5% */
129 d_len = s_len - (s_len >> 3);
130 c_len = ci->ci_compress(src, dst, s_len, d_len, ci->ci_level);
133 ZCOMPSTAT_BUMP(zcompstat_skipped_insufficient_gain);
137 ASSERT3U(c_len, <=, d_len);
142 zio_decompress_data(enum zio_compress c, void *src, void *dst,
143 size_t s_len, size_t d_len)
145 zio_compress_info_t *ci = &zio_compress_table[c];
147 if ((uint_t)c >= ZIO_COMPRESS_FUNCTIONS || ci->ci_decompress == NULL)
148 return (SET_ERROR(EINVAL));
150 return (ci->ci_decompress(src, dst, s_len, d_len, ci->ci_level));
154 zio_compress_init(void)
157 zcomp_ksp = kstat_create("zfs", 0, "zcompstats", "misc",
158 KSTAT_TYPE_NAMED, sizeof (zcomp_stats) / sizeof (kstat_named_t),
161 if (zcomp_ksp != NULL) {
162 zcomp_ksp->ks_data = &zcomp_stats;
163 kstat_install(zcomp_ksp);
168 zio_compress_fini(void)
170 if (zcomp_ksp != NULL) {
171 kstat_delete(zcomp_ksp);