src/share/vm/memory/blockOffsetTable.hpp

Thu, 27 May 2010 19:08:38 -0700

author
trims
date
Thu, 27 May 2010 19:08:38 -0700
changeset 1907
c18cbe5936b8
parent 1873
3bfae429e2cf
child 2071
be3f9c242c9d
permissions
-rw-r--r--

6941466: Oracle rebranding changes for Hotspot repositories
Summary: Change all the Sun copyrights to Oracle copyright
Reviewed-by: ohair

     1 /*
     2  * Copyright (c) 2000, 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  */
    25 // The CollectedHeap type requires subtypes to implement a method
    26 // "block_start".  For some subtypes, notably generational
    27 // systems using card-table-based write barriers, the efficiency of this
    28 // operation may be important.  Implementations of the "BlockOffsetArray"
    29 // class may be useful in providing such efficient implementations.
    30 //
    31 // BlockOffsetTable (abstract)
    32 //   - BlockOffsetArray (abstract)
    33 //     - BlockOffsetArrayNonContigSpace
    34 //     - BlockOffsetArrayContigSpace
    35 //
    37 class ContiguousSpace;
    38 class SerializeOopClosure;
    40 //////////////////////////////////////////////////////////////////////////
    41 // The BlockOffsetTable "interface"
    42 //////////////////////////////////////////////////////////////////////////
    43 class BlockOffsetTable VALUE_OBJ_CLASS_SPEC {
    44   friend class VMStructs;
    45 protected:
    46   // These members describe the region covered by the table.
    48   // The space this table is covering.
    49   HeapWord* _bottom;    // == reserved.start
    50   HeapWord* _end;       // End of currently allocated region.
    52 public:
    53   // Initialize the table to cover the given space.
    54   // The contents of the initial table are undefined.
    55   BlockOffsetTable(HeapWord* bottom, HeapWord* end):
    56     _bottom(bottom), _end(end) {
    57     assert(_bottom <= _end, "arguments out of order");
    58   }
    60   // Note that the committed size of the covered space may have changed,
    61   // so the table size might also wish to change.
    62   virtual void resize(size_t new_word_size) = 0;
    64   virtual void set_bottom(HeapWord* new_bottom) {
    65     assert(new_bottom <= _end, "new_bottom > _end");
    66     _bottom = new_bottom;
    67     resize(pointer_delta(_end, _bottom));
    68   }
    70   // Requires "addr" to be contained by a block, and returns the address of
    71   // the start of that block.
    72   virtual HeapWord* block_start_unsafe(const void* addr) const = 0;
    74   // Returns the address of the start of the block containing "addr", or
    75   // else "null" if it is covered by no block.
    76   HeapWord* block_start(const void* addr) const;
    77 };
    79 //////////////////////////////////////////////////////////////////////////
    80 // One implementation of "BlockOffsetTable," the BlockOffsetArray,
    81 // divides the covered region into "N"-word subregions (where
    82 // "N" = 2^"LogN".  An array with an entry for each such subregion
    83 // indicates how far back one must go to find the start of the
    84 // chunk that includes the first word of the subregion.
    85 //
    86 // Each BlockOffsetArray is owned by a Space.  However, the actual array
    87 // may be shared by several BlockOffsetArrays; this is useful
    88 // when a single resizable area (such as a generation) is divided up into
    89 // several spaces in which contiguous allocation takes place.  (Consider,
    90 // for example, the garbage-first generation.)
    92 // Here is the shared array type.
    93 //////////////////////////////////////////////////////////////////////////
    94 // BlockOffsetSharedArray
    95 //////////////////////////////////////////////////////////////////////////
    96 class BlockOffsetSharedArray: public CHeapObj {
    97   friend class BlockOffsetArray;
    98   friend class BlockOffsetArrayNonContigSpace;
    99   friend class BlockOffsetArrayContigSpace;
   100   friend class VMStructs;
   102  private:
   103   enum SomePrivateConstants {
   104     LogN = 9,
   105     LogN_words = LogN - LogHeapWordSize,
   106     N_bytes = 1 << LogN,
   107     N_words = 1 << LogN_words
   108   };
   110   // The reserved region covered by the shared array.
   111   MemRegion _reserved;
   113   // End of the current committed region.
   114   HeapWord* _end;
   116   // Array for keeping offsets for retrieving object start fast given an
   117   // address.
   118   VirtualSpace _vs;
   119   u_char* _offset_array;          // byte array keeping backwards offsets
   121  protected:
   122   // Bounds checking accessors:
   123   // For performance these have to devolve to array accesses in product builds.
   124   u_char offset_array(size_t index) const {
   125     assert(index < _vs.committed_size(), "index out of range");
   126     return _offset_array[index];
   127   }
   128   void set_offset_array(size_t index, u_char offset) {
   129     assert(index < _vs.committed_size(), "index out of range");
   130     _offset_array[index] = offset;
   131   }
   132   void set_offset_array(size_t index, HeapWord* high, HeapWord* low) {
   133     assert(index < _vs.committed_size(), "index out of range");
   134     assert(high >= low, "addresses out of order");
   135     assert(pointer_delta(high, low) <= N_words, "offset too large");
   136     _offset_array[index] = (u_char)pointer_delta(high, low);
   137   }
   138   void set_offset_array(HeapWord* left, HeapWord* right, u_char offset) {
   139     assert(index_for(right - 1) < _vs.committed_size(),
   140            "right address out of range");
   141     assert(left  < right, "Heap addresses out of order");
   142     size_t num_cards = pointer_delta(right, left) >> LogN_words;
   144     // Below, we may use an explicit loop instead of memset()
   145     // because on certain platforms memset() can give concurrent
   146     // readers "out-of-thin-air," phantom zeros; see 6948537.
   147     if (UseMemSetInBOT) {
   148       memset(&_offset_array[index_for(left)], offset, num_cards);
   149     } else {
   150       size_t i = index_for(left);
   151       const size_t end = i + num_cards;
   152       for (; i < end; i++) {
   153         _offset_array[i] = offset;
   154       }
   155     }
   156   }
   158   void set_offset_array(size_t left, size_t right, u_char offset) {
   159     assert(right < _vs.committed_size(), "right address out of range");
   160     assert(left  <= right, "indexes out of order");
   161     size_t num_cards = right - left + 1;
   163     // Below, we may use an explicit loop instead of memset
   164     // because on certain platforms memset() can give concurrent
   165     // readers "out-of-thin-air," phantom zeros; see 6948537.
   166     if (UseMemSetInBOT) {
   167       memset(&_offset_array[left], offset, num_cards);
   168     } else {
   169       size_t i = left;
   170       const size_t end = i + num_cards;
   171       for (; i < end; i++) {
   172         _offset_array[i] = offset;
   173       }
   174     }
   175   }
   177   void check_offset_array(size_t index, HeapWord* high, HeapWord* low) const {
   178     assert(index < _vs.committed_size(), "index out of range");
   179     assert(high >= low, "addresses out of order");
   180     assert(pointer_delta(high, low) <= N_words, "offset too large");
   181     assert(_offset_array[index] == pointer_delta(high, low),
   182            "Wrong offset");
   183   }
   185   bool is_card_boundary(HeapWord* p) const;
   187   // Return the number of slots needed for an offset array
   188   // that covers mem_region_words words.
   189   // We always add an extra slot because if an object
   190   // ends on a card boundary we put a 0 in the next
   191   // offset array slot, so we want that slot always
   192   // to be reserved.
   194   size_t compute_size(size_t mem_region_words) {
   195     size_t number_of_slots = (mem_region_words / N_words) + 1;
   196     return ReservedSpace::allocation_align_size_up(number_of_slots);
   197   }
   199 public:
   200   // Initialize the table to cover from "base" to (at least)
   201   // "base + init_word_size".  In the future, the table may be expanded
   202   // (see "resize" below) up to the size of "_reserved" (which must be at
   203   // least "init_word_size".)  The contents of the initial table are
   204   // undefined; it is the responsibility of the constituent
   205   // BlockOffsetTable(s) to initialize cards.
   206   BlockOffsetSharedArray(MemRegion reserved, size_t init_word_size);
   208   // Notes a change in the committed size of the region covered by the
   209   // table.  The "new_word_size" may not be larger than the size of the
   210   // reserved region this table covers.
   211   void resize(size_t new_word_size);
   213   void set_bottom(HeapWord* new_bottom);
   215   // Updates all the BlockOffsetArray's sharing this shared array to
   216   // reflect the current "top"'s of their spaces.
   217   void update_offset_arrays();   // Not yet implemented!
   219   // Return the appropriate index into "_offset_array" for "p".
   220   size_t index_for(const void* p) const;
   222   // Return the address indicating the start of the region corresponding to
   223   // "index" in "_offset_array".
   224   HeapWord* address_for_index(size_t index) const;
   226   // Return the address "p" incremented by the size of
   227   // a region.  This method does not align the address
   228   // returned to the start of a region.  It is a simple
   229   // primitive.
   230   HeapWord* inc_by_region_size(HeapWord* p) const { return p + N_words; }
   232   // Shared space support
   233   void serialize(SerializeOopClosure* soc, HeapWord* start, HeapWord* end);
   234 };
   236 //////////////////////////////////////////////////////////////////////////
   237 // The BlockOffsetArray whose subtypes use the BlockOffsetSharedArray.
   238 //////////////////////////////////////////////////////////////////////////
   239 class BlockOffsetArray: public BlockOffsetTable {
   240   friend class VMStructs;
   241   friend class G1BlockOffsetArray; // temp. until we restructure and cleanup
   242  protected:
   243   // The following enums are used by do_block_internal() below
   244   enum Action {
   245     Action_single,      // BOT records a single block (see single_block())
   246     Action_mark,        // BOT marks the start of a block (see mark_block())
   247     Action_check        // Check that BOT records block correctly
   248                         // (see verify_single_block()).
   249   };
   251   enum SomePrivateConstants {
   252     N_words = BlockOffsetSharedArray::N_words,
   253     LogN    = BlockOffsetSharedArray::LogN,
   254     // entries "e" of at least N_words mean "go back by Base^(e-N_words)."
   255     // All entries are less than "N_words + N_powers".
   256     LogBase = 4,
   257     Base = (1 << LogBase),
   258     N_powers = 14
   259   };
   261   static size_t power_to_cards_back(uint i) {
   262     return (size_t)(1 << (LogBase * i));
   263   }
   264   static size_t power_to_words_back(uint i) {
   265     return power_to_cards_back(i) * N_words;
   266   }
   267   static size_t entry_to_cards_back(u_char entry) {
   268     assert(entry >= N_words, "Precondition");
   269     return power_to_cards_back(entry - N_words);
   270   }
   271   static size_t entry_to_words_back(u_char entry) {
   272     assert(entry >= N_words, "Precondition");
   273     return power_to_words_back(entry - N_words);
   274   }
   276   // The shared array, which is shared with other BlockOffsetArray's
   277   // corresponding to different spaces within a generation or span of
   278   // memory.
   279   BlockOffsetSharedArray* _array;
   281   // The space that owns this subregion.
   282   Space* _sp;
   284   // If true, array entries are initialized to 0; otherwise, they are
   285   // initialized to point backwards to the beginning of the covered region.
   286   bool _init_to_zero;
   288   // Sets the entries
   289   // corresponding to the cards starting at "start" and ending at "end"
   290   // to point back to the card before "start": the interval [start, end)
   291   // is right-open.
   292   void set_remainder_to_point_to_start(HeapWord* start, HeapWord* end);
   293   // Same as above, except that the args here are a card _index_ interval
   294   // that is closed: [start_index, end_index]
   295   void set_remainder_to_point_to_start_incl(size_t start, size_t end);
   297   // A helper function for BOT adjustment/verification work
   298   void do_block_internal(HeapWord* blk_start, HeapWord* blk_end, Action action);
   300  public:
   301   // The space may not have its bottom and top set yet, which is why the
   302   // region is passed as a parameter.  If "init_to_zero" is true, the
   303   // elements of the array are initialized to zero.  Otherwise, they are
   304   // initialized to point backwards to the beginning.
   305   BlockOffsetArray(BlockOffsetSharedArray* array, MemRegion mr,
   306                    bool init_to_zero);
   308   // Note: this ought to be part of the constructor, but that would require
   309   // "this" to be passed as a parameter to a member constructor for
   310   // the containing concrete subtype of Space.
   311   // This would be legal C++, but MS VC++ doesn't allow it.
   312   void set_space(Space* sp) { _sp = sp; }
   314   // Resets the covered region to the given "mr".
   315   void set_region(MemRegion mr) {
   316     _bottom = mr.start();
   317     _end = mr.end();
   318   }
   320   // Note that the committed size of the covered space may have changed,
   321   // so the table size might also wish to change.
   322   virtual void resize(size_t new_word_size) {
   323     HeapWord* new_end = _bottom + new_word_size;
   324     if (_end < new_end && !init_to_zero()) {
   325       // verify that the old and new boundaries are also card boundaries
   326       assert(_array->is_card_boundary(_end),
   327              "_end not a card boundary");
   328       assert(_array->is_card_boundary(new_end),
   329              "new _end would not be a card boundary");
   330       // set all the newly added cards
   331       _array->set_offset_array(_end, new_end, N_words);
   332     }
   333     _end = new_end;  // update _end
   334   }
   336   // Adjust the BOT to show that it has a single block in the
   337   // range [blk_start, blk_start + size). All necessary BOT
   338   // cards are adjusted, but _unallocated_block isn't.
   339   void single_block(HeapWord* blk_start, HeapWord* blk_end);
   340   void single_block(HeapWord* blk, size_t size) {
   341     single_block(blk, blk + size);
   342   }
   344   // When the alloc_block() call returns, the block offset table should
   345   // have enough information such that any subsequent block_start() call
   346   // with an argument equal to an address that is within the range
   347   // [blk_start, blk_end) would return the value blk_start, provided
   348   // there have been no calls in between that reset this information
   349   // (e.g. see BlockOffsetArrayNonContigSpace::single_block() call
   350   // for an appropriate range covering the said interval).
   351   // These methods expect to be called with [blk_start, blk_end)
   352   // representing a block of memory in the heap.
   353   virtual void alloc_block(HeapWord* blk_start, HeapWord* blk_end);
   354   void alloc_block(HeapWord* blk, size_t size) {
   355     alloc_block(blk, blk + size);
   356   }
   358   // If true, initialize array slots with no allocated blocks to zero.
   359   // Otherwise, make them point back to the front.
   360   bool init_to_zero() { return _init_to_zero; }
   362   // Debugging
   363   // Return the index of the last entry in the "active" region.
   364   virtual size_t last_active_index() const = 0;
   365   // Verify the block offset table
   366   void verify() const;
   367   void check_all_cards(size_t left_card, size_t right_card) const;
   368 };
   370 ////////////////////////////////////////////////////////////////////////////
   371 // A subtype of BlockOffsetArray that takes advantage of the fact
   372 // that its underlying space is a NonContiguousSpace, so that some
   373 // specialized interfaces can be made available for spaces that
   374 // manipulate the table.
   375 ////////////////////////////////////////////////////////////////////////////
   376 class BlockOffsetArrayNonContigSpace: public BlockOffsetArray {
   377   friend class VMStructs;
   378  private:
   379   // The portion [_unallocated_block, _sp.end()) of the space that
   380   // is a single block known not to contain any objects.
   381   // NOTE: See BlockOffsetArrayUseUnallocatedBlock flag.
   382   HeapWord* _unallocated_block;
   384  public:
   385   BlockOffsetArrayNonContigSpace(BlockOffsetSharedArray* array, MemRegion mr):
   386     BlockOffsetArray(array, mr, false),
   387     _unallocated_block(_bottom) { }
   389   // accessor
   390   HeapWord* unallocated_block() const {
   391     assert(BlockOffsetArrayUseUnallocatedBlock,
   392            "_unallocated_block is not being maintained");
   393     return _unallocated_block;
   394   }
   396   void set_unallocated_block(HeapWord* block) {
   397     assert(BlockOffsetArrayUseUnallocatedBlock,
   398            "_unallocated_block is not being maintained");
   399     assert(block >= _bottom && block <= _end, "out of range");
   400     _unallocated_block = block;
   401   }
   403   // These methods expect to be called with [blk_start, blk_end)
   404   // representing a block of memory in the heap.
   405   void alloc_block(HeapWord* blk_start, HeapWord* blk_end);
   406   void alloc_block(HeapWord* blk, size_t size) {
   407     alloc_block(blk, blk + size);
   408   }
   410   // The following methods are useful and optimized for a
   411   // non-contiguous space.
   413   // Given a block [blk_start, blk_start + full_blk_size), and
   414   // a left_blk_size < full_blk_size, adjust the BOT to show two
   415   // blocks [blk_start, blk_start + left_blk_size) and
   416   // [blk_start + left_blk_size, blk_start + full_blk_size).
   417   // It is assumed (and verified in the non-product VM) that the
   418   // BOT was correct for the original block.
   419   void split_block(HeapWord* blk_start, size_t full_blk_size,
   420                            size_t left_blk_size);
   422   // Adjust BOT to show that it has a block in the range
   423   // [blk_start, blk_start + size). Only the first card
   424   // of BOT is touched. It is assumed (and verified in the
   425   // non-product VM) that the remaining cards of the block
   426   // are correct.
   427   void mark_block(HeapWord* blk_start, HeapWord* blk_end);
   428   void mark_block(HeapWord* blk, size_t size) {
   429     mark_block(blk, blk + size);
   430   }
   432   // Adjust _unallocated_block to indicate that a particular
   433   // block has been newly allocated or freed. It is assumed (and
   434   // verified in the non-product VM) that the BOT is correct for
   435   // the given block.
   436   void allocated(HeapWord* blk_start, HeapWord* blk_end) {
   437     // Verify that the BOT shows [blk, blk + blk_size) to be one block.
   438     verify_single_block(blk_start, blk_end);
   439     if (BlockOffsetArrayUseUnallocatedBlock) {
   440       _unallocated_block = MAX2(_unallocated_block, blk_end);
   441     }
   442   }
   444   void allocated(HeapWord* blk, size_t size) {
   445     allocated(blk, blk + size);
   446   }
   448   void freed(HeapWord* blk_start, HeapWord* blk_end);
   449   void freed(HeapWord* blk, size_t size) {
   450     freed(blk, blk + size);
   451   }
   453   HeapWord* block_start_unsafe(const void* addr) const;
   455   // Requires "addr" to be the start of a card and returns the
   456   // start of the block that contains the given address.
   457   HeapWord* block_start_careful(const void* addr) const;
   460   // Verification & debugging: ensure that the offset table reflects
   461   // the fact that the block [blk_start, blk_end) or [blk, blk + size)
   462   // is a single block of storage. NOTE: can't const this because of
   463   // call to non-const do_block_internal() below.
   464   void verify_single_block(HeapWord* blk_start, HeapWord* blk_end)
   465     PRODUCT_RETURN;
   466   void verify_single_block(HeapWord* blk, size_t size) PRODUCT_RETURN;
   468   // Verify that the given block is before _unallocated_block
   469   void verify_not_unallocated(HeapWord* blk_start, HeapWord* blk_end)
   470     const PRODUCT_RETURN;
   471   void verify_not_unallocated(HeapWord* blk, size_t size)
   472     const PRODUCT_RETURN;
   474   // Debugging support
   475   virtual size_t last_active_index() const;
   476 };
   478 ////////////////////////////////////////////////////////////////////////////
   479 // A subtype of BlockOffsetArray that takes advantage of the fact
   480 // that its underlying space is a ContiguousSpace, so that its "active"
   481 // region can be more efficiently tracked (than for a non-contiguous space).
   482 ////////////////////////////////////////////////////////////////////////////
   483 class BlockOffsetArrayContigSpace: public BlockOffsetArray {
   484   friend class VMStructs;
   485  private:
   486   // allocation boundary at which offset array must be updated
   487   HeapWord* _next_offset_threshold;
   488   size_t    _next_offset_index;      // index corresponding to that boundary
   490   // Work function when allocation start crosses threshold.
   491   void alloc_block_work(HeapWord* blk_start, HeapWord* blk_end);
   493  public:
   494   BlockOffsetArrayContigSpace(BlockOffsetSharedArray* array, MemRegion mr):
   495     BlockOffsetArray(array, mr, true) {
   496     _next_offset_threshold = NULL;
   497     _next_offset_index = 0;
   498   }
   500   void set_contig_space(ContiguousSpace* sp) { set_space((Space*)sp); }
   502   // Initialize the threshold for an empty heap.
   503   HeapWord* initialize_threshold();
   504   // Zero out the entry for _bottom (offset will be zero)
   505   void      zero_bottom_entry();
   507   // Return the next threshold, the point at which the table should be
   508   // updated.
   509   HeapWord* threshold() const { return _next_offset_threshold; }
   511   // In general, these methods expect to be called with
   512   // [blk_start, blk_end) representing a block of memory in the heap.
   513   // In this implementation, however, we are OK even if blk_start and/or
   514   // blk_end are NULL because NULL is represented as 0, and thus
   515   // never exceeds the "_next_offset_threshold".
   516   void alloc_block(HeapWord* blk_start, HeapWord* blk_end) {
   517     if (blk_end > _next_offset_threshold) {
   518       alloc_block_work(blk_start, blk_end);
   519     }
   520   }
   521   void alloc_block(HeapWord* blk, size_t size) {
   522     alloc_block(blk, blk + size);
   523   }
   525   HeapWord* block_start_unsafe(const void* addr) const;
   527   void serialize(SerializeOopClosure* soc);
   529   // Debugging support
   530   virtual size_t last_active_index() const;
   531 };

mercurial