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 package gc.g1.humongousObjects;
  26 
  27 import gc.testlibrary.Helpers;
  28 import sun.hotspot.WhiteBox;
  29 
  30 import java.io.IOException;
  31 import java.nio.file.Path;
  32 
  33 /**
  34  * Provides generated and compiled sample classes and theirs expected sizes.
  35  */
  36 public enum G1SampleClass {
  37 
  38     LARGEST_NON_HUMONGOUS {
  39         @Override
  40         public long expectedInstanceSize() {
  41             return HALF_G1_REGION_SIZE;
  42         }
  43     },
  44     SMALLEST_HUMONGOUS {
  45         @Override
  46         public long expectedInstanceSize() {
  47             return HALF_G1_REGION_SIZE + Helpers.SIZE_OF_LONG;
  48         }
  49     },
  50     ONE_REGION_HUMONGOUS {
  51         @Override
  52         public long expectedInstanceSize() {
  53             return G1_REGION_SIZE;
  54         }
  55     },
  56     TWO_REGION_HUMONGOUS {
  57         @Override
  58         public long expectedInstanceSize() {
  59             return G1_REGION_SIZE + Helpers.SIZE_OF_LONG;
  60         }
  61     },
  62     MORE_THAN_TWO_REGION_HUMONGOUS {
  63         @Override
  64         public long expectedInstanceSize() {
  65             return G1_REGION_SIZE * 2 + Helpers.SIZE_OF_LONG;
  66         }
  67     };
  68 
  69     private static final long G1_REGION_SIZE = WhiteBox.getWhiteBox().g1RegionSize();
  70     private static final long HALF_G1_REGION_SIZE = G1_REGION_SIZE / 2;
  71 
  72     /**
  73      * Generates and compiles class with instance of specified size and loads it in specified class loader
  74      *
  75      * @param classLoader class loader which will be used to load class
  76      * @param wrkDir working dir where generated classes are put and compiled
  77      * @param classNamePrefix prefix for service classes (ones we use to create chain of inheritance)
  78      * @return a class of specified size loaded in specified class loader
  79      * @throws IOException
  80      * @throws ClassNotFoundException
  81      */
  82 
  83     public Class<?> getCls(ClassLoader classLoader, Path wrkDir, String classNamePrefix)
  84             throws IOException, ClassNotFoundException {
  85         return Helpers.generateCompileAndLoad(classLoader, Helpers.enumNameToClassName(name()) + "Class",
  86                 expectedInstanceSize(), wrkDir, classNamePrefix);
  87     }
  88 
  89     /**
  90      * @return G1SampleClass instance expected size
  91      */
  92     public abstract long expectedInstanceSize();
  93 }