rev 58017 : [mq]: 8238854-remove-superfluous-alloc-checks
1 /*
2 * Copyright (c) 2001, 2020, 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/g1BarrierSet.hpp"
27 #include "gc/g1/g1ConcurrentRefine.hpp"
28 #include "gc/g1/g1ConcurrentRefineThread.hpp"
29 #include "gc/g1/g1DirtyCardQueue.hpp"
30 #include "logging/log.hpp"
31 #include "memory/allocation.inline.hpp"
32 #include "memory/iterator.hpp"
33 #include "runtime/java.hpp"
34 #include "runtime/thread.hpp"
35 #include "utilities/debug.hpp"
36 #include "utilities/globalDefinitions.hpp"
37 #include "utilities/pair.hpp"
38 #include <math.h>
39
40 G1ConcurrentRefineThread* G1ConcurrentRefineThreadControl::create_refinement_thread(uint worker_id, bool initializing) {
41 G1ConcurrentRefineThread* result = NULL;
42 if (initializing || !InjectGCWorkerCreationFailure) {
43 result = new G1ConcurrentRefineThread(_cr, worker_id);
44 }
45 if (result == NULL || result->osthread() == NULL) {
46 log_warning(gc)("Failed to create refinement thread %u, no more %s",
47 worker_id,
48 result == NULL ? "memory" : "OS threads");
49 }
50 return result;
51 }
52
53 G1ConcurrentRefineThreadControl::G1ConcurrentRefineThreadControl() :
54 _cr(NULL),
55 _threads(NULL),
56 _num_max_threads(0)
57 {
58 }
59
60 G1ConcurrentRefineThreadControl::~G1ConcurrentRefineThreadControl() {
61 for (uint i = 0; i < _num_max_threads; i++) {
62 G1ConcurrentRefineThread* t = _threads[i];
63 if (t != NULL) {
64 delete t;
65 }
66 }
67 FREE_C_HEAP_ARRAY(G1ConcurrentRefineThread*, _threads);
68 }
69
70 jint G1ConcurrentRefineThreadControl::initialize(G1ConcurrentRefine* cr, uint num_max_threads) {
71 assert(cr != NULL, "G1ConcurrentRefine must not be NULL");
72 _cr = cr;
73 _num_max_threads = num_max_threads;
74
75 _threads = NEW_C_HEAP_ARRAY(G1ConcurrentRefineThread*, num_max_threads, mtGC);
76
77 for (uint i = 0; i < num_max_threads; i++) {
78 if (UseDynamicNumberOfGCThreads && i != 0 /* Always start first thread. */) {
79 _threads[i] = NULL;
80 } else {
81 _threads[i] = create_refinement_thread(i, true);
82 if (_threads[i] == NULL) {
83 vm_shutdown_during_initialization("Could not allocate refinement threads.");
84 return JNI_ENOMEM;
85 }
86 }
87 }
88
89 if (num_max_threads > 0) {
90 G1BarrierSet::dirty_card_queue_set().set_primary_refinement_thread(_threads[0]);
91 }
92
93 return JNI_OK;
94 }
95
96 void G1ConcurrentRefineThreadControl::maybe_activate_next(uint cur_worker_id) {
97 assert(cur_worker_id < _num_max_threads,
98 "Activating another thread from %u not allowed since there can be at most %u",
99 cur_worker_id, _num_max_threads);
100 if (cur_worker_id == (_num_max_threads - 1)) {
101 // Already the last thread, there is no more thread to activate.
102 return;
103 }
104
105 uint worker_id = cur_worker_id + 1;
106 G1ConcurrentRefineThread* thread_to_activate = _threads[worker_id];
107 if (thread_to_activate == NULL) {
108 // Still need to create the thread...
109 _threads[worker_id] = create_refinement_thread(worker_id, false);
110 thread_to_activate = _threads[worker_id];
111 }
112 if (thread_to_activate != NULL) {
113 thread_to_activate->activate();
114 }
115 }
116
117 void G1ConcurrentRefineThreadControl::print_on(outputStream* st) const {
118 for (uint i = 0; i < _num_max_threads; ++i) {
119 if (_threads[i] != NULL) {
120 _threads[i]->print_on(st);
121 st->cr();
122 }
123 }
124 }
125
126 void G1ConcurrentRefineThreadControl::worker_threads_do(ThreadClosure* tc) {
127 for (uint i = 0; i < _num_max_threads; i++) {
128 if (_threads[i] != NULL) {
129 tc->do_thread(_threads[i]);
130 }
131 }
132 }
133
134 void G1ConcurrentRefineThreadControl::stop() {
135 for (uint i = 0; i < _num_max_threads; i++) {
136 if (_threads[i] != NULL) {
137 _threads[i]->stop();
138 }
139 }
140 }
141
142 // Arbitrary but large limits, to simplify some of the zone calculations.
143 // The general idea is to allow expressions like
144 // MIN2(x OP y, max_XXX_zone)
145 // without needing to check for overflow in "x OP y", because the
146 // ranges for x and y have been restricted.
147 STATIC_ASSERT(sizeof(LP64_ONLY(jint) NOT_LP64(jshort)) <= (sizeof(size_t)/2));
148 const size_t max_yellow_zone = LP64_ONLY(max_jint) NOT_LP64(max_jshort);
149 const size_t max_green_zone = max_yellow_zone / 2;
150 const size_t max_red_zone = INT_MAX; // For dcqs.set_max_cards.
151 STATIC_ASSERT(max_yellow_zone <= max_red_zone);
152
153 // Range check assertions for green zone values.
154 #define assert_zone_constraints_g(green) \
155 do { \
156 size_t azc_g_green = (green); \
157 assert(azc_g_green <= max_green_zone, \
158 "green exceeds max: " SIZE_FORMAT, azc_g_green); \
159 } while (0)
160
161 // Range check assertions for green and yellow zone values.
162 #define assert_zone_constraints_gy(green, yellow) \
163 do { \
164 size_t azc_gy_green = (green); \
165 size_t azc_gy_yellow = (yellow); \
166 assert_zone_constraints_g(azc_gy_green); \
167 assert(azc_gy_yellow <= max_yellow_zone, \
168 "yellow exceeds max: " SIZE_FORMAT, azc_gy_yellow); \
169 assert(azc_gy_green <= azc_gy_yellow, \
170 "green (" SIZE_FORMAT ") exceeds yellow (" SIZE_FORMAT ")", \
171 azc_gy_green, azc_gy_yellow); \
172 } while (0)
173
174 // Range check assertions for green, yellow, and red zone values.
175 #define assert_zone_constraints_gyr(green, yellow, red) \
176 do { \
177 size_t azc_gyr_green = (green); \
178 size_t azc_gyr_yellow = (yellow); \
179 size_t azc_gyr_red = (red); \
180 assert_zone_constraints_gy(azc_gyr_green, azc_gyr_yellow); \
181 assert(azc_gyr_red <= max_red_zone, \
182 "red exceeds max: " SIZE_FORMAT, azc_gyr_red); \
183 assert(azc_gyr_yellow <= azc_gyr_red, \
184 "yellow (" SIZE_FORMAT ") exceeds red (" SIZE_FORMAT ")", \
185 azc_gyr_yellow, azc_gyr_red); \
186 } while (0)
187
188 // Logging tag sequence for refinement control updates.
189 #define CTRL_TAGS gc, ergo, refine
190
191 // For logging zone values, ensuring consistency of level and tags.
192 #define LOG_ZONES(...) log_debug( CTRL_TAGS )(__VA_ARGS__)
193
194 static size_t buffers_to_cards(size_t value) {
195 return value * G1UpdateBufferSize;
196 }
197
198 // Package for pair of refinement thread activation and deactivation
199 // thresholds. The activation and deactivation levels are resp. the first
200 // and second values of the pair.
201 typedef Pair<size_t, size_t> Thresholds;
202 inline size_t activation_level(const Thresholds& t) { return t.first; }
203 inline size_t deactivation_level(const Thresholds& t) { return t.second; }
204
205 static Thresholds calc_thresholds(size_t green_zone,
206 size_t yellow_zone,
207 uint worker_id) {
208 double yellow_size = yellow_zone - green_zone;
209 double step = yellow_size / G1ConcurrentRefine::max_num_threads();
210 if (worker_id == 0) {
211 // Potentially activate worker 0 more aggressively, to keep
212 // available buffers near green_zone value. When yellow_size is
213 // large we don't want to allow a full step to accumulate before
214 // doing any processing, as that might lead to significantly more
215 // than green_zone buffers to be processed during pause. So limit
216 // to an extra half buffer per pause-time processing thread.
217 step = MIN2(step, buffers_to_cards(ParallelGCThreads) / 2.0);
218 }
219 size_t activate_offset = static_cast<size_t>(ceil(step * (worker_id + 1)));
220 size_t deactivate_offset = static_cast<size_t>(floor(step * worker_id));
221 return Thresholds(green_zone + activate_offset,
222 green_zone + deactivate_offset);
223 }
224
225 G1ConcurrentRefine::G1ConcurrentRefine(size_t green_zone,
226 size_t yellow_zone,
227 size_t red_zone,
228 size_t min_yellow_zone_size) :
229 _thread_control(),
230 _green_zone(green_zone),
231 _yellow_zone(yellow_zone),
232 _red_zone(red_zone),
233 _min_yellow_zone_size(min_yellow_zone_size)
234 {
235 assert_zone_constraints_gyr(green_zone, yellow_zone, red_zone);
236 }
237
238 jint G1ConcurrentRefine::initialize() {
239 return _thread_control.initialize(this, max_num_threads());
240 }
241
242 static size_t calc_min_yellow_zone_size() {
243 size_t step = buffers_to_cards(G1ConcRefinementThresholdStep);
244 uint n_workers = G1ConcurrentRefine::max_num_threads();
245 if ((max_yellow_zone / step) < n_workers) {
246 return max_yellow_zone;
247 } else {
248 return step * n_workers;
249 }
250 }
251
252 static size_t calc_init_green_zone() {
253 size_t green = G1ConcRefinementGreenZone;
254 if (FLAG_IS_DEFAULT(G1ConcRefinementGreenZone)) {
255 green = ParallelGCThreads;
256 }
257 green = buffers_to_cards(green);
258 return MIN2(green, max_green_zone);
259 }
260
261 static size_t calc_init_yellow_zone(size_t green, size_t min_size) {
262 size_t config = buffers_to_cards(G1ConcRefinementYellowZone);
263 size_t size = 0;
264 if (FLAG_IS_DEFAULT(G1ConcRefinementYellowZone)) {
265 size = green * 2;
266 } else if (green < config) {
267 size = config - green;
268 }
269 size = MAX2(size, min_size);
270 size = MIN2(size, max_yellow_zone);
271 return MIN2(green + size, max_yellow_zone);
272 }
273
274 static size_t calc_init_red_zone(size_t green, size_t yellow) {
275 size_t size = yellow - green;
276 if (!FLAG_IS_DEFAULT(G1ConcRefinementRedZone)) {
277 size_t config = buffers_to_cards(G1ConcRefinementRedZone);
278 if (yellow < config) {
279 size = MAX2(size, config - yellow);
280 }
281 }
282 return MIN2(yellow + size, max_red_zone);
283 }
284
285 G1ConcurrentRefine* G1ConcurrentRefine::create(jint* ecode) {
286 size_t min_yellow_zone_size = calc_min_yellow_zone_size();
287 size_t green_zone = calc_init_green_zone();
288 size_t yellow_zone = calc_init_yellow_zone(green_zone, min_yellow_zone_size);
289 size_t red_zone = calc_init_red_zone(green_zone, yellow_zone);
290
291 LOG_ZONES("Initial Refinement Zones: "
292 "green: " SIZE_FORMAT ", "
293 "yellow: " SIZE_FORMAT ", "
294 "red: " SIZE_FORMAT ", "
295 "min yellow size: " SIZE_FORMAT,
296 green_zone, yellow_zone, red_zone, min_yellow_zone_size);
297
298 G1ConcurrentRefine* cr = new G1ConcurrentRefine(green_zone,
299 yellow_zone,
300 red_zone,
301 min_yellow_zone_size);
302 *ecode = cr->initialize();
303 return cr;
304 }
305
306 void G1ConcurrentRefine::stop() {
307 _thread_control.stop();
308 }
309
310 G1ConcurrentRefine::~G1ConcurrentRefine() {
311 }
312
313 void G1ConcurrentRefine::threads_do(ThreadClosure *tc) {
314 _thread_control.worker_threads_do(tc);
315 }
316
317 uint G1ConcurrentRefine::max_num_threads() {
318 return G1ConcRefinementThreads;
319 }
320
321 void G1ConcurrentRefine::print_threads_on(outputStream* st) const {
322 _thread_control.print_on(st);
323 }
324
325 static size_t calc_new_green_zone(size_t green,
326 double logged_cards_scan_time,
327 size_t processed_logged_cards,
328 double goal_ms) {
329 // Adjust green zone based on whether we're meeting the time goal.
330 // Limit to max_green_zone.
331 const double inc_k = 1.1, dec_k = 0.9;
332 if (logged_cards_scan_time > goal_ms) {
333 if (green > 0) {
334 green = static_cast<size_t>(green * dec_k);
335 }
336 } else if (logged_cards_scan_time < goal_ms &&
337 processed_logged_cards > green) {
338 green = static_cast<size_t>(MAX2(green * inc_k, green + 1.0));
339 green = MIN2(green, max_green_zone);
340 }
341 return green;
342 }
343
344 static size_t calc_new_yellow_zone(size_t green, size_t min_yellow_size) {
345 size_t size = green * 2;
346 size = MAX2(size, min_yellow_size);
347 return MIN2(green + size, max_yellow_zone);
348 }
349
350 static size_t calc_new_red_zone(size_t green, size_t yellow) {
351 return MIN2(yellow + (yellow - green), max_red_zone);
352 }
353
354 void G1ConcurrentRefine::update_zones(double logged_cards_scan_time,
355 size_t processed_logged_cards,
356 double goal_ms) {
357 log_trace( CTRL_TAGS )("Updating Refinement Zones: "
358 "logged cards scan time: %.3fms, "
359 "processed cards: " SIZE_FORMAT ", "
360 "goal time: %.3fms",
361 logged_cards_scan_time,
362 processed_logged_cards,
363 goal_ms);
364
365 _green_zone = calc_new_green_zone(_green_zone,
366 logged_cards_scan_time,
367 processed_logged_cards,
368 goal_ms);
369 _yellow_zone = calc_new_yellow_zone(_green_zone, _min_yellow_zone_size);
370 _red_zone = calc_new_red_zone(_green_zone, _yellow_zone);
371
372 assert_zone_constraints_gyr(_green_zone, _yellow_zone, _red_zone);
373 LOG_ZONES("Updated Refinement Zones: "
374 "green: " SIZE_FORMAT ", "
375 "yellow: " SIZE_FORMAT ", "
376 "red: " SIZE_FORMAT,
377 _green_zone, _yellow_zone, _red_zone);
378 }
379
380 void G1ConcurrentRefine::adjust(double logged_cards_scan_time,
381 size_t processed_logged_cards,
382 double goal_ms) {
383 G1DirtyCardQueueSet& dcqs = G1BarrierSet::dirty_card_queue_set();
384
385 if (G1UseAdaptiveConcRefinement) {
386 update_zones(logged_cards_scan_time, processed_logged_cards, goal_ms);
387
388 // Change the barrier params
389 if (max_num_threads() == 0) {
390 // Disable dcqs notification when there are no threads to notify.
391 dcqs.set_process_cards_threshold(G1DirtyCardQueueSet::ProcessCardsThresholdNever);
392 } else {
393 // Worker 0 is the primary; wakeup is via dcqs notification.
394 STATIC_ASSERT(max_yellow_zone <= INT_MAX);
395 size_t activate = activation_threshold(0);
396 dcqs.set_process_cards_threshold(activate);
397 }
398 dcqs.set_max_cards(red_zone());
399 }
400
401 size_t curr_queue_size = dcqs.num_cards();
402 if ((dcqs.max_cards() > 0) &&
403 (curr_queue_size >= yellow_zone())) {
404 dcqs.set_max_cards_padding(curr_queue_size);
405 } else {
406 dcqs.set_max_cards_padding(0);
407 }
408 dcqs.notify_if_necessary();
409 }
410
411 G1ConcurrentRefine::RefinementStats G1ConcurrentRefine::total_refinement_stats() const {
412 struct CollectData : public ThreadClosure {
413 Tickspan _total_time;
414 size_t _total_cards;
415 CollectData() : _total_time(), _total_cards(0) {}
416 virtual void do_thread(Thread* t) {
417 G1ConcurrentRefineThread* crt = static_cast<G1ConcurrentRefineThread*>(t);
418 _total_time += crt->total_refinement_time();
419 _total_cards += crt->total_refined_cards();
420 }
421 } collector;
422 // Cast away const so we can call non-modifying closure on threads.
423 const_cast<G1ConcurrentRefine*>(this)->threads_do(&collector);
424 return RefinementStats(collector._total_time, collector._total_cards);
425 }
426
427 size_t G1ConcurrentRefine::activation_threshold(uint worker_id) const {
428 Thresholds thresholds = calc_thresholds(_green_zone, _yellow_zone, worker_id);
429 return activation_level(thresholds);
430 }
431
432 size_t G1ConcurrentRefine::deactivation_threshold(uint worker_id) const {
433 Thresholds thresholds = calc_thresholds(_green_zone, _yellow_zone, worker_id);
434 return deactivation_level(thresholds);
435 }
436
437 uint G1ConcurrentRefine::worker_id_offset() {
438 return G1DirtyCardQueueSet::num_par_ids();
439 }
440
441 void G1ConcurrentRefine::maybe_activate_more_threads(uint worker_id, size_t num_cur_cards) {
442 if (num_cur_cards > activation_threshold(worker_id + 1)) {
443 _thread_control.maybe_activate_next(worker_id);
444 }
445 }
446
447 bool G1ConcurrentRefine::do_refinement_step(uint worker_id,
448 size_t* total_refined_cards) {
449 G1DirtyCardQueueSet& dcqs = G1BarrierSet::dirty_card_queue_set();
450
451 size_t curr_cards = dcqs.num_cards();
452 // If the number of the cards falls down into the yellow zone,
453 // that means that the transition period after the evacuation pause has ended.
454 // Since the value written to the DCQS is the same for all threads, there is no
455 // need to synchronize.
456 if (dcqs.max_cards_padding() > 0 && curr_cards <= yellow_zone()) {
457 dcqs.set_max_cards_padding(0);
458 }
459
460 maybe_activate_more_threads(worker_id, curr_cards);
461
462 // Process the next buffer, if there are enough left.
463 return dcqs.refine_completed_buffer_concurrently(worker_id + worker_id_offset(),
464 deactivation_threshold(worker_id),
465 total_refined_cards);
466 }
--- EOF ---