1 /*
   2  * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
   3  * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
   4  */
   5 import java.util.ArrayList;
   6 import java.util.List;
   7 
   8 import jrockit.Asserts;
   9 import jrockit.jfr.TestRecording;
  10 import oracle.jrockit.jfr.parser.FLREvent;
  11 
  12 import static jrockit.Asserts.*;
  13 
  14 /*
  15  * @test TestYoungGarbageCollectionEvent
  16  * @key jfr
  17  * @library ../common
  18  *
  19  * @run main/othervm -XX:+UnlockCommercialFeatures -XX:+FlightRecorder -Xmx50m -Xmn2m -XX:+UseSerialGC -XX:+UnlockExperimentalVMOptions -XX:-UseFastUnorderedTimeStamps TestYoungGarbageCollectionEvent DefNew
  20  * @run main/othervm -XX:+UnlockCommercialFeatures -XX:+FlightRecorder -Xmx50m -Xmn2m -XX:+UseParallelGC -XX:+UnlockExperimentalVMOptions -XX:-UseFastUnorderedTimeStamps TestYoungGarbageCollectionEvent ParallelScavenge
  21  * @run main/othervm -XX:+UnlockCommercialFeatures -XX:+FlightRecorder -Xmx50m -Xmn2m -XX:+UseParNewGC -XX:+UnlockExperimentalVMOptions -XX:-UseFastUnorderedTimeStamps TestYoungGarbageCollectionEvent ParNew
  22  * @run main/othervm -XX:+UnlockCommercialFeatures -XX:+FlightRecorder -Xmx50m -Xmn2m -XX:+UseG1GC -XX:+UnlockExperimentalVMOptions -XX:-UseFastUnorderedTimeStamps TestYoungGarbageCollectionEvent G1New
  23  */
  24 public class TestYoungGarbageCollectionEvent {
  25     private static final int UNSET_TIMESTAMP = -1;
  26     private static final String youngGCEvent = "vm/gc/collector/young_garbage_collection";
  27 
  28     public static void main(String[] args) throws Exception {
  29         TestRecording r = new TestRecording();
  30         try {
  31             enableEvent(r, youngGCEvent);
  32 
  33             r.start();
  34             triggerGC();
  35             r.stop();
  36 
  37             List<FLREvent> allEvents = getEventsFromRecording(r, ".*");
  38             List<FLREvent> ycEvents = selectEventsWithPath(allEvents, youngGCEvent);
  39             assertFalse(ycEvents.isEmpty(), "ycEvents must not be empty");
  40 
  41             for (FLREvent e : ycEvents) {
  42                 Integer gcId = (Integer) e.getValue("gcId");
  43                 assertNE(gcId, -1, "Expected a valid GC id");
  44 
  45                 Integer tenuringThreshold = (Integer) e.getValue("tenuringThreshold");
  46                 assertGT(tenuringThreshold, 0, "Expected tenuring threshold to be larger than 0");
  47 
  48                 Long startTime = e.getStartTime();
  49                 Long endTime = e.getTimestamp();
  50 
  51                 assertNE(startTime, UNSET_TIMESTAMP, "Expected start time of event to be defined");
  52                 assertNE(endTime, UNSET_TIMESTAMP, "Expected end time of event to be defined");
  53                 assertGT(endTime, startTime, "Expected the end time of the event to be later than the start time");
  54             }
  55         } catch (Throwable t) {
  56             r.copyTo("TestYoungGarbageCollectionEvent.jfr");
  57             throw t;
  58         } finally {
  59             r.close();
  60         }
  61     }
  62 
  63     private static void enableEvent(TestRecording r, String path) throws Exception {
  64         r.createJVMSetting(path, true, false, 0, 0);
  65     }
  66 
  67 
  68     public static byte[] garbage;
  69     private static void triggerGC() {
  70         for (int i = 0; i < 2048; i++) {
  71             garbage = new byte[1024];
  72         }
  73     }
  74 
  75     private static List<FLREvent> getEventsFromRecording(TestRecording r, String regex) throws Exception {
  76         return r.parser().findJVMEvents(regex);
  77     }
  78 
  79     private static List<FLREvent> selectEventsWithPath(List<FLREvent> events, String path)
  80         throws Exception {
  81         List<FLREvent> selection = new ArrayList<FLREvent>();
  82         for (FLREvent e : events) {
  83             if (e.getPath().equals(path)) {
  84                 selection.add(e);
  85             }
  86         }
  87         return selection;
  88     }
  89 }