1 /*
   2  * Copyright (c) 2019, Red Hat, Inc. All rights reserved.
   3  *
   4  * This code is free software; you can redistribute it and/or modify it
   5  * under the terms of the GNU General Public License version 2 only, as
   6  * published by the Free Software Foundation.
   7  *
   8  * This code is distributed in the hope that it will be useful, but WITHOUT
   9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  11  * version 2 for more details (a copy is included in the LICENSE file that
  12  * accompanied this code).
  13  *
  14  * You should have received a copy of the GNU General Public License version
  15  * 2 along with this work; if not, write to the Free Software Foundation,
  16  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  17  *
  18  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  19  * or visit www.oracle.com if you need additional information or have any
  20  * questions.
  21  *
  22  */
  23 
  24 package jdk.jfr.event.gc.detailed;
  25 
  26 import java.nio.file.Paths;
  27 import java.time.Duration;
  28 import java.util.List;
  29 
  30 import jdk.jfr.Recording;
  31 import jdk.jfr.consumer.RecordedEvent;
  32 import jdk.test.lib.Asserts;
  33 import jdk.test.lib.jfr.EventNames;
  34 import jdk.test.lib.jfr.Events;
  35 import jdk.test.lib.jfr.GCHelper;
  36 
  37 /**
  38  * @test
  39  * @bug 8221507
  40  * @requires vm.hasJFR
  41  * @requires vm.gc == "Shenandoah" | vm.gc == null
  42  * @key jfr
  43  * @library /test/lib /test/jdk
  44  * @run main/othervm  -Xmx32m -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC -XX:ShenandoahGarbageThreshold=1 jdk.jfr.event.gc.detailed.TestShenandoahHeapRegionStateChangeEvent
  45  */
  46 
  47 public class TestShenandoahHeapRegionStateChangeEvent {
  48     private final static String EVENT_NAME = EventNames.ShenandoahHeapRegionStateChange;
  49 
  50     public static void main(String[] args) throws Exception {
  51         Recording recording = null;
  52         try {
  53             recording = new Recording();
  54             // activate the event we are interested in and start recording
  55             recording.enable(EVENT_NAME).withThreshold(Duration.ofMillis(0));
  56             recording.start();
  57 
  58             byte[][] array = new byte[1024][];
  59             for (int i = 0; i < array.length; i++) {
  60                 array[i] = new byte[20 * 1024];
  61             }
  62             recording.stop();
  63 
  64             // Verify recording
  65             List<RecordedEvent> events = Events.fromRecording(recording);
  66             Asserts.assertFalse(events.isEmpty(), "No events found");
  67 
  68             for (RecordedEvent event : events) {
  69                 Events.assertField(event, "index").notEqual(-1);
  70                 GCHelper.assertIsValidShenandoahHeapRegionState(Events.assertField(event, "from").getValue());
  71                 GCHelper.assertIsValidShenandoahHeapRegionState(Events.assertField(event, "to").getValue());
  72                 Events.assertField(event, "used").atMost(1L*1024*1024);
  73             }
  74         } finally {
  75             if (recording != null) {
  76                 recording.close();
  77             }
  78         }
  79     }
  80 }