From patchwork Mon Jul 16 18:05:25 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: liubo X-Patchwork-Id: 1199961 Return-Path: X-Original-To: patchwork-linux-btrfs@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork1.kernel.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by patchwork1.kernel.org (Postfix) with ESMTP id E98293FC33 for ; Mon, 16 Jul 2012 06:05:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751182Ab2GPGFQ (ORCPT ); Mon, 16 Jul 2012 02:05:16 -0400 Received: from cn.fujitsu.com ([222.73.24.84]:16546 "EHLO song.cn.fujitsu.com" rhost-flags-OK-FAIL-OK-OK) by vger.kernel.org with ESMTP id S1751124Ab2GPGFO (ORCPT ); Mon, 16 Jul 2012 02:05:14 -0400 X-IronPort-AV: E=Sophos;i="4.77,592,1336320000"; d="scan'208";a="5402928" Received: from unknown (HELO tang.cn.fujitsu.com) ([10.167.250.3]) by song.cn.fujitsu.com with ESMTP; 16 Jul 2012 14:04:18 +0800 Received: from fnstmail02.fnst.cn.fujitsu.com (tang.cn.fujitsu.com [127.0.0.1]) by tang.cn.fujitsu.com (8.14.3/8.13.1) with ESMTP id q6G658qV029453; Mon, 16 Jul 2012 14:05:10 +0800 Received: from localhost.localdomain ([10.167.225.27]) by fnstmail02.fnst.cn.fujitsu.com (Lotus Domino Release 8.5.3) with ESMTP id 2012071614045544-706910 ; Mon, 16 Jul 2012 14:04:55 +0800 From: Liu Bo To: Cc: , Subject: [PATCH v3] Btrfs: improve multi-thread buffer read Date: Mon, 16 Jul 2012 14:05:25 -0400 Message-Id: <1342461925-22495-1-git-send-email-liubo2009@cn.fujitsu.com> X-Mailer: git-send-email 1.6.5.2 X-MIMETrack: Itemize by SMTP Server on mailserver/fnst(Release 8.5.3|September 15, 2011) at 2012/07/16 14:04:55, Serialize by Router on mailserver/fnst(Release 8.5.3|September 15, 2011) at 2012/07/16 14:04:57, Serialize complete at 2012/07/16 14:04:57 Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org While testing with my buffer read fio jobs[1], I find that btrfs does not perform well enough. Here is a scenario in fio jobs: We have 4 threads, "t1 t2 t3 t4", starting to buffer read a same file, and all of them will race on add_to_page_cache_lru(), and if one thread successfully puts its page into the page cache, it takes the responsibility to read the page's data. And what's more, reading a page needs a period of time to finish, in which other threads can slide in and process rest pages: t1 t2 t3 t4 add Page1 read Page1 add Page2 | read Page2 add Page3 | | read Page3 add Page4 | | | read Page4 -----|------------|-----------|-----------|-------- v v v v bio bio bio bio Now we have four bios, each of which holds only one page since we need to maintain consecutive pages in bio. Thus, we can end up with far more bios than we need. Here we're going to a) delay the real read-page section and b) try to put more pages into page cache. With that said, we can make each bio hold more pages and reduce the number of bios we need. Here is some numbers taken from fio results: w/o patch w patch ------------- -------- --------------- READ: 745MB/s +19% 887MB/s [1]: [global] group_reporting thread numjobs=4 bs=32k rw=read ioengine=sync directory=/mnt/btrfs/ [READ] filename=foobar size=2000M invalidate=1 Signed-off-by: Liu Bo --- v2->v3: adopt kernel native pagevec instead of kmalloc. v1->v2: if we fail to make a allocation, just fall back to the old way to read page. fs/btrfs/extent_io.c | 16 +++++++++++++++- 1 files changed, 15 insertions(+), 1 deletions(-) diff --git a/fs/btrfs/extent_io.c b/fs/btrfs/extent_io.c index 01c21b6..2fcbcac 100644 --- a/fs/btrfs/extent_io.c +++ b/fs/btrfs/extent_io.c @@ -3557,7 +3557,10 @@ int extent_readpages(struct extent_io_tree *tree, struct bio *bio = NULL; unsigned page_idx; unsigned long bio_flags = 0; + struct pagevec pvec; + int i = 0; + pagevec_init(&pvec, 0); for (page_idx = 0; page_idx < nr_pages; page_idx++) { struct page *page = list_entry(pages->prev, struct page, lru); @@ -3565,11 +3568,22 @@ int extent_readpages(struct extent_io_tree *tree, list_del(&page->lru); if (!add_to_page_cache_lru(page, mapping, page->index, GFP_NOFS)) { - __extent_read_full_page(tree, page, get_extent, + page_cache_get(page); + if (pagevec_add(&pvec, page) == 0) { + for (i = 0; i < pagevec_count(&pvec); i++) + __extent_read_full_page(tree, + pvec.pages[i], get_extent, &bio, 0, &bio_flags); + pagevec_release(&pvec); + } } page_cache_release(page); } + for (i = 0; i < pagevec_count(&pvec); i++) + __extent_read_full_page(tree, pvec.pages[i], get_extent, + &bio, 0, &bio_flags); + pagevec_release(&pvec); + BUG_ON(!list_empty(pages)); if (bio) return submit_one_bio(READ, bio, 0, bio_flags);