Java Code Examples for jdk.jfr.consumer.RecordedObject#getValue()

The following examples show how to use jdk.jfr.consumer.RecordedObject#getValue() . 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: Events.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
public static EventField assertField(RecordedEvent event, String name)  {
    String[] partNames = name.split("\\.");
    RecordedObject struct = event;
    try {
        for (int i=0; i<partNames.length; ++i) {
            final String partName =  partNames[i];
            final boolean isLastPart = i == partNames.length - 1;
            ValueDescriptor d = getValueDescriptor(struct, partName);
            if (isLastPart) {
                return new EventField(struct, d);
            } else {
                assertTrue(struct.getValue(partName) instanceof RecordedObject, "Expected '" + partName + "' to be a struct");
                struct = struct.getValue(partName);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    System.out.printf("Failed event:%n%s%n", event.toString());
    fail(String.format("Field %s not in event", name));
    return null;
}
 
Example 2
Source File: TestAllocationTime.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static RecordedObject findLeak(List<RecordedEvent> events, String name) throws Exception {
    for (RecordedEvent e : events) {
        if (e.getEventType().getName().equals(EventNames.OldObjectSample)) {
            RecordedObject object = e.getValue("object");
            RecordedClass rc = object.getValue("type");
            if (rc.getName().equals(Leak[].class.getName())) {
                return e;
            }
        }
    }
    return null;
}
 
Example 3
Source File: EventPrintWriter.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
protected Object getValue(RecordedObject object, ValueDescriptor v) {
    ValueType valueType = typeOfValues.get(v);
    if (valueType == null) {
        valueType = determineValueType(v);
        typeOfValues.put(v, valueType);
    }
    switch (valueType) {
    case TIMESPAN:
        return object.getDuration(v.getName());
    case TIMESTAMP:
        return RecordingInternals.INSTANCE.getOffsetDataTime(object, v.getName());
    default:
        return object.getValue(v.getName());
    }
}
 
Example 4
Source File: PrettyWriter.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private void printReferenceChain(RecordedObject object) {
    printObject(object, currentEvent.getLong("arrayElements"));
    for (RecordedObject ref = object.getValue("referrer"); ref != null; ref = object.getValue("referrer")) {
        long skip = ref.getLong("skip");
        if (skip > 0) {
            printIndent();
            println("...");
        }
        String objectHolder = "";
        long size = Long.MIN_VALUE;
        RecordedObject array = ref.getValue("array");
        if (array != null) {
            long index = array.getLong("index");
            size = array.getLong("size");
            objectHolder = "[" + index + "]";
        }
        RecordedObject field = ref.getValue("field");
        if (field != null) {
            objectHolder = field.getString("name");
        }
        printIndent();
        print(objectHolder);
        print(" : ");
        object = ref.getValue("object");
        if (object != null) {
            printObject(object, size);
        }
    }
}
 
Example 5
Source File: Events.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Validates the recored name field
 *
 * @param ro should be a Package or a Module
 * @param targetName name to match
 */
private static boolean isMatchingTargetName(final RecordedObject ro, final String targetName) {
    if (ro == null) {
        return targetName == null;
    }

    final String recordedName = ro.getValue("name");

    if (recordedName == null) {
        return targetName == null;
    }

    return recordedName.equals(targetName);
}
 
Example 6
Source File: PrettyWriter.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private void printReferenceChain(RecordedObject object) {
    printObject(object, currentEvent.getLong("arrayElements"));
    for (RecordedObject ref = object.getValue("referrer"); ref != null; ref = object.getValue("referrer")) {
        long skip = ref.getLong("skip");
        if (skip > 0) {
            printIndent();
            println("...");
        }
        String objectHolder = "";
        long size = Long.MIN_VALUE;
        RecordedObject array = ref.getValue("array");
        if (array != null) {
            long index = array.getLong("index");
            size = array.getLong("size");
            objectHolder = "[" + index + "]";
        }
        RecordedObject field = ref.getValue("field");
        if (field != null) {
            objectHolder = field.getString("name");
        }
        printIndent();
        print(objectHolder);
        print(" : ");
        object = ref.getValue("object");
        if (object != null) {
            printObject(object, size);
        }
    }
}
 
Example 7
Source File: OldObjects.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void validateReferenceChainLimit(RecordedEvent e, int maxLength) {
    int length = 0;
    RecordedObject object = e.getValue("object");
    while (object != null) {
        ++length;
        RecordedObject referrer = object.getValue("referrer");
        object = referrer != null ? referrer.getValue("object") : null;
    }
    if (length > maxLength) {
        throw new RuntimeException("Reference chain max length not respected. Found a chain of length " + length);
    }
}
 
Example 8
Source File: Events.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Validates the recored name field
 *
 * @param ro should be a Package or a Module
 * @param targetName name to match
 */
private static boolean isMatchingTargetName(final RecordedObject ro, final String targetName) {
    if (ro == null) {
        return targetName == null;
    }

    final String recordedName = ro.getValue("name");

    if (recordedName == null) {
        return targetName == null;
    }

    return recordedName.equals(targetName);
}
 
Example 9
Source File: TestAllocationTime.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private static RecordedObject findLeak(List<RecordedEvent> events, String name) throws Exception {
    for (RecordedEvent e : events) {
        if (e.getEventType().getName().equals(EventNames.OldObjectSample)) {
            RecordedObject object = e.getValue("object");
            RecordedClass rc = object.getValue("type");
            if (rc.getName().equals(Leak[].class.getName())) {
                return e;
            }
        }
    }
    return null;
}
 
Example 10
Source File: TestLargeRootSet.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 {
    WhiteBox.setWriteAllObjectSamples(true);

    List<RootThread> threads = new ArrayList<>();
    try (Recording r = new Recording()) {
        r.enable(EventNames.OldObjectSample).withStackTrace().with("cutoff", "infinity");
        r.start();
        CyclicBarrier cb = new CyclicBarrier(THREAD_COUNT + 1);
        for (int i = 0; i < THREAD_COUNT; i++) {
            RootThread t = new RootThread(cb);
            t.start();
            if (i % 10 == 0) {
                // Give threads some breathing room before starting next batch
                Thread.sleep(100);
            }
            threads.add(t);
        }
        cb.await();
        System.gc();
        r.stop();
        cb.await();
        List<RecordedEvent> events = Events.fromRecording(r);
        Events.hasEvents(events);
        for (RecordedEvent e : events) {
            RecordedObject ro = e.getValue("object");
            RecordedClass rc = ro.getValue("type");
            System.out.println(rc.getName());
            if (rc.getName().equals(StackObject[].class.getName())) {
                return; // ok
            }
        }
        Asserts.fail("Could not find root object");
    }
}
 
Example 11
Source File: Events.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private static RecordedObject getRecordedModule(final RecordedObject pkg) {
    if (pkg == null) {
        // null package is an unnamed module (null)
        return null;
    }

    return pkg.getValue("module");
}
 
Example 12
Source File: OldObjects.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public static void validateReferenceChainLimit(RecordedEvent e, int maxLength) {
    int length = 0;
    RecordedObject object = e.getValue("object");
    while (object != null) {
        ++length;
        RecordedObject referrer = object.getValue("referrer");
        object = referrer != null ? referrer.getValue("object") : null;
    }
    if (length > maxLength) {
        throw new RuntimeException("Reference chain max length not respected. Found a chain of length " + length);
    }
}
 
Example 13
Source File: TestFieldInformation.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static boolean hasValidField(RecordedEvent e) throws Exception {
    RecordedObject object = e.getValue("object");
    Set<Long> visited = new HashSet<>();
    while (object != null) {
        Long address = object.getValue("address");
        if (visited.contains(address)) {
            return false;
        }
        visited.add(address);
        RecordedObject referrer = object.getValue("referrer");
        RecordedObject fieldObject = referrer != null ? referrer.getValue("field") : null;
        if (fieldObject != null) {
            String name = fieldObject.getValue("name");
            if (name != null && name.equals("testField")) {
                int modifiers = (short) fieldObject.getValue("modifiers");
                if (!Modifier.isStatic(modifiers)) {
                    throw new Exception("Field should be static");
                }
                if (!Modifier.isPublic(modifiers)) {
                    throw new Exception("Field should be private");
                }
                if (!Modifier.isFinal(modifiers)) {
                    throw new Exception("Field should be final");
                }
                if (Modifier.isTransient(modifiers)) {
                    throw new Exception("Field should not be transient");
                }
                if (Modifier.isVolatile(modifiers)) {
                    throw new Exception("Field should not be volatile");
                }
                return true;
            }
        }
        object = referrer != null ? referrer.getValue("object") : null;
    }
    return false;
}
 
Example 14
Source File: TestArrayInformation.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static void verifyObjectArray(List<RecordedEvent> events) throws Exception {
    for (RecordedEvent e : events) {
        RecordedObject object = e.getValue("object");
        RecordedClass objectType = object.getValue("type");
        RecordedObject referrer = object.getValue("referrer");
        System.out.println(objectType.getName());
        if (objectType.getName().equals(ArrayLeak[].class.getName())) {
            for (int i = 0; i < CHAIN_DEPTH; i++) {
                object = referrer.getValue("object");
                if (object == null) {
                    throw new Exception("Expected referrer object");
                }
                objectType = object.getValue("type");
                if (!objectType.getName().equals(Object[].class.getName())) {
                    throw new Exception("Expect array class to be named " + Object[].class + " but found " + objectType.getName());
                }
                RecordedObject field = referrer.getValue("field");
                if (field != null) {
                    throw new Exception("Didn't expect to find field");
                }
                RecordedObject array = referrer.getValue("array");
                if (array == null) {
                    throw new Exception("Expected array object, but got null");
                }
                int index = referrer.getValue("array.index");
                if (index != ARRAY_INDEX) {
                    throw new Exception("Expected array index: " + ARRAY_INDEX + ", but got " + index);
                }
                int size = referrer.getValue("array.size");
                if (size != ARRAY_SIZE) {
                    throw new Exception("Expected array size: " + ARRAY_SIZE + ", but got " + size);
                }
                referrer = object.getValue("referrer");
            }
            return;
        }
    }
    throw new Exception("Could not find event with " + ArrayLeak[].class + " as (leak) object");
}
 
Example 15
Source File: TestRecordedObject.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private static void testGetClass(RecordedObject e) {
    RecordedClass clazz = e.getValue("classField");
    if (!clazz.getName().equals(CLASS_VALUE.getName())) {
        throw new AssertionError("Expected class to have name " + CLASS_VALUE.getName());
    }
    assertGetter(x -> e.getClass(x), clazz, "class");
}
 
Example 16
Source File: TestClassLoaderLeak.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 {
    WhiteBox.setWriteAllObjectSamples(true);

    try (Recording r = new Recording()) {
        r.enable(EventNames.OldObjectSample).withStackTrace().with("cutoff", "infinity");
        r.start();
        TestClassLoader testClassLoader = new TestClassLoader();
        for (Class<?> clazz : testClassLoader.loadClasses(OldObjects.MIN_SIZE / 20)) {
            // Allocate array to trigger sampling code path for interpreter / c1
            for (int i = 0; i < 20; i++) {
                Object classArray = Array.newInstance(clazz, 20);
                Array.set(classArray, i, clazz.newInstance());
                classObjects.add(classArray);
            }
        }
        r.stop();
        List<RecordedEvent> events = Events.fromRecording(r);
        Events.hasEvents(events);
        for (RecordedEvent e : events) {
            RecordedObject object = e.getValue("object");
            RecordedClass rc = object.getValue("type");
            if (rc.getName().contains("TestClass")) {
                return;
            }
        }
        Asserts.fail("Could not find class leak");
    }
}
 
Example 17
Source File: TestJcmdDumpPathToGCRoots.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private static boolean hasChains(List<RecordedEvent> events) throws IOException {
    for (RecordedEvent e : events) {
        RecordedObject ro = e.getValue("object");
        if (ro.getValue("referrer") != null) {
            return true;
        }
    }
    return false;
}
 
Example 18
Source File: TestPrintXML.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
static boolean compare(Object eventObject, Object xmlObject) {
    if (eventObject == null) {
        return xmlObject == null;
    }
    if (eventObject instanceof RecordedObject) {
        RecordedObject re = (RecordedObject) eventObject;
        Map<String, Object> xmlMap = (Map<String, Object>) xmlObject;
        List<ValueDescriptor> fields = re.getFields();
        if (fields.size() != xmlMap.size()) {
            return false;
        }
        for (ValueDescriptor v : fields) {
            String name = v.getName();
            Object xmlValue = xmlMap.get(name);
            Object expectedValue = re.getValue(name);
            if (v.getAnnotation(Timestamp.class) != null) {
                // Make instant of OffsetDateTime
                xmlValue = OffsetDateTime.parse("" + xmlValue).toInstant().toString();
                expectedValue = re.getInstant(name);
            }
            if (v.getAnnotation(Timespan.class) != null) {
                expectedValue = re.getDuration(name);
            }
            if (!compare(expectedValue, xmlValue)) {
                return false;
            }
        }
        return true;
    }
    if (eventObject.getClass().isArray()) {
        Object[] array = (Object[]) eventObject;
        Object[] xmlArray = (Object[]) xmlObject;
        if (array.length != xmlArray.length) {
            return false;
        }
        for (int i = 0; i < array.length; i++) {
            if (!compare(array[i], xmlArray[i])) {
                return false;
            }
        }
        return true;
    }
    String s1 = String.valueOf(eventObject);
    String s2 = (String) xmlObject;
    return s1.equals(s2);
}
 
Example 19
Source File: TestCompilerInlining.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
private static MethodDesc ciMethodToMethodDesc(RecordedObject ciMethod) {
    String internalClassName = ciMethod.getValue("type");
    String methodName = ciMethod.getValue("name");
    String methodDescriptor = ciMethod.getValue("descriptor");
    return new MethodDesc(internalClassName, methodName, methodDescriptor);
}
 
Example 20
Source File: TestAllocationTime.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    WhiteBox.setWriteAllObjectSamples(true);

    while(true) {
        try (Recording recording = new Recording()) {
            leak.clear();
            recording.enable(EventNames.OldObjectSample).withStackTrace().with("cutoff", "1 h");
            recording.start();

            BeforeLeakEvent be = new BeforeLeakEvent();
            be.commit();

            // Allocate array to trigger sampling code path for interpreter / c1
            for (int i = 0; i < OldObjects.MIN_SIZE; i++) {
                leak.add(new Leak[0]);
            }

            AfterLeakEvent ae = new AfterLeakEvent();
            ae.commit();

            recording.stop();

            List<RecordedEvent> events = Events.fromRecording(recording);
            Events.hasEvents(events);
            RecordedObject sample = findLeak(events, BeforeLeakEvent.class.getName());
            if (sample != null)  {
                long beforeTime = find(events, BeforeLeakEvent.class.getName()).getValue("startTime");
                long allocationTime = sample.getValue("allocationTime");
                long afterTime = find(events, AfterLeakEvent.class.getName()).getValue("startTime");
                System.out.println("Before time     : " + beforeTime);
                System.out.println("Allocation time : " + allocationTime);
                System.out.println("After time      : " + afterTime);

                if (allocationTime < beforeTime) {
                    throw new Exception("Allocation should not happen this early");
                }
                if (allocationTime > afterTime) {
                    throw new Exception("Allocation should not happen this late");
                }
                return; // sample ok
            }
        }
    }
}