@@ -7,7 +7,7 @@ mod parser;
77mod picker;
88
99#[ cfg( target_os = "linux" ) ]
10- use crate :: picker:: { get_pickers, get_stats, Picker } ;
10+ use crate :: picker:: { get_disk_sum , get_pickers, get_stats, Picker } ;
1111use clap:: value_parser;
1212#[ allow( unused_imports) ]
1313use clap:: { arg, crate_version, ArgMatches , Command } ;
@@ -26,22 +26,31 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
2626 let matches = uu_app ( ) . try_get_matches_from ( args) ?;
2727 #[ cfg( target_os = "linux" ) ]
2828 {
29- if matches. get_flag ( "forks" ) {
30- return print_forks ( ) ;
31- }
32- if matches. get_flag ( "stats" ) {
33- return print_stats ( ) ;
34- }
35-
29+ let wide = matches. get_flag ( "wide" ) ;
3630 let one_header = matches. get_flag ( "one-header" ) ;
3731 let no_first = matches. get_flag ( "no-first" ) ;
3832 let term_height = terminal_size:: terminal_size ( )
3933 . map ( |size| size. 1 . 0 )
4034 . unwrap_or ( 0 ) ;
4135
36+ if matches. get_flag ( "forks" ) {
37+ return print_forks ( ) ;
38+ }
4239 if matches. get_flag ( "slabs" ) {
4340 return print_slabs ( one_header, term_height) ;
4441 }
42+ if matches. get_flag ( "stats" ) {
43+ return print_stats ( ) ;
44+ }
45+ if matches. get_flag ( "disk" ) {
46+ return print_disk ( wide, one_header, term_height) ;
47+ }
48+ if matches. get_flag ( "disk-sum" ) {
49+ return print_disk_sum ( ) ;
50+ }
51+ if let Some ( device) = matches. get_one :: < String > ( "partition" ) {
52+ return print_partition ( device) ;
53+ }
4554
4655 // validate unit
4756 if let Some ( unit) = matches. get_one :: < String > ( "unit" ) {
@@ -141,6 +150,110 @@ fn print_slab_header() {
141150 ) ;
142151}
143152
153+ #[ cfg( target_os = "linux" ) ]
154+ fn print_disk_header ( wide : bool ) {
155+ if wide {
156+ println ! ( "disk- -------------------reads------------------- -------------------writes------------------ ------IO-------" ) ;
157+ println ! (
158+ "{:>15} {:>9} {:>11} {:>11} {:>9} {:>9} {:>11} {:>11} {:>7} {:>7}" ,
159+ "total" , "merged" , "sectors" , "ms" , "total" , "merged" , "sectors" , "ms" , "cur" , "sec"
160+ ) ;
161+ } else {
162+ println ! ( "disk- ------------reads------------ ------------writes----------- -----IO------" ) ;
163+ println ! (
164+ "{:>12} {:>6} {:>7} {:>7} {:>6} {:>6} {:>7} {:>7} {:>6} {:>6}" ,
165+ "total" , "merged" , "sectors" , "ms" , "total" , "merged" , "sectors" , "ms" , "cur" , "sec"
166+ ) ;
167+ }
168+ }
169+
170+ #[ cfg( target_os = "linux" ) ]
171+ fn print_disk ( wide : bool , one_header : bool , term_height : u16 ) -> UResult < ( ) > {
172+ let disk_data = DiskStat :: current ( )
173+ . map_err ( |_| USimpleError :: new ( 1 , "Unable to retrieve disk statistics" ) ) ?;
174+
175+ let mut line_count = 0 ;
176+
177+ print_disk_header ( wide) ;
178+
179+ for disk in disk_data {
180+ if !disk. is_disk ( ) {
181+ continue ;
182+ }
183+
184+ if needs_header ( one_header, term_height, line_count) {
185+ print_disk_header ( wide) ;
186+ }
187+ line_count += 1 ;
188+
189+ if wide {
190+ println ! (
191+ "{:<5} {:>9} {:>9} {:>11} {:>11} {:>9} {:>9} {:>11} {:>11} {:>7} {:>7}" ,
192+ disk. device,
193+ disk. reads_completed,
194+ disk. reads_merged,
195+ disk. sectors_read,
196+ disk. milliseconds_spent_reading,
197+ disk. writes_completed,
198+ disk. writes_merged,
199+ disk. sectors_written,
200+ disk. milliseconds_spent_writing,
201+ disk. ios_currently_in_progress / 1000 ,
202+ disk. milliseconds_spent_doing_ios / 1000
203+ ) ;
204+ } else {
205+ println ! (
206+ "{:<5} {:>6} {:>6} {:>7} {:>7} {:>6} {:>6} {:>7} {:>7} {:>6} {:>6}" ,
207+ disk. device,
208+ disk. reads_completed,
209+ disk. reads_merged,
210+ disk. sectors_read,
211+ disk. milliseconds_spent_reading,
212+ disk. writes_completed,
213+ disk. writes_merged,
214+ disk. sectors_written,
215+ disk. milliseconds_spent_writing,
216+ disk. ios_currently_in_progress / 1000 ,
217+ disk. milliseconds_spent_doing_ios / 1000
218+ ) ;
219+ }
220+ }
221+
222+ Ok ( ( ) )
223+ }
224+
225+ #[ cfg( target_os = "linux" ) ]
226+ fn print_disk_sum ( ) -> UResult < ( ) > {
227+ let data = get_disk_sum ( ) ?;
228+
229+ data. iter ( )
230+ . for_each ( |( name, value) | println ! ( "{value:>13} {name}" ) ) ;
231+
232+ Ok ( ( ) )
233+ }
234+
235+ #[ cfg( target_os = "linux" ) ]
236+ fn print_partition ( device : & str ) -> UResult < ( ) > {
237+ let disk_data = DiskStat :: current ( )
238+ . map_err ( |_| USimpleError :: new ( 1 , "Unable to retrieve disk statistics" ) ) ?;
239+
240+ let disk = disk_data
241+ . iter ( )
242+ . find ( |disk| disk. device == device)
243+ . ok_or_else ( || USimpleError :: new ( 1 , format ! ( "Disk/Partition {device} not found" ) ) ) ?;
244+
245+ println ! (
246+ "{device:<9} {:>11} {:>17} {:>11} {:>17}" ,
247+ "reads" , "read sectors" , "writes" , "requested writes"
248+ ) ;
249+ println ! (
250+ "{:>21} {:>17} {:>11} {:>17}" ,
251+ disk. reads_completed, disk. sectors_read, disk. writes_completed, disk. sectors_written
252+ ) ;
253+
254+ Ok ( ( ) )
255+ }
256+
144257#[ cfg( target_os = "linux" ) ]
145258fn print_header ( pickers : & [ Picker ] ) {
146259 let mut section: Vec < & str > = vec ! [ ] ;
@@ -191,15 +304,18 @@ pub fn uu_app() -> Command {
191304 . value_parser ( value_parser ! ( u64 ) ) ,
192305 arg ! ( -a --active "Display active and inactive memory" ) ,
193306 arg ! ( -f --forks "switch displays the number of forks since boot" )
194- . conflicts_with_all ( [ "slabs" , "stats" , /* "disk", "disk-sum", "partition"*/ ] ) ,
307+ . conflicts_with_all ( [ "slabs" , "stats" , "disk" , "disk-sum" , "partition" ] ) ,
195308 arg ! ( -m --slabs "Display slabinfo" )
196- . conflicts_with_all ( [ "forks" , "stats" , /* "disk", "disk-sum", "partition"*/ ] ) ,
309+ . conflicts_with_all ( [ "forks" , "stats" , "disk" , "disk-sum" , "partition" ] ) ,
197310 arg ! ( -n --"one-header" "Display the header only once rather than periodically" ) ,
198311 arg ! ( -s --stats "Displays a table of various event counters and memory statistics" )
199- . conflicts_with_all ( [ "forks" , "slabs" , /*"disk", "disk-sum", "partition"*/ ] ) ,
200- // arg!(-d --disk "Report disk statistics"),
201- // arg!(-D --"disk-sum" "Report some summary statistics about disk activity"),
202- // arg!(-p --partition <device> "Detailed statistics about partition"),
312+ . conflicts_with_all ( [ "forks" , "slabs" , "disk" , "disk-sum" , "partition" ] ) ,
313+ arg ! ( -d --disk "Report disk statistics" )
314+ . conflicts_with_all ( [ "forks" , "slabs" , "stats" , "disk-sum" , "partition" ] ) ,
315+ arg ! ( -D --"disk-sum" "Report some summary statistics about disk activity" )
316+ . conflicts_with_all ( [ "forks" , "slabs" , "stats" , "disk" , "partition" ] ) ,
317+ arg ! ( -p --partition <device> "Detailed statistics about partition" )
318+ . conflicts_with_all ( [ "forks" , "slabs" , "stats" , "disk" , "disk-sum" ] ) ,
203319 arg ! ( -S --unit <character> "Switches outputs between 1000 (k), 1024 (K), 1000000 (m), or 1048576 (M) bytes" ) ,
204320 arg ! ( -t --timestamp "Append timestamp to each line" ) ,
205321 arg ! ( -w --wide "Wide output mode" ) ,
0 commit comments