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. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 package jdk.jfr.event.gc.objectcount; 25 26 import java.util.List; 27 import java.util.HashMap; 28 29 import jdk.jfr.consumer.RecordedEvent; 30 import jdk.test.lib.Asserts; 31 import jdk.test.lib.jfr.Events; 32 33 class Foo { 34 } 35 36 class Constants { 37 public static final int oneMB = 1048576; 38 } 39 40 public class ObjectCountEventVerifier { 41 public static Foo[] foos; 42 43 public static void createTestData() { 44 foos = new Foo[Constants.oneMB]; 45 } 46 47 public static void verify(List<RecordedEvent> objectCountEvents) throws Exception { 48 Asserts.assertFalse(objectCountEvents.isEmpty(), "Expected at least one object count event"); 49 // Object count events should be sent only for those classes which instances occupy over 0.5% 50 // of the heap. Therefore there can't be more than 200 events 51 Asserts.assertLessThanOrEqual(objectCountEvents.size(), 200, "Expected at most 200 object count events"); 52 53 HashMap<String, Long> numInstancesOfClass = new HashMap<String, Long>(); 54 HashMap<String, Long> sizeOfInstances = new HashMap<String, Long>(); 55 56 for (RecordedEvent event : objectCountEvents) { 57 String className = Events.assertField(event, "objectClass.name").notEmpty().getValue(); 58 long count = Events.assertField(event, "count").atLeast(0L).getValue(); 59 long totalSize = Events.assertField(event, "totalSize").atLeast(1L).getValue(); 60 System.out.println(className); 61 numInstancesOfClass.put(className, count); 62 sizeOfInstances.put(className, totalSize); 63 } 64 System.out.println(numInstancesOfClass); 65 final String fooArrayName = "[Ljdk/jfr/event/gc/objectcount/Foo;"; 66 Asserts.assertTrue(numInstancesOfClass.containsKey(fooArrayName), "Expected an event for the Foo array"); 67 Asserts.assertEquals(sizeOfInstances.get(fooArrayName), expectedFooArraySize(Constants.oneMB), "Wrong size of the Foo array"); 68 } 69 70 private static long expectedFooArraySize(long count) { 71 boolean runsOn32Bit = System.getProperty("sun.arch.data.model").equals("32"); 72 int bytesPerWord = runsOn32Bit ? 4 : 8; 73 // length will be in klass-gap on 64 bits, extra field on 32 bits. 74 int objectHeaderSize = bytesPerWord * (runsOn32Bit ? 3 : 2); 75 int alignmentInOopArray = runsOn32Bit ? 4 : 0; 76 int ptrSize = bytesPerWord; 77 return objectHeaderSize + alignmentInOopArray + count * ptrSize; 78 } 79 }