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