Skip to content

Commit 60cf85a

Browse files
authored
Display thread dumps within VisualVM viewer (#97)
* Display thread dumps within VisualVM viewer * Updates
1 parent 52e7aff commit 60cf85a

File tree

4 files changed

+108
-13
lines changed

4 files changed

+108
-13
lines changed

coherence-visualvm-plugin/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@
6666
<groupId>org.graalvm.visualvm.api</groupId>
6767
<artifactId>org-graalvm-visualvm-application-views</artifactId>
6868
</dependency>
69+
<dependency>
70+
<groupId>org.graalvm.visualvm.api</groupId>
71+
<artifactId>org-graalvm-visualvm-threaddump</artifactId>
72+
</dependency>
6973
<dependency>
7074
<groupId>org.graalvm.visualvm.api</groupId>
7175
<artifactId>org-graalvm-visualvm-charts</artifactId>

coherence-visualvm-plugin/src/main/java/com/oracle/coherence/plugin/visualvm/panel/CoherenceMemberPanel.java

Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, 2023 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2020, 2024 Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -46,20 +46,27 @@
4646
import java.awt.event.ActionEvent;
4747

4848
import java.awt.event.ActionListener;
49+
import java.io.File;
50+
import java.io.IOException;
51+
import java.io.PrintWriter;
52+
4953
import java.util.ArrayList;
5054
import java.util.Date;
5155
import java.util.List;
5256
import java.util.Map;
5357
import java.util.Map.Entry;
5458

5559
import java.util.logging.Logger;
60+
import com.oracle.coherence.plugin.visualvm.threaddump.ThreadDumpImpl;
5661
import javax.swing.*;
5762
import javax.swing.border.CompoundBorder;
5863
import javax.swing.border.EmptyBorder;
5964
import javax.swing.border.TitledBorder;
6065

6166
import javax.swing.event.ChangeListener;
6267
import org.graalvm.visualvm.charts.SimpleXYChartSupport;
68+
import org.graalvm.visualvm.core.datasource.DataSource;
69+
import org.graalvm.visualvm.core.ui.DataSourceWindowManager;
6370
import org.openide.awt.StatusDisplayer;
6471

6572
import static com.oracle.coherence.plugin.visualvm.Localization.getLocalText;
@@ -267,7 +274,6 @@ public void updateGUI()
267274
{
268275
GraphHelper.addValuesToClusterMemoryGraph(f_memoryGraph, cTotalMemory, cTotalMemoryUsed);
269276
}
270-
271277
}
272278

273279
@Override
@@ -515,7 +521,7 @@ public void actionPerformed(ActionEvent e)
515521

516522
DialogHelper.showInfoDialog(getLocalText("LBL_thread_dump_confirmation"));
517523

518-
StringBuilder sb = new StringBuilder(sMessage).append("\n").append(generateHeader(nNode));
524+
StringBuilder sb = new StringBuilder(sMessage);
519525

520526
Timer timer = new Timer(nDelay * 1000, null);
521527

@@ -542,7 +548,7 @@ public void actionPerformed(ActionEvent e)
542548
timer.stop();
543549
status[0] = StatusDisplayer.getDefault().setStatusText(getLocalText("LBL_thread_dump_completed"),5);
544550
status[0].clear(5000);
545-
showThreadDumpResult(nNode, sb.toString());
551+
showThreadDumpInVisualVM(nNode, sb.toString());
546552
}
547553
}
548554

@@ -555,7 +561,7 @@ public void actionPerformed(ActionEvent e)
555561
}
556562
else
557563
{
558-
showThreadDumpResult(nNodeId, generateHeader(nNodeId) + " " + m_requestSender.getNodeState(nNodeId));
564+
showThreadDumpInVisualVM(nNodeId, m_requestSender.getNodeState(nNodeId));
559565
}
560566
}
561567
catch (Exception ee)
@@ -568,17 +574,48 @@ public void actionPerformed(ActionEvent e)
568574
// ------ helpers -----------------------------------------------
569575

570576
/**
571-
* Display the thread dump result.
572-
*
573-
* @param nNode node id
574-
* @param sThreadDump the result
577+
* Show the therad dump result in VisualVM thread dump viewer.
578+
* @param nNode node id
579+
* @param sThreadDump thread dump content
575580
*/
576-
private void showThreadDumpResult(int nNode, String sThreadDump)
581+
private void showThreadDumpInVisualVM(int nNode, String sThreadDump)
577582
{
578-
showMessageDialog(getLocalizedText("LBL_state_for_node") + " " + nNode,
579-
sThreadDump, JOptionPane.INFORMATION_MESSAGE, 500, 400, true);
583+
// create a temp file
584+
try
585+
{
586+
String sPrefix = "node-" + nNode + "-";
587+
File fileTempDir = new File(System.getProperty("java.io.tmpdir"));
588+
File fileTemp = File.createTempFile(sPrefix, null, fileTempDir);
589+
boolean fResult1 = fileTemp.setReadable(false);
590+
boolean fResult2 = fileTemp.setWritable(false);
591+
boolean fResult3 = fileTemp.setExecutable(false);
592+
boolean fResult4 = fileTemp.setReadable(true, true);
593+
boolean fResult5 = fileTemp.setWritable(true, true);
594+
boolean fResult6 = fileTemp.setExecutable(true, true);
595+
596+
if (!fResult1 || !fResult2 || !fResult3 || !fResult4 || !fResult5 || !fResult6)
597+
{
598+
throw new RuntimeException("unable to set file permissions for " + fileTemp.getAbsolutePath());
599+
}
600+
601+
try (PrintWriter pw = new PrintWriter(fileTemp, "UTF-8"))
602+
{
603+
pw.write(sThreadDump);
604+
}
605+
606+
final ThreadDumpImpl threadDump = new ThreadDumpImpl(fileTemp, null);
607+
608+
DataSource.EVENT_QUEUE.post(new Runnable()
609+
{
610+
public void run() { DataSourceWindowManager.sharedInstance().openDataSource(threadDump); }
611+
});
612+
}
613+
catch (IOException e)
614+
{
615+
LOGGER.warning(e.getMessage());
616+
}
580617
}
581-
}
618+
}
582619

583620
/**
584621
* Generate a thread dump and save the output in the {@link StringBuilder}.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright (c) 2021, 2024 Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
26+
package com.oracle.coherence.plugin.visualvm.threaddump;
27+
28+
import org.graalvm.visualvm.core.datasource.DataSource;
29+
import org.graalvm.visualvm.threaddump.ThreadDump;
30+
import java.io.File;
31+
32+
/**
33+
* Copied from VisualVM Source.
34+
*
35+
* @author Jiri Sedlacek
36+
*/
37+
public final class ThreadDumpImpl extends ThreadDump
38+
{
39+
40+
public ThreadDumpImpl(File file, DataSource master)
41+
{
42+
super(file, master);
43+
}
44+
45+
void forceViewClosable(boolean closable)
46+
{
47+
getStorage().setCustomProperty( "prop_view_closable", Boolean.toString(closable));
48+
}
49+
}

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,11 @@
122122
<artifactId>org-graalvm-visualvm-charts</artifactId>
123123
<version>${visualvm.version}</version>
124124
</dependency>
125+
<dependency>
126+
<groupId>org.graalvm.visualvm.api</groupId>
127+
<artifactId>org-graalvm-visualvm-threaddump</artifactId>
128+
<version>${visualvm.version}</version>
129+
</dependency>
125130
<dependency>
126131
<groupId>org.graalvm.visualvm.api</groupId>
127132
<artifactId>org-graalvm-visualvm-core</artifactId>

0 commit comments

Comments
 (0)