|
| 1 | +/* |
| 2 | + * Copyright (c) 2023, 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 | +#include "precompiled.hpp" |
| 25 | +#include "gc/z/zAddress.inline.hpp" |
| 26 | +#include "gc/z/zGlobals.hpp" |
| 27 | +#include "gc/z/zNMT.hpp" |
| 28 | +#include "gc/z/zVirtualMemory.hpp" |
| 29 | +#include "memory/allocation.hpp" |
| 30 | +#include "services/memTracker.hpp" |
| 31 | +#include "utilities/nativeCallStack.hpp" |
| 32 | + |
| 33 | +ZNMT::Reservation ZNMT::_reservations[ZMaxVirtualReservations] = {}; |
| 34 | +size_t ZNMT::_num_reservations = 0; |
| 35 | + |
| 36 | +size_t ZNMT::reservation_index(zoffset offset, size_t* offset_in_reservation) { |
| 37 | + assert(_num_reservations > 0, "at least one reservation must exist"); |
| 38 | + |
| 39 | + size_t index = 0; |
| 40 | + *offset_in_reservation = untype(offset); |
| 41 | + for (; index < _num_reservations; ++index) { |
| 42 | + const size_t reservation_size = _reservations[index]._size; |
| 43 | + if (*offset_in_reservation < reservation_size) { |
| 44 | + break; |
| 45 | + } |
| 46 | + *offset_in_reservation -= reservation_size; |
| 47 | + } |
| 48 | + |
| 49 | + assert(index != _num_reservations, "failed to find reservation index"); |
| 50 | + return index; |
| 51 | +} |
| 52 | + |
| 53 | +void ZNMT::process_fake_mapping(zoffset offset, size_t size, bool commit) { |
| 54 | + // In order to satisfy NTM's requirement of an 1:1 mapping between committed |
| 55 | + // and reserved addresses, a fake mapping from the offset into the reservation |
| 56 | + // is used. |
| 57 | + // |
| 58 | + // These mappings from |
| 59 | + // [offset, offset + size) -> {[virtual address range], ...} |
| 60 | + // are stable after the heap has been reserved. No commits proceed any |
| 61 | + // reservations. Committing and uncommitting the same [offset, offset + size) |
| 62 | + // range will result in same virtual memory ranges. |
| 63 | + |
| 64 | + size_t left_to_process = size; |
| 65 | + size_t offset_in_reservation; |
| 66 | + for (size_t i = reservation_index(offset, &offset_in_reservation); i < _num_reservations; ++i) { |
| 67 | + const zaddress_unsafe reservation_start = _reservations[i]._start; |
| 68 | + const size_t reservation_size = _reservations[i]._size; |
| 69 | + const size_t sub_range_size = MIN2(left_to_process, reservation_size - offset_in_reservation); |
| 70 | + const uintptr_t sub_range_addr = untype(reservation_start) + offset_in_reservation; |
| 71 | + |
| 72 | + // commit / uncommit memory |
| 73 | + if (commit) { |
| 74 | + MemTracker::record_virtual_memory_commit((void*)sub_range_addr, sub_range_size, CALLER_PC); |
| 75 | + } else { |
| 76 | + if (MemTracker::enabled()) { |
| 77 | + Tracker tracker(Tracker::uncommit); |
| 78 | + tracker.record((address)sub_range_addr, sub_range_size); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + left_to_process -= sub_range_size; |
| 83 | + if (left_to_process == 0) { |
| 84 | + // Processed all nmt registrations |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + offset_in_reservation = 0; |
| 89 | + } |
| 90 | + |
| 91 | + assert(left_to_process == 0, "everything was not commited"); |
| 92 | +} |
| 93 | + |
| 94 | +void ZNMT::reserve(zaddress_unsafe start, size_t size) { |
| 95 | + assert(_num_reservations < ZMaxVirtualReservations, "too many reservations"); |
| 96 | + // Keep track of the reservations made in order to create fake mappings |
| 97 | + // between the reserved and commited memory. |
| 98 | + // See details in ZNMT::process_fake_mapping |
| 99 | + _reservations[_num_reservations++] = {start, size}; |
| 100 | + |
| 101 | + MemTracker::record_virtual_memory_reserve((void*)untype(start), size, CALLER_PC, mtJavaHeap); |
| 102 | +} |
| 103 | + |
| 104 | +void ZNMT::commit(zoffset offset, size_t size) { |
| 105 | + // NMT expects a 1-to-1 mapping between virtual and physical memory. |
| 106 | + // ZGC can temporarily have multiple virtual addresses pointing to |
| 107 | + // the same physical memory. |
| 108 | + // |
| 109 | + // When this function is called we don't know where in the virtual memory |
| 110 | + // this physical memory will be mapped. So we fake the virtual memory |
| 111 | + // address by mapping the physical offset into offsets in the reserved |
| 112 | + // memory space. |
| 113 | + process_fake_mapping(offset, size, true); |
| 114 | +} |
| 115 | + |
| 116 | +void ZNMT::uncommit(zoffset offset, size_t size) { |
| 117 | + // We fake the virtual memory address by mapping the physical offset |
| 118 | + // into offsets in the reserved memory space. |
| 119 | + // See comment in ZNMT::commit |
| 120 | + process_fake_mapping(offset, size, false); |
| 121 | +} |
0 commit comments