From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr0-f195.google.com ([209.85.128.195]:37172 "EHLO mail-wr0-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750715AbdHWWqC (ORCPT ); Wed, 23 Aug 2017 18:46:02 -0400 Received: by mail-wr0-f195.google.com with SMTP id 72so520463wrc.4 for ; Wed, 23 Aug 2017 15:46:02 -0700 (PDT) From: Timofey Titovets To: linux-btrfs@vger.kernel.org Cc: Timofey Titovets Subject: [PATCH v6 5/6] Btrfs: heuristic add byte set calculation Date: Thu, 24 Aug 2017 01:45:44 +0300 Message-Id: <20170823224545.16375-6-nefelim4ag@gmail.com> In-Reply-To: <20170823224545.16375-1-nefelim4ag@gmail.com> References: <20170823224545.16375-1-nefelim4ag@gmail.com> Sender: linux-btrfs-owner@vger.kernel.org List-ID: Calculate byte set size for data sample: Calculate how many unique bytes has been in sample By count all bytes in bucket with count > 0 If byte set low (~25%), data are easily compressible Signed-off-by: Timofey Titovets --- fs/btrfs/heuristic.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/fs/btrfs/heuristic.c b/fs/btrfs/heuristic.c index 8df56ea93064..4815d1becf7e 100644 --- a/fs/btrfs/heuristic.c +++ b/fs/btrfs/heuristic.c @@ -24,6 +24,7 @@ #define ITER_SHIFT 256 #define BUCKET_SIZE 256 #define MAX_SAMPLE_SIZE (BTRFS_MAX_UNCOMPRESSED*READ_SIZE/ITER_SHIFT) +#define BYTE_SET_THRESHOLD 64 struct bucket_item { u32 count; @@ -66,6 +67,27 @@ static struct list_head *heuristic_alloc_workspace(void) return ERR_PTR(-ENOMEM); } +static u32 byte_set_size(const struct workspace *ws) +{ + u32 a = 0; + u32 byte_set_size = 0; + + for (; a < BYTE_SET_THRESHOLD; a++) { + if (ws->bucket[a].count > 0) + byte_set_size++; + } + + for (; a < BUCKET_SIZE; a++) { + if (ws->bucket[a].count > 0) { + byte_set_size++; + if (byte_set_size > BYTE_SET_THRESHOLD) + return byte_set_size; + } + } + + return byte_set_size; +} + static bool sample_repeated_patterns(struct workspace *ws) { u32 i = 0; @@ -141,6 +163,10 @@ static int heuristic(struct list_head *ws, struct inode *inode, workspace->bucket[byte].count++; } + a = byte_set_size(workspace); + if (a > BYTE_SET_THRESHOLD) + return 2; + return 1; } -- 2.14.1