1 /*
   2  * Copyright (c) 2018, 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 package sun.jvm.hotspot.gc.z;
  26 
  27 import sun.jvm.hotspot.debugger.Address;
  28 import sun.jvm.hotspot.runtime.VM;
  29 import sun.jvm.hotspot.runtime.VMObject;
  30 import sun.jvm.hotspot.types.AddressField;
  31 import sun.jvm.hotspot.types.CIntegerField;
  32 import sun.jvm.hotspot.types.Type;
  33 import sun.jvm.hotspot.types.TypeDataBase;
  34 
  35 public class ZForwardingTable extends VMObject {
  36     private static AddressField tableField;
  37     private static CIntegerField sizeField;
  38 
  39     static {
  40         VM.registerVMInitializedObserver((o, d) -> initialize(VM.getVM().getTypeDataBase()));
  41     }
  42 
  43     static private synchronized void initialize(TypeDataBase db) {
  44         Type type = db.lookupType("ZForwardingTable");
  45 
  46         tableField = type.getAddressField("_table");
  47         sizeField = type.getCIntegerField("_size");
  48     }
  49 
  50     public ZForwardingTable(Address addr) {
  51         super(addr);
  52     }
  53 
  54     Address table() {
  55         return tableField.getAddress(addr);
  56     }
  57 
  58     long size() {
  59         return sizeField.getJLong(addr);
  60     }
  61 
  62     ZForwardingTableEntry at(ZForwardingTableCursor cursor) {
  63         return new ZForwardingTableEntry(table().getAddressAt(cursor._value * VM.getVM().getBytesPerLong()));
  64     }
  65 
  66     ZForwardingTableEntry first(long from_index, ZForwardingTableCursor cursor) {
  67         long mask = size() - 1;
  68         long hash = ZHash.uint32_to_uint32(from_index);
  69         cursor._value = hash & mask;
  70         return at(cursor);
  71     }
  72 
  73     ZForwardingTableEntry next(ZForwardingTableCursor cursor) {
  74         long mask = size() - 1;
  75         cursor._value = (cursor._value + 1) & mask;
  76         return at(cursor);
  77     }
  78 
  79     ZForwardingTableEntry find(long from_index, ZForwardingTableCursor cursor) {
  80         // Reading entries in the table races with the atomic cas done for
  81         // insertion into the table. This is safe because each entry is at
  82         // most updated once (from -1 to something else).
  83         ZForwardingTableEntry entry = first(from_index, cursor);
  84         while (!entry.is_empty()) {
  85             if (entry.from_index() == from_index) {
  86                 // Match found, return matching entry
  87                 return entry;
  88             }
  89 
  90             entry = next(cursor);
  91         }
  92 
  93         // Match not found, return empty entry
  94         return entry;
  95     }
  96 
  97     ZForwardingTableEntry find(long from_index) {
  98         ZForwardingTableCursor dummy = new ZForwardingTableCursor();
  99         return find(from_index, dummy);
 100     }
 101 
 102     void dump() {
 103         long s = size();
 104         long count = 0;
 105         System.out.println("Dumping ZForwardingTable[" + s + "]:");
 106         ZForwardingTableCursor cursor = new ZForwardingTableCursor();
 107         for (long i = 0; i < s; i++) {
 108             cursor._value = i;
 109             ZForwardingTableEntry entry = at(cursor);
 110             if (!entry.is_empty()) {
 111                 long hash = ZHash.uint32_to_uint32(entry.from_index());
 112                 System.out.println(i + " " + count + " " + entry + " hash: " + hash + " masked_hash: " + (hash & (s - 1)));
 113                 count++;
 114             }
 115         }
 116     }
 117 }