Skip to content

Commit

Permalink
check thread contention monitoring enabled/disabled via PA setting
Browse files Browse the repository at this point in the history
Signed-off-by: Surya Sashank Nistala <snistala@amazon.com>
  • Loading branch information
eirsep committed Apr 4, 2022
1 parent bbc6813 commit e4656c5
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 9 deletions.
1 change: 1 addition & 0 deletions null/data/batch_metrics_enabled.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
true
1 change: 1 addition & 0 deletions null/data/thread_contention_monitoring_enabled.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
true
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ public void collectMetrics(long startTime) {
SchedMetricsGenerator schedMetricsGenerator = osMetricsGenerator.getSchedMetricsGenerator();
schedMetricsGenerator.addSample();

Map<Long, ThreadList.ThreadState> threadStates = ThreadList.getNativeTidMap();
Map<Long, ThreadList.ThreadState> threadStates =
ThreadList.getNativeTidMap(getThreadContentionMonitoringEnabled());

DiskIOMetricsGenerator diskIOMetricsGenerator =
osMetricsGenerator.getDiskIOMetricsGenerator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ enum State {
private String collectorName;
protected StringBuilder value;
protected State state;
private boolean threadContentionMonitoringEnabled;

protected PerformanceAnalyzerMetricsCollector(int timeInterval, String collectorName) {
this.timeInterval = timeInterval;
Expand Down Expand Up @@ -113,4 +114,12 @@ public State getState() {
public void setState(State state) {
this.state = state;
}

public void setThreadContentionMonitoringEnabled(boolean enabled) {
this.threadContentionMonitoringEnabled = enabled;
}

public boolean getThreadContentionMonitoringEnabled() {
return threadContentionMonitoringEnabled;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public class ScheduledMetricCollectorsExecutor extends Thread {
private static final int COLLECTOR_THREAD_KEEPALIVE_SECS = 1000;
private final boolean checkFeatureDisabledFlag;
private boolean paEnabled = false;
private boolean threadContentionMonitoringEnabled = false;
private int minTimeIntervalToSleep = Integer.MAX_VALUE;
private Map<PerformanceAnalyzerMetricsCollector, Long> metricsCollectors;

Expand Down Expand Up @@ -73,7 +74,19 @@ public synchronized boolean getEnabled() {
return paEnabled;
}

public synchronized void setThreadContentionMonitoringEnabled(final boolean enabled) {
metricsCollectors
.keySet()
.forEach(collector -> collector.setThreadContentionMonitoringEnabled(enabled));
threadContentionMonitoringEnabled = enabled;
}

private synchronized boolean getThreadContentionMonitoringEnabled() {
return threadContentionMonitoringEnabled;
}

public void addScheduledMetricCollector(PerformanceAnalyzerMetricsCollector task) {
task.setThreadContentionMonitoringEnabled(getThreadContentionMonitoringEnabled());
metricsCollectors.put(task, System.currentTimeMillis() + task.getTimeInterval());
if (task.getTimeInterval() < minTimeIntervalToSleep) {
minTimeIntervalToSleep = task.getTimeInterval();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,6 @@ public class ThreadList {

private static Lock vmAttachLock = new ReentrantLock();

static {
if (threadBean.isThreadContentionMonitoringSupported()) {
threadBean.setThreadContentionMonitoringEnabled(true);
}
}

public static class ThreadState {
public long javaTid;
public long nativeTid;
Expand Down Expand Up @@ -144,8 +138,13 @@ public String toString() {
* acquire this lock and move on if we could not get it.
*
* @return A hashmap of threadId to threadState.
* @param threadContentionMonitoringEnabled
*/
public static Map<Long, ThreadState> getNativeTidMap() {
public static Map<Long, ThreadState> getNativeTidMap(
boolean threadContentionMonitoringEnabled) {
if (threadBean.isThreadContentionMonitoringSupported()) {
threadBean.setThreadContentionMonitoringEnabled(threadContentionMonitoringEnabled);
}
if (vmAttachLock.tryLock()) {
try {
// Thread dumps are expensive and therefore we make sure that at least
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ private static void runOnce() throws InterruptedException {
String params[] = new String[0];
while (true) {
ThreadList.runThreadDump(OSGlobals.getPid(), params);
ThreadList.LOGGER.info(ThreadList.getNativeTidMap().values());
ThreadList.LOGGER.info(ThreadList.getNativeTidMap(false).values());

/*GCMetrics.runOnce();
HeapMetrics.runOnce();
Expand Down

0 comments on commit e4656c5

Please sign in to comment.