| 1 | /* |
| 2 | * Copyright (c) 2017, 2018, 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 "jfr/recorder/storage/jfrVirtualMemory.hpp" |
| 27 | #include "memory/virtualspace.hpp" |
| 28 | #include "runtime/orderAccess.hpp" |
| 29 | #include "runtime/os.hpp" |
| 30 | #include "services/memTracker.hpp" |
| 31 | #include "utilities/globalDefinitions.hpp" |
| 32 | |
| 33 | /* |
| 34 | * A memory segment represents a virtual memory reservation. |
| 35 | * It provides ways to commit and decommit physical storage |
| 36 | * onto its virtual memory reservation. |
| 37 | */ |
| 38 | |
| 39 | class JfrVirtualMemorySegment : public JfrCHeapObj { |
| 40 | friend class JfrVirtualMemoryManager; |
| 41 | private: |
| 42 | JfrVirtualMemorySegment* _next; |
| 43 | char* _top; |
| 44 | ReservedSpace _rs; |
| 45 | VirtualSpace _virtual_memory; |
| 46 | |
| 47 | // Convenience functions to access the underlying virtual space metadata |
| 48 | const u1* committed_low() const { return (const u1*)_virtual_memory.low(); } |
| 49 | const u1* committed_high() const { return (const u1*)_virtual_memory.high(); } |
| 50 | const u1* reserved_low() const { return (const u1*)_virtual_memory.low_boundary(); } |
| 51 | const u1* reserved_high() const { return (const u1*)_virtual_memory.high_boundary(); } |
| 52 | size_t reserved_words() const { return _virtual_memory.reserved_size() / BytesPerWord; } |
| 53 | size_t committed_words() const { return _virtual_memory.actual_committed_size() / BytesPerWord; } |
| 54 | bool is_pre_committed() const { return _virtual_memory.special(); } |
| 55 | VirtualSpace& virtual_space() { return _virtual_memory; } |
| 56 | |
| 57 | JfrVirtualMemorySegment(); |
| 58 | ~JfrVirtualMemorySegment(); |
| 59 | |
| 60 | JfrVirtualMemorySegment* next() const { return _next; } |
| 61 | void set_next(JfrVirtualMemorySegment* v) { _next = v; } |
| 62 | |
| 63 | // Returns true if requested size is available in the committed area |
| 64 | bool is_available(size_t block_size_request_words) { |
| 65 | return block_size_request_words <= pointer_delta(committed_high(), _top, sizeof(char*)); |
| 66 | } |
| 67 | |
| 68 | // allocation pointer committed memory |
| 69 | char* top() const { return _top; } |
| 70 | void inc_top(size_t size_in_words) { |
| 71 | assert(is_available(size_in_words), "invariant" ); |
| 72 | _top += size_in_words * BytesPerWord; |
| 73 | assert(_top <= _virtual_memory.high(), "invariant" ); |
| 74 | } |
| 75 | |
| 76 | // initialization is the virtual memory reservation |
| 77 | bool initialize(size_t reservation_size_request_bytes); |
| 78 | void* take_from_committed(size_t block_size_request_words); |
| 79 | |
| 80 | // Returns committed memory |
| 81 | void* commit(size_t block_size_request_words) { |
| 82 | return take_from_committed(block_size_request_words); |
| 83 | } |
| 84 | |
| 85 | // Commit more memory in a reservation |
| 86 | bool expand_by(size_t block_size_request_words); |
| 87 | |
| 88 | // Decommits all committed memory in this reservation segment. |
| 89 | void decommit(); |
| 90 | }; |
| 91 | |
| 92 | JfrVirtualMemorySegment::JfrVirtualMemorySegment() : |
| 93 | _next(NULL), |
| 94 | _top(NULL), |
| 95 | _rs(), |
| 96 | _virtual_memory() {} |
| 97 | |
| 98 | JfrVirtualMemorySegment::~JfrVirtualMemorySegment() { |
| 99 | decommit(); |
| 100 | _rs.release(); |
| 101 | } |
| 102 | |
| 103 | bool JfrVirtualMemorySegment::initialize(size_t reservation_size_request_bytes) { |
| 104 | assert(is_aligned(reservation_size_request_bytes, os::vm_allocation_granularity()), "invariant" ); |
| 105 | _rs = ReservedSpace(reservation_size_request_bytes, |
| 106 | os::vm_allocation_granularity(), |
| 107 | UseLargePages && os::can_commit_large_page_memory(), |
| 108 | false); |
| 109 | if (!_rs.is_reserved()) { |
| 110 | return false; |
| 111 | } |
| 112 | assert(_rs.base() != NULL, "invariant" ); |
| 113 | assert(_rs.size() != 0, "invariant" ); |
| 114 | assert(is_aligned(_rs.base(), os::vm_allocation_granularity()), "invariant" ); |
| 115 | assert(is_aligned(_rs.size(), os::vm_allocation_granularity()), "invariant" ); |
| 116 | os::trace_page_sizes("Jfr" , reservation_size_request_bytes, |
| 117 | reservation_size_request_bytes, |
| 118 | os::vm_page_size(), |
| 119 | _rs.base(), |
| 120 | _rs.size()); |
| 121 | MemTracker::record_virtual_memory_type((address)_rs.base(), mtTracing); |
| 122 | assert(is_aligned(_rs.base(), os::vm_page_size()), "invariant" ); |
| 123 | assert(is_aligned(_rs.size(), os::vm_page_size()), "invariant" ); |
| 124 | |
| 125 | // ReservedSpaces marked as special will have the entire memory |
| 126 | // pre-committed. Setting a committed size will make sure that |
| 127 | // committed_size and actual_committed_size agrees. |
| 128 | const size_t pre_committed_size = _rs.special() ? _rs.size() : 0; |
| 129 | const bool result = virtual_space().initialize_with_granularity(_rs, pre_committed_size, os::vm_page_size()); |
| 130 | |
| 131 | if (result) { |
| 132 | assert(virtual_space().committed_size() == virtual_space().actual_committed_size(), |
| 133 | "Checking that the pre-committed memory was registered by the VirtualSpace" ); |
| 134 | _top = virtual_space().low(); |
| 135 | } |
| 136 | return result; |
| 137 | } |
| 138 | |
| 139 | bool JfrVirtualMemorySegment::expand_by(size_t block_size_request_words) { |
| 140 | size_t block_size_request_bytes = block_size_request_words * BytesPerWord; |
| 141 | const size_t uncommitted = virtual_space().reserved_size() - virtual_space().actual_committed_size(); |
| 142 | if (uncommitted < block_size_request_bytes) { |
| 143 | // commit whatever is left in the reservation |
| 144 | block_size_request_bytes = uncommitted; |
| 145 | } |
| 146 | assert(is_aligned(block_size_request_bytes, os::vm_allocation_granularity()), "invariant" ); |
| 147 | // commit block in reserved memory |
| 148 | bool result = virtual_space().expand_by(block_size_request_bytes, false); |
| 149 | assert(result, "Failed to commit memory" ); |
| 150 | return result; |
| 151 | } |
| 152 | |
| 153 | void JfrVirtualMemorySegment::decommit() { |
| 154 | assert(_virtual_memory.committed_size() == _virtual_memory.actual_committed_size(), |
| 155 | "The committed memory doesn't match the expanded memory." ); |
| 156 | |
| 157 | const size_t committed_size = virtual_space().actual_committed_size(); |
| 158 | if (committed_size > 0) { |
| 159 | virtual_space().shrink_by(committed_size); |
| 160 | } |
| 161 | |
| 162 | assert(_virtual_memory.actual_committed_size() == 0, "invariant" ); |
| 163 | } |
| 164 | |
| 165 | // Attempt to get a committed block |
| 166 | void* JfrVirtualMemorySegment::take_from_committed(size_t block_size_request_words) { |
| 167 | // The virtual spaces are always expanded by the |
| 168 | // commit granularity to enforce the following condition. |
| 169 | // Without this the is_available check will not work correctly. |
| 170 | assert(_virtual_memory.committed_size() == _virtual_memory.actual_committed_size(), |
| 171 | "The committed memory doesn't match the expanded memory." ); |
| 172 | if (!is_available(block_size_request_words)) { |
| 173 | return NULL; |
| 174 | } |
| 175 | void* const block = top(); |
| 176 | assert(block != NULL, "invariant" ); |
| 177 | inc_top(block_size_request_words); |
| 178 | return block; |
| 179 | } |
| 180 | |
| 181 | class JfrVirtualMemoryManager : public JfrCHeapObj { |
| 182 | typedef JfrVirtualMemorySegment Segment; |
| 183 | private: |
| 184 | Segment* _segments; |
| 185 | Segment* _current_segment; |
| 186 | size_t _reservation_size_request_words; |
| 187 | size_t _reservation_size_request_limit_words; // total reservation limit |
| 188 | |
| 189 | // Sum of reserved and committed memory in the segments |
| 190 | size_t _current_reserved_words; |
| 191 | size_t _current_committed_words; |
| 192 | |
| 193 | void link(Segment* segment); |
| 194 | Segment* current(); |
| 195 | |
| 196 | void inc_reserved_words(size_t words); |
| 197 | void inc_committed_words(size_t words); |
| 198 | |
| 199 | bool new_segment(size_t reservation_size_request_words); |
| 200 | |
| 201 | bool expand_segment_by(Segment* segment, size_t block_size_request_words); |
| 202 | |
| 203 | bool expand_by(size_t block_size_request_words, size_t reservation_size_request_words); |
| 204 | bool can_reserve() const; |
| 205 | |
| 206 | public: |
| 207 | JfrVirtualMemoryManager(); |
| 208 | ~JfrVirtualMemoryManager(); |
| 209 | |
| 210 | bool initialize(size_t reservation_size_request_words, size_t segment_count = 1); |
| 211 | void* commit(size_t requested_block_size_words); |
| 212 | |
| 213 | bool is_full() const { |
| 214 | return reserved_high() == committed_high(); |
| 215 | } |
| 216 | |
| 217 | const u1* committed_low() const { return _current_segment->committed_low(); } |
| 218 | const u1* committed_high() const { return _current_segment->committed_high(); } |
| 219 | const u1* reserved_low() const { return _current_segment->reserved_low(); } |
| 220 | const u1* reserved_high() const { return _current_segment->reserved_high(); } |
| 221 | }; |
| 222 | |
| 223 | JfrVirtualMemoryManager::JfrVirtualMemoryManager() : |
| 224 | _segments(NULL), |
| 225 | _current_segment(NULL), |
| 226 | _reservation_size_request_words(0), |
| 227 | _reservation_size_request_limit_words(0), |
| 228 | _current_reserved_words(0), |
| 229 | _current_committed_words(0) {} |
| 230 | |
| 231 | JfrVirtualMemoryManager::~JfrVirtualMemoryManager() { |
| 232 | JfrVirtualMemorySegment* segment = _segments; |
| 233 | while (segment != NULL) { |
| 234 | JfrVirtualMemorySegment* next_segment = segment->next(); |
| 235 | delete segment; |
| 236 | segment = next_segment; |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | // for now only allow a singleton segment per virtual memory client |
| 241 | bool JfrVirtualMemoryManager::initialize(size_t reservation_size_request_words, size_t segment_count /* 1 */) { |
| 242 | assert(is_aligned(reservation_size_request_words * BytesPerWord, os::vm_allocation_granularity()), "invariant" ); |
| 243 | _reservation_size_request_words = reservation_size_request_words; |
| 244 | assert(segment_count > 0, "invariant" ); |
| 245 | _reservation_size_request_limit_words = reservation_size_request_words * segment_count; |
| 246 | assert(is_aligned(_reservation_size_request_limit_words * BytesPerWord, os::vm_allocation_granularity()), "invariant" ); |
| 247 | return new_segment(_reservation_size_request_words); |
| 248 | } |
| 249 | |
| 250 | bool JfrVirtualMemoryManager::can_reserve() const { |
| 251 | return _reservation_size_request_limit_words == 0 ? true : _current_reserved_words < _reservation_size_request_limit_words; |
| 252 | } |
| 253 | |
| 254 | // Allocate another segment and add it to the list. |
| 255 | bool JfrVirtualMemoryManager::new_segment(size_t reservation_size_request_words) { |
| 256 | assert(reservation_size_request_words > 0, "invariant" ); |
| 257 | assert(is_aligned(reservation_size_request_words * BytesPerWord, os::vm_allocation_granularity()), "invariant" ); |
| 258 | Segment* segment = new Segment(); |
| 259 | if (NULL == segment) { |
| 260 | return false; |
| 261 | } |
| 262 | if (!segment->initialize(reservation_size_request_words * BytesPerWord)) { |
| 263 | delete segment; |
| 264 | return false; |
| 265 | } |
| 266 | assert(segment->reserved_words() == reservation_size_request_words, |
| 267 | "Actual reserved memory size differs from requested reservation memory size" ); |
| 268 | link(segment); |
| 269 | return true; |
| 270 | } |
| 271 | |
| 272 | bool JfrVirtualMemoryManager::expand_segment_by(JfrVirtualMemorySegment* segment, size_t block_size_request_words) { |
| 273 | assert(segment != NULL, "invariant" ); |
| 274 | const size_t before = segment->committed_words(); |
| 275 | const bool result = segment->expand_by(block_size_request_words); |
| 276 | const size_t after = segment->committed_words(); |
| 277 | // after and before can be the same if the memory was pre-committed. |
| 278 | assert(after >= before, "Inconsistency" ); |
| 279 | inc_committed_words(after - before); |
| 280 | return result; |
| 281 | } |
| 282 | |
| 283 | void JfrVirtualMemoryManager::inc_reserved_words(size_t words) { |
| 284 | _current_reserved_words += words; |
| 285 | } |
| 286 | |
| 287 | JfrVirtualMemorySegment* JfrVirtualMemoryManager::current() { |
| 288 | return _current_segment; |
| 289 | } |
| 290 | |
| 291 | void JfrVirtualMemoryManager::inc_committed_words(size_t words) { |
| 292 | _current_committed_words += words; |
| 293 | } |
| 294 | |
| 295 | bool JfrVirtualMemoryManager::expand_by(size_t block_size_request_words, size_t reservation_size_request_words) { |
| 296 | assert(is_aligned(block_size_request_words * BytesPerWord, os::vm_page_size()), "invariant" ); |
| 297 | assert(is_aligned(block_size_request_words * BytesPerWord, os::vm_allocation_granularity()), "invariant" ); |
| 298 | assert(is_aligned(reservation_size_request_words * BytesPerWord, os::vm_page_size()), "invariant" ); |
| 299 | assert(is_aligned(reservation_size_request_words * BytesPerWord, os::vm_allocation_granularity()), "invariant" ); |
| 300 | assert(block_size_request_words <= reservation_size_request_words, "invariant" ); |
| 301 | // Attempt to commit more memory from the the current virtual space reservation. |
| 302 | if (expand_segment_by(current(), block_size_request_words)) { |
| 303 | return true; |
| 304 | } |
| 305 | |
| 306 | // reached limit of what is allowed to be reserved? |
| 307 | if (!can_reserve()) { |
| 308 | return false; |
| 309 | } |
| 310 | |
| 311 | // Get another segment. |
| 312 | if (!new_segment(reservation_size_request_words)) { |
| 313 | return false; |
| 314 | } |
| 315 | |
| 316 | if (current()->is_pre_committed()) { |
| 317 | // The memory was pre-committed, so we are done here. |
| 318 | assert(block_size_request_words <= current()->committed_words(), |
| 319 | "The new VirtualSpace was pre-committed, so it" |
| 320 | "should be large enough to fit the alloc request." ); |
| 321 | return true; |
| 322 | } |
| 323 | return expand_segment_by(current(), block_size_request_words); |
| 324 | } |
| 325 | |
| 326 | void JfrVirtualMemoryManager::link(JfrVirtualMemorySegment* segment) { |
| 327 | assert(segment != NULL, "invariant" ); |
| 328 | if (_segments == NULL) { |
| 329 | _segments = segment; |
| 330 | } else { |
| 331 | assert(_current_segment != NULL, "invariant" ); |
| 332 | assert(_segments == _current_segment, "invariant" ); |
| 333 | _current_segment->set_next(segment); |
| 334 | } |
| 335 | _current_segment = segment; |
| 336 | inc_reserved_words(segment->reserved_words()); |
| 337 | inc_committed_words(segment->committed_words()); |
| 338 | } |
| 339 | |
| 340 | void* JfrVirtualMemoryManager::commit(size_t block_size_request_words) { |
| 341 | assert(is_aligned(block_size_request_words * BytesPerWord, os::vm_allocation_granularity()), "invariant" ); |
| 342 | void* block = current()->commit(block_size_request_words); |
| 343 | if (block != NULL) { |
| 344 | return block; |
| 345 | } |
| 346 | assert(block == NULL, "invariant" ); |
| 347 | if (is_full()) { |
| 348 | return NULL; |
| 349 | } |
| 350 | assert(block_size_request_words <= _reservation_size_request_words, "invariant" ); |
| 351 | if (expand_by(block_size_request_words, _reservation_size_request_words)) { |
| 352 | block = current()->commit(block_size_request_words); |
| 353 | assert(block != NULL, "The allocation was expected to succeed after the expansion" ); |
| 354 | } |
| 355 | return block; |
| 356 | } |
| 357 | |
| 358 | JfrVirtualMemory::JfrVirtualMemory() : |
| 359 | _vmm(NULL), |
| 360 | _reserved_low(), |
| 361 | _reserved_high(), |
| 362 | _top(NULL), |
| 363 | _commit_point(NULL), |
| 364 | _physical_commit_size_request_words(0), |
| 365 | _aligned_datum_size_bytes(0) {} |
| 366 | |
| 367 | JfrVirtualMemory::~JfrVirtualMemory() { |
| 368 | assert(_vmm != NULL, "invariant" ); |
| 369 | delete _vmm; |
| 370 | } |
| 371 | |
| 372 | size_t JfrVirtualMemory::aligned_datum_size_bytes() const { |
| 373 | return _aligned_datum_size_bytes; |
| 374 | } |
| 375 | |
| 376 | static void adjust_allocation_ratio(size_t* const reservation_size_bytes, size_t* const commit_size_bytes) { |
| 377 | assert(reservation_size_bytes != NULL, "invariant" ); |
| 378 | assert(*reservation_size_bytes > 0, "invariant" ); |
| 379 | assert(commit_size_bytes != NULL, "invariant" ); |
| 380 | assert(*commit_size_bytes > 0, "invariant" ); |
| 381 | assert(*reservation_size_bytes >= *commit_size_bytes, "invariant" ); |
| 382 | assert(is_aligned(*reservation_size_bytes, os::vm_allocation_granularity()), "invariant" ); |
| 383 | assert(is_aligned(*commit_size_bytes, os::vm_allocation_granularity()), "invariant" ); |
| 384 | |
| 385 | size_t reservation_size_units = *reservation_size_bytes / os::vm_allocation_granularity(); |
| 386 | size_t commit_size_units = *commit_size_bytes / os::vm_allocation_granularity(); |
| 387 | assert(reservation_size_units > 0, "invariant" ); |
| 388 | assert(commit_size_units > 0, "invariant" ); |
| 389 | |
| 390 | size_t original_ratio_units = reservation_size_units / commit_size_units; |
| 391 | size_t rem = reservation_size_units % commit_size_units; |
| 392 | assert(original_ratio_units > 0, "invariant" ); |
| 393 | |
| 394 | if (rem > 0) { |
| 395 | reservation_size_units -= rem % original_ratio_units; |
| 396 | commit_size_units += rem / original_ratio_units; |
| 397 | } |
| 398 | |
| 399 | assert(commit_size_units > 0, "invariant" ); |
| 400 | assert(reservation_size_units % original_ratio_units == 0, "invariant" ); |
| 401 | assert(original_ratio_units * commit_size_units == reservation_size_units , "invariant" ); |
| 402 | assert(original_ratio_units == reservation_size_units / commit_size_units, "invariant" ); |
| 403 | *reservation_size_bytes = reservation_size_units * os::vm_allocation_granularity(); |
| 404 | *commit_size_bytes = commit_size_units * os::vm_allocation_granularity(); |
| 405 | assert((*reservation_size_bytes % *commit_size_bytes) == 0, "invariant" ); |
| 406 | } |
| 407 | |
| 408 | |
| 409 | void* JfrVirtualMemory::initialize(size_t reservation_size_request_bytes, |
| 410 | size_t block_size_request_bytes, |
| 411 | size_t datum_size_bytes /* 1 */) { |
| 412 | assert(_vmm == NULL, "invariant" ); |
| 413 | _vmm = new JfrVirtualMemoryManager(); |
| 414 | |
| 415 | if (_vmm == NULL) { |
| 416 | return NULL; |
| 417 | } |
| 418 | |
| 419 | assert(reservation_size_request_bytes > 0, "invariant" ); |
| 420 | _aligned_datum_size_bytes = align_up(datum_size_bytes, BytesPerWord); |
| 421 | assert(is_aligned(_aligned_datum_size_bytes, BytesPerWord), "invariant" ); |
| 422 | |
| 423 | reservation_size_request_bytes = ReservedSpace::allocation_align_size_up(reservation_size_request_bytes); |
| 424 | assert(is_aligned(reservation_size_request_bytes, os::vm_allocation_granularity()), "invariant" ); |
| 425 | assert(is_aligned(reservation_size_request_bytes, _aligned_datum_size_bytes), "invariant" ); |
| 426 | block_size_request_bytes = MAX2(block_size_request_bytes, (size_t)os::vm_allocation_granularity()); |
| 427 | block_size_request_bytes = ReservedSpace::allocation_align_size_up(block_size_request_bytes); |
| 428 | assert(is_aligned(block_size_request_bytes, os::vm_allocation_granularity()), "invariant" ); |
| 429 | assert(is_aligned(block_size_request_bytes, _aligned_datum_size_bytes), "invariant" ); |
| 430 | // adjustment to valid ratio in units of vm_allocation_granularity |
| 431 | adjust_allocation_ratio(&reservation_size_request_bytes, &block_size_request_bytes); |
| 432 | assert(is_aligned(reservation_size_request_bytes, os::vm_allocation_granularity()), "invariant" ); |
| 433 | assert(is_aligned(reservation_size_request_bytes, _aligned_datum_size_bytes), "invariant" ); |
| 434 | assert(is_aligned(block_size_request_bytes, os::vm_allocation_granularity()), "invariant" ); |
| 435 | assert(is_aligned(block_size_request_bytes, _aligned_datum_size_bytes), "invariant" ); |
| 436 | assert((reservation_size_request_bytes % block_size_request_bytes) == 0, "invariant" ); |
| 437 | const size_t reservation_size_request_words = reservation_size_request_bytes / BytesPerWord; |
| 438 | _physical_commit_size_request_words = block_size_request_bytes / BytesPerWord; |
| 439 | // virtual memory reservation |
| 440 | if (!_vmm->initialize(reservation_size_request_words)) { |
| 441 | // is implicitly "full" if reservation fails |
| 442 | assert(is_full(), "invariant" ); |
| 443 | return NULL; |
| 444 | } |
| 445 | _reserved_low = (const u1*)_vmm->reserved_low(); |
| 446 | _reserved_high = (const u1*)_vmm->reserved_high(); |
| 447 | // reservation complete |
| 448 | _top = (u1*)_vmm->committed_high(); |
| 449 | _commit_point = _top; |
| 450 | assert(_reserved_low == _top, "invariant" ); // initial empty state |
| 451 | assert((size_t)(_reserved_high - _reserved_low) == reservation_size_request_bytes, "invariant" ); |
| 452 | // initial commit |
| 453 | commit_memory_block(); |
| 454 | return _top; |
| 455 | } |
| 456 | |
| 457 | void* JfrVirtualMemory::commit(size_t block_size_request_words) { |
| 458 | assert(_vmm != NULL, "invariant" ); |
| 459 | assert(is_aligned(block_size_request_words * BytesPerWord, os::vm_allocation_granularity()), "invariant" ); |
| 460 | return _vmm->commit(block_size_request_words); |
| 461 | } |
| 462 | |
| 463 | bool JfrVirtualMemory::is_full() const { |
| 464 | return _top == _reserved_high; |
| 465 | } |
| 466 | |
| 467 | bool JfrVirtualMemory::is_empty() const { |
| 468 | return _top == _reserved_low; |
| 469 | } |
| 470 | |
| 471 | bool JfrVirtualMemory::commit_memory_block() { |
| 472 | assert(_vmm != NULL, "invariant" ); |
| 473 | assert(!is_full(), "invariant" ); |
| 474 | assert(_top == _commit_point, "invariant" ); |
| 475 | |
| 476 | void* const block = _vmm->commit(_physical_commit_size_request_words); |
| 477 | if (block != NULL) { |
| 478 | _commit_point = _vmm->committed_high(); |
| 479 | return true; |
| 480 | } |
| 481 | // all reserved virtual memory is committed |
| 482 | assert(block == NULL, "invariant" ); |
| 483 | assert(_vmm->reserved_high() == _vmm->committed_high(), "invariant" ); |
| 484 | return false; |
| 485 | } |
| 486 | |
| 487 | void* JfrVirtualMemory::new_datum() { |
| 488 | assert(_vmm != NULL, "invariant" ); |
| 489 | assert(!is_full(), "invariant" ); |
| 490 | if (_top == _commit_point) { |
| 491 | if (!commit_memory_block()) { |
| 492 | assert(is_full(), "invariant" ); |
| 493 | return NULL; |
| 494 | } |
| 495 | } |
| 496 | assert(_top + _aligned_datum_size_bytes <= _commit_point, "invariant" ); |
| 497 | u1* allocation = _top; |
| 498 | _top += _aligned_datum_size_bytes; |
| 499 | assert(is_aligned(allocation, _aligned_datum_size_bytes), "invariant" ); |
| 500 | return allocation; |
| 501 | } |
| 502 | |
| 503 | void* JfrVirtualMemory::index_ptr(size_t index) { |
| 504 | assert((index * _aligned_datum_size_bytes) + _reserved_low < _commit_point, "invariant" ); |
| 505 | return (void*)((index * _aligned_datum_size_bytes) + _reserved_low); |
| 506 | } |
| 507 | |
| 508 | void* JfrVirtualMemory::get(size_t index) { |
| 509 | return index_ptr(index); |
| 510 | } |
| 511 | |
| 512 | size_t JfrVirtualMemory::count() const { |
| 513 | return (_top - _reserved_low) / _aligned_datum_size_bytes; |
| 514 | } |
| 515 | |
| 516 | size_t JfrVirtualMemory::live_set() const { |
| 517 | return _top - _reserved_low; |
| 518 | } |
| 519 | |
| 520 | size_t JfrVirtualMemory::reserved_size() const { |
| 521 | return _reserved_high - _reserved_low; |
| 522 | } |
| 523 | |
| 524 | bool JfrVirtualMemory::compact(size_t index) { |
| 525 | assert(index > 0, "invariant" ); |
| 526 | assert(index <= reserved_size(), "invariant" ); |
| 527 | const u1* low = static_cast<u1*>(index_ptr(index)); |
| 528 | const size_t block_size = _top - low; |
| 529 | memcpy(const_cast<u1*>(_reserved_low), low, block_size); |
| 530 | _top = const_cast<u1*>(_reserved_low) + block_size; |
| 531 | assert(live_set() == block_size, "invariant" ); |
| 532 | return true; |
| 533 | } |
| 534 | |