author | coleenp |
Thu, 25 Apr 2019 10:56:31 -0400 | |
changeset 54623 | 1126f0607c70 |
parent 54486 | 7fd299216e97 |
child 54786 | ebf733a324d4 |
permissions | -rw-r--r-- |
50574 | 1 |
/* |
54623
1126f0607c70
8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents:
54486
diff
changeset
|
2 |
* Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved. |
50574 | 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 |
#include "precompiled.hpp" |
|
26 |
#include "classfile/altHashing.hpp" |
|
27 |
#include "classfile/javaClasses.inline.hpp" |
|
28 |
#include "gc/shared/stringdedup/stringDedup.hpp" |
|
29 |
#include "gc/shared/stringdedup/stringDedupTable.hpp" |
|
30 |
#include "gc/shared/suspendibleThreadSet.hpp" |
|
31 |
#include "logging/log.hpp" |
|
32 |
#include "memory/padded.inline.hpp" |
|
33 |
#include "oops/access.inline.hpp" |
|
34 |
#include "oops/arrayOop.inline.hpp" |
|
35 |
#include "oops/oop.inline.hpp" |
|
36 |
#include "oops/typeArrayOop.hpp" |
|
37 |
#include "runtime/mutexLocker.hpp" |
|
38 |
#include "runtime/safepointVerifiers.hpp" |
|
39 |
||
40 |
// |
|
41 |
// List of deduplication table entries. Links table |
|
42 |
// entries together using their _next fields. |
|
43 |
// |
|
44 |
class StringDedupEntryList : public CHeapObj<mtGC> { |
|
45 |
private: |
|
46 |
StringDedupEntry* _list; |
|
47 |
size_t _length; |
|
48 |
||
49 |
public: |
|
50 |
StringDedupEntryList() : |
|
51 |
_list(NULL), |
|
52 |
_length(0) { |
|
53 |
} |
|
54 |
||
55 |
void add(StringDedupEntry* entry) { |
|
56 |
entry->set_next(_list); |
|
57 |
_list = entry; |
|
58 |
_length++; |
|
59 |
} |
|
60 |
||
61 |
StringDedupEntry* remove() { |
|
62 |
StringDedupEntry* entry = _list; |
|
63 |
if (entry != NULL) { |
|
64 |
_list = entry->next(); |
|
65 |
_length--; |
|
66 |
} |
|
67 |
return entry; |
|
68 |
} |
|
69 |
||
70 |
StringDedupEntry* remove_all() { |
|
71 |
StringDedupEntry* list = _list; |
|
72 |
_list = NULL; |
|
73 |
return list; |
|
74 |
} |
|
75 |
||
76 |
size_t length() { |
|
77 |
return _length; |
|
78 |
} |
|
79 |
}; |
|
80 |
||
81 |
// |
|
82 |
// Cache of deduplication table entries. This cache provides fast allocation and |
|
83 |
// reuse of table entries to lower the pressure on the underlying allocator. |
|
84 |
// But more importantly, it provides fast/deferred freeing of table entries. This |
|
85 |
// is important because freeing of table entries is done during stop-the-world |
|
86 |
// phases and it is not uncommon for large number of entries to be freed at once. |
|
87 |
// Tables entries that are freed during these phases are placed onto a freelist in |
|
88 |
// the cache. The deduplication thread, which executes in a concurrent phase, will |
|
89 |
// later reuse or free the underlying memory for these entries. |
|
90 |
// |
|
91 |
// The cache allows for single-threaded allocations and multi-threaded frees. |
|
92 |
// Allocations are synchronized by StringDedupTable_lock as part of a table |
|
93 |
// modification. |
|
94 |
// |
|
95 |
class StringDedupEntryCache : public CHeapObj<mtGC> { |
|
96 |
private: |
|
97 |
// One cache/overflow list per GC worker to allow lock less freeing of |
|
98 |
// entries while doing a parallel scan of the table. Using PaddedEnd to |
|
99 |
// avoid false sharing. |
|
100 |
size_t _nlists; |
|
101 |
size_t _max_list_length; |
|
102 |
PaddedEnd<StringDedupEntryList>* _cached; |
|
103 |
PaddedEnd<StringDedupEntryList>* _overflowed; |
|
104 |
||
105 |
public: |
|
106 |
StringDedupEntryCache(size_t max_size); |
|
107 |
~StringDedupEntryCache(); |
|
108 |
||
109 |
// Set max number of table entries to cache. |
|
110 |
void set_max_size(size_t max_size); |
|
111 |
||
112 |
// Get a table entry from the cache, or allocate a new entry if the cache is empty. |
|
113 |
StringDedupEntry* alloc(); |
|
114 |
||
115 |
// Insert a table entry into the cache. |
|
116 |
void free(StringDedupEntry* entry, uint worker_id); |
|
117 |
||
118 |
// Returns current number of entries in the cache. |
|
119 |
size_t size(); |
|
120 |
||
121 |
// Deletes overflowed entries. |
|
122 |
void delete_overflowed(); |
|
123 |
}; |
|
124 |
||
125 |
StringDedupEntryCache::StringDedupEntryCache(size_t max_size) : |
|
126 |
_nlists(ParallelGCThreads), |
|
127 |
_max_list_length(0), |
|
128 |
_cached(PaddedArray<StringDedupEntryList, mtGC>::create_unfreeable((uint)_nlists)), |
|
129 |
_overflowed(PaddedArray<StringDedupEntryList, mtGC>::create_unfreeable((uint)_nlists)) { |
|
130 |
set_max_size(max_size); |
|
131 |
} |
|
132 |
||
133 |
StringDedupEntryCache::~StringDedupEntryCache() { |
|
134 |
ShouldNotReachHere(); |
|
135 |
} |
|
136 |
||
137 |
void StringDedupEntryCache::set_max_size(size_t size) { |
|
138 |
_max_list_length = size / _nlists; |
|
139 |
} |
|
140 |
||
141 |
StringDedupEntry* StringDedupEntryCache::alloc() { |
|
142 |
for (size_t i = 0; i < _nlists; i++) { |
|
143 |
StringDedupEntry* entry = _cached[i].remove(); |
|
144 |
if (entry != NULL) { |
|
145 |
return entry; |
|
146 |
} |
|
147 |
} |
|
148 |
return new StringDedupEntry(); |
|
149 |
} |
|
150 |
||
151 |
void StringDedupEntryCache::free(StringDedupEntry* entry, uint worker_id) { |
|
152 |
assert(entry->obj() != NULL, "Double free"); |
|
153 |
assert(worker_id < _nlists, "Invalid worker id"); |
|
154 |
||
155 |
entry->set_obj(NULL); |
|
156 |
entry->set_hash(0); |
|
157 |
||
158 |
if (_cached[worker_id].length() < _max_list_length) { |
|
159 |
// Cache is not full |
|
160 |
_cached[worker_id].add(entry); |
|
161 |
} else { |
|
162 |
// Cache is full, add to overflow list for later deletion |
|
163 |
_overflowed[worker_id].add(entry); |
|
164 |
} |
|
165 |
} |
|
166 |
||
167 |
size_t StringDedupEntryCache::size() { |
|
168 |
size_t size = 0; |
|
169 |
for (size_t i = 0; i < _nlists; i++) { |
|
170 |
size += _cached[i].length(); |
|
171 |
} |
|
172 |
return size; |
|
173 |
} |
|
174 |
||
175 |
void StringDedupEntryCache::delete_overflowed() { |
|
176 |
double start = os::elapsedTime(); |
|
177 |
uintx count = 0; |
|
178 |
||
179 |
for (size_t i = 0; i < _nlists; i++) { |
|
180 |
StringDedupEntry* entry; |
|
181 |
||
182 |
{ |
|
183 |
// The overflow list can be modified during safepoints, therefore |
|
184 |
// we temporarily join the suspendible thread set while removing |
|
185 |
// all entries from the list. |
|
186 |
SuspendibleThreadSetJoiner sts_join; |
|
187 |
entry = _overflowed[i].remove_all(); |
|
188 |
} |
|
189 |
||
190 |
// Delete all entries |
|
191 |
while (entry != NULL) { |
|
192 |
StringDedupEntry* next = entry->next(); |
|
193 |
delete entry; |
|
194 |
entry = next; |
|
195 |
count++; |
|
196 |
} |
|
197 |
} |
|
198 |
||
199 |
double end = os::elapsedTime(); |
|
200 |
log_trace(gc, stringdedup)("Deleted " UINTX_FORMAT " entries, " STRDEDUP_TIME_FORMAT_MS, |
|
201 |
count, STRDEDUP_TIME_PARAM_MS(end - start)); |
|
202 |
} |
|
203 |
||
204 |
StringDedupTable* StringDedupTable::_table = NULL; |
|
205 |
StringDedupEntryCache* StringDedupTable::_entry_cache = NULL; |
|
206 |
||
207 |
const size_t StringDedupTable::_min_size = (1 << 10); // 1024 |
|
208 |
const size_t StringDedupTable::_max_size = (1 << 24); // 16777216 |
|
209 |
const double StringDedupTable::_grow_load_factor = 2.0; // Grow table at 200% load |
|
210 |
const double StringDedupTable::_shrink_load_factor = _grow_load_factor / 3.0; // Shrink table at 67% load |
|
211 |
const double StringDedupTable::_max_cache_factor = 0.1; // Cache a maximum of 10% of the table size |
|
212 |
const uintx StringDedupTable::_rehash_multiple = 60; // Hash bucket has 60 times more collisions than expected |
|
213 |
const uintx StringDedupTable::_rehash_threshold = (uintx)(_rehash_multiple * _grow_load_factor); |
|
214 |
||
215 |
uintx StringDedupTable::_entries_added = 0; |
|
216 |
uintx StringDedupTable::_entries_removed = 0; |
|
217 |
uintx StringDedupTable::_resize_count = 0; |
|
218 |
uintx StringDedupTable::_rehash_count = 0; |
|
219 |
||
220 |
StringDedupTable* StringDedupTable::_resized_table = NULL; |
|
221 |
StringDedupTable* StringDedupTable::_rehashed_table = NULL; |
|
222 |
volatile size_t StringDedupTable::_claimed_index = 0; |
|
223 |
||
224 |
StringDedupTable::StringDedupTable(size_t size, jint hash_seed) : |
|
225 |
_size(size), |
|
226 |
_entries(0), |
|
51332 | 227 |
_shrink_threshold((uintx)(size * _shrink_load_factor)), |
50574 | 228 |
_grow_threshold((uintx)(size * _grow_load_factor)), |
229 |
_rehash_needed(false), |
|
230 |
_hash_seed(hash_seed) { |
|
231 |
assert(is_power_of_2(size), "Table size must be a power of 2"); |
|
232 |
_buckets = NEW_C_HEAP_ARRAY(StringDedupEntry*, _size, mtGC); |
|
233 |
memset(_buckets, 0, _size * sizeof(StringDedupEntry*)); |
|
234 |
} |
|
235 |
||
236 |
StringDedupTable::~StringDedupTable() { |
|
237 |
FREE_C_HEAP_ARRAY(G1StringDedupEntry*, _buckets); |
|
238 |
} |
|
239 |
||
240 |
void StringDedupTable::create() { |
|
241 |
assert(_table == NULL, "One string deduplication table allowed"); |
|
242 |
_entry_cache = new StringDedupEntryCache(_min_size * _max_cache_factor); |
|
243 |
_table = new StringDedupTable(_min_size); |
|
244 |
} |
|
245 |
||
246 |
void StringDedupTable::add(typeArrayOop value, bool latin1, unsigned int hash, StringDedupEntry** list) { |
|
247 |
StringDedupEntry* entry = _entry_cache->alloc(); |
|
248 |
entry->set_obj(value); |
|
249 |
entry->set_hash(hash); |
|
250 |
entry->set_latin1(latin1); |
|
251 |
entry->set_next(*list); |
|
252 |
*list = entry; |
|
253 |
_entries++; |
|
254 |
} |
|
255 |
||
256 |
void StringDedupTable::remove(StringDedupEntry** pentry, uint worker_id) { |
|
257 |
StringDedupEntry* entry = *pentry; |
|
258 |
*pentry = entry->next(); |
|
259 |
_entry_cache->free(entry, worker_id); |
|
260 |
} |
|
261 |
||
262 |
void StringDedupTable::transfer(StringDedupEntry** pentry, StringDedupTable* dest) { |
|
263 |
StringDedupEntry* entry = *pentry; |
|
264 |
*pentry = entry->next(); |
|
265 |
unsigned int hash = entry->hash(); |
|
266 |
size_t index = dest->hash_to_index(hash); |
|
267 |
StringDedupEntry** list = dest->bucket(index); |
|
268 |
entry->set_next(*list); |
|
269 |
*list = entry; |
|
270 |
} |
|
271 |
||
272 |
typeArrayOop StringDedupTable::lookup(typeArrayOop value, bool latin1, unsigned int hash, |
|
273 |
StringDedupEntry** list, uintx &count) { |
|
274 |
for (StringDedupEntry* entry = *list; entry != NULL; entry = entry->next()) { |
|
275 |
if (entry->hash() == hash && entry->latin1() == latin1) { |
|
50803
45c1fde86050
8205559: Remove IN_CONCURRENT_ROOT Access decorator
kbarrett
parents:
50676
diff
changeset
|
276 |
oop* obj_addr = (oop*)entry->obj_addr(); |
45c1fde86050
8205559: Remove IN_CONCURRENT_ROOT Access decorator
kbarrett
parents:
50676
diff
changeset
|
277 |
oop obj = NativeAccess<ON_PHANTOM_OOP_REF | AS_NO_KEEPALIVE>::oop_load(obj_addr); |
54102
fab8496eb541
8220496: Race in java_lang_String::length() when deduplicating
redestad
parents:
52070
diff
changeset
|
278 |
if (java_lang_String::value_equals(value, static_cast<typeArrayOop>(obj))) { |
50803
45c1fde86050
8205559: Remove IN_CONCURRENT_ROOT Access decorator
kbarrett
parents:
50676
diff
changeset
|
279 |
obj = NativeAccess<ON_PHANTOM_OOP_REF>::oop_load(obj_addr); |
45c1fde86050
8205559: Remove IN_CONCURRENT_ROOT Access decorator
kbarrett
parents:
50676
diff
changeset
|
280 |
return static_cast<typeArrayOop>(obj); |
50574 | 281 |
} |
282 |
} |
|
283 |
count++; |
|
284 |
} |
|
285 |
||
286 |
// Not found |
|
287 |
return NULL; |
|
288 |
} |
|
289 |
||
290 |
typeArrayOop StringDedupTable::lookup_or_add_inner(typeArrayOop value, bool latin1, unsigned int hash) { |
|
291 |
size_t index = hash_to_index(hash); |
|
292 |
StringDedupEntry** list = bucket(index); |
|
293 |
uintx count = 0; |
|
294 |
||
295 |
// Lookup in list |
|
296 |
typeArrayOop existing_value = lookup(value, latin1, hash, list, count); |
|
297 |
||
298 |
// Check if rehash is needed |
|
299 |
if (count > _rehash_threshold) { |
|
300 |
_rehash_needed = true; |
|
301 |
} |
|
302 |
||
303 |
if (existing_value == NULL) { |
|
304 |
// Not found, add new entry |
|
305 |
add(value, latin1, hash, list); |
|
306 |
||
307 |
// Update statistics |
|
308 |
_entries_added++; |
|
309 |
} |
|
310 |
||
311 |
return existing_value; |
|
312 |
} |
|
313 |
||
314 |
unsigned int StringDedupTable::hash_code(typeArrayOop value, bool latin1) { |
|
315 |
unsigned int hash; |
|
316 |
int length = value->length(); |
|
317 |
if (latin1) { |
|
318 |
const jbyte* data = (jbyte*)value->base(T_BYTE); |
|
319 |
if (use_java_hash()) { |
|
320 |
hash = java_lang_String::hash_code(data, length); |
|
321 |
} else { |
|
322 |
hash = AltHashing::murmur3_32(_table->_hash_seed, data, length); |
|
323 |
} |
|
324 |
} else { |
|
325 |
length /= sizeof(jchar) / sizeof(jbyte); // Convert number of bytes to number of chars |
|
326 |
const jchar* data = (jchar*)value->base(T_CHAR); |
|
327 |
if (use_java_hash()) { |
|
328 |
hash = java_lang_String::hash_code(data, length); |
|
329 |
} else { |
|
330 |
hash = AltHashing::murmur3_32(_table->_hash_seed, data, length); |
|
331 |
} |
|
332 |
} |
|
333 |
||
334 |
return hash; |
|
335 |
} |
|
336 |
||
337 |
void StringDedupTable::deduplicate(oop java_string, StringDedupStat* stat) { |
|
338 |
assert(java_lang_String::is_instance(java_string), "Must be a string"); |
|
339 |
NoSafepointVerifier nsv; |
|
340 |
||
341 |
stat->inc_inspected(); |
|
342 |
||
343 |
typeArrayOop value = java_lang_String::value(java_string); |
|
344 |
if (value == NULL) { |
|
345 |
// String has no value |
|
346 |
stat->inc_skipped(); |
|
347 |
return; |
|
348 |
} |
|
349 |
||
350 |
bool latin1 = java_lang_String::is_latin1(java_string); |
|
351 |
unsigned int hash = 0; |
|
352 |
||
353 |
if (use_java_hash()) { |
|
54486
7fd299216e97
8221836: Avoid recalculating String.hash when zero
redestad
parents:
54102
diff
changeset
|
354 |
if (!java_lang_String::hash_is_set(java_string)) { |
7fd299216e97
8221836: Avoid recalculating String.hash when zero
redestad
parents:
54102
diff
changeset
|
355 |
stat->inc_hashed(); |
7fd299216e97
8221836: Avoid recalculating String.hash when zero
redestad
parents:
54102
diff
changeset
|
356 |
} |
7fd299216e97
8221836: Avoid recalculating String.hash when zero
redestad
parents:
54102
diff
changeset
|
357 |
hash = java_lang_String::hash_code(java_string); |
7fd299216e97
8221836: Avoid recalculating String.hash when zero
redestad
parents:
54102
diff
changeset
|
358 |
} else { |
50574 | 359 |
// Compute hash |
360 |
hash = hash_code(value, latin1); |
|
361 |
stat->inc_hashed(); |
|
362 |
} |
|
363 |
||
364 |
typeArrayOop existing_value = lookup_or_add(value, latin1, hash); |
|
52070 | 365 |
if (oopDesc::equals_raw(existing_value, value)) { |
50574 | 366 |
// Same value, already known |
367 |
stat->inc_known(); |
|
368 |
return; |
|
369 |
} |
|
370 |
||
371 |
// Get size of value array |
|
372 |
uintx size_in_bytes = value->size() * HeapWordSize; |
|
373 |
stat->inc_new(size_in_bytes); |
|
374 |
||
375 |
if (existing_value != NULL) { |
|
376 |
// Existing value found, deduplicate string |
|
377 |
java_lang_String::set_value(java_string, existing_value); |
|
378 |
stat->deduped(value, size_in_bytes); |
|
379 |
} |
|
380 |
} |
|
381 |
||
382 |
bool StringDedupTable::is_resizing() { |
|
383 |
return _resized_table != NULL; |
|
384 |
} |
|
385 |
||
386 |
bool StringDedupTable::is_rehashing() { |
|
387 |
return _rehashed_table != NULL; |
|
388 |
} |
|
389 |
||
390 |
StringDedupTable* StringDedupTable::prepare_resize() { |
|
391 |
size_t size = _table->_size; |
|
392 |
||
393 |
// Check if the hashtable needs to be resized |
|
394 |
if (_table->_entries > _table->_grow_threshold) { |
|
395 |
// Grow table, double the size |
|
396 |
size *= 2; |
|
397 |
if (size > _max_size) { |
|
398 |
// Too big, don't resize |
|
399 |
return NULL; |
|
400 |
} |
|
401 |
} else if (_table->_entries < _table->_shrink_threshold) { |
|
402 |
// Shrink table, half the size |
|
403 |
size /= 2; |
|
404 |
if (size < _min_size) { |
|
405 |
// Too small, don't resize |
|
406 |
return NULL; |
|
407 |
} |
|
408 |
} else if (StringDeduplicationResizeALot) { |
|
409 |
// Force grow |
|
410 |
size *= 2; |
|
411 |
if (size > _max_size) { |
|
412 |
// Too big, force shrink instead |
|
413 |
size /= 4; |
|
414 |
} |
|
415 |
} else { |
|
416 |
// Resize not needed |
|
417 |
return NULL; |
|
418 |
} |
|
419 |
||
420 |
// Update statistics |
|
421 |
_resize_count++; |
|
422 |
||
423 |
// Update max cache size |
|
424 |
_entry_cache->set_max_size(size * _max_cache_factor); |
|
425 |
||
426 |
// Allocate the new table. The new table will be populated by workers |
|
427 |
// calling unlink_or_oops_do() and finally installed by finish_resize(). |
|
428 |
return new StringDedupTable(size, _table->_hash_seed); |
|
429 |
} |
|
430 |
||
431 |
void StringDedupTable::finish_resize(StringDedupTable* resized_table) { |
|
432 |
assert(resized_table != NULL, "Invalid table"); |
|
433 |
||
434 |
resized_table->_entries = _table->_entries; |
|
435 |
||
436 |
// Free old table |
|
437 |
delete _table; |
|
438 |
||
439 |
// Install new table |
|
440 |
_table = resized_table; |
|
441 |
} |
|
442 |
||
443 |
void StringDedupTable::unlink_or_oops_do(StringDedupUnlinkOrOopsDoClosure* cl, uint worker_id) { |
|
444 |
// The table is divided into partitions to allow lock-less parallel processing by |
|
445 |
// multiple worker threads. A worker thread first claims a partition, which ensures |
|
446 |
// exclusive access to that part of the table, then continues to process it. To allow |
|
447 |
// shrinking of the table in parallel we also need to make sure that the same worker |
|
448 |
// thread processes all partitions where entries will hash to the same destination |
|
449 |
// partition. Since the table size is always a power of two and we always shrink by |
|
450 |
// dividing the table in half, we know that for a given partition there is only one |
|
451 |
// other partition whoes entries will hash to the same destination partition. That |
|
452 |
// other partition is always the sibling partition in the second half of the table. |
|
453 |
// For example, if the table is divided into 8 partitions, the sibling of partition 0 |
|
454 |
// is partition 4, the sibling of partition 1 is partition 5, etc. |
|
455 |
size_t table_half = _table->_size / 2; |
|
456 |
||
457 |
// Let each partition be one page worth of buckets |
|
458 |
size_t partition_size = MIN2(table_half, os::vm_page_size() / sizeof(StringDedupEntry*)); |
|
459 |
assert(table_half % partition_size == 0, "Invalid partition size"); |
|
460 |
||
461 |
// Number of entries removed during the scan |
|
462 |
uintx removed = 0; |
|
463 |
||
464 |
for (;;) { |
|
465 |
// Grab next partition to scan |
|
466 |
size_t partition_begin = claim_table_partition(partition_size); |
|
467 |
size_t partition_end = partition_begin + partition_size; |
|
468 |
if (partition_begin >= table_half) { |
|
469 |
// End of table |
|
470 |
break; |
|
471 |
} |
|
472 |
||
473 |
// Scan the partition followed by the sibling partition in the second half of the table |
|
474 |
removed += unlink_or_oops_do(cl, partition_begin, partition_end, worker_id); |
|
475 |
removed += unlink_or_oops_do(cl, table_half + partition_begin, table_half + partition_end, worker_id); |
|
476 |
} |
|
477 |
||
478 |
// Delayed update to avoid contention on the table lock |
|
479 |
if (removed > 0) { |
|
54623
1126f0607c70
8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents:
54486
diff
changeset
|
480 |
MutexLocker ml(StringDedupTable_lock, Mutex::_no_safepoint_check_flag); |
50574 | 481 |
_table->_entries -= removed; |
482 |
_entries_removed += removed; |
|
483 |
} |
|
484 |
} |
|
485 |
||
486 |
uintx StringDedupTable::unlink_or_oops_do(StringDedupUnlinkOrOopsDoClosure* cl, |
|
487 |
size_t partition_begin, |
|
488 |
size_t partition_end, |
|
489 |
uint worker_id) { |
|
490 |
uintx removed = 0; |
|
491 |
for (size_t bucket = partition_begin; bucket < partition_end; bucket++) { |
|
492 |
StringDedupEntry** entry = _table->bucket(bucket); |
|
493 |
while (*entry != NULL) { |
|
494 |
oop* p = (oop*)(*entry)->obj_addr(); |
|
495 |
if (cl->is_alive(*p)) { |
|
496 |
cl->keep_alive(p); |
|
497 |
if (is_resizing()) { |
|
498 |
// We are resizing the table, transfer entry to the new table |
|
499 |
_table->transfer(entry, _resized_table); |
|
500 |
} else { |
|
501 |
if (is_rehashing()) { |
|
502 |
// We are rehashing the table, rehash the entry but keep it |
|
503 |
// in the table. We can't transfer entries into the new table |
|
504 |
// at this point since we don't have exclusive access to all |
|
505 |
// destination partitions. finish_rehash() will do a single |
|
506 |
// threaded transfer of all entries. |
|
507 |
typeArrayOop value = (typeArrayOop)*p; |
|
508 |
bool latin1 = (*entry)->latin1(); |
|
509 |
unsigned int hash = hash_code(value, latin1); |
|
510 |
(*entry)->set_hash(hash); |
|
511 |
} |
|
512 |
||
513 |
// Move to next entry |
|
514 |
entry = (*entry)->next_addr(); |
|
515 |
} |
|
516 |
} else { |
|
517 |
// Not alive, remove entry from table |
|
518 |
_table->remove(entry, worker_id); |
|
519 |
removed++; |
|
520 |
} |
|
521 |
} |
|
522 |
} |
|
523 |
||
524 |
return removed; |
|
525 |
} |
|
526 |
||
527 |
void StringDedupTable::gc_prologue(bool resize_and_rehash_table) { |
|
528 |
assert(!is_resizing() && !is_rehashing(), "Already in progress?"); |
|
529 |
||
530 |
_claimed_index = 0; |
|
531 |
if (resize_and_rehash_table) { |
|
532 |
// If both resize and rehash is needed, only do resize. Rehash of |
|
533 |
// the table will eventually happen if the situation persists. |
|
534 |
_resized_table = StringDedupTable::prepare_resize(); |
|
535 |
if (!is_resizing()) { |
|
536 |
_rehashed_table = StringDedupTable::prepare_rehash(); |
|
537 |
} |
|
538 |
} |
|
539 |
} |
|
540 |
||
541 |
void StringDedupTable::gc_epilogue() { |
|
542 |
assert(!is_resizing() || !is_rehashing(), "Can not both resize and rehash"); |
|
543 |
assert(_claimed_index >= _table->_size / 2 || _claimed_index == 0, "All or nothing"); |
|
544 |
||
545 |
if (is_resizing()) { |
|
546 |
StringDedupTable::finish_resize(_resized_table); |
|
547 |
_resized_table = NULL; |
|
548 |
} else if (is_rehashing()) { |
|
549 |
StringDedupTable::finish_rehash(_rehashed_table); |
|
550 |
_rehashed_table = NULL; |
|
551 |
} |
|
552 |
} |
|
553 |
||
554 |
StringDedupTable* StringDedupTable::prepare_rehash() { |
|
555 |
if (!_table->_rehash_needed && !StringDeduplicationRehashALot) { |
|
556 |
// Rehash not needed |
|
557 |
return NULL; |
|
558 |
} |
|
559 |
||
560 |
// Update statistics |
|
561 |
_rehash_count++; |
|
562 |
||
563 |
// Compute new hash seed |
|
564 |
_table->_hash_seed = AltHashing::compute_seed(); |
|
565 |
||
566 |
// Allocate the new table, same size and hash seed |
|
567 |
return new StringDedupTable(_table->_size, _table->_hash_seed); |
|
568 |
} |
|
569 |
||
570 |
void StringDedupTable::finish_rehash(StringDedupTable* rehashed_table) { |
|
571 |
assert(rehashed_table != NULL, "Invalid table"); |
|
572 |
||
573 |
// Move all newly rehashed entries into the correct buckets in the new table |
|
574 |
for (size_t bucket = 0; bucket < _table->_size; bucket++) { |
|
575 |
StringDedupEntry** entry = _table->bucket(bucket); |
|
576 |
while (*entry != NULL) { |
|
577 |
_table->transfer(entry, rehashed_table); |
|
578 |
} |
|
579 |
} |
|
580 |
||
581 |
rehashed_table->_entries = _table->_entries; |
|
582 |
||
583 |
// Free old table |
|
584 |
delete _table; |
|
585 |
||
586 |
// Install new table |
|
587 |
_table = rehashed_table; |
|
588 |
} |
|
589 |
||
590 |
size_t StringDedupTable::claim_table_partition(size_t partition_size) { |
|
591 |
return Atomic::add(partition_size, &_claimed_index) - partition_size; |
|
592 |
} |
|
593 |
||
594 |
void StringDedupTable::verify() { |
|
595 |
for (size_t bucket = 0; bucket < _table->_size; bucket++) { |
|
596 |
// Verify entries |
|
597 |
StringDedupEntry** entry = _table->bucket(bucket); |
|
598 |
while (*entry != NULL) { |
|
599 |
typeArrayOop value = (*entry)->obj(); |
|
600 |
guarantee(value != NULL, "Object must not be NULL"); |
|
601 |
guarantee(Universe::heap()->is_in_reserved(value), "Object must be on the heap"); |
|
602 |
guarantee(!value->is_forwarded(), "Object must not be forwarded"); |
|
603 |
guarantee(value->is_typeArray(), "Object must be a typeArrayOop"); |
|
604 |
bool latin1 = (*entry)->latin1(); |
|
605 |
unsigned int hash = hash_code(value, latin1); |
|
606 |
guarantee((*entry)->hash() == hash, "Table entry has inorrect hash"); |
|
607 |
guarantee(_table->hash_to_index(hash) == bucket, "Table entry has incorrect index"); |
|
608 |
entry = (*entry)->next_addr(); |
|
609 |
} |
|
610 |
||
611 |
// Verify that we do not have entries with identical oops or identical arrays. |
|
612 |
// We only need to compare entries in the same bucket. If the same oop or an |
|
613 |
// identical array has been inserted more than once into different/incorrect |
|
614 |
// buckets the verification step above will catch that. |
|
615 |
StringDedupEntry** entry1 = _table->bucket(bucket); |
|
616 |
while (*entry1 != NULL) { |
|
617 |
typeArrayOop value1 = (*entry1)->obj(); |
|
618 |
bool latin1_1 = (*entry1)->latin1(); |
|
619 |
StringDedupEntry** entry2 = (*entry1)->next_addr(); |
|
620 |
while (*entry2 != NULL) { |
|
621 |
typeArrayOop value2 = (*entry2)->obj(); |
|
622 |
bool latin1_2 = (*entry2)->latin1(); |
|
54102
fab8496eb541
8220496: Race in java_lang_String::length() when deduplicating
redestad
parents:
52070
diff
changeset
|
623 |
guarantee(latin1_1 != latin1_2 || !java_lang_String::value_equals(value1, value2), "Table entries must not have identical arrays"); |
50574 | 624 |
entry2 = (*entry2)->next_addr(); |
625 |
} |
|
626 |
entry1 = (*entry1)->next_addr(); |
|
627 |
} |
|
628 |
} |
|
629 |
} |
|
630 |
||
631 |
void StringDedupTable::clean_entry_cache() { |
|
632 |
_entry_cache->delete_overflowed(); |
|
633 |
} |
|
634 |
||
635 |
void StringDedupTable::print_statistics() { |
|
636 |
Log(gc, stringdedup) log; |
|
637 |
log.debug(" Table"); |
|
638 |
log.debug(" Memory Usage: " STRDEDUP_BYTES_FORMAT_NS, |
|
639 |
STRDEDUP_BYTES_PARAM(_table->_size * sizeof(StringDedupEntry*) + (_table->_entries + _entry_cache->size()) * sizeof(StringDedupEntry))); |
|
640 |
log.debug(" Size: " SIZE_FORMAT ", Min: " SIZE_FORMAT ", Max: " SIZE_FORMAT, _table->_size, _min_size, _max_size); |
|
641 |
log.debug(" Entries: " UINTX_FORMAT ", Load: " STRDEDUP_PERCENT_FORMAT_NS ", Cached: " UINTX_FORMAT ", Added: " UINTX_FORMAT ", Removed: " UINTX_FORMAT, |
|
50676
8c0a5b51559b
8203030: Zero s390 31 bit size_t type conflicts in shared code
chrisphi
parents:
50599
diff
changeset
|
642 |
_table->_entries, percent_of((size_t)_table->_entries, _table->_size), _entry_cache->size(), _entries_added, _entries_removed); |
50574 | 643 |
log.debug(" Resize Count: " UINTX_FORMAT ", Shrink Threshold: " UINTX_FORMAT "(" STRDEDUP_PERCENT_FORMAT_NS "), Grow Threshold: " UINTX_FORMAT "(" STRDEDUP_PERCENT_FORMAT_NS ")", |
644 |
_resize_count, _table->_shrink_threshold, _shrink_load_factor * 100.0, _table->_grow_threshold, _grow_load_factor * 100.0); |
|
645 |
log.debug(" Rehash Count: " UINTX_FORMAT ", Rehash Threshold: " UINTX_FORMAT ", Hash Seed: 0x%x", _rehash_count, _rehash_threshold, _table->_hash_seed); |
|
646 |
log.debug(" Age Threshold: " UINTX_FORMAT, StringDeduplicationAgeThreshold); |
|
647 |
} |