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


##########
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:
   updated



-- 
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-unsubscr...@storm.apache.org

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

Reply via email to