Thu, 28 Jun 2012 17:03:16 -0400
6995781: Native Memory Tracking (Phase 1)
7151532: DCmd for hotspot native memory tracking
Summary: Implementation of native memory tracking phase 1, which tracks VM native memory usage, and related DCmd
Reviewed-by: acorn, coleenp, fparain
1 /*
2 * Copyright (c) 2000, 2011, 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 */
25 #include "precompiled.hpp"
26 #include "gc_interface/collectedHeap.inline.hpp"
27 #include "memory/blockOffsetTable.inline.hpp"
28 #include "memory/iterator.hpp"
29 #include "memory/space.inline.hpp"
30 #include "memory/universe.hpp"
31 #include "oops/oop.inline.hpp"
32 #include "runtime/java.hpp"
33 #include "services/memTracker.hpp"
35 //////////////////////////////////////////////////////////////////////
36 // BlockOffsetSharedArray
37 //////////////////////////////////////////////////////////////////////
39 BlockOffsetSharedArray::BlockOffsetSharedArray(MemRegion reserved,
40 size_t init_word_size):
41 _reserved(reserved), _end(NULL)
42 {
43 size_t size = compute_size(reserved.word_size());
44 ReservedSpace rs(size);
45 if (!rs.is_reserved()) {
46 vm_exit_during_initialization("Could not reserve enough space for heap offset array");
47 }
49 MemTracker::record_virtual_memory_type((address)rs.base(), mtGC);
51 if (!_vs.initialize(rs, 0)) {
52 vm_exit_during_initialization("Could not reserve enough space for heap offset array");
53 }
54 _offset_array = (u_char*)_vs.low_boundary();
55 resize(init_word_size);
56 if (TraceBlockOffsetTable) {
57 gclog_or_tty->print_cr("BlockOffsetSharedArray::BlockOffsetSharedArray: ");
58 gclog_or_tty->print_cr(" "
59 " rs.base(): " INTPTR_FORMAT
60 " rs.size(): " INTPTR_FORMAT
61 " rs end(): " INTPTR_FORMAT,
62 rs.base(), rs.size(), rs.base() + rs.size());
63 gclog_or_tty->print_cr(" "
64 " _vs.low_boundary(): " INTPTR_FORMAT
65 " _vs.high_boundary(): " INTPTR_FORMAT,
66 _vs.low_boundary(),
67 _vs.high_boundary());
68 }
69 }
71 void BlockOffsetSharedArray::resize(size_t new_word_size) {
72 assert(new_word_size <= _reserved.word_size(), "Resize larger than reserved");
73 size_t new_size = compute_size(new_word_size);
74 size_t old_size = _vs.committed_size();
75 size_t delta;
76 char* high = _vs.high();
77 _end = _reserved.start() + new_word_size;
78 if (new_size > old_size) {
79 delta = ReservedSpace::page_align_size_up(new_size - old_size);
80 assert(delta > 0, "just checking");
81 if (!_vs.expand_by(delta)) {
82 // Do better than this for Merlin
83 vm_exit_out_of_memory(delta, "offset table expansion");
84 }
85 assert(_vs.high() == high + delta, "invalid expansion");
86 } else {
87 delta = ReservedSpace::page_align_size_down(old_size - new_size);
88 if (delta == 0) return;
89 _vs.shrink_by(delta);
90 assert(_vs.high() == high - delta, "invalid expansion");
91 }
92 }
94 bool BlockOffsetSharedArray::is_card_boundary(HeapWord* p) const {
95 assert(p >= _reserved.start(), "just checking");
96 size_t delta = pointer_delta(p, _reserved.start());
97 return (delta & right_n_bits(LogN_words)) == (size_t)NoBits;
98 }
101 void BlockOffsetSharedArray::serialize(SerializeOopClosure* soc,
102 HeapWord* start, HeapWord* end) {
103 assert(_offset_array[0] == 0, "objects can't cross covered areas");
104 assert(start <= end, "bad address range");
105 size_t start_index = index_for(start);
106 size_t end_index = index_for(end-1)+1;
107 soc->do_region(&_offset_array[start_index],
108 (end_index - start_index) * sizeof(_offset_array[0]));
109 }
111 //////////////////////////////////////////////////////////////////////
112 // BlockOffsetArray
113 //////////////////////////////////////////////////////////////////////
115 BlockOffsetArray::BlockOffsetArray(BlockOffsetSharedArray* array,
116 MemRegion mr, bool init_to_zero_) :
117 BlockOffsetTable(mr.start(), mr.end()),
118 _array(array)
119 {
120 assert(_bottom <= _end, "arguments out of order");
121 set_init_to_zero(init_to_zero_);
122 if (!init_to_zero_) {
123 // initialize cards to point back to mr.start()
124 set_remainder_to_point_to_start(mr.start() + N_words, mr.end());
125 _array->set_offset_array(0, 0); // set first card to 0
126 }
127 }
130 // The arguments follow the normal convention of denoting
131 // a right-open interval: [start, end)
132 void
133 BlockOffsetArray::
134 set_remainder_to_point_to_start(HeapWord* start, HeapWord* end, bool reducing) {
136 check_reducing_assertion(reducing);
137 if (start >= end) {
138 // The start address is equal to the end address (or to
139 // the right of the end address) so there are not cards
140 // that need to be updated..
141 return;
142 }
144 // Write the backskip value for each region.
145 //
146 // offset
147 // card 2nd 3rd
148 // | +- 1st | |
149 // v v v v
150 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+-+-+-
151 // |x|0|0|0|0|0|0|0|1|1|1|1|1|1| ... |1|1|1|1|2|2|2|2|2|2| ...
152 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+-+-+-
153 // 11 19 75
154 // 12
155 //
156 // offset card is the card that points to the start of an object
157 // x - offset value of offset card
158 // 1st - start of first logarithmic region
159 // 0 corresponds to logarithmic value N_words + 0 and 2**(3 * 0) = 1
160 // 2nd - start of second logarithmic region
161 // 1 corresponds to logarithmic value N_words + 1 and 2**(3 * 1) = 8
162 // 3rd - start of third logarithmic region
163 // 2 corresponds to logarithmic value N_words + 2 and 2**(3 * 2) = 64
164 //
165 // integer below the block offset entry is an example of
166 // the index of the entry
167 //
168 // Given an address,
169 // Find the index for the address
170 // Find the block offset table entry
171 // Convert the entry to a back slide
172 // (e.g., with today's, offset = 0x81 =>
173 // back slip = 2**(3*(0x81 - N_words)) = 2**3) = 8
174 // Move back N (e.g., 8) entries and repeat with the
175 // value of the new entry
176 //
177 size_t start_card = _array->index_for(start);
178 size_t end_card = _array->index_for(end-1);
179 assert(start ==_array->address_for_index(start_card), "Precondition");
180 assert(end ==_array->address_for_index(end_card)+N_words, "Precondition");
181 set_remainder_to_point_to_start_incl(start_card, end_card, reducing); // closed interval
182 }
185 // Unlike the normal convention in this code, the argument here denotes
186 // a closed, inclusive interval: [start_card, end_card], cf set_remainder_to_point_to_start()
187 // above.
188 void
189 BlockOffsetArray::set_remainder_to_point_to_start_incl(size_t start_card, size_t end_card, bool reducing) {
191 check_reducing_assertion(reducing);
192 if (start_card > end_card) {
193 return;
194 }
195 assert(start_card > _array->index_for(_bottom), "Cannot be first card");
196 assert(_array->offset_array(start_card-1) <= N_words,
197 "Offset card has an unexpected value");
198 size_t start_card_for_region = start_card;
199 u_char offset = max_jubyte;
200 for (int i = 0; i < N_powers; i++) {
201 // -1 so that the the card with the actual offset is counted. Another -1
202 // so that the reach ends in this region and not at the start
203 // of the next.
204 size_t reach = start_card - 1 + (power_to_cards_back(i+1) - 1);
205 offset = N_words + i;
206 if (reach >= end_card) {
207 _array->set_offset_array(start_card_for_region, end_card, offset, reducing);
208 start_card_for_region = reach + 1;
209 break;
210 }
211 _array->set_offset_array(start_card_for_region, reach, offset, reducing);
212 start_card_for_region = reach + 1;
213 }
214 assert(start_card_for_region > end_card, "Sanity check");
215 DEBUG_ONLY(check_all_cards(start_card, end_card);)
216 }
218 // The card-interval [start_card, end_card] is a closed interval; this
219 // is an expensive check -- use with care and only under protection of
220 // suitable flag.
221 void BlockOffsetArray::check_all_cards(size_t start_card, size_t end_card) const {
223 if (end_card < start_card) {
224 return;
225 }
226 guarantee(_array->offset_array(start_card) == N_words, "Wrong value in second card");
227 u_char last_entry = N_words;
228 for (size_t c = start_card + 1; c <= end_card; c++ /* yeah! */) {
229 u_char entry = _array->offset_array(c);
230 guarantee(entry >= last_entry, "Monotonicity");
231 if (c - start_card > power_to_cards_back(1)) {
232 guarantee(entry > N_words, "Should be in logarithmic region");
233 }
234 size_t backskip = entry_to_cards_back(entry);
235 size_t landing_card = c - backskip;
236 guarantee(landing_card >= (start_card - 1), "Inv");
237 if (landing_card >= start_card) {
238 guarantee(_array->offset_array(landing_card) <= entry, "Monotonicity");
239 } else {
240 guarantee(landing_card == (start_card - 1), "Tautology");
241 // Note that N_words is the maximum offset value
242 guarantee(_array->offset_array(landing_card) <= N_words, "Offset value");
243 }
244 last_entry = entry; // remember for monotonicity test
245 }
246 }
249 void
250 BlockOffsetArray::alloc_block(HeapWord* blk_start, HeapWord* blk_end) {
251 assert(blk_start != NULL && blk_end > blk_start,
252 "phantom block");
253 single_block(blk_start, blk_end);
254 }
256 // Action_mark - update the BOT for the block [blk_start, blk_end).
257 // Current typical use is for splitting a block.
258 // Action_single - udpate the BOT for an allocation.
259 // Action_verify - BOT verification.
260 void
261 BlockOffsetArray::do_block_internal(HeapWord* blk_start,
262 HeapWord* blk_end,
263 Action action, bool reducing) {
264 assert(Universe::heap()->is_in_reserved(blk_start),
265 "reference must be into the heap");
266 assert(Universe::heap()->is_in_reserved(blk_end-1),
267 "limit must be within the heap");
268 // This is optimized to make the test fast, assuming we only rarely
269 // cross boundaries.
270 uintptr_t end_ui = (uintptr_t)(blk_end - 1);
271 uintptr_t start_ui = (uintptr_t)blk_start;
272 // Calculate the last card boundary preceding end of blk
273 intptr_t boundary_before_end = (intptr_t)end_ui;
274 clear_bits(boundary_before_end, right_n_bits(LogN));
275 if (start_ui <= (uintptr_t)boundary_before_end) {
276 // blk starts at or crosses a boundary
277 // Calculate index of card on which blk begins
278 size_t start_index = _array->index_for(blk_start);
279 // Index of card on which blk ends
280 size_t end_index = _array->index_for(blk_end - 1);
281 // Start address of card on which blk begins
282 HeapWord* boundary = _array->address_for_index(start_index);
283 assert(boundary <= blk_start, "blk should start at or after boundary");
284 if (blk_start != boundary) {
285 // blk starts strictly after boundary
286 // adjust card boundary and start_index forward to next card
287 boundary += N_words;
288 start_index++;
289 }
290 assert(start_index <= end_index, "monotonicity of index_for()");
291 assert(boundary <= (HeapWord*)boundary_before_end, "tautology");
292 switch (action) {
293 case Action_mark: {
294 if (init_to_zero()) {
295 _array->set_offset_array(start_index, boundary, blk_start, reducing);
296 break;
297 } // Else fall through to the next case
298 }
299 case Action_single: {
300 _array->set_offset_array(start_index, boundary, blk_start, reducing);
301 // We have finished marking the "offset card". We need to now
302 // mark the subsequent cards that this blk spans.
303 if (start_index < end_index) {
304 HeapWord* rem_st = _array->address_for_index(start_index) + N_words;
305 HeapWord* rem_end = _array->address_for_index(end_index) + N_words;
306 set_remainder_to_point_to_start(rem_st, rem_end, reducing);
307 }
308 break;
309 }
310 case Action_check: {
311 _array->check_offset_array(start_index, boundary, blk_start);
312 // We have finished checking the "offset card". We need to now
313 // check the subsequent cards that this blk spans.
314 check_all_cards(start_index + 1, end_index);
315 break;
316 }
317 default:
318 ShouldNotReachHere();
319 }
320 }
321 }
323 // The range [blk_start, blk_end) represents a single contiguous block
324 // of storage; modify the block offset table to represent this
325 // information; Right-open interval: [blk_start, blk_end)
326 // NOTE: this method does _not_ adjust _unallocated_block.
327 void
328 BlockOffsetArray::single_block(HeapWord* blk_start,
329 HeapWord* blk_end) {
330 do_block_internal(blk_start, blk_end, Action_single);
331 }
333 void BlockOffsetArray::verify() const {
334 // For each entry in the block offset table, verify that
335 // the entry correctly finds the start of an object at the
336 // first address covered by the block or to the left of that
337 // first address.
339 size_t next_index = 1;
340 size_t last_index = last_active_index();
342 // Use for debugging. Initialize to NULL to distinguish the
343 // first iteration through the while loop.
344 HeapWord* last_p = NULL;
345 HeapWord* last_start = NULL;
346 oop last_o = NULL;
348 while (next_index <= last_index) {
349 // Use an address past the start of the address for
350 // the entry.
351 HeapWord* p = _array->address_for_index(next_index) + 1;
352 if (p >= _end) {
353 // That's all of the allocated block table.
354 return;
355 }
356 // block_start() asserts that start <= p.
357 HeapWord* start = block_start(p);
358 // First check if the start is an allocated block and only
359 // then if it is a valid object.
360 oop o = oop(start);
361 assert(!Universe::is_fully_initialized() ||
362 _sp->is_free_block(start) ||
363 o->is_oop_or_null(), "Bad object was found");
364 next_index++;
365 last_p = p;
366 last_start = start;
367 last_o = o;
368 }
369 }
371 //////////////////////////////////////////////////////////////////////
372 // BlockOffsetArrayNonContigSpace
373 //////////////////////////////////////////////////////////////////////
375 // The block [blk_start, blk_end) has been allocated;
376 // adjust the block offset table to represent this information;
377 // NOTE: Clients of BlockOffsetArrayNonContigSpace: consider using
378 // the somewhat more lightweight split_block() or
379 // (when init_to_zero()) mark_block() wherever possible.
380 // right-open interval: [blk_start, blk_end)
381 void
382 BlockOffsetArrayNonContigSpace::alloc_block(HeapWord* blk_start,
383 HeapWord* blk_end) {
384 assert(blk_start != NULL && blk_end > blk_start,
385 "phantom block");
386 single_block(blk_start, blk_end);
387 allocated(blk_start, blk_end);
388 }
390 // Adjust BOT to show that a previously whole block has been split
391 // into two. We verify the BOT for the first part (prefix) and
392 // update the BOT for the second part (suffix).
393 // blk is the start of the block
394 // blk_size is the size of the original block
395 // left_blk_size is the size of the first part of the split
396 void BlockOffsetArrayNonContigSpace::split_block(HeapWord* blk,
397 size_t blk_size,
398 size_t left_blk_size) {
399 // Verify that the BOT shows [blk, blk + blk_size) to be one block.
400 verify_single_block(blk, blk_size);
401 // Update the BOT to indicate that [blk + left_blk_size, blk + blk_size)
402 // is one single block.
403 assert(blk_size > 0, "Should be positive");
404 assert(left_blk_size > 0, "Should be positive");
405 assert(left_blk_size < blk_size, "Not a split");
407 // Start addresses of prefix block and suffix block.
408 HeapWord* pref_addr = blk;
409 HeapWord* suff_addr = blk + left_blk_size;
410 HeapWord* end_addr = blk + blk_size;
412 // Indices for starts of prefix block and suffix block.
413 size_t pref_index = _array->index_for(pref_addr);
414 if (_array->address_for_index(pref_index) != pref_addr) {
415 // pref_addr does not begin pref_index
416 pref_index++;
417 }
419 size_t suff_index = _array->index_for(suff_addr);
420 if (_array->address_for_index(suff_index) != suff_addr) {
421 // suff_addr does not begin suff_index
422 suff_index++;
423 }
425 // Definition: A block B, denoted [B_start, B_end) __starts__
426 // a card C, denoted [C_start, C_end), where C_start and C_end
427 // are the heap addresses that card C covers, iff
428 // B_start <= C_start < B_end.
429 //
430 // We say that a card C "is started by" a block B, iff
431 // B "starts" C.
432 //
433 // Note that the cardinality of the set of cards {C}
434 // started by a block B can be 0, 1, or more.
435 //
436 // Below, pref_index and suff_index are, respectively, the
437 // first (least) card indices that the prefix and suffix of
438 // the split start; end_index is one more than the index of
439 // the last (greatest) card that blk starts.
440 size_t end_index = _array->index_for(end_addr - 1) + 1;
442 // Calculate the # cards that the prefix and suffix affect.
443 size_t num_pref_cards = suff_index - pref_index;
445 size_t num_suff_cards = end_index - suff_index;
446 // Change the cards that need changing
447 if (num_suff_cards > 0) {
448 HeapWord* boundary = _array->address_for_index(suff_index);
449 // Set the offset card for suffix block
450 _array->set_offset_array(suff_index, boundary, suff_addr, true /* reducing */);
451 // Change any further cards that need changing in the suffix
452 if (num_pref_cards > 0) {
453 if (num_pref_cards >= num_suff_cards) {
454 // Unilaterally fix all of the suffix cards: closed card
455 // index interval in args below.
456 set_remainder_to_point_to_start_incl(suff_index + 1, end_index - 1, true /* reducing */);
457 } else {
458 // Unilaterally fix the first (num_pref_cards - 1) following
459 // the "offset card" in the suffix block.
460 set_remainder_to_point_to_start_incl(suff_index + 1,
461 suff_index + num_pref_cards - 1, true /* reducing */);
462 // Fix the appropriate cards in the remainder of the
463 // suffix block -- these are the last num_pref_cards
464 // cards in each power block of the "new" range plumbed
465 // from suff_addr.
466 bool more = true;
467 uint i = 1;
468 while (more && (i < N_powers)) {
469 size_t back_by = power_to_cards_back(i);
470 size_t right_index = suff_index + back_by - 1;
471 size_t left_index = right_index - num_pref_cards + 1;
472 if (right_index >= end_index - 1) { // last iteration
473 right_index = end_index - 1;
474 more = false;
475 }
476 if (back_by > num_pref_cards) {
477 // Fill in the remainder of this "power block", if it
478 // is non-null.
479 if (left_index <= right_index) {
480 _array->set_offset_array(left_index, right_index,
481 N_words + i - 1, true /* reducing */);
482 } else {
483 more = false; // we are done
484 }
485 i++;
486 break;
487 }
488 i++;
489 }
490 while (more && (i < N_powers)) {
491 size_t back_by = power_to_cards_back(i);
492 size_t right_index = suff_index + back_by - 1;
493 size_t left_index = right_index - num_pref_cards + 1;
494 if (right_index >= end_index - 1) { // last iteration
495 right_index = end_index - 1;
496 if (left_index > right_index) {
497 break;
498 }
499 more = false;
500 }
501 assert(left_index <= right_index, "Error");
502 _array->set_offset_array(left_index, right_index, N_words + i - 1, true /* reducing */);
503 i++;
504 }
505 }
506 } // else no more cards to fix in suffix
507 } // else nothing needs to be done
508 // Verify that we did the right thing
509 verify_single_block(pref_addr, left_blk_size);
510 verify_single_block(suff_addr, blk_size - left_blk_size);
511 }
514 // Mark the BOT such that if [blk_start, blk_end) straddles a card
515 // boundary, the card following the first such boundary is marked
516 // with the appropriate offset.
517 // NOTE: this method does _not_ adjust _unallocated_block or
518 // any cards subsequent to the first one.
519 void
520 BlockOffsetArrayNonContigSpace::mark_block(HeapWord* blk_start,
521 HeapWord* blk_end, bool reducing) {
522 do_block_internal(blk_start, blk_end, Action_mark, reducing);
523 }
525 HeapWord* BlockOffsetArrayNonContigSpace::block_start_unsafe(
526 const void* addr) const {
527 assert(_array->offset_array(0) == 0, "objects can't cross covered areas");
528 assert(_bottom <= addr && addr < _end,
529 "addr must be covered by this Array");
530 // Must read this exactly once because it can be modified by parallel
531 // allocation.
532 HeapWord* ub = _unallocated_block;
533 if (BlockOffsetArrayUseUnallocatedBlock && addr >= ub) {
534 assert(ub < _end, "tautology (see above)");
535 return ub;
536 }
538 // Otherwise, find the block start using the table.
539 size_t index = _array->index_for(addr);
540 HeapWord* q = _array->address_for_index(index);
542 uint offset = _array->offset_array(index); // Extend u_char to uint.
543 while (offset >= N_words) {
544 // The excess of the offset from N_words indicates a power of Base
545 // to go back by.
546 size_t n_cards_back = entry_to_cards_back(offset);
547 q -= (N_words * n_cards_back);
548 assert(q >= _sp->bottom(),
549 err_msg("q = " PTR_FORMAT " crossed below bottom = " PTR_FORMAT,
550 q, _sp->bottom()));
551 assert(q < _sp->end(),
552 err_msg("q = " PTR_FORMAT " crossed above end = " PTR_FORMAT,
553 q, _sp->end()));
554 index -= n_cards_back;
555 offset = _array->offset_array(index);
556 }
557 assert(offset < N_words, "offset too large");
558 index--;
559 q -= offset;
560 assert(q >= _sp->bottom(),
561 err_msg("q = " PTR_FORMAT " crossed below bottom = " PTR_FORMAT,
562 q, _sp->bottom()));
563 assert(q < _sp->end(),
564 err_msg("q = " PTR_FORMAT " crossed above end = " PTR_FORMAT,
565 q, _sp->end()));
566 HeapWord* n = q;
568 while (n <= addr) {
569 debug_only(HeapWord* last = q); // for debugging
570 q = n;
571 n += _sp->block_size(n);
572 assert(n > q,
573 err_msg("Looping at n = " PTR_FORMAT " with last = " PTR_FORMAT","
574 " while querying blk_start(" PTR_FORMAT ")"
575 " on _sp = [" PTR_FORMAT "," PTR_FORMAT ")",
576 n, last, addr, _sp->bottom(), _sp->end()));
577 }
578 assert(q <= addr,
579 err_msg("wrong order for current (" INTPTR_FORMAT ")" " <= arg (" INTPTR_FORMAT ")",
580 q, addr));
581 assert(addr <= n,
582 err_msg("wrong order for arg (" INTPTR_FORMAT ") <= next (" INTPTR_FORMAT ")",
583 addr, n));
584 return q;
585 }
587 HeapWord* BlockOffsetArrayNonContigSpace::block_start_careful(
588 const void* addr) const {
589 assert(_array->offset_array(0) == 0, "objects can't cross covered areas");
591 assert(_bottom <= addr && addr < _end,
592 "addr must be covered by this Array");
593 // Must read this exactly once because it can be modified by parallel
594 // allocation.
595 HeapWord* ub = _unallocated_block;
596 if (BlockOffsetArrayUseUnallocatedBlock && addr >= ub) {
597 assert(ub < _end, "tautology (see above)");
598 return ub;
599 }
601 // Otherwise, find the block start using the table, but taking
602 // care (cf block_start_unsafe() above) not to parse any objects/blocks
603 // on the cards themsleves.
604 size_t index = _array->index_for(addr);
605 assert(_array->address_for_index(index) == addr,
606 "arg should be start of card");
608 HeapWord* q = (HeapWord*)addr;
609 uint offset;
610 do {
611 offset = _array->offset_array(index);
612 if (offset < N_words) {
613 q -= offset;
614 } else {
615 size_t n_cards_back = entry_to_cards_back(offset);
616 q -= (n_cards_back * N_words);
617 index -= n_cards_back;
618 }
619 } while (offset >= N_words);
620 assert(q <= addr, "block start should be to left of arg");
621 return q;
622 }
624 #ifndef PRODUCT
625 // Verification & debugging - ensure that the offset table reflects the fact
626 // that the block [blk_start, blk_end) or [blk, blk + size) is a
627 // single block of storage. NOTE: can't const this because of
628 // call to non-const do_block_internal() below.
629 void BlockOffsetArrayNonContigSpace::verify_single_block(
630 HeapWord* blk_start, HeapWord* blk_end) {
631 if (VerifyBlockOffsetArray) {
632 do_block_internal(blk_start, blk_end, Action_check);
633 }
634 }
636 void BlockOffsetArrayNonContigSpace::verify_single_block(
637 HeapWord* blk, size_t size) {
638 verify_single_block(blk, blk + size);
639 }
641 // Verify that the given block is before _unallocated_block
642 void BlockOffsetArrayNonContigSpace::verify_not_unallocated(
643 HeapWord* blk_start, HeapWord* blk_end) const {
644 if (BlockOffsetArrayUseUnallocatedBlock) {
645 assert(blk_start < blk_end, "Block inconsistency?");
646 assert(blk_end <= _unallocated_block, "_unallocated_block problem");
647 }
648 }
650 void BlockOffsetArrayNonContigSpace::verify_not_unallocated(
651 HeapWord* blk, size_t size) const {
652 verify_not_unallocated(blk, blk + size);
653 }
654 #endif // PRODUCT
656 size_t BlockOffsetArrayNonContigSpace::last_active_index() const {
657 if (_unallocated_block == _bottom) {
658 return 0;
659 } else {
660 return _array->index_for(_unallocated_block - 1);
661 }
662 }
664 //////////////////////////////////////////////////////////////////////
665 // BlockOffsetArrayContigSpace
666 //////////////////////////////////////////////////////////////////////
668 HeapWord* BlockOffsetArrayContigSpace::block_start_unsafe(const void* addr) const {
669 assert(_array->offset_array(0) == 0, "objects can't cross covered areas");
671 // Otherwise, find the block start using the table.
672 assert(_bottom <= addr && addr < _end,
673 "addr must be covered by this Array");
674 size_t index = _array->index_for(addr);
675 // We must make sure that the offset table entry we use is valid. If
676 // "addr" is past the end, start at the last known one and go forward.
677 index = MIN2(index, _next_offset_index-1);
678 HeapWord* q = _array->address_for_index(index);
680 uint offset = _array->offset_array(index); // Extend u_char to uint.
681 while (offset > N_words) {
682 // The excess of the offset from N_words indicates a power of Base
683 // to go back by.
684 size_t n_cards_back = entry_to_cards_back(offset);
685 q -= (N_words * n_cards_back);
686 assert(q >= _sp->bottom(), "Went below bottom!");
687 index -= n_cards_back;
688 offset = _array->offset_array(index);
689 }
690 while (offset == N_words) {
691 assert(q >= _sp->bottom(), "Went below bottom!");
692 q -= N_words;
693 index--;
694 offset = _array->offset_array(index);
695 }
696 assert(offset < N_words, "offset too large");
697 q -= offset;
698 HeapWord* n = q;
700 while (n <= addr) {
701 debug_only(HeapWord* last = q); // for debugging
702 q = n;
703 n += _sp->block_size(n);
704 }
705 assert(q <= addr, "wrong order for current and arg");
706 assert(addr <= n, "wrong order for arg and next");
707 return q;
708 }
710 //
711 // _next_offset_threshold
712 // | _next_offset_index
713 // v v
714 // +-------+-------+-------+-------+-------+
715 // | i-1 | i | i+1 | i+2 | i+3 |
716 // +-------+-------+-------+-------+-------+
717 // ( ^ ]
718 // block-start
719 //
721 void BlockOffsetArrayContigSpace::alloc_block_work(HeapWord* blk_start,
722 HeapWord* blk_end) {
723 assert(blk_start != NULL && blk_end > blk_start,
724 "phantom block");
725 assert(blk_end > _next_offset_threshold,
726 "should be past threshold");
727 assert(blk_start <= _next_offset_threshold,
728 "blk_start should be at or before threshold");
729 assert(pointer_delta(_next_offset_threshold, blk_start) <= N_words,
730 "offset should be <= BlockOffsetSharedArray::N");
731 assert(Universe::heap()->is_in_reserved(blk_start),
732 "reference must be into the heap");
733 assert(Universe::heap()->is_in_reserved(blk_end-1),
734 "limit must be within the heap");
735 assert(_next_offset_threshold ==
736 _array->_reserved.start() + _next_offset_index*N_words,
737 "index must agree with threshold");
739 debug_only(size_t orig_next_offset_index = _next_offset_index;)
741 // Mark the card that holds the offset into the block. Note
742 // that _next_offset_index and _next_offset_threshold are not
743 // updated until the end of this method.
744 _array->set_offset_array(_next_offset_index,
745 _next_offset_threshold,
746 blk_start);
748 // We need to now mark the subsequent cards that this blk spans.
750 // Index of card on which blk ends.
751 size_t end_index = _array->index_for(blk_end - 1);
753 // Are there more cards left to be updated?
754 if (_next_offset_index + 1 <= end_index) {
755 HeapWord* rem_st = _array->address_for_index(_next_offset_index + 1);
756 // Calculate rem_end this way because end_index
757 // may be the last valid index in the covered region.
758 HeapWord* rem_end = _array->address_for_index(end_index) + N_words;
759 set_remainder_to_point_to_start(rem_st, rem_end);
760 }
762 // _next_offset_index and _next_offset_threshold updated here.
763 _next_offset_index = end_index + 1;
764 // Calculate _next_offset_threshold this way because end_index
765 // may be the last valid index in the covered region.
766 _next_offset_threshold = _array->address_for_index(end_index) + N_words;
767 assert(_next_offset_threshold >= blk_end, "Incorrect offset threshold");
769 #ifdef ASSERT
770 // The offset can be 0 if the block starts on a boundary. That
771 // is checked by an assertion above.
772 size_t start_index = _array->index_for(blk_start);
773 HeapWord* boundary = _array->address_for_index(start_index);
774 assert((_array->offset_array(orig_next_offset_index) == 0 &&
775 blk_start == boundary) ||
776 (_array->offset_array(orig_next_offset_index) > 0 &&
777 _array->offset_array(orig_next_offset_index) <= N_words),
778 "offset array should have been set");
779 for (size_t j = orig_next_offset_index + 1; j <= end_index; j++) {
780 assert(_array->offset_array(j) > 0 &&
781 _array->offset_array(j) <= (u_char) (N_words+N_powers-1),
782 "offset array should have been set");
783 }
784 #endif
785 }
787 HeapWord* BlockOffsetArrayContigSpace::initialize_threshold() {
788 assert(!Universe::heap()->is_in_reserved(_array->_offset_array),
789 "just checking");
790 _next_offset_index = _array->index_for(_bottom);
791 _next_offset_index++;
792 _next_offset_threshold =
793 _array->address_for_index(_next_offset_index);
794 return _next_offset_threshold;
795 }
797 void BlockOffsetArrayContigSpace::zero_bottom_entry() {
798 assert(!Universe::heap()->is_in_reserved(_array->_offset_array),
799 "just checking");
800 size_t bottom_index = _array->index_for(_bottom);
801 _array->set_offset_array(bottom_index, 0);
802 }
805 void BlockOffsetArrayContigSpace::serialize(SerializeOopClosure* soc) {
806 if (soc->reading()) {
807 // Null these values so that the serializer won't object to updating them.
808 _next_offset_threshold = NULL;
809 _next_offset_index = 0;
810 }
811 soc->do_ptr(&_next_offset_threshold);
812 soc->do_size_t(&_next_offset_index);
813 }
815 size_t BlockOffsetArrayContigSpace::last_active_index() const {
816 size_t result = _next_offset_index - 1;
817 return result >= 0 ? result : 0;
818 }