test/hotspot/jtreg/runtime/Thread/CountStackFramesAtExit.java
branchstuefe-new-metaspace-branch
changeset 58948 18659e040c64
parent 58883 08102295011d
parent 58946 83810b7d12e7
child 59107 245a39d9e24d
equal deleted inserted replaced
58883:08102295011d 58948:18659e040c64
     1 /*
       
     2  * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 /**
       
    25  * @test
       
    26  * @bug 8167108
       
    27  * @summary Stress test java.lang.Thread.countStackFrames() at thread exit.
       
    28  * @run main/othervm -Xlog:thread+smr=debug CountStackFramesAtExit
       
    29  */
       
    30 
       
    31 import java.util.concurrent.CountDownLatch;
       
    32 
       
    33 public class CountStackFramesAtExit extends Thread {
       
    34     final static int N_THREADS = 32;
       
    35     final static int N_LATE_CALLS = 1000;
       
    36 
       
    37     public CountDownLatch exitSyncObj = new CountDownLatch(1);
       
    38     public CountDownLatch startSyncObj = new CountDownLatch(1);
       
    39 
       
    40     @Override
       
    41     public void run() {
       
    42         // Tell main thread we have started.
       
    43         startSyncObj.countDown();
       
    44         try {
       
    45             // Wait for main thread to interrupt us so we
       
    46             // can race to exit.
       
    47             exitSyncObj.await();
       
    48         } catch (InterruptedException e) {
       
    49             // ignore because we expect one
       
    50         }
       
    51     }
       
    52 
       
    53     public static void main(String[] args) {
       
    54         CountStackFramesAtExit threads[] = new CountStackFramesAtExit[N_THREADS];
       
    55 
       
    56         for (int i = 0; i < N_THREADS; i++ ) {
       
    57             threads[i] = new CountStackFramesAtExit();
       
    58             int late_count = 1;
       
    59             threads[i].start();
       
    60             try {
       
    61                 // Wait for the worker thread to get going.
       
    62                 threads[i].startSyncObj.await();
       
    63 
       
    64                 // This interrupt() call will break the worker out of
       
    65                 // the exitSyncObj.await() call and the countStackFrames()
       
    66                 // calls will come in during thread exit.
       
    67                 threads[i].interrupt();
       
    68                 for (; late_count <= N_LATE_CALLS; late_count++) {
       
    69                     try {
       
    70                         threads[i].countStackFrames();
       
    71                     } catch (IllegalThreadStateException itse) {
       
    72                         // ignore because we expect it
       
    73                     }
       
    74 
       
    75                     if (!threads[i].isAlive()) {
       
    76                         // Done with Thread.countStackFrames() calls since
       
    77                         // thread is not alive.
       
    78                         break;
       
    79                     }
       
    80                 }
       
    81             } catch (InterruptedException e) {
       
    82                 throw new Error("Unexpected: " + e);
       
    83             }
       
    84 
       
    85             System.out.println("INFO: thread #" + i + ": made " + late_count +
       
    86                                " late calls to java.lang.Thread.countStackFrames()");
       
    87             System.out.println("INFO: thread #" + i + ": N_LATE_CALLS==" +
       
    88                                N_LATE_CALLS + " value is " +
       
    89                                ((late_count >= N_LATE_CALLS) ? "NOT " : "") +
       
    90                                "large enough to cause a Thread.countStackFrames() " +
       
    91                                "call after thread exit.");
       
    92 
       
    93             try {
       
    94                 threads[i].join();
       
    95             } catch (InterruptedException e) {
       
    96                 throw new Error("Unexpected: " + e);
       
    97             }
       
    98             threads[i].countStackFrames();
       
    99             if (threads[i].isAlive()) {
       
   100                 throw new Error("Expected !Thread.isAlive() after thread #" +
       
   101                                 i + " has been join()'ed");
       
   102             }
       
   103         }
       
   104 
       
   105         String cmd = System.getProperty("sun.java.command");
       
   106         if (cmd != null && !cmd.startsWith("com.sun.javatest.regtest.agent.MainWrapper")) {
       
   107             // Exit with success in a non-JavaTest environment:
       
   108             System.exit(0);
       
   109         }
       
   110     }
       
   111 }