@@ -38,6 +38,13 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
3838
3939 let one_header = matches. get_flag ( "one-header" ) ;
4040 let no_first = matches. get_flag ( "no-first" ) ;
41+ let term_height = terminal_size:: terminal_size ( )
42+ . map ( |size| size. 1 . 0 )
43+ . unwrap_or ( 0 ) ;
44+
45+ if matches. get_flag ( "slabs" ) {
46+ return print_slabs ( one_header, term_height) ;
47+ }
4148
4249 let delay = matches. get_one :: < u64 > ( "delay" ) ;
4350 let count = matches. get_one :: < u64 > ( "count" ) ;
@@ -57,14 +64,10 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
5764 line_count += 1 ;
5865 }
5966
60- let term_height = terminal_size:: terminal_size ( )
61- . map ( |size| size. 1 . 0 )
62- . unwrap_or ( 0 ) ;
63-
6467 while count. is_none ( ) || line_count < count. unwrap ( ) {
6568 std:: thread:: sleep ( std:: time:: Duration :: from_secs ( delay) ) ;
6669 let proc_data_now = ProcData :: new ( ) ;
67- if ! one_header && term_height > 0 && ( ( line_count + 3 ) % term_height as u64 == 0 ) {
70+ if needs_header ( one_header, term_height, line_count) {
6871 print_header ( & pickers) ;
6972 }
7073 print_data ( & pickers, & proc_data_now, Some ( & proc_data) , & matches) ;
@@ -76,6 +79,41 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
7679 Ok ( ( ) )
7780}
7881
82+ #[ cfg( target_os = "linux" ) ]
83+ fn print_slabs ( one_header : bool , term_height : u16 ) -> UResult < ( ) > {
84+ let mut slab_data = uu_slabtop:: SlabInfo :: new ( ) ?. data ;
85+
86+ slab_data. sort_by_key ( |k| k. 0 . to_lowercase ( ) ) ;
87+
88+ print_slab_header ( ) ;
89+
90+ for ( line_count, slab_item) in slab_data. into_iter ( ) . enumerate ( ) {
91+ if needs_header ( one_header, term_height, line_count as u64 ) {
92+ print_slab_header ( ) ;
93+ }
94+
95+ println ! (
96+ "{:<24} {:>6} {:>6} {:>6} {:>6}" ,
97+ slab_item. 0 , slab_item. 1 [ 0 ] , slab_item. 1 [ 1 ] , slab_item. 1 [ 2 ] , slab_item. 1 [ 3 ]
98+ ) ;
99+ }
100+
101+ Ok ( ( ) )
102+ }
103+
104+ #[ cfg( target_os = "linux" ) ]
105+ fn needs_header ( one_header : bool , term_height : u16 , line_count : u64 ) -> bool {
106+ !one_header && term_height > 0 && ( ( line_count + 3 ) % term_height as u64 == 0 )
107+ }
108+
109+ #[ cfg( target_os = "linux" ) ]
110+ fn print_slab_header ( ) {
111+ println ! (
112+ "{:<24} {:>6} {:>6} {:>6} {:>6}" ,
113+ "Cache" , "Num" , "Total" , "Size" , "Pages"
114+ ) ;
115+ }
116+
79117#[ cfg( target_os = "linux" ) ]
80118fn print_header ( pickers : & [ Picker ] ) {
81119 let mut section: Vec < & str > = vec ! [ ] ;
@@ -126,7 +164,7 @@ pub fn uu_app() -> Command {
126164 . value_parser ( value_parser ! ( u64 ) ) ,
127165 arg ! ( -a --active "Display active and inactive memory" ) ,
128166 // arg!(-f --forks "switch displays the number of forks since boot"),
129- // arg!(-m --slabs "Display slabinfo"),
167+ arg ! ( -m --slabs "Display slabinfo" ) ,
130168 arg ! ( -n --"one-header" "Display the header only once rather than periodically" ) ,
131169 // arg!(-s --stats "Displays a table of various event counters and memory statistics"),
132170 // arg!(-d --disk "Report disk statistics"),
0 commit comments