Sat, 07 Nov 2020 10:30:02 +0800
Added tag mips-jdk8u275-b01 for changeset d3b4d62f391f
1 /*
2 * Copyright (c) 2014, 2016, 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 */
25 #include "precompiled.hpp"
26 #include "classfile/altHashing.hpp"
27 #include "classfile/javaClasses.hpp"
28 #include "gc_implementation/g1/g1CollectedHeap.inline.hpp"
29 #include "gc_implementation/g1/g1SATBCardTableModRefBS.hpp"
30 #include "gc_implementation/g1/g1StringDedupTable.hpp"
31 #include "gc_implementation/shared/concurrentGCThread.hpp"
32 #include "memory/gcLocker.hpp"
33 #include "memory/padded.inline.hpp"
34 #include "oops/typeArrayOop.hpp"
35 #include "runtime/mutexLocker.hpp"
37 //
38 // List of deduplication table entries. Links table
39 // entries together using their _next fields.
40 //
41 class G1StringDedupEntryList : public CHeapObj<mtGC> {
42 private:
43 G1StringDedupEntry* _list;
44 size_t _length;
46 public:
47 G1StringDedupEntryList() :
48 _list(NULL),
49 _length(0) {
50 }
52 void add(G1StringDedupEntry* entry) {
53 entry->set_next(_list);
54 _list = entry;
55 _length++;
56 }
58 G1StringDedupEntry* remove() {
59 G1StringDedupEntry* entry = _list;
60 if (entry != NULL) {
61 _list = entry->next();
62 _length--;
63 }
64 return entry;
65 }
67 G1StringDedupEntry* remove_all() {
68 G1StringDedupEntry* list = _list;
69 _list = NULL;
70 return list;
71 }
73 size_t length() {
74 return _length;
75 }
76 };
78 //
79 // Cache of deduplication table entries. This cache provides fast allocation and
80 // reuse of table entries to lower the pressure on the underlying allocator.
81 // But more importantly, it provides fast/deferred freeing of table entries. This
82 // is important because freeing of table entries is done during stop-the-world
83 // phases and it is not uncommon for large number of entries to be freed at once.
84 // Tables entries that are freed during these phases are placed onto a freelist in
85 // the cache. The deduplication thread, which executes in a concurrent phase, will
86 // later reuse or free the underlying memory for these entries.
87 //
88 // The cache allows for single-threaded allocations and multi-threaded frees.
89 // Allocations are synchronized by StringDedupTable_lock as part of a table
90 // modification.
91 //
92 class G1StringDedupEntryCache : public CHeapObj<mtGC> {
93 private:
94 // One cache/overflow list per GC worker to allow lock less freeing of
95 // entries while doing a parallel scan of the table. Using PaddedEnd to
96 // avoid false sharing.
97 size_t _nlists;
98 size_t _max_list_length;
99 PaddedEnd<G1StringDedupEntryList>* _cached;
100 PaddedEnd<G1StringDedupEntryList>* _overflowed;
102 public:
103 G1StringDedupEntryCache(size_t max_size);
104 ~G1StringDedupEntryCache();
106 // Set max number of table entries to cache.
107 void set_max_size(size_t max_size);
109 // Get a table entry from the cache, or allocate a new entry if the cache is empty.
110 G1StringDedupEntry* alloc();
112 // Insert a table entry into the cache.
113 void free(G1StringDedupEntry* entry, uint worker_id);
115 // Returns current number of entries in the cache.
116 size_t size();
118 // Deletes overflowed entries.
119 void delete_overflowed();
120 };
122 G1StringDedupEntryCache::G1StringDedupEntryCache(size_t max_size) :
123 _nlists(MAX2(ParallelGCThreads, (size_t)1)),
124 _max_list_length(0),
125 _cached(PaddedArray<G1StringDedupEntryList, mtGC>::create_unfreeable((uint)_nlists)),
126 _overflowed(PaddedArray<G1StringDedupEntryList, mtGC>::create_unfreeable((uint)_nlists)) {
127 set_max_size(max_size);
128 }
130 G1StringDedupEntryCache::~G1StringDedupEntryCache() {
131 ShouldNotReachHere();
132 }
134 void G1StringDedupEntryCache::set_max_size(size_t size) {
135 _max_list_length = size / _nlists;
136 }
138 G1StringDedupEntry* G1StringDedupEntryCache::alloc() {
139 for (size_t i = 0; i < _nlists; i++) {
140 G1StringDedupEntry* entry = _cached[i].remove();
141 if (entry != NULL) {
142 return entry;
143 }
144 }
145 return new G1StringDedupEntry();
146 }
148 void G1StringDedupEntryCache::free(G1StringDedupEntry* entry, uint worker_id) {
149 assert(entry->obj() != NULL, "Double free");
150 assert(worker_id < _nlists, "Invalid worker id");
152 entry->set_obj(NULL);
153 entry->set_hash(0);
155 if (_cached[worker_id].length() < _max_list_length) {
156 // Cache is not full
157 _cached[worker_id].add(entry);
158 } else {
159 // Cache is full, add to overflow list for later deletion
160 _overflowed[worker_id].add(entry);
161 }
162 }
164 size_t G1StringDedupEntryCache::size() {
165 size_t size = 0;
166 for (size_t i = 0; i < _nlists; i++) {
167 size += _cached[i].length();
168 }
169 return size;
170 }
172 void G1StringDedupEntryCache::delete_overflowed() {
173 double start = os::elapsedTime();
174 uintx count = 0;
176 for (size_t i = 0; i < _nlists; i++) {
177 G1StringDedupEntry* entry;
179 {
180 // The overflow list can be modified during safepoints, therefore
181 // we temporarily join the suspendible thread set while removing
182 // all entries from the list.
183 SuspendibleThreadSetJoiner sts_join;
184 entry = _overflowed[i].remove_all();
185 }
187 // Delete all entries
188 while (entry != NULL) {
189 G1StringDedupEntry* next = entry->next();
190 delete entry;
191 entry = next;
192 count++;
193 }
194 }
196 double end = os::elapsedTime();
197 if (PrintStringDeduplicationStatistics) {
198 gclog_or_tty->print_cr("[GC concurrent-string-deduplication, deleted " UINTX_FORMAT " entries, " G1_STRDEDUP_TIME_FORMAT "]", count, end - start);
199 }
200 }
202 G1StringDedupTable* G1StringDedupTable::_table = NULL;
203 G1StringDedupEntryCache* G1StringDedupTable::_entry_cache = NULL;
205 const size_t G1StringDedupTable::_min_size = (1 << 10); // 1024
206 const size_t G1StringDedupTable::_max_size = (1 << 24); // 16777216
207 const double G1StringDedupTable::_grow_load_factor = 2.0; // Grow table at 200% load
208 const double G1StringDedupTable::_shrink_load_factor = _grow_load_factor / 3.0; // Shrink table at 67% load
209 const double G1StringDedupTable::_max_cache_factor = 0.1; // Cache a maximum of 10% of the table size
210 const uintx G1StringDedupTable::_rehash_multiple = 60; // Hash bucket has 60 times more collisions than expected
211 const uintx G1StringDedupTable::_rehash_threshold = (uintx)(_rehash_multiple * _grow_load_factor);
213 uintx G1StringDedupTable::_entries_added = 0;
214 uintx G1StringDedupTable::_entries_removed = 0;
215 uintx G1StringDedupTable::_resize_count = 0;
216 uintx G1StringDedupTable::_rehash_count = 0;
218 G1StringDedupTable::G1StringDedupTable(size_t size, uint64_t hash_seed) :
219 _size(size),
220 _entries(0),
221 _grow_threshold((uintx)(size * _grow_load_factor)),
222 _shrink_threshold((uintx)(size * _shrink_load_factor)),
223 _rehash_needed(false),
224 _hash_seed(hash_seed) {
225 assert(is_power_of_2(size), "Table size must be a power of 2");
226 _buckets = NEW_C_HEAP_ARRAY(G1StringDedupEntry*, _size, mtGC);
227 memset(_buckets, 0, _size * sizeof(G1StringDedupEntry*));
228 }
230 G1StringDedupTable::~G1StringDedupTable() {
231 FREE_C_HEAP_ARRAY(G1StringDedupEntry*, _buckets, mtGC);
232 }
234 void G1StringDedupTable::create() {
235 assert(_table == NULL, "One string deduplication table allowed");
236 _entry_cache = new G1StringDedupEntryCache((size_t)(_min_size * _max_cache_factor));
237 _table = new G1StringDedupTable(_min_size);
238 }
240 void G1StringDedupTable::add(typeArrayOop value, unsigned int hash, G1StringDedupEntry** list) {
241 G1StringDedupEntry* entry = _entry_cache->alloc();
242 entry->set_obj(value);
243 entry->set_hash(hash);
244 entry->set_next(*list);
245 *list = entry;
246 _entries++;
247 }
249 void G1StringDedupTable::remove(G1StringDedupEntry** pentry, uint worker_id) {
250 G1StringDedupEntry* entry = *pentry;
251 *pentry = entry->next();
252 _entry_cache->free(entry, worker_id);
253 }
255 void G1StringDedupTable::transfer(G1StringDedupEntry** pentry, G1StringDedupTable* dest) {
256 G1StringDedupEntry* entry = *pentry;
257 *pentry = entry->next();
258 unsigned int hash = entry->hash();
259 size_t index = dest->hash_to_index(hash);
260 G1StringDedupEntry** list = dest->bucket(index);
261 entry->set_next(*list);
262 *list = entry;
263 }
265 bool G1StringDedupTable::equals(typeArrayOop value1, typeArrayOop value2) {
266 return (value1 == value2 ||
267 (value1->length() == value2->length() &&
268 (!memcmp(value1->base(T_CHAR),
269 value2->base(T_CHAR),
270 value1->length() * sizeof(jchar)))));
271 }
273 typeArrayOop G1StringDedupTable::lookup(typeArrayOop value, unsigned int hash,
274 G1StringDedupEntry** list, uintx &count) {
275 for (G1StringDedupEntry* entry = *list; entry != NULL; entry = entry->next()) {
276 if (entry->hash() == hash) {
277 typeArrayOop existing_value = entry->obj();
278 if (equals(value, existing_value)) {
279 // Match found
280 return existing_value;
281 }
282 }
283 count++;
284 }
286 // Not found
287 return NULL;
288 }
290 typeArrayOop G1StringDedupTable::lookup_or_add_inner(typeArrayOop value, unsigned int hash) {
291 size_t index = hash_to_index(hash);
292 G1StringDedupEntry** list = bucket(index);
293 uintx count = 0;
295 // Lookup in list
296 typeArrayOop existing_value = lookup(value, hash, list, count);
298 // Check if rehash is needed
299 if (count > _rehash_threshold) {
300 _rehash_needed = true;
301 }
303 if (existing_value == NULL) {
304 // Not found, add new entry
305 add(value, hash, list);
307 // Update statistics
308 _entries_added++;
309 }
311 return existing_value;
312 }
314 unsigned int G1StringDedupTable::hash_code(typeArrayOop value) {
315 unsigned int hash;
316 int length = value->length();
317 const jchar* data = (jchar*)value->base(T_CHAR);
319 if (use_java_hash()) {
320 hash = java_lang_String::hash_code(data, length);
321 } else {
322 hash = AltHashing::halfsiphash_32(_table->_hash_seed, (const uint16_t*)data, length);
323 }
324 return hash;
325 }
327 void G1StringDedupTable::deduplicate(oop java_string, G1StringDedupStat& stat) {
328 assert(java_lang_String::is_instance(java_string), "Must be a string");
329 No_Safepoint_Verifier nsv;
331 stat.inc_inspected();
333 typeArrayOop value = java_lang_String::value(java_string);
334 if (value == NULL) {
335 // String has no value
336 stat.inc_skipped();
337 return;
338 }
340 unsigned int hash = 0;
342 if (use_java_hash()) {
343 // Get hash code from cache
344 hash = java_lang_String::hash(java_string);
345 }
347 if (hash == 0) {
348 // Compute hash
349 hash = hash_code(value);
350 stat.inc_hashed();
351 }
353 if (use_java_hash() && hash != 0) {
354 // Store hash code in cache
355 java_lang_String::set_hash(java_string, hash);
356 }
358 typeArrayOop existing_value = lookup_or_add(value, hash);
359 if (existing_value == value) {
360 // Same value, already known
361 stat.inc_known();
362 return;
363 }
365 // Get size of value array
366 uintx size_in_bytes = value->size() * HeapWordSize;
367 stat.inc_new(size_in_bytes);
369 if (existing_value != NULL) {
370 // Enqueue the reference to make sure it is kept alive. Concurrent mark might
371 // otherwise declare it dead if there are no other strong references to this object.
372 G1SATBCardTableModRefBS::enqueue(existing_value);
374 // Existing value found, deduplicate string
375 java_lang_String::set_value(java_string, existing_value);
377 if (G1CollectedHeap::heap()->is_in_young(value)) {
378 stat.inc_deduped_young(size_in_bytes);
379 } else {
380 stat.inc_deduped_old(size_in_bytes);
381 }
382 }
383 }
385 G1StringDedupTable* G1StringDedupTable::prepare_resize() {
386 size_t size = _table->_size;
388 // Check if the hashtable needs to be resized
389 if (_table->_entries > _table->_grow_threshold) {
390 // Grow table, double the size
391 size *= 2;
392 if (size > _max_size) {
393 // Too big, don't resize
394 return NULL;
395 }
396 } else if (_table->_entries < _table->_shrink_threshold) {
397 // Shrink table, half the size
398 size /= 2;
399 if (size < _min_size) {
400 // Too small, don't resize
401 return NULL;
402 }
403 } else if (StringDeduplicationResizeALot) {
404 // Force grow
405 size *= 2;
406 if (size > _max_size) {
407 // Too big, force shrink instead
408 size /= 4;
409 }
410 } else {
411 // Resize not needed
412 return NULL;
413 }
415 // Update statistics
416 _resize_count++;
418 // Update max cache size
419 _entry_cache->set_max_size((size_t)(size * _max_cache_factor));
421 // Allocate the new table. The new table will be populated by workers
422 // calling unlink_or_oops_do() and finally installed by finish_resize().
423 return new G1StringDedupTable(size, _table->_hash_seed);
424 }
426 void G1StringDedupTable::finish_resize(G1StringDedupTable* resized_table) {
427 assert(resized_table != NULL, "Invalid table");
429 resized_table->_entries = _table->_entries;
431 // Free old table
432 delete _table;
434 // Install new table
435 _table = resized_table;
436 }
438 void G1StringDedupTable::unlink_or_oops_do(G1StringDedupUnlinkOrOopsDoClosure* cl, uint worker_id) {
439 // The table is divided into partitions to allow lock-less parallel processing by
440 // multiple worker threads. A worker thread first claims a partition, which ensures
441 // exclusive access to that part of the table, then continues to process it. To allow
442 // shrinking of the table in parallel we also need to make sure that the same worker
443 // thread processes all partitions where entries will hash to the same destination
444 // partition. Since the table size is always a power of two and we always shrink by
445 // dividing the table in half, we know that for a given partition there is only one
446 // other partition whoes entries will hash to the same destination partition. That
447 // other partition is always the sibling partition in the second half of the table.
448 // For example, if the table is divided into 8 partitions, the sibling of partition 0
449 // is partition 4, the sibling of partition 1 is partition 5, etc.
450 size_t table_half = _table->_size / 2;
452 // Let each partition be one page worth of buckets
453 size_t partition_size = MIN2(table_half, os::vm_page_size() / sizeof(G1StringDedupEntry*));
454 assert(table_half % partition_size == 0, "Invalid partition size");
456 // Number of entries removed during the scan
457 uintx removed = 0;
459 for (;;) {
460 // Grab next partition to scan
461 size_t partition_begin = cl->claim_table_partition(partition_size);
462 size_t partition_end = partition_begin + partition_size;
463 if (partition_begin >= table_half) {
464 // End of table
465 break;
466 }
468 // Scan the partition followed by the sibling partition in the second half of the table
469 removed += unlink_or_oops_do(cl, partition_begin, partition_end, worker_id);
470 removed += unlink_or_oops_do(cl, table_half + partition_begin, table_half + partition_end, worker_id);
471 }
473 // Delayed update to avoid contention on the table lock
474 if (removed > 0) {
475 MutexLockerEx ml(StringDedupTable_lock, Mutex::_no_safepoint_check_flag);
476 _table->_entries -= removed;
477 _entries_removed += removed;
478 }
479 }
481 uintx G1StringDedupTable::unlink_or_oops_do(G1StringDedupUnlinkOrOopsDoClosure* cl,
482 size_t partition_begin,
483 size_t partition_end,
484 uint worker_id) {
485 uintx removed = 0;
486 for (size_t bucket = partition_begin; bucket < partition_end; bucket++) {
487 G1StringDedupEntry** entry = _table->bucket(bucket);
488 while (*entry != NULL) {
489 oop* p = (oop*)(*entry)->obj_addr();
490 if (cl->is_alive(*p)) {
491 cl->keep_alive(p);
492 if (cl->is_resizing()) {
493 // We are resizing the table, transfer entry to the new table
494 _table->transfer(entry, cl->resized_table());
495 } else {
496 if (cl->is_rehashing()) {
497 // We are rehashing the table, rehash the entry but keep it
498 // in the table. We can't transfer entries into the new table
499 // at this point since we don't have exclusive access to all
500 // destination partitions. finish_rehash() will do a single
501 // threaded transfer of all entries.
502 typeArrayOop value = (typeArrayOop)*p;
503 unsigned int hash = hash_code(value);
504 (*entry)->set_hash(hash);
505 }
507 // Move to next entry
508 entry = (*entry)->next_addr();
509 }
510 } else {
511 // Not alive, remove entry from table
512 _table->remove(entry, worker_id);
513 removed++;
514 }
515 }
516 }
518 return removed;
519 }
521 G1StringDedupTable* G1StringDedupTable::prepare_rehash() {
522 if (!_table->_rehash_needed && !StringDeduplicationRehashALot) {
523 // Rehash not needed
524 return NULL;
525 }
527 // Update statistics
528 _rehash_count++;
530 // Compute new hash seed
531 _table->_hash_seed = AltHashing::compute_seed();
533 // Allocate the new table, same size and hash seed
534 return new G1StringDedupTable(_table->_size, _table->_hash_seed);
535 }
537 void G1StringDedupTable::finish_rehash(G1StringDedupTable* rehashed_table) {
538 assert(rehashed_table != NULL, "Invalid table");
540 // Move all newly rehashed entries into the correct buckets in the new table
541 for (size_t bucket = 0; bucket < _table->_size; bucket++) {
542 G1StringDedupEntry** entry = _table->bucket(bucket);
543 while (*entry != NULL) {
544 _table->transfer(entry, rehashed_table);
545 }
546 }
548 rehashed_table->_entries = _table->_entries;
550 // Free old table
551 delete _table;
553 // Install new table
554 _table = rehashed_table;
555 }
557 void G1StringDedupTable::verify() {
558 for (size_t bucket = 0; bucket < _table->_size; bucket++) {
559 // Verify entries
560 G1StringDedupEntry** entry = _table->bucket(bucket);
561 while (*entry != NULL) {
562 typeArrayOop value = (*entry)->obj();
563 guarantee(value != NULL, "Object must not be NULL");
564 guarantee(Universe::heap()->is_in_reserved(value), "Object must be on the heap");
565 guarantee(!value->is_forwarded(), "Object must not be forwarded");
566 guarantee(value->is_typeArray(), "Object must be a typeArrayOop");
567 unsigned int hash = hash_code(value);
568 guarantee((*entry)->hash() == hash, "Table entry has inorrect hash");
569 guarantee(_table->hash_to_index(hash) == bucket, "Table entry has incorrect index");
570 entry = (*entry)->next_addr();
571 }
573 // Verify that we do not have entries with identical oops or identical arrays.
574 // We only need to compare entries in the same bucket. If the same oop or an
575 // identical array has been inserted more than once into different/incorrect
576 // buckets the verification step above will catch that.
577 G1StringDedupEntry** entry1 = _table->bucket(bucket);
578 while (*entry1 != NULL) {
579 typeArrayOop value1 = (*entry1)->obj();
580 G1StringDedupEntry** entry2 = (*entry1)->next_addr();
581 while (*entry2 != NULL) {
582 typeArrayOop value2 = (*entry2)->obj();
583 guarantee(!equals(value1, value2), "Table entries must not have identical arrays");
584 entry2 = (*entry2)->next_addr();
585 }
586 entry1 = (*entry1)->next_addr();
587 }
588 }
589 }
591 void G1StringDedupTable::clean_entry_cache() {
592 _entry_cache->delete_overflowed();
593 }
595 void G1StringDedupTable::print_statistics(outputStream* st) {
596 st->print_cr(
597 " [Table]\n"
598 " [Memory Usage: " G1_STRDEDUP_BYTES_FORMAT_NS "]\n"
599 " [Size: " SIZE_FORMAT ", Min: " SIZE_FORMAT ", Max: " SIZE_FORMAT "]\n"
600 " [Entries: " UINTX_FORMAT ", Load: " G1_STRDEDUP_PERCENT_FORMAT_NS ", Cached: " UINTX_FORMAT ", Added: " UINTX_FORMAT ", Removed: " UINTX_FORMAT "]\n"
601 " [Resize Count: " UINTX_FORMAT ", Shrink Threshold: " UINTX_FORMAT "(" G1_STRDEDUP_PERCENT_FORMAT_NS "), Grow Threshold: " UINTX_FORMAT "(" G1_STRDEDUP_PERCENT_FORMAT_NS ")]\n"
602 " [Rehash Count: " UINTX_FORMAT ", Rehash Threshold: " UINTX_FORMAT ", Hash Seed: " UINT64_FORMAT "]\n"
603 " [Age Threshold: " UINTX_FORMAT "]",
604 G1_STRDEDUP_BYTES_PARAM(_table->_size * sizeof(G1StringDedupEntry*) + (_table->_entries + _entry_cache->size()) * sizeof(G1StringDedupEntry)),
605 _table->_size, _min_size, _max_size,
606 _table->_entries, (double)_table->_entries / (double)_table->_size * 100.0, _entry_cache->size(), _entries_added, _entries_removed,
607 _resize_count, _table->_shrink_threshold, _shrink_load_factor * 100.0, _table->_grow_threshold, _grow_load_factor * 100.0,
608 _rehash_count, _rehash_threshold, _table->_hash_seed,
609 StringDeduplicationAgeThreshold);
610 }