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