1 /*
2  * Copyright (c) 2007, 2018, 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 package nsk.jvmti.ResourceExhausted;
24 
25 import java.io.PrintStream;
26 import java.util.concurrent.atomic.AtomicInteger;
27 
28 import nsk.share.Consts;
29 import nsk.share.test.Stresser;
30 
31 public class resexhausted001 {
32     static {
33         System.loadLibrary("resexhausted");
34     }
35 
36     static final long MAX_ITERATIONS = 2000000L; // Resasonable limit on number of threads
37 
38     static Object hanger = new Object();
39     static boolean threadsDone;
40     static AtomicInteger threadCount = new AtomicInteger();
41 
run(String args[], PrintStream out)42     public static int run(String args[], PrintStream out) {
43 
44         Stresser stress = new Stresser(args);
45 
46         int count = 0;
47         threadsDone = false;
48         threadCount.set(0);
49         Helper.resetExhaustedEvent();
50 
51         System.out.println("Creating threads...");
52         stress.start(MAX_ITERATIONS);
53         try {
54             while ( stress.iteration() ) {
55                 makeThread();
56             }
57 
58             System.out.println("Can't reproduce OOME due to a limit on iterations/execution time. Test was useless.");
59             return Consts.TEST_PASSED;
60 
61         } catch (OutOfMemoryError e) {
62             count = threadCount.get();
63             threadsDone = true;
64             synchronized (hanger) {
65                 hanger.notifyAll();
66             }
67         } finally {
68             stress.finish();
69         }
70 
71         while ( threadCount.get() > 2 ) {
72             try { Thread.sleep(100); } catch ( InterruptedException swallow ) {}
73         }
74 
75         System.gc();
76         if (!Helper.checkResult("creating " + count + " threads")) {
77             return Consts.TEST_FAILED;
78         }
79 
80         return Consts.TEST_PASSED;
81     }
82 
makeThread()83     static Thread makeThread() {
84         final Thread thr = new Thread(new Runnable() {
85             public void run() {
86                 threadCount.getAndIncrement();
87                 while (!threadsDone) {
88                     try {
89                         synchronized (hanger) {
90                             hanger.wait();
91                         }
92                     } catch (InterruptedException ignored) {}
93                 }
94                 threadCount.getAndDecrement();
95             }
96         }, "fleece");
97         thr.start();
98         return thr;
99     }
100 
main(String[] args)101     public static void main(String[] args) {
102         args = nsk.share.jvmti.JVMTITest.commonInit(args);
103 
104         int result = run(args, System.out);
105         System.out.println(result == Consts.TEST_PASSED ? "TEST PASSED" : "TEST FAILED");
106         System.exit(result + Consts.JCK_STATUS_BASE);
107     }
108 }
109