1 /*
   2  * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2020 SAP SE. All rights reserved.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  *
  24  */
  25 
  26 #include "precompiled.hpp"
  27 
  28 #include "memory/metaspace/msArenaGrowthPolicy.hpp"
  29 #include "utilities/globalDefinitions.hpp"
  30 
  31 namespace metaspace {
  32 
  33 // hard-coded chunk allocation sequences for various space types
  34 //  (Note: when modifying this, don't add jumps of more than double the
  35 //   last chunk size. There is a gtest testing this, see test_arenagrowthpolicy.cpp)
  36 
  37 static const chunklevel_t g_sequ_standard_non_class[] = {
  38     chunklevel::CHUNK_LEVEL_4K,
  39     chunklevel::CHUNK_LEVEL_4K,
  40     chunklevel::CHUNK_LEVEL_4K,
  41     chunklevel::CHUNK_LEVEL_8K,
  42     chunklevel::CHUNK_LEVEL_16K
  43     // .. repeat last
  44 };
  45 
  46 static const chunklevel_t g_sequ_standard_class[] = {
  47     chunklevel::CHUNK_LEVEL_2K,
  48     chunklevel::CHUNK_LEVEL_2K,
  49     chunklevel::CHUNK_LEVEL_4K,
  50     chunklevel::CHUNK_LEVEL_8K,
  51     chunklevel::CHUNK_LEVEL_16K
  52     // .. repeat last
  53 };
  54 
  55 static const chunklevel_t g_sequ_anon_non_class[] = {
  56    chunklevel::CHUNK_LEVEL_1K,
  57    // .. repeat last
  58 };
  59 
  60 static const chunklevel_t g_sequ_anon_class[] = {
  61     chunklevel::CHUNK_LEVEL_1K,
  62     // .. repeat last
  63 };
  64 
  65 static const chunklevel_t g_sequ_refl_non_class[] = {
  66     chunklevel::CHUNK_LEVEL_2K,
  67     chunklevel::CHUNK_LEVEL_1K
  68     // .. repeat last
  69 };
  70 
  71 static const chunklevel_t g_sequ_refl_class[] = {
  72     chunklevel::CHUNK_LEVEL_1K,
  73     // .. repeat last
  74 };
  75 
  76 // Boot class loader: give it large chunks: beyond commit granule size
  77 // (typically 64K) the costs for large chunks largely diminishes since
  78 // they are committed on the fly.
  79 static const chunklevel_t g_sequ_boot_non_class[] = {
  80     chunklevel::CHUNK_LEVEL_4M,
  81     chunklevel::CHUNK_LEVEL_1M
  82     // .. repeat last
  83 };
  84 
  85 static const chunklevel_t g_sequ_boot_class[] = {
  86     chunklevel::CHUNK_LEVEL_256K
  87     // .. repeat last
  88 };
  89 
  90 const ArenaGrowthPolicy* ArenaGrowthPolicy::policy_for_space_type(Metaspace::MetaspaceType space_type, bool is_class) {
  91 
  92 #define DEFINE_CLASS_FOR_ARRAY(what) \
  93   static ArenaGrowthPolicy chunk_alloc_sequence_##what (g_sequ_##what, sizeof(g_sequ_##what)/sizeof(chunklevel_t));
  94 
  95   DEFINE_CLASS_FOR_ARRAY(standard_non_class)
  96   DEFINE_CLASS_FOR_ARRAY(standard_class)
  97   DEFINE_CLASS_FOR_ARRAY(anon_non_class)
  98   DEFINE_CLASS_FOR_ARRAY(anon_class)
  99   DEFINE_CLASS_FOR_ARRAY(refl_non_class)
 100   DEFINE_CLASS_FOR_ARRAY(refl_class)
 101   DEFINE_CLASS_FOR_ARRAY(boot_non_class)
 102   DEFINE_CLASS_FOR_ARRAY(boot_class)
 103 
 104   if (is_class) {
 105     switch(space_type) {
 106     case Metaspace::StandardMetaspaceType:          return &chunk_alloc_sequence_standard_class;
 107     case Metaspace::ReflectionMetaspaceType:        return &chunk_alloc_sequence_refl_class;
 108     case Metaspace::ClassMirrorHolderMetaspaceType: return &chunk_alloc_sequence_anon_class;
 109     case Metaspace::BootMetaspaceType:              return &chunk_alloc_sequence_boot_class;
 110     default: ShouldNotReachHere();
 111     }
 112   } else {
 113     switch(space_type) {
 114     case Metaspace::StandardMetaspaceType:          return &chunk_alloc_sequence_standard_non_class;
 115     case Metaspace::ReflectionMetaspaceType:        return &chunk_alloc_sequence_refl_non_class;
 116     case Metaspace::ClassMirrorHolderMetaspaceType: return &chunk_alloc_sequence_anon_non_class;
 117     case Metaspace::BootMetaspaceType:              return &chunk_alloc_sequence_boot_non_class;
 118     default: ShouldNotReachHere();
 119     }
 120   }
 121 
 122   return NULL;
 123 
 124 }
 125 
 126 } // namespace
 127