@@ -118,6 +118,7 @@ public abstract class BTraceRuntimeImplBase implements BTraceRuntime.Impl, Runti
118118
119119 private static final int CMD_QUEUE_LIMIT_DEFAULT = 100 ;
120120 private static int CMD_QUEUE_LIMIT ;
121+ private boolean shouldInitializeMBeans = true ; // mbean initialization guard; synchronized over *this*
121122
122123 /**
123124 * Utility to create a new jvmstat perf counter. Called
@@ -523,6 +524,7 @@ final void init(Class cl, TimerHandler[] tHandlers, EventHandler[] evHandlers, E
523524 * just at the end of it's class initializer.
524525 */
525526 public final void start () {
527+ initMBeans ();
526528 if (timerHandlers != null ) {
527529 timer = new Timer (true );
528530 TimerTask [] timerTasks = new TimerTask [timerHandlers .length ];
@@ -539,7 +541,6 @@ public final void start() {
539541 }
540542
541543 if (lowMemoryHandlers != null ) {
542- initMBeans ();
543544 lowMemoryHandlerMap = new HashMap <>();
544545 for (LowMemoryHandler lmh : lowMemoryHandlers ) {
545546 String poolName = args .template (lmh .pool );
@@ -804,31 +805,26 @@ public final Profiler newProfiler(int expectedMethodCnt) {
804805
805806 @ Override
806807 public final RuntimeMXBean getRuntimeMXBean () {
807- initRuntimeMBean ();
808808 return runtimeMBean ;
809809 }
810810
811811 @ Override
812812 public final ThreadMXBean getThreadMXBean () {
813- initThreadMBean ();
814813 return threadMBean ;
815814 }
816815
817816 @ Override
818817 public final OperatingSystemMXBean getOperatingSystemMXBean () {
819- initOperatingSystemMBean ();
820818 return operatingSystemMXBean ;
821819 }
822820
823821 @ Override
824822 public final List <GarbageCollectorMXBean > getGCMBeans () {
825- initGcMBeans ();
826823 return gcBeanList ;
827824 }
828825
829826 @ Override
830827 public final HotSpotDiagnosticMXBean getHotspotMBean () {
831- initHotspotMBean ();
832828 return hotspotMBean ;
833829 }
834830
@@ -955,15 +951,21 @@ public Thread newThread(Runnable r) {
955951 }
956952 }
957953
958- private void initMBeans () {
959- initMemoryMBean ();
960- initOperatingSystemMBean ();
961- initRuntimeMBean ();
962- initThreadMBean ();
963- initHotspotMBean ();
964- initGcMBeans ();
965- initMemoryPoolList ();
966- initMemoryListener ();
954+ /**
955+ * Must be called exactly once before the runtime starts
956+ */
957+ private synchronized void initMBeans () {
958+ if (shouldInitializeMBeans ) {
959+ initMemoryMBean ();
960+ initOperatingSystemMBean ();
961+ initRuntimeMBean ();
962+ initThreadMBean ();
963+ initHotspotMBean ();
964+ initGcMBeans ();
965+ initMemoryPoolList ();
966+ initMemoryListener ();
967+ shouldInitializeMBeans = false ;
968+ }
967969 }
968970
969971 private void initMemoryListener () {
@@ -1185,118 +1187,94 @@ public List<MemoryPoolMXBean> run() throws Exception {
11851187 }
11861188
11871189 private static void initMemoryMBean () {
1188- synchronized (BTraceRuntimeImplBase .class ) {
1189- if (memoryMBean == null ) {
1190- try {
1191- memoryMBean = AccessController .doPrivileged (
1192- new PrivilegedExceptionAction <MemoryMXBean >() {
1193- @ Override
1194- public MemoryMXBean run () throws Exception {
1195- return ManagementFactory .getMemoryMXBean ();
1196- }
1197- });
1198- } catch (Exception exp ) {
1199- throw new UnsupportedOperationException (exp );
1200- }
1201- }
1190+ try {
1191+ memoryMBean = AccessController .doPrivileged (
1192+ new PrivilegedExceptionAction <MemoryMXBean >() {
1193+ @ Override
1194+ public MemoryMXBean run () throws Exception {
1195+ return ManagementFactory .getMemoryMXBean ();
1196+ }
1197+ });
1198+ } catch (Exception exp ) {
1199+ throw new UnsupportedOperationException (exp );
12021200 }
12031201 }
12041202
12051203 private static void initOperatingSystemMBean () {
1206- synchronized (BTraceRuntimeImplBase .class ) {
1207- if (operatingSystemMXBean == null ) {
1208- try {
1209- operatingSystemMXBean = AccessController .doPrivileged (
1210- new PrivilegedExceptionAction <OperatingSystemMXBean >() {
1211- @ Override
1212- public OperatingSystemMXBean run () throws Exception {
1213- return ManagementFactory .getOperatingSystemMXBean ();
1214- }
1215- }
1216- );
1217- } catch (Exception e ) {
1218- throw new UnsupportedOperationException (e );
1219- }
1220- }
1204+ try {
1205+ operatingSystemMXBean = AccessController .doPrivileged (
1206+ new PrivilegedExceptionAction <OperatingSystemMXBean >() {
1207+ @ Override
1208+ public OperatingSystemMXBean run () throws Exception {
1209+ return ManagementFactory .getOperatingSystemMXBean ();
1210+ }
1211+ }
1212+ );
1213+ } catch (Exception e ) {
1214+ throw new UnsupportedOperationException (e );
12211215 }
12221216 }
12231217
12241218 private static void initRuntimeMBean () {
1225- synchronized (BTraceRuntimeImplBase .class ) {
1226- if (runtimeMBean == null ) {
1227- try {
1228- runtimeMBean = AccessController .doPrivileged (new PrivilegedExceptionAction <RuntimeMXBean >() {
1229- @ Override
1230- public RuntimeMXBean run () throws Exception {
1231- return ManagementFactory .getRuntimeMXBean ();
1232- }
1233- });
1234- } catch (Exception e ) {
1235- throw new RuntimeException (e );
1219+ try {
1220+ runtimeMBean = AccessController .doPrivileged (new PrivilegedExceptionAction <RuntimeMXBean >() {
1221+ @ Override
1222+ public RuntimeMXBean run () throws Exception {
1223+ return ManagementFactory .getRuntimeMXBean ();
12361224 }
1237- }
1225+ });
1226+ } catch (Exception e ) {
1227+ throw new RuntimeException (e );
12381228 }
12391229 }
12401230
12411231 private static void initThreadMBean () {
1242- synchronized (BTraceRuntimeImplBase .class ) {
1243- if (threadMBean == null ) {
1244- try {
1245- threadMBean = AccessController .doPrivileged (new PrivilegedExceptionAction <ThreadMXBean >() {
1246- @ Override
1247- public ThreadMXBean run () throws Exception {
1248- return ManagementFactory .getThreadMXBean ();
1249- }
1250- });
1251- } catch (Exception e ) {
1252- throw new RuntimeException (e );
1232+ try {
1233+ threadMBean = AccessController .doPrivileged (new PrivilegedExceptionAction <ThreadMXBean >() {
1234+ @ Override
1235+ public ThreadMXBean run () throws Exception {
1236+ return ManagementFactory .getThreadMXBean ();
12531237 }
1254- }
1238+ });
1239+ } catch (Exception e ) {
1240+ throw new RuntimeException (e );
12551241 }
12561242 }
12571243
12581244 private static void initGcMBeans () {
1259- synchronized (BTraceRuntimeImplBase .class ) {
1260- if (gcBeanList == null ) {
1261- try {
1262- gcBeanList = AccessController .doPrivileged (new PrivilegedExceptionAction <List <GarbageCollectorMXBean >>() {
1263- @ Override
1264- public List <GarbageCollectorMXBean > run () throws Exception {
1265- return ManagementFactory .getGarbageCollectorMXBeans ();
1266- }
1267- });
1268- } catch (Exception e ) {
1269- throw new RuntimeException (e );
1245+ try {
1246+ gcBeanList = AccessController .doPrivileged (new PrivilegedExceptionAction <List <GarbageCollectorMXBean >>() {
1247+ @ Override
1248+ public List <GarbageCollectorMXBean > run () throws Exception {
1249+ return ManagementFactory .getGarbageCollectorMXBeans ();
12701250 }
1271- }
1251+ });
1252+ } catch (Exception e ) {
1253+ throw new RuntimeException (e );
12721254 }
12731255 }
12741256
12751257 private static void initHotspotMBean () {
1276- synchronized (BTraceRuntimeImplBase .class ) {
1277- if (hotspotMBean == null ) {
1278- try {
1279- hotspotMBean = AccessController .doPrivileged (new PrivilegedExceptionAction <HotSpotDiagnosticMXBean >() {
1280- @ Override
1281- public HotSpotDiagnosticMXBean run () throws Exception {
1282- MBeanServer server = ManagementFactory .getPlatformMBeanServer ();
1283- Set <ObjectName > s = server .queryNames (new ObjectName (HOTSPOT_BEAN_NAME ), null );
1284- Iterator <ObjectName > itr = s .iterator ();
1285- if (itr .hasNext ()) {
1286- ObjectName name = itr .next ();
1287- HotSpotDiagnosticMXBean bean =
1288- ManagementFactory .newPlatformMXBeanProxy (server ,
1289- name .toString (), HotSpotDiagnosticMXBean .class );
1290- return bean ;
1291- } else {
1292- return null ;
1293- }
1294- }
1295- });
1296- } catch (Exception e ) {
1297- throw new UnsupportedOperationException (e );
1258+ try {
1259+ hotspotMBean = AccessController .doPrivileged (new PrivilegedExceptionAction <HotSpotDiagnosticMXBean >() {
1260+ @ Override
1261+ public HotSpotDiagnosticMXBean run () throws Exception {
1262+ MBeanServer server = ManagementFactory .getPlatformMBeanServer ();
1263+ Set <ObjectName > s = server .queryNames (new ObjectName (HOTSPOT_BEAN_NAME ), null );
1264+ Iterator <ObjectName > itr = s .iterator ();
1265+ if (itr .hasNext ()) {
1266+ ObjectName name = itr .next ();
1267+ HotSpotDiagnosticMXBean bean =
1268+ ManagementFactory .newPlatformMXBeanProxy (server ,
1269+ name .toString (), HotSpotDiagnosticMXBean .class );
1270+ return bean ;
1271+ } else {
1272+ return null ;
1273+ }
12981274 }
1299- }
1275+ });
1276+ } catch (Exception e ) {
1277+ throw new UnsupportedOperationException (e );
13001278 }
13011279 }
13021280
@@ -1316,9 +1294,6 @@ public MemoryMXBean getMemoryMXBean() {
13161294 }
13171295
13181296 protected static PerfReader getPerfReader () {
1319- if (perfReader == null ) {
1320- throw new UnsupportedOperationException ();
1321- }
13221297 return perfReader ;
13231298 }
13241299
0 commit comments