Skip to content

Commit d329ddb

Browse files
authored
Fixes calculation of average thread blocked time and average thread waited time (#118)
* Enables thread contention monitoring. Fixes calculation of average thread blocked time and average thread waited time Signed-off-by: Surya Sashank Nistala <[email protected]> * check thread contention monitoring enabled/disabled via PA setting Signed-off-by: Surya Sashank Nistala <[email protected]> * remove test generated files Signed-off-by: Surya Sashank Nistala <[email protected]>
1 parent 8526243 commit d329ddb

File tree

5 files changed

+36
-3
lines changed

5 files changed

+36
-3
lines changed

src/main/java/org/opensearch/performanceanalyzer/collectors/OSMetricsCollector.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ public void collectMetrics(long startTime) {
4545
SchedMetricsGenerator schedMetricsGenerator = osMetricsGenerator.getSchedMetricsGenerator();
4646
schedMetricsGenerator.addSample();
4747

48-
Map<Long, ThreadList.ThreadState> threadStates = ThreadList.getNativeTidMap();
48+
Map<Long, ThreadList.ThreadState> threadStates =
49+
ThreadList.getNativeTidMap(getThreadContentionMonitoringEnabled());
4950

5051
DiskIOMetricsGenerator diskIOMetricsGenerator =
5152
osMetricsGenerator.getDiskIOMetricsGenerator();

src/main/java/org/opensearch/performanceanalyzer/collectors/PerformanceAnalyzerMetricsCollector.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ enum State {
3333
private String collectorName;
3434
protected StringBuilder value;
3535
protected State state;
36+
private boolean threadContentionMonitoringEnabled;
3637

3738
protected PerformanceAnalyzerMetricsCollector(int timeInterval, String collectorName) {
3839
this.timeInterval = timeInterval;
@@ -92,4 +93,12 @@ public State getState() {
9293
public void setState(State state) {
9394
this.state = state;
9495
}
96+
97+
public void setThreadContentionMonitoringEnabled(boolean enabled) {
98+
this.threadContentionMonitoringEnabled = enabled;
99+
}
100+
101+
public boolean getThreadContentionMonitoringEnabled() {
102+
return threadContentionMonitoringEnabled;
103+
}
95104
}

src/main/java/org/opensearch/performanceanalyzer/collectors/ScheduledMetricCollectorsExecutor.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public class ScheduledMetricCollectorsExecutor extends Thread {
2525
private static final int COLLECTOR_THREAD_KEEPALIVE_SECS = 1000;
2626
private final boolean checkFeatureDisabledFlag;
2727
private boolean paEnabled = false;
28+
private boolean threadContentionMonitoringEnabled = false;
2829
private int minTimeIntervalToSleep = Integer.MAX_VALUE;
2930
private Map<PerformanceAnalyzerMetricsCollector, Long> metricsCollectors;
3031

@@ -52,7 +53,19 @@ public synchronized boolean getEnabled() {
5253
return paEnabled;
5354
}
5455

56+
public synchronized void setThreadContentionMonitoringEnabled(final boolean enabled) {
57+
metricsCollectors
58+
.keySet()
59+
.forEach(collector -> collector.setThreadContentionMonitoringEnabled(enabled));
60+
threadContentionMonitoringEnabled = enabled;
61+
}
62+
63+
private synchronized boolean getThreadContentionMonitoringEnabled() {
64+
return threadContentionMonitoringEnabled;
65+
}
66+
5567
public void addScheduledMetricCollector(PerformanceAnalyzerMetricsCollector task) {
68+
task.setThreadContentionMonitoringEnabled(getThreadContentionMonitoringEnabled());
5669
metricsCollectors.put(task, System.currentTimeMillis() + task.getTimeInterval());
5770
if (task.getTimeInterval() < minTimeIntervalToSleep) {
5871
minTimeIntervalToSleep = task.getTimeInterval();

src/main/java/org/opensearch/performanceanalyzer/jvm/ThreadList.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,13 @@ public String toString() {
117117
* acquire this lock and move on if we could not get it.
118118
*
119119
* @return A hashmap of threadId to threadState.
120+
* @param threadContentionMonitoringEnabled
120121
*/
121-
public static Map<Long, ThreadState> getNativeTidMap() {
122+
public static Map<Long, ThreadState> getNativeTidMap(
123+
boolean threadContentionMonitoringEnabled) {
124+
if (threadBean.isThreadContentionMonitoringSupported()) {
125+
threadBean.setThreadContentionMonitoringEnabled(threadContentionMonitoringEnabled);
126+
}
122127
if (vmAttachLock.tryLock()) {
123128
try {
124129
// Thread dumps are expensive and therefore we make sure that at least
@@ -264,6 +269,9 @@ private static void parseThreadInfo(final ThreadInfo info) {
264269
1.0e-3
265270
* (t.blockedTime - oldt.blockedTime)
266271
/ (t.blockedCount - oldt.blockedCount);
272+
} else if (t.blockedCount == oldt.blockedCount && t.blockedTime > oldt.blockedTime) {
273+
t.avgBlockedTime =
274+
1.0e-3 * (t.blockedTime - oldt.blockedTime + oldt.avgBlockedTime);
267275
} else {
268276
CircularLongArray arr = ThreadHistory.blockedTidHistoryMap.get(t.nativeTid);
269277
// NOTE: this is an upper bound
@@ -276,6 +284,8 @@ private static void parseThreadInfo(final ThreadInfo info) {
276284
1.0e-3
277285
* (t.waitedTime - oldt.waitedTime)
278286
/ (t.waitedCount - oldt.waitedCount);
287+
} else if (t.waitedCount == oldt.waitedCount && t.waitedTime > oldt.waitedTime) {
288+
t.avgWaitedTime = 1.0e-3 * (t.waitedTime - oldt.waitedTime + oldt.avgWaitedTime);
279289
} else {
280290
CircularLongArray arr = ThreadHistory.waitedTidHistoryMap.get(t.nativeTid);
281291
// NOTE: this is an upper bound

src/test/java/org/opensearch/performanceanalyzer/jvm/ThreadListTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ private static void runOnce() throws InterruptedException {
3636
String params[] = new String[0];
3737
while (true) {
3838
ThreadList.runThreadDump(OSGlobals.getPid(), params);
39-
ThreadList.LOGGER.info(ThreadList.getNativeTidMap().values());
39+
ThreadList.LOGGER.info(ThreadList.getNativeTidMap(false).values());
4040

4141
/*GCMetrics.runOnce();
4242
HeapMetrics.runOnce();

0 commit comments

Comments
 (0)