hotspot/agent/src/share/classes/sun/jvm/hotspot/code/CodeCache.java
author twisti
Tue, 24 Jul 2012 10:51:00 -0700
changeset 13391 30245956af37
parent 10517 f92c9ff3a15f
child 13963 e5b53c306fb5
permissions -rw-r--r--
7023639: JSR 292 method handle invocation needs a fast path for compiled code 6984705: JSR 292 method handle creation should not go through JNI Summary: remove assembly code for JDK 7 chained method handles Reviewed-by: jrose, twisti, kvn, mhaupt Contributed-by: John Rose <john.r.rose@oracle.com>, Christian Thalinger <christian.thalinger@oracle.com>, Michael Haupt <michael.haupt@oracle.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
9946
b3d5b50e2289 7045513: JSR 292 inlining causes crashes in methodHandleWalk.cpp
never
parents: 5547
diff changeset
     2
 * Copyright (c) 2000, 2011, 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: 4095
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 4095
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: 4095
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.code;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    26
489c9b5090e2 Initial load
duke
parents:
diff changeset
    27
import java.util.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    28
import sun.jvm.hotspot.debugger.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
import sun.jvm.hotspot.memory.*;
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
public class CodeCache {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
  private static AddressField       heapField;
3908
24b55ad4c228 6863023: need non-perm oops in code cache for JSR 292
jrose
parents: 1
diff changeset
    36
  private static AddressField       scavengeRootNMethodsField;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
  private static VirtualConstructor virtualConstructor;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
  private CodeHeap heap;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
  static {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
    VM.registerVMInitializedObserver(new Observer() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
        public void update(Observable o, Object data) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
          initialize(VM.getVM().getTypeDataBase());
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
      });
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
  private static synchronized void initialize(TypeDataBase db) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
    Type type = db.lookupType("CodeCache");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
    heapField = type.getAddressField("_heap");
3908
24b55ad4c228 6863023: need non-perm oops in code cache for JSR 292
jrose
parents: 1
diff changeset
    53
    scavengeRootNMethodsField = type.getAddressField("_scavenge_root_nmethods");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
    virtualConstructor = new VirtualConstructor(db);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
    // Add mappings for all possible CodeBlob subclasses
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
    virtualConstructor.addMapping("BufferBlob", BufferBlob.class);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
    virtualConstructor.addMapping("nmethod", NMethod.class);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
    virtualConstructor.addMapping("RuntimeStub", RuntimeStub.class);
9946
b3d5b50e2289 7045513: JSR 292 inlining causes crashes in methodHandleWalk.cpp
never
parents: 5547
diff changeset
    60
    virtualConstructor.addMapping("AdapterBlob", AdapterBlob.class);
10517
f92c9ff3a15f 7051798: SA-JDI: NPE in Frame.addressOfStackSlot(Frame.java:244)
never
parents: 9946
diff changeset
    61
    virtualConstructor.addMapping("MethodHandlesAdapterBlob", MethodHandlesAdapterBlob.class);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
    virtualConstructor.addMapping("SafepointBlob", SafepointBlob.class);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
    virtualConstructor.addMapping("DeoptimizationBlob", DeoptimizationBlob.class);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
    if (VM.getVM().isServerCompiler()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
      virtualConstructor.addMapping("ExceptionBlob", ExceptionBlob.class);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
      virtualConstructor.addMapping("UncommonTrapBlob", UncommonTrapBlob.class);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
  public CodeCache() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
    heap = (CodeHeap) VMObjectFactory.newObject(CodeHeap.class, heapField.getValue());
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
3908
24b55ad4c228 6863023: need non-perm oops in code cache for JSR 292
jrose
parents: 1
diff changeset
    74
  public NMethod scavengeRootMethods() {
24b55ad4c228 6863023: need non-perm oops in code cache for JSR 292
jrose
parents: 1
diff changeset
    75
    return (NMethod) VMObjectFactory.newObject(NMethod.class, scavengeRootNMethodsField.getValue());
24b55ad4c228 6863023: need non-perm oops in code cache for JSR 292
jrose
parents: 1
diff changeset
    76
  }
24b55ad4c228 6863023: need non-perm oops in code cache for JSR 292
jrose
parents: 1
diff changeset
    77
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
  public boolean contains(Address p) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
    return getHeap().contains(p);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
  /** When VM.getVM().isDebugging() returns true, this behaves like
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
      findBlobUnsafe */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
  public CodeBlob findBlob(Address start) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
    CodeBlob result = findBlobUnsafe(start);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
    if (result == null) return null;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
    if (VM.getVM().isDebugging()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
      return result;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
    // We could potientially look up non_entrant methods
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
    // NOTE: this is effectively a "guarantee", and is slightly different from the one in the VM
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
    if (Assert.ASSERTS_ENABLED) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
      Assert.that(!(result.isZombie() || result.isLockedByVM()), "unsafe access to zombie method");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
    return result;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
  public CodeBlob findBlobUnsafe(Address start) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
    CodeBlob result = null;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
    try {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
      result = (CodeBlob) virtualConstructor.instantiateWrapperFor(getHeap().findStart(start));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
    catch (WrongTypeException wte) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
      Address cbAddr = null;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
      try {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
        cbAddr = getHeap().findStart(start);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
      catch (Exception findEx) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
        findEx.printStackTrace();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
      String message = "Couldn't deduce type of CodeBlob ";
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
      if (cbAddr != null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
        message = message + "@" + cbAddr + " ";
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
      message = message + "for PC=" + start;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
      throw new RuntimeException(message, wte);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
    if (result == null) return null;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
    if (Assert.ASSERTS_ENABLED) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
      // The HeapBlock that contains this blob is outside of the blob
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
      // but it shouldn't be an error to find a blob based on the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
      // pointer to the HeapBlock.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
      Assert.that(result.blobContains(start) || result.blobContains(start.addOffsetTo(8)),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
                                                                    "found wrong CodeBlob");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
    return result;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
  public NMethod findNMethod(Address start) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
    CodeBlob cb = findBlob(start);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
    if (Assert.ASSERTS_ENABLED) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
      Assert.that(cb == null || cb.isNMethod(), "did not find an nmethod");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
    return (NMethod) cb;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
  public NMethod findNMethodUnsafe(Address start) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
    CodeBlob cb = findBlobUnsafe(start);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
    if (Assert.ASSERTS_ENABLED) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
      Assert.that(cb == null || cb.isNMethod(), "did not find an nmethod");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
    return (NMethod) cb;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
  /** Routine for instantiating appropriately-typed wrapper for a
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
      CodeBlob. Used by CodeCache, Runtime1, etc. */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
  public CodeBlob createCodeBlobWrapper(Address codeBlobAddr) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
    try {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
      return (CodeBlob) virtualConstructor.instantiateWrapperFor(codeBlobAddr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
    catch (Exception e) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
      String message = "Unable to deduce type of CodeBlob from address " + codeBlobAddr +
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
                       " (expected type nmethod, RuntimeStub, ";
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
      if (VM.getVM().isClientCompiler()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
        message = message + " or ";
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
      message = message + "SafepointBlob";
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
      if (VM.getVM().isServerCompiler()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
        message = message + ", DeoptimizationBlob, or ExceptionBlob";
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
      message = message + ")";
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
      throw new RuntimeException(message);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
  public void iterate(CodeCacheVisitor visitor) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
    CodeHeap heap = getHeap();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
    Address ptr = heap.begin();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
    Address end = heap.end();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
    visitor.prologue(ptr, end);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
    CodeBlob lastBlob = null;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
    while (ptr != null && ptr.lessThan(end)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
      try {
4095
6e0acfda1d47 6892186: SA does not dump debug info for scalar replaced objects
kvn
parents: 3908
diff changeset
   178
        // Use findStart to get a pointer inside blob other findBlob asserts
6e0acfda1d47 6892186: SA does not dump debug info for scalar replaced objects
kvn
parents: 3908
diff changeset
   179
        CodeBlob blob = findBlobUnsafe(heap.findStart(ptr));
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
        if (blob != null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
          visitor.visit(blob);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
          if (blob == lastBlob) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
            throw new InternalError("saw same blob twice");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
          }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
          lastBlob = blob;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
      } catch (RuntimeException e) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   188
        e.printStackTrace();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   190
      Address next = heap.nextBlock(ptr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   191
      if (next != null && next.lessThan(ptr)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   192
        throw new InternalError("pointer moved backwards");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   193
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   194
      ptr = next;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   195
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   196
    visitor.epilogue();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   197
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   198
489c9b5090e2 Initial load
duke
parents:
diff changeset
   199
  //--------------------------------------------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   200
  // Internals only below this point
489c9b5090e2 Initial load
duke
parents:
diff changeset
   201
  //
489c9b5090e2 Initial load
duke
parents:
diff changeset
   202
489c9b5090e2 Initial load
duke
parents:
diff changeset
   203
  private CodeHeap getHeap() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   204
    return heap;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   205
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   206
}