|
1 /* |
|
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
3 * |
|
4 * This code is free software; you can redistribute it and/or modify it |
|
5 * under the terms of the GNU General Public License version 2 only, as |
|
6 * published by the Free Software Foundation. |
|
7 * |
|
8 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
11 * version 2 for more details (a copy is included in the LICENSE file that |
|
12 * accompanied this code). |
|
13 * |
|
14 * You should have received a copy of the GNU General Public License version |
|
15 * 2 along with this work; if not, write to the Free Software Foundation, |
|
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
17 * |
|
18 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
19 * or visit www.oracle.com if you need additional information or have any |
|
20 * questions. |
|
21 */ |
|
22 |
|
23 /* |
|
24 * This file is available under and governed by the GNU General Public |
|
25 * License version 2 only, as published by the Free Software Foundation. |
|
26 * However, the following notice accompanied the original version of this |
|
27 * file: |
|
28 * |
|
29 * Written by Doug Lea with assistance from members of JCP JSR-166 |
|
30 * Expert Group and released to the public domain, as explained at |
|
31 * http://creativecommons.org/publicdomain/zero/1.0/ |
|
32 */ |
|
33 /* |
|
34 * @test |
|
35 * @bug 6785442 |
|
36 * @summary Benchmark that tries to GC-tenure head, followed by |
|
37 * many add/remove operations. |
|
38 * @run main GCRetention 12345 |
|
39 */ |
|
40 |
|
41 import static java.util.concurrent.TimeUnit.SECONDS; |
|
42 |
|
43 import java.lang.ref.WeakReference; |
|
44 import java.util.concurrent.ArrayBlockingQueue; |
|
45 import java.util.concurrent.ConcurrentHashMap; |
|
46 import java.util.concurrent.ConcurrentLinkedDeque; |
|
47 import java.util.concurrent.ConcurrentLinkedQueue; |
|
48 import java.util.concurrent.CountDownLatch; |
|
49 import java.util.concurrent.LinkedBlockingDeque; |
|
50 import java.util.concurrent.LinkedBlockingQueue; |
|
51 import java.util.concurrent.LinkedTransferQueue; |
|
52 import java.util.concurrent.PriorityBlockingQueue; |
|
53 import java.util.LinkedList; |
|
54 import java.util.PriorityQueue; |
|
55 import java.util.ArrayList; |
|
56 import java.util.Collection; |
|
57 import java.util.Collections; |
|
58 import java.util.List; |
|
59 import java.util.Queue; |
|
60 import java.util.Map; |
|
61 |
|
62 public class GCRetention { |
|
63 // Suitable for benchmarking. Overridden by args[0] for testing. |
|
64 int count = 1024 * 1024; |
|
65 |
|
66 /** No guarantees, but effective in practice. */ |
|
67 static void forceFullGc() { |
|
68 CountDownLatch finalizeDone = new CountDownLatch(1); |
|
69 WeakReference<?> ref = new WeakReference<Object>(new Object() { |
|
70 protected void finalize() { finalizeDone.countDown(); }}); |
|
71 try { |
|
72 for (int i = 0; i < 10; i++) { |
|
73 System.gc(); |
|
74 if (finalizeDone.await(1L, SECONDS) && ref.get() == null) { |
|
75 System.runFinalization(); // try to pick up stragglers |
|
76 return; |
|
77 } |
|
78 } |
|
79 } catch (InterruptedException unexpected) { |
|
80 throw new AssertionError("unexpected InterruptedException"); |
|
81 } |
|
82 throw new AssertionError("failed to do a \"full\" gc"); |
|
83 } |
|
84 |
|
85 final Map<String,String> results = new ConcurrentHashMap<>(); |
|
86 |
|
87 Collection<Queue<Boolean>> queues() { |
|
88 List<Queue<Boolean>> queues = new ArrayList<>(); |
|
89 queues.add(new ConcurrentLinkedDeque<Boolean>()); |
|
90 queues.add(new ConcurrentLinkedQueue<Boolean>()); |
|
91 queues.add(new ArrayBlockingQueue<Boolean>(count, false)); |
|
92 queues.add(new ArrayBlockingQueue<Boolean>(count, true)); |
|
93 queues.add(new LinkedBlockingQueue<Boolean>()); |
|
94 queues.add(new LinkedBlockingDeque<Boolean>()); |
|
95 queues.add(new PriorityBlockingQueue<Boolean>()); |
|
96 queues.add(new PriorityQueue<Boolean>()); |
|
97 queues.add(new LinkedList<Boolean>()); |
|
98 queues.add(new LinkedTransferQueue<Boolean>()); |
|
99 |
|
100 // Following additional implementations are available from: |
|
101 // http://gee.cs.oswego.edu/dl/concurrency-interest/index.html |
|
102 // queues.add(new SynchronizedLinkedListQueue<Boolean>()); |
|
103 |
|
104 // Avoid "first fast, second slow" benchmark effect. |
|
105 Collections.shuffle(queues); |
|
106 return queues; |
|
107 } |
|
108 |
|
109 void prettyPrintResults() { |
|
110 List<String> classNames = new ArrayList<>(results.keySet()); |
|
111 Collections.sort(classNames); |
|
112 int maxClassNameLength = 0; |
|
113 int maxNanosLength = 0; |
|
114 for (String name : classNames) { |
|
115 if (maxClassNameLength < name.length()) |
|
116 maxClassNameLength = name.length(); |
|
117 if (maxNanosLength < results.get(name).length()) |
|
118 maxNanosLength = results.get(name).length(); |
|
119 } |
|
120 String format = String.format("%%%ds %%%ds nanos/item%%n", |
|
121 maxClassNameLength, maxNanosLength); |
|
122 for (String name : classNames) |
|
123 System.out.printf(format, name, results.get(name)); |
|
124 } |
|
125 |
|
126 void test(String[] args) { |
|
127 if (args.length > 0) |
|
128 count = Integer.valueOf(args[0]); |
|
129 // Warmup |
|
130 for (Queue<Boolean> queue : queues()) |
|
131 test(queue); |
|
132 results.clear(); |
|
133 for (Queue<Boolean> queue : queues()) |
|
134 test(queue); |
|
135 |
|
136 prettyPrintResults(); |
|
137 } |
|
138 |
|
139 void test(Queue<Boolean> q) { |
|
140 long t0 = System.nanoTime(); |
|
141 for (int i = 0; i < count; i++) |
|
142 check(q.add(Boolean.TRUE)); |
|
143 forceFullGc(); |
|
144 // forceFullGc(); |
|
145 Boolean x; |
|
146 while ((x = q.poll()) != null) |
|
147 equal(x, Boolean.TRUE); |
|
148 check(q.isEmpty()); |
|
149 |
|
150 for (int i = 0; i < 10 * count; i++) { |
|
151 for (int k = 0; k < 3; k++) |
|
152 check(q.add(Boolean.TRUE)); |
|
153 for (int k = 0; k < 3; k++) |
|
154 if (q.poll() != Boolean.TRUE) |
|
155 fail(); |
|
156 } |
|
157 check(q.isEmpty()); |
|
158 |
|
159 String className = q.getClass().getSimpleName(); |
|
160 long elapsed = System.nanoTime() - t0; |
|
161 int nanos = (int) ((double) elapsed / (10 * 3 * count)); |
|
162 results.put(className, String.valueOf(nanos)); |
|
163 } |
|
164 |
|
165 //--------------------- Infrastructure --------------------------- |
|
166 volatile int passed = 0, failed = 0; |
|
167 void pass() {passed++;} |
|
168 void fail() {failed++; Thread.dumpStack();} |
|
169 void fail(String msg) {System.err.println(msg); fail();} |
|
170 void unexpected(Throwable t) {failed++; t.printStackTrace();} |
|
171 void check(boolean cond) {if (cond) pass(); else fail();} |
|
172 void equal(Object x, Object y) { |
|
173 if (x == null ? y == null : x.equals(y)) pass(); |
|
174 else fail(x + " not equal to " + y);} |
|
175 public static void main(String[] args) throws Throwable { |
|
176 new GCRetention().instanceMain(args);} |
|
177 public void instanceMain(String[] args) throws Throwable { |
|
178 try {test(args);} catch (Throwable t) {unexpected(t);} |
|
179 System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); |
|
180 if (failed > 0) throw new AssertionError("Some tests failed");} |
|
181 } |