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