Path: blob/master/src/jdk.hotspot.agent/share/classes/sun/jvm/hotspot/utilities/CompactHashTable.java
41161 views
/*1* Copyright (c) 2015, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324package sun.jvm.hotspot.utilities;2526import java.util.*;27import sun.jvm.hotspot.debugger.*;28import sun.jvm.hotspot.oops.*;29import sun.jvm.hotspot.types.*;30import sun.jvm.hotspot.runtime.*;31import sun.jvm.hotspot.utilities.*;32import sun.jvm.hotspot.utilities.Observable;33import sun.jvm.hotspot.utilities.Observer;3435public class CompactHashTable extends VMObject {36static {37VM.registerVMInitializedObserver(new Observer() {38public void update(Observable o, Object data) {39initialize(VM.getVM().getTypeDataBase());40}41});42}4344private static synchronized void initialize(TypeDataBase db) throws WrongTypeException {45Type type = db.lookupType("SymbolCompactHashTable");46baseAddressField = type.getAddressField("_base_address");47bucketCountField = type.getCIntegerField("_bucket_count");48entryCountField = type.getCIntegerField("_entry_count");49bucketsField = type.getAddressField("_buckets");50entriesField = type.getAddressField("_entries");51uintSize = db.lookupType("u4").getSize();52}5354// Fields55private static CIntegerField bucketCountField;56private static CIntegerField entryCountField;57private static AddressField baseAddressField;58private static AddressField bucketsField;59private static AddressField entriesField;60private static long uintSize;6162private static int BUCKET_OFFSET_MASK = 0x3FFFFFFF;63private static int BUCKET_TYPE_SHIFT = 30;64private static int VALUE_ONLY_BUCKET_TYPE = 1;6566public CompactHashTable(Address addr) {67super(addr);68}6970private int bucketCount() {71return (int)bucketCountField.getValue(addr);72}7374private boolean isValueOnlyBucket(int bucket_info) {75return (bucket_info >> BUCKET_TYPE_SHIFT) == VALUE_ONLY_BUCKET_TYPE;76}7778private int bucketOffset(int bucket_info) {79return bucket_info & BUCKET_OFFSET_MASK;80}8182public Symbol probe(byte[] name, long hash) {83if (bucketCount() <= 0) {84// This CompactHashTable is not in use85return null;86}8788long symOffset;89Symbol sym;90Address baseAddress = baseAddressField.getValue(addr);91Address bucket = bucketsField.getValue(addr);92long index = hash % bucketCount();93int bucketInfo = (int)bucket.getCIntegerAt(index * uintSize, uintSize, true);94int bucketOffset = bucketOffset(bucketInfo);95int nextBucketInfo = (int)bucket.getCIntegerAt((index+1) * uintSize, uintSize, true);96int nextBucketOffset = bucketOffset(nextBucketInfo);9798Address entry = entriesField.getValue(addr).addOffsetTo(bucketOffset * uintSize);99100if (isValueOnlyBucket(bucketInfo)) {101symOffset = entry.getCIntegerAt(0, uintSize, true);102sym = Symbol.create(baseAddress.addOffsetTo(symOffset));103if (sym.equals(name)) {104return sym;105}106} else {107Address entryMax = entriesField.getValue(addr).addOffsetTo(nextBucketOffset * uintSize);108while (entry.lessThan(entryMax)) {109long symHash = entry.getCIntegerAt(0, uintSize, true);110if (symHash == hash) {111symOffset = entry.getCIntegerAt(uintSize, uintSize, true);112Address symAddr = baseAddress.addOffsetTo(symOffset);113sym = Symbol.create(symAddr);114if (sym.equals(name)) {115return sym;116}117}118entry = entry.addOffsetTo(2 * uintSize);119}120}121return null;122}123124public interface SymbolVisitor {125public void visit(Symbol sym);126}127128public void symbolsDo(SymbolVisitor visitor) {129long symOffset;130Symbol sym;131Address baseAddress = baseAddressField.getValue(addr);132Address bucket = bucketsField.getValue(addr);133for (long index = 0; index < bucketCount(); index++) {134int bucketInfo = (int)bucket.getCIntegerAt(index * uintSize, uintSize, true);135int bucketOffset = bucketOffset(bucketInfo);136int nextBucketInfo = (int)bucket.getCIntegerAt((index+1) * uintSize, uintSize, true);137int nextBucketOffset = bucketOffset(nextBucketInfo);138139Address entry = entriesField.getValue(addr).addOffsetTo(bucketOffset * uintSize);140141if (isValueOnlyBucket(bucketInfo)) {142symOffset = entry.getCIntegerAt(0, uintSize, true);143sym = Symbol.create(baseAddress.addOffsetTo(symOffset));144visitor.visit(sym);145} else {146Address entryMax = entriesField.getValue(addr).addOffsetTo(nextBucketOffset * uintSize);147while (entry.lessThan(entryMax)) {148symOffset = entry.getCIntegerAt(uintSize, uintSize, true);149Address symAddr = baseAddress.addOffsetTo(symOffset);150sym = Symbol.create(symAddr);151visitor.visit(sym);152entry = entry.addOffsetTo(2 * uintSize);153}154}155}156}157}158159160161