From 0c52d5bb344462452c53c36449231b89a9d900b1 Mon Sep 17 00:00:00 2001 From: LFC <990479+MichaelScofield@users.noreply.github.com> Date: Mon, 15 Dec 2025 17:40:49 +0800 Subject: [PATCH] fix: cpu cores got wrongly calculated to 0 (#7405) * fix: cpu cores got wrongly calculated to 0 Signed-off-by: luofucong * Update src/common/stat/src/resource.rs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Signed-off-by: luofucong Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/common/stat/src/resource.rs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/common/stat/src/resource.rs b/src/common/stat/src/resource.rs index babfa54a19..7894ccfda3 100644 --- a/src/common/stat/src/resource.rs +++ b/src/common/stat/src/resource.rs @@ -58,10 +58,14 @@ pub fn get_total_memory_bytes() -> i64 { } } -/// Get the total CPU cores. The result will be rounded to the nearest integer. -/// For example, if the total CPU is 1.5 cores(1500 millicores), the result will be 2. +/// Get the total CPU cores. The result will be rounded up to the next integer (ceiling). +/// For example, if the total CPU is 1.1 cores (1100 millicores) or 1.5 cores (1500 millicores), the result will be 2. pub fn get_total_cpu_cores() -> usize { - ((get_total_cpu_millicores() as f64) / 1000.0).round() as usize + cpu_cores(get_total_cpu_millicores()) +} + +fn cpu_cores(cpu_millicores: i64) -> usize { + ((cpu_millicores as f64) / 1_000.0).ceil() as usize } /// Get the total memory in readable size. @@ -178,6 +182,13 @@ mod tests { #[test] fn test_get_total_cpu_cores() { assert!(get_total_cpu_cores() > 0); + assert_eq!(cpu_cores(1), 1); + assert_eq!(cpu_cores(100), 1); + assert_eq!(cpu_cores(500), 1); + assert_eq!(cpu_cores(1000), 1); + assert_eq!(cpu_cores(1100), 2); + assert_eq!(cpu_cores(1900), 2); + assert_eq!(cpu_cores(10_000), 10); } #[test]