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.test.lib.jfr;
  26 
  27 import static jdk.test.lib.Asserts.assertEquals;
  28 import static jdk.test.lib.Asserts.fail;
  29 
  30 import java.io.IOException;
  31 import java.nio.file.Files;
  32 import java.nio.file.Path;
  33 import java.util.List;
  34 
  35 import jdk.jfr.Recording;
  36 import jdk.jfr.RecordingState;
  37 import jdk.jfr.consumer.RecordedEvent;
  38 import jdk.jfr.consumer.RecordingFile;
  39 import jdk.test.lib.Asserts;
  40 import jdk.test.lib.Utils;
  41 
  42 
  43 /**
  44  * Common helper class.
  45  */
  46 public final class CommonHelper {
  47 
  48     private static RecordedEvent timeStampCounterEvent;
  49 
  50     public static void verifyException(VoidFunction f, String msg, Class<? extends Throwable> expectedException) throws Throwable {
  51         try {
  52             f.run();
  53         } catch (Throwable t) {
  54             if (expectedException.isAssignableFrom(t.getClass())) {
  55                 return;
  56             }
  57             t.printStackTrace();
  58             assertEquals(t.getClass(), expectedException, "Wrong exception class");
  59         }
  60         fail("Missing Exception for: " + msg);
  61     }
  62 
  63     public static Recording verifyExists(long recId, List<Recording> recordings) {
  64         for (Recording r : recordings) {
  65             if (recId == r.getId()) {
  66                 return r;
  67             }
  68         }
  69         Asserts.fail("Recording not found, id=" + recId);
  70         return null;
  71     }
  72 
  73 
  74     public static void waitForRecordingState(Recording r, RecordingState expectedState) throws Exception {
  75         while (r.getState() != expectedState) {
  76             Thread.sleep(20);
  77         }
  78     }
  79 
  80     public static void verifyRecordingState(Recording r, RecordingState expectedState) throws Exception {
  81         assertEquals(expectedState, r.getState(), "Wrong state");
  82     }
  83 
  84     public static boolean hasFastTimeEnabled() throws Exception {
  85         return getTimeStampCounterEvent().getValue("fastTimeEnabled");
  86     }
  87 
  88     private synchronized static RecordedEvent getTimeStampCounterEvent() throws IOException, Exception {
  89         if (timeStampCounterEvent == null) {
  90             try (Recording r = new Recording()) {
  91                 r.enable(EventNames.CPUTimeStampCounter);
  92                 r.start();
  93                 r.stop();
  94                 Path p = Utils.createTempFile("timestamo", ".jfr");
  95                 r.dump(p);
  96                 List<RecordedEvent> events = RecordingFile.readAllEvents(p);
  97                 Files.deleteIfExists(p);
  98                 if (events.isEmpty()) {
  99                     throw new Exception("Could not locate CPUTimeStampCounter event");
 100                 }
 101                 timeStampCounterEvent = events.get(0);
 102             }
 103         }
 104         return timeStampCounterEvent;
 105     }
 106 
 107     public static void waitForSystemCurrentMillisToChange() {
 108         long t = System.currentTimeMillis();
 109         while (t == System.currentTimeMillis()) {
 110             try {
 111                 Thread.sleep(2);
 112             } catch (InterruptedException e) {
 113                throw new Error("Sleep interupted", e);
 114             }
 115         }
 116     }
 117 }