1 /*
   2  * Copyright (c) 2017, 2019, Red Hat, Inc. All rights reserved.
   3  *
   4  * This code is free software; you can redistribute it and/or modify it
   5  * under the terms of the GNU General Public License version 2 only, as
   6  * published by the Free Software Foundation.
   7  *
   8  * This code is distributed in the hope that it will be useful, but WITHOUT
   9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  11  * version 2 for more details (a copy is included in the LICENSE file that
  12  * accompanied this code).
  13  *
  14  * You should have received a copy of the GNU General Public License version
  15  * 2 along with this work; if not, write to the Free Software Foundation,
  16  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  17  *
  18  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  19  * or visit www.oracle.com if you need additional information or have any
  20  * questions.
  21  *
  22  */
  23 
  24 #include "precompiled.hpp"
  25 
  26 #include "gc/shared/stringdedup/stringDedup.inline.hpp"
  27 #include "gc/shared/workgroup.hpp"
  28 #include "gc/shenandoah/shenandoahCollectionSet.inline.hpp"
  29 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
  30 #include "gc/shenandoah/shenandoahMarkingContext.inline.hpp"
  31 #include "gc/shenandoah/shenandoahStringDedup.hpp"
  32 #include "gc/shenandoah/shenandoahStrDedupQueue.hpp"
  33 #include "gc/shenandoah/shenandoahTimingTracker.hpp"
  34 #include "gc/shenandoah/shenandoahUtils.hpp"
  35 #include "runtime/thread.hpp"
  36 
  37 void ShenandoahStringDedup::initialize() {
  38   assert(UseShenandoahGC, "String deduplication available with Shenandoah GC");
  39   StringDedup::initialize_impl<ShenandoahStrDedupQueue, StringDedupStat>();
  40 }
  41 
  42 /* Enqueue candidates for deduplication.
  43  * The method should only be called by GC worker threads during marking phases.
  44  */
  45 void ShenandoahStringDedup::enqueue_candidate(oop java_string) {
  46   assert(Thread::current()->is_Worker_thread(),
  47         "Only from a GC worker thread");
  48 
  49   if (java_string->age() <= StringDeduplicationAgeThreshold) {
  50     const markOop mark = java_string->mark();
  51 
  52     // Having/had displaced header, too risk to deal with them, skip
  53     if (mark == markOopDesc::INFLATING() || mark->has_displaced_mark_helper()) {
  54       return;
  55     }
  56 
  57     // Increase string age and enqueue it when it rearches age threshold
  58     markOop new_mark = mark->incr_age();
  59     if (mark == java_string->cas_set_mark(new_mark, mark)) {
  60       if (mark->age() == StringDeduplicationAgeThreshold) {
  61         StringDedupQueue::push(ShenandoahWorkerSession::worker_id(), java_string);
  62       }
  63     }
  64   }
  65 }
  66 
  67 // Deduplicate a string, return true if it is deduplicated.
  68 void ShenandoahStringDedup::deduplicate(oop java_string) {
  69   assert(is_enabled(), "String deduplication not enabled");
  70   StringDedupStat dummy; // Statistics from this path is never used
  71   StringDedupTable::deduplicate(java_string, &dummy);
  72 }
  73 
  74 void ShenandoahStringDedup::parallel_oops_do(BoolObjectClosure* is_alive, OopClosure* cl, uint worker_id) {
  75   assert(SafepointSynchronize::is_at_safepoint(), "Must be at a safepoint");
  76   assert(is_enabled(), "String deduplication not enabled");
  77 
  78   ShenandoahWorkerTimings* worker_times = ShenandoahHeap::heap()->phase_timings()->worker_times();
  79 
  80   StringDedupUnlinkOrOopsDoClosure sd_cl(is_alive, cl);
  81 
  82   {
  83     ShenandoahWorkerTimingsTracker x(worker_times, ShenandoahPhaseTimings::StringDedupQueueRoots, worker_id);
  84     StringDedupQueue::unlink_or_oops_do(&sd_cl);
  85   }
  86   {
  87     ShenandoahWorkerTimingsTracker x(worker_times, ShenandoahPhaseTimings::StringDedupTableRoots, worker_id);
  88     StringDedupTable::unlink_or_oops_do(&sd_cl, worker_id);
  89   }
  90 }
  91 
  92 void ShenandoahStringDedup::oops_do_slow(OopClosure* cl) {
  93   assert(SafepointSynchronize::is_at_safepoint(), "Must be at a safepoint");
  94   assert(is_enabled(), "String deduplication not enabled");
  95   AlwaysTrueClosure always_true;
  96   StringDedupUnlinkOrOopsDoClosure sd_cl(&always_true, cl);
  97   StringDedupQueue::unlink_or_oops_do(&sd_cl);
  98   StringDedupTable::unlink_or_oops_do(&sd_cl, 0);
  99 }
 100 
 101 class ShenandoahIsMarkedNextClosure : public BoolObjectClosure {
 102 private:
 103   ShenandoahMarkingContext* const _mark_context;
 104 
 105 public:
 106   ShenandoahIsMarkedNextClosure() : _mark_context(ShenandoahHeap::heap()->marking_context()) { }
 107 
 108   bool do_object_b(oop obj) {
 109     return _mark_context->is_marked(obj);
 110   }
 111 };
 112 
 113 void ShenandoahStringDedup::parallel_cleanup() {
 114   assert(SafepointSynchronize::is_at_safepoint(), "Must be at a safepoint");
 115   log_debug(gc, stringdedup)("String dedup cleanup");
 116   ShenandoahIsMarkedNextClosure cl;
 117 
 118   unlink_or_oops_do(&cl, NULL, true);
 119 }
 120 
 121 //
 122 // Task for parallel unlink_or_oops_do() operation on the deduplication queue
 123 // and table.
 124 //
 125 class ShenandoahStringDedupUnlinkOrOopsDoTask : public AbstractGangTask {
 126 private:
 127   StringDedupUnlinkOrOopsDoClosure _cl;
 128 
 129 public:
 130   ShenandoahStringDedupUnlinkOrOopsDoTask(BoolObjectClosure* is_alive,
 131                                   OopClosure* keep_alive,
 132                                   bool allow_resize_and_rehash) :
 133     AbstractGangTask("StringDedupUnlinkOrOopsDoTask"),
 134     _cl(is_alive, keep_alive) {
 135       StringDedup::gc_prologue(allow_resize_and_rehash);
 136   }
 137 
 138   ~ShenandoahStringDedupUnlinkOrOopsDoTask() {
 139     StringDedup::gc_epilogue();
 140   }
 141 
 142   virtual void work(uint worker_id) {
 143     StringDedupQueue::unlink_or_oops_do(&_cl);
 144     StringDedupTable::unlink_or_oops_do(&_cl, worker_id);
 145   }
 146 };
 147 
 148 void ShenandoahStringDedup::unlink_or_oops_do(BoolObjectClosure* is_alive,
 149                                       OopClosure* keep_alive,
 150                                       bool allow_resize_and_rehash) {
 151   assert(is_enabled(), "String deduplication not enabled");
 152 
 153   ShenandoahStringDedupUnlinkOrOopsDoTask task(is_alive, keep_alive, allow_resize_and_rehash);
 154   ShenandoahHeap* heap = ShenandoahHeap::heap();
 155   heap->workers()->run_task(&task);
 156 }