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 MetaspaceArena;
 37 }
 38 
 39 // A ClassLoaderMetaspace manages MetaspaceArena(s) for a CLD.
 40 //
 41 // A CLD owns one MetaspaceArena if UseCompressedClassPointers is false. Otherwise
 42 // it owns two - one for the Klass* objects from the class space, one for the other
 43 // types of MetaspaceObjs from the non-class space.
 44 //
 45 // +------+       +----------------------+       +-------------------+
 46 // | CLD  | --->  | ClassLoaderMetaspace | ----> | (non class) Arena |
 47 // +------+       +----------------------+  |    +-------------------+     allocation top
 48 //                                          |       |                        v
 49 //                                          |       + chunk -- chunk ... -- chunk
 50 //                                          |
 51 //                                          |    +-------------------+
 52 //                                          +--> | (class) Arena     |
 53 //                                               +-------------------+
 54 //                                                  |
 55 //                                                  + chunk ... chunk
 56 //                                                               ^
 57 //                                                               alloc top
 58 //
 59 class ClassLoaderMetaspace : public CHeapObj<mtClass> {
 60 
 61   // A reference to an outside lock, held by the CLD.
 62   Mutex* const _lock;
 63 
 64   const Metaspace::MetaspaceType _space_type;
 65 
 66   // Arena for allocations from non-class  metaspace
 67   //  (resp. for all allocations if -XX:-UseCompressedClassPointers).
 68   metaspace::MetaspaceArena* _non_class_space_arena;
 69 
 70   // Arena for allocations from class space
 71   //  (null if -XX:-UseCompressedClassPointers).
 72   metaspace::MetaspaceArena* _class_space_arena;
 73 
 74   Mutex* lock() const                             { return _lock; }
 75   metaspace::MetaspaceArena* non_class_space_arena() const   { return _non_class_space_arena; }
 76   metaspace::MetaspaceArena* class_space_arena() const       { return _class_space_arena; }
 77 
 78 public:
 79 
 80   ClassLoaderMetaspace(Mutex* lock, Metaspace::MetaspaceType space_type);
 81 
 82   ~ClassLoaderMetaspace();
 83 
 84   Metaspace::MetaspaceType space_type() const { return _space_type; }
 85 
 86   // Allocate word_size words from Metaspace.
 87   MetaWord* allocate(size_t word_size, Metaspace::MetadataType mdType);
 88 
 89   // Attempt to expand the GC threshold to be good for at least another word_size words
 90   // and allocate. Returns null if failure. Used during Metaspace GC.
 91   MetaWord* expand_and_allocate(size_t word_size, Metaspace::MetadataType mdType);
 92 
 93   // Prematurely returns a metaspace allocation to the _block_freelists
 94   // because it is not needed anymore.
 95   void deallocate(MetaWord* ptr, size_t word_size, bool is_class);
 96 
 97   // Update statistics. This walks all in-use chunks.
 98   void add_to_statistics(metaspace::ClmsStats* out) const;
 99 
100   DEBUG_ONLY(void verify() const;)
101 
102   // Convenience method to get the most important usage statistics for either class
103   // or non-class space. For more detailed statistics, use add_to_statistics().
104   void usage_numbers(Metaspace::MetadataType mdType, size_t* p_used_words,
105                      size_t* p_committed_words, size_t* p_capacity_words) const;
106 
107   // Convenience method to get the most important usage statistics (totals; both class- and non-class spaces)
108   // For more detailed statistics, use add_to_statistics().
109   void usage_numbers(size_t* p_used_words, size_t* p_committed_words,
110                      size_t* p_capacity_words) const;
111 
112 }; // end: ClassLoaderMetaspace
113 
114 
115 #endif // SHARE_MEMORY_CLASSLOADERMETASPACE_HPP