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.time.Duration;
  32 import java.time.Instant;
  33 
  34 import jdk.jfr.Recording;
  35 import jdk.jfr.consumer.RecordedEvent;
  36 import jdk.test.lib.jfr.Events;
  37 import jdk.test.lib.jfr.GCHelper;
  38 
  39 
  40 public class YoungGarbageCollectionEvent {
  41 
  42     private static final String EVENT_NAME = GCHelper.event_young_garbage_collection;
  43 
  44     public static void test() throws Exception {
  45         Recording recording = new Recording();
  46         recording.enable(EVENT_NAME).withThreshold(Duration.ofMillis(0));
  47         recording.start();
  48         triggerGC();
  49         recording.stop();
  50 
  51         boolean isAnyFound = false;
  52         for (RecordedEvent event : Events.fromRecording(recording)) {
  53             if (!EVENT_NAME.equals(event.getEventType().getName())) {
  54                 continue;
  55             }
  56             System.out.println("Event: " + event);
  57             isAnyFound = true;
  58             Events.assertField(event, "gcId").atLeast(0);
  59             Events.assertField(event, "tenuringThreshold").atLeast(1);
  60 
  61             Instant startTime = event.getStartTime();
  62             Instant endTime = event.getEndTime();
  63             Duration duration = event.getDuration();
  64             assertGreaterThan(startTime, Instant.EPOCH, "startTime should be at least 0");
  65             assertGreaterThan(endTime, Instant.EPOCH, "endTime should be at least 0");
  66             // TODO: Maybe we should accept duration of 0, but old test did not.
  67             assertGreaterThan(duration, Duration.ZERO, "Duration should be above 0");
  68             assertGreaterThan(endTime, startTime, "End time should be after start time");
  69         }
  70         assertTrue(isAnyFound, "No matching event found");
  71     }
  72 
  73     public static byte[] garbage;
  74     private static void triggerGC() {
  75         for (int i = 0; i < 3072; i++) {
  76             garbage = new byte[1024];
  77         }
  78     }
  79 }