1 /* 2 * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved. 3 * Copyright (c) 2020 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 #ifndef SHARE_MEMORY_METASPACE_MSCOMMITMASK_HPP 27 #define SHARE_MEMORY_METASPACE_MSCOMMITMASK_HPP 28 29 #include "utilities/debug.hpp" 30 #include "utilities/bitMap.hpp" 31 #include "utilities/globalDefinitions.hpp" 32 33 class outputStream; 34 35 namespace metaspace { 36 37 // The CommitMask is a bitmask used to store the commit state of commit granules. 38 // It keeps one bit per granule; 1 means committed, 0 means uncommitted. 39 40 class CommitMask : public CHeapBitMap { 41 42 const MetaWord* const _base; 43 const size_t _word_size; 44 const size_t _words_per_bit; 45 46 // Given an offset, in words, into the area, return the number of the bit 47 // covering it. 48 static idx_t bitno_for_word_offset(size_t offset, size_t words_per_bit) { 49 return offset / words_per_bit; 50 } 51 52 idx_t bitno_for_address(const MetaWord* p) const { 53 // Note: we allow one-beyond since this is a typical need. 54 assert(p >= _base && p <= _base + _word_size, "Invalid address"); 55 const size_t off = p - _base; 56 return bitno_for_word_offset(off, _words_per_bit); 57 } 58 59 static idx_t mask_size(size_t word_size, size_t words_per_bit) { 60 return bitno_for_word_offset(word_size, words_per_bit); 61 } 62 63 #ifdef ASSERT 64 // Given a pointer, check if it points into the range this bitmap covers. 65 bool is_pointer_valid(const MetaWord* p) const { 66 return p >= _base && p < _base + _word_size; 67 } 68 69 // Given a pointer, check if it points into the range this bitmap covers. 70 void check_pointer(const MetaWord* p) const { 71 assert(is_pointer_valid(p), 72 "Pointer " PTR_FORMAT " not in range of this bitmap [" PTR_FORMAT ", " PTR_FORMAT ").", 73 p2i(p), p2i(_base), p2i(_base + _word_size)); 74 } 75 // Given a pointer, check if it points into the range this bitmap covers, 76 // and if it is aligned to commit granule border. 77 void check_pointer_aligned(const MetaWord* p) const { 78 check_pointer(p); 79 assert(is_aligned(p, _words_per_bit * BytesPerWord), 80 "Pointer " PTR_FORMAT " should be aligned to commit granule size " SIZE_FORMAT ".", 81 p2i(p), _words_per_bit * BytesPerWord); 82 } 83 // Given a range, check if it points into the range this bitmap covers, 84 // and if its borders are aligned to commit granule border. 85 void check_range(const MetaWord* start, size_t word_size) const { 86 check_pointer_aligned(start); 87 assert(is_aligned(word_size, _words_per_bit), 88 "Range " SIZE_FORMAT " should be aligned to commit granule size " SIZE_FORMAT ".", 89 word_size, _words_per_bit); 90 check_pointer(start + word_size - 1); 91 } 92 #endif 93 94 // Marks a single commit granule as committed (value == true) 95 // or uncomitted (value == false) and returns 96 // its prior state. 97 bool mark_granule(idx_t bitno, bool value) { 98 bool b = at(bitno); 99 at_put(bitno, value); 100 return b; 101 } 102 103 public: 104 105 // Create a commit mask covering a range [start, start + word_size). 106 CommitMask(const MetaWord* start, size_t word_size); 107 108 const MetaWord* base() const { return _base; } 109 size_t word_size() const { return _word_size; } 110 const MetaWord* end() const { return _base + word_size(); } 111 112 // Given an address, returns true if the address is committed, false if not. 113 bool is_committed_address(const MetaWord* p) const { 114 DEBUG_ONLY(check_pointer(p)); 115 const idx_t bitno = bitno_for_address(p); 116 return at(bitno); 117 } 118 119 // Given an address range, return size, in number of words, of committed area within that range. 120 size_t get_committed_size_in_range(const MetaWord* start, size_t word_size) const { 121 DEBUG_ONLY(check_range(start, word_size)); 122 assert(word_size > 0, "zero range"); 123 const idx_t b1 = bitno_for_address(start); 124 const idx_t b2 = bitno_for_address(start + word_size); 125 const idx_t num_bits = count_one_bits(b1, b2); 126 return num_bits * _words_per_bit; 127 } 128 129 // Return total committed size, in number of words. 130 size_t get_committed_size() const { 131 return count_one_bits() * _words_per_bit; 132 } 133 134 // Mark a whole address range [start, end) as committed. 135 // Return the number of words which had already been committed before this operation. 136 size_t mark_range_as_committed(const MetaWord* start, size_t word_size) { 137 DEBUG_ONLY(check_range(start, word_size)); 138 assert(word_size > 0, "zero range"); 139 const idx_t b1 = bitno_for_address(start); 140 const idx_t b2 = bitno_for_address(start + word_size); 141 if (b1 == b2) { // Simple case, 1 granule 142 bool was_committed = mark_granule(b1, true); 143 return was_committed ? _words_per_bit : 0; 144 } 145 const idx_t one_bits_in_range_before = count_one_bits(b1, b2); 146 set_range(b1, b2); 147 return one_bits_in_range_before * _words_per_bit; 148 } 149 150 // Mark a whole address range [start, end) as uncommitted. 151 // Return the number of words which had already been uncommitted before this operation. 152 size_t mark_range_as_uncommitted(const MetaWord* start, size_t word_size) { 153 DEBUG_ONLY(check_range(start, word_size)); 154 assert(word_size > 0, "zero range"); 155 const idx_t b1 = bitno_for_address(start); 156 const idx_t b2 = bitno_for_address(start + word_size); 157 if (b1 == b2) { // Simple case, 1 granule 158 bool was_committed = mark_granule(b1, false); 159 return was_committed ? 0 : _words_per_bit; 160 } 161 const idx_t zero_bits_in_range_before = 162 (b2 - b1) - count_one_bits(b1, b2); 163 clear_range(b1, b2); 164 return zero_bits_in_range_before * _words_per_bit; 165 } 166 167 //// Debug stuff //// 168 169 // Verify internals. 170 DEBUG_ONLY(void verify() const;) 171 172 void print_on(outputStream* st) const; 173 174 }; 175 176 } // namespace metaspace 177 178 #endif // SHARE_MEMORY_METASPACE_MSCOMMITMASK_HPP