You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by GitBox <gi...@apache.org> on 2019/01/11 02:41:42 UTC

[GitHub] wujimin commented on a change in pull request #1047: [SCB-1096]change the method calculate process cpu rate to same with top

wujimin commented on a change in pull request #1047: [SCB-1096]change the method calculate process cpu rate to same with top
URL: https://github.com/apache/servicecomb-java-chassis/pull/1047#discussion_r246988131
 
 

 ##########
 File path: metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/meter/os/cpu/OsCpuUsage.java
 ##########
 @@ -19,48 +19,52 @@
 import com.netflix.spectator.api.Id;
 
 /*
- * unit : 1 jiffies = 10ms = 0.01 s
+ * unit : 1 jiffies
  * more details :
  * http://man7.org/linux/man-pages/man5/proc.5.html
  * CMD :  /proc/stat
  * cpu  2445171 599297 353967 24490633 11242   0    10780    2993             0      0
  * cpu  user    nice   system idle     iowait  irq  softirq  stealstolen      guest  guest_nice
- * 0    1       2      3      4        5        6   7        8
+ * 0    1       2      3      4        5        6   7        8                9      10
  * total = user + nice + system + idle + iowait + irq + softirq + stealstolen
  * busy = total - idle
  */
 public class OsCpuUsage extends AbstractCpuUsage {
-  private long lastTotalTime;
 
-  private long currentTotalTime;
+  private Period total = new Period();
+
+  private Period idle = new Period();
 
   public OsCpuUsage(Id id) {
-    super(id, "/proc/stat");
+    super(id);
   }
 
-  @Override
-  protected void update(String[] stats) {
-    currentTotalTime = readCurrentTotalTime(stats);
-    periodTotalTime = currentTotalTime - lastTotalTime;
-    lastTotalTime = currentTotalTime;
-
-    super.update(stats);
+  public void update() {
+    String[] stats = CpuUtils.readAndSplitFirstLine(CpuUtils.PROC_STAT);
+    if (stats == null) {
+      return;
+    }
+    update(stats);
   }
 
-  private long readCurrentTotalTime(String[] stats) {
-    long total = 0L;
-    for (int i = 1; i <= 8; i++) {
-      total += Long.parseLong(stats[i]);
+  private void update(String[] stats) {
+
+    long currentIdle = Long.parseLong(stats[4]);
+    idle.update(currentIdle);
+
+    long totalCpu = 0L;
+    for (int i = 1; i < 9; i++) {
+      totalCpu += Long.parseLong(stats[i]);
     }
-    return total;
+    total.update(totalCpu);
+    updateUsage(total.period - idle.period, total.period);
   }
 
   @Override
-  protected long readCurrentBusyTime(String[] stats) {
-    return currentTotalTime - Long.parseLong(stats[4]);
-  }
-
-  public long getLastTotalTime() {
-    return lastTotalTime;
+  protected void updateUsage(double periodBusy, double periodTotal) {
 
 Review comment:
   move to parent class
   ```
     protected void updateUsage(double periodBusy, double periodTotal, boolean irixMode) {
       usage = periodTotal == 0 ? 0 : periodBusy / periodTotal;
       if (usage > 1) {
         usage = 1;
       }
       if (irixMode) {
         usage *= cpuCount;
       }
     }
   ```

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services