1 /*
2  * Copyright (c) 2001, 2014, 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 #include "precompiled.hpp"
26 #include "gc_implementation/g1/g1BlockOffsetTable.inline.hpp"
27 #include "gc_implementation/g1/heapRegion.hpp"
28 #include "memory/space.hpp"
29 #include "oops/oop.inline.hpp"
30 #include "runtime/java.hpp"
31 #include "services/memTracker.hpp"
32 
33 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
34 
35 //////////////////////////////////////////////////////////////////////
36 // G1BlockOffsetSharedArray
37 //////////////////////////////////////////////////////////////////////
38 
G1BlockOffsetSharedArray(MemRegion heap,G1RegionToSpaceMapper * storage)39 G1BlockOffsetSharedArray::G1BlockOffsetSharedArray(MemRegion heap, G1RegionToSpaceMapper* storage) :
40   _reserved(), _end(NULL), _listener(), _offset_array(NULL) {
41 
42   _reserved = heap;
43   _end = NULL;
44 
45   MemRegion bot_reserved = storage->reserved();
46 
47   _offset_array = (u_char*)bot_reserved.start();
48   _end = _reserved.end();
49 
50   storage->set_mapping_changed_listener(&_listener);
51 
52   if (TraceBlockOffsetTable) {
53     gclog_or_tty->print_cr("G1BlockOffsetSharedArray::G1BlockOffsetSharedArray: ");
54     gclog_or_tty->print_cr("  "
55                   "  rs.base(): " INTPTR_FORMAT
56                   "  rs.size(): " INTPTR_FORMAT
57                   "  rs end(): " INTPTR_FORMAT,
58                   bot_reserved.start(), bot_reserved.byte_size(), bot_reserved.end());
59   }
60 }
61 
is_card_boundary(HeapWord * p) const62 bool G1BlockOffsetSharedArray::is_card_boundary(HeapWord* p) const {
63   assert(p >= _reserved.start(), "just checking");
64   size_t delta = pointer_delta(p, _reserved.start());
65   return (delta & right_n_bits(LogN_words)) == (size_t)NoBits;
66 }
67 
68 //////////////////////////////////////////////////////////////////////
69 // G1BlockOffsetArray
70 //////////////////////////////////////////////////////////////////////
71 
G1BlockOffsetArray(G1BlockOffsetSharedArray * array,MemRegion mr)72 G1BlockOffsetArray::G1BlockOffsetArray(G1BlockOffsetSharedArray* array,
73                                        MemRegion mr) :
74   G1BlockOffsetTable(mr.start(), mr.end()),
75   _unallocated_block(_bottom),
76   _array(array), _gsp(NULL) {
77   assert(_bottom <= _end, "arguments out of order");
78 }
79 
set_space(G1OffsetTableContigSpace * sp)80 void G1BlockOffsetArray::set_space(G1OffsetTableContigSpace* sp) {
81   _gsp = sp;
82 }
83 
84 // The arguments follow the normal convention of denoting
85 // a right-open interval: [start, end)
86 void
set_remainder_to_point_to_start(HeapWord * start,HeapWord * end)87 G1BlockOffsetArray:: set_remainder_to_point_to_start(HeapWord* start, HeapWord* end) {
88 
89   if (start >= end) {
90     // The start address is equal to the end address (or to
91     // the right of the end address) so there are not cards
92     // that need to be updated..
93     return;
94   }
95 
96   // Write the backskip value for each region.
97   //
98   //    offset
99   //    card             2nd                       3rd
100   //     | +- 1st        |                         |
101   //     v v             v                         v
102   //    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+     +-+-+-+-+-+-+-+-+-+-+-
103   //    |x|0|0|0|0|0|0|0|1|1|1|1|1|1| ... |1|1|1|1|2|2|2|2|2|2| ...
104   //    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+     +-+-+-+-+-+-+-+-+-+-+-
105   //    11              19                        75
106   //      12
107   //
108   //    offset card is the card that points to the start of an object
109   //      x - offset value of offset card
110   //    1st - start of first logarithmic region
111   //      0 corresponds to logarithmic value N_words + 0 and 2**(3 * 0) = 1
112   //    2nd - start of second logarithmic region
113   //      1 corresponds to logarithmic value N_words + 1 and 2**(3 * 1) = 8
114   //    3rd - start of third logarithmic region
115   //      2 corresponds to logarithmic value N_words + 2 and 2**(3 * 2) = 64
116   //
117   //    integer below the block offset entry is an example of
118   //    the index of the entry
119   //
120   //    Given an address,
121   //      Find the index for the address
122   //      Find the block offset table entry
123   //      Convert the entry to a back slide
124   //        (e.g., with today's, offset = 0x81 =>
125   //          back slip = 2**(3*(0x81 - N_words)) = 2**3) = 8
126   //      Move back N (e.g., 8) entries and repeat with the
127   //        value of the new entry
128   //
129   size_t start_card = _array->index_for(start);
130   size_t end_card = _array->index_for(end-1);
131   assert(start ==_array->address_for_index(start_card), "Precondition");
132   assert(end ==_array->address_for_index(end_card)+N_words, "Precondition");
133   set_remainder_to_point_to_start_incl(start_card, end_card); // closed interval
134 }
135 
136 // Unlike the normal convention in this code, the argument here denotes
137 // a closed, inclusive interval: [start_card, end_card], cf set_remainder_to_point_to_start()
138 // above.
139 void
set_remainder_to_point_to_start_incl(size_t start_card,size_t end_card)140 G1BlockOffsetArray::set_remainder_to_point_to_start_incl(size_t start_card, size_t end_card) {
141   if (start_card > end_card) {
142     return;
143   }
144   assert(start_card > _array->index_for(_bottom), "Cannot be first card");
145   assert(_array->offset_array(start_card-1) <= N_words,
146          "Offset card has an unexpected value");
147   size_t start_card_for_region = start_card;
148   u_char offset = max_jubyte;
149   for (int i = 0; i < BlockOffsetArray::N_powers; i++) {
150     // -1 so that the the card with the actual offset is counted.  Another -1
151     // so that the reach ends in this region and not at the start
152     // of the next.
153     size_t reach = start_card - 1 + (BlockOffsetArray::power_to_cards_back(i+1) - 1);
154     offset = N_words + i;
155     if (reach >= end_card) {
156       _array->set_offset_array(start_card_for_region, end_card, offset);
157       start_card_for_region = reach + 1;
158       break;
159     }
160     _array->set_offset_array(start_card_for_region, reach, offset);
161     start_card_for_region = reach + 1;
162   }
163   assert(start_card_for_region > end_card, "Sanity check");
164   DEBUG_ONLY(check_all_cards(start_card, end_card);)
165 }
166 
167 // The card-interval [start_card, end_card] is a closed interval; this
168 // is an expensive check -- use with care and only under protection of
169 // suitable flag.
check_all_cards(size_t start_card,size_t end_card) const170 void G1BlockOffsetArray::check_all_cards(size_t start_card, size_t end_card) const {
171 
172   if (end_card < start_card) {
173     return;
174   }
175   guarantee(_array->offset_array(start_card) == N_words, "Wrong value in second card");
176   for (size_t c = start_card + 1; c <= end_card; c++ /* yeah! */) {
177     u_char entry = _array->offset_array(c);
178     if (c - start_card > BlockOffsetArray::power_to_cards_back(1)) {
179       guarantee(entry > N_words,
180                 err_msg("Should be in logarithmic region - "
181                         "entry: " UINT32_FORMAT ", "
182                         "_array->offset_array(c): " UINT32_FORMAT ", "
183                         "N_words: " UINT32_FORMAT,
184                         entry, _array->offset_array(c), N_words));
185     }
186     size_t backskip = BlockOffsetArray::entry_to_cards_back(entry);
187     size_t landing_card = c - backskip;
188     guarantee(landing_card >= (start_card - 1), "Inv");
189     if (landing_card >= start_card) {
190       guarantee(_array->offset_array(landing_card) <= entry,
191                 err_msg("Monotonicity - landing_card offset: " UINT32_FORMAT ", "
192                         "entry: " UINT32_FORMAT,
193                         _array->offset_array(landing_card), entry));
194     } else {
195       guarantee(landing_card == start_card - 1, "Tautology");
196       // Note that N_words is the maximum offset value
197       guarantee(_array->offset_array(landing_card) <= N_words,
198                 err_msg("landing card offset: " UINT32_FORMAT ", "
199                         "N_words: " UINT32_FORMAT,
200                         _array->offset_array(landing_card), N_words));
201     }
202   }
203 }
204 
block_start_unsafe(const void * addr)205 HeapWord* G1BlockOffsetArray::block_start_unsafe(const void* addr) {
206   assert(_bottom <= addr && addr < _end,
207          "addr must be covered by this Array");
208   // Must read this exactly once because it can be modified by parallel
209   // allocation.
210   HeapWord* ub = _unallocated_block;
211   if (BlockOffsetArrayUseUnallocatedBlock && addr >= ub) {
212     assert(ub < _end, "tautology (see above)");
213     return ub;
214   }
215   // Otherwise, find the block start using the table.
216   HeapWord* q = block_at_or_preceding(addr, false, 0);
217   return forward_to_block_containing_addr(q, addr);
218 }
219 
220 // This duplicates a little code from the above: unavoidable.
221 HeapWord*
block_start_unsafe_const(const void * addr) const222 G1BlockOffsetArray::block_start_unsafe_const(const void* addr) const {
223   assert(_bottom <= addr && addr < _end,
224          "addr must be covered by this Array");
225   // Must read this exactly once because it can be modified by parallel
226   // allocation.
227   HeapWord* ub = _unallocated_block;
228   if (BlockOffsetArrayUseUnallocatedBlock && addr >= ub) {
229     assert(ub < _end, "tautology (see above)");
230     return ub;
231   }
232   // Otherwise, find the block start using the table.
233   HeapWord* q = block_at_or_preceding(addr, false, 0);
234   HeapWord* n = q + block_size(q);
235   return forward_to_block_containing_addr_const(q, n, addr);
236 }
237 
238 
239 HeapWord*
forward_to_block_containing_addr_slow(HeapWord * q,HeapWord * n,const void * addr)240 G1BlockOffsetArray::forward_to_block_containing_addr_slow(HeapWord* q,
241                                                           HeapWord* n,
242                                                           const void* addr) {
243   // We're not in the normal case.  We need to handle an important subcase
244   // here: LAB allocation.  An allocation previously recorded in the
245   // offset table was actually a lab allocation, and was divided into
246   // several objects subsequently.  Fix this situation as we answer the
247   // query, by updating entries as we cross them.
248 
249   // If the fist object's end q is at the card boundary. Start refining
250   // with the corresponding card (the value of the entry will be basically
251   // set to 0). If the object crosses the boundary -- start from the next card.
252   size_t n_index = _array->index_for(n);
253   size_t next_index = _array->index_for(n) + !_array->is_card_boundary(n);
254   // Calculate a consistent next boundary.  If "n" is not at the boundary
255   // already, step to the boundary.
256   HeapWord* next_boundary = _array->address_for_index(n_index) +
257                             (n_index == next_index ? 0 : N_words);
258   assert(next_boundary <= _array->_end,
259          err_msg("next_boundary is beyond the end of the covered region "
260                  " next_boundary " PTR_FORMAT " _array->_end " PTR_FORMAT,
261                  next_boundary, _array->_end));
262   if (addr >= gsp()->top()) return gsp()->top();
263   while (next_boundary < addr) {
264     while (n <= next_boundary) {
265       q = n;
266       oop obj = oop(q);
267       if (obj->klass_or_null_acquire() == NULL) return q;
268       n += block_size(q);
269     }
270     assert(q <= next_boundary && n > next_boundary, "Consequence of loop");
271     // [q, n) is the block that crosses the boundary.
272     alloc_block_work2(&next_boundary, &next_index, q, n);
273   }
274   return forward_to_block_containing_addr_const(q, n, addr);
275 }
276 
277 // Note that the committed size of the covered space may have changed,
278 // so the table size might also wish to change.
resize(size_t new_word_size)279 void G1BlockOffsetArray::resize(size_t new_word_size) {
280   HeapWord* new_end = _bottom + new_word_size;
281   _end = new_end;  // update _end
282 }
283 
284 //
285 //              threshold_
286 //              |   _index_
287 //              v   v
288 //      +-------+-------+-------+-------+-------+
289 //      | i-1   |   i   | i+1   | i+2   | i+3   |
290 //      +-------+-------+-------+-------+-------+
291 //       ( ^    ]
292 //         block-start
293 //
alloc_block_work2(HeapWord ** threshold_,size_t * index_,HeapWord * blk_start,HeapWord * blk_end)294 void G1BlockOffsetArray::alloc_block_work2(HeapWord** threshold_, size_t* index_,
295                                            HeapWord* blk_start, HeapWord* blk_end) {
296   // For efficiency, do copy-in/copy-out.
297   HeapWord* threshold = *threshold_;
298   size_t    index = *index_;
299 
300   assert(blk_start != NULL && blk_end > blk_start,
301          "phantom block");
302   assert(blk_end > threshold, "should be past threshold");
303   assert(blk_start <= threshold, "blk_start should be at or before threshold");
304   assert(pointer_delta(threshold, blk_start) <= N_words,
305          "offset should be <= BlockOffsetSharedArray::N");
306   assert(Universe::heap()->is_in_reserved(blk_start),
307          "reference must be into the heap");
308   assert(Universe::heap()->is_in_reserved(blk_end-1),
309          "limit must be within the heap");
310   assert(threshold == _array->_reserved.start() + index*N_words,
311          "index must agree with threshold");
312 
313   DEBUG_ONLY(size_t orig_index = index;)
314 
315   // Mark the card that holds the offset into the block.  Note
316   // that _next_offset_index and _next_offset_threshold are not
317   // updated until the end of this method.
318   _array->set_offset_array(index, threshold, blk_start);
319 
320   // We need to now mark the subsequent cards that this blk spans.
321 
322   // Index of card on which blk ends.
323   size_t end_index   = _array->index_for(blk_end - 1);
324 
325   // Are there more cards left to be updated?
326   if (index + 1 <= end_index) {
327     HeapWord* rem_st  = _array->address_for_index(index + 1);
328     // Calculate rem_end this way because end_index
329     // may be the last valid index in the covered region.
330     HeapWord* rem_end = _array->address_for_index(end_index) +  N_words;
331     set_remainder_to_point_to_start(rem_st, rem_end);
332   }
333 
334   index = end_index + 1;
335   // Calculate threshold_ this way because end_index
336   // may be the last valid index in the covered region.
337   threshold = _array->address_for_index(end_index) + N_words;
338   assert(threshold >= blk_end, "Incorrect offset threshold");
339 
340   // index_ and threshold_ updated here.
341   *threshold_ = threshold;
342   *index_ = index;
343 
344 #ifdef ASSERT
345   // The offset can be 0 if the block starts on a boundary.  That
346   // is checked by an assertion above.
347   size_t start_index = _array->index_for(blk_start);
348   HeapWord* boundary = _array->address_for_index(start_index);
349   assert((_array->offset_array(orig_index) == 0 &&
350           blk_start == boundary) ||
351           (_array->offset_array(orig_index) > 0 &&
352          _array->offset_array(orig_index) <= N_words),
353          err_msg("offset array should have been set - "
354                   "orig_index offset: " UINT32_FORMAT ", "
355                   "blk_start: " PTR_FORMAT ", "
356                   "boundary: " PTR_FORMAT,
357                   _array->offset_array(orig_index),
358                   blk_start, boundary));
359   for (size_t j = orig_index + 1; j <= end_index; j++) {
360     assert(_array->offset_array(j) > 0 &&
361            _array->offset_array(j) <=
362              (u_char) (N_words+BlockOffsetArray::N_powers-1),
363            err_msg("offset array should have been set - "
364                    UINT32_FORMAT " not > 0 OR "
365                    UINT32_FORMAT " not <= " UINT32_FORMAT,
366                    _array->offset_array(j),
367                    _array->offset_array(j),
368                    (u_char) (N_words+BlockOffsetArray::N_powers-1)));
369   }
370 #endif
371 }
372 
373 bool
verify_for_object(HeapWord * obj_start,size_t word_size) const374 G1BlockOffsetArray::verify_for_object(HeapWord* obj_start,
375                                       size_t word_size) const {
376   size_t first_card = _array->index_for(obj_start);
377   size_t last_card = _array->index_for(obj_start + word_size - 1);
378   if (!_array->is_card_boundary(obj_start)) {
379     // If the object is not on a card boundary the BOT entry of the
380     // first card should point to another object so we should not
381     // check that one.
382     first_card += 1;
383   }
384   for (size_t card = first_card; card <= last_card; card += 1) {
385     HeapWord* card_addr = _array->address_for_index(card);
386     HeapWord* block_start = block_start_const(card_addr);
387     if (block_start != obj_start) {
388       gclog_or_tty->print_cr("block start: " PTR_FORMAT " is incorrect - "
389                              "card index: " SIZE_FORMAT " "
390                              "card addr: " PTR_FORMAT " BOT entry: %u "
391                              "obj: " PTR_FORMAT " word size: " SIZE_FORMAT " "
392                              "cards: [" SIZE_FORMAT "," SIZE_FORMAT "]",
393                              block_start, card, card_addr,
394                              _array->offset_array(card),
395                              obj_start, word_size, first_card, last_card);
396       return false;
397     }
398   }
399   return true;
400 }
401 
402 #ifndef PRODUCT
403 void
print_on(outputStream * out)404 G1BlockOffsetArray::print_on(outputStream* out) {
405   size_t from_index = _array->index_for(_bottom);
406   size_t to_index = _array->index_for(_end);
407   out->print_cr(">> BOT for area [" PTR_FORMAT "," PTR_FORMAT ") "
408                 "cards [" SIZE_FORMAT "," SIZE_FORMAT ")",
409                 _bottom, _end, from_index, to_index);
410   for (size_t i = from_index; i < to_index; ++i) {
411     out->print_cr("  entry " SIZE_FORMAT_W(8) " | " PTR_FORMAT " : %3u",
412                   i, _array->address_for_index(i),
413                   (uint) _array->offset_array(i));
414   }
415 }
416 #endif // !PRODUCT
417 
418 //////////////////////////////////////////////////////////////////////
419 // G1BlockOffsetArrayContigSpace
420 //////////////////////////////////////////////////////////////////////
421 
422 HeapWord*
block_start_unsafe(const void * addr)423 G1BlockOffsetArrayContigSpace::block_start_unsafe(const void* addr) {
424   assert(_bottom <= addr && addr < _end,
425          "addr must be covered by this Array");
426   HeapWord* q = block_at_or_preceding(addr, true, _next_offset_index-1);
427   return forward_to_block_containing_addr(q, addr);
428 }
429 
430 HeapWord*
431 G1BlockOffsetArrayContigSpace::
block_start_unsafe_const(const void * addr) const432 block_start_unsafe_const(const void* addr) const {
433   assert(_bottom <= addr && addr < _end,
434          "addr must be covered by this Array");
435   HeapWord* q = block_at_or_preceding(addr, true, _next_offset_index-1);
436   HeapWord* n = q + block_size(q);
437   return forward_to_block_containing_addr_const(q, n, addr);
438 }
439 
440 G1BlockOffsetArrayContigSpace::
G1BlockOffsetArrayContigSpace(G1BlockOffsetSharedArray * array,MemRegion mr)441 G1BlockOffsetArrayContigSpace(G1BlockOffsetSharedArray* array,
442                               MemRegion mr) :
443   G1BlockOffsetArray(array, mr)
444 {
445   _next_offset_threshold = NULL;
446   _next_offset_index = 0;
447 }
448 
initialize_threshold_raw()449 HeapWord* G1BlockOffsetArrayContigSpace::initialize_threshold_raw() {
450   _next_offset_index = _array->index_for_raw(_bottom);
451   _next_offset_index++;
452   _next_offset_threshold =
453     _array->address_for_index_raw(_next_offset_index);
454   return _next_offset_threshold;
455 }
456 
zero_bottom_entry_raw()457 void G1BlockOffsetArrayContigSpace::zero_bottom_entry_raw() {
458   size_t bottom_index = _array->index_for_raw(_bottom);
459   assert(_array->address_for_index_raw(bottom_index) == _bottom,
460          "Precondition of call");
461   _array->set_offset_array_raw(bottom_index, 0);
462 }
463 
initialize_threshold()464 HeapWord* G1BlockOffsetArrayContigSpace::initialize_threshold() {
465   _next_offset_index = _array->index_for(_bottom);
466   _next_offset_index++;
467   _next_offset_threshold =
468     _array->address_for_index(_next_offset_index);
469   return _next_offset_threshold;
470 }
471 
472 void
set_for_starts_humongous(HeapWord * new_top)473 G1BlockOffsetArrayContigSpace::set_for_starts_humongous(HeapWord* new_top) {
474   assert(new_top <= _end, "_end should have already been updated");
475 
476   // The first BOT entry should have offset 0.
477   reset_bot();
478   alloc_block(_bottom, new_top);
479  }
480 
481 #ifndef PRODUCT
482 void
print_on(outputStream * out)483 G1BlockOffsetArrayContigSpace::print_on(outputStream* out) {
484   G1BlockOffsetArray::print_on(out);
485   out->print_cr("  next offset threshold: " PTR_FORMAT, _next_offset_threshold);
486   out->print_cr("  next offset index:     " SIZE_FORMAT, _next_offset_index);
487 }
488 #endif // !PRODUCT
489