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/metaspaceAlignment.hpp"
 32 #include "memory/metaspace/chunkManager.hpp"
 33 #include "memory/metaspace/internalStats.hpp"
 34 #include "memory/metaspace/metaspaceArena.hpp"
 35 #include "memory/metaspace/metaspaceArenaGrowthPolicy.hpp"
 36 #include "memory/metaspace/metaspaceSettings.hpp"
 37 #include "memory/metaspace/metaspaceStatistics.hpp"
 38 #include "memory/metaspace/runningCounters.hpp"
 39 #include "memory/metaspaceTracer.hpp"
 40 #include "oops/compressedKlass.hpp"
 41 #include "utilities/align.hpp"
 42 #include "utilities/debug.hpp"
 43 #include "utilities/globalDefinitions.hpp"
 44 
 45 using metaspace::ChunkManager;
 46 using metaspace::MetaspaceArena;
 47 using metaspace::ArenaGrowthPolicy;
 48 using metaspace::RunningCounters;
 49 using metaspace::InternalStats;
 50 
 51 #define LOGFMT         "CLMS @" PTR_FORMAT " "
 52 #define LOGFMT_ARGS    p2i(this)
 53 
 54 ClassLoaderMetaspace::ClassLoaderMetaspace(Mutex* lock, Metaspace::MetaspaceType space_type) :
 55   _lock(lock),
 56   _space_type(space_type),
 57   _non_class_space_arena(NULL),
 58   _class_space_arena(NULL)
 59 {
 60   ChunkManager* const non_class_cm =
 61           ChunkManager::chunkmanager_nonclass();
 62 
 63   const int klass_alignment_words = KlassAlignmentInBytes / BytesPerWord;
 64 
 65   // Initialize non-class Arena
 66   _non_class_space_arena = new MetaspaceArena(
 67       non_class_cm,
 68       ArenaGrowthPolicy::policy_for_space_type(space_type, false),
 69       metaspace::MetaspaceMinAlignmentWords,
 70       lock,
 71       RunningCounters::used_nonclass_counter(),
 72       "non-class sm");
 73 
 74   // If needed, initialize class arena
 75   if (Metaspace::using_class_space()) {
 76     // Klass instances live in class space and must be aligned correctly.
 77     ChunkManager* const class_cm =
 78             ChunkManager::chunkmanager_class();
 79     _class_space_arena = new MetaspaceArena(
 80         class_cm,
 81         ArenaGrowthPolicy::policy_for_space_type(space_type, true),
 82         klass_alignment_words,
 83         lock,
 84         RunningCounters::used_class_counter(),
 85         "class sm");
 86   } else {
 87     // note for lilliput, this path should be restricted to 32bit only. There, klass alignment
 88     //  should be compatible with metaspace minimal alignment since we store Klass structures
 89     //  in regular metaspace.
 90     NOT_LP64(STATIC_ASSERT(metaspace::MetaspaceMinAlignmentBytes == KlassAlignmentInBytes));
 91   }
 92 
 93   UL2(debug, "born (nonclass arena: " PTR_FORMAT ", class arena: " PTR_FORMAT ".",
 94       p2i(_non_class_space_arena), p2i(_class_space_arena));
 95 }
 96 
 97 ClassLoaderMetaspace::~ClassLoaderMetaspace() {
 98   UL(debug, "dies.");
 99 
100   delete _non_class_space_arena;
101   delete _class_space_arena;
102 
103 }
104 
105 // Allocate word_size words from Metaspace.
106 MetaWord* ClassLoaderMetaspace::allocate(size_t word_size, Metaspace::MetadataType mdType) {
107   if (Metaspace::is_class_space_allocation(mdType)) {
108     return class_space_arena()->allocate(word_size);
109   } else {
110     return non_class_space_arena()->allocate(word_size);
111   }
112 }
113 
114 // Attempt to expand the GC threshold to be good for at least another word_size words
115 // and allocate. Returns NULL if failure. Used during Metaspace GC.
116 MetaWord* ClassLoaderMetaspace::expand_and_allocate(size_t word_size, Metaspace::MetadataType mdType) {
117   size_t delta_bytes = MetaspaceGC::delta_capacity_until_GC(word_size * BytesPerWord);
118   assert(delta_bytes > 0, "Must be");
119 
120   size_t before = 0;
121   size_t after = 0;
122   bool can_retry = true;
123   MetaWord* res;
124   bool incremented;
125 
126   // Each thread increments the HWM at most once. Even if the thread fails to increment
127   // the HWM, an allocation is still attempted. This is because another thread must then
128   // have incremented the HWM and therefore the allocation might still succeed.
129   do {
130     incremented = MetaspaceGC::inc_capacity_until_GC(delta_bytes, &after, &before, &can_retry);
131     res = allocate(word_size, mdType);
132   } while (!incremented && res == NULL && can_retry);
133 
134   if (incremented) {
135     Metaspace::tracer()->report_gc_threshold(before, after,
136                                   MetaspaceGCThresholdUpdater::ExpandAndAllocate);
137     // Keeping both for now until I am sure the old variant (gc + metaspace) is not needed anymore
138     log_trace(gc, metaspace)("Increase capacity to GC from " SIZE_FORMAT " to " SIZE_FORMAT, before, after);
139     UL2(info, "GC threshold increased: " SIZE_FORMAT "->" SIZE_FORMAT ".", before, after);
140   }
141 
142   return res;
143 }
144 
145 // Prematurely returns a metaspace allocation to the _block_freelists
146 // because it is not needed anymore.
147 void ClassLoaderMetaspace::deallocate(MetaWord* ptr, size_t word_size, bool is_class) {
148   if (Metaspace::using_class_space() && is_class) {
149     class_space_arena()->deallocate(ptr, word_size);
150   } else {
151     non_class_space_arena()->deallocate(ptr, word_size);
152   }
153   DEBUG_ONLY(InternalStats::inc_num_deallocs();)
154 }
155 
156 // Update statistics. This walks all in-use chunks.
157 void ClassLoaderMetaspace::add_to_statistics(metaspace::ClmsStats* out) const {
158   if (non_class_space_arena() != NULL) {
159     non_class_space_arena()->add_to_statistics(&out->_arena_stats_nonclass);
160   }
161   if (class_space_arena() != NULL) {
162     class_space_arena()->add_to_statistics(&out->_arena_stats_class);
163   }
164 }
165 
166 #ifdef ASSERT
167 void ClassLoaderMetaspace::verify() const {
168   if (non_class_space_arena() != NULL) {
169     non_class_space_arena()->verify();
170   }
171   if (class_space_arena() != NULL) {
172     class_space_arena()->verify();
173   }
174 }
175 #endif // ASSERT
176 
177 // This only exists for JFR and jcmd VM.classloader_stats. We may want to
178 //  change this. Capacity as a stat is of questionable use since it may
179 //  contain committed and uncommitted areas. For now we do this to maintain
180 //  backward compatibility with JFR.
181 void ClassLoaderMetaspace::calculate_jfr_stats(size_t* p_used_bytes, size_t* p_capacity_bytes) const {
182   // Implement this using the standard statistics objects.
183   size_t used_c = 0, cap_c = 0, used_nc = 0, cap_nc = 0;
184   if (non_class_space_arena() != NULL) {
185     non_class_space_arena()->usage_numbers(&used_nc, NULL, &cap_nc);
186   }
187   if (class_space_arena() != NULL) {
188     class_space_arena()->usage_numbers(&used_c, NULL, &cap_c);
189   }
190   if (p_used_bytes != NULL) {
191     *p_used_bytes = used_c + used_nc;
192   }
193   if (p_capacity_bytes != NULL) {
194     *p_capacity_bytes = cap_c + cap_nc;
195   }
196 }
197 
--- EOF ---