1 /*
2 * Copyright (c) 2001, 2019, 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/g1/g1BiasedArray.hpp"
27 #include "gc/g1/g1NUMA.hpp"
28 #include "gc/g1/g1RegionToSpaceMapper.hpp"
29 #include "memory/allocation.inline.hpp"
30 #include "memory/virtualspace.hpp"
31 #include "runtime/mutexLocker.hpp"
32 #include "services/memTracker.hpp"
33 #include "utilities/align.hpp"
34 #include "utilities/bitMap.inline.hpp"
35 #include "utilities/powerOfTwo.hpp"
36
G1RegionToSpaceMapper(ReservedSpace rs,size_t used_size,size_t page_size,size_t region_granularity,size_t commit_factor,MEMFLAGS type)37 G1RegionToSpaceMapper::G1RegionToSpaceMapper(ReservedSpace rs,
38 size_t used_size,
39 size_t page_size,
40 size_t region_granularity,
41 size_t commit_factor,
42 MEMFLAGS type) :
43 _listener(NULL),
44 _storage(rs, used_size, page_size),
45 _region_granularity(region_granularity),
46 _region_commit_map(rs.size() * commit_factor / region_granularity, mtGC),
47 _memory_type(type) {
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 // Used to manually signal a mapper to handle a set of regions as committed.
55 // Setting the 'zero_filled' parameter to false signals the mapper that the
56 // regions have not been cleared by the OS and that they need to be clear
57 // explicitly.
signal_mapping_changed(uint start_idx,size_t num_regions)58 void G1RegionToSpaceMapper::signal_mapping_changed(uint start_idx, size_t num_regions) {
59 fire_on_commit(start_idx, num_regions, false);
60 }
61
62 // G1RegionToSpaceMapper implementation where the region granularity is larger than
63 // or the same as the commit granularity.
64 // Basically, the space corresponding to one region region spans several OS pages.
65 class G1RegionsLargerThanCommitSizeMapper : public G1RegionToSpaceMapper {
66 private:
67 size_t _pages_per_region;
68
69 public:
G1RegionsLargerThanCommitSizeMapper(ReservedSpace rs,size_t actual_size,size_t page_size,size_t alloc_granularity,size_t commit_factor,MEMFLAGS type)70 G1RegionsLargerThanCommitSizeMapper(ReservedSpace rs,
71 size_t actual_size,
72 size_t page_size,
73 size_t alloc_granularity,
74 size_t commit_factor,
75 MEMFLAGS type) :
76 G1RegionToSpaceMapper(rs, actual_size, page_size, alloc_granularity, commit_factor, type),
77 _pages_per_region(alloc_granularity / (page_size * commit_factor)) {
78
79 guarantee(alloc_granularity >= page_size, "allocation granularity smaller than commit granularity");
80 }
81
is_range_committed(uint start_idx,size_t num_regions)82 bool is_range_committed(uint start_idx, size_t num_regions) {
83 BitMap::idx_t end = start_idx + num_regions;
84 return _region_commit_map.get_next_zero_offset(start_idx, end) == end;
85 }
86
is_range_uncommitted(uint start_idx,size_t num_regions)87 bool is_range_uncommitted(uint start_idx, size_t num_regions) {
88 BitMap::idx_t end = start_idx + num_regions;
89 return _region_commit_map.get_next_one_offset(start_idx, end) == end;
90 }
91
commit_regions(uint start_idx,size_t num_regions,WorkGang * pretouch_gang)92 virtual void commit_regions(uint start_idx, size_t num_regions, WorkGang* pretouch_gang) {
93 guarantee(is_range_uncommitted(start_idx, num_regions),
94 "Range not uncommitted, start: %u, num_regions: " SIZE_FORMAT,
95 start_idx, num_regions);
96
97 const size_t start_page = (size_t)start_idx * _pages_per_region;
98 const size_t size_in_pages = num_regions * _pages_per_region;
99 bool zero_filled = _storage.commit(start_page, size_in_pages);
100 if (_memory_type == mtJavaHeap) {
101 for (uint region_index = start_idx; region_index < start_idx + num_regions; region_index++ ) {
102 void* address = _storage.page_start(region_index * _pages_per_region);
103 size_t size_in_bytes = _storage.page_size() * _pages_per_region;
104 G1NUMA::numa()->request_memory_on_node(address, size_in_bytes, region_index);
105 }
106 }
107 if (AlwaysPreTouch) {
108 _storage.pretouch(start_page, size_in_pages, pretouch_gang);
109 }
110 _region_commit_map.par_set_range(start_idx, start_idx + num_regions, BitMap::unknown_range);
111 fire_on_commit(start_idx, num_regions, zero_filled);
112 }
113
uncommit_regions(uint start_idx,size_t num_regions)114 virtual void uncommit_regions(uint start_idx, size_t num_regions) {
115 guarantee(is_range_committed(start_idx, num_regions),
116 "Range not committed, start: %u, num_regions: " SIZE_FORMAT,
117 start_idx, num_regions);
118
119 _storage.uncommit((size_t)start_idx * _pages_per_region, num_regions * _pages_per_region);
120 _region_commit_map.par_clear_range(start_idx, start_idx + num_regions, BitMap::unknown_range);
121 }
122 };
123
124 // G1RegionToSpaceMapper implementation where the region granularity is smaller
125 // than the commit granularity.
126 // Basically, the contents of one OS page span several regions.
127 class G1RegionsSmallerThanCommitSizeMapper : public G1RegionToSpaceMapper {
128 size_t _regions_per_page;
129 // Lock to prevent bitmap updates and the actual underlying
130 // commit to get out of order. This can happen in the cases
131 // where one thread is expanding the heap during a humongous
132 // allocation and at the same time the service thread is
133 // doing uncommit. These operations will not operate on the
134 // same regions, but they might operate on regions sharing
135 // an underlying OS page. So we need to make sure that both
136 // those resources are in sync:
137 // - G1RegionToSpaceMapper::_region_commit_map;
138 // - G1PageBasedVirtualSpace::_committed (_storage.commit())
139 Mutex _lock;
140
region_idx_to_page_idx(uint region_idx) const141 size_t region_idx_to_page_idx(uint region_idx) const {
142 return region_idx / _regions_per_page;
143 }
144
is_page_committed(size_t page_idx)145 bool is_page_committed(size_t page_idx) {
146 size_t region = page_idx * _regions_per_page;
147 size_t region_limit = region + _regions_per_page;
148 // Committed if there is a bit set in the range.
149 return _region_commit_map.get_next_one_offset(region, region_limit) != region_limit;
150 }
151
numa_request_on_node(size_t page_idx)152 void numa_request_on_node(size_t page_idx) {
153 if (_memory_type == mtJavaHeap) {
154 uint region = (uint)(page_idx * _regions_per_page);
155 void* address = _storage.page_start(page_idx);
156 size_t size_in_bytes = _storage.page_size();
157 G1NUMA::numa()->request_memory_on_node(address, size_in_bytes, region);
158 }
159 }
160
161 public:
G1RegionsSmallerThanCommitSizeMapper(ReservedSpace rs,size_t actual_size,size_t page_size,size_t alloc_granularity,size_t commit_factor,MEMFLAGS type)162 G1RegionsSmallerThanCommitSizeMapper(ReservedSpace rs,
163 size_t actual_size,
164 size_t page_size,
165 size_t alloc_granularity,
166 size_t commit_factor,
167 MEMFLAGS type) :
168 G1RegionToSpaceMapper(rs, actual_size, page_size, alloc_granularity, commit_factor, type),
169 _regions_per_page((page_size * commit_factor) / alloc_granularity),
170 _lock(Mutex::leaf, "G1 mapper lock", true, Mutex::_safepoint_check_never) {
171
172 guarantee((page_size * commit_factor) >= alloc_granularity, "allocation granularity smaller than commit granularity");
173 }
174
commit_regions(uint start_idx,size_t num_regions,WorkGang * pretouch_gang)175 virtual void commit_regions(uint start_idx, size_t num_regions, WorkGang* pretouch_gang) {
176 uint region_limit = (uint)(start_idx + num_regions);
177 assert(num_regions > 0, "Must commit at least one region");
178 assert(_region_commit_map.get_next_one_offset(start_idx, region_limit) == region_limit,
179 "Should be no committed regions in the range [%u, %u)", start_idx, region_limit);
180
181 size_t const NoPage = ~(size_t)0;
182
183 size_t first_committed = NoPage;
184 size_t num_committed = 0;
185
186 size_t start_page = region_idx_to_page_idx(start_idx);
187 size_t end_page = region_idx_to_page_idx(region_limit - 1);
188
189 bool all_zero_filled = true;
190
191 // Concurrent operations might operate on regions sharing the same
192 // underlying OS page. See lock declaration for more details.
193 {
194 MutexLocker ml(&_lock, Mutex::_no_safepoint_check_flag);
195 for (size_t page = start_page; page <= end_page; page++) {
196 if (!is_page_committed(page)) {
197 // Page not committed.
198 if (num_committed == 0) {
199 first_committed = page;
200 }
201 num_committed++;
202
203 if (!_storage.commit(page, 1)) {
204 // Found dirty region during commit.
205 all_zero_filled = false;
206 }
207
208 // Move memory to correct NUMA node for the heap.
209 numa_request_on_node(page);
210 } else {
211 // Page already committed.
212 all_zero_filled = false;
213 }
214 }
215
216 // Update the commit map for the given range. Not using the par_set_range
217 // since updates to _region_commit_map for this mapper is protected by _lock.
218 _region_commit_map.set_range(start_idx, region_limit, BitMap::unknown_range);
219 }
220
221 if (AlwaysPreTouch && num_committed > 0) {
222 _storage.pretouch(first_committed, num_committed, pretouch_gang);
223 }
224
225 fire_on_commit(start_idx, num_regions, all_zero_filled);
226 }
227
uncommit_regions(uint start_idx,size_t num_regions)228 virtual void uncommit_regions(uint start_idx, size_t num_regions) {
229 uint region_limit = (uint)(start_idx + num_regions);
230 assert(num_regions > 0, "Must uncommit at least one region");
231 assert(_region_commit_map.get_next_zero_offset(start_idx, region_limit) == region_limit,
232 "Should only be committed regions in the range [%u, %u)", start_idx, region_limit);
233
234 size_t start_page = region_idx_to_page_idx(start_idx);
235 size_t end_page = region_idx_to_page_idx(region_limit - 1);
236
237 // Concurrent operations might operate on regions sharing the same
238 // underlying OS page. See lock declaration for more details.
239 MutexLocker ml(&_lock, Mutex::_no_safepoint_check_flag);
240 // Clear commit map for the given range. Not using the par_clear_range since
241 // updates to _region_commit_map for this mapper is protected by _lock.
242 _region_commit_map.clear_range(start_idx, region_limit, BitMap::unknown_range);
243
244 for (size_t page = start_page; page <= end_page; page++) {
245 // We know all pages were committed before clearing the map. If the
246 // the page is still marked as committed after the clear we should
247 // not uncommit it.
248 if (!is_page_committed(page)) {
249 _storage.uncommit(page, 1);
250 }
251 }
252 }
253 };
254
fire_on_commit(uint start_idx,size_t num_regions,bool zero_filled)255 void G1RegionToSpaceMapper::fire_on_commit(uint start_idx, size_t num_regions, bool zero_filled) {
256 if (_listener != NULL) {
257 _listener->on_commit(start_idx, num_regions, zero_filled);
258 }
259 }
260
create_mapper(ReservedSpace rs,size_t actual_size,size_t page_size,size_t region_granularity,size_t commit_factor,MEMFLAGS type)261 G1RegionToSpaceMapper* G1RegionToSpaceMapper::create_mapper(ReservedSpace rs,
262 size_t actual_size,
263 size_t page_size,
264 size_t region_granularity,
265 size_t commit_factor,
266 MEMFLAGS type) {
267 if (region_granularity >= (page_size * commit_factor)) {
268 return new G1RegionsLargerThanCommitSizeMapper(rs, actual_size, page_size, region_granularity, commit_factor, type);
269 } else {
270 return new G1RegionsSmallerThanCommitSizeMapper(rs, actual_size, page_size, region_granularity, commit_factor, type);
271 }
272 }
273