-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#4: Add (deactivated) functionality to write execAndSave()-dumps to a…
… directory Since this only one piece for the directory support, it is not activated for use in the command line tool or UI.
- Loading branch information
Showing
8 changed files
with
206 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,31 @@ | ||
package jdump.dump; | ||
|
||
import com.sun.tools.attach.VirtualMachineDescriptor; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
import jdump.output.Output; | ||
|
||
/** | ||
* Supports dumping native memory tracks. | ||
*/ | ||
public class NMTDump extends HotspotDump { | ||
private final String outputDirectory; | ||
private final Configuration configuration; | ||
|
||
NMTDump(Configuration configuration) { | ||
this.outputDirectory = configuration.outputDirectory(); | ||
this.configuration = configuration; | ||
} | ||
|
||
@Override | ||
void performFor(VirtualMachineDescriptor vmd) { | ||
System.out.println("Dump NMT for JVM " + vmd.id()); | ||
execAndSave(vmd, new File(filenameFor(vmd)), "jcmd","VM.native_memory"); | ||
var path = Paths.get(filenameFor(vmd)); | ||
try(var lines = Files.lines(path)) { | ||
if (lines.anyMatch(line -> line.contains("Native memory tracking is not enabled"))) { | ||
System.err.println("Native memory tracking is not enabled for JVM " + vmd.id()); | ||
Files.delete(path); | ||
} | ||
} catch (IOException e) { | ||
System.err.println("Failure generating NMT dump:" + e.getMessage()); | ||
var output = Output.using(configuration); | ||
execAndSave(vmd, output.file(filenameFor(vmd)), "jcmd","VM.native_memory"); | ||
if (output.aLineMatches("Native memory tracking is not enabled")) { | ||
System.err.println("Native memory tracking is not enabled for JVM " + vmd.id()); | ||
output.deleteFile(); | ||
} | ||
} | ||
|
||
@Override | ||
String filenameFor(VirtualMachineDescriptor vmd) { | ||
return outputDirectory + File.separator + "jdump-nmt-" + vmd.id() + ".txt"; | ||
return "jdump-nmt-" + vmd.id() + ".txt"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,26 @@ | ||
package jdump.dump; | ||
|
||
import com.sun.tools.attach.VirtualMachineDescriptor; | ||
|
||
import java.io.File; | ||
import jdump.output.Output; | ||
|
||
/** | ||
* Supports dumping threads, i.e. stack traces of all running threads. | ||
*/ | ||
class ThreadDump extends HotspotDump { | ||
private final String outputDirectory; | ||
private final Configuration configuration; | ||
|
||
ThreadDump(Configuration configuration) { | ||
this.outputDirectory = configuration.outputDirectory(); | ||
this.configuration = configuration; | ||
} | ||
|
||
@Override | ||
void performFor(VirtualMachineDescriptor vmd) { | ||
System.out.println("Dumping threads for JVM " + vmd.id()); | ||
execAndSave(vmd, new File(filenameFor(vmd)), "threaddump"); | ||
execAndSave(vmd, Output.using(configuration).file(filenameFor(vmd)), "threaddump"); | ||
} | ||
|
||
@Override | ||
String filenameFor(VirtualMachineDescriptor virtualMachineDescriptor) { | ||
return outputDirectory + File.separator + "jdump-threads-" + virtualMachineDescriptor.id() + ".txt"; | ||
return "jdump-threads-" + virtualMachineDescriptor.id() + ".txt"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package jdump.output; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
|
||
public class DirectoryOutput implements Output { | ||
private final File directory; | ||
private File currentFile; | ||
|
||
DirectoryOutput(String dirname) { | ||
directory = new File(dirname); | ||
if (!directory.mkdirs()) { | ||
throw new RuntimeException("Could not create directory " + dirname); | ||
} | ||
System.out.println("created directory " + directory); | ||
} | ||
|
||
@Override | ||
public Output file(String filename) { | ||
currentFile = new File(directory.getPath() + File.separator + filename); | ||
return this; | ||
} | ||
|
||
@Override | ||
public Path path() { | ||
return currentFile.toPath(); | ||
} | ||
|
||
@Override | ||
public boolean aLineMatches(String matchString) { | ||
try (var lines = Files.lines(path())) { | ||
return lines.anyMatch(line -> line.contains(matchString)); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public void deleteFile() { | ||
currentFile.delete(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package jdump.output; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
|
||
public class FileOutput implements Output { | ||
private File file; | ||
private final String directoryName; | ||
|
||
FileOutput(String directoryName) { | ||
this.directoryName = directoryName; | ||
} | ||
|
||
@Override | ||
public FileOutput file(String filename) { | ||
file = new File(directoryName + File.separator + filename); | ||
return this; | ||
} | ||
|
||
@Override | ||
public Path path() { | ||
return file.toPath(); | ||
} | ||
|
||
@Override | ||
public boolean aLineMatches(String matchString) { | ||
try (var lines = Files.lines(path())) { | ||
return lines.anyMatch(line -> line.contains(matchString)); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public void deleteFile() { | ||
file.delete(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package jdump.output; | ||
|
||
import jdump.dump.Configuration; | ||
|
||
import java.nio.file.Path; | ||
|
||
public interface Output { | ||
Output file(String filename); | ||
Path path(); | ||
boolean aLineMatches(String matchString); | ||
void deleteFile(); | ||
|
||
static Output using(Configuration configuration) { | ||
switch (configuration.outputType()) { | ||
case FILE: | ||
return new FileOutput(configuration.outputDirectory()); | ||
case DIRECTORY: | ||
return new DirectoryOutput(configuration.outputDirectory()); | ||
default: throw new RuntimeException("Internal error: could not handle output type"); | ||
} | ||
} | ||
|
||
enum TYPE { | ||
FILE, DIRECTORY | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package jdump.dump; | ||
|
||
import jdump.output.Output; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Objects; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.TimeoutException; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
public class DirectoryOutputTest { | ||
|
||
@Test | ||
public void testDirectoryOutput() throws ExecutionException, InterruptedException, TimeoutException, IOException { | ||
try (JVM jvm = new JVM("-XX:NativeMemoryTracking=summary")) { | ||
var directoryName = System.getProperty("user.dir") + File.separator + "foo"; | ||
jvm.spawn(); | ||
var dump = new NMTDump(new Configuration.Mutable() | ||
.outputDirectory(directoryName) | ||
.outputType(Output.TYPE.DIRECTORY) | ||
.makeImmutable()); | ||
dump.performFor(jvm.descriptor()); | ||
var path = Path.of(directoryName); | ||
assertTrue(Files.exists(path), "Folder " + directoryName + "was created"); | ||
int filesDeleted = 0; | ||
for (File file : Objects.requireNonNull(path.toFile().listFiles())) { | ||
Files.delete(file.toPath()); | ||
filesDeleted++; | ||
} | ||
assertEquals(1, filesDeleted, "One file was created"); | ||
Files.delete(path); | ||
} | ||
} | ||
} | ||
|