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

The following examples show how to use jdk.jfr.consumer.RecordedEvent#getStackTrace() . 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: 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 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: 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 4
Source File: TestRecordedMethodDescriptor.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 {
    Recording recording = new Recording();
    recording.enable(MyEvent.class).withStackTrace();
    recording.start();

    MyEvent event = new MyEvent();
    event.begin();
    event.end();
    event.commit();
    recording.stop();

    List<RecordedEvent> recordedEvents = Events.fromRecording(recording);
    assertEquals(1, recordedEvents.size(), "Expected one event");
    RecordedEvent recordedEvent = recordedEvents.get(0);

    RecordedStackTrace stacktrace = recordedEvent.getStackTrace();
    List<RecordedFrame> frames = stacktrace.getFrames();
    assertFalse(frames.isEmpty(), "Stacktrace frames was empty");
    for (RecordedFrame frame : frames) {
        analyzeRecordedMethodDescriptor(frame.getMethod());
    }

    assertTrue(isMainMethodDescriptorRecorded, "main() method descriptor has never been recorded");
}
 
Example 5
Source File: PrettyWriter.java    From openjdk-jdk8u 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 6
Source File: TestRecordedMethodDescriptor.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 recording = new Recording();
    recording.enable(MyEvent.class).withStackTrace();
    recording.start();

    MyEvent event = new MyEvent();
    event.begin();
    event.end();
    event.commit();
    recording.stop();

    List<RecordedEvent> recordedEvents = Events.fromRecording(recording);
    assertEquals(1, recordedEvents.size(), "Expected one event");
    RecordedEvent recordedEvent = recordedEvents.get(0);

    RecordedStackTrace stacktrace = recordedEvent.getStackTrace();
    List<RecordedFrame> frames = stacktrace.getFrames();
    assertFalse(frames.isEmpty(), "Stacktrace frames was empty");
    for (RecordedFrame frame : frames) {
        analyzeRecordedMethodDescriptor(frame.getMethod());
    }

    assertTrue(isMainMethodDescriptorRecorded, "main() method descriptor has never been recorded");
}
 
Example 7
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 8
Source File: TestRecordedFullStackTrace.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private static void checkEvent(RecordedEvent event, int expectedDepth) throws Throwable {
    RecordedStackTrace stacktrace = null;
    try {
        stacktrace = event.getStackTrace();
        List<RecordedFrame> frames = stacktrace.getFrames();
        Asserts.assertEquals(Math.min(MAX_DEPTH, expectedDepth), frames.size(), "Wrong stacktrace depth. Expected:" + expectedDepth);
        List<String> expectedMethods = getExpectedMethods(expectedDepth);
        Asserts.assertEquals(expectedMethods.size(), frames.size(), "Wrong expectedMethods depth. Test error.");

        for (int i = 0; i < frames.size(); ++i) {
            String name = frames.get(i).getMethod().getName();
            String expectedName = expectedMethods.get(i);
            System.out.printf("method[%d]=%s, expected=%s%n", i, name, expectedName);
            Asserts.assertEquals(name, expectedName, "Wrong method name");
        }

        boolean isTruncated = stacktrace.isTruncated();
        boolean isTruncateExpected = expectedDepth > MAX_DEPTH;
        Asserts.assertEquals(isTruncated, isTruncateExpected, "Wrong value for isTruncated. Expected:" + isTruncateExpected);

        String firstMethod = frames.get(frames.size() - 1).getMethod().getName();
        boolean isFullTrace = "run".equals(firstMethod);
        String msg = String.format("Wrong values for isTruncated=%b, isFullTrace=%b", isTruncated, isFullTrace);
        Asserts.assertTrue(isTruncated != isFullTrace, msg);
    } catch (Throwable t) {
        System.out.println(String.format("stacktrace:%n%s", stacktrace));
        throw t;
    }
}
 
Example 9
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 10
Source File: TestHiddenMethod.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private static boolean hasHiddenStackFrame(RecordedEvent event) throws Throwable {
    RecordedStackTrace stacktrace = event.getStackTrace();
    List<RecordedFrame> frames = stacktrace.getFrames();
    assertFalse(frames.isEmpty(), "Stacktrace frames was empty");
    for (RecordedFrame frame : frames) {
        if (frame.getMethod().isHidden()) {
            return true;
        }
    }
    return false;
}
 
Example 11
Source File: TestGetStackTrace.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static void assertStackTrace(RecordedEvent re) {
    assertNotNull(re.getStackTrace());
    RecordedStackTrace strace = re.getStackTrace();
    assertEquals(strace.isTruncated(), false);
    List<RecordedFrame> frames = strace.getFrames();
    assertTrue(frames.size() > 0);
    for (RecordedFrame frame : frames) {
        assertFrame(frame);
    }
}
 
Example 12
Source File: TestRecordedFullStackTrace.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private static void checkEvent(RecordedEvent event, int expectedDepth) throws Throwable {
    RecordedStackTrace stacktrace = null;
    try {
        stacktrace = event.getStackTrace();
        List<RecordedFrame> frames = stacktrace.getFrames();
        Asserts.assertEquals(Math.min(MAX_DEPTH, expectedDepth), frames.size(), "Wrong stacktrace depth. Expected:" + expectedDepth);
        List<String> expectedMethods = getExpectedMethods(expectedDepth);
        Asserts.assertEquals(expectedMethods.size(), frames.size(), "Wrong expectedMethods depth. Test error.");

        for (int i = 0; i < frames.size(); ++i) {
            String name = frames.get(i).getMethod().getName();
            String expectedName = expectedMethods.get(i);
            System.out.printf("method[%d]=%s, expected=%s%n", i, name, expectedName);
            Asserts.assertEquals(name, expectedName, "Wrong method name");
        }

        boolean isTruncated = stacktrace.isTruncated();
        boolean isTruncateExpected = expectedDepth > MAX_DEPTH;
        Asserts.assertEquals(isTruncated, isTruncateExpected, "Wrong value for isTruncated. Expected:" + isTruncateExpected);

        String firstMethod = frames.get(frames.size() - 1).getMethod().getName();
        boolean isFullTrace = "run".equals(firstMethod);
        String msg = String.format("Wrong values for isTruncated=%b, isFullTrace=%b", isTruncated, isFullTrace);
        Asserts.assertTrue(isTruncated != isFullTrace, msg);
    } catch (Throwable t) {
        System.out.println(String.format("stacktrace:%n%s", stacktrace));
        throw t;
    }
}
 
Example 13
Source File: TestHiddenMethod.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private static boolean hasHiddenStackFrame(RecordedEvent event) throws Throwable {
    RecordedStackTrace stacktrace = event.getStackTrace();
    List<RecordedFrame> frames = stacktrace.getFrames();
    assertFalse(frames.isEmpty(), "Stacktrace frames was empty");
    for (RecordedFrame frame : frames) {
        if (frame.getMethod().isHidden()) {
            return true;
        }
    }
    return false;
}
 
Example 14
Source File: TestGetStackTrace.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private static void assertStackTrace(RecordedEvent re) {
    assertNotNull(re.getStackTrace());
    RecordedStackTrace strace = re.getStackTrace();
    assertEquals(strace.isTruncated(), false);
    List<RecordedFrame> frames = strace.getFrames();
    assertTrue(frames.size() > 0);
    for (RecordedFrame frame : frames) {
        assertFrame(frame);
    }
}
 
Example 15
Source File: TestHiddenMethod.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static boolean hasHiddenStackFrame(RecordedEvent event) throws Throwable {
    RecordedStackTrace stacktrace = event.getStackTrace();
    List<RecordedFrame> frames = stacktrace.getFrames();
    assertFalse(frames.isEmpty(), "Stacktrace frames was empty");
    for (RecordedFrame frame : frames) {
        if (frame.getMethod().isHidden()) {
            return true;
        }
    }
    return false;
}
 
Example 16
Source File: TestRecordedFullStackTrace.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static void checkEvent(RecordedEvent event, int expectedDepth) throws Throwable {
    RecordedStackTrace stacktrace = null;
    try {
        stacktrace = event.getStackTrace();
        List<RecordedFrame> frames = stacktrace.getFrames();
        Asserts.assertEquals(Math.min(MAX_DEPTH, expectedDepth), frames.size(), "Wrong stacktrace depth. Expected:" + expectedDepth);
        List<String> expectedMethods = getExpectedMethods(expectedDepth);
        Asserts.assertEquals(expectedMethods.size(), frames.size(), "Wrong expectedMethods depth. Test error.");

        for (int i = 0; i < frames.size(); ++i) {
            String name = frames.get(i).getMethod().getName();
            String expectedName = expectedMethods.get(i);
            System.out.printf("method[%d]=%s, expected=%s%n", i, name, expectedName);
            Asserts.assertEquals(name, expectedName, "Wrong method name");
        }

        boolean isTruncated = stacktrace.isTruncated();
        boolean isTruncateExpected = expectedDepth > MAX_DEPTH;
        Asserts.assertEquals(isTruncated, isTruncateExpected, "Wrong value for isTruncated. Expected:" + isTruncateExpected);

        String firstMethod = frames.get(frames.size() - 1).getMethod().getName();
        boolean isFullTrace = "run".equals(firstMethod);
        String msg = String.format("Wrong values for isTruncated=%b, isFullTrace=%b", isTruncated, isFullTrace);
        Asserts.assertTrue(isTruncated != isFullTrace, msg);
    } catch (Throwable t) {
        System.out.println(String.format("stacktrace:%n%s", stacktrace));
        throw t;
    }
}
 
Example 17
Source File: TestRecordedFrame.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
public static void doTest(int lineNumber) throws IOException {

        System.out.println("Enetring method");

        Recording recording = new Recording();
        recording.start();

        SimpleEvent ev = new SimpleEvent();
        commitEvent(ev);
        recording.stop();

        List<RecordedEvent> recordedEvents = Events.fromRecording(recording);
        Events.hasEvents(recordedEvents);
        RecordedEvent recordedEvent = recordedEvents.get(0);

        RecordedStackTrace stacktrace = recordedEvent.getStackTrace();
        List<RecordedFrame> frames = stacktrace.getFrames();
        for (RecordedFrame frame : frames) {

            // All frames are java frames
            Asserts.assertTrue(frame.isJavaFrame());
            // Verify the main() method frame
            RecordedMethod method = frame.getMethod();
            if (method.getName().equals("main")) {

                // Frame type
                String type = frame.getType();
                System.out.println("type: " + type);
                Asserts.assertTrue(
                        type.equals("Interpreted")
                        || type.equals("JIT compiled")
                        || type.equals("Inlined"));

                Asserts.assertEquals(lineNumber, frame.getLineNumber());

                boolean isInterpreted = "Interpreted".equals(type);
                boolean expectedInterpreted = "true".equals(System.getProperty("interpreted"));
                Asserts.assertEquals(isInterpreted, expectedInterpreted);

                int bci = frame.getBytecodeIndex();

                System.out.println("bci: " + bci);
                Asserts.assertTrue(bci > 0);
            }

        }

    }
 
Example 18
Source File: TestRecordedFrame.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
public static void doTest(int lineNumber) throws IOException {

        System.out.println("Enetring method");

        Recording recording = new Recording();
        recording.start();

        SimpleEvent ev = new SimpleEvent();
        commitEvent(ev);
        recording.stop();

        List<RecordedEvent> recordedEvents = Events.fromRecording(recording);
        Events.hasEvents(recordedEvents);
        RecordedEvent recordedEvent = recordedEvents.get(0);

        RecordedStackTrace stacktrace = recordedEvent.getStackTrace();
        List<RecordedFrame> frames = stacktrace.getFrames();
        for (RecordedFrame frame : frames) {

            // All frames are java frames
            Asserts.assertTrue(frame.isJavaFrame());
            // Verify the main() method frame
            RecordedMethod method = frame.getMethod();
            if (method.getName().equals("main")) {

                // Frame type
                String type = frame.getType();
                System.out.println("type: " + type);
                Asserts.assertTrue(
                        type.equals("Interpreted")
                        || type.equals("JIT compiled")
                        || type.equals("Inlined"));

                Asserts.assertEquals(lineNumber, frame.getLineNumber());

                boolean isInterpreted = "Interpreted".equals(type);
                boolean expectedInterpreted = "true".equals(System.getProperty("interpreted"));
                Asserts.assertEquals(isInterpreted, expectedInterpreted);

                int bci = frame.getBytecodeIndex();

                System.out.println("bci: " + bci);
                Asserts.assertTrue(bci > 0);
            }

        }

    }
 
Example 19
Source File: TestRecordedFrame.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
public static void doTest(int lineNumber) throws IOException {

        System.out.println("Enetring method");

        Recording recording = new Recording();
        recording.start();

        SimpleEvent ev = new SimpleEvent();
        commitEvent(ev);
        recording.stop();

        List<RecordedEvent> recordedEvents = Events.fromRecording(recording);
        Events.hasEvents(recordedEvents);
        RecordedEvent recordedEvent = recordedEvents.get(0);

        RecordedStackTrace stacktrace = recordedEvent.getStackTrace();
        List<RecordedFrame> frames = stacktrace.getFrames();
        for (RecordedFrame frame : frames) {

            // All frames are java frames
            Asserts.assertTrue(frame.isJavaFrame());
            // Verify the main() method frame
            RecordedMethod method = frame.getMethod();
            if (method.getName().equals("main")) {

                // Frame type
                String type = frame.getType();
                System.out.println("type: " + type);
                Asserts.assertTrue(
                        type.equals("Interpreted")
                        || type.equals("JIT compiled")
                        || type.equals("Inlined"));

                Asserts.assertEquals(lineNumber, frame.getLineNumber());

                boolean isInterpreted = "Interpreted".equals(type);
                boolean expectedInterpreted = "true".equals(System.getProperty("interpreted"));
                Asserts.assertEquals(isInterpreted, expectedInterpreted);

                int bci = frame.getBytecodeIndex();

                System.out.println("bci: " + bci);
                Asserts.assertTrue(bci > 0);
            }

        }

    }
 
Example 20
Source File: ProfileResults.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
/**
 * Process all the JFR files passed in args and print a merged summary.
 */
public static void printReport(List<String> files, String mode, int stacksize, int count, boolean lineNumbers) throws IOException {
  if (!"cpu".equals(mode) && !"heap".equals(mode)) {
    throw new IllegalArgumentException("tests.profile.mode must be one of (cpu,heap)");
  }
  if (stacksize < 1) {
    throw new IllegalArgumentException("tests.profile.stacksize must be positive");
  }
  if (count < 1) {
    throw new IllegalArgumentException("tests.profile.count must be positive");
  }
  Map<String, SimpleEntry<String, Long>> histogram = new HashMap<>();
  int totalEvents = 0;
  long sumValues = 0;
  String framePadding = " ".repeat(COLUMN_SIZE * 2);
  for (String file : files) {
    try (RecordingFile recording = new RecordingFile(Paths.get(file))) {
      while (recording.hasMoreEvents()) {
        RecordedEvent event = recording.readEvent();
        if (!isInteresting(mode, event)) {
          continue;
        }
        RecordedStackTrace trace = event.getStackTrace();
        if (trace != null) {
          StringBuilder stack = new StringBuilder();
          for (int i = 0; i < Math.min(stacksize, trace.getFrames().size()); i++) {
            if (stack.length() > 0) {
              stack.append("\n")
                   .append(framePadding)
                   .append("  at ");
            }
            stack.append(frameToString(trace.getFrames().get(i), lineNumbers));
          }
          String line = stack.toString();
          SimpleEntry<String,Long> entry = histogram.computeIfAbsent(line, u -> new SimpleEntry<String, Long>(line, 0L));
          long value = getValue(event);
          entry.setValue(entry.getValue() + value);
          totalEvents++;
          sumValues += value;
        }
      }
    }
  }
  // print summary from histogram
  System.out.printf(Locale.ROOT, "PROFILE SUMMARY from %d events (total: %s)\n", totalEvents, formatValue(sumValues));
  System.out.printf(Locale.ROOT, "  tests.profile.mode=%s\n", mode);
  System.out.printf(Locale.ROOT, "  tests.profile.count=%d\n", count);
  System.out.printf(Locale.ROOT, "  tests.profile.stacksize=%d\n", stacksize);
  System.out.printf(Locale.ROOT, "  tests.profile.linenumbers=%b\n", lineNumbers);
  System.out.printf(Locale.ROOT, "%s%sSTACK\n", pad("PERCENT"), pad(mode.toUpperCase(Locale.ROOT) + " SAMPLES"));
  List<SimpleEntry<String, Long>> entries = new ArrayList<>(histogram.values());
  entries.sort((u, v) -> v.getValue().compareTo(u.getValue()));
  int seen = 0;
  for (SimpleEntry<String, Long> c : entries) {
    if (seen++ == count) {
      break;
    }
    String percent = String.format("%2.2f%%", 100 * (c.getValue() / (float) sumValues));
    System.out.printf(Locale.ROOT, "%s%s%s\n", pad(percent), pad(formatValue(c.getValue())), c.getKey());
  }
}