File: blkio_test.go

package info (click to toggle)
golang-github-opencontainers-cgroups 0.0.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 844 kB
  • sloc: makefile: 2
file content (857 lines) | stat: -rw-r--r-- 30,074 bytes parent folder | download
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
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
package fs

import (
	"maps"
	"strconv"
	"testing"

	"github.com/opencontainers/cgroups"
	"github.com/opencontainers/cgroups/fscommon"
)

const (
	sectorsRecursiveContents      = `8:0 1024`
	sectorsRecursiveContentsBFQ   = `8:0 2048`
	serviceBytesRecursiveContents = `8:0 Read 100
8:0 Write 200
8:0 Sync 300
8:0 Async 500
8:0 Total 500
Total 500`

	serviceBytesRecursiveContentsBFQ = `8:0 Read 1100
8:0 Write 1200
8:0 Sync 1300
8:0 Async 1500
8:0 Total 1500
Total 1500`
	servicedRecursiveContents = `8:0 Read 10
8:0 Write 40
8:0 Sync 20
8:0 Async 30
8:0 Total 50
Total 50`
	servicedRecursiveContentsBFQ = `8:0 Read 11
8:0 Write 41
8:0 Sync 21
8:0 Async 31
8:0 Total 51
Total 51`
	queuedRecursiveContents = `8:0 Read 1
8:0 Write 4
8:0 Sync 2
8:0 Async 3
8:0 Total 5
Total 5`
	queuedRecursiveContentsBFQ = `8:0 Read 2
8:0 Write 3
8:0 Sync 4
8:0 Async 5
8:0 Total 6
Total 6`
	serviceTimeRecursiveContents = `8:0 Read 173959
8:0 Write 0
8:0 Sync 0
8:0 Async 173959
8:0 Total 17395
Total 17395`
	serviceTimeRecursiveContentsBFQ = `8:0 Read 173959
8:0 Write 0
8:0 Sync 0
8:0 Async 173
8:0 Total 174
Total 174`
	waitTimeRecursiveContents = `8:0 Read 15571
8:0 Write 0
8:0 Sync 0
8:0 Async 15571
8:0 Total 15571`
	waitTimeRecursiveContentsBFQ = `8:0 Read 1557
8:0 Write 0
8:0 Sync 0
8:0 Async 1557
8:0 Total 1557`
	mergedRecursiveContents = `8:0 Read 5
8:0 Write 10
8:0 Sync 0
8:0 Async 0
8:0 Total 15
Total 15`
	mergedRecursiveContentsBFQ = `8:0 Read 51
8:0 Write 101
8:0 Sync 0
8:0 Async 0
8:0 Total 151
Total 151`
	timeRecursiveContents    = `8:0 8`
	timeRecursiveContentsBFQ = `8:0 16`
	throttleServiceBytes     = `8:0 Read 11030528
8:0 Write 23
8:0 Sync 42
8:0 Async 11030528
8:0 Total 11030528
252:0 Read 11030528
252:0 Write 23
252:0 Sync 42
252:0 Async 11030528
252:0 Total 11030528
Total 22061056`
	throttleServiceBytesRecursive = `8:0 Read 110305281
8:0 Write 231
8:0 Sync 421
8:0 Async 110305281
8:0 Total 110305281
252:0 Read 110305281
252:0 Write 231
252:0 Sync 421
252:0 Async 110305281
252:0 Total 110305281
Total 220610561`
	throttleServiced = `8:0 Read 164
8:0 Write 23
8:0 Sync 42
8:0 Async 164
8:0 Total 164
252:0 Read 164
252:0 Write 23
252:0 Sync 42
252:0 Async 164
252:0 Total 164
Total 328`
	throttleServicedRecursive = `8:0 Read 1641
8:0 Write 231
8:0 Sync 421
8:0 Async 1641
8:0 Total 1641
252:0 Read 1641
252:0 Write 231
252:0 Sync 421
252:0 Async 1641
252:0 Total 1641
Total 3281`
)

var blkioBFQDebugStatsTestFiles = map[string]string{
	"blkio.bfq.io_service_bytes_recursive": serviceBytesRecursiveContentsBFQ,
	"blkio.bfq.io_serviced_recursive":      servicedRecursiveContentsBFQ,
	"blkio.bfq.io_queued_recursive":        queuedRecursiveContentsBFQ,
	"blkio.bfq.io_service_time_recursive":  serviceTimeRecursiveContentsBFQ,
	"blkio.bfq.io_wait_time_recursive":     waitTimeRecursiveContentsBFQ,
	"blkio.bfq.io_merged_recursive":        mergedRecursiveContentsBFQ,
	"blkio.bfq.time_recursive":             timeRecursiveContentsBFQ,
	"blkio.bfq.sectors_recursive":          sectorsRecursiveContentsBFQ,
}

var blkioBFQStatsTestFiles = map[string]string{
	"blkio.bfq.io_service_bytes_recursive": serviceBytesRecursiveContentsBFQ,
	"blkio.bfq.io_serviced_recursive":      servicedRecursiveContentsBFQ,
}

var blkioCFQStatsTestFiles = map[string]string{
	"blkio.io_service_bytes_recursive": serviceBytesRecursiveContents,
	"blkio.io_serviced_recursive":      servicedRecursiveContents,
	"blkio.io_queued_recursive":        queuedRecursiveContents,
	"blkio.io_service_time_recursive":  serviceTimeRecursiveContents,
	"blkio.io_wait_time_recursive":     waitTimeRecursiveContents,
	"blkio.io_merged_recursive":        mergedRecursiveContents,
	"blkio.time_recursive":             timeRecursiveContents,
	"blkio.sectors_recursive":          sectorsRecursiveContents,
}

type blkioStatFailureTestCase struct {
	desc     string
	filename string
}

func appendBlkioStatEntry(blkioStatEntries *[]cgroups.BlkioStatEntry, major, minor, value uint64, op string) { //nolint:unparam
	*blkioStatEntries = append(*blkioStatEntries, cgroups.BlkioStatEntry{Major: major, Minor: minor, Value: value, Op: op})
}

func TestBlkioSetWeight(t *testing.T) {
	const (
		weightBefore = 100
		weightAfter  = 200
	)

	for _, legacyIOScheduler := range []bool{false, true} {
		// Populate cgroup
		path := tempDir(t, "blkio")
		weightFilename := "blkio.bfq.weight"
		if legacyIOScheduler {
			weightFilename = "blkio.weight"
		}
		writeFileContents(t, path, map[string]string{
			weightFilename: strconv.Itoa(weightBefore),
		})
		// Apply new configuration
		r := &cgroups.Resources{
			BlkioWeight: weightAfter,
		}
		blkio := &BlkioGroup{}
		if err := blkio.Set(path, r); err != nil {
			t.Fatal(err)
		}
		// Verify results
		if weightFilename != blkio.weightFilename {
			t.Fatalf("weight filename detection failed: expected %q, detected %q", weightFilename, blkio.weightFilename)
		}
		value, err := fscommon.GetCgroupParamUint(path, weightFilename)
		if err != nil {
			t.Fatal(err)
		}
		if value != weightAfter {
			t.Fatalf("Got the wrong value, set %s failed.", weightFilename)
		}
	}
}

func TestBlkioSetWeightDevice(t *testing.T) {
	const (
		weightDeviceBefore = "8:0 400"
	)

	for _, legacyIOScheduler := range []bool{false, true} {
		// Populate cgroup
		path := tempDir(t, "blkio")
		weightFilename := "blkio.bfq.weight"
		weightDeviceFilename := "blkio.bfq.weight_device"
		if legacyIOScheduler {
			weightFilename = "blkio.weight"
			weightDeviceFilename = "blkio.weight_device"
		}
		writeFileContents(t, path, map[string]string{
			weightFilename:       "",
			weightDeviceFilename: weightDeviceBefore,
		})
		// Apply new configuration
		wd := cgroups.NewWeightDevice(8, 0, 500, 0)
		weightDeviceAfter := wd.WeightString()
		r := &cgroups.Resources{
			BlkioWeightDevice: []*cgroups.WeightDevice{wd},
		}
		blkio := &BlkioGroup{}
		if err := blkio.Set(path, r); err != nil {
			t.Fatal(err)
		}
		// Verify results
		if weightDeviceFilename != blkio.weightDeviceFilename {
			t.Fatalf("weight_device filename detection failed: expected %q, detected %q", weightDeviceFilename, blkio.weightDeviceFilename)
		}
		value, err := fscommon.GetCgroupParamString(path, weightDeviceFilename)
		if err != nil {
			t.Fatal(err)
		}
		if value != weightDeviceAfter {
			t.Fatalf("Got the wrong value, set %s failed.", weightDeviceFilename)
		}
	}
}

// regression #274
func TestBlkioSetMultipleWeightDevice(t *testing.T) {
	path := tempDir(t, "blkio")

	const (
		weightDeviceBefore = "8:0 400"
	)

	wd1 := cgroups.NewWeightDevice(8, 0, 500, 0)
	wd2 := cgroups.NewWeightDevice(8, 16, 500, 0)
	// we cannot actually set and check both because normal os.WriteFile
	// when writing to cgroup file will overwrite the whole file content instead
	// of updating it as the kernel is doing. Just check the second device
	// is present will suffice for the test to ensure multiple writes are done.
	weightDeviceAfter := wd2.WeightString()

	blkio := &BlkioGroup{}
	blkio.detectWeightFilenames(path)
	if blkio.weightDeviceFilename != "blkio.bfq.weight_device" {
		t.Fatalf("when blkio controller is unavailable, expected to use \"blkio.bfq.weight_device\", tried to use %q", blkio.weightDeviceFilename)
	}
	writeFileContents(t, path, map[string]string{
		blkio.weightDeviceFilename: weightDeviceBefore,
	})

	r := &cgroups.Resources{
		BlkioWeightDevice: []*cgroups.WeightDevice{wd1, wd2},
	}
	if err := blkio.Set(path, r); err != nil {
		t.Fatal(err)
	}

	value, err := fscommon.GetCgroupParamString(path, blkio.weightDeviceFilename)
	if err != nil {
		t.Fatal(err)
	}
	if value != weightDeviceAfter {
		t.Fatalf("Got the wrong value, set %s failed.", blkio.weightDeviceFilename)
	}
}

func TestBlkioBFQDebugStats(t *testing.T) {
	path := tempDir(t, "blkio")
	writeFileContents(t, path, blkioBFQDebugStatsTestFiles)
	blkio := &BlkioGroup{}
	actualStats := *cgroups.NewStats()
	err := blkio.GetStats(path, &actualStats)
	if err != nil {
		t.Fatal(err)
	}

	expectedStats := cgroups.BlkioStats{}
	appendBlkioStatEntry(&expectedStats.SectorsRecursive, 8, 0, 2048, "")

	appendBlkioStatEntry(&expectedStats.IoServiceBytesRecursive, 8, 0, 1100, "Read")
	appendBlkioStatEntry(&expectedStats.IoServiceBytesRecursive, 8, 0, 1200, "Write")
	appendBlkioStatEntry(&expectedStats.IoServiceBytesRecursive, 8, 0, 1300, "Sync")
	appendBlkioStatEntry(&expectedStats.IoServiceBytesRecursive, 8, 0, 1500, "Async")
	appendBlkioStatEntry(&expectedStats.IoServiceBytesRecursive, 8, 0, 1500, "Total")

	appendBlkioStatEntry(&expectedStats.IoServicedRecursive, 8, 0, 11, "Read")
	appendBlkioStatEntry(&expectedStats.IoServicedRecursive, 8, 0, 41, "Write")
	appendBlkioStatEntry(&expectedStats.IoServicedRecursive, 8, 0, 21, "Sync")
	appendBlkioStatEntry(&expectedStats.IoServicedRecursive, 8, 0, 31, "Async")
	appendBlkioStatEntry(&expectedStats.IoServicedRecursive, 8, 0, 51, "Total")

	appendBlkioStatEntry(&expectedStats.IoQueuedRecursive, 8, 0, 2, "Read")
	appendBlkioStatEntry(&expectedStats.IoQueuedRecursive, 8, 0, 3, "Write")
	appendBlkioStatEntry(&expectedStats.IoQueuedRecursive, 8, 0, 4, "Sync")
	appendBlkioStatEntry(&expectedStats.IoQueuedRecursive, 8, 0, 5, "Async")
	appendBlkioStatEntry(&expectedStats.IoQueuedRecursive, 8, 0, 6, "Total")

	appendBlkioStatEntry(&expectedStats.IoServiceTimeRecursive, 8, 0, 173959, "Read")
	appendBlkioStatEntry(&expectedStats.IoServiceTimeRecursive, 8, 0, 0, "Write")
	appendBlkioStatEntry(&expectedStats.IoServiceTimeRecursive, 8, 0, 0, "Sync")
	appendBlkioStatEntry(&expectedStats.IoServiceTimeRecursive, 8, 0, 173, "Async")
	appendBlkioStatEntry(&expectedStats.IoServiceTimeRecursive, 8, 0, 174, "Total")

	appendBlkioStatEntry(&expectedStats.IoWaitTimeRecursive, 8, 0, 1557, "Read")
	appendBlkioStatEntry(&expectedStats.IoWaitTimeRecursive, 8, 0, 0, "Write")
	appendBlkioStatEntry(&expectedStats.IoWaitTimeRecursive, 8, 0, 0, "Sync")
	appendBlkioStatEntry(&expectedStats.IoWaitTimeRecursive, 8, 0, 1557, "Async")
	appendBlkioStatEntry(&expectedStats.IoWaitTimeRecursive, 8, 0, 1557, "Total")

	appendBlkioStatEntry(&expectedStats.IoMergedRecursive, 8, 0, 51, "Read")
	appendBlkioStatEntry(&expectedStats.IoMergedRecursive, 8, 0, 101, "Write")
	appendBlkioStatEntry(&expectedStats.IoMergedRecursive, 8, 0, 0, "Sync")
	appendBlkioStatEntry(&expectedStats.IoMergedRecursive, 8, 0, 0, "Async")
	appendBlkioStatEntry(&expectedStats.IoMergedRecursive, 8, 0, 151, "Total")

	appendBlkioStatEntry(&expectedStats.IoTimeRecursive, 8, 0, 16, "")

	expectBlkioStatsEquals(t, expectedStats, actualStats.BlkioStats)
}

func TestBlkioMultipleStatsFiles(t *testing.T) {
	path := tempDir(t, "blkio")
	writeFileContents(t, path, blkioBFQDebugStatsTestFiles)
	writeFileContents(t, path, blkioCFQStatsTestFiles)
	blkio := &BlkioGroup{}
	actualStats := *cgroups.NewStats()
	err := blkio.GetStats(path, &actualStats)
	if err != nil {
		t.Fatal(err)
	}

	expectedStats := cgroups.BlkioStats{}
	appendBlkioStatEntry(&expectedStats.SectorsRecursive, 8, 0, 2048, "")

	appendBlkioStatEntry(&expectedStats.IoServiceBytesRecursive, 8, 0, 1100, "Read")
	appendBlkioStatEntry(&expectedStats.IoServiceBytesRecursive, 8, 0, 1200, "Write")
	appendBlkioStatEntry(&expectedStats.IoServiceBytesRecursive, 8, 0, 1300, "Sync")
	appendBlkioStatEntry(&expectedStats.IoServiceBytesRecursive, 8, 0, 1500, "Async")
	appendBlkioStatEntry(&expectedStats.IoServiceBytesRecursive, 8, 0, 1500, "Total")

	appendBlkioStatEntry(&expectedStats.IoServicedRecursive, 8, 0, 11, "Read")
	appendBlkioStatEntry(&expectedStats.IoServicedRecursive, 8, 0, 41, "Write")
	appendBlkioStatEntry(&expectedStats.IoServicedRecursive, 8, 0, 21, "Sync")
	appendBlkioStatEntry(&expectedStats.IoServicedRecursive, 8, 0, 31, "Async")
	appendBlkioStatEntry(&expectedStats.IoServicedRecursive, 8, 0, 51, "Total")

	appendBlkioStatEntry(&expectedStats.IoQueuedRecursive, 8, 0, 2, "Read")
	appendBlkioStatEntry(&expectedStats.IoQueuedRecursive, 8, 0, 3, "Write")
	appendBlkioStatEntry(&expectedStats.IoQueuedRecursive, 8, 0, 4, "Sync")
	appendBlkioStatEntry(&expectedStats.IoQueuedRecursive, 8, 0, 5, "Async")
	appendBlkioStatEntry(&expectedStats.IoQueuedRecursive, 8, 0, 6, "Total")

	appendBlkioStatEntry(&expectedStats.IoServiceTimeRecursive, 8, 0, 173959, "Read")
	appendBlkioStatEntry(&expectedStats.IoServiceTimeRecursive, 8, 0, 0, "Write")
	appendBlkioStatEntry(&expectedStats.IoServiceTimeRecursive, 8, 0, 0, "Sync")
	appendBlkioStatEntry(&expectedStats.IoServiceTimeRecursive, 8, 0, 173, "Async")
	appendBlkioStatEntry(&expectedStats.IoServiceTimeRecursive, 8, 0, 174, "Total")

	appendBlkioStatEntry(&expectedStats.IoWaitTimeRecursive, 8, 0, 1557, "Read")
	appendBlkioStatEntry(&expectedStats.IoWaitTimeRecursive, 8, 0, 0, "Write")
	appendBlkioStatEntry(&expectedStats.IoWaitTimeRecursive, 8, 0, 0, "Sync")
	appendBlkioStatEntry(&expectedStats.IoWaitTimeRecursive, 8, 0, 1557, "Async")
	appendBlkioStatEntry(&expectedStats.IoWaitTimeRecursive, 8, 0, 1557, "Total")

	appendBlkioStatEntry(&expectedStats.IoMergedRecursive, 8, 0, 51, "Read")
	appendBlkioStatEntry(&expectedStats.IoMergedRecursive, 8, 0, 101, "Write")
	appendBlkioStatEntry(&expectedStats.IoMergedRecursive, 8, 0, 0, "Sync")
	appendBlkioStatEntry(&expectedStats.IoMergedRecursive, 8, 0, 0, "Async")
	appendBlkioStatEntry(&expectedStats.IoMergedRecursive, 8, 0, 151, "Total")

	appendBlkioStatEntry(&expectedStats.IoTimeRecursive, 8, 0, 16, "")

	expectBlkioStatsEquals(t, expectedStats, actualStats.BlkioStats)
}

func TestBlkioBFQStats(t *testing.T) {
	path := tempDir(t, "blkio")
	writeFileContents(t, path, blkioBFQStatsTestFiles)
	blkio := &BlkioGroup{}
	actualStats := *cgroups.NewStats()
	err := blkio.GetStats(path, &actualStats)
	if err != nil {
		t.Fatal(err)
	}

	expectedStats := cgroups.BlkioStats{}

	appendBlkioStatEntry(&expectedStats.IoServiceBytesRecursive, 8, 0, 1100, "Read")
	appendBlkioStatEntry(&expectedStats.IoServiceBytesRecursive, 8, 0, 1200, "Write")
	appendBlkioStatEntry(&expectedStats.IoServiceBytesRecursive, 8, 0, 1300, "Sync")
	appendBlkioStatEntry(&expectedStats.IoServiceBytesRecursive, 8, 0, 1500, "Async")
	appendBlkioStatEntry(&expectedStats.IoServiceBytesRecursive, 8, 0, 1500, "Total")

	appendBlkioStatEntry(&expectedStats.IoServicedRecursive, 8, 0, 11, "Read")
	appendBlkioStatEntry(&expectedStats.IoServicedRecursive, 8, 0, 41, "Write")
	appendBlkioStatEntry(&expectedStats.IoServicedRecursive, 8, 0, 21, "Sync")
	appendBlkioStatEntry(&expectedStats.IoServicedRecursive, 8, 0, 31, "Async")
	appendBlkioStatEntry(&expectedStats.IoServicedRecursive, 8, 0, 51, "Total")

	expectBlkioStatsEquals(t, expectedStats, actualStats.BlkioStats)
}

func TestBlkioStatsNoFilesBFQDebug(t *testing.T) {
	if testing.Short() {
		t.Skip("skipping test in short mode.")
	}
	testCases := []blkioStatFailureTestCase{
		{
			desc:     "missing blkio.bfq.io_service_bytes_recursive file",
			filename: "blkio.bfq.io_service_bytes_recursive",
		},
		{
			desc:     "missing blkio.bfq.io_serviced_recursive file",
			filename: "blkio.bfq.io_serviced_recursive",
		},
		{
			desc:     "missing blkio.bfq.io_queued_recursive file",
			filename: "blkio.bfq.io_queued_recursive",
		},
		{
			desc:     "missing blkio.bfq.sectors_recursive file",
			filename: "blkio.bfq.sectors_recursive",
		},
		{
			desc:     "missing blkio.bfq.io_service_time_recursive file",
			filename: "blkio.bfq.io_service_time_recursive",
		},
		{
			desc:     "missing blkio.bfq.io_wait_time_recursive file",
			filename: "blkio.bfq.io_wait_time_recursive",
		},
		{
			desc:     "missing blkio.bfq.io_merged_recursive file",
			filename: "blkio.bfq.io_merged_recursive",
		},
		{
			desc:     "missing blkio.bfq.time_recursive file",
			filename: "blkio.bfq.time_recursive",
		},
	}

	for _, testCase := range testCases {
		path := tempDir(t, "cpuset")

		tempBlkioTestFiles := maps.Clone(blkioBFQDebugStatsTestFiles)
		delete(tempBlkioTestFiles, testCase.filename)

		writeFileContents(t, path, tempBlkioTestFiles)
		cpuset := &CpusetGroup{}
		actualStats := *cgroups.NewStats()
		err := cpuset.GetStats(path, &actualStats)
		if err != nil {
			t.Errorf("%s: want no error, got: %+v", testCase.desc, err)
		}
	}
}

func TestBlkioCFQStats(t *testing.T) {
	path := tempDir(t, "blkio")
	writeFileContents(t, path, blkioCFQStatsTestFiles)

	blkio := &BlkioGroup{}
	actualStats := *cgroups.NewStats()
	err := blkio.GetStats(path, &actualStats)
	if err != nil {
		t.Fatal(err)
	}

	// Verify expected stats.
	expectedStats := cgroups.BlkioStats{}
	appendBlkioStatEntry(&expectedStats.SectorsRecursive, 8, 0, 1024, "")

	appendBlkioStatEntry(&expectedStats.IoServiceBytesRecursive, 8, 0, 100, "Read")
	appendBlkioStatEntry(&expectedStats.IoServiceBytesRecursive, 8, 0, 200, "Write")
	appendBlkioStatEntry(&expectedStats.IoServiceBytesRecursive, 8, 0, 300, "Sync")
	appendBlkioStatEntry(&expectedStats.IoServiceBytesRecursive, 8, 0, 500, "Async")
	appendBlkioStatEntry(&expectedStats.IoServiceBytesRecursive, 8, 0, 500, "Total")

	appendBlkioStatEntry(&expectedStats.IoServicedRecursive, 8, 0, 10, "Read")
	appendBlkioStatEntry(&expectedStats.IoServicedRecursive, 8, 0, 40, "Write")
	appendBlkioStatEntry(&expectedStats.IoServicedRecursive, 8, 0, 20, "Sync")
	appendBlkioStatEntry(&expectedStats.IoServicedRecursive, 8, 0, 30, "Async")
	appendBlkioStatEntry(&expectedStats.IoServicedRecursive, 8, 0, 50, "Total")

	appendBlkioStatEntry(&expectedStats.IoQueuedRecursive, 8, 0, 1, "Read")
	appendBlkioStatEntry(&expectedStats.IoQueuedRecursive, 8, 0, 4, "Write")
	appendBlkioStatEntry(&expectedStats.IoQueuedRecursive, 8, 0, 2, "Sync")
	appendBlkioStatEntry(&expectedStats.IoQueuedRecursive, 8, 0, 3, "Async")
	appendBlkioStatEntry(&expectedStats.IoQueuedRecursive, 8, 0, 5, "Total")

	appendBlkioStatEntry(&expectedStats.IoServiceTimeRecursive, 8, 0, 173959, "Read")
	appendBlkioStatEntry(&expectedStats.IoServiceTimeRecursive, 8, 0, 0, "Write")
	appendBlkioStatEntry(&expectedStats.IoServiceTimeRecursive, 8, 0, 0, "Sync")
	appendBlkioStatEntry(&expectedStats.IoServiceTimeRecursive, 8, 0, 173959, "Async")
	appendBlkioStatEntry(&expectedStats.IoServiceTimeRecursive, 8, 0, 17395, "Total")

	appendBlkioStatEntry(&expectedStats.IoWaitTimeRecursive, 8, 0, 15571, "Read")
	appendBlkioStatEntry(&expectedStats.IoWaitTimeRecursive, 8, 0, 0, "Write")
	appendBlkioStatEntry(&expectedStats.IoWaitTimeRecursive, 8, 0, 0, "Sync")
	appendBlkioStatEntry(&expectedStats.IoWaitTimeRecursive, 8, 0, 15571, "Async")
	appendBlkioStatEntry(&expectedStats.IoWaitTimeRecursive, 8, 0, 15571, "Total")

	appendBlkioStatEntry(&expectedStats.IoMergedRecursive, 8, 0, 5, "Read")
	appendBlkioStatEntry(&expectedStats.IoMergedRecursive, 8, 0, 10, "Write")
	appendBlkioStatEntry(&expectedStats.IoMergedRecursive, 8, 0, 0, "Sync")
	appendBlkioStatEntry(&expectedStats.IoMergedRecursive, 8, 0, 0, "Async")
	appendBlkioStatEntry(&expectedStats.IoMergedRecursive, 8, 0, 15, "Total")

	appendBlkioStatEntry(&expectedStats.IoTimeRecursive, 8, 0, 8, "")

	expectBlkioStatsEquals(t, expectedStats, actualStats.BlkioStats)
}

func TestBlkioStatsNoFilesCFQ(t *testing.T) {
	if testing.Short() {
		t.Skip("skipping test in short mode.")
	}
	testCases := []blkioStatFailureTestCase{
		{
			desc:     "missing blkio.io_service_bytes_recursive file",
			filename: "blkio.io_service_bytes_recursive",
		},
		{
			desc:     "missing blkio.io_serviced_recursive file",
			filename: "blkio.io_serviced_recursive",
		},
		{
			desc:     "missing blkio.io_queued_recursive file",
			filename: "blkio.io_queued_recursive",
		},
		{
			desc:     "missing blkio.sectors_recursive file",
			filename: "blkio.sectors_recursive",
		},
		{
			desc:     "missing blkio.io_service_time_recursive file",
			filename: "blkio.io_service_time_recursive",
		},
		{
			desc:     "missing blkio.io_wait_time_recursive file",
			filename: "blkio.io_wait_time_recursive",
		},
		{
			desc:     "missing blkio.io_merged_recursive file",
			filename: "blkio.io_merged_recursive",
		},
		{
			desc:     "missing blkio.time_recursive file",
			filename: "blkio.time_recursive",
		},
	}

	for _, testCase := range testCases {
		path := tempDir(t, "cpuset")

		tempBlkioTestFiles := maps.Clone(blkioCFQStatsTestFiles)
		delete(tempBlkioTestFiles, testCase.filename)

		writeFileContents(t, path, tempBlkioTestFiles)
		cpuset := &CpusetGroup{}
		actualStats := *cgroups.NewStats()
		err := cpuset.GetStats(path, &actualStats)
		if err != nil {
			t.Errorf("%s: want no error, got %+v", testCase.desc, err)
		}
	}
}

func TestBlkioStatsUnexpectedNumberOfFields(t *testing.T) {
	path := tempDir(t, "blkio")
	writeFileContents(t, path, map[string]string{
		"blkio.io_service_bytes_recursive": "8:0 Read 100 100",
		"blkio.io_serviced_recursive":      servicedRecursiveContents,
		"blkio.io_queued_recursive":        queuedRecursiveContents,
		"blkio.sectors_recursive":          sectorsRecursiveContents,
		"blkio.io_service_time_recursive":  serviceTimeRecursiveContents,
		"blkio.io_wait_time_recursive":     waitTimeRecursiveContents,
		"blkio.io_merged_recursive":        mergedRecursiveContents,
		"blkio.time_recursive":             timeRecursiveContents,
	})

	blkio := &BlkioGroup{}
	actualStats := *cgroups.NewStats()
	err := blkio.GetStats(path, &actualStats)
	if err == nil {
		t.Fatal("Expected to fail, but did not")
	}
}

func TestBlkioStatsUnexpectedFieldType(t *testing.T) {
	path := tempDir(t, "blkio")
	writeFileContents(t, path, map[string]string{
		"blkio.io_service_bytes_recursive": "8:0 Read Write",
		"blkio.io_serviced_recursive":      servicedRecursiveContents,
		"blkio.io_queued_recursive":        queuedRecursiveContents,
		"blkio.sectors_recursive":          sectorsRecursiveContents,
		"blkio.io_service_time_recursive":  serviceTimeRecursiveContents,
		"blkio.io_wait_time_recursive":     waitTimeRecursiveContents,
		"blkio.io_merged_recursive":        mergedRecursiveContents,
		"blkio.time_recursive":             timeRecursiveContents,
	})

	blkio := &BlkioGroup{}
	actualStats := *cgroups.NewStats()
	err := blkio.GetStats(path, &actualStats)
	if err == nil {
		t.Fatal("Expected to fail, but did not")
	}
}

func TestThrottleRecursiveBlkioStats(t *testing.T) {
	path := tempDir(t, "blkio")
	writeFileContents(t, path, map[string]string{
		"blkio.io_service_bytes_recursive":          "",
		"blkio.io_serviced_recursive":               "",
		"blkio.io_queued_recursive":                 "",
		"blkio.sectors_recursive":                   "",
		"blkio.io_service_time_recursive":           "",
		"blkio.io_wait_time_recursive":              "",
		"blkio.io_merged_recursive":                 "",
		"blkio.time_recursive":                      "",
		"blkio.throttle.io_service_bytes_recursive": throttleServiceBytesRecursive,
		"blkio.throttle.io_serviced_recursive":      throttleServicedRecursive,
	})

	blkio := &BlkioGroup{}
	actualStats := *cgroups.NewStats()
	err := blkio.GetStats(path, &actualStats)
	if err != nil {
		t.Fatal(err)
	}

	// Verify expected stats.
	expectedStats := cgroups.BlkioStats{}

	appendBlkioStatEntry(&expectedStats.IoServiceBytesRecursive, 8, 0, 110305281, "Read")
	appendBlkioStatEntry(&expectedStats.IoServiceBytesRecursive, 8, 0, 231, "Write")
	appendBlkioStatEntry(&expectedStats.IoServiceBytesRecursive, 8, 0, 421, "Sync")
	appendBlkioStatEntry(&expectedStats.IoServiceBytesRecursive, 8, 0, 110305281, "Async")
	appendBlkioStatEntry(&expectedStats.IoServiceBytesRecursive, 8, 0, 110305281, "Total")
	appendBlkioStatEntry(&expectedStats.IoServiceBytesRecursive, 252, 0, 110305281, "Read")
	appendBlkioStatEntry(&expectedStats.IoServiceBytesRecursive, 252, 0, 231, "Write")
	appendBlkioStatEntry(&expectedStats.IoServiceBytesRecursive, 252, 0, 421, "Sync")
	appendBlkioStatEntry(&expectedStats.IoServiceBytesRecursive, 252, 0, 110305281, "Async")
	appendBlkioStatEntry(&expectedStats.IoServiceBytesRecursive, 252, 0, 110305281, "Total")

	appendBlkioStatEntry(&expectedStats.IoServicedRecursive, 8, 0, 1641, "Read")
	appendBlkioStatEntry(&expectedStats.IoServicedRecursive, 8, 0, 231, "Write")
	appendBlkioStatEntry(&expectedStats.IoServicedRecursive, 8, 0, 421, "Sync")
	appendBlkioStatEntry(&expectedStats.IoServicedRecursive, 8, 0, 1641, "Async")
	appendBlkioStatEntry(&expectedStats.IoServicedRecursive, 8, 0, 1641, "Total")
	appendBlkioStatEntry(&expectedStats.IoServicedRecursive, 252, 0, 1641, "Read")
	appendBlkioStatEntry(&expectedStats.IoServicedRecursive, 252, 0, 231, "Write")
	appendBlkioStatEntry(&expectedStats.IoServicedRecursive, 252, 0, 421, "Sync")
	appendBlkioStatEntry(&expectedStats.IoServicedRecursive, 252, 0, 1641, "Async")
	appendBlkioStatEntry(&expectedStats.IoServicedRecursive, 252, 0, 1641, "Total")

	expectBlkioStatsEquals(t, expectedStats, actualStats.BlkioStats)
}

func TestThrottleBlkioStats(t *testing.T) {
	path := tempDir(t, "blkio")
	writeFileContents(t, path, map[string]string{
		"blkio.io_service_bytes_recursive": "",
		"blkio.io_serviced_recursive":      "",
		"blkio.io_queued_recursive":        "",
		"blkio.sectors_recursive":          "",
		"blkio.io_service_time_recursive":  "",
		"blkio.io_wait_time_recursive":     "",
		"blkio.io_merged_recursive":        "",
		"blkio.time_recursive":             "",
		"blkio.throttle.io_service_bytes":  throttleServiceBytes,
		"blkio.throttle.io_serviced":       throttleServiced,
	})

	blkio := &BlkioGroup{}
	actualStats := *cgroups.NewStats()
	err := blkio.GetStats(path, &actualStats)
	if err != nil {
		t.Fatal(err)
	}

	// Verify expected stats.
	expectedStats := cgroups.BlkioStats{}

	appendBlkioStatEntry(&expectedStats.IoServiceBytesRecursive, 8, 0, 11030528, "Read")
	appendBlkioStatEntry(&expectedStats.IoServiceBytesRecursive, 8, 0, 23, "Write")
	appendBlkioStatEntry(&expectedStats.IoServiceBytesRecursive, 8, 0, 42, "Sync")
	appendBlkioStatEntry(&expectedStats.IoServiceBytesRecursive, 8, 0, 11030528, "Async")
	appendBlkioStatEntry(&expectedStats.IoServiceBytesRecursive, 8, 0, 11030528, "Total")
	appendBlkioStatEntry(&expectedStats.IoServiceBytesRecursive, 252, 0, 11030528, "Read")
	appendBlkioStatEntry(&expectedStats.IoServiceBytesRecursive, 252, 0, 23, "Write")
	appendBlkioStatEntry(&expectedStats.IoServiceBytesRecursive, 252, 0, 42, "Sync")
	appendBlkioStatEntry(&expectedStats.IoServiceBytesRecursive, 252, 0, 11030528, "Async")
	appendBlkioStatEntry(&expectedStats.IoServiceBytesRecursive, 252, 0, 11030528, "Total")

	appendBlkioStatEntry(&expectedStats.IoServicedRecursive, 8, 0, 164, "Read")
	appendBlkioStatEntry(&expectedStats.IoServicedRecursive, 8, 0, 23, "Write")
	appendBlkioStatEntry(&expectedStats.IoServicedRecursive, 8, 0, 42, "Sync")
	appendBlkioStatEntry(&expectedStats.IoServicedRecursive, 8, 0, 164, "Async")
	appendBlkioStatEntry(&expectedStats.IoServicedRecursive, 8, 0, 164, "Total")
	appendBlkioStatEntry(&expectedStats.IoServicedRecursive, 252, 0, 164, "Read")
	appendBlkioStatEntry(&expectedStats.IoServicedRecursive, 252, 0, 23, "Write")
	appendBlkioStatEntry(&expectedStats.IoServicedRecursive, 252, 0, 42, "Sync")
	appendBlkioStatEntry(&expectedStats.IoServicedRecursive, 252, 0, 164, "Async")
	appendBlkioStatEntry(&expectedStats.IoServicedRecursive, 252, 0, 164, "Total")

	expectBlkioStatsEquals(t, expectedStats, actualStats.BlkioStats)
}

func TestBlkioSetThrottleReadBpsDevice(t *testing.T) {
	path := tempDir(t, "blkio")

	const (
		throttleBefore = `8:0 1024`
	)

	td := cgroups.NewThrottleDevice(8, 0, 2048)
	throttleAfter := td.String()

	writeFileContents(t, path, map[string]string{
		"blkio.throttle.read_bps_device": throttleBefore,
	})

	r := &cgroups.Resources{
		BlkioThrottleReadBpsDevice: []*cgroups.ThrottleDevice{td},
	}
	blkio := &BlkioGroup{}
	if err := blkio.Set(path, r); err != nil {
		t.Fatal(err)
	}

	value, err := fscommon.GetCgroupParamString(path, "blkio.throttle.read_bps_device")
	if err != nil {
		t.Fatal(err)
	}
	if value != throttleAfter {
		t.Fatal("Got the wrong value, set blkio.throttle.read_bps_device failed.")
	}
}

func TestBlkioSetThrottleWriteBpsDevice(t *testing.T) {
	path := tempDir(t, "blkio")

	const (
		throttleBefore = `8:0 1024`
	)

	td := cgroups.NewThrottleDevice(8, 0, 2048)
	throttleAfter := td.String()

	writeFileContents(t, path, map[string]string{
		"blkio.throttle.write_bps_device": throttleBefore,
	})

	r := &cgroups.Resources{
		BlkioThrottleWriteBpsDevice: []*cgroups.ThrottleDevice{td},
	}
	blkio := &BlkioGroup{}
	if err := blkio.Set(path, r); err != nil {
		t.Fatal(err)
	}

	value, err := fscommon.GetCgroupParamString(path, "blkio.throttle.write_bps_device")
	if err != nil {
		t.Fatal(err)
	}
	if value != throttleAfter {
		t.Fatal("Got the wrong value, set blkio.throttle.write_bps_device failed.")
	}
}

func TestBlkioSetThrottleReadIOpsDevice(t *testing.T) {
	path := tempDir(t, "blkio")

	const (
		throttleBefore = `8:0 1024`
	)

	td := cgroups.NewThrottleDevice(8, 0, 2048)
	throttleAfter := td.String()

	writeFileContents(t, path, map[string]string{
		"blkio.throttle.read_iops_device": throttleBefore,
	})

	r := &cgroups.Resources{
		BlkioThrottleReadIOPSDevice: []*cgroups.ThrottleDevice{td},
	}
	blkio := &BlkioGroup{}
	if err := blkio.Set(path, r); err != nil {
		t.Fatal(err)
	}

	value, err := fscommon.GetCgroupParamString(path, "blkio.throttle.read_iops_device")
	if err != nil {
		t.Fatal(err)
	}
	if value != throttleAfter {
		t.Fatal("Got the wrong value, set blkio.throttle.read_iops_device failed.")
	}
}

func TestBlkioSetThrottleWriteIOpsDevice(t *testing.T) {
	path := tempDir(t, "blkio")

	const (
		throttleBefore = `8:0 1024`
	)

	td := cgroups.NewThrottleDevice(8, 0, 2048)
	throttleAfter := td.String()

	writeFileContents(t, path, map[string]string{
		"blkio.throttle.write_iops_device": throttleBefore,
	})

	r := &cgroups.Resources{
		BlkioThrottleWriteIOPSDevice: []*cgroups.ThrottleDevice{td},
	}
	blkio := &BlkioGroup{}
	if err := blkio.Set(path, r); err != nil {
		t.Fatal(err)
	}

	value, err := fscommon.GetCgroupParamString(path, "blkio.throttle.write_iops_device")
	if err != nil {
		t.Fatal(err)
	}
	if value != throttleAfter {
		t.Fatal("Got the wrong value, set blkio.throttle.write_iops_device failed.")
	}
}