Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

system-monitor-service: support percentages from bigger numbers (backport #22591) #22597

Merged
merged 1 commit into from
Jan 20, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions core/src/system_monitor_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,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 @@ -273,4 +273,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);
}
}