1 /*
   2  * Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2018, 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 #ifndef SHARE_MEMORY_METASPACE_MSCOMMON_HPP
  27 #define SHARE_MEMORY_METASPACE_MSCOMMON_HPP
  28 
  29 #include "runtime/globals.hpp"
  30 #include "utilities/align.hpp"
  31 #include "utilities/debug.hpp"
  32 #include "utilities/globalDefinitions.hpp"
  33 
  34 class outputStream;
  35 
  36 namespace metaspace {
  37 
  38 // Metaspace allocation alignment:
  39 
  40 // 1) Metaspace allocations have to be aligned such that 64bit values are aligned
  41 //  correctly.
  42 //
  43 // 2) Klass* structures allocated from Metaspace have to be aligned to KlassAlignmentInBytes.
  44 //
  45 // At the moment LogKlassAlignmentInBytes is 3, so KlassAlignmentInBytes == 8,
  46 //  so (1) and (2) can both be fulfilled with an alignment of 8. Should we increase
  47 //  KlassAlignmentInBytes at any time this will increase the necessary alignment as well. In
  48 //  that case we may think about introducing a separate alignment just for the class space
  49 //  since that alignment would only be needed for Klass structures.
  50 
  51 static const size_t AllocationAlignmentByteSize = 8;
  52 STATIC_ASSERT(AllocationAlignmentByteSize == (size_t)KlassAlignmentInBytes);
  53 
  54 static const size_t AllocationAlignmentWordSize = AllocationAlignmentByteSize / BytesPerWord;
  55 
  56 // Returns the raw word size allocated for a given net allocation
  57 size_t get_raw_word_size_for_requested_word_size(size_t word_size);
  58 
  59 // Utility functions
  60 
  61 // Print a size, in words, scaled.
  62 void print_scaled_words(outputStream* st, size_t word_size, size_t scale = 0, int width = -1);
  63 
  64 // Convenience helper: prints a size value and a percentage.
  65 void print_scaled_words_and_percentage(outputStream* st, size_t word_size, size_t compare_word_size, size_t scale = 0, int width = -1);
  66 
  67 // Print a human readable size.
  68 // byte_size: size, in bytes, to be printed.
  69 // scale: one of 1 (byte-wise printing), sizeof(word) (word-size printing), K, M, G (scaled by KB, MB, GB respectively,
  70 //         or 0, which means the best scale is choosen dynamically.
  71 // width: printing width.
  72 void print_human_readable_size(outputStream* st, size_t byte_size, size_t scale = 0, int width = -1);
  73 
  74 // Prints a percentage value. Values smaller than 1% but not 0 are displayed as "<1%", values
  75 // larger than 99% but not 100% are displayed as ">100%".
  76 void print_percentage(outputStream* st, size_t total, size_t part);
  77 
  78 #ifdef ASSERT
  79 #define assert_is_aligned(value, alignment)                  \
  80   assert(is_aligned((value), (alignment)),                   \
  81          SIZE_FORMAT_HEX " is not aligned to "               \
  82          SIZE_FORMAT_HEX, (size_t)(uintptr_t)value, (size_t)(alignment))
  83 #else
  84 #define assert_is_aligned(value, alignment)
  85 #endif
  86 
  87 // Pretty printing helpers
  88 const char* classes_plural(uintx num);
  89 const char* loaders_plural(uintx num);
  90 void print_number_of_classes(outputStream* out, uintx classes, uintx classes_shared);
  91 
  92 // Since Metaspace verifications are expensive, we want to do them at a reduced rate,
  93 // but not completely avoiding them.
  94 // For that we introduce the macros SOMETIMES() and ASSERT_SOMETIMES() which will
  95 // execute code or assert at intervals controlled via VerifyMetaspaceInterval.
  96 #ifdef ASSERT
  97 
  98 #define EVERY_NTH(n)          \
  99 { static int counter_ = 0;    \
 100   if (n > 0) {                \
 101     counter_++;              \
 102     if (counter_ > n) {       \
 103       counter_ = 0;           \
 104 
 105 #define END_EVERY_NTH         } } }
 106 
 107 #define SOMETIMES(code) \
 108     EVERY_NTH(VerifyMetaspaceInterval) \
 109     { code } \
 110     END_EVERY_NTH
 111 
 112 #define ASSERT_SOMETIMES(condition, ...) \
 113     EVERY_NTH(VerifyMetaspaceInterval) \
 114     assert( (condition), __VA_ARGS__); \
 115     END_EVERY_NTH
 116 
 117 #else
 118 
 119 #define SOMETIMES(code)
 120 #define ASSERT_SOMETIMES(condition, ...)
 121 
 122 #endif // ASSERT
 123 
 124 ///////// Logging //////////////
 125 
 126 // What we log at which levels:
 127 
 128 // "info" : metaspace failed allocation, commit failure, reserve failure, metaspace oom, metaspace gc threshold changed, Arena created, destroyed, metaspace purged
 129 
 130 // "debug" : "info" + vslist extended, memory committed/uncommitted, chunk created/split/merged/enlarged, chunk returned
 131 
 132 // "trace" : "debug" + every single allocation and deallocation, internals
 133 
 134 #define HAVE_UL
 135 
 136 #ifdef HAVE_UL
 137 #define UL(level, message)        log_##level(metaspace)(LOGFMT ": " message, LOGFMT_ARGS);
 138 #define UL2(level, message, ...)  log_##level(metaspace)(LOGFMT ": " message, LOGFMT_ARGS, __VA_ARGS__);
 139 #else
 140 #define UL(level, ...)
 141 #define UL2(level, ...)
 142 #endif
 143 
 144 } // namespace metaspace
 145 
 146 #endif // SHARE_MEMORY_METASPACE_MSCOMMON_HPP