1 /*
   2  * Copyright (c) 2013, 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.startupargs;
  27 
  28 import java.io.IOException;
  29 import java.nio.file.DirectoryStream;
  30 import java.nio.file.Files;
  31 import java.nio.file.Path;
  32 import java.util.ArrayList;
  33 import java.util.Arrays;
  34 import java.util.List;
  35 import java.util.stream.Collectors;
  36 
  37 import jdk.jfr.FlightRecorder;
  38 import jdk.jfr.Recording;
  39 import jdk.testlibrary.Asserts;
  40 import jdk.testlibrary.dcmd.CommandExecutor;
  41 import jdk.testlibrary.dcmd.PidJcmdExecutor;
  42 import jdk.testlibrary.OutputAnalyzer;
  43 
  44 
  45 
  46 public class StartupHelper {
  47 
  48     public static Recording getRecording(String name) {
  49         for (Recording r : FlightRecorder.getFlightRecorder().getRecordings()) {
  50             System.out.println("Recording=" + r.getName());
  51             if (name.equals(r.getName())) {
  52                 return r;
  53             }
  54         }
  55         Asserts.fail("No recording with name " + name);
  56         return null;
  57     }
  58 
  59     public static List<Path> listFilesInDir(Path root) throws IOException {
  60         List<Path> paths = new ArrayList<>();
  61         List<Path> searchPaths = new ArrayList<>();
  62         searchPaths.add(root);
  63 
  64         while (!searchPaths.isEmpty()) {
  65             Path currRoot = searchPaths.remove(searchPaths.size() - 1);
  66             DirectoryStream<Path> contents = Files.newDirectoryStream(currRoot);
  67             for (Path path : contents) {
  68                 paths.add(path);
  69                 if (Files.isDirectory(path)) {
  70                     searchPaths.add(path);
  71                 }
  72             }
  73         }
  74 
  75         System.out.println("Files in '" + root + "':");
  76         for (Path path : paths) {
  77             System.out.println("  path: " + path);
  78         }
  79         return paths;
  80     }
  81 
  82     public static Path findRecordingFileInRepo(Path baseLocation) throws IOException {
  83         // Check that repo contains a file ending in "jfr" or "part"
  84         for (Path path : listFilesInDir(baseLocation)) {
  85             if (Files.isRegularFile(path)) {
  86                 String filename = path.toString();
  87                 if (filename.endsWith("jfr") || filename.endsWith("part")) {
  88                     return path;
  89                 }
  90             }
  91         }
  92         return null;
  93     }
  94 
  95     public static OutputAnalyzer jcmd(String... args) {
  96         String argsString = Arrays.stream(args).collect(Collectors.joining(" "));
  97         CommandExecutor executor = new PidJcmdExecutor();
  98         OutputAnalyzer oa = executor.execute(argsString);
  99         oa.shouldHaveExitValue(0);
 100         return oa;
 101     }
 102 
 103     public static OutputAnalyzer jcmdCheck(String recordingName) {
 104         return jcmd("JFR.check", "name=" + recordingName, "verbose=true");
 105     }
 106 }