src/share/vm/memory/space.cpp

Thu, 12 Oct 2017 21:27:07 +0800

author
aoqi
date
Thu, 12 Oct 2017 21:27:07 +0800
changeset 7535
7ae4e26cb1e0
parent 7031
ee019285a52c
parent 6876
710a3c8b516e
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #include "precompiled.hpp"
aoqi@0 26 #include "classfile/systemDictionary.hpp"
aoqi@0 27 #include "classfile/vmSymbols.hpp"
aoqi@0 28 #include "gc_implementation/shared/liveRange.hpp"
aoqi@0 29 #include "gc_implementation/shared/markSweep.hpp"
aoqi@0 30 #include "gc_implementation/shared/spaceDecorator.hpp"
jmasa@7031 31 #include "gc_interface/collectedHeap.inline.hpp"
aoqi@0 32 #include "memory/blockOffsetTable.inline.hpp"
aoqi@0 33 #include "memory/defNewGeneration.hpp"
aoqi@0 34 #include "memory/genCollectedHeap.hpp"
aoqi@0 35 #include "memory/space.hpp"
aoqi@0 36 #include "memory/space.inline.hpp"
aoqi@0 37 #include "memory/universe.inline.hpp"
aoqi@0 38 #include "oops/oop.inline.hpp"
aoqi@0 39 #include "oops/oop.inline2.hpp"
aoqi@0 40 #include "runtime/java.hpp"
goetz@6912 41 #include "runtime/prefetch.inline.hpp"
goetz@6911 42 #include "runtime/orderAccess.inline.hpp"
aoqi@0 43 #include "runtime/safepoint.hpp"
aoqi@0 44 #include "utilities/copy.hpp"
aoqi@0 45 #include "utilities/globalDefinitions.hpp"
aoqi@0 46 #include "utilities/macros.hpp"
aoqi@0 47
aoqi@0 48 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
aoqi@0 49
aoqi@0 50 HeapWord* DirtyCardToOopClosure::get_actual_top(HeapWord* top,
aoqi@0 51 HeapWord* top_obj) {
aoqi@0 52 if (top_obj != NULL) {
aoqi@0 53 if (_sp->block_is_obj(top_obj)) {
aoqi@0 54 if (_precision == CardTableModRefBS::ObjHeadPreciseArray) {
aoqi@0 55 if (oop(top_obj)->is_objArray() || oop(top_obj)->is_typeArray()) {
aoqi@0 56 // An arrayOop is starting on the dirty card - since we do exact
aoqi@0 57 // store checks for objArrays we are done.
aoqi@0 58 } else {
aoqi@0 59 // Otherwise, it is possible that the object starting on the dirty
aoqi@0 60 // card spans the entire card, and that the store happened on a
aoqi@0 61 // later card. Figure out where the object ends.
aoqi@0 62 // Use the block_size() method of the space over which
aoqi@0 63 // the iteration is being done. That space (e.g. CMS) may have
aoqi@0 64 // specific requirements on object sizes which will
aoqi@0 65 // be reflected in the block_size() method.
aoqi@0 66 top = top_obj + oop(top_obj)->size();
aoqi@0 67 }
aoqi@0 68 }
aoqi@0 69 } else {
aoqi@0 70 top = top_obj;
aoqi@0 71 }
aoqi@0 72 } else {
aoqi@0 73 assert(top == _sp->end(), "only case where top_obj == NULL");
aoqi@0 74 }
aoqi@0 75 return top;
aoqi@0 76 }
aoqi@0 77
aoqi@0 78 void DirtyCardToOopClosure::walk_mem_region(MemRegion mr,
aoqi@0 79 HeapWord* bottom,
aoqi@0 80 HeapWord* top) {
aoqi@0 81 // 1. Blocks may or may not be objects.
aoqi@0 82 // 2. Even when a block_is_obj(), it may not entirely
aoqi@0 83 // occupy the block if the block quantum is larger than
aoqi@0 84 // the object size.
aoqi@0 85 // We can and should try to optimize by calling the non-MemRegion
aoqi@0 86 // version of oop_iterate() for all but the extremal objects
aoqi@0 87 // (for which we need to call the MemRegion version of
aoqi@0 88 // oop_iterate()) To be done post-beta XXX
aoqi@0 89 for (; bottom < top; bottom += _sp->block_size(bottom)) {
aoqi@0 90 // As in the case of contiguous space above, we'd like to
aoqi@0 91 // just use the value returned by oop_iterate to increment the
aoqi@0 92 // current pointer; unfortunately, that won't work in CMS because
aoqi@0 93 // we'd need an interface change (it seems) to have the space
aoqi@0 94 // "adjust the object size" (for instance pad it up to its
aoqi@0 95 // block alignment or minimum block size restrictions. XXX
aoqi@0 96 if (_sp->block_is_obj(bottom) &&
aoqi@0 97 !_sp->obj_allocated_since_save_marks(oop(bottom))) {
aoqi@0 98 oop(bottom)->oop_iterate(_cl, mr);
aoqi@0 99 }
aoqi@0 100 }
aoqi@0 101 }
aoqi@0 102
aoqi@0 103 // We get called with "mr" representing the dirty region
aoqi@0 104 // that we want to process. Because of imprecise marking,
aoqi@0 105 // we may need to extend the incoming "mr" to the right,
aoqi@0 106 // and scan more. However, because we may already have
aoqi@0 107 // scanned some of that extended region, we may need to
aoqi@0 108 // trim its right-end back some so we do not scan what
aoqi@0 109 // we (or another worker thread) may already have scanned
aoqi@0 110 // or planning to scan.
aoqi@0 111 void DirtyCardToOopClosure::do_MemRegion(MemRegion mr) {
aoqi@0 112
aoqi@0 113 // Some collectors need to do special things whenever their dirty
aoqi@0 114 // cards are processed. For instance, CMS must remember mutator updates
aoqi@0 115 // (i.e. dirty cards) so as to re-scan mutated objects.
aoqi@0 116 // Such work can be piggy-backed here on dirty card scanning, so as to make
aoqi@0 117 // it slightly more efficient than doing a complete non-detructive pre-scan
aoqi@0 118 // of the card table.
aoqi@0 119 MemRegionClosure* pCl = _sp->preconsumptionDirtyCardClosure();
aoqi@0 120 if (pCl != NULL) {
aoqi@0 121 pCl->do_MemRegion(mr);
aoqi@0 122 }
aoqi@0 123
aoqi@0 124 HeapWord* bottom = mr.start();
aoqi@0 125 HeapWord* last = mr.last();
aoqi@0 126 HeapWord* top = mr.end();
aoqi@0 127 HeapWord* bottom_obj;
aoqi@0 128 HeapWord* top_obj;
aoqi@0 129
aoqi@0 130 assert(_precision == CardTableModRefBS::ObjHeadPreciseArray ||
aoqi@0 131 _precision == CardTableModRefBS::Precise,
aoqi@0 132 "Only ones we deal with for now.");
aoqi@0 133
aoqi@0 134 assert(_precision != CardTableModRefBS::ObjHeadPreciseArray ||
aoqi@0 135 _cl->idempotent() || _last_bottom == NULL ||
aoqi@0 136 top <= _last_bottom,
aoqi@0 137 "Not decreasing");
aoqi@0 138 NOT_PRODUCT(_last_bottom = mr.start());
aoqi@0 139
aoqi@0 140 bottom_obj = _sp->block_start(bottom);
aoqi@0 141 top_obj = _sp->block_start(last);
aoqi@0 142
aoqi@0 143 assert(bottom_obj <= bottom, "just checking");
aoqi@0 144 assert(top_obj <= top, "just checking");
aoqi@0 145
aoqi@0 146 // Given what we think is the top of the memory region and
aoqi@0 147 // the start of the object at the top, get the actual
aoqi@0 148 // value of the top.
aoqi@0 149 top = get_actual_top(top, top_obj);
aoqi@0 150
aoqi@0 151 // If the previous call did some part of this region, don't redo.
aoqi@0 152 if (_precision == CardTableModRefBS::ObjHeadPreciseArray &&
aoqi@0 153 _min_done != NULL &&
aoqi@0 154 _min_done < top) {
aoqi@0 155 top = _min_done;
aoqi@0 156 }
aoqi@0 157
aoqi@0 158 // Top may have been reset, and in fact may be below bottom,
aoqi@0 159 // e.g. the dirty card region is entirely in a now free object
aoqi@0 160 // -- something that could happen with a concurrent sweeper.
aoqi@0 161 bottom = MIN2(bottom, top);
aoqi@0 162 MemRegion extended_mr = MemRegion(bottom, top);
aoqi@0 163 assert(bottom <= top &&
aoqi@0 164 (_precision != CardTableModRefBS::ObjHeadPreciseArray ||
aoqi@0 165 _min_done == NULL ||
aoqi@0 166 top <= _min_done),
aoqi@0 167 "overlap!");
aoqi@0 168
aoqi@0 169 // Walk the region if it is not empty; otherwise there is nothing to do.
aoqi@0 170 if (!extended_mr.is_empty()) {
aoqi@0 171 walk_mem_region(extended_mr, bottom_obj, top);
aoqi@0 172 }
aoqi@0 173
aoqi@0 174 // An idempotent closure might be applied in any order, so we don't
aoqi@0 175 // record a _min_done for it.
aoqi@0 176 if (!_cl->idempotent()) {
aoqi@0 177 _min_done = bottom;
aoqi@0 178 } else {
aoqi@0 179 assert(_min_done == _last_explicit_min_done,
aoqi@0 180 "Don't update _min_done for idempotent cl");
aoqi@0 181 }
aoqi@0 182 }
aoqi@0 183
aoqi@0 184 DirtyCardToOopClosure* Space::new_dcto_cl(ExtendedOopClosure* cl,
aoqi@0 185 CardTableModRefBS::PrecisionStyle precision,
aoqi@0 186 HeapWord* boundary) {
aoqi@0 187 return new DirtyCardToOopClosure(this, cl, precision, boundary);
aoqi@0 188 }
aoqi@0 189
aoqi@0 190 HeapWord* ContiguousSpaceDCTOC::get_actual_top(HeapWord* top,
aoqi@0 191 HeapWord* top_obj) {
aoqi@0 192 if (top_obj != NULL && top_obj < (_sp->toContiguousSpace())->top()) {
aoqi@0 193 if (_precision == CardTableModRefBS::ObjHeadPreciseArray) {
aoqi@0 194 if (oop(top_obj)->is_objArray() || oop(top_obj)->is_typeArray()) {
aoqi@0 195 // An arrayOop is starting on the dirty card - since we do exact
aoqi@0 196 // store checks for objArrays we are done.
aoqi@0 197 } else {
aoqi@0 198 // Otherwise, it is possible that the object starting on the dirty
aoqi@0 199 // card spans the entire card, and that the store happened on a
aoqi@0 200 // later card. Figure out where the object ends.
aoqi@0 201 assert(_sp->block_size(top_obj) == (size_t) oop(top_obj)->size(),
aoqi@0 202 "Block size and object size mismatch");
aoqi@0 203 top = top_obj + oop(top_obj)->size();
aoqi@0 204 }
aoqi@0 205 }
aoqi@0 206 } else {
aoqi@0 207 top = (_sp->toContiguousSpace())->top();
aoqi@0 208 }
aoqi@0 209 return top;
aoqi@0 210 }
aoqi@0 211
aoqi@0 212 void Filtering_DCTOC::walk_mem_region(MemRegion mr,
aoqi@0 213 HeapWord* bottom,
aoqi@0 214 HeapWord* top) {
aoqi@0 215 // Note that this assumption won't hold if we have a concurrent
aoqi@0 216 // collector in this space, which may have freed up objects after
aoqi@0 217 // they were dirtied and before the stop-the-world GC that is
aoqi@0 218 // examining cards here.
aoqi@0 219 assert(bottom < top, "ought to be at least one obj on a dirty card.");
aoqi@0 220
aoqi@0 221 if (_boundary != NULL) {
aoqi@0 222 // We have a boundary outside of which we don't want to look
aoqi@0 223 // at objects, so create a filtering closure around the
aoqi@0 224 // oop closure before walking the region.
aoqi@0 225 FilteringClosure filter(_boundary, _cl);
aoqi@0 226 walk_mem_region_with_cl(mr, bottom, top, &filter);
aoqi@0 227 } else {
aoqi@0 228 // No boundary, simply walk the heap with the oop closure.
aoqi@0 229 walk_mem_region_with_cl(mr, bottom, top, _cl);
aoqi@0 230 }
aoqi@0 231
aoqi@0 232 }
aoqi@0 233
aoqi@0 234 // We must replicate this so that the static type of "FilteringClosure"
aoqi@0 235 // (see above) is apparent at the oop_iterate calls.
aoqi@0 236 #define ContiguousSpaceDCTOC__walk_mem_region_with_cl_DEFN(ClosureType) \
aoqi@0 237 void ContiguousSpaceDCTOC::walk_mem_region_with_cl(MemRegion mr, \
aoqi@0 238 HeapWord* bottom, \
aoqi@0 239 HeapWord* top, \
aoqi@0 240 ClosureType* cl) { \
aoqi@0 241 bottom += oop(bottom)->oop_iterate(cl, mr); \
aoqi@0 242 if (bottom < top) { \
aoqi@0 243 HeapWord* next_obj = bottom + oop(bottom)->size(); \
aoqi@0 244 while (next_obj < top) { \
aoqi@0 245 /* Bottom lies entirely below top, so we can call the */ \
aoqi@0 246 /* non-memRegion version of oop_iterate below. */ \
aoqi@0 247 oop(bottom)->oop_iterate(cl); \
aoqi@0 248 bottom = next_obj; \
aoqi@0 249 next_obj = bottom + oop(bottom)->size(); \
aoqi@0 250 } \
aoqi@0 251 /* Last object. */ \
aoqi@0 252 oop(bottom)->oop_iterate(cl, mr); \
aoqi@0 253 } \
aoqi@0 254 }
aoqi@0 255
aoqi@0 256 // (There are only two of these, rather than N, because the split is due
aoqi@0 257 // only to the introduction of the FilteringClosure, a local part of the
aoqi@0 258 // impl of this abstraction.)
aoqi@0 259 ContiguousSpaceDCTOC__walk_mem_region_with_cl_DEFN(ExtendedOopClosure)
aoqi@0 260 ContiguousSpaceDCTOC__walk_mem_region_with_cl_DEFN(FilteringClosure)
aoqi@0 261
aoqi@0 262 DirtyCardToOopClosure*
aoqi@0 263 ContiguousSpace::new_dcto_cl(ExtendedOopClosure* cl,
aoqi@0 264 CardTableModRefBS::PrecisionStyle precision,
aoqi@0 265 HeapWord* boundary) {
aoqi@0 266 return new ContiguousSpaceDCTOC(this, cl, precision, boundary);
aoqi@0 267 }
aoqi@0 268
aoqi@0 269 void Space::initialize(MemRegion mr,
aoqi@0 270 bool clear_space,
aoqi@0 271 bool mangle_space) {
aoqi@0 272 HeapWord* bottom = mr.start();
aoqi@0 273 HeapWord* end = mr.end();
aoqi@0 274 assert(Universe::on_page_boundary(bottom) && Universe::on_page_boundary(end),
aoqi@0 275 "invalid space boundaries");
aoqi@0 276 set_bottom(bottom);
aoqi@0 277 set_end(end);
aoqi@0 278 if (clear_space) clear(mangle_space);
aoqi@0 279 }
aoqi@0 280
aoqi@0 281 void Space::clear(bool mangle_space) {
aoqi@0 282 if (ZapUnusedHeapArea && mangle_space) {
aoqi@0 283 mangle_unused_area();
aoqi@0 284 }
aoqi@0 285 }
aoqi@0 286
aoqi@0 287 ContiguousSpace::ContiguousSpace(): CompactibleSpace(), _top(NULL),
aoqi@0 288 _concurrent_iteration_safe_limit(NULL) {
aoqi@0 289 _mangler = new GenSpaceMangler(this);
aoqi@0 290 }
aoqi@0 291
aoqi@0 292 ContiguousSpace::~ContiguousSpace() {
aoqi@0 293 delete _mangler;
aoqi@0 294 }
aoqi@0 295
aoqi@0 296 void ContiguousSpace::initialize(MemRegion mr,
aoqi@0 297 bool clear_space,
aoqi@0 298 bool mangle_space)
aoqi@0 299 {
aoqi@0 300 CompactibleSpace::initialize(mr, clear_space, mangle_space);
aoqi@0 301 set_concurrent_iteration_safe_limit(top());
aoqi@0 302 }
aoqi@0 303
aoqi@0 304 void ContiguousSpace::clear(bool mangle_space) {
aoqi@0 305 set_top(bottom());
aoqi@0 306 set_saved_mark();
aoqi@0 307 CompactibleSpace::clear(mangle_space);
aoqi@0 308 }
aoqi@0 309
aoqi@0 310 bool ContiguousSpace::is_free_block(const HeapWord* p) const {
aoqi@0 311 return p >= _top;
aoqi@0 312 }
aoqi@0 313
aoqi@0 314 void OffsetTableContigSpace::clear(bool mangle_space) {
aoqi@0 315 ContiguousSpace::clear(mangle_space);
aoqi@0 316 _offsets.initialize_threshold();
aoqi@0 317 }
aoqi@0 318
aoqi@0 319 void OffsetTableContigSpace::set_bottom(HeapWord* new_bottom) {
aoqi@0 320 Space::set_bottom(new_bottom);
aoqi@0 321 _offsets.set_bottom(new_bottom);
aoqi@0 322 }
aoqi@0 323
aoqi@0 324 void OffsetTableContigSpace::set_end(HeapWord* new_end) {
aoqi@0 325 // Space should not advertize an increase in size
aoqi@0 326 // until after the underlying offest table has been enlarged.
aoqi@0 327 _offsets.resize(pointer_delta(new_end, bottom()));
aoqi@0 328 Space::set_end(new_end);
aoqi@0 329 }
aoqi@0 330
aoqi@0 331 #ifndef PRODUCT
aoqi@0 332
aoqi@0 333 void ContiguousSpace::set_top_for_allocations(HeapWord* v) {
aoqi@0 334 mangler()->set_top_for_allocations(v);
aoqi@0 335 }
aoqi@0 336 void ContiguousSpace::set_top_for_allocations() {
aoqi@0 337 mangler()->set_top_for_allocations(top());
aoqi@0 338 }
aoqi@0 339 void ContiguousSpace::check_mangled_unused_area(HeapWord* limit) {
aoqi@0 340 mangler()->check_mangled_unused_area(limit);
aoqi@0 341 }
aoqi@0 342
aoqi@0 343 void ContiguousSpace::check_mangled_unused_area_complete() {
aoqi@0 344 mangler()->check_mangled_unused_area_complete();
aoqi@0 345 }
aoqi@0 346
aoqi@0 347 // Mangled only the unused space that has not previously
aoqi@0 348 // been mangled and that has not been allocated since being
aoqi@0 349 // mangled.
aoqi@0 350 void ContiguousSpace::mangle_unused_area() {
aoqi@0 351 mangler()->mangle_unused_area();
aoqi@0 352 }
aoqi@0 353 void ContiguousSpace::mangle_unused_area_complete() {
aoqi@0 354 mangler()->mangle_unused_area_complete();
aoqi@0 355 }
aoqi@0 356 void ContiguousSpace::mangle_region(MemRegion mr) {
aoqi@0 357 // Although this method uses SpaceMangler::mangle_region() which
aoqi@0 358 // is not specific to a space, the when the ContiguousSpace version
aoqi@0 359 // is called, it is always with regard to a space and this
aoqi@0 360 // bounds checking is appropriate.
aoqi@0 361 MemRegion space_mr(bottom(), end());
aoqi@0 362 assert(space_mr.contains(mr), "Mangling outside space");
aoqi@0 363 SpaceMangler::mangle_region(mr);
aoqi@0 364 }
aoqi@0 365 #endif // NOT_PRODUCT
aoqi@0 366
aoqi@0 367 void CompactibleSpace::initialize(MemRegion mr,
aoqi@0 368 bool clear_space,
aoqi@0 369 bool mangle_space) {
aoqi@0 370 Space::initialize(mr, clear_space, mangle_space);
aoqi@0 371 set_compaction_top(bottom());
aoqi@0 372 _next_compaction_space = NULL;
aoqi@0 373 }
aoqi@0 374
aoqi@0 375 void CompactibleSpace::clear(bool mangle_space) {
aoqi@0 376 Space::clear(mangle_space);
aoqi@0 377 _compaction_top = bottom();
aoqi@0 378 }
aoqi@0 379
aoqi@0 380 HeapWord* CompactibleSpace::forward(oop q, size_t size,
aoqi@0 381 CompactPoint* cp, HeapWord* compact_top) {
aoqi@0 382 // q is alive
aoqi@0 383 // First check if we should switch compaction space
aoqi@0 384 assert(this == cp->space, "'this' should be current compaction space.");
aoqi@0 385 size_t compaction_max_size = pointer_delta(end(), compact_top);
aoqi@0 386 while (size > compaction_max_size) {
aoqi@0 387 // switch to next compaction space
aoqi@0 388 cp->space->set_compaction_top(compact_top);
aoqi@0 389 cp->space = cp->space->next_compaction_space();
aoqi@0 390 if (cp->space == NULL) {
aoqi@0 391 cp->gen = GenCollectedHeap::heap()->prev_gen(cp->gen);
aoqi@0 392 assert(cp->gen != NULL, "compaction must succeed");
aoqi@0 393 cp->space = cp->gen->first_compaction_space();
aoqi@0 394 assert(cp->space != NULL, "generation must have a first compaction space");
aoqi@0 395 }
aoqi@0 396 compact_top = cp->space->bottom();
aoqi@0 397 cp->space->set_compaction_top(compact_top);
aoqi@0 398 cp->threshold = cp->space->initialize_threshold();
aoqi@0 399 compaction_max_size = pointer_delta(cp->space->end(), compact_top);
aoqi@0 400 }
aoqi@0 401
aoqi@0 402 // store the forwarding pointer into the mark word
aoqi@0 403 if ((HeapWord*)q != compact_top) {
aoqi@0 404 q->forward_to(oop(compact_top));
aoqi@0 405 assert(q->is_gc_marked(), "encoding the pointer should preserve the mark");
aoqi@0 406 } else {
aoqi@0 407 // if the object isn't moving we can just set the mark to the default
aoqi@0 408 // mark and handle it specially later on.
aoqi@0 409 q->init_mark();
aoqi@0 410 assert(q->forwardee() == NULL, "should be forwarded to NULL");
aoqi@0 411 }
aoqi@0 412
aoqi@0 413 compact_top += size;
aoqi@0 414
aoqi@0 415 // we need to update the offset table so that the beginnings of objects can be
aoqi@0 416 // found during scavenge. Note that we are updating the offset table based on
aoqi@0 417 // where the object will be once the compaction phase finishes.
aoqi@0 418 if (compact_top > cp->threshold)
aoqi@0 419 cp->threshold =
aoqi@0 420 cp->space->cross_threshold(compact_top - size, compact_top);
aoqi@0 421 return compact_top;
aoqi@0 422 }
aoqi@0 423
aoqi@0 424
aoqi@0 425 bool CompactibleSpace::insert_deadspace(size_t& allowed_deadspace_words,
aoqi@0 426 HeapWord* q, size_t deadlength) {
aoqi@0 427 if (allowed_deadspace_words >= deadlength) {
aoqi@0 428 allowed_deadspace_words -= deadlength;
aoqi@0 429 CollectedHeap::fill_with_object(q, deadlength);
aoqi@0 430 oop(q)->set_mark(oop(q)->mark()->set_marked());
aoqi@0 431 assert((int) deadlength == oop(q)->size(), "bad filler object size");
aoqi@0 432 // Recall that we required "q == compaction_top".
aoqi@0 433 return true;
aoqi@0 434 } else {
aoqi@0 435 allowed_deadspace_words = 0;
aoqi@0 436 return false;
aoqi@0 437 }
aoqi@0 438 }
aoqi@0 439
aoqi@0 440 #define block_is_always_obj(q) true
aoqi@0 441 #define obj_size(q) oop(q)->size()
aoqi@0 442 #define adjust_obj_size(s) s
aoqi@0 443
aoqi@0 444 void CompactibleSpace::prepare_for_compaction(CompactPoint* cp) {
aoqi@0 445 SCAN_AND_FORWARD(cp, end, block_is_obj, block_size);
aoqi@0 446 }
aoqi@0 447
aoqi@0 448 // Faster object search.
aoqi@0 449 void ContiguousSpace::prepare_for_compaction(CompactPoint* cp) {
aoqi@0 450 SCAN_AND_FORWARD(cp, top, block_is_always_obj, obj_size);
aoqi@0 451 }
aoqi@0 452
aoqi@0 453 void Space::adjust_pointers() {
aoqi@0 454 // adjust all the interior pointers to point at the new locations of objects
aoqi@0 455 // Used by MarkSweep::mark_sweep_phase3()
aoqi@0 456
aoqi@0 457 // First check to see if there is any work to be done.
aoqi@0 458 if (used() == 0) {
aoqi@0 459 return; // Nothing to do.
aoqi@0 460 }
aoqi@0 461
aoqi@0 462 // Otherwise...
aoqi@0 463 HeapWord* q = bottom();
aoqi@0 464 HeapWord* t = end();
aoqi@0 465
aoqi@0 466 debug_only(HeapWord* prev_q = NULL);
aoqi@0 467 while (q < t) {
aoqi@0 468 if (oop(q)->is_gc_marked()) {
aoqi@0 469 // q is alive
aoqi@0 470
aoqi@0 471 // point all the oops to the new location
aoqi@0 472 size_t size = oop(q)->adjust_pointers();
aoqi@0 473
aoqi@0 474 debug_only(prev_q = q);
aoqi@0 475
aoqi@0 476 q += size;
aoqi@0 477 } else {
aoqi@0 478 // q is not a live object. But we're not in a compactible space,
aoqi@0 479 // So we don't have live ranges.
aoqi@0 480 debug_only(prev_q = q);
aoqi@0 481 q += block_size(q);
aoqi@0 482 assert(q > prev_q, "we should be moving forward through memory");
aoqi@0 483 }
aoqi@0 484 }
aoqi@0 485 assert(q == t, "just checking");
aoqi@0 486 }
aoqi@0 487
aoqi@0 488 void CompactibleSpace::adjust_pointers() {
aoqi@0 489 // Check first is there is any work to do.
aoqi@0 490 if (used() == 0) {
aoqi@0 491 return; // Nothing to do.
aoqi@0 492 }
aoqi@0 493
aoqi@0 494 SCAN_AND_ADJUST_POINTERS(adjust_obj_size);
aoqi@0 495 }
aoqi@0 496
aoqi@0 497 void CompactibleSpace::compact() {
aoqi@0 498 SCAN_AND_COMPACT(obj_size);
aoqi@0 499 }
aoqi@0 500
aoqi@0 501 void Space::print_short() const { print_short_on(tty); }
aoqi@0 502
aoqi@0 503 void Space::print_short_on(outputStream* st) const {
aoqi@0 504 st->print(" space " SIZE_FORMAT "K, %3d%% used", capacity() / K,
aoqi@0 505 (int) ((double) used() * 100 / capacity()));
aoqi@0 506 }
aoqi@0 507
aoqi@0 508 void Space::print() const { print_on(tty); }
aoqi@0 509
aoqi@0 510 void Space::print_on(outputStream* st) const {
aoqi@0 511 print_short_on(st);
aoqi@0 512 st->print_cr(" [" INTPTR_FORMAT ", " INTPTR_FORMAT ")",
aoqi@0 513 bottom(), end());
aoqi@0 514 }
aoqi@0 515
aoqi@0 516 void ContiguousSpace::print_on(outputStream* st) const {
aoqi@0 517 print_short_on(st);
aoqi@0 518 st->print_cr(" [" INTPTR_FORMAT ", " INTPTR_FORMAT ", " INTPTR_FORMAT ")",
aoqi@0 519 bottom(), top(), end());
aoqi@0 520 }
aoqi@0 521
aoqi@0 522 void OffsetTableContigSpace::print_on(outputStream* st) const {
aoqi@0 523 print_short_on(st);
aoqi@0 524 st->print_cr(" [" INTPTR_FORMAT ", " INTPTR_FORMAT ", "
aoqi@0 525 INTPTR_FORMAT ", " INTPTR_FORMAT ")",
aoqi@0 526 bottom(), top(), _offsets.threshold(), end());
aoqi@0 527 }
aoqi@0 528
aoqi@0 529 void ContiguousSpace::verify() const {
aoqi@0 530 HeapWord* p = bottom();
aoqi@0 531 HeapWord* t = top();
aoqi@0 532 HeapWord* prev_p = NULL;
aoqi@0 533 while (p < t) {
aoqi@0 534 oop(p)->verify();
aoqi@0 535 prev_p = p;
aoqi@0 536 p += oop(p)->size();
aoqi@0 537 }
aoqi@0 538 guarantee(p == top(), "end of last object must match end of space");
aoqi@0 539 if (top() != end()) {
aoqi@0 540 guarantee(top() == block_start_const(end()-1) &&
aoqi@0 541 top() == block_start_const(top()),
aoqi@0 542 "top should be start of unallocated block, if it exists");
aoqi@0 543 }
aoqi@0 544 }
aoqi@0 545
aoqi@0 546 void Space::oop_iterate(ExtendedOopClosure* blk) {
aoqi@0 547 ObjectToOopClosure blk2(blk);
aoqi@0 548 object_iterate(&blk2);
aoqi@0 549 }
aoqi@0 550
aoqi@0 551 bool Space::obj_is_alive(const HeapWord* p) const {
aoqi@0 552 assert (block_is_obj(p), "The address should point to an object");
aoqi@0 553 return true;
aoqi@0 554 }
aoqi@0 555
aoqi@0 556 #if INCLUDE_ALL_GCS
aoqi@0 557 #define ContigSpace_PAR_OOP_ITERATE_DEFN(OopClosureType, nv_suffix) \
aoqi@0 558 \
aoqi@0 559 void ContiguousSpace::par_oop_iterate(MemRegion mr, OopClosureType* blk) {\
aoqi@0 560 HeapWord* obj_addr = mr.start(); \
aoqi@0 561 HeapWord* t = mr.end(); \
aoqi@0 562 while (obj_addr < t) { \
aoqi@0 563 assert(oop(obj_addr)->is_oop(), "Should be an oop"); \
aoqi@0 564 obj_addr += oop(obj_addr)->oop_iterate(blk); \
aoqi@0 565 } \
aoqi@0 566 }
aoqi@0 567
aoqi@0 568 ALL_PAR_OOP_ITERATE_CLOSURES(ContigSpace_PAR_OOP_ITERATE_DEFN)
aoqi@0 569
aoqi@0 570 #undef ContigSpace_PAR_OOP_ITERATE_DEFN
aoqi@0 571 #endif // INCLUDE_ALL_GCS
aoqi@0 572
aoqi@0 573 void ContiguousSpace::oop_iterate(ExtendedOopClosure* blk) {
aoqi@0 574 if (is_empty()) return;
aoqi@0 575 HeapWord* obj_addr = bottom();
aoqi@0 576 HeapWord* t = top();
aoqi@0 577 // Could call objects iterate, but this is easier.
aoqi@0 578 while (obj_addr < t) {
aoqi@0 579 obj_addr += oop(obj_addr)->oop_iterate(blk);
aoqi@0 580 }
aoqi@0 581 }
aoqi@0 582
aoqi@0 583 void ContiguousSpace::object_iterate(ObjectClosure* blk) {
aoqi@0 584 if (is_empty()) return;
aoqi@0 585 WaterMark bm = bottom_mark();
aoqi@0 586 object_iterate_from(bm, blk);
aoqi@0 587 }
aoqi@0 588
aoqi@0 589 // For a continguous space object_iterate() and safe_object_iterate()
aoqi@0 590 // are the same.
aoqi@0 591 void ContiguousSpace::safe_object_iterate(ObjectClosure* blk) {
aoqi@0 592 object_iterate(blk);
aoqi@0 593 }
aoqi@0 594
aoqi@0 595 void ContiguousSpace::object_iterate_from(WaterMark mark, ObjectClosure* blk) {
aoqi@0 596 assert(mark.space() == this, "Mark does not match space");
aoqi@0 597 HeapWord* p = mark.point();
aoqi@0 598 while (p < top()) {
aoqi@0 599 blk->do_object(oop(p));
aoqi@0 600 p += oop(p)->size();
aoqi@0 601 }
aoqi@0 602 }
aoqi@0 603
aoqi@0 604 HeapWord*
aoqi@0 605 ContiguousSpace::object_iterate_careful(ObjectClosureCareful* blk) {
aoqi@0 606 HeapWord * limit = concurrent_iteration_safe_limit();
aoqi@0 607 assert(limit <= top(), "sanity check");
aoqi@0 608 for (HeapWord* p = bottom(); p < limit;) {
aoqi@0 609 size_t size = blk->do_object_careful(oop(p));
aoqi@0 610 if (size == 0) {
aoqi@0 611 return p; // failed at p
aoqi@0 612 } else {
aoqi@0 613 p += size;
aoqi@0 614 }
aoqi@0 615 }
aoqi@0 616 return NULL; // all done
aoqi@0 617 }
aoqi@0 618
aoqi@0 619 #define ContigSpace_OOP_SINCE_SAVE_MARKS_DEFN(OopClosureType, nv_suffix) \
aoqi@0 620 \
aoqi@0 621 void ContiguousSpace:: \
aoqi@0 622 oop_since_save_marks_iterate##nv_suffix(OopClosureType* blk) { \
aoqi@0 623 HeapWord* t; \
aoqi@0 624 HeapWord* p = saved_mark_word(); \
aoqi@0 625 assert(p != NULL, "expected saved mark"); \
aoqi@0 626 \
aoqi@0 627 const intx interval = PrefetchScanIntervalInBytes; \
aoqi@0 628 do { \
aoqi@0 629 t = top(); \
aoqi@0 630 while (p < t) { \
aoqi@0 631 Prefetch::write(p, interval); \
aoqi@0 632 debug_only(HeapWord* prev = p); \
aoqi@0 633 oop m = oop(p); \
aoqi@0 634 p += m->oop_iterate(blk); \
aoqi@0 635 } \
aoqi@0 636 } while (t < top()); \
aoqi@0 637 \
aoqi@0 638 set_saved_mark_word(p); \
aoqi@0 639 }
aoqi@0 640
aoqi@0 641 ALL_SINCE_SAVE_MARKS_CLOSURES(ContigSpace_OOP_SINCE_SAVE_MARKS_DEFN)
aoqi@0 642
aoqi@0 643 #undef ContigSpace_OOP_SINCE_SAVE_MARKS_DEFN
aoqi@0 644
aoqi@0 645 // Very general, slow implementation.
aoqi@0 646 HeapWord* ContiguousSpace::block_start_const(const void* p) const {
aoqi@0 647 assert(MemRegion(bottom(), end()).contains(p),
aoqi@0 648 err_msg("p (" PTR_FORMAT ") not in space [" PTR_FORMAT ", " PTR_FORMAT ")",
aoqi@0 649 p, bottom(), end()));
aoqi@0 650 if (p >= top()) {
aoqi@0 651 return top();
aoqi@0 652 } else {
aoqi@0 653 HeapWord* last = bottom();
aoqi@0 654 HeapWord* cur = last;
aoqi@0 655 while (cur <= p) {
aoqi@0 656 last = cur;
aoqi@0 657 cur += oop(cur)->size();
aoqi@0 658 }
aoqi@0 659 assert(oop(last)->is_oop(),
aoqi@0 660 err_msg(PTR_FORMAT " should be an object start", last));
aoqi@0 661 return last;
aoqi@0 662 }
aoqi@0 663 }
aoqi@0 664
aoqi@0 665 size_t ContiguousSpace::block_size(const HeapWord* p) const {
aoqi@0 666 assert(MemRegion(bottom(), end()).contains(p),
aoqi@0 667 err_msg("p (" PTR_FORMAT ") not in space [" PTR_FORMAT ", " PTR_FORMAT ")",
aoqi@0 668 p, bottom(), end()));
aoqi@0 669 HeapWord* current_top = top();
aoqi@0 670 assert(p <= current_top,
aoqi@0 671 err_msg("p > current top - p: " PTR_FORMAT ", current top: " PTR_FORMAT,
aoqi@0 672 p, current_top));
aoqi@0 673 assert(p == current_top || oop(p)->is_oop(),
aoqi@0 674 err_msg("p (" PTR_FORMAT ") is not a block start - "
aoqi@0 675 "current_top: " PTR_FORMAT ", is_oop: %s",
aoqi@0 676 p, current_top, BOOL_TO_STR(oop(p)->is_oop())));
aoqi@0 677 if (p < current_top) {
aoqi@0 678 return oop(p)->size();
aoqi@0 679 } else {
aoqi@0 680 assert(p == current_top, "just checking");
aoqi@0 681 return pointer_delta(end(), (HeapWord*) p);
aoqi@0 682 }
aoqi@0 683 }
aoqi@0 684
aoqi@0 685 // This version requires locking.
aoqi@0 686 inline HeapWord* ContiguousSpace::allocate_impl(size_t size,
aoqi@0 687 HeapWord* const end_value) {
aoqi@0 688 assert(Heap_lock->owned_by_self() ||
mgerdin@6990 689 (SafepointSynchronize::is_at_safepoint() && Thread::current()->is_VM_thread()),
aoqi@0 690 "not locked");
aoqi@0 691 HeapWord* obj = top();
aoqi@0 692 if (pointer_delta(end_value, obj) >= size) {
aoqi@0 693 HeapWord* new_top = obj + size;
aoqi@0 694 set_top(new_top);
aoqi@0 695 assert(is_aligned(obj) && is_aligned(new_top), "checking alignment");
aoqi@0 696 return obj;
aoqi@0 697 } else {
aoqi@0 698 return NULL;
aoqi@0 699 }
aoqi@0 700 }
aoqi@0 701
aoqi@0 702 // This version is lock-free.
aoqi@0 703 inline HeapWord* ContiguousSpace::par_allocate_impl(size_t size,
aoqi@0 704 HeapWord* const end_value) {
aoqi@0 705 do {
aoqi@0 706 HeapWord* obj = top();
aoqi@0 707 if (pointer_delta(end_value, obj) >= size) {
aoqi@0 708 HeapWord* new_top = obj + size;
aoqi@0 709 HeapWord* result = (HeapWord*)Atomic::cmpxchg_ptr(new_top, top_addr(), obj);
aoqi@0 710 // result can be one of two:
aoqi@0 711 // the old top value: the exchange succeeded
aoqi@0 712 // otherwise: the new value of the top is returned.
aoqi@0 713 if (result == obj) {
aoqi@0 714 assert(is_aligned(obj) && is_aligned(new_top), "checking alignment");
aoqi@0 715 return obj;
aoqi@0 716 }
aoqi@0 717 } else {
aoqi@0 718 return NULL;
aoqi@0 719 }
aoqi@0 720 } while (true);
aoqi@0 721 }
aoqi@0 722
jmasa@7031 723 HeapWord* ContiguousSpace::allocate_aligned(size_t size) {
jmasa@7031 724 assert(Heap_lock->owned_by_self() || (SafepointSynchronize::is_at_safepoint() && Thread::current()->is_VM_thread()), "not locked");
jmasa@7031 725 HeapWord* end_value = end();
jmasa@7031 726
jmasa@7031 727 HeapWord* obj = CollectedHeap::align_allocation_or_fail(top(), end_value, SurvivorAlignmentInBytes);
jmasa@7031 728 if (obj == NULL) {
jmasa@7031 729 return NULL;
jmasa@7031 730 }
jmasa@7031 731
jmasa@7031 732 if (pointer_delta(end_value, obj) >= size) {
jmasa@7031 733 HeapWord* new_top = obj + size;
jmasa@7031 734 set_top(new_top);
jmasa@7031 735 assert(is_ptr_aligned(obj, SurvivorAlignmentInBytes) && is_aligned(new_top),
jmasa@7031 736 "checking alignment");
jmasa@7031 737 return obj;
jmasa@7031 738 } else {
jmasa@7031 739 set_top(obj);
jmasa@7031 740 return NULL;
jmasa@7031 741 }
jmasa@7031 742 }
jmasa@7031 743
aoqi@0 744 // Requires locking.
aoqi@0 745 HeapWord* ContiguousSpace::allocate(size_t size) {
aoqi@0 746 return allocate_impl(size, end());
aoqi@0 747 }
aoqi@0 748
aoqi@0 749 // Lock-free.
aoqi@0 750 HeapWord* ContiguousSpace::par_allocate(size_t size) {
aoqi@0 751 return par_allocate_impl(size, end());
aoqi@0 752 }
aoqi@0 753
aoqi@0 754 void ContiguousSpace::allocate_temporary_filler(int factor) {
aoqi@0 755 // allocate temporary type array decreasing free size with factor 'factor'
aoqi@0 756 assert(factor >= 0, "just checking");
aoqi@0 757 size_t size = pointer_delta(end(), top());
aoqi@0 758
aoqi@0 759 // if space is full, return
aoqi@0 760 if (size == 0) return;
aoqi@0 761
aoqi@0 762 if (factor > 0) {
aoqi@0 763 size -= size/factor;
aoqi@0 764 }
aoqi@0 765 size = align_object_size(size);
aoqi@0 766
aoqi@0 767 const size_t array_header_size = typeArrayOopDesc::header_size(T_INT);
aoqi@0 768 if (size >= (size_t)align_object_size(array_header_size)) {
aoqi@0 769 size_t length = (size - array_header_size) * (HeapWordSize / sizeof(jint));
aoqi@0 770 // allocate uninitialized int array
aoqi@0 771 typeArrayOop t = (typeArrayOop) allocate(size);
aoqi@0 772 assert(t != NULL, "allocation should succeed");
aoqi@0 773 t->set_mark(markOopDesc::prototype());
aoqi@0 774 t->set_klass(Universe::intArrayKlassObj());
aoqi@0 775 t->set_length((int)length);
aoqi@0 776 } else {
aoqi@0 777 assert(size == CollectedHeap::min_fill_size(),
aoqi@0 778 "size for smallest fake object doesn't match");
aoqi@0 779 instanceOop obj = (instanceOop) allocate(size);
aoqi@0 780 obj->set_mark(markOopDesc::prototype());
aoqi@0 781 obj->set_klass_gap(0);
aoqi@0 782 obj->set_klass(SystemDictionary::Object_klass());
aoqi@0 783 }
aoqi@0 784 }
aoqi@0 785
aoqi@0 786 void EdenSpace::clear(bool mangle_space) {
aoqi@0 787 ContiguousSpace::clear(mangle_space);
aoqi@0 788 set_soft_end(end());
aoqi@0 789 }
aoqi@0 790
aoqi@0 791 // Requires locking.
aoqi@0 792 HeapWord* EdenSpace::allocate(size_t size) {
aoqi@0 793 return allocate_impl(size, soft_end());
aoqi@0 794 }
aoqi@0 795
aoqi@0 796 // Lock-free.
aoqi@0 797 HeapWord* EdenSpace::par_allocate(size_t size) {
aoqi@0 798 return par_allocate_impl(size, soft_end());
aoqi@0 799 }
aoqi@0 800
aoqi@0 801 HeapWord* ConcEdenSpace::par_allocate(size_t size)
aoqi@0 802 {
aoqi@0 803 do {
aoqi@0 804 // The invariant is top() should be read before end() because
aoqi@0 805 // top() can't be greater than end(), so if an update of _soft_end
aoqi@0 806 // occurs between 'end_val = end();' and 'top_val = top();' top()
aoqi@0 807 // also can grow up to the new end() and the condition
aoqi@0 808 // 'top_val > end_val' is true. To ensure the loading order
aoqi@0 809 // OrderAccess::loadload() is required after top() read.
aoqi@0 810 HeapWord* obj = top();
aoqi@0 811 OrderAccess::loadload();
aoqi@0 812 if (pointer_delta(*soft_end_addr(), obj) >= size) {
aoqi@0 813 HeapWord* new_top = obj + size;
aoqi@0 814 HeapWord* result = (HeapWord*)Atomic::cmpxchg_ptr(new_top, top_addr(), obj);
aoqi@0 815 // result can be one of two:
aoqi@0 816 // the old top value: the exchange succeeded
aoqi@0 817 // otherwise: the new value of the top is returned.
aoqi@0 818 if (result == obj) {
aoqi@0 819 assert(is_aligned(obj) && is_aligned(new_top), "checking alignment");
aoqi@0 820 return obj;
aoqi@0 821 }
aoqi@0 822 } else {
aoqi@0 823 return NULL;
aoqi@0 824 }
aoqi@0 825 } while (true);
aoqi@0 826 }
aoqi@0 827
aoqi@0 828
aoqi@0 829 HeapWord* OffsetTableContigSpace::initialize_threshold() {
aoqi@0 830 return _offsets.initialize_threshold();
aoqi@0 831 }
aoqi@0 832
aoqi@0 833 HeapWord* OffsetTableContigSpace::cross_threshold(HeapWord* start, HeapWord* end) {
aoqi@0 834 _offsets.alloc_block(start, end);
aoqi@0 835 return _offsets.threshold();
aoqi@0 836 }
aoqi@0 837
aoqi@0 838 OffsetTableContigSpace::OffsetTableContigSpace(BlockOffsetSharedArray* sharedOffsetArray,
aoqi@0 839 MemRegion mr) :
aoqi@0 840 _offsets(sharedOffsetArray, mr),
aoqi@0 841 _par_alloc_lock(Mutex::leaf, "OffsetTableContigSpace par alloc lock", true)
aoqi@0 842 {
aoqi@0 843 _offsets.set_contig_space(this);
aoqi@0 844 initialize(mr, SpaceDecorator::Clear, SpaceDecorator::Mangle);
aoqi@0 845 }
aoqi@0 846
aoqi@0 847 #define OBJ_SAMPLE_INTERVAL 0
aoqi@0 848 #define BLOCK_SAMPLE_INTERVAL 100
aoqi@0 849
aoqi@0 850 void OffsetTableContigSpace::verify() const {
aoqi@0 851 HeapWord* p = bottom();
aoqi@0 852 HeapWord* prev_p = NULL;
aoqi@0 853 int objs = 0;
aoqi@0 854 int blocks = 0;
aoqi@0 855
aoqi@0 856 if (VerifyObjectStartArray) {
aoqi@0 857 _offsets.verify();
aoqi@0 858 }
aoqi@0 859
aoqi@0 860 while (p < top()) {
aoqi@0 861 size_t size = oop(p)->size();
aoqi@0 862 // For a sampling of objects in the space, find it using the
aoqi@0 863 // block offset table.
aoqi@0 864 if (blocks == BLOCK_SAMPLE_INTERVAL) {
aoqi@0 865 guarantee(p == block_start_const(p + (size/2)),
aoqi@0 866 "check offset computation");
aoqi@0 867 blocks = 0;
aoqi@0 868 } else {
aoqi@0 869 blocks++;
aoqi@0 870 }
aoqi@0 871
aoqi@0 872 if (objs == OBJ_SAMPLE_INTERVAL) {
aoqi@0 873 oop(p)->verify();
aoqi@0 874 objs = 0;
aoqi@0 875 } else {
aoqi@0 876 objs++;
aoqi@0 877 }
aoqi@0 878 prev_p = p;
aoqi@0 879 p += size;
aoqi@0 880 }
aoqi@0 881 guarantee(p == top(), "end of last object must match end of space");
aoqi@0 882 }
aoqi@0 883
aoqi@0 884
aoqi@0 885 size_t TenuredSpace::allowed_dead_ratio() const {
aoqi@0 886 return MarkSweepDeadRatio;
aoqi@0 887 }

mercurial