1 /*
   2  * Copyright (c) 2017, 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 package jdk.jfr.api.recording.settings;
  26 
  27 import java.io.IOException;
  28 import java.nio.file.Path;
  29 import java.nio.file.Paths;
  30 import java.util.HashMap;
  31 import java.util.HashSet;
  32 import java.util.Map;
  33 import java.util.Set;
  34 
  35 import jdk.jfr.EventType;
  36 import jdk.jfr.FlightRecorder;
  37 import jdk.jfr.Recording;
  38 import jdk.jfr.SettingDescriptor;
  39 import jdk.jfr.consumer.RecordingFile;
  40 import jdk.testlibrary.jfr.EventNames;
  41 
  42 /*
  43  * @test
  44  * @summary Verifies that event types has the correct type of settings
  45  * @key jfr
  46  * @library /lib/testlibrary
  47  * @run main/othervm jdk.jfr.api.recording.settings.TestSettingsAvailability
  48  */
  49 public class TestSettingsAvailability {
  50     public static void main(String[] args) throws Throwable {
  51         testKnownSettings();
  52         testSettingPersistence();
  53     }
  54 
  55     private static void testSettingPersistence() throws IOException, Exception {
  56         Map<String, EventType> inMemoryTypes = new HashMap<>();
  57         for (EventType type : FlightRecorder.getFlightRecorder().getEventTypes()) {
  58             inMemoryTypes.put(type.getName(), type);
  59         }
  60 
  61         Path p = Paths.get("recording.jfr");
  62         try (Recording r = new Recording()) {
  63             r.start();
  64             r.stop();
  65             r.dump(p);
  66             try (RecordingFile rf = new RecordingFile(p)) {
  67                 for (EventType parsedType : rf.readEventTypes()) {
  68                     EventType inMem = inMemoryTypes.get(parsedType.getName());
  69                     if (inMem == null) {
  70                         throw new Exception("Superflous event type " + parsedType.getName() + " in recording");
  71                     }
  72                     Set<String> inMemsettings = new HashSet<>();
  73                     for (SettingDescriptor sd : inMem.getSettingDescriptors()) {
  74                         inMemsettings.add(sd.getName());
  75                     }
  76 
  77                     for (SettingDescriptor parsedSetting : parsedType.getSettingDescriptors()) {
  78                         if (!inMemsettings.contains(parsedSetting.getName())) {
  79                             throw new Exception("Superflous setting " + parsedSetting.getName() + " in " + parsedType.getName());
  80                         }
  81                         inMemsettings.remove(parsedSetting.getName());
  82                     }
  83                     if (!inMemsettings.isEmpty()) {
  84                         throw new Exception("Missing settings " + inMemsettings + " for event type " + parsedType.getName() + " in recording");
  85                     }
  86                 }
  87             }
  88         }
  89     }
  90 
  91     private static void testKnownSettings() throws Exception {
  92         testSetting(EventNames.JVMInformation, "enabled", "period");
  93         testSetting(EventNames.FileRead, "enabled", "threshold", "stackTrace");
  94         testSetting(EventNames.FileWrite, "enabled", "threshold","stackTrace");
  95         testSetting(EventNames.ExceptionStatistics, "enabled", "period");
  96         testSetting(EventNames.SocketRead, "enabled", "threshold", "stackTrace");
  97         testSetting(EventNames.SocketWrite, "enabled", "threshold", "stackTrace");
  98         testSetting(EventNames.ActiveRecording, "enabled", "threshold", "stackTrace");
  99         testSetting(EventNames.ActiveSetting, "enabled", "threshold", "stackTrace");
 100         testSetting(EventNames.JavaExceptionThrow, "enabled", "threshold", "stackTrace");
 101     }
 102 
 103     private static void testSetting(String eventName, String... settingNames) throws Exception {
 104         for (EventType type : FlightRecorder.getFlightRecorder().getEventTypes()) {
 105             if (eventName.equals(type.getName())) {
 106                 Set<String> settings = new HashSet<>();
 107                 for (SettingDescriptor sd : type.getSettingDescriptors()) {
 108                     settings.add(sd.getName());
 109                 }
 110                 for (String settingName : settingNames) {
 111                     if (!settings.contains(settingName)) {
 112                         throw new Exception("Missing setting " + settingName + " in " + eventName);
 113                     }
 114                     settings.remove(settingName);
 115                 }
 116                 if (!settings.isEmpty()) {
 117                     throw new Exception("Superflous settings " + settings + " in event " + eventName);
 118                 }
 119             }
 120         }
 121     }
 122 }