Path: blob/master/src/hotspot/share/memory/classLoaderMetaspace.cpp
40949 views
/*1* Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved.2* Copyright (c) 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 "logging/log.hpp"27#include "memory/classLoaderMetaspace.hpp"28#include "memory/metaspace.hpp"29#include "memory/metaspaceUtils.hpp"30#include "memory/metaspace/chunkManager.hpp"31#include "memory/metaspace/internalStats.hpp"32#include "memory/metaspace/metaspaceArena.hpp"33#include "memory/metaspace/metaspaceArenaGrowthPolicy.hpp"34#include "memory/metaspace/metaspaceSettings.hpp"35#include "memory/metaspace/metaspaceStatistics.hpp"36#include "memory/metaspace/runningCounters.hpp"37#include "memory/metaspaceTracer.hpp"38#include "utilities/debug.hpp"3940using metaspace::ChunkManager;41using metaspace::MetaspaceArena;42using metaspace::ArenaGrowthPolicy;43using metaspace::RunningCounters;44using metaspace::InternalStats;4546#define LOGFMT "CLMS @" PTR_FORMAT " "47#define LOGFMT_ARGS p2i(this)4849ClassLoaderMetaspace::ClassLoaderMetaspace(Mutex* lock, Metaspace::MetaspaceType space_type) :50_lock(lock),51_space_type(space_type),52_non_class_space_arena(NULL),53_class_space_arena(NULL)54{55ChunkManager* const non_class_cm =56ChunkManager::chunkmanager_nonclass();5758// Initialize non-class Arena59_non_class_space_arena = new MetaspaceArena(60non_class_cm,61ArenaGrowthPolicy::policy_for_space_type(space_type, false),62lock,63RunningCounters::used_nonclass_counter(),64"non-class sm");6566// If needed, initialize class arena67if (Metaspace::using_class_space()) {68ChunkManager* const class_cm =69ChunkManager::chunkmanager_class();70_class_space_arena = new MetaspaceArena(71class_cm,72ArenaGrowthPolicy::policy_for_space_type(space_type, true),73lock,74RunningCounters::used_class_counter(),75"class sm");76}7778UL2(debug, "born (nonclass arena: " PTR_FORMAT ", class arena: " PTR_FORMAT ".",79p2i(_non_class_space_arena), p2i(_class_space_arena));80}8182ClassLoaderMetaspace::~ClassLoaderMetaspace() {83UL(debug, "dies.");8485delete _non_class_space_arena;86delete _class_space_arena;8788}8990// Allocate word_size words from Metaspace.91MetaWord* ClassLoaderMetaspace::allocate(size_t word_size, Metaspace::MetadataType mdType) {92if (Metaspace::is_class_space_allocation(mdType)) {93return class_space_arena()->allocate(word_size);94} else {95return non_class_space_arena()->allocate(word_size);96}97}9899// Attempt to expand the GC threshold to be good for at least another word_size words100// and allocate. Returns NULL if failure. Used during Metaspace GC.101MetaWord* ClassLoaderMetaspace::expand_and_allocate(size_t word_size, Metaspace::MetadataType mdType) {102size_t delta_bytes = MetaspaceGC::delta_capacity_until_GC(word_size * BytesPerWord);103assert(delta_bytes > 0, "Must be");104105size_t before = 0;106size_t after = 0;107bool can_retry = true;108MetaWord* res;109bool incremented;110111// Each thread increments the HWM at most once. Even if the thread fails to increment112// the HWM, an allocation is still attempted. This is because another thread must then113// have incremented the HWM and therefore the allocation might still succeed.114do {115incremented = MetaspaceGC::inc_capacity_until_GC(delta_bytes, &after, &before, &can_retry);116res = allocate(word_size, mdType);117} while (!incremented && res == NULL && can_retry);118119if (incremented) {120Metaspace::tracer()->report_gc_threshold(before, after,121MetaspaceGCThresholdUpdater::ExpandAndAllocate);122// Keeping both for now until I am sure the old variant (gc + metaspace) is not needed anymore123log_trace(gc, metaspace)("Increase capacity to GC from " SIZE_FORMAT " to " SIZE_FORMAT, before, after);124UL2(info, "GC threshold increased: " SIZE_FORMAT "->" SIZE_FORMAT ".", before, after);125}126127return res;128}129130// Prematurely returns a metaspace allocation to the _block_freelists131// because it is not needed anymore.132void ClassLoaderMetaspace::deallocate(MetaWord* ptr, size_t word_size, bool is_class) {133if (Metaspace::using_class_space() && is_class) {134class_space_arena()->deallocate(ptr, word_size);135} else {136non_class_space_arena()->deallocate(ptr, word_size);137}138DEBUG_ONLY(InternalStats::inc_num_deallocs();)139}140141// Update statistics. This walks all in-use chunks.142void ClassLoaderMetaspace::add_to_statistics(metaspace::ClmsStats* out) const {143if (non_class_space_arena() != NULL) {144non_class_space_arena()->add_to_statistics(&out->_arena_stats_nonclass);145}146if (class_space_arena() != NULL) {147class_space_arena()->add_to_statistics(&out->_arena_stats_class);148}149}150151#ifdef ASSERT152void ClassLoaderMetaspace::verify() const {153if (non_class_space_arena() != NULL) {154non_class_space_arena()->verify();155}156if (class_space_arena() != NULL) {157class_space_arena()->verify();158}159}160#endif // ASSERT161162// This only exists for JFR and jcmd VM.classloader_stats. We may want to163// change this. Capacity as a stat is of questionable use since it may164// contain committed and uncommitted areas. For now we do this to maintain165// backward compatibility with JFR.166void ClassLoaderMetaspace::calculate_jfr_stats(size_t* p_used_bytes, size_t* p_capacity_bytes) const {167// Implement this using the standard statistics objects.168size_t used_c = 0, cap_c = 0, used_nc = 0, cap_nc = 0;169if (non_class_space_arena() != NULL) {170non_class_space_arena()->usage_numbers(&used_nc, NULL, &cap_nc);171}172if (class_space_arena() != NULL) {173class_space_arena()->usage_numbers(&used_c, NULL, &cap_c);174}175if (p_used_bytes != NULL) {176*p_used_bytes = used_c + used_nc;177}178if (p_capacity_bytes != NULL) {179*p_capacity_bytes = cap_c + cap_nc;180}181}182183184185