1 /*
  2  * Copyright (c) 2011, 2023, Oracle and/or its affiliates. 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 it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 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 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  *
 23  */
 24 #ifndef SHARE_MEMORY_CLASSLOADERMETASPACE_HPP
 25 #define SHARE_MEMORY_CLASSLOADERMETASPACE_HPP
 26 
 27 #include "memory/allocation.hpp"
 28 #include "memory/metaspace.hpp"
 29 #include "utilities/debug.hpp"
 30 #include "utilities/globalDefinitions.hpp"
 31 
 32 class outputStream;
 33 
 34 namespace metaspace {
 35   struct ClmsStats;
 36   class ClmsTester;
 37   class MetaspaceArena;
 38   class MetaspaceContext;
 39 }
 40 
 41 // A ClassLoaderMetaspace manages MetaspaceArena(s) for a CLD.
 42 //
 43 // A CLD owns one MetaspaceArena if UseCompressedClassPointers is false. Otherwise
 44 // it owns two - one for the Klass* objects from the class space, one for the other
 45 // types of MetaspaceObjs from the non-class space.
 46 //
 47 // +------+       +----------------------+       +-------------------+
 48 // | CLD  | --->  | ClassLoaderMetaspace | ----> | (non class) Arena |
 49 // +------+       +----------------------+  |    +-------------------+     allocation top
 50 //                                          |       |                        v
 51 //                                          |       + chunk -- chunk ... -- chunk
 52 //                                          |
 53 //                                          |    +-------------------+
 54 //                                          +--> | (class) Arena     |
 55 //                                               +-------------------+
 56 //                                                  |
 57 //                                                  + chunk ... chunk
 58 //                                                               ^
 59 //                                                               alloc top
 60 //
 61 class ClassLoaderMetaspace : public CHeapObj<mtClass> {
 62   friend class metaspace::ClmsTester; // for gtests
 63 
 64   // A reference to an outside lock, held by the CLD.
 65   Mutex* const _lock;
 66 
 67   const Metaspace::MetaspaceType _space_type;
 68 
 69   // Arena for allocations from non-class  metaspace
 70   //  (resp. for all allocations if -XX:-UseCompressedClassPointers).
 71   metaspace::MetaspaceArena* _non_class_space_arena;
 72 
 73   // Arena for allocations from class space
 74   //  (null if -XX:-UseCompressedClassPointers).
 75   metaspace::MetaspaceArena* _class_space_arena;
 76 
 77   Mutex* lock() const                             { return _lock; }
 78   metaspace::MetaspaceArena* non_class_space_arena() const   { return _non_class_space_arena; }
 79   metaspace::MetaspaceArena* class_space_arena() const       { return _class_space_arena; }
 80 
 81   bool have_class_space_arena() const { return _class_space_arena != nullptr; }
 82 
 83   ClassLoaderMetaspace(Mutex* lock, Metaspace::MetaspaceType space_type,
 84                        metaspace::MetaspaceContext* non_class_context,
 85                        metaspace::MetaspaceContext* class_context,
 86                        size_t klass_alignment_words);
 87 
 88 public:
 89   ClassLoaderMetaspace(Mutex* lock, Metaspace::MetaspaceType space_type);
 90 
 91   ~ClassLoaderMetaspace();
 92 
 93   Metaspace::MetaspaceType space_type() const { return _space_type; }
 94 
 95   // Allocate word_size words from Metaspace.
 96   MetaWord* allocate(size_t word_size, Metaspace::MetadataType mdType);
 97 
 98   // Attempt to expand the GC threshold to be good for at least another word_size words
 99   // and allocate. Returns null if failure. Used during Metaspace GC.
100   MetaWord* expand_and_allocate(size_t word_size, Metaspace::MetadataType mdType);
101 
102   // Prematurely returns a metaspace allocation to the _block_freelists
103   // because it is not needed anymore.
104   void deallocate(MetaWord* ptr, size_t word_size, bool is_class);
105 
106   // Update statistics. This walks all in-use chunks.
107   void add_to_statistics(metaspace::ClmsStats* out) const;
108 
109   DEBUG_ONLY(void verify() const;)
110 
111   // Convenience method to get the most important usage statistics for either class
112   // or non-class space. For more detailed statistics, use add_to_statistics().
113   void usage_numbers(Metaspace::MetadataType mdType, size_t* p_used_words,
114                      size_t* p_committed_words, size_t* p_capacity_words) const;
115 
116   // Convenience method to get the most important usage statistics (totals; both class- and non-class spaces)
117   // For more detailed statistics, use add_to_statistics().
118   void usage_numbers(size_t* p_used_words, size_t* p_committed_words,
119                      size_t* p_capacity_words) const;
120 
121 }; // end: ClassLoaderMetaspace
122 
123 
124 #endif // SHARE_MEMORY_CLASSLOADERMETASPACE_HPP