Java Code Examples for jdk.test.lib.Utils#createTempFile()

The following examples show how to use jdk.test.lib.Utils#createTempFile() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestRecordingFile.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
private static Path createBrokenWIthZeros(Path valid) throws Exception {
    try {
        Path broken = Utils.createTempFile("broken-events", ".jfr");
        Files.delete(broken);
        Files.copy(valid, broken);
        RandomAccessFile raf = new RandomAccessFile(broken.toFile(), "rw");
        raf.seek(HEADER_SIZE);
        int size = (int) Files.size(broken);
        byte[] ones = new byte[size - HEADER_SIZE];
        for (int i = 0; i < ones.length; i++) {
            ones[i] = (byte) 0xFF;
        }
        raf.write(ones, 0, ones.length);
        raf.close();
        return broken;
    } catch (IOException ioe) {
        throw new Exception("Could not produce a broken file " + valid, ioe);
    }
}
 
Example 2
Source File: CommonHelper.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
private synchronized static RecordedEvent getTimeStampCounterEvent() throws IOException, Exception {
    if (timeStampCounterEvent == null) {
        try (Recording r = new Recording()) {
            r.enable(EventNames.CPUTimeStampCounter);
            r.start();
            r.stop();
            Path p = Utils.createTempFile("timestamo", ".jfr");
            r.dump(p);
            List<RecordedEvent> events = RecordingFile.readAllEvents(p);
            Files.deleteIfExists(p);
            if (events.isEmpty()) {
                throw new Exception("Could not locate CPUTimeStampCounter event");
            }
            timeStampCounterEvent = events.get(0);
        }
    }
    return timeStampCounterEvent;
}
 
Example 3
Source File: TestRecordingFile.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private static Path createBrokenWIthZeros(Path valid) throws Exception {
    try {
        Path broken = Utils.createTempFile("broken-events", ".jfr");
        Files.delete(broken);
        Files.copy(valid, broken);
        RandomAccessFile raf = new RandomAccessFile(broken.toFile(), "rw");
        raf.seek(HEADER_SIZE);
        int size = (int) Files.size(broken);
        byte[] ones = new byte[size - HEADER_SIZE];
        for (int i = 0; i < ones.length; i++) {
            ones[i] = (byte) 0xFF;
        }
        raf.write(ones, 0, ones.length);
        raf.close();
        return broken;
    } catch (IOException ioe) {
        throw new Exception("Could not produce a broken file " + valid, ioe);
    }
}
 
Example 4
Source File: TestRecordingFile.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private static Path createBrokenMetadata(Path valid) throws Exception {
    try {
        Path broken = Utils.createTempFile("broken-metadata", ".jfr");
        Files.delete(broken);
        Files.copy(valid, broken);
        RandomAccessFile raf = new RandomAccessFile(broken.toFile(), "rw");
        raf.seek(METADATA_OFFSET);
        long metadataOffset = raf.readLong();
        raf.seek(metadataOffset);
        raf.writeLong(Long.MAX_VALUE);
        raf.writeLong(Long.MAX_VALUE);
        raf.close();
        return broken;
    } catch (IOException ioe) {
        throw new Exception("Could not produce a broken EventSet from file " + valid, ioe);
    }
}
 
Example 5
Source File: CommonHelper.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private synchronized static RecordedEvent getTimeStampCounterEvent() throws IOException, Exception {
    if (timeStampCounterEvent == null) {
        try (Recording r = new Recording()) {
            r.enable(EventNames.CPUTimeStampCounter);
            r.start();
            r.stop();
            Path p = Utils.createTempFile("timestamo", ".jfr");
            r.dump(p);
            List<RecordedEvent> events = RecordingFile.readAllEvents(p);
            Files.deleteIfExists(p);
            if (events.isEmpty()) {
                throw new Exception("Could not locate CPUTimeStampCounter event");
            }
            timeStampCounterEvent = events.get(0);
        }
    }
    return timeStampCounterEvent;
}
 
Example 6
Source File: TestRecordingFile.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private static void recordAndVerify(boolean disk, boolean background,  int[] shouldExist, int[] shouldNotExist) throws Exception {
    StringJoiner sb  = new StringJoiner("-");
    for (int i = 0; i <shouldExist.length; i++) {
        sb.add(Integer.toString(shouldExist[i]));
    }
    System.out.println("Verifying recordings: disk=" + disk + " background=" + background);
    System.out.println("Should exist: " + Arrays.toString(shouldExist));
    System.out.println("Should not exist: " + Arrays.toString(shouldNotExist));

    Path p = Utils.createTempFile(sb.toString(), ".jfr");
    System.out.println("Filename: " + p);
    try (Recording r = new Recording()) {
        r.start();
        r.stop();
        r.dump(p);
        try (RecordingFile f = new RecordingFile(p)) {
            List<EventType> types = f.readEventTypes();
            for (int i = 0; i< shouldExist.length; i++) {
                assertHasEventType(types, "Event" + shouldExist[i]);
            }
            for (int i = 0; i< shouldNotExist.length; i++) {
                assertMissingEventType(types, "Event" + shouldNotExist[i]);
            }
        }
    }
    System.out.println();
    System.out.println();
}
 
Example 7
Source File: TestRecordingFile.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Throwable {

        // create some recording data
        Recording r = new Recording();
        r.enable(TestEvent1.class).withoutStackTrace();
        r.enable(TestEvent2.class).withoutStackTrace();
        r.enable(TestEvent3.class).withoutStackTrace();
        r.start();
        TestEvent1 t1 = new TestEvent1();
        t1.commit();
        TestEvent2 t2 = new TestEvent2();
        t2.commit();
        TestEvent3 t3 = new TestEvent3();
        t3.commit();
        r.stop();
        Path valid = Utils.createTempFile("three-event-recording", ".jfr");
        r.dump(valid);
        r.close();

        Path brokenWithZeros = createBrokenWIthZeros(valid);
        Path brokenMetadata = createBrokenMetadata(valid);
        // prepare event sets
        testNewRecordingFile(valid, brokenWithZeros);
        testIterate(valid, brokenWithZeros);
        testReadAllEvents(valid, brokenWithZeros);
        testReadEventTypes(valid, brokenMetadata);
        testClose(valid);
        testReadEventTypesMultiChunk();
        testReadEventTypeWithUnregistration(false, false);
        testReadEventTypeWithUnregistration(false, true);
        testReadEventTypeWithUnregistration(true, false);
        testReadEventTypeWithUnregistration(true, true);
    }
 
Example 8
Source File: ExecuteHelper.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public static Path createProfilingRecording() throws Exception {
    Path file = Utils.createTempFile("profiling-recording", ".jfr");
    // Create a recording with some data
    try (Recording r = new Recording(Configuration.getConfiguration("profile"))) {
        r.start();

        // Allocation event
        array = new Object[1000000];
        array = null;

        // Class loading event etc
        provokeClassLoading();

        // GC events
        System.gc();

        // ExecutionSample
        long t = System.currentTimeMillis();
        while (System.currentTimeMillis() - t < 50) {
            // do nothing
        }

        // Other periodic events, i.e CPU load
        Thread.sleep(1000);

        r.stop();
        r.dump(file);
    }

    return file;
}
 
Example 9
Source File: TestInheritedAnnotations.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String... args) throws Exception {
    try (Recording r = new Recording()) {
        r.enable(EventNames.ActiveSetting);
        r.start();
        UncleEvent u = new UncleEvent();
        u.commit();
        FatherEvent f = new FatherEvent();
        f.commit();
        SonEvent s = new SonEvent();
        s.commit();
        AuntEvent a = new AuntEvent();
        a.commit();
        CousineEvent c = new CousineEvent();
        c.commit();

        r.stop();
        Path p = Utils.createTempFile("inherited-annotations", ".jfr");
        r.dump(p);
        List<RecordedEvent> events = RecordingFile.readAllEvents(p);
        assertNoGrandFather(events);
        assertUncle(events);
        assertNoFather(events);
        assertNoAunt();
        assertNoCousine(events);
        assertSon(events);
        assertSettings(events);
    }
}
 
Example 10
Source File: TestUnloadingEventClass.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private static Object getEventType(Recording r, long id, String eventName) throws IOException {
    Path p = Utils.createTempFile("unloading-event-class-recording-" + id + "_", ".jfr");
    r.dump(p);
    try (RecordingFile rf = new RecordingFile(p)) {
        for (EventType et : rf.readEventTypes()) {
            if (et.getName().equals(eventName)) {
                return et;
            }
        }

    }
    return null;
}
 
Example 11
Source File: TestRecordedEventGetThreadOther.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private static Path postEventAndDumpToFile() throws Throwable {
    Recording r = new Recording();
    r.start();
    TestEvent t = new TestEvent();
    t.commit();
    r.stop();
    Path path = Utils.createTempFile("event-thread", ".jfr");
    System.out.println("Created path: " + path);
    r.dump(path);
    r.close();
    return path;
}
 
Example 12
Source File: TestInheritedAnnotations.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String... args) throws Exception {
    try (Recording r = new Recording()) {
        r.enable(EventNames.ActiveSetting);
        r.start();
        UncleEvent u = new UncleEvent();
        u.commit();
        FatherEvent f = new FatherEvent();
        f.commit();
        SonEvent s = new SonEvent();
        s.commit();
        AuntEvent a = new AuntEvent();
        a.commit();
        CousineEvent c = new CousineEvent();
        c.commit();

        r.stop();
        Path p = Utils.createTempFile("inherited-annotations", ".jfr");
        r.dump(p);
        List<RecordedEvent> events = RecordingFile.readAllEvents(p);
        assertNoGrandFather(events);
        assertUncle(events);
        assertNoFather(events);
        assertNoAunt();
        assertNoCousine(events);
        assertSon(events);
        assertSettings(events);
    }
}
 
Example 13
Source File: TestUnloadingEventClass.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private static Object getEventType(Recording r, long id, String eventName) throws IOException {
    Path p = Utils.createTempFile("unloading-event-class-recording-" + id + "_", ".jfr");
    r.dump(p);
    try (RecordingFile rf = new RecordingFile(p)) {
        for (EventType et : rf.readEventTypes()) {
            if (et.getName().equals(eventName)) {
                return et;
            }
        }

    }
    return null;
}
 
Example 14
Source File: TestReadTwice.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Throwable {
    Recording r = new Recording();
    r.enable(MyEvent.class).withoutStackTrace();
    r.start();

    // Commit a single event to the recording
    MyEvent event = new MyEvent();
    event.commit();

    r.stop();

    // Dump the recording to a file
    Path path = Utils.createTempFile("read-twice", ".jfr");
    System.out.println("Dumping to " + path);
    r.dump(path);
    r.close();

    // Read all events from the file in one go
    List<RecordedEvent> events = RecordingFile.readAllEvents(path);

    // Read again the same events one by one
    RecordingFile rfile = new RecordingFile(path);
    List<RecordedEvent> events2 = new LinkedList<>();
    while (rfile.hasMoreEvents()) {
        events2.add(rfile.readEvent());
    }

    // Compare sizes
    Asserts.assertEquals(events.size(), events2.size());
    rfile.close();
}
 
Example 15
Source File: TestRecordedFullStackTrace.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private static void assertStackTraces(RecurseThread[] threads) throws Throwable {
    Path path = null;
    do {
        Recording recording = new Recording();
        recording.enable(EVENT_NAME).withPeriod(Duration.ofMillis(50));
        recording.start();
        Thread.sleep(500);
        recording.stop();
        // Dump the recording to a file
        path = Utils.createTempFile("execution-stack-trace", ".jfr");
        System.out.println("Dumping to " + path);
        recording.dump(path);
        recording.close();
    } while (!hasValidStackTraces(path, threads));
}
 
Example 16
Source File: TestRecordedEventGetThreadOther.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private static Path postEventAndDumpToFile() throws Throwable {
    Recording r = new Recording();
    r.start();
    TestEvent t = new TestEvent();
    t.commit();
    r.stop();
    Path path = Utils.createTempFile("event-thread", ".jfr");
    System.out.println("Created path: " + path);
    r.dump(path);
    r.close();
    return path;
}
 
Example 17
Source File: TestRecordingFile.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private static void recordAndVerify(boolean disk, boolean background,  int[] shouldExist, int[] shouldNotExist) throws Exception {
    StringJoiner sb  = new StringJoiner("-");
    for (int i = 0; i <shouldExist.length; i++) {
        sb.add(Integer.toString(shouldExist[i]));
    }
    System.out.println("Verifying recordings: disk=" + disk + " background=" + background);
    System.out.println("Should exist: " + Arrays.toString(shouldExist));
    System.out.println("Should not exist: " + Arrays.toString(shouldNotExist));

    Path p = Utils.createTempFile(sb.toString(), ".jfr");
    System.out.println("Filename: " + p);
    try (Recording r = new Recording()) {
        r.start();
        r.stop();
        r.dump(p);
        try (RecordingFile f = new RecordingFile(p)) {
            List<EventType> types = f.readEventTypes();
            for (int i = 0; i< shouldExist.length; i++) {
                assertHasEventType(types, "Event" + shouldExist[i]);
            }
            for (int i = 0; i< shouldNotExist.length; i++) {
                assertMissingEventType(types, "Event" + shouldNotExist[i]);
            }
        }
    }
    System.out.println();
    System.out.println();
}
 
Example 18
Source File: TestRecordingFile.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Throwable {

        // create some recording data
        Recording r = new Recording();
        r.enable(TestEvent1.class).withoutStackTrace();
        r.enable(TestEvent2.class).withoutStackTrace();
        r.enable(TestEvent3.class).withoutStackTrace();
        r.start();
        TestEvent1 t1 = new TestEvent1();
        t1.commit();
        TestEvent2 t2 = new TestEvent2();
        t2.commit();
        TestEvent3 t3 = new TestEvent3();
        t3.commit();
        r.stop();
        Path valid = Utils.createTempFile("three-event-recording", ".jfr");
        r.dump(valid);
        r.close();

        Path brokenWithZeros = createBrokenWIthZeros(valid);
        Path brokenMetadata = createBrokenMetadata(valid);
        // prepare event sets
        testNewRecordingFile(valid, brokenWithZeros);
        testIterate(valid, brokenWithZeros);
        testReadAllEvents(valid, brokenWithZeros);
        testReadEventTypes(valid, brokenMetadata);
        testClose(valid);
        testReadEventTypesMultiChunk();
        testReadEventTypeWithUnregistration(false, false);
        testReadEventTypeWithUnregistration(false, true);
        testReadEventTypeWithUnregistration(true, false);
        testReadEventTypeWithUnregistration(true, true);
    }
 
Example 19
Source File: TestReadTwice.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Throwable {
    Recording r = new Recording();
    r.enable(MyEvent.class).withoutStackTrace();
    r.start();

    // Commit a single event to the recording
    MyEvent event = new MyEvent();
    event.commit();

    r.stop();

    // Dump the recording to a file
    Path path = Utils.createTempFile("read-twice", ".jfr");
    System.out.println("Dumping to " + path);
    r.dump(path);
    r.close();

    // Read all events from the file in one go
    List<RecordedEvent> events = RecordingFile.readAllEvents(path);

    // Read again the same events one by one
    RecordingFile rfile = new RecordingFile(path);
    List<RecordedEvent> events2 = new LinkedList<>();
    while (rfile.hasMoreEvents()) {
        events2.add(rfile.readEvent());
    }

    // Compare sizes
    Asserts.assertEquals(events.size(), events2.size());
    rfile.close();
}
 
Example 20
Source File: TestUnsupportedVM.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String... args) throws Exception {
    if (Boolean.getBoolean("prepare-recording")) {
        Recording r = new Recording(Configuration.getConfiguration("default"));
        r.start();
        r.stop();
        r.dump(RECORDING_FILE);
        r.close();
        return;
    }

    System.out.println("jdk.jfr.unsupportedvm=" + System.getProperty("jdk.jfr.unsupportedvm"));
    // Class FlightRecorder
    if (FlightRecorder.isAvailable()) {
        throw new AssertionError("JFR should not be available on an unsupported VM");
    }

    if (FlightRecorder.isInitialized()) {
        throw new AssertionError("JFR should not be initialized on an unsupported VM");
    }

    assertIllegalStateException(() -> FlightRecorder.getFlightRecorder());
    assertSwallow(() -> FlightRecorder.addListener(new FlightRecorderListener() {}));
    assertSwallow(() -> FlightRecorder.removeListener(new FlightRecorderListener() {}));
    assertSwallow(() -> FlightRecorder.register(MyEvent.class));
    assertSwallow(() -> FlightRecorder.unregister(MyEvent.class));
    assertSwallow(() -> FlightRecorder.addPeriodicEvent(MyEvent.class, new Runnable() { public void run() {} }));
    assertSwallow(() -> FlightRecorder.removePeriodicEvent(new Runnable() { public void run() {} }));

    // Class Configuration
    if (!Configuration.getConfigurations().isEmpty()) {
        throw new AssertionError("Configuration files should not exist on an unsupported VM");
    }
    Path jfcFile = Utils.createTempFile("empty", ".jfr");
    assertIOException(() -> Configuration.getConfiguration("default"));
    assertIOException(() -> Configuration.create(jfcFile));
    assertIOException(() -> Configuration.create(new FileReader(jfcFile.toFile())));

    // Class EventType
    assertInternalError(() -> EventType.getEventType(MyEvent.class));

    // Class EventFactory
    assertInternalError(() -> EventFactory.create(new ArrayList<>(), new ArrayList<>()));

    // Create a static event
    MyEvent myEvent = new MyEvent();
    myEvent.begin();
    myEvent.end();
    myEvent.shouldCommit();
    myEvent.commit();

    // Trigger class initialization failure
    for (Class<?> c : APIClasses) {
        assertNoClassInitFailure(c);
    }

    // jdk.jfr.consumer.*
    // Only run this part of tests if we are on VM
    // that can produce a recording file
    if (Files.exists(RECORDING_FILE)) {
        for(RecordedEvent re : RecordingFile.readAllEvents(RECORDING_FILE)) {
            System.out.println(re);
        }
    }
}