Java Code Examples for jdk.jfr.consumer.RecordedEvent#getThread()

The following examples show how to use jdk.jfr.consumer.RecordedEvent#getThread() . 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: TestRecordedEventGetThread.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 {
    Thread currentThread = Thread.currentThread();
    currentThread.setName(MY_THREAD_NAME);
    long expectedThreadId = currentThread.getId();

    Recording r = new Recording();
    r.start();
    SimpleEvent t = new SimpleEvent();
    t.commit();
    r.stop();
    List<RecordedEvent> events = Events.fromRecording(r);
    r.close();
    Events.hasEvents(events);
    RecordedEvent event = events.get(0);
    RecordedThread recordedThread = event.getThread();

    Asserts.assertNotNull(recordedThread);
    Asserts.assertEquals(recordedThread.getJavaName(), MY_THREAD_NAME);
    Asserts.assertEquals(recordedThread.getJavaThreadId(), expectedThreadId);
    Asserts.assertNotNull(recordedThread.getOSThreadId());
    Asserts.assertNotNull(recordedThread.getId());
    Asserts.assertEquals(recordedThread.getOSName(), MY_THREAD_NAME);
}
 
Example 2
Source File: TestMetadataRetention.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private static void validateOldObjectEvent(List<RecordedEvent> events, Instant chunkRotation) throws Throwable {
    for (RecordedEvent event : events) {
        if (event.getEventType().getName().equals(EventNames.OldObjectSample)) {
            // Only check event after the rotation
            if (!event.getStartTime().isBefore(chunkRotation)) {
                System.out.println(event);
                RecordedThread rt = event.getThread();
                if (rt.getJavaName().equals(ALLOCATOR_THREAD_NAME)) {
                    RecordedStackTrace s = event.getStackTrace();
                    assertStackTrace(s, "startRecurse");
                    assertStackTrace(s, "recurse");
                    return;
                }
            }
        }
    }

    Asserts.fail("Did not find an old object event with thread " + ALLOCATOR_THREAD_NAME);
}
 
Example 3
Source File: TestExceptionEvents.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private static void checkThrowableEvents(List<RecordedEvent> events, String eventName,
    int excpectedEvents, Class<?> expectedClass, String expectedMessage) throws Exception {
    int count = 0;
    for(RecordedEvent event : events) {
        if (Events.isEventType(event, eventName)) {
            String message = Events.assertField(event, "message").getValue();
            if (expectedMessage.equals(message)) {
                RecordedThread t = event.getThread();
                String threadName = t.getJavaName();
                if (threadName != null && threadName.equals(Thread.currentThread().getName())) {
                    RecordedClass jc = event.getValue("thrownClass");
                    if (jc.getName().equals(expectedClass.getName())) {
                        count++;
                    }
                }
            }
        }
    }
    Asserts.assertEquals(count, excpectedEvents, "Wrong event count for type " + eventName);
}
 
Example 4
Source File: TestMetadataRetention.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
private static void validateOldObjectEvent(List<RecordedEvent> events, Instant chunkRotation) throws Throwable {
    for (RecordedEvent event : events) {
        if (event.getEventType().getName().equals(EventNames.OldObjectSample)) {
            // Only check event after the rotation
            if (!event.getStartTime().isBefore(chunkRotation)) {
                System.out.println(event);
                RecordedThread rt = event.getThread();
                if (rt.getJavaName().equals(ALLOCATOR_THREAD_NAME)) {
                    RecordedStackTrace s = event.getStackTrace();
                    assertStackTrace(s, "startRecurse");
                    assertStackTrace(s, "recurse");
                    return;
                }
            }
        }
    }

    Asserts.fail("Did not find an old object event with thread " + ALLOCATOR_THREAD_NAME);
}
 
Example 5
Source File: TestRecordedEventGetThread.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Throwable {
    Thread currentThread = Thread.currentThread();
    currentThread.setName(MY_THREAD_NAME);
    long expectedThreadId = currentThread.getId();

    Recording r = new Recording();
    r.start();
    SimpleEvent t = new SimpleEvent();
    t.commit();
    r.stop();
    List<RecordedEvent> events = Events.fromRecording(r);
    r.close();
    Events.hasEvents(events);
    RecordedEvent event = events.get(0);
    RecordedThread recordedThread = event.getThread();

    Asserts.assertNotNull(recordedThread);
    Asserts.assertEquals(recordedThread.getJavaName(), MY_THREAD_NAME);
    Asserts.assertEquals(recordedThread.getJavaThreadId(), expectedThreadId);
    Asserts.assertNotNull(recordedThread.getOSThreadId());
    Asserts.assertNotNull(recordedThread.getId());
    Asserts.assertEquals(recordedThread.getOSName(), MY_THREAD_NAME);
}
 
Example 6
Source File: TestRecordedEventGetThreadOther.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Throwable {
    Thread.currentThread().setName("MyMainThread");

    PostingThread thread = new PostingThread();
    thread.start();
    thread.join();
    System.out.println("testing dump in file " + dumpFilePath);

    List<RecordedEvent> events = RecordingFile.readAllEvents(dumpFilePath);
    Asserts.assertEquals(events.size(), 1);

    RecordedEvent event = events.get(0);
    RecordedThread recordedThread = event.getThread();

    Asserts.assertNotNull(recordedThread);
    Asserts.assertEquals(recordedThread.getJavaName(), MY_THREAD_NAME);
    Asserts.assertEquals(recordedThread.getJavaThreadId(), expectedThreadId);
    Asserts.assertNotNull(recordedThread.getId());
    Asserts.assertEquals(recordedThread.getOSName(), MY_THREAD_NAME);
}
 
Example 7
Source File: PrettyWriter.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
public void print(RecordedEvent event) {
    currentEvent = event;
    print(event.getEventType().getName(), " ");
    println("{");
    indent();
    for (ValueDescriptor v : event.getFields()) {
        String name = v.getName();
        if (!isZeroDuration(event, name) && !isLateField(name)) {
            printFieldValue(event, v);
        }
    }
    if (event.getThread() != null) {
        printIndent();
        print(EVENT_THREAD_FIELD + " = ");
        printThread(event.getThread(), "");
    }
    if (event.getStackTrace() != null) {
        printIndent();
        print(STACK_TRACE_FIELD + " = ");
        printStackTrace(event.getStackTrace());
    }
    retract();
    printIndent();
    println("}");
    println();
}
 
Example 8
Source File: TestMetadataRetention.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
private static void validateOldObjectEvent(List<RecordedEvent> events, Instant chunkRotation) throws Throwable {
    for (RecordedEvent event : events) {
        if (event.getEventType().getName().equals(EventNames.OldObjectSample)) {
            // Only check event after the rotation
            if (!event.getStartTime().isBefore(chunkRotation)) {
                System.out.println(event);
                RecordedThread rt = event.getThread();
                if (rt.getJavaName().equals(ALLOCATOR_THREAD_NAME)) {
                    RecordedStackTrace s = event.getStackTrace();
                    assertStackTrace(s, "startRecurse");
                    assertStackTrace(s, "recurse");
                    return;
                }
            }
        }
    }

    Asserts.fail("Did not find an old object event with thread " + ALLOCATOR_THREAD_NAME);
}
 
Example 9
Source File: TestJcmdStartWithSettings.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
private static void assertHasEvent(File file, String eventType, String threadName) throws Exception {
    for (RecordedEvent event : RecordingFile.readAllEvents(file.toPath())) {
        if (Events.isEventType(event, eventType)) {
            System.out.println(event);
            RecordedThread t = event.getThread();
            if (t == null) {
                throw new Exception("Thread null for event " + eventType);
            }
            if (threadName.equals(t.getJavaName())) {
                System.out.println("Found event: " + event);
                return;
            }
        }
    }
    Asserts.fail("No events of type " + eventType);
}
 
Example 10
Source File: TestRecordedEventGetThreadOther.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 {
    Thread.currentThread().setName("MyMainThread");

    PostingThread thread = new PostingThread();
    thread.start();
    thread.join();
    System.out.println("testing dump in file " + dumpFilePath);

    List<RecordedEvent> events = RecordingFile.readAllEvents(dumpFilePath);
    Asserts.assertEquals(events.size(), 1);

    RecordedEvent event = events.get(0);
    RecordedThread recordedThread = event.getThread();

    Asserts.assertNotNull(recordedThread);
    Asserts.assertEquals(recordedThread.getJavaName(), MY_THREAD_NAME);
    Asserts.assertEquals(recordedThread.getJavaThreadId(), expectedThreadId);
    Asserts.assertNotNull(recordedThread.getId());
    Asserts.assertEquals(recordedThread.getOSName(), MY_THREAD_NAME);
}
 
Example 11
Source File: StressAllocationGCEvents.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private void checkEvent(RecordedEvent event) throws Exception {
    // skip check if allocation failure comes not from diver

    RecordedThread thread = event.getThread();
    String threadName = thread.getJavaName();

    if (!threadName.contains(THREAD_NAME)) {
        System.out.println("Skip event not from pool (from internals)");
        System.out.println(" Thread Id: " + thread.getJavaThreadId()
                + " Thread name: " + threadName);
        return;
    }

    RecordedStackTrace stackTrace = event.getStackTrace();

    List<RecordedFrame> frames = stackTrace.getFrames();
    //String[] stacktrace = StackTraceHelper.buildStackTraceFromFrames(frames);

    if (!(frames.get(0).getMethod().getName().equals(DIVER_FRAME_NAME))) {
        System.out.println("Skip stacktrace check for: \n"
                + String.join("\n", threadName));
        return;
    }

    assertTrue(frames.size() > RECURSION_DEPTH,
            "Stack trace should contain at least one more entry than the ones generated by the test recursion");
    for (int i = 0; i < RECURSION_DEPTH; i++) {
        assertEquals(frames.get(i).getMethod().getName(), DIVER_FRAME_NAME,
                "Frame " + i + " is wrong: \n"
                + String.join("\n", threadName));
    }
    assertNotEquals(frames.get(RECURSION_DEPTH).getMethod().getName(), DIVER_FRAME_NAME,
            "Too many diver frames: \n"
            + String.join("\n", threadName));
}
 
Example 12
Source File: Events.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void assertEventThread(RecordedEvent event) {
    RecordedThread eventThread = event.getThread();
    if (eventThread == null) {
        System.out.printf("Failed event:%n%s%n", event.toString());
        fail("No thread in event");
    }
}
 
Example 13
Source File: StressAllocationGCEvents.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private void checkEvent(RecordedEvent event) throws Exception {
    // skip check if allocation failure comes not from diver

    RecordedThread thread = event.getThread();
    String threadName = thread.getJavaName();

    if (!threadName.contains(THREAD_NAME)) {
        System.out.println("Skip event not from pool (from internals)");
        System.out.println(" Thread Id: " + thread.getJavaThreadId()
                + " Thread name: " + threadName);
        return;
    }

    RecordedStackTrace stackTrace = event.getStackTrace();

    List<RecordedFrame> frames = stackTrace.getFrames();
    //String[] stacktrace = StackTraceHelper.buildStackTraceFromFrames(frames);

    if (!(frames.get(0).getMethod().getName().equals(DIVER_FRAME_NAME))) {
        System.out.println("Skip stacktrace check for: \n"
                + String.join("\n", threadName));
        return;
    }

    assertTrue(frames.size() > RECURSION_DEPTH,
            "Stack trace should contain at least one more entry than the ones generated by the test recursion");
    for (int i = 0; i < RECURSION_DEPTH; i++) {
        assertEquals(frames.get(i).getMethod().getName(), DIVER_FRAME_NAME,
                "Frame " + i + " is wrong: \n"
                + String.join("\n", threadName));
    }
    assertNotEquals(frames.get(RECURSION_DEPTH).getMethod().getName(), DIVER_FRAME_NAME,
            "Too many diver frames: \n"
            + String.join("\n", threadName));
}
 
Example 14
Source File: StressAllocationGCEvents.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private void checkEvent(RecordedEvent event) throws Exception {
    // skip check if allocation failure comes not from diver

    RecordedThread thread = event.getThread();
    String threadName = thread.getJavaName();

    if (!threadName.contains(THREAD_NAME)) {
        System.out.println("Skip event not from pool (from internals)");
        System.out.println(" Thread Id: " + thread.getJavaThreadId()
                + " Thread name: " + threadName);
        return;
    }

    RecordedStackTrace stackTrace = event.getStackTrace();

    List<RecordedFrame> frames = stackTrace.getFrames();
    //String[] stacktrace = StackTraceHelper.buildStackTraceFromFrames(frames);

    if (!(frames.get(0).getMethod().getName().equals(DIVER_FRAME_NAME))) {
        System.out.println("Skip stacktrace check for: \n"
                + String.join("\n", threadName));
        return;
    }

    assertTrue(frames.size() > RECURSION_DEPTH,
            "Stack trace should contain at least one more entry than the ones generated by the test recursion");
    for (int i = 0; i < RECURSION_DEPTH; i++) {
        assertEquals(frames.get(i).getMethod().getName(), DIVER_FRAME_NAME,
                "Frame " + i + " is wrong: \n"
                + String.join("\n", threadName));
    }
    assertNotEquals(frames.get(RECURSION_DEPTH).getMethod().getName(), DIVER_FRAME_NAME,
            "Too many diver frames: \n"
            + String.join("\n", threadName));
}
 
Example 15
Source File: Events.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public static void assertEventThread(RecordedEvent event) {
    RecordedThread eventThread = event.getThread();
    if (eventThread == null) {
        System.out.printf("Failed event:%n%s%n", event.toString());
        fail("No thread in event");
    }
}
 
Example 16
Source File: StressAllocationGCEvents.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private void checkEvent(RecordedEvent event) throws Exception {
    // skip check if allocation failure comes not from diver

    RecordedThread thread = event.getThread();
    String threadName = thread.getJavaName();

    if (!threadName.contains(THREAD_NAME)) {
        System.out.println("Skip event not from pool (from internals)");
        System.out.println(" Thread Id: " + thread.getJavaThreadId()
                + " Thread name: " + threadName);
        return;
    }

    RecordedStackTrace stackTrace = event.getStackTrace();

    List<RecordedFrame> frames = stackTrace.getFrames();
    //String[] stacktrace = StackTraceHelper.buildStackTraceFromFrames(frames);

    if (!(frames.get(0).getMethod().getName().equals(DIVER_FRAME_NAME))) {
        System.out.println("Skip stacktrace check for: \n"
                + String.join("\n", threadName));
        return;
    }

    assertTrue(frames.size() > RECURSION_DEPTH,
            "Stack trace should contain at least one more entry than the ones generated by the test recursion");
    for (int i = 0; i < RECURSION_DEPTH; i++) {
        assertEquals(frames.get(i).getMethod().getName(), DIVER_FRAME_NAME,
                "Frame " + i + " is wrong: \n"
                + String.join("\n", threadName));
    }
    assertNotEquals(frames.get(RECURSION_DEPTH).getMethod().getName(), DIVER_FRAME_NAME,
            "Too many diver frames: \n"
            + String.join("\n", threadName));
}
 
Example 17
Source File: TestThreadCpuTimeEvent.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
static void testEventAtThreadExit() throws Throwable {
    Recording recording = new Recording();

    recording.enable(EventNames.ThreadCPULoad).withPeriod(Duration.ofHours(10));
    recording.start();

    Duration testRunTime = Duration.ofMillis(eventPeriodMillis * cpuConsumerRunFactor);
    CyclicBarrier barrier = new CyclicBarrier(2);
    CpuConsumingThread thread = new CpuConsumingThread(testRunTime, barrier);

    // Run a single pass
    thread.start();
    barrier.await();
    barrier.await();

    thread.interrupt();
    thread.join();

    recording.stop();

    List<RecordedEvent> events = Events.fromRecording(recording);
    verifyPerThreadInvariant(events, cpuConsumerThreadName);

    int exitingCount = 0;
    for (RecordedEvent event : events) {
        RecordedThread eventThread = event.getThread();
        if (eventThread.getJavaName().equals(cpuConsumerThreadName)) {
            exitingCount++;
        }
    }
    Asserts.assertEquals(exitingCount, 1);
}
 
Example 18
Source File: AllocationStackTrace.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Performs JFR recording, GC provocation/detection and stacktrace
 * verification for JFR event.
 *
 * @param bean MX bean for desired GC
 * @param memory allocator for desired type of allocations
 * @param expectedStack array of expected frames
 * @throws Exception
 */
private static boolean allocAndCheck(GarbageCollectorMXBean bean, MemoryAllocator memory,
        String[] expectedStack) throws Exception {
    String threadName = Thread.currentThread().getName();
    String event = EventNames.AllocationRequiringGC;

    Recording r = new Recording();
    r.enable(event).withStackTrace();
    r.start();

    long prevCollectionCount = bean.getCollectionCount();
    long collectionCount = -1;

    long iterationCount = 0;

    do {
        memory.allocate();
        collectionCount = bean.getCollectionCount();
        iterationCount++;
    } while (collectionCount == prevCollectionCount);

    System.out.println("Allocation num: " + iterationCount);
    System.out.println("GC detected: " + collectionCount);

    r.stop();
    List<RecordedEvent> events = Events.fromRecording(r);

    System.out.println("JFR GC events found: " + events.size());

    // Find any event that matched the expected stack trace
    for (RecordedEvent e : events) {
        System.out.println("Event: " + e);
        RecordedThread thread = e.getThread();
        String eventThreadName = thread.getJavaName();
        if (!threadName.equals(eventThreadName)) {
            continue;
        }
        if (matchingStackTrace(e.getStackTrace(), expectedStack)) {
            return true;
        }
    }
    return false;
}
 
Example 19
Source File: AllocationStackTrace.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Performs JFR recording, GC provocation/detection and stacktrace
 * verification for JFR event.
 *
 * @param bean MX bean for desired GC
 * @param memory allocator for desired type of allocations
 * @param expectedStack array of expected frames
 * @throws Exception
 */
private static boolean allocAndCheck(GarbageCollectorMXBean bean, MemoryAllocator memory,
        String[] expectedStack) throws Exception {
    String threadName = Thread.currentThread().getName();
    String event = EventNames.AllocationRequiringGC;

    Recording r = new Recording();
    r.enable(event).withStackTrace();
    r.start();

    long prevCollectionCount = bean.getCollectionCount();
    long collectionCount = -1;

    long iterationCount = 0;

    do {
        memory.allocate();
        collectionCount = bean.getCollectionCount();
        iterationCount++;
    } while (collectionCount == prevCollectionCount);

    System.out.println("Allocation num: " + iterationCount);
    System.out.println("GC detected: " + collectionCount);

    r.stop();
    List<RecordedEvent> events = Events.fromRecording(r);

    System.out.println("JFR GC events found: " + events.size());

    // Find any event that matched the expected stack trace
    for (RecordedEvent e : events) {
        System.out.println("Event: " + e);
        RecordedThread thread = e.getThread();
        String eventThreadName = thread.getJavaName();
        if (!threadName.equals(eventThreadName)) {
            continue;
        }
        if (matchingStackTrace(e.getStackTrace(), expectedStack)) {
            return true;
        }
    }
    return false;
}
 
Example 20
Source File: AllocationStackTrace.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Performs JFR recording, GC provocation/detection and stacktrace
 * verification for JFR event.
 *
 * @param bean MX bean for desired GC
 * @param memory allocator for desired type of allocations
 * @param expectedStack array of expected frames
 * @throws Exception
 */
private static boolean allocAndCheck(GarbageCollectorMXBean bean, MemoryAllocator memory,
        String[] expectedStack) throws Exception {
    String threadName = Thread.currentThread().getName();
    String event = EventNames.AllocationRequiringGC;

    Recording r = new Recording();
    r.enable(event).withStackTrace();
    r.start();

    long prevCollectionCount = bean.getCollectionCount();
    long collectionCount = -1;

    long iterationCount = 0;

    do {
        memory.allocate();
        collectionCount = bean.getCollectionCount();
        iterationCount++;
    } while (collectionCount == prevCollectionCount);

    System.out.println("Allocation num: " + iterationCount);
    System.out.println("GC detected: " + collectionCount);

    r.stop();
    List<RecordedEvent> events = Events.fromRecording(r);

    System.out.println("JFR GC events found: " + events.size());

    // Find any event that matched the expected stack trace
    for (RecordedEvent e : events) {
        System.out.println("Event: " + e);
        RecordedThread thread = e.getThread();
        String eventThreadName = thread.getJavaName();
        if (!threadName.equals(eventThreadName)) {
            continue;
        }
        if (matchingStackTrace(e.getStackTrace(), expectedStack)) {
            return true;
        }
    }
    return false;
}