117117 */
118118@ AutomaticFeature
119119public class JfrFeature implements Feature {
120-
121- private final boolean hostedEnabled ;
122-
123- public JfrFeature () {
124- hostedEnabled = Boolean . parseBoolean ( getDiagnosticBean (). getVMOption ( "FlightRecorder" ). getValue ());
125- }
120+ /*
121+ * Note that we could initialize the native part of JFR at image build time and that the native
122+ * code sets the FlightRecorder option as a side effect. Therefore, we must ensure that we check
123+ * the value of the option before it can be affected by image building.
124+ */
125+ private static final boolean HOSTED_ENABLED = Boolean . parseBoolean ( getDiagnosticBean (). getVMOption ( "FlightRecorder" ). getValue ());
126126
127127 @ Override
128128 public boolean isInConfiguration (IsInConfigurationAccess access ) {
129129 boolean systemSupported = osSupported ();
130- if (hostedEnabled && !systemSupported ) {
130+ if (HOSTED_ENABLED && !systemSupported ) {
131131 throw UserError .abort ("FlightRecorder cannot be used to profile the image generator on this platform. " +
132132 "The image generator can only be profiled on platforms where FlightRecoder is also supported at run time." );
133133 }
134134 boolean runtimeEnabled = VMInspectionOptions .AllowVMInspection .getValue ();
135- if (hostedEnabled && !runtimeEnabled ) {
135+ if (HOSTED_ENABLED && !runtimeEnabled ) {
136136 System .err .println ("Warning: When FlightRecoder is used to profile the image generator, it is also automatically enabled in the native image at run time. " +
137137 "This can affect the measurements because it can can make the image larger and image build time longer." );
138138 runtimeEnabled = true ;
@@ -173,7 +173,7 @@ public void afterRegistration(AfterRegistrationAccess access) {
173173 List <Configuration > knownConfigurations = JFC .getConfigurations ();
174174 JVM .getJVM ().createNativeJFR ();
175175
176- ImageSingletons .add (JfrManager .class , new JfrManager (hostedEnabled ));
176+ ImageSingletons .add (JfrManager .class , new JfrManager (HOSTED_ENABLED ));
177177 ImageSingletons .add (SubstrateJVM .class , new SubstrateJVM (knownConfigurations ));
178178 ImageSingletons .add (JfrSerializerSupport .class , new JfrSerializerSupport ());
179179 ImageSingletons .add (JfrTraceIdMap .class , new JfrTraceIdMap ());
@@ -183,7 +183,7 @@ public void afterRegistration(AfterRegistrationAccess access) {
183183 JfrSerializerSupport .get ().register (new JfrThreadStateSerializer ());
184184 ThreadListenerSupport .get ().register (SubstrateJVM .getThreadLocal ());
185185
186- if (hostedEnabled ) {
186+ if (HOSTED_ENABLED ) {
187187 RuntimeClassInitializationSupport rci = ImageSingletons .lookup (RuntimeClassInitializationSupport .class );
188188 rci .initializeAtBuildTime ("jdk.management.jfr" , "Allow FlightRecorder to be used at image build time" );
189189 rci .initializeAtBuildTime ("com.sun.jmx.mbeanserver" , "Allow FlightRecorder to be used at image build time" );
0 commit comments