8150778: Reduce Throwable.getStackTrace() calls to the JVM
authorcoleenp
Thu, 10 Mar 2016 14:20:59 -0500
changeset 37313 b0c043d9ad18
parent 37312 064d4853a909
child 37314 341fa2e9011f
8150778: Reduce Throwable.getStackTrace() calls to the JVM Summary: replace JVM_GetStackTraceDepth and JVM_GetStackTraceElement, with JVM_GetStackTraceElements that gets all the elements in the StackTraceElement[] Reviewed-by: dfuchs, mchung, shade, hseigel
jdk/make/mapfiles/libjava/mapfile-vers
jdk/make/mapfiles/libjava/reorder-sparc
jdk/make/mapfiles/libjava/reorder-sparcv9
jdk/make/mapfiles/libjava/reorder-x86
jdk/src/java.base/share/classes/java/lang/StackTraceElement.java
jdk/src/java.base/share/classes/java/lang/Throwable.java
jdk/src/java.base/share/native/include/jvm.h
jdk/src/java.base/share/native/libjava/Throwable.c
--- a/jdk/make/mapfiles/libjava/mapfile-vers	Wed Mar 09 14:18:11 2016 +0100
+++ b/jdk/make/mapfiles/libjava/mapfile-vers	Thu Mar 10 14:20:59 2016 -0500
@@ -227,8 +227,7 @@
 		Java_java_lang_System_setOut0;
 		Java_java_lang_Thread_registerNatives;
 		Java_java_lang_Throwable_fillInStackTrace;
-                Java_java_lang_Throwable_getStackTraceDepth;
-                Java_java_lang_Throwable_getStackTraceElement;
+                Java_java_lang_Throwable_getStackTraceElements;
 		Java_java_security_AccessController_doPrivileged__Ljava_security_PrivilegedAction_2;
 		Java_java_security_AccessController_doPrivileged__Ljava_security_PrivilegedAction_2Ljava_security_AccessControlContext_2;
 		Java_java_security_AccessController_doPrivileged__Ljava_security_PrivilegedExceptionAction_2;
--- a/jdk/make/mapfiles/libjava/reorder-sparc	Wed Mar 09 14:18:11 2016 +0100
+++ b/jdk/make/mapfiles/libjava/reorder-sparc	Thu Mar 10 14:20:59 2016 -0500
@@ -78,8 +78,7 @@
 text: .text%JNU_GetEnv;
 text: .text%Java_java_io_UnixFileSystem_checkAccess;
 text: .text%Java_java_lang_reflect_Array_newArray;
-text: .text%Java_java_lang_Throwable_getStackTraceDepth;
-text: .text%Java_java_lang_Throwable_getStackTraceElement;
+text: .text%Java_java_lang_Throwable_getStackTraceElements;
 text: .text%throwFileNotFoundException;
 text: .text%JNU_NotifyAll;
 # Test LoadFrame
--- a/jdk/make/mapfiles/libjava/reorder-sparcv9	Wed Mar 09 14:18:11 2016 +0100
+++ b/jdk/make/mapfiles/libjava/reorder-sparcv9	Thu Mar 10 14:20:59 2016 -0500
@@ -74,8 +74,7 @@
 text: .text%JNU_GetEnv;
 text: .text%Java_java_io_UnixFileSystem_checkAccess;
 text: .text%Java_java_lang_reflect_Array_newArray;
-text: .text%Java_java_lang_Throwable_getStackTraceDepth;
-text: .text%Java_java_lang_Throwable_getStackTraceElement;
+text: .text%Java_java_lang_Throwable_getStackTraceElements;
 text: .text%throwFileNotFoundException: OUTPUTDIR/io_util.o;
 text: .text%JNU_NotifyAll;
 # Test LoadFrame
--- a/jdk/make/mapfiles/libjava/reorder-x86	Wed Mar 09 14:18:11 2016 +0100
+++ b/jdk/make/mapfiles/libjava/reorder-x86	Thu Mar 10 14:20:59 2016 -0500
@@ -78,8 +78,7 @@
 text: .text%Java_sun_reflect_NativeMethodAccessorImpl_invoke0;
 text: .text%Java_java_io_FileInputStream_available;
 text: .text%Java_java_lang_reflect_Array_newArray;
-text: .text%Java_java_lang_Throwable_getStackTraceDepth;
-text: .text%Java_java_lang_Throwable_getStackTraceElement;
+text: .text%Java_java_lang_Throwable_getStackTraceElements;
 text: .text%Java_java_lang_System_identityHashCode;
 text: .text%JNU_NotifyAll;
 # Test LoadFrame
--- a/jdk/src/java.base/share/classes/java/lang/StackTraceElement.java	Wed Mar 09 14:18:11 2016 +0100
+++ b/jdk/src/java.base/share/classes/java/lang/StackTraceElement.java	Thu Mar 10 14:20:59 2016 -0500
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -74,6 +74,12 @@
         this.lineNumber     = lineNumber;
     }
 
+
+    /**
+     * Creates an empty stack frame element to be filled in by Throwable.
+     */
+    StackTraceElement() { }
+
     /**
      * Returns the name of the source file containing the execution point
      * represented by this stack trace element.  Generally, this corresponds
--- a/jdk/src/java.base/share/classes/java/lang/Throwable.java	Wed Mar 09 14:18:11 2016 +0100
+++ b/jdk/src/java.base/share/classes/java/lang/Throwable.java	Thu Mar 10 14:20:59 2016 -0500
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1994, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1994, 2016, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -118,7 +118,7 @@
     private static final long serialVersionUID = -3042686055658047285L;
 
     /**
-     * Native code saves some indication of the stack backtrace in this slot.
+     * The JVM saves some indication of the stack backtrace in this slot.
      */
     private transient Object backtrace;
 
@@ -211,6 +211,11 @@
      */
     private StackTraceElement[] stackTrace = UNASSIGNED_STACK;
 
+    /**
+     * The JVM code sets the depth of the backtrace for later retrieval
+     */
+    private transient int depth;
+
     // Setting this static field introduces an acceptable
     // initialization dependency on a few java.util classes.
     private static final List<Throwable> SUPPRESSED_SENTINEL = Collections.emptyList();
@@ -828,10 +833,11 @@
             if (backtrace instanceof StackStreamFactory.StackTrace) {
                 stackTrace = ((StackStreamFactory.StackTrace)backtrace).getStackTraceElements();
             } else {
-                int depth = getStackTraceDepth();
                 stackTrace = new StackTraceElement[depth];
-                for (int i = 0; i < depth; i++)
-                    stackTrace[i] = getStackTraceElement(i);
+                for (int i = 0; i < depth; i++) {
+                    stackTrace[i] = new StackTraceElement();
+                }
+                getStackTraceElements(stackTrace);
             }
         } else if (stackTrace == null) {
             return UNASSIGNED_STACK;
@@ -884,23 +890,11 @@
     }
 
     /**
-     * Returns the number of elements in the stack trace (or 0 if the stack
-     * trace is unavailable).
-     *
-     * package-protection for use by SharedSecrets.
+     * Gets the stack trace elements.
+     * @param  elements
+     * @throws IndexOutOfBoundsException if {@code elements.length != depth }
      */
-    native int getStackTraceDepth();
-
-    /**
-     * Returns the specified element of the stack trace.
-     *
-     * package-protection for use by SharedSecrets.
-     *
-     * @param index index of the element to return.
-     * @throws IndexOutOfBoundsException if {@code index < 0 ||
-     *         index >= getStackTraceDepth() }
-     */
-    native StackTraceElement getStackTraceElement(int index);
+    private native void getStackTraceElements(StackTraceElement[] elements);
 
     /**
      * Reads a {@code Throwable} from a stream, enforcing
--- a/jdk/src/java.base/share/native/include/jvm.h	Wed Mar 09 14:18:11 2016 +0100
+++ b/jdk/src/java.base/share/native/include/jvm.h	Thu Mar 10 14:20:59 2016 -0500
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -171,11 +171,8 @@
 JNIEXPORT void JNICALL
 JVM_FillInStackTrace(JNIEnv *env, jobject throwable);
 
-JNIEXPORT jint JNICALL
-JVM_GetStackTraceDepth(JNIEnv *env, jobject throwable);
-
-JNIEXPORT jobject JNICALL
-JVM_GetStackTraceElement(JNIEnv *env, jobject throwable, jint index);
+JNIEXPORT void JNICALL
+JVM_GetStackTraceElements(JNIEnv *env, jobject throwable, jobjectArray elements);
 
 /*
  * java.lang.StackWalker
--- a/jdk/src/java.base/share/native/libjava/Throwable.c	Wed Mar 09 14:18:11 2016 +0100
+++ b/jdk/src/java.base/share/native/libjava/Throwable.c	Thu Mar 10 14:20:59 2016 -0500
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1994, 2011, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1994, 2016, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -50,15 +50,9 @@
     return throwable;
 }
 
-JNIEXPORT jint JNICALL
-Java_java_lang_Throwable_getStackTraceDepth(JNIEnv *env, jobject throwable)
+JNIEXPORT void JNICALL
+Java_java_lang_Throwable_getStackTraceElements(JNIEnv *env,
+                                               jobject throwable, jobjectArray elements)
 {
-    return JVM_GetStackTraceDepth(env, throwable);
+    JVM_GetStackTraceElements(env, throwable, elements);
 }
-
-JNIEXPORT jobject JNICALL
-Java_java_lang_Throwable_getStackTraceElement(JNIEnv *env,
-                                              jobject throwable, jint index)
-{
-    return JVM_GetStackTraceElement(env, throwable, index);
-}