src/hotspot/share/gc/g1/g1StringDedup.hpp
changeset 47216 71c04702a3d5
parent 46348 5803de68c14d
child 47885 5caa1d5f74c1
equal deleted inserted replaced
47215:4ebc2e2fb97c 47216:71c04702a3d5
       
     1 /*
       
     2  * Copyright (c) 2014, 2017, 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 #ifndef SHARE_VM_GC_G1_G1STRINGDEDUP_HPP
       
    26 #define SHARE_VM_GC_G1_G1STRINGDEDUP_HPP
       
    27 
       
    28 //
       
    29 // String Deduplication
       
    30 //
       
    31 // String deduplication aims to reduce the heap live-set by deduplicating identical
       
    32 // instances of String so that they share the same backing character array.
       
    33 //
       
    34 // The deduplication process is divided in two main parts, 1) finding the objects to
       
    35 // deduplicate, and 2) deduplicating those objects. The first part is done as part of
       
    36 // a normal GC cycle when objects are marked or evacuated. At this time a check is
       
    37 // applied on each object to check if it is a candidate for deduplication. If so, the
       
    38 // object is placed on the deduplication queue for later processing. The second part,
       
    39 // processing the objects on the deduplication queue, is a concurrent phase which
       
    40 // starts right after the stop-the-wold marking/evacuation phase. This phase is
       
    41 // executed by the deduplication thread, which pulls deduplication candidates of the
       
    42 // deduplication queue and tries to deduplicate them.
       
    43 //
       
    44 // A deduplication hashtable is used to keep track of all unique character arrays
       
    45 // used by String objects. When deduplicating, a lookup is made in this table to see
       
    46 // if there is already an identical character array somewhere on the heap. If so, the
       
    47 // String object is adjusted to point to that character array, releasing the reference
       
    48 // to the original array allowing it to eventually be garbage collected. If the lookup
       
    49 // fails the character array is instead inserted into the hashtable so that this array
       
    50 // can be shared at some point in the future.
       
    51 //
       
    52 // Candidate selection
       
    53 //
       
    54 // An object is considered a deduplication candidate if all of the following
       
    55 // statements are true:
       
    56 //
       
    57 // - The object is an instance of java.lang.String
       
    58 //
       
    59 // - The object is being evacuated from a young heap region
       
    60 //
       
    61 // - The object is being evacuated to a young/survivor heap region and the
       
    62 //   object's age is equal to the deduplication age threshold
       
    63 //
       
    64 //   or
       
    65 //
       
    66 //   The object is being evacuated to an old heap region and the object's age is
       
    67 //   less than the deduplication age threshold
       
    68 //
       
    69 // Once an string object has been promoted to an old region, or its age is higher
       
    70 // than the deduplication age threshold, is will never become a candidate again.
       
    71 // This approach avoids making the same object a candidate more than once.
       
    72 //
       
    73 // Interned strings are a bit special. They are explicitly deduplicated just before
       
    74 // being inserted into the StringTable (to avoid counteracting C2 optimizations done
       
    75 // on string literals), then they also become deduplication candidates if they reach
       
    76 // the deduplication age threshold or are evacuated to an old heap region. The second
       
    77 // attempt to deduplicate such strings will be in vain, but we have no fast way of
       
    78 // filtering them out. This has not shown to be a problem, as the number of interned
       
    79 // strings is usually dwarfed by the number of normal (non-interned) strings.
       
    80 //
       
    81 // For additional information on string deduplication, please see JEP 192,
       
    82 // http://openjdk.java.net/jeps/192
       
    83 //
       
    84 
       
    85 #include "memory/allocation.hpp"
       
    86 #include "oops/oop.hpp"
       
    87 
       
    88 class OopClosure;
       
    89 class BoolObjectClosure;
       
    90 class ThreadClosure;
       
    91 class outputStream;
       
    92 class G1StringDedupTable;
       
    93 class G1StringDedupUnlinkOrOopsDoClosure;
       
    94 class G1GCPhaseTimes;
       
    95 
       
    96 //
       
    97 // Main interface for interacting with string deduplication.
       
    98 //
       
    99 class G1StringDedup : public AllStatic {
       
   100 private:
       
   101   // Single state for checking if both G1 and string deduplication is enabled.
       
   102   static bool _enabled;
       
   103 
       
   104   // Candidate selection policies, returns true if the given object is
       
   105   // candidate for string deduplication.
       
   106   static bool is_candidate_from_mark(oop obj);
       
   107   static bool is_candidate_from_evacuation(bool from_young, bool to_young, oop obj);
       
   108 
       
   109 public:
       
   110   // Returns true if both G1 and string deduplication is enabled.
       
   111   static bool is_enabled() {
       
   112     return _enabled;
       
   113   }
       
   114 
       
   115   // Initialize string deduplication.
       
   116   static void initialize();
       
   117 
       
   118   // Stop the deduplication thread.
       
   119   static void stop();
       
   120 
       
   121   // Immediately deduplicates the given String object, bypassing the
       
   122   // the deduplication queue.
       
   123   static void deduplicate(oop java_string);
       
   124 
       
   125   // Enqueues a deduplication candidate for later processing by the deduplication
       
   126   // thread. Before enqueuing, these functions apply the appropriate candidate
       
   127   // selection policy to filters out non-candidates.
       
   128   static void enqueue_from_mark(oop java_string);
       
   129   static void enqueue_from_evacuation(bool from_young, bool to_young,
       
   130                                       unsigned int queue, oop java_string);
       
   131 
       
   132   static void oops_do(OopClosure* keep_alive);
       
   133   static void parallel_unlink(G1StringDedupUnlinkOrOopsDoClosure* unlink, uint worker_id);
       
   134   static void unlink_or_oops_do(BoolObjectClosure* is_alive, OopClosure* keep_alive,
       
   135                                 bool allow_resize_and_rehash, G1GCPhaseTimes* phase_times = NULL);
       
   136 
       
   137   static void threads_do(ThreadClosure* tc);
       
   138   static void print_worker_threads_on(outputStream* st);
       
   139   static void verify();
       
   140 };
       
   141 
       
   142 //
       
   143 // This closure encapsulates the state and the closures needed when scanning
       
   144 // the deduplication queue and table during the unlink_or_oops_do() operation.
       
   145 // A single instance of this closure is created and then shared by all worker
       
   146 // threads participating in the scan. The _next_queue and _next_bucket fields
       
   147 // provide a simple mechanism for GC workers to claim exclusive access to a
       
   148 // queue or a table partition.
       
   149 //
       
   150 class G1StringDedupUnlinkOrOopsDoClosure : public StackObj {
       
   151 private:
       
   152   BoolObjectClosure*  _is_alive;
       
   153   OopClosure*         _keep_alive;
       
   154   G1StringDedupTable* _resized_table;
       
   155   G1StringDedupTable* _rehashed_table;
       
   156   size_t              _next_queue;
       
   157   size_t              _next_bucket;
       
   158 
       
   159 public:
       
   160   G1StringDedupUnlinkOrOopsDoClosure(BoolObjectClosure* is_alive,
       
   161                                      OopClosure* keep_alive,
       
   162                                      bool allow_resize_and_rehash);
       
   163   ~G1StringDedupUnlinkOrOopsDoClosure();
       
   164 
       
   165   bool is_resizing() {
       
   166     return _resized_table != NULL;
       
   167   }
       
   168 
       
   169   G1StringDedupTable* resized_table() {
       
   170     return _resized_table;
       
   171   }
       
   172 
       
   173   bool is_rehashing() {
       
   174     return _rehashed_table != NULL;
       
   175   }
       
   176 
       
   177   // Atomically claims the next available queue for exclusive access by
       
   178   // the current thread. Returns the queue number of the claimed queue.
       
   179   size_t claim_queue();
       
   180 
       
   181   // Atomically claims the next available table partition for exclusive
       
   182   // access by the current thread. Returns the table bucket number where
       
   183   // the claimed partition starts.
       
   184   size_t claim_table_partition(size_t partition_size);
       
   185 
       
   186   // Applies and returns the result from the is_alive closure, or
       
   187   // returns true if no such closure was provided.
       
   188   bool is_alive(oop o) {
       
   189     if (_is_alive != NULL) {
       
   190       return _is_alive->do_object_b(o);
       
   191     }
       
   192     return true;
       
   193   }
       
   194 
       
   195   // Applies the keep_alive closure, or does nothing if no such
       
   196   // closure was provided.
       
   197   void keep_alive(oop* p) {
       
   198     if (_keep_alive != NULL) {
       
   199       _keep_alive->do_oop(p);
       
   200     }
       
   201   }
       
   202 };
       
   203 
       
   204 #endif // SHARE_VM_GC_G1_G1STRINGDEDUP_HPP