g1PageBasedVirtualSpace.cpp revision 11658:8a5735c11a84
1/*
2 * Copyright (c) 2014, 2016, 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/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
33G1PageBasedVirtualSpace::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
39void 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);
49  guarantee(used_size <= rs.size(),
50            "Used size of reserved space " SIZE_FORMAT " bytes is smaller than reservation at " SIZE_FORMAT " bytes", used_size, rs.size());
51  guarantee(is_size_aligned(rs.size(), page_size),
52            "Expected that the virtual space is size aligned, but " SIZE_FORMAT " is not aligned to page size " SIZE_FORMAT, rs.size(), page_size);
53
54  _low_boundary  = rs.base();
55  _high_boundary = _low_boundary + used_size;
56
57  _special = rs.special();
58  _executable = rs.executable();
59
60  _page_size = page_size;
61
62  vmassert(_committed.size() == 0, "virtual space initialized more than once");
63  BitMap::idx_t size_in_pages = rs.size() / page_size;
64  _committed.initialize(size_in_pages);
65  if (_special) {
66    _dirty.initialize(size_in_pages);
67  }
68
69  _tail_size = used_size % _page_size;
70}
71
72G1PageBasedVirtualSpace::~G1PageBasedVirtualSpace() {
73  // This does not release memory it never reserved.
74  // Caller must release via rs.release();
75  _low_boundary           = NULL;
76  _high_boundary          = NULL;
77  _special                = false;
78  _executable             = false;
79  _page_size              = 0;
80  _tail_size              = 0;
81}
82
83size_t G1PageBasedVirtualSpace::committed_size() const {
84  size_t result = _committed.count_one_bits() * _page_size;
85  // The last page might not be in full.
86  if (is_last_page_partial() && _committed.at(_committed.size() - 1)) {
87    result -= _page_size - _tail_size;
88  }
89  return result;
90}
91
92size_t G1PageBasedVirtualSpace::reserved_size() const {
93  return pointer_delta(_high_boundary, _low_boundary, sizeof(char));
94}
95
96size_t G1PageBasedVirtualSpace::uncommitted_size()  const {
97  return reserved_size() - committed_size();
98}
99
100size_t G1PageBasedVirtualSpace::addr_to_page_index(char* addr) const {
101  return (addr - _low_boundary) / _page_size;
102}
103
104bool G1PageBasedVirtualSpace::is_area_committed(size_t start_page, size_t size_in_pages) const {
105  size_t end_page = start_page + size_in_pages;
106  return _committed.get_next_zero_offset(start_page, end_page) >= end_page;
107}
108
109bool G1PageBasedVirtualSpace::is_area_uncommitted(size_t start_page, size_t size_in_pages) const {
110  size_t end_page = start_page + size_in_pages;
111  return _committed.get_next_one_offset(start_page, end_page) >= end_page;
112}
113
114char* G1PageBasedVirtualSpace::page_start(size_t index) const {
115  return _low_boundary + index * _page_size;
116}
117
118bool G1PageBasedVirtualSpace::is_after_last_page(size_t index) const {
119  guarantee(index <= _committed.size(),
120            "Given boundary page " SIZE_FORMAT " is beyond managed page count " SIZE_FORMAT, index, _committed.size());
121  return index == _committed.size();
122}
123
124void G1PageBasedVirtualSpace::commit_preferred_pages(size_t start, size_t num_pages) {
125  vmassert(num_pages > 0, "No full pages to commit");
126  vmassert(start + num_pages <= _committed.size(),
127           "Tried to commit area from page " SIZE_FORMAT " to page " SIZE_FORMAT " "
128           "that is outside of managed space of " SIZE_FORMAT " pages",
129           start, start + num_pages, _committed.size());
130
131  char* start_addr = page_start(start);
132  size_t size = num_pages * _page_size;
133
134  os::commit_memory_or_exit(start_addr, size, _page_size, _executable,
135                            err_msg("Failed to commit area from " PTR_FORMAT " to " PTR_FORMAT " of length " SIZE_FORMAT ".",
136                            p2i(start_addr), p2i(start_addr + size), size));
137}
138
139void G1PageBasedVirtualSpace::commit_tail() {
140  vmassert(_tail_size > 0, "The size of the tail area must be > 0 when reaching here");
141
142  char* const aligned_end_address = (char*)align_ptr_down(_high_boundary, _page_size);
143  os::commit_memory_or_exit(aligned_end_address, _tail_size, os::vm_page_size(), _executable,
144                            err_msg("Failed to commit tail area from " PTR_FORMAT " to " PTR_FORMAT " of length " SIZE_FORMAT ".",
145                            p2i(aligned_end_address), p2i(_high_boundary), _tail_size));
146}
147
148void G1PageBasedVirtualSpace::commit_internal(size_t start_page, size_t end_page) {
149  guarantee(start_page < end_page,
150            "Given start page " SIZE_FORMAT " is larger or equal to end page " SIZE_FORMAT, start_page, end_page);
151  guarantee(end_page <= _committed.size(),
152            "Given end page " SIZE_FORMAT " is beyond end of managed page amount of " SIZE_FORMAT, end_page, _committed.size());
153
154  size_t pages = end_page - start_page;
155  bool need_to_commit_tail = is_after_last_page(end_page) && is_last_page_partial();
156
157  // If we have to commit some (partial) tail area, decrease the amount of pages to avoid
158  // committing that in the full-page commit code.
159  if (need_to_commit_tail) {
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
172char* G1PageBasedVirtualSpace::bounded_end_addr(size_t end_page) const {
173  return MIN2(_high_boundary, page_start(end_page));
174}
175
176void 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
183bool 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
207void 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
215void 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
230bool G1PageBasedVirtualSpace::contains(const void* p) const {
231  return _low_boundary <= (const char*) p && (const char*) p < _high_boundary;
232}
233
234#ifndef PRODUCT
235void 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
245void G1PageBasedVirtualSpace::print() {
246  print_on(tty);
247}
248#endif
249