From patchwork Mon Jul 17 13:52: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: 9845139 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 8428B6037F for ; Mon, 17 Jul 2017 13:53:13 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 752D728504 for ; Mon, 17 Jul 2017 13:53:13 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 6A0B22851E; Mon, 17 Jul 2017 13:53:13 +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 E902C28504 for ; Mon, 17 Jul 2017 13:53:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751300AbdGQNxJ (ORCPT ); Mon, 17 Jul 2017 09:53:09 -0400 Received: from mail-wm0-f68.google.com ([74.125.82.68]:33762 "EHLO mail-wm0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751286AbdGQNxH (ORCPT ); Mon, 17 Jul 2017 09:53:07 -0400 Received: by mail-wm0-f68.google.com with SMTP id 65so3399752wmf.0 for ; Mon, 17 Jul 2017 06:53:07 -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; bh=Q7R8nEYY5VGSybZSkEiRp0km+6Y8/ZbWSC+CgZgFKOY=; b=X5i6zIhGvIDT2F60VBcjvHE8/RbpGqKN+5oL3pigEgaHy+LPB7JT6laL00+vKk3YcO WbcTEaWThrcEteltlZIW9GkU+NUGWkMZg/5DKz02zRKn6gXabb1pPqxsE3da0pfFemN3 h4UKDCdmCV53EbhRV9s6SxUqbV2D9cPyf6xBFPNQ5lVCjb62O6mmi3gFm0HlkHT5FGod jr+JN5X0bvu3htvI3JAkB1UrgDJ+k/vtreQJnlny+KILij4lNRHgFPZPzdbD/x+eUn1w 58CE+6rzq1Da5Yb0kejdNNJuJjPHU4ucqJJIVYM6tzjS8R/1DnG1LnxmtugbktTptdvS CIAw== 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; bh=Q7R8nEYY5VGSybZSkEiRp0km+6Y8/ZbWSC+CgZgFKOY=; b=o48wlGwatrxd5oeTtfVgShl6N7zFK3NqQJ2XsX+Ge5skgYiu0EgrAlwzoEUAjVB86z 0ASuesosbSx3r3u8Nt61wDxHFe0rAYerezALONxJbwUe/gm/HiHUrIpFeY2Zg42+l4Oq SBSfyJ3fo/exmX7T9KERwoBVcmVSIcmMj9oDCfDWDNsSJfdDu4aDzyvkNfTaFRCUlwIK taiyIZ49iMPMvdAzrDFb7WccUw79rJ1l5TSJcAFKCu90nlf37Q2UPnUZ4reF0ameiwRf hhYhzynfqL/8Mok9RpzkakC+ObRxfOvEbI8sjmBmO/uNh4YLkJP9jziyw95pa8H+mtUv RmiA== X-Gm-Message-State: AIVw112AW/GPU7GXoEts0iWRObacBvc0ueQWUHHm7oGgTcIknpgVEY6m UqxDNTk14VFY/xW6 X-Received: by 10.28.169.207 with SMTP id s198mr4570838wme.8.1500299586040; Mon, 17 Jul 2017 06:53:06 -0700 (PDT) Received: from titovetst-beplan.itransition.corp ([93.171.6.183]) by smtp.gmail.com with ESMTPSA id w30sm16585497wrb.49.2017.07.17.06.53.05 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 17 Jul 2017 06:53:05 -0700 (PDT) From: Timofey Titovets To: linux-btrfs@vger.kernel.org Cc: Timofey Titovets Subject: [PATCH v3] Btrfs: add skeleton code for compression heuristic Date: Mon, 17 Jul 2017 16:52:58 +0300 Message-Id: <20170717135258.15865-1-nefelim4ag@gmail.com> X-Mailer: git-send-email 2.13.3 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 For now that code just return true Later more complex heuristic code will be added Signed-off-by: Timofey Titovets --- fs/btrfs/compression.c | 30 ++++++++++++++++++++++++++++++ fs/btrfs/compression.h | 2 ++ fs/btrfs/inode.c | 10 +++++----- 3 files changed, 37 insertions(+), 5 deletions(-) -- 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 d2ef9ac2a630..27ba11a74eb2 100644 --- a/fs/btrfs/compression.c +++ b/fs/btrfs/compression.c @@ -1047,3 +1047,33 @@ int btrfs_decompress_buf2page(const char *buf, unsigned long buf_start, return 1; } + +/* + * Heuristic skeleton + * For now just would be a naive and very optimistic 'return true'. + * Heuristic proporsed to fast (in compare to direct compression) detect + * data type (compressible/uncompressible) for avoid vaste of cpu time + * on compression uncompressible data. + * In near time that logic will be added: + * 0. Get sample of input data + * 1. Detect Mostly Zeroed data + * 2. Detect Data with low "byte set" size (Text & etc) + * 3. Detect Data with low/high core "byte set" + */ +int btrfs_compress_heuristic(struct inode *inode, u64 start, u64 end) +{ + u64 index = start >> PAGE_SHIFT; + u64 end_index = end >> PAGE_SHIFT; + struct page *page; + int ret = 1; + + while (index <= end_index) { + page = find_get_page(inode->i_mapping, index); + kmap(page); + kunmap(page); + put_page(page); + index++; + } + + return ret; +} diff --git a/fs/btrfs/compression.h b/fs/btrfs/compression.h index 87f6d3332163..8508ba6b9aef 100644 --- a/fs/btrfs/compression.h +++ b/fs/btrfs/compression.h @@ -129,4 +129,6 @@ struct btrfs_compress_op { extern const struct btrfs_compress_op btrfs_zlib_compress; extern const struct btrfs_compress_op btrfs_lzo_compress; +int btrfs_compress_heuristic(struct inode *inode, u64 start, u64 end); + #endif diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c index 95c212037095..c23b7047fc39 100644 --- a/fs/btrfs/inode.c +++ b/fs/btrfs/inode.c @@ -392,20 +392,20 @@ static noinline int add_async_extent(struct async_cow *cow, return 0; } -static inline int inode_need_compress(struct inode *inode) +static inline int inode_need_compress(struct inode *inode, u64 start, u64 end) { struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); /* force compress */ if (btrfs_test_opt(fs_info, FORCE_COMPRESS)) - return 1; + return btrfs_compress_heuristic(inode, start, end); /* bad compression ratios */ if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) return 0; if (btrfs_test_opt(fs_info, COMPRESS) || BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS || BTRFS_I(inode)->force_compress) - return 1; + return btrfs_compress_heuristic(inode, start, end); return 0; } @@ -503,7 +503,7 @@ static noinline void compress_file_range(struct inode *inode, * inode has not been flagged as nocompress. This flag can * change at any time if we discover bad compression ratios. */ - if (inode_need_compress(inode)) { + if (inode_need_compress(inode, start, end)) { WARN_ON(pages); pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS); if (!pages) { @@ -1576,7 +1576,7 @@ static int run_delalloc_range(void *private_data, struct page *locked_page, } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) { ret = run_delalloc_nocow(inode, locked_page, start, end, page_started, 0, nr_written); - } else if (!inode_need_compress(inode)) { + } else if (!inode_need_compress(inode, start, end)) { ret = cow_file_range(inode, locked_page, start, end, end, page_started, nr_written, 1, NULL); } else {