< prev index next >

src/hotspot/share/gc/shared/stringdedup/stringDedup.cpp

Print this page




   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 #include "precompiled.hpp"
  26 #include "classfile/javaClasses.inline.hpp"
  27 #include "gc/g1/g1CollectedHeap.inline.hpp"
  28 #include "gc/g1/g1GCPhaseTimes.hpp"
  29 #include "gc/g1/g1StringDedup.hpp"
  30 #include "gc/g1/g1StringDedupQueue.hpp"
  31 #include "gc/g1/g1StringDedupStat.hpp"
  32 #include "gc/g1/g1StringDedupTable.hpp"
  33 #include "gc/g1/g1StringDedupThread.hpp"
  34 #include "oops/oop.inline.hpp"
  35 #include "runtime/atomic.hpp"
  36 
  37 bool G1StringDedup::_enabled = false;



  38 
  39 void G1StringDedup::initialize() {
  40   assert(UseG1GC, "String deduplication only available with G1");
  41   if (UseStringDeduplication) {
  42     _enabled = true;
  43     G1StringDedupQueue::create();
  44     G1StringDedupTable::create();
  45     G1StringDedupThread::create();
  46   }
  47 }
  48 
  49 void G1StringDedup::stop() {
  50   assert(is_enabled(), "String deduplication not enabled");
  51   G1StringDedupThread::thread()->stop();
  52 }
  53 
  54 bool G1StringDedup::is_candidate_from_mark(oop obj) {
  55   if (java_lang_String::is_instance_inlined(obj)) {
  56     bool from_young = G1CollectedHeap::heap()->heap_region_containing(obj)->is_young();
  57     if (from_young && obj->age() < StringDeduplicationAgeThreshold) {
  58       // Candidate found. String is being evacuated from young to old but has not
  59       // reached the deduplication age threshold, i.e. has not previously been a
  60       // candidate during its life in the young generation.
  61       return true;
  62     }
  63   }
  64 
  65   // Not a candidate
  66   return false;
  67 }
  68 
  69 void G1StringDedup::enqueue_from_mark(oop java_string, uint worker_id) {
  70   assert(is_enabled(), "String deduplication not enabled");
  71   if (is_candidate_from_mark(java_string)) {
  72     G1StringDedupQueue::push(worker_id, java_string);
  73   }
  74 }
  75 
  76 bool G1StringDedup::is_candidate_from_evacuation(bool from_young, bool to_young, oop obj) {
  77   if (from_young && java_lang_String::is_instance_inlined(obj)) {
  78     if (to_young && obj->age() == StringDeduplicationAgeThreshold) {
  79       // Candidate found. String is being evacuated from young to young and just
  80       // reached the deduplication age threshold.
  81       return true;
  82     }
  83     if (!to_young && obj->age() < StringDeduplicationAgeThreshold) {
  84       // Candidate found. String is being evacuated from young to old but has not
  85       // reached the deduplication age threshold, i.e. has not previously been a
  86       // candidate during its life in the young generation.
  87       return true;
  88     }
  89   }
  90 
  91   // Not a candidate
  92   return false;
  93 }
  94 
  95 void G1StringDedup::enqueue_from_evacuation(bool from_young, bool to_young, uint worker_id, oop java_string) {
  96   assert(is_enabled(), "String deduplication not enabled");
  97   if (is_candidate_from_evacuation(from_young, to_young, java_string)) {
  98     G1StringDedupQueue::push(worker_id, java_string);
  99   }
 100 }
 101 
 102 void G1StringDedup::deduplicate(oop java_string) {
 103   assert(is_enabled(), "String deduplication not enabled");
 104   G1StringDedupStat dummy; // Statistics from this path is never used
 105   G1StringDedupTable::deduplicate(java_string, dummy);
 106 }
 107 
 108 void G1StringDedup::oops_do(OopClosure* keep_alive) {
 109   assert(is_enabled(), "String deduplication not enabled");
 110   unlink_or_oops_do(NULL, keep_alive, true /* allow_resize_and_rehash */);
 111 }
 112 
 113 void G1StringDedup::parallel_unlink(G1StringDedupUnlinkOrOopsDoClosure* unlink, uint worker_id) {
 114   assert(is_enabled(), "String deduplication not enabled");
 115   G1StringDedupQueue::unlink_or_oops_do(unlink);
 116   G1StringDedupTable::unlink_or_oops_do(unlink, worker_id);
 117 }
 118 
 119 //
 120 // Task for parallel unlink_or_oops_do() operation on the deduplication queue
 121 // and table.
 122 //
 123 class G1StringDedupUnlinkOrOopsDoTask : public AbstractGangTask {
 124 private:
 125   G1StringDedupUnlinkOrOopsDoClosure _cl;
 126   G1GCPhaseTimes* _phase_times;
 127 
 128 public:
 129   G1StringDedupUnlinkOrOopsDoTask(BoolObjectClosure* is_alive,
 130                                   OopClosure* keep_alive,
 131                                   bool allow_resize_and_rehash,
 132                                   G1GCPhaseTimes* phase_times) :
 133     AbstractGangTask("G1StringDedupUnlinkOrOopsDoTask"),
 134     _cl(is_alive, keep_alive, allow_resize_and_rehash), _phase_times(phase_times) { }
 135 
 136   virtual void work(uint worker_id) {
 137     {
 138       G1GCParPhaseTimesTracker x(_phase_times, G1GCPhaseTimes::StringDedupQueueFixup, worker_id);
 139       G1StringDedupQueue::unlink_or_oops_do(&_cl);
 140     }
 141     {
 142       G1GCParPhaseTimesTracker x(_phase_times, G1GCPhaseTimes::StringDedupTableFixup, worker_id);
 143       G1StringDedupTable::unlink_or_oops_do(&_cl, worker_id);
 144     }
 145   }
 146 };
 147 
 148 void G1StringDedup::unlink_or_oops_do(BoolObjectClosure* is_alive,
 149                                       OopClosure* keep_alive,
 150                                       bool allow_resize_and_rehash,
 151                                       G1GCPhaseTimes* phase_times) {
 152   assert(is_enabled(), "String deduplication not enabled");
 153 
 154   G1StringDedupUnlinkOrOopsDoTask task(is_alive, keep_alive, allow_resize_and_rehash, phase_times);
 155   G1CollectedHeap* g1h = G1CollectedHeap::heap();
 156   g1h->workers()->run_task(&task);
 157 }
 158 
 159 void G1StringDedup::threads_do(ThreadClosure* tc) {
 160   assert(is_enabled(), "String deduplication not enabled");
 161   tc->do_thread(G1StringDedupThread::thread());
 162 }
 163 
 164 void G1StringDedup::print_worker_threads_on(outputStream* st) {
 165   assert(is_enabled(), "String deduplication not enabled");
 166   G1StringDedupThread::thread()->print_on(st);
 167   st->cr();
 168 }
 169 
 170 void G1StringDedup::verify() {
 171   assert(is_enabled(), "String deduplication not enabled");
 172   G1StringDedupQueue::verify();
 173   G1StringDedupTable::verify();
 174 }
 175 
 176 G1StringDedupUnlinkOrOopsDoClosure::G1StringDedupUnlinkOrOopsDoClosure(BoolObjectClosure* is_alive,
 177                                                                        OopClosure* keep_alive,
 178                                                                        bool allow_resize_and_rehash) :
 179   _is_alive(is_alive),
 180   _keep_alive(keep_alive),
 181   _resized_table(NULL),
 182   _rehashed_table(NULL),
 183   _next_queue(0),
 184   _next_bucket(0) {
 185   if (allow_resize_and_rehash) {
 186     // If both resize and rehash is needed, only do resize. Rehash of
 187     // the table will eventually happen if the situation persists.
 188     _resized_table = G1StringDedupTable::prepare_resize();
 189     if (!is_resizing()) {
 190       _rehashed_table = G1StringDedupTable::prepare_rehash();
 191     }
 192   }
 193 }
 194 
 195 G1StringDedupUnlinkOrOopsDoClosure::~G1StringDedupUnlinkOrOopsDoClosure() {
 196   assert(!is_resizing() || !is_rehashing(), "Can not both resize and rehash");
 197   if (is_resizing()) {
 198     G1StringDedupTable::finish_resize(_resized_table);
 199   } else if (is_rehashing()) {
 200     G1StringDedupTable::finish_rehash(_rehashed_table);
 201   }
 202 }
 203 
 204 // Atomically claims the next available queue for exclusive access by
 205 // the current thread. Returns the queue number of the claimed queue.
 206 size_t G1StringDedupUnlinkOrOopsDoClosure::claim_queue() {
 207   return Atomic::add((size_t)1, &_next_queue) - 1;
 208 }
 209 
 210 // Atomically claims the next available table partition for exclusive
 211 // access by the current thread. Returns the table bucket number where
 212 // the claimed partition starts.
 213 size_t G1StringDedupUnlinkOrOopsDoClosure::claim_table_partition(size_t partition_size) {
 214   return Atomic::add(partition_size, &_next_bucket) - partition_size;
 215 }


   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 #include "precompiled.hpp"










  25 
  26 #include "gc/shared/stringdedup/stringDedup.hpp"
  27 #include "gc/shared/stringdedup/stringDedupQueue.hpp"
  28 #include "gc/shared/stringdedup/stringDedupTable.hpp"
  29 #include "gc/shared/stringdedup/stringDedupThread.hpp"
  30 
  31 bool StringDedup::_enabled = false;








  32 
  33 void StringDedup::gc_prologue(bool resize_and_rehash_table) {
  34   assert(is_enabled(), "String deduplication not enabled");
  35   StringDedupQueue::gc_prologue();
  36   StringDedupTable::gc_prologue(resize_and_rehash_table);






















  37 

















  38 }
  39 void StringDedup::gc_epilogue() {








  40   assert(is_enabled(), "String deduplication not enabled");
  41   StringDedupQueue::gc_epilogue();
  42   StringDedupTable::gc_epilogue();
  43 }
  44 
  45 void StringDedup::stop() {
  46   assert(is_enabled(), "String deduplication not enabled");
  47   StringDedupThread::thread()->stop();
  48 }
  49 
  50 void StringDedup::deduplicate(oop java_string) {
  51   assert(is_enabled(), "String deduplication not enabled");
  52   StringDedupStat dummy; // Statistics from this path is never used
  53   StringDedupTable::deduplicate(java_string, &dummy);
  54 }
  55 








  56 
  57 void StringDedup::parallel_unlink(StringDedupUnlinkOrOopsDoClosure* unlink, uint worker_id) {























  58   assert(is_enabled(), "String deduplication not enabled");
  59   StringDedupQueue::unlink_or_oops_do(unlink);
  60   StringDedupTable::unlink_or_oops_do(unlink, worker_id);


  61 }
  62 
  63 void StringDedup::threads_do(ThreadClosure* tc) {
  64   assert(is_enabled(), "String deduplication not enabled");
  65   tc->do_thread(StringDedupThread::thread());
  66 }
  67 
  68 void StringDedup::print_worker_threads_on(outputStream* st) {
  69   assert(is_enabled(), "String deduplication not enabled");
  70   StringDedupThread::thread()->print_on(st);
  71   st->cr();
  72 }
  73 
  74 void StringDedup::verify() {
  75   assert(is_enabled(), "String deduplication not enabled");
  76   StringDedupQueue::verify();
  77   StringDedupTable::verify();
  78 }
  79 
  80 
  81 StringDedupUnlinkOrOopsDoClosure::StringDedupUnlinkOrOopsDoClosure(BoolObjectClosure* is_alive,
  82                                                                    OopClosure* keep_alive) :
  83   _is_alive(is_alive), _keep_alive(keep_alive) {



































  84 }
< prev index next >