author | coffeys |
Wed, 09 Oct 2019 10:14:03 +0000 | |
changeset 58513 | 7605e97c9491 |
parent 49834 | 99644c75eaed |
permissions | -rw-r--r-- |
2 | 1 |
/* |
58513
7605e97c9491
8231770: Test java/util/zip/FlaterTest.java fails with -Xcheck:jni
coffeys
parents:
49834
diff
changeset
|
2 |
* Copyright (c) 2005, 2019, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 22 |
*/ |
23 |
||
24 |
/** |
|
25 |
* @test |
|
58513
7605e97c9491
8231770: Test java/util/zip/FlaterTest.java fails with -Xcheck:jni
coffeys
parents:
49834
diff
changeset
|
26 |
* @bug 6348045 6341887 8231770 |
2 | 27 |
* @summary GZipOutputStream/InputStream goes critical(calls JNI_Get*Critical) |
58513
7605e97c9491
8231770: Test java/util/zip/FlaterTest.java fails with -Xcheck:jni
coffeys
parents:
49834
diff
changeset
|
28 |
* and causes slowness. This test uses Deflater and Inflater directly. |
30046 | 29 |
* @key randomness |
58513
7605e97c9491
8231770: Test java/util/zip/FlaterTest.java fails with -Xcheck:jni
coffeys
parents:
49834
diff
changeset
|
30 |
* @run main/othervm -Xcheck:jni FlaterTest |
2 | 31 |
*/ |
32 |
||
33 |
import java.nio.*; |
|
34 |
import java.util.*; |
|
35 |
import java.util.zip.*; |
|
36 |
||
37 |
/** |
|
38 |
* This test runs Inflater and Defalter in a number of simultaneous threads, |
|
39 |
* validating that the deflated & then inflated data matches the original |
|
40 |
* data. |
|
41 |
*/ |
|
42 |
public class FlaterTest extends Thread { |
|
43 |
private static final int DATA_LEN = 1024 * 128; |
|
49834
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
44 |
|
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
45 |
private static ByteBuffer dataDirect; |
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
46 |
private static ByteBuffer dataHeap; |
2 | 47 |
|
48 |
// If true, print extra info. |
|
49 |
private static final boolean debug = false; |
|
50 |
||
51 |
// Set of Flater threads running. |
|
49834
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
52 |
private static Set<Flater> flaters = |
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
53 |
Collections.synchronizedSet(new HashSet<>()); |
2 | 54 |
|
55 |
/** Fill in {@code data} with random values. */ |
|
56 |
static void createData() { |
|
49834
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
57 |
ByteBuffer bb = ByteBuffer.allocateDirect(DATA_LEN * 8); |
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
58 |
for (int i = 0; i < DATA_LEN * 8; i += 8) { |
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
59 |
bb.putDouble(i, Math.random()); |
2 | 60 |
} |
49834
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
61 |
dataDirect = bb; |
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
62 |
final ByteBuffer hb = ByteBuffer.allocate(bb.capacity()); |
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
63 |
hb.duplicate().put(bb.duplicate()); |
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
64 |
dataHeap = hb; |
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
65 |
if (debug) System.out.println("data length is " + bb.capacity()); |
2 | 66 |
} |
67 |
||
68 |
/** @return the length of the deflated {@code data}. */ |
|
49834
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
69 |
private static int getDeflatedLength() { |
2 | 70 |
Deflater deflater = new Deflater(); |
49834
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
71 |
deflater.setInput(dataDirect.duplicate()); |
2 | 72 |
deflater.finish(); |
49834
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
73 |
byte[] out = new byte[dataDirect.capacity()]; |
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
74 |
int rc = deflater.deflate(out); |
2 | 75 |
deflater.end(); |
76 |
if (debug) System.out.println("deflatedLength is " + rc); |
|
77 |
return rc; |
|
78 |
} |
|
79 |
||
80 |
/** Compares given bytes with those in {@code data}. |
|
81 |
* @throws Exception if given bytes don't match {@code data}. |
|
82 |
*/ |
|
49834
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
83 |
private static void validate(ByteBuffer buf, int offset, int len) throws Exception { |
2 | 84 |
for (int i = 0; i < len; i++ ) { |
49834
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
85 |
if (buf.get(i) != dataDirect.get(offset+i)) { |
2 | 86 |
throw new Exception("mismatch at " + (offset + i)); |
87 |
} |
|
88 |
} |
|
89 |
} |
|
90 |
||
49834
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
91 |
public static void realMain(String[] args) { |
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
92 |
int numThreads = args.length > 0 ? Integer.parseInt(args[0]) : 5; |
2 | 93 |
createData(); |
49834
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
94 |
for (int srcMode = 0; srcMode <= 2; srcMode ++) { |
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
95 |
for (int dstMode = 0; dstMode <= 2; dstMode ++) { |
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
96 |
new FlaterTest().go(numThreads, srcMode, dstMode); |
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
97 |
} |
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
98 |
} |
2 | 99 |
} |
100 |
||
49834
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
101 |
private synchronized void go(int numThreads, int srcMode, int dstMode) { |
2 | 102 |
int deflatedLength = getDeflatedLength(); |
103 |
||
104 |
long time = System.currentTimeMillis(); |
|
105 |
for (int i = 0; i < numThreads; i++) { |
|
49834
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
106 |
Flater f = new Flater(deflatedLength, srcMode, dstMode); |
2 | 107 |
flaters.add(f); |
108 |
f.start(); |
|
109 |
} |
|
49834
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
110 |
synchronized (flaters) { |
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
111 |
while (flaters.size() != 0) { |
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
112 |
try { |
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
113 |
flaters.wait(); |
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
114 |
} catch (InterruptedException ex) { |
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
115 |
unexpected(ex); |
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
116 |
} |
2 | 117 |
} |
118 |
} |
|
119 |
time = System.currentTimeMillis() - time; |
|
120 |
System.out.println("Time needed for " + numThreads |
|
49834
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
121 |
+ " threads to deflate/inflate: " + time + " ms (srcMode="+srcMode+",dstMode="+dstMode+")"); |
2 | 122 |
} |
123 |
||
124 |
/** Deflates and inflates data. */ |
|
125 |
static class Flater extends Thread { |
|
126 |
private final int deflatedLength; |
|
49834
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
127 |
private final int srcMode, dstMode; |
2 | 128 |
|
49834
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
129 |
private Flater(int length, int srcMode, int dstMode) { |
2 | 130 |
this.deflatedLength = length; |
49834
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
131 |
this.srcMode = srcMode; |
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
132 |
this.dstMode = dstMode; |
2 | 133 |
} |
134 |
||
135 |
/** Deflates and inflates {@code data}. */ |
|
136 |
public void run() { |
|
137 |
if (debug) System.out.println(getName() + " starting run()"); |
|
138 |
try { |
|
49834
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
139 |
ByteBuffer deflated = DeflateData(deflatedLength); |
2 | 140 |
InflateData(deflated); |
141 |
} catch (Throwable t) { |
|
142 |
t.printStackTrace(); |
|
143 |
fail(getName() + " failed"); |
|
144 |
} finally { |
|
49834
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
145 |
synchronized (flaters) { |
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
146 |
flaters.remove(this); |
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
147 |
if (flaters.isEmpty()) { |
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
148 |
flaters.notifyAll(); |
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
149 |
} |
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
150 |
} |
2 | 151 |
} |
152 |
} |
|
153 |
||
154 |
/** Returns a copy of {@code data} in deflated form. */ |
|
49834
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
155 |
private ByteBuffer DeflateData(int length) { |
2 | 156 |
Deflater deflater = new Deflater(); |
49834
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
157 |
if (srcMode == 0) { |
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
158 |
deflater.setInput(dataHeap.array()); |
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
159 |
} else if (srcMode == 1) { |
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
160 |
deflater.setInput(dataHeap.duplicate()); |
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
161 |
} else { |
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
162 |
assert srcMode == 2; |
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
163 |
deflater.setInput(dataDirect.duplicate()); |
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
164 |
} |
2 | 165 |
deflater.finish(); |
49834
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
166 |
ByteBuffer out = dstMode == 2 ? ByteBuffer.allocateDirect(length) : ByteBuffer.allocate(length); |
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
167 |
int deflated; |
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
168 |
if (dstMode == 0) { |
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
169 |
deflated = deflater.deflate(out.array(), 0, length); |
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
170 |
out.position(deflated); |
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
171 |
} else { |
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
172 |
deflater.deflate(out); |
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
173 |
} |
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
174 |
out.flip(); |
2 | 175 |
return out; |
176 |
} |
|
177 |
||
178 |
/** Inflate a byte array, comparing it with {@code data} during |
|
179 |
* inflation. |
|
180 |
* @throws Exception if inflated bytes don't match {@code data}. |
|
181 |
*/ |
|
49834
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
182 |
private void InflateData(ByteBuffer bytes) throws Throwable { |
2 | 183 |
Inflater inflater = new Inflater(); |
49834
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
184 |
if (dstMode == 0) { |
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
185 |
inflater.setInput(bytes.array(), 0, bytes.remaining()); |
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
186 |
} else { |
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
187 |
inflater.setInput(bytes); |
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
188 |
} |
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
189 |
if (inflater.getRemaining() == 0) { |
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
190 |
throw new Exception("Nothing to inflate (bytes=" + bytes + ")"); |
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
191 |
} |
2 | 192 |
int len = 1024 * 8; |
193 |
int offset = 0; |
|
49834
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
194 |
ByteBuffer buf = srcMode == 2 ? ByteBuffer.allocateDirect(len) : ByteBuffer.allocate(len); |
2 | 195 |
while (inflater.getRemaining() > 0) { |
49834
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
196 |
buf.clear(); |
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
197 |
int inflated; |
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
198 |
if (srcMode == 0) { |
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
199 |
inflated = inflater.inflate(buf.array(), 0, buf.remaining()); |
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
200 |
} else { |
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
201 |
inflated = inflater.inflate(buf); |
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
202 |
} |
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
203 |
if (inflated == 0) { |
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
204 |
throw new Exception("Nothing inflated (dst=" + buf + ",offset=" + offset + ",rem=" + inflater.getRemaining() + ",srcMode="+srcMode+",dstMode="+dstMode+")"); |
99644c75eaed
6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents:
47216
diff
changeset
|
205 |
} |
2 | 206 |
validate(buf, offset, inflated); |
207 |
offset += inflated; |
|
208 |
} |
|
209 |
} |
|
210 |
} |
|
211 |
||
212 |
//--------------------- Infrastructure --------------------------- |
|
213 |
static volatile int passed = 0, failed = 0; |
|
214 |
static void pass() {passed++;} |
|
215 |
static void fail() {failed++; Thread.dumpStack();} |
|
216 |
static void fail(String msg) {System.out.println(msg); fail();} |
|
217 |
static void unexpected(Throwable t) {failed++; t.printStackTrace();} |
|
218 |
static void check(boolean cond) {if (cond) pass(); else fail();} |
|
219 |
static void equal(Object x, Object y) { |
|
220 |
if (x == null ? y == null : x.equals(y)) pass(); |
|
221 |
else fail(x + " not equal to " + y);} |
|
222 |
public static void main(String[] args) throws Throwable { |
|
223 |
try {realMain(args);} catch (Throwable t) {unexpected(t);} |
|
224 |
System.out.println("\nPassed = " + passed + " failed = " + failed); |
|
225 |
if (failed > 0) throw new AssertionError("Some tests failed");} |
|
226 |
} |