sahildevgon commented on code in PR #60777:
URL: https://github.com/apache/doris/pull/60777#discussion_r2905852877


##########
fe/fe-core/src/main/java/org/apache/doris/metric/SystemMetrics.java:
##########
@@ -145,4 +204,139 @@ private void updateMemoryMetrics() {
         }
     }
 
+    private void updateCpuMetrics() {
+        String procFile = "/proc/stat";
+        if (FeConstants.runningUnitTest) {
+            procFile = 
getClass().getClassLoader().getResource("data/stat_normal").getFile();
+        }
+
+        try (FileReader fileReader = new FileReader(procFile);
+                BufferedReader br = new BufferedReader(fileReader)) {
+            String line;
+            boolean cpuLineFound = false;
+
+            // Store previous values for delta calculation
+            long prevTotal = prevCpuUser + prevCpuNice + prevCpuSystem + 
prevCpuIdle
+                    + prevCpuIowait + prevCpuIrq + prevCpuSoftirq + 
prevCpuSteal;
+
+            while ((line = br.readLine()) != null) {
+                if (line.startsWith("cpu ")) {  // Overall CPU stats (not 
per-core)
+                    cpuLineFound = true;
+                    String[] parts = line.split("\\s+");
+                    if (parts.length >= 11) {
+                        // Full format with guest/guest_nice (kernel >= 2.6.24 
with guest, >= 2.6.33 with guest_nice)
+                        cpuUser = Long.parseLong(parts[1]);
+                        cpuNice = Long.parseLong(parts[2]);
+                        cpuSystem = Long.parseLong(parts[3]);
+                        cpuIdle = Long.parseLong(parts[4]);
+                        cpuIowait = Long.parseLong(parts[5]);
+                        cpuIrq = Long.parseLong(parts[6]);
+                        cpuSoftirq = Long.parseLong(parts[7]);
+                        cpuSteal = Long.parseLong(parts[8]);
+                        cpuGuest = Long.parseLong(parts[9]);
+                        cpuGuestNice = Long.parseLong(parts[10]);
+                    } else if (parts.length >= 9) {
+                        // Format with steal but without guest/guest_nice 
(kernel >= 2.6.11)
+                        cpuUser = Long.parseLong(parts[1]);
+                        cpuNice = Long.parseLong(parts[2]);
+                        cpuSystem = Long.parseLong(parts[3]);
+                        cpuIdle = Long.parseLong(parts[4]);
+                        cpuIowait = Long.parseLong(parts[5]);
+                        cpuIrq = Long.parseLong(parts[6]);
+                        cpuSoftirq = Long.parseLong(parts[7]);
+                        cpuSteal = Long.parseLong(parts[8]);
+                        cpuGuest = 0;
+                        cpuGuestNice = 0;
+                    } else if (parts.length >= 8) {
+                        // Older format without steal (kernel < 2.6.11)
+                        cpuUser = Long.parseLong(parts[1]);
+                        cpuNice = Long.parseLong(parts[2]);
+                        cpuSystem = Long.parseLong(parts[3]);
+                        cpuIdle = Long.parseLong(parts[4]);
+                        cpuIowait = Long.parseLong(parts[5]);
+                        cpuIrq = Long.parseLong(parts[6]);
+                        cpuSoftirq = Long.parseLong(parts[7]);
+                        cpuSteal = 0;
+                        cpuGuest = 0;
+                        cpuGuestNice = 0;
+                    }
+                } else if (line.startsWith("ctxt ")) {
+                    String[] parts = line.split("\\s+");
+                    if (parts.length >= 2) {
+                        ctxt = Long.parseLong(parts[1]);
+                    }
+                } else if (line.startsWith("procs_running ")) {
+                    String[] parts = line.split("\\s+");
+                    if (parts.length >= 2) {
+                        procsRunning = Long.parseLong(parts[1]);
+                    }
+                } else if (line.startsWith("procs_blocked ")) {
+                    String[] parts = line.split("\\s+");
+                    if (parts.length >= 2) {
+                        procsBlocked = Long.parseLong(parts[1]);
+                    }
+                }
+            }
+
+            // Validate that CPU line was found
+            if (!cpuLineFound) {
+                LOG.warn("failed to get /proc/stat: cpu line not found");

Review Comment:
   Fixed , added the block



-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


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

Reply via email to