jdk.jfr.ValueDescriptor Java Examples

The following examples show how to use jdk.jfr.ValueDescriptor. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RecordedObject.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private Duration getDuration(long timespan, String name) throws InternalError {
    ValueDescriptor v = getValueDescriptor(descriptors, name, null);
    if (timespan == Long.MIN_VALUE) {
        return Duration.ofSeconds(Long.MIN_VALUE, 0);
    }
    Timespan ts = v.getAnnotation(Timespan.class);
    if (ts != null) {
        switch (ts.value()) {
        case Timespan.MICROSECONDS:
            return Duration.ofNanos(1000 * timespan);
        case Timespan.SECONDS:
            return Duration.ofSeconds(timespan);
        case Timespan.MILLISECONDS:
            return Duration.ofMillis(timespan);
        case Timespan.NANOSECONDS:
            return Duration.ofNanos(timespan);
        case Timespan.TICKS:
            return Duration.ofNanos(timeConverter.convertTimespan(timespan));
        }
        throw new IllegalArgumentException("Attempt to get " + v.getTypeName() + " field \"" + name + "\" with illegal timespan unit " + ts.value());
    }
    throw new IllegalArgumentException("Attempt to get " + v.getTypeName() + " field \"" + name + "\" with missing @Timespan");
}
 
Example #2
Source File: TypeLibrary.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Iterates all reachable types from a start collection
 *
 * @param rootSet the types to start from
 * @param p if a type should be accepted
 * @param c action to take on an accepted type
 */
private  static void visitReachable(Collection<Type> rootSet, Predicate<Type> p,  Consumer<Type> c) {
    Queue<Type> typeQ = new ArrayDeque<>(rootSet);
    while (!typeQ.isEmpty()) {
        Type type = typeQ.poll();
        if (p.test(type)) {
            c.accept(type);
            visitAnnotations(typeQ, type.getAnnotationElements());
            for (ValueDescriptor v : type.getFields()) {
                typeQ.add(PrivateAccess.getInstance().getType(v));
                visitAnnotations(typeQ, v.getAnnotationElements());
            }
            if (type instanceof PlatformEventType) {
                PlatformEventType pe = (PlatformEventType) type;
                for (SettingDescriptor s : pe.getAllSettings()) {
                    typeQ.add(PrivateAccess.getInstance().getType(s));
                    visitAnnotations(typeQ, s.getAnnotationElements());
                }
            }
        }
    }
}
 
Example #3
Source File: RecordedObject.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
private Object[] structifyArray(ValueDescriptor v, Object[] array, int dimension) {
    if (array == null) {
        return null;
    }
    Object[] structArray = new Object[array.length];
    for (int i = 0; i < structArray.length; i++) {
        Object arrayElement = array[i];
        if (dimension == 0) {
            // Hack, no general way to handle structarrays
            // without invoking ObjectFarctory for every instance.
            if ((Type.ORACLE_TYPE_PREFIX + "StackFrame").equals(v.getTypeName())) {
                structArray[i] = new RecordedFrame(v.getFields(), (Object[]) arrayElement, timeConverter);
            } else {
                structArray[i] = new RecordedObject(v.getFields(), (Object[]) arrayElement, timeConverter);
            }
        } else {
            structArray[i] = structifyArray(v, (Object[]) arrayElement, dimension - 1);
        }
    }
    return structArray;
}
 
Example #4
Source File: PrettyWriter.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private void printField(int commentIndex, ValueDescriptor v, boolean first) {
    if (!first) {
        println();
    }
    printAnnotations(commentIndex, v.getAnnotationElements());
    printIndent();
    Type vType = PrivateAccess.getInstance().getType(v);
    if (Type.SUPER_TYPE_SETTING.equals(vType.getSuperType())) {
        print("static ");
    }
    print(makeSimpleType(v.getTypeName()));
    if (v.isArray()) {
        print("[]");
    }
    print(" ");
    print(v.getName());
    print(";");
    printCommentRef(commentIndex, v.getTypeId());
}
 
Example #5
Source File: XMLWriter.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
private void printValueDescriptor(ValueDescriptor vd, Object value, int index) {
    boolean arrayElement = index != -1;
    String name = arrayElement ? null : vd.getName();
    if (vd.isArray() && !arrayElement) {
        if (printBeginElement("array", name, value, index)) {
            printArray(vd, (Object[]) value);
            printIndent();
            printEndElement("array");
        }
        return;
    }
    if (!vd.getFields().isEmpty()) {
        if (printBeginElement("struct", name, value, index)) {
            printObject((RecordedObject) value);
            printIndent();
            printEndElement("struct");
        }
        return;
    }
    if (printBeginElement("value", name, value, index)) {
        printEscaped(String.valueOf(value));
        printEndElement("value");
    }
}
 
Example #6
Source File: MetadataRepository.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private EventHandler makeHandler(Class<? extends Event> eventClass, List<AnnotationElement> dynamicAnnotations, List<ValueDescriptor> dynamicFields) throws InternalError {
    SecuritySupport.addHandlerExport(eventClass);
    PlatformEventType pEventType = (PlatformEventType) TypeLibrary.createType(eventClass, dynamicAnnotations, dynamicFields);
    EventType eventType = PrivateAccess.getInstance().newEventType(pEventType);
    EventControl ec = new EventControl(pEventType, eventClass);
    Class<? extends EventHandler> handlerClass = null;
    try {
        String eventHandlerName = EventHandlerCreator.makeEventHandlerName(eventType.getId());
        handlerClass = Class.forName(eventHandlerName, false, Event.class.getClassLoader()).asSubclass(EventHandler.class);
        // Created eagerly on class load, tag as instrumented
        pEventType.setInstrumented();
        Logger.log(JFR_SYSTEM, DEBUG, "Found existing event handler for " + eventType.getName());
   } catch (ClassNotFoundException cne) {
       EventHandlerCreator ehc = new EventHandlerCreator(eventType.getId(),  ec.getSettingInfos(), eventType, eventClass);
       handlerClass = ehc.makeEventHandlerClass();
       Logger.log(LogTag.JFR_SYSTEM, DEBUG, "Created event handler for " + eventType.getName());
   }
    EventHandler handler = EventHandlerCreator.instantiateEventHandler(handlerClass, true, eventType, ec);
    Utils.setHandler(eventClass, handler);
    return handler;
}
 
Example #7
Source File: TestValueDescriptorRecorded.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Throwable {
    Recording r = new Recording();
    r.enable(MyEvent.class).withoutStackTrace();
    r.start();
    MyEvent event = new MyEvent();
    event.commit();
    r.stop();

    List<RecordedEvent> events = Events.fromRecording(r);
    Events.hasEvents(events);
    RecordedEvent recordedEvent = events.get(0);

    for (ValueDescriptor desc : recordedEvent.getFields()) {
        if ("myValue".equals(desc.getName())) {
            Asserts.assertEquals(desc.getLabel(), "myLabel");
            Asserts.assertEquals(desc.getDescription(), "myDescription");
            Asserts.assertEquals(desc.getTypeName(), int.class.getName());
            Asserts.assertFalse(desc.isArray());
            Asserts.assertNull(desc.getContentType());
        }
    }
}
 
Example #8
Source File: EventClassBuilder.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
private void buildSetMethod() {
    GeneratorAdapter ga = new GeneratorAdapter(Opcodes.ACC_PUBLIC, SET_METHOD, null, null, classWriter);
    int index = 0;
    for (ValueDescriptor v : fields) {
        ga.loadArg(0);
        ga.visitLdcInsn(index);
        Label notEqual = new Label();
        ga.ifICmp(GeneratorAdapter.NE, notEqual);
        ga.loadThis();
        ga.loadArg(1);
        Type fieldType = ASMToolkit.toType(v);
        ga.unbox(ASMToolkit.toType(v));
        ga.putField(type, v.getName(), fieldType);
        ga.visitInsn(Opcodes.RETURN);
        ga.visitLabel(notEqual);
        index++;
    }
    ga.throwException(TYPE_IOBE, "Index must between 0 and " + fields.size());
    ga.endMethod();
}
 
Example #9
Source File: TestConstructor.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Throwable {
    ValueDescriptor vdSimple = new ValueDescriptor(String.class, "message");
    Asserts.assertNull(vdSimple.getAnnotation(Label.class), "Expected getAnnotation()==null");
    Asserts.assertEquals(0, vdSimple.getAnnotationElements().size(), "Expected getAnnotations().size() == 0");

    // Add labelA and verify we can read it back
    List<AnnotationElement> annos = new ArrayList<>();
    AnnotationElement labelA = new AnnotationElement(Label.class, "labelA");
    annos.add(labelA);
    System.out.println("labelA.getClass()" + labelA.getClass());
    ValueDescriptor vdComplex = new ValueDescriptor(String.class, "message", annos);

    final Label outLabel = vdComplex.getAnnotation(Label.class);
    Asserts.assertFalse(outLabel == null, "getLabel(Label.class) was null");
    System.out.println("outLabel.value() = " + outLabel.value());

    // Get labelA from getAnnotations() list
    Asserts.assertEquals(1, vdComplex.getAnnotationElements().size(), "Expected getAnnotations().size() == 1");
    final AnnotationElement outAnnotation = vdComplex.getAnnotationElements().get(0);
    Asserts.assertNotNull(outAnnotation, "outAnnotation was null");
    System.out.printf("Annotation: %s = %s%n", outAnnotation.getTypeName(), outAnnotation.getValue("value"));
    Asserts.assertEquals(outAnnotation, labelA, "Expected firstAnnotation == labelA");

}
 
Example #10
Source File: MainTest.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
private static void assertMetadata(List<RecordedEvent> events) {
    for (RecordedEvent e : events) {
        EventType type = e.getEventType();
        ModularizedAnnotation maType = type.getAnnotation(ModularizedAnnotation.class);
        if (maType == null) {
            fail("Missing @ModularizedAnnotation on type " + type);
        }
        assertEquals(maType.value(), "hello type");
        assertMetaAnnotation(type.getAnnotationElements());

        ValueDescriptor messageField = type.getField("message");
        ModularizedAnnotation maField = messageField.getAnnotation(ModularizedAnnotation.class);
        if (maField == null) {
            fail("Missing @ModularizedAnnotation on field in " + type);
        }
        assertEquals(maField.value(), "hello field");
        assertMetaAnnotation(messageField.getAnnotationElements());
    }
}
 
Example #11
Source File: TestDynamicAnnotations.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void testEventFactoryExample() throws IOException {
     List<ValueDescriptor> fields = new ArrayList<>();
     List<AnnotationElement> messageAnnotations = Collections.singletonList(new AnnotationElement(Label.class, "Message"));
     fields.add(new ValueDescriptor(String.class, "message", messageAnnotations));
     List<AnnotationElement> numberAnnotations = Collections.singletonList(new AnnotationElement(Label.class, "Number"));
     fields.add(new ValueDescriptor(int.class, "number", numberAnnotations));

     String[] category = { "Example", "Getting Started" };
     List<AnnotationElement> eventAnnotations = new ArrayList<>();
     eventAnnotations.add(new AnnotationElement(Name.class, "com.example.HelloWorld"));
     eventAnnotations.add(new AnnotationElement(Label.class, "Hello World"));
     eventAnnotations.add(new AnnotationElement(Description.class, "Helps programmer getting started"));
     eventAnnotations.add(new AnnotationElement(Category.class, category));

     EventFactory f = EventFactory.create(eventAnnotations, fields);

     Event event = f.newEvent();
     event.set(0, "hello, world!");
     event.set(1, 4711);
     event.commit();
}
 
Example #12
Source File: PrettyWriter.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
private void printType(Type t) throws IOException {
    print("// id: ");
    println(String.valueOf(t.getId()));
    int commentIndex = t.getName().length() + 10;
    String typeName = t.getName();
    int index = typeName.lastIndexOf(".");
    if (index != -1) {
        println("package " + typeName.substring(0, index) + ";");
    }
    printAnnotations(commentIndex, t.getAnnotationElements());
    print("class " + typeName.substring(index + 1));
    String superType = t.getSuperType();
    if (superType != null) {
        print(" extends " + superType);
    }
    println(" {");
    indent();
    for (ValueDescriptor v : t.getFields()) {
        printField(commentIndex, v);
    }
    retract();
    println("}");
    println();
}
 
Example #13
Source File: XMLWriter.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private void printEvent(RecordedEvent event) {
    EventType type = event.getEventType();
    printIndent();
    print("<event");
    printAttribute("type", type.getName());
    print(">");
    println();
    indent();
    for (ValueDescriptor v : event.getFields()) {
        printValueDescriptor(v, getValue(event, v), -1);
    }
    retract();
    printIndent();
    println("</event>");
    println();
}
 
Example #14
Source File: EventPrintWriter.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private ValueType determineValueType(ValueDescriptor v) {
    if (v.getAnnotation(Timespan.class) != null) {
        return ValueType.TIMESPAN;
    }
    if (v.getAnnotation(Timestamp.class) != null) {
        return ValueType.TIMESTAMP;
    }
    return ValueType.OTHER;
}
 
Example #15
Source File: TypeLibrary.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private static ValueDescriptor createStartTimeField() {
    List<AnnotationElement> annos = createStandardAnnotations("Start Time", null);
    annos.add(new jdk.jfr.AnnotationElement(Timestamp.class, Timestamp.TICKS));
    return PrivateAccess.getInstance().newValueDescriptor(EventInstrumentation.FIELD_START_TIME, Type.LONG, annos, 0, false,
            EventInstrumentation.FIELD_START_TIME);

}
 
Example #16
Source File: XMLWriter.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public void printObject(RecordedObject struct) {
    println();
    indent();
    for (ValueDescriptor v : struct.getFields()) {
        printValueDescriptor(v, getValue(struct, v), -1);
    }
    retract();
}
 
Example #17
Source File: TestRelational.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    EventType t = EventType.getEventType(UserEvent.class);
    ValueDescriptor field = t.getField("id");
    AnnotationElement userId = Events.getAnnotation(field, UserId.class);
    Relational r = userId.getAnnotation(Relational.class);
    Asserts.assertTrue(r != null, "Expected relational annotation to have annotation @Relational");
}
 
Example #18
Source File: TestMetadata.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    EventType t = EventType.getEventType(MetadataEvent.class);
    ValueDescriptor field = t.getField("field");
    Events.hasAnnotation(field, FirstJFRAnnotation.class);
    Asserts.assertTrue(field.getAnnotation(OtherAnnotation.class) == null, "Only annotation annotated with @Metadata should exist");

    AnnotationElement a = Events.getAnnotationByName(t, FirstJFRAnnotation.class.getName());
    Asserts.assertTrue(a.getAnnotation(SecondJFRAnnotation.class) != null , "Annotations with @Metadata should be followed for indirect annotations");
}
 
Example #19
Source File: TestDynamicAnnotations.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void testECID() throws Exception {
    List<ValueDescriptor> fields = new ArrayList<>();

    List<AnnotationElement> fieldAnnotations = new ArrayList<>();
    fieldAnnotations.add(new AnnotationElement(ECID.class));
    ValueDescriptor ecidField = new ValueDescriptor(String.class, "ecid", fieldAnnotations);
    fields.add(ecidField);

    EventFactory f = EventFactory.create(fieldAnnotations, fields);

    String ecidValue = "131739871298371279812";
    try (Recording r = new Recording()) {
        r.start();
        Event event = f.newEvent();
        event.set(0, ecidValue);
        event.commit();
        r.stop();
        List<RecordedEvent> events = Events.fromRecording(r);
        Events.hasEvents(events);
        Events.assertField(events.get(0), "ecid").equal(ecidValue);
    }
    EventType type = f.getEventType();
    ECID e = type.getAnnotation(ECID.class);
    if (e == null) {
        throw new Exception("Missing ECID annotation");
    }
}
 
Example #20
Source File: RecordedClassLoader.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
static ObjectFactory<RecordedClassLoader> createFactory(Type type, TimeConverter timeConverter) {
    return new ObjectFactory<RecordedClassLoader>(type) {
        @Override
        RecordedClassLoader createTyped(List<ValueDescriptor> desc, long id, Object[] object) {
            return new RecordedClassLoader(desc, id, object, timeConverter);
        }
    };
}
 
Example #21
Source File: TestFieldAnnotations.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    EventType t = EventType.getEventType(FieldAnnotationEvent.class);

    ValueDescriptor field = t.getField("memoryAmount");
    Events.hasAnnotation(field, DataAmount.class);

    field = t.getField("frequency");
    Events.hasAnnotation(field, Frequency.class);

    field = t.getField("memoryAddress");
    Events.hasAnnotation(field, MemoryAddress.class);

    field = t.getField("percentage");
    Events.hasAnnotation(field, Percentage.class);

    field = t.getField("fromThread");
    Events.hasAnnotation(field, TransitionFrom.class);

    field = t.getField("toThread");
    Events.hasAnnotation(field, TransitionTo.class);

    field = t.getField("unsigned");
    Events.hasAnnotation(field, Unsigned.class);

    field = t.getField("timespan");
    Events.assertAnnotation(field, Timespan.class, Timespan.MILLISECONDS);

    field = t.getField("timestamp");
    Events.assertAnnotation(field, Timestamp.class, Timestamp.MILLISECONDS_SINCE_EPOCH);
}
 
Example #22
Source File: JSONWriter.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private void printEvent(RecordedEvent event) {
    printObjectBegin();
    EventType type = event.getEventType();
    printValue(true, false, "type", type.getName());
    printNewDataStructure(false, false, "values");
    printObjectBegin();
    boolean first = true;
    for (ValueDescriptor v : event.getFields()) {
        printValueDescriptor(first, false, v, getValue(event, v));
        first = false;
    }
    printObjectEnd();
    printObjectEnd();
}
 
Example #23
Source File: TestIsArray.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Throwable {
    EventType type = EventType.getEventType(EventWithArray.class);

    ValueDescriptor d = type.getField("myIds");

    Asserts.assertNull(d, "ValueDescriptor for int[] was not null");

    type = EventType.getEventType(EventWithoutArray.class);
    d = type.getField("myId");
    Asserts.assertFalse(d.isArray(), "isArray() was true for int");
}
 
Example #24
Source File: TypeLibrary.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
static AnnotationElement createAnnotation(Annotation annotation) {
    Class<? extends Annotation> annotationType = annotation.annotationType();
    Type type = createAnnotationType(annotationType);
    if (type != null) {
        List<Object> values = new ArrayList<>();
        for (ValueDescriptor v : type.getFields()) {
            values.add(invokeAnnotation(annotation, v.getName()));
        }

        return PrivateAccess.getInstance().newAnnotation(type, values, annotation.annotationType().getClassLoader() == null);
    }
    return null;
}
 
Example #25
Source File: TestEventFactory.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static EventTypePrototype makeEventType(String eventName) {
    EventTypePrototype prototype = new EventTypePrototype(eventName);
    prototype.addAnnotation(new AnnotationElement(TestAnnotation.class, "type"));
    for (Map.Entry<String, Object> entry : EVENT_VALUES.entrySet()) {
        Class<?> type = makePrimitive(entry.getValue().getClass());
        String fieldName = entry.getKey();
        prototype.addField(new ValueDescriptor(type, fieldName));
    }
    // add an annotated field
    List<AnnotationElement> annos = new ArrayList<>();
    annos.add(new AnnotationElement(TestAnnotation.class, "field"));
    prototype.addField( new ValueDescriptor(int.class, "annotatedField", annos));

    return prototype;
}
 
Example #26
Source File: JSONWriter.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public void printObject(RecordedObject object) {
    printObjectBegin();
    boolean first = true;
    for (ValueDescriptor v : object.getFields()) {
        printValueDescriptor(first, false, v, getValue(object, v));
        first = false;
    }
    printObjectEnd();
}
 
Example #27
Source File: GCHelper.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static boolean isGcEvent(RecordedEvent event) {
    for (ValueDescriptor v : event.getFields()) {
        if ("gcId".equals(v.getName())) {
            return true;
        }
    }
    return false;
}
 
Example #28
Source File: TestEventMetadata.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private static void verifyValueDesscriptors(List<ValueDescriptor> fields, Set<String> visitedTypes) {
    for (ValueDescriptor v : fields) {
        if (!visitedTypes.contains(v.getTypeName())) {
            visitedTypes.add(v.getTypeName());
            verifyValueDesscriptors(v.getFields(), visitedTypes);
        }
        verifyValueDescriptor(v);
    }
}
 
Example #29
Source File: ASMToolkit.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static Type toType(ValueDescriptor v) {
    String typeName = v.getTypeName();

    switch (typeName) {
    case "byte":
        return Type.BYTE_TYPE;
    case "short":
        return Type.SHORT_TYPE;
    case "int":
        return Type.INT_TYPE;
    case "long":
        return Type.LONG_TYPE;
    case "double":
        return Type.DOUBLE_TYPE;
    case "float":
        return Type.FLOAT_TYPE;
    case "char":
        return Type.CHAR_TYPE;
    case "boolean":
        return Type.BOOLEAN_TYPE;
    case "java.lang.String":
        return TYPE_STRING;
    case "java.lang.Thread":
        return Type_THREAD;
    case "java.lang.Class":
        return TYPE_CLASS;
    }
    // Add support for SettingControl?
   throw new Error("Not a valid type " + v.getTypeName());
}
 
Example #30
Source File: TypeLibrary.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private static ValueDescriptor createStartTimeField() {
    List<AnnotationElement> annos = createStandardAnnotations("Start Time", null);
    annos.add(new jdk.jfr.AnnotationElement(Timestamp.class, Timestamp.TICKS));
    return PrivateAccess.getInstance().newValueDescriptor(EventInstrumentation.FIELD_START_TIME, Type.LONG, annos, 0, false,
            EventInstrumentation.FIELD_START_TIME);

}