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



##########
File path: 
pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/impl/LinuxBrokerHostUsageImpl.java
##########
@@ -73,6 +80,16 @@ public LinuxBrokerHostUsageImpl(int 
hostUsageCheckIntervalMin,
         this.overrideBrokerNicSpeedGbps = overrideBrokerNicSpeedGbps;
         executorService.scheduleAtFixedRate(this::calculateBrokerHostUsage, 0,
                 hostUsageCheckIntervalMin, TimeUnit.MINUTES);
+
+        boolean isCGroupsEnabled = false;
+        try {
+             isCGroupsEnabled = 
Files.exists(Paths.get(CGROUPS_CPU_USAGE_PATH));
+        } catch (Exception e) {
+            log.warn("Failed to check cgroup CPU usage file", e.getMessage());

Review comment:
       `log.warn("Failed to check cgroup CPU usage file {}", e.getMessage());`

##########
File path: 
pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/impl/LinuxBrokerHostUsageImpl.java
##########
@@ -137,18 +167,36 @@ private double getTotalCpuLimit() {
      * Line is split in "words", filtering the first. The sum of all numbers 
give the amount of cpu cycles used this
      * far. Real CPU usage should equal the sum substracting the idle cycles, 
this would include iowait, irq and steal.
      */
-    private CpuStat getTotalCpuUsage() {
+    private double getTotalCpuUsageForEntireHost() {
         try (Stream<String> stream = Files.lines(Paths.get("/proc/stat"))) {
             String[] words = stream.findFirst().get().split("\\s+");
 
             long total = Arrays.stream(words).filter(s -> 
!s.contains("cpu")).mapToLong(Long::parseLong).sum();
-
             long idle = Long.parseLong(words[4]);
+            long usage = total - idle;
+
+            double currentUsage = (usage - lastCpuUsage)  / (total - 
lastCpuTotalTime) * getTotalCpuLimit();
+
+            lastCpuUsage = usage;
+            lastCpuTotalTime = total;
 
-            return new CpuStat(total, total - idle);
+            return currentUsage;
         } catch (IOException e) {
-            LOG.error("Failed to read CPU usage from /proc/stat", e);
-            return null;
+            log.error("Failed to read CPU usage from /proc/stat", e);
+            return -1;
+        }
+    }
+
+    private double getTotalCpuUsageForCGroup(double elapsedTimeSeconds) {
+        try {
+            long usage = readLongFromFile(CGROUPS_CPU_USAGE_PATH);
+            double currentUsage = usage - lastCpuUsage;
+            lastCpuUsage = usage;
+
+            return 100 * currentUsage / elapsedTimeSeconds / 
TimeUnit.SECONDS.toNanos(1);
+        } catch (IOException e) {
+            log.error("Failed to read CPU usage from " + 
CGROUPS_CPU_USAGE_PATH, e);

Review comment:
       `log.error("Failed to read CPU usage from {}", CGROUPS_CPU_USAGE_PATH, 
e);`




----------------------------------------------------------------
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:
[email protected]


Reply via email to