Received: by 2002:a05:6a11:4021:0:0:0:0 with SMTP id ky33csp945887pxb; Tue, 14 Sep 2021 12:13:31 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwRZj8aIX+8eqxt2UPSQWV0NxCJaHpMV9y5zG5XUIfJIf8Wr1k7C2+kHWWF8VxJo+UVr1iZ X-Received: by 2002:a92:de41:: with SMTP id e1mr10280413ilr.85.1631646811112; Tue, 14 Sep 2021 12:13:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1631646811; cv=none; d=google.com; s=arc-20160816; b=bUnczNHIA+zYJjcaYw84JQF0vAbApMjUlnUKYkwS7yH8vgEM1lNUel2E6IEdYtRo28 bzv+zC7DBf2BXxZpfVsvlNMZ1ibPLnoUjgcniMIsJTMaVZa+j3SBXhDBbBhCPDtrnntf 05svF1c0pRJ3yOkCJu/tVq6WTvl5qxnTZ14Z36V9Pcqe9Wm2130nneRsIPpSiBRm4F4E rMi9Wiuwm5Ii1agZJHXrwYrzFWlBFDBnPfiUUNG+fVluwnFPAZmZa9DMDj5GQBFDe96V 9A4OEVp/wb/rX0K1zSYITgUSy0FKkbFsfStA431X13vDJvBJly/hza9j4FFVQvEwpGiV Ioaw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=CzZMnwKSLyg4ojLwD3A0Ua382YVJ94p3HdAk94o+tnA=; b=aN/6kP3BWdOIviehAlYT2HADPi+H0j/blXtSYjUjrKyKaaeTJpsX3msTk8RBeDAl3f hznRBGM/3/6wKT/GQEUO0D4cI1/okLhu6r1i0oWaJvrDm+/PC2e3GG0MD9zLNDDqCxD8 5sFTqLNBxUu2HWmmxLUGOtgBHYR6J/ij1mMOXfh8zShhrtbzVbl9adq+KTjoZWhRVL3T vd+GmsDIB1x3lpsy6FHjlMu2CsODS7ikW5VUSoj1PcV5N8bxFKJuDln8t7Yc+ObOEYqQ lXYf1FgrBfl4wly2jAchNR+HN5K4iYr4asdGze000InoSb4Fj+giMc8k/s/PiOYvtkau 6BNA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id l20si13729776jaj.132.2021.09.14.12.13.18; Tue, 14 Sep 2021 12:13:31 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232547AbhINTMt (ORCPT + 99 others); Tue, 14 Sep 2021 15:12:49 -0400 Received: from outgoing-auth-1.mit.edu ([18.9.28.11]:51813 "EHLO outgoing.mit.edu" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S233038AbhINTMf (ORCPT ); Tue, 14 Sep 2021 15:12:35 -0400 Received: from cwcc.thunk.org (pool-72-74-133-215.bstnma.fios.verizon.net [72.74.133.215]) (authenticated bits=0) (User authenticated as tytso@ATHENA.MIT.EDU) by outgoing.mit.edu (8.14.7/8.12.4) with ESMTP id 18EJBB6o024798 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 14 Sep 2021 15:11:12 -0400 Received: by cwcc.thunk.org (Postfix, from userid 15806) id D957B15C3424; Tue, 14 Sep 2021 15:11:11 -0400 (EDT) From: "Theodore Ts'o" To: Ext4 Developers List Cc: "Theodore Ts'o" Subject: [PATCH 3/3] resize2fs: optimize resize2fs_calculate_summary_stats() Date: Tue, 14 Sep 2021 15:11:04 -0400 Message-Id: <20210914191104.2283033-3-tytso@mit.edu> X-Mailer: git-send-email 2.31.0 In-Reply-To: <20210914191104.2283033-1-tytso@mit.edu> References: <20210914191104.2283033-1-tytso@mit.edu> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-ext4@vger.kernel.org Speed up an off-line resize of a 10GB file system to 64TB located on tmpfs from 90 seconds to 16 seconds by extracting block group bitmaps using a population count function to count the blocks in use instead checking each bit in the block bitmap. Signed-off-by: Theodore Ts'o --- resize/resize2fs.c | 74 ++++++++++++++-------------------------------- 1 file changed, 23 insertions(+), 51 deletions(-) diff --git a/resize/resize2fs.c b/resize/resize2fs.c index 5ed0c9ee..f7ffaac5 100644 --- a/resize/resize2fs.c +++ b/resize/resize2fs.c @@ -2844,67 +2844,39 @@ errout: */ static errcode_t resize2fs_calculate_summary_stats(ext2_filsys fs) { - blk64_t blk; + errcode_t retval; + blk64_t blk = fs->super->s_first_data_block; ext2_ino_t ino; - unsigned int group = 0; - unsigned int count = 0; + unsigned int n, c, group, count; blk64_t total_blocks_free = 0; int total_inodes_free = 0; int group_free = 0; int uninit = 0; - blk64_t super_blk, old_desc_blk, new_desc_blk; - int old_desc_blocks; + char *bitmap_buf; /* * First calculate the block statistics */ - uninit = ext2fs_bg_flags_test(fs, group, EXT2_BG_BLOCK_UNINIT); - ext2fs_super_and_bgd_loc2(fs, group, &super_blk, &old_desc_blk, - &new_desc_blk, 0); - if (ext2fs_has_feature_meta_bg(fs->super)) - old_desc_blocks = fs->super->s_first_meta_bg; - else - old_desc_blocks = fs->desc_blocks + - fs->super->s_reserved_gdt_blocks; - for (blk = B2C(fs->super->s_first_data_block); - blk < ext2fs_blocks_count(fs->super); - blk += EXT2FS_CLUSTER_RATIO(fs)) { - if ((uninit && - !(EQ_CLSTR(blk, super_blk) || - ((old_desc_blk && old_desc_blocks && - GE_CLSTR(blk, old_desc_blk) && - LT_CLSTR(blk, old_desc_blk + old_desc_blocks))) || - ((new_desc_blk && EQ_CLSTR(blk, new_desc_blk))) || - EQ_CLSTR(blk, ext2fs_block_bitmap_loc(fs, group)) || - EQ_CLSTR(blk, ext2fs_inode_bitmap_loc(fs, group)) || - ((GE_CLSTR(blk, ext2fs_inode_table_loc(fs, group)) && - LT_CLSTR(blk, ext2fs_inode_table_loc(fs, group) - + fs->inode_blocks_per_group))))) || - (!ext2fs_fast_test_block_bitmap2(fs->block_map, blk))) { - group_free++; - total_blocks_free++; - } - count++; - if ((count == fs->super->s_clusters_per_group) || - EQ_CLSTR(blk, ext2fs_blocks_count(fs->super)-1)) { - ext2fs_bg_free_blocks_count_set(fs, group, group_free); - ext2fs_group_desc_csum_set(fs, group); - group++; - if (group >= fs->group_desc_count) - break; - count = 0; - group_free = 0; - uninit = ext2fs_bg_flags_test(fs, group, EXT2_BG_BLOCK_UNINIT); - ext2fs_super_and_bgd_loc2(fs, group, &super_blk, - &old_desc_blk, - &new_desc_blk, 0); - if (ext2fs_has_feature_meta_bg(fs->super)) - old_desc_blocks = fs->super->s_first_meta_bg; - else - old_desc_blocks = fs->desc_blocks + - fs->super->s_reserved_gdt_blocks; + bitmap_buf = malloc(fs->blocksize); + if (!bitmap_buf) + return ENOMEM; + for (group = 0; group < fs->group_desc_count; + group++) { + retval = ext2fs_get_block_bitmap_range2(fs->block_map, + C2B(blk), fs->super->s_clusters_per_group, bitmap_buf); + if (retval) { + free(bitmap_buf); + return retval; } - } + n = ext2fs_bitcount(bitmap_buf, + fs->super->s_clusters_per_group / 8); + group_free = fs->super->s_clusters_per_group - n; + total_blocks_free += group_free; + ext2fs_bg_free_blocks_count_set(fs, group, group_free); + ext2fs_group_desc_csum_set(fs, group); + blk += EXT2FS_NUM_B2C(fs, fs->super->s_clusters_per_group); + } + free(bitmap_buf); total_blocks_free = C2B(total_blocks_free); ext2fs_free_blocks_count_set(fs->super, total_blocks_free); -- 2.31.0