1 /*
2  * Copyright (c) 2017, 2020, Red Hat, Inc. 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 #include "precompiled.hpp"
26 
27 #include "gc/shared/stringdedup/stringDedup.inline.hpp"
28 #include "gc/shared/workgroup.hpp"
29 #include "gc/shenandoah/shenandoahCollectionSet.inline.hpp"
30 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
31 #include "gc/shenandoah/shenandoahMarkingContext.inline.hpp"
32 #include "gc/shenandoah/shenandoahPhaseTimings.hpp"
33 #include "gc/shenandoah/shenandoahStringDedup.hpp"
34 #include "gc/shenandoah/shenandoahStrDedupQueue.hpp"
35 #include "gc/shenandoah/shenandoahUtils.hpp"
36 #include "runtime/thread.hpp"
37 
initialize()38 void ShenandoahStringDedup::initialize() {
39   assert(UseShenandoahGC, "String deduplication available with Shenandoah GC");
40   StringDedup::initialize_impl<ShenandoahStrDedupQueue, StringDedupStat>();
41 }
42 
43 /* Enqueue candidates for deduplication.
44  * The method should only be called by GC worker threads during marking phases.
45  */
enqueue_candidate(oop java_string)46 void ShenandoahStringDedup::enqueue_candidate(oop java_string) {
47   assert(Thread::current()->is_Worker_thread(),
48         "Only from a GC worker thread");
49 
50   if (java_string->age() <= StringDeduplicationAgeThreshold) {
51     const markWord mark = java_string->mark();
52 
53     // Having/had displaced header, too risk to deal with them, skip
54     if (mark == markWord::INFLATING() || mark.has_displaced_mark_helper()) {
55       return;
56     }
57 
58     // Increase string age and enqueue it when it rearches age threshold
59     markWord new_mark = mark.incr_age();
60     if (mark == java_string->cas_set_mark(new_mark, mark)) {
61       if (mark.age() == StringDeduplicationAgeThreshold) {
62         StringDedupQueue::push(ShenandoahWorkerSession::worker_id(), java_string);
63       }
64     }
65   }
66 }
67 
68 // Deduplicate a string, return true if it is deduplicated.
deduplicate(oop java_string)69 void ShenandoahStringDedup::deduplicate(oop java_string) {
70   assert(is_enabled(), "String deduplication not enabled");
71   StringDedupStat dummy; // Statistics from this path is never used
72   StringDedupTable::deduplicate(java_string, &dummy);
73 }
74 
parallel_oops_do(ShenandoahPhaseTimings::Phase phase,BoolObjectClosure * is_alive,OopClosure * cl,uint worker_id)75 void ShenandoahStringDedup::parallel_oops_do(ShenandoahPhaseTimings::Phase phase,
76         BoolObjectClosure* is_alive, OopClosure* cl, uint worker_id) {
77   assert(SafepointSynchronize::is_at_safepoint(), "Must be at a safepoint");
78   assert(is_enabled(), "String deduplication not enabled");
79 
80   StringDedupUnlinkOrOopsDoClosure sd_cl(is_alive, cl);
81   {
82     ShenandoahWorkerTimingsTracker x(phase, ShenandoahPhaseTimings::StringDedupQueueRoots, worker_id);
83     StringDedupQueue::unlink_or_oops_do(&sd_cl);
84   }
85 
86   {
87     ShenandoahWorkerTimingsTracker x(phase, ShenandoahPhaseTimings::StringDedupTableRoots, worker_id);
88     StringDedupTable::unlink_or_oops_do(&sd_cl, worker_id);
89   }
90 }
91 
oops_do_slow(OopClosure * cl)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 //
102 // Task for parallel unlink_or_oops_do() operation on the deduplication queue
103 // and table.
104 //
105 class ShenandoahStringDedupUnlinkOrOopsDoTask : public AbstractGangTask {
106 private:
107   StringDedupUnlinkOrOopsDoClosure _cl;
108 
109 public:
ShenandoahStringDedupUnlinkOrOopsDoTask(BoolObjectClosure * is_alive,OopClosure * keep_alive,bool allow_resize_and_rehash)110   ShenandoahStringDedupUnlinkOrOopsDoTask(BoolObjectClosure* is_alive,
111                                   OopClosure* keep_alive,
112                                   bool allow_resize_and_rehash) :
113     AbstractGangTask("StringDedupUnlinkOrOopsDoTask"),
114     _cl(is_alive, keep_alive) {
115       StringDedup::gc_prologue(allow_resize_and_rehash);
116   }
117 
~ShenandoahStringDedupUnlinkOrOopsDoTask()118   ~ShenandoahStringDedupUnlinkOrOopsDoTask() {
119     StringDedup::gc_epilogue();
120   }
121 
work(uint worker_id)122   virtual void work(uint worker_id) {
123     StringDedupQueue::unlink_or_oops_do(&_cl);
124     StringDedupTable::unlink_or_oops_do(&_cl, worker_id);
125   }
126 };
127 
unlink_or_oops_do(BoolObjectClosure * is_alive,OopClosure * keep_alive,bool allow_resize_and_rehash)128 void ShenandoahStringDedup::unlink_or_oops_do(BoolObjectClosure* is_alive,
129                                       OopClosure* keep_alive,
130                                       bool allow_resize_and_rehash) {
131   assert(is_enabled(), "String deduplication not enabled");
132 
133   ShenandoahStringDedupUnlinkOrOopsDoTask task(is_alive, keep_alive, allow_resize_and_rehash);
134   ShenandoahHeap* heap = ShenandoahHeap::heap();
135   heap->workers()->run_task(&task);
136 }
137