annotate src/share/vm/gc_implementation/g1/heapRegion.hpp @ 5188:5afc5a089c2c

8014555: G1: Memory ordering problem with Conc refinement and card marking Summary: Add a StoreLoad barrier in the G1 post-barrier to fix a race with concurrent refinement. Reviewed-by: brutisso, tschatzl, roland
author mgerdin
date Tue, 08 Oct 2013 17:35:51 +0200
parents a2f7274eb6ef
children b86041bd7b99
rev   line source
ysr@345 1 /*
johnc@3276 2 * Copyright (c) 2001, 2012, Oracle and/or its affiliates. All rights reserved.
ysr@345 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ysr@345 4 *
ysr@345 5 * This code is free software; you can redistribute it and/or modify it
ysr@345 6 * under the terms of the GNU General Public License version 2 only, as
ysr@345 7 * published by the Free Software Foundation.
ysr@345 8 *
ysr@345 9 * This code is distributed in the hope that it will be useful, but WITHOUT
ysr@345 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ysr@345 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ysr@345 12 * version 2 for more details (a copy is included in the LICENSE file that
ysr@345 13 * accompanied this code).
ysr@345 14 *
ysr@345 15 * You should have received a copy of the GNU General Public License version
ysr@345 16 * 2 along with this work; if not, write to the Free Software Foundation,
ysr@345 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ysr@345 18 *
trims@1563 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1563 20 * or visit www.oracle.com if you need additional information or have any
trims@1563 21 * questions.
ysr@345 22 *
ysr@345 23 */
ysr@345 24
stefank@1992 25 #ifndef SHARE_VM_GC_IMPLEMENTATION_G1_HEAPREGION_HPP
stefank@1992 26 #define SHARE_VM_GC_IMPLEMENTATION_G1_HEAPREGION_HPP
stefank@1992 27
stefank@1992 28 #include "gc_implementation/g1/g1BlockOffsetTable.inline.hpp"
stefank@1992 29 #include "gc_implementation/g1/g1_specialized_oop_closures.hpp"
stefank@1992 30 #include "gc_implementation/g1/survRateGroup.hpp"
stefank@1992 31 #include "gc_implementation/shared/ageTable.hpp"
stefank@1992 32 #include "gc_implementation/shared/spaceDecorator.hpp"
stefank@1992 33 #include "memory/space.inline.hpp"
stefank@1992 34 #include "memory/watermark.hpp"
stefank@1992 35
ysr@345 36 #ifndef SERIALGC
ysr@345 37
ysr@345 38 // A HeapRegion is the smallest piece of a G1CollectedHeap that
ysr@345 39 // can be collected independently.
ysr@345 40
ysr@345 41 // NOTE: Although a HeapRegion is a Space, its
ysr@345 42 // Space::initDirtyCardClosure method must not be called.
ysr@345 43 // The problem is that the existence of this method breaks
ysr@345 44 // the independence of barrier sets from remembered sets.
ysr@345 45 // The solution is to remove this method from the definition
ysr@345 46 // of a Space.
ysr@345 47
ysr@345 48 class CompactibleSpace;
ysr@345 49 class ContiguousSpace;
ysr@345 50 class HeapRegionRemSet;
ysr@345 51 class HeapRegionRemSetIterator;
ysr@345 52 class HeapRegion;
tonyp@2165 53 class HeapRegionSetBase;
johnc@5169 54 class nmethod;
tonyp@2165 55
tonyp@3953 56 #define HR_FORMAT "%u:(%s)["PTR_FORMAT","PTR_FORMAT","PTR_FORMAT"]"
tonyp@2761 57 #define HR_FORMAT_PARAMS(_hr_) \
tonyp@2761 58 (_hr_)->hrs_index(), \
tonyp@4192 59 (_hr_)->is_survivor() ? "S" : (_hr_)->is_young() ? "E" : \
tonyp@4192 60 (_hr_)->startsHumongous() ? "HS" : \
tonyp@4192 61 (_hr_)->continuesHumongous() ? "HC" : \
tonyp@4192 62 !(_hr_)->is_empty() ? "O" : "F", \
tonyp@2761 63 (_hr_)->bottom(), (_hr_)->top(), (_hr_)->end()
ysr@345 64
tonyp@3953 65 // sentinel value for hrs_index
tonyp@3953 66 #define G1_NULL_HRS_INDEX ((uint) -1)
tonyp@3953 67
ysr@345 68 // A dirty card to oop closure for heap regions. It
ysr@345 69 // knows how to get the G1 heap and how to use the bitmap
ysr@345 70 // in the concurrent marker used by G1 to filter remembered
ysr@345 71 // sets.
ysr@345 72
ysr@345 73 class HeapRegionDCTOC : public ContiguousSpaceDCTOC {
ysr@345 74 public:
ysr@345 75 // Specification of possible DirtyCardToOopClosure filtering.
ysr@345 76 enum FilterKind {
ysr@345 77 NoFilterKind,
ysr@345 78 IntoCSFilterKind,
ysr@345 79 OutOfRegionFilterKind
ysr@345 80 };
ysr@345 81
ysr@345 82 protected:
ysr@345 83 HeapRegion* _hr;
ysr@345 84 FilterKind _fk;
ysr@345 85 G1CollectedHeap* _g1;
ysr@345 86
ysr@345 87 void walk_mem_region_with_cl(MemRegion mr,
ysr@345 88 HeapWord* bottom, HeapWord* top,
ysr@345 89 OopClosure* cl);
ysr@345 90
ysr@345 91 // We don't specialize this for FilteringClosure; filtering is handled by
ysr@345 92 // the "FilterKind" mechanism. But we provide this to avoid a compiler
ysr@345 93 // warning.
ysr@345 94 void walk_mem_region_with_cl(MemRegion mr,
ysr@345 95 HeapWord* bottom, HeapWord* top,
ysr@345 96 FilteringClosure* cl) {
ysr@345 97 HeapRegionDCTOC::walk_mem_region_with_cl(mr, bottom, top,
ysr@345 98 (OopClosure*)cl);
ysr@345 99 }
ysr@345 100
ysr@345 101 // Get the actual top of the area on which the closure will
ysr@345 102 // operate, given where the top is assumed to be (the end of the
ysr@345 103 // memory region passed to do_MemRegion) and where the object
ysr@345 104 // at the top is assumed to start. For example, an object may
ysr@345 105 // start at the top but actually extend past the assumed top,
ysr@345 106 // in which case the top becomes the end of the object.
ysr@345 107 HeapWord* get_actual_top(HeapWord* top, HeapWord* top_obj) {
ysr@345 108 return ContiguousSpaceDCTOC::get_actual_top(top, top_obj);
ysr@345 109 }
ysr@345 110
ysr@345 111 // Walk the given memory region from bottom to (actual) top
ysr@345 112 // looking for objects and applying the oop closure (_cl) to
ysr@345 113 // them. The base implementation of this treats the area as
ysr@345 114 // blocks, where a block may or may not be an object. Sub-
ysr@345 115 // classes should override this to provide more accurate
ysr@345 116 // or possibly more efficient walking.
ysr@345 117 void walk_mem_region(MemRegion mr, HeapWord* bottom, HeapWord* top) {
ysr@345 118 Filtering_DCTOC::walk_mem_region(mr, bottom, top);
ysr@345 119 }
ysr@345 120
ysr@345 121 public:
ysr@345 122 HeapRegionDCTOC(G1CollectedHeap* g1,
ysr@345 123 HeapRegion* hr, OopClosure* cl,
ysr@345 124 CardTableModRefBS::PrecisionStyle precision,
ysr@345 125 FilterKind fk);
ysr@345 126 };
ysr@345 127
ysr@345 128 // The complicating factor is that BlockOffsetTable diverged
ysr@345 129 // significantly, and we need functionality that is only in the G1 version.
ysr@345 130 // So I copied that code, which led to an alternate G1 version of
ysr@345 131 // OffsetTableContigSpace. If the two versions of BlockOffsetTable could
ysr@345 132 // be reconciled, then G1OffsetTableContigSpace could go away.
ysr@345 133
ysr@345 134 // The idea behind time stamps is the following. Doing a save_marks on
ysr@345 135 // all regions at every GC pause is time consuming (if I remember
ysr@345 136 // well, 10ms or so). So, we would like to do that only for regions
ysr@345 137 // that are GC alloc regions. To achieve this, we use time
ysr@345 138 // stamps. For every evacuation pause, G1CollectedHeap generates a
ysr@345 139 // unique time stamp (essentially a counter that gets
ysr@345 140 // incremented). Every time we want to call save_marks on a region,
ysr@345 141 // we set the saved_mark_word to top and also copy the current GC
ysr@345 142 // time stamp to the time stamp field of the space. Reading the
ysr@345 143 // saved_mark_word involves checking the time stamp of the
ysr@345 144 // region. If it is the same as the current GC time stamp, then we
ysr@345 145 // can safely read the saved_mark_word field, as it is valid. If the
ysr@345 146 // time stamp of the region is not the same as the current GC time
ysr@345 147 // stamp, then we instead read top, as the saved_mark_word field is
ysr@345 148 // invalid. Time stamps (on the regions and also on the
ysr@345 149 // G1CollectedHeap) are reset at every cleanup (we iterate over
ysr@345 150 // the regions anyway) and at the end of a Full GC. The current scheme
ysr@345 151 // that uses sequential unsigned ints will fail only if we have 4b
ysr@345 152 // evacuation pauses between two cleanups, which is _highly_ unlikely.
ysr@345 153
ysr@345 154 class G1OffsetTableContigSpace: public ContiguousSpace {
ysr@345 155 friend class VMStructs;
ysr@345 156 protected:
ysr@345 157 G1BlockOffsetArrayContigSpace _offsets;
ysr@345 158 Mutex _par_alloc_lock;
ysr@345 159 volatile unsigned _gc_time_stamp;
tonyp@2417 160 // When we need to retire an allocation region, while other threads
tonyp@2417 161 // are also concurrently trying to allocate into it, we typically
tonyp@2417 162 // allocate a dummy object at the end of the region to ensure that
tonyp@2417 163 // no more allocations can take place in it. However, sometimes we
tonyp@2417 164 // want to know where the end of the last "real" object we allocated
tonyp@2417 165 // into the region was and this is what this keeps track.
tonyp@2417 166 HeapWord* _pre_dummy_top;
ysr@345 167
ysr@345 168 public:
ysr@345 169 // Constructor. If "is_zeroed" is true, the MemRegion "mr" may be
ysr@345 170 // assumed to contain zeros.
ysr@345 171 G1OffsetTableContigSpace(G1BlockOffsetSharedArray* sharedOffsetArray,
ysr@345 172 MemRegion mr, bool is_zeroed = false);
ysr@345 173
ysr@345 174 void set_bottom(HeapWord* value);
ysr@345 175 void set_end(HeapWord* value);
ysr@345 176
ysr@345 177 virtual HeapWord* saved_mark_word() const;
ysr@345 178 virtual void set_saved_mark();
ysr@345 179 void reset_gc_time_stamp() { _gc_time_stamp = 0; }
tonyp@4192 180 unsigned get_gc_time_stamp() { return _gc_time_stamp; }
ysr@345 181
tonyp@2417 182 // See the comment above in the declaration of _pre_dummy_top for an
tonyp@2417 183 // explanation of what it is.
tonyp@2417 184 void set_pre_dummy_top(HeapWord* pre_dummy_top) {
tonyp@2417 185 assert(is_in(pre_dummy_top) && pre_dummy_top <= top(), "pre-condition");
tonyp@2417 186 _pre_dummy_top = pre_dummy_top;
tonyp@2417 187 }
tonyp@2417 188 HeapWord* pre_dummy_top() {
tonyp@2417 189 return (_pre_dummy_top == NULL) ? top() : _pre_dummy_top;
tonyp@2417 190 }
tonyp@2417 191 void reset_pre_dummy_top() { _pre_dummy_top = NULL; }
tonyp@2417 192
tonyp@359 193 virtual void initialize(MemRegion mr, bool clear_space, bool mangle_space);
tonyp@359 194 virtual void clear(bool mangle_space);
ysr@345 195
ysr@345 196 HeapWord* block_start(const void* p);
ysr@345 197 HeapWord* block_start_const(const void* p) const;
ysr@345 198
ysr@345 199 // Add offset table update.
ysr@345 200 virtual HeapWord* allocate(size_t word_size);
ysr@345 201 HeapWord* par_allocate(size_t word_size);
ysr@345 202
ysr@345 203 // MarkSweep support phase3
ysr@345 204 virtual HeapWord* initialize_threshold();
ysr@345 205 virtual HeapWord* cross_threshold(HeapWord* start, HeapWord* end);
ysr@345 206
ysr@345 207 virtual void print() const;
tonyp@2146 208
tonyp@2146 209 void reset_bot() {
tonyp@2146 210 _offsets.zero_bottom_entry();
tonyp@2146 211 _offsets.initialize_threshold();
tonyp@2146 212 }
tonyp@2146 213
tonyp@2146 214 void update_bot_for_object(HeapWord* start, size_t word_size) {
tonyp@2146 215 _offsets.alloc_block(start, word_size);
tonyp@2146 216 }
tonyp@2146 217
tonyp@2146 218 void print_bot_on(outputStream* out) {
tonyp@2146 219 _offsets.print_on(out);
tonyp@2146 220 }
ysr@345 221 };
ysr@345 222
ysr@345 223 class HeapRegion: public G1OffsetTableContigSpace {
ysr@345 224 friend class VMStructs;
ysr@345 225 private:
ysr@345 226
tonyp@358 227 enum HumongousType {
tonyp@358 228 NotHumongous = 0,
tonyp@358 229 StartsHumongous,
tonyp@358 230 ContinuesHumongous
tonyp@358 231 };
tonyp@358 232
ysr@345 233 // Requires that the region "mr" be dense with objects, and begin and end
ysr@345 234 // with an object.
ysr@345 235 void oops_in_mr_iterate(MemRegion mr, OopClosure* cl);
ysr@345 236
ysr@345 237 // The remembered set for this region.
ysr@345 238 // (Might want to make this "inline" later, to avoid some alloc failure
ysr@345 239 // issues.)
ysr@345 240 HeapRegionRemSet* _rem_set;
ysr@345 241
ysr@345 242 G1BlockOffsetArrayContigSpace* offsets() { return &_offsets; }
ysr@345 243
ysr@345 244 protected:
tonyp@2761 245 // The index of this region in the heap region sequence.
tonyp@3953 246 uint _hrs_index;
ysr@345 247
tonyp@358 248 HumongousType _humongous_type;
ysr@345 249 // For a humongous region, region in which it starts.
ysr@345 250 HeapRegion* _humongous_start_region;
ysr@345 251 // For the start region of a humongous sequence, it's original end().
ysr@345 252 HeapWord* _orig_end;
ysr@345 253
ysr@345 254 // True iff the region is in current collection_set.
ysr@345 255 bool _in_collection_set;
ysr@345 256
ysr@345 257 // True iff an attempt to evacuate an object in the region failed.
ysr@345 258 bool _evacuation_failed;
ysr@345 259
ysr@345 260 // A heap region may be a member one of a number of special subsets, each
ysr@345 261 // represented as linked lists through the field below. Currently, these
ysr@345 262 // sets include:
ysr@345 263 // The collection set.
ysr@345 264 // The set of allocation regions used in a collection pause.
ysr@345 265 // Spaces that may contain gray objects.
ysr@345 266 HeapRegion* _next_in_special_set;
ysr@345 267
ysr@345 268 // next region in the young "generation" region set
ysr@345 269 HeapRegion* _next_young_region;
ysr@345 270
apetrusenko@844 271 // Next region whose cards need cleaning
apetrusenko@844 272 HeapRegion* _next_dirty_cards_region;
apetrusenko@844 273
tonyp@2165 274 // Fields used by the HeapRegionSetBase class and subclasses.
tonyp@2165 275 HeapRegion* _next;
tonyp@2165 276 #ifdef ASSERT
tonyp@2165 277 HeapRegionSetBase* _containing_set;
tonyp@2165 278 #endif // ASSERT
tonyp@2165 279 bool _pending_removal;
tonyp@2165 280
ysr@345 281 // For parallel heapRegion traversal.
ysr@345 282 jint _claimed;
ysr@345 283
ysr@345 284 // We use concurrent marking to determine the amount of live data
ysr@345 285 // in each heap region.
ysr@345 286 size_t _prev_marked_bytes; // Bytes known to be live via last completed marking.
ysr@345 287 size_t _next_marked_bytes; // Bytes known to be live via in-progress marking.
ysr@345 288
tonyp@3954 289 // The calculated GC efficiency of the region.
ysr@345 290 double _gc_efficiency;
ysr@345 291
ysr@345 292 enum YoungType {
ysr@345 293 NotYoung, // a region is not young
ysr@345 294 Young, // a region is young
tonyp@2761 295 Survivor // a region is young and it contains survivors
ysr@345 296 };
ysr@345 297
johnc@1685 298 volatile YoungType _young_type;
ysr@345 299 int _young_index_in_cset;
ysr@345 300 SurvRateGroup* _surv_rate_group;
ysr@345 301 int _age_index;
ysr@345 302
ysr@345 303 // The start of the unmarked area. The unmarked area extends from this
ysr@345 304 // word until the top and/or end of the region, and is the part
ysr@345 305 // of the region for which no marking was done, i.e. objects may
ysr@345 306 // have been allocated in this part since the last mark phase.
ysr@345 307 // "prev" is the top at the start of the last completed marking.
ysr@345 308 // "next" is the top at the start of the in-progress marking (if any.)
ysr@345 309 HeapWord* _prev_top_at_mark_start;
ysr@345 310 HeapWord* _next_top_at_mark_start;
ysr@345 311 // If a collection pause is in progress, this is the top at the start
ysr@345 312 // of that pause.
ysr@345 313
ysr@345 314 void init_top_at_mark_start() {
ysr@345 315 assert(_prev_marked_bytes == 0 &&
ysr@345 316 _next_marked_bytes == 0,
ysr@345 317 "Must be called after zero_marked_bytes.");
ysr@345 318 HeapWord* bot = bottom();
ysr@345 319 _prev_top_at_mark_start = bot;
ysr@345 320 _next_top_at_mark_start = bot;
ysr@345 321 }
ysr@345 322
ysr@345 323 void set_young_type(YoungType new_type) {
ysr@345 324 //assert(_young_type != new_type, "setting the same type" );
ysr@345 325 // TODO: add more assertions here
ysr@345 326 _young_type = new_type;
ysr@345 327 }
ysr@345 328
johnc@1483 329 // Cached attributes used in the collection set policy information
johnc@1483 330
johnc@1483 331 // The RSet length that was added to the total value
johnc@1483 332 // for the collection set.
johnc@1483 333 size_t _recorded_rs_length;
johnc@1483 334
johnc@1483 335 // The predicted elapsed time that was added to total value
johnc@1483 336 // for the collection set.
johnc@1483 337 double _predicted_elapsed_time_ms;
johnc@1483 338
johnc@1483 339 // The predicted number of bytes to copy that was added to
johnc@1483 340 // the total value for the collection set.
johnc@1483 341 size_t _predicted_bytes_to_copy;
johnc@1483 342
ysr@345 343 public:
ysr@345 344 // If "is_zeroed" is "true", the region "mr" can be assumed to contain zeros.
tonyp@3953 345 HeapRegion(uint hrs_index,
tonyp@2761 346 G1BlockOffsetSharedArray* sharedOffsetArray,
ysr@345 347 MemRegion mr, bool is_zeroed);
ysr@345 348
johnc@3035 349 static int LogOfHRGrainBytes;
johnc@3035 350 static int LogOfHRGrainWords;
johnc@3035 351
johnc@3035 352 static size_t GrainBytes;
johnc@3035 353 static size_t GrainWords;
johnc@3035 354 static size_t CardsPerRegion;
tonyp@996 355
tonyp@2968 356 static size_t align_up_to_region_byte_size(size_t sz) {
tonyp@2968 357 return (sz + (size_t) GrainBytes - 1) &
tonyp@2968 358 ~((1 << (size_t) LogOfHRGrainBytes) - 1);
tonyp@2968 359 }
tonyp@2968 360
tonyp@996 361 // It sets up the heap region size (GrainBytes / GrainWords), as
tonyp@996 362 // well as other related fields that are based on the heap region
tonyp@996 363 // size (LogOfHRGrainBytes / LogOfHRGrainWords /
tonyp@996 364 // CardsPerRegion). All those fields are considered constant
tonyp@996 365 // throughout the JVM's execution, therefore they should only be set
tonyp@996 366 // up once during initialization time.
tonyp@996 367 static void setup_heap_region_size(uintx min_heap_size);
ysr@345 368
tonyp@358 369 enum ClaimValues {
johnc@3156 370 InitialClaimValue = 0,
johnc@3156 371 FinalCountClaimValue = 1,
johnc@3156 372 NoteEndClaimValue = 2,
johnc@3156 373 ScrubRemSetClaimValue = 3,
johnc@3156 374 ParVerifyClaimValue = 4,
johnc@3156 375 RebuildRSClaimValue = 5,
tonyp@3931 376 ParEvacFailureClaimValue = 6,
tonyp@3931 377 AggregateCountClaimValue = 7,
johnc@5169 378 VerifyCountClaimValue = 8,
johnc@5169 379 ParMarkRootClaimValue = 9
tonyp@358 380 };
tonyp@358 381
tonyp@2147 382 inline HeapWord* par_allocate_no_bot_updates(size_t word_size) {
tonyp@2147 383 assert(is_young(), "we can only skip BOT updates on young regions");
tonyp@2147 384 return ContiguousSpace::par_allocate(word_size);
tonyp@2147 385 }
tonyp@2147 386 inline HeapWord* allocate_no_bot_updates(size_t word_size) {
tonyp@2147 387 assert(is_young(), "we can only skip BOT updates on young regions");
tonyp@2147 388 return ContiguousSpace::allocate(word_size);
tonyp@2147 389 }
tonyp@2147 390
ysr@345 391 // If this region is a member of a HeapRegionSeq, the index in that
ysr@345 392 // sequence, otherwise -1.
tonyp@3953 393 uint hrs_index() const { return _hrs_index; }
ysr@345 394
ysr@345 395 // The number of bytes marked live in the region in the last marking phase.
ysr@345 396 size_t marked_bytes() { return _prev_marked_bytes; }
tonyp@2419 397 size_t live_bytes() {
tonyp@2419 398 return (top() - prev_top_at_mark_start()) * HeapWordSize + marked_bytes();
tonyp@2419 399 }
tonyp@2419 400
ysr@345 401 // The number of bytes counted in the next marking.
ysr@345 402 size_t next_marked_bytes() { return _next_marked_bytes; }
ysr@345 403 // The number of bytes live wrt the next marking.
ysr@345 404 size_t next_live_bytes() {
tonyp@2419 405 return
tonyp@2419 406 (top() - next_top_at_mark_start()) * HeapWordSize + next_marked_bytes();
ysr@345 407 }
ysr@345 408
ysr@345 409 // A lower bound on the amount of garbage bytes in the region.
ysr@345 410 size_t garbage_bytes() {
ysr@345 411 size_t used_at_mark_start_bytes =
ysr@345 412 (prev_top_at_mark_start() - bottom()) * HeapWordSize;
ysr@345 413 assert(used_at_mark_start_bytes >= marked_bytes(),
ysr@345 414 "Can't mark more than we have.");
ysr@345 415 return used_at_mark_start_bytes - marked_bytes();
ysr@345 416 }
ysr@345 417
tonyp@3414 418 // Return the amount of bytes we'll reclaim if we collect this
tonyp@3414 419 // region. This includes not only the known garbage bytes in the
tonyp@3414 420 // region but also any unallocated space in it, i.e., [top, end),
tonyp@3414 421 // since it will also be reclaimed if we collect the region.
tonyp@3414 422 size_t reclaimable_bytes() {
tonyp@3414 423 size_t known_live_bytes = live_bytes();
tonyp@3414 424 assert(known_live_bytes <= capacity(), "sanity");
tonyp@3414 425 return capacity() - known_live_bytes;
tonyp@3414 426 }
tonyp@3414 427
ysr@345 428 // An upper bound on the number of live bytes in the region.
ysr@345 429 size_t max_live_bytes() { return used() - garbage_bytes(); }
ysr@345 430
ysr@345 431 void add_to_marked_bytes(size_t incr_bytes) {
ysr@345 432 _next_marked_bytes = _next_marked_bytes + incr_bytes;
johnc@3152 433 assert(_next_marked_bytes <= used(), "invariant" );
ysr@345 434 }
ysr@345 435
ysr@345 436 void zero_marked_bytes() {
ysr@345 437 _prev_marked_bytes = _next_marked_bytes = 0;
ysr@345 438 }
ysr@345 439
tonyp@358 440 bool isHumongous() const { return _humongous_type != NotHumongous; }
tonyp@358 441 bool startsHumongous() const { return _humongous_type == StartsHumongous; }
tonyp@358 442 bool continuesHumongous() const { return _humongous_type == ContinuesHumongous; }
ysr@345 443 // For a humongous region, region in which it starts.
ysr@345 444 HeapRegion* humongous_start_region() const {
ysr@345 445 return _humongous_start_region;
ysr@345 446 }
ysr@345 447
tonyp@4192 448 // Return the number of distinct regions that are covered by this region:
tonyp@4192 449 // 1 if the region is not humongous, >= 1 if the region is humongous.
tonyp@4192 450 uint region_num() const {
tonyp@4192 451 if (!isHumongous()) {
tonyp@4192 452 return 1U;
tonyp@4192 453 } else {
tonyp@4192 454 assert(startsHumongous(), "doesn't make sense on HC regions");
tonyp@4192 455 assert(capacity() % HeapRegion::GrainBytes == 0, "sanity");
tonyp@4192 456 return (uint) (capacity() >> HeapRegion::LogOfHRGrainBytes);
tonyp@4192 457 }
tonyp@4192 458 }
tonyp@4192 459
tonyp@4192 460 // Return the index + 1 of the last HC regions that's associated
tonyp@4192 461 // with this HS region.
tonyp@4192 462 uint last_hc_index() const {
tonyp@4192 463 assert(startsHumongous(), "don't call this otherwise");
tonyp@4192 464 return hrs_index() + region_num();
tonyp@4192 465 }
tonyp@4192 466
brutisso@3071 467 // Same as Space::is_in_reserved, but will use the original size of the region.
brutisso@3071 468 // The original size is different only for start humongous regions. They get
brutisso@3071 469 // their _end set up to be the end of the last continues region of the
brutisso@3071 470 // corresponding humongous object.
brutisso@3071 471 bool is_in_reserved_raw(const void* p) const {
brutisso@3071 472 return _bottom <= p && p < _orig_end;
brutisso@3071 473 }
brutisso@3071 474
tonyp@2146 475 // Makes the current region be a "starts humongous" region, i.e.,
tonyp@2146 476 // the first region in a series of one or more contiguous regions
tonyp@2146 477 // that will contain a single "humongous" object. The two parameters
tonyp@2146 478 // are as follows:
tonyp@2146 479 //
tonyp@2146 480 // new_top : The new value of the top field of this region which
tonyp@2146 481 // points to the end of the humongous object that's being
tonyp@2146 482 // allocated. If there is more than one region in the series, top
tonyp@2146 483 // will lie beyond this region's original end field and on the last
tonyp@2146 484 // region in the series.
tonyp@2146 485 //
tonyp@2146 486 // new_end : The new value of the end field of this region which
tonyp@2146 487 // points to the end of the last region in the series. If there is
tonyp@2146 488 // one region in the series (namely: this one) end will be the same
tonyp@2146 489 // as the original end of this region.
tonyp@2146 490 //
tonyp@2146 491 // Updating top and end as described above makes this region look as
tonyp@2146 492 // if it spans the entire space taken up by all the regions in the
tonyp@2146 493 // series and an single allocation moved its top to new_top. This
tonyp@2146 494 // ensures that the space (capacity / allocated) taken up by all
tonyp@2146 495 // humongous regions can be calculated by just looking at the
tonyp@2146 496 // "starts humongous" regions and by ignoring the "continues
tonyp@2146 497 // humongous" regions.
tonyp@2146 498 void set_startsHumongous(HeapWord* new_top, HeapWord* new_end);
ysr@345 499
tonyp@2146 500 // Makes the current region be a "continues humongous'
tonyp@2146 501 // region. first_hr is the "start humongous" region of the series
tonyp@2146 502 // which this region will be part of.
tonyp@2146 503 void set_continuesHumongous(HeapRegion* first_hr);
ysr@345 504
tonyp@2165 505 // Unsets the humongous-related fields on the region.
tonyp@2165 506 void set_notHumongous();
tonyp@2165 507
ysr@345 508 // If the region has a remembered set, return a pointer to it.
ysr@345 509 HeapRegionRemSet* rem_set() const {
ysr@345 510 return _rem_set;
ysr@345 511 }
ysr@345 512
ysr@345 513 // True iff the region is in current collection_set.
ysr@345 514 bool in_collection_set() const {
ysr@345 515 return _in_collection_set;
ysr@345 516 }
ysr@345 517 void set_in_collection_set(bool b) {
ysr@345 518 _in_collection_set = b;
ysr@345 519 }
ysr@345 520 HeapRegion* next_in_collection_set() {
ysr@345 521 assert(in_collection_set(), "should only invoke on member of CS.");
ysr@345 522 assert(_next_in_special_set == NULL ||
ysr@345 523 _next_in_special_set->in_collection_set(),
ysr@345 524 "Malformed CS.");
ysr@345 525 return _next_in_special_set;
ysr@345 526 }
ysr@345 527 void set_next_in_collection_set(HeapRegion* r) {
ysr@345 528 assert(in_collection_set(), "should only invoke on member of CS.");
ysr@345 529 assert(r == NULL || r->in_collection_set(), "Malformed CS.");
ysr@345 530 _next_in_special_set = r;
ysr@345 531 }
ysr@345 532
tonyp@2165 533 // Methods used by the HeapRegionSetBase class and subclasses.
tonyp@2165 534
tonyp@2165 535 // Getter and setter for the next field used to link regions into
tonyp@2165 536 // linked lists.
tonyp@2165 537 HeapRegion* next() { return _next; }
tonyp@2165 538
tonyp@2165 539 void set_next(HeapRegion* next) { _next = next; }
tonyp@2165 540
tonyp@2165 541 // Every region added to a set is tagged with a reference to that
tonyp@2165 542 // set. This is used for doing consistency checking to make sure that
tonyp@2165 543 // the contents of a set are as they should be and it's only
tonyp@2165 544 // available in non-product builds.
tonyp@2165 545 #ifdef ASSERT
tonyp@2165 546 void set_containing_set(HeapRegionSetBase* containing_set) {
tonyp@2165 547 assert((containing_set == NULL && _containing_set != NULL) ||
tonyp@2165 548 (containing_set != NULL && _containing_set == NULL),
tonyp@2165 549 err_msg("containing_set: "PTR_FORMAT" "
tonyp@2165 550 "_containing_set: "PTR_FORMAT,
tonyp@2165 551 containing_set, _containing_set));
tonyp@2165 552
tonyp@2165 553 _containing_set = containing_set;
tonyp@2340 554 }
tonyp@2165 555
tonyp@2165 556 HeapRegionSetBase* containing_set() { return _containing_set; }
tonyp@2165 557 #else // ASSERT
tonyp@2165 558 void set_containing_set(HeapRegionSetBase* containing_set) { }
tonyp@2165 559
tonyp@2340 560 // containing_set() is only used in asserts so there's no reason
tonyp@2165 561 // to provide a dummy version of it.
tonyp@2165 562 #endif // ASSERT
tonyp@2165 563
tonyp@2165 564 // If we want to remove regions from a list in bulk we can simply tag
tonyp@2165 565 // them with the pending_removal tag and call the
tonyp@2165 566 // remove_all_pending() method on the list.
tonyp@2165 567
tonyp@2165 568 bool pending_removal() { return _pending_removal; }
tonyp@2165 569
tonyp@2165 570 void set_pending_removal(bool pending_removal) {
tonyp@2340 571 if (pending_removal) {
tonyp@2340 572 assert(!_pending_removal && containing_set() != NULL,
tonyp@2340 573 "can only set pending removal to true if it's false and "
tonyp@2340 574 "the region belongs to a region set");
tonyp@2340 575 } else {
tonyp@2340 576 assert( _pending_removal && containing_set() == NULL,
tonyp@2340 577 "can only set pending removal to false if it's true and "
tonyp@2340 578 "the region does not belong to a region set");
tonyp@2340 579 }
tonyp@2165 580
tonyp@2165 581 _pending_removal = pending_removal;
ysr@345 582 }
ysr@345 583
ysr@345 584 HeapRegion* get_next_young_region() { return _next_young_region; }
ysr@345 585 void set_next_young_region(HeapRegion* hr) {
ysr@345 586 _next_young_region = hr;
ysr@345 587 }
ysr@345 588
apetrusenko@844 589 HeapRegion* get_next_dirty_cards_region() const { return _next_dirty_cards_region; }
apetrusenko@844 590 HeapRegion** next_dirty_cards_region_addr() { return &_next_dirty_cards_region; }
apetrusenko@844 591 void set_next_dirty_cards_region(HeapRegion* hr) { _next_dirty_cards_region = hr; }
apetrusenko@844 592 bool is_on_dirty_cards_region_list() const { return get_next_dirty_cards_region() != NULL; }
apetrusenko@844 593
tonyp@2761 594 HeapWord* orig_end() { return _orig_end; }
tonyp@2761 595
ysr@345 596 // Allows logical separation between objects allocated before and after.
ysr@345 597 void save_marks();
ysr@345 598
ysr@345 599 // Reset HR stuff to default values.
ysr@345 600 void hr_clear(bool par, bool clear_space);
tonyp@2558 601 void par_clear();
ysr@345 602
tonyp@359 603 void initialize(MemRegion mr, bool clear_space, bool mangle_space);
ysr@345 604
ysr@345 605 // Get the start of the unmarked area in this region.
ysr@345 606 HeapWord* prev_top_at_mark_start() const { return _prev_top_at_mark_start; }
ysr@345 607 HeapWord* next_top_at_mark_start() const { return _next_top_at_mark_start; }
ysr@345 608
ysr@345 609 // Apply "cl->do_oop" to (the addresses of) all reference fields in objects
ysr@345 610 // allocated in the current region before the last call to "save_mark".
ysr@345 611 void oop_before_save_marks_iterate(OopClosure* cl);
ysr@345 612
ysr@345 613 // Note the start or end of marking. This tells the heap region
ysr@345 614 // that the collector is about to start or has finished (concurrently)
ysr@345 615 // marking the heap.
ysr@345 616
tonyp@3280 617 // Notify the region that concurrent marking is starting. Initialize
tonyp@3280 618 // all fields related to the next marking info.
tonyp@3280 619 inline void note_start_of_marking();
ysr@345 620
tonyp@3280 621 // Notify the region that concurrent marking has finished. Copy the
tonyp@3280 622 // (now finalized) next marking info fields into the prev marking
tonyp@3280 623 // info fields.
tonyp@3280 624 inline void note_end_of_marking();
ysr@345 625
tonyp@3280 626 // Notify the region that it will be used as to-space during a GC
tonyp@3280 627 // and we are about to start copying objects into it.
tonyp@3280 628 inline void note_start_of_copying(bool during_initial_mark);
ysr@345 629
tonyp@3280 630 // Notify the region that it ceases being to-space during a GC and
tonyp@3280 631 // we will not copy objects into it any more.
tonyp@3280 632 inline void note_end_of_copying(bool during_initial_mark);
tonyp@3280 633
tonyp@3280 634 // Notify the region that we are about to start processing
tonyp@3280 635 // self-forwarded objects during evac failure handling.
tonyp@3280 636 void note_self_forwarding_removal_start(bool during_initial_mark,
tonyp@3280 637 bool during_conc_mark);
tonyp@3280 638
tonyp@3280 639 // Notify the region that we have finished processing self-forwarded
tonyp@3280 640 // objects during evac failure handling.
tonyp@3280 641 void note_self_forwarding_removal_end(bool during_initial_mark,
tonyp@3280 642 bool during_conc_mark,
tonyp@3280 643 size_t marked_bytes);
ysr@345 644
ysr@345 645 // Returns "false" iff no object in the region was allocated when the
ysr@345 646 // last mark phase ended.
ysr@345 647 bool is_marked() { return _prev_top_at_mark_start != bottom(); }
ysr@345 648
ysr@345 649 void reset_during_compaction() {
tonyp@4192 650 assert(isHumongous() && startsHumongous(),
tonyp@4192 651 "should only be called for starts humongous regions");
ysr@345 652
ysr@345 653 zero_marked_bytes();
ysr@345 654 init_top_at_mark_start();
ysr@345 655 }
ysr@345 656
ysr@345 657 void calc_gc_efficiency(void);
ysr@345 658 double gc_efficiency() { return _gc_efficiency;}
ysr@345 659
ysr@345 660 bool is_young() const { return _young_type != NotYoung; }
ysr@345 661 bool is_survivor() const { return _young_type == Survivor; }
ysr@345 662
ysr@345 663 int young_index_in_cset() const { return _young_index_in_cset; }
ysr@345 664 void set_young_index_in_cset(int index) {
ysr@345 665 assert( (index == -1) || is_young(), "pre-condition" );
ysr@345 666 _young_index_in_cset = index;
ysr@345 667 }
ysr@345 668
ysr@345 669 int age_in_surv_rate_group() {
ysr@345 670 assert( _surv_rate_group != NULL, "pre-condition" );
ysr@345 671 assert( _age_index > -1, "pre-condition" );
ysr@345 672 return _surv_rate_group->age_in_group(_age_index);
ysr@345 673 }
ysr@345 674
ysr@345 675 void record_surv_words_in_group(size_t words_survived) {
ysr@345 676 assert( _surv_rate_group != NULL, "pre-condition" );
ysr@345 677 assert( _age_index > -1, "pre-condition" );
ysr@345 678 int age_in_group = age_in_surv_rate_group();
ysr@345 679 _surv_rate_group->record_surviving_words(age_in_group, words_survived);
ysr@345 680 }
ysr@345 681
ysr@345 682 int age_in_surv_rate_group_cond() {
ysr@345 683 if (_surv_rate_group != NULL)
ysr@345 684 return age_in_surv_rate_group();
ysr@345 685 else
ysr@345 686 return -1;
ysr@345 687 }
ysr@345 688
ysr@345 689 SurvRateGroup* surv_rate_group() {
ysr@345 690 return _surv_rate_group;
ysr@345 691 }
ysr@345 692
ysr@345 693 void install_surv_rate_group(SurvRateGroup* surv_rate_group) {
ysr@345 694 assert( surv_rate_group != NULL, "pre-condition" );
ysr@345 695 assert( _surv_rate_group == NULL, "pre-condition" );
ysr@345 696 assert( is_young(), "pre-condition" );
ysr@345 697
ysr@345 698 _surv_rate_group = surv_rate_group;
ysr@345 699 _age_index = surv_rate_group->next_age_index();
ysr@345 700 }
ysr@345 701
ysr@345 702 void uninstall_surv_rate_group() {
ysr@345 703 if (_surv_rate_group != NULL) {
ysr@345 704 assert( _age_index > -1, "pre-condition" );
ysr@345 705 assert( is_young(), "pre-condition" );
ysr@345 706
ysr@345 707 _surv_rate_group = NULL;
ysr@345 708 _age_index = -1;
ysr@345 709 } else {
ysr@345 710 assert( _age_index == -1, "pre-condition" );
ysr@345 711 }
ysr@345 712 }
ysr@345 713
ysr@345 714 void set_young() { set_young_type(Young); }
ysr@345 715
ysr@345 716 void set_survivor() { set_young_type(Survivor); }
ysr@345 717
ysr@345 718 void set_not_young() { set_young_type(NotYoung); }
ysr@345 719
ysr@345 720 // Determine if an object has been allocated since the last
ysr@345 721 // mark performed by the collector. This returns true iff the object
ysr@345 722 // is within the unmarked area of the region.
ysr@345 723 bool obj_allocated_since_prev_marking(oop obj) const {
ysr@345 724 return (HeapWord *) obj >= prev_top_at_mark_start();
ysr@345 725 }
ysr@345 726 bool obj_allocated_since_next_marking(oop obj) const {
ysr@345 727 return (HeapWord *) obj >= next_top_at_mark_start();
ysr@345 728 }
ysr@345 729
ysr@345 730 // For parallel heapRegion traversal.
ysr@345 731 bool claimHeapRegion(int claimValue);
ysr@345 732 jint claim_value() { return _claimed; }
ysr@345 733 // Use this carefully: only when you're sure no one is claiming...
ysr@345 734 void set_claim_value(int claimValue) { _claimed = claimValue; }
ysr@345 735
ysr@345 736 // Returns the "evacuation_failed" property of the region.
ysr@345 737 bool evacuation_failed() { return _evacuation_failed; }
ysr@345 738
ysr@345 739 // Sets the "evacuation_failed" property of the region.
ysr@345 740 void set_evacuation_failed(bool b) {
ysr@345 741 _evacuation_failed = b;
ysr@345 742
ysr@345 743 if (b) {
ysr@345 744 _next_marked_bytes = 0;
ysr@345 745 }
ysr@345 746 }
ysr@345 747
ysr@345 748 // Requires that "mr" be entirely within the region.
ysr@345 749 // Apply "cl->do_object" to all objects that intersect with "mr".
ysr@345 750 // If the iteration encounters an unparseable portion of the region,
ysr@345 751 // or if "cl->abort()" is true after a closure application,
ysr@345 752 // terminate the iteration and return the address of the start of the
ysr@345 753 // subregion that isn't done. (The two can be distinguished by querying
ysr@345 754 // "cl->abort()".) Return of "NULL" indicates that the iteration
ysr@345 755 // completed.
ysr@345 756 HeapWord*
ysr@345 757 object_iterate_mem_careful(MemRegion mr, ObjectClosure* cl);
ysr@345 758
tonyp@2558 759 // filter_young: if true and the region is a young region then we
tonyp@2558 760 // skip the iteration.
tonyp@2558 761 // card_ptr: if not NULL, and we decide that the card is not young
tonyp@2558 762 // and we iterate over it, we'll clean the card before we start the
tonyp@2558 763 // iteration.
ysr@345 764 HeapWord*
ysr@345 765 oops_on_card_seq_iterate_careful(MemRegion mr,
johnc@1685 766 FilterOutOfRegionClosure* cl,
tonyp@2558 767 bool filter_young,
tonyp@2558 768 jbyte* card_ptr);
ysr@345 769
ysr@345 770 // A version of block start that is guaranteed to find *some* block
ysr@345 771 // boundary at or before "p", but does not object iteration, and may
ysr@345 772 // therefore be used safely when the heap is unparseable.
ysr@345 773 HeapWord* block_start_careful(const void* p) const {
ysr@345 774 return _offsets.block_start_careful(p);
ysr@345 775 }
ysr@345 776
ysr@345 777 // Requires that "addr" is within the region. Returns the start of the
ysr@345 778 // first ("careful") block that starts at or after "addr", or else the
ysr@345 779 // "end" of the region if there is no such block.
ysr@345 780 HeapWord* next_block_start_careful(HeapWord* addr);
ysr@345 781
johnc@1483 782 size_t recorded_rs_length() const { return _recorded_rs_length; }
johnc@1483 783 double predicted_elapsed_time_ms() const { return _predicted_elapsed_time_ms; }
johnc@1483 784 size_t predicted_bytes_to_copy() const { return _predicted_bytes_to_copy; }
johnc@1483 785
johnc@1483 786 void set_recorded_rs_length(size_t rs_length) {
johnc@1483 787 _recorded_rs_length = rs_length;
johnc@1483 788 }
johnc@1483 789
johnc@1483 790 void set_predicted_elapsed_time_ms(double ms) {
johnc@1483 791 _predicted_elapsed_time_ms = ms;
johnc@1483 792 }
johnc@1483 793
johnc@1483 794 void set_predicted_bytes_to_copy(size_t bytes) {
johnc@1483 795 _predicted_bytes_to_copy = bytes;
johnc@1483 796 }
johnc@1483 797
ysr@345 798 #define HeapRegion_OOP_SINCE_SAVE_MARKS_DECL(OopClosureType, nv_suffix) \
ysr@345 799 virtual void oop_since_save_marks_iterate##nv_suffix(OopClosureType* cl);
ysr@345 800 SPECIALIZED_SINCE_SAVE_MARKS_CLOSURES(HeapRegion_OOP_SINCE_SAVE_MARKS_DECL)
ysr@345 801
tonyp@4192 802 virtual CompactibleSpace* next_compaction_space() const;
ysr@345 803
ysr@345 804 virtual void reset_after_compaction();
ysr@345 805
johnc@5169 806 // Routines for managing a list of code roots (attached to the
johnc@5169 807 // this region's RSet) that point into this heap region.
johnc@5169 808 void add_strong_code_root(nmethod* nm);
johnc@5169 809 void remove_strong_code_root(nmethod* nm);
johnc@5169 810
johnc@5169 811 // During a collection, migrate the successfully evacuated
johnc@5169 812 // strong code roots that referenced into this region to the
johnc@5169 813 // new regions that they now point into. Unsuccessfully
johnc@5169 814 // evacuated code roots are not migrated.
johnc@5169 815 void migrate_strong_code_roots();
johnc@5169 816
johnc@5169 817 // Applies blk->do_code_blob() to each of the entries in
johnc@5169 818 // the strong code roots list for this region
johnc@5169 819 void strong_code_roots_do(CodeBlobClosure* blk) const;
johnc@5169 820
johnc@5169 821 // Verify that the entries on the strong code root list for this
johnc@5169 822 // region are live and include at least one pointer into this region.
johnc@5169 823 void verify_strong_code_roots(VerifyOption vo, bool* failures) const;
johnc@5169 824
ysr@345 825 void print() const;
ysr@345 826 void print_on(outputStream* st) const;
ysr@345 827
johnc@2767 828 // vo == UsePrevMarking -> use "prev" marking information,
johnc@2767 829 // vo == UseNextMarking -> use "next" marking information
johnc@2767 830 // vo == UseMarkWord -> use the mark word in the object header
johnc@2767 831 //
tonyp@860 832 // NOTE: Only the "prev" marking information is guaranteed to be
tonyp@860 833 // consistent most of the time, so most calls to this should use
johnc@2767 834 // vo == UsePrevMarking.
johnc@2767 835 // Currently, there is only one case where this is called with
johnc@2767 836 // vo == UseNextMarking, which is to verify the "next" marking
johnc@2767 837 // information at the end of remark.
johnc@2767 838 // Currently there is only one place where this is called with
johnc@2767 839 // vo == UseMarkWord, which is to verify the marking during a
johnc@2767 840 // full GC.
brutisso@3951 841 void verify(VerifyOption vo, bool *failures) const;
tonyp@860 842
tonyp@860 843 // Override; it uses the "prev" marking information
brutisso@3951 844 virtual void verify() const;
ysr@345 845 };
ysr@345 846
ysr@345 847 // HeapRegionClosure is used for iterating over regions.
ysr@345 848 // Terminates the iteration when the "doHeapRegion" method returns "true".
ysr@345 849 class HeapRegionClosure : public StackObj {
ysr@345 850 friend class HeapRegionSeq;
ysr@345 851 friend class G1CollectedHeap;
ysr@345 852
ysr@345 853 bool _complete;
ysr@345 854 void incomplete() { _complete = false; }
ysr@345 855
ysr@345 856 public:
ysr@345 857 HeapRegionClosure(): _complete(true) {}
ysr@345 858
ysr@345 859 // Typically called on each region until it returns true.
ysr@345 860 virtual bool doHeapRegion(HeapRegion* r) = 0;
ysr@345 861
ysr@345 862 // True after iteration if the closure was applied to all heap regions
ysr@345 863 // and returned "false" in all cases.
ysr@345 864 bool complete() { return _complete; }
ysr@345 865 };
ysr@345 866
ysr@345 867 #endif // SERIALGC
stefank@1992 868
stefank@1992 869 #endif // SHARE_VM_GC_IMPLEMENTATION_G1_HEAPREGION_HPP