test/jdk/jdk/jfr/event/gc/collection/GCGarbageCollectionUtil.java
changeset 50113 caf115bb98ad
equal deleted inserted replaced
50112:7a2a740815b7 50113:caf115bb98ad
       
     1 /*
       
     2  * Copyright (c) 2013, 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.gc.collection;
       
    27 
       
    28 import static jdk.test.lib.Asserts.assertGreaterThan;
       
    29 import static jdk.test.lib.Asserts.assertTrue;
       
    30 
       
    31 import java.io.File;
       
    32 import java.nio.file.Paths;
       
    33 import java.util.ArrayList;
       
    34 import java.util.Collections;
       
    35 import java.util.HashSet;
       
    36 import java.util.List;
       
    37 import java.util.Set;
       
    38 
       
    39 import jdk.jfr.consumer.RecordedEvent;
       
    40 import jdk.jfr.consumer.RecordingFile;
       
    41 import jdk.test.lib.jfr.AppExecutorHelper;
       
    42 import jdk.test.lib.jfr.Events;
       
    43 import jdk.test.lib.process.OutputAnalyzer;
       
    44 
       
    45 
       
    46 /**
       
    47  * Class to verify GarbageCollection event.
       
    48  * It starts the application provoking GCGarbageCollection with enabled JFR,
       
    49  * collects events and verify the 'name' and 'cause' fields to be expected.
       
    50  * It's supposed to be invoked from tests.
       
    51  */
       
    52 public class GCGarbageCollectionUtil {
       
    53     private final static String EVENT_SETTINGS_FILE =
       
    54             System.getProperty("test.src", ".") + File.separator + "gc-testsettings.jfc";
       
    55 
       
    56     /**
       
    57      * Verifies the 'name' and 'cause' fields of received events to be expected.
       
    58      * @param testID - a string to identify test
       
    59      * @param testFlags - VM flags including GC to start the app
       
    60      * @param gcNames - expected values for the 'name' field
       
    61      * @param gcCauses - expected values for the 'cause' field
       
    62      * @throws Exception in case of any failure
       
    63      */
       
    64     public static void test(String testID, String[] testFlags,
       
    65             String[] gcNames, String... gcCauses) throws Exception {
       
    66 
       
    67         String jfrFile = testID + ".jfr";
       
    68 
       
    69         List<String> summaryFlags = new ArrayList<>();
       
    70         Collections.addAll(summaryFlags, testFlags);
       
    71         summaryFlags.add("-Xmx100m");
       
    72         summaryFlags.add("-XX:+UnlockExperimentalVMOptions");
       
    73         summaryFlags.add("-XX:-UseFastUnorderedTimeStamps");
       
    74         summaryFlags.add("-Xlog:gc*=debug");
       
    75 
       
    76 
       
    77         String args[] = {};
       
    78         OutputAnalyzer analyzer = AppExecutorHelper.executeAndRecord(EVENT_SETTINGS_FILE, jfrFile,
       
    79                 (String[])summaryFlags.toArray(new String[0]), AppGCProvoker.class.getName(), args);
       
    80         analyzer.shouldHaveExitValue(0);
       
    81 
       
    82         Set<String> gcValidNames = new HashSet<>();
       
    83         for (String n: gcNames) {
       
    84             gcValidNames.add(n);
       
    85         }
       
    86         Set<String> gcValidCauses = new HashSet<>();
       
    87         for (String n: gcCauses) {
       
    88             gcValidCauses.add(n);
       
    89         }
       
    90 
       
    91         int total = 0;
       
    92         for (RecordedEvent event : RecordingFile.readAllEvents(Paths.get(jfrFile))) {
       
    93             total++;
       
    94             System.out.println("Event: " + event);
       
    95 
       
    96             final String name = Events.assertField(event, "name").notEmpty().getValue();
       
    97             assertTrue(gcValidNames.contains(name), "GC name '" + name + "' not in the valid list" + gcValidNames);
       
    98 
       
    99             final String cause = Events.assertField(event, "cause").notEmpty().getValue();
       
   100             assertTrue(gcValidCauses.contains(cause), "GC cause '" + cause + "' not in the valid causes" + gcValidCauses);
       
   101         }
       
   102         assertGreaterThan(total, 0, "Expected at least one event");
       
   103     }
       
   104 }