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
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
use std::collections::HashMap;
use std::io::Write;
use std::path::PathBuf;
use std::sync::mpsc::Receiver;
use crate::error::ErrorKind::*;
use crate::error::*;
use crate::events;
use crate::{read_i64_from, read_string_from, read_u64_from};
use crate::flat_keyed_to_hashmap;
use crate::{
ControllIdentifier, ControllerInternal, Controllers, CustomizedAttribute, MaxValue,
MemoryResources, Resources, Subsystem,
};
#[derive(Debug, Clone)]
pub struct MemController {
base: PathBuf,
path: PathBuf,
v2: bool,
}
#[derive(Default, Debug, PartialEq, Eq)]
pub struct SetMemory {
pub low: Option<MaxValue>,
pub high: Option<MaxValue>,
pub min: Option<MaxValue>,
pub max: Option<MaxValue>,
}
#[derive(Default, Debug, PartialEq, Eq)]
pub struct OomControl {
pub oom_kill_disable: bool,
pub under_oom: bool,
pub oom_kill: u64,
}
#[allow(clippy::unnecessary_wraps)]
fn parse_oom_control(s: String) -> Result<OomControl> {
let spl = s.split_whitespace().collect::<Vec<_>>();
let oom_kill_disable = if spl.len() > 1 {
spl[1].parse::<u64>().unwrap() == 1
} else {
false
};
let under_oom = if spl.len() > 3 {
spl[3].parse::<u64>().unwrap() == 1
} else {
false
};
let oom_kill = if spl.len() > 5 {
spl[5].parse::<u64>().unwrap()
} else {
0
};
Ok(OomControl {
oom_kill_disable,
under_oom,
oom_kill,
})
}
#[derive(Default, Debug, PartialEq, Eq)]
pub struct NumaStat {
pub total_pages: u64,
pub total_pages_per_node: Vec<u64>,
pub file_pages: u64,
pub file_pages_per_node: Vec<u64>,
pub anon_pages: u64,
pub anon_pages_per_node: Vec<u64>,
pub unevictable_pages: u64,
pub unevictable_pages_per_node: Vec<u64>,
pub hierarchical_total_pages: u64,
pub hierarchical_total_pages_per_node: Vec<u64>,
pub hierarchical_file_pages: u64,
pub hierarchical_file_pages_per_node: Vec<u64>,
pub hierarchical_anon_pages: u64,
pub hierarchical_anon_pages_per_node: Vec<u64>,
pub hierarchical_unevictable_pages: u64,
pub hierarchical_unevictable_pages_per_node: Vec<u64>,
}
#[allow(clippy::unnecessary_wraps)]
fn parse_numa_stat(s: String) -> Result<NumaStat> {
let _nodes = (s.split_whitespace().count() - 8) / 8;
let mut ls = s.lines();
let total_line = ls.next().unwrap();
let file_line = ls.next().unwrap();
let anon_line = ls.next().unwrap();
let unevict_line = ls.next().unwrap();
let hier_total_line = ls.next().unwrap_or_default();
let hier_file_line = ls.next().unwrap_or_default();
let hier_anon_line = ls.next().unwrap_or_default();
let hier_unevict_line = ls.next().unwrap_or_default();
Ok(NumaStat {
total_pages: total_line
.split(|x| x == ' ' || x == '=')
.collect::<Vec<_>>()[1]
.parse::<u64>()
.unwrap_or(0),
total_pages_per_node: {
let spl = &total_line.split(' ').collect::<Vec<_>>()[1..];
spl.iter()
.map(|x| {
x.split('=').collect::<Vec<_>>()[1]
.parse::<u64>()
.unwrap_or(0)
})
.collect()
},
file_pages: file_line
.split(|x| x == ' ' || x == '=')
.collect::<Vec<_>>()[1]
.parse::<u64>()
.unwrap_or(0),
file_pages_per_node: {
let spl = &file_line.split(' ').collect::<Vec<_>>()[1..];
spl.iter()
.map(|x| {
x.split('=').collect::<Vec<_>>()[1]
.parse::<u64>()
.unwrap_or(0)
})
.collect()
},
anon_pages: anon_line
.split(|x| x == ' ' || x == '=')
.collect::<Vec<_>>()[1]
.parse::<u64>()
.unwrap_or(0),
anon_pages_per_node: {
let spl = &anon_line.split(' ').collect::<Vec<_>>()[1..];
spl.iter()
.map(|x| {
x.split('=').collect::<Vec<_>>()[1]
.parse::<u64>()
.unwrap_or(0)
})
.collect()
},
unevictable_pages: unevict_line
.split(|x| x == ' ' || x == '=')
.collect::<Vec<_>>()[1]
.parse::<u64>()
.unwrap_or(0),
unevictable_pages_per_node: {
let spl = &unevict_line.split(' ').collect::<Vec<_>>()[1..];
spl.iter()
.map(|x| {
x.split('=').collect::<Vec<_>>()[1]
.parse::<u64>()
.unwrap_or(0)
})
.collect()
},
hierarchical_total_pages: {
if !hier_total_line.is_empty() {
hier_total_line
.split(|x| x == ' ' || x == '=')
.collect::<Vec<_>>()[1]
.parse::<u64>()
.unwrap_or(0)
} else {
0
}
},
hierarchical_total_pages_per_node: {
if !hier_total_line.is_empty() {
let spl = &hier_total_line.split(' ').collect::<Vec<_>>()[1..];
spl.iter()
.map(|x| {
x.split('=').collect::<Vec<_>>()[1]
.parse::<u64>()
.unwrap_or(0)
})
.collect()
} else {
Vec::new()
}
},
hierarchical_file_pages: {
if !hier_file_line.is_empty() {
hier_file_line
.split(|x| x == ' ' || x == '=')
.collect::<Vec<_>>()[1]
.parse::<u64>()
.unwrap_or(0)
} else {
0
}
},
hierarchical_file_pages_per_node: {
if !hier_file_line.is_empty() {
let spl = &hier_file_line.split(' ').collect::<Vec<_>>()[1..];
spl.iter()
.map(|x| {
x.split('=').collect::<Vec<_>>()[1]
.parse::<u64>()
.unwrap_or(0)
})
.collect()
} else {
Vec::new()
}
},
hierarchical_anon_pages: {
if !hier_anon_line.is_empty() {
hier_anon_line
.split(|x| x == ' ' || x == '=')
.collect::<Vec<_>>()[1]
.parse::<u64>()
.unwrap_or(0)
} else {
0
}
},
hierarchical_anon_pages_per_node: {
if !hier_anon_line.is_empty() {
let spl = &hier_anon_line.split(' ').collect::<Vec<_>>()[1..];
spl.iter()
.map(|x| {
x.split('=').collect::<Vec<_>>()[1]
.parse::<u64>()
.unwrap_or(0)
})
.collect()
} else {
Vec::new()
}
},
hierarchical_unevictable_pages: {
if !hier_unevict_line.is_empty() {
hier_unevict_line
.split(|x| x == ' ' || x == '=')
.collect::<Vec<_>>()[1]
.parse::<u64>()
.unwrap_or(0)
} else {
0
}
},
hierarchical_unevictable_pages_per_node: {
if !hier_unevict_line.is_empty() {
let spl = &hier_unevict_line.split(' ').collect::<Vec<_>>()[1..];
spl.iter()
.map(|x| {
x.split('=').collect::<Vec<_>>()[1]
.parse::<u64>()
.unwrap_or(0)
})
.collect()
} else {
Vec::new()
}
},
})
}
#[derive(Default, Debug, PartialEq, Eq)]
pub struct MemoryStat {
pub cache: u64,
pub rss: u64,
pub rss_huge: u64,
pub shmem: u64,
pub mapped_file: u64,
pub dirty: u64,
pub writeback: u64,
pub swap: u64,
pub pgpgin: u64,
pub pgpgout: u64,
pub pgfault: u64,
pub pgmajfault: u64,
pub inactive_anon: u64,
pub active_anon: u64,
pub inactive_file: u64,
pub active_file: u64,
pub unevictable: u64,
pub hierarchical_memory_limit: i64,
pub hierarchical_memsw_limit: i64,
pub total_cache: u64,
pub total_rss: u64,
pub total_rss_huge: u64,
pub total_shmem: u64,
pub total_mapped_file: u64,
pub total_dirty: u64,
pub total_writeback: u64,
pub total_swap: u64,
pub total_pgpgin: u64,
pub total_pgpgout: u64,
pub total_pgfault: u64,
pub total_pgmajfault: u64,
pub total_inactive_anon: u64,
pub total_active_anon: u64,
pub total_inactive_file: u64,
pub total_active_file: u64,
pub total_unevictable: u64,
pub raw: HashMap<String, u64>,
}
#[allow(clippy::unnecessary_wraps)]
fn parse_memory_stat(s: String) -> Result<MemoryStat> {
let mut raw = HashMap::new();
for l in s.lines() {
let t: Vec<&str> = l.split(' ').collect();
if t.len() != 2 {
continue;
}
let n = t[1].trim().parse::<u64>();
if n.is_err() {
continue;
}
raw.insert(t[0].to_string(), n.unwrap());
}
Ok(MemoryStat {
cache: *raw.get("cache").unwrap_or(&0),
rss: *raw.get("rss").unwrap_or(&0),
rss_huge: *raw.get("rss_huge").unwrap_or(&0),
shmem: *raw.get("shmem").unwrap_or(&0),
mapped_file: *raw.get("mapped_file").unwrap_or(&0),
dirty: *raw.get("dirty").unwrap_or(&0),
writeback: *raw.get("writeback").unwrap_or(&0),
swap: *raw.get("swap").unwrap_or(&0),
pgpgin: *raw.get("pgpgin").unwrap_or(&0),
pgpgout: *raw.get("pgpgout").unwrap_or(&0),
pgfault: *raw.get("pgfault").unwrap_or(&0),
pgmajfault: *raw.get("pgmajfault").unwrap_or(&0),
inactive_anon: *raw.get("inactive_anon").unwrap_or(&0),
active_anon: *raw.get("active_anon").unwrap_or(&0),
inactive_file: *raw.get("inactive_file").unwrap_or(&0),
active_file: *raw.get("active_file").unwrap_or(&0),
unevictable: *raw.get("unevictable").unwrap_or(&0),
hierarchical_memory_limit: *raw.get("hierarchical_memory_limit").unwrap_or(&0) as i64,
hierarchical_memsw_limit: *raw.get("hierarchical_memsw_limit").unwrap_or(&0) as i64,
total_cache: *raw.get("total_cache").unwrap_or(&0),
total_rss: *raw.get("total_rss").unwrap_or(&0),
total_rss_huge: *raw.get("total_rss_huge").unwrap_or(&0),
total_shmem: *raw.get("total_shmem").unwrap_or(&0),
total_mapped_file: *raw.get("total_mapped_file").unwrap_or(&0),
total_dirty: *raw.get("total_dirty").unwrap_or(&0),
total_writeback: *raw.get("total_writeback").unwrap_or(&0),
total_swap: *raw.get("total_swap").unwrap_or(&0),
total_pgpgin: *raw.get("total_pgpgin").unwrap_or(&0),
total_pgpgout: *raw.get("total_pgpgout").unwrap_or(&0),
total_pgfault: *raw.get("total_pgfault").unwrap_or(&0),
total_pgmajfault: *raw.get("total_pgmajfault").unwrap_or(&0),
total_inactive_anon: *raw.get("total_inactive_anon").unwrap_or(&0),
total_active_anon: *raw.get("total_active_anon").unwrap_or(&0),
total_inactive_file: *raw.get("total_inactive_file").unwrap_or(&0),
total_active_file: *raw.get("total_active_file").unwrap_or(&0),
total_unevictable: *raw.get("total_unevictable").unwrap_or(&0),
raw,
})
}
#[derive(Debug)]
pub struct MemSwap {
pub fail_cnt: u64,
pub limit_in_bytes: i64,
pub usage_in_bytes: u64,
pub max_usage_in_bytes: u64,
}
#[derive(Debug)]
pub struct Memory {
pub fail_cnt: u64,
pub limit_in_bytes: i64,
pub usage_in_bytes: u64,
pub max_usage_in_bytes: u64,
pub move_charge_at_immigrate: u64,
pub numa_stat: NumaStat,
pub oom_control: OomControl,
pub soft_limit_in_bytes: i64,
pub stat: MemoryStat,
pub swappiness: u64,
pub use_hierarchy: u64,
}
#[derive(Debug)]
pub struct Tcp {
pub fail_cnt: u64,
pub limit_in_bytes: i64,
pub usage_in_bytes: u64,
pub max_usage_in_bytes: u64,
}
#[derive(Debug)]
pub struct Kmem {
pub fail_cnt: u64,
pub limit_in_bytes: i64,
pub usage_in_bytes: u64,
pub max_usage_in_bytes: u64,
pub slabinfo: String,
}
impl ControllerInternal for MemController {
fn control_type(&self) -> Controllers {
Controllers::Mem
}
fn get_path(&self) -> &PathBuf {
&self.path
}
fn get_path_mut(&mut self) -> &mut PathBuf {
&mut self.path
}
fn get_base(&self) -> &PathBuf {
&self.base
}
fn is_v2(&self) -> bool {
self.v2
}
fn apply(&self, res: &Resources) -> Result<()> {
let memres: &MemoryResources = &res.memory;
update!(self, set_limit, memres.memory_hard_limit);
update!(self, set_soft_limit, memres.memory_soft_limit);
update!(self, set_kmem_limit, memres.kernel_memory_limit);
update!(self, set_memswap_limit, memres.memory_swap_limit);
update!(self, set_tcp_limit, memres.kernel_tcp_memory_limit);
update!(self, set_swappiness, memres.swappiness);
memres.attrs.iter().for_each(|(k, v)| {
let _ = self.set(k, v);
});
Ok(())
}
}
impl MemController {
pub fn new(root: PathBuf, v2: bool) -> Self {
Self {
base: root.clone(),
path: root,
v2,
}
}
pub fn set_mem(&self, m: SetMemory) -> Result<()> {
let values = vec![
(m.high, "memory.high"),
(m.low, "memory.low"),
(m.max, "memory.max"),
(m.min, "memory.min"),
];
for value in values {
let v = value.0;
let f = value.1;
if let Some(v) = v {
let v = v.to_string();
self.open_path(f, true).and_then(|mut file| {
file.write_all(v.as_ref())
.map_err(|e| Error::with_cause(WriteFailed, e))
})?;
}
}
Ok(())
}
pub fn get_mem(&self) -> Result<SetMemory> {
let mut m: SetMemory = Default::default();
self.get_max_value("memory.high")
.map(|x| m.high = Some(x))?;
self.get_max_value("memory.low").map(|x| m.low = Some(x))?;
self.get_max_value("memory.max").map(|x| m.max = Some(x))?;
self.get_max_value("memory.min").map(|x| m.min = Some(x))?;
Ok(m)
}
fn memory_stat_v2(&self) -> Memory {
let set = self.get_mem().unwrap();
Memory {
fail_cnt: 0,
limit_in_bytes: set.max.unwrap().to_i64(),
usage_in_bytes: self
.open_path("memory.current", false)
.and_then(read_u64_from)
.unwrap_or(0),
max_usage_in_bytes: 0,
move_charge_at_immigrate: 0,
numa_stat: NumaStat::default(),
oom_control: OomControl::default(),
soft_limit_in_bytes: set.low.unwrap().to_i64(),
stat: self
.open_path("memory.stat", false)
.and_then(read_string_from)
.and_then(parse_memory_stat)
.unwrap_or_default(),
swappiness: self
.open_path("memory.swap.current", false)
.and_then(read_u64_from)
.unwrap_or(0),
use_hierarchy: 0,
}
}
pub fn memory_stat(&self) -> Memory {
if self.v2 {
return self.memory_stat_v2();
}
Memory {
fail_cnt: self
.open_path("memory.failcnt", false)
.and_then(read_u64_from)
.unwrap_or(0),
limit_in_bytes: self
.open_path("memory.limit_in_bytes", false)
.and_then(read_i64_from)
.unwrap_or(0),
usage_in_bytes: self
.open_path("memory.usage_in_bytes", false)
.and_then(read_u64_from)
.unwrap_or(0),
max_usage_in_bytes: self
.open_path("memory.max_usage_in_bytes", false)
.and_then(read_u64_from)
.unwrap_or(0),
move_charge_at_immigrate: self
.open_path("memory.move_charge_at_immigrate", false)
.and_then(read_u64_from)
.unwrap_or(0),
numa_stat: self
.open_path("memory.numa_stat", false)
.and_then(read_string_from)
.and_then(parse_numa_stat)
.unwrap_or_default(),
oom_control: self
.open_path("memory.oom_control", false)
.and_then(read_string_from)
.and_then(parse_oom_control)
.unwrap_or_default(),
soft_limit_in_bytes: self
.open_path("memory.soft_limit_in_bytes", false)
.and_then(read_i64_from)
.unwrap_or(0),
stat: self
.open_path("memory.stat", false)
.and_then(read_string_from)
.and_then(parse_memory_stat)
.unwrap_or_default(),
swappiness: self
.open_path("memory.swappiness", false)
.and_then(read_u64_from)
.unwrap_or(0),
use_hierarchy: self
.open_path("memory.use_hierarchy", false)
.and_then(read_u64_from)
.unwrap_or(0),
}
}
pub fn kmem_stat(&self) -> Kmem {
Kmem {
fail_cnt: self
.open_path("memory.kmem.failcnt", false)
.and_then(read_u64_from)
.unwrap_or(0),
limit_in_bytes: self
.open_path("memory.kmem.limit_in_bytes", false)
.and_then(read_i64_from)
.unwrap_or(-1),
usage_in_bytes: self
.open_path("memory.kmem.usage_in_bytes", false)
.and_then(read_u64_from)
.unwrap_or(0),
max_usage_in_bytes: self
.open_path("memory.kmem.max_usage_in_bytes", false)
.and_then(read_u64_from)
.unwrap_or(0),
slabinfo: self
.open_path("memory.kmem.slabinfo", false)
.and_then(read_string_from)
.unwrap_or_default(),
}
}
pub fn kmem_tcp_stat(&self) -> Tcp {
Tcp {
fail_cnt: self
.open_path("memory.kmem.tcp.failcnt", false)
.and_then(read_u64_from)
.unwrap_or(0),
limit_in_bytes: self
.open_path("memory.kmem.tcp.limit_in_bytes", false)
.and_then(read_i64_from)
.unwrap_or(0),
usage_in_bytes: self
.open_path("memory.kmem.tcp.usage_in_bytes", false)
.and_then(read_u64_from)
.unwrap_or(0),
max_usage_in_bytes: self
.open_path("memory.kmem.tcp.max_usage_in_bytes", false)
.and_then(read_u64_from)
.unwrap_or(0),
}
}
pub fn memswap_v2(&self) -> MemSwap {
MemSwap {
fail_cnt: self
.open_path("memory.swap.events", false)
.and_then(flat_keyed_to_hashmap)
.map(|x| *x.get("fail").unwrap_or(&0) as u64)
.unwrap(),
limit_in_bytes: self
.open_path("memory.swap.max", false)
.and_then(read_i64_from)
.unwrap_or(0),
usage_in_bytes: self
.open_path("memory.swap.current", false)
.and_then(read_u64_from)
.unwrap_or(0),
max_usage_in_bytes: 0,
}
}
pub fn memswap(&self) -> MemSwap {
if self.v2 {
return self.memswap_v2();
}
MemSwap {
fail_cnt: self
.open_path("memory.memsw.failcnt", false)
.and_then(read_u64_from)
.unwrap_or(0),
limit_in_bytes: self
.open_path("memory.memsw.limit_in_bytes", false)
.and_then(read_i64_from)
.unwrap_or(0),
usage_in_bytes: self
.open_path("memory.memsw.usage_in_bytes", false)
.and_then(read_u64_from)
.unwrap_or(0),
max_usage_in_bytes: self
.open_path("memory.memsw.max_usage_in_bytes", false)
.and_then(read_u64_from)
.unwrap_or(0),
}
}
pub fn reset_fail_count(&self) -> Result<()> {
self.open_path("memory.failcnt", true).and_then(|mut file| {
file.write_all("0".to_string().as_ref())
.map_err(|e| Error::with_cause(WriteFailed, e))
})
}
pub fn reset_kmem_fail_count(&self) -> Result<()> {
if self.v2 {
return Ok(());
}
self.open_path("memory.kmem.failcnt", true)
.and_then(|mut file| {
file.write_all("0".to_string().as_ref())
.map_err(|e| Error::with_cause(WriteFailed, e))
})
}
pub fn reset_tcp_fail_count(&self) -> Result<()> {
if self.v2 {
return Ok(());
}
self.open_path("memory.kmem.tcp.failcnt", true)
.and_then(|mut file| {
file.write_all("0".to_string().as_ref())
.map_err(|e| Error::with_cause(WriteFailed, e))
})
}
pub fn reset_memswap_fail_count(&self) -> Result<()> {
self.open_path("memory.memsw.failcnt", true)
.and_then(|mut file| {
file.write_all("0".to_string().as_ref())
.map_err(|e| Error::with_cause(WriteFailed, e))
})
}
pub fn reset_max_usage(&self) -> Result<()> {
self.open_path("memory.max_usage_in_bytes", true)
.and_then(|mut file| {
file.write_all("0".to_string().as_ref())
.map_err(|e| Error::with_cause(WriteFailed, e))
})
}
pub fn set_limit(&self, limit: i64) -> Result<()> {
let mut file = "memory.limit_in_bytes";
if self.v2 {
file = "memory.max";
}
self.open_path(file, true).and_then(|mut file| {
file.write_all(limit.to_string().as_ref())
.map_err(|e| Error::with_cause(WriteFailed, e))
})
}
pub fn set_kmem_limit(&self, limit: i64) -> Result<()> {
if self.v2 {
return Ok(());
}
self.open_path("memory.kmem.limit_in_bytes", true)
.and_then(|mut file| {
file.write_all(limit.to_string().as_ref())
.map_err(|e| Error::with_cause(WriteFailed, e))
})
}
pub fn set_memswap_limit(&self, limit: i64) -> Result<()> {
let mut file = "memory.memsw.limit_in_bytes";
if self.v2 {
file = "memory.swap.max";
}
self.open_path(file, true).and_then(|mut file| {
file.write_all(limit.to_string().as_ref())
.map_err(|e| Error::with_cause(WriteFailed, e))
})
}
pub fn set_tcp_limit(&self, limit: i64) -> Result<()> {
if self.v2 {
return Ok(());
}
self.open_path("memory.kmem.tcp.limit_in_bytes", true)
.and_then(|mut file| {
file.write_all(limit.to_string().as_ref())
.map_err(|e| Error::with_cause(WriteFailed, e))
})
}
pub fn set_soft_limit(&self, limit: i64) -> Result<()> {
let mut file = "memory.soft_limit_in_bytes";
if self.v2 {
file = "memory.low"
}
self.open_path(file, true).and_then(|mut file| {
file.write_all(limit.to_string().as_ref())
.map_err(|e| Error::with_cause(WriteFailed, e))
})
}
pub fn set_swappiness(&self, swp: u64) -> Result<()> {
let mut file = "memory.swappiness";
if self.v2 {
file = "memory.swap.max"
}
self.open_path(file, true).and_then(|mut file| {
file.write_all(swp.to_string().as_ref())
.map_err(|e| Error::with_cause(WriteFailed, e))
})
}
pub fn disable_oom_killer(&self) -> Result<()> {
self.open_path("memory.oom_control", true)
.and_then(|mut file| {
file.write_all("1".to_string().as_ref())
.map_err(|e| Error::with_cause(WriteFailed, e))
})
}
pub fn register_oom_event(&self, key: &str) -> Result<Receiver<String>> {
if self.v2 {
events::notify_on_oom_v2(key, self.get_path())
} else {
events::notify_on_oom_v1(key, self.get_path())
}
}
}
impl ControllIdentifier for MemController {
fn controller_type() -> Controllers {
Controllers::Mem
}
}
impl CustomizedAttribute for MemController {}
impl<'a> From<&'a Subsystem> for &'a MemController {
fn from(sub: &'a Subsystem) -> &'a MemController {
unsafe {
match sub {
Subsystem::Mem(c) => c,
_ => {
assert_eq!(1, 0);
let v = std::mem::MaybeUninit::uninit();
v.assume_init()
}
}
}
}
}
#[cfg(test)]
mod tests {
use crate::memory::{
parse_memory_stat, parse_numa_stat, parse_oom_control, MemoryStat, NumaStat, OomControl,
};
static GOOD_VALUE: &str = "\
total=51189 N0=51189 N1=123
file=50175 N0=50175 N1=123
anon=1014 N0=1014 N1=123
unevictable=0 N0=0 N1=123
hierarchical_total=1628573 N0=1628573 N1=123
hierarchical_file=858151 N0=858151 N1=123
hierarchical_anon=770402 N0=770402 N1=123
hierarchical_unevictable=20 N0=20 N1=123
";
static GOOD_VALUE_NON_HIERARCHICAL: &str = "\
total=51189 N0=51189 N1=123
file=50175 N0=50175 N1=123
anon=1014 N0=1014 N1=123
unevictable=0 N0=0 N1=123
";
static GOOD_OOMCONTROL_VAL_1: &str = "\
oom_kill_disable 0
oom_kill 1337
";
static GOOD_OOMCONTROL_VAL_2: &str = "\
oom_kill_disable 0
under_oom 1
";
static GOOD_OOMCONTROL_VAL_3: &str = "\
oom_kill_disable 0
under_oom 1
oom_kill 1337
";
static GOOD_MEMORYSTAT_VAL: &str = "\
cache 178880512
rss 4206592
rss_huge 0
shmem 106496
mapped_file 7491584
dirty 114688
writeback 49152
swap 0
pgpgin 213928
pgpgout 169220
pgfault 87064
pgmajfault 202
inactive_anon 0
active_anon 4153344
inactive_file 84779008
active_file 94273536
unevictable 0
hierarchical_memory_limit 9223372036854771712
hierarchical_memsw_limit 9223372036854771712
total_cache 4200333312
total_rss 2927677440
total_rss_huge 0
total_shmem 590061568
total_mapped_file 1086164992
total_dirty 1769472
total_writeback 602112
total_swap 0
total_pgpgin 5267326291
total_pgpgout 5265586647
total_pgfault 9947902469
total_pgmajfault 25132
total_inactive_anon 585981952
total_active_anon 2928996352
total_inactive_file 1272135680
total_active_file 2338816000
total_unevictable 81920
";
#[test]
fn test_parse_numa_stat() {
let ok = parse_numa_stat(GOOD_VALUE.to_string()).unwrap();
assert_eq!(
ok,
NumaStat {
total_pages: 51189,
total_pages_per_node: vec![51189, 123],
file_pages: 50175,
file_pages_per_node: vec![50175, 123],
anon_pages: 1014,
anon_pages_per_node: vec![1014, 123],
unevictable_pages: 0,
unevictable_pages_per_node: vec![0, 123],
hierarchical_total_pages: 1628573,
hierarchical_total_pages_per_node: vec![1628573, 123],
hierarchical_file_pages: 858151,
hierarchical_file_pages_per_node: vec![858151, 123],
hierarchical_anon_pages: 770402,
hierarchical_anon_pages_per_node: vec![770402, 123],
hierarchical_unevictable_pages: 20,
hierarchical_unevictable_pages_per_node: vec![20, 123],
}
);
let ok = parse_numa_stat(GOOD_VALUE_NON_HIERARCHICAL.to_string()).unwrap();
assert_eq!(
ok,
NumaStat {
total_pages: 51189,
total_pages_per_node: vec![51189, 123],
file_pages: 50175,
file_pages_per_node: vec![50175, 123],
anon_pages: 1014,
anon_pages_per_node: vec![1014, 123],
unevictable_pages: 0,
unevictable_pages_per_node: vec![0, 123],
hierarchical_total_pages: 0,
hierarchical_total_pages_per_node: vec![],
hierarchical_file_pages: 0,
hierarchical_file_pages_per_node: vec![],
hierarchical_anon_pages: 0,
hierarchical_anon_pages_per_node: vec![],
hierarchical_unevictable_pages: 0,
hierarchical_unevictable_pages_per_node: vec![],
}
);
}
#[test]
fn test_parse_oom_control() {
let ok = parse_oom_control("".to_string()).unwrap();
assert_eq!(
ok,
OomControl {
oom_kill_disable: false,
under_oom: false,
oom_kill: 0,
}
);
let ok = parse_oom_control(GOOD_OOMCONTROL_VAL_1.to_string()).unwrap();
assert_eq!(
ok,
OomControl {
oom_kill_disable: false,
under_oom: false,
oom_kill: 0,
}
);
let ok = parse_oom_control(GOOD_OOMCONTROL_VAL_2.to_string()).unwrap();
assert_eq!(
ok,
OomControl {
oom_kill_disable: false,
under_oom: true,
oom_kill: 0,
}
);
let ok = parse_oom_control(GOOD_OOMCONTROL_VAL_3.to_string()).unwrap();
assert_eq!(
ok,
OomControl {
oom_kill_disable: false,
under_oom: true,
oom_kill: 1337,
}
);
}
#[test]
fn test_parse_memory_stat() {
let ok = parse_memory_stat(GOOD_MEMORYSTAT_VAL.to_string()).unwrap();
let raw = ok.raw.clone();
assert_eq!(
ok,
MemoryStat {
cache: 178880512,
rss: 4206592,
rss_huge: 0,
shmem: 106496,
mapped_file: 7491584,
dirty: 114688,
writeback: 49152,
swap: 0,
pgpgin: 213928,
pgpgout: 169220,
pgfault: 87064,
pgmajfault: 202,
inactive_anon: 0,
active_anon: 4153344,
inactive_file: 84779008,
active_file: 94273536,
unevictable: 0,
hierarchical_memory_limit: 9223372036854771712,
hierarchical_memsw_limit: 9223372036854771712,
total_cache: 4200333312,
total_rss: 2927677440,
total_rss_huge: 0,
total_shmem: 590061568,
total_mapped_file: 1086164992,
total_dirty: 1769472,
total_writeback: 602112,
total_swap: 0,
total_pgpgin: 5267326291,
total_pgpgout: 5265586647,
total_pgfault: 9947902469,
total_pgmajfault: 25132,
total_inactive_anon: 585981952,
total_active_anon: 2928996352,
total_inactive_file: 1272135680,
total_active_file: 2338816000,
total_unevictable: 81920,
raw,
}
);
}
}