@@ -7,38 +7,6 @@ use crate::picker::{sysinfo, systemstat};
7
7
use bytesize:: ByteSize ;
8
8
use clap:: ArgMatches ;
9
9
use systemstat:: Platform ;
10
- #[ cfg( not( any( target_os = "macos" , target_os = "linux" ) ) ) ]
11
- use {
12
- std:: sync:: { Mutex , OnceLock } ,
13
- systemstat:: { CPULoad , DelayedMeasurement } ,
14
- } ;
15
-
16
- #[ cfg( not( any( target_os = "macos" , target_os = "linux" ) ) ) ]
17
- static LOAD_AVERAGE : OnceLock < Mutex < DelayedMeasurement < CPULoad > > > = OnceLock :: new ( ) ;
18
-
19
- #[ cfg( not( any( target_os = "macos" , target_os = "linux" ) ) ) ]
20
- pub ( crate ) fn cpu_load ( ) -> CPULoad {
21
- match LOAD_AVERAGE . get ( ) {
22
- None => {
23
- LOAD_AVERAGE . get_or_init ( || {
24
- Mutex :: new ( systemstat ( ) . read ( ) . unwrap ( ) . cpu_load_aggregate ( ) . unwrap ( ) )
25
- } ) ;
26
- systemstat ( )
27
- . read ( )
28
- . unwrap ( )
29
- . cpu_load_aggregate ( )
30
- . unwrap ( )
31
- . done ( )
32
- . unwrap ( )
33
- }
34
- Some ( v) => {
35
- let mut v = v. lock ( ) . unwrap ( ) ;
36
- let load = v. done ( ) . unwrap ( ) ;
37
- * v = systemstat ( ) . read ( ) . unwrap ( ) . cpu_load_aggregate ( ) . unwrap ( ) ;
38
- load
39
- }
40
- }
41
- }
42
10
43
11
pub ( crate ) fn header ( arg : & ArgMatches ) -> String {
44
12
format ! (
@@ -296,16 +264,64 @@ fn cpu() -> String {
296
264
)
297
265
}
298
266
299
- //TODO: Implement io wait, hardware interrupt, software interrupt and steal time
300
- #[ cfg( not( any( target_os = "macos" , target_os = "linux" ) ) ) ]
267
+ #[ cfg( target_os = "windows" ) ]
301
268
fn cpu ( ) -> String {
302
- let cpu = cpu_load ( ) ;
269
+ use libc:: malloc;
270
+ use windows:: Wdk :: System :: SystemInformation :: NtQuerySystemInformation ;
271
+
272
+ #[ repr( C ) ]
273
+ #[ derive( Debug ) ]
274
+ struct SystemProcessorPerformanceInformation {
275
+ idle_time : i64 , // LARGE_INTEGER
276
+ kernel_time : i64 , // LARGE_INTEGER
277
+ user_time : i64 , // LARGE_INTEGER
278
+ dpc_time : i64 , // LARGE_INTEGER
279
+ interrupt_time : i64 , // LARGE_INTEGER
280
+ interrupt_count : u32 , // ULONG
281
+ }
282
+
283
+ let n_cpu = sysinfo ( ) . read ( ) . unwrap ( ) . cpus ( ) . len ( ) ;
284
+ let mut cpu_load = SystemProcessorPerformanceInformation {
285
+ idle_time : 0 ,
286
+ kernel_time : 0 ,
287
+ user_time : 0 ,
288
+ dpc_time : 0 ,
289
+ interrupt_time : 0 ,
290
+ interrupt_count : 0 ,
291
+ } ;
292
+ unsafe {
293
+ let len = n_cpu * size_of :: < SystemProcessorPerformanceInformation > ( ) ;
294
+ let data = malloc ( len) ;
295
+ let _ = NtQuerySystemInformation (
296
+ windows:: Wdk :: System :: SystemInformation :: SystemProcessorPerformanceInformation ,
297
+ data,
298
+ ( n_cpu * size_of :: < SystemProcessorPerformanceInformation > ( ) ) as u32 ,
299
+ std:: ptr:: null_mut ( ) ,
300
+ ) ;
301
+ for i in 0 ..n_cpu {
302
+ let cpu = data. add ( i * size_of :: < SystemProcessorPerformanceInformation > ( ) )
303
+ as * const SystemProcessorPerformanceInformation ;
304
+ let cpu = cpu. as_ref ( ) . unwrap ( ) ;
305
+ cpu_load. idle_time += cpu. idle_time ;
306
+ cpu_load. kernel_time += cpu. kernel_time ;
307
+ cpu_load. user_time += cpu. user_time ;
308
+ cpu_load. dpc_time += cpu. dpc_time ;
309
+ cpu_load. interrupt_time += cpu. interrupt_time ;
310
+ cpu_load. interrupt_count += cpu. interrupt_count ;
311
+ }
312
+ }
313
+ let total = cpu_load. idle_time
314
+ + cpu_load. kernel_time
315
+ + cpu_load. user_time
316
+ + cpu_load. dpc_time
317
+ + cpu_load. interrupt_time ;
303
318
format ! (
304
- "%Cpu(s): {:.1} us, {:.1} sy, {:.1} ni, {:.1} id" ,
305
- cpu. user * 100.0 ,
306
- cpu. system * 100.0 ,
307
- cpu. nice * 100.0 ,
308
- cpu. idle * 100.0
319
+ "%Cpu(s): {:.1} us, {:.1} sy, {:.1} id, {:.1} hi, {:.1} si" ,
320
+ cpu_load. user_time as f64 / total as f64 * 100.0 ,
321
+ cpu_load. kernel_time as f64 / total as f64 * 100.0 ,
322
+ cpu_load. idle_time as f64 / total as f64 * 100.0 ,
323
+ cpu_load. interrupt_time as f64 / total as f64 * 100.0 ,
324
+ cpu_load. dpc_time as f64 / total as f64 * 100.0 ,
309
325
)
310
326
}
311
327
0 commit comments