author | jcbeyler |
Tue, 17 Jul 2018 19:59:38 -0700 | |
changeset 51138 | 914f305ba6fa |
parent 51074 | bf686c47c109 |
child 51559 | 57f1bf06742e |
permissions | -rw-r--r-- |
50579 | 1 |
/* |
2 |
* Copyright (c) 2018, Google 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 |
package MyPackage; |
|
25 |
||
26 |
import java.util.ArrayList; |
|
27 |
import java.util.List; |
|
28 |
||
51074 | 29 |
// Graal is not tested here due to Graal not supporting DisableIntrinsic. |
50579 | 30 |
/** |
31 |
* @test |
|
32 |
* @summary Verifies that when the VM event is sent, sampled events are also collected. |
|
33 |
* @build Frame HeapMonitor |
|
34 |
* @compile HeapMonitorVMEventsTest.java |
|
51074 | 35 |
* @requires !vm.graal.enabled |
50579 | 36 |
* @run main/othervm/native -XX:+UnlockDiagnosticVMOptions |
37 |
* -XX:DisableIntrinsic=_clone |
|
38 |
* -agentlib:HeapMonitorTest MyPackage.HeapMonitorVMEventsTest |
|
39 |
*/ |
|
40 |
||
41 |
public class HeapMonitorVMEventsTest implements Cloneable { |
|
42 |
private static native int vmEvents(); |
|
43 |
private static final int ITERATIONS = 1 << 15; |
|
44 |
private static final Object[] array = new Object[ITERATIONS]; |
|
45 |
||
46 |
private static void cloneObjects(int iterations) { |
|
47 |
HeapMonitorVMEventsTest object = new HeapMonitorVMEventsTest(); |
|
48 |
for (int i = 0; i < iterations; i++) { |
|
49 |
try { |
|
50 |
array[i] = object.clone(); |
|
51 |
} catch (Exception e) { |
|
52 |
throw new RuntimeException(e); |
|
53 |
} |
|
54 |
} |
|
55 |
} |
|
56 |
||
57 |
public Object clone() throws CloneNotSupportedException { |
|
58 |
return super.clone(); |
|
59 |
} |
|
60 |
||
61 |
private static void checkDifference(int first, int second) { |
|
62 |
double diff = Math.abs(first - second) * 100; |
|
63 |
diff /= first; |
|
64 |
||
51138
914f305ba6fa
8205725: Update the JVMTI Spec for Heap Sampling
jcbeyler
parents:
51074
diff
changeset
|
65 |
// Accept a 10% error interval: with objects being allocated: this allows a bit of room in |
50579 | 66 |
// case other items are getting allocated during the test. |
67 |
if (diff > 10) { |
|
51138
914f305ba6fa
8205725: Update the JVMTI Spec for Heap Sampling
jcbeyler
parents:
51074
diff
changeset
|
68 |
throw new RuntimeException("Error interval is over the accepted interval: " + diff |
50579 | 69 |
+ ": " + first + " , " + second); |
70 |
} |
|
71 |
} |
|
72 |
||
73 |
private static void compareSampledAndVM() { |
|
74 |
HeapMonitor.resetEventStorage(); |
|
75 |
cloneObjects(ITERATIONS); |
|
76 |
||
77 |
int onlySampleCount = HeapMonitor.sampledEvents(); |
|
78 |
||
79 |
HeapMonitor.enableVMEvents(); |
|
80 |
HeapMonitor.resetEventStorage(); |
|
81 |
if (!HeapMonitor.eventStorageIsEmpty()) { |
|
82 |
throw new RuntimeException("Storage is not empty after reset."); |
|
83 |
} |
|
84 |
||
85 |
cloneObjects(ITERATIONS); |
|
86 |
||
87 |
int sampleCount = HeapMonitor.sampledEvents(); |
|
88 |
int vmCount = vmEvents(); |
|
89 |
||
90 |
System.err.println("Obtained: " + onlySampleCount + " - " + sampleCount + " - " + vmCount); |
|
91 |
checkDifference(onlySampleCount, sampleCount); |
|
92 |
checkDifference(onlySampleCount, vmCount); |
|
93 |
} |
|
94 |
||
95 |
public static void main(String[] args) { |
|
96 |
if (!HeapMonitor.eventStorageIsEmpty()) { |
|
97 |
throw new RuntimeException("Storage is not empty at test start..."); |
|
98 |
} |
|
99 |
||
100 |
HeapMonitor.sampleEverything(); |
|
101 |
compareSampledAndVM(); |
|
102 |
} |
|
103 |
} |