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