Skip to content

Commit

Permalink
Handle addition overflow when calculating CPU interrupts and DPC coun…
Browse files Browse the repository at this point in the history
…t stats for Windows (closes #250)
  • Loading branch information
svartalf committed Jun 22, 2020
1 parent 3636b71 commit a4b0089
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -38,6 +38,7 @@ for information about previous releases.
### Fixed

* `cpu::times` for Linux correctly parses `/proc/stat` (#233)
* Handle addition overflow when calculating CPU interrupts stats for Windows (#250)

## Older versions

Expand Down
21 changes: 15 additions & 6 deletions heim-cpu/src/sys/windows/stats.rs
Expand Up @@ -44,20 +44,29 @@ fn system_performance_info() -> Result<(u64, u64)> {
fn dpc_count() -> Result<u64> {
let info: Vec<winternl::SYSTEM_INTERRUPT_INFORMATION> = winternl::query_system_information()?;

let count = info.into_iter().fold(0, |acc, item| acc + item.DpcCount);
let count = info.into_iter()
.fold(0u64, |acc, item| {
// TODO: Log the overflow (`info` level?)
acc.overflowing_add(item.DpcCount.into()).0
});

Ok(count.into())
Ok(count)
}

fn interrupts() -> Result<u64> {
let info: Vec<winternl::SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION> =
winternl::query_system_information()?;

let count = info
.into_iter()
.fold(0, |acc, item| acc + item.InterruptCount);
let count = info.into_iter().fold(0u64, |acc, item| {
// `InterruptCount` type is `u32` (`ULONG`) and working with `u32`
// in here can overflow really quick (see #250).
// `u64` will not overflow that fast, but we still want
// to handle that case.
// TODO: Log the overflow (`info` level?)
acc.overflowing_add(item.InterruptCount.into()).0
});

Ok(count.into())
Ok(count)
}

pub async fn stats() -> Result<CpuStats> {
Expand Down

0 comments on commit a4b0089

Please sign in to comment.