From patchwork Thu Sep 28 14:33:38 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Timofey Titovets X-Patchwork-Id: 9976249 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 91B246037E for ; Thu, 28 Sep 2017 14:34:00 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8424A2951B for ; Thu, 28 Sep 2017 14:34:00 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7926E29577; Thu, 28 Sep 2017 14:34:00 +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 1811C2951B for ; Thu, 28 Sep 2017 14:34:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753348AbdI1Od4 (ORCPT ); Thu, 28 Sep 2017 10:33:56 -0400 Received: from mail-wr0-f196.google.com ([209.85.128.196]:38297 "EHLO mail-wr0-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753336AbdI1Odx (ORCPT ); Thu, 28 Sep 2017 10:33:53 -0400 Received: by mail-wr0-f196.google.com with SMTP id p37so2234019wrb.5 for ; Thu, 28 Sep 2017 07:33:53 -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=xp9qkflQ3WG6dNuJsoGiACWtBH3bgKJKgk7j3OifHLs=; b=uDy/rvD6g5mVJuIxxc9Rhit++yr/q/TPy5jC9NunFnvFoY8MILWn4CS7AW4YbpYeGa il2dyIJnN7WE3GQKW2mnX7LK8FGahDaGrAJFvjVqQBl65eWzB87X2OkdyjJhofMC1uPv urAElRNumuD2GVxYIAUNoEFZJ64963ahZ+LpqYylTEqTblESxJ+fKWaey2xbKrROWOqU 8wZklyN2luumOdbR+f7CMS5o9x1L2SDucQtqucZDhjH/OdHLcPMKsdGI/Uz+ioJLBFUJ Gjt42lUZqfjVr1SMnMB8Y0lh8BPh6zLkEU0GDV583/bFmkOY4f7pOn/EIfLAUUAKgYMd PejA== 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=xp9qkflQ3WG6dNuJsoGiACWtBH3bgKJKgk7j3OifHLs=; b=p8hvwtJC3Wz/A4/d3PudjCotdLsUr4+cH66HAVK/r4BLOaq9s0EtvULkuzghMHk6vu IZi0RsHo//e6U3iDUlYm6q0OwegjucYVFUwBxC5GqzP/vp5rsBCxukgdDOywat1fyroR nI0jeblyXIQv1mLjQobnbqr2mGjTLowefVXWqaGKl/1r8r9oDyFeZL9Kvy49+U5k7WRe 37N4Wc9gmyZQzPhBsRp1Yt8lnXK3XhG1fqwzMP/WDuHfwneYSP0yE9qSOnGm1qZbfDvd gw0hi0HsAZoz+zVrzhYCwt0CYjJ4jqNP/TzjZwDwF0ot9sqXC21yoU+zRKEQuRn08FP6 Gr7Q== X-Gm-Message-State: AHPjjUhaglAM3DloNcYo9OLfGiNI0F1JcM6he3QZj6bsTz8RwW5t+rCs gxBHWKaMFnjHT6ZPiPMTf497Zw== X-Google-Smtp-Source: AOwi7QAFYsm/p+MzhLuITy/tHR7gNfGd7EApS/lyvS49sMwBing9wUyh/qU4Pfwl20SYchyaqP9nbw== X-Received: by 10.223.186.140 with SMTP id p12mr4675790wrg.235.1506609231962; Thu, 28 Sep 2017 07:33:51 -0700 (PDT) Received: from titovetst-l.itransition.corp ([93.171.6.182]) by smtp.gmail.com with ESMTPSA id k82sm460633wmf.19.2017.09.28.07.33.51 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 28 Sep 2017 07:33:51 -0700 (PDT) From: Timofey Titovets To: linux-btrfs@vger.kernel.org Cc: Timofey Titovets Subject: [PATCH v8 3/6] Btrfs: implement heuristic sampling logic Date: Thu, 28 Sep 2017 17:33:38 +0300 Message-Id: <20170928143341.24491-4-nefelim4ag@gmail.com> X-Mailer: git-send-email 2.14.2 In-Reply-To: <20170928143341.24491-1-nefelim4ag@gmail.com> References: <20170928143341.24491-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 Copy sample data from input data range to sample buffer then calculate byte type count for that sample into bucket. Signed-off-by: Timofey Titovets --- fs/btrfs/compression.c | 71 +++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 61 insertions(+), 10 deletions(-) -- 2.14.2 -- 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 1715655d050e..e2419639ae7f 100644 --- a/fs/btrfs/compression.c +++ b/fs/btrfs/compression.c @@ -725,7 +725,6 @@ blk_status_t btrfs_submit_compressed_read(struct inode *inode, struct bio *bio, #define MAX_SAMPLE_SIZE (BTRFS_MAX_UNCOMPRESSED * \ SAMPLING_READ_SIZE / SAMPLING_INTERVAL) - struct bucket_item { u32 count; }; @@ -733,6 +732,7 @@ struct bucket_item { struct heuristic_ws { /* Partial copy of input data */ u8 *sample; + u32 sample_size; /* Bucket store counter for each byte type */ struct bucket_item *bucket; struct list_head list; @@ -1200,6 +1200,57 @@ int btrfs_decompress_buf2page(const char *buf, unsigned long buf_start, return 1; } +static void heuristic_collect_sample(struct inode *inode, u64 start, u64 end, + struct heuristic_ws *ws) +{ + struct page *page; + u64 index, index_end; + u32 i, curr_sample_pos; + u8 *in_data; + + /* + * Compression only handle first 128kb of input range + * And just shift over range in loop for compressing it. + * Let's do the same. + * + * MAX_SAMPLE_SIZE - calculated in assume that heuristic will process + * not more then BTRFS_MAX_UNCOMPRESSED at run + */ + if (end - start > BTRFS_MAX_UNCOMPRESSED) + end = start + BTRFS_MAX_UNCOMPRESSED; + + index = start >> PAGE_SHIFT; + index_end = end >> PAGE_SHIFT; + + /* Don't miss unaligned end */ + if (!IS_ALIGNED(end, PAGE_SIZE)) + index_end++; + + curr_sample_pos = 0; + while (index < index_end) { + page = find_get_page(inode->i_mapping, index); + in_data = kmap(page); + /* Handle case where start unaligned to PAGE_SIZE */ + i = start % PAGE_SIZE; + while (i < PAGE_SIZE - SAMPLING_READ_SIZE) { + /* Don't sample mem trash from last page */ + if (start > end - SAMPLING_READ_SIZE) + break; + memcpy(&ws->sample[curr_sample_pos], + &in_data[i], SAMPLING_READ_SIZE); + i += SAMPLING_INTERVAL; + start += SAMPLING_INTERVAL; + curr_sample_pos += SAMPLING_READ_SIZE; + } + kunmap(page); + put_page(page); + + index++; + } + + ws->sample_size = curr_sample_pos; +} + /* * Compression heuristic. * @@ -1219,19 +1270,19 @@ int btrfs_compress_heuristic(struct inode *inode, u64 start, u64 end) { struct list_head *ws_list = __find_workspace(0, true); struct heuristic_ws *ws; - u64 index = start >> PAGE_SHIFT; - u64 end_index = end >> PAGE_SHIFT; - struct page *page; + u32 i; + u8 byte; int ret = 1; ws = list_entry(ws_list, struct heuristic_ws, list); - while (index <= end_index) { - page = find_get_page(inode->i_mapping, index); - kmap(page); - kunmap(page); - put_page(page); - index++; + heuristic_collect_sample(inode, start, end, ws); + + memset(ws->bucket, 0, sizeof(*ws->bucket)*BUCKET_SIZE); + + for (i = 0; i < ws->sample_size; i++) { + byte = ws->sample[i]; + ws->bucket[byte].count++; } __free_workspace(0, ws_list, true);