Przeglądaj źródła

Merge pull request #12175 from ghalliday/issue21485

HPCC-21485 Fix potential divide by zero in new CpuInfo::getPercentCpu()

Reviewed-By: Jake Smith <jake.smith@lexisnexis.com>
Reviewed-By: Richard Chapman <rchapman@hpccsystems.com>
Richard Chapman 6 lat temu
rodzic
commit
65e16a72c1
1 zmienionych plików z 17 dodań i 5 usunięć
  1. 17 5
      system/jlib/jdebug.cpp

+ 17 - 5
system/jlib/jdebug.cpp

@@ -896,7 +896,7 @@ unsigned CpuInfo::getPercentCpu() const
     __uint64 total = getTotal();
     if (total == 0)
         return 0;
-    unsigned percent = (unsigned)(((total - idle) * 100) / idle);
+    unsigned percent = (unsigned)(((total - idle) * 100) / total);
     if (percent > 100)
         percent = 100;
     return percent;
@@ -919,22 +919,34 @@ __uint64 CpuInfo::getTotalNs() const
 
 unsigned CpuInfo::getIdlePercent() const
 {
-    return (unsigned)((idle * 100) / getTotal());
+    __uint64 total = getTotal();
+    if (total == 0)
+        return 0;
+    return (unsigned)((idle * 100) / total);
 }
 
 unsigned CpuInfo::getIoWaitPercent() const
 {
-    return (unsigned)((iowait * 100) / getTotal());
+    __uint64 total = getTotal();
+    if (total == 0)
+        return 0;
+    return (unsigned)((iowait * 100) / total);
 }
 
 unsigned CpuInfo::getSystemPercent() const
 {
-    return (unsigned)((system * 100) / getTotal());
+    __uint64 total = getTotal();
+    if (total == 0)
+        return 0;
+    return (unsigned)((system * 100) / total);
 }
 
 unsigned CpuInfo::getUserPercent() const
 {
-    return (unsigned)((user * 100) / getTotal());
+    __uint64 total = getTotal();
+    if (total == 0)
+        return 0;
+    return (unsigned)((user * 100) / total);
 }
 
 //===========================================================================