Last active
June 17, 2018 11:38
-
-
Save ka-ka-xyz/9000a886c7a8634506dee852c99c3dd5 to your computer and use it in GitHub Desktop.
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
package jfr_example; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.nio.file.Files; | |
import java.nio.file.Path; | |
import java.util.zip.GZIPInputStream; | |
import java.util.Iterator; | |
import oracle.jrockit.jfr.parser.*; | |
public class Jdk8JfrReader { | |
@SuppressWarnings({ "deprecation", "restriction" }) | |
public Path read(File file) throws IOException { | |
if (!file.exists() || !file.getName().endsWith(".jfr")) { | |
return null; | |
} | |
File jfr = null; | |
if (isGzipCompressed(file)) { | |
jfr = decompress(file); | |
} else { | |
jfr = file; | |
} | |
Path dump = Files.createTempFile("dump_temp_", null); | |
try (Parser parser = new Parser(jfr)) { | |
Iterator<ChunkParser> chunkIter = parser.iterator(); | |
while (chunkIter.hasNext()) { | |
// ChunkParser chunkParser = chunkIter.next(); | |
// for (FLREvent event : chunkParser) { | |
// System.out.println(event); | |
// } | |
} | |
} | |
return dump; | |
} | |
private File decompress(final File srcFile) throws IOException { | |
byte[] buffer = new byte[1024]; | |
File target = File.createTempFile("flightrecorder_", null); | |
target.deleteOnExit(); | |
try (FileOutputStream os = new FileOutputStream(target); | |
GZIPInputStream zipIs = new GZIPInputStream(new FileInputStream(srcFile))) { | |
int bytes; | |
while ((bytes = zipIs.read(buffer)) > 0) { | |
os.write(buffer, 0, bytes); | |
} | |
} | |
return target; | |
} | |
private boolean isGzipCompressed(File file) throws IOException { | |
try (FileInputStream ins = new FileInputStream(file)) { | |
return (byte) ins.read() == (byte) GZIPInputStream.GZIP_MAGIC | |
&& (byte) ins.read() == (byte) (GZIPInputStream.GZIP_MAGIC >> 8); | |
} | |
} | |
} |
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
package jfr_example; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.nio.file.Files; | |
import java.nio.file.Path; | |
import java.util.zip.GZIPInputStream; | |
import jdk.jfr.Recording; | |
import jdk.jfr.consumer.RecordedEvent; | |
import jdk.jfr.consumer.RecordingFile; | |
public class Jdk9JfrReader { | |
public Path read(File file) throws IOException { | |
if (!file.exists() || !file.getName().endsWith(".jfr")) { | |
return null; | |
} | |
File jfr = null; | |
if (isGzipCompressed(file)) { | |
jfr = decompress(file); | |
} else { | |
jfr = file; | |
} | |
Path dump = Files.createTempFile("dump_temp_", null); | |
try (Recording recording = new Recording()) { | |
for (RecordedEvent event : RecordingFile.readAllEvents(jfr.toPath())) { | |
//System.out.println(event); | |
} | |
} | |
return dump; | |
} | |
private File decompress(final File srcFile) throws IOException { | |
byte[] buffer = new byte[1024]; | |
File target = File.createTempFile("flightrecorder_", null); | |
target.deleteOnExit(); | |
try (FileOutputStream os = new FileOutputStream(target); | |
GZIPInputStream zipIs = new GZIPInputStream(new FileInputStream(srcFile))) { | |
int bytes; | |
while ((bytes = zipIs.read(buffer)) > 0) { | |
os.write(buffer, 0, bytes); | |
} | |
} | |
return target; | |
} | |
private boolean isGzipCompressed(File file) throws IOException { | |
try (FileInputStream ins = new FileInputStream(file)) { | |
return (byte) ins.read() == (byte) GZIPInputStream.GZIP_MAGIC | |
&& (byte) ins.read() == (byte) (GZIPInputStream.GZIP_MAGIC >> 8); | |
} | |
} | |
} |
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
package jfr_example; | |
import java.io.File; | |
import java.io.IOException; | |
public class Main { | |
public static void main(String[] args) throws IOException { | |
File file = new File(args[0]); | |
if (!file.exists()) { | |
System.err.println("file not found" + file); | |
System.exit(1); | |
} | |
System.out.println("try to read jfr: " + file); | |
try { | |
Jdk8JfrReader jdk8Reader = new Jdk8JfrReader(); | |
System.out.println("try to read jfr using jfr.jar."); | |
jdk8Reader.read(file); | |
System.out.println("read jfr successfully using jfr.jar."); | |
} catch (Throwable t) { | |
t.printStackTrace(); | |
} | |
System.out.println("-------------------------------"); | |
try { | |
System.out.println("try to read jfr using java JFR api."); | |
Jdk9JfrReader jdk9Reader = new Jdk9JfrReader(); | |
jdk9Reader.read(file); | |
System.out.println("read jfr successfully using java JFR api."); | |
} catch (Throwable t) { | |
t.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment