You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by GitBox <gi...@apache.org> on 2020/07/08 11:27:16 UTC

[GitHub] [pulsar] ivankelly commented on a change in pull request #7475: Use CGroup CPU usage when present

ivankelly commented on a change in pull request #7475:
URL: https://github.com/apache/pulsar/pull/7475#discussion_r451470550



##########
File path: pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/impl/LinuxBrokerHostUsageImpl.java
##########
@@ -87,45 +100,36 @@ public void calculateBrokerHostUsage() {
         double totalNicUsageTx = getTotalNicUsageTxKb(nics);
         double totalNicUsageRx = getTotalNicUsageRxKb(nics);
         double totalCpuLimit = getTotalCpuLimit();
-        CpuStat cpuStat = getTotalCpuUsage();
 
         SystemResourceUsage usage = new SystemResourceUsage();
         long now = System.currentTimeMillis();
+        double elapsedSeconds = (now - lastCollection) / 1000d;
+        double cpuUsage = getTotalCpuUsage(elapsedSeconds);
 
         if (lastCollection == 0L) {
             usage.setMemory(getMemUsage());
             usage.setBandwidthIn(new ResourceUsage(0d, totalNicLimit));
             usage.setBandwidthOut(new ResourceUsage(0d, totalNicLimit));
-            usage.setCpu(new ResourceUsage(0d, totalCpuLimit));
         } else {
-            double elapsedSeconds = (now - lastCollection) / 1000d;
             double nicUsageTx = (totalNicUsageTx - lastTotalNicUsageTx) / elapsedSeconds;
             double nicUsageRx = (totalNicUsageRx - lastTotalNicUsageRx) / elapsedSeconds;
-
-            if (cpuStat != null && lastCpuStat != null) {
-                // we need two non null stats to get a usage report
-                long cpuTimeDiff = cpuStat.getTotalTime() - lastCpuStat.getTotalTime();
-                long cpuUsageDiff = cpuStat.getUsage() - lastCpuStat.getUsage();
-                double cpuUsage = ((double) cpuUsageDiff / (double) cpuTimeDiff) * totalCpuLimit;
-                usage.setCpu(new ResourceUsage(cpuUsage, totalCpuLimit));
-            }
-
-            usage.setMemory(getMemUsage());
-            usage.setBandwidthIn(new ResourceUsage(nicUsageRx, totalNicLimit));
-            usage.setBandwidthOut(new ResourceUsage(nicUsageTx, totalNicLimit));
         }
 
-        lastTotalNicUsageTx = totalNicUsageTx;
-        lastTotalNicUsageRx = totalNicUsageRx;
-        lastCpuStat = cpuStat;
-        lastCollection = System.currentTimeMillis();
-        this.usage = usage;
+        usage.setCpu(new ResourceUsage(cpuUsage, totalCpuLimit));
     }
 
     private double getTotalCpuLimit() {
         return 100 * Runtime.getRuntime().availableProcessors();

Review comment:
       If we take usage from cgroup, we should take limit from cgroup also.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to 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