1 /* 2 * Copyright (c) 2001, 2021, 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 #ifndef SHARE_GC_G1_HEAPREGION_INLINE_HPP 26 #define SHARE_GC_G1_HEAPREGION_INLINE_HPP 27 28 #include "gc/g1/heapRegion.hpp" 29 30 #include "gc/g1/g1BlockOffsetTable.inline.hpp" 31 #include "gc/g1/g1CollectedHeap.inline.hpp" 32 #include "gc/g1/g1ConcurrentMarkBitMap.inline.hpp" 33 #include "gc/g1/g1Predictions.hpp" 34 #include "oops/oop.inline.hpp" 35 #include "runtime/atomic.hpp" 36 #include "runtime/prefetch.inline.hpp" 37 #include "utilities/align.hpp" 38 #include "utilities/globalDefinitions.hpp" 39 40 inline HeapWord* HeapRegion::allocate_impl(size_t min_word_size, 41 size_t desired_word_size, 42 size_t* actual_size) { 43 HeapWord* obj = top(); 44 size_t available = pointer_delta(end(), obj); 45 size_t want_to_allocate = MIN2(available, desired_word_size); 46 if (want_to_allocate >= min_word_size) { 47 HeapWord* new_top = obj + want_to_allocate; 48 set_top(new_top); 49 assert(is_object_aligned(obj) && is_object_aligned(new_top), "checking alignment"); 50 *actual_size = want_to_allocate; 51 return obj; 52 } else { 53 return NULL; 54 } 55 } 56 57 inline HeapWord* HeapRegion::par_allocate_impl(size_t min_word_size, 58 size_t desired_word_size, 59 size_t* actual_size) { 60 do { 61 HeapWord* obj = top(); 62 size_t available = pointer_delta(end(), obj); 63 size_t want_to_allocate = MIN2(available, desired_word_size); 64 if (want_to_allocate >= min_word_size) { 65 HeapWord* new_top = obj + want_to_allocate; 66 HeapWord* result = Atomic::cmpxchg(&_top, obj, new_top); 67 // result can be one of two: 68 // the old top value: the exchange succeeded 69 // otherwise: the new value of the top is returned. 70 if (result == obj) { 71 assert(is_object_aligned(obj) && is_object_aligned(new_top), "checking alignment"); 72 *actual_size = want_to_allocate; 73 return obj; 74 } 75 } else { 76 return NULL; 77 } 78 } while (true); 79 } 80 81 inline HeapWord* HeapRegion::allocate(size_t min_word_size, 82 size_t desired_word_size, 83 size_t* actual_size) { 84 HeapWord* res = allocate_impl(min_word_size, desired_word_size, actual_size); 85 if (res != NULL) { 86 _bot_part.alloc_block(res, *actual_size); 87 } 88 return res; 89 } 90 91 inline HeapWord* HeapRegion::allocate(size_t word_size) { 92 size_t temp; 93 return allocate(word_size, word_size, &temp); 94 } 95 96 inline HeapWord* HeapRegion::par_allocate(size_t word_size) { 97 size_t temp; 98 return par_allocate(word_size, word_size, &temp); 99 } 100 101 // Because of the requirement of keeping "_offsets" up to date with the 102 // allocations, we sequentialize these with a lock. Therefore, best if 103 // this is used for larger LAB allocations only. 104 inline HeapWord* HeapRegion::par_allocate(size_t min_word_size, 105 size_t desired_word_size, 106 size_t* actual_size) { 107 MutexLocker x(&_par_alloc_lock); 108 return allocate(min_word_size, desired_word_size, actual_size); 109 } 110 111 inline HeapWord* HeapRegion::block_start(const void* p) { 112 return _bot_part.block_start(p); 113 } 114 115 inline HeapWord* HeapRegion::block_start_const(const void* p) const { 116 return _bot_part.block_start_const(p); 117 } 118 119 inline bool HeapRegion::is_obj_dead_with_size(const oop obj, const G1CMBitMap* const prev_bitmap, size_t* size) const { 120 HeapWord* addr = cast_from_oop<HeapWord*>(obj); 121 122 assert(addr < top(), "must be"); 123 assert(!is_closed_archive(), 124 "Closed archive regions should not have references into other regions"); 125 assert(!is_humongous(), "Humongous objects not handled here"); 126 bool obj_is_dead = is_obj_dead(obj, prev_bitmap); 127 128 if (ClassUnloading && obj_is_dead) { 129 assert(!block_is_obj(addr), "must be"); 130 *size = block_size_using_bitmap(addr, prev_bitmap); 131 } else { 132 assert(block_is_obj(addr), "must be"); 133 *size = obj->size(); 134 } 135 return obj_is_dead; 136 } 137 138 inline bool HeapRegion::block_is_obj(const HeapWord* p) const { 139 G1CollectedHeap* g1h = G1CollectedHeap::heap(); 140 141 if (!this->is_in(p)) { 142 assert(is_continues_humongous(), "This case can only happen for humongous regions"); 143 return (p == humongous_start_region()->bottom()); 144 } 145 // When class unloading is enabled it is not safe to only consider top() to conclude if the 146 // given pointer is a valid object. The situation can occur both for class unloading in a 147 // Full GC and during a concurrent cycle. 148 // During a Full GC regions can be excluded from compaction due to high live ratio, and 149 // because of this there can be stale objects for unloaded classes left in these regions. 150 // During a concurrent cycle class unloading is done after marking is complete and objects 151 // for the unloaded classes will be stale until the regions are collected. 152 if (ClassUnloading) { 153 return !g1h->is_obj_dead(cast_to_oop(p), this); 154 } 155 return p < top(); 156 } 157 158 inline size_t HeapRegion::block_size_using_bitmap(const HeapWord* addr, const G1CMBitMap* const prev_bitmap) const { 159 assert(ClassUnloading, 160 "All blocks should be objects if class unloading isn't used, so this method should not be called. " 161 "HR: [" PTR_FORMAT ", " PTR_FORMAT ", " PTR_FORMAT ") " 162 "addr: " PTR_FORMAT, 163 p2i(bottom()), p2i(top()), p2i(end()), p2i(addr)); 164 165 // Old regions' dead objects may have dead classes 166 // We need to find the next live object using the bitmap 167 HeapWord* next = prev_bitmap->get_next_marked_addr(addr, prev_top_at_mark_start()); 168 169 assert(next > addr, "must get the next live object"); 170 return pointer_delta(next, addr); 171 } 172 173 inline bool HeapRegion::is_obj_dead(const oop obj, const G1CMBitMap* const prev_bitmap) const { 174 assert(is_in_reserved(obj), "Object " PTR_FORMAT " must be in region", p2i(obj)); 175 return !obj_allocated_since_prev_marking(obj) && 176 !prev_bitmap->is_marked(obj) && 177 !is_closed_archive(); 178 } 179 180 inline size_t HeapRegion::block_size(const HeapWord *addr) const { 181 if (addr == top()) { 182 return pointer_delta(end(), addr); 183 } 184 185 if (block_is_obj(addr)) { 186 return cast_to_oop(addr)->size(); 187 } 188 189 return block_size_using_bitmap(addr, G1CollectedHeap::heap()->concurrent_mark()->prev_mark_bitmap()); 190 } 191 192 inline void HeapRegion::reset_compaction_top_after_compaction() { 193 set_top(compaction_top()); 194 _compaction_top = bottom(); 195 } 196 197 inline void HeapRegion::reset_compacted_after_full_gc() { 198 assert(!is_pinned(), "must be"); 199 200 reset_compaction_top_after_compaction(); 201 // After a compaction the mark bitmap in a non-pinned regions is invalid. 202 // We treat all objects as being above PTAMS. 203 zero_marked_bytes(); 204 init_top_at_mark_start(); 205 206 reset_after_full_gc_common(); 207 } 208 209 inline void HeapRegion::reset_skip_compacting_after_full_gc() { 210 assert(!is_free(), "must be"); 211 212 assert(compaction_top() == bottom(), 213 "region %u compaction_top " PTR_FORMAT " must not be different from bottom " PTR_FORMAT, 214 hrm_index(), p2i(compaction_top()), p2i(bottom())); 215 216 _prev_top_at_mark_start = top(); // Keep existing top and usage. 217 _prev_marked_bytes = used(); 218 _next_top_at_mark_start = bottom(); 219 _next_marked_bytes = 0; 220 221 reset_after_full_gc_common(); 222 } 223 224 inline void HeapRegion::reset_after_full_gc_common() { 225 if (is_empty()) { 226 reset_bot(); 227 } 228 229 // Clear unused heap memory in debug builds. 230 if (ZapUnusedHeapArea) { 231 mangle_unused_area(); 232 } 233 } 234 235 template<typename ApplyToMarkedClosure> 236 inline void HeapRegion::apply_to_marked_objects(G1CMBitMap* bitmap, ApplyToMarkedClosure* closure) { 237 HeapWord* limit = top(); 238 HeapWord* next_addr = bottom(); 239 240 while (next_addr < limit) { 241 Prefetch::write(next_addr, PrefetchScanIntervalInBytes); 242 // This explicit is_marked check is a way to avoid 243 // some extra work done by get_next_marked_addr for 244 // the case where next_addr is marked. 245 if (bitmap->is_marked(next_addr)) { 246 oop current = cast_to_oop(next_addr); 247 next_addr += closure->apply(current); 248 } else { 249 next_addr = bitmap->get_next_marked_addr(next_addr, limit); 250 } 251 } 252 253 assert(next_addr == limit, "Should stop the scan at the limit."); 254 } 255 256 inline HeapWord* HeapRegion::par_allocate_no_bot_updates(size_t min_word_size, 257 size_t desired_word_size, 258 size_t* actual_word_size) { 259 assert(is_young(), "we can only skip BOT updates on young regions"); 260 return par_allocate_impl(min_word_size, desired_word_size, actual_word_size); 261 } 262 263 inline HeapWord* HeapRegion::allocate_no_bot_updates(size_t word_size) { 264 size_t temp; 265 return allocate_no_bot_updates(word_size, word_size, &temp); 266 } 267 268 inline HeapWord* HeapRegion::allocate_no_bot_updates(size_t min_word_size, 269 size_t desired_word_size, 270 size_t* actual_word_size) { 271 assert(is_young(), "we can only skip BOT updates on young regions"); 272 return allocate_impl(min_word_size, desired_word_size, actual_word_size); 273 } 274 275 inline void HeapRegion::note_start_of_marking() { 276 _next_marked_bytes = 0; 277 _next_top_at_mark_start = top(); 278 _gc_efficiency = -1.0; 279 } 280 281 inline void HeapRegion::note_end_of_marking() { 282 _prev_top_at_mark_start = _next_top_at_mark_start; 283 _next_top_at_mark_start = bottom(); 284 _prev_marked_bytes = _next_marked_bytes; 285 _next_marked_bytes = 0; 286 } 287 288 inline bool HeapRegion::in_collection_set() const { 289 return G1CollectedHeap::heap()->is_in_cset(this); 290 } 291 292 template <class Closure, bool is_gc_active> 293 HeapWord* HeapRegion::do_oops_on_memregion_in_humongous(MemRegion mr, 294 Closure* cl, 295 G1CollectedHeap* g1h) { 296 assert(is_humongous(), "precondition"); 297 HeapRegion* sr = humongous_start_region(); 298 oop obj = cast_to_oop(sr->bottom()); 299 300 // If concurrent and klass_or_null is NULL, then space has been 301 // allocated but the object has not yet been published by setting 302 // the klass. That can only happen if the card is stale. However, 303 // we've already set the card clean, so we must return failure, 304 // since the allocating thread could have performed a write to the 305 // card that might be missed otherwise. 306 if (!is_gc_active && (obj->klass_or_null_acquire() == NULL)) { 307 return NULL; 308 } 309 310 // We have a well-formed humongous object at the start of sr. 311 // Only filler objects follow a humongous object in the containing 312 // regions, and we can ignore those. So only process the one 313 // humongous object. 314 if (g1h->is_obj_dead(obj, sr)) { 315 // The object is dead. There can be no other object in this region, so return 316 // the end of that region. 317 return end(); 318 } 319 if (obj->is_objArray() || (sr->bottom() < mr.start())) { 320 // objArrays are always marked precisely, so limit processing 321 // with mr. Non-objArrays might be precisely marked, and since 322 // it's humongous it's worthwhile avoiding full processing. 323 // However, the card could be stale and only cover filler 324 // objects. That should be rare, so not worth checking for; 325 // instead let it fall out from the bounded iteration. 326 obj->oop_iterate(cl, mr); 327 return mr.end(); 328 } else { 329 // If obj is not an objArray and mr contains the start of the 330 // obj, then this could be an imprecise mark, and we need to 331 // process the entire object. 332 int size = obj->oop_iterate_size(cl); 333 // We have scanned to the end of the object, but since there can be no objects 334 // after this humongous object in the region, we can return the end of the 335 // region if it is greater. 336 return MAX2(cast_from_oop<HeapWord*>(obj) + size, mr.end()); 337 } 338 } 339 340 template <bool is_gc_active, class Closure> 341 HeapWord* HeapRegion::oops_on_memregion_seq_iterate_careful(MemRegion mr, 342 Closure* cl) { 343 assert(MemRegion(bottom(), end()).contains(mr), "Card region not in heap region"); 344 G1CollectedHeap* g1h = G1CollectedHeap::heap(); 345 346 // Special handling for humongous regions. 347 if (is_humongous()) { 348 return do_oops_on_memregion_in_humongous<Closure, is_gc_active>(mr, cl, g1h); 349 } 350 assert(is_old() || is_archive(), "Wrongly trying to iterate over region %u type %s", _hrm_index, get_type_str()); 351 352 // Because mr has been trimmed to what's been allocated in this 353 // region, the parts of the heap that are examined here are always 354 // parsable; there's no need to use klass_or_null to detect 355 // in-progress allocation. 356 357 // Cache the boundaries of the memory region in some const locals 358 HeapWord* const start = mr.start(); 359 HeapWord* const end = mr.end(); 360 361 // Find the obj that extends onto mr.start(). 362 // Update BOT as needed while finding start of (possibly dead) 363 // object containing the start of the region. 364 HeapWord* cur = block_start(start); 365 366 #ifdef ASSERT 367 { 368 assert(cur <= start, 369 "cur: " PTR_FORMAT ", start: " PTR_FORMAT, p2i(cur), p2i(start)); 370 HeapWord* next = cur + block_size(cur); 371 assert(start < next, 372 "start: " PTR_FORMAT ", next: " PTR_FORMAT, p2i(start), p2i(next)); 373 } 374 #endif 375 376 const G1CMBitMap* const bitmap = g1h->concurrent_mark()->prev_mark_bitmap(); 377 while (true) { 378 oop obj = cast_to_oop(cur); 379 assert(oopDesc::is_oop(obj, true), "Not an oop at " PTR_FORMAT, p2i(cur)); 380 assert(obj->klass_or_null() != NULL, 381 "Unparsable heap at " PTR_FORMAT, p2i(cur)); 382 383 size_t size; 384 bool is_dead = is_obj_dead_with_size(obj, bitmap, &size); 385 bool is_precise = false; 386 387 cur += size; 388 if (!is_dead) { 389 // Process live object's references. 390 391 // Non-objArrays are usually marked imprecise at the object 392 // start, in which case we need to iterate over them in full. 393 // objArrays are precisely marked, but can still be iterated 394 // over in full if completely covered. 395 if (!obj->is_objArray() || (cast_from_oop<HeapWord*>(obj) >= start && cur <= end)) { 396 obj->oop_iterate(cl); 397 } else { 398 obj->oop_iterate(cl, mr); 399 is_precise = true; 400 } 401 } 402 if (cur >= end) { 403 return is_precise ? end : cur; 404 } 405 } 406 } 407 408 inline int HeapRegion::age_in_surv_rate_group() const { 409 assert(has_surv_rate_group(), "pre-condition"); 410 assert(has_valid_age_in_surv_rate(), "pre-condition"); 411 return _surv_rate_group->age_in_group(_age_index); 412 } 413 414 inline bool HeapRegion::has_valid_age_in_surv_rate() const { 415 return G1SurvRateGroup::is_valid_age_index(_age_index); 416 } 417 418 inline bool HeapRegion::has_surv_rate_group() const { 419 return _surv_rate_group != NULL; 420 } 421 422 inline double HeapRegion::surv_rate_prediction(G1Predictions const& predictor) const { 423 assert(has_surv_rate_group(), "pre-condition"); 424 return _surv_rate_group->surv_rate_pred(predictor, age_in_surv_rate_group()); 425 } 426 427 inline void HeapRegion::install_surv_rate_group(G1SurvRateGroup* surv_rate_group) { 428 assert(surv_rate_group != NULL, "pre-condition"); 429 assert(!has_surv_rate_group(), "pre-condition"); 430 assert(is_young(), "pre-condition"); 431 432 _surv_rate_group = surv_rate_group; 433 _age_index = surv_rate_group->next_age_index(); 434 } 435 436 inline void HeapRegion::uninstall_surv_rate_group() { 437 if (has_surv_rate_group()) { 438 assert(has_valid_age_in_surv_rate(), "pre-condition"); 439 assert(is_young(), "pre-condition"); 440 441 _surv_rate_group = NULL; 442 _age_index = G1SurvRateGroup::InvalidAgeIndex; 443 } else { 444 assert(!has_valid_age_in_surv_rate(), "pre-condition"); 445 } 446 } 447 448 inline void HeapRegion::record_surv_words_in_group(size_t words_survived) { 449 assert(has_surv_rate_group(), "pre-condition"); 450 assert(has_valid_age_in_surv_rate(), "pre-condition"); 451 int age_in_group = age_in_surv_rate_group(); 452 _surv_rate_group->record_surviving_words(age_in_group, words_survived); 453 } 454 455 #endif // SHARE_GC_G1_HEAPREGION_INLINE_HPP