hotspot/test/runtime/Throwable/TestThrowable.java
author coleenp
Thu, 10 Mar 2016 13:43:47 -0500
changeset 37064 5c82fa70d313
child 38152 80e5da81fb2c
permissions -rw-r--r--
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: shade, mchung, dholmes, hseigel
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
37064
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
     1
/*
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
     4
 *
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
     7
 * published by the Free Software Foundation.
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
     8
 *
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    13
 * accompanied this code).
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    14
 *
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    18
 *
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    21
 * questions.
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    22
 */
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    23
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    24
/*
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    25
 * @test
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    26
 * @bug 8150778
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    27
 * @summary Test exception depths, and code to get stack traces
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    28
 * @library /testlibrary
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    29
 * @run main/othervm -XX:MaxJavaStackTraceDepth=1024 TestThrowable
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    30
 */
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    31
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    32
import java.lang.reflect.Field;
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    33
import jdk.test.lib.Asserts;
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    34
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    35
public class TestThrowable {
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    36
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    37
  // Inner class that throws a lot of exceptions
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    38
  static class Thrower {
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    39
    static int MaxJavaStackTraceDepth = 1024; // as above
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    40
    int[] depths = {10, 34, 100, 1024, 2042};
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    41
    int count = 0;
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    42
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    43
    int getDepth(Throwable t) throws Exception {
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    44
      Field f = Throwable.class.getDeclaredField("depth");
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    45
      f.setAccessible(true); // it's private
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    46
      return f.getInt(t);
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    47
    }
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    48
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    49
    void callThrow(int depth) {
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    50
      if (++count < depth) {
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    51
        callThrow(depth);
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    52
      } else {
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    53
        throw new RuntimeException("depth tested " + depth);
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    54
      }
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    55
    }
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    56
    void testThrow() throws Exception {
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    57
      for (int d : depths) {
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    58
        try {
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    59
          count = getDepth(new Throwable());
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    60
          callThrow(d);
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    61
        } catch(Exception e) {
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    62
          e.getStackTrace();
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    63
          System.out.println(e.getMessage());
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    64
          int throwableDepth = getDepth(e);
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    65
          Asserts.assertTrue(throwableDepth == d ||
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    66
                     (d > MaxJavaStackTraceDepth && throwableDepth == MaxJavaStackTraceDepth),
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    67
                     "depth should return the correct value: depth tested=" +
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    68
                     d + " throwableDepth=" + throwableDepth);
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    69
        }
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    70
      }
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    71
    }
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    72
  }
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    73
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    74
  public static void main(String... unused) throws Exception {
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    75
    Thrower t = new Thrower();
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    76
    t.testThrow();
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    77
  }
5c82fa70d313 8150778: Reduce Throwable.getStackTrace() calls to the JVM
coleenp
parents:
diff changeset
    78
}