100100final class SamplerImpl {
101101
102102 private static final Logger LOGGER = Logger .getLogger (SamplerImpl .class .getName ());
103+
104+ private static final boolean MEMORY_INITIALLY_AVAILABLE = Boolean .getBoolean ("visualvm.graalsampler.memory.alwaysEnabled" ); // NOI18N
103105
104106 private static enum State { TERMINATED , INACTIVE , CPU , MEMORY , TRANSITION };
105107
@@ -108,13 +110,16 @@ private static enum State { TERMINATED, INACTIVE, CPU, MEMORY, TRANSITION };
108110
109111 private HTMLTextArea summaryArea ;
110112 private String cpuStatus = NbBundle .getMessage (SamplerImpl .class , "MSG_Checking_Availability" ); // NOI18N
111- private String memoryStatus = NbBundle .getMessage (SamplerImpl .class , "MSG_Checking_Availability" ); // NOI18N
113+ private String memoryStatus = NbBundle .getMessage (SamplerImpl .class , MEMORY_INITIALLY_AVAILABLE ? "MSG_Memory_experimental2" : "MSG_Memory_experimental1" ); // NOI18N
114+ // private String memoryStatus = NbBundle.getMessage(SamplerImpl.class, "MSG_Checking_Availability"); // NOI18N
112115
113116 private boolean cpuProfilingSupported ;
114117 private AbstractSamplerSupport cpuSampler ;
115118 private CPUSettingsSupport cpuSettings ;
116119
117- private boolean memoryProfilingSupported ;
120+ private boolean memoryProfilingSupported = MEMORY_INITIALLY_AVAILABLE ;
121+ private boolean memoryInitializationPending ;
122+ // private boolean memoryProfilingSupported;
118123 private AbstractSamplerSupport memorySampler ;
119124 private MemorySettingsSupport memorySettings ;
120125
@@ -170,7 +175,7 @@ public void hierarchyChanged(HierarchyEvent e) {
170175 if ((e .getChangeFlags () & HierarchyEvent .SHOWING_CHANGED ) != 0 ) {
171176 if (view .isShowing ()) {
172177 initializeCpuSampling ();
173- initializeMemorySampling ();
178+ // initializeMemorySampling();
174179 view .removeHierarchyListener (this );
175180 }
176181 }
@@ -333,7 +338,7 @@ private void updateButtons() {
333338 cpuButton .setEnabled (cpuProfilingSupported );
334339
335340 memoryButton .setSelected (false );
336- memoryButton .setEnabled (memoryProfilingSupported );
341+ memoryButton .setEnabled (memoryProfilingSupported || memoryInitializationPending );
337342
338343 stopButton .setEnabled (false );
339344
@@ -344,7 +349,7 @@ private void updateButtons() {
344349 cpuButton .setEnabled (true );
345350
346351 memoryButton .setSelected (false );
347- memoryButton .setEnabled (memoryProfilingSupported );
352+ memoryButton .setEnabled (memoryProfilingSupported || memoryInitializationPending );
348353
349354 stopButton .setEnabled (true );
350355
@@ -422,50 +427,57 @@ public void run() {
422427 }
423428
424429 private void handleMemoryProfiling () {
425- if (!memorySettings .settingsValid ()) {
426- memoryButton .setSelected (false );
427- if (dvc != null ) memorySettings .showSettings (dvc );
428- ProfilerDialogs .displayError (NbBundle .getMessage (SamplerImpl .class , "MSG_Incorrect_Memory_settings" )); // NOI18N
429- return ;
430- }
431-
432- State currentState = getState ();
433- if (currentState .equals (State .MEMORY ) ||
434- currentState .equals (State .TERMINATED ) ||
435- currentState .equals (State .TRANSITION )) return ;
436- setState (State .TRANSITION );
437-
438- final Runnable sessionStarter = new Runnable () {
430+ Runnable memoryProfilingHandler = new Runnable () {
439431 public void run () {
440- SwingUtilities .invokeLater (new Runnable () {
432+ if (!memorySettings .settingsValid ()) {
433+ memoryButton .setSelected (false );
434+ if (dvc != null ) memorySettings .showSettings (dvc );
435+ ProfilerDialogs .displayError (NbBundle .getMessage (SamplerImpl .class , "MSG_Incorrect_Memory_settings" )); // NOI18N
436+ return ;
437+ }
438+
439+ State currentState = getState ();
440+ if (currentState .equals (State .MEMORY ) ||
441+ currentState .equals (State .TERMINATED ) ||
442+ currentState .equals (State .TRANSITION )) return ;
443+ setState (State .TRANSITION );
444+
445+ final Runnable sessionStarter = new Runnable () {
441446 public void run () {
442- setCurrentViews (NbBundle .getMessage (SamplerImpl .class ,
443- "LBL_Memory_samples" ), memorySampler .getDetailsView ()); // NOI18N
444- RequestProcessor .getDefault ().post (new Runnable () {
447+ SwingUtilities .invokeLater (new Runnable () {
445448 public void run () {
446- memorySettings .saveSettings ();
447- setState (memorySampler .startSampling (
448- memorySettings .getSettings (),
449- memorySettings .getSamplingRate (),
450- memorySettings .getRefreshRate ()) ?
451- State .MEMORY : State .INACTIVE );
449+ setCurrentViews (NbBundle .getMessage (SamplerImpl .class ,
450+ "LBL_Memory_samples" ), memorySampler .getDetailsView ()); // NOI18N
451+ RequestProcessor .getDefault ().post (new Runnable () {
452+ public void run () {
453+ memorySettings .saveSettings ();
454+ setState (memorySampler .startSampling (
455+ memorySettings .getSettings (),
456+ memorySettings .getSamplingRate (),
457+ memorySettings .getRefreshRate ()) ?
458+ State .MEMORY : State .INACTIVE );
459+ }
460+ });
452461 }
453462 });
454463 }
455- });
456- }
457- };
464+ };
458465
459- if (currentState .equals (State .CPU )) {
460- RequestProcessor .getDefault ().post (new Runnable () {
461- public void run () {
462- cpuSampler .stopSampling ();
466+ if (currentState .equals (State .CPU )) {
467+ RequestProcessor .getDefault ().post (new Runnable () {
468+ public void run () {
469+ cpuSampler .stopSampling ();
470+ sessionStarter .run ();
471+ }
472+ });
473+ } else {
463474 sessionStarter .run ();
464475 }
465- });
466- } else {
467- sessionStarter .run ();
468- }
476+ }
477+ };
478+
479+ if (memorySampler == null ) initializeMemorySampling (memoryProfilingHandler );
480+ else if (memoryProfilingSupported ) memoryProfilingHandler .run ();
469481 }
470482
471483 private void handleStopProfiling () {
@@ -579,7 +591,7 @@ public void run() {
579591 });
580592 }
581593
582- private void initializeMemorySampling () {
594+ private void initializeMemorySampling (Runnable onSuccess ) {
583595 RequestProcessor .getDefault ().post (new Runnable () {
584596 public void run () {
585597 MemoryHistogramProvider histogramProvider = new MemoryHistogramProvider (application );
@@ -588,8 +600,17 @@ public void run() {
588600 if (status != null ) {
589601 SwingUtilities .invokeLater (new Runnable () {
590602 public void run () {
591- memoryStatus = status ;
592- refreshSummary ();
603+ memoryProfilingSupported = false ; // may be initially true (visualvm.graalsampler.memory.alwaysEnabled)
604+ memoryInitializationPending = false ;
605+
606+ handleStopProfiling ();
607+
608+ // memoryStatus = status;
609+ // refreshSummary();
610+ updateButtons ();
611+ updateSettings ();
612+
613+ ProfilerDialogs .displayError (status , NbBundle .getMessage (SamplerImpl .class , "CAP_Memory_error" ), null ); // NOI18N
593614 }
594615 });
595616 return ;
@@ -671,11 +692,13 @@ public void takeHeapDump(boolean openView) {
671692 };
672693 SwingUtilities .invokeLater (new Runnable () {
673694 public void run () {
695+ int i = 0 ;
696+
674697 String avail = NbBundle .getMessage (SamplerImpl .class ,
675698 "MSG_Available" ); // NOI18N
676699 if (noPerformGC != null || noHeapDump != null ) {
677700 String [] msgs = new String [3 ];
678- int i = 0 ;
701+ // int i = 0;
679702 if (noHeapDump != null ) {
680703 msgs [i ++] = noHeapDump ;
681704 }
@@ -688,17 +711,22 @@ public void run() {
688711 } else if (i == 2 ) {
689712 avail = NbBundle .getMessage (SamplerImpl .class ,
690713 "MSG_Available_details2" , msgs [0 ], msgs [1 ]); // NOI18N
691- } else {
692- avail = NbBundle .getMessage (SamplerImpl .class ,
693- "MSG_Available_details3" , msgs [0 ], msgs [1 ], msgs [2 ]); // NOI18N
714+ // } else {
715+ // avail = NbBundle.getMessage(SamplerImpl.class,
716+ // "MSG_Available_details3", msgs[0], msgs[1], msgs[2]); // NOI18N
694717 }
695718 }
696- memoryStatus = avail + " " + NbBundle .getMessage ( // NOI18N
697- SamplerImpl .class , "MSG_Press_mem" ); // NOI18N
719+ // memoryStatus = avail + " " + NbBundle.getMessage( // NOI18N
720+ // SamplerImpl.class, "MSG_Press_mem"); // NOI18N
698721 memoryProfilingSupported = true ;
699- refreshSummary ();
722+ memoryInitializationPending = false ;
723+ // refreshSummary();
700724 updateButtons ();
701725 updateSettings ();
726+
727+ if (i > 0 ) ProfilerDialogs .displayWarningDNSA (avail , NbBundle .getMessage (SamplerImpl .class , "CAP_Memory_warning" ), null , SamplerImpl .class .getName (), false ); // NOI18N
728+
729+ onSuccess .run ();
702730 }
703731 });
704732 }
@@ -713,7 +741,18 @@ private synchronized Timer getTimer() {
713741 }
714742
715743 private DataViewComponent .DetailsView [] createSummaryView () {
716- summaryArea = new HTMLTextArea ();
744+ summaryArea = new HTMLTextArea () {
745+ protected void showURL (URL url ) {
746+ SwingUtilities .invokeLater (new Runnable () {
747+ public void run () {
748+ memoryButton .setEnabled (true );
749+ memoryInitializationPending = true ;
750+ memoryStatus = NbBundle .getMessage (SamplerImpl .class , "MSG_Memory_experimental2" ); // NOI18N
751+ refreshSummary ();
752+ }
753+ });
754+ }
755+ };
717756 summaryArea .setBorder (BorderFactory .createEmptyBorder (10 , 10 , 10 , 10 ));
718757
719758 refreshSummary ();
0 commit comments