8264001: JFR: Modernize implementation

Reviewed-by: mgronlun
This commit is contained in:
Erik Gahlin 2021-03-24 09:54:29 +00:00
parent fad8484058
commit ae9af57bf6
57 changed files with 239 additions and 322 deletions

View file

@ -100,7 +100,7 @@ public final class AnnotationElement {
} }
checkType(Utils.unboxType(valueType)); checkType(Utils.unboxType(valueType));
} }
this.annotationValues = Utils.smallUnmodifiable(objects); this.annotationValues = List.copyOf(objects);
this.inBootClassLoader = boot; this.inBootClassLoader = boot;
} }
@ -203,7 +203,7 @@ public final class AnnotationElement {
} }
v.add(object); v.add(object);
} }
this.annotationValues = Utils.smallUnmodifiable(v); this.annotationValues = List.copyOf(v);
this.inBootClassLoader = annotationType.getClassLoader() == null; this.inBootClassLoader = annotationType.getClassLoader() == null;
} }

View file

@ -30,8 +30,6 @@ import java.io.Reader;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.text.ParseException; import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -190,8 +188,8 @@ public final class Configuration {
*/ */
public static List<Configuration> getConfigurations() { public static List<Configuration> getConfigurations() {
if (JVMSupport.isNotAvailable()) { if (JVMSupport.isNotAvailable()) {
return new ArrayList<>(); return List.of();
} }
return Collections.unmodifiableList(JFC.getConfigurations()); return List.copyOf(JFC.getConfigurations());
} }
} }

View file

@ -97,6 +97,7 @@ abstract public class Event extends jdk.internal.event.Event {
/** /**
* Starts the timing of this event. * Starts the timing of this event.
*/ */
@Override
final public void begin() { final public void begin() {
} }
@ -105,6 +106,7 @@ abstract public class Event extends jdk.internal.event.Event {
* *
* The {@code end} method must be invoked after the {@code begin} method. * The {@code end} method must be invoked after the {@code begin} method.
*/ */
@Override
final public void end() { final public void end() {
} }
@ -116,6 +118,7 @@ abstract public class Event extends jdk.internal.event.Event {
* not end with an explicit invocation of the {@code end} method, then the event * not end with an explicit invocation of the {@code end} method, then the event
* ends when the {@code commit} method is invoked. * ends when the {@code commit} method is invoked.
*/ */
@Override
final public void commit() { final public void commit() {
} }
@ -126,6 +129,7 @@ abstract public class Event extends jdk.internal.event.Event {
* *
* @return {@code true} if event is enabled, {@code false} otherwise * @return {@code true} if event is enabled, {@code false} otherwise
*/ */
@Override
final public boolean isEnabled() { final public boolean isEnabled() {
return false; return false;
} }
@ -139,6 +143,7 @@ abstract public class Event extends jdk.internal.event.Event {
* @return {@code true} if the event can be written to the Flight Recorder * @return {@code true} if the event can be written to the Flight Recorder
* system, {@code false} otherwise * system, {@code false} otherwise
*/ */
@Override
final public boolean shouldCommit() { final public boolean shouldCommit() {
return false; return false;
} }
@ -164,6 +169,7 @@ abstract public class Event extends jdk.internal.event.Event {
* @see EventType#getFields() * @see EventType#getFields()
* @see EventFactory * @see EventFactory
*/ */
@Override
final public void set(int index, Object value) { final public void set(int index, Object value) {
} }
} }

View file

@ -26,7 +26,6 @@
package jdk.jfr; package jdk.jfr;
import java.lang.annotation.Annotation; import java.lang.annotation.Annotation;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
@ -45,8 +44,8 @@ import jdk.jfr.internal.Utils;
* @since 9 * @since 9
*/ */
public final class EventType { public final class EventType {
private static final List<String> UNCATEGORIZED = List.of("Uncategorized");
private final PlatformEventType platformEventType; private final PlatformEventType platformEventType;
private final List<String> UNCATEGORIZED = Collections.singletonList("Uncategorized");
private Map<String, ValueDescriptor> cache; // create lazy to avoid memory overhead private Map<String, ValueDescriptor> cache; // create lazy to avoid memory overhead
// helper constructor // helper constructor
EventType(PlatformEventType platformEventType) { EventType(PlatformEventType platformEventType) {
@ -79,11 +78,11 @@ public final class EventType {
Objects.requireNonNull(name); Objects.requireNonNull(name);
if (cache == null) { if (cache == null) {
List<ValueDescriptor> fields = getFields(); List<ValueDescriptor> fields = getFields();
Map<String, ValueDescriptor> newCache = new LinkedHashMap<String, ValueDescriptor>(fields.size()); Map<String, ValueDescriptor> newCache = new LinkedHashMap<>(fields.size());
for (ValueDescriptor v :fields) { for (ValueDescriptor v :fields) {
newCache.put(v.getName(), v); newCache.put(v.getName(), v);
} }
cache = newCache; cache = Map.copyOf(newCache);
} }
ValueDescriptor result = cache.get(name); ValueDescriptor result = cache.get(name);
if (result == null) { if (result == null) {
@ -230,7 +229,7 @@ public final class EventType {
if (c == null) { if (c == null) {
return UNCATEGORIZED; return UNCATEGORIZED;
} }
return Collections.unmodifiableList(Arrays.asList(c.value())); return List.of(c.value());
} }
// package private // package private

View file

@ -38,7 +38,6 @@ import java.util.Objects;
import jdk.jfr.internal.JVM; import jdk.jfr.internal.JVM;
import jdk.jfr.internal.JVMSupport; import jdk.jfr.internal.JVMSupport;
import jdk.jfr.internal.LogLevel;
import jdk.jfr.internal.Logger; import jdk.jfr.internal.Logger;
import jdk.jfr.internal.MetadataRepository; import jdk.jfr.internal.MetadataRepository;
import jdk.jfr.internal.Options; import jdk.jfr.internal.Options;

View file

@ -26,7 +26,6 @@
package jdk.jfr; package jdk.jfr;
import java.security.AccessControlContext; import java.security.AccessControlContext;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
@ -109,17 +108,17 @@ public final class FlightRecorderPermission extends java.security.BasicPermissio
@Override @Override
public Type getType(Object o) { public Type getType(Object o) {
if (o instanceof AnnotationElement) { if (o instanceof AnnotationElement ae) {
return ((AnnotationElement) o).getType(); return ae.getType();
} }
if (o instanceof EventType) { if (o instanceof EventType et) {
return ((EventType) o).getType(); return et.getType();
} }
if (o instanceof ValueDescriptor) { if (o instanceof ValueDescriptor vd) {
return ((ValueDescriptor) o).getType(); return vd.getType();
} }
if (o instanceof SettingDescriptor) { if (o instanceof SettingDescriptor sd) {
return ((SettingDescriptor) o).getType(); return sd.getType();
} }
throw new Error("Unknown type " + o.getClass()); throw new Error("Unknown type " + o.getClass());
} }
@ -176,7 +175,7 @@ public final class FlightRecorderPermission extends java.security.BasicPermissio
@Override @Override
public ValueDescriptor newValueDescriptor(Class<?> type, String name) { public ValueDescriptor newValueDescriptor(Class<?> type, String name) {
return new ValueDescriptor(type, name, Collections.emptyList(), true); return new ValueDescriptor(type, name, List.of(), true);
} }
@Override @Override

View file

@ -135,7 +135,7 @@ public final class Recording implements Closeable {
* FlightRecorderPermission "accessFlightRecorder" is not set. * FlightRecorderPermission "accessFlightRecorder" is not set.
*/ */
public Recording() { public Recording() {
this(new HashMap<String, String>()); this(Map.of());
} }
/** /**

View file

@ -26,7 +26,6 @@
package jdk.jfr; package jdk.jfr;
import java.lang.annotation.Annotation; import java.lang.annotation.Annotation;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
@ -196,7 +195,7 @@ public final class SettingDescriptor {
* @return a list of annotations, not {@code null} * @return a list of annotations, not {@code null}
*/ */
public List<AnnotationElement> getAnnotationElements() { public List<AnnotationElement> getAnnotationElements() {
return Collections.unmodifiableList(annotationConstruct.getUnmodifiableAnnotationElements()); return annotationConstruct.getUnmodifiableAnnotationElements();
} }
/** /**

View file

@ -26,7 +26,6 @@
package jdk.jfr; package jdk.jfr;
import java.lang.annotation.Annotation; import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
@ -131,7 +130,7 @@ public final class ValueDescriptor {
* doesn't have {@code FlightRecorderPermission("registerEvent")} * doesn't have {@code FlightRecorderPermission("registerEvent")}
*/ */
public ValueDescriptor(Class<?> type, String name, List<AnnotationElement> annotations) { public ValueDescriptor(Class<?> type, String name, List<AnnotationElement> annotations) {
this(type, name, new ArrayList<>(annotations), false); this(type, name, List.copyOf(annotations), false);
} }
@ -289,7 +288,7 @@ public final class ValueDescriptor {
*/ */
public List<ValueDescriptor> getFields() { public List<ValueDescriptor> getFields() {
if (type.isSimpleType()) { if (type.isSimpleType()) {
return Collections.emptyList(); return List.of();
} }
return type.getFields(); return type.getFields();
} }

View file

@ -263,6 +263,7 @@ public interface EventStream extends AutoCloseable {
* <p> * <p>
* Closing a previously closed stream has no effect. * Closing a previously closed stream has no effect.
*/ */
@Override
void close(); void close();
/** /**

View file

@ -112,7 +112,7 @@ public final class MetadataEvent {
added.add(eventType); added.add(eventType);
} }
} }
this.removed = Collections.unmodifiableList(new ArrayList<>(previousSet.values())); this.removed = List.copyOf(previousSet.values());
this.added = Collections.unmodifiableList(added); this.added = List.copyOf(added);
} }
} }

View file

@ -120,6 +120,7 @@ public final class RecordedEvent extends RecordedObject {
return objectContext.fields; return objectContext.fields;
} }
@Override
final Object objectAt(int index) { final Object objectAt(int index) {
if (index == 0) { if (index == 0) {
return startTimeTicks; return startTimeTicks;

View file

@ -34,7 +34,6 @@ import java.time.OffsetDateTime;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.function.Consumer;
import jdk.jfr.Configuration; import jdk.jfr.Configuration;
import jdk.jfr.EventType; import jdk.jfr.EventType;
@ -43,7 +42,6 @@ import jdk.jfr.Timestamp;
import jdk.jfr.ValueDescriptor; import jdk.jfr.ValueDescriptor;
import jdk.jfr.internal.PrivateAccess; import jdk.jfr.internal.PrivateAccess;
import jdk.jfr.internal.Type; import jdk.jfr.internal.Type;
import jdk.jfr.internal.consumer.EventDirectoryStream;
import jdk.jfr.internal.consumer.JdkJfrConsumer; import jdk.jfr.internal.consumer.JdkJfrConsumer;
import jdk.jfr.internal.consumer.ObjectContext; import jdk.jfr.internal.consumer.ObjectContext;
import jdk.jfr.internal.consumer.ObjectFactory; import jdk.jfr.internal.consumer.ObjectFactory;
@ -63,10 +61,12 @@ public class RecordedObject {
static{ static{
JdkJfrConsumer access = new JdkJfrConsumer() { JdkJfrConsumer access = new JdkJfrConsumer() {
@Override
public List<Type> readTypes(RecordingFile file) throws IOException { public List<Type> readTypes(RecordingFile file) throws IOException {
return file.readTypes(); return file.readTypes();
} }
@Override
public boolean isLastEventInChunk(RecordingFile file) { public boolean isLastEventInChunk(RecordingFile file) {
return file.isLastEventInChunk(); return file.isLastEventInChunk();
} }
@ -430,8 +430,8 @@ public class RecordedObject {
*/ */
public final boolean getBoolean(String name) { public final boolean getBoolean(String name) {
Object o = getValue(name); Object o = getValue(name);
if (o instanceof Boolean) { if (o instanceof Boolean b) {
return ((Boolean) o).booleanValue(); return b;
} }
throw newIllegalArgumentException(name, "boolean"); throw newIllegalArgumentException(name, "boolean");
} }
@ -457,8 +457,8 @@ public class RecordedObject {
*/ */
public final byte getByte(String name) { public final byte getByte(String name) {
Object o = getValue(name); Object o = getValue(name);
if (o instanceof Byte) { if (o instanceof Byte b) {
return ((Byte) o).byteValue(); return b;
} }
throw newIllegalArgumentException(name, "byte"); throw newIllegalArgumentException(name, "byte");
} }
@ -484,8 +484,8 @@ public class RecordedObject {
*/ */
public final char getChar(String name) { public final char getChar(String name) {
Object o = getValue(name); Object o = getValue(name);
if (o instanceof Character) { if (o instanceof Character c) {
return ((Character) o).charValue(); return c;
} }
throw newIllegalArgumentException(name, "char"); throw newIllegalArgumentException(name, "char");
@ -519,19 +519,19 @@ public class RecordedObject {
*/ */
public final short getShort(String name) { public final short getShort(String name) {
Object o = getValue(name, true); Object o = getValue(name, true);
if (o instanceof Short) { if (o instanceof Short s) {
return ((Short) o).shortValue(); return s;
} }
if (o instanceof Byte) { if (o instanceof Byte b) {
return ((Byte) o).byteValue(); return b;
} }
if (o instanceof UnsignedValue) { if (o instanceof UnsignedValue unsigned) {
Object u = ((UnsignedValue) o).value(); Object u = unsigned.value();
if (u instanceof Short) { if (u instanceof Short s) {
return ((Short) u).shortValue(); return s;
} }
if (u instanceof Byte) { if (u instanceof Byte b) {
return (short) Byte.toUnsignedInt(((Byte) u)); return (short) Byte.toUnsignedInt(b);
} }
} }
throw newIllegalArgumentException(name, "short"); throw newIllegalArgumentException(name, "short");
@ -566,28 +566,28 @@ public class RecordedObject {
*/ */
public final int getInt(String name) { public final int getInt(String name) {
Object o = getValue(name, true); Object o = getValue(name, true);
if (o instanceof Integer) { if (o instanceof Integer i) {
return ((Integer) o).intValue(); return i;
} }
if (o instanceof Short) { if (o instanceof Short s) {
return ((Short) o).intValue(); return s;
} }
if (o instanceof Character) { if (o instanceof Character c) {
return ((Character) o).charValue(); return c;
} }
if (o instanceof Byte) { if (o instanceof Byte b) {
return ((Byte) o).intValue(); return b;
} }
if (o instanceof UnsignedValue) { if (o instanceof UnsignedValue unsigned) {
Object u = ((UnsignedValue) o).value(); Object u = unsigned.value();
if (u instanceof Integer) { if (u instanceof Integer i) {
return ((Integer) u).intValue(); return i;
} }
if (u instanceof Short) { if (u instanceof Short s) {
return Short.toUnsignedInt(((Short) u)); return Short.toUnsignedInt(s);
} }
if (u instanceof Byte) { if (u instanceof Byte b) {
return Byte.toUnsignedInt(((Byte) u)); return Byte.toUnsignedInt(b);
} }
} }
throw newIllegalArgumentException(name, "int"); throw newIllegalArgumentException(name, "int");
@ -619,23 +619,23 @@ public class RecordedObject {
*/ */
public final float getFloat(String name) { public final float getFloat(String name) {
Object o = getValue(name); Object o = getValue(name);
if (o instanceof Float) { if (o instanceof Float f) {
return ((Float) o).floatValue(); return f;
} }
if (o instanceof Long) { if (o instanceof Long l) {
return ((Long) o).floatValue(); return l;
} }
if (o instanceof Integer) { if (o instanceof Integer i) {
return ((Integer) o).floatValue(); return i;
} }
if (o instanceof Short) { if (o instanceof Short s) {
return ((Short) o).floatValue(); return s;
} }
if (o instanceof Byte) { if (o instanceof Byte b) {
return ((Byte) o).byteValue(); return b;
} }
if (o instanceof Character) { if (o instanceof Character c) {
return ((Character) o).charValue(); return c;
} }
throw newIllegalArgumentException(name, "float"); throw newIllegalArgumentException(name, "float");
} }
@ -669,31 +669,31 @@ public class RecordedObject {
*/ */
public final long getLong(String name) { public final long getLong(String name) {
Object o = getValue(name, true); Object o = getValue(name, true);
if (o instanceof Long) { if (o instanceof Long l) {
return ((Long) o).longValue(); return l;
} }
if (o instanceof Integer) { if (o instanceof Integer i) {
return ((Integer) o).longValue(); return i;
} }
if (o instanceof Short) { if (o instanceof Short s) {
return ((Short) o).longValue(); return s;
} }
if (o instanceof Character) { if (o instanceof Character c) {
return ((Character) o).charValue(); return c;
} }
if (o instanceof Byte) { if (o instanceof Byte b) {
return ((Byte) o).longValue(); return b.longValue();
} }
if (o instanceof UnsignedValue) { if (o instanceof UnsignedValue unsigned) {
Object u = ((UnsignedValue) o).value(); Object u = unsigned.value();
if (u instanceof Integer) { if (u instanceof Integer i) {
return Integer.toUnsignedLong(((Integer) u)); return Integer.toUnsignedLong(i);
} }
if (u instanceof Short) { if (u instanceof Short s) {
return Short.toUnsignedLong(((Short) u)); return Short.toUnsignedLong(s);
} }
if (u instanceof Byte) { if (u instanceof Byte b) {
return Byte.toUnsignedLong(((Byte) u)); return Byte.toUnsignedLong(b);
} }
} }
throw newIllegalArgumentException(name, "long"); throw newIllegalArgumentException(name, "long");
@ -725,26 +725,26 @@ public class RecordedObject {
*/ */
public final double getDouble(String name) { public final double getDouble(String name) {
Object o = getValue(name); Object o = getValue(name);
if (o instanceof Double) { if (o instanceof Double d) {
return ((Double) o).doubleValue(); return d.doubleValue();
} }
if (o instanceof Float) { if (o instanceof Float f) {
return ((Float) o).doubleValue(); return f.doubleValue();
} }
if (o instanceof Long) { if (o instanceof Long l) {
return ((Long) o).doubleValue(); return l.doubleValue();
} }
if (o instanceof Integer) { if (o instanceof Integer i) {
return ((Integer) o).doubleValue(); return i.doubleValue();
} }
if (o instanceof Short) { if (o instanceof Short s) {
return ((Short) o).doubleValue(); return s.doubleValue();
} }
if (o instanceof Byte) { if (o instanceof Byte b) {
return ((Byte) o).byteValue(); return b.doubleValue();
} }
if (o instanceof Character) { if (o instanceof Character c) {
return ((Character) o).charValue(); return c;
} }
throw newIllegalArgumentException(name, "double"); throw newIllegalArgumentException(name, "double");
} }
@ -797,31 +797,31 @@ public class RecordedObject {
*/ */
public final Duration getDuration(String name) { public final Duration getDuration(String name) {
Object o = getValue(name); Object o = getValue(name);
if (o instanceof Long) { if (o instanceof Long l) {
return getDuration(((Long) o).longValue(), name); return getDuration(l, name);
} }
if (o instanceof Integer) { if (o instanceof Integer i) {
return getDuration(((Integer) o).longValue(), name); return getDuration(i, name);
} }
if (o instanceof Short) { if (o instanceof Short s) {
return getDuration(((Short) o).longValue(), name); return getDuration(s, name);
} }
if (o instanceof Character) { if (o instanceof Character c) {
return getDuration(((Character) o).charValue(), name); return getDuration(c, name);
} }
if (o instanceof Byte) { if (o instanceof Byte b) {
return getDuration(((Byte) o).longValue(), name); return getDuration(b, name);
} }
if (o instanceof UnsignedValue) { if (o instanceof UnsignedValue unsigned) {
Object u = ((UnsignedValue) o).value(); Object u = unsigned.value();
if (u instanceof Integer) { if (u instanceof Integer i) {
return getDuration(Integer.toUnsignedLong((Integer) u), name); return getDuration(Integer.toUnsignedLong(i), name);
} }
if (u instanceof Short) { if (u instanceof Short s) {
return getDuration(Short.toUnsignedLong((Short) u), name); return getDuration(Short.toUnsignedLong(s), name);
} }
if (u instanceof Byte) { if (u instanceof Byte b) {
return getDuration(Short.toUnsignedLong((Byte) u), name); return getDuration(Short.toUnsignedLong(b), name);
} }
} }
throw newIllegalArgumentException(name, "java.time.Duration"); throw newIllegalArgumentException(name, "java.time.Duration");
@ -876,31 +876,31 @@ public class RecordedObject {
*/ */
public final Instant getInstant(String name) { public final Instant getInstant(String name) {
Object o = getValue(name, true); Object o = getValue(name, true);
if (o instanceof Long) { if (o instanceof Long l) {
return getInstant(((Long) o).longValue(), name); return getInstant(l, name);
} }
if (o instanceof Integer) { if (o instanceof Integer i) {
return getInstant(((Integer) o).longValue(), name); return getInstant(i, name);
} }
if (o instanceof Short) { if (o instanceof Short s) {
return getInstant(((Short) o).longValue(), name); return getInstant(s, name);
} }
if (o instanceof Character) { if (o instanceof Character c) {
return getInstant(((Character) o).charValue(), name); return getInstant(c, name);
} }
if (o instanceof Byte) { if (o instanceof Byte b) {
return getInstant(((Byte) o).longValue(), name); return getInstant(b, name);
} }
if (o instanceof UnsignedValue) { if (o instanceof UnsignedValue unsigned) {
Object u = ((UnsignedValue) o).value(); Object u = unsigned.value();
if (u instanceof Integer) { if (u instanceof Integer i) {
return getInstant(Integer.toUnsignedLong((Integer) u), name); return getInstant(Integer.toUnsignedLong(i), name);
} }
if (u instanceof Short) { if (u instanceof Short s) {
return getInstant(Short.toUnsignedLong((Short) u), name); return getInstant(Short.toUnsignedLong(s), name);
} }
if (u instanceof Byte) { if (u instanceof Byte b) {
return getInstant(Short.toUnsignedLong((Byte) u), name); return getInstant(Short.toUnsignedLong(b), name);
} }
} }
throw newIllegalArgumentException(name, "java.time.Instant"); throw newIllegalArgumentException(name, "java.time.Instant");
@ -982,8 +982,8 @@ public class RecordedObject {
StringWriter s = new StringWriter(); StringWriter s = new StringWriter();
PrettyWriter p = new PrettyWriter(new PrintWriter(s)); PrettyWriter p = new PrettyWriter(new PrintWriter(s));
p.setStackDepth(5); p.setStackDepth(5);
if (this instanceof RecordedEvent) { if (this instanceof RecordedEvent event) {
p.print((RecordedEvent) this); p.print(event);
} else { } else {
p.print(this, ""); p.print(this, "");
} }

View file

@ -25,8 +25,8 @@
package jdk.jfr.consumer; package jdk.jfr.consumer;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections;
import java.util.List; import java.util.List;
import jdk.jfr.internal.consumer.ObjectContext; import jdk.jfr.internal.consumer.ObjectContext;
@ -51,7 +51,7 @@ public final class RecordedStackTrace extends RecordedObject {
public List<RecordedFrame> getFrames() { public List<RecordedFrame> getFrames() {
Object[] array = getTyped("frames", Object[].class, null); Object[] array = getTyped("frames", Object[].class, null);
if (array == null) { if (array == null) {
return Collections.EMPTY_LIST; return new ArrayList<>(0);
} }
List<?> list = Arrays.asList(array); List<?> list = Arrays.asList(array);
return (List<RecordedFrame>) list; return (List<RecordedFrame>) list;

View file

@ -193,6 +193,7 @@ public final class RecordingFile implements Closeable {
* *
* @throws IOException if an I/O error occurred * @throws IOException if an I/O error occurred
*/ */
@Override
public void close() throws IOException { public void close() throws IOException {
if (input != null) { if (input != null) {
eof = true; eof = true;

View file

@ -48,7 +48,7 @@
* .stream() * .stream()
* .sorted((a, b) -> b.getValue().compareTo(a.getValue())) * .sorted((a, b) -> b.getValue().compareTo(a.getValue()))
* .forEach(e -> System.out.printf("%8d %s\n", e.getValue(), e.getKey())); * .forEach(e -> System.out.printf("%8d %s\n", e.getValue(), e.getKey()));
* } * }
* }</pre> * }</pre>
* <p> * <p>
* <b>Null-handling</b> * <b>Null-handling</b>

View file

@ -28,7 +28,6 @@ package jdk.jfr.events;
import jdk.internal.misc.VM.BufferPool; import jdk.internal.misc.VM.BufferPool;
import jdk.internal.misc.VM; import jdk.internal.misc.VM;
import jdk.jfr.*; import jdk.jfr.*;
import jdk.jfr.internal.Type;
@Category({ "Java Application", "Statistics" }) @Category({ "Java Application", "Statistics" })
public abstract class AbstractBufferStatisticsEvent extends AbstractJDKEvent { public abstract class AbstractBufferStatisticsEvent extends AbstractJDKEvent {

View file

@ -40,12 +40,8 @@ import jdk.jfr.internal.Type;
@StackTrace(false) @StackTrace(false)
public final class ActiveRecordingEvent extends AbstractJDKEvent { public final class ActiveRecordingEvent extends AbstractJDKEvent {
public static final ThreadLocal<ActiveRecordingEvent> EVENT = new ThreadLocal<ActiveRecordingEvent>() { // To be accessed when holding recorder lock
@Override public static final ActiveRecordingEvent EVENT = new ActiveRecordingEvent();
protected ActiveRecordingEvent initialValue() {
return new ActiveRecordingEvent();
}
};
@Label("Id") @Label("Id")
public long id; public long id;

View file

@ -29,7 +29,6 @@ import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationHandler; import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.lang.reflect.Proxy; import java.lang.reflect.Proxy;
import java.util.Collections;
import java.util.List; import java.util.List;
import jdk.jfr.AnnotationElement; import jdk.jfr.AnnotationElement;
@ -58,17 +57,18 @@ public final class AnnotationConstruct {
} }
} }
private List<AnnotationElement> annotationElements = Collections.emptyList(); private List<AnnotationElement> annotationElements;
private byte unsignedFlag = -1; private byte unsignedFlag = -1;
public AnnotationConstruct(List<AnnotationElement> ann) { public AnnotationConstruct(List<AnnotationElement> elements) {
this.annotationElements = ann; this.annotationElements = List.copyOf(elements);
} }
public AnnotationConstruct() { public AnnotationConstruct() {
this(List.of());
} }
public void setAnnotationElements(List<AnnotationElement> elements) { public void setAnnotationElements(List<AnnotationElement> elements) {
annotationElements = Utils.smallUnmodifiable(elements); annotationElements = List.copyOf(elements);
} }
public String getLabel() { public String getLabel() {
@ -100,11 +100,6 @@ public final class AnnotationConstruct {
return annotationElements; return annotationElements;
} }
// package private
boolean remove(AnnotationElement annotation) {
return annotationElements.remove(annotation);
}
private AnnotationElement getAnnotationElement(Class<? extends Annotation> clazz) { private AnnotationElement getAnnotationElement(Class<? extends Annotation> clazz) {
// if multiple annotation elements with the same name exists, prioritize // if multiple annotation elements with the same name exists, prioritize
// the one with the same id. Note, id alone is not a guarantee, since it // the one with the same id. Note, id alone is not a guarantee, since it

View file

@ -95,14 +95,13 @@ public final class EventControl {
} }
addControl(Enabled.NAME, defineEnabled(eventType)); addControl(Enabled.NAME, defineEnabled(eventType));
ArrayList<AnnotationElement> aes = new ArrayList<>(eventType.getAnnotationElements()); List<AnnotationElement> aes = new ArrayList<>(eventType.getAnnotationElements());
remove(eventType, aes, Threshold.class); remove(eventType, aes, Threshold.class);
remove(eventType, aes, Period.class); remove(eventType, aes, Period.class);
remove(eventType, aes, Enabled.class); remove(eventType, aes, Enabled.class);
remove(eventType, aes, StackTrace.class); remove(eventType, aes, StackTrace.class);
remove(eventType, aes, Cutoff.class); remove(eventType, aes, Cutoff.class);
remove(eventType, aes, Throttle.class); remove(eventType, aes, Throttle.class);
aes.trimToSize();
eventType.setAnnotations(aes); eventType.setAnnotations(aes);
this.type = eventType; this.type = eventType;
this.idName = String.valueOf(eventType.getId()); this.idName = String.valueOf(eventType.getId());

View file

@ -195,9 +195,8 @@ public final class EventInstrumentation {
if (values != null && values.size() == 2) { if (values != null && values.size() == 2) {
Object key = values.get(0); Object key = values.get(0);
Object value = values.get(1); Object value = values.get(1);
if (key instanceof String && value != null) { if (key instanceof String keyName && value != null) {
if (type == value.getClass()) { if (type == value.getClass()) {
String keyName = (String) key;
if ("value".equals(keyName)) { if ("value".equals(keyName)) {
return (T) value; return (T) value;
} }

View file

@ -25,8 +25,6 @@
package jdk.jfr.internal; package jdk.jfr.internal;
import java.util.function.Supplier;
/** /**
* JFR logger * JFR logger
* *

View file

@ -252,6 +252,7 @@ public final class LongMap<T> {
return count; return count;
} }
@Override
public String toString() { public String toString() {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
for (int i = 0; i < objects.length; i++) { for (int i = 0; i < objects.length; i++) {

View file

@ -25,13 +25,10 @@
package jdk.jfr.internal; package jdk.jfr.internal;
import java.io.BufferedInputStream;
import java.io.DataInputStream; import java.io.DataInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.lang.annotation.Annotation; import java.lang.annotation.Annotation;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;

View file

@ -124,8 +124,8 @@ final class MetadataReader {
type.setAnnotations(aes); type.setAnnotations(aes);
int index = 0; int index = 0;
if (type instanceof PlatformEventType) { if (type instanceof PlatformEventType pType) {
List<SettingDescriptor> settings = ((PlatformEventType) type).getAllSettings(); List<SettingDescriptor> settings = pType.getAllSettings();
for (Element settingElement : typeElement.elements(ELEMENT_SETTING)) { for (Element settingElement : typeElement.elements(ELEMENT_SETTING)) {
ArrayList<AnnotationElement> annotations = new ArrayList<>(); ArrayList<AnnotationElement> annotations = new ArrayList<>();
for (Element annotationElement : settingElement.elements(ELEMENT_ANNOTATION)) { for (Element annotationElement : settingElement.elements(ELEMENT_ANNOTATION)) {
@ -221,8 +221,8 @@ final class MetadataReader {
private void buildEvenTypes() { private void buildEvenTypes() {
for (Type type : descriptor.types) { for (Type type : descriptor.types) {
if (type instanceof PlatformEventType) { if (type instanceof PlatformEventType pType) {
descriptor.eventTypes.add(PrivateAccess.getInstance().newEventType((PlatformEventType) type)); descriptor.eventTypes.add(PrivateAccess.getInstance().newEventType(pType));
} }
} }
} }

View file

@ -70,8 +70,7 @@ public final class MetadataRepository {
private void initializeJVMEventTypes() { private void initializeJVMEventTypes() {
List<RequestHook> requestHooks = new ArrayList<>(); List<RequestHook> requestHooks = new ArrayList<>();
for (Type type : typeLibrary.getTypes()) { for (Type type : typeLibrary.getTypes()) {
if (type instanceof PlatformEventType) { if (type instanceof PlatformEventType pEventType) {
PlatformEventType pEventType = (PlatformEventType) type;
EventType eventType = PrivateAccess.getInstance().newEventType(pEventType); EventType eventType = PrivateAccess.getInstance().newEventType(pEventType);
pEventType.setHasDuration(eventType.getAnnotation(Threshold.class) != null); pEventType.setHasDuration(eventType.getAnnotation(Threshold.class) != null);
pEventType.setHasStackTrace(eventType.getAnnotation(StackTrace.class) != null); pEventType.setHasStackTrace(eventType.getAnnotation(StackTrace.class) != null);
@ -291,9 +290,8 @@ public final class MetadataRepository {
knownIds.add(Type.getTypeId(ec)); knownIds.add(Type.getTypeId(ec));
} }
for (Type type : typeLibrary.getTypes()) { for (Type type : typeLibrary.getTypes()) {
if (type instanceof PlatformEventType) { if (type instanceof PlatformEventType pe) {
if (!knownIds.contains(type.getId())) { if (!knownIds.contains(pe.getId())) {
PlatformEventType pe = (PlatformEventType) type;
if (!pe.isJVM()) { if (!pe.isJVM()) {
pe.setRegistered(false); pe.setRegistered(false);
} }

View file

@ -169,8 +169,8 @@ final class MetadataWriter {
element.addAttribute(ATTRIBUTE_SIMPLE_TYPE, true); element.addAttribute(ATTRIBUTE_SIMPLE_TYPE, true);
} }
element.addAttribute(ATTRIBUTE_ID, type.getId()); element.addAttribute(ATTRIBUTE_ID, type.getId());
if (type instanceof PlatformEventType) { if (type instanceof PlatformEventType pType) {
for (SettingDescriptor v : ((PlatformEventType)type).getSettings()) { for (SettingDescriptor v : pType.getSettings()) {
makeSettingElement(element, v); makeSettingElement(element, v);
} }
} }

View file

@ -439,7 +439,7 @@ public final class PlatformRecorder {
private void writeMetaEvents() { private void writeMetaEvents() {
if (activeRecordingEvent.isEnabled()) { if (activeRecordingEvent.isEnabled()) {
ActiveRecordingEvent event = ActiveRecordingEvent.EVENT.get(); ActiveRecordingEvent event = ActiveRecordingEvent.EVENT;
for (PlatformRecording r : getRecordings()) { for (PlatformRecording r : getRecordings()) {
if (r.getState() == RecordingState.RUNNING && r.shouldWriteMetadataEvent()) { if (r.getState() == RecordingState.RUNNING && r.shouldWriteMetadataEvent()) {
event.id = r.getId(); event.id = r.getId();

View file

@ -28,17 +28,12 @@ package jdk.jfr.internal;
import java.io.IOException; import java.io.IOException;
import java.io.RandomAccessFile; import java.io.RandomAccessFile;
import java.nio.channels.ReadableByteChannel; import java.nio.channels.ReadableByteChannel;
import java.nio.file.Path;
import java.time.Instant; import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.util.Comparator; import java.util.Comparator;
import jdk.jfr.internal.SecuritySupport.SafePath; import jdk.jfr.internal.SecuritySupport.SafePath;
final class RepositoryChunk { final class RepositoryChunk {
private static final int MAX_CHUNK_NAMES = 100;
private static final String FILE_EXTENSION = ".jfr";
static final Comparator<RepositoryChunk> END_TIME_COMPARATOR = new Comparator<RepositoryChunk>() { static final Comparator<RepositoryChunk> END_TIME_COMPARATOR = new Comparator<RepositoryChunk>() {
@Override @Override

View file

@ -172,6 +172,7 @@ public final class SecuritySupport {
return path.toFile(); return path.toFile();
} }
@Override
public String toString() { public String toString() {
return text; return text;
} }
@ -183,8 +184,8 @@ public final class SecuritySupport {
@Override @Override
public boolean equals(Object other) { public boolean equals(Object other) {
if(other != null && other instanceof SafePath){ if(other != null && other instanceof SafePath s){
return this.toPath().equals(((SafePath) other).toPath()); return this.toPath().equals(s.toPath());
} }
return false; return false;
} }

View file

@ -100,6 +100,7 @@ final class ShutdownHook implements Runnable {
} }
static final class ExceptionHandler implements Thread.UncaughtExceptionHandler { static final class ExceptionHandler implements Thread.UncaughtExceptionHandler {
@Override
public void uncaughtException(Thread t, Throwable e) { public void uncaughtException(Thread t, Throwable e) {
JVM.getJVM().uncaughtException(t, e); JVM.getJVM().uncaughtException(t, e);
} }

View file

@ -28,7 +28,6 @@ package jdk.jfr.internal;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -212,8 +211,8 @@ public class Type implements Comparable<Type> {
} }
public List<ValueDescriptor> getFields() { public List<ValueDescriptor> getFields() {
if (fields instanceof ArrayList) { if (fields instanceof ArrayList<?> list) {
((ArrayList<ValueDescriptor>) fields).trimToSize(); list.trimToSize();
fields = Collections.unmodifiableList(fields); fields = Collections.unmodifiableList(fields);
} }
return fields; return fields;
@ -291,8 +290,7 @@ public class Type implements Comparable<Type> {
@Override @Override
public boolean equals(Object object) { public boolean equals(Object object) {
if (object instanceof Type) { if (object instanceof Type that) {
Type that = (Type) object;
return that.id == this.id; return that.id == this.id;
} }
return false; return false;

View file

@ -26,7 +26,6 @@
package jdk.jfr.internal; package jdk.jfr.internal;
import java.io.IOException; import java.io.IOException;
import java.io.PrintWriter;
import java.lang.annotation.Annotation; import java.lang.annotation.Annotation;
import java.lang.annotation.Repeatable; import java.lang.annotation.Repeatable;
import java.lang.reflect.Field; import java.lang.reflect.Field;
@ -58,7 +57,6 @@ import jdk.jfr.SettingDescriptor;
import jdk.jfr.Timespan; import jdk.jfr.Timespan;
import jdk.jfr.Timestamp; import jdk.jfr.Timestamp;
import jdk.jfr.ValueDescriptor; import jdk.jfr.ValueDescriptor;
import jdk.jfr.internal.tool.PrettyWriter;
public final class TypeLibrary { public final class TypeLibrary {
@ -79,7 +77,7 @@ public final class TypeLibrary {
} }
private static ValueDescriptor createStartTimeField() { private static ValueDescriptor createStartTimeField() {
List<AnnotationElement> annos = createStandardAnnotations("Start Time", null); var annos = createStandardAnnotations("Start Time", null);
annos.add(new jdk.jfr.AnnotationElement(Timestamp.class, Timestamp.TICKS)); annos.add(new jdk.jfr.AnnotationElement(Timestamp.class, Timestamp.TICKS));
return PrivateAccess.getInstance().newValueDescriptor(EventInstrumentation.FIELD_START_TIME, Type.LONG, annos, 0, false, return PrivateAccess.getInstance().newValueDescriptor(EventInstrumentation.FIELD_START_TIME, Type.LONG, annos, 0, false,
EventInstrumentation.FIELD_START_TIME); EventInstrumentation.FIELD_START_TIME);
@ -87,22 +85,19 @@ public final class TypeLibrary {
} }
private static ValueDescriptor createStackTraceField() { private static ValueDescriptor createStackTraceField() {
List<AnnotationElement> annos = new ArrayList<>(); var annos = createStandardAnnotations("Stack Trace", "Stack Trace starting from the method the event was committed in");
annos = createStandardAnnotations("Stack Trace", "Stack Trace starting from the method the event was committed in");
return PrivateAccess.getInstance().newValueDescriptor(EventInstrumentation.FIELD_STACK_TRACE, Type.STACK_TRACE, annos, 0, true, return PrivateAccess.getInstance().newValueDescriptor(EventInstrumentation.FIELD_STACK_TRACE, Type.STACK_TRACE, annos, 0, true,
EventInstrumentation.FIELD_STACK_TRACE); EventInstrumentation.FIELD_STACK_TRACE);
} }
private static ValueDescriptor createThreadField() { private static ValueDescriptor createThreadField() {
List<AnnotationElement> annos = new ArrayList<>(); var annos = createStandardAnnotations("Event Thread", "Thread in which event was committed in");
annos = createStandardAnnotations("Event Thread", "Thread in which event was committed in");
return PrivateAccess.getInstance().newValueDescriptor(EventInstrumentation.FIELD_EVENT_THREAD, Type.THREAD, annos, 0, true, return PrivateAccess.getInstance().newValueDescriptor(EventInstrumentation.FIELD_EVENT_THREAD, Type.THREAD, annos, 0, true,
EventInstrumentation.FIELD_EVENT_THREAD); EventInstrumentation.FIELD_EVENT_THREAD);
} }
private static ValueDescriptor createDurationField() { private static ValueDescriptor createDurationField() {
List<AnnotationElement> annos = new ArrayList<>(); var annos = createStandardAnnotations("Duration", null);
annos = createStandardAnnotations("Duration", null);
annos.add(new jdk.jfr.AnnotationElement(Timespan.class, Timespan.TICKS)); annos.add(new jdk.jfr.AnnotationElement(Timespan.class, Timespan.TICKS));
return PrivateAccess.getInstance().newValueDescriptor(EventInstrumentation.FIELD_DURATION, Type.LONG, annos, 0, false, EventInstrumentation.FIELD_DURATION); return PrivateAccess.getInstance().newValueDescriptor(EventInstrumentation.FIELD_DURATION, Type.LONG, annos, 0, false, EventInstrumentation.FIELD_DURATION);
} }
@ -142,7 +137,6 @@ public final class TypeLibrary {
aes.add(ae); aes.add(ae);
} }
} }
aes.trimToSize();
type.setAnnotations(aes); type.setAnnotations(aes);
} }
return getType(a); return getType(a);
@ -419,8 +413,8 @@ public final class TypeLibrary {
Logger.log(LogTag.JFR_METADATA, LogLevel.TRACE, "Cleaning out obsolete metadata"); Logger.log(LogTag.JFR_METADATA, LogLevel.TRACE, "Cleaning out obsolete metadata");
List<Type> registered = new ArrayList<>(); List<Type> registered = new ArrayList<>();
for (Type type : types.values()) { for (Type type : types.values()) {
if (type instanceof PlatformEventType) { if (type instanceof PlatformEventType pType) {
if (((PlatformEventType) type).isRegistered()) { if (pType.isRegistered()) {
registered.add(type); registered.add(type);
} }
} }
@ -472,8 +466,7 @@ public final class TypeLibrary {
typeQ.add(PrivateAccess.getInstance().getType(v)); typeQ.add(PrivateAccess.getInstance().getType(v));
visitAnnotations(typeQ, v.getAnnotationElements()); visitAnnotations(typeQ, v.getAnnotationElements());
} }
if (type instanceof PlatformEventType) { if (type instanceof PlatformEventType pe) {
PlatformEventType pe = (PlatformEventType) type;
for (SettingDescriptor s : pe.getAllSettings()) { for (SettingDescriptor s : pe.getAllSettings()) {
typeQ.add(PrivateAccess.getInstance().getType(s)); typeQ.add(PrivateAccess.getInstance().getType(s));
visitAnnotations(typeQ, s.getAnnotationElements()); visitAnnotations(typeQ, s.getAnnotationElements());

View file

@ -48,7 +48,6 @@ import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit; import java.time.temporal.ChronoUnit;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@ -207,22 +206,20 @@ public final class Utils {
} }
enum ThrottleUnit { enum ThrottleUnit {
NANOSECONDS("ns", TimeUnit.NANOSECONDS, TimeUnit.SECONDS.toNanos(1), TimeUnit.SECONDS.toMillis(1)), NANOSECONDS("ns", TimeUnit.SECONDS.toNanos(1), TimeUnit.SECONDS.toMillis(1)),
MICROSECONDS("us", TimeUnit.MICROSECONDS, TimeUnit.SECONDS.toNanos(1) / 1000, TimeUnit.SECONDS.toMillis(1)), MICROSECONDS("us", TimeUnit.SECONDS.toNanos(1) / 1000, TimeUnit.SECONDS.toMillis(1)),
MILLISECONDS("ms", TimeUnit.MILLISECONDS, TimeUnit.SECONDS.toMillis(1), TimeUnit.SECONDS.toMillis(1)), MILLISECONDS("ms", TimeUnit.SECONDS.toMillis(1), TimeUnit.SECONDS.toMillis(1)),
SECONDS("s", TimeUnit.SECONDS, 1, TimeUnit.SECONDS.toMillis(1)), SECONDS("s", 1, TimeUnit.SECONDS.toMillis(1)),
MINUTES("m", TimeUnit.MINUTES, 1, TimeUnit.MINUTES.toMillis(1)), MINUTES("m", 1, TimeUnit.MINUTES.toMillis(1)),
HOUR("h", TimeUnit.HOURS, 1, TimeUnit.HOURS.toMillis(1)), HOUR("h", 1, TimeUnit.HOURS.toMillis(1)),
DAY("d", TimeUnit.DAYS, 1, TimeUnit.DAYS.toMillis(1)); DAY("d", 1, TimeUnit.DAYS.toMillis(1));
private final String text; private final String text;
private final TimeUnit timeUnit;
private final long factor; private final long factor;
private final long millis; private final long millis;
ThrottleUnit(String t, TimeUnit u, long factor, long millis) { ThrottleUnit(String t, long factor, long millis) {
this.text = t; this.text = t;
this.timeUnit = u;
this.factor = factor; this.factor = factor;
this.millis = millis; this.millis = millis;
} }
@ -365,9 +362,7 @@ public final class Utils {
} }
} }
} }
List<Annotation> annos = new ArrayList<>(); return List.of(a);
annos.add(a);
return annos;
} }
static boolean isAfter(RecordingState stateToTest, RecordingState b) { static boolean isAfter(RecordingState stateToTest, RecordingState b) {
@ -649,16 +644,6 @@ public final class Utils {
return knownType; return knownType;
} }
public static <T> List<T> smallUnmodifiable(List<T> list) {
if (list.isEmpty()) {
return Collections.emptyList();
}
if (list.size() == 1) {
return Collections.singletonList(list.get(0));
}
return Collections.unmodifiableList(list);
}
public static String upgradeLegacyJDKEvent(String eventName) { public static String upgradeLegacyJDKEvent(String eventName) {
if (eventName.length() <= LEGACY_EVENT_NAME_PREFIX.length()) { if (eventName.length() <= LEGACY_EVENT_NAME_PREFIX.length()) {
return eventName; return eventName;

View file

@ -31,7 +31,6 @@ import java.security.AccessController;
import java.security.PrivilegedAction; import java.security.PrivilegedAction;
import java.time.Duration; import java.time.Duration;
import java.time.Instant; import java.time.Instant;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicLong;

View file

@ -165,8 +165,7 @@ public final class ChunkParser {
void updateConfiguration(ParserConfiguration configuration, boolean resetEventCache) { void updateConfiguration(ParserConfiguration configuration, boolean resetEventCache) {
this.configuration = configuration; this.configuration = configuration;
parsers.forEach(p -> { parsers.forEach(p -> {
if (p instanceof EventParser) { if (p instanceof EventParser ep) {
EventParser ep = (EventParser) p;
if (resetEventCache) { if (resetEventCache) {
ep.resetCache(); ep.resetCache();
} }
@ -244,9 +243,8 @@ public final class ChunkParser {
} }
long typeId = input.readLong(); long typeId = input.readLong();
Parser p = parsers.get(typeId); Parser p = parsers.get(typeId);
if (p instanceof EventParser) { if (p instanceof EventParser ep) {
// Fast path // Fast path
EventParser ep = (EventParser) p;
RecordedEvent event = ep.parse(input); RecordedEvent event = ep.parse(input);
if (event != null) { if (event != null) {
input.position(pos + size); input.position(pos + size);
@ -398,8 +396,8 @@ public final class ChunkParser {
if (o == null) { // should not happen if (o == null) { // should not happen
return "null"; return "null";
} }
if (o instanceof String) { if (o instanceof String s) {
return "\"" + String.valueOf(o) + "\""; return "\"" + s + "\"";
} }
if (o instanceof RecordedObject) { if (o instanceof RecordedObject) {
return o.getClass().getName(); return o.getClass().getName();
@ -484,8 +482,7 @@ public final class ChunkParser {
LongMap<Parser> ps = this.parsers; LongMap<Parser> ps = this.parsers;
if (ps != null) { if (ps != null) {
ps.forEach(p -> { ps.forEach(p -> {
if (p instanceof EventParser) { if (p instanceof EventParser ep) {
EventParser ep = (EventParser) p;
ep.resetCache(); ep.resetCache();
} }
}); });

View file

@ -60,6 +60,7 @@ final class ConstantMap {
return pool.get(key); return pool.get(key);
} }
@Override
public String toString() { public String toString() {
return "ref: " + pool.name + "[" + key + "]"; return "ref: " + pool.name + "[" + key + "]";
} }
@ -137,8 +138,8 @@ final class ConstantMap {
} }
private static Object resolve(Object o) { private static Object resolve(Object o) {
if (o instanceof Reference) { if (o instanceof Reference r) {
return resolve(((Reference) o).resolve()); return resolve(r.resolve());
} }
if (o != null && o.getClass().isArray()) { if (o != null && o.getClass().isArray()) {
final Object[] array = (Object[]) o; final Object[] array = (Object[]) o;

View file

@ -28,7 +28,6 @@ package jdk.jfr.internal.consumer;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Path; import java.nio.file.Path;
import java.security.AccessControlContext; import java.security.AccessControlContext;
import java.time.Duration;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;

View file

@ -33,8 +33,6 @@ import java.util.List;
import jdk.jfr.EventType; import jdk.jfr.EventType;
import jdk.jfr.ValueDescriptor; import jdk.jfr.ValueDescriptor;
import jdk.jfr.consumer.RecordedEvent; import jdk.jfr.consumer.RecordedEvent;
import jdk.jfr.internal.consumer.Parser;
import jdk.jfr.internal.consumer.RecordingInput;
/** /**
* Parses an event and returns a {@link RecordedEvent}. * Parses an event and returns a {@link RecordedEvent}.
@ -110,6 +108,7 @@ final class EventParser extends Parser {
return enabled; return enabled;
} }
@Override
public RecordedEvent parse(RecordingInput input) throws IOException { public RecordedEvent parse(RecordingInput input) throws IOException {
if (!enabled) { if (!enabled) {
return null; return null;

View file

@ -40,6 +40,7 @@ public final class FinishedStream extends EventByteStream {
this.buffer = new byte[blockSize]; this.buffer = new byte[blockSize];
} }
@Override
public synchronized byte[] read() throws IOException { public synchronized byte[] read() throws IOException {
// OK to reuse buffer since this // OK to reuse buffer since this
// is only used for serialization // is only used for serialization

View file

@ -28,11 +28,9 @@ package jdk.jfr.internal.consumer;
import java.io.IOException; import java.io.IOException;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.function.Consumer;
import jdk.jfr.Configuration; import jdk.jfr.Configuration;
import jdk.jfr.EventType; import jdk.jfr.EventType;
import jdk.jfr.consumer.EventStream;
import jdk.jfr.consumer.MetadataEvent; import jdk.jfr.consumer.MetadataEvent;
import jdk.jfr.consumer.RecordedClass; import jdk.jfr.consumer.RecordedClass;
import jdk.jfr.consumer.RecordedClassLoader; import jdk.jfr.consumer.RecordedClassLoader;

View file

@ -143,8 +143,8 @@ public abstract class ObjectFactory<T> {
if (value == null) { if (value == null) {
return null; return null;
} }
if (value instanceof Object[]) { if (value instanceof Object[] array) {
return createTyped(objectContext, id, (Object[]) value); return createTyped(objectContext, id, array);
} }
throw new InternalError("Object factory must have struct type. Type was " + value.getClass().getName()); throw new InternalError("Object factory must have struct type. Type was " + value.getClass().getName());
} }

View file

@ -27,11 +27,9 @@ package jdk.jfr.internal.consumer;
import java.io.IOException; import java.io.IOException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.file.Path; import java.nio.file.Path;
import java.time.Instant;
import jdk.jfr.Recording; import jdk.jfr.Recording;
import jdk.jfr.RecordingState; import jdk.jfr.RecordingState;
import jdk.jfr.internal.Utils;
import jdk.jfr.internal.SecuritySupport; import jdk.jfr.internal.SecuritySupport;
import jdk.jfr.internal.SecuritySupport.SafePath; import jdk.jfr.internal.SecuritySupport.SafePath;
import jdk.jfr.internal.management.EventByteStream; import jdk.jfr.internal.management.EventByteStream;
@ -66,6 +64,7 @@ public final class OngoingStream extends EventByteStream {
this.repositoryFiles = new RepositoryFiles(SecuritySupport.PRIVILEGED, null); this.repositoryFiles = new RepositoryFiles(SecuritySupport.PRIVILEGED, null);
} }
@Override
public synchronized byte[] read() throws IOException { public synchronized byte[] read() throws IOException {
try { try {
return readBytes(); return readBytes();

View file

@ -35,8 +35,6 @@ import jdk.jfr.internal.LongMap;
import jdk.jfr.internal.MetadataDescriptor; import jdk.jfr.internal.MetadataDescriptor;
import jdk.jfr.internal.PrivateAccess; import jdk.jfr.internal.PrivateAccess;
import jdk.jfr.internal.Type; import jdk.jfr.internal.Type;
import jdk.jfr.internal.consumer.Parser;
import jdk.jfr.internal.consumer.RecordingInput;
/** /**
* Class that create parsers suitable for reading events and constant pools * Class that create parsers suitable for reading events and constant pools

View file

@ -65,6 +65,7 @@ final class ParserFilter {
return -1; return -1;
} }
@Override
public String toString() { public String toString() {
if (acceptAll) { if (acceptAll) {
return "ACCEPT ALL"; return "ACCEPT ALL";

View file

@ -58,8 +58,7 @@ final class SocketChannelImplInstrumentor {
long duration = EventHandler.timestamp() - start; long duration = EventHandler.timestamp() - start;
if (handler.shouldCommit(duration)) { if (handler.shouldCommit(duration)) {
SocketAddress remoteAddress = getRemoteAddress(); SocketAddress remoteAddress = getRemoteAddress();
if (remoteAddress instanceof InetSocketAddress) { if (remoteAddress instanceof InetSocketAddress isa) {
InetSocketAddress isa = (InetSocketAddress) remoteAddress;
String hostString = isa.getAddress().toString(); String hostString = isa.getAddress().toString();
int delimiterIndex = hostString.lastIndexOf('/'); int delimiterIndex = hostString.lastIndexOf('/');
@ -101,8 +100,7 @@ final class SocketChannelImplInstrumentor {
long duration = EventHandler.timestamp() - start; long duration = EventHandler.timestamp() - start;
if (handler.shouldCommit(duration)) { if (handler.shouldCommit(duration)) {
SocketAddress remoteAddress = getRemoteAddress(); SocketAddress remoteAddress = getRemoteAddress();
if (remoteAddress instanceof InetSocketAddress) { if (remoteAddress instanceof InetSocketAddress isa) {
InetSocketAddress isa = (InetSocketAddress) remoteAddress;
String hostString = isa.getAddress().toString(); String hostString = isa.getAddress().toString();
int delimiterIndex = hostString.lastIndexOf('/'); int delimiterIndex = hostString.lastIndexOf('/');
@ -145,8 +143,7 @@ final class SocketChannelImplInstrumentor {
if (handler.shouldCommit(duration)) { if (handler.shouldCommit(duration)) {
long bytes = bytesWritten < 0 ? 0 : bytesWritten; long bytes = bytesWritten < 0 ? 0 : bytesWritten;
SocketAddress remoteAddress = getRemoteAddress(); SocketAddress remoteAddress = getRemoteAddress();
if (remoteAddress instanceof InetSocketAddress) { if (remoteAddress instanceof InetSocketAddress isa) {
InetSocketAddress isa = (InetSocketAddress) remoteAddress;
String hostString = isa.getAddress().toString(); String hostString = isa.getAddress().toString();
int delimiterIndex = hostString.lastIndexOf('/'); int delimiterIndex = hostString.lastIndexOf('/');
@ -186,8 +183,7 @@ final class SocketChannelImplInstrumentor {
if (handler.shouldCommit(duration)) { if (handler.shouldCommit(duration)) {
long bytes = bytesWritten < 0 ? 0 : bytesWritten; long bytes = bytesWritten < 0 ? 0 : bytesWritten;
SocketAddress remoteAddress = getRemoteAddress(); SocketAddress remoteAddress = getRemoteAddress();
if (remoteAddress instanceof InetSocketAddress) { if (remoteAddress instanceof InetSocketAddress isa) {
InetSocketAddress isa = (InetSocketAddress) remoteAddress;
String hostString = isa.getAddress().toString(); String hostString = isa.getAddress().toString();
int delimiterIndex = hostString.lastIndexOf('/'); int delimiterIndex = hostString.lastIndexOf('/');

View file

@ -27,13 +27,9 @@ package jdk.jfr.internal.management;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.nio.file.Path; import java.nio.file.Path;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.ZoneId;
import java.io.IOException; import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
import jdk.jfr.internal.SecuritySupport; import jdk.jfr.internal.SecuritySupport;
import jdk.jfr.internal.SecuritySupport.SafePath;
import jdk.jfr.internal.Utils; import jdk.jfr.internal.Utils;
import jdk.jfr.internal.consumer.FileAccess; import jdk.jfr.internal.consumer.FileAccess;

View file

@ -30,7 +30,6 @@ import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.time.Duration; import java.time.Duration;
import java.time.Instant; import java.time.Instant;
import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -54,7 +53,6 @@ import jdk.jfr.internal.Utils;
import jdk.jfr.internal.WriteableUserPath; import jdk.jfr.internal.WriteableUserPath;
import jdk.jfr.internal.consumer.EventDirectoryStream; import jdk.jfr.internal.consumer.EventDirectoryStream;
import jdk.jfr.internal.consumer.FileAccess; import jdk.jfr.internal.consumer.FileAccess;
import jdk.jfr.internal.consumer.JdkJfrConsumer;
import jdk.jfr.internal.instrument.JDKEvents; import jdk.jfr.internal.instrument.JDKEvents;
/** /**
@ -84,7 +82,7 @@ public final class ManagementSupport {
// would normally be checked when a Flight Recorder instance is created // would normally be checked when a Flight Recorder instance is created
Utils.checkAccessFlightRecorder(); Utils.checkAccessFlightRecorder();
if (JVMSupport.isNotAvailable()) { if (JVMSupport.isNotAvailable()) {
return new ArrayList<>(); return List.of();
} }
JDKEvents.initialize(); // make sure JDK events are available JDKEvents.initialize(); // make sure JDK events are available
return Collections.unmodifiableList(MetadataRepository.getInstance().getRegisteredEventTypes()); return Collections.unmodifiableList(MetadataRepository.getInstance().getRegisteredEventTypes());

View file

@ -35,8 +35,6 @@ import java.util.Timer;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import jdk.jfr.Recording; import jdk.jfr.Recording;
import jdk.jfr.internal.consumer.FinishedStream;
import jdk.jfr.internal.consumer.OngoingStream;
// Exposes EventByteStreams to the FlightRecorderMXBean // Exposes EventByteStreams to the FlightRecorderMXBean
public final class StreamManager { public final class StreamManager {

View file

@ -26,22 +26,13 @@
package jdk.jfr.internal.settings; package jdk.jfr.internal.settings;
import static java.util.concurrent.TimeUnit.MICROSECONDS;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.NANOSECONDS;
import static java.util.concurrent.TimeUnit.SECONDS;
import java.util.concurrent.TimeUnit;
import java.util.Objects; import java.util.Objects;
import java.util.Set; import java.util.Set;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import jdk.jfr.Description; import jdk.jfr.Description;
import jdk.jfr.Label; import jdk.jfr.Label;
import jdk.jfr.MetadataDefinition; import jdk.jfr.MetadataDefinition;
import jdk.jfr.Name; import jdk.jfr.Name;
import jdk.jfr.Timespan;
import jdk.jfr.internal.PlatformEventType; import jdk.jfr.internal.PlatformEventType;
import jdk.jfr.internal.Type; import jdk.jfr.internal.Type;
import jdk.jfr.internal.Utils; import jdk.jfr.internal.Utils;

View file

@ -31,7 +31,6 @@ import java.io.IOError;
import java.io.IOException; import java.io.IOException;
import java.io.PrintStream; import java.io.PrintStream;
import java.io.RandomAccessFile; import java.io.RandomAccessFile;
import java.nio.charset.Charset;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.InvalidPathException; import java.nio.file.InvalidPathException;
import java.nio.file.Path; import java.nio.file.Path;

View file

@ -26,7 +26,6 @@
package jdk.jfr.internal.tool; package jdk.jfr.internal.tool;
import java.io.PrintStream; import java.io.PrintStream;
import java.util.Collections;
import java.util.Deque; import java.util.Deque;
import java.util.List; import java.util.List;
@ -39,9 +38,10 @@ final class Help extends Command {
@Override @Override
public List<String> getOptionSyntax() { public List<String> getOptionSyntax() {
return Collections.singletonList("[<command>]"); return List.of("[<command>]");
} }
@Override
protected List<String> getAliases() { protected List<String> getAliases() {
return List.of("--help", "-h", "-?"); return List.of("--help", "-h", "-?");
} }

View file

@ -90,8 +90,7 @@ final class JSONWriter extends EventPrintWriter {
printAsString(value); printAsString(value);
return; return;
} }
if (value instanceof Double) { if (value instanceof Double dValue) {
Double dValue = (Double) value;
if (Double.isNaN(dValue) || Double.isInfinite(dValue)) { if (Double.isNaN(dValue) || Double.isInfinite(dValue)) {
printNull(); printNull();
return; return;
@ -99,8 +98,7 @@ final class JSONWriter extends EventPrintWriter {
printAsString(value); printAsString(value);
return; return;
} }
if (value instanceof Float) { if (value instanceof Float fValue) {
Float fValue = (Float) value;
if (Float.isNaN(fValue) || Float.isInfinite(fValue)) { if (Float.isNaN(fValue) || Float.isInfinite(fValue)) {
printNull(); printNull();
return; return;

View file

@ -293,27 +293,26 @@ public final class PrettyWriter extends EventPrintWriter {
return; return;
} }
if (value instanceof RecordedObject) { if (value instanceof RecordedObject) {
if (value instanceof RecordedThread) { if (value instanceof RecordedThread rt) {
printThread((RecordedThread) value, postFix); printThread(rt, postFix);
return; return;
} }
if (value instanceof RecordedClass) { if (value instanceof RecordedClass rc) {
printClass((RecordedClass) value, postFix); printClass(rc, postFix);
return; return;
} }
if (value instanceof RecordedClassLoader) { if (value instanceof RecordedClassLoader rcl) {
printClassLoader((RecordedClassLoader) value, postFix); printClassLoader(rcl, postFix);
return; return;
} }
if (value instanceof RecordedFrame) { if (value instanceof RecordedFrame frame) {
RecordedFrame frame = (RecordedFrame) value;
if (frame.isJavaFrame()) { if (frame.isJavaFrame()) {
printJavaFrame((RecordedFrame) value, postFix); printJavaFrame((RecordedFrame) value, postFix);
return; return;
} }
} }
if (value instanceof RecordedMethod) { if (value instanceof RecordedMethod rm) {
println(formatMethod((RecordedMethod) value)); println(formatMethod(rm));
return; return;
} }
if (field.getTypeName().equals(TYPE_OLD_OBJECT)) { if (field.getTypeName().equals(TYPE_OLD_OBJECT)) {
@ -328,29 +327,25 @@ public final class PrettyWriter extends EventPrintWriter {
return; return;
} }
if (value instanceof Double) { if (value instanceof Double d) {
Double d = (Double) value;
if (Double.isNaN(d) || d == Double.NEGATIVE_INFINITY) { if (Double.isNaN(d) || d == Double.NEGATIVE_INFINITY) {
println("N/A"); println("N/A");
return; return;
} }
} }
if (value instanceof Float) { if (value instanceof Float f) {
Float f = (Float) value;
if (Float.isNaN(f) || f == Float.NEGATIVE_INFINITY) { if (Float.isNaN(f) || f == Float.NEGATIVE_INFINITY) {
println("N/A"); println("N/A");
return; return;
} }
} }
if (value instanceof Long) { if (value instanceof Long l) {
Long l = (Long) value;
if (l == Long.MIN_VALUE) { if (l == Long.MIN_VALUE) {
println("N/A"); println("N/A");
return; return;
} }
} }
if (value instanceof Integer) { if (value instanceof Integer i) {
Integer i = (Integer) value;
if (i == Integer.MIN_VALUE) { if (i == Integer.MIN_VALUE) {
println("N/A"); println("N/A");
return; return;
@ -552,8 +547,7 @@ public final class PrettyWriter extends EventPrintWriter {
} }
private boolean printFormatted(ValueDescriptor field, Object value) { private boolean printFormatted(ValueDescriptor field, Object value) {
if (value instanceof Duration) { if (value instanceof Duration d) {
Duration d = (Duration) value;
if (d.getSeconds() == Long.MIN_VALUE && d.getNano() == 0) { if (d.getSeconds() == Long.MIN_VALUE && d.getNano() == 0) {
println("N/A"); println("N/A");
return true; return true;
@ -561,8 +555,7 @@ public final class PrettyWriter extends EventPrintWriter {
println(Utils.formatDuration(d)); println(Utils.formatDuration(d));
return true; return true;
} }
if (value instanceof OffsetDateTime) { if (value instanceof OffsetDateTime odt) {
OffsetDateTime odt = (OffsetDateTime) value;
if (odt.equals(OffsetDateTime.MIN)) { if (odt.equals(OffsetDateTime.MIN)) {
println("N/A"); println("N/A");
return true; return true;
@ -572,16 +565,15 @@ public final class PrettyWriter extends EventPrintWriter {
} }
Percentage percentage = field.getAnnotation(Percentage.class); Percentage percentage = field.getAnnotation(Percentage.class);
if (percentage != null) { if (percentage != null) {
if (value instanceof Number) { if (value instanceof Number n) {
double d = ((Number) value).doubleValue(); double d = n.doubleValue();
println(String.format("%.2f", d * 100) + "%"); println(String.format("%.2f", d * 100) + "%");
return true; return true;
} }
} }
DataAmount dataAmount = field.getAnnotation(DataAmount.class); DataAmount dataAmount = field.getAnnotation(DataAmount.class);
if (dataAmount != null) { if (dataAmount != null) {
if (value instanceof Number) { if (value instanceof Number n) {
Number n = (Number) value;
long amount = n.longValue(); long amount = n.longValue();
if (field.getAnnotation(Frequency.class) != null) { if (field.getAnnotation(Frequency.class) != null) {
if (dataAmount.value().equals(DataAmount.BYTES)) { if (dataAmount.value().equals(DataAmount.BYTES)) {
@ -606,8 +598,8 @@ public final class PrettyWriter extends EventPrintWriter {
} }
MemoryAddress memoryAddress = field.getAnnotation(MemoryAddress.class); MemoryAddress memoryAddress = field.getAnnotation(MemoryAddress.class);
if (memoryAddress != null) { if (memoryAddress != null) {
if (value instanceof Number) { if (value instanceof Number n) {
long d = ((Number) value).longValue(); long d = n.longValue();
println(String.format("0x%08X", d)); println(String.format("0x%08X", d));
return true; return true;
} }

View file

@ -25,7 +25,6 @@
package jdk.jfr.internal.tool; package jdk.jfr.internal.tool;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.PrintStream; import java.io.PrintStream;
import java.io.PrintWriter; import java.io.PrintWriter;

View file

@ -64,7 +64,7 @@ final class Summary extends Command {
@Override @Override
public List<String> getOptionSyntax() { public List<String> getOptionSyntax() {
return Collections.singletonList("<file>"); return List.of("<file>");
} }
@Override @Override

View file

@ -44,6 +44,7 @@ final class Version extends Command {
System.out.println("1.0"); System.out.println("1.0");
} }
@Override
protected List<String> getAliases() { protected List<String> getAliases() {
return List.of("--version"); return List.of("--version");
} }