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