src/hotspot/share/memory/archiveUtils.cpp
changeset 59070 22ee476cc664
equal deleted inserted replaced
59069:e0d59f0c2b7d 59070:22ee476cc664
       
     1 /*
       
     2  * Copyright (c) 2019, 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     if (value != NULL) {
       
    62       assert(uintx(ptr_loc) % sizeof(intptr_t) == 0, "pointers must be stored in aligned addresses");
       
    63       size_t idx = ptr_loc - _ptr_base;
       
    64       if (_ptrmap->size() <= idx) {
       
    65         _ptrmap->resize((idx + 1) * 2);
       
    66       }
       
    67       assert(idx < _ptrmap->size(), "must be");
       
    68       _ptrmap->set_bit(idx);
       
    69       //tty->print_cr("Marking pointer [%p] -> %p @ " SIZE_FORMAT_W(9), ptr_loc, *ptr_loc, idx);
       
    70     }
       
    71   }
       
    72 }
       
    73 
       
    74 class ArchivePtrBitmapCleaner: public BitMapClosure {
       
    75   CHeapBitMap* _ptrmap;
       
    76   address* _ptr_base;
       
    77   address  _relocatable_base;
       
    78   address  _relocatable_end;
       
    79   size_t   _max_non_null_offset;
       
    80 
       
    81 public:
       
    82   ArchivePtrBitmapCleaner(CHeapBitMap* ptrmap, address* ptr_base, address relocatable_base, address relocatable_end) :
       
    83     _ptrmap(ptrmap), _ptr_base(ptr_base),
       
    84     _relocatable_base(relocatable_base), _relocatable_end(relocatable_end), _max_non_null_offset(0) {}
       
    85 
       
    86   bool do_bit(size_t offset) {
       
    87     address* ptr_loc = _ptr_base + offset;
       
    88     address  ptr_value = *ptr_loc;
       
    89     if (ptr_value != NULL) {
       
    90       assert(_relocatable_base <= ptr_value && ptr_value < _relocatable_end, "do not point to arbitrary locations!");
       
    91       if (_max_non_null_offset < offset) {
       
    92         _max_non_null_offset = offset;
       
    93       }
       
    94     } else {
       
    95       _ptrmap->clear_bit(offset);
       
    96       DEBUG_ONLY(log_trace(cds, reloc)("Clearing pointer [" PTR_FORMAT  "] -> NULL @ " SIZE_FORMAT_W(9), p2i(ptr_loc), offset));
       
    97     }
       
    98 
       
    99     return true;
       
   100   }
       
   101 
       
   102   size_t max_non_null_offset() const { return _max_non_null_offset; }
       
   103 };
       
   104 
       
   105 void ArchivePtrMarker::compact(address relocatable_base, address relocatable_end) {
       
   106   assert(!_compacted, "cannot compact again");
       
   107   ArchivePtrBitmapCleaner cleaner(_ptrmap, _ptr_base, relocatable_base, relocatable_end);
       
   108   _ptrmap->iterate(&cleaner);
       
   109   compact(cleaner.max_non_null_offset());
       
   110 }
       
   111 
       
   112 void ArchivePtrMarker::compact(size_t max_non_null_offset) {
       
   113   assert(!_compacted, "cannot compact again");
       
   114   _ptrmap->resize(max_non_null_offset + 1);
       
   115   _compacted = true;
       
   116 }
       
   117 
       
   118 #endif // INCLUDE_CDS