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