Java源码示例:jdk.jfr.internal.PlatformRecorder
示例1
public void dump(PlatformRecorder recorder, Recording recording, String name, String filename, Long maxSize, Boolean pathToGcRoots, Instant beginTime, Instant endTime) throws DCmdException {
try (PlatformRecording r = newSnapShot(recorder, recording, pathToGcRoots)) {
r.filter(beginTime, endTime, maxSize);
if (r.getChunks().isEmpty()) {
throw new DCmdException("Dump failed. No data found in the specified interval.");
}
SafePath dumpFile = resolvePath(recording, filename);
// Needed for JVM
Utils.touch(dumpFile.toPath());
r.dumpStopped(new WriteableUserPath(dumpFile.toPath()));
reportOperationComplete("Dumped", name, dumpFile);
} catch (IOException | InvalidPathException e) {
throw new DCmdException("Dump failed. Could not copy recording data. %s", e.getMessage());
}
}
示例2
public void dump(PlatformRecorder recorder, Recording recording, String name, String filename, Long maxSize, Boolean pathToGcRoots, Instant beginTime, Instant endTime) throws DCmdException {
try (PlatformRecording r = newSnapShot(recorder, recording, pathToGcRoots)) {
r.filter(beginTime, endTime, maxSize);
if (r.getChunks().isEmpty()) {
throw new DCmdException("Dump failed. No data found in the specified interval.");
}
SafePath dumpFile = resolvePath(recording, filename);
// Needed for JVM
Utils.touch(dumpFile.toPath());
r.dumpStopped(new WriteableUserPath(dumpFile.toPath()));
reportOperationComplete("Dumped", name, dumpFile);
} catch (IOException | InvalidPathException e) {
throw new DCmdException("Dump failed. Could not copy recording data. %s", e.getMessage());
}
}
示例3
/**
* Returns the Flight Recorder for the platform.
*
* @return a Flight Recorder instance, not {@code null}
*
* @throws IllegalStateException if the platform Flight Recorder couldn't be
* created (for example, if the file repository can't be created or
* accessed)
*
* @throws SecurityException if a security manager exists and the caller does
* not have {@code FlightRecorderPermission("accessFlightRecorder")}
*/
public static FlightRecorder getFlightRecorder() throws IllegalStateException, SecurityException {
synchronized (PlatformRecorder.class) {
Utils.checkAccessFlightRecorder();
JVMSupport.ensureWithIllegalStateException();
if (platformRecorder == null) {
try {
platformRecorder = new FlightRecorder(new PlatformRecorder());
} catch (IllegalStateException ise) {
throw ise;
} catch (Exception e) {
throw new IllegalStateException("Can't create Flight Recorder. " + e.getMessage(), e);
}
// Must be in synchronized block to prevent instance leaking out
// before initialization is done
initialized = true;
Logger.log(JFR, INFO, "Flight Recorder initialized");
Logger.log(JFR, DEBUG, "maxchunksize: " + Options.getMaxChunkSize()+ " bytes");
Logger.log(JFR, DEBUG, "memorysize: " + Options.getMemorySize()+ " bytes");
Logger.log(JFR, DEBUG, "globalbuffersize: " + Options.getGlobalBufferSize()+ " bytes");
Logger.log(JFR, DEBUG, "globalbuffercount: " + Options.getGlobalBufferCount());
Logger.log(JFR, DEBUG, "dumppath: " + Options.getDumpPath());
Logger.log(JFR, DEBUG, "samplethreads: " + Options.getSampleThreads());
Logger.log(JFR, DEBUG, "stackdepth: " + Options.getStackDepth());
Logger.log(JFR, DEBUG, "threadbuffersize: " + Options.getThreadBufferSize());
Logger.log(JFR, LogLevel.INFO, "Created repository " + Repository.getRepository().getRepositoryPath().toString());
PlatformRecorder.notifyRecorderInitialized(platformRecorder);
}
}
return platformRecorder;
}
示例4
public Recording(Map<String, String> settings) {
PlatformRecorder r = FlightRecorder.getFlightRecorder().getInternal();
synchronized (r) {
this.internal = r.newRecording(settings);
this.internal.setRecording(this);
if (internal.getRecording() != this) {
throw new InternalError("Internal recording not properly setup");
}
}
}
示例5
/**
* Returns the Flight Recorder for the platform.
*
* @return a Flight Recorder instance, not {@code null}
*
* @throws IllegalStateException if Flight Recorder can't be created (for
* example, if the Java Virtual Machine (JVM) lacks Flight Recorder
* support, or if the file repository can't be created or accessed)
*
* @throws SecurityException if a security manager exists and the caller does
* not have {@code FlightRecorderPermission("accessFlightRecorder")}
*/
public static FlightRecorder getFlightRecorder() throws IllegalStateException, SecurityException {
synchronized (PlatformRecorder.class) {
Utils.checkAccessFlightRecorder();
JVMSupport.ensureWithIllegalStateException();
if (platformRecorder == null) {
try {
platformRecorder = new FlightRecorder(new PlatformRecorder());
} catch (IllegalStateException ise) {
throw ise;
} catch (Exception e) {
throw new IllegalStateException("Can't create Flight Recorder. " + e.getMessage(), e);
}
// Must be in synchronized block to prevent instance leaking out
// before initialization is done
initialized = true;
Logger.log(JFR, INFO, "Flight Recorder initialized");
Logger.log(JFR, DEBUG, "maxchunksize: " + Options.getMaxChunkSize()+ " bytes");
Logger.log(JFR, DEBUG, "memorysize: " + Options.getMemorySize()+ " bytes");
Logger.log(JFR, DEBUG, "globalbuffersize: " + Options.getGlobalBufferSize()+ " bytes");
Logger.log(JFR, DEBUG, "globalbuffercount: " + Options.getGlobalBufferCount());
Logger.log(JFR, DEBUG, "dumppath: " + Options.getDumpPath());
Logger.log(JFR, DEBUG, "samplethreads: " + Options.getSampleThreads());
Logger.log(JFR, DEBUG, "stackdepth: " + Options.getStackDepth());
Logger.log(JFR, DEBUG, "threadbuffersize: " + Options.getThreadBufferSize());
Logger.log(JFR, LogLevel.INFO, "Created repository " + Repository.getRepository().getRepositoryPath().toString());
PlatformRecorder.notifyRecorderInitialized(platformRecorder);
}
}
return platformRecorder;
}
示例6
private PlatformRecording newSnapShot(PlatformRecorder recorder, Recording recording, Boolean pathToGcRoots) throws DCmdException, IOException {
if (recording == null) {
// Operate on all recordings
PlatformRecording snapshot = recorder.newTemporaryRecording();
recorder.fillWithRecordedData(snapshot, pathToGcRoots);
return snapshot;
}
PlatformRecording pr = PrivateAccess.getInstance().getPlatformRecording(recording);
return pr.newSnapshotClone("Dumped by user", pathToGcRoots);
}
示例7
public Recording(Map<String, String> settings) {
PlatformRecorder r = FlightRecorder.getFlightRecorder().getInternal();
synchronized (r) {
this.internal = r.newRecording(settings);
this.internal.setRecording(this);
if (internal.getRecording() != this) {
throw new InternalError("Internal recording not properly setup");
}
}
}
示例8
/**
* Returns the Flight Recorder for the platform.
*
* @return a Flight Recorder instance, not {@code null}
*
* @throws IllegalStateException if Flight Recorder can't be created (for
* example, if the Java Virtual Machine (JVM) lacks Flight Recorder
* support, or if the file repository can't be created or accessed)
*
* @throws SecurityException if a security manager exists and the caller does
* not have {@code FlightRecorderPermission("accessFlightRecorder")}
*/
public static FlightRecorder getFlightRecorder() throws IllegalStateException, SecurityException {
synchronized (PlatformRecorder.class) {
Utils.checkAccessFlightRecorder();
JVMSupport.ensureWithIllegalStateException();
if (platformRecorder == null) {
try {
platformRecorder = new FlightRecorder(new PlatformRecorder());
} catch (IllegalStateException ise) {
throw ise;
} catch (Exception e) {
throw new IllegalStateException("Can't create Flight Recorder. " + e.getMessage(), e);
}
// Must be in synchronized block to prevent instance leaking out
// before initialization is done
initialized = true;
Logger.log(JFR, INFO, "Flight Recorder initialized");
Logger.log(JFR, DEBUG, "maxchunksize: " + Options.getMaxChunkSize()+ " bytes");
Logger.log(JFR, DEBUG, "memorysize: " + Options.getMemorySize()+ " bytes");
Logger.log(JFR, DEBUG, "globalbuffersize: " + Options.getGlobalBufferSize()+ " bytes");
Logger.log(JFR, DEBUG, "globalbuffercount: " + Options.getGlobalBufferCount());
Logger.log(JFR, DEBUG, "dumppath: " + Options.getDumpPath());
Logger.log(JFR, DEBUG, "samplethreads: " + Options.getSampleThreads());
Logger.log(JFR, DEBUG, "stackdepth: " + Options.getStackDepth());
Logger.log(JFR, DEBUG, "threadbuffersize: " + Options.getThreadBufferSize());
Logger.log(JFR, LogLevel.INFO, "Created repository " + Repository.getRepository().getRepositoryPath().toString());
PlatformRecorder.notifyRecorderInitialized(platformRecorder);
}
}
return platformRecorder;
}
示例9
private PlatformRecording newSnapShot(PlatformRecorder recorder, Recording recording, Boolean pathToGcRoots) throws DCmdException, IOException {
if (recording == null) {
// Operate on all recordings
PlatformRecording snapshot = recorder.newTemporaryRecording();
recorder.fillWithRecordedData(snapshot, pathToGcRoots);
return snapshot;
}
PlatformRecording pr = PrivateAccess.getInstance().getPlatformRecording(recording);
return pr.newSnapshotClone("Dumped by user", pathToGcRoots);
}
示例10
private FlightRecorder(PlatformRecorder internal) {
this.internal = internal;
}
示例11
private FlightRecorder(PlatformRecorder internal) {
this.internal = internal;
}
示例12
PlatformRecorder getInternal() {
return internal;
}
示例13
@Override
public PlatformRecorder getPlatformRecorder() {
return FlightRecorder.getFlightRecorder().getInternal();
}
示例14
/**
* Execute JFR.dump.
*
* @param name name or id of the recording to dump, or <code>null</code> to dump everything
*
* @param filename file path where recording should be written, not null
* @param maxAge how far back in time to dump, may be null
* @param maxSize how far back in size to dump data from, may be null
* @param begin point in time to dump data from, may be null
* @param end point in time to dump data to, may be null
* @param pathToGcRoots if Java heap should be swept for reference chains
*
* @return result output
*
* @throws DCmdException if the dump could not be completed
*/
public String execute(String name, String filename, Long maxAge, Long maxSize, String begin, String end, Boolean pathToGcRoots) throws DCmdException {
if (LogTag.JFR_DCMD.shouldLog(LogLevel.DEBUG)) {
Logger.log(LogTag.JFR_DCMD, LogLevel.DEBUG,
"Executing DCmdDump: name=" + name +
", filename=" + filename +
", maxage=" + maxAge +
", maxsize=" + maxSize +
", begin=" + begin +
", end" + end +
", path-to-gc-roots=" + pathToGcRoots);
}
if (FlightRecorder.getFlightRecorder().getRecordings().isEmpty()) {
throw new DCmdException("No recordings to dump from. Use JFR.start to start a recording.");
}
if (maxAge != null) {
if (end != null || begin != null) {
throw new DCmdException("Dump failed, maxage can't be combined with begin or end.");
}
if (maxAge < 0) {
throw new DCmdException("Dump failed, maxage can't be negative.");
}
if (maxAge == 0) {
maxAge = Long.MAX_VALUE / 2; // a high value that won't overflow
}
}
if (maxSize!= null) {
if (maxSize < 0) {
throw new DCmdException("Dump failed, maxsize can't be negative.");
}
if (maxSize == 0) {
maxSize = Long.MAX_VALUE / 2; // a high value that won't overflow
}
}
Instant beginTime = parseTime(begin, "begin");
Instant endTime = parseTime(end, "end");
if (beginTime != null && endTime != null) {
if (endTime.isBefore(beginTime)) {
throw new DCmdException("Dump failed, begin must preceed end.");
}
}
Duration duration = null;
if (maxAge != null) {
duration = Duration.ofNanos(maxAge);
beginTime = Instant.now().minus(duration);
}
Recording recording = null;
if (name != null) {
recording = findRecording(name);
}
PlatformRecorder recorder = PrivateAccess.getInstance().getPlatformRecorder();
synchronized (recorder) {
dump(recorder, recording, name, filename, maxSize, pathToGcRoots, beginTime, endTime);
}
return getResult();
}
示例15
private FlightRecorder(PlatformRecorder internal) {
this.internal = internal;
}
示例16
PlatformRecorder getInternal() {
return internal;
}
示例17
@Override
public PlatformRecorder getPlatformRecorder() {
return FlightRecorder.getFlightRecorder().getInternal();
}
示例18
/**
* Execute JFR.dump.
*
* @param name name or id of the recording to dump, or <code>null</code> to dump everything
*
* @param filename file path where recording should be written, not null
* @param maxAge how far back in time to dump, may be null
* @param maxSize how far back in size to dump data from, may be null
* @param begin point in time to dump data from, may be null
* @param end point in time to dump data to, may be null
* @param pathToGcRoots if Java heap should be swept for reference chains
*
* @return result output
*
* @throws DCmdException if the dump could not be completed
*/
public String execute(String name, String filename, Long maxAge, Long maxSize, String begin, String end, Boolean pathToGcRoots) throws DCmdException {
if (Logger.shouldLog(LogTag.JFR_DCMD, LogLevel.DEBUG)) {
Logger.log(LogTag.JFR_DCMD, LogLevel.DEBUG,
"Executing DCmdDump: name=" + name +
", filename=" + filename +
", maxage=" + maxAge +
", maxsize=" + maxSize +
", begin=" + begin +
", end" + end +
", path-to-gc-roots=" + pathToGcRoots);
}
if (FlightRecorder.getFlightRecorder().getRecordings().isEmpty()) {
throw new DCmdException("No recordings to dump from. Use JFR.start to start a recording.");
}
if (maxAge != null) {
if (end != null || begin != null) {
throw new DCmdException("Dump failed, maxage can't be combined with begin or end.");
}
if (maxAge < 0) {
throw new DCmdException("Dump failed, maxage can't be negative.");
}
if (maxAge == 0) {
maxAge = Long.MAX_VALUE / 2; // a high value that won't overflow
}
}
if (maxSize!= null) {
if (maxSize < 0) {
throw new DCmdException("Dump failed, maxsize can't be negative.");
}
if (maxSize == 0) {
maxSize = Long.MAX_VALUE / 2; // a high value that won't overflow
}
}
Instant beginTime = parseTime(begin, "begin");
Instant endTime = parseTime(end, "end");
if (beginTime != null && endTime != null) {
if (endTime.isBefore(beginTime)) {
throw new DCmdException("Dump failed, begin must preceed end.");
}
}
Duration duration = null;
if (maxAge != null) {
duration = Duration.ofNanos(maxAge);
beginTime = Instant.now().minus(duration);
}
Recording recording = null;
if (name != null) {
recording = findRecording(name);
}
PlatformRecorder recorder = PrivateAccess.getInstance().getPlatformRecorder();
synchronized (recorder) {
dump(recorder, recording, name, filename, maxSize, pathToGcRoots, beginTime, endTime);
}
return getResult();
}
示例19
/**
* Adds a recorder listener and captures the {@code AccessControlContext} to
* use when invoking the listener.
* <p>
* If Flight Recorder is already initialized when the listener is added, the the method
* {@link FlightRecorderListener#recorderInitialized(FlightRecorder)} method is
* invoked before returning from this method.
*
* @param changeListener the listener to add, not {@code null}
*
* @throws SecurityException if a security manager exists and the caller
* does not have
* {@code FlightRecorderPermission("accessFlightRecorder")}
*/
public static void addListener(FlightRecorderListener changeListener) {
Objects.requireNonNull(changeListener);
Utils.checkAccessFlightRecorder();
if (JVMSupport.isNotAvailable()) {
return;
}
PlatformRecorder.addListener(changeListener);
}
示例20
/**
* Removes a recorder listener.
* <p>
* If the same listener is added multiple times, only one instance is
* removed.
*
* @param changeListener listener to remove, not {@code null}
*
* @throws SecurityException if a security manager exists and the caller
* does not have
* {@code FlightRecorderPermission("accessFlightRecorder")}
*
* @return {@code true}, if the listener could be removed, {@code false}
* otherwise
*/
public static boolean removeListener(FlightRecorderListener changeListener) {
Objects.requireNonNull(changeListener);
Utils.checkAccessFlightRecorder();
if (JVMSupport.isNotAvailable()) {
return false;
}
return PlatformRecorder.removeListener(changeListener);
}
示例21
/**
* Adds a recorder listener and captures the {@code AccessControlContext} to
* use when invoking the listener.
* <p>
* If Flight Recorder is already initialized when the listener is added, then the method
* {@link FlightRecorderListener#recorderInitialized(FlightRecorder)} method is
* invoked before returning from this method.
*
* @param changeListener the listener to add, not {@code null}
*
* @throws SecurityException if a security manager exists and the caller
* does not have
* {@code FlightRecorderPermission("accessFlightRecorder")}
*/
public static void addListener(FlightRecorderListener changeListener) {
Objects.requireNonNull(changeListener);
Utils.checkAccessFlightRecorder();
if (JVMSupport.isNotAvailable()) {
return;
}
PlatformRecorder.addListener(changeListener);
}
示例22
/**
* Removes a recorder listener.
* <p>
* If the same listener is added multiple times, only one instance is
* removed.
*
* @param changeListener listener to remove, not {@code null}
*
* @throws SecurityException if a security manager exists and the caller
* does not have
* {@code FlightRecorderPermission("accessFlightRecorder")}
*
* @return {@code true}, if the listener could be removed, {@code false}
* otherwise
*/
public static boolean removeListener(FlightRecorderListener changeListener) {
Objects.requireNonNull(changeListener);
Utils.checkAccessFlightRecorder();
if (JVMSupport.isNotAvailable()) {
return false;
}
return PlatformRecorder.removeListener(changeListener);
}
示例23
/**
* Adds a recorder listener and captures the {@code AccessControlContext} to
* use when invoking the listener.
* <p>
* If Flight Recorder is already initialized when the listener is added, then the method
* {@link FlightRecorderListener#recorderInitialized(FlightRecorder)} method is
* invoked before returning from this method.
*
* @param changeListener the listener to add, not {@code null}
*
* @throws SecurityException if a security manager exists and the caller
* does not have
* {@code FlightRecorderPermission("accessFlightRecorder")}
*/
public static void addListener(FlightRecorderListener changeListener) {
Objects.requireNonNull(changeListener);
Utils.checkAccessFlightRecorder();
if (JVMSupport.isNotAvailable()) {
return;
}
PlatformRecorder.addListener(changeListener);
}
示例24
/**
* Removes a recorder listener.
* <p>
* If the same listener is added multiple times, only one instance is
* removed.
*
* @param changeListener listener to remove, not {@code null}
*
* @throws SecurityException if a security manager exists and the caller
* does not have
* {@code FlightRecorderPermission("accessFlightRecorder")}
*
* @return {@code true}, if the listener could be removed, {@code false}
* otherwise
*/
public static boolean removeListener(FlightRecorderListener changeListener) {
Objects.requireNonNull(changeListener);
Utils.checkAccessFlightRecorder();
if (JVMSupport.isNotAvailable()) {
return false;
}
return PlatformRecorder.removeListener(changeListener);
}