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/g1PageBasedVirtualSpace.hpp"
27 #include "oops/markOop.hpp"
28 #include "oops/oop.inline.hpp"
29 #include "runtime/os.inline.hpp"
30 #include "services/memTracker.hpp"
31 #include "utilities/bitMap.inline.hpp"
32
33 G1PageBasedVirtualSpace::G1PageBasedVirtualSpace(ReservedSpace rs, size_t used_size, size_t page_size) :
34 _low_boundary(NULL), _high_boundary(NULL), _committed(), _page_size(0), _special(false),
35 _dirty(), _executable(false) {
36 initialize_with_page_size(rs, used_size, page_size);
37 }
38
39 void G1PageBasedVirtualSpace::initialize_with_page_size(ReservedSpace rs, size_t used_size, size_t page_size) {
40 guarantee(rs.is_reserved(), "Given reserved space must have been reserved already.");
41
42 vmassert(_low_boundary == NULL, "VirtualSpace already initialized");
43 vmassert(page_size > 0, "Page size must be non-zero.");
44
45 guarantee(is_ptr_aligned(rs.base(), page_size),
46 "Reserved space base " PTR_FORMAT " is not aligned to requested page size " SIZE_FORMAT, p2i(rs.base()), page_size);
47 guarantee(is_size_aligned(used_size, os::vm_page_size()),
48 "Given used reserved space size needs to be OS page size aligned (%d bytes) but is " SIZE_FORMAT, os::vm_page_size(), used_size);
160 pages--;
161 }
162
163 if (pages > 0) {
164 commit_preferred_pages(start_page, pages);
165 }
166
167 if (need_to_commit_tail) {
168 commit_tail();
169 }
170 }
171
172 char* G1PageBasedVirtualSpace::bounded_end_addr(size_t end_page) const {
173 return MIN2(_high_boundary, page_start(end_page));
174 }
175
176 void G1PageBasedVirtualSpace::pretouch_internal(size_t start_page, size_t end_page) {
177 guarantee(start_page < end_page,
178 "Given start page " SIZE_FORMAT " is larger or equal to end page " SIZE_FORMAT, start_page, end_page);
179
180 os::pretouch_memory(page_start(start_page), bounded_end_addr(end_page));
181 }
182
183 bool G1PageBasedVirtualSpace::commit(size_t start_page, size_t size_in_pages) {
184 // We need to make sure to commit all pages covered by the given area.
185 guarantee(is_area_uncommitted(start_page, size_in_pages), "Specified area is not uncommitted");
186
187 bool zero_filled = true;
188 size_t end_page = start_page + size_in_pages;
189
190 if (_special) {
191 // Check for dirty pages and update zero_filled if any found.
192 if (_dirty.get_next_one_offset(start_page, end_page) < end_page) {
193 zero_filled = false;
194 _dirty.clear_range(start_page, end_page);
195 }
196 } else {
197 commit_internal(start_page, end_page);
198 }
199 _committed.set_range(start_page, end_page);
200
201 if (AlwaysPreTouch) {
202 pretouch_internal(start_page, end_page);
203 }
204 return zero_filled;
205 }
206
207 void G1PageBasedVirtualSpace::uncommit_internal(size_t start_page, size_t end_page) {
208 guarantee(start_page < end_page,
209 "Given start page " SIZE_FORMAT " is larger or equal to end page " SIZE_FORMAT, start_page, end_page);
210
211 char* start_addr = page_start(start_page);
212 os::uncommit_memory(start_addr, pointer_delta(bounded_end_addr(end_page), start_addr, sizeof(char)));
213 }
214
215 void G1PageBasedVirtualSpace::uncommit(size_t start_page, size_t size_in_pages) {
216 guarantee(is_area_committed(start_page, size_in_pages), "checking");
217
218 size_t end_page = start_page + size_in_pages;
219 if (_special) {
220 // Mark that memory is dirty. If committed again the memory might
221 // need to be cleared explicitly.
222 _dirty.set_range(start_page, end_page);
223 } else {
224 uncommit_internal(start_page, end_page);
225 }
226
227 _committed.clear_range(start_page, end_page);
228 }
229
230 bool G1PageBasedVirtualSpace::contains(const void* p) const {
231 return _low_boundary <= (const char*) p && (const char*) p < _high_boundary;
232 }
233
234 #ifndef PRODUCT
235 void G1PageBasedVirtualSpace::print_on(outputStream* out) {
236 out->print ("Virtual space:");
237 if (_special) out->print(" (pinned in memory)");
238 out->cr();
239 out->print_cr(" - committed: " SIZE_FORMAT, committed_size());
240 out->print_cr(" - reserved: " SIZE_FORMAT, reserved_size());
241 out->print_cr(" - preferred page size: " SIZE_FORMAT, _page_size);
242 out->print_cr(" - [low_b, high_b]: [" PTR_FORMAT ", " PTR_FORMAT "]", p2i(_low_boundary), p2i(_high_boundary));
243 }
244
245 void G1PageBasedVirtualSpace::print() {
246 print_on(tty);
247 }
|
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/g1PageBasedVirtualSpace.hpp"
27 #include "gc/shared/workgroup.hpp"
28 #include "oops/markOop.hpp"
29 #include "oops/oop.inline.hpp"
30 #include "runtime/atomic.hpp"
31 #include "runtime/os.inline.hpp"
32 #include "services/memTracker.hpp"
33 #include "utilities/bitMap.inline.hpp"
34
35 G1PageBasedVirtualSpace::G1PageBasedVirtualSpace(ReservedSpace rs, size_t used_size, size_t page_size) :
36 _low_boundary(NULL), _high_boundary(NULL), _committed(), _page_size(0), _special(false),
37 _dirty(), _executable(false) {
38 initialize_with_page_size(rs, used_size, page_size);
39 }
40
41 void G1PageBasedVirtualSpace::initialize_with_page_size(ReservedSpace rs, size_t used_size, size_t page_size) {
42 guarantee(rs.is_reserved(), "Given reserved space must have been reserved already.");
43
44 vmassert(_low_boundary == NULL, "VirtualSpace already initialized");
45 vmassert(page_size > 0, "Page size must be non-zero.");
46
47 guarantee(is_ptr_aligned(rs.base(), page_size),
48 "Reserved space base " PTR_FORMAT " is not aligned to requested page size " SIZE_FORMAT, p2i(rs.base()), page_size);
49 guarantee(is_size_aligned(used_size, os::vm_page_size()),
50 "Given used reserved space size needs to be OS page size aligned (%d bytes) but is " SIZE_FORMAT, os::vm_page_size(), used_size);
162 pages--;
163 }
164
165 if (pages > 0) {
166 commit_preferred_pages(start_page, pages);
167 }
168
169 if (need_to_commit_tail) {
170 commit_tail();
171 }
172 }
173
174 char* G1PageBasedVirtualSpace::bounded_end_addr(size_t end_page) const {
175 return MIN2(_high_boundary, page_start(end_page));
176 }
177
178 void G1PageBasedVirtualSpace::pretouch_internal(size_t start_page, size_t end_page) {
179 guarantee(start_page < end_page,
180 "Given start page " SIZE_FORMAT " is larger or equal to end page " SIZE_FORMAT, start_page, end_page);
181
182 os::pretouch_memory(page_start(start_page), bounded_end_addr(end_page), _page_size);
183 }
184
185 bool G1PageBasedVirtualSpace::commit(size_t start_page, size_t size_in_pages) {
186 // We need to make sure to commit all pages covered by the given area.
187 guarantee(is_area_uncommitted(start_page, size_in_pages), "Specified area is not uncommitted");
188
189 bool zero_filled = true;
190 size_t end_page = start_page + size_in_pages;
191
192 if (_special) {
193 // Check for dirty pages and update zero_filled if any found.
194 if (_dirty.get_next_one_offset(start_page, end_page) < end_page) {
195 zero_filled = false;
196 _dirty.clear_range(start_page, end_page);
197 }
198 } else {
199 commit_internal(start_page, end_page);
200 }
201 _committed.set_range(start_page, end_page);
202
203 return zero_filled;
204 }
205
206 void G1PageBasedVirtualSpace::uncommit_internal(size_t start_page, size_t end_page) {
207 guarantee(start_page < end_page,
208 "Given start page " SIZE_FORMAT " is larger or equal to end page " SIZE_FORMAT, start_page, end_page);
209
210 char* start_addr = page_start(start_page);
211 os::uncommit_memory(start_addr, pointer_delta(bounded_end_addr(end_page), start_addr, sizeof(char)));
212 }
213
214 void G1PageBasedVirtualSpace::uncommit(size_t start_page, size_t size_in_pages) {
215 guarantee(is_area_committed(start_page, size_in_pages), "checking");
216
217 size_t end_page = start_page + size_in_pages;
218 if (_special) {
219 // Mark that memory is dirty. If committed again the memory might
220 // need to be cleared explicitly.
221 _dirty.set_range(start_page, end_page);
222 } else {
223 uncommit_internal(start_page, end_page);
224 }
225
226 _committed.clear_range(start_page, end_page);
227 }
228
229 class G1PretouchTask : public AbstractGangTask {
230 private:
231 char* volatile _cur_addr;
232 char* const _start_addr;
233 char* const _end_addr;
234 size_t const _page_size;
235 public:
236 G1PretouchTask(char* start_address, char* end_address, size_t page_size) :
237 AbstractGangTask("G1 PreTouch",
238 Universe::is_fully_initialized() ? GCId::current_raw() :
239 // During VM initialization there is
240 // no GC cycle that this task can be
241 // associated with.
242 GCId::undefined()),
243 _cur_addr(start_address),
244 _start_addr(start_address),
245 _end_addr(end_address),
246 _page_size(page_size) {
247 }
248
249 virtual void work(uint worker_id) {
250 size_t const actual_chunk_size = MAX2(chunk_size(), _page_size);
251 while (true) {
252 char* touch_addr = (char*)Atomic::add_ptr((intptr_t)actual_chunk_size, (volatile void*) &_cur_addr) - actual_chunk_size;
253 if (touch_addr < _start_addr || touch_addr >= _end_addr) {
254 break;
255 }
256 char* end_addr = touch_addr + MIN2(actual_chunk_size, pointer_delta(_end_addr, touch_addr, sizeof(char)));
257 os::pretouch_memory(touch_addr, end_addr, _page_size);
258 }
259 }
260
261 static size_t chunk_size() { return PreTouchParallelChunkSize; }
262 };
263
264 void G1PageBasedVirtualSpace::pretouch(size_t start_page, size_t size_in_pages, WorkGang* pretouch_gang) {
265 guarantee(pretouch_gang != NULL, "No pretouch gang specified.");
266
267 size_t num_chunks = MAX2((size_t)1, size_in_pages * _page_size / MAX2(G1PretouchTask::chunk_size(), _page_size));
268
269 uint num_workers = MIN2((uint)num_chunks, pretouch_gang->active_workers());
270 G1PretouchTask cl(page_start(start_page), bounded_end_addr(start_page + size_in_pages), _page_size);
271 log_debug(gc, heap)("Running %s with %u workers for " SIZE_FORMAT " work units pre-touching " SIZE_FORMAT "B.",
272 cl.name(), num_workers, num_chunks, size_in_pages * _page_size);
273 pretouch_gang->run_task(&cl, num_workers);
274 }
275
276 bool G1PageBasedVirtualSpace::contains(const void* p) const {
277 return _low_boundary <= (const char*) p && (const char*) p < _high_boundary;
278 }
279
280 #ifndef PRODUCT
281 void G1PageBasedVirtualSpace::print_on(outputStream* out) {
282 out->print ("Virtual space:");
283 if (_special) out->print(" (pinned in memory)");
284 out->cr();
285 out->print_cr(" - committed: " SIZE_FORMAT, committed_size());
286 out->print_cr(" - reserved: " SIZE_FORMAT, reserved_size());
287 out->print_cr(" - preferred page size: " SIZE_FORMAT, _page_size);
288 out->print_cr(" - [low_b, high_b]: [" PTR_FORMAT ", " PTR_FORMAT "]", p2i(_low_boundary), p2i(_high_boundary));
289 }
290
291 void G1PageBasedVirtualSpace::print() {
292 print_on(tty);
293 }
|