39
39
import java .util .Map ;
40
40
41
41
import jdk .test .lib .JDKToolLauncher ;
42
+ import jdk .test .lib .JDKToolFinder ;
42
43
import jdk .test .lib .process .OutputAnalyzer ;
43
44
import jdk .test .lib .process .ProcessTools ;
44
45
import jdk .test .lib .hprof .parser .HprofReader ;
47
48
48
49
public class JShellHeapDumpTest {
49
50
50
- protected static Process process ;
51
-
52
- private static long pid ;
51
+ protected static Process jShellProcess ;
53
52
54
53
public static void launch (String expectedMessage , List <String > toolArgs )
55
54
throws IOException {
56
55
57
56
try {
58
57
launchJshell ();
58
+ long jShellPID = jShellProcess .pid ();
59
59
60
- System .out .println ("Starting " + toolArgs .get (0 ) + " against " + pid );
60
+ System .out .println ("Starting " + toolArgs .get (0 ) + " against " + jShellPID );
61
61
JDKToolLauncher launcher = JDKToolLauncher .createUsingTestJDK ("jhsdb" );
62
62
63
63
for (String cmd : toolArgs ) {
64
64
launcher .addToolArg (cmd );
65
65
}
66
66
67
- launcher .addToolArg ("--pid=" + Long .toString (pid ));
67
+ launcher .addToolArg ("--pid=" + Long .toString (jShellPID ));
68
68
69
69
ProcessBuilder processBuilder = new ProcessBuilder (launcher .getCommand ());
70
- processBuilder .redirectError (ProcessBuilder .Redirect .INHERIT );
71
70
OutputAnalyzer output = ProcessTools .executeProcess (processBuilder );
72
- System .out .println ("stdout:" );
71
+ System .out .println ("jhsdb jmap stdout:" );
73
72
System .out .println (output .getStdout ());
74
- System .out .println ("stderr:" );
73
+ System .out .println ("jhsdb jmap stderr:" );
75
74
System .out .println (output .getStderr ());
75
+ System .out .println ("###### End of all output:" );
76
76
output .shouldNotContain ("null" );
77
77
output .shouldHaveExitValue (0 );
78
78
} catch (Exception ex ) {
79
79
throw new RuntimeException ("Test ERROR " + ex , ex );
80
80
} finally {
81
- if (process .isAlive ()) {
82
- process .destroy ();
83
- }
81
+ if (jShellProcess .isAlive ()) {
82
+ System .out .println ("Destroying jshell" );
83
+ jShellProcess .destroy ();
84
+ System .out .println ("Jshell destroyed" );
85
+ } else {
86
+ System .out .println ("Jshell not alive" );
87
+ }
84
88
}
85
89
}
86
90
@@ -102,38 +106,45 @@ public static void printStackTraces(String file) throws IOException {
102
106
}
103
107
104
108
public static void testHeapDump () throws IOException {
105
- File dump = new File ("jhsdb.jmap.heap." +
109
+ File hprofFile = new File ("jhsdb.jmap.heap." +
106
110
System .currentTimeMillis () + ".hprof" );
107
- if (dump .exists ()) {
108
- dump .delete ();
111
+ if (hprofFile .exists ()) {
112
+ hprofFile .delete ();
109
113
}
110
114
111
115
launch ("heap written to" , "jmap" ,
112
- "--binaryheap" , "--dumpfile=" + dump .getAbsolutePath ());
116
+ "--binaryheap" , "--dumpfile=" + hprofFile .getAbsolutePath ());
113
117
114
- assertTrue (dump .exists () && dump .isFile (),
115
- "Could not create dump file " + dump .getAbsolutePath ());
118
+ assertTrue (hprofFile .exists () && hprofFile .isFile (),
119
+ "Could not create dump file " + hprofFile .getAbsolutePath ());
116
120
117
- printStackTraces (dump .getAbsolutePath ());
121
+ printStackTraces (hprofFile .getAbsolutePath ());
118
122
119
- dump .delete ();
123
+ System .out .println ("hprof file size: " + hprofFile .length ());
124
+ hprofFile .delete ();
120
125
}
121
126
122
127
public static void launchJshell () throws IOException {
123
128
System .out .println ("Starting Jshell" );
124
- String jdkPath = System .getProperty ("test.jdk" );
125
- if (jdkPath == null ) {
126
- // we are not under jtreg, try env
127
- Map <String , String > env = System .getenv ();
128
- jdkPath = env .get ("TESTJAVA" );
129
+ long startTime = System .currentTimeMillis ();
130
+ try {
131
+ ProcessBuilder pb = new ProcessBuilder (JDKToolFinder .getTestJDKTool ("jshell" ));
132
+ jShellProcess = ProcessTools .startProcess ("JShell" , pb ,
133
+ s -> { // warm-up predicate
134
+ return s .contains ("Welcome to JShell" );
135
+ });
136
+ } catch (Exception ex ) {
137
+ throw new RuntimeException ("Test ERROR " + ex , ex );
129
138
}
130
- if (jdkPath == null ) {
131
- throw new RuntimeException ("Can't determine jdk path neither test.jdk property no TESTJAVA env are set" );
139
+
140
+ long elapsedTime = System .currentTimeMillis () - startTime ;
141
+ System .out .println ("Jshell Started in " + elapsedTime + "ms" );
142
+
143
+ // Give jshell a chance to fully start up. This makes SA more stable for the jmap dump.
144
+ try {
145
+ Thread .sleep (2000 );
146
+ } catch (Exception e ) {
132
147
}
133
- String osname = System .getProperty ("os.name" );
134
- String jshell = jdkPath + ((osname .startsWith ("window" )) ? "/bin/jshell.exe" : "/bin/jshell" );
135
- process = Runtime .getRuntime ().exec (jshell );
136
- pid = process .pid ();
137
148
}
138
149
139
150
public static void main (String [] args ) throws Exception {
0 commit comments