aoqi@0: /* aoqi@0: * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #ifndef SHARE_VM_GC_IMPLEMENTATION_G1_G1STRINGDEDUP_HPP aoqi@0: #define SHARE_VM_GC_IMPLEMENTATION_G1_G1STRINGDEDUP_HPP aoqi@0: aoqi@0: // aoqi@0: // String Deduplication aoqi@0: // aoqi@0: // String deduplication aims to reduce the heap live-set by deduplicating identical aoqi@0: // instances of String so that they share the same backing character array. aoqi@0: // aoqi@0: // The deduplication process is divided in two main parts, 1) finding the objects to aoqi@0: // deduplicate, and 2) deduplicating those objects. The first part is done as part of aoqi@0: // a normal GC cycle when objects are marked or evacuated. At this time a check is aoqi@0: // applied on each object to check if it is a candidate for deduplication. If so, the aoqi@0: // object is placed on the deduplication queue for later processing. The second part, aoqi@0: // processing the objects on the deduplication queue, is a concurrent phase which aoqi@0: // starts right after the stop-the-wold marking/evacuation phase. This phase is aoqi@0: // executed by the deduplication thread, which pulls deduplication candidates of the aoqi@0: // deduplication queue and tries to deduplicate them. aoqi@0: // aoqi@0: // A deduplication hashtable is used to keep track of all unique character arrays aoqi@0: // used by String objects. When deduplicating, a lookup is made in this table to see aoqi@0: // if there is already an identical character array somewhere on the heap. If so, the aoqi@0: // String object is adjusted to point to that character array, releasing the reference aoqi@0: // to the original array allowing it to eventually be garbage collected. If the lookup aoqi@0: // fails the character array is instead inserted into the hashtable so that this array aoqi@0: // can be shared at some point in the future. aoqi@0: // aoqi@0: // Candidate selection aoqi@0: // aoqi@0: // An object is considered a deduplication candidate if all of the following aoqi@0: // statements are true: aoqi@0: // aoqi@0: // - The object is an instance of java.lang.String aoqi@0: // aoqi@0: // - The object is being evacuated from a young heap region aoqi@0: // aoqi@0: // - The object is being evacuated to a young/survivor heap region and the aoqi@0: // object's age is equal to the deduplication age threshold aoqi@0: // aoqi@0: // or aoqi@0: // aoqi@0: // The object is being evacuated to an old heap region and the object's age is aoqi@0: // less than the deduplication age threshold aoqi@0: // aoqi@0: // Once an string object has been promoted to an old region, or its age is higher aoqi@0: // than the deduplication age threshold, is will never become a candidate again. aoqi@0: // This approach avoids making the same object a candidate more than once. aoqi@0: // aoqi@0: // Interned strings are a bit special. They are explicitly deduplicated just before aoqi@0: // being inserted into the StringTable (to avoid counteracting C2 optimizations done aoqi@0: // on string literals), then they also become deduplication candidates if they reach aoqi@0: // the deduplication age threshold or are evacuated to an old heap region. The second aoqi@0: // attempt to deduplicate such strings will be in vain, but we have no fast way of aoqi@0: // filtering them out. This has not shown to be a problem, as the number of interned aoqi@0: // strings is usually dwarfed by the number of normal (non-interned) strings. aoqi@0: // aoqi@0: // For additional information on string deduplication, please see JEP 192, aoqi@0: // http://openjdk.java.net/jeps/192 aoqi@0: // aoqi@0: aoqi@0: #include "memory/allocation.hpp" aoqi@0: #include "oops/oop.hpp" aoqi@0: aoqi@0: class OopClosure; aoqi@0: class BoolObjectClosure; aoqi@0: class ThreadClosure; aoqi@0: class outputStream; aoqi@0: class G1StringDedupTable; aoqi@0: aoqi@0: // aoqi@0: // Main interface for interacting with string deduplication. aoqi@0: // aoqi@0: class G1StringDedup : public AllStatic { aoqi@0: private: aoqi@0: // Single state for checking if both G1 and string deduplication is enabled. aoqi@0: static bool _enabled; aoqi@0: aoqi@0: // Candidate selection policies, returns true if the given object is aoqi@0: // candidate for string deduplication. aoqi@0: static bool is_candidate_from_mark(oop obj); aoqi@0: static bool is_candidate_from_evacuation(bool from_young, bool to_young, oop obj); aoqi@0: aoqi@0: public: aoqi@0: // Returns true if both G1 and string deduplication is enabled. aoqi@0: static bool is_enabled() { aoqi@0: return _enabled; aoqi@0: } aoqi@0: aoqi@0: // Initialize string deduplication. aoqi@0: static void initialize(); aoqi@0: aoqi@0: // Stop the deduplication thread. aoqi@0: static void stop(); aoqi@0: aoqi@0: // Immediately deduplicates the given String object, bypassing the aoqi@0: // the deduplication queue. aoqi@0: static void deduplicate(oop java_string); aoqi@0: aoqi@0: // Enqueues a deduplication candidate for later processing by the deduplication aoqi@0: // thread. Before enqueuing, these functions apply the appropriate candidate aoqi@0: // selection policy to filters out non-candidates. aoqi@0: static void enqueue_from_mark(oop java_string); aoqi@0: static void enqueue_from_evacuation(bool from_young, bool to_young, aoqi@0: unsigned int queue, oop java_string); aoqi@0: aoqi@0: static void oops_do(OopClosure* keep_alive); aoqi@0: static void unlink(BoolObjectClosure* is_alive); aoqi@0: static void unlink_or_oops_do(BoolObjectClosure* is_alive, OopClosure* keep_alive, aoqi@0: bool allow_resize_and_rehash = true); aoqi@0: aoqi@0: static void threads_do(ThreadClosure* tc); aoqi@0: static void print_worker_threads_on(outputStream* st); aoqi@0: static void verify(); aoqi@0: }; aoqi@0: aoqi@0: // aoqi@0: // This closure encapsulates the state and the closures needed when scanning aoqi@0: // the deduplication queue and table during the unlink_or_oops_do() operation. aoqi@0: // A single instance of this closure is created and then shared by all worker aoqi@0: // threads participating in the scan. The _next_queue and _next_bucket fields aoqi@0: // provide a simple mechanism for GC workers to claim exclusive access to a aoqi@0: // queue or a table partition. aoqi@0: // aoqi@0: class G1StringDedupUnlinkOrOopsDoClosure : public StackObj { aoqi@0: private: aoqi@0: BoolObjectClosure* _is_alive; aoqi@0: OopClosure* _keep_alive; aoqi@0: G1StringDedupTable* _resized_table; aoqi@0: G1StringDedupTable* _rehashed_table; aoqi@0: size_t _next_queue; aoqi@0: size_t _next_bucket; aoqi@0: aoqi@0: public: aoqi@0: G1StringDedupUnlinkOrOopsDoClosure(BoolObjectClosure* is_alive, aoqi@0: OopClosure* keep_alive, aoqi@0: bool allow_resize_and_rehash); aoqi@0: ~G1StringDedupUnlinkOrOopsDoClosure(); aoqi@0: aoqi@0: bool is_resizing() { aoqi@0: return _resized_table != NULL; aoqi@0: } aoqi@0: aoqi@0: G1StringDedupTable* resized_table() { aoqi@0: return _resized_table; aoqi@0: } aoqi@0: aoqi@0: bool is_rehashing() { aoqi@0: return _rehashed_table != NULL; aoqi@0: } aoqi@0: aoqi@0: // Atomically claims the next available queue for exclusive access by aoqi@0: // the current thread. Returns the queue number of the claimed queue. aoqi@0: size_t claim_queue() { aoqi@0: return (size_t)Atomic::add_ptr(1, &_next_queue) - 1; aoqi@0: } aoqi@0: aoqi@0: // Atomically claims the next available table partition for exclusive aoqi@0: // access by the current thread. Returns the table bucket number where aoqi@0: // the claimed partition starts. aoqi@0: size_t claim_table_partition(size_t partition_size) { aoqi@0: return (size_t)Atomic::add_ptr(partition_size, &_next_bucket) - partition_size; aoqi@0: } aoqi@0: aoqi@0: // Applies and returns the result from the is_alive closure, or aoqi@0: // returns true if no such closure was provided. aoqi@0: bool is_alive(oop o) { aoqi@0: if (_is_alive != NULL) { aoqi@0: return _is_alive->do_object_b(o); aoqi@0: } aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: // Applies the keep_alive closure, or does nothing if no such aoqi@0: // closure was provided. aoqi@0: void keep_alive(oop* p) { aoqi@0: if (_keep_alive != NULL) { aoqi@0: _keep_alive->do_oop(p); aoqi@0: } aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: #endif // SHARE_VM_GC_IMPLEMENTATION_G1_G1STRINGDEDUP_HPP