1 /* 2 * Copyright (c) 1997, 2009, 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 // A space is an abstraction for the "storage units" backing 26 // up the generation abstraction. It includes specific 27 // implementations for keeping track of free and used space, 28 // for iterating over objects and free blocks, etc. 29 30 // Here's the Space hierarchy: 31 // 32 // - Space -- an asbtract base class describing a heap area 33 // - CompactibleSpace -- a space supporting compaction 34 // - CompactibleFreeListSpace -- (used for CMS generation) 35 // - ContiguousSpace -- a compactible space in which all free space 36 // is contiguous 37 // - EdenSpace -- contiguous space used as nursery 38 // - ConcEdenSpace -- contiguous space with a 'soft end safe' allocation 39 // - OffsetTableContigSpace -- contiguous space with a block offset array 40 // that allows "fast" block_start calls 41 // - TenuredSpace -- (used for TenuredGeneration) 42 // - ContigPermSpace -- an offset table contiguous space for perm gen 43 44 // Forward decls. 45 class Space; 46 class BlockOffsetArray; 47 class BlockOffsetArrayContigSpace; 48 class Generation; 49 class CompactibleSpace; 50 class BlockOffsetTable; 51 class GenRemSet; 52 class CardTableRS; 53 class DirtyCardToOopClosure; 54 55 // An oop closure that is circumscribed by a filtering memory region. 56 class SpaceMemRegionOopsIterClosure: public OopClosure { 57 private: 58 OopClosure* _cl; 59 MemRegion _mr; 60 protected: 61 template <class T> void do_oop_work(T* p) { 62 if (_mr.contains(p)) { 63 _cl->do_oop(p); 64 } 65 } 66 public: 67 SpaceMemRegionOopsIterClosure(OopClosure* cl, MemRegion mr): 68 _cl(cl), _mr(mr) {} 69 virtual void do_oop(oop* p); 70 virtual void do_oop(narrowOop* p); 71 }; 72 73 // A Space describes a heap area. Class Space is an abstract 74 // base class. 75 // 76 // Space supports allocation, size computation and GC support is provided. 77 // 78 // Invariant: bottom() and end() are on page_size boundaries and 79 // bottom() <= top() <= end() 80 // top() is inclusive and end() is exclusive. 81 82 class Space: public CHeapObj { 83 friend class VMStructs; 84 protected: 85 HeapWord* _bottom; 86 HeapWord* _end; 87 88 // Used in support of save_marks() 89 HeapWord* _saved_mark_word; 90 91 MemRegionClosure* _preconsumptionDirtyCardClosure; 92 93 // A sequential tasks done structure. This supports 94 // parallel GC, where we have threads dynamically 95 // claiming sub-tasks from a larger parallel task. 96 SequentialSubTasksDone _par_seq_tasks; 97 98 Space(): 99 _bottom(NULL), _end(NULL), _preconsumptionDirtyCardClosure(NULL) { } 100 101 public: 102 // Accessors 103 HeapWord* bottom() const { return _bottom; } 104 HeapWord* end() const { return _end; } 105 virtual void set_bottom(HeapWord* value) { _bottom = value; } 106 virtual void set_end(HeapWord* value) { _end = value; } 107 108 virtual HeapWord* saved_mark_word() const { return _saved_mark_word; } 109 110 void set_saved_mark_word(HeapWord* p) { _saved_mark_word = p; } 111 112 MemRegionClosure* preconsumptionDirtyCardClosure() const { 113 return _preconsumptionDirtyCardClosure; 114 } 115 void setPreconsumptionDirtyCardClosure(MemRegionClosure* cl) { 116 _preconsumptionDirtyCardClosure = cl; 117 } 118 119 // Returns a subregion of the space containing all the objects in 120 // the space. 121 virtual MemRegion used_region() const { return MemRegion(bottom(), end()); } 122 123 // Returns a region that is guaranteed to contain (at least) all objects 124 // allocated at the time of the last call to "save_marks". If the space 125 // initializes its DirtyCardToOopClosure's specifying the "contig" option 126 // (that is, if the space is contiguous), then this region must contain only 127 // such objects: the memregion will be from the bottom of the region to the 128 // saved mark. Otherwise, the "obj_allocated_since_save_marks" method of 129 // the space must distiguish between objects in the region allocated before 130 // and after the call to save marks. 131 virtual MemRegion used_region_at_save_marks() const { 132 return MemRegion(bottom(), saved_mark_word()); 133 } 134 135 // Initialization. 136 // "initialize" should be called once on a space, before it is used for 137 // any purpose. The "mr" arguments gives the bounds of the space, and 138 // the "clear_space" argument should be true unless the memory in "mr" is 139 // known to be zeroed. 140 virtual void initialize(MemRegion mr, bool clear_space, bool mangle_space); 141 142 // The "clear" method must be called on a region that may have 143 // had allocation performed in it, but is now to be considered empty. 144 virtual void clear(bool mangle_space); 145 146 // For detecting GC bugs. Should only be called at GC boundaries, since 147 // some unused space may be used as scratch space during GC's. 148 // Default implementation does nothing. We also call this when expanding 149 // a space to satisfy an allocation request. See bug #4668531 150 virtual void mangle_unused_area() {} 151 virtual void mangle_unused_area_complete() {} 152 virtual void mangle_region(MemRegion mr) {} 153 154 // Testers 155 bool is_empty() const { return used() == 0; } 156 bool not_empty() const { return used() > 0; } 157 158 // Returns true iff the given the space contains the 159 // given address as part of an allocated object. For 160 // ceratin kinds of spaces, this might be a potentially 161 // expensive operation. To prevent performance problems 162 // on account of its inadvertent use in product jvm's, 163 // we restrict its use to assertion checks only. 164 virtual bool is_in(const void* p) const; 165 166 // Returns true iff the given reserved memory of the space contains the 167 // given address. 168 bool is_in_reserved(const void* p) const { return _bottom <= p && p < _end; } 169 170 // Returns true iff the given block is not allocated. 171 virtual bool is_free_block(const HeapWord* p) const = 0; 172 173 // Test whether p is double-aligned 174 static bool is_aligned(void* p) { 175 return ((intptr_t)p & (sizeof(double)-1)) == 0; 176 } 177 178 // Size computations. Sizes are in bytes. 179 size_t capacity() const { return byte_size(bottom(), end()); } 180 virtual size_t used() const = 0; 181 virtual size_t free() const = 0; 182 183 // Iterate over all the ref-containing fields of all objects in the 184 // space, calling "cl.do_oop" on each. Fields in objects allocated by 185 // applications of the closure are not included in the iteration. 186 virtual void oop_iterate(OopClosure* cl); 187 188 // Same as above, restricted to the intersection of a memory region and 189 // the space. Fields in objects allocated by applications of the closure 190 // are not included in the iteration. 191 virtual void oop_iterate(MemRegion mr, OopClosure* cl) = 0; 192 193 // Iterate over all objects in the space, calling "cl.do_object" on 194 // each. Objects allocated by applications of the closure are not 195 // included in the iteration. 196 virtual void object_iterate(ObjectClosure* blk) = 0; 197 // Similar to object_iterate() except only iterates over 198 // objects whose internal references point to objects in the space. 199 virtual void safe_object_iterate(ObjectClosure* blk) = 0; 200 201 // Iterate over all objects that intersect with mr, calling "cl->do_object" 202 // on each. There is an exception to this: if this closure has already 203 // been invoked on an object, it may skip such objects in some cases. This is 204 // Most likely to happen in an "upwards" (ascending address) iteration of 205 // MemRegions. 206 virtual void object_iterate_mem(MemRegion mr, UpwardsObjectClosure* cl); 207 208 // Iterate over as many initialized objects in the space as possible, 209 // calling "cl.do_object_careful" on each. Return NULL if all objects 210 // in the space (at the start of the iteration) were iterated over. 211 // Return an address indicating the extent of the iteration in the 212 // event that the iteration had to return because of finding an 213 // uninitialized object in the space, or if the closure "cl" 214 // signalled early termination. 215 virtual HeapWord* object_iterate_careful(ObjectClosureCareful* cl); 216 virtual HeapWord* object_iterate_careful_m(MemRegion mr, 217 ObjectClosureCareful* cl); 218 219 // Create and return a new dirty card to oop closure. Can be 220 // overriden to return the appropriate type of closure 221 // depending on the type of space in which the closure will 222 // operate. ResourceArea allocated. 223 virtual DirtyCardToOopClosure* new_dcto_cl(OopClosure* cl, 224 CardTableModRefBS::PrecisionStyle precision, 225 HeapWord* boundary = NULL); 226 227 // If "p" is in the space, returns the address of the start of the 228 // "block" that contains "p". We say "block" instead of "object" since 229 // some heaps may not pack objects densely; a chunk may either be an 230 // object or a non-object. If "p" is not in the space, return NULL. 231 virtual HeapWord* block_start_const(const void* p) const = 0; 232 233 // The non-const version may have benevolent side effects on the data 234 // structure supporting these calls, possibly speeding up future calls. 235 // The default implementation, however, is simply to call the const 236 // version. 237 inline virtual HeapWord* block_start(const void* p); 238 239 // Requires "addr" to be the start of a chunk, and returns its size. 240 // "addr + size" is required to be the start of a new chunk, or the end 241 // of the active area of the heap. 242 virtual size_t block_size(const HeapWord* addr) const = 0; 243 244 // Requires "addr" to be the start of a block, and returns "TRUE" iff 245 // the block is an object. 246 virtual bool block_is_obj(const HeapWord* addr) const = 0; 247 248 // Requires "addr" to be the start of a block, and returns "TRUE" iff 249 // the block is an object and the object is alive. 250 virtual bool obj_is_alive(const HeapWord* addr) const; 251 252 // Allocation (return NULL if full). Assumes the caller has established 253 // mutually exclusive access to the space. 254 virtual HeapWord* allocate(size_t word_size) = 0; 255 256 // Allocation (return NULL if full). Enforces mutual exclusion internally. 257 virtual HeapWord* par_allocate(size_t word_size) = 0; 258 259 // Returns true if this object has been allocated since a 260 // generation's "save_marks" call. 261 virtual bool obj_allocated_since_save_marks(const oop obj) const = 0; 262 263 // Mark-sweep-compact support: all spaces can update pointers to objects 264 // moving as a part of compaction. 265 virtual void adjust_pointers(); 266 267 // PrintHeapAtGC support 268 virtual void print() const; 269 virtual void print_on(outputStream* st) const; 270 virtual void print_short() const; 271 virtual void print_short_on(outputStream* st) const; 272 273 274 // Accessor for parallel sequential tasks. 275 SequentialSubTasksDone* par_seq_tasks() { return &_par_seq_tasks; } 276 277 // IF "this" is a ContiguousSpace, return it, else return NULL. 278 virtual ContiguousSpace* toContiguousSpace() { 279 return NULL; 280 } 281 282 // Debugging 283 virtual void verify(bool allow_dirty) const = 0; 284 }; 285 286 // A MemRegionClosure (ResourceObj) whose "do_MemRegion" function applies an 287 // OopClosure to (the addresses of) all the ref-containing fields that could 288 // be modified by virtue of the given MemRegion being dirty. (Note that 289 // because of the imprecise nature of the write barrier, this may iterate 290 // over oops beyond the region.) 291 // This base type for dirty card to oop closures handles memory regions 292 // in non-contiguous spaces with no boundaries, and should be sub-classed 293 // to support other space types. See ContiguousDCTOC for a sub-class 294 // that works with ContiguousSpaces. 295 296 class DirtyCardToOopClosure: public MemRegionClosureRO { 297 protected: 298 OopClosure* _cl; 299 Space* _sp; 300 CardTableModRefBS::PrecisionStyle _precision; 301 HeapWord* _boundary; // If non-NULL, process only non-NULL oops 302 // pointing below boundary. 303 HeapWord* _min_done; // ObjHeadPreciseArray precision requires 304 // a downwards traversal; this is the 305 // lowest location already done (or, 306 // alternatively, the lowest address that 307 // shouldn't be done again. NULL means infinity.) 308 NOT_PRODUCT(HeapWord* _last_bottom;) 309 NOT_PRODUCT(HeapWord* _last_explicit_min_done;) 310 311 // Get the actual top of the area on which the closure will 312 // operate, given where the top is assumed to be (the end of the 313 // memory region passed to do_MemRegion) and where the object 314 // at the top is assumed to start. For example, an object may 315 // start at the top but actually extend past the assumed top, 316 // in which case the top becomes the end of the object. 317 virtual HeapWord* get_actual_top(HeapWord* top, HeapWord* top_obj); 318 319 // Walk the given memory region from bottom to (actual) top 320 // looking for objects and applying the oop closure (_cl) to 321 // them. The base implementation of this treats the area as 322 // blocks, where a block may or may not be an object. Sub- 323 // classes should override this to provide more accurate 324 // or possibly more efficient walking. 325 virtual void walk_mem_region(MemRegion mr, HeapWord* bottom, HeapWord* top); 326 327 public: 328 DirtyCardToOopClosure(Space* sp, OopClosure* cl, 329 CardTableModRefBS::PrecisionStyle precision, 330 HeapWord* boundary) : 331 _sp(sp), _cl(cl), _precision(precision), _boundary(boundary), 332 _min_done(NULL) { 333 NOT_PRODUCT(_last_bottom = NULL); 334 NOT_PRODUCT(_last_explicit_min_done = NULL); 335 } 336 337 void do_MemRegion(MemRegion mr); 338 339 void set_min_done(HeapWord* min_done) { 340 _min_done = min_done; 341 NOT_PRODUCT(_last_explicit_min_done = _min_done); 342 } 343 #ifndef PRODUCT 344 void set_last_bottom(HeapWord* last_bottom) { 345 _last_bottom = last_bottom; 346 } 347 #endif 348 }; 349 350 // A structure to represent a point at which objects are being copied 351 // during compaction. 352 class CompactPoint : public StackObj { 353 public: 354 Generation* gen; 355 CompactibleSpace* space; 356 HeapWord* threshold; 357 CompactPoint(Generation* _gen, CompactibleSpace* _space, 358 HeapWord* _threshold) : 359 gen(_gen), space(_space), threshold(_threshold) {} 360 }; 361 362 363 // A space that supports compaction operations. This is usually, but not 364 // necessarily, a space that is normally contiguous. But, for example, a 365 // free-list-based space whose normal collection is a mark-sweep without 366 // compaction could still support compaction in full GC's. 367 368 class CompactibleSpace: public Space { 369 friend class VMStructs; 370 friend class CompactibleFreeListSpace; 371 friend class CompactingPermGenGen; 372 friend class CMSPermGenGen; 373 private: 374 HeapWord* _compaction_top; 375 CompactibleSpace* _next_compaction_space; 376 377 public: 378 CompactibleSpace() : 379 _compaction_top(NULL), _next_compaction_space(NULL) {} 380 381 virtual void initialize(MemRegion mr, bool clear_space, bool mangle_space); 382 virtual void clear(bool mangle_space); 383 384 // Used temporarily during a compaction phase to hold the value 385 // top should have when compaction is complete. 386 HeapWord* compaction_top() const { return _compaction_top; } 387 388 void set_compaction_top(HeapWord* value) { 389 assert(value == NULL || (value >= bottom() && value <= end()), 390 "should point inside space"); 391 _compaction_top = value; 392 } 393 394 // Perform operations on the space needed after a compaction 395 // has been performed. 396 virtual void reset_after_compaction() {} 397 398 // Returns the next space (in the current generation) to be compacted in 399 // the global compaction order. Also is used to select the next 400 // space into which to compact. 401 402 virtual CompactibleSpace* next_compaction_space() const { 403 return _next_compaction_space; 404 } 405 406 void set_next_compaction_space(CompactibleSpace* csp) { 407 _next_compaction_space = csp; 408 } 409 410 // MarkSweep support phase2 411 412 // Start the process of compaction of the current space: compute 413 // post-compaction addresses, and insert forwarding pointers. The fields 414 // "cp->gen" and "cp->compaction_space" are the generation and space into 415 // which we are currently compacting. This call updates "cp" as necessary, 416 // and leaves the "compaction_top" of the final value of 417 // "cp->compaction_space" up-to-date. Offset tables may be updated in 418 // this phase as if the final copy had occurred; if so, "cp->threshold" 419 // indicates when the next such action should be taken. 420 virtual void prepare_for_compaction(CompactPoint* cp); 421 // MarkSweep support phase3 422 virtual void adjust_pointers(); 423 // MarkSweep support phase4 424 virtual void compact(); 425 426 // The maximum percentage of objects that can be dead in the compacted 427 // live part of a compacted space ("deadwood" support.) 428 virtual size_t allowed_dead_ratio() const { return 0; }; 429 430 // Some contiguous spaces may maintain some data structures that should 431 // be updated whenever an allocation crosses a boundary. This function 432 // returns the first such boundary. 433 // (The default implementation returns the end of the space, so the 434 // boundary is never crossed.) 435 virtual HeapWord* initialize_threshold() { return end(); } 436 437 // "q" is an object of the given "size" that should be forwarded; 438 // "cp" names the generation ("gen") and containing "this" (which must 439 // also equal "cp->space"). "compact_top" is where in "this" the 440 // next object should be forwarded to. If there is room in "this" for 441 // the object, insert an appropriate forwarding pointer in "q". 442 // If not, go to the next compaction space (there must 443 // be one, since compaction must succeed -- we go to the first space of 444 // the previous generation if necessary, updating "cp"), reset compact_top 445 // and then forward. In either case, returns the new value of "compact_top". 446 // If the forwarding crosses "cp->threshold", invokes the "cross_threhold" 447 // function of the then-current compaction space, and updates "cp->threshold 448 // accordingly". 449 virtual HeapWord* forward(oop q, size_t size, CompactPoint* cp, 450 HeapWord* compact_top); 451 452 // Return a size with adjusments as required of the space. 453 virtual size_t adjust_object_size_v(size_t size) const { return size; } 454 455 protected: 456 // Used during compaction. 457 HeapWord* _first_dead; 458 HeapWord* _end_of_live; 459 460 // Minimum size of a free block. 461 virtual size_t minimum_free_block_size() const = 0; 462 463 // This the function is invoked when an allocation of an object covering 464 // "start" to "end occurs crosses the threshold; returns the next 465 // threshold. (The default implementation does nothing.) 466 virtual HeapWord* cross_threshold(HeapWord* start, HeapWord* the_end) { 467 return end(); 468 } 469 470 // Requires "allowed_deadspace_words > 0", that "q" is the start of a 471 // free block of the given "word_len", and that "q", were it an object, 472 // would not move if forwared. If the size allows, fill the free 473 // block with an object, to prevent excessive compaction. Returns "true" 474 // iff the free region was made deadspace, and modifies 475 // "allowed_deadspace_words" to reflect the number of available deadspace 476 // words remaining after this operation. 477 bool insert_deadspace(size_t& allowed_deadspace_words, HeapWord* q, 478 size_t word_len); 479 }; 480 481 #define SCAN_AND_FORWARD(cp,scan_limit,block_is_obj,block_size) { \ 482 /* Compute the new addresses for the live objects and store it in the mark \ 483 * Used by universe::mark_sweep_phase2() \ 484 */ \ 485 HeapWord* compact_top; /* This is where we are currently compacting to. */ \ 486 \ 487 /* We're sure to be here before any objects are compacted into this \ 488 * space, so this is a good time to initialize this: \ 489 */ \ 490 set_compaction_top(bottom()); \ 491 \ 492 if (cp->space == NULL) { \ 493 assert(cp->gen != NULL, "need a generation"); \ 494 assert(cp->threshold == NULL, "just checking"); \ 495 assert(cp->gen->first_compaction_space() == this, "just checking"); \ 496 cp->space = cp->gen->first_compaction_space(); \ 497 compact_top = cp->space->bottom(); \ 498 cp->space->set_compaction_top(compact_top); \ 499 cp->threshold = cp->space->initialize_threshold(); \ 500 } else { \ 501 compact_top = cp->space->compaction_top(); \ 502 } \ 503 \ 504 /* We allow some amount of garbage towards the bottom of the space, so \ 505 * we don't start compacting before there is a significant gain to be made.\ 506 * Occasionally, we want to ensure a full compaction, which is determined \ 507 * by the MarkSweepAlwaysCompactCount parameter. \ 508 */ \ 509 int invocations = SharedHeap::heap()->perm_gen()->stat_record()->invocations;\ 510 bool skip_dead = ((invocations % MarkSweepAlwaysCompactCount) != 0); \ 511 \ 512 size_t allowed_deadspace = 0; \ 513 if (skip_dead) { \ 514 const size_t ratio = allowed_dead_ratio(); \ 515 allowed_deadspace = (capacity() * ratio / 100) / HeapWordSize; \ 516 } \ 517 \ 518 HeapWord* q = bottom(); \ 519 HeapWord* t = scan_limit(); \ 520 \ 521 HeapWord* end_of_live= q; /* One byte beyond the last byte of the last \ 522 live object. */ \ 523 HeapWord* first_dead = end();/* The first dead object. */ \ 524 LiveRange* liveRange = NULL; /* The current live range, recorded in the \ 525 first header of preceding free area. */ \ 526 _first_dead = first_dead; \ 527 \ 528 const intx interval = PrefetchScanIntervalInBytes; \ 529 \ 530 while (q < t) { \ 531 assert(!block_is_obj(q) || \ 532 oop(q)->mark()->is_marked() || oop(q)->mark()->is_unlocked() || \ 533 oop(q)->mark()->has_bias_pattern(), \ 534 "these are the only valid states during a mark sweep"); \ 535 if (block_is_obj(q) && oop(q)->is_gc_marked()) { \ 536 /* prefetch beyond q */ \ 537 Prefetch::write(q, interval); \ 538 /* size_t size = oop(q)->size(); changing this for cms for perm gen */\ 539 size_t size = block_size(q); \ 540 compact_top = cp->space->forward(oop(q), size, cp, compact_top); \ 541 q += size; \ 542 end_of_live = q; \ 543 } else { \ 544 /* run over all the contiguous dead objects */ \ 545 HeapWord* end = q; \ 546 do { \ 547 /* prefetch beyond end */ \ 548 Prefetch::write(end, interval); \ 549 end += block_size(end); \ 550 } while (end < t && (!block_is_obj(end) || !oop(end)->is_gc_marked()));\ 551 \ 552 /* see if we might want to pretend this object is alive so that \ 553 * we don't have to compact quite as often. \ 554 */ \ 555 if (allowed_deadspace > 0 && q == compact_top) { \ 556 size_t sz = pointer_delta(end, q); \ 557 if (insert_deadspace(allowed_deadspace, q, sz)) { \ 558 compact_top = cp->space->forward(oop(q), sz, cp, compact_top); \ 559 q = end; \ 560 end_of_live = end; \ 561 continue; \ 562 } \ 563 } \ 564 \ 565 /* otherwise, it really is a free region. */ \ 566 \ 567 /* for the previous LiveRange, record the end of the live objects. */ \ 568 if (liveRange) { \ 569 liveRange->set_end(q); \ 570 } \ 571 \ 572 /* record the current LiveRange object. \ 573 * liveRange->start() is overlaid on the mark word. \ 574 */ \ 575 liveRange = (LiveRange*)q; \ 576 liveRange->set_start(end); \ 577 liveRange->set_end(end); \ 578 \ 579 /* see if this is the first dead region. */ \ 580 if (q < first_dead) { \ 581 first_dead = q; \ 582 } \ 583 \ 584 /* move on to the next object */ \ 585 q = end; \ 586 } \ 587 } \ 588 \ 589 assert(q == t, "just checking"); \ 590 if (liveRange != NULL) { \ 591 liveRange->set_end(q); \ 592 } \ 593 _end_of_live = end_of_live; \ 594 if (end_of_live < first_dead) { \ 595 first_dead = end_of_live; \ 596 } \ 597 _first_dead = first_dead; \ 598 \ 599 /* save the compaction_top of the compaction space. */ \ 600 cp->space->set_compaction_top(compact_top); \ 601 } 602 603 #define SCAN_AND_ADJUST_POINTERS(adjust_obj_size) { \ 604 /* adjust all the interior pointers to point at the new locations of objects \ 605 * Used by MarkSweep::mark_sweep_phase3() */ \ 606 \ 607 HeapWord* q = bottom(); \ 608 HeapWord* t = _end_of_live; /* Established by "prepare_for_compaction". */ \ 609 \ 610 assert(_first_dead <= _end_of_live, "Stands to reason, no?"); \ 611 \ 612 if (q < t && _first_dead > q && \ 613 !oop(q)->is_gc_marked()) { \ 614 /* we have a chunk of the space which hasn't moved and we've \ 615 * reinitialized the mark word during the previous pass, so we can't \ 616 * use is_gc_marked for the traversal. */ \ 617 HeapWord* end = _first_dead; \ 618 \ 619 while (q < end) { \ 620 /* I originally tried to conjoin "block_start(q) == q" to the \ 621 * assertion below, but that doesn't work, because you can't \ 622 * accurately traverse previous objects to get to the current one \ 623 * after their pointers (including pointers into permGen) have been \ 624 * updated, until the actual compaction is done. dld, 4/00 */ \ 625 assert(block_is_obj(q), \ 626 "should be at block boundaries, and should be looking at objs"); \ 627 \ 628 VALIDATE_MARK_SWEEP_ONLY(MarkSweep::track_interior_pointers(oop(q))); \ 629 \ 630 /* point all the oops to the new location */ \ 631 size_t size = oop(q)->adjust_pointers(); \ 632 size = adjust_obj_size(size); \ 633 \ 634 VALIDATE_MARK_SWEEP_ONLY(MarkSweep::check_interior_pointers()); \ 635 \ 636 VALIDATE_MARK_SWEEP_ONLY(MarkSweep::validate_live_oop(oop(q), size)); \ 637 \ 638 q += size; \ 639 } \ 640 \ 641 if (_first_dead == t) { \ 642 q = t; \ 643 } else { \ 644 /* $$$ This is funky. Using this to read the previously written \ 645 * LiveRange. See also use below. */ \ 646 q = (HeapWord*)oop(_first_dead)->mark()->decode_pointer(); \ 647 } \ 648 } \ 649 \ 650 const intx interval = PrefetchScanIntervalInBytes; \ 651 \ 652 debug_only(HeapWord* prev_q = NULL); \ 653 while (q < t) { \ 654 /* prefetch beyond q */ \ 655 Prefetch::write(q, interval); \ 656 if (oop(q)->is_gc_marked()) { \ 657 /* q is alive */ \ 658 VALIDATE_MARK_SWEEP_ONLY(MarkSweep::track_interior_pointers(oop(q))); \ 659 /* point all the oops to the new location */ \ 660 size_t size = oop(q)->adjust_pointers(); \ 661 size = adjust_obj_size(size); \ 662 VALIDATE_MARK_SWEEP_ONLY(MarkSweep::check_interior_pointers()); \ 663 VALIDATE_MARK_SWEEP_ONLY(MarkSweep::validate_live_oop(oop(q), size)); \ 664 debug_only(prev_q = q); \ 665 q += size; \ 666 } else { \ 667 /* q is not a live object, so its mark should point at the next \ 668 * live object */ \ 669 debug_only(prev_q = q); \ 670 q = (HeapWord*) oop(q)->mark()->decode_pointer(); \ 671 assert(q > prev_q, "we should be moving forward through memory"); \ 672 } \ 673 } \ 674 \ 675 assert(q == t, "just checking"); \ 676 } 677 678 #define SCAN_AND_COMPACT(obj_size) { \ 679 /* Copy all live objects to their new location \ 680 * Used by MarkSweep::mark_sweep_phase4() */ \ 681 \ 682 HeapWord* q = bottom(); \ 683 HeapWord* const t = _end_of_live; \ 684 debug_only(HeapWord* prev_q = NULL); \ 685 \ 686 if (q < t && _first_dead > q && \ 687 !oop(q)->is_gc_marked()) { \ 688 debug_only( \ 689 /* we have a chunk of the space which hasn't moved and we've reinitialized \ 690 * the mark word during the previous pass, so we can't use is_gc_marked for \ 691 * the traversal. */ \ 692 HeapWord* const end = _first_dead; \ 693 \ 694 while (q < end) { \ 695 size_t size = obj_size(q); \ 696 assert(!oop(q)->is_gc_marked(), \ 697 "should be unmarked (special dense prefix handling)"); \ 698 VALIDATE_MARK_SWEEP_ONLY(MarkSweep::live_oop_moved_to(q, size, q)); \ 699 debug_only(prev_q = q); \ 700 q += size; \ 701 } \ 702 ) /* debug_only */ \ 703 \ 704 if (_first_dead == t) { \ 705 q = t; \ 706 } else { \ 707 /* $$$ Funky */ \ 708 q = (HeapWord*) oop(_first_dead)->mark()->decode_pointer(); \ 709 } \ 710 } \ 711 \ 712 const intx scan_interval = PrefetchScanIntervalInBytes; \ 713 const intx copy_interval = PrefetchCopyIntervalInBytes; \ 714 while (q < t) { \ 715 if (!oop(q)->is_gc_marked()) { \ 716 /* mark is pointer to next marked oop */ \ 717 debug_only(prev_q = q); \ 718 q = (HeapWord*) oop(q)->mark()->decode_pointer(); \ 719 assert(q > prev_q, "we should be moving forward through memory"); \ 720 } else { \ 721 /* prefetch beyond q */ \ 722 Prefetch::read(q, scan_interval); \ 723 \ 724 /* size and destination */ \ 725 size_t size = obj_size(q); \ 726 HeapWord* compaction_top = (HeapWord*)oop(q)->forwardee(); \ 727 \ 728 /* prefetch beyond compaction_top */ \ 729 Prefetch::write(compaction_top, copy_interval); \ 730 \ 731 /* copy object and reinit its mark */ \ 732 VALIDATE_MARK_SWEEP_ONLY(MarkSweep::live_oop_moved_to(q, size, \ 733 compaction_top)); \ 734 assert(q != compaction_top, "everything in this pass should be moving"); \ 735 Copy::aligned_conjoint_words(q, compaction_top, size); \ 736 oop(compaction_top)->init_mark(); \ 737 assert(oop(compaction_top)->klass() != NULL, "should have a class"); \ 738 \ 739 debug_only(prev_q = q); \ 740 q += size; \ 741 } \ 742 } \ 743 \ 744 /* Let's remember if we were empty before we did the compaction. */ \ 745 bool was_empty = used_region().is_empty(); \ 746 /* Reset space after compaction is complete */ \ 747 reset_after_compaction(); \ 748 /* We do this clear, below, since it has overloaded meanings for some */ \ 749 /* space subtypes. For example, OffsetTableContigSpace's that were */ \ 750 /* compacted into will have had their offset table thresholds updated */ \ 751 /* continuously, but those that weren't need to have their thresholds */ \ 752 /* re-initialized. Also mangles unused area for debugging. */ \ 753 if (used_region().is_empty()) { \ 754 if (!was_empty) clear(SpaceDecorator::Mangle); \ 755 } else { \ 756 if (ZapUnusedHeapArea) mangle_unused_area(); \ 757 } \ 758 } 759 760 class GenSpaceMangler; 761 762 // A space in which the free area is contiguous. It therefore supports 763 // faster allocation, and compaction. 764 class ContiguousSpace: public CompactibleSpace { 765 friend class OneContigSpaceCardGeneration; 766 friend class VMStructs; 767 protected: 768 HeapWord* _top; 769 HeapWord* _concurrent_iteration_safe_limit; 770 // A helper for mangling the unused area of the space in debug builds. 771 GenSpaceMangler* _mangler; 772 773 GenSpaceMangler* mangler() { return _mangler; } 774 775 // Allocation helpers (return NULL if full). 776 inline HeapWord* allocate_impl(size_t word_size, HeapWord* end_value); 777 inline HeapWord* par_allocate_impl(size_t word_size, HeapWord* end_value); 778 779 public: 780 ContiguousSpace(); 781 ~ContiguousSpace(); 782 783 virtual void initialize(MemRegion mr, bool clear_space, bool mangle_space); 784 virtual void clear(bool mangle_space); 785 786 // Accessors 787 HeapWord* top() const { return _top; } 788 void set_top(HeapWord* value) { _top = value; } 789 790 virtual void set_saved_mark() { _saved_mark_word = top(); } 791 void reset_saved_mark() { _saved_mark_word = bottom(); } 792 793 WaterMark bottom_mark() { return WaterMark(this, bottom()); } 794 WaterMark top_mark() { return WaterMark(this, top()); } 795 WaterMark saved_mark() { return WaterMark(this, saved_mark_word()); } 796 bool saved_mark_at_top() const { return saved_mark_word() == top(); } 797 798 // In debug mode mangle (write it with a particular bit 799 // pattern) the unused part of a space. 800 801 // Used to save the an address in a space for later use during mangling. 802 void set_top_for_allocations(HeapWord* v) PRODUCT_RETURN; 803 // Used to save the space's current top for later use during mangling. 804 void set_top_for_allocations() PRODUCT_RETURN; 805 806 // Mangle regions in the space from the current top up to the 807 // previously mangled part of the space. 808 void mangle_unused_area() PRODUCT_RETURN; 809 // Mangle [top, end) 810 void mangle_unused_area_complete() PRODUCT_RETURN; 811 // Mangle the given MemRegion. 812 void mangle_region(MemRegion mr) PRODUCT_RETURN; 813 814 // Do some sparse checking on the area that should have been mangled. 815 void check_mangled_unused_area(HeapWord* limit) PRODUCT_RETURN; 816 // Check the complete area that should have been mangled. 817 // This code may be NULL depending on the macro DEBUG_MANGLING. 818 void check_mangled_unused_area_complete() PRODUCT_RETURN; 819 820 // Size computations: sizes in bytes. 821 size_t capacity() const { return byte_size(bottom(), end()); } 822 size_t used() const { return byte_size(bottom(), top()); } 823 size_t free() const { return byte_size(top(), end()); } 824 825 // Override from space. 826 bool is_in(const void* p) const; 827 828 virtual bool is_free_block(const HeapWord* p) const; 829 830 // In a contiguous space we have a more obvious bound on what parts 831 // contain objects. 832 MemRegion used_region() const { return MemRegion(bottom(), top()); } 833 834 MemRegion used_region_at_save_marks() const { 835 return MemRegion(bottom(), saved_mark_word()); 836 } 837 838 // Allocation (return NULL if full) 839 virtual HeapWord* allocate(size_t word_size); 840 virtual HeapWord* par_allocate(size_t word_size); 841 842 virtual bool obj_allocated_since_save_marks(const oop obj) const { 843 return (HeapWord*)obj >= saved_mark_word(); 844 } 845 846 // Iteration 847 void oop_iterate(OopClosure* cl); 848 void oop_iterate(MemRegion mr, OopClosure* cl); 849 void object_iterate(ObjectClosure* blk); 850 // For contiguous spaces this method will iterate safely over objects 851 // in the space (i.e., between bottom and top) when at a safepoint. 852 void safe_object_iterate(ObjectClosure* blk); 853 void object_iterate_mem(MemRegion mr, UpwardsObjectClosure* cl); 854 // iterates on objects up to the safe limit 855 HeapWord* object_iterate_careful(ObjectClosureCareful* cl); 856 inline HeapWord* concurrent_iteration_safe_limit(); 857 // changes the safe limit, all objects from bottom() to the new 858 // limit should be properly initialized 859 inline void set_concurrent_iteration_safe_limit(HeapWord* new_limit); 860 861 #ifndef SERIALGC 862 // In support of parallel oop_iterate. 863 #define ContigSpace_PAR_OOP_ITERATE_DECL(OopClosureType, nv_suffix) \ 864 void par_oop_iterate(MemRegion mr, OopClosureType* blk); 865 866 ALL_PAR_OOP_ITERATE_CLOSURES(ContigSpace_PAR_OOP_ITERATE_DECL) 867 #undef ContigSpace_PAR_OOP_ITERATE_DECL 868 #endif // SERIALGC 869 870 // Compaction support 871 virtual void reset_after_compaction() { 872 assert(compaction_top() >= bottom() && compaction_top() <= end(), "should point inside space"); 873 set_top(compaction_top()); 874 // set new iteration safe limit 875 set_concurrent_iteration_safe_limit(compaction_top()); 876 } 877 virtual size_t minimum_free_block_size() const { return 0; } 878 879 // Override. 880 DirtyCardToOopClosure* new_dcto_cl(OopClosure* cl, 881 CardTableModRefBS::PrecisionStyle precision, 882 HeapWord* boundary = NULL); 883 884 // Apply "blk->do_oop" to the addresses of all reference fields in objects 885 // starting with the _saved_mark_word, which was noted during a generation's 886 // save_marks and is required to denote the head of an object. 887 // Fields in objects allocated by applications of the closure 888 // *are* included in the iteration. 889 // Updates _saved_mark_word to point to just after the last object 890 // iterated over. 891 #define ContigSpace_OOP_SINCE_SAVE_MARKS_DECL(OopClosureType, nv_suffix) \ 892 void oop_since_save_marks_iterate##nv_suffix(OopClosureType* blk); 893 894 ALL_SINCE_SAVE_MARKS_CLOSURES(ContigSpace_OOP_SINCE_SAVE_MARKS_DECL) 895 #undef ContigSpace_OOP_SINCE_SAVE_MARKS_DECL 896 897 // Same as object_iterate, but starting from "mark", which is required 898 // to denote the start of an object. Objects allocated by 899 // applications of the closure *are* included in the iteration. 900 virtual void object_iterate_from(WaterMark mark, ObjectClosure* blk); 901 902 // Very inefficient implementation. 903 virtual HeapWord* block_start_const(const void* p) const; 904 size_t block_size(const HeapWord* p) const; 905 // If a block is in the allocated area, it is an object. 906 bool block_is_obj(const HeapWord* p) const { return p < top(); } 907 908 // Addresses for inlined allocation 909 HeapWord** top_addr() { return &_top; } 910 HeapWord** end_addr() { return &_end; } 911 912 // Overrides for more efficient compaction support. 913 void prepare_for_compaction(CompactPoint* cp); 914 915 // PrintHeapAtGC support. 916 virtual void print_on(outputStream* st) const; 917 918 // Checked dynamic downcasts. 919 virtual ContiguousSpace* toContiguousSpace() { 920 return this; 921 } 922 923 // Debugging 924 virtual void verify(bool allow_dirty) const; 925 926 // Used to increase collection frequency. "factor" of 0 means entire 927 // space. 928 void allocate_temporary_filler(int factor); 929 930 }; 931 932 933 // A dirty card to oop closure that does filtering. 934 // It knows how to filter out objects that are outside of the _boundary. 935 class Filtering_DCTOC : public DirtyCardToOopClosure { 936 protected: 937 // Override. 938 void walk_mem_region(MemRegion mr, 939 HeapWord* bottom, HeapWord* top); 940 941 // Walk the given memory region, from bottom to top, applying 942 // the given oop closure to (possibly) all objects found. The 943 // given oop closure may or may not be the same as the oop 944 // closure with which this closure was created, as it may 945 // be a filtering closure which makes use of the _boundary. 946 // We offer two signatures, so the FilteringClosure static type is 947 // apparent. 948 virtual void walk_mem_region_with_cl(MemRegion mr, 949 HeapWord* bottom, HeapWord* top, 950 OopClosure* cl) = 0; 951 virtual void walk_mem_region_with_cl(MemRegion mr, 952 HeapWord* bottom, HeapWord* top, 953 FilteringClosure* cl) = 0; 954 955 public: 956 Filtering_DCTOC(Space* sp, OopClosure* cl, 957 CardTableModRefBS::PrecisionStyle precision, 958 HeapWord* boundary) : 959 DirtyCardToOopClosure(sp, cl, precision, boundary) {} 960 }; 961 962 // A dirty card to oop closure for contiguous spaces 963 // (ContiguousSpace and sub-classes). 964 // It is a FilteringClosure, as defined above, and it knows: 965 // 966 // 1. That the actual top of any area in a memory region 967 // contained by the space is bounded by the end of the contiguous 968 // region of the space. 969 // 2. That the space is really made up of objects and not just 970 // blocks. 971 972 class ContiguousSpaceDCTOC : public Filtering_DCTOC { 973 protected: 974 // Overrides. 975 HeapWord* get_actual_top(HeapWord* top, HeapWord* top_obj); 976 977 virtual void walk_mem_region_with_cl(MemRegion mr, 978 HeapWord* bottom, HeapWord* top, 979 OopClosure* cl); 980 virtual void walk_mem_region_with_cl(MemRegion mr, 981 HeapWord* bottom, HeapWord* top, 982 FilteringClosure* cl); 983 984 public: 985 ContiguousSpaceDCTOC(ContiguousSpace* sp, OopClosure* cl, 986 CardTableModRefBS::PrecisionStyle precision, 987 HeapWord* boundary) : 988 Filtering_DCTOC(sp, cl, precision, boundary) 989 {} 990 }; 991 992 993 // Class EdenSpace describes eden-space in new generation. 994 995 class DefNewGeneration; 996 997 class EdenSpace : public ContiguousSpace { 998 friend class VMStructs; 999 private: 1000 DefNewGeneration* _gen; 1001 1002 // _soft_end is used as a soft limit on allocation. As soft limits are 1003 // reached, the slow-path allocation code can invoke other actions and then 1004 // adjust _soft_end up to a new soft limit or to end(). 1005 HeapWord* _soft_end; 1006 1007 public: 1008 EdenSpace(DefNewGeneration* gen) : 1009 _gen(gen), _soft_end(NULL) {} 1010 1011 // Get/set just the 'soft' limit. 1012 HeapWord* soft_end() { return _soft_end; } 1013 HeapWord** soft_end_addr() { return &_soft_end; } 1014 void set_soft_end(HeapWord* value) { _soft_end = value; } 1015 1016 // Override. 1017 void clear(bool mangle_space); 1018 1019 // Set both the 'hard' and 'soft' limits (_end and _soft_end). 1020 void set_end(HeapWord* value) { 1021 set_soft_end(value); 1022 ContiguousSpace::set_end(value); 1023 } 1024 1025 // Allocation (return NULL if full) 1026 HeapWord* allocate(size_t word_size); 1027 HeapWord* par_allocate(size_t word_size); 1028 }; 1029 1030 // Class ConcEdenSpace extends EdenSpace for the sake of safe 1031 // allocation while soft-end is being modified concurrently 1032 1033 class ConcEdenSpace : public EdenSpace { 1034 public: 1035 ConcEdenSpace(DefNewGeneration* gen) : EdenSpace(gen) { } 1036 1037 // Allocation (return NULL if full) 1038 HeapWord* par_allocate(size_t word_size); 1039 }; 1040 1041 1042 // A ContigSpace that Supports an efficient "block_start" operation via 1043 // a BlockOffsetArray (whose BlockOffsetSharedArray may be shared with 1044 // other spaces.) This is the abstract base class for old generation 1045 // (tenured, perm) spaces. 1046 1047 class OffsetTableContigSpace: public ContiguousSpace { 1048 friend class VMStructs; 1049 protected: 1050 BlockOffsetArrayContigSpace _offsets; 1051 Mutex _par_alloc_lock; 1052 1053 public: 1054 // Constructor 1055 OffsetTableContigSpace(BlockOffsetSharedArray* sharedOffsetArray, 1056 MemRegion mr); 1057 1058 void set_bottom(HeapWord* value); 1059 void set_end(HeapWord* value); 1060 1061 void clear(bool mangle_space); 1062 1063 inline HeapWord* block_start_const(const void* p) const; 1064 1065 // Add offset table update. 1066 virtual inline HeapWord* allocate(size_t word_size); 1067 inline HeapWord* par_allocate(size_t word_size); 1068 1069 // MarkSweep support phase3 1070 virtual HeapWord* initialize_threshold(); 1071 virtual HeapWord* cross_threshold(HeapWord* start, HeapWord* end); 1072 1073 virtual void print_on(outputStream* st) const; 1074 1075 // Debugging 1076 void verify(bool allow_dirty) const; 1077 1078 // Shared space support 1079 void serialize_block_offset_array_offsets(SerializeOopClosure* soc); 1080 }; 1081 1082 1083 // Class TenuredSpace is used by TenuredGeneration 1084 1085 class TenuredSpace: public OffsetTableContigSpace { 1086 friend class VMStructs; 1087 protected: 1088 // Mark sweep support 1089 size_t allowed_dead_ratio() const; 1090 public: 1091 // Constructor 1092 TenuredSpace(BlockOffsetSharedArray* sharedOffsetArray, 1093 MemRegion mr) : 1094 OffsetTableContigSpace(sharedOffsetArray, mr) {} 1095 }; 1096 1097 1098 // Class ContigPermSpace is used by CompactingPermGen 1099 1100 class ContigPermSpace: public OffsetTableContigSpace { 1101 friend class VMStructs; 1102 protected: 1103 // Mark sweep support 1104 size_t allowed_dead_ratio() const; 1105 public: 1106 // Constructor 1107 ContigPermSpace(BlockOffsetSharedArray* sharedOffsetArray, MemRegion mr) : 1108 OffsetTableContigSpace(sharedOffsetArray, mr) {} 1109 };