1 /*
   2  * Copyright (c) 2017, 2020, 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/shenandoahPhaseTimings.hpp"
  32 #include "gc/shenandoah/shenandoahStringDedup.hpp"
  33 #include "gc/shenandoah/shenandoahStrDedupQueue.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(ShenandoahPhaseTimings::Phase phase,
  75         BoolObjectClosure* is_alive, OopClosure* cl, uint worker_id) {
  76   assert(SafepointSynchronize::is_at_safepoint(), "Must be at a safepoint");
  77   assert(is_enabled(), "String deduplication not enabled");
  78 
  79   StringDedupUnlinkOrOopsDoClosure sd_cl(is_alive, cl);
  80   {
  81     ShenandoahWorkerTimingsTracker x(phase, ShenandoahPhaseTimings::StringDedupQueueRoots, worker_id);
  82     StringDedupQueue::unlink_or_oops_do(&sd_cl);
  83   }
  84 
  85   {
  86     ShenandoahWorkerTimingsTracker x(phase, ShenandoahPhaseTimings::StringDedupTableRoots, worker_id);
  87     StringDedupTable::unlink_or_oops_do(&sd_cl, worker_id);
  88   }
  89 }
  90 
  91 void ShenandoahStringDedup::oops_do_slow(OopClosure* cl) {
  92   assert(SafepointSynchronize::is_at_safepoint(), "Must be at a safepoint");
  93   assert(is_enabled(), "String deduplication not enabled");
  94   AlwaysTrueClosure always_true;
  95   StringDedupUnlinkOrOopsDoClosure sd_cl(&always_true, cl);
  96   StringDedupQueue::unlink_or_oops_do(&sd_cl);
  97   StringDedupTable::unlink_or_oops_do(&sd_cl, 0);
  98 }
  99 
 100 class ShenandoahIsMarkedNextClosure : public BoolObjectClosure {
 101 private:
 102   ShenandoahMarkingContext* const _mark_context;
 103 
 104 public:
 105   ShenandoahIsMarkedNextClosure() : _mark_context(ShenandoahHeap::heap()->marking_context()) { }
 106 
 107   bool do_object_b(oop obj) {
 108     return _mark_context->is_marked(obj);
 109   }
 110 };
 111 
 112 void ShenandoahStringDedup::parallel_cleanup() {
 113   assert(SafepointSynchronize::is_at_safepoint(), "Must be at a safepoint");
 114   log_debug(gc, stringdedup)("String dedup cleanup");
 115   ShenandoahIsMarkedNextClosure cl;
 116 
 117   unlink_or_oops_do(&cl, NULL, true);
 118 }
 119 
 120 //
 121 // Task for parallel unlink_or_oops_do() operation on the deduplication queue
 122 // and table.
 123 //
 124 class ShenandoahStringDedupUnlinkOrOopsDoTask : public AbstractGangTask {
 125 private:
 126   StringDedupUnlinkOrOopsDoClosure _cl;
 127 
 128 public:
 129   ShenandoahStringDedupUnlinkOrOopsDoTask(BoolObjectClosure* is_alive,
 130                                   OopClosure* keep_alive,
 131                                   bool allow_resize_and_rehash) :
 132     AbstractGangTask("StringDedupUnlinkOrOopsDoTask"),
 133     _cl(is_alive, keep_alive) {
 134       StringDedup::gc_prologue(allow_resize_and_rehash);
 135   }
 136 
 137   ~ShenandoahStringDedupUnlinkOrOopsDoTask() {
 138     StringDedup::gc_epilogue();
 139   }
 140 
 141   virtual void work(uint worker_id) {
 142     StringDedupQueue::unlink_or_oops_do(&_cl);
 143     StringDedupTable::unlink_or_oops_do(&_cl, worker_id);
 144   }
 145 };
 146 
 147 void ShenandoahStringDedup::unlink_or_oops_do(BoolObjectClosure* is_alive,
 148                                       OopClosure* keep_alive,
 149                                       bool allow_resize_and_rehash) {
 150   assert(is_enabled(), "String deduplication not enabled");
 151 
 152   ShenandoahStringDedupUnlinkOrOopsDoTask task(is_alive, keep_alive, allow_resize_and_rehash);
 153   ShenandoahHeap* heap = ShenandoahHeap::heap();
 154   heap->workers()->run_task(&task);
 155 }