1 /* 2 * Copyright (c) 2016, 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 26 package jdk.jfr.cmd; 27 28 import java.io.FileWriter; 29 import java.io.IOException; 30 import java.nio.file.Files; 31 import java.nio.file.Path; 32 import java.nio.file.Paths; 33 34 import jdk.jfr.Event; 35 import jdk.jfr.Name; 36 import jdk.jfr.Recording; 37 import jdk.jfr.consumer.RecordedEvent; 38 import jdk.jfr.consumer.RecordingFile; 39 import jdk.jfr.internal.Repository; 40 import jdk.jfr.internal.SecuritySupport.SafePath; 41 import jdk.test.lib.Asserts; 42 import jdk.test.lib.process.OutputAnalyzer; 43 44 /* 45 * @test 46 * @summary Test jfr reconstruct 47 * @key jfr 48 * @library /test/lib /test/jdk 49 * @modules jdk.jfr/jdk.jfr.internal 50 * @run main/othervm jdk.jfr.cmd.TestReconstruct 51 */ 52 public class TestReconstruct { 53 54 @Name("Correlation") 55 static class CorrelationEvent extends Event { 56 int id; 57 } 58 private static int RECORDING_COUNT = 5; 59 60 @SuppressWarnings("resource") 61 public static void main(String[] args) throws Exception { 62 // Create some disk recordings 63 Recording[] recordings = new Recording[5]; 64 for (int i = 0; i < RECORDING_COUNT; i++) { 65 Recording r = new Recording(); 66 r.setToDisk(true); 67 r.start(); 68 CorrelationEvent ce = new CorrelationEvent(); 69 ce.id = i; 70 ce.commit(); 71 r.stop(); 72 recordings[i] = r; 73 } 74 Path dir = Paths.get("reconstruction-parts"); 75 Files.createDirectories(dir); 76 77 long expectedCount = 0; 78 for (int i = 0; i < RECORDING_COUNT; i++) { 79 Path tmp = dir.resolve("chunk-part-" + i + ".jfr"); 80 recordings[i].dump(tmp); 81 expectedCount += countEventInRecording(tmp); 82 } 83 84 SafePath repository = Repository.getRepository().getRepositoryPath(); 85 Path destinationPath = Paths.get("reconstructed.jfr"); 86 87 String directory = repository.toString(); 88 String destination = destinationPath.toAbsolutePath().toString(); 89 90 // Test failure 91 OutputAnalyzer output = ExecuteHelper.run("reconstruct"); 92 93 output.shouldContain("Too few arguments"); 94 95 output = ExecuteHelper.run("reconstruct", directory); 96 output.shouldContain("Too few arguments"); 97 98 output = ExecuteHelper.run("reconstruct", "not-a-directory", destination); 99 output.shouldContain("Could not find disk repository at"); 100 101 output = ExecuteHelper.run("reconstruct", directory, "not-a-destination"); 102 output.shouldContain("Filename must end with .jfr"); 103 104 output = ExecuteHelper.run("reconstruct", "--wrongOption", directory, destination); 105 output.shouldContain("Too many arguments"); 106 107 FileWriter fw = new FileWriter(destination); 108 fw.write('d'); 109 fw.close(); 110 output = ExecuteHelper.run("reconstruct", directory, destination); 111 output.shouldContain("already exists"); 112 Files.delete(destinationPath); 113 114 // test success 115 output = ExecuteHelper.run("reconstruct", directory, destination); 116 System.out.println(output.getOutput()); 117 output.shouldContain("Reconstruction complete"); 118 119 long reconstructedCount = countEventInRecording(destinationPath); 120 Asserts.assertEquals(expectedCount, reconstructedCount); 121 // Cleanup 122 for (int i = 0; i < RECORDING_COUNT; i++) { 123 recordings[i].close(); 124 } 125 } 126 127 private static long countEventInRecording(Path file) throws IOException { 128 Integer lastId = -1; 129 try (RecordingFile rf = new RecordingFile(file)) { 130 long count = 0; 131 while (rf.hasMoreEvents()) { 132 RecordedEvent re = rf.readEvent(); 133 if (re.getEventType().getName().equals("Correlation")) { 134 Integer id = re.getValue("id"); 135 if (id < lastId) { 136 Asserts.fail("Expected chunk number to increase"); 137 } 138 lastId = id; 139 } 140 count++; 141 } 142 return count; 143 } 144 } 145 }