1 /*
  2  * Copyright (c) 2018, 2021, 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_METASPACECOMMON_HPP
 27 #define SHARE_MEMORY_METASPACE_METASPACECOMMON_HPP
 28 
 29 #include "oops/compressedKlass.hpp"
 30 #include "runtime/globals.hpp"
 31 #include "utilities/align.hpp"
 32 #include "utilities/debug.hpp"
 33 #include "utilities/globalDefinitions.hpp"
 34 
 35 class outputStream;
 36 
 37 namespace metaspace {
 38 
 39 // Metaspace allocation alignment:
 40 
 41 // Metaspace allocations have to be aligned such that 64-bit values are aligned
 42 //  correctly. We currently don't hold members with a larger alignment requirement
 43 //  than 64-bit inside MetaData, so 8-byte alignment is enough.
 44 //
 45 // Klass* structures need to be aligned to KlassAlignmentInBytes, but since that is
 46 // 64-bit, we don't need special handling for allocating Klass*.
 47 //
 48 // On 64-bit platforms, we align to word size; on 32-bit, we align to two words.
 49 
 50 static const size_t AllocationAlignmentByteSize = 8;
 51 STATIC_ASSERT(AllocationAlignmentByteSize == (size_t)KlassAlignmentInBytes);
 52 
 53 static const size_t AllocationAlignmentWordSize = AllocationAlignmentByteSize / BytesPerWord;
 54 
 55 // Returns the raw word size allocated for a given net allocation. This only matters on 32-bit, where
 56 // allocations have to be 64-bit aligned too and therefore must be 2-word-aligned.
 57 inline size_t get_raw_word_size_for_requested_word_size(size_t word_size) {
 58   LP64_ONLY(STATIC_ASSERT(AllocationAlignmentWordSize == 1)); // rewrite if this does not hold true anymore
 59   return LP64_ONLY(word_size) // no-op on 64-bit
 60          NOT_LP64(align_up(word_size, AllocationAlignmentWordSize));
 61 }
 62 
 63 // Utility functions
 64 
 65 // Print a size, in words, scaled.
 66 void print_scaled_words(outputStream* st, size_t word_size, size_t scale = 0, int width = -1);
 67 
 68 // Convenience helper: prints a size value and a percentage.
 69 void print_scaled_words_and_percentage(outputStream* st, size_t word_size, size_t compare_word_size, size_t scale = 0, int width = -1);
 70 
 71 // Print a human readable size.
 72 // byte_size: size, in bytes, to be printed.
 73 // scale: one of 1 (byte-wise printing), sizeof(word) (word-size printing), K, M, G (scaled by KB, MB, GB respectively,
 74 //         or 0, which means the best scale is chosen dynamically.
 75 // width: printing width.
 76 void print_human_readable_size(outputStream* st, size_t byte_size, size_t scale = 0, int width = -1);
 77 
 78 // Prints a percentage value. Values smaller than 1% but not 0 are displayed as "<1%", values
 79 // larger than 99% but not 100% are displayed as ">100%".
 80 void print_percentage(outputStream* st, size_t total, size_t part);
 81 
 82 #ifdef ASSERT
 83 #define assert_is_aligned(value, alignment)                  \
 84   assert(is_aligned((value), (alignment)),                   \
 85          SIZE_FORMAT_X " is not aligned to "                 \
 86          SIZE_FORMAT_X, (size_t)(uintptr_t)value, (size_t)(alignment))
 87 #define assert_is_aligned_metaspace_pointer(p) \
 88   assert_is_aligned((p), metaspace::AllocationAlignmentByteSize);
 89 #else
 90 #define assert_is_aligned(value, alignment)
 91 #define assert_is_aligned_metaspace_pointer(pointer)
 92 #endif
 93 
 94 // Pretty printing helpers
 95 const char* classes_plural(uintx num);
 96 const char* loaders_plural(uintx num);
 97 void print_number_of_classes(outputStream* out, uintx classes, uintx classes_shared);
 98 
 99 // Since Metaspace verifications are expensive, we want to do them at a reduced rate,
100 // but not completely avoiding them.
101 // For that we introduce the macros SOMETIMES() and ASSERT_SOMETIMES() which will
102 // execute code or assert at intervals controlled via VerifyMetaspaceInterval.
103 #ifdef ASSERT
104 
105 #define EVERY_NTH(n)          \
106 { static int counter_ = 0;    \
107   if (n > 0) {                \
108     counter_++;              \
109     if (counter_ >= n) {      \
110       counter_ = 0;           \
111 
112 #define END_EVERY_NTH         } } }
113 
114 #define SOMETIMES(code) \
115     EVERY_NTH(VerifyMetaspaceInterval) \
116     { code } \
117     END_EVERY_NTH
118 
119 #define ASSERT_SOMETIMES(condition, ...) \
120     EVERY_NTH(VerifyMetaspaceInterval) \
121     assert( (condition), __VA_ARGS__); \
122     END_EVERY_NTH
123 
124 #else
125 
126 #define SOMETIMES(code)
127 #define ASSERT_SOMETIMES(condition, ...)
128 
129 #endif // ASSERT
130 
131 ///////// Logging //////////////
132 
133 // What we log at which levels:
134 
135 // "info" : metaspace failed allocation, commit failure, reserve failure, metaspace oom, metaspace gc threshold changed, Arena created, destroyed, metaspace purged
136 
137 // "debug" : "info" + vslist extended, memory committed/uncommitted, chunk created/split/merged/enlarged, chunk returned
138 
139 // "trace" : "debug" + every single allocation and deallocation, internals
140 
141 #define HAVE_UL
142 
143 #ifdef HAVE_UL
144 #define UL(level, message)        log_##level(metaspace)(LOGFMT ": " message, LOGFMT_ARGS);
145 #define UL2(level, message, ...)  log_##level(metaspace)(LOGFMT ": " message, LOGFMT_ARGS, __VA_ARGS__);
146 #else
147 #define UL(level, ...)
148 #define UL2(level, ...)
149 #endif
150 
151 } // namespace metaspace
152 
153 #endif // SHARE_MEMORY_METASPACE_METASPACECOMMON_HPP