24
25 #include "precompiled.hpp"
26 #include "gc/shared/satbMarkQueue.hpp"
27 #include "gc/shared/collectedHeap.hpp"
28 #include "logging/log.hpp"
29 #include "memory/allocation.inline.hpp"
30 #include "oops/oop.inline.hpp"
31 #include "runtime/mutexLocker.hpp"
32 #include "runtime/os.hpp"
33 #include "runtime/safepoint.hpp"
34 #include "runtime/thread.hpp"
35 #include "runtime/threadSMR.hpp"
36 #include "runtime/vmThread.hpp"
37
38 SATBMarkQueue::SATBMarkQueue(SATBMarkQueueSet* qset, bool permanent) :
39 // SATB queues are only active during marking cycles. We create
40 // them with their active field set to false. If a thread is
41 // created during a cycle and its SATB queue needs to be activated
42 // before the thread starts running, we'll need to set its active
43 // field to true. This must be done in the collector-specific
44 // BarrierSet::on_thread_attach() implementation.
45 PtrQueue(qset, permanent, false /* active */)
46 { }
47
48 void SATBMarkQueue::flush() {
49 // Filter now to possibly save work later. If filtering empties the
50 // buffer then flush_impl can deallocate the buffer.
51 filter();
52 flush_impl();
53 }
54
55 // This method will first apply filtering to the buffer. If filtering
56 // retains a small enough collection in the buffer, we can continue to
57 // use the buffer as-is, instead of enqueueing and replacing it.
58
59 bool SATBMarkQueue::should_enqueue_buffer() {
60 assert(_lock == NULL || _lock->owned_by_self(),
61 "we should have taken the lock before calling this");
62
63 // This method should only be called if there is a non-NULL buffer
64 // that is full.
65 assert(index() == 0, "pre-condition");
66 assert(_buf != NULL, "pre-condition");
67
68 filter();
69
70 SATBMarkQueueSet* satb_qset = static_cast<SATBMarkQueueSet*>(qset());
71 size_t threshold = satb_qset->buffer_enqueue_threshold();
72 // Ensure we'll enqueue completely full buffers.
73 assert(threshold > 0, "enqueue threshold = 0");
74 // Ensure we won't enqueue empty buffers.
75 assert(threshold <= capacity(),
76 "enqueue threshold " SIZE_FORMAT " exceeds capacity " SIZE_FORMAT,
77 threshold, capacity());
78 return index() < threshold;
79 }
80
81 void SATBMarkQueue::apply_closure_and_empty(SATBBufferClosure* cl) {
82 assert(SafepointSynchronize::is_at_safepoint(),
90 #ifndef PRODUCT
91 // Helpful for debugging
92
93 static void print_satb_buffer(const char* name,
94 void** buf,
95 size_t index,
96 size_t capacity) {
97 tty->print_cr(" SATB BUFFER [%s] buf: " PTR_FORMAT " index: " SIZE_FORMAT
98 " capacity: " SIZE_FORMAT,
99 name, p2i(buf), index, capacity);
100 }
101
102 void SATBMarkQueue::print(const char* name) {
103 print_satb_buffer(name, _buf, index(), capacity());
104 }
105
106 #endif // PRODUCT
107
108 SATBMarkQueueSet::SATBMarkQueueSet() :
109 PtrQueueSet(),
110 _shared_satb_queue(this, true /* permanent */),
111 _buffer_enqueue_threshold(0)
112 {}
113
114 void SATBMarkQueueSet::initialize(Monitor* cbl_mon,
115 BufferNode::Allocator* allocator,
116 size_t process_completed_buffers_threshold,
117 uint buffer_enqueue_threshold_percentage,
118 Mutex* lock) {
119 PtrQueueSet::initialize(cbl_mon, allocator);
120 set_process_completed_buffers_threshold(process_completed_buffers_threshold);
121 _shared_satb_queue.set_lock(lock);
122 assert(buffer_size() != 0, "buffer size not initialized");
123 // Minimum threshold of 1 ensures enqueuing of completely full buffers.
124 size_t size = buffer_size();
125 size_t enqueue_qty = (size * buffer_enqueue_threshold_percentage) / 100;
126 _buffer_enqueue_threshold = MAX2(size - enqueue_qty, (size_t)1);
127 }
128
129 #ifdef ASSERT
130 void SATBMarkQueueSet::dump_active_states(bool expected_active) {
131 log_error(gc, verify)("Expected SATB active state: %s", expected_active ? "ACTIVE" : "INACTIVE");
132 log_error(gc, verify)("Actual SATB active states:");
133 log_error(gc, verify)(" Queue set: %s", is_active() ? "ACTIVE" : "INACTIVE");
134 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *t = jtiwh.next(); ) {
135 log_error(gc, verify)(" Thread \"%s\" queue: %s", t->name(), satb_queue_for_thread(t).is_active() ? "ACTIVE" : "INACTIVE");
136 }
137 log_error(gc, verify)(" Shared queue: %s", shared_satb_queue()->is_active() ? "ACTIVE" : "INACTIVE");
138 }
139
140 void SATBMarkQueueSet::verify_active_states(bool expected_active) {
141 // Verify queue set state
142 if (is_active() != expected_active) {
143 dump_active_states(expected_active);
144 guarantee(false, "SATB queue set has an unexpected active state");
145 }
146
147 // Verify thread queue states
148 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *t = jtiwh.next(); ) {
149 if (satb_queue_for_thread(t).is_active() != expected_active) {
150 dump_active_states(expected_active);
151 guarantee(false, "Thread SATB queue has an unexpected active state");
152 }
153 }
154
155 // Verify shared queue state
156 if (shared_satb_queue()->is_active() != expected_active) {
157 dump_active_states(expected_active);
158 guarantee(false, "Shared SATB queue has an unexpected active state");
159 }
160 }
161 #endif // ASSERT
162
163 void SATBMarkQueueSet::set_active_all_threads(bool active, bool expected_active) {
164 assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint.");
165 #ifdef ASSERT
166 verify_active_states(expected_active);
167 #endif // ASSERT
168 _all_active = active;
169 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *t = jtiwh.next(); ) {
170 satb_queue_for_thread(t).set_active(active);
171 }
172 shared_satb_queue()->set_active(active);
173 }
174
175 void SATBMarkQueueSet::filter_thread_buffers() {
176 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *t = jtiwh.next(); ) {
177 satb_queue_for_thread(t).filter();
178 }
179 shared_satb_queue()->filter();
180 }
181
182 bool SATBMarkQueueSet::apply_closure_to_completed_buffer(SATBBufferClosure* cl) {
183 BufferNode* nd = get_completed_buffer();
184 if (nd != NULL) {
185 void **buf = BufferNode::make_buffer_from_node(nd);
186 size_t index = nd->index();
187 size_t size = buffer_size();
188 assert(index <= size, "invariant");
189 cl->do_buffer(buf + index, size - index);
190 deallocate_buffer(nd);
191 return true;
192 } else {
193 return false;
194 }
195 }
196
197 #ifndef PRODUCT
198 // Helpful for debugging
199
200 #define SATB_PRINTER_BUFFER_SIZE 256
201
202 void SATBMarkQueueSet::print_all(const char* msg) {
203 char buffer[SATB_PRINTER_BUFFER_SIZE];
204 assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint.");
205
206 tty->cr();
207 tty->print_cr("SATB BUFFERS [%s]", msg);
208
209 BufferNode* nd = completed_buffers_head();
210 int i = 0;
211 while (nd != NULL) {
212 void** buf = BufferNode::make_buffer_from_node(nd);
213 os::snprintf(buffer, SATB_PRINTER_BUFFER_SIZE, "Enqueued: %d", i);
214 print_satb_buffer(buffer, buf, nd->index(), buffer_size());
215 nd = nd->next();
216 i += 1;
217 }
218
219 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *t = jtiwh.next(); ) {
220 os::snprintf(buffer, SATB_PRINTER_BUFFER_SIZE, "Thread: %s", t->name());
221 satb_queue_for_thread(t).print(buffer);
222 }
223
224 shared_satb_queue()->print("Shared");
225
226 tty->cr();
227 }
228 #endif // PRODUCT
229
230 void SATBMarkQueueSet::abandon_partial_marking() {
231 abandon_completed_buffers();
232 assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint.");
233 // So we can safely manipulate these queues.
234 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *t = jtiwh.next(); ) {
235 satb_queue_for_thread(t).reset();
236 }
237 shared_satb_queue()->reset();
238 }
|
24
25 #include "precompiled.hpp"
26 #include "gc/shared/satbMarkQueue.hpp"
27 #include "gc/shared/collectedHeap.hpp"
28 #include "logging/log.hpp"
29 #include "memory/allocation.inline.hpp"
30 #include "oops/oop.inline.hpp"
31 #include "runtime/mutexLocker.hpp"
32 #include "runtime/os.hpp"
33 #include "runtime/safepoint.hpp"
34 #include "runtime/thread.hpp"
35 #include "runtime/threadSMR.hpp"
36 #include "runtime/vmThread.hpp"
37
38 SATBMarkQueue::SATBMarkQueue(SATBMarkQueueSet* qset, bool permanent) :
39 // SATB queues are only active during marking cycles. We create
40 // them with their active field set to false. If a thread is
41 // created during a cycle and its SATB queue needs to be activated
42 // before the thread starts running, we'll need to set its active
43 // field to true. This must be done in the collector-specific
44 // BarrierSet thread attachment protocol.
45 PtrQueue(qset, permanent, false /* active */)
46 { }
47
48 void SATBMarkQueue::flush() {
49 // Filter now to possibly save work later. If filtering empties the
50 // buffer then flush_impl can deallocate the buffer.
51 filter();
52 flush_impl();
53 }
54
55 // This method will first apply filtering to the buffer. If filtering
56 // retains a small enough collection in the buffer, we can continue to
57 // use the buffer as-is, instead of enqueueing and replacing it.
58
59 bool SATBMarkQueue::should_enqueue_buffer() {
60 // This method should only be called if there is a non-NULL buffer
61 // that is full.
62 assert(index() == 0, "pre-condition");
63 assert(_buf != NULL, "pre-condition");
64
65 filter();
66
67 SATBMarkQueueSet* satb_qset = static_cast<SATBMarkQueueSet*>(qset());
68 size_t threshold = satb_qset->buffer_enqueue_threshold();
69 // Ensure we'll enqueue completely full buffers.
70 assert(threshold > 0, "enqueue threshold = 0");
71 // Ensure we won't enqueue empty buffers.
72 assert(threshold <= capacity(),
73 "enqueue threshold " SIZE_FORMAT " exceeds capacity " SIZE_FORMAT,
74 threshold, capacity());
75 return index() < threshold;
76 }
77
78 void SATBMarkQueue::apply_closure_and_empty(SATBBufferClosure* cl) {
79 assert(SafepointSynchronize::is_at_safepoint(),
87 #ifndef PRODUCT
88 // Helpful for debugging
89
90 static void print_satb_buffer(const char* name,
91 void** buf,
92 size_t index,
93 size_t capacity) {
94 tty->print_cr(" SATB BUFFER [%s] buf: " PTR_FORMAT " index: " SIZE_FORMAT
95 " capacity: " SIZE_FORMAT,
96 name, p2i(buf), index, capacity);
97 }
98
99 void SATBMarkQueue::print(const char* name) {
100 print_satb_buffer(name, _buf, index(), capacity());
101 }
102
103 #endif // PRODUCT
104
105 SATBMarkQueueSet::SATBMarkQueueSet() :
106 PtrQueueSet(),
107 _buffer_enqueue_threshold(0)
108 {}
109
110 void SATBMarkQueueSet::initialize(Monitor* cbl_mon,
111 BufferNode::Allocator* allocator,
112 size_t process_completed_buffers_threshold,
113 uint buffer_enqueue_threshold_percentage) {
114 PtrQueueSet::initialize(cbl_mon, allocator);
115 set_process_completed_buffers_threshold(process_completed_buffers_threshold);
116 assert(buffer_size() != 0, "buffer size not initialized");
117 // Minimum threshold of 1 ensures enqueuing of completely full buffers.
118 size_t size = buffer_size();
119 size_t enqueue_qty = (size * buffer_enqueue_threshold_percentage) / 100;
120 _buffer_enqueue_threshold = MAX2(size - enqueue_qty, (size_t)1);
121 }
122
123 #ifdef ASSERT
124 void SATBMarkQueueSet::dump_active_states(bool expected_active) {
125 log_error(gc, verify)("Expected SATB active state: %s", expected_active ? "ACTIVE" : "INACTIVE");
126 log_error(gc, verify)("Actual SATB active states:");
127 log_error(gc, verify)(" Queue set: %s", is_active() ? "ACTIVE" : "INACTIVE");
128
129 class DumpThreadStateClosure : public ThreadClosure {
130 SATBMarkQueueSet* _qset;
131 public:
132 DumpThreadStateClosure(SATBMarkQueueSet* qset) : _qset(qset) {}
133 virtual void do_thread(Thread* t) {
134 SATBMarkQueue& queue = _qset->satb_queue_for_thread(t);
135 log_error(gc, verify)(" Thread \"%s\" queue: %s",
136 t->name(),
137 queue.is_active() ? "ACTIVE" : "INACTIVE");
138 }
139 } closure(this);
140 Threads::threads_do(&closure);
141 }
142
143 void SATBMarkQueueSet::verify_active_states(bool expected_active) {
144 // Verify queue set state
145 if (is_active() != expected_active) {
146 dump_active_states(expected_active);
147 fatal("SATB queue set has an unexpected active state");
148 }
149
150 // Verify thread queue states
151 class VerifyThreadStatesClosure : public ThreadClosure {
152 SATBMarkQueueSet* _qset;
153 bool _expected_active;
154 public:
155 VerifyThreadStatesClosure(SATBMarkQueueSet* qset, bool expected_active) :
156 _qset(qset), _expected_active(expected_active) {}
157 virtual void do_thread(Thread* t) {
158 if (_qset->satb_queue_for_thread(t).is_active() != _expected_active) {
159 _qset->dump_active_states(_expected_active);
160 fatal("Thread SATB queue has an unexpected active state");
161 }
162 }
163 } closure(this, expected_active);
164 Threads::threads_do(&closure);
165 }
166 #endif // ASSERT
167
168 void SATBMarkQueueSet::set_active_all_threads(bool active, bool expected_active) {
169 assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint.");
170 #ifdef ASSERT
171 verify_active_states(expected_active);
172 #endif // ASSERT
173 _all_active = active;
174
175 class SetThreadActiveClosure : public ThreadClosure {
176 SATBMarkQueueSet* _qset;
177 bool _active;
178 public:
179 SetThreadActiveClosure(SATBMarkQueueSet* qset, bool active) :
180 _qset(qset), _active(active) {}
181 virtual void do_thread(Thread* t) {
182 _qset->satb_queue_for_thread(t).set_active(_active);
183 }
184 } closure(this, active);
185 Threads::threads_do(&closure);
186 }
187
188 void SATBMarkQueueSet::filter_thread_buffers() {
189 class FilterThreadBufferClosure : public ThreadClosure {
190 SATBMarkQueueSet* _qset;
191 public:
192 FilterThreadBufferClosure(SATBMarkQueueSet* qset) : _qset(qset) {}
193 virtual void do_thread(Thread* t) {
194 _qset->satb_queue_for_thread(t).filter();
195 }
196 } closure(this);
197 Threads::threads_do(&closure);
198 }
199
200 bool SATBMarkQueueSet::apply_closure_to_completed_buffer(SATBBufferClosure* cl) {
201 BufferNode* nd = get_completed_buffer();
202 if (nd != NULL) {
203 void **buf = BufferNode::make_buffer_from_node(nd);
204 size_t index = nd->index();
205 size_t size = buffer_size();
206 assert(index <= size, "invariant");
207 cl->do_buffer(buf + index, size - index);
208 deallocate_buffer(nd);
209 return true;
210 } else {
211 return false;
212 }
213 }
214
215 #ifndef PRODUCT
216 // Helpful for debugging
217
218 #define SATB_PRINTER_BUFFER_SIZE 256
219
220 void SATBMarkQueueSet::print_all(const char* msg) {
221 char buffer[SATB_PRINTER_BUFFER_SIZE];
222 assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint.");
223
224 tty->cr();
225 tty->print_cr("SATB BUFFERS [%s]", msg);
226
227 BufferNode* nd = completed_buffers_head();
228 int i = 0;
229 while (nd != NULL) {
230 void** buf = BufferNode::make_buffer_from_node(nd);
231 os::snprintf(buffer, SATB_PRINTER_BUFFER_SIZE, "Enqueued: %d", i);
232 print_satb_buffer(buffer, buf, nd->index(), buffer_size());
233 nd = nd->next();
234 i += 1;
235 }
236
237 class PrintThreadClosure : public ThreadClosure {
238 SATBMarkQueueSet* _qset;
239 char* _buffer;
240
241 public:
242 PrintThreadClosure(SATBMarkQueueSet* qset, char* buffer) :
243 _qset(qset), _buffer(buffer) {}
244
245 virtual void do_thread(Thread* t) {
246 os::snprintf(_buffer, SATB_PRINTER_BUFFER_SIZE, "Thread: %s", t->name());
247 _qset->satb_queue_for_thread(t).print(_buffer);
248 }
249 } closure(this, buffer);
250 Threads::threads_do(&closure);
251
252 tty->cr();
253 }
254 #endif // PRODUCT
255
256 void SATBMarkQueueSet::abandon_partial_marking() {
257 assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint.");
258 abandon_completed_buffers();
259
260 class AbandonThreadQueueClosure : public ThreadClosure {
261 SATBMarkQueueSet* _qset;
262 public:
263 AbandonThreadQueueClosure(SATBMarkQueueSet* qset) : _qset(qset) {}
264 virtual void do_thread(Thread* t) {
265 _qset->satb_queue_for_thread(t).reset();
266 }
267 } closure(this);
268 Threads::threads_do(&closure);
269 }
|