1 /* 2 * Copyright (c) 2019, 2020, 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 "memory/archiveUtils.hpp" 27 #include "memory/metaspace.hpp" 28 #include "utilities/bitMap.inline.hpp" 29 30 #if INCLUDE_CDS 31 32 CHeapBitMap* ArchivePtrMarker::_ptrmap = NULL; 33 address* ArchivePtrMarker::_ptr_base; 34 address* ArchivePtrMarker::_ptr_end; 35 bool ArchivePtrMarker::_compacted; 36 37 void ArchivePtrMarker::initialize(CHeapBitMap* ptrmap, address* ptr_base, address* ptr_end) { 38 assert(_ptrmap == NULL, "initialize only once"); 39 _ptr_base = ptr_base; 40 _ptr_end = ptr_end; 41 _compacted = false; 42 _ptrmap = ptrmap; 43 44 // Use this as initial guesstimate. We should need less space in the 45 // archive, but if we're wrong the bitmap will be expanded automatically. 46 size_t estimated_archive_size = MetaspaceGC::capacity_until_GC(); 47 // But set it smaller in debug builds so we always test the expansion code. 48 // (Default archive is about 12MB). 49 DEBUG_ONLY(estimated_archive_size = 6 * M); 50 51 // We need one bit per pointer in the archive. 52 _ptrmap->initialize(estimated_archive_size / sizeof(intptr_t)); 53 } 54 55 void ArchivePtrMarker::mark_pointer(address* ptr_loc) { 56 assert(_ptrmap != NULL, "not initialized"); 57 assert(!_compacted, "cannot mark anymore"); 58 59 if (_ptr_base <= ptr_loc && ptr_loc < _ptr_end) { 60 address value = *ptr_loc; 61 // We don't want any pointer that points to very bottom of the archive, otherwise when 62 // MetaspaceShared::default_base_address()==0, we can't distinguish between a pointer 63 // to nothing (NULL) vs a pointer to an objects that happens to be at the very bottom 64 // of the archive. 65 assert(value != (address)_ptr_base, "don't point to the bottom of the archive"); 66 67 if (value != NULL) { 68 assert(uintx(ptr_loc) % sizeof(intptr_t) == 0, "pointers must be stored in aligned addresses"); 69 size_t idx = ptr_loc - _ptr_base; 70 if (_ptrmap->size() <= idx) { 71 _ptrmap->resize((idx + 1) * 2); 72 } 73 assert(idx < _ptrmap->size(), "must be"); 74 _ptrmap->set_bit(idx); 75 //tty->print_cr("Marking pointer [%p] -> %p @ " SIZE_FORMAT_W(9), ptr_loc, *ptr_loc, idx); 76 } 77 } 78 } 79 80 class ArchivePtrBitmapCleaner: public BitMapClosure { 81 CHeapBitMap* _ptrmap; 82 address* _ptr_base; 83 address _relocatable_base; 84 address _relocatable_end; 85 size_t _max_non_null_offset; 86 87 public: 88 ArchivePtrBitmapCleaner(CHeapBitMap* ptrmap, address* ptr_base, address relocatable_base, address relocatable_end) : 89 _ptrmap(ptrmap), _ptr_base(ptr_base), 90 _relocatable_base(relocatable_base), _relocatable_end(relocatable_end), _max_non_null_offset(0) {} 91 92 bool do_bit(size_t offset) { 93 address* ptr_loc = _ptr_base + offset; 94 address ptr_value = *ptr_loc; 95 if (ptr_value != NULL) { 96 assert(_relocatable_base <= ptr_value && ptr_value < _relocatable_end, "do not point to arbitrary locations!"); 97 if (_max_non_null_offset < offset) { 98 _max_non_null_offset = offset; 99 } 100 } else { 101 _ptrmap->clear_bit(offset); 102 DEBUG_ONLY(log_trace(cds, reloc)("Clearing pointer [" PTR_FORMAT "] -> NULL @ " SIZE_FORMAT_W(9), p2i(ptr_loc), offset)); 103 } 104 105 return true; 106 } 107 108 size_t max_non_null_offset() const { return _max_non_null_offset; } 109 }; 110 111 void ArchivePtrMarker::compact(address relocatable_base, address relocatable_end) { 112 assert(!_compacted, "cannot compact again"); 113 ArchivePtrBitmapCleaner cleaner(_ptrmap, _ptr_base, relocatable_base, relocatable_end); 114 _ptrmap->iterate(&cleaner); 115 compact(cleaner.max_non_null_offset()); 116 } 117 118 void ArchivePtrMarker::compact(size_t max_non_null_offset) { 119 assert(!_compacted, "cannot compact again"); 120 _ptrmap->resize(max_non_null_offset + 1); 121 _compacted = true; 122 } 123 124 #endif // INCLUDE_CDS