1 /* 2 * Copyright (c) 2015, 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 25 import java.lang.management.MemoryPoolMXBean; 26 27 import sun.hotspot.WhiteBox; 28 import jdk.test.lib.Asserts; 29 30 /** 31 * @test ValueTypeDensity 32 * @summary Heap density test for ValueTypes 33 * @library /testlibrary /test/lib 34 * @build sun.hotspot.WhiteBox 35 * ValueTypeDensity 36 * @run driver ClassFileInstaller sun.hotspot.WhiteBox 37 * sun.hotspot.WhiteBox$WhiteBoxPermission 38 * @run main/othervm -noverify -Xint -XX:+ValueArrayFlatten -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI ValueTypeDensity 39 */ 40 41 public class ValueTypeDensity { 42 43 private static final WhiteBox WHITE_BOX = WhiteBox.getWhiteBox(); 44 45 public ValueTypeDensity() { 46 if (!WHITE_BOX.getBooleanVMFlag("ValueArrayFlatten")) { 47 throw new IllegalStateException("ValueArrayFlatten false"); 48 } 49 } 50 51 interface LocalDate { 52 public int getYear(); 53 public short getMonth(); 54 public short getDay(); 55 } 56 57 interface LocalTime { 58 public byte getHour(); 59 public byte getMinute(); 60 public byte getSecond(); 61 public int getNano(); 62 } 63 64 interface LocalDateTime extends LocalDate, LocalTime {} 65 66 static final __ByValue class LocalDateValue implements LocalDate { 67 final int year; 68 final short month; 69 final short day; 70 71 LocalDateValue(int year, short month, short day) { 72 this.year = year; 73 this.month = month; 74 this.day = day; 75 } 76 77 public int getYear() { return year; } 78 public short getMonth() { return month; } 79 public short getDay() { return day; } 80 81 public static LocalDateValue create(int year, short month, short day) { 82 return __Make LocalDateValue(year, month, day); 83 } 84 } 85 86 static final __ByValue class LocalTimeValue implements LocalTime { 87 final byte hour; 88 final byte minute; 89 final byte second; 90 final int nano; 91 92 LocalTimeValue(byte hour, byte minute, byte second, int nano) { 93 this.hour = hour; 94 this.minute = minute; 95 this.second = second; 96 this.nano = nano; 97 } 98 99 public byte getHour() { return hour; } 100 public byte getMinute() { return minute; } 101 public byte getSecond() { return second; } 102 public int getNano() { return nano; } 103 104 public static LocalTimeValue create(byte hour, byte minute, byte second, int nano) { 105 return __Make LocalTimeValue(hour, minute, second, nano); 106 } 107 } 108 109 static final __ByValue class LocalDateTimeValue implements LocalDateTime { 110 final LocalDateValue date; 111 final LocalTimeValue time; 112 113 LocalDateTimeValue(LocalDateValue date, LocalTimeValue time) { 114 this.date = date; 115 this.time = time; 116 } 117 118 public int getYear() { return date.year; } 119 public short getMonth() { return date.month; } 120 public short getDay() { return date.day; } 121 122 public byte getHour() { return time.hour; } 123 public byte getMinute() { return time.minute; } 124 public byte getSecond() { return time.second; } 125 public int getNano() { return time.nano; } 126 127 public static LocalDateTimeValue create(LocalDateValue date, LocalTimeValue time) { 128 return __Make LocalDateTimeValue(date, time); 129 } 130 } 131 132 static final class LocalDateClass implements LocalDate { 133 final int year; 134 final short month; 135 final short day; 136 137 LocalDateClass(int year, short month, short day) { 138 this.year = year; 139 this.month = month; 140 this.day = day; 141 } 142 143 public int getYear() { return year; } 144 public short getMonth() { return month; } 145 public short getDay() { return day; } 146 } 147 148 static final class LocalTimeClass implements LocalTime { 149 final byte hour; 150 final byte minute; 151 final byte second; 152 final int nano; 153 154 LocalTimeClass(byte hour, byte minute, byte second, int nano) { 155 this.hour = hour; 156 this.minute = minute; 157 this.second = second; 158 this.nano = nano; 159 } 160 161 public byte getHour() { return hour; } 162 public byte getMinute() { return minute; } 163 public byte getSecond() { return second; } 164 public int getNano() { return nano; } 165 } 166 167 static final class LocalDateTimeClass implements LocalDateTime { 168 final LocalDateClass date; 169 final LocalTimeClass time; 170 171 LocalDateTimeClass(LocalDateClass date, LocalTimeClass time) { 172 this.date = date; 173 this.time = time; 174 } 175 176 public LocalDateClass getDate() { return date; } 177 public LocalTimeClass getTime() { return time; } 178 179 public int getYear() { return date.year; } 180 public short getMonth() { return date.month; } 181 public short getDay() { return date.day; } 182 183 public byte getHour() { return time.hour; } 184 public byte getMinute() { return time.minute; } 185 public byte getSecond() { return time.second; } 186 public int getNano() { return time.nano; } 187 } 188 189 public void ensureArraySizeWin() { 190 int arrayLength = 1000; 191 System.out.println("ensureArraySizeWin for length " + arrayLength); 192 LocalDateTimeClass[] objectArray = new LocalDateTimeClass[arrayLength]; 193 for (int i = 0; i < arrayLength; i++) { 194 objectArray[i] = new LocalDateTimeClass(new LocalDateClass(0, (short)0, (short)0), 195 new LocalTimeClass((byte)0, (byte)0, (byte)0, 0)); 196 } 197 198 long objectArraySize = WHITE_BOX.getObjectSize(objectArray); 199 System.out.println("Empty object array size: " + objectArraySize); 200 objectArraySize += (arrayLength * 201 (WHITE_BOX.getObjectSize(objectArray[0]) + 202 WHITE_BOX.getObjectSize(objectArray[0].getDate()) + 203 WHITE_BOX.getObjectSize(objectArray[0].getTime()))); 204 205 LocalDateTimeValue[] valueArray = new LocalDateTimeValue[arrayLength]; 206 // CMH: add "isFlatValueArray" to WhiteBox API, to ensure we are correctly account size 207 208 long valueArraySize = WHITE_BOX.getObjectSize(valueArray); 209 System.out.println("Object array and elements: " + objectArraySize + " versus Value Array: " + valueArraySize); 210 Asserts.assertLessThan(valueArraySize, objectArraySize, "Value array accounts for more heap than object array + elements !"); 211 } 212 213 public void test() { 214 ensureArraySizeWin(); 215 } 216 217 public static void main(String[] args) { 218 new ValueTypeDensity().test(); 219 } 220 221 } 222