src/share/vm/gc_implementation/g1/heapRegionManager.hpp

Print this page
rev 7056 : [mq]: 8058298
rev 7057 : [mq]: review


  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 #ifndef SHARE_VM_GC_IMPLEMENTATION_G1_HEAPREGIONMANAGER_HPP
  26 #define SHARE_VM_GC_IMPLEMENTATION_G1_HEAPREGIONMANAGER_HPP
  27 
  28 #include "gc_implementation/g1/g1BiasedArray.hpp"
  29 #include "gc_implementation/g1/g1RegionToSpaceMapper.hpp"
  30 #include "gc_implementation/g1/heapRegionSet.hpp"
  31 
  32 class HeapRegion;
  33 class HeapRegionClosure;

  34 class FreeRegionList;
  35 
  36 class G1HeapRegionTable : public G1BiasedMappedArray<HeapRegion*> {
  37  protected:
  38   virtual HeapRegion* default_value() const { return NULL; }
  39 };
  40 
  41 // This class keeps track of the actual heap memory, auxiliary data
  42 // and its metadata (i.e., HeapRegion instances) and the list of free regions.
  43 //
  44 // This allows maximum flexibility for deciding what to commit or uncommit given
  45 // a request from outside.
  46 //
  47 // HeapRegions are kept in the _regions array in address order. A region's
  48 // index in the array corresponds to its index in the heap (i.e., 0 is the
  49 // region at the bottom of the heap, 1 is the one after it, etc.). Two
  50 // regions that are consecutive in the array should also be adjacent in the
  51 // address space (i.e., region(i).end() == region(i+1).bottom().
  52 //
  53 // We create a HeapRegion when we commit the region's address space
  54 // for the first time. When we uncommit the address space of a
  55 // region we retain the HeapRegion to be able to re-use it in the
  56 // future (in case we recommit it).
  57 //
  58 // We keep track of three lengths:
  59 //
  60 // * _num_committed (returned by length()) is the number of currently
  61 //   committed regions. These may not be contiguous.
  62 // * _allocated_heapregions_length (not exposed outside this class) is the
  63 //   number of regions+1 for which we have HeapRegions.
  64 // * max_length() returns the maximum number of regions the heap can have.
  65 //
  66 
  67 class HeapRegionManager: public CHeapObj<mtGC> {
  68   friend class VMStructs;

  69 
  70   G1HeapRegionTable _regions;
  71 
  72   G1RegionToSpaceMapper* _heap_mapper;
  73   G1RegionToSpaceMapper* _prev_bitmap_mapper;
  74   G1RegionToSpaceMapper* _next_bitmap_mapper;
  75   G1RegionToSpaceMapper* _bot_mapper;
  76   G1RegionToSpaceMapper* _cardtable_mapper;
  77   G1RegionToSpaceMapper* _card_counts_mapper;
  78 
  79   FreeRegionList _free_list;
  80 
  81   // Each bit in this bitmap indicates that the corresponding region is available
  82   // for allocation.
  83   BitMap _available_map;
  84 
  85    // The number of regions committed in the heap.
  86   uint _num_committed;
  87 
  88   // Internal only. The highest heap region +1 we allocated a HeapRegion instance for.
  89   uint _allocated_heapregions_length;
  90 
  91   HeapWord* heap_bottom() const { return _regions.bottom_address_mapped(); }
  92   HeapWord* heap_end() const {return _regions.end_address_mapped(); }
  93 
  94   void make_regions_available(uint index, uint num_regions = 1);
  95 
  96   // Pass down commit calls to the VirtualSpace.
  97   void commit_regions(uint index, size_t num_regions = 1);
  98   void uncommit_regions(uint index, size_t num_regions = 1);
  99 
 100   // Notify other data structures about change in the heap layout.
 101   void update_committed_space(HeapWord* old_end, HeapWord* new_end);
 102   // Calculate the starting region for each worker during parallel iteration so
 103   // that they do not all start from the same region.
 104   uint start_region_for_worker(uint worker_i, uint num_workers, uint num_regions) const;
 105 
 106   // Find a contiguous set of empty or uncommitted regions of length num and return
 107   // the index of the first region or G1_NO_HRM_INDEX if the search was unsuccessful.
 108   // If only_empty is true, only empty regions are considered.
 109   // Searches from bottom to top of the heap, doing a first-fit.
 110   uint find_contiguous(size_t num, bool only_empty);
 111   // Finds the next sequence of unavailable regions starting from start_idx. Returns the
 112   // length of the sequence found. If this result is zero, no such sequence could be found,
 113   // otherwise res_idx indicates the start index of these regions.
 114   uint find_unavailable_from_idx(uint start_idx, uint* res_idx) const;
 115   // Finds the next sequence of empty regions starting from start_idx, going backwards in
 116   // the heap. Returns the length of the sequence found. If this value is zero, no
 117   // sequence could be found, otherwise res_idx contains the start index of this range.
 118   uint find_empty_from_idx_reverse(uint start_idx, uint* res_idx) const;
 119   // Allocate a new HeapRegion for the given index.
 120   HeapRegion* new_heap_region(uint hrm_index);
 121 #ifdef ASSERT
 122 public:
 123   bool is_free(HeapRegion* hr) const;
 124 #endif


 206   uint expand_by(uint num_regions);
 207 
 208   // Makes sure that the regions from start to start+num_regions-1 are available
 209   // for allocation. Returns the number of regions that were committed to achieve
 210   // this.
 211   uint expand_at(uint start, uint num_regions);
 212 
 213   // Find a contiguous set of empty regions of length num. Returns the start index of
 214   // that set, or G1_NO_HRM_INDEX.
 215   uint find_contiguous_only_empty(size_t num) { return find_contiguous(num, true); }
 216   // Find a contiguous set of empty or unavailable regions of length num. Returns the
 217   // start index of that set, or G1_NO_HRM_INDEX.
 218   uint find_contiguous_empty_or_unavailable(size_t num) { return find_contiguous(num, false); }
 219 
 220   HeapRegion* next_region_in_heap(const HeapRegion* r) const;
 221 
 222   // Apply blk->doHeapRegion() on all committed regions in address order,
 223   // terminating the iteration early if doHeapRegion() returns true.
 224   void iterate(HeapRegionClosure* blk) const;
 225 
 226   void par_iterate(HeapRegionClosure* blk, uint worker_id, uint no_of_par_workers, jint claim_value) const;
 227 
 228   // Uncommit up to num_regions_to_remove regions that are completely free.
 229   // Return the actual number of uncommitted regions.
 230   uint shrink_by(uint num_regions_to_remove);
 231 
 232   void verify();
 233 
 234   // Do some sanity checking.
 235   void verify_optional() PRODUCT_RETURN;
 236 };
 237 







































 238 #endif // SHARE_VM_GC_IMPLEMENTATION_G1_HEAPREGIONMANAGER_HPP
 239 


  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 #ifndef SHARE_VM_GC_IMPLEMENTATION_G1_HEAPREGIONMANAGER_HPP
  26 #define SHARE_VM_GC_IMPLEMENTATION_G1_HEAPREGIONMANAGER_HPP
  27 
  28 #include "gc_implementation/g1/g1BiasedArray.hpp"
  29 #include "gc_implementation/g1/g1RegionToSpaceMapper.hpp"
  30 #include "gc_implementation/g1/heapRegionSet.hpp"
  31 
  32 class HeapRegion;
  33 class HeapRegionClosure;
  34 class HeapRegionClaimer;
  35 class FreeRegionList;
  36 
  37 class G1HeapRegionTable : public G1BiasedMappedArray<HeapRegion*> {
  38  protected:
  39   virtual HeapRegion* default_value() const { return NULL; }
  40 };
  41 
  42 // This class keeps track of the actual heap memory, auxiliary data
  43 // and its metadata (i.e., HeapRegion instances) and the list of free regions.
  44 //
  45 // This allows maximum flexibility for deciding what to commit or uncommit given
  46 // a request from outside.
  47 //
  48 // HeapRegions are kept in the _regions array in address order. A region's
  49 // index in the array corresponds to its index in the heap (i.e., 0 is the
  50 // region at the bottom of the heap, 1 is the one after it, etc.). Two
  51 // regions that are consecutive in the array should also be adjacent in the
  52 // address space (i.e., region(i).end() == region(i+1).bottom().
  53 //
  54 // We create a HeapRegion when we commit the region's address space
  55 // for the first time. When we uncommit the address space of a
  56 // region we retain the HeapRegion to be able to re-use it in the
  57 // future (in case we recommit it).
  58 //
  59 // We keep track of three lengths:
  60 //
  61 // * _num_committed (returned by length()) is the number of currently
  62 //   committed regions. These may not be contiguous.
  63 // * _allocated_heapregions_length (not exposed outside this class) is the
  64 //   number of regions+1 for which we have HeapRegions.
  65 // * max_length() returns the maximum number of regions the heap can have.
  66 //
  67 
  68 class HeapRegionManager: public CHeapObj<mtGC> {
  69   friend class VMStructs;
  70   friend class HeapRegionClaimer;
  71 
  72   G1HeapRegionTable _regions;
  73 
  74   G1RegionToSpaceMapper* _heap_mapper;
  75   G1RegionToSpaceMapper* _prev_bitmap_mapper;
  76   G1RegionToSpaceMapper* _next_bitmap_mapper;
  77   G1RegionToSpaceMapper* _bot_mapper;
  78   G1RegionToSpaceMapper* _cardtable_mapper;
  79   G1RegionToSpaceMapper* _card_counts_mapper;
  80 
  81   FreeRegionList _free_list;
  82 
  83   // Each bit in this bitmap indicates that the corresponding region is available
  84   // for allocation.
  85   BitMap _available_map;
  86 
  87    // The number of regions committed in the heap.
  88   uint _num_committed;
  89 
  90   // Internal only. The highest heap region +1 we allocated a HeapRegion instance for.
  91   uint _allocated_heapregions_length;
  92 
  93   HeapWord* heap_bottom() const { return _regions.bottom_address_mapped(); }
  94   HeapWord* heap_end() const {return _regions.end_address_mapped(); }
  95 
  96   void make_regions_available(uint index, uint num_regions = 1);
  97 
  98   // Pass down commit calls to the VirtualSpace.
  99   void commit_regions(uint index, size_t num_regions = 1);
 100   void uncommit_regions(uint index, size_t num_regions = 1);
 101 
 102   // Notify other data structures about change in the heap layout.
 103   void update_committed_space(HeapWord* old_end, HeapWord* new_end);



 104 
 105   // Find a contiguous set of empty or uncommitted regions of length num and return
 106   // the index of the first region or G1_NO_HRM_INDEX if the search was unsuccessful.
 107   // If only_empty is true, only empty regions are considered.
 108   // Searches from bottom to top of the heap, doing a first-fit.
 109   uint find_contiguous(size_t num, bool only_empty);
 110   // Finds the next sequence of unavailable regions starting from start_idx. Returns the
 111   // length of the sequence found. If this result is zero, no such sequence could be found,
 112   // otherwise res_idx indicates the start index of these regions.
 113   uint find_unavailable_from_idx(uint start_idx, uint* res_idx) const;
 114   // Finds the next sequence of empty regions starting from start_idx, going backwards in
 115   // the heap. Returns the length of the sequence found. If this value is zero, no
 116   // sequence could be found, otherwise res_idx contains the start index of this range.
 117   uint find_empty_from_idx_reverse(uint start_idx, uint* res_idx) const;
 118   // Allocate a new HeapRegion for the given index.
 119   HeapRegion* new_heap_region(uint hrm_index);
 120 #ifdef ASSERT
 121 public:
 122   bool is_free(HeapRegion* hr) const;
 123 #endif


 205   uint expand_by(uint num_regions);
 206 
 207   // Makes sure that the regions from start to start+num_regions-1 are available
 208   // for allocation. Returns the number of regions that were committed to achieve
 209   // this.
 210   uint expand_at(uint start, uint num_regions);
 211 
 212   // Find a contiguous set of empty regions of length num. Returns the start index of
 213   // that set, or G1_NO_HRM_INDEX.
 214   uint find_contiguous_only_empty(size_t num) { return find_contiguous(num, true); }
 215   // Find a contiguous set of empty or unavailable regions of length num. Returns the
 216   // start index of that set, or G1_NO_HRM_INDEX.
 217   uint find_contiguous_empty_or_unavailable(size_t num) { return find_contiguous(num, false); }
 218 
 219   HeapRegion* next_region_in_heap(const HeapRegion* r) const;
 220 
 221   // Apply blk->doHeapRegion() on all committed regions in address order,
 222   // terminating the iteration early if doHeapRegion() returns true.
 223   void iterate(HeapRegionClosure* blk) const;
 224 
 225   void par_iterate(HeapRegionClosure* blk, uint worker_id, HeapRegionClaimer* hrclaimer) const;
 226 
 227   // Uncommit up to num_regions_to_remove regions that are completely free.
 228   // Return the actual number of uncommitted regions.
 229   uint shrink_by(uint num_regions_to_remove);
 230 
 231   void verify();
 232 
 233   // Do some sanity checking.
 234   void verify_optional() PRODUCT_RETURN;
 235 };
 236 
 237 // The HeapRegionClaimer is used during parallel iteration over heap regions,
 238 // allowing workers to claim heap regions, gaining exclusive rights to these regions.
 239 class HeapRegionClaimer : public StackObj {
 240   uint  _n_workers;
 241   uint  _n_regions;
 242   uint* _claims;
 243 
 244   static const uint Unclaimed = 0;
 245   static const uint Claimed   = 1;
 246 
 247  public:
 248   HeapRegionClaimer() : _n_workers(0), _n_regions(0), _claims(NULL) {}
 249 
 250   HeapRegionClaimer(uint n_workers) : _n_workers(n_workers), _n_regions(0), _claims(NULL) {
 251     initialize(n_workers);
 252   }
 253 
 254   ~HeapRegionClaimer() {
 255     if (_claims != NULL) {
 256       FREE_C_HEAP_ARRAY(uint, _claims, mtGC);
 257     }
 258   }
 259 
 260   inline uint n_regions() const {
 261     return _n_regions;
 262   }
 263 
 264   void initialize(uint n_workers);
 265 
 266   // Calculate the starting region for given worker so
 267   // that they do not all start from the same region.
 268   uint start_region_for_worker(uint worker_id) const;
 269 
 270   // Check if region has been claimed with this HRClaimer.
 271   bool is_region_claimed(uint region_index) const;
 272 
 273   // Claim the given region, returns true if successfully claimed.
 274   bool claim_region(uint region_index);
 275 };
 276 #endif // SHARE_VM_GC_IMPLEMENTATION_G1_HEAPREGIONMANAGER_HPP
 277