1 /*
   2  * Copyright (c) 2017, 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.event.sampling;
  27 
  28 import java.io.File;
  29 import java.nio.file.Paths;
  30 import java.time.Duration;
  31 
  32 import jdk.jfr.FlightRecorder;
  33 import jdk.jfr.Recording;
  34 import jdk.jfr.consumer.RecordedEvent;
  35 import jdk.jfr.consumer.RecordingFile;
  36 import jdk.test.lib.process.OutputAnalyzer;
  37 import jdk.test.lib.process.ProcessTools;
  38 
  39 /*
  40  * @test
  41  * @key jfr
  42  * @library /test/lib
  43  * @modules java.base/jdk.internal.misc
  44  * @run main/native jdk.jfr.event.sampling.TestNative
  45  */
  46 public class TestNative {
  47 
  48     public final static String EVENT_SETTINGS_FILE = System.getProperty("test.src", ".") + File.separator + "sampling.jfc";
  49     public final static String JFR_DUMP = "samples.jfr";
  50     public final static String EXCEPTION = "No native samples found";
  51     public final static String NATIVE_EVENT = "com.oracle.jdk.NativeMethodSample";
  52     public static Recording recording;
  53 
  54     public static native void longTime();
  55 
  56     public static void main(String[] args) throws Exception {
  57         String lib = System.getProperty("test.nativepath");
  58         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(true, "-Djava.library.path=" + lib, "jdk.jfr.event.sampling.TestNative$Test");
  59 
  60         OutputAnalyzer output = ProcessTools.executeProcess(pb);
  61         output.shouldHaveExitValue(0);
  62         output.stdoutShouldNotContain("No native samples found");
  63     }
  64 
  65     static class Test {
  66         public static void main(String[] args) throws Exception {
  67             System.loadLibrary("TestNative");
  68             FlightRecorder.getFlightRecorder();
  69             recording = new Recording();
  70             recording.setToDisk(true);
  71             recording.setDestination(Paths.get(JFR_DUMP));
  72             recording.enable(NATIVE_EVENT).withPeriod(Duration.ofMillis(10));
  73             recording.start();
  74 
  75             longTime();
  76 
  77             recording.stop();
  78             recording.close();
  79 
  80             try (RecordingFile rf = new RecordingFile(Paths.get(JFR_DUMP))) {
  81                 while (rf.hasMoreEvents()) {
  82                     RecordedEvent re = rf.readEvent();
  83                     if (re.getEventType().getName().equals(NATIVE_EVENT)) {
  84                         return;
  85                     }
  86                 }
  87             }
  88 
  89             throw new Exception("No native samples found");
  90         }
  91     }
  92 }