1 /*
   2  * Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package jdk.jfr.event.runtime;
  26 
  27 import static jdk.test.lib.Asserts.assertTrue;
  28 
  29 import java.nio.file.Paths;
  30 import java.time.Duration;
  31 import java.util.*;
  32 
  33 import jdk.jfr.Recording;
  34 import jdk.jfr.consumer.RecordedEvent;
  35 
  36 import jdk.test.lib.Asserts;
  37 import jdk.test.lib.jfr.EventNames;
  38 import jdk.test.lib.jfr.Events;
  39 import sun.hotspot.WhiteBox;
  40 
  41 /*
  42  * @test TestSafepointEvents
  43  * @key jfr
  44  * @library /test/lib
  45  * @build sun.hotspot.WhiteBox
  46  * @run main ClassFileInstaller sun.hotspot.WhiteBox
  47  *     sun.hotspot.WhiteBox$WhiteBoxPermission
  48  * @run main/othervm -Xbootclasspath/a:.
  49  *                   -XX:+FlightRecorder -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
  50  *                   jdk.jfr.event.runtime.TestSafepointEvents
  51  */
  52 public class TestSafepointEvents {
  53 
  54     static final String[] EVENT_NAMES = new String[] {
  55         EventNames.SafepointBegin,
  56         EventNames.SafepointStateSyncronization,
  57         EventNames.SafepointWaitBlocked,
  58         EventNames.SafepointCleanup,
  59         EventNames.SafepointCleanupTask,
  60         EventNames.SafepointEnd
  61     };
  62 
  63     public static void main(String[] args) throws Exception {
  64         Recording recording = new Recording();
  65         for (String name : EVENT_NAMES) {
  66             recording.enable(name).withThreshold(Duration.ofMillis(0));
  67         }
  68         recording.start();
  69         WhiteBox.getWhiteBox().forceSafepoint();
  70         recording.stop();
  71 
  72         try {
  73             // Verify that each event type was seen at least once
  74             for (String name : EVENT_NAMES) {
  75                 boolean found = false;
  76                 for (RecordedEvent event : Events.fromRecording(recording)) {
  77                     found = event.getEventType().getName().equals(name);
  78                     if (found) {
  79                         break;
  80                     }
  81                 }
  82                 assertTrue(found, "Expected event from test [" + name + "]");
  83             }
  84 
  85             // Collect all events grouped by safepoint id
  86             SortedMap<Integer, Set<String>> safepointIds = new TreeMap<>();
  87             for (RecordedEvent event : Events.fromRecording(recording)) {
  88                 Integer safepointId = event.getValue("safepointId");
  89                 if (!safepointIds.containsKey(safepointId)) {
  90                     safepointIds.put(safepointId, new HashSet<>());
  91                 }
  92                 safepointIds.get(safepointId).add(event.getEventType().getName());
  93             }
  94 
  95             // The last safepoint may be related to stopping the recording and can thus be
  96             // incomplete - so if there is more than one, ignore the last one
  97             if (safepointIds.size() > 1) {
  98                 safepointIds.remove(safepointIds.lastKey());
  99             }
 100             Asserts.assertGreaterThanOrEqual(safepointIds.size(), 1, "At least 1 safepoint must have occured");
 101 
 102             // Verify that each safepoint id has an occurence of every event type,
 103             // this ensures that all events related to a given safepoint had the same id
 104             for (Set<String> safepointEvents : safepointIds.values()) {
 105                 for (String name : EVENT_NAMES) {
 106                     assertTrue(safepointEvents.contains(name), "Expected event '" + name + "' to be present");
 107                 }
 108             }
 109         } catch (Throwable e) {
 110             recording.dump(Paths.get("failed.jfr"));
 111             throw e;
 112         } finally {
 113             recording.close();
 114         }
 115     }
 116 }