27 #include "oops/markOop.hpp"
28 #include "oops/oop.inline.hpp"
29 #include "services/memTracker.hpp"
30 #ifdef TARGET_OS_FAMILY_linux
31 # include "os_linux.inline.hpp"
32 #endif
33 #ifdef TARGET_OS_FAMILY_solaris
34 # include "os_solaris.inline.hpp"
35 #endif
36 #ifdef TARGET_OS_FAMILY_windows
37 # include "os_windows.inline.hpp"
38 #endif
39 #ifdef TARGET_OS_FAMILY_aix
40 # include "os_aix.inline.hpp"
41 #endif
42 #ifdef TARGET_OS_FAMILY_bsd
43 # include "os_bsd.inline.hpp"
44 #endif
45 #include "utilities/bitMap.inline.hpp"
46
47 G1PageBasedVirtualSpace::G1PageBasedVirtualSpace() : _low_boundary(NULL),
48 _high_boundary(NULL), _committed(), _commit_size(0), _special(false),
49 _dirty(), _executable(false) {
50 }
51
52 bool G1PageBasedVirtualSpace::initialize_with_granularity(ReservedSpace rs, size_t actual_size, size_t commit_size) {
53 if (!rs.is_reserved()) {
54 return false; // Allocation failed.
55 }
56 assert(_low_boundary == NULL, "VirtualSpace already initialized");
57 assert(commit_size > 0, "Granularity must be non-zero.");
58
59 guarantee(is_ptr_aligned(rs.base(), commit_size),
60 err_msg("Reserved space base " PTR_FORMAT" is not aligned to requested page size " SIZE_FORMAT, p2i(rs.base()), commit_size));
61 guarantee(is_size_aligned(actual_size, os::vm_page_size()),
62 err_msg("Given actual reserved space size needs to be OS page size aligned (%d bytes) but is " SIZE_FORMAT, os::vm_page_size(), actual_size));
63 guarantee(actual_size <= rs.size(),
64 err_msg("Actual size of reserved space " SIZE_FORMAT" bytes is smaller than reservation at " SIZE_FORMAT" bytes", actual_size, rs.size()));
65
66 _low_boundary = rs.base();
67 _high_boundary = _low_boundary + actual_size;
68
69 _special = rs.special();
70 _executable = rs.executable();
71
72 _commit_size = commit_size;
73
74 assert(_committed.size() == 0, "virtual space initialized more than once");
75 BitMap::idx_t size_in_commit_pages = round_to(rs.size(), commit_size) / commit_size;
76 _committed.resize(size_in_commit_pages, /* in_resource_area */ false);
77 if (_special) {
78 _dirty.resize(size_in_commit_pages, /* in_resource_area */ false);
79 }
80
81 return true;
82 }
83
84 G1PageBasedVirtualSpace::~G1PageBasedVirtualSpace() {
85 release();
86 }
87
88 void G1PageBasedVirtualSpace::release() {
89 // This does not release memory it never reserved.
90 // Caller must release via rs.release();
91 _low_boundary = NULL;
92 _high_boundary = NULL;
93 _special = false;
94 _executable = false;
95 _commit_size = 0;
96 _committed.resize(0, false);
97 _dirty.resize(0, false);
98 }
99
100 size_t G1PageBasedVirtualSpace::committed_size() const {
101 size_t result = _committed.count_one_bits() * _commit_size;
102 // The last page might not be in full.
103 if (_committed.at(_committed.size()-1)) {
104 result -= pointer_delta((char*)align_ptr_up(_high_boundary, _commit_size), _high_boundary, sizeof(char));
105 }
106 return result;
107 }
108
109 size_t G1PageBasedVirtualSpace::reserved_size() const {
110 return pointer_delta(_high_boundary, _low_boundary, sizeof(char));
111 }
112
113 size_t G1PageBasedVirtualSpace::uncommitted_size() const {
114 return reserved_size() - committed_size();
115 }
116
117 uintptr_t G1PageBasedVirtualSpace::addr_to_page_index(char* addr) const {
118 return (addr - _low_boundary) / _commit_size;
119 }
120
121 bool G1PageBasedVirtualSpace::is_area_committed(uintptr_t start, size_t size_in_pages) const {
122 uintptr_t end = start + size_in_pages;
123 return _committed.get_next_zero_offset(start, end) >= end;
124 }
125
126 bool G1PageBasedVirtualSpace::is_area_uncommitted(uintptr_t start, size_t size_in_pages) const {
127 uintptr_t end = start + size_in_pages;
128 return _committed.get_next_one_offset(start, end) >= end;
129 }
130
131 char* G1PageBasedVirtualSpace::page_start(uintptr_t index) {
132 return _low_boundary + index * _commit_size;
133 }
134
135 char* G1PageBasedVirtualSpace::page_end(uintptr_t index) {
136 guarantee(index < _committed.size(), "invariant");
137 if (index != (_committed.size() - 1)) {
138 return page_start(index + 1);
139 }
140 return _high_boundary;
141 }
142
143 void G1PageBasedVirtualSpace::commit_internal(char* start, char* end) {
144 guarantee(start >= _low_boundary && start < _high_boundary,
145 err_msg("Start address " PTR_FORMAT" is outside of reserved space.", p2i(start)));
146 guarantee(is_ptr_aligned(start, _commit_size),
147 err_msg("Start address should be aligned to commit size " SIZE_FORMAT" but got " PTR_FORMAT".",
148 _commit_size, p2i(start)));
149
150 guarantee(end >= _low_boundary && end <= _high_boundary,
151 err_msg("End address " PTR_FORMAT" is outside of reserved space.", p2i(end)));
152 bool is_high_aligned_to_commit_size = is_ptr_aligned(_high_boundary, _commit_size);
153 guarantee(is_ptr_aligned(end, is_high_aligned_to_commit_size ? _commit_size : os::vm_page_size()),
154 err_msg("End address should be aligned to page size " SIZE_FORMAT" but got " PTR_FORMAT".",
155 is_high_aligned_to_commit_size ? _commit_size : os::vm_page_size(),
156 p2i(end)));
157 // First try to commit in commit_size chunks.
158 char* const aligned_end_address = (char*)align_ptr_down(end, _commit_size);
159 size_t const size = pointer_delta(aligned_end_address, start, sizeof(char));
160 if (size != 0) {
161 os::commit_memory_or_exit(start, size, _commit_size, _executable,
162 err_msg("Failed to commit area from " PTR_FORMAT" to " PTR_FORMAT" of length " SIZE_FORMAT".",
163 p2i(start), p2i(aligned_end_address), size));
164 }
165 // Finally, commit any remaining tail.
166 if (end != aligned_end_address) {
167 size_t const tail_size = pointer_delta(end, aligned_end_address, sizeof(char));
168 guarantee(tail_size < _commit_size,
169 err_msg("Remaining size " SIZE_FORMAT "must be smaller than commit size of " SIZE_FORMAT, tail_size, _commit_size));
170 os::commit_memory_or_exit(start, tail_size, _executable,
171 err_msg("Failed to commit remainder pages from " PTR_FORMAT" to " PTR_FORMAT" of length "SIZE_FORMAT".",
172 p2i(aligned_end_address), p2i(end), tail_size));
173 }
174 }
175
176 bool G1PageBasedVirtualSpace::commit(uintptr_t start, size_t size_in_pages) {
177 // We need to make sure to commit all pages covered by the given area.
178 guarantee(is_area_uncommitted(start, size_in_pages), "Specified area is not uncommitted");
179
180 bool zero_filled = true;
181 uintptr_t end = start + size_in_pages;
182
183 if (_special) {
184 // Check for dirty pages and update zero_filled if any found.
185 if (_dirty.get_next_one_offset(start,end) < end) {
186 zero_filled = false;
187 _dirty.clear_range(start, end);
188 }
189 } else {
190 commit_internal(page_start(start), page_end(end - 1));
191 }
192 _committed.set_range(start, end);
193
194 if (AlwaysPreTouch) {
195 os::pretouch_memory(page_start(start), page_end(end - 1));
196 }
197 return zero_filled;
198 }
199
200 void G1PageBasedVirtualSpace::uncommit_internal(char* start, char* end) {
201 os::uncommit_memory(start, pointer_delta(end, start, sizeof(char)));
202 }
203
204 void G1PageBasedVirtualSpace::uncommit(uintptr_t start, size_t size_in_pages) {
205 guarantee(is_area_committed(start, size_in_pages), "checking");
206
207 uintptr_t end = start + size_in_pages;
208 if (_special) {
209 // Mark that memory is dirty. If committed again the memory might
210 // need to be cleared explicitly.
211 _dirty.set_range(start, end);
212 } else {
213 uncommit_internal(page_start(start), page_end(end - 1));
214 }
215
216 _committed.clear_range(start, end);
217 }
218
219 bool G1PageBasedVirtualSpace::contains(const void* p) const {
220 return _low_boundary <= (const char*) p && (const char*) p < _high_boundary;
221 }
222
223 #ifndef PRODUCT
224 void G1PageBasedVirtualSpace::print_on(outputStream* out) {
225 out->print ("Virtual space:");
226 if (_special) out->print(" (pinned in memory)");
227 out->cr();
228 out->print_cr(" - committed: " SIZE_FORMAT, committed_size());
229 out->print_cr(" - reserved: " SIZE_FORMAT, reserved_size());
230 out->print_cr(" - [low_b, high_b]: [" PTR_FORMAT", " PTR_FORMAT"]", p2i(_low_boundary), p2i(_high_boundary));
231 }
232
233 void G1PageBasedVirtualSpace::print() {
234 print_on(tty);
235 }
236 #endif
|
27 #include "oops/markOop.hpp"
28 #include "oops/oop.inline.hpp"
29 #include "services/memTracker.hpp"
30 #ifdef TARGET_OS_FAMILY_linux
31 # include "os_linux.inline.hpp"
32 #endif
33 #ifdef TARGET_OS_FAMILY_solaris
34 # include "os_solaris.inline.hpp"
35 #endif
36 #ifdef TARGET_OS_FAMILY_windows
37 # include "os_windows.inline.hpp"
38 #endif
39 #ifdef TARGET_OS_FAMILY_aix
40 # include "os_aix.inline.hpp"
41 #endif
42 #ifdef TARGET_OS_FAMILY_bsd
43 # include "os_bsd.inline.hpp"
44 #endif
45 #include "utilities/bitMap.inline.hpp"
46
47 G1PageBasedVirtualSpace::G1PageBasedVirtualSpace(ReservedSpace rs, size_t used_size, size_t page_size) :
48 _low_boundary(NULL), _high_boundary(NULL), _committed(), _page_size(0), _special(false),
49 _dirty(), _executable(false) {
50 initialize_with_page_size(rs, used_size, page_size);
51 }
52
53 void G1PageBasedVirtualSpace::initialize_with_page_size(ReservedSpace rs, size_t used_size, size_t page_size) {
54 guarantee(rs.is_reserved(), "Given reserved space must have been reserved already.");
55
56 vmassert(_low_boundary == NULL, "VirtualSpace already initialized");
57 vmassert(page_size > 0, "Page size must be non-zero.");
58
59 guarantee(is_ptr_aligned(rs.base(), page_size),
60 err_msg("Reserved space base " PTR_FORMAT " is not aligned to requested page size " SIZE_FORMAT, p2i(rs.base()), page_size));
61 guarantee(is_size_aligned(used_size, os::vm_page_size()),
62 err_msg("Given used reserved space size needs to be OS page size aligned (%d bytes) but is " SIZE_FORMAT, os::vm_page_size(), used_size));
63 guarantee(used_size <= rs.size(),
64 err_msg("Used size of reserved space " SIZE_FORMAT " bytes is smaller than reservation at " SIZE_FORMAT " bytes", used_size, rs.size()));
65 guarantee(is_size_aligned(rs.size(), page_size),
66 err_msg("Expected that the virtual space is size aligned, but " SIZE_FORMAT " is not aligned to page size " SIZE_FORMAT, rs.size(), page_size));
67
68 _low_boundary = rs.base();
69 _high_boundary = _low_boundary + used_size;
70
71 _special = rs.special();
72 _executable = rs.executable();
73
74 _page_size = page_size;
75
76 vmassert(_committed.size() == 0, "virtual space initialized more than once");
77 BitMap::idx_t size_in_pages = rs.size() / page_size;
78 _committed.resize(size_in_pages, /* in_resource_area */ false);
79 if (_special) {
80 _dirty.resize(size_in_pages, /* in_resource_area */ false);
81 }
82 }
83
84 G1PageBasedVirtualSpace::~G1PageBasedVirtualSpace() {
85 release();
86 }
87
88 void G1PageBasedVirtualSpace::release() {
89 // This does not release memory it never reserved.
90 // Caller must release via rs.release();
91 _low_boundary = NULL;
92 _high_boundary = NULL;
93 _special = false;
94 _executable = false;
95 _page_size = 0;
96 _committed.resize(0, false);
97 _dirty.resize(0, false);
98 }
99
100 size_t G1PageBasedVirtualSpace::committed_size() const {
101 size_t result = _committed.count_one_bits() * _page_size;
102 // The last page might not be in full.
103 if (_committed.at(_committed.size() - 1)) {
104 char* aligned_high_boundary = (char*)align_ptr_up(_high_boundary, _page_size);
105 result -= pointer_delta(aligned_high_boundary, _high_boundary, sizeof(char));
106 }
107 return result;
108 }
109
110 size_t G1PageBasedVirtualSpace::reserved_size() const {
111 return pointer_delta(_high_boundary, _low_boundary, sizeof(char));
112 }
113
114 size_t G1PageBasedVirtualSpace::uncommitted_size() const {
115 return reserved_size() - committed_size();
116 }
117
118 size_t G1PageBasedVirtualSpace::addr_to_page_index(char* addr) const {
119 return (addr - _low_boundary) / _page_size;
120 }
121
122 bool G1PageBasedVirtualSpace::is_area_committed(size_t start, size_t size_in_pages) const {
123 size_t end = start + size_in_pages;
124 return _committed.get_next_zero_offset(start, end) >= end;
125 }
126
127 bool G1PageBasedVirtualSpace::is_area_uncommitted(size_t start, size_t size_in_pages) const {
128 size_t end = start + size_in_pages;
129 return _committed.get_next_one_offset(start, end) >= end;
130 }
131
132 char* G1PageBasedVirtualSpace::page_start(size_t index) {
133 return _low_boundary + index * _page_size;
134 }
135
136 bool G1PageBasedVirtualSpace::is_after_last_page(size_t index) {
137 guarantee(index <= _committed.size(),
138 err_msg("Given boundary page " SIZE_FORMAT " is beyond managed page count " SIZE_FORMAT, index, _committed.size()));
139 return index == _committed.size();
140 }
141
142 void G1PageBasedVirtualSpace::commit_full_pages(size_t start, size_t num_pages) {
143 vmassert(num_pages > 0, "No full pages to commit");
144 vmassert(start + num_pages <= _committed.size(),
145 err_msg("Tried to commit area from page " SIZE_FORMAT " to page " SIZE_FORMAT " "
146 "that is outside of managed space of " SIZE_FORMAT " pages",
147 start, start + num_pages, _committed.size()));
148
149 char* start_addr = page_start(start);
150 size_t size = num_pages * _page_size;
151
152 os::commit_memory_or_exit(start_addr, size, _page_size, _executable,
153 err_msg("Failed to commit area from " PTR_FORMAT " to " PTR_FORMAT " of length " SIZE_FORMAT ".",
154 p2i(start_addr), p2i(start_addr + size), size));
155 }
156
157 void G1PageBasedVirtualSpace::commit_tail() {
158 char* const aligned_end_address = (char*)align_ptr_down(_high_boundary, _page_size);
159 size_t const tail_size = pointer_delta(_high_boundary, aligned_end_address, sizeof(char));
160
161 os::commit_memory_or_exit(aligned_end_address, tail_size, os::vm_page_size(), _executable,
162 err_msg("Failed to commit tail area from " PTR_FORMAT " to " PTR_FORMAT " of length " SIZE_FORMAT ".",
163 p2i(aligned_end_address), p2i(_high_boundary), tail_size));
164 }
165
166 void G1PageBasedVirtualSpace::commit_internal(size_t start_page, size_t end_page) {
167 guarantee(start_page < end_page,
168 err_msg("Given start page " SIZE_FORMAT " is larger or equal to end page " SIZE_FORMAT, start_page, end_page));
169 guarantee(end_page <= _committed.size(),
170 err_msg("Given end page " SIZE_FORMAT " is beyond end of managed page amount of " SIZE_FORMAT, end_page, _committed.size()));
171
172 size_t pages = end_page - start_page;
173 bool need_to_commit_tail = is_after_last_page(end_page) && is_last_page_partial();
174
175 // If we have to commit some (partial) tail area, decrease the amount of pages to avoid
176 // committing that in the full-page commit code.
177 if (need_to_commit_tail) {
178 pages--;
179 }
180
181 if (pages > 0) {
182 commit_full_pages(start_page, pages);
183 }
184
185 if (need_to_commit_tail) {
186 commit_tail();
187 }
188 }
189
190 char* G1PageBasedVirtualSpace::bounded_end_addr(size_t end_page) {
191 return MIN2(_high_boundary, page_start(end_page));
192 }
193
194 void G1PageBasedVirtualSpace::pretouch_internal(size_t start_page, size_t end_page) {
195 guarantee(start_page < end_page,
196 err_msg("Given start page " SIZE_FORMAT " is larger or equal to end page " SIZE_FORMAT, start_page, end_page));
197
198 os::pretouch_memory(page_start(start_page), bounded_end_addr(end_page));
199 }
200
201 bool G1PageBasedVirtualSpace::commit(size_t start, size_t size_in_pages) {
202 // We need to make sure to commit all pages covered by the given area.
203 guarantee(is_area_uncommitted(start, size_in_pages), "Specified area is not uncommitted");
204
205 bool zero_filled = true;
206 size_t end = start + size_in_pages;
207
208 if (_special) {
209 // Check for dirty pages and update zero_filled if any found.
210 if (_dirty.get_next_one_offset(start, end) < end) {
211 zero_filled = false;
212 _dirty.clear_range(start, end);
213 }
214 } else {
215 commit_internal(start, end);
216 }
217 _committed.set_range(start, end);
218
219 if (AlwaysPreTouch) {
220 pretouch_internal(start, end);
221 }
222 return zero_filled;
223 }
224
225 void G1PageBasedVirtualSpace::uncommit_internal(size_t start_page, size_t end_page) {
226 guarantee(start_page < end_page,
227 err_msg("Given start page " SIZE_FORMAT " is larger or equal to end page " SIZE_FORMAT, start_page, end_page));
228
229 char* start_addr = page_start(start_page);
230 os::uncommit_memory(start_addr, pointer_delta(bounded_end_addr(end_page), start_addr, sizeof(char)));
231 }
232
233 void G1PageBasedVirtualSpace::uncommit(size_t start, size_t size_in_pages) {
234 guarantee(is_area_committed(start, size_in_pages), "checking");
235
236 size_t end = start + size_in_pages;
237 if (_special) {
238 // Mark that memory is dirty. If committed again the memory might
239 // need to be cleared explicitly.
240 _dirty.set_range(start, end);
241 } else {
242 uncommit_internal(start, end);
243 }
244
245 _committed.clear_range(start, end);
246 }
247
248 bool G1PageBasedVirtualSpace::contains(const void* p) const {
249 return _low_boundary <= (const char*) p && (const char*) p < _high_boundary;
250 }
251
252 #ifndef PRODUCT
253 void G1PageBasedVirtualSpace::print_on(outputStream* out) {
254 out->print ("Virtual space:");
255 if (_special) out->print(" (pinned in memory)");
256 out->cr();
257 out->print_cr(" - committed: " SIZE_FORMAT, committed_size());
258 out->print_cr(" - reserved: " SIZE_FORMAT, reserved_size());
259 out->print_cr(" - preferred page size: " SIZE_FORMAT, _page_size);
260 out->print_cr(" - [low_b, high_b]: [" PTR_FORMAT ", " PTR_FORMAT "]", p2i(_low_boundary), p2i(_high_boundary));
261 }
262
263 void G1PageBasedVirtualSpace::print() {
264 print_on(tty);
265 }
266 #endif
|