Debian Codegraph @

examples/show_free_space_fragmentation.py

raw symbols SCIP
1 #!/usr/bin/python3
2
3 import btrfs
4 import math
5 import sys
6
7 if len(sys.argv) < 2:
8 print("Usage: {} [min_score] <mountpoint>".format(sys.argv[0]))
9 sys.exit(1)
10
11
12 def extent_tree_free_space_extents(min_vaddr, max_vaddr):
13 cur_end = min_vaddr
14 for extent in fs.extents(min_vaddr, max_vaddr):
15 next_start = extent.vaddr
16 if isinstance(extent, btrfs.ctree.ExtentItem):
17 next_end = next_start + extent.length
18 elif isinstance(extent, btrfs.ctree.MetaDataItem):
19 next_end = next_start + fs.nodesize
20 if next_start > cur_end:
21 yield btrfs.free_space_tree.FreeSpaceExtent(cur_end, next_start - cur_end)
22 cur_end = next_end
23 if cur_end < max_vaddr:
24 yield btrfs.free_space_tree.FreeSpaceExtent(cur_end, max_vaddr + 1 - cur_end)
25
26
27 with btrfs.FileSystem(sys.argv[-1]) as fs:
28 if len(sys.argv) == 3:
29 min_score = int(sys.argv[1])
30 else:
31 min_score = 250
32 print("Showing all block groups with free space fragmentation score >= {}".format(min_score))
33
34 try:
35 list(fs.free_space_extents(0, 0))
36 free_space_extents = fs.free_space_extents
37 except:
38 print("No Free Space Tree (space_cache=v2) found!")
39 print("Falling back to using the extent tree to determine free space extents.")
40 free_space_extents = extent_tree_free_space_extents
41
42 bad_chunks = 0
43 for chunk in fs.chunks():
44 if not chunk.type & btrfs.BLOCK_GROUP_DATA:
45 continue
46 try:
47 block_group = fs.block_group(chunk.vaddr, chunk.length)
48 except IndexError:
49 continue
50
51 min_vaddr = block_group.vaddr
52 max_vaddr = block_group.vaddr + block_group.length - 1
53
54 log2_bg_length = math.log2(block_group.length)
55 half_width = (log2_bg_length - 11) / 2
56 shift = (log2_bg_length + 11) / 2
57 # When drawing the function seen below (score += ...), like...
58 #
59 # | x - shift |
60 # y = 1 - | ---------- |
61 # | half_width |
62 #
63 # ... you'll see an upside down V shape. The x values are the log2() of a
64 # free space extent size, so e.g. 16 for 64KiB of free space. The y value
65 # is the fragmentation score. The function will give a low score to free
66 # space fragments that are very small (but when you have an enormous amount
67 # of them, it'll add up again) and also a low score to very big ones
68 # (they're good). Fragments in the middle get a higher score.
69 #
70 # Experience learns that scores above 250 point at chunks in which free
71 # space fragmentation is getting quite bad. Use btrfs-heatmap to create
72 # images of specific block groups to see what's happening inside.
73 fragments = 0
74 score = 0
75 for free_space_extent in free_space_extents(min_vaddr, max_vaddr):
76 fragments += 1
77 score += 1 - abs((math.log2(free_space_extent.length) - shift) / half_width)
78 if score >= min_score:
79 bad_chunks += 1
80 print("vaddr {} length {} used_pct {} free space fragments {} score {}".format(
81 chunk.vaddr, chunk.length, block_group.used_pct, fragments, int(score)))
82
83 if bad_chunks == 0:
84 print("No block groups found with free space fragmentation score >= {}".format(min_score))