src/share/vm/gc_implementation/g1/g1RegionToSpaceMapper.cpp

changeset 7051
1f1d373cd044
child 7053
7b2fc3129653
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/vm/gc_implementation/g1/g1RegionToSpaceMapper.cpp	Thu Aug 21 11:47:10 2014 +0200
     1.3 @@ -0,0 +1,158 @@
     1.4 +/*
     1.5 + * Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + *
    1.26 + */
    1.27 +
    1.28 +#include "precompiled.hpp"
    1.29 +#include "gc_implementation/g1/g1BiasedArray.hpp"
    1.30 +#include "gc_implementation/g1/g1RegionToSpaceMapper.hpp"
    1.31 +#include "runtime/virtualspace.hpp"
    1.32 +#include "services/memTracker.hpp"
    1.33 +#include "utilities/bitMap.inline.hpp"
    1.34 +
    1.35 +G1RegionToSpaceMapper::G1RegionToSpaceMapper(ReservedSpace rs,
    1.36 +                                             size_t commit_granularity,
    1.37 +                                             size_t region_granularity,
    1.38 +                                             MemoryType type) :
    1.39 +  _storage(),
    1.40 +  _commit_granularity(commit_granularity),
    1.41 +  _region_granularity(region_granularity),
    1.42 +  _listener(NULL),
    1.43 +  _commit_map() {
    1.44 +  guarantee(is_power_of_2(commit_granularity), "must be");
    1.45 +  guarantee(is_power_of_2(region_granularity), "must be");
    1.46 +  _storage.initialize_with_granularity(rs, commit_granularity);
    1.47 +
    1.48 +  MemTracker::record_virtual_memory_type((address)rs.base(), type);
    1.49 +}
    1.50 +
    1.51 +// G1RegionToSpaceMapper implementation where the region granularity is larger than
    1.52 +// or the same as the commit granularity.
    1.53 +// Basically, the space corresponding to one region region spans several OS pages.
    1.54 +class G1RegionsLargerThanCommitSizeMapper : public G1RegionToSpaceMapper {
    1.55 + private:
    1.56 +  size_t _pages_per_region;
    1.57 +
    1.58 + public:
    1.59 +  G1RegionsLargerThanCommitSizeMapper(ReservedSpace rs,
    1.60 +                                      size_t os_commit_granularity,
    1.61 +                                      size_t alloc_granularity,
    1.62 +                                      size_t commit_factor,
    1.63 +                                      MemoryType type) :
    1.64 +     G1RegionToSpaceMapper(rs, os_commit_granularity, alloc_granularity, type),
    1.65 +    _pages_per_region(alloc_granularity / (os_commit_granularity * commit_factor)) {
    1.66 +
    1.67 +    guarantee(alloc_granularity >= os_commit_granularity, "allocation granularity smaller than commit granularity");
    1.68 +    _commit_map.resize(rs.size() * commit_factor / alloc_granularity, /* in_resource_area */ false);
    1.69 +  }
    1.70 +
    1.71 +  virtual void commit_regions(uintptr_t start_idx, size_t num_regions) {
    1.72 +    _storage.commit(start_idx * _pages_per_region, num_regions * _pages_per_region);
    1.73 +    _commit_map.set_range(start_idx, start_idx + num_regions);
    1.74 +    fire_on_commit(start_idx, num_regions);
    1.75 +  }
    1.76 +
    1.77 +  virtual void uncommit_regions(uintptr_t start_idx, size_t num_regions) {
    1.78 +    _storage.uncommit(start_idx * _pages_per_region, num_regions * _pages_per_region);
    1.79 +    _commit_map.clear_range(start_idx, start_idx + num_regions);
    1.80 +  }
    1.81 +};
    1.82 +
    1.83 +// G1RegionToSpaceMapper implementation where the region granularity is smaller
    1.84 +// than the commit granularity.
    1.85 +// Basically, the contents of one OS page span several regions.
    1.86 +class G1RegionsSmallerThanCommitSizeMapper : public G1RegionToSpaceMapper {
    1.87 + private:
    1.88 +  class CommitRefcountArray : public G1BiasedMappedArray<uint> {
    1.89 +   protected:
    1.90 +     virtual uint default_value() const { return 0; }
    1.91 +  };
    1.92 +
    1.93 +  size_t _regions_per_page;
    1.94 +
    1.95 +  CommitRefcountArray _refcounts;
    1.96 +
    1.97 +  uintptr_t region_idx_to_page_idx(uint region) const {
    1.98 +    return region / _regions_per_page;
    1.99 +  }
   1.100 +
   1.101 + public:
   1.102 +  G1RegionsSmallerThanCommitSizeMapper(ReservedSpace rs,
   1.103 +                                       size_t os_commit_granularity,
   1.104 +                                       size_t alloc_granularity,
   1.105 +                                       size_t commit_factor,
   1.106 +                                       MemoryType type) :
   1.107 +     G1RegionToSpaceMapper(rs, os_commit_granularity, alloc_granularity, type),
   1.108 +    _regions_per_page((os_commit_granularity * commit_factor) / alloc_granularity), _refcounts() {
   1.109 +
   1.110 +    guarantee((os_commit_granularity * commit_factor) >= alloc_granularity, "allocation granularity smaller than commit granularity");
   1.111 +    _refcounts.initialize((HeapWord*)rs.base(), (HeapWord*)(rs.base() + rs.size()), os_commit_granularity);
   1.112 +    _commit_map.resize(rs.size() * commit_factor / alloc_granularity, /* in_resource_area */ false);
   1.113 +  }
   1.114 +
   1.115 +  virtual void commit_regions(uintptr_t start_idx, size_t num_regions) {
   1.116 +    for (uintptr_t i = start_idx; i < start_idx + num_regions; i++) {
   1.117 +      assert(!_commit_map.at(i), err_msg("Trying to commit storage at region "INTPTR_FORMAT" that is already committed", i));
   1.118 +      uintptr_t idx = region_idx_to_page_idx(i);
   1.119 +      uint old_refcount = _refcounts.get_by_index(idx);
   1.120 +      if (old_refcount == 0) {
   1.121 +        _storage.commit(idx, 1);
   1.122 +      }
   1.123 +      _refcounts.set_by_index(idx, old_refcount + 1);
   1.124 +      _commit_map.set_bit(i);
   1.125 +      fire_on_commit(i, 1);
   1.126 +    }
   1.127 +  }
   1.128 +
   1.129 +  virtual void uncommit_regions(uintptr_t start_idx, size_t num_regions) {
   1.130 +    for (uintptr_t i = start_idx; i < start_idx + num_regions; i++) {
   1.131 +      assert(_commit_map.at(i), err_msg("Trying to uncommit storage at region "INTPTR_FORMAT" that is not committed", i));
   1.132 +      uintptr_t idx = region_idx_to_page_idx(i);
   1.133 +      uint old_refcount = _refcounts.get_by_index(idx);
   1.134 +      assert(old_refcount > 0, "must be");
   1.135 +      if (old_refcount == 1) {
   1.136 +        _storage.uncommit(idx, 1);
   1.137 +      }
   1.138 +      _refcounts.set_by_index(idx, old_refcount - 1);
   1.139 +      _commit_map.clear_bit(i);
   1.140 +    }
   1.141 +  }
   1.142 +};
   1.143 +
   1.144 +void G1RegionToSpaceMapper::fire_on_commit(uint start_idx, size_t num_regions) {
   1.145 +  if (_listener != NULL) {
   1.146 +    _listener->on_commit(start_idx, num_regions);
   1.147 +  }
   1.148 +}
   1.149 +
   1.150 +G1RegionToSpaceMapper* G1RegionToSpaceMapper::create_mapper(ReservedSpace rs,
   1.151 +                                                            size_t os_commit_granularity,
   1.152 +                                                            size_t region_granularity,
   1.153 +                                                            size_t commit_factor,
   1.154 +                                                            MemoryType type) {
   1.155 +
   1.156 +  if (region_granularity >= (os_commit_granularity * commit_factor)) {
   1.157 +    return new G1RegionsLargerThanCommitSizeMapper(rs, os_commit_granularity, region_granularity, commit_factor, type);
   1.158 +  } else {
   1.159 +    return new G1RegionsSmallerThanCommitSizeMapper(rs, os_commit_granularity, region_granularity, commit_factor, type);
   1.160 +  }
   1.161 +}

mercurial