test/jdk/jdk/jfr/event/gc/objectcount/ObjectCountEventVerifier.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.objectcount;
       
    27 
       
    28 import java.util.List;
       
    29 import java.util.HashMap;
       
    30 
       
    31 import jdk.jfr.consumer.RecordedEvent;
       
    32 import jdk.test.lib.Asserts;
       
    33 import jdk.test.lib.jfr.Events;
       
    34 
       
    35 class Foo {
       
    36 }
       
    37 
       
    38 class Constants {
       
    39     public static final int oneMB = 1048576;
       
    40 }
       
    41 
       
    42 public class ObjectCountEventVerifier {
       
    43     public static Foo[] foos;
       
    44 
       
    45     public static void createTestData() {
       
    46         foos = new Foo[Constants.oneMB];
       
    47     }
       
    48 
       
    49     public static void verify(List<RecordedEvent> objectCountEvents) throws Exception {
       
    50         Asserts.assertFalse(objectCountEvents.isEmpty(), "Expected at least one object count event");
       
    51         // Object count events should be sent only for those classes which instances occupy over 0.5%
       
    52         // of the heap. Therefore there can't be more than 200 events
       
    53         Asserts.assertLessThanOrEqual(objectCountEvents.size(), 200, "Expected at most 200 object count events");
       
    54 
       
    55         HashMap<String, Long> numInstancesOfClass = new HashMap<String, Long>();
       
    56         HashMap<String, Long> sizeOfInstances = new HashMap<String, Long>();
       
    57 
       
    58         for (RecordedEvent event : objectCountEvents) {
       
    59             String className = Events.assertField(event, "objectClass.name").notEmpty().getValue();
       
    60             long count = Events.assertField(event, "count").atLeast(0L).getValue();
       
    61             long totalSize = Events.assertField(event, "totalSize").atLeast(1L).getValue();
       
    62             System.out.println(className);
       
    63             numInstancesOfClass.put(className, count);
       
    64             sizeOfInstances.put(className, totalSize);
       
    65         }
       
    66         System.out.println(numInstancesOfClass);
       
    67         final String fooArrayName = "[Ljdk/jfr/event/gc/objectcount/Foo;";
       
    68         Asserts.assertTrue(numInstancesOfClass.containsKey(fooArrayName), "Expected an event for the Foo array");
       
    69         Asserts.assertEquals(sizeOfInstances.get(fooArrayName), expectedFooArraySize(Constants.oneMB), "Wrong size of the Foo array");
       
    70     }
       
    71 
       
    72     private static long expectedFooArraySize(long count) {
       
    73         boolean runsOn32Bit = System.getProperty("sun.arch.data.model").equals("32");
       
    74         int bytesPerWord = runsOn32Bit ? 4 : 8;
       
    75         int objectHeaderSize = bytesPerWord * 3; // length will be aligned on 64 bits
       
    76         int alignmentInOopArray = runsOn32Bit ? 4 : 0;
       
    77         int ptrSize = bytesPerWord;
       
    78         return objectHeaderSize + alignmentInOopArray + count * ptrSize;
       
    79     }
       
    80 }