# Re: btrfs mount takes too long time

- From: Joshua \<hidden\>
- Date: 2021-09-02 17:39:49 
- List: linux-btrfs
- Page: https://ratatoskr.run/linux-btrfs/2021/09/7546232
- Thread: https://ratatoskr.run/linux-btrfs/2021/08/7546084/t.md

```text
September 2, 2021 9:56 AM, "Dāvis Mosāns" [ref] wrote:

> ceturtd., 2021. g. 2. sept., plkst. 00:31 — lietotājs Anand Jain
> ([ref]) rakstīja:
> 
>> On 02/09/2021 00:11, Dāvis Mosāns wrote:
>> pirmd., 2021. g. 30. aug., plkst. 16:08 — lietotājs Anand Jain
>> ([ref]) rakstīja:
>> 
>> open_ctree() took 228254398 us. And 98% of it that is 225418272 us
>> was taken by btrfs_read_block_groups().
>> 
>> -------------------
>> 1) $ 225418272 us | } /* btrfs_read_block_groups [btrfs] */
>> 1) * 16934.96 us | btrfs_check_rw_degradable [btrfs]();
>> 0) 0.967 us | btrfs_apply_pending_changes [btrfs]();
>> 0) 0.239 us | btrfs_read_qgroup_config [btrfs]();
>> 0) * 21017.34 us | btrfs_get_root_ref [btrfs]();
>> 0) + 15.717 us | btrfs_start_pre_rw_mount [btrfs]();
>> 0) 0.865 us | btrfs_discard_resume [btrfs]();
>> 0) $ 228254398 us | } /* open_ctree [btrfs] */
>> -------------------
>> 
>> Now we need to run the same thing on btrfs_read_block_groups(),
>> could you please run.. [1] (no need of the time).
>> 
>> [1]
>> $ umount /btrfs;
>> $./ftracegraph btrfs_read_block_groups 2 "*:mod:btrfs" "mount
>> /dev/vg/scratch0 /btrfs"
>> 
>> Thanks, Anand
>> 
>> Hi,
>> 
>> I also have a btrfs filesystem that takes a while to mount.
>> So I'm interested if this could be improved.
>> 
>> $ ./ftracegraph open_ctree 2 "*:mod:btrfs" "time mount /dev/md127 -o
>> space_cache=v2,compress=zstd,acl,subvol=Data /mnt/Data/"
>> 
>> It is better if we don't use the time prefix for the mount command
>> here. The ftrace, traces time syscall as well, which is unessential.
>> And we lose a lot of trace-buffer to it.
>> 
>> kernel.ftrace_enabled = 1
>> 
>> real 1m33,638s
>> user 0m0,000s
>> sys 0m1,130s
>> 
>> Here's the trace output https://dāvis.lv/files/ftracegraph.out.gz
>> 
>> The filesystem is on top of RAID6 mdadm array which is from 9x 3TB HDDs.
>> 
>> So here is a case of a non-zoned device.
>> 
>> Again it is btrfs_read_block_groups() which is taking ~98% of the time.
>> 
>> 3) $ 91607669 us | } /* btrfs_read_block_groups [btrfs] */
>> 3) # 9399.566 us | btrfs_check_rw_degradable [btrfs]();
>> 3) 0.922 us | btrfs_apply_pending_changes [btrfs]();
>> 3) ! 186.540 us | btrfs_read_qgroup_config [btrfs]();
>> 3) * 26109.92 us | btrfs_get_root_ref [btrfs]();
>> 3) + 23.965 us | btrfs_start_pre_rw_mount [btrfs]();
>> 3) 1.192 us | btrfs_discard_resume [btrfs]();
>> 3) $ 93501136 us | } /* open_ctree [btrfs] */
>> 
>> Could we pls get this?
>> 
>> $ ./ftracegraph btrfs_read_block_groups 2 "*:mod:btrfs" "mount ..."
>> 
>> Hopefully, there won't be a trace-buffer rollover here, as we saw in
>> the other case so that we could account for all the time spent.
> 
> Sure, here https://dāvis.lv/files/ftracegraph_v2.out.gz
> 
>> Also, let's understand how many block groups are there.
>> 
>> $ btrfs in dump-tree <dev> | grep BLOCK_GROUP_ITEM | wc -l
> 
> It's 22660
> Also by the way `-t EXTENT_TREE` should be faster

Just to add more data, I have a 10-device raid1 array with ~86TB used.

My results:
root@SERVER:~# btrfs in dump-tree -t EXTENT_TREE /dev/sdf | grep BLOCK_GROUP_ITEM | wc -l
44986

I estimate it takes ~83 seconds to mount. (by looking at dmesg) It sometimes times out on boot and drops to recovery mode, as systemd will only wait 90 seconds, and for some reason 'x-systemd.mount-timeout=500' doesn't work

Note that I defragment the subvolume and extent tree for each subvolume weekly, as it seems to reduce my mount times, and make it *usually* boot without dropping to recovery mode.

--Joshua
```
