1 /*
   2  * Copyright (c) 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.event.metadata;
  26 
  27 import java.io.File;
  28 import java.io.IOException;
  29 import java.lang.reflect.Field;
  30 import java.nio.file.Files;
  31 import java.nio.file.Path;
  32 import java.nio.file.Paths;
  33 import java.util.Arrays;
  34 import java.util.HashSet;
  35 import java.util.List;
  36 import java.util.Set;
  37 import java.util.stream.Collectors;
  38 import java.util.stream.Stream;
  39 
  40 import jdk.jfr.EventType;
  41 import jdk.jfr.Experimental;
  42 import jdk.jfr.FlightRecorder;
  43 import jdk.test.lib.jfr.EventNames;
  44 import jdk.test.lib.Utils;
  45 
  46 /**
  47  * @test Check for JFR events not covered by tests
  48  * @key jfr
  49  * @requires vm.hasJFR
  50  * @library /test/lib /test/jdk
  51  * @run main jdk.jfr.event.metadata.TestLookForUntestedEvents
  52  */
  53 public class TestLookForUntestedEvents {
  54     private static final Path jfrTestRoot = Paths.get(Utils.TEST_SRC).getParent().getParent();
  55     private static final String MSG_SEPARATOR = "==========================";
  56     private static Set<String> jfrEventTypes = new HashSet<>();
  57 
  58     private static final Set<String> knownEventsMissingFromEventNames = new HashSet<>(
  59         Arrays.asList(
  60             // The Z* events below should be marked as experimental; see: JDK-8213966
  61             "ZStatisticsSampler", "ZStatisticsCounter",
  62             "ZPageAllocation", "ZThreadPhase"
  63         )
  64     );
  65 
  66     private static final Set<String> hardToTestEvents = new HashSet<>(
  67         Arrays.asList(
  68             "DataLoss", "IntFlag", "ReservedStackActivation",
  69             "DoubleFlag", "UnsignedLongFlagChanged", "IntFlagChanged",
  70             "UnsignedIntFlag", "UnsignedIntFlagChanged", "DoubleFlagChanged")
  71     );
  72 
  73     // GC uses specific framework to test the events, instead of using event names literally.
  74     // GC tests were inspected, as well as runtime output of GC tests.
  75     // The following events below are know to be covered based on that inspection.
  76     private static final Set<String> coveredGcEvents = new HashSet<>(
  77         Arrays.asList(
  78             "MetaspaceGCThreshold", "MetaspaceAllocationFailure", "MetaspaceOOM",
  79             "MetaspaceChunkFreeListSummary", "G1HeapSummary", "ParallelOldGarbageCollection",
  80             "OldGarbageCollection", "G1GarbageCollection", "GCPhasePause",
  81             "GCPhasePauseLevel1", "GCPhasePauseLevel2", "GCPhasePauseLevel3",
  82             "GCPhasePauseLevel4", "GCPhaseConcurrent")
  83     );
  84 
  85     // This is a "known failure list" for this test.
  86     // NOTE: if the event is not covered, a bug should be open, and bug number
  87     // noted in the comments for this set.
  88     private static final Set<String> knownNotCoveredEvents = new HashSet<>(
  89         // DumpReason: JDK-8213918, Shutdown: JDK-8213917
  90         Arrays.asList("DumpReason", "Shutdown")
  91     );
  92 
  93 
  94     public static void main(String[] args) throws Exception {
  95         for (EventType type : FlightRecorder.getFlightRecorder().getEventTypes()) {
  96             if (type.getAnnotation(Experimental.class) == null) {
  97                 jfrEventTypes.add(type.getName().replace("jdk.", ""));
  98             }
  99         }
 100 
 101         checkEventNamesClass();
 102         lookForEventsNotCoveredByTests();
 103     }
 104 
 105     // Look thru JFR tests to make sure JFR events are referenced in the tests
 106     private static void lookForEventsNotCoveredByTests() throws Exception {
 107         List<Path> paths = Files.walk(jfrTestRoot)
 108             .filter(Files::isRegularFile)
 109             .filter(path -> isJavaFile(path))
 110             .collect(Collectors.toList());
 111 
 112         Set<String> eventsNotCoveredByTest = new HashSet<>(jfrEventTypes);
 113         for (String event : jfrEventTypes) {
 114             for (Path p : paths) {
 115                 if (findStringInFile(p, event)) {
 116                     eventsNotCoveredByTest.remove(event);
 117                     break;
 118                 }
 119             }
 120         }
 121 
 122         // Account for hard-to-test, experimental and GC tested events
 123         eventsNotCoveredByTest.removeAll(hardToTestEvents);
 124         eventsNotCoveredByTest.removeAll(coveredGcEvents);
 125         eventsNotCoveredByTest.removeAll(knownNotCoveredEvents);
 126 
 127         if (!eventsNotCoveredByTest.isEmpty()) {
 128             print(MSG_SEPARATOR + " Events not covered by test");
 129             for (String event: eventsNotCoveredByTest) {
 130                 print(event);
 131             }
 132             print(MSG_SEPARATOR);
 133             throw new RuntimeException("Found JFR events not covered by tests");
 134         }
 135     }
 136 
 137     // Make sure all the JFR events are accounted for in jdk.test.lib.jfr.EventNames
 138     private static void checkEventNamesClass() throws Exception {
 139         // jdk.test.lib.jfr.EventNames
 140         Set<String> eventsFromEventNamesClass = new HashSet<>();
 141         for (Field f : EventNames.class.getFields()) {
 142             String name = f.getName();
 143             if (!name.equals("PREFIX")) {
 144                 String eventName = (String) f.get(null);
 145                 eventName = eventName.replace(EventNames.PREFIX, "");
 146                 eventsFromEventNamesClass.add(eventName);
 147             }
 148         }
 149 
 150         // Account for the events that are known to be missing from the EventNames.java
 151         eventsFromEventNamesClass.addAll(knownEventsMissingFromEventNames);
 152 
 153         if (!jfrEventTypes.equals(eventsFromEventNamesClass)) {
 154             String exceptionMsg = "Events declared in jdk.test.lib.jfr.EventNames differ " +
 155                          "from events returned by FlightRecorder.getEventTypes()";
 156             print(MSG_SEPARATOR);
 157             print(exceptionMsg);
 158             print("");
 159             printSetDiff(jfrEventTypes, eventsFromEventNamesClass,
 160                         "jfrEventTypes", "eventsFromEventNamesClass");
 161             print("");
 162 
 163             print("This could be because:");
 164             print("1) You forgot to write a unit test. Please do so in test/jdk/jdk/jfr/event/");
 165             print("2) You wrote a unit test, but you didn't reference the event in");
 166             print("   test/lib/jdk/test/lib/jfr/EventNames.java. ");
 167             print("3) It is not feasible to test the event, not even a sanity test. ");
 168             print("   Add the event name to test/lib/jdk/test/lib/jfr/EventNames.java ");
 169             print("   and a short comment why it can't be tested");
 170             print("4) The event is experimental. Please add 'experimental=\"true\"' to <Event> ");
 171             print("   element in metadata.xml if it is a native event, or @Experimental if it is a ");
 172             print("   Java event. The event will now not show up in JMC");
 173             System.out.println(MSG_SEPARATOR);
 174             throw new RuntimeException(exceptionMsg);
 175         }
 176     }
 177 
 178     // ================ Helper methods
 179     private static boolean isJavaFile(Path p) {
 180         String fileName = p.getFileName().toString();
 181         int i = fileName.lastIndexOf('.');
 182         if ( (i < 0) || (i > fileName.length()) ) {
 183             return false;
 184         }
 185         return "java".equals(fileName.substring(i+1));
 186     }
 187 
 188     private static boolean findStringInFile(Path p, String searchTerm) throws IOException {
 189         long c = 0;
 190         try (Stream<String> stream = Files.lines(p)) {
 191             c = stream
 192                 .filter(line -> line.contains(searchTerm))
 193                 .count();
 194         }
 195         return (c != 0);
 196     }
 197 
 198     private static void printSetDiff(Set<String> a, Set<String> b,
 199         String setAName, String setBName) {
 200         if (a.size() > b.size()) {
 201             a.removeAll(b);
 202             System.out.printf("Set %s has more elements than set %s:", setAName, setBName);
 203             System.out.println();
 204             printSet(a);
 205         } else {
 206             b.removeAll(a);
 207             System.out.printf("Set %s has more elements than set %s:", setBName, setAName);
 208             System.out.println();
 209             printSet(b);
 210         }
 211     }
 212 
 213     private static void printSet(Set<String> set) {
 214         for (String e : set) {
 215             System.out.println(e);
 216         }
 217     }
 218 
 219     private static void print(String s) {
 220         System.out.println(s);
 221     }
 222 }