comparison src/share/vm/gc_implementation/g1/g1CodeCacheRemSet.cpp @ 17753:191174b49bec

8035406: Improve data structure for Code Cache remembered sets Summary: Change the code cache remembered sets data structure from a GrowableArray to a chunked list of nmethods. This makes the data structure more amenable to parallelization, and decreases freeing time. Reviewed-by: mgerdin, brutisso
author tschatzl
date Mon, 24 Mar 2014 15:30:14 +0100
parents
children 78bbf4d43a14
comparison
equal deleted inserted replaced
17750:f53edbc2b728 17753:191174b49bec
1 /*
2 * Copyright (c) 2014, 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
25
26 #include "precompiled.hpp"
27 #include "code/nmethod.hpp"
28 #include "gc_implementation/g1/g1CodeCacheRemSet.hpp"
29 #include "memory/iterator.hpp"
30
31 G1CodeRootChunk::G1CodeRootChunk() : _top(NULL), _next(NULL), _prev(NULL) {
32 _top = bottom();
33 }
34
35 void G1CodeRootChunk::reset() {
36 _next = _prev = NULL;
37 _top = bottom();
38 }
39
40 void G1CodeRootChunk::nmethods_do(CodeBlobClosure* cl) {
41 nmethod** cur = bottom();
42 while (cur != _top) {
43 cl->do_code_blob(*cur);
44 cur++;
45 }
46 }
47
48 FreeList<G1CodeRootChunk> G1CodeRootSet::_free_list;
49 size_t G1CodeRootSet::_num_chunks_handed_out = 0;
50
51 G1CodeRootChunk* G1CodeRootSet::new_chunk() {
52 G1CodeRootChunk* result = _free_list.get_chunk_at_head();
53 if (result == NULL) {
54 result = new G1CodeRootChunk();
55 }
56 G1CodeRootSet::_num_chunks_handed_out++;
57 result->reset();
58 return result;
59 }
60
61 void G1CodeRootSet::free_chunk(G1CodeRootChunk* chunk) {
62 _free_list.return_chunk_at_head(chunk);
63 G1CodeRootSet::_num_chunks_handed_out--;
64 }
65
66 void G1CodeRootSet::free_all_chunks(FreeList<G1CodeRootChunk>* list) {
67 G1CodeRootSet::_num_chunks_handed_out -= list->count();
68 _free_list.prepend(list);
69 }
70
71 void G1CodeRootSet::purge_chunks(size_t keep_ratio) {
72 size_t keep = G1CodeRootSet::_num_chunks_handed_out * keep_ratio / 100;
73
74 if (keep >= (size_t)_free_list.count()) {
75 return;
76 }
77
78 FreeList<G1CodeRootChunk> temp;
79 temp.initialize();
80 temp.set_size(G1CodeRootChunk::word_size());
81
82 _free_list.getFirstNChunksFromList((size_t)_free_list.count() - keep, &temp);
83
84 G1CodeRootChunk* cur = temp.get_chunk_at_head();
85 while (cur != NULL) {
86 delete cur;
87 cur = temp.get_chunk_at_head();
88 }
89 }
90
91 size_t G1CodeRootSet::static_mem_size() {
92 return sizeof(_free_list) + sizeof(_num_chunks_handed_out);
93 }
94
95 size_t G1CodeRootSet::fl_mem_size() {
96 return _free_list.count() * _free_list.size();
97 }
98
99 void G1CodeRootSet::initialize() {
100 _free_list.initialize();
101 _free_list.set_size(G1CodeRootChunk::word_size());
102 }
103
104 G1CodeRootSet::G1CodeRootSet() : _list(), _length(0) {
105 _list.initialize();
106 _list.set_size(G1CodeRootChunk::word_size());
107 }
108
109 G1CodeRootSet::~G1CodeRootSet() {
110 clear();
111 }
112
113 void G1CodeRootSet::add(nmethod* method) {
114 if (!contains(method)) {
115 // Try to add the nmethod. If there is not enough space, get a new chunk.
116 if (_list.head() == NULL || _list.head()->is_full()) {
117 G1CodeRootChunk* cur = new_chunk();
118 _list.return_chunk_at_head(cur);
119 }
120 bool result = _list.head()->add(method);
121 guarantee(result, err_msg("Not able to add nmethod "PTR_FORMAT" to newly allocated chunk.", method));
122 _length++;
123 }
124 }
125
126 void G1CodeRootSet::remove(nmethod* method) {
127 G1CodeRootChunk* found = find(method);
128 if (found != NULL) {
129 bool result = found->remove(method);
130 guarantee(result, err_msg("could not find nmethod "PTR_FORMAT" during removal although we previously found it", method));
131 // eventually free completely emptied chunk
132 if (found->is_empty()) {
133 _list.remove_chunk(found);
134 free(found);
135 }
136 _length--;
137 }
138 assert(!contains(method), err_msg(PTR_FORMAT" still contains nmethod "PTR_FORMAT, this, method));
139 }
140
141 nmethod* G1CodeRootSet::pop() {
142 do {
143 G1CodeRootChunk* cur = _list.head();
144 if (cur == NULL) {
145 assert(_length == 0, "when there are no chunks, there should be no elements");
146 return NULL;
147 }
148 nmethod* result = cur->pop();
149 if (result != NULL) {
150 _length--;
151 return result;
152 } else {
153 free(_list.get_chunk_at_head());
154 }
155 } while (true);
156 }
157
158 G1CodeRootChunk* G1CodeRootSet::find(nmethod* method) {
159 G1CodeRootChunk* cur = _list.head();
160 while (cur != NULL) {
161 if (cur->contains(method)) {
162 return cur;
163 }
164 cur = (G1CodeRootChunk*)cur->next();
165 }
166 return NULL;
167 }
168
169 void G1CodeRootSet::free(G1CodeRootChunk* chunk) {
170 free_chunk(chunk);
171 }
172
173 bool G1CodeRootSet::contains(nmethod* method) {
174 return find(method) != NULL;
175 }
176
177 void G1CodeRootSet::clear() {
178 free_all_chunks(&_list);
179 _length = 0;
180 }
181
182 void G1CodeRootSet::nmethods_do(CodeBlobClosure* blk) const {
183 G1CodeRootChunk* cur = _list.head();
184 while (cur != NULL) {
185 cur->nmethods_do(blk);
186 cur = (G1CodeRootChunk*)cur->next();
187 }
188 }
189
190 size_t G1CodeRootSet::mem_size() {
191 return sizeof(this) + _list.count() * _list.size();
192 }
193
194 #ifndef PRODUCT
195
196 void G1CodeRootSet::test() {
197 initialize();
198
199 assert(_free_list.count() == 0, "Free List must be empty");
200 assert(_num_chunks_handed_out == 0, "No elements must have been handed out yet");
201
202 // The number of chunks that we allocate for purge testing.
203 size_t const num_chunks = 10;
204 {
205 G1CodeRootSet set1;
206 assert(set1.is_empty(), "Code root set must be initially empty but is not.");
207
208 set1.add((nmethod*)1);
209 assert(_num_chunks_handed_out == 1,
210 err_msg("Must have allocated and handed out one chunk, but handed out "
211 SIZE_FORMAT" chunks", _num_chunks_handed_out));
212 assert(set1.length() == 1, err_msg("Added exactly one element, but set contains "
213 SIZE_FORMAT" elements", set1.length()));
214
215 // G1CodeRootChunk::word_size() is larger than G1CodeRootChunk::num_entries which
216 // we cannot access.
217 for (uint i = 0; i < G1CodeRootChunk::word_size() + 1; i++) {
218 set1.add((nmethod*)1);
219 }
220 assert(_num_chunks_handed_out == 1,
221 err_msg("Duplicate detection must have prevented allocation of further "
222 "chunks but contains "SIZE_FORMAT, _num_chunks_handed_out));
223 assert(set1.length() == 1,
224 err_msg("Duplicate detection should not have increased the set size but "
225 "is "SIZE_FORMAT, set1.length()));
226
227 size_t num_total_after_add = G1CodeRootChunk::word_size() + 1;
228 for (size_t i = 0; i < num_total_after_add - 1; i++) {
229 set1.add((nmethod*)(2 + i));
230 }
231 assert(_num_chunks_handed_out > 1,
232 "After adding more code roots, more than one chunks should have been handed out");
233 assert(set1.length() == num_total_after_add,
234 err_msg("After adding in total "SIZE_FORMAT" distinct code roots, they "
235 "need to be in the set, but there are only "SIZE_FORMAT,
236 num_total_after_add, set1.length()));
237
238 size_t num_popped = 0;
239 while (set1.pop() != NULL) {
240 num_popped++;
241 }
242 assert(num_popped == num_total_after_add,
243 err_msg("Managed to pop "SIZE_FORMAT" code roots, but only "SIZE_FORMAT" "
244 "were added", num_popped, num_total_after_add));
245 assert(_num_chunks_handed_out == 0,
246 err_msg("After popping all elements, all chunks must have been returned "
247 "but are still "SIZE_FORMAT, _num_chunks_handed_out));
248
249 purge_chunks(0);
250 assert(_free_list.count() == 0,
251 err_msg("After purging everything, the free list must be empty but still "
252 "contains "SIZE_FORMAT" chunks", _free_list.count()));
253
254 // Add some more handed out chunks.
255 size_t i = 0;
256 while (_num_chunks_handed_out < num_chunks) {
257 set1.add((nmethod*)i);
258 i++;
259 }
260
261 {
262 // Generate chunks on the free list.
263 G1CodeRootSet set2;
264 size_t i = 0;
265 while (_num_chunks_handed_out < num_chunks * 2) {
266 set2.add((nmethod*)i);
267 i++;
268 }
269 // Exit of the scope of the set2 object will call the destructor that generates
270 // num_chunks elements on the free list.
271 }
272
273 assert(_num_chunks_handed_out == num_chunks,
274 err_msg("Deletion of the second set must have resulted in giving back "
275 "those, but there is still "SIZE_FORMAT" handed out, expecting "
276 SIZE_FORMAT, _num_chunks_handed_out, num_chunks));
277 assert((size_t)_free_list.count() == num_chunks,
278 err_msg("After freeing "SIZE_FORMAT" chunks, they must be on the free list "
279 "but there are only "SIZE_FORMAT, num_chunks, _free_list.count()));
280
281 size_t const test_percentage = 50;
282 purge_chunks(test_percentage);
283 assert(_num_chunks_handed_out == num_chunks,
284 err_msg("Purging must not hand out chunks but there are "SIZE_FORMAT,
285 _num_chunks_handed_out));
286 assert((size_t)_free_list.count() == (ssize_t)(num_chunks * test_percentage / 100),
287 err_msg("Must have purged "SIZE_FORMAT" percent of "SIZE_FORMAT" chunks"
288 "but there are "SSIZE_FORMAT, test_percentage, num_chunks,
289 _free_list.count()));
290 // Purge the remainder of the chunks on the free list.
291 purge_chunks(0);
292 assert(_free_list.count() == 0, "Free List must be empty");
293 assert(_num_chunks_handed_out == num_chunks,
294 err_msg("Expected to be "SIZE_FORMAT" chunks handed out from the first set "
295 "but there are "SIZE_FORMAT, num_chunks, _num_chunks_handed_out));
296
297 // Exit of the scope of the set1 object will call the destructor that generates
298 // num_chunks additional elements on the free list.
299 }
300
301 assert(_num_chunks_handed_out == 0,
302 err_msg("Deletion of the only set must have resulted in no chunks handed "
303 "out, but there is still "SIZE_FORMAT" handed out", _num_chunks_handed_out));
304 assert((size_t)_free_list.count() == num_chunks,
305 err_msg("After freeing "SIZE_FORMAT" chunks, they must be on the free list "
306 "but there are only "SSIZE_FORMAT, num_chunks, _free_list.count()));
307
308 // Restore initial state.
309 purge_chunks(0);
310 assert(_free_list.count() == 0, "Free List must be empty");
311 assert(_num_chunks_handed_out == 0, "No elements must have been handed out yet");
312 }
313
314 void TestCodeCacheRemSet_test() {
315 G1CodeRootSet::test();
316 }
317 #endif