| 1 | /* |
| 2 | * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. |
| 3 | * Copyright (c) 2018 SAP SE. All rights reserved. |
| 4 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 5 | * |
| 6 | * This code is free software; you can redistribute it and/or modify it |
| 7 | * under the terms of the GNU General Public License version 2 only, as |
| 8 | * published by the Free Software Foundation. |
| 9 | * |
| 10 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 11 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 13 | * version 2 for more details (a copy is included in the LICENSE file that |
| 14 | * accompanied this code). |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License version |
| 17 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 18 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 | * |
| 20 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 21 | * or visit www.oracle.com if you need additional information or have any |
| 22 | * questions. |
| 23 | * |
| 24 | */ |
| 25 | |
| 26 | #include "precompiled.hpp" |
| 27 | #include "utilities/debug.hpp" |
| 28 | #include "utilities/globalDefinitions.hpp" |
| 29 | #include "memory/metaspace/metachunk.hpp" |
| 30 | #include "memory/metaspace/occupancyMap.hpp" |
| 31 | #include "runtime/os.hpp" |
| 32 | |
| 33 | namespace metaspace { |
| 34 | |
| 35 | OccupancyMap::OccupancyMap(const MetaWord* reference_address, size_t word_size, size_t smallest_chunk_word_size) : |
| 36 | _reference_address(reference_address), _word_size(word_size), |
| 37 | _smallest_chunk_word_size(smallest_chunk_word_size) |
| 38 | { |
| 39 | assert(reference_address != NULL, "invalid reference address" ); |
| 40 | assert(is_aligned(reference_address, smallest_chunk_word_size), |
| 41 | "Reference address not aligned to smallest chunk size." ); |
| 42 | assert(is_aligned(word_size, smallest_chunk_word_size), |
| 43 | "Word_size shall be a multiple of the smallest chunk size." ); |
| 44 | // Calculate bitmap size: one bit per smallest_chunk_word_size'd area. |
| 45 | size_t num_bits = word_size / smallest_chunk_word_size; |
| 46 | _map_size = (num_bits + 7) / 8; |
| 47 | assert(_map_size * 8 >= num_bits, "sanity" ); |
| 48 | _map[0] = (uint8_t*) os::malloc(_map_size, mtInternal); |
| 49 | _map[1] = (uint8_t*) os::malloc(_map_size, mtInternal); |
| 50 | assert(_map[0] != NULL && _map[1] != NULL, "Occupancy Map: allocation failed." ); |
| 51 | memset(_map[1], 0, _map_size); |
| 52 | memset(_map[0], 0, _map_size); |
| 53 | // Sanity test: the first respectively last possible chunk start address in |
| 54 | // the covered range shall map to the first and last bit in the bitmap. |
| 55 | assert(get_bitpos_for_address(reference_address) == 0, |
| 56 | "First chunk address in range must map to fist bit in bitmap." ); |
| 57 | assert(get_bitpos_for_address(reference_address + word_size - smallest_chunk_word_size) == num_bits - 1, |
| 58 | "Last chunk address in range must map to last bit in bitmap." ); |
| 59 | } |
| 60 | |
| 61 | OccupancyMap::~OccupancyMap() { |
| 62 | os::free(_map[0]); |
| 63 | os::free(_map[1]); |
| 64 | } |
| 65 | |
| 66 | #ifdef ASSERT |
| 67 | // Verify occupancy map for the address range [from, to). |
| 68 | // We need to tell it the address range, because the memory the |
| 69 | // occupancy map is covering may not be fully comitted yet. |
| 70 | void OccupancyMap::verify(MetaWord* from, MetaWord* to) { |
| 71 | Metachunk* chunk = NULL; |
| 72 | int nth_bit_for_chunk = 0; |
| 73 | MetaWord* chunk_end = NULL; |
| 74 | for (MetaWord* p = from; p < to; p += _smallest_chunk_word_size) { |
| 75 | const unsigned pos = get_bitpos_for_address(p); |
| 76 | // Check the chunk-starts-info: |
| 77 | if (get_bit_at_position(pos, layer_chunk_start_map)) { |
| 78 | // Chunk start marked in bitmap. |
| 79 | chunk = (Metachunk*) p; |
| 80 | if (chunk_end != NULL) { |
| 81 | assert(chunk_end == p, "Unexpected chunk start found at %p (expected " |
| 82 | "the next chunk to start at %p)." , p, chunk_end); |
| 83 | } |
| 84 | assert(chunk->is_valid_sentinel(), "Invalid chunk at address %p." , p); |
| 85 | if (chunk->get_chunk_type() != HumongousIndex) { |
| 86 | guarantee(is_aligned(p, chunk->word_size()), "Chunk %p not aligned." , p); |
| 87 | } |
| 88 | chunk_end = p + chunk->word_size(); |
| 89 | nth_bit_for_chunk = 0; |
| 90 | assert(chunk_end <= to, "Chunk end overlaps test address range." ); |
| 91 | } else { |
| 92 | // No chunk start marked in bitmap. |
| 93 | assert(chunk != NULL, "Chunk should start at start of address range." ); |
| 94 | assert(p < chunk_end, "Did not find expected chunk start at %p." , p); |
| 95 | nth_bit_for_chunk ++; |
| 96 | } |
| 97 | // Check the in-use-info: |
| 98 | const bool in_use_bit = get_bit_at_position(pos, layer_in_use_map); |
| 99 | if (in_use_bit) { |
| 100 | assert(!chunk->is_tagged_free(), "Chunk %p: marked in-use in map but is free (bit %u)." , |
| 101 | chunk, nth_bit_for_chunk); |
| 102 | } else { |
| 103 | assert(chunk->is_tagged_free(), "Chunk %p: marked free in map but is in use (bit %u)." , |
| 104 | chunk, nth_bit_for_chunk); |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | // Verify that a given chunk is correctly accounted for in the bitmap. |
| 110 | void OccupancyMap::verify_for_chunk(Metachunk* chunk) { |
| 111 | assert(chunk_starts_at_address((MetaWord*) chunk), |
| 112 | "No chunk start marked in map for chunk %p." , chunk); |
| 113 | // For chunks larger than the minimal chunk size, no other chunk |
| 114 | // must start in its area. |
| 115 | if (chunk->word_size() > _smallest_chunk_word_size) { |
| 116 | assert(!is_any_bit_set_in_region(((MetaWord*) chunk) + _smallest_chunk_word_size, |
| 117 | chunk->word_size() - _smallest_chunk_word_size, layer_chunk_start_map), |
| 118 | "No chunk must start within another chunk." ); |
| 119 | } |
| 120 | if (!chunk->is_tagged_free()) { |
| 121 | assert(is_region_in_use((MetaWord*)chunk, chunk->word_size()), |
| 122 | "Chunk %p is in use but marked as free in map (%d %d)." , |
| 123 | chunk, chunk->get_chunk_type(), chunk->get_origin()); |
| 124 | } else { |
| 125 | assert(!is_region_in_use((MetaWord*)chunk, chunk->word_size()), |
| 126 | "Chunk %p is free but marked as in-use in map (%d %d)." , |
| 127 | chunk, chunk->get_chunk_type(), chunk->get_origin()); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | #endif // ASSERT |
| 132 | |
| 133 | } // namespace metaspace |
| 134 | |
| 135 | |
| 136 | |