From patchwork Wed Jul 26 12:49:58 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Timofey Titovets X-Patchwork-Id: 9864741 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 D45F660382 for ; Wed, 26 Jul 2017 12:50:17 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D57F128748 for ; Wed, 26 Jul 2017 12:50:17 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id CAA142874C; Wed, 26 Jul 2017 12:50:17 +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.8 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, 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 6DD492875A for ; Wed, 26 Jul 2017 12:50:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751804AbdGZMuP (ORCPT ); Wed, 26 Jul 2017 08:50:15 -0400 Received: from mail-lf0-f65.google.com ([209.85.215.65]:34498 "EHLO mail-lf0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751797AbdGZMuN (ORCPT ); Wed, 26 Jul 2017 08:50:13 -0400 Received: by mail-lf0-f65.google.com with SMTP id d80so4955927lfg.1 for ; Wed, 26 Jul 2017 05:50:12 -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=SMPM4uHmlkjVS047HGLgfvlqMMUI0AD60gidtKjt6og=; b=tXS/HlTzqaoFHpOcdTIVN4ysbPgyS8IeUUO8EGG6VJVxu6a0KjFsH8WunY04DyxaXv fe9Xv4Akll6vcdv53f2mfof+rvE/f/K/VkIpN1/lcbl+agibspeskAupR9V9dkMOTm77 Qqn1gUFQpnW3uMnI1NM66BLTKjYq8+JrcQJq4aSba3iFmZOWJBlDh/q6Ajl9b4nfDnxa 93v/U34jpX2SOdlCVj9WVMWbAekyfBZlo7JN9jofe33IitwpkHAWX2SQuBRYf9pOcPBH 19ALAfZl9cgEhkvR/5k8OuQRR7k8PRkhx7k80/WTt/tnbdLMBE/bzPiPMZnCOVmiRJRz 699Q== 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=SMPM4uHmlkjVS047HGLgfvlqMMUI0AD60gidtKjt6og=; b=h1V6FDSsiuKRv8K9zHQbONlL0S+zO6r8dkJBqLNFFAZKZj5kI+hPzFqsHqaG7XmdrQ P0uMI0CF3RPXvH8Tb9l2HiKwWW9i53TeFJTGv7ugxJ7I6nN2Uhv9+tNQPQg+QCoLDnvS TzsE9OE5QTVP7K/qfg8N573J4xHNEI2BW9DkKBseHvy+Gdt3T3RrU2YMEuhT/6QU/Vzp G9qsvWKXOod6dfh38l9S4u7HtwUctucIhS81SqJXE3sRINrOwc8XsjIm8Rl9gDB1nKvy ZPeSn75ku530vOdmI1N+ox79QfStTaOxwFvEVllt4I6p3Rhk53/L4iVEhdCP4N+EGORq aPJA== X-Gm-Message-State: AIVw1132tREjbXnWSdiRxGsn4OoLyID4dG+PGb/kEZOvot8/qKWnJQRi Ys7Me/GGk1wTXogu X-Received: by 10.25.141.10 with SMTP id p10mr336080lfd.188.1501073411697; Wed, 26 Jul 2017 05:50:11 -0700 (PDT) Received: from titovetst-l.itransition.corp ([93.171.6.183]) by smtp.gmail.com with ESMTPSA id e14sm3405444lji.53.2017.07.26.05.50.10 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 26 Jul 2017 05:50:11 -0700 (PDT) From: Timofey Titovets To: linux-btrfs@vger.kernel.org Cc: Timofey Titovets Subject: [PATCH v2 3/3] Btrfs: heuristic add byte core set calculation Date: Wed, 26 Jul 2017 15:49:58 +0300 Message-Id: <20170726124958.18837-4-nefelim4ag@gmail.com> X-Mailer: git-send-email 2.13.3 In-Reply-To: <20170726124958.18837-1-nefelim4ag@gmail.com> References: <20170726124958.18837-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 | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++ fs/btrfs/compression.h | 2 ++ 2 files changed, 59 insertions(+) -- 2.13.3 -- 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 1429b11f2c5f..314cbdd8d175 100644 --- a/fs/btrfs/compression.c +++ b/fs/btrfs/compression.c @@ -1069,6 +1069,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 +1128,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; + u64 input_size = start - end; ret = 1; @@ -1123,6 +1161,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 03857967815a..0fcd1a485adb 100644 --- a/fs/btrfs/compression.h +++ b/fs/btrfs/compression.h @@ -139,6 +139,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);