src/share/vm/gc_implementation/g1/g1StringDedup.hpp

Wed, 27 Apr 2016 01:25:04 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:25:04 +0800
changeset 0
f90c822e73f8
child 6876
710a3c8b516e
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/
changeset: 6782:28b50d07f6f8
tag: jdk8u25-b17

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #ifndef SHARE_VM_GC_IMPLEMENTATION_G1_G1STRINGDEDUP_HPP
aoqi@0 26 #define SHARE_VM_GC_IMPLEMENTATION_G1_G1STRINGDEDUP_HPP
aoqi@0 27
aoqi@0 28 //
aoqi@0 29 // String Deduplication
aoqi@0 30 //
aoqi@0 31 // String deduplication aims to reduce the heap live-set by deduplicating identical
aoqi@0 32 // instances of String so that they share the same backing character array.
aoqi@0 33 //
aoqi@0 34 // The deduplication process is divided in two main parts, 1) finding the objects to
aoqi@0 35 // deduplicate, and 2) deduplicating those objects. The first part is done as part of
aoqi@0 36 // a normal GC cycle when objects are marked or evacuated. At this time a check is
aoqi@0 37 // applied on each object to check if it is a candidate for deduplication. If so, the
aoqi@0 38 // object is placed on the deduplication queue for later processing. The second part,
aoqi@0 39 // processing the objects on the deduplication queue, is a concurrent phase which
aoqi@0 40 // starts right after the stop-the-wold marking/evacuation phase. This phase is
aoqi@0 41 // executed by the deduplication thread, which pulls deduplication candidates of the
aoqi@0 42 // deduplication queue and tries to deduplicate them.
aoqi@0 43 //
aoqi@0 44 // A deduplication hashtable is used to keep track of all unique character arrays
aoqi@0 45 // used by String objects. When deduplicating, a lookup is made in this table to see
aoqi@0 46 // if there is already an identical character array somewhere on the heap. If so, the
aoqi@0 47 // String object is adjusted to point to that character array, releasing the reference
aoqi@0 48 // to the original array allowing it to eventually be garbage collected. If the lookup
aoqi@0 49 // fails the character array is instead inserted into the hashtable so that this array
aoqi@0 50 // can be shared at some point in the future.
aoqi@0 51 //
aoqi@0 52 // Candidate selection
aoqi@0 53 //
aoqi@0 54 // An object is considered a deduplication candidate if all of the following
aoqi@0 55 // statements are true:
aoqi@0 56 //
aoqi@0 57 // - The object is an instance of java.lang.String
aoqi@0 58 //
aoqi@0 59 // - The object is being evacuated from a young heap region
aoqi@0 60 //
aoqi@0 61 // - The object is being evacuated to a young/survivor heap region and the
aoqi@0 62 // object's age is equal to the deduplication age threshold
aoqi@0 63 //
aoqi@0 64 // or
aoqi@0 65 //
aoqi@0 66 // The object is being evacuated to an old heap region and the object's age is
aoqi@0 67 // less than the deduplication age threshold
aoqi@0 68 //
aoqi@0 69 // Once an string object has been promoted to an old region, or its age is higher
aoqi@0 70 // than the deduplication age threshold, is will never become a candidate again.
aoqi@0 71 // This approach avoids making the same object a candidate more than once.
aoqi@0 72 //
aoqi@0 73 // Interned strings are a bit special. They are explicitly deduplicated just before
aoqi@0 74 // being inserted into the StringTable (to avoid counteracting C2 optimizations done
aoqi@0 75 // on string literals), then they also become deduplication candidates if they reach
aoqi@0 76 // the deduplication age threshold or are evacuated to an old heap region. The second
aoqi@0 77 // attempt to deduplicate such strings will be in vain, but we have no fast way of
aoqi@0 78 // filtering them out. This has not shown to be a problem, as the number of interned
aoqi@0 79 // strings is usually dwarfed by the number of normal (non-interned) strings.
aoqi@0 80 //
aoqi@0 81 // For additional information on string deduplication, please see JEP 192,
aoqi@0 82 // http://openjdk.java.net/jeps/192
aoqi@0 83 //
aoqi@0 84
aoqi@0 85 #include "memory/allocation.hpp"
aoqi@0 86 #include "oops/oop.hpp"
aoqi@0 87
aoqi@0 88 class OopClosure;
aoqi@0 89 class BoolObjectClosure;
aoqi@0 90 class ThreadClosure;
aoqi@0 91 class outputStream;
aoqi@0 92 class G1StringDedupTable;
aoqi@0 93
aoqi@0 94 //
aoqi@0 95 // Main interface for interacting with string deduplication.
aoqi@0 96 //
aoqi@0 97 class G1StringDedup : public AllStatic {
aoqi@0 98 private:
aoqi@0 99 // Single state for checking if both G1 and string deduplication is enabled.
aoqi@0 100 static bool _enabled;
aoqi@0 101
aoqi@0 102 // Candidate selection policies, returns true if the given object is
aoqi@0 103 // candidate for string deduplication.
aoqi@0 104 static bool is_candidate_from_mark(oop obj);
aoqi@0 105 static bool is_candidate_from_evacuation(bool from_young, bool to_young, oop obj);
aoqi@0 106
aoqi@0 107 public:
aoqi@0 108 // Returns true if both G1 and string deduplication is enabled.
aoqi@0 109 static bool is_enabled() {
aoqi@0 110 return _enabled;
aoqi@0 111 }
aoqi@0 112
aoqi@0 113 // Initialize string deduplication.
aoqi@0 114 static void initialize();
aoqi@0 115
aoqi@0 116 // Stop the deduplication thread.
aoqi@0 117 static void stop();
aoqi@0 118
aoqi@0 119 // Immediately deduplicates the given String object, bypassing the
aoqi@0 120 // the deduplication queue.
aoqi@0 121 static void deduplicate(oop java_string);
aoqi@0 122
aoqi@0 123 // Enqueues a deduplication candidate for later processing by the deduplication
aoqi@0 124 // thread. Before enqueuing, these functions apply the appropriate candidate
aoqi@0 125 // selection policy to filters out non-candidates.
aoqi@0 126 static void enqueue_from_mark(oop java_string);
aoqi@0 127 static void enqueue_from_evacuation(bool from_young, bool to_young,
aoqi@0 128 unsigned int queue, oop java_string);
aoqi@0 129
aoqi@0 130 static void oops_do(OopClosure* keep_alive);
aoqi@0 131 static void unlink(BoolObjectClosure* is_alive);
aoqi@0 132 static void unlink_or_oops_do(BoolObjectClosure* is_alive, OopClosure* keep_alive,
aoqi@0 133 bool allow_resize_and_rehash = true);
aoqi@0 134
aoqi@0 135 static void threads_do(ThreadClosure* tc);
aoqi@0 136 static void print_worker_threads_on(outputStream* st);
aoqi@0 137 static void verify();
aoqi@0 138 };
aoqi@0 139
aoqi@0 140 //
aoqi@0 141 // This closure encapsulates the state and the closures needed when scanning
aoqi@0 142 // the deduplication queue and table during the unlink_or_oops_do() operation.
aoqi@0 143 // A single instance of this closure is created and then shared by all worker
aoqi@0 144 // threads participating in the scan. The _next_queue and _next_bucket fields
aoqi@0 145 // provide a simple mechanism for GC workers to claim exclusive access to a
aoqi@0 146 // queue or a table partition.
aoqi@0 147 //
aoqi@0 148 class G1StringDedupUnlinkOrOopsDoClosure : public StackObj {
aoqi@0 149 private:
aoqi@0 150 BoolObjectClosure* _is_alive;
aoqi@0 151 OopClosure* _keep_alive;
aoqi@0 152 G1StringDedupTable* _resized_table;
aoqi@0 153 G1StringDedupTable* _rehashed_table;
aoqi@0 154 size_t _next_queue;
aoqi@0 155 size_t _next_bucket;
aoqi@0 156
aoqi@0 157 public:
aoqi@0 158 G1StringDedupUnlinkOrOopsDoClosure(BoolObjectClosure* is_alive,
aoqi@0 159 OopClosure* keep_alive,
aoqi@0 160 bool allow_resize_and_rehash);
aoqi@0 161 ~G1StringDedupUnlinkOrOopsDoClosure();
aoqi@0 162
aoqi@0 163 bool is_resizing() {
aoqi@0 164 return _resized_table != NULL;
aoqi@0 165 }
aoqi@0 166
aoqi@0 167 G1StringDedupTable* resized_table() {
aoqi@0 168 return _resized_table;
aoqi@0 169 }
aoqi@0 170
aoqi@0 171 bool is_rehashing() {
aoqi@0 172 return _rehashed_table != NULL;
aoqi@0 173 }
aoqi@0 174
aoqi@0 175 // Atomically claims the next available queue for exclusive access by
aoqi@0 176 // the current thread. Returns the queue number of the claimed queue.
aoqi@0 177 size_t claim_queue() {
aoqi@0 178 return (size_t)Atomic::add_ptr(1, &_next_queue) - 1;
aoqi@0 179 }
aoqi@0 180
aoqi@0 181 // Atomically claims the next available table partition for exclusive
aoqi@0 182 // access by the current thread. Returns the table bucket number where
aoqi@0 183 // the claimed partition starts.
aoqi@0 184 size_t claim_table_partition(size_t partition_size) {
aoqi@0 185 return (size_t)Atomic::add_ptr(partition_size, &_next_bucket) - partition_size;
aoqi@0 186 }
aoqi@0 187
aoqi@0 188 // Applies and returns the result from the is_alive closure, or
aoqi@0 189 // returns true if no such closure was provided.
aoqi@0 190 bool is_alive(oop o) {
aoqi@0 191 if (_is_alive != NULL) {
aoqi@0 192 return _is_alive->do_object_b(o);
aoqi@0 193 }
aoqi@0 194 return true;
aoqi@0 195 }
aoqi@0 196
aoqi@0 197 // Applies the keep_alive closure, or does nothing if no such
aoqi@0 198 // closure was provided.
aoqi@0 199 void keep_alive(oop* p) {
aoqi@0 200 if (_keep_alive != NULL) {
aoqi@0 201 _keep_alive->do_oop(p);
aoqi@0 202 }
aoqi@0 203 }
aoqi@0 204 };
aoqi@0 205
aoqi@0 206 #endif // SHARE_VM_GC_IMPLEMENTATION_G1_G1STRINGDEDUP_HPP

mercurial