aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTimofey Titovets <nefelim4ag@gmail.com>2017-09-28 10:33:41 -0400
committerDavid Sterba <dsterba@suse.com>2017-11-01 15:45:36 -0400
commit858177d38d4681dad6efc015b99e4c786a34aca5 (patch)
tree9867f207eadb3114bae7e39d4140604d0787e4ae
parenta288e92cacdc4729ad8f83d018fb0f3f5ded6c37 (diff)
Btrfs: heuristic: add byte core set calculation
Calculate byte core set for data sample: - sort buckets' numbers in decreasing order - count how many values cover 90% of the sample If the core set size is low (<=25%), data are easily compressible. If the core set size is high (>=80%), data are not compressible. Signed-off-by: Timofey Titovets <nefelim4ag@gmail.com> Reviewed-by: David Sterba <dsterba@suse.com> [ update comments ] Signed-off-by: David Sterba <dsterba@suse.com>
-rw-r--r--fs/btrfs/compression.c65
1 files changed, 65 insertions, 0 deletions
diff --git a/fs/btrfs/compression.c b/fs/btrfs/compression.c
index e949f078a81b..c551d8a979f4 100644
--- a/fs/btrfs/compression.c
+++ b/fs/btrfs/compression.c
@@ -33,6 +33,7 @@
33#include <linux/bit_spinlock.h> 33#include <linux/bit_spinlock.h>
34#include <linux/slab.h> 34#include <linux/slab.h>
35#include <linux/sched/mm.h> 35#include <linux/sched/mm.h>
36#include <linux/sort.h>
36#include "ctree.h" 37#include "ctree.h"
37#include "disk-io.h" 38#include "disk-io.h"
38#include "transaction.h" 39#include "transaction.h"
@@ -1222,6 +1223,59 @@ int btrfs_decompress_buf2page(const char *buf, unsigned long buf_start,
1222 return 1; 1223 return 1;
1223} 1224}
1224 1225
1226/* Compare buckets by size, ascending */
1227static int bucket_comp_rev(const void *lv, const void *rv)
1228{
1229 const struct bucket_item *l = (const struct bucket_item *)lv;
1230 const struct bucket_item *r = (const struct bucket_item *)rv;
1231
1232 return r->count - l->count;
1233}
1234
1235/*
1236 * Size of the core byte set - how many bytes cover 90% of the sample
1237 *
1238 * There are several types of structured binary data that use nearly all byte
1239 * values. The distribution can be uniform and counts in all buckets will be
1240 * nearly the same (eg. encrypted data). Unlikely to be compressible.
1241 *
1242 * Other possibility is normal (Gaussian) distribution, where the data could
1243 * be potentially compressible, but we have to take a few more steps to decide
1244 * how much.
1245 *
1246 * @BYTE_CORE_SET_LOW - main part of byte values repeated frequently,
1247 * compression algo can easy fix that
1248 * @BYTE_CORE_SET_HIGH - data have uniform distribution and with high
1249 * probability is not compressible
1250 */
1251#define BYTE_CORE_SET_LOW (64)
1252#define BYTE_CORE_SET_HIGH (200)
1253
1254static int byte_core_set_size(struct heuristic_ws *ws)
1255{
1256 u32 i;
1257 u32 coreset_sum = 0;
1258 const u32 core_set_threshold = ws->sample_size * 90 / 100;
1259 struct bucket_item *bucket = ws->bucket;
1260
1261 /* Sort in reverse order */
1262 sort(bucket, BUCKET_SIZE, sizeof(*bucket), &bucket_comp_rev, NULL);
1263
1264 for (i = 0; i < BYTE_CORE_SET_LOW; i++)
1265 coreset_sum += bucket[i].count;
1266
1267 if (coreset_sum > core_set_threshold)
1268 return i;
1269
1270 for (; i < BYTE_CORE_SET_HIGH && bucket[i].count > 0; i++) {
1271 coreset_sum += bucket[i].count;
1272 if (coreset_sum > core_set_threshold)
1273 break;
1274 }
1275
1276 return i;
1277}
1278
1225/* 1279/*
1226 * Count byte values in buckets. 1280 * Count byte values in buckets.
1227 * This heuristic can detect textual data (configs, xml, json, html, etc). 1281 * This heuristic can detect textual data (configs, xml, json, html, etc).
@@ -1366,6 +1420,17 @@ int btrfs_compress_heuristic(struct inode *inode, u64 start, u64 end)
1366 goto out; 1420 goto out;
1367 } 1421 }
1368 1422
1423 i = byte_core_set_size(ws);
1424 if (i <= BYTE_CORE_SET_LOW) {
1425 ret = 3;
1426 goto out;
1427 }
1428
1429 if (i >= BYTE_CORE_SET_HIGH) {
1430 ret = 0;
1431 goto out;
1432 }
1433
1369out: 1434out:
1370 __free_workspace(0, ws_list, true); 1435 __free_workspace(0, ws_list, true);
1371 return ret; 1436 return ret;