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
use cmp;
use Rc;
use avl;
use ObjectSet;
use ;
use ;
use txg;
use util;
use vdev;
use zfs;
// A metaslab class encompasses a category of allocatable top-level vdevs.
// Each top-level vdev is associated with a metaslab group which defines
// the allocatable region for that vdev. Examples of these categories include
// "normal" for data block allocations (i.e. main pool allocations) or "log"
// for allocations designated for intent log devices (i.e. slog devices).
// When a block allocation is requested from the SPA it is associated with a
// metaslab_class_t, and only top-level vdevs (i.e. metaslab groups) belonging
// to the class can be used to satisfy that request. Allocations are done
// by traversing the metaslab groups that are linked off of the `rotor` field.
// This rotor points to the next metaslab group where allocations will be
// attempted. Allocating a block is a 3 step process -- select the metaslab
// group, select the metaslab, and then allocate the block. The metaslab
// class defines the low-level block allocator that will be used as the
// final step in allocation. These allocators are pluggable allowing each class
// to use a block allocator that best suits that class.
//
// Metaslab groups encapsulate all the allocatable regions (i.e. metaslabs)
// of a top-level vdev. They are linked togther to form a circular linked
// list and can belong to only one metaslab class. Metaslab groups may become
// ineligible for allocations for a number of reasons such as limited free
// space, fragmentation, or going offline. When this happens the allocator will
// simply find the next metaslab group in the linked list and attempt
// to allocate from that group instead.
//
/// /////////////////////////////////////////////////////////////////////////////////////////////////
// This value defines the number of elements in the lbas array. The value
// of 64 was chosen as it covers all power of 2 buckets up to UINT64_MAX.
// This is the equivalent of highbit(UINT64_MAX).
const MAX_LBAS: usize = 64;
// Each metaslab maintains a set of in-core trees to track metaslab operations.
// The in-core free tree (ms_tree) contains the current list of free segments.
// As blocks are allocated, the allocated segment are removed from the ms_tree
// and added to a per txg allocation tree (ms_alloctree). As blocks are freed,
// they are added to the per txg free tree (ms_freetree). These per txg
// trees allow us to process all allocations and frees in syncing context
// where it is safe to update the on-disk space maps. One additional in-core
// tree is maintained to track deferred frees (ms_defertree). Once a block
// is freed it will move from the ms_freetree to the ms_defertree. A deferred
// free means that a block has been freed but cannot be used by the pool
// until TXG_DEFER_SIZE transactions groups later. For example, a block
// that is freed in txg 50 will not be available for reallocation until
// txg 52 (50 + TXG_DEFER_SIZE). This provides a safety net for uberblock
// rollback. A pool could be safely rolled back TXG_DEFERS_SIZE
// transactions groups and ensure that no block has been reallocated.
//
// The simplified transition diagram looks like this:
//
//
// ALLOCATE
// |
// V
// free segment (tree) --------> alloc_tree ----> (write to space map)
// ^
// |
// | free_tree <--- FREE
// | |
// | |
// | |
// +----------- defer_tree <-------+---------> (write to space map)
//
//
// Each metaslab's space is tracked in a single space map in the MOS,
// which is only updated in syncing context. Each time we sync a txg,
// we append the allocs and frees from that txg to the space map.
// The pool space is only updated once all metaslabs have finished syncing.
//
// To load the in-core free tree we read the space map from disk.
// This object contains a series of alloc and free records that are
// combined to make up the list of all free segments in this metaslab. These
// segments are represented in-core by the ms_tree and are stored in an
// AVL tree.
//
// As the space map grows (as a result of the appends) it will
// eventually become space-inefficient. When the metaslab's in-core free tree
// is zfs_condense_pct/100 times the size of the minimal on-disk
// representation, we rewrite it in its minimized form. If a metaslab
// needs to condense then we must set the condensing flag to ensure
// that allocations are not performed on the metaslab that is being written.
//
/// /////////////////////////////////////////////////////////////////////////////////////////////////
/// /////////////////////////////////////////////////////////////////////////////////////////////////
// The first-fit block allocator
/// /////////////////////////////////////////////////////////////////////////////////////////////////
// This is a helper function that can be used by the allocator to find
// a suitable block to allocate. This will search the specified AVL
// tree looking for a block that matches the specified criteria.
/*fn metaslab_block_picker(tree: &mut avl::Tree, cursor: &mut u64, size: u64, align: u64) -> u64 {
range_seg_t *rs, rsearch;
avl_index_t where;
rsearch.rs_start = *cursor;
rsearch.rs_end = *cursor + size;
rs = tree.find(&rsearch, &where);
if rs == NULL {
rs = tree.nearest(where, AVL_AFTER);
}
while rs != NULL {
let offset: u64 = util::p2roundup(rs->rs_start, align);
if offset + size <= rs->rs_end {
cursor = offset + size;
return (offset);
}
rs = AVL_NEXT(t, rs);
}*/
// If we know we've searched the whole map (*cursor == 0), give up.
// Otherwise, reset the cursor to the beginning and try again.
// if *cursor == 0 {
// return (-1ULL);
// }
//
// cursor = 0;
// return metaslab_block_picker(tree, cursor, size, align);
// }
/// /////////////////////////////////////////////////////////////////////////////////////////////////
/// /////////////////////////////////////////////////////////////////////////////////////////////////
// Allow allocations to switch to gang blocks quickly. We do this to
// avoid having to load lots of space_maps in a given txg. There are,
// however, some cases where we want to avoid "fast" ganging and instead
// we want to do an exhaustive search of all metaslabs on this device.
// Currently we don't allow any gang, slog, or dump device related allocations
// to "fast" gang.
// fn can_fast_gang(flags) -> bool {
// (flags) & (METASLAB_GANG_CHILD | METASLAB_GANG_HEADER | METASLAB_GANG_AVOID) == 0
// }
const METASLAB_WEIGHT_PRIMARY: u64 = 1 << 63;
const METASLAB_WEIGHT_SECONDARY: u64 = 1 << 62;
const METASLAB_ACTIVE_MASK: u64 = METASLAB_WEIGHT_PRIMARY | METASLAB_WEIGHT_SECONDARY;
// Metaslab granularity, in bytes. This is roughly similar to what would be
// referred to as the "stripe size" in traditional RAID arrays. In normal
// operation, we will try to write this amount of data to a top-level vdev
// before moving on to the next one.
static metaslab_aliquot: usize = 512 << 10;
// static metaslab_gang_bang: u64 = SPA_MAXBLOCKSIZE + 1; /* force gang blocks */
// The in-core space map representation is more compact than its on-disk form.
// The zfs_condense_pct determines how much more compact the in-core
// space_map representation must be before we compact it on-disk.
// Values should be greater than or equal to 100.
static zfs_condense_pct: isize = 200;
// Condensing a metaslab is not guaranteed to actually reduce the amount of
// space used on disk. In particular, a space map uses data in increments of
// MAX(1 << ashift, space_map_blksz), so a metaslab might use the
// same number of blocks after condensing. Since the goal of condensing is to
// reduce the number of IOPs required to read the space map, we only want to
// condense when we can be sure we will reduce the number of blocks used by the
// space map. Unfortunately, we cannot precisely compute whether or not this is
// the case in metaslab_should_condense since we are holding ms_lock. Instead,
// we apply the following heuristic: do not condense a spacemap unless the
// uncondensed size consumes greater than zfs_metaslab_condense_block_threshold
// blocks.
static zfs_metaslab_condense_block_threshold: isize = 4;
// The zfs_mg_noalloc_threshold defines which metaslab groups should
// be eligible for allocation. The value is defined as a percentage of
// free space. Metaslab groups that have more free space than
// zfs_mg_noalloc_threshold are always eligible for allocations. Once
// a metaslab group's free space is less than or equal to the
// zfs_mg_noalloc_threshold the allocator will avoid allocating to that
// group unless all groups in the pool have reached zfs_mg_noalloc_threshold.
// Once all groups in the pool reach zfs_mg_noalloc_threshold then all
// groups are allowed to accept allocations. Gang blocks are always
// eligible to allocate on any metaslab group. The default value of 0 means
// no metaslab group will be excluded based on this criterion.
static zfs_mg_noalloc_threshold: isize = 0;
// Metaslab groups are considered eligible for allocations if their
// fragmenation metric (measured as a percentage) is less than or equal to
// zfs_mg_fragmentation_threshold. If a metaslab group exceeds this threshold
// then it will be skipped unless all metaslab groups within the metaslab
// class have also crossed this threshold.
static zfs_mg_fragmentation_threshold: isize = 85;
// Allow metaslabs to keep their active state as long as their fragmentation
// percentage is less than or equal to zfs_metaslab_fragmentation_threshold. An
// active metaslab that exceeds this threshold will no longer keep its active
// status allowing better metaslabs to be selected.
static zfs_metaslab_fragmentation_threshold: isize = 70;
// When set will load all metaslabs when pool is first opened.
static metaslab_debug_load: isize = 0;
// When set will prevent metaslabs from being unloaded.
static metaslab_debug_unload: isize = 0;
// Minimum size which forces the dynamic allocator to change
// it's allocation strategy. Once the space map cannot satisfy
// an allocation of this size then it switches to using more
// aggressive strategy (i.e search by size rather than offset).
// static metaslab_df_alloc_threshold: u64 = SPA_MAXBLOCKSIZE;
// The minimum free space, in percent, which must be available
// in a space map to continue allocations in a first-fit fashion.
// Once the space_map's free space drops below this level we dynamically
// switch to using best-fit allocations.
static metaslab_df_free_pct: isize = 4;
// Percentage of all cpus that can be used by the metaslab taskq.
static metaslab_load_pct: isize = 50;
// Determines how many txgs a metaslab may remain loaded without having any
// allocations from it. As long as a metaslab continues to be used we will
// keep it loaded.
static metaslab_unload_delay: usize = TXG_SIZE * 2;
// Max number of metaslabs per group to preload.
// static metaslab_preload_limit: isize = SPA_DVAS_PER_BP;
// Enable/disable preloading of metaslab.
static metaslab_preload_enabled: bool = true;
// Enable/disable fragmentation weighting on metaslabs.
static metaslab_fragmentation_factor_enabled: bool = true;
// Enable/disable lba weighting (i.e. outer tracks are given preference).
static metaslab_lba_weighting_enabled: bool = true;
// Enable/disable metaslab group biasing.
static metaslab_bias_enabled: bool = true;
// static uint64_t metaslab_fragmentation(metaslab_t *);