1 /* 2 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. 3 * 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5 * 6 * The contents of this file are subject to the terms of either the Universal Permissive License 7 * v 1.0 as shown at http://oss.oracle.com/licenses/upl 8 * 9 * or the following license: 10 * 11 * Redistribution and use in source and binary forms, with or without modification, are permitted 12 * provided that the following conditions are met: 13 * 14 * 1. Redistributions of source code must retain the above copyright notice, this list of conditions 15 * and the following disclaimer. 16 * 17 * 2. Redistributions in binary form must reproduce the above copyright notice, this list of 18 * conditions and the following disclaimer in the documentation and/or other materials provided with 19 * the distribution. 20 * 21 * 3. Neither the name of the copyright holder nor the names of its contributors may be used to 22 * endorse or promote products derived from this software without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR 25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 26 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 30 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY 31 * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 package org.openjdk.jmc.rjmx.services.jfr.test; 34 35 import static org.junit.Assert.assertEquals; 36 import static org.openjdk.jmc.common.unit.UnitLookup.SECOND; 37 38 import java.util.Random; 39 40 import org.openjdk.jmc.common.unit.IConstrainedMap; 41 import org.openjdk.jmc.common.unit.IQuantity; 42 import org.openjdk.jmc.flightrecorder.configuration.events.EventOptionID; 43 import org.openjdk.jmc.flightrecorder.configuration.events.IEventTypeID; 44 import org.openjdk.jmc.flightrecorder.configuration.internal.EventTypeIDV1; 45 import org.openjdk.jmc.flightrecorder.configuration.internal.EventTypeIDV2; 46 import org.openjdk.jmc.flightrecorder.configuration.recording.RecordingOptionsBuilder; 47 import org.openjdk.jmc.rjmx.ConnectionException; 48 import org.openjdk.jmc.rjmx.ServiceNotAvailableException; 49 import org.openjdk.jmc.rjmx.services.jfr.FlightRecorderException; 50 import org.openjdk.jmc.rjmx.services.jfr.IFlightRecorderService; 51 import org.openjdk.jmc.rjmx.services.jfr.IRecordingDescriptor; 52 import org.openjdk.jmc.rjmx.test.RjmxTestCase; 53 54 @SuppressWarnings("nls") 55 public abstract class JfrTestCase extends RjmxTestCase { 56 public static void assertSame(IQuantity expected, IQuantity actual) { 57 if (expected.compareTo(actual) == 0) { 58 return; 59 } 60 failNotEquals(null, expected, actual); 61 } 62 63 protected IFlightRecorderService getFlightRecorderService() 64 throws ConnectionException, ServiceNotAvailableException { 65 return getConnectionHandle().getServiceOrThrow(IFlightRecorderService.class); 66 } 67 68 // We can only rely on that id = 0 if we run against a VM with the default recording enabled 69 protected IRecordingDescriptor getDefaultRecording() 70 throws FlightRecorderException, ConnectionException, ServiceNotAvailableException { 71 for (IRecordingDescriptor availableRecording : getFlightRecorderService().getAvailableRecordings()) { 72 if (availableRecording.getId().longValue() == 0L) { 73 return availableRecording; 74 } 75 } 76 return null; 77 } 78 79 protected IRecordingDescriptor getContinuousRecording() 80 throws FlightRecorderException, ConnectionException, ServiceNotAvailableException { 81 for (IRecordingDescriptor availableRecording : getFlightRecorderService().getAvailableRecordings()) { 82 if (availableRecording.isContinuous()) { 83 return availableRecording; 84 } 85 } 86 return null; 87 } 88 89 protected IRecordingDescriptor startContinuousRecording() throws Exception { 90 Random rnd = new Random(); 91 String name = "test_recording_" + rnd.nextInt() % 4711; //$NON-NLS-1$ 92 IFlightRecorderService service = getFlightRecorderService(); 93 IConstrainedMap<String> recordingOptions = new RecordingOptionsBuilder(service).name(name).duration(0L).build(); 94 IRecordingDescriptor recording = service.start(recordingOptions, service.getDefaultEventOptions()); 95 System.out.println("Started " + recording.getName()); //$NON-NLS-1$ 96 IConstrainedMap<String> options = service.getRecordingOptions(recording); 97 Object durationOption = options.get(RecordingOptionsBuilder.KEY_DURATION); 98 // FIXME: It seems duration may be null for continuous recordings on Java 9 and later. Check with specification. 99 // assertNotNull(durationOption); 100 if (durationOption != null) { 101 assertSame(SECOND.quantity(0), (IQuantity) durationOption); 102 } 103 assertEquals(IRecordingDescriptor.RecordingState.RUNNING, recording.getState()); 104 105 assertEquals(name, recording.getName()); 106 return recording; 107 } 108 109 protected void stopRecording(IRecordingDescriptor recording) throws Exception { 110 IFlightRecorderService service = getFlightRecorderService(); 111 service.stop(recording); 112 for (int stopCount = 0; stopCount < 15; stopCount += 1) { 113 recording = service.getUpdatedRecordingDescription(recording); 114 if (!recording.getState().equals(IRecordingDescriptor.RecordingState.STOPPING)) { 115 break; 116 } 117 Thread.sleep(1000); 118 } 119 assertEquals(IRecordingDescriptor.RecordingState.STOPPED, recording.getState()); 120 System.out.println("Stopped " + recording.getName()); //$NON-NLS-1$ 121 service.close(recording); 122 recording = service.getUpdatedRecordingDescription(recording); 123 assertNull(recording); 124 } 125 126 protected static EventOptionID jvm(String path, String option) { 127 return new EventOptionID(jvm(path), option); 128 } 129 130 protected static EventOptionID jdk(String path, String option) { 131 return new EventOptionID(jdk(path), option); 132 } 133 134 protected static EventOptionID jfr_info(String path, String option) { 135 return new EventOptionID(jfr_info(path), option); 136 } 137 138 protected static EventOptionID v2(String name, String option) { 139 return new EventOptionID(v2(name), option); 140 } 141 142 protected static IEventTypeID jvm(String path) { 143 return new EventTypeIDV1("http://www.oracle.com/hotspot/jvm/", path); 144 } 145 146 protected static IEventTypeID jdk(String path) { 147 return new EventTypeIDV1("http://www.oracle.com/hotspot/jdk/", path); 148 } 149 150 protected static IEventTypeID jfr_info(String path) { 151 return new EventTypeIDV1("http://www.oracle.com/hotspot/jfr-info/", path); 152 } 153 154 protected static IEventTypeID v2(String name) { 155 return new EventTypeIDV2(name); 156 } 157 158 }