Path: blob/master/src/hotspot/share/memory/metaspace/metaspaceCommon.cpp
40957 views
/*1* Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.2* Copyright (c) 2018, 2020 SAP SE. 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 it6* under the terms of the GNU General Public License version 2 only, as7* published by the Free Software Foundation.8*9* This code is distributed in the hope that it will be useful, but WITHOUT10* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or11* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License12* version 2 for more details (a copy is included in the LICENSE file that13* accompanied this code).14*15* You should have received a copy of the GNU General Public License version16* 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 USA20* or visit www.oracle.com if you need additional information or have any21* questions.22*23*/2425#include "precompiled.hpp"26#include "memory/metaspace/allocationGuard.hpp"27#include "memory/metaspace/freeBlocks.hpp"28#include "memory/metaspace/metaspaceCommon.hpp"29#include "memory/metaspace/metaspaceSettings.hpp"30#include "memory/metaspace/virtualSpaceNode.hpp"31#include "utilities/align.hpp"32#include "utilities/debug.hpp"33#include "utilities/globalDefinitions.hpp"34#include "utilities/ostream.hpp"3536namespace metaspace {3738// Print a size, in words, scaled.39void print_scaled_words(outputStream* st, size_t word_size, size_t scale, int width) {40print_human_readable_size(st, word_size * sizeof(MetaWord), scale, width);41}4243// Convenience helper: prints a size value and a percentage.44void print_scaled_words_and_percentage(outputStream* st, size_t word_size, size_t compare_word_size, size_t scale, int width) {45print_scaled_words(st, word_size, scale, width);46st->print(" (");47print_percentage(st, compare_word_size, word_size);48st->print(")");49}5051static const char* display_unit_for_scale(size_t scale) {52const char* s = NULL;53switch(scale) {54case 1: s = "bytes"; break;55case BytesPerWord: s = "words"; break;56case K: s = "KB"; break;57case M: s = "MB"; break;58case G: s = "GB"; break;59default:60ShouldNotReachHere();61}62return s;63}6465// Print a human readable size.66// byte_size: size, in bytes, to be printed.67// scale: one of 1 (byte-wise printing), sizeof(word) (word-size printing), K, M, G (scaled by KB, MB, GB respectively,68// or 0, which means the best scale is choosen dynamically.69// width: printing width.70void print_human_readable_size(outputStream* st, size_t byte_size, size_t scale, int width) {71if (scale == 0) {72// Dynamic mode. Choose scale for this value.73if (byte_size == 0) {74// Zero values are printed as bytes.75scale = 1;76} else {77if (byte_size >= G) {78scale = G;79} else if (byte_size >= M) {80scale = M;81} else if (byte_size >= K) {82scale = K;83} else {84scale = 1;85}86}87return print_human_readable_size(st, byte_size, scale, width);88}8990#ifdef ASSERT91assert(scale == 1 || scale == BytesPerWord ||92scale == K || scale == M || scale == G, "Invalid scale");93// Special case: printing wordsize should only be done with word-sized values94if (scale == BytesPerWord) {95assert(byte_size % BytesPerWord == 0, "not word sized");96}97#endif9899if (width == -1) {100if (scale == 1) {101st->print(SIZE_FORMAT " bytes", byte_size);102} else if (scale == BytesPerWord) {103st->print(SIZE_FORMAT " words", byte_size / BytesPerWord);104} else {105const char* display_unit = display_unit_for_scale(scale);106float display_value = (float) byte_size / scale;107// Prevent very small but non-null values showing up as 0.00.108if (byte_size > 0 && display_value < 0.01f) {109st->print("<0.01 %s", display_unit);110} else {111st->print("%.2f %s", display_value, display_unit);112}113}114} else {115if (scale == 1) {116st->print("%*" PRIuPTR " bytes", width, byte_size);117} else if (scale == BytesPerWord) {118st->print("%*" PRIuPTR " words", width, byte_size / BytesPerWord);119} else {120const char* display_unit = display_unit_for_scale(scale);121float display_value = (float) byte_size / scale;122// Since we use width to display a number with two trailing digits, increase it a bit.123width += 3;124// Prevent very small but non-null values showing up as 0.00.125if (byte_size > 0 && display_value < 0.01f) {126st->print("%*s %s", width, "<0.01", display_unit);127} else {128st->print("%*.2f %s", width, display_value, display_unit);129}130}131}132}133134// Prints a percentage value. Values smaller than 1% but not 0 are displayed as "<1%", values135// larger than 99% but not 100% are displayed as ">100%".136void print_percentage(outputStream* st, size_t total, size_t part) {137if (total == 0) {138st->print(" ?%%");139} else if (part == 0) {140st->print(" 0%%");141} else if (part == total) {142st->print("100%%");143} else {144// Note: clearly print very-small-but-not-0% and very-large-but-not-100% percentages.145float p = ((float)part / total) * 100.0f;146if (p < 1.0f) {147st->print(" <1%%");148} else if (p > 99.0f){149st->print(">99%%");150} else {151st->print("%3.0f%%", p);152}153}154}155156const char* loaders_plural(uintx num) {157return num == 1 ? "loader" : "loaders";158}159160const char* classes_plural(uintx num) {161return num == 1 ? "class" : "classes";162}163164void print_number_of_classes(outputStream* out, uintx classes, uintx classes_shared) {165out->print(UINTX_FORMAT " %s", classes, classes_plural(classes));166if (classes_shared > 0) {167out->print(" (" UINTX_FORMAT " shared)", classes_shared);168}169}170171// Given a net allocation word size, return the raw word size we actually allocate.172// Note: externally visible for gtests.173//static174size_t get_raw_word_size_for_requested_word_size(size_t word_size) {175size_t byte_size = word_size * BytesPerWord;176177// Deallocated metablocks are kept in a binlist which limits their minimal178// size to at least the size of a binlist item (2 words).179byte_size = MAX2(byte_size, FreeBlocks::MinWordSize * BytesPerWord);180181// Metaspace allocations are aligned to word size.182byte_size = align_up(byte_size, AllocationAlignmentByteSize);183184// If we guard allocations, we need additional space for a prefix.185#ifdef ASSERT186if (Settings::use_allocation_guard()) {187byte_size += align_up(prefix_size(), AllocationAlignmentByteSize);188}189#endif190size_t raw_word_size = byte_size / BytesPerWord;191assert(raw_word_size * BytesPerWord == byte_size, "Sanity");192return raw_word_size;193}194195} // namespace metaspace196197198199