1 /*
   2  * Copyright (c) 2001, 2013, 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/g1BiasedArray.hpp"
  27 #include "gc_implementation/g1/g1RegionToSpaceMapper.hpp"
  28 #include "runtime/virtualspace.hpp"
  29 #include "services/memTracker.hpp"
  30 
  31 G1RegionToSpaceMapper::G1RegionToSpaceMapper(ReservedSpace rs,
  32                                              size_t commit_granularity,
  33                                              size_t region_granularity,
  34                                              MemoryType type) :
  35   _storage(),
  36   _commit_granularity(commit_granularity),
  37   _region_granularity(region_granularity),
  38   _listener(NULL),
  39   _commit_map() {
  40   guarantee(is_power_of_2(commit_granularity), "must be");
  41   guarantee(is_power_of_2(region_granularity), "must be");
  42   _storage.initialize_with_granularity(rs, commit_granularity);
  43 
  44   MemTracker::record_virtual_memory_type((address)rs.base(), type);
  45 }
  46 
  47 // G1RegionToSpaceMapper implementation where the region granularity is larger than
  48 // or the same as the commit granularity.
  49 // Basically, the space corresponding to one region region spans several OS pages.
  50 class G1RegionsLargerThanCommitSizeMapper : public G1RegionToSpaceMapper {
  51  private:
  52   size_t _pages_per_region;
  53 
  54  public:
  55   G1RegionsLargerThanCommitSizeMapper(ReservedSpace rs,
  56                                       size_t os_commit_granularity,
  57                                       size_t alloc_granularity,
  58                                       size_t commit_factor,
  59                                       MemoryType type) :
  60      G1RegionToSpaceMapper(rs, os_commit_granularity, alloc_granularity, type),
  61     _pages_per_region(alloc_granularity / (os_commit_granularity * commit_factor)) {
  62 
  63     guarantee(alloc_granularity >= os_commit_granularity, "allocation granularity smaller than commit granularity");
  64     _commit_map.resize(rs.size() * commit_factor / alloc_granularity, /* in_resource_area */ false);
  65   }
  66 
  67   virtual void commit_regions(uintptr_t start_idx, size_t num_regions) {
  68     _storage.commit(start_idx * _pages_per_region, num_regions * _pages_per_region);
  69     _commit_map.set_range(start_idx, start_idx + num_regions);
  70     fire_on_commit(start_idx, num_regions);
  71   }
  72 
  73   virtual void uncommit_regions(uintptr_t start_idx, size_t num_regions) {
  74     _storage.uncommit(start_idx * _pages_per_region, num_regions * _pages_per_region);
  75     _commit_map.clear_range(start_idx, start_idx + num_regions);
  76   }
  77 };
  78 
  79 // G1RegionToSpaceMapper implementation where the region granularity is smaller
  80 // than the commit granularity.
  81 // Basically, the contents of one OS page span several regions.
  82 class G1RegionsSmallerThanCommitSizeMapper : public G1RegionToSpaceMapper {
  83  private:
  84   class CommitRefcountArray : public G1BiasedMappedArray<uint> {
  85    protected:
  86      virtual uint default_value() const { return 0; }
  87   };
  88 
  89   size_t _regions_per_page;
  90 
  91   CommitRefcountArray _refcounts;
  92 
  93   uintptr_t region_idx_to_page_idx(uint region) const {
  94     return region / _regions_per_page;
  95   }
  96 
  97  public:
  98   G1RegionsSmallerThanCommitSizeMapper(ReservedSpace rs,
  99                                        size_t os_commit_granularity,
 100                                        size_t alloc_granularity,
 101                                        size_t commit_factor,
 102                                        MemoryType type) :
 103      G1RegionToSpaceMapper(rs, os_commit_granularity, alloc_granularity, type),
 104     _regions_per_page((os_commit_granularity * commit_factor) / alloc_granularity), _refcounts() {
 105 
 106     guarantee((os_commit_granularity * commit_factor) >= alloc_granularity, "allocation granularity smaller than commit granularity");
 107     _refcounts.initialize((HeapWord*)rs.base(), (HeapWord*)(rs.base() + rs.size()), os_commit_granularity);
 108     _commit_map.resize(rs.size() * commit_factor / alloc_granularity, /* in_resource_area */ false);
 109   }
 110 
 111   virtual void commit_regions(uintptr_t start_idx, size_t num_regions) {
 112     for (uintptr_t i = start_idx; i < start_idx + num_regions; i++) {
 113       assert(!_commit_map.at(i), err_msg("Trying to commit storage at region "INTPTR_FORMAT" that is already committed", i));
 114       uintptr_t idx = region_idx_to_page_idx(i);
 115       uint old_refcount = _refcounts.get_by_index(idx);
 116       if (old_refcount == 0) {
 117         _storage.commit(idx, 1);
 118       }
 119       _refcounts.set_by_index(idx, old_refcount + 1);
 120       _commit_map.set_bit(i);
 121       fire_on_commit(i, 1);
 122     }
 123   }
 124 
 125   virtual void uncommit_regions(uintptr_t start_idx, size_t num_regions) {
 126     for (uintptr_t i = start_idx; i < start_idx + num_regions; i++) {
 127       assert(_commit_map.at(i), err_msg("Trying to uncommit storage at region "INTPTR_FORMAT" that is not committed", i));
 128       uintptr_t idx = region_idx_to_page_idx(i);
 129       uint old_refcount = _refcounts.get_by_index(idx);
 130       assert(old_refcount > 0, "must be");
 131       if (old_refcount == 1) {
 132         _storage.uncommit(idx, 1);
 133       }
 134       _refcounts.set_by_index(idx, old_refcount - 1);
 135       _commit_map.clear_bit(i);
 136     }
 137   }
 138 };
 139 
 140 void G1RegionToSpaceMapper::fire_on_commit(uint start_idx, size_t num_regions) {
 141   if (_listener != NULL) {
 142     _listener->on_commit(start_idx, num_regions);
 143   }
 144 }
 145 
 146 G1RegionToSpaceMapper* G1RegionToSpaceMapper::create_mapper(ReservedSpace rs,
 147                                                             size_t os_commit_granularity,
 148                                                             size_t region_granularity,
 149                                                             size_t commit_factor,
 150                                                             MemoryType type) {
 151 
 152   if (region_granularity >= (os_commit_granularity * commit_factor)) {
 153     return new G1RegionsLargerThanCommitSizeMapper(rs, os_commit_granularity, region_granularity, commit_factor, type);
 154   } else {
 155     return new G1RegionsSmallerThanCommitSizeMapper(rs, os_commit_granularity, region_granularity, commit_factor, type);
 156   }
 157 }