1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/vm/gc_implementation/g1/g1StringDedup.hpp Tue Mar 18 19:07:22 2014 +0100 1.3 @@ -0,0 +1,202 @@ 1.4 +/* 1.5 + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + * 1.26 + */ 1.27 + 1.28 +#ifndef SHARE_VM_GC_IMPLEMENTATION_G1_G1STRINGDEDUP_HPP 1.29 +#define SHARE_VM_GC_IMPLEMENTATION_G1_G1STRINGDEDUP_HPP 1.30 + 1.31 +// 1.32 +// String Deduplication 1.33 +// 1.34 +// String deduplication aims to reduce the heap live-set by deduplicating identical 1.35 +// instances of String so that they share the same backing character array. 1.36 +// 1.37 +// The deduplication process is divided in two main parts, 1) finding the objects to 1.38 +// deduplicate, and 2) deduplicating those objects. The first part is done as part of 1.39 +// a normal GC cycle when objects are marked or evacuated. At this time a check is 1.40 +// applied on each object to check if it is a candidate for deduplication. If so, the 1.41 +// object is placed on the deduplication queue for later processing. The second part, 1.42 +// processing the objects on the deduplication queue, is a concurrent phase which 1.43 +// starts right after the stop-the-wold marking/evacuation phase. This phase is 1.44 +// executed by the deduplication thread, which pulls deduplication candidates of the 1.45 +// deduplication queue and tries to deduplicate them. 1.46 +// 1.47 +// A deduplication hashtable is used to keep track of all unique character arrays 1.48 +// used by String objects. When deduplicating, a lookup is made in this table to see 1.49 +// if there is already an identical character array somewhere on the heap. If so, the 1.50 +// String object is adjusted to point to that character array, releasing the reference 1.51 +// to the original array allowing it to eventually be garbage collected. If the lookup 1.52 +// fails the character array is instead inserted into the hashtable so that this array 1.53 +// can be shared at some point in the future. 1.54 +// 1.55 +// Candidate selection 1.56 +// 1.57 +// An object is considered a deduplication candidate if all of the following 1.58 +// statements are true: 1.59 +// 1.60 +// - The object is an instance of java.lang.String 1.61 +// 1.62 +// - The object is being evacuated from a young heap region 1.63 +// 1.64 +// - The object is being evacuated to a young/survivor heap region and the 1.65 +// object's age is equal to the deduplication age threshold 1.66 +// 1.67 +// or 1.68 +// 1.69 +// The object is being evacuated to an old heap region and the object's age is 1.70 +// less than the deduplication age threshold 1.71 +// 1.72 +// Once an string object has been promoted to an old region, or its age is higher 1.73 +// than the deduplication age threshold, is will never become a candidate again. 1.74 +// This approach avoids making the same object a candidate more than once. 1.75 +// 1.76 +// Interned strings are a bit special. They are explicitly deduplicated just before 1.77 +// being inserted into the StringTable (to avoid counteracting C2 optimizations done 1.78 +// on string literals), then they also become deduplication candidates if they reach 1.79 +// the deduplication age threshold or are evacuated to an old heap region. The second 1.80 +// attempt to deduplicate such strings will be in vain, but we have no fast way of 1.81 +// filtering them out. This has not shown to be a problem, as the number of interned 1.82 +// strings is usually dwarfed by the number of normal (non-interned) strings. 1.83 +// 1.84 +// For additional information on string deduplication, please see JEP 192, 1.85 +// http://openjdk.java.net/jeps/192 1.86 +// 1.87 + 1.88 +#include "memory/allocation.hpp" 1.89 +#include "oops/oop.hpp" 1.90 + 1.91 +class OopClosure; 1.92 +class BoolObjectClosure; 1.93 +class ThreadClosure; 1.94 +class outputStream; 1.95 +class G1StringDedupTable; 1.96 + 1.97 +// 1.98 +// Main interface for interacting with string deduplication. 1.99 +// 1.100 +class G1StringDedup : public AllStatic { 1.101 +private: 1.102 + // Single state for checking if both G1 and string deduplication is enabled. 1.103 + static bool _enabled; 1.104 + 1.105 + // Candidate selection policies, returns true if the given object is 1.106 + // candidate for string deduplication. 1.107 + static bool is_candidate_from_mark(oop obj); 1.108 + static bool is_candidate_from_evacuation(bool from_young, bool to_young, oop obj); 1.109 + 1.110 +public: 1.111 + // Returns true if both G1 and string deduplication is enabled. 1.112 + static bool is_enabled() { 1.113 + return _enabled; 1.114 + } 1.115 + 1.116 + static void initialize(); 1.117 + 1.118 + // Immediately deduplicates the given String object, bypassing the 1.119 + // the deduplication queue. 1.120 + static void deduplicate(oop java_string); 1.121 + 1.122 + // Enqueues a deduplication candidate for later processing by the deduplication 1.123 + // thread. Before enqueuing, these functions apply the appropriate candidate 1.124 + // selection policy to filters out non-candidates. 1.125 + static void enqueue_from_mark(oop java_string); 1.126 + static void enqueue_from_evacuation(bool from_young, bool to_young, 1.127 + unsigned int queue, oop java_string); 1.128 + 1.129 + static void oops_do(OopClosure* keep_alive); 1.130 + static void unlink(BoolObjectClosure* is_alive); 1.131 + static void unlink_or_oops_do(BoolObjectClosure* is_alive, OopClosure* keep_alive, 1.132 + bool allow_resize_and_rehash = true); 1.133 + 1.134 + static void threads_do(ThreadClosure* tc); 1.135 + static void print_worker_threads_on(outputStream* st); 1.136 + static void verify(); 1.137 +}; 1.138 + 1.139 +// 1.140 +// This closure encapsulates the state and the closures needed when scanning 1.141 +// the deduplication queue and table during the unlink_or_oops_do() operation. 1.142 +// A single instance of this closure is created and then shared by all worker 1.143 +// threads participating in the scan. The _next_queue and _next_bucket fields 1.144 +// provide a simple mechanism for GC workers to claim exclusive access to a 1.145 +// queue or a table partition. 1.146 +// 1.147 +class G1StringDedupUnlinkOrOopsDoClosure : public StackObj { 1.148 +private: 1.149 + BoolObjectClosure* _is_alive; 1.150 + OopClosure* _keep_alive; 1.151 + G1StringDedupTable* _resized_table; 1.152 + G1StringDedupTable* _rehashed_table; 1.153 + size_t _next_queue; 1.154 + size_t _next_bucket; 1.155 + 1.156 +public: 1.157 + G1StringDedupUnlinkOrOopsDoClosure(BoolObjectClosure* is_alive, 1.158 + OopClosure* keep_alive, 1.159 + bool allow_resize_and_rehash); 1.160 + ~G1StringDedupUnlinkOrOopsDoClosure(); 1.161 + 1.162 + bool is_resizing() { 1.163 + return _resized_table != NULL; 1.164 + } 1.165 + 1.166 + G1StringDedupTable* resized_table() { 1.167 + return _resized_table; 1.168 + } 1.169 + 1.170 + bool is_rehashing() { 1.171 + return _rehashed_table != NULL; 1.172 + } 1.173 + 1.174 + // Atomically claims the next available queue for exclusive access by 1.175 + // the current thread. Returns the queue number of the claimed queue. 1.176 + size_t claim_queue() { 1.177 + return (size_t)Atomic::add_ptr(1, &_next_queue) - 1; 1.178 + } 1.179 + 1.180 + // Atomically claims the next available table partition for exclusive 1.181 + // access by the current thread. Returns the table bucket number where 1.182 + // the claimed partition starts. 1.183 + size_t claim_table_partition(size_t partition_size) { 1.184 + return (size_t)Atomic::add_ptr(partition_size, &_next_bucket) - partition_size; 1.185 + } 1.186 + 1.187 + // Applies and returns the result from the is_alive closure, or 1.188 + // returns true if no such closure was provided. 1.189 + bool is_alive(oop o) { 1.190 + if (_is_alive != NULL) { 1.191 + return _is_alive->do_object_b(o); 1.192 + } 1.193 + return true; 1.194 + } 1.195 + 1.196 + // Applies the keep_alive closure, or does nothing if no such 1.197 + // closure was provided. 1.198 + void keep_alive(oop* p) { 1.199 + if (_keep_alive != NULL) { 1.200 + _keep_alive->do_oop(p); 1.201 + } 1.202 + } 1.203 +}; 1.204 + 1.205 +#endif // SHARE_VM_GC_IMPLEMENTATION_G1_G1STRINGDEDUP_HPP