1 /*
  2  * Copyright (c) 2020, 2021, 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 "logging/log.hpp"
 28 #include "memory/classLoaderMetaspace.hpp"
 29 #include "memory/metaspace.hpp"
 30 #include "memory/metaspaceUtils.hpp"

 31 #include "memory/metaspace/chunkManager.hpp"
 32 #include "memory/metaspace/internalStats.hpp"
 33 #include "memory/metaspace/metaspaceArena.hpp"
 34 #include "memory/metaspace/metaspaceArenaGrowthPolicy.hpp"
 35 #include "memory/metaspace/metaspaceSettings.hpp"
 36 #include "memory/metaspace/metaspaceStatistics.hpp"
 37 #include "memory/metaspace/runningCounters.hpp"
 38 #include "memory/metaspaceTracer.hpp"


 39 #include "utilities/debug.hpp"

 40 
 41 using metaspace::ChunkManager;
 42 using metaspace::MetaspaceArena;
 43 using metaspace::ArenaGrowthPolicy;
 44 using metaspace::RunningCounters;
 45 using metaspace::InternalStats;
 46 
 47 #define LOGFMT         "CLMS @" PTR_FORMAT " "
 48 #define LOGFMT_ARGS    p2i(this)
 49 
 50 ClassLoaderMetaspace::ClassLoaderMetaspace(Mutex* lock, Metaspace::MetaspaceType space_type) :
 51   _lock(lock),
 52   _space_type(space_type),
 53   _non_class_space_arena(NULL),
 54   _class_space_arena(NULL)
 55 {
 56   ChunkManager* const non_class_cm =
 57           ChunkManager::chunkmanager_nonclass();
 58 


 59   // Initialize non-class Arena
 60   _non_class_space_arena = new MetaspaceArena(
 61       non_class_cm,
 62       ArenaGrowthPolicy::policy_for_space_type(space_type, false),

 63       lock,
 64       RunningCounters::used_nonclass_counter(),
 65       "non-class sm");
 66 
 67   // If needed, initialize class arena
 68   if (Metaspace::using_class_space()) {

 69     ChunkManager* const class_cm =
 70             ChunkManager::chunkmanager_class();
 71     _class_space_arena = new MetaspaceArena(
 72         class_cm,
 73         ArenaGrowthPolicy::policy_for_space_type(space_type, true),

 74         lock,
 75         RunningCounters::used_class_counter(),
 76         "class sm");





 77   }
 78 
 79   UL2(debug, "born (nonclass arena: " PTR_FORMAT ", class arena: " PTR_FORMAT ".",
 80       p2i(_non_class_space_arena), p2i(_class_space_arena));
 81 }
 82 
 83 ClassLoaderMetaspace::~ClassLoaderMetaspace() {
 84   UL(debug, "dies.");
 85 
 86   delete _non_class_space_arena;
 87   delete _class_space_arena;
 88 
 89 }
 90 
 91 // Allocate word_size words from Metaspace.
 92 MetaWord* ClassLoaderMetaspace::allocate(size_t word_size, Metaspace::MetadataType mdType) {
 93   if (Metaspace::is_class_space_allocation(mdType)) {
 94     return class_space_arena()->allocate(word_size);
 95   } else {
 96     return non_class_space_arena()->allocate(word_size);
 97   }
 98 }
 99 
100 // Attempt to expand the GC threshold to be good for at least another word_size words
101 // and allocate. Returns NULL if failure. Used during Metaspace GC.
102 MetaWord* ClassLoaderMetaspace::expand_and_allocate(size_t word_size, Metaspace::MetadataType mdType) {
103   size_t delta_bytes = MetaspaceGC::delta_capacity_until_GC(word_size * BytesPerWord);
104   assert(delta_bytes > 0, "Must be");
105 
106   size_t before = 0;
107   size_t after = 0;
108   bool can_retry = true;
109   MetaWord* res;
110   bool incremented;
111 
112   // Each thread increments the HWM at most once. Even if the thread fails to increment
113   // the HWM, an allocation is still attempted. This is because another thread must then
114   // have incremented the HWM and therefore the allocation might still succeed.
115   do {
116     incremented = MetaspaceGC::inc_capacity_until_GC(delta_bytes, &after, &before, &can_retry);
117     res = allocate(word_size, mdType);
118   } while (!incremented && res == NULL && can_retry);
119 
120   if (incremented) {
121     Metaspace::tracer()->report_gc_threshold(before, after,
122                                   MetaspaceGCThresholdUpdater::ExpandAndAllocate);
123     // Keeping both for now until I am sure the old variant (gc + metaspace) is not needed anymore
124     log_trace(gc, metaspace)("Increase capacity to GC from " SIZE_FORMAT " to " SIZE_FORMAT, before, after);
125     UL2(info, "GC threshold increased: " SIZE_FORMAT "->" SIZE_FORMAT ".", before, after);
126   }
127 
128   return res;
129 }
130 
131 // Prematurely returns a metaspace allocation to the _block_freelists
132 // because it is not needed anymore.
133 void ClassLoaderMetaspace::deallocate(MetaWord* ptr, size_t word_size, bool is_class) {
134   if (Metaspace::using_class_space() && is_class) {
135     class_space_arena()->deallocate(ptr, word_size);
136   } else {
137     non_class_space_arena()->deallocate(ptr, word_size);
138   }
139   DEBUG_ONLY(InternalStats::inc_num_deallocs();)
140 }
141 
142 // Update statistics. This walks all in-use chunks.
143 void ClassLoaderMetaspace::add_to_statistics(metaspace::ClmsStats* out) const {
144   if (non_class_space_arena() != NULL) {
145     non_class_space_arena()->add_to_statistics(&out->_arena_stats_nonclass);
146   }
147   if (class_space_arena() != NULL) {
148     class_space_arena()->add_to_statistics(&out->_arena_stats_class);
149   }
150 }
151 
152 #ifdef ASSERT
153 void ClassLoaderMetaspace::verify() const {
154   if (non_class_space_arena() != NULL) {
155     non_class_space_arena()->verify();
156   }
157   if (class_space_arena() != NULL) {
158     class_space_arena()->verify();
159   }
160 }
161 #endif // ASSERT
162 
163 // This only exists for JFR and jcmd VM.classloader_stats. We may want to
164 //  change this. Capacity as a stat is of questionable use since it may
165 //  contain committed and uncommitted areas. For now we do this to maintain
166 //  backward compatibility with JFR.
167 void ClassLoaderMetaspace::calculate_jfr_stats(size_t* p_used_bytes, size_t* p_capacity_bytes) const {
168   // Implement this using the standard statistics objects.
169   size_t used_c = 0, cap_c = 0, used_nc = 0, cap_nc = 0;
170   if (non_class_space_arena() != NULL) {
171     non_class_space_arena()->usage_numbers(&used_nc, NULL, &cap_nc);
172   }
173   if (class_space_arena() != NULL) {
174     class_space_arena()->usage_numbers(&used_c, NULL, &cap_c);
175   }
176   if (p_used_bytes != NULL) {
177     *p_used_bytes = used_c + used_nc;
178   }
179   if (p_capacity_bytes != NULL) {
180     *p_capacity_bytes = cap_c + cap_nc;
181   }
182 }
183 
--- EOF ---