You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@storm.apache.org by "rzo1 (via GitHub)" <gi...@apache.org> on 2024/04/15 19:52:36 UTC

Re: [PR] STORM-4054 -Add Worker CPU Metric (storm)

rzo1 commented on code in PR #3639:
URL: https://github.com/apache/storm/pull/3639#discussion_r1566353967


##########
storm-client/src/jvm/org/apache/storm/metric/SystemBolt.java:
##########
@@ -67,12 +68,56 @@ public Long getValue() {
 
 
         context.registerGauge("newWorkerEvent", new NewWorkerGauge());
+        context.registerGauge("workerCpuUsage", new WorkerCpuMetric());
 
         int bucketSize = ObjectReader.getInt(topoConf.get(Config.TOPOLOGY_BUILTIN_METRICS_BUCKET_SIZE_SECS));
         registerMetrics(context, (Map<String, String>) topoConf.get(Config.WORKER_METRICS), bucketSize, topoConf);
         registerMetrics(context, (Map<String, String>) topoConf.get(Config.TOPOLOGY_WORKER_METRICS), bucketSize, topoConf);
     }
 
+    private class WorkerCpuMetric implements Gauge<Double> {
+        private long lastCalculationTimeNsec;
+        private long previousCpuTotal;
+        private double cpuUsage;
+
+        WorkerCpuMetric() {
+            lastCalculationTimeNsec = System.nanoTime();
+            previousCpuTotal = getTotalCpuUsage();
+            cpuUsage = 0.0d;
+        }
+
+        private long getTotalCpuUsage() {
+            long totalCpuNsecs = 0L;
+            ThreadMXBean threadMxBean = ManagementFactory.getThreadMXBean();
+            for (Long threadId : threadMxBean.getAllThreadIds()) {
+                long threadCpu = threadMxBean.getThreadCpuTime(threadId);
+                if (threadCpu > 0L) {
+                    totalCpuNsecs += threadCpu;
+                }
+            }
+            return totalCpuNsecs;
+        }
+
+        private void updateCalculation() {
+            // we could have multiple reporters calling getValue() one right
+            // after another, with inaccurate reporting due to the small time difference.
+            long elapsed = System.nanoTime() - this.lastCalculationTimeNsec;
+            if (elapsed >= 1000000000L) {

Review Comment:
   Maybe introduce a constant here? I think, that a constant instead of this magic number  would be more readable here. Something like `private static final long THRESHOLD = TimeUnit.NANOSECONDS.convert(1, TimeUnit.SECONDS);` , wdyt?



-- 
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: dev-unsubscribe@storm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org