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