TestThreadStartEndEvents.java 5.6 KB
Newer Older
A
apetushkov 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*
 * Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

package jdk.jfr.event.runtime;

28 29 30
import static jdk.test.lib.Asserts.assertEquals;
import static jdk.test.lib.Asserts.assertNotNull;

A
apetushkov 已提交
31 32 33 34 35 36
import java.time.Duration;
import java.util.List;
import java.util.concurrent.CountDownLatch;

import jdk.jfr.Recording;
import jdk.jfr.consumer.RecordedEvent;
37 38
import jdk.jfr.consumer.RecordedMethod;
import jdk.jfr.consumer.RecordedStackTrace;
A
apetushkov 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
import jdk.test.lib.jfr.EventNames;
import jdk.test.lib.jfr.Events;

/**
 * @test
 * @key jfr
 *
 * @library /lib /
 * @run main/othervm jdk.jfr.event.runtime.TestThreadStartEndEvents
 */

/**
 * Starts and stops a number of threads in order.
 * Verifies that events are in the same order.
 */
public class TestThreadStartEndEvents {
    private final static String EVENT_NAME_THREAD_START = EventNames.ThreadStart;
    private final static String EVENT_NAME_THREAD_END = EventNames.ThreadEnd;
    private static final String THREAD_NAME_PREFIX = "TestThread-";

    public static void main(String[] args) throws Throwable {
        // Test Java Thread Start event
        Recording recording = new Recording();
        recording.enable(EVENT_NAME_THREAD_START).withThreshold(Duration.ofMillis(0));
        recording.enable(EVENT_NAME_THREAD_END).withThreshold(Duration.ofMillis(0));
        recording.start();
        LatchedThread[] threads = startThreads();
        stopThreads(threads);
        recording.stop();

        int currThreadIndex = 0;
        List<RecordedEvent> events = Events.fromRecording(recording);
71
        events.sort((e1, e2) -> e1.getStartTime().compareTo(e2.getStartTime()));
A
apetushkov 已提交
72 73
        Events.hasEvents(events);
        for (RecordedEvent event : events) {
74
            if (!event.getThread().getJavaName().startsWith(THREAD_NAME_PREFIX)) {
A
apetushkov 已提交
75 76
                continue;
            }
77
            System.out.println("Event:" + event);
A
apetushkov 已提交
78 79 80 81
            // Threads should be started and stopped in the correct order.
            Events.assertEventThread(event, threads[currThreadIndex % threads.length]);
            String eventName = currThreadIndex < threads.length ? EVENT_NAME_THREAD_START : EVENT_NAME_THREAD_END;
            if (!eventName.equals(event.getEventType().getName())) {
82 83 84 85 86 87 88 89 90
                throw new Exception("Expected event of type " + eventName + " but got " + event.getEventType().getName());
            }

            if (eventName == EVENT_NAME_THREAD_START) {
                Events.assertEventThread(event, "parentThread", Thread.currentThread());
                RecordedStackTrace stackTrace = event.getValue("stackTrace");
                assertNotNull(stackTrace);
                RecordedMethod topMethod = stackTrace.getFrames().get(0).getMethod();
                assertEquals(topMethod.getName(), "startThread");
A
apetushkov 已提交
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
            }
            currThreadIndex++;
        }
    }

    private static LatchedThread[] startThreads() {
        LatchedThread threads[] = new LatchedThread[10];
        ThreadGroup threadGroup = new ThreadGroup("TestThreadGroup");
        for (int i = 0; i < threads.length; i++) {
            threads[i] = new LatchedThread(threadGroup, THREAD_NAME_PREFIX + i);
            threads[i].startThread();
            System.out.println("Started thread id=" + threads[i].getId());
        }
        return threads;
    }

    private static void stopThreads(LatchedThread[] threads) {
        for (LatchedThread thread : threads) {
            thread.stopThread();
            while (thread.isAlive()) {
                try {
                    Thread.sleep(5);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private static class LatchedThread extends Thread {
        private final CountDownLatch start = new CountDownLatch(1);
        private final CountDownLatch stop = new CountDownLatch(1);

        public LatchedThread(ThreadGroup threadGroup, String name) {
            super(threadGroup, name);
        }

        public void run() {
            start.countDown();
            try {
                stop.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        public void startThread() {
            this.start();
            try {
                start.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        public void stopThread() {
            stop.countDown();
        }
    }

}