@@ -9,27 +9,29 @@ use std::{
9
9
io:: { Error , ErrorKind } ,
10
10
} ;
11
11
12
- #[ derive( Debug , Default ) ]
12
+ #[ derive( Debug , Default , Clone ) ]
13
13
pub ( crate ) struct SlabInfo {
14
- pub ( crate ) meta : Vec < String > ,
15
- pub ( crate ) data : Vec < ( String , Vec < u64 > ) > ,
14
+ pub ( crate ) meta : im :: Vector < String > ,
15
+ pub ( crate ) data : im :: Vector < ( String , Vec < u64 > ) > ,
16
16
}
17
17
18
18
impl SlabInfo {
19
19
// parse slabinfo from /proc/slabinfo
20
+ //
20
21
// need root permission
21
22
pub fn new ( ) -> Result < SlabInfo , Error > {
22
23
let content = fs:: read_to_string ( "/proc/slabinfo" ) ?;
23
24
24
- Self :: parse ( & content) . ok_or ( ErrorKind :: Unsupported . into ( ) )
25
+ Self :: with_slabinfo ( & content) . ok_or ( ErrorKind :: Unsupported . into ( ) )
25
26
}
26
27
27
- pub fn parse ( content : & str ) -> Option < SlabInfo > {
28
+ pub fn with_slabinfo ( content : & str ) -> Option < SlabInfo > {
28
29
let mut lines: Vec < & str > = content. lines ( ) . collect ( ) ;
29
30
30
31
let _ = parse_version ( lines. remove ( 0 ) ) ?;
31
32
let meta = parse_meta ( lines. remove ( 0 ) ) ;
32
- let data: Vec < ( String , Vec < u64 > ) > = lines. into_iter ( ) . filter_map ( parse_data) . collect ( ) ;
33
+ let data: im:: Vector < ( String , Vec < u64 > ) > =
34
+ lines. into_iter ( ) . filter_map ( parse_data) . collect ( ) ;
33
35
34
36
Some ( SlabInfo { meta, data } )
35
37
}
@@ -43,6 +45,7 @@ impl SlabInfo {
43
45
item. get ( offset) . copied ( )
44
46
}
45
47
48
+ // Get all processes name
46
49
pub fn names ( & self ) -> Vec < & String > {
47
50
self . data . iter ( ) . map ( |( k, _) | k) . collect ( )
48
51
}
@@ -190,15 +193,17 @@ impl SlabInfo {
190
193
return 0 ;
191
194
} ;
192
195
193
- let iter = self . data . iter ( ) . filter_map ( |( _, data) | data. get ( offset) ) ;
194
-
195
- let count = iter. clone ( ) . count ( ) ;
196
- let sum = iter. sum :: < u64 > ( ) ;
196
+ let objsize = self
197
+ . data
198
+ . iter ( )
199
+ . filter_map ( |( _, data) | data. get ( offset) )
200
+ . collect :: < im:: Vector < _ > > ( ) ;
197
201
202
+ let count = objsize. clone ( ) . iter ( ) . count ( ) ;
198
203
if count == 0 {
199
204
0
200
205
} else {
201
- ( sum) / ( count as u64 )
206
+ ( objsize . into_iter ( ) . sum :: < u64 > ( ) ) / ( count as u64 )
202
207
}
203
208
}
204
209
@@ -266,7 +271,7 @@ pub(crate) fn parse_version(line: &str) -> Option<String> {
266
271
. map ( String :: from)
267
272
}
268
273
269
- pub ( crate ) fn parse_meta ( line : & str ) -> Vec < String > {
274
+ pub ( crate ) fn parse_meta ( line : & str ) -> im :: Vector < String > {
270
275
line. replace ( [ '#' , ':' ] , " " )
271
276
. split_whitespace ( )
272
277
. filter ( |it| it. starts_with ( '<' ) && it. ends_with ( '>' ) )
@@ -310,8 +315,8 @@ mod tests {
310
315
let result = parse_meta ( test) ;
311
316
312
317
assert_eq ! (
313
- result,
314
- [
318
+ result. into_iter ( ) . collect :: < Vec <_>> ( ) ,
319
+ vec! [
315
320
"active_objs" ,
316
321
"num_objs" ,
317
322
"objsize" ,
@@ -348,7 +353,7 @@ mod tests {
348
353
#[ test]
349
354
fn test_parse ( ) {
350
355
let test = include_str ! ( "../../../../tests/fixtures/slabtop/data.txt" ) ;
351
- let result = SlabInfo :: parse ( test. into ( ) ) . unwrap ( ) ;
356
+ let result = SlabInfo :: with_slabinfo ( test) . unwrap ( ) ;
352
357
353
358
assert_eq ! ( result. fetch( "nf_conntrack_expect" , "objsize" ) . unwrap( ) , 208 ) ;
354
359
assert_eq ! (
0 commit comments