Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/hotspot/share/memory/metaspace/freeChunkList.hpp
40957 views
1
/*
2
* Copyright (c) 2020, 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
#ifndef SHARE_MEMORY_METASPACE_FREECHUNKLIST_HPP
27
#define SHARE_MEMORY_METASPACE_FREECHUNKLIST_HPP
28
29
#include "memory/allocation.hpp"
30
#include "memory/metaspace/chunklevel.hpp"
31
#include "memory/metaspace/counters.hpp"
32
#include "memory/metaspace/metachunk.hpp"
33
#include "memory/metaspace/metachunkList.hpp"
34
35
class outputStream;
36
37
namespace metaspace {
38
39
// This is the free list underlying the ChunkManager.
40
//
41
// Chunks are kept in a vector of double-linked double-headed lists
42
// (using Metachunk::prev/next). One list per chunk level exists.
43
//
44
// Chunks in these lists are roughly ordered: uncommitted chunks
45
// are added to the back of the list, fully or partially committed
46
// chunks to the front. We do not use a more elaborate sorting on
47
// insert since that path is used during class unloading, hence timing
48
// sensitive.
49
//
50
// During retrieval (at class loading), we search the list for a chunk
51
// of at least n committed words to satisfy the caller requested
52
// committed word size. We stop searching at the first fully uncommitted
53
// chunk.
54
//
55
// Note that even though this is an O(n) search, partially committed chunks are
56
// very rare. A partially committed chunk is one spanning multiple commit
57
// granules, of which some are committed and some are not.
58
// If metaspace reclamation is on (MetaspaceReclaimPolicy=balanced|aggressive), these
59
// chunks will become uncommitted after they are returned to the ChunkManager.
60
// If metaspace reclamation is off (MetaspaceReclaimPolicy=none) they are fully
61
// committed when handed out and will not be uncommitted when returned to the
62
// ChunkManager.
63
//
64
// Therefore in all likelihood the chunk lists only contain fully committed or
65
// fully uncommitted chunks; either way search will stop at the first chunk.
66
67
class FreeChunkList {
68
69
Metachunk* _first;
70
Metachunk* _last;
71
72
IntCounter _num_chunks;
73
74
void add_front(Metachunk* c) {
75
if (_first == NULL) {
76
assert(_last == NULL, "Sanity");
77
_first = _last = c;
78
c->set_prev(NULL);
79
c->set_next(NULL);
80
} else {
81
assert(_last != NULL, "Sanity");
82
c->set_next(_first);
83
c->set_prev(NULL);
84
_first->set_prev(c);
85
_first = c;
86
}
87
}
88
89
// Add chunk to the back of the list.
90
void add_back(Metachunk* c) {
91
if (_last == NULL) {
92
assert(_first == NULL, "Sanity");
93
_last = _first = c;
94
c->set_prev(NULL);
95
c->set_next(NULL);
96
} else {
97
assert(_first != NULL, "Sanity");
98
c->set_next(NULL);
99
c->set_prev(_last);
100
_last->set_next(c);
101
_last = c;
102
}
103
}
104
105
public:
106
107
FreeChunkList() :
108
_first(NULL),
109
_last(NULL)
110
{}
111
112
// Remove given chunk from anywhere in the list.
113
Metachunk* remove(Metachunk* c) {
114
assert(contains(c), "Must be contained here");
115
Metachunk* pred = c->prev();
116
Metachunk* succ = c->next();
117
if (pred) {
118
pred->set_next(succ);
119
}
120
if (succ) {
121
succ->set_prev(pred);
122
}
123
if (_first == c) {
124
_first = succ;
125
}
126
if (_last == c) {
127
_last = pred;
128
}
129
c->set_next(NULL);
130
c->set_prev(NULL);
131
_num_chunks.decrement();
132
return c;
133
}
134
135
void add(Metachunk* c) {
136
assert(contains(c) == false, "Chunk already in freelist");
137
assert(_first == NULL || _first->level() == c->level(),
138
"List should only contains chunks of the same level.");
139
// Uncomitted chunks go to the back, fully or partially committed to the front.
140
if (c->committed_words() == 0) {
141
add_back(c);
142
} else {
143
add_front(c);
144
}
145
_num_chunks.increment();
146
}
147
148
// Removes the first chunk from the list and returns it. Returns NULL if list is empty.
149
Metachunk* remove_first() {
150
Metachunk* c = _first;
151
if (c != NULL) {
152
remove(c);
153
}
154
return c;
155
}
156
157
// Returns reference to the first chunk in the list, or NULL
158
Metachunk* first() const { return _first; }
159
160
// Returns reference to the fist chunk in the list with a committed word
161
// level >= min_committed_words, or NULL.
162
Metachunk* first_minimally_committed(size_t min_committed_words) const {
163
// Since uncommitted chunks are added to the back we can stop looking once
164
// we encounter a fully uncommitted chunk.
165
Metachunk* c = first();
166
while (c != NULL &&
167
c->committed_words() < min_committed_words &&
168
c->committed_words() > 0) {
169
c = c->next();
170
}
171
if (c != NULL &&
172
c->committed_words() >= min_committed_words) {
173
return c;
174
}
175
return NULL;
176
}
177
178
#ifdef ASSERT
179
bool contains(const Metachunk* c) const;
180
void verify() const;
181
#endif
182
183
// Returns number of chunks
184
int num_chunks() const { return _num_chunks.get(); }
185
186
// Calculates total number of committed words over all chunks (walks chunks).
187
size_t calc_committed_word_size() const;
188
189
void print_on(outputStream* st) const;
190
191
};
192
193
// A vector of free chunk lists, one per chunk level
194
class FreeChunkListVector {
195
196
FreeChunkList _lists[chunklevel::NUM_CHUNK_LEVELS];
197
198
const FreeChunkList* list_for_level(chunklevel_t lvl) const { DEBUG_ONLY(chunklevel::check_valid_level(lvl)); return _lists + lvl; }
199
FreeChunkList* list_for_level(chunklevel_t lvl) { DEBUG_ONLY(chunklevel::check_valid_level(lvl)); return _lists + lvl; }
200
201
const FreeChunkList* list_for_chunk(const Metachunk* c) const { return list_for_level(c->level()); }
202
FreeChunkList* list_for_chunk(const Metachunk* c) { return list_for_level(c->level()); }
203
204
public:
205
206
// Remove given chunk from its list. List must contain that chunk.
207
void remove(Metachunk* c) {
208
list_for_chunk(c)->remove(c);
209
}
210
211
// Remove first node unless empty. Returns node or NULL.
212
Metachunk* remove_first(chunklevel_t lvl) {
213
Metachunk* c = list_for_level(lvl)->remove_first();
214
return c;
215
}
216
217
void add(Metachunk* c) {
218
list_for_chunk(c)->add(c);
219
}
220
221
// Returns number of chunks for a given level.
222
int num_chunks_at_level(chunklevel_t lvl) const {
223
return list_for_level(lvl)->num_chunks();
224
}
225
226
// Returns reference to first chunk at this level, or NULL if sublist is empty.
227
Metachunk* first_at_level(chunklevel_t lvl) const {
228
return list_for_level(lvl)->first();
229
}
230
231
// Look for a chunk: starting at level, up to and including max_level,
232
// return the first chunk whose committed words >= min_committed_words.
233
// Return NULL if no such chunk was found.
234
Metachunk* search_chunk_ascending(chunklevel_t level, chunklevel_t max_level,
235
size_t min_committed_words);
236
237
// Look for a chunk: starting at level, down to (including) the root chunk level,
238
// return the first chunk whose committed words >= min_committed_words.
239
// Return NULL if no such chunk was found.
240
Metachunk* search_chunk_descending(chunklevel_t level, size_t min_committed_words);
241
242
// Returns total size in all lists (including uncommitted areas)
243
size_t word_size() const;
244
245
// Calculates total number of committed words over all chunks (walks chunks).
246
size_t calc_committed_word_size_at_level(chunklevel_t lvl) const;
247
248
// Calculates total number of committed words over all chunks (walks chunks).
249
size_t calc_committed_word_size() const;
250
251
// Returns number of chunks in all lists
252
int num_chunks() const;
253
254
#ifdef ASSERT
255
bool contains(const Metachunk* c) const;
256
void verify() const;
257
#endif
258
259
void print_on(outputStream* st) const;
260
261
};
262
263
} // namespace metaspace
264
265
#endif // SHARE_MEMORY_METASPACE_FREECHUNKLIST_HPP
266
267