1 /*
   2  * Copyright (c) 2015, 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 #include "precompiled.hpp"
  25 #include "gc/shared/collectedHeap.hpp"
  26 #include "gc/z/zAddress.inline.hpp"
  27 #include "gc/z/zForwardingTable.inline.hpp"
  28 #include "gc/z/zHeap.inline.hpp"
  29 #include "gc/z/zLiveMap.inline.hpp"
  30 #include "gc/z/zMark.hpp"
  31 #include "gc/z/zPage.inline.hpp"
  32 #include "gc/z/zPhysicalMemory.inline.hpp"
  33 #include "gc/z/zStat.hpp"
  34 #include "gc/z/zThread.hpp"
  35 #include "gc/z/zUtils.inline.hpp"
  36 #include "logging/log.hpp"
  37 #include "runtime/orderAccess.hpp"
  38 #include "utilities/align.hpp"
  39 #include "utilities/debug.hpp"
  40 #include "utilities/globalDefinitions.hpp"
  41 
  42 static const ZStatCounter ZCounterRelocationContention("Contention", "Relocation Contention", ZStatUnitOpsPerSecond);
  43 
  44 ZPage::ZPage(uint8_t type, ZVirtualMemory vmem, ZPhysicalMemory pmem) :
  45     _type(type),
  46     _pinned(0),
  47     _numa_id((uint8_t)-1),
  48     _seqnum(0),
  49     _virtual(vmem),
  50     _top(start()),
  51     _livemap(object_max_count()),
  52     _refcount(0),
  53     _forwarding(),
  54     _physical(pmem) {
  55   assert(!_physical.is_null(), "Should not be null");
  56   assert(!_virtual.is_null(), "Should not be null");
  57   assert((type == ZPageTypeSmall && size() == ZPageSizeSmall) ||
  58          (type == ZPageTypeMedium && size() == ZPageSizeMedium) ||
  59          (type == ZPageTypeLarge && is_aligned(size(), ZGranuleSize)),
  60          "Page type/size mismatch");
  61 }
  62 
  63 ZPage::~ZPage() {
  64   assert(!is_active(), "Should not be active");
  65   assert(is_detached(), "Should be detached");
  66 }
  67 
  68 void ZPage::reset() {
  69   assert(!is_active(), "Should not be active");
  70   assert(!is_pinned(), "Should not be pinned");
  71   assert(!is_detached(), "Should not be detached");
  72 
  73   _seqnum = ZGlobalSeqNum;
  74   _top = start();
  75   _livemap.reset();
  76 
  77   // Make sure we don't make the page active before
  78   // the reset of the above fields are visible.
  79   OrderAccess::storestore();
  80 
  81   _refcount = 1;
  82 }
  83 
  84 uintptr_t ZPage::relocate_object_inner(uintptr_t from_index, uintptr_t from_offset) {
  85   ZForwardingTableCursor cursor;
  86 
  87   // Lookup address in forwarding table
  88   const ZForwardingTableEntry entry = _forwarding.find(from_index, &cursor);
  89   if (entry.from_index() == from_index) {
  90     // Already relocated, return new address
  91     return entry.to_offset();
  92   }
  93 
  94   // Not found in forwarding table, relocate object
  95   assert(is_object_marked(from_offset), "Should be marked");
  96 
  97   if (is_pinned()) {
  98     // In-place forward
  99     return _forwarding.insert(from_index, from_offset, &cursor);
 100   }
 101 
 102   // Allocate object
 103   const uintptr_t from_good = ZAddress::good(from_offset);
 104   const size_t size = ZUtils::object_size(from_good);
 105   const uintptr_t to_good = ZHeap::heap()->alloc_object_for_relocation(size);
 106   if (to_good == 0) {
 107     // Failed, in-place forward
 108     return _forwarding.insert(from_index, from_offset, &cursor);
 109   }
 110 
 111   // Copy object
 112   ZUtils::object_copy(from_good, to_good, size);
 113 
 114   // Update forwarding table
 115   const uintptr_t to_offset = ZAddress::offset(to_good);
 116   const uintptr_t to_offset_final = _forwarding.insert(from_index, to_offset, &cursor);
 117   if (to_offset_final == to_offset) {
 118     // Relocation succeeded
 119     return to_offset;
 120   }
 121 
 122   // Relocation contention
 123   ZStatInc(ZCounterRelocationContention);
 124   log_trace(gc)("Relocation contention, thread: " PTR_FORMAT " (%s), page: " PTR_FORMAT
 125                 ", entry: " SIZE_FORMAT ", oop: " PTR_FORMAT ", size: " SIZE_FORMAT,
 126                 ZThread::id(), ZThread::name(), p2i(this), cursor, from_good, size);
 127 
 128   // Try undo allocation
 129   ZHeap::heap()->undo_alloc_object_for_relocation(to_good, size);
 130 
 131   return to_offset_final;
 132 }
 133 
 134 uintptr_t ZPage::relocate_object(uintptr_t from) {
 135   assert(ZHeap::heap()->is_relocating(from), "Should be relocating");
 136 
 137   const uintptr_t from_offset = ZAddress::offset(from);
 138   const uintptr_t from_index = (from_offset - start()) >> object_alignment_shift();
 139   const uintptr_t to_offset = relocate_object_inner(from_index, from_offset);
 140   if (from_offset == to_offset) {
 141     // In-place forwarding, pin page
 142     set_pinned();
 143   }
 144 
 145   return ZAddress::good(to_offset);
 146 }
 147 
 148 uintptr_t ZPage::forward_object(uintptr_t from) {
 149   assert(ZHeap::heap()->is_relocating(from), "Should be relocated");
 150 
 151   // Lookup address in forwarding table
 152   const uintptr_t from_offset = ZAddress::offset(from);
 153   const uintptr_t from_index = (from_offset - start()) >> object_alignment_shift();
 154   const ZForwardingTableEntry entry = _forwarding.find(from_index);
 155   assert(entry.from_index() == from_index, "Should be forwarded");
 156 
 157   return ZAddress::good(entry.to_offset());
 158 }
 159 
 160 void ZPage::print_on(outputStream* out) const {
 161   out->print_cr(" %-6s  " PTR_FORMAT " " PTR_FORMAT " " PTR_FORMAT " %s%s%s%s%s%s",
 162                 type_to_string(), start(), top(), end(),
 163                 is_allocating()  ? " Allocating"  : "",
 164                 is_relocatable() ? " Relocatable" : "",
 165                 is_forwarding()  ? " Forwarding"  : "",
 166                 is_pinned()      ? " Pinned"      : "",
 167                 is_detached()    ? " Detached"    : "",
 168                 !is_active()     ? " Inactive"    : "");
 169 }
 170 
 171 void ZPage::print() const {
 172   print_on(tty);
 173 }