1/*
2 * Copyright (c) 2001, 2019, 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#include "precompiled.hpp"
26#include "gc/g1/g1CollectedHeap.inline.hpp"
27#include "gc/g1/g1DirtyCardQueue.hpp"
28#include "gc/g1/g1FreeIdSet.hpp"
29#include "gc/g1/g1RemSet.hpp"
30#include "gc/g1/g1ThreadLocalData.hpp"
31#include "gc/g1/heapRegionRemSet.hpp"
32#include "gc/shared/suspendibleThreadSet.hpp"
33#include "gc/shared/workgroup.hpp"
34#include "runtime/atomic.hpp"
35#include "runtime/flags/flagSetting.hpp"
36#include "runtime/mutexLocker.hpp"
37#include "runtime/safepoint.hpp"
38#include "runtime/thread.inline.hpp"
39#include "runtime/threadSMR.hpp"
40
41// Closure used for updating remembered sets and recording references that
42// point into the collection set while the mutator is running.
43// Assumed to be only executed concurrently with the mutator. Yields via
44// SuspendibleThreadSet after every card.
45class G1RefineCardConcurrentlyClosure: public G1CardTableEntryClosure {
46public:
47 bool do_card_ptr(CardValue* card_ptr, uint worker_i) {
48 G1CollectedHeap::heap()->rem_set()->refine_card_concurrently(card_ptr, worker_i);
49
50 if (SuspendibleThreadSet::should_yield()) {
51 // Caller will actually yield.
52 return false;
53 }
54 // Otherwise, we finished successfully; return true.
55 return true;
56 }
57};
58
59G1DirtyCardQueue::G1DirtyCardQueue(G1DirtyCardQueueSet* qset) :
60 // Dirty card queues are always active, so we create them with their
61 // active field set to true.
62 PtrQueue(qset, true /* active */)
63{ }
64
65G1DirtyCardQueue::~G1DirtyCardQueue() {
66 flush();
67}
68
69void G1DirtyCardQueue::handle_completed_buffer() {
70 assert(_buf != NULL, "precondition");
71 BufferNode* node = BufferNode::make_node_from_buffer(_buf, index());
72 G1DirtyCardQueueSet* dcqs = dirty_card_qset();
73 if (dcqs->process_or_enqueue_completed_buffer(node)) {
74 reset(); // Buffer fully processed, reset index.
75 } else {
76 allocate_buffer(); // Buffer enqueued, get a new one.
77 }
78}
79
80G1DirtyCardQueueSet::G1DirtyCardQueueSet(bool notify_when_complete) :
81 PtrQueueSet(notify_when_complete),
82 _max_completed_buffers(MaxCompletedBuffersUnlimited),
83 _completed_buffers_padding(0),
84 _free_ids(NULL),
85 _processed_buffers_mut(0),
86 _processed_buffers_rs_thread(0),
87 _cur_par_buffer_node(NULL)
88{
89 _all_active = true;
90}
91
92G1DirtyCardQueueSet::~G1DirtyCardQueueSet() {
93 delete _free_ids;
94}
95
96// Determines how many mutator threads can process the buffers in parallel.
97uint G1DirtyCardQueueSet::num_par_ids() {
98 return (uint)os::initial_active_processor_count();
99}
100
101void G1DirtyCardQueueSet::initialize(Monitor* cbl_mon,
102 BufferNode::Allocator* allocator,
103 bool init_free_ids) {
104 PtrQueueSet::initialize(cbl_mon, allocator);
105 if (init_free_ids) {
106 _free_ids = new G1FreeIdSet(0, num_par_ids());
107 }
108}
109
110void G1DirtyCardQueueSet::handle_zero_index_for_thread(Thread* t) {
111 G1ThreadLocalData::dirty_card_queue(t).handle_zero_index();
112}
113
114bool G1DirtyCardQueueSet::apply_closure_to_buffer(G1CardTableEntryClosure* cl,
115 BufferNode* node,
116 bool consume,
117 uint worker_i) {
118 if (cl == NULL) return true;
119 bool result = true;
120 void** buf = BufferNode::make_buffer_from_node(node);
121 size_t i = node->index();
122 size_t limit = buffer_size();
123 for ( ; i < limit; ++i) {
124 CardTable::CardValue* card_ptr = static_cast<CardTable::CardValue*>(buf[i]);
125 assert(card_ptr != NULL, "invariant");
126 if (!cl->do_card_ptr(card_ptr, worker_i)) {
127 result = false; // Incomplete processing.
128 break;
129 }
130 }
131 if (consume) {
132 assert(i <= buffer_size(), "invariant");
133 node->set_index(i);
134 }
135 return result;
136}
137
138#ifndef ASSERT
139#define assert_fully_consumed(node, buffer_size)
140#else
141#define assert_fully_consumed(node, buffer_size) \
142 do { \
143 size_t _afc_index = (node)->index(); \
144 size_t _afc_size = (buffer_size); \
145 assert(_afc_index == _afc_size, \
146 "Buffer was not fully consumed as claimed: index: " \
147 SIZE_FORMAT ", size: " SIZE_FORMAT, \
148 _afc_index, _afc_size); \
149 } while (0)
150#endif // ASSERT
151
152bool G1DirtyCardQueueSet::process_or_enqueue_completed_buffer(BufferNode* node) {
153 if (Thread::current()->is_Java_thread()) {
154 // If the number of buffers exceeds the limit, make this Java
155 // thread do the processing itself. We don't lock to access
156 // buffer count or padding; it is fine to be imprecise here. The
157 // add of padding could overflow, which is treated as unlimited.
158 size_t max_buffers = max_completed_buffers();
159 size_t limit = max_buffers + completed_buffers_padding();
160 if ((completed_buffers_num() > limit) && (limit >= max_buffers)) {
161 if (mut_process_buffer(node)) {
162 return true;
163 }
164 }
165 }
166 enqueue_completed_buffer(node);
167 return false;
168}
169
170bool G1DirtyCardQueueSet::mut_process_buffer(BufferNode* node) {
171 guarantee(_free_ids != NULL, "must be");
172
173 uint worker_i = _free_ids->claim_par_id(); // temporarily claim an id
174 G1RefineCardConcurrentlyClosure cl;
175 bool result = apply_closure_to_buffer(&cl, node, true, worker_i);
176 _free_ids->release_par_id(worker_i); // release the id
177
178 if (result) {
179 assert_fully_consumed(node, buffer_size());
180 Atomic::inc(&_processed_buffers_mut);
181 }
182 return result;
183}
184
185bool G1DirtyCardQueueSet::refine_completed_buffer_concurrently(uint worker_i, size_t stop_at) {
186 G1RefineCardConcurrentlyClosure cl;
187 return apply_closure_to_completed_buffer(&cl, worker_i, stop_at, false);
188}
189
190bool G1DirtyCardQueueSet::apply_closure_during_gc(G1CardTableEntryClosure* cl, uint worker_i) {
191 assert_at_safepoint();
192 return apply_closure_to_completed_buffer(cl, worker_i, 0, true);
193}
194
195bool G1DirtyCardQueueSet::apply_closure_to_completed_buffer(G1CardTableEntryClosure* cl,
196 uint worker_i,
197 size_t stop_at,
198 bool during_pause) {
199 assert(!during_pause || stop_at == 0, "Should not leave any completed buffers during a pause");
200 BufferNode* nd = get_completed_buffer(stop_at);
201 if (nd == NULL) {
202 return false;
203 } else {
204 if (apply_closure_to_buffer(cl, nd, true, worker_i)) {
205 assert_fully_consumed(nd, buffer_size());
206 // Done with fully processed buffer.
207 deallocate_buffer(nd);
208 Atomic::inc(&_processed_buffers_rs_thread);
209 } else {
210 // Return partially processed buffer to the queue.
211 guarantee(!during_pause, "Should never stop early");
212 enqueue_completed_buffer(nd);
213 }
214 return true;
215 }
216}
217
218void G1DirtyCardQueueSet::par_apply_closure_to_all_completed_buffers(G1CardTableEntryClosure* cl) {
219 BufferNode* nd = _cur_par_buffer_node;
220 while (nd != NULL) {
221 BufferNode* next = nd->next();
222 BufferNode* actual = Atomic::cmpxchg(next, &_cur_par_buffer_node, nd);
223 if (actual == nd) {
224 bool b = apply_closure_to_buffer(cl, nd, false);
225 guarantee(b, "Should not stop early.");
226 nd = next;
227 } else {
228 nd = actual;
229 }
230 }
231}
232
233void G1DirtyCardQueueSet::abandon_logs() {
234 assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint.");
235 abandon_completed_buffers();
236
237 // Since abandon is done only at safepoints, we can safely manipulate
238 // these queues.
239 struct AbandonThreadLogClosure : public ThreadClosure {
240 virtual void do_thread(Thread* t) {
241 G1ThreadLocalData::dirty_card_queue(t).reset();
242 }
243 } closure;
244 Threads::threads_do(&closure);
245
246 G1BarrierSet::shared_dirty_card_queue().reset();
247}
248
249void G1DirtyCardQueueSet::concatenate_logs() {
250 // Iterate over all the threads, if we find a partial log add it to
251 // the global list of logs. Temporarily turn off the limit on the number
252 // of outstanding buffers.
253 assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint.");
254 size_t old_limit = max_completed_buffers();
255 set_max_completed_buffers(MaxCompletedBuffersUnlimited);
256
257 struct ConcatenateThreadLogClosure : public ThreadClosure {
258 virtual void do_thread(Thread* t) {
259 G1DirtyCardQueue& dcq = G1ThreadLocalData::dirty_card_queue(t);
260 if (!dcq.is_empty()) {
261 dcq.flush();
262 }
263 }
264 } closure;
265 Threads::threads_do(&closure);
266
267 G1BarrierSet::shared_dirty_card_queue().flush();
268 set_max_completed_buffers(old_limit);
269}
270