24 |
24 |
25 #ifndef SHARE_MEMORY_METASPACE_CHUNKMANAGER_HPP |
25 #ifndef SHARE_MEMORY_METASPACE_CHUNKMANAGER_HPP |
26 #define SHARE_MEMORY_METASPACE_CHUNKMANAGER_HPP |
26 #define SHARE_MEMORY_METASPACE_CHUNKMANAGER_HPP |
27 |
27 |
28 #include "memory/allocation.hpp" |
28 #include "memory/allocation.hpp" |
29 #include "memory/binaryTreeDictionary.hpp" |
29 #include "memory/metaspace/chunkLevel.hpp" |
30 #include "memory/freeList.hpp" |
|
31 #include "memory/metaspace/metachunk.hpp" |
|
32 #include "memory/metaspace/metaspaceStatistics.hpp" |
|
33 #include "memory/metaspaceChunkFreeListSummary.hpp" |
|
34 #include "utilities/globalDefinitions.hpp" |
|
35 |
|
36 class ChunkManagerTestAccessor; |
|
37 |
30 |
38 namespace metaspace { |
31 namespace metaspace { |
39 |
32 |
40 typedef class FreeList<Metachunk> ChunkList; |
33 class VirtualSpaceList; |
41 typedef BinaryTreeDictionary<Metachunk, FreeList<Metachunk> > ChunkTreeDictionary; |
34 class Metachunk; |
42 |
35 |
43 // Manages the global free lists of chunks. |
36 |
|
37 // class ChunkManager |
|
38 // |
|
39 // The ChunkManager has a central role. Callers request chunks from it. |
|
40 // It keeps the freelists for chunks. If the freelist is exhausted it |
|
41 // allocates new chunks from a connected VirtualSpaceList. |
|
42 // |
44 class ChunkManager : public CHeapObj<mtInternal> { |
43 class ChunkManager : public CHeapObj<mtInternal> { |
45 friend class ::ChunkManagerTestAccessor; |
|
46 |
44 |
47 // Free list of chunks of different sizes. |
45 // A chunk manager is connected to a virtual space list which is used |
48 // SpecializedChunk |
46 // to allocate new root chunks when no free chunks are found. |
49 // SmallChunk |
47 VirtualSpaceList* const _vs_list; |
50 // MediumChunk |
|
51 ChunkList _free_chunks[NumberOfFreeLists]; |
|
52 |
48 |
53 // Whether or not this is the class chunkmanager. |
49 // Name |
54 const bool _is_class; |
50 const char* const _name; |
55 |
51 |
56 // Return non-humongous chunk list by its index. |
52 // Freelist |
57 ChunkList* free_chunks(ChunkIndex index); |
53 Metachunk* _chunks [NUM_CHUNK_LEVELS]; |
58 |
54 |
59 // Returns non-humongous chunk list for the given chunk word size. |
55 // Statistics. |
60 ChunkList* find_free_chunks_list(size_t word_size); |
56 // Number of counters per level. |
|
57 int _num_chunks [NUM_CHUNK_LEVELS]; |
61 |
58 |
62 // HumongousChunk |
59 // Total size in all chunks, and total number of chunks. |
63 ChunkTreeDictionary _humongous_dictionary; |
60 size_t _total_word_size; |
|
61 int _total_num_chunks; |
64 |
62 |
65 // Returns the humongous chunk dictionary. |
63 // Remove a chunk of the given level from its freelist, and adjust accounting. |
66 ChunkTreeDictionary* humongous_dictionary() { return &_humongous_dictionary; } |
64 // If no chunk of this given level is free, return NULL. |
67 const ChunkTreeDictionary* humongous_dictionary() const { return &_humongous_dictionary; } |
65 Metachunk* get_chunk_simple(chklvl_t level); |
68 |
66 |
69 // Size, in metaspace words, of all chunks managed by this ChunkManager |
67 // Update counters after a chunk has been added or removed. |
70 size_t _free_chunks_total; |
|
71 // Number of chunks in this ChunkManager |
|
72 size_t _free_chunks_count; |
|
73 |
|
74 // Update counters after a chunk was added or removed removed. |
|
75 void account_for_added_chunk(const Metachunk* c); |
68 void account_for_added_chunk(const Metachunk* c); |
76 void account_for_removed_chunk(const Metachunk* c); |
69 void account_for_removed_chunk(const Metachunk* c); |
77 |
70 |
78 // Given a pointer to a chunk, attempts to merge it with neighboring |
71 // Returns true if this manager contains the given chunk. Slow (walks free list) and |
79 // free chunks to form a bigger chunk. Returns true if successful. |
72 // only needed for verifications. |
80 bool attempt_to_coalesce_around_chunk(Metachunk* chunk, ChunkIndex target_chunk_type); |
73 DEBUG_ONLY(bool contains_chunk(Metachunk* metachunk) const;) |
81 |
74 |
82 // Helper for chunk merging: |
75 public: |
83 // Given an address range with 1-n chunks which are all supposed to be |
|
84 // free and hence currently managed by this ChunkManager, remove them |
|
85 // from this ChunkManager and mark them as invalid. |
|
86 // - This does not correct the occupancy map. |
|
87 // - This does not adjust the counters in ChunkManager. |
|
88 // - Does not adjust container count counter in containing VirtualSpaceNode. |
|
89 // Returns number of chunks removed. |
|
90 int remove_chunks_in_area(MetaWord* p, size_t word_size); |
|
91 |
76 |
92 // Helper for chunk splitting: given a target chunk size and a larger free chunk, |
77 // Creates a chunk manager with a given name (which is for debug purposes only) |
93 // split up the larger chunk into n smaller chunks, at least one of which should be |
78 // and an associated space list which will be used to request new chunks from |
94 // the target chunk of target chunk size. The smaller chunks, including the target |
79 // (see get_chunk()) |
95 // chunk, are returned to the freelist. The pointer to the target chunk is returned. |
80 ChunkManager(const char* name, VirtualSpaceList* space_list); |
96 // Note that this chunk is supposed to be removed from the freelist right away. |
|
97 Metachunk* split_chunk(size_t target_chunk_word_size, Metachunk* chunk); |
|
98 |
81 |
99 public: |
82 // Get a chunk and be smart about it. |
|
83 // - Attempt to find a free chunk of exactly the pref_level level |
|
84 // - Failing that, attempt to find a chunk smaller or equal the minimal level. |
|
85 // - Failing that, attempt to find a free chunk of larger size and split it. |
|
86 // - Failing that, attempt to allocate a new chunk from the connected virtual space. |
|
87 // This may fail if we hit GC threshold or metaspace limit. |
|
88 // - Failing that, give up and return NULL. |
|
89 Metachunk* get_chunk(chklvl_t min_level, chklvl_t pref_level); |
100 |
90 |
101 ChunkManager(bool is_class); |
91 // Remove the given chunk from its free list and adjust accounting. |
102 |
92 // (Called during VirtualSpaceNode purging which happens during a Metaspace GC.) |
103 // Add or delete (return) a chunk to the global freelist. |
|
104 Metachunk* chunk_freelist_allocate(size_t word_size); |
|
105 |
|
106 // Map a size to a list index assuming that there are lists |
|
107 // for special, small, medium, and humongous chunks. |
|
108 ChunkIndex list_index(size_t size); |
|
109 |
|
110 // Map a given index to the chunk size. |
|
111 size_t size_by_index(ChunkIndex index) const; |
|
112 |
|
113 bool is_class() const { return _is_class; } |
|
114 |
|
115 // Convenience accessors. |
|
116 size_t medium_chunk_word_size() const { return size_by_index(MediumIndex); } |
|
117 size_t small_chunk_word_size() const { return size_by_index(SmallIndex); } |
|
118 size_t specialized_chunk_word_size() const { return size_by_index(SpecializedIndex); } |
|
119 |
|
120 // Take a chunk from the ChunkManager. The chunk is expected to be in |
|
121 // the chunk manager (the freelist if non-humongous, the dictionary if |
|
122 // humongous). |
|
123 void remove_chunk(Metachunk* chunk); |
93 void remove_chunk(Metachunk* chunk); |
124 |
94 |
125 // Return a single chunk of type index to the ChunkManager. |
95 // Return a chunk to the ChunkManager and adjust accounting. May merge chunk |
126 void return_single_chunk(Metachunk* chunk); |
96 // with neighbors. |
|
97 // Happens after a Classloader was unloaded and releases its metaspace chunks. |
|
98 // !! Note: this may invalidate the chunk. Do not access the chunk after |
|
99 // this function returns !! |
|
100 void return_chunk(Metachunk* chunk); |
127 |
101 |
128 // Add the simple linked list of chunks to the freelist of chunks |
102 // Uncommit payload area of free chunks. Will be called during Metaspace GC. |
129 // of type index. |
103 void uncommit_free_chunks(); |
130 void return_chunk_list(Metachunk* chunk); |
|
131 |
104 |
132 // Total of the space in the free chunks list |
105 // Run verifications. slow=true: verify chunk-internal integrity too. |
133 size_t free_chunks_total_words() const { return _free_chunks_total; } |
106 DEBUG_ONLY(void verify(bool slow) const;) |
134 size_t free_chunks_total_bytes() const { return free_chunks_total_words() * BytesPerWord; } |
107 DEBUG_ONLY(void locked_verify(bool slow) const;) |
135 |
108 |
136 // Number of chunks in the free chunks list |
109 // Returns the name of this chunk manager. |
137 size_t free_chunks_count() const { return _free_chunks_count; } |
110 const char* name() const { return _name; } |
138 |
|
139 // Remove from a list by size. Selects list based on size of chunk. |
|
140 Metachunk* free_chunks_get(size_t chunk_word_size); |
|
141 |
|
142 #define index_bounds_check(index) \ |
|
143 assert(is_valid_chunktype(index), "Bad index: %d", (int) index) |
|
144 |
|
145 size_t num_free_chunks(ChunkIndex index) const { |
|
146 index_bounds_check(index); |
|
147 |
|
148 if (index == HumongousIndex) { |
|
149 return _humongous_dictionary.total_free_blocks(); |
|
150 } |
|
151 |
|
152 ssize_t count = _free_chunks[index].count(); |
|
153 return count == -1 ? 0 : (size_t) count; |
|
154 } |
|
155 |
|
156 size_t size_free_chunks_in_bytes(ChunkIndex index) const { |
|
157 index_bounds_check(index); |
|
158 |
|
159 size_t word_size = 0; |
|
160 if (index == HumongousIndex) { |
|
161 word_size = _humongous_dictionary.total_size(); |
|
162 } else { |
|
163 const size_t size_per_chunk_in_words = _free_chunks[index].size(); |
|
164 word_size = size_per_chunk_in_words * num_free_chunks(index); |
|
165 } |
|
166 |
|
167 return word_size * BytesPerWord; |
|
168 } |
|
169 |
|
170 MetaspaceChunkFreeListSummary chunk_free_list_summary() const { |
|
171 return MetaspaceChunkFreeListSummary(num_free_chunks(SpecializedIndex), |
|
172 num_free_chunks(SmallIndex), |
|
173 num_free_chunks(MediumIndex), |
|
174 num_free_chunks(HumongousIndex), |
|
175 size_free_chunks_in_bytes(SpecializedIndex), |
|
176 size_free_chunks_in_bytes(SmallIndex), |
|
177 size_free_chunks_in_bytes(MediumIndex), |
|
178 size_free_chunks_in_bytes(HumongousIndex)); |
|
179 } |
|
180 |
|
181 #ifdef ASSERT |
|
182 // Debug support |
|
183 // Verify free list integrity. slow=true: verify chunk-internal integrity too. |
|
184 void verify(bool slow) const; |
|
185 void locked_verify(bool slow) const; |
|
186 #endif |
|
187 |
|
188 void locked_print_free_chunks(outputStream* st); |
|
189 |
|
190 // Fill in current statistic values to the given statistics object. |
|
191 void collect_statistics(ChunkManagerStatistics* out) const; |
|
192 |
111 |
193 }; |
112 }; |
194 |
113 |
195 } // namespace metaspace |
114 } // namespace metaspace |
196 |
115 |
197 |
|
198 #endif // SHARE_MEMORY_METASPACE_CHUNKMANAGER_HPP |
116 #endif // SHARE_MEMORY_METASPACE_CHUNKMANAGER_HPP |