SynchronizationStatistics(HotspotRuntimeMBean hsRuntime) { super(SYNCLOCKS_HEADER); this.hsRuntime = hsRuntime; int countOfCounters = 0; for (Counter counter : hsRuntime.getInternalRuntimeCounters()) { String counterName = counter.getName(); if (this.counters.contains(counterName)) countOfCounters++; } if (countOfCounters == 0) throw new RuntimeException("No sync locks counters found"); }
@Override protected void innerCollect(StatisticsSink sink) throws Throwable { sink.add(SAFEPOINTS_VAR_COUNT, hsRuntime.getSafepointCount()); sink.add(SAFEPOINTS_VAR_SYNCTIME, hsRuntime.getSafepointSyncTime()); sink.add(SAFEPOINTS_VAR_TOTALTIME, hsRuntime.getTotalSafepointTime()); } }
@Override protected void innerCollect(StatisticsSink sink) { for (Counter counter : hsRuntime.getInternalRuntimeCounters()) { String counterName = counter.getName(); if (counters.contains(counterName)) { counterName = counterName.substring("sun.rt._sync_".length()).toLowerCase(); Object value = counter.getValue(); sink.add(counterName, value.toString()); } } } }
import sun.management.ManagementFactoryHelper; import sun.management.counter.Counter; import java.util.NoSuchElementException; public class Test { public static void main(String[] args) { System.out.println(getContendedLockAttempts()); } private static long getContendedLockAttempts() { for (Counter counter : ManagementFactoryHelper.getHotspotRuntimeMBean().getInternalRuntimeCounters()) { if ("sun.rt._sync_ContendedLockAttempts".equals(counter.getName())) { return (long) counter.getValue(); } } throw new NoSuchElementException(); } }