Skip to content

Commit

Permalink
system-monitor-service: support percentages from bigger numbers (#22598)
Browse files Browse the repository at this point in the history
(cherry picked from commit cca3dbc)

Co-authored-by: Trent Nelson <trent@solana.com>
  • Loading branch information
mergify[bot] and t-nelson committed Jan 20, 2022
1 parent 66b94b8 commit e9e35fd
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions core/src/system_monitor_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,11 @@ impl SystemMonitorService {
);
}

fn calc_percent(numerator: u64, denom: u64) -> f32 {
fn calc_percent(numerator: u64, denom: u64) -> f64 {
if denom == 0 {
0.0
} else {
(numerator as f32 / denom as f32) * 100.0
(numerator as f64 / denom as f64) * 100.0
}
}

Expand Down Expand Up @@ -281,4 +281,11 @@ UdpLite: 0 0 0 0 0 0 0 0" as &[u8];
let stats = parse_udp_stats(&mut mock_snmp);
assert!(stats.is_err());
}

#[test]
fn test_calc_percent() {
assert!(SystemMonitorService::calc_percent(99, 100) < 100.0);
let one_tb_as_kb = (1u64 << 40) >> 10;
assert!(SystemMonitorService::calc_percent(one_tb_as_kb - 1, one_tb_as_kb) < 100.0);
}
}

0 comments on commit e9e35fd

Please sign in to comment.