src/jdk.hotspot.agent/share/classes/sun/jvm/hotspot/oops/Symbol.java
author phh
Sat, 30 Nov 2019 14:33:05 -0800
changeset 59330 5b96c12f909d
parent 51531 948c62200f8c
permissions -rw-r--r--
8234541: C1 emits an empty message when it inlines successfully Summary: Use "inline" as the message when successfull Reviewed-by: thartmann, mdoerr Contributed-by: navy.xliu@gmail.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
50929
ef57cfcd22ff 8205534: Remove SymbolTable dependency from serviceability agent
coleenp
parents: 47216
diff changeset
     2
 * Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     4
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
489c9b5090e2 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     8
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
489c9b5090e2 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    14
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
489c9b5090e2 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    18
 *
5547
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
    21
 * questions.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
489c9b5090e2 Initial load
duke
parents:
diff changeset
    25
package sun.jvm.hotspot.oops;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    26
489c9b5090e2 Initial load
duke
parents:
diff changeset
    27
import java.io.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    28
import java.util.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
import sun.jvm.hotspot.debugger.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
import sun.jvm.hotspot.runtime.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
import sun.jvm.hotspot.types.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
import sun.jvm.hotspot.utilities.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
// A Symbol is a canonicalized string.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
// All Symbols reside in global symbolTable.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
8076
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 5547
diff changeset
    37
public class Symbol extends VMObject {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
  static {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
    VM.registerVMInitializedObserver(new Observer() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
        public void update(Observable o, Object data) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
          initialize(VM.getVM().getTypeDataBase());
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
      });
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
  private static synchronized void initialize(TypeDataBase db) throws WrongTypeException {
8076
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 5547
diff changeset
    47
    Type type  = db.lookupType("Symbol");
51179
516acf6956a2 8207359: Make SymbolTable increment_refcount disallow zero
coleenp
parents: 50929
diff changeset
    48
    lengthAndRefcount = type.getCIntegerField("_length_and_refcount");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
    baseOffset = type.getField("_body").getOffset();
31616
01c68517607d 8130135: backout 8087143 due to failures in 8130115
dcubed
parents: 31614
diff changeset
    50
    idHash = type.getCIntegerField("_identity_hash");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
8076
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 5547
diff changeset
    53
  public static Symbol create(Address addr) {
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 5547
diff changeset
    54
    if (addr == null) {
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 5547
diff changeset
    55
      return null;
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 5547
diff changeset
    56
    }
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 5547
diff changeset
    57
    return new Symbol(addr);
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 5547
diff changeset
    58
  }
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 5547
diff changeset
    59
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 5547
diff changeset
    60
  Symbol(Address addr) {
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 5547
diff changeset
    61
    super(addr);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
  public boolean isSymbol()            { return true; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
  private static long baseOffset; // tells where the array part starts
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
  // Fields
51179
516acf6956a2 8207359: Make SymbolTable increment_refcount disallow zero
coleenp
parents: 50929
diff changeset
    69
  private static CIntegerField lengthAndRefcount;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
  // Accessors for declared fields
51179
516acf6956a2 8207359: Make SymbolTable increment_refcount disallow zero
coleenp
parents: 50929
diff changeset
    72
  public long getLength() {
516acf6956a2 8207359: Make SymbolTable increment_refcount disallow zero
coleenp
parents: 50929
diff changeset
    73
    long i = lengthAndRefcount.getValue(this.addr);
516acf6956a2 8207359: Make SymbolTable increment_refcount disallow zero
coleenp
parents: 50929
diff changeset
    74
    return (i >> 16) & 0xffff;
516acf6956a2 8207359: Make SymbolTable increment_refcount disallow zero
coleenp
parents: 50929
diff changeset
    75
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
  public byte getByteAt(long index) {
8076
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 5547
diff changeset
    78
    return addr.getJByteAt(baseOffset + index);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
  }
32357
43087bc6dd04 8130115: REDO - Reduce Symbol::_identity_hash to 2 bytes
minqi
parents: 31616
diff changeset
    80
  // _identity_hash is a short
31616
01c68517607d 8130135: backout 8087143 due to failures in 8130115
dcubed
parents: 31614
diff changeset
    81
  private static CIntegerField idHash;
8076
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 5547
diff changeset
    82
51531
948c62200f8c 8204308: SA: serviceability/sa/TestInstanceKlassSize*.java fails when running in CDS mode
jgeorge
parents: 51179
diff changeset
    83
  public long identityHash() {
32357
43087bc6dd04 8130115: REDO - Reduce Symbol::_identity_hash to 2 bytes
minqi
parents: 31616
diff changeset
    84
    long addr_value = getAddress().asLongValue();
51531
948c62200f8c 8204308: SA: serviceability/sa/TestInstanceKlassSize*.java fails when running in CDS mode
jgeorge
parents: 51179
diff changeset
    85
    long addr_bits =
948c62200f8c 8204308: SA: serviceability/sa/TestInstanceKlassSize*.java fails when running in CDS mode
jgeorge
parents: 51179
diff changeset
    86
      (addr_value >> (VM.getVM().getLogMinObjAlignmentInBytes() + 3)) & 0xffffffffL;
32357
43087bc6dd04 8130115: REDO - Reduce Symbol::_identity_hash to 2 bytes
minqi
parents: 31616
diff changeset
    87
    int  length = (int)getLength();
43087bc6dd04 8130115: REDO - Reduce Symbol::_identity_hash to 2 bytes
minqi
parents: 31616
diff changeset
    88
    int  byte0 = getByteAt(0);
43087bc6dd04 8130115: REDO - Reduce Symbol::_identity_hash to 2 bytes
minqi
parents: 31616
diff changeset
    89
    int  byte1 = getByteAt(1);
51531
948c62200f8c 8204308: SA: serviceability/sa/TestInstanceKlassSize*.java fails when running in CDS mode
jgeorge
parents: 51179
diff changeset
    90
    long id_hash = 0xffffL & (long)idHash.getValue(this.addr);
948c62200f8c 8204308: SA: serviceability/sa/TestInstanceKlassSize*.java fails when running in CDS mode
jgeorge
parents: 51179
diff changeset
    91
    return (id_hash |
948c62200f8c 8204308: SA: serviceability/sa/TestInstanceKlassSize*.java fails when running in CDS mode
jgeorge
parents: 51179
diff changeset
    92
      ((addr_bits ^ (length << 8) ^ ((byte0 << 8) | byte1)) << 16)) & 0xffffffffL;
32357
43087bc6dd04 8130115: REDO - Reduce Symbol::_identity_hash to 2 bytes
minqi
parents: 31616
diff changeset
    93
  }
8076
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 5547
diff changeset
    94
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
  public boolean equals(byte[] modUTF8Chars) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
    int l = (int) getLength();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
    if (l != modUTF8Chars.length) return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
    while (l-- > 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
      if (modUTF8Chars[l] != getByteAt(l)) return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
    if (Assert.ASSERTS_ENABLED) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
      Assert.that(l == -1, "we should be at the beginning");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
    return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
50929
ef57cfcd22ff 8205534: Remove SymbolTable dependency from serviceability agent
coleenp
parents: 47216
diff changeset
   107
  public boolean equals(String string) {
ef57cfcd22ff 8205534: Remove SymbolTable dependency from serviceability agent
coleenp
parents: 47216
diff changeset
   108
    return asString().equals(string);
ef57cfcd22ff 8205534: Remove SymbolTable dependency from serviceability agent
coleenp
parents: 47216
diff changeset
   109
  }
ef57cfcd22ff 8205534: Remove SymbolTable dependency from serviceability agent
coleenp
parents: 47216
diff changeset
   110
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
  public byte[] asByteArray() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
    int length = (int) getLength();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
    byte [] result = new byte [length];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
    for (int index = 0; index < length; index++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
      result[index] = getByteAt(index);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
    return result;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
  public String asString() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
    // Decode the byte array and return the string.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
    try {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
      return readModifiedUTF8(asByteArray());
8076
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 5547
diff changeset
   124
    } catch(Exception e) {
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 5547
diff changeset
   125
      System.err.println(addr);
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 5547
diff changeset
   126
      e.printStackTrace();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
      return null;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
  public boolean startsWith(String str) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
    return asString().startsWith(str);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
  public void printValueOn(PrintStream tty) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
    tty.print("#" + asString());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
  /** Note: this comparison is used for vtable sorting only; it
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
      doesn't matter what order it defines, as long as it is a total,
8076
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 5547
diff changeset
   141
      time-invariant order Since Symbol* are in C_HEAP, their
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
      relative order in memory never changes, so use address
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
      comparison for speed. */
42590
48d874bf85fb 8169232: SA: TestCpoolForInvokeDynamic.java fails with sun.jvm.hotspot.debugger.DebuggerException: binary search bug: should have found entry 1
vtewari
parents: 35217
diff changeset
   144
  public long fastCompare(Symbol other) {
48d874bf85fb 8169232: SA: TestCpoolForInvokeDynamic.java fails with sun.jvm.hotspot.debugger.DebuggerException: binary search bug: should have found entry 1
vtewari
parents: 35217
diff changeset
   145
    return addr.minus(other.addr);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
  private static String readModifiedUTF8(byte[] buf) throws IOException {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
    final int len = buf.length;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
    byte[] tmp = new byte[len + 2];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
    // write modified UTF-8 length as short in big endian
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
    tmp[0] = (byte) ((len >>> 8) & 0xFF);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
    tmp[1] = (byte) ((len >>> 0) & 0xFF);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
    // copy the data
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
    System.arraycopy(buf, 0, tmp, 2, len);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
    DataInputStream dis = new DataInputStream(new ByteArrayInputStream(tmp));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
    return dis.readUTF();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
}