-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathfs_usage.py
765 lines (656 loc) · 33.4 KB
/
fs_usage.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
# Copyright (C) 2018 Hans van Kranenburg <hans@knorrie.org>
#
# This file is part of the python-btrfs module.
#
# python-btrfs is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# python-btrfs is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with python-btrfs. If not, see <http://www.gnu.org/licenses/>.
"""
This module provides advanced usage reporting for a btrfs filesystem.
By calling the :func:`~btrfs.ctree.FileSystem.usage` function on a
:class:`btrfs.ctree.FileSystem` object, an :class:`FsUsage` object is returned
that can be inspected.
Example::
>>> import btrfs
>>> with btrfs.FileSystem('/') as fs:
... usage = fs.usage()
... btrfs.utils.pretty_print(usage)
.. note::
If you're not yet familiar with it, btrfs terminology can be quite
confusing.
Here's just an example: In btrfs terminology, a ‘space’ is the collection
of all block groups that have identical type and profile flags. For
example, Metadata, DUP is a ‘space’. The word ‘space’ is also used for the
distinction between ‘physical address space’ and ‘virtual address space’.
"""
import btrfs
import copy
from btrfs.ctree import ( # noqa
BLOCK_GROUP_DATA, BLOCK_GROUP_SYSTEM, BLOCK_GROUP_METADATA,
BLOCK_GROUP_TYPE_MASK, BLOCK_GROUP_PROFILE_MASK,
)
class DevSpaceUsage(object):
"""Physical usage details for a single space per device.
For example, a `Data, DUP` chunk of 1GiB results in a 2GiB allocation of
physical bytes on the device. A `Data, RAID5` chunk of 3GiB, allocated over
4 devices results in a 1GiB allocation on each device, with 256MiB reserved
for parity.
:ivar int flags: Block group type and profile, e.g. `Data, RAID1`.
:ivar int devid: Device ID
:ivar int allocated: Total amount of allocated physical bytes.
:ivar int parity: Amount of allocated physical bytes reserved for parity.
.. note::
Objects of this type are provided as part of an :class:`FsUsage` object.
"""
def __init__(self, devid, flags):
self.flags = flags
self.devid = devid
self.allocated = 0
self.parity = 0
def _add_usage(self, allocated, parity):
self.allocated += allocated
self.parity += parity
@staticmethod
def _pretty_properties():
return [
(btrfs.utils.block_group_flags_str, 'flags'),
(btrfs.utils.pretty_size, 'allocated'),
(btrfs.utils.pretty_size, 'parity'),
]
class DevUsage(object):
"""Physical usage details for a device.
:ivar int devid: Device ID
:ivar int total: Total amount of bytes.
:ivar int allocated: Total amount of allocated bytes.
:ivar dev_space_usage: Allocated and parity bytes per space for this
device, indexed by space flags.
:vartype dev_space_usage: dict of DevSpaceUsage
:ivar int unallocatable: Physical bytes that are not allocatable because of
unbalanced device sizes.
:ivar int unallocatable_reclaimable: Physical bytes that are not
allocatable because of unbalanced allocations.
.. note::
Objects of this type are provided as part of an :class:`FsUsage` object.
"""
def __init__(self, device):
self.devid = device.devid
self.total = device.total_bytes
self.allocated = device.bytes_used
self.unallocated = self.total - self.allocated
self.dev_space_usage = {}
self.unallocatable_soft = None # set during FsUsage init
self.unallocatable_hard = None # set during FsUsage init
self.unallocatable_reclaimable = None # set during FsUsage init
def _dev_space_usage_key_str(flags):
return btrfs.utils.block_group_flags_str(flags)
def _add_usage(self, flags, allocated, parity):
if flags not in self.dev_space_usage:
self.dev_space_usage[flags] = DevSpaceUsage(self.devid, flags)
self.dev_space_usage[flags]._add_usage(allocated, parity)
def _init_unallocatable_reclaimable(self):
self.unallocatable_reclaimable = \
max(self.unallocatable_soft - self.unallocatable_hard, 0)
@staticmethod
def _pretty_properties():
return [
(btrfs.utils.pretty_size, 'total'),
(btrfs.utils.pretty_size, 'allocated'),
(btrfs.utils.pretty_size, 'unallocated'),
(btrfs.utils.pretty_size, 'unallocatable_soft'),
(btrfs.utils.pretty_size, 'unallocatable_hard'),
(btrfs.utils.pretty_size, 'unallocatable_reclaimable'),
]
class RawSpaceUsage(object):
"""Physical usage details per space.
For example, if the `Metadata, RAID1` space has a 2GiB size in terms of
virtual addressing, in which 768MiB is used, then the allocated physical
size is 4GiB and amount of physical bytes used is 1.5GiB. A `Data, RAID6`
space of 8GiB, consisting of two 4GiB block groups (virtual address space),
each distributed over 6 devices, will occupy 2*(4+2) = 12 GiB physical
allocated bytes and have 4GiB of allocated bytes reserved for parity
blocks.
:ivar int flags: Block group type and profile, e.g. `Data, RAID1`.
:ivar int allocated: Total amount of allocated bytes.
:ivar int parity: Total amount of allocated bytes reserved for parity
blocks.
:ivar int used: Total amount of physical bytes used.
.. note::
Objects of this type are provided as part of an :class:`FsUsage` object.
"""
def __init__(self, space):
self.flags = space.flags
ratio = btrfs.volumes.block_group_profile_ncopies(space.flags)
self.allocated = space.total_bytes * ratio # initially missing parity blocks
self.used = space.used_bytes * ratio
self.parity = 0
def _add_usage(self, parity):
self.allocated += parity
self.parity += parity
@staticmethod
def _pretty_properties():
return [
(btrfs.utils.block_group_flags_str, 'flags'),
(btrfs.utils.pretty_size, 'allocated'),
(btrfs.utils.pretty_size, 'parity'),
(btrfs.utils.pretty_size, 'used'),
]
class BlockGroupTypeUsage(object):
"""Physical usage details per block group type.
Totals per block group type (`System`, `Data`, `Metadata`, or,
`Data+Metadata` for mixed mode), disregarding the block group profile
(`Single`, `RAID1`, etc).
:ivar int type: Block group type, e.g. `Metadata`.
:ivar int allocated: Total amount of allocated bytes.
:ivar int parity: Total amount of allocated bytes reserved for parity
blocks.
:ivar int used: Total amount of physical bytes used.
.. note::
Objects of this type are provided as part of an :class:`FsUsage` object.
"""
def __init__(self, block_group_type):
self.type = block_group_type
self.allocated = 0
self.used = 0
self.parity = 0
def _add_usage(self, allocated, parity, used):
self.allocated += allocated
self.parity += parity
self.used += used
@staticmethod
def _pretty_properties():
return [
(btrfs.utils.block_group_flags_str, 'type'),
(btrfs.utils.pretty_size, 'allocated'),
(btrfs.utils.pretty_size, 'parity'),
(btrfs.utils.pretty_size, 'used'),
]
class VirtualSpaceUsage(object):
"""Virtual usage per space.
:ivar int flags: Block group type and profile, e.g. `Data, RAID1`.
:ivar int total: Total amount of allocated bytes for this space.
:ivar int used: Total amount of virtual bytes used.
.. note::
Objects of this type are provided as part of an :class:`FsUsage` object.
"""
def __init__(self, space):
self.flags = space.flags
self.total = space.total_bytes
self.used = space.used_bytes
@staticmethod
def _pretty_properties():
return [
(btrfs.utils.block_group_flags_str, 'flags'),
(btrfs.utils.pretty_size, 'total'),
(btrfs.utils.pretty_size, 'used'),
]
class VirtualBlockGroupTypeUsage(object):
"""Virtual address space usage per block group type.
Totals for the virtual address space per block group type (`System`,
`Data`, `Metadata`, or, `Data+Metadata` for mixed mode), disregarding the
block group profile (`Single`, `RAID1`, etc).
:ivar int flags: Block group type, e.g. `Metadata`.
:ivar int total: Total amount of allocated bytes.
:ivar int used: Total amount of virtual bytes used.
:ivar int unused: Amount of allocated but unused virtual bytes.
.. note::
Objects of this type are provided as part of an :class:`FsUsage` object.
"""
def __init__(self, block_group_type):
self.type = block_group_type
self.total = 0
self.used = 0
self.unused = None
def _add_usage(self, total, used):
self.total += total
self.used += used
self.unused = self.total - self.used
@staticmethod
def _pretty_properties():
return [
(btrfs.utils.block_group_flags_str, 'type'),
(btrfs.utils.pretty_size, 'total'),
(btrfs.utils.pretty_size, 'used'),
(btrfs.utils.pretty_size, 'unused'),
]
class FsUsage(object):
"""Detailed usage information for a file system.
When creating an object of this type, the first argument, fs, is mandatory.
The other arguments can be used to influence the simulation to predict free
space and unallocatable space with explicit hints instead of using
information from the current filesystem, This is used by the
space-calculator program to run the simulation starting with a completely
empty filesystem.
:param btrfs.ctree.FileSystem fs: Filesystem to examine.
:param int data_metadata_ratio: Data to metadata ratio to use when running
the simulation to predict free space and unallocatable space.
:param int target_profile_metadata: Explicitly set metadata profile to use
for new allocations when running the simulation to predict free
and unallocatable space (not for a mixed filesystem).
:param int target_profile_data: Explicitly set data profile to use for new
allocations when running the simulation to predict free and
unallocatable space (not for a mixed filesystem).
:param int target_profile_mixed: Explicitly set metadata and data profile
to use for new allocations when running the simulation to predict free
and unallocatable space (only for a mixed filesystem).
Target block group profiles (used for new chunk allocations):
:ivar int target_profile_system: Profile for new System chunk allocations.
:ivar int target_profile_metadata: Profile for new Metadata chunk
allocations (not for a mixed filesystem).
:ivar int target_profile_data: Profile for new Data chunk allocations (not
for a mixed filesystem).
:ivar int target_profile_mixed: Profile for new Data+Metadata chunk
allocations (only for a mixed filesystem).
Usage details for the physical address space:
:ivar int total: Total amount of physical bytes in the filesystem.
:ivar int allocated: Total amount of allocated physical bytes.
:ivar int parity: Total amount of allocated bytes reserved for parity
blocks.
:ivar dev_usage: Physical usage details per device, indexed by Device ID.
:vartype dev_usage: dict of DevUsage
:ivar block_group_type_usage: Physical usage details per block group type,
indexed by block group type.
:vartype block_group_type_usage: dict of BlockGroupTypeUsage
:ivar raw_space_usage: Physical usage details per space, indexed by space
flags.
:vartype raw_space_usage: dict of RawSpaceUsage
Usage details for the virtual address space:
:ivar int virtual_total: Total amount of virtual address space.
:ivar int virtual_used: Total amount of bytes used inside the virtual
address space.
:ivar virtual_block_group_type_usage: Virtual address space usage per block
group type, indexed by block group type.
:vartype virtual_block_group_type_usage: dict of VirtualBlockGroupTypeUsage
:ivar VirtualSpaceUsage virtual_space_usage: Virtual usage per space,
indexed by space flags.
:vartype virtual_space_usage: dict of VirtualSpaceUsage
Allocatable space information:
The *soft* unallocatable amount of bytes is the currently unallocatable
part of the physical bytes on attached devices because the allocations in
the filesystem are unbalanced. This value is estimated by extrapolating the
current usage pattern and simulating new chunk allocations using the
current target allocation profiles. By doing so, we also discover how much
extra *virtual* address space these allocations would result in.
:ivar int unallocatable_soft: Unallocatable physical disk space because of
unbalanced allocations.
:ivar int estimated_allocatable_virtual_metadata: Estimated amount of
virtual address space bytes that can be added by allocating physical
bytes for metadata, based on the current usage pattern (not for a mixed
filesystem).
:ivar int estimated_allocatable_virtual_data: Estimated amount of virtual
address space bytes that can be added by allocating physical bytes for
data, based on the current usage pattern (not for a mixed filesystem).
:ivar int estimated_allocatable_virtual_mixed: Estimated amount of virtual
address space bytes that can be added by allocating physical bytes for
metadata and data,, based on the current usage pattern (only for a
mixed filesystem).
The *hard* unallocatable amount of bytes is the amount of physical bytes
that cannot be used for allocations, because of having different sizes of
devices attached. These values are unallocatable disk space that remains
after trying to simulate data and metadata allocations in a ratio similar
to current usage, starting with all disks being empty.
:ivar int unallocatable_hard: Unallocatable physical disk space because of
unbalanced device sizes.
:ivar int estimated_full_allocatable_virtual_metadata: Estimated amount of
virtual address space bytes for metadata, in case of optimally balanced
allocations. (not for a mixed filesystem).
:ivar int estimated_full_allocatable_virtual_data: Estimated amount of
virtual address space bytes for data, in case of optimally balanced
allocations. (not for a mixed filesystem).
:ivar int estimated_full_allocatable_virtual_mixed: Estimated amount of
virtual address space bytes for metadata and data, in case of optimally
balanced allocations. (only for a mixed filesystem).
The difference between *soft* and *hard* unallocatable bytes is the amount
of physical disk space that can be reclaimed for allocations when
rebalancing the filesystem.
:ivar int unallocatable_reclaimable: Unallocatable physical bytes that can
be reclaimed when balancing the filesystem.
Some other totals for convenience:
:ivar int allocatable: The total amount of physical bytes that are
allocatable in this filesystem. I.e. total size minus
unallocatable_soft.
:ivar int allocatable_left: The amount of allocatable physical bytes
remaining, until the filesystem will report being out of space, given
current usage pattern and target profiles.
By combining the unused virtual space in already allocated chunks and
estimated allocatable virtual bytes, we get actual numbers of estimated
free space. I.e., what we would like df to show us.
:ivar int free_metadata: Estimated virtual space left to use for metadata
(not for a mixed filesystem).
:ivar int free_data: Estimated virtual space left to use for data (not for
a mixed filesystem).
:ivar int free_mixed: Estimated virtual space left to use for metadata and
data (only for a mixed filesystem).
"""
def __init__(self, fs, data_metadata_ratio=None,
target_profile_metadata=None,
target_profile_data=None,
target_profile_mixed=None):
self._mixed_groups = fs.mixed_groups()
# Spaces and devices are a source of information
spaces = [
space
for space in fs.space_info()
if space.flags != btrfs.ctree.SPACE_INFO_GLOBAL_RSV
]
devices = list(fs.devices())
self.raw_space_usage = {
space.flags: RawSpaceUsage(space)
for space in spaces
}
self.dev_usage = {
device.devid: DevUsage(device)
for device in devices
}
self.total = sum(
device.total_bytes
for device in devices
)
self.allocated = sum(
device.bytes_used
for device in devices
)
self.parity = 0
BLOCK_GROUP_MIXED = btrfs.BLOCK_GROUP_METADATA | btrfs.BLOCK_GROUP_DATA
if not self._mixed_groups:
if target_profile_metadata is not None:
self.target_profile_system = target_profile_metadata | btrfs.BLOCK_GROUP_SYSTEM
self.target_profile_metadata = target_profile_metadata | btrfs.BLOCK_GROUP_METADATA
if target_profile_data is not None:
self.target_profile_data = target_profile_data | btrfs.BLOCK_GROUP_DATA
else:
if target_profile_mixed is not None:
self.target_profile_system = target_profile_mixed | btrfs.BLOCK_GROUP_SYSTEM
self.target_profile_mixed = target_profile_mixed | BLOCK_GROUP_MIXED
# We walk the chunk list because every block group / chunk can be laid
# out over any amount of disks. To collect e.g. the amount of parity
# bytes, we need to look at all of them.
#
# Confusing: chunk.type is actually all the flags, so type and profile
# combined.
for chunk in fs.chunks():
flags = chunk.type
if flags not in self.raw_space_usage:
continue # A conversion to this profile just started right now?
# Remember last seen chunk types as target profiles
block_group_type = flags & BLOCK_GROUP_TYPE_MASK
if block_group_type == BLOCK_GROUP_SYSTEM:
self.target_profile_system = flags
elif not self._mixed_groups:
if block_group_type == BLOCK_GROUP_DATA:
self.target_profile_data = flags
elif block_group_type == BLOCK_GROUP_METADATA:
self.target_profile_metadata = flags
else:
self.target_profile_mixed = flags
dev_extent_length = btrfs.volumes.chunk_to_dev_extent_length(chunk)
chunk_raw_parity_bytes = btrfs.volumes.chunk_to_raw_parity_bytes(chunk)
self.parity += chunk_raw_parity_bytes
self.raw_space_usage[flags]._add_usage(chunk_raw_parity_bytes)
dev_extent_parity_bytes = chunk_raw_parity_bytes // len(chunk.stripes)
for stripe in chunk.stripes:
if stripe.devid not in self.dev_usage:
continue # A device just got added?
self.dev_usage[stripe.devid]._add_usage(flags,
dev_extent_length,
dev_extent_parity_bytes)
# Combine information from different spaces with same chunk type into
# totals per block group type. So, e.g. all DATA space, regardless of
# being single, RAID1, etc...
self.block_group_type_usage = {}
for raw_space in self.raw_space_usage.values():
space_type = raw_space.flags & BLOCK_GROUP_TYPE_MASK
if space_type not in self.block_group_type_usage:
self.block_group_type_usage[space_type] = BlockGroupTypeUsage(space_type)
self.block_group_type_usage[space_type]._add_usage(
raw_space.allocated, raw_space.parity, raw_space.used)
self.virtual_space_usage = {
space.flags: VirtualSpaceUsage(space)
for space in spaces
}
# Combine information from different spaces with same chunk type into
# totals per block group type. So, e.g. all DATA space, regardless of
# being single, RAID1, etc...
self.virtual_block_group_type_usage = {}
for virtual_space in self.virtual_space_usage.values():
space_type = virtual_space.flags & BLOCK_GROUP_TYPE_MASK
if space_type not in self.virtual_block_group_type_usage:
self.virtual_block_group_type_usage[space_type] = \
VirtualBlockGroupTypeUsage(space_type)
self.virtual_block_group_type_usage[space_type]._add_usage(
virtual_space.total, virtual_space.used)
# The total size of the block groups in the virtual address space
self.virtual_total = sum(
virtual_space.total
for virtual_space in self.virtual_space_usage.values()
)
self.virtual_used = sum(
virtual_space.used
for virtual_space in self.virtual_space_usage.values()
)
if data_metadata_ratio is not None:
self.default_data_metadata_ratio = data_metadata_ratio
else:
self.default_data_metadata_ratio = 200
# Estimate the amount of unallocatable raw disk space if the sizes of
# attached block devices are unbalanced. We start the simulation with
# the entire sizes of the attached devices and keep allocating chunks
# until not possible any more.
#
device_sizes = {
devid: dev_usage.total
for devid, dev_usage in self.dev_usage.items()
}
if not self._mixed_groups:
# The estimated "full" allocatable numbers are the estimation of
# virtual space to be used for data and metadata when the
# filesystem would be totally empty to begin with.
#
# This is e.g. used by the space calculator example.
dev_unallocatable_hard, \
self.estimated_full_allocatable_virtual_metadata, \
self.estimated_full_allocatable_virtual_data = \
self._simulate_chunk_allocations(device_sizes)
else:
dev_unallocatable_hard, \
self.estimated_full_allocatable_virtual_mixed = \
self._simulate_chunk_allocations(device_sizes)
for devid, unallocatable_hard in dev_unallocatable_hard.items():
self.dev_usage[devid].unallocatable_hard = unallocatable_hard
self.unallocatable_hard = sum(dev_unallocatable_hard.values())
# Next, we estimate the amount of unallocatable raw disk space when
# starting out with the current state of the filesystem.
unallocated_sizes = {
devid: dev_usage.unallocated
for devid, dev_usage in self.dev_usage.items()
}
if not self._mixed_groups:
dev_unallocatable_soft, \
self.estimated_allocatable_virtual_metadata, \
self.estimated_allocatable_virtual_data = \
self._simulate_chunk_allocations(unallocated_sizes)
else:
dev_unallocatable_soft, \
self.estimated_allocatable_virtual_mixed = \
self._simulate_chunk_allocations(unallocated_sizes)
for devid, unallocatable_soft in dev_unallocatable_soft.items():
self.dev_usage[devid].unallocatable_soft = unallocatable_soft
self.unallocatable_soft = sum(dev_unallocatable_soft.values())
# At this point, it is possible that the unallocatable_hard amounts are
# higher than the unallocatable_soft amounts. E.g. if we just switched
# to another target profile. In that case, this means we can not fully
# rewrite all data to the target profile!
# For convenience reasons, we provide a few more derived numbers...
#
# If the soft unallocatable amount of bytes is higher than the hard
# amount, we can reclaim space by balancing the filesytsem.
self.unallocatable_reclaimable = \
max(self.unallocatable_soft - self.unallocatable_hard, 0)
for dev_usage in self.dev_usage.values():
dev_usage._init_unallocatable_reclaimable()
self.allocatable = self.total - self.unallocatable_soft
self.allocatable_left = self.allocatable - self.allocated
if not self._mixed_groups:
self.free_metadata = self.estimated_allocatable_virtual_metadata
if btrfs.BLOCK_GROUP_METADATA in self.virtual_block_group_type_usage:
self.free_metadata += \
self.virtual_block_group_type_usage[btrfs.BLOCK_GROUP_METADATA].unused
self.free_data = self.estimated_allocatable_virtual_data
if btrfs.BLOCK_GROUP_DATA in self.virtual_block_group_type_usage:
self.free_data += \
self.virtual_block_group_type_usage[btrfs.BLOCK_GROUP_DATA].unused
else:
self.free_mixed = self.estimated_allocatable_virtual_mixed
if BLOCK_GROUP_MIXED in self.virtual_block_group_type_usage:
self.free_mixed += \
self.virtual_block_group_type_usage[BLOCK_GROUP_MIXED].unused
def _raw_space_usage_key_str(flags):
return btrfs.utils.block_group_flags_str(flags)
def _block_group_type_usage_key_str(block_group_type):
return btrfs.utils.block_group_flags_str(block_group_type)
def _virtual_space_usage_key_str(flags):
return btrfs.utils.block_group_flags_str(flags)
def _virtual_block_group_type_usage_key_str(block_group_type):
return btrfs.utils.block_group_flags_str(block_group_type)
def _data_metadata_ratio(self):
"""
Determine data to metadata ratio for allocation simulation for wasted space.
E.g. a ratio of 200 means that for every X bytes of metadata, we can
allocate 200*X bytes for data.
Actual simulation ratio is a weighted combination of the current data to
metadata ratio and the default of 200, where the weight of the current
ratio increases if the filesystem is filled up more.
If an empty filesystem is presented (this is used for the space
calculator), then just use the default that was set earlier.
"""
if self._mixed_groups:
raise ValueError("Data to metadata ratio is irrelevant for mixed groups.")
if BLOCK_GROUP_METADATA not in self.virtual_block_group_type_usage:
return self.default_data_metadata_ratio
used_fraction = self.virtual_used / self.total
used_metadata = self.virtual_block_group_type_usage[BLOCK_GROUP_METADATA].used
used_data = self.virtual_block_group_type_usage[BLOCK_GROUP_DATA].used
used_ratio = used_data / used_metadata
return used_fraction * used_ratio + (1 - used_fraction) * self.default_data_metadata_ratio
def _alloc_chunk(self, sizes, flags):
"""
This is used by the wasted space calculator.
sizes is a dictionary {devid: allocatable_bytes, ...}
which will be modified in place as side effect
flags contains allocation type, which is DATA (also for mixed) or METADATA
This function tries to reduce unallocated raw bytes on each disk in a way
similar to the workings of the btrfs chunk allocator. The sizes dictionary
will be modified in place while doing so. When returning False, no chunk
allocation is possible any more, and sizes will show the amount of
unallocatable bytes per device.
"""
attrs = btrfs.volumes._raid_attrs(flags & BLOCK_GROUP_PROFILE_MASK)
if flags & BLOCK_GROUP_DATA:
max_stripe_size = btrfs.utils.SZ_1G
max_chunk_size = btrfs.volumes.BTRFS_MAX_DATA_CHUNK_SIZE
elif flags & BLOCK_GROUP_METADATA:
if self.total > 50 * btrfs.utils.SZ_1G:
max_stripe_size = btrfs.utils.SZ_1G
else:
max_stripe_size = btrfs.utils.SZ_256M
max_chunk_size = max_stripe_size
else:
raise ValueError("Only DATA and METADATA supported here")
# we don't want a chunk larger than 10% of writeable space
max_chunk_size = min(self.total // 10, max_chunk_size)
# [(devid, unallocated), ...], most unallocated space per device first
non_zero_sizes = {
devid: unallocated
for devid, unallocated in sizes.items()
if unallocated > 0
}
sorted_sizes = sorted(list(non_zero_sizes.items()), key=lambda x: -x[1])
# Keep a multiple of devs_increment, chop off the rest
sorted_sizes = sorted_sizes[:len(sorted_sizes) -
(len(sorted_sizes) % attrs.devs_increment)]
if len(sorted_sizes) < attrs.devs_min:
return 0
# Keep only the amount we need for a single chunk allocation
if attrs.devs_max != 0:
sorted_sizes = sorted_sizes[:min(len(sorted_sizes), attrs.devs_max)]
# Actual device extent size is limited by the device with least amount of
# available space and by max_stripe_size.
stripe_size = min(max_stripe_size, sorted_sizes[-1][1] // attrs.dev_stripes)
# But, there's another limit, the max_chunk_size...
num_stripes = len(sorted_sizes) * attrs.dev_stripes
chunk_size = btrfs.volumes.dev_extent_length_to_chunk_length(
flags, num_stripes, stripe_size)
if chunk_size > max_chunk_size:
stripe_size = btrfs.volumes.chunk_length_to_dev_extent_length(
flags, num_stripes, max_chunk_size)
chunk_size = max_chunk_size
# Finally, decrease unallocated space
for devid, _ in sorted_sizes:
sizes[devid] -= stripe_size * attrs.dev_stripes
return chunk_size
def _simulate_chunk_allocations(self, sizes):
"""
Try to do metadata and data allocations until no longer possible. The sizes
dictionary is modified in place and lists the amount of unallocatable space
per disk when the function returns.
"""
_sizes = copy.deepcopy(sizes) # copy will be modified in place
if not self._mixed_groups:
ratio = self._data_metadata_ratio()
metadata_flags = self.target_profile_metadata
data_flags = self.target_profile_data
virtual_data = 0
virtual_metadata = 0
while True:
chunk_size = self._alloc_chunk(_sizes, metadata_flags)
if chunk_size == 0:
return _sizes, virtual_metadata, virtual_data
virtual_metadata += chunk_size
while virtual_data / virtual_metadata < ratio:
chunk_size = self._alloc_chunk(_sizes, data_flags)
if chunk_size == 0:
return _sizes, virtual_metadata, virtual_data
virtual_data += chunk_size
else:
flags = self.target_profile_mixed
virtual_mixed = 0
while True:
chunk_size = self._alloc_chunk(_sizes, flags)
if chunk_size == 0:
return _sizes, virtual_mixed
virtual_mixed += chunk_size
@staticmethod
def _pretty_properties():
return [
(btrfs.utils.space_profile_description, 'target_profile_system'),
(btrfs.utils.space_profile_description, 'target_profile_metadata'),
(btrfs.utils.space_profile_description, 'target_profile_data'),
(btrfs.utils.space_profile_description, 'target_profile_mixed'),
(btrfs.utils.btrfs.utils.pretty_size, 'total'),
(btrfs.utils.btrfs.utils.pretty_size, 'allocated'),
(btrfs.utils.btrfs.utils.pretty_size, 'parity'),
(btrfs.utils.btrfs.utils.pretty_size, 'virtual_total'),
(btrfs.utils.btrfs.utils.pretty_size, 'virtual_used'),
(btrfs.utils.btrfs.utils.pretty_size, 'unalloctable_soft'),
(btrfs.utils.btrfs.utils.pretty_size, 'estimated_allocatable_virtual_metadata'),
(btrfs.utils.btrfs.utils.pretty_size, 'estimated_allocatable_virtual_data'),
(btrfs.utils.btrfs.utils.pretty_size, 'estimated_allocatable_virtual_mixed'),
(btrfs.utils.btrfs.utils.pretty_size, 'unallocatable_hard'),
(btrfs.utils.btrfs.utils.pretty_size, 'estimated_full_allocatable_virtual_metadata'),
(btrfs.utils.btrfs.utils.pretty_size, 'estimated_full_allocatable_virtual_data'),
(btrfs.utils.btrfs.utils.pretty_size, 'estimated_full_allocatable_virtual_mixed'),
(btrfs.utils.btrfs.utils.pretty_size, 'unallocatable_reclaimable'),
(btrfs.utils.btrfs.utils.pretty_size, 'allocatable'),
(btrfs.utils.btrfs.utils.pretty_size, 'allocatable_left'),
(btrfs.utils.btrfs.utils.pretty_size, 'free_metadata'),
(btrfs.utils.btrfs.utils.pretty_size, 'free_data'),
(btrfs.utils.btrfs.utils.pretty_size, 'free_mixed'),
]