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