Skip to content

Commit 3045140

Browse files
committed
check thread contention monitoring enabled/disabled via PA setting
Signed-off-by: Surya Sashank Nistala <[email protected]>
1 parent 4688445 commit 3045140

File tree

7 files changed

+33
-9
lines changed

7 files changed

+33
-9
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
true
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
true

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: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,6 @@ public class ThreadList {
5252

5353
private static Lock vmAttachLock = new ReentrantLock();
5454

55-
static {
56-
if (threadBean.isThreadContentionMonitoringSupported()) {
57-
threadBean.setThreadContentionMonitoringEnabled(true);
58-
}
59-
}
60-
6155
public static class ThreadState {
6256
public long javaTid;
6357
public long nativeTid;
@@ -123,8 +117,13 @@ public String toString() {
123117
* acquire this lock and move on if we could not get it.
124118
*
125119
* @return A hashmap of threadId to threadState.
120+
* @param threadContentionMonitoringEnabled
126121
*/
127-
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+
}
128127
if (vmAttachLock.tryLock()) {
129128
try {
130129
// Thread dumps are expensive and therefore we make sure that at least

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)