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  *
  45  * @library /lib /
  46  * @build sun.hotspot.WhiteBox
  47  * @run main ClassFileInstaller sun.hotspot.WhiteBox
  48  *     sun.hotspot.WhiteBox$WhiteBoxPermission
  49  * @run main/othervm -Xbootclasspath/a:.
  50  *                   -XX:+FlightRecorder -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
  51  *                   jdk.jfr.event.runtime.TestSafepointEvents
  52  */
  53 public class TestSafepointEvents {
  54 
  55     static final String[] EVENT_NAMES = new String[] {
  56         EventNames.SafepointBegin,
  57         EventNames.SafepointStateSynchronization,
  58         EventNames.SafepointWaitBlocked,
  59         EventNames.SafepointCleanup,
  60         EventNames.SafepointCleanupTask,
  61         EventNames.SafepointEnd
  62     };
  63 
  64     public static void main(String[] args) throws Exception {
  65         Recording recording = new Recording();
  66         for (String name : EVENT_NAMES) {
  67             recording.enable(name).withThreshold(Duration.ofMillis(0));
  68         }
  69         recording.start();
  70         WhiteBox.getWhiteBox().forceSafepoint();
  71         recording.stop();
  72 
  73         try {
  74             // Verify that each event type was seen at least once
  75             for (String name : EVENT_NAMES) {
  76                 boolean found = false;
  77                 for (RecordedEvent event : Events.fromRecording(recording)) {
  78                     found = event.getEventType().getName().equals(name);
  79                     if (found) {
  80                         break;
  81                     }
  82                 }
  83                 assertTrue(found, "Expected event from test [" + name + "]");
  84             }
  85 
  86             // Collect all events grouped by safepoint id
  87             SortedMap<Integer, Set<String>> safepointIds = new TreeMap<>();
  88             for (RecordedEvent event : Events.fromRecording(recording)) {
  89                 Integer safepointId = event.getValue("safepointId");
  90                 if (!safepointIds.containsKey(safepointId)) {
  91                     safepointIds.put(safepointId, new HashSet<>());
  92                 }
  93                 safepointIds.get(safepointId).add(event.getEventType().getName());
  94             }
  95 
  96             // The last safepoint may be related to stopping the recording and can thus be
  97             // incomplete - so if there is more than one, ignore the last one
  98             if (safepointIds.size() > 1) {
  99                 safepointIds.remove(safepointIds.lastKey());
 100             }
 101             Asserts.assertGreaterThanOrEqual(safepointIds.size(), 1, "At least 1 safepoint must have occured");
 102 
 103             // Verify that each safepoint id has an occurence of every event type,
 104             // this ensures that all events related to a given safepoint had the same id
 105             for (Set<String> safepointEvents : safepointIds.values()) {
 106                 for (String name : EVENT_NAMES) {
 107                     assertTrue(safepointEvents.contains(name), "Expected event '" + name + "' to be present");
 108                 }
 109             }
 110         } catch (Throwable e) {
 111             recording.dump(Paths.get("failed.jfr"));
 112             throw e;
 113         } finally {
 114             recording.close();
 115         }
 116     }
 117 }