This is an automated email from the ASF dual-hosted git repository.

coolfrog pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-horaedb.git


The following commit(s) were added to refs/heads/main by this push:
     new 07dc2011 refactor: adjust cpu's stats (#1457)
07dc2011 is described below

commit 07dc2011b8ed2d9e15b25293fdf02e792e17ebc4
Author: WEI Xikai <[email protected]>
AuthorDate: Mon Jan 22 20:08:05 2024 +0800

    refactor: adjust cpu's stats (#1457)
    
    ## Rationale
    Only the total cpu usage is implicit if the numbers of cpus are
    different across the cluster nodes.
    
    ## Detailed Changes
    Introduce the `num_cpus` in the system stats, and adjust the average
    cpu_usage to the sum of all the cpus' usage, whose range is `[0.0,
    num_cpus]`.
    
    ## Test Plan
    Add a new check to ensure the `cpu_usage`'s range to to be `[0.0,
    num_cpus]`.
---
 src/components/system_stats/src/lib.rs | 16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)

diff --git a/src/components/system_stats/src/lib.rs 
b/src/components/system_stats/src/lib.rs
index ff8344a4..00892efd 100644
--- a/src/components/system_stats/src/lib.rs
+++ b/src/components/system_stats/src/lib.rs
@@ -25,8 +25,10 @@ use sysinfo::{Cpu, CpuRefreshKind, MemoryRefreshKind, 
RefreshKind, System};
 /// The stats about the system.
 #[derive(Debug)]
 pub struct SystemStats {
-    /// The usage's range is [0, 1.0]
+    /// The valid range is [0.0, total_cpu].
     pub cpu_usage: f32,
+    /// The total cpu
+    pub num_cpus: u32,
     /// The memory is counted in byte.
     pub used_memory: u64,
     /// The memory is counted in byte.
@@ -76,6 +78,7 @@ impl SystemStatsCollector {
 
         SystemStats {
             cpu_usage: self.compute_cpu_usage(system.cpus()),
+            num_cpus: system.cpus().len() as u32,
             used_memory: system.used_memory(),
             total_memory: self.total_memory,
             load_avg: System::load_average(),
@@ -84,19 +87,13 @@ impl SystemStatsCollector {
 
     // Refresh and compute the latest cpu usage.
     fn compute_cpu_usage(&self, cpus: &[Cpu]) -> f32 {
-        let mut num_cpus = 0;
         let mut total_cpu_usage = 0.0;
         let valid_cpus = cpus.iter().filter(|v| !v.cpu_usage().is_nan());
         for cpu in valid_cpus {
             total_cpu_usage += cpu.cpu_usage();
-            num_cpus += 1;
         }
 
-        if num_cpus != 0 {
-            total_cpu_usage / (num_cpus as f32) / 100.0
-        } else {
-            0f32
-        }
+        total_cpu_usage / 100.0
     }
 
     #[inline]
@@ -130,13 +127,14 @@ mod tests {
         assert!(stats.used_memory > 0);
         assert!(stats.used_memory < stats.total_memory);
         assert!(stats.cpu_usage >= 0.0);
+        assert!(stats.cpu_usage <= stats.num_cpus as f32);
         assert!(stats.load_avg.one >= 0.0);
         assert!(stats.load_avg.five >= 0.0);
         assert!(stats.load_avg.fifteen >= 0.0);
     }
 
     #[tokio::test]
-    async fn test_normal_case() {
+    async fn test_collect_system_stats() {
         let collector = SystemStatsCollector::try_new().unwrap();
         let stats = collector
             .collect_and_report(Duration::from_millis(500))


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to