From patchwork Sun Aug 20 18:11:16 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Timofey Titovets X-Patchwork-Id: 9911285 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id E921A600C8 for ; Sun, 20 Aug 2017 18:11:45 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id DF816286F6 for ; Sun, 20 Aug 2017 18:11:45 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D470A2874A; Sun, 20 Aug 2017 18:11:45 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.3 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, RCVD_IN_SORBS_SPAM, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 741CD28703 for ; Sun, 20 Aug 2017 18:11:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753342AbdHTSLe (ORCPT ); Sun, 20 Aug 2017 14:11:34 -0400 Received: from mail-wr0-f194.google.com ([209.85.128.194]:36285 "EHLO mail-wr0-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753332AbdHTSL3 (ORCPT ); Sun, 20 Aug 2017 14:11:29 -0400 Received: by mail-wr0-f194.google.com with SMTP id f8so9040557wrf.3 for ; Sun, 20 Aug 2017 11:11:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=nnN8nRlVhXxt32I6fPdbx+2iUh+c/RNn4avIl7A9nvI=; b=kS1JlvlWxdglo9qRJ4bKSG9XAHkeodEyQahRBOMgT9oF8DotYhscG5kyGluQi7Hfwj gaF7rWoWW6iVEzJxVffZ9zEkxXf5beq4acvyXJKqbXNoLA/n1NxbwiMhiqifvD6B8c+w YGKxq/eWxYZA7BKcoDa0Yh3fdAr/fzziAWzd303gf1/sWa2M+tr3INUtBxIx/L0xYlFT 9tCtoXYtMb40IumyUYj6LwW4QSDbiaEenHzV11NStY34FtL4lky61aVmbCzZ7Qg6tfS2 wgtpSa0Nqv1RNgisFy6v3rnrSlsENcGWDrHIsAuXvypm26hV5S0hQxYI26Et28vrG9p5 FgAA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=nnN8nRlVhXxt32I6fPdbx+2iUh+c/RNn4avIl7A9nvI=; b=RKhvP3FhNvFoWyW2UrQdycT4q4oteETkPxWhfZFwweNWCyQ34Y3L58jvDci7JL6+RJ 6o0fbQy+LkaxyCTjEuzHsDI4lWtKQNzW0c3RwzlPvOjo6jpOeUskCS2TIzsLAviMDepx OWhKcxwKJ1QKyBV5xOF9sW2m3gh8zCWUlQaqwqdLM+OuxEIC62GpmzJsmdc+y+UVITlp NTMPEpfGCEJLBk5cd+d6TgaIo8TxbW6SEOLgTDctSOjqs8RH+YGOIWb8ZkxfxVBk3jbG K8QCbOCmgFS8Un9IRBRW5Tiq5ouKK7TIHEnNkoVpoLhn7AoS02UZ65jb1tkgVLNb1CUc 9ilA== X-Gm-Message-State: AHYfb5iAz0TkboCnW+AYAFywBFA3Dv5SS2x7ORCSir+iEtUwGIYDyibN lK2MCa0hTDmviSRd X-Received: by 10.28.39.70 with SMTP id n67mr5383603wmn.163.1503252688045; Sun, 20 Aug 2017 11:11:28 -0700 (PDT) Received: from titovetst-l.itransition.corp (nat3-minsk-pool-46-53-180-190.telecom.by. [46.53.180.190]) by smtp.gmail.com with ESMTPSA id a1sm14406344wra.17.2017.08.20.11.11.27 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 20 Aug 2017 11:11:27 -0700 (PDT) From: Timofey Titovets To: linux-btrfs@vger.kernel.org Cc: Timofey Titovets Subject: [PATCH v4 3/3] Btrfs: heuristic add byte core set calculation Date: Sun, 20 Aug 2017 21:11:16 +0300 Message-Id: <20170820181116.5131-4-nefelim4ag@gmail.com> X-Mailer: git-send-email 2.14.1 In-Reply-To: <20170820181116.5131-1-nefelim4ag@gmail.com> References: <20170820181116.5131-1-nefelim4ag@gmail.com> Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Calculate byte core set for data sample: Sort bucket's numbers in decreasing order Count how many numbers use 90% of sample If core set are low (<=25%), data are easily compressible If core set high (>=80%), data are not compressible Signed-off-by: Timofey Titovets --- fs/btrfs/compression.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++ fs/btrfs/compression.h | 2 ++ 2 files changed, 60 insertions(+) -- 2.14.1 -- To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html diff --git a/fs/btrfs/compression.c b/fs/btrfs/compression.c index fe26a44bcc9b..8dc92f42f7c2 100644 --- a/fs/btrfs/compression.c +++ b/fs/btrfs/compression.c @@ -33,6 +33,7 @@ #include #include #include +#include #include "ctree.h" #include "disk-io.h" #include "transaction.h" @@ -1069,6 +1070,42 @@ static inline int byte_set_size(const struct heuristic_bucket_item *bucket) return byte_set_size; } +/* For bucket sorting */ +static inline int heuristic_bucket_compare(const void *lv, const void *rv) +{ + struct heuristic_bucket_item *l = (struct heuristic_bucket_item *)(lv); + struct heuristic_bucket_item *r = (struct heuristic_bucket_item *)(rv); + + return r->count - l->count; +} + +/* + * Byte Core set size + * How many bytes use 90% of sample + */ +static inline int byte_core_set_size(struct heuristic_bucket_item *bucket, + u32 core_set_threshold) +{ + int a = 0; + u32 coreset_sum = 0; + + for (; a < BTRFS_HEURISTIC_BYTE_CORE_SET_LOW; a++) + coreset_sum += bucket[a].count; + + if (coreset_sum > core_set_threshold) + return a; + + for (; a < BTRFS_HEURISTIC_BYTE_CORE_SET_HIGH; a++) { + if (bucket[a].count == 0) + break; + coreset_sum += bucket[a].count; + if (coreset_sum > core_set_threshold) + break; + } + + return a; +} + /* * Compression heuristic. * @@ -1092,6 +1129,8 @@ int btrfs_compress_heuristic(struct inode *inode, u64 start, u64 end) struct heuristic_bucket_item *bucket; int a, b, ret; u8 symbol, *input_data; + u32 core_set_threshold; + u32 input_size = end - start; ret = 1; @@ -1123,6 +1162,25 @@ int btrfs_compress_heuristic(struct inode *inode, u64 start, u64 end) goto out; } + /* Sort in reverse order */ + sort(bucket, BTRFS_HEURISTIC_BUCKET_SIZE, + sizeof(struct heuristic_bucket_item), &heuristic_bucket_compare, + NULL); + + core_set_threshold = (input_size*90)/(BTRFS_HEURISTIC_ITER_OFFSET*100); + core_set_threshold *= BTRFS_HEURISTIC_READ_SIZE; + + a = byte_core_set_size(bucket, core_set_threshold); + if (a <= BTRFS_HEURISTIC_BYTE_CORE_SET_LOW) { + ret = 2; + goto out; + } + + if (a >= BTRFS_HEURISTIC_BYTE_CORE_SET_HIGH) { + ret = 0; + goto out; + } + out: kfree(bucket); return ret; diff --git a/fs/btrfs/compression.h b/fs/btrfs/compression.h index 07e3d0652e62..b369a95c1241 100644 --- a/fs/btrfs/compression.h +++ b/fs/btrfs/compression.h @@ -136,6 +136,8 @@ struct heuristic_bucket_item { #define BTRFS_HEURISTIC_ITER_OFFSET 256 #define BTRFS_HEURISTIC_BUCKET_SIZE 256 #define BTRFS_HEURISTIC_BYTE_SET_THRESHOLD 64 +#define BTRFS_HEURISTIC_BYTE_CORE_SET_LOW BTRFS_HEURISTIC_BYTE_SET_THRESHOLD +#define BTRFS_HEURISTIC_BYTE_CORE_SET_HIGH 200 // 80% int btrfs_compress_heuristic(struct inode *inode, u64 start, u64 end);