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 [" PTR_FORMAT "] -> " PTR_FORMAT " @ " SIZE_FORMAT_W(5), p2i(ptr_loc), p2i(*ptr_loc), idx);
  76     }
  77   }
  78 }
  79 
  80 void ArchivePtrMarker::clear_pointer(address* ptr_loc) {
  81   assert(_ptrmap != NULL, "not initialized");
  82   assert(!_compacted, "cannot clear anymore");
  83 
  84   assert(_ptr_base <= ptr_loc && ptr_loc < _ptr_end, "must be");
  85   assert(uintx(ptr_loc) % sizeof(intptr_t) == 0, "pointers must be stored in aligned addresses");
  86   size_t idx = ptr_loc - _ptr_base;
  87   assert(idx < _ptrmap->size(), "cannot clear pointers that have not been marked");
  88   _ptrmap->clear_bit(idx);
  89   //tty->print_cr("Clearing pointer [" PTR_FORMAT "] -> " PTR_FORMAT " @ " SIZE_FORMAT_W(5), p2i(ptr_loc), p2i(*ptr_loc), idx);
  90 }
  91 
  92 class ArchivePtrBitmapCleaner: public BitMapClosure {
  93   CHeapBitMap* _ptrmap;
  94   address* _ptr_base;
  95   address  _relocatable_base;
  96   address  _relocatable_end;
  97   size_t   _max_non_null_offset;
  98 
  99 public:
 100   ArchivePtrBitmapCleaner(CHeapBitMap* ptrmap, address* ptr_base, address relocatable_base, address relocatable_end) :
 101     _ptrmap(ptrmap), _ptr_base(ptr_base),
 102     _relocatable_base(relocatable_base), _relocatable_end(relocatable_end), _max_non_null_offset(0) {}
 103 
 104   bool do_bit(size_t offset) {
 105     address* ptr_loc = _ptr_base + offset;
 106     address  ptr_value = *ptr_loc;
 107     if (ptr_value != NULL) {
 108       assert(_relocatable_base <= ptr_value && ptr_value < _relocatable_end, "do not point to arbitrary locations!");
 109       if (_max_non_null_offset < offset) {
 110         _max_non_null_offset = offset;
 111       }
 112     } else {
 113       _ptrmap->clear_bit(offset);
 114       DEBUG_ONLY(log_trace(cds, reloc)("Clearing pointer [" PTR_FORMAT  "] -> NULL @ " SIZE_FORMAT_W(9), p2i(ptr_loc), offset));
 115     }
 116 
 117     return true;
 118   }
 119 
 120   size_t max_non_null_offset() const { return _max_non_null_offset; }
 121 };
 122 
 123 void ArchivePtrMarker::compact(address relocatable_base, address relocatable_end) {
 124   assert(!_compacted, "cannot compact again");
 125   ArchivePtrBitmapCleaner cleaner(_ptrmap, _ptr_base, relocatable_base, relocatable_end);
 126   _ptrmap->iterate(&cleaner);
 127   compact(cleaner.max_non_null_offset());
 128 }
 129 
 130 void ArchivePtrMarker::compact(size_t max_non_null_offset) {
 131   assert(!_compacted, "cannot compact again");
 132   _ptrmap->resize(max_non_null_offset + 1);
 133   _compacted = true;
 134 }
 135 
 136 #endif // INCLUDE_CDS