Path: blob/master/test/jdk/jdk/jfr/api/consumer/TestRecordedFullStackTrace.java
42281 views
/*1* Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22package jdk.jfr.api.consumer;2324import java.nio.file.Path;25import java.time.Duration;26import java.util.ArrayList;27import java.util.List;2829import jdk.jfr.Recording;30import jdk.jfr.consumer.RecordedEvent;31import jdk.jfr.consumer.RecordedFrame;32import jdk.jfr.consumer.RecordedStackTrace;33import jdk.jfr.consumer.RecordingFile;34import jdk.test.lib.Asserts;35import jdk.test.lib.Utils;36import jdk.test.lib.jfr.EventNames;37import jdk.test.lib.jfr.Events;38import jdk.test.lib.jfr.RecurseThread;3940/**41* @test42* @key jfr43* @requires vm.hasJFR44* @library /test/lib45* @run main/othervm jdk.jfr.api.consumer.TestRecordedFullStackTrace46*/47public class TestRecordedFullStackTrace {4849private final static String EVENT_NAME = EventNames.ExecutionSample;50private final static int MAX_DEPTH = 64; // currently hardcoded in jvm5152public static void main(String[] args) throws Throwable {5354RecurseThread[] threads = new RecurseThread[3];55for (int i = 0; i < threads.length; ++i) {56int depth = MAX_DEPTH - 1 + i;57threads[i] = new RecurseThread(depth);58threads[i].setName("recursethread-" + depth);59threads[i].start();60}6162for (RecurseThread thread : threads) {63while (!thread.isInRunLoop()) {64Thread.sleep(20);65}66}6768assertStackTraces(threads);6970for (RecurseThread thread : threads) {71thread.quit();72thread.join();73}74}7576private static void assertStackTraces(RecurseThread[] threads) throws Throwable {77Path path = null;78do {79try (Recording recording = new Recording()) {80recording.enable(EVENT_NAME).withPeriod(Duration.ofMillis(1));81recording.start();82Thread.sleep(50);83recording.stop();84// Dump the recording to a file85path = Utils.createTempFile("execution-stack-trace", ".jfr");86System.out.println("Dumping to " + path);87recording.dump(path);88}89} while (!hasValidStackTraces(path, threads));90}9192private static boolean hasValidStackTraces(Path path, RecurseThread[] threads) throws Throwable {93boolean[] isEventFound = new boolean[threads.length];9495for (RecordedEvent event : RecordingFile.readAllEvents(path)) {96//System.out.println("Event: " + event);97String threadName = Events.assertField(event, "sampledThread.javaName").getValue();98long threadId = Events.assertField(event, "sampledThread.javaThreadId").getValue();99100for (int threadIndex = 0; threadIndex < threads.length; ++threadIndex) {101RecurseThread currThread = threads[threadIndex];102if (threadId == currThread.getId()) {103Asserts.assertEquals(threadName, currThread.getName(), "Wrong thread name, deptth=" + currThread.totalDepth);104if ("recurseEnd".equals(getTopMethodName(event))) {105isEventFound[threadIndex] = true;106checkEvent(event, currThread.totalDepth);107break;108}109}110}111}112113for (int i = 0; i < threads.length; ++i) {114String msg = "threadIndex=%d, recurseDepth=%d, isEventFound=%b%n";115System.out.printf(msg, i, threads[i].totalDepth, isEventFound[i]);116}117for (int i = 0; i < threads.length; ++i) {118if (!isEventFound[i]) {119// no assertion, let's retry.120// Could be race condition, i.e safe point during Thread.sleep121System.out.println("Falied to validate all threads, will retry.");122return false;123}124}125return true;126}127128public static String getTopMethodName(RecordedEvent event) {129List<RecordedFrame> frames = event.getStackTrace().getFrames();130Asserts.assertFalse(frames.isEmpty(), "JavaFrames was empty");131return frames.get(0).getMethod().getName();132}133134private static void checkEvent(RecordedEvent event, int expectedDepth) throws Throwable {135RecordedStackTrace stacktrace = null;136try {137stacktrace = event.getStackTrace();138List<RecordedFrame> frames = stacktrace.getFrames();139Asserts.assertEquals(Math.min(MAX_DEPTH, expectedDepth), frames.size(), "Wrong stacktrace depth. Expected:" + expectedDepth);140List<String> expectedMethods = getExpectedMethods(expectedDepth);141Asserts.assertEquals(expectedMethods.size(), frames.size(), "Wrong expectedMethods depth. Test error.");142143for (int i = 0; i < frames.size(); ++i) {144String name = frames.get(i).getMethod().getName();145String expectedName = expectedMethods.get(i);146Asserts.assertEquals(name, expectedName, "Wrong method name at index " + i);147}148149boolean isTruncated = stacktrace.isTruncated();150boolean isTruncateExpected = expectedDepth > MAX_DEPTH;151Asserts.assertEquals(isTruncated, isTruncateExpected, "Wrong value for isTruncated. Expected:" + isTruncateExpected);152153String firstMethod = frames.get(frames.size() - 1).getMethod().getName();154boolean isFullTrace = "run".equals(firstMethod);155String msg = String.format("Wrong values for isTruncated=%b, isFullTrace=%b", isTruncated, isFullTrace);156Asserts.assertTrue(isTruncated != isFullTrace, msg);157} catch (Throwable t) {158System.out.println(String.format("stacktrace:%n%s", stacktrace));159throw t;160}161}162163private static List<String> getExpectedMethods(int depth) {164List<String> methods = new ArrayList<>();165methods.add("recurseEnd");166for (int i = 0; i < depth - 2; ++i) {167methods.add((i % 2) == 0 ? "recurseA" : "recurseB");168}169methods.add("run");170if (depth > MAX_DEPTH) {171methods = methods.subList(0, MAX_DEPTH);172}173return methods;174}175}176177178