< prev index next >

src/hotspot/share/gc/g1/g1RegionToSpaceMapper.cpp

Print this page




   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/g1/g1BiasedArray.hpp"

  27 #include "gc/g1/g1RegionToSpaceMapper.hpp"
  28 #include "logging/log.hpp"
  29 #include "memory/allocation.inline.hpp"
  30 #include "memory/virtualspace.hpp"
  31 #include "runtime/java.hpp"
  32 #include "runtime/os.inline.hpp"
  33 #include "services/memTracker.hpp"
  34 #include "utilities/align.hpp"
  35 #include "utilities/bitMap.inline.hpp"
  36 #include "utilities/formatBuffer.hpp"
  37 
  38 G1RegionToSpaceMapper::G1RegionToSpaceMapper(ReservedSpace rs,
  39                                              size_t used_size,
  40                                              size_t page_size,
  41                                              size_t region_granularity,
  42                                              size_t commit_factor,
  43                                              MemoryType type) :
  44   _listener(NULL),
  45   _storage(rs, used_size, page_size),
  46   _region_granularity(region_granularity),
  47   _commit_map(rs.size() * commit_factor / region_granularity, mtGC) {

  48   guarantee(is_power_of_2(page_size), "must be");
  49   guarantee(is_power_of_2(region_granularity), "must be");
  50 
  51   MemTracker::record_virtual_memory_type((address)rs.base(), type);
  52 }
  53 
  54 // G1RegionToSpaceMapper implementation where the region granularity is larger than
  55 // or the same as the commit granularity.
  56 // Basically, the space corresponding to one region region spans several OS pages.
  57 class G1RegionsLargerThanCommitSizeMapper : public G1RegionToSpaceMapper {
  58  private:
  59   size_t _pages_per_region;
  60 
  61  public:
  62   G1RegionsLargerThanCommitSizeMapper(ReservedSpace rs,
  63                                       size_t actual_size,
  64                                       size_t page_size,
  65                                       size_t alloc_granularity,
  66                                       size_t commit_factor,
  67                                       MemoryType type) :
  68     G1RegionToSpaceMapper(rs, actual_size, page_size, alloc_granularity, commit_factor, type),
  69     _pages_per_region(alloc_granularity / (page_size * commit_factor)) {
  70 
  71     guarantee(alloc_granularity >= page_size, "allocation granularity smaller than commit granularity");
  72   }
  73 
  74   virtual void commit_regions(uint start_idx, size_t num_regions, WorkGang* pretouch_gang) {
  75     size_t const start_page = (size_t)start_idx * _pages_per_region;
  76     bool zero_filled = _storage.commit(start_page, num_regions * _pages_per_region);








  77     if (AlwaysPreTouch) {
  78       _storage.pretouch(start_page, num_regions * _pages_per_region, pretouch_gang);
  79     }
  80     _commit_map.set_range(start_idx, start_idx + num_regions);
  81     fire_on_commit(start_idx, num_regions, zero_filled);
  82   }
  83 
  84   virtual void uncommit_regions(uint start_idx, size_t num_regions) {
  85     _storage.uncommit((size_t)start_idx * _pages_per_region, num_regions * _pages_per_region);
  86     _commit_map.clear_range(start_idx, start_idx + num_regions);
  87   }
  88 };
  89 
  90 // G1RegionToSpaceMapper implementation where the region granularity is smaller
  91 // than the commit granularity.
  92 // Basically, the contents of one OS page span several regions.
  93 class G1RegionsSmallerThanCommitSizeMapper : public G1RegionToSpaceMapper {
  94  private:
  95   class CommitRefcountArray : public G1BiasedMappedArray<uint> {
  96    protected:
  97      virtual uint default_value() const { return 0; }
  98   };


 109   G1RegionsSmallerThanCommitSizeMapper(ReservedSpace rs,
 110                                        size_t actual_size,
 111                                        size_t page_size,
 112                                        size_t alloc_granularity,
 113                                        size_t commit_factor,
 114                                        MemoryType type) :
 115     G1RegionToSpaceMapper(rs, actual_size, page_size, alloc_granularity, commit_factor, type),
 116     _regions_per_page((page_size * commit_factor) / alloc_granularity), _refcounts() {
 117 
 118     guarantee((page_size * commit_factor) >= alloc_granularity, "allocation granularity smaller than commit granularity");
 119     _refcounts.initialize((HeapWord*)rs.base(), (HeapWord*)(rs.base() + align_up(rs.size(), page_size)), page_size);
 120   }
 121 
 122   virtual void commit_regions(uint start_idx, size_t num_regions, WorkGang* pretouch_gang) {
 123     size_t const NoPage = ~(size_t)0;
 124 
 125     size_t first_committed = NoPage;
 126     size_t num_committed = 0;
 127 
 128     bool all_zero_filled = true;

 129 
 130     for (uint i = start_idx; i < start_idx + num_regions; i++) {
 131       assert(!_commit_map.at(i), "Trying to commit storage at region %u that is already committed", i);
 132       size_t idx = region_idx_to_page_idx(i);
 133       uint old_refcount = _refcounts.get_by_index(idx);
 134 
 135       bool zero_filled = false;
 136       if (old_refcount == 0) {
 137         if (first_committed == NoPage) {
 138           first_committed = idx;
 139           num_committed = 1;
 140         } else {
 141           num_committed++;
 142         }
 143         zero_filled = _storage.commit(idx, 1);





 144       }
 145       all_zero_filled &= zero_filled;
 146 
 147       _refcounts.set_by_index(idx, old_refcount + 1);
 148       _commit_map.set_bit(i);
 149     }
 150     if (AlwaysPreTouch && num_committed > 0) {
 151       _storage.pretouch(first_committed, num_committed, pretouch_gang);
 152     }
 153     fire_on_commit(start_idx, num_regions, all_zero_filled);
 154   }
 155 
 156   virtual void uncommit_regions(uint start_idx, size_t num_regions) {
 157     for (uint i = start_idx; i < start_idx + num_regions; i++) {
 158       assert(_commit_map.at(i), "Trying to uncommit storage at region %u that is not committed", i);
 159       size_t idx = region_idx_to_page_idx(i);
 160       uint old_refcount = _refcounts.get_by_index(idx);
 161       assert(old_refcount > 0, "must be");
 162       if (old_refcount == 1) {
 163         _storage.uncommit(idx, 1);
 164       }
 165       _refcounts.set_by_index(idx, old_refcount - 1);
 166       _commit_map.clear_bit(i);
 167     }
 168   }




   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/g1/g1BiasedArray.hpp"
  27 #include "gc/g1/g1NUMA.hpp"
  28 #include "gc/g1/g1RegionToSpaceMapper.hpp"
  29 #include "logging/log.hpp"
  30 #include "memory/allocation.inline.hpp"
  31 #include "memory/virtualspace.hpp"
  32 #include "runtime/java.hpp"
  33 #include "runtime/os.inline.hpp"
  34 #include "services/memTracker.hpp"
  35 #include "utilities/align.hpp"
  36 #include "utilities/bitMap.inline.hpp"
  37 #include "utilities/formatBuffer.hpp"
  38 
  39 G1RegionToSpaceMapper::G1RegionToSpaceMapper(ReservedSpace rs,
  40                                              size_t used_size,
  41                                              size_t page_size,
  42                                              size_t region_granularity,
  43                                              size_t commit_factor,
  44                                              MemoryType type) :
  45   _listener(NULL),
  46   _storage(rs, used_size, page_size),
  47   _region_granularity(region_granularity),
  48   _commit_map(rs.size() * commit_factor / region_granularity, mtGC),
  49   _memory_type(type) {
  50   guarantee(is_power_of_2(page_size), "must be");
  51   guarantee(is_power_of_2(region_granularity), "must be");
  52 
  53   MemTracker::record_virtual_memory_type((address)rs.base(), type);
  54 }
  55 
  56 // G1RegionToSpaceMapper implementation where the region granularity is larger than
  57 // or the same as the commit granularity.
  58 // Basically, the space corresponding to one region region spans several OS pages.
  59 class G1RegionsLargerThanCommitSizeMapper : public G1RegionToSpaceMapper {
  60  private:
  61   size_t _pages_per_region;
  62 
  63  public:
  64   G1RegionsLargerThanCommitSizeMapper(ReservedSpace rs,
  65                                       size_t actual_size,
  66                                       size_t page_size,
  67                                       size_t alloc_granularity,
  68                                       size_t commit_factor,
  69                                       MemoryType type) :
  70     G1RegionToSpaceMapper(rs, actual_size, page_size, alloc_granularity, commit_factor, type),
  71     _pages_per_region(alloc_granularity / (page_size * commit_factor)) {
  72 
  73     guarantee(alloc_granularity >= page_size, "allocation granularity smaller than commit granularity");
  74   }
  75 
  76   virtual void commit_regions(uint start_idx, size_t num_regions, WorkGang* pretouch_gang) {
  77     const size_t start_page = (size_t)start_idx * _pages_per_region;
  78     const size_t size_in_pages = num_regions * _pages_per_region;
  79     bool zero_filled = _storage.commit(start_page, size_in_pages);
  80     if (_memory_type == mtJavaHeap) {
  81       for (uint region_index = start_idx; region_index < start_idx + num_regions; region_index++ ) {
  82         void* address = _storage.page_start(region_index * _pages_per_region);
  83         size_t size_in_bytes = _storage.page_size() * _pages_per_region;
  84         G1NUMA::numa()->request_memory_on_node(address, size_in_bytes, region_index);
  85       }
  86     }
  87     if (AlwaysPreTouch) {
  88       _storage.pretouch(start_page, size_in_pages, pretouch_gang);
  89     }
  90     _commit_map.set_range(start_idx, start_idx + num_regions);
  91     fire_on_commit(start_idx, num_regions, zero_filled);
  92   }
  93 
  94   virtual void uncommit_regions(uint start_idx, size_t num_regions) {
  95     _storage.uncommit((size_t)start_idx * _pages_per_region, num_regions * _pages_per_region);
  96     _commit_map.clear_range(start_idx, start_idx + num_regions);
  97   }
  98 };
  99 
 100 // G1RegionToSpaceMapper implementation where the region granularity is smaller
 101 // than the commit granularity.
 102 // Basically, the contents of one OS page span several regions.
 103 class G1RegionsSmallerThanCommitSizeMapper : public G1RegionToSpaceMapper {
 104  private:
 105   class CommitRefcountArray : public G1BiasedMappedArray<uint> {
 106    protected:
 107      virtual uint default_value() const { return 0; }
 108   };


 119   G1RegionsSmallerThanCommitSizeMapper(ReservedSpace rs,
 120                                        size_t actual_size,
 121                                        size_t page_size,
 122                                        size_t alloc_granularity,
 123                                        size_t commit_factor,
 124                                        MemoryType type) :
 125     G1RegionToSpaceMapper(rs, actual_size, page_size, alloc_granularity, commit_factor, type),
 126     _regions_per_page((page_size * commit_factor) / alloc_granularity), _refcounts() {
 127 
 128     guarantee((page_size * commit_factor) >= alloc_granularity, "allocation granularity smaller than commit granularity");
 129     _refcounts.initialize((HeapWord*)rs.base(), (HeapWord*)(rs.base() + align_up(rs.size(), page_size)), page_size);
 130   }
 131 
 132   virtual void commit_regions(uint start_idx, size_t num_regions, WorkGang* pretouch_gang) {
 133     size_t const NoPage = ~(size_t)0;
 134 
 135     size_t first_committed = NoPage;
 136     size_t num_committed = 0;
 137 
 138     bool all_zero_filled = true;
 139     G1NUMA* numa = G1NUMA::numa();
 140 
 141     for (uint region_idx = start_idx; region_idx < start_idx + num_regions; region_idx++) {
 142       assert(!_commit_map.at(region_idx), "Trying to commit storage at region %u that is already committed", region_idx);
 143       size_t page_idx = region_idx_to_page_idx(region_idx);
 144       uint old_refcount = _refcounts.get_by_index(page_idx);
 145 
 146       bool zero_filled = false;
 147       if (old_refcount == 0) {
 148         if (first_committed == NoPage) {
 149           first_committed = page_idx;
 150           num_committed = 1;
 151         } else {
 152           num_committed++;
 153         }
 154         zero_filled = _storage.commit(page_idx, 1);
 155         if (_memory_type == mtJavaHeap) {
 156           void* address = _storage.page_start(page_idx);
 157           size_t size_in_bytes = _storage.page_size();
 158           numa->request_memory_on_node(address, size_in_bytes, region_idx);
 159         }
 160       }
 161       all_zero_filled &= zero_filled;
 162 
 163       _refcounts.set_by_index(page_idx, old_refcount + 1);
 164       _commit_map.set_bit(region_idx);
 165     }
 166     if (AlwaysPreTouch && num_committed > 0) {
 167       _storage.pretouch(first_committed, num_committed, pretouch_gang);
 168     }
 169     fire_on_commit(start_idx, num_regions, all_zero_filled);
 170   }
 171 
 172   virtual void uncommit_regions(uint start_idx, size_t num_regions) {
 173     for (uint i = start_idx; i < start_idx + num_regions; i++) {
 174       assert(_commit_map.at(i), "Trying to uncommit storage at region %u that is not committed", i);
 175       size_t idx = region_idx_to_page_idx(i);
 176       uint old_refcount = _refcounts.get_by_index(idx);
 177       assert(old_refcount > 0, "must be");
 178       if (old_refcount == 1) {
 179         _storage.uncommit(idx, 1);
 180       }
 181       _refcounts.set_by_index(idx, old_refcount - 1);
 182       _commit_map.clear_bit(i);
 183     }
 184   }


< prev index next >