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