diff --git a/src/main/java/org/opensearch/performanceanalyzer/transport/RTFPerformanceAnalyzerTransportChannel.java b/src/main/java/org/opensearch/performanceanalyzer/transport/RTFPerformanceAnalyzerTransportChannel.java index d1b9b04d..c6994eb4 100644 --- a/src/main/java/org/opensearch/performanceanalyzer/transport/RTFPerformanceAnalyzerTransportChannel.java +++ b/src/main/java/org/opensearch/performanceanalyzer/transport/RTFPerformanceAnalyzerTransportChannel.java @@ -42,6 +42,8 @@ public final class RTFPerformanceAnalyzerTransportChannel implements TransportCh private ShardId shardId; private boolean primary; + private long threadID; + void set( TransportChannel original, Histogram cpuUtilizationHistogram, @@ -55,7 +57,9 @@ void set( this.primary = bPrimary; this.operationStartTime = System.nanoTime(); - this.cpuStartTime = threadMXBean.getCurrentThreadCpuTime(); + threadID = Thread.currentThread().getId(); + this.cpuStartTime = threadMXBean.getThreadCpuTime(threadID); + LOG.info("111 - Thread Name {}", Thread.currentThread().getName()); this.scClkTck = OSGlobals.getScClkTck(); } @@ -88,8 +92,9 @@ private void emitMetrics(Exception exception) { } private double calculateCPUUtilization(long phaseStartTime, long phaseCPUStartTime) { + LOG.info("111 - Thread Name-1 {}", Thread.currentThread().getName()); long totalCpuTime = - Math.min(0, (threadMXBean.getCurrentThreadCpuTime() - phaseCPUStartTime)); + Math.max(0, (threadMXBean.getThreadCpuTime(threadID) - phaseCPUStartTime)); return Utils.calculateCPUUtilization( totalCpuTime, scClkTck, System.nanoTime() - phaseStartTime); } diff --git a/src/main/java/org/opensearch/performanceanalyzer/util/Utils.java b/src/main/java/org/opensearch/performanceanalyzer/util/Utils.java index 2f8e6a98..9597a377 100644 --- a/src/main/java/org/opensearch/performanceanalyzer/util/Utils.java +++ b/src/main/java/org/opensearch/performanceanalyzer/util/Utils.java @@ -116,6 +116,6 @@ public static double calculateCPUUtilization(long totalCpuTime, long scClkTck, l LOG.debug("totalCpuTime {}", totalCpuTime); LOG.debug("scClkTck {}", scClkTck); LOG.debug("totalTime {}", totalTime); - return (1.0e3 * totalCpuTime / Math.max(1, scClkTck)) / Math.max(1, totalTime); + return (1.0e3 * Math.max(0, totalCpuTime) / Math.max(1, scClkTck)) / Math.max(1, totalTime); } }