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 #include "memory/metaspace/msChunklevel.hpp"
  28 #include "utilities/debug.hpp"
  29 #include "utilities/globalDefinitions.hpp"
  30 #include "utilities/ostream.hpp"
  31 #include "utilities/powerOfTwo.hpp"
  32 
  33 namespace metaspace {
  34 
  35 using namespace chunklevel;
  36 
  37 chunklevel_t chunklevel::level_fitting_word_size(size_t word_size) {
  38 
  39   assert(MAX_CHUNK_WORD_SIZE >= word_size,
  40          SIZE_FORMAT " - too large allocation size.", word_size * BytesPerWord);
  41 
  42   if (word_size <= MIN_CHUNK_WORD_SIZE) {
  43     return HIGHEST_CHUNK_LEVEL;
  44   }
  45 
  46   const size_t v_pow2 = round_up_power_of_2(word_size);
  47   const chunklevel_t lvl =  (chunklevel_t)(exact_log2(MAX_CHUNK_WORD_SIZE) - exact_log2(v_pow2));
  48 
  49   return lvl;
  50 
  51 }
  52 
  53 void chunklevel::print_chunk_size(outputStream* st, chunklevel_t lvl) {
  54   if (chunklevel::is_valid_level(lvl)) {
  55     const size_t s = chunklevel::word_size_for_level(lvl) * BytesPerWord;
  56     if (s < 1 * M) {
  57       st->print("%3uk", (unsigned)(s / K));
  58     } else {
  59       st->print("%3um", (unsigned)(s / M));
  60     }
  61   } else {
  62     st->print("?-?");
  63   }
  64 
  65 }
  66 
  67 } // namespace metaspace
  68