1 /*
   2  * Copyright (c) 2013, 2019, 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 jfr.event.gc.heapsummary;
  27 
  28 import java.time.Duration;
  29 
  30 import jdk.jfr.Recording;
  31 import jdk.jfr.consumer.RecordedEvent;
  32 import com.oracle.java.testlibrary.Asserts;
  33 import com.oracle.java.testlibrary.jfr.EventNames;
  34 import com.oracle.java.testlibrary.jfr.EventVerifier;
  35 import com.oracle.java.testlibrary.jfr.Events;
  36 import com.oracle.java.testlibrary.jfr.GCHelper;
  37 import sun.hotspot.WhiteBox;
  38 import com.oracle.java.testlibrary.jfr.HeapSummaryEventAllGcs;
  39 
  40 /*
  41  * @test
  42  * @requires vm.gc == "Parallel" | vm.gc == null
  43  * @library /testlibrary /testlibrary/whitebox
  44  * @build sun.hotspot.WhiteBox
  45  * @run main ClassFileInstaller sun.hotspot.WhiteBox
  46  * @run main/othervm -XX:+UnlockExperimentalVMOptions
  47                      -XX:-UseFastUnorderedTimeStamps -Xmx16m -XX:+UseParallelGC
  48                      -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -XX:+EnableJFR
  49                      jfr.event.gc.heapsummary.TestHeapSummaryCommittedSize
  50  */
  51 public class TestHeapSummaryCommittedSize {
  52     private final static String EVENT_NAME = EventNames.GCHeapSummary;
  53 
  54     public static void main(String[] args) throws Exception {
  55         Recording recording = new Recording();
  56         recording.enable(EVENT_NAME).withThreshold(Duration.ofMillis(0));
  57 
  58         recording.start();
  59         System.gc();
  60         recording.stop();
  61 
  62         boolean isAnyFound = false;
  63         for (RecordedEvent event : Events.fromRecording(recording)) {
  64             System.out.println("Event: " + event);
  65             if (!Events.isEventType(event, EVENT_NAME)) {
  66                 continue;
  67             }
  68             isAnyFound = true;
  69             CommittedHeapSizeVerifier verifier = new CommittedHeapSizeVerifier(event);
  70             verifier.verify();
  71         }
  72         Asserts.assertTrue(isAnyFound, "No matching event");
  73     }
  74 }
  75 
  76 class CommittedHeapSizeVerifier extends EventVerifier {
  77     private final static long  MAX_UNALIGNED_COMMITTED_SIZE  = 16 * 1024 * 1024;
  78     private final long  MAX_ALIGNED_COMMITTED_SIZE;
  79 
  80     public CommittedHeapSizeVerifier(RecordedEvent event) {
  81         super(event);
  82         WhiteBox wb = WhiteBox.getWhiteBox();
  83         long heapAlignment = wb.getHeapAlignment();
  84         MAX_ALIGNED_COMMITTED_SIZE = GCHelper.alignUp(
  85                 MAX_UNALIGNED_COMMITTED_SIZE,heapAlignment);
  86     }
  87 
  88     public void verify() throws Exception {
  89         Events.assertField(event, "heapSpace.committedSize").atLeast(0L).atMost(MAX_ALIGNED_COMMITTED_SIZE);
  90     }
  91 }