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.tool; 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 /lib / 49 * @modules jdk.jfr/jdk.jfr.internal 50 * @run main/othervm jdk.jfr.tool.TestAssemble 51 */ 52 public class TestAssemble { 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 Throwable { 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.jfr("assemble"); 92 output.shouldContain("too few arguments"); 93 94 output = ExecuteHelper.jfr("assemble", directory); 95 output.shouldContain("too few arguments"); 96 97 output = ExecuteHelper.jfr("assemble", "not-a-directory", destination); 98 output.shouldContain("directory does not exist, not-a-directory"); 99 100 output = ExecuteHelper.jfr("assemble", directory, "not-a-destination"); 101 output.shouldContain("filename must end with '.jfr'"); 102 103 output = ExecuteHelper.jfr("assemble", "--wrongOption", directory, destination); 104 output.shouldContain("too many arguments"); 105 106 FileWriter fw = new FileWriter(destination); 107 fw.write('d'); 108 fw.close(); 109 output = ExecuteHelper.jfr("assemble", directory, destination); 110 output.shouldContain("already exists"); 111 Files.delete(destinationPath); 112 113 // test success 114 output = ExecuteHelper.jfr("assemble", directory, destination); 115 System.out.println(output.getOutput()); 116 output.shouldContain("Finished."); 117 118 long reconstructedCount = countEventInRecording(destinationPath); 119 Asserts.assertEquals(expectedCount, reconstructedCount); 120 // Cleanup 121 for (int i = 0; i < RECORDING_COUNT; i++) { 122 recordings[i].close(); 123 } 124 } 125 126 private static long countEventInRecording(Path file) throws IOException { 127 Integer lastId = -1; 128 try (RecordingFile rf = new RecordingFile(file)) { 129 long count = 0; 130 while (rf.hasMoreEvents()) { 131 RecordedEvent re = rf.readEvent(); 132 if (re.getEventType().getName().equals("Correlation")) { 133 Integer id = re.getValue("id"); 134 if (id < lastId) { 135 Asserts.fail("Expected chunk number to increase"); 136 } 137 lastId = id; 138 } 139 count++; 140 } 141 return count; 142 } 143 } 144 }