hotspot/src/share/vm/gc_implementation/g1/g1RegionToSpaceMapper.cpp
author tschatzl
Tue, 19 Aug 2014 14:09:10 +0200
changeset 26160 aba6b01cb988
child 26163 89f44df5b438
permissions -rw-r--r--
8038423: G1: Decommit memory within heap Summary: Allow G1 to decommit memory of arbitrary regions within the heap and their associated auxiliary data structures card table, BOT, hot card cache, and mark bitmaps. Reviewed-by: mgerdin, brutisso, jwilhelm
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
26160
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
     1
/*
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
     2
 * Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved.
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
     4
 *
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
     7
 * published by the Free Software Foundation.
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
     8
 *
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    13
 * accompanied this code).
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    14
 *
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    18
 *
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    21
 * questions.
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    22
 *
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    23
 */
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    24
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    25
#include "precompiled.hpp"
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    26
#include "gc_implementation/g1/g1BiasedArray.hpp"
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    27
#include "gc_implementation/g1/g1RegionToSpaceMapper.hpp"
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    28
#include "runtime/virtualspace.hpp"
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    29
#include "services/memTracker.hpp"
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    30
#include "utilities/bitMap.inline.hpp"
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    31
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    32
G1RegionToSpaceMapper::G1RegionToSpaceMapper(ReservedSpace rs,
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    33
                                             size_t commit_granularity,
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    34
                                             size_t region_granularity,
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    35
                                             MemoryType type) :
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    36
  _storage(),
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    37
  _commit_granularity(commit_granularity),
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    38
  _region_granularity(region_granularity),
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    39
  _listener(NULL),
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    40
  _commit_map() {
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    41
  guarantee(is_power_of_2(commit_granularity), "must be");
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    42
  guarantee(is_power_of_2(region_granularity), "must be");
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    43
  _storage.initialize_with_granularity(rs, commit_granularity);
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    44
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    45
  MemTracker::record_virtual_memory_type((address)rs.base(), type);
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    46
}
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    47
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    48
// G1RegionToSpaceMapper implementation where the region granularity is larger than
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    49
// or the same as the commit granularity.
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    50
// Basically, the space corresponding to one region region spans several OS pages.
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    51
class G1RegionsLargerThanCommitSizeMapper : public G1RegionToSpaceMapper {
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    52
 private:
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    53
  size_t _pages_per_region;
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    54
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    55
 public:
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    56
  G1RegionsLargerThanCommitSizeMapper(ReservedSpace rs,
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    57
                                      size_t os_commit_granularity,
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    58
                                      size_t alloc_granularity,
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    59
                                      size_t commit_factor,
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    60
                                      MemoryType type) :
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    61
     G1RegionToSpaceMapper(rs, os_commit_granularity, alloc_granularity, type),
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    62
    _pages_per_region(alloc_granularity / (os_commit_granularity * commit_factor)) {
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    63
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    64
    guarantee(alloc_granularity >= os_commit_granularity, "allocation granularity smaller than commit granularity");
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    65
    _commit_map.resize(rs.size() * commit_factor / alloc_granularity, /* in_resource_area */ false);
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    66
  }
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    67
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    68
  virtual void commit_regions(uintptr_t start_idx, size_t num_regions) {
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    69
    _storage.commit(start_idx * _pages_per_region, num_regions * _pages_per_region);
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    70
    _commit_map.set_range(start_idx, start_idx + num_regions);
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    71
    fire_on_commit(start_idx, num_regions);
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    72
  }
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    73
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    74
  virtual void uncommit_regions(uintptr_t start_idx, size_t num_regions) {
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    75
    _storage.uncommit(start_idx * _pages_per_region, num_regions * _pages_per_region);
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    76
    _commit_map.clear_range(start_idx, start_idx + num_regions);
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    77
  }
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    78
};
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    79
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    80
// G1RegionToSpaceMapper implementation where the region granularity is smaller
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    81
// than the commit granularity.
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    82
// Basically, the contents of one OS page span several regions.
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    83
class G1RegionsSmallerThanCommitSizeMapper : public G1RegionToSpaceMapper {
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    84
 private:
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    85
  class CommitRefcountArray : public G1BiasedMappedArray<uint> {
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    86
   protected:
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    87
     virtual uint default_value() const { return 0; }
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    88
  };
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    89
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    90
  size_t _regions_per_page;
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    91
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    92
  CommitRefcountArray _refcounts;
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    93
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    94
  uintptr_t region_idx_to_page_idx(uint region) const {
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    95
    return region / _regions_per_page;
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    96
  }
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    97
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    98
 public:
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
    99
  G1RegionsSmallerThanCommitSizeMapper(ReservedSpace rs,
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
   100
                                       size_t os_commit_granularity,
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
   101
                                       size_t alloc_granularity,
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
   102
                                       size_t commit_factor,
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
   103
                                       MemoryType type) :
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
   104
     G1RegionToSpaceMapper(rs, os_commit_granularity, alloc_granularity, type),
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
   105
    _regions_per_page((os_commit_granularity * commit_factor) / alloc_granularity), _refcounts() {
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
   106
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
   107
    guarantee((os_commit_granularity * commit_factor) >= alloc_granularity, "allocation granularity smaller than commit granularity");
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
   108
    _refcounts.initialize((HeapWord*)rs.base(), (HeapWord*)(rs.base() + rs.size()), os_commit_granularity);
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
   109
    _commit_map.resize(rs.size() * commit_factor / alloc_granularity, /* in_resource_area */ false);
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
   110
  }
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
   111
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
   112
  virtual void commit_regions(uintptr_t start_idx, size_t num_regions) {
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
   113
    for (uintptr_t i = start_idx; i < start_idx + num_regions; i++) {
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
   114
      assert(!_commit_map.at(i), err_msg("Trying to commit storage at region "INTPTR_FORMAT" that is already committed", i));
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
   115
      uintptr_t idx = region_idx_to_page_idx(i);
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
   116
      uint old_refcount = _refcounts.get_by_index(idx);
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
   117
      if (old_refcount == 0) {
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
   118
        _storage.commit(idx, 1);
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
   119
      }
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
   120
      _refcounts.set_by_index(idx, old_refcount + 1);
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
   121
      _commit_map.set_bit(i);
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
   122
      fire_on_commit(i, 1);
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
   123
    }
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
   124
  }
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
   125
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
   126
  virtual void uncommit_regions(uintptr_t start_idx, size_t num_regions) {
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
   127
    for (uintptr_t i = start_idx; i < start_idx + num_regions; i++) {
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
   128
      assert(_commit_map.at(i), err_msg("Trying to uncommit storage at region "INTPTR_FORMAT" that is not committed", i));
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
   129
      uintptr_t idx = region_idx_to_page_idx(i);
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
   130
      uint old_refcount = _refcounts.get_by_index(idx);
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
   131
      assert(old_refcount > 0, "must be");
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
   132
      if (old_refcount == 1) {
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
   133
        _storage.uncommit(idx, 1);
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
   134
      }
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
   135
      _refcounts.set_by_index(idx, old_refcount - 1);
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
   136
      _commit_map.clear_bit(i);
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
   137
    }
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
   138
  }
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
   139
};
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
   140
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
   141
void G1RegionToSpaceMapper::fire_on_commit(uint start_idx, size_t num_regions) {
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
   142
  if (_listener != NULL) {
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
   143
    _listener->on_commit(start_idx, num_regions);
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
   144
  }
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
   145
}
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
   146
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
   147
G1RegionToSpaceMapper* G1RegionToSpaceMapper::create_mapper(ReservedSpace rs,
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
   148
                                                            size_t os_commit_granularity,
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
   149
                                                            size_t region_granularity,
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
   150
                                                            size_t commit_factor,
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
   151
                                                            MemoryType type) {
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
   152
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
   153
  if (region_granularity >= (os_commit_granularity * commit_factor)) {
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
   154
    return new G1RegionsLargerThanCommitSizeMapper(rs, os_commit_granularity, region_granularity, commit_factor, type);
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
   155
  } else {
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
   156
    return new G1RegionsSmallerThanCommitSizeMapper(rs, os_commit_granularity, region_granularity, commit_factor, type);
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
   157
  }
aba6b01cb988 8038423: G1: Decommit memory within heap
tschatzl
parents:
diff changeset
   158
}