author | ohair |
Tue, 28 Dec 2010 15:53:50 -0800 | |
changeset 7668 | d4a77089c587 |
parent 5808 | 3a1f603c5ca7 |
child 21413 | d5d767325325 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
7668 | 2 |
* Copyright (c) 2007, 2010, 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 |
|
26 |
* @bug 6546089 |
|
27 |
* @summary Basic unit test of MemoryPoolMXBean.isUsageThresholdExceeded() and |
|
28 |
* MemoryPoolMXBean.isCollectionThresholdExceeded(). |
|
29 |
* @author Mandy Chung |
|
30 |
* |
|
31 |
* @run main ThresholdTest |
|
32 |
*/ |
|
33 |
||
34 |
import java.lang.management.*; |
|
35 |
import java.util.*; |
|
36 |
||
37 |
public class ThresholdTest { |
|
38 |
public static void main(String args[]) throws Exception { |
|
39 |
List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans(); |
|
5808
3a1f603c5ca7
6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents:
5506
diff
changeset
|
40 |
try { |
3a1f603c5ca7
6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents:
5506
diff
changeset
|
41 |
for (MemoryPoolMXBean p : pools) { |
3a1f603c5ca7
6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents:
5506
diff
changeset
|
42 |
// verify if isUsageThresholdExceeded() returns correct value |
3a1f603c5ca7
6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents:
5506
diff
changeset
|
43 |
checkUsageThreshold(p); |
3a1f603c5ca7
6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents:
5506
diff
changeset
|
44 |
// verify if isCollectionUsageThresholdExceeded() returns correct value |
3a1f603c5ca7
6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents:
5506
diff
changeset
|
45 |
checkCollectionUsageThreshold(p); |
3a1f603c5ca7
6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents:
5506
diff
changeset
|
46 |
} |
3a1f603c5ca7
6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents:
5506
diff
changeset
|
47 |
} finally { |
3a1f603c5ca7
6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents:
5506
diff
changeset
|
48 |
// restore the default |
3a1f603c5ca7
6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents:
5506
diff
changeset
|
49 |
for (MemoryPoolMXBean p : pools) { |
3a1f603c5ca7
6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents:
5506
diff
changeset
|
50 |
if (p.isUsageThresholdSupported()) { |
3a1f603c5ca7
6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents:
5506
diff
changeset
|
51 |
p.setUsageThreshold(0); |
3a1f603c5ca7
6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents:
5506
diff
changeset
|
52 |
} |
3a1f603c5ca7
6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents:
5506
diff
changeset
|
53 |
if (p.isCollectionUsageThresholdSupported()) { |
3a1f603c5ca7
6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents:
5506
diff
changeset
|
54 |
p.setCollectionUsageThreshold(0); |
3a1f603c5ca7
6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents:
5506
diff
changeset
|
55 |
} |
3a1f603c5ca7
6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents:
5506
diff
changeset
|
56 |
} |
2 | 57 |
} |
58 |
||
59 |
System.out.println("Test passed."); |
|
60 |
} |
|
61 |
||
62 |
private static void checkUsageThreshold(MemoryPoolMXBean p) throws Exception { |
|
63 |
||
64 |
if (!p.isUsageThresholdSupported()) { |
|
65 |
return; |
|
66 |
} |
|
67 |
||
68 |
long threshold = p.getUsageThreshold(); |
|
69 |
if (threshold != 0) { |
|
70 |
// Expect the default threshold is zero (disabled) |
|
71 |
throw new RuntimeException("TEST FAILED: " + |
|
72 |
"Pool " + p.getName() + |
|
73 |
" has non-zero threshold (" + threshold); |
|
74 |
} |
|
75 |
||
76 |
// isUsageThresholdExceeded() should return false if threshold == 0 |
|
77 |
if (p.isUsageThresholdExceeded()) { |
|
78 |
throw new RuntimeException("TEST FAILED: " + |
|
79 |
"Pool " + p.getName() + |
|
80 |
" isUsageThresholdExceeded() returned true" + |
|
81 |
" but threshold = 0"); |
|
82 |
} |
|
83 |
||
84 |
p.setUsageThreshold(1); |
|
85 |
MemoryUsage u = p.getUsage(); |
|
86 |
if (u.getUsed() >= 1) { |
|
87 |
if (!p.isUsageThresholdExceeded()) { |
|
88 |
throw new RuntimeException("TEST FAILED: " + |
|
89 |
"Pool " + p.getName() + |
|
90 |
" isUsageThresholdExceeded() returned false but " + |
|
91 |
" threshold(" + p.getUsageThreshold() + |
|
92 |
") <= used(" + u.getUsed() + ")"); |
|
93 |
} |
|
94 |
} else { |
|
95 |
if (p.isUsageThresholdExceeded()) { |
|
96 |
throw new RuntimeException("TEST FAILED: " + |
|
97 |
"Pool " + p.getName() + |
|
98 |
" isUsageThresholdExceeded() returned true but" + |
|
99 |
" threshold(" + p.getUsageThreshold() + |
|
100 |
") > used(" + u.getUsed() + ")"); |
|
101 |
} |
|
102 |
} |
|
103 |
||
104 |
// disable low memory detection and isUsageThresholdExceeded() |
|
105 |
// should return false |
|
106 |
p.setUsageThreshold(0); |
|
107 |
if (p.isUsageThresholdExceeded()) { |
|
108 |
throw new RuntimeException("TEST FAILED: " + |
|
109 |
"Pool " + p.getName() + |
|
110 |
" isUsageThresholdExceeded() returned true but threshold = 0"); |
|
111 |
} |
|
112 |
} |
|
113 |
||
114 |
private static void checkCollectionUsageThreshold(MemoryPoolMXBean p) throws Exception { |
|
115 |
||
116 |
if (!p.isCollectionUsageThresholdSupported()) { |
|
117 |
return; |
|
118 |
} |
|
119 |
||
120 |
long threshold = p.getCollectionUsageThreshold(); |
|
121 |
if (threshold != 0) { |
|
122 |
// Expect the default threshold is zero (disabled) |
|
123 |
throw new RuntimeException("TEST FAILED: " + |
|
124 |
"Pool " + p.getName() + |
|
125 |
" has non-zero threshold (" + threshold); |
|
126 |
} |
|
127 |
||
128 |
// isCollectionUsageThresholdExceeded() should return false if threshold == 0 |
|
129 |
if (p.isCollectionUsageThresholdExceeded()) { |
|
130 |
throw new RuntimeException("TEST FAILED: " + |
|
131 |
"Pool " + p.getName() + |
|
132 |
" isCollectionUsageThresholdExceeded() returned true" + |
|
133 |
" but threshold = 0"); |
|
134 |
} |
|
135 |
||
136 |
p.setCollectionUsageThreshold(1); |
|
137 |
MemoryUsage u = p.getCollectionUsage(); |
|
138 |
if (u == null) { |
|
139 |
if (p.isCollectionUsageThresholdExceeded()) { |
|
140 |
throw new RuntimeException("TEST FAILED: " + |
|
141 |
"Pool " + p.getName() + |
|
142 |
" isCollectionUsageThresholdExceeded() returned true but" + |
|
143 |
" getCollectionUsage() return null"); |
|
144 |
} |
|
145 |
} else if (u.getUsed() >= 1) { |
|
146 |
if (!p.isCollectionUsageThresholdExceeded()) { |
|
147 |
throw new RuntimeException("TEST FAILED: " + |
|
148 |
"Pool " + p.getName() + |
|
149 |
" isCollectionUsageThresholdExceeded() returned false but " + |
|
150 |
" threshold(" + p.getCollectionUsageThreshold() + |
|
151 |
") < used(" + u.getUsed() + ")"); |
|
152 |
} |
|
153 |
} else { |
|
154 |
if (p.isCollectionUsageThresholdExceeded()) { |
|
155 |
throw new RuntimeException("TEST FAILED: " + |
|
156 |
"Pool " + p.getName() + |
|
157 |
" isCollectionUsageThresholdExceeded() returned true but" + |
|
158 |
" threshold(" + p.getCollectionUsageThreshold() + |
|
159 |
") > used(" + u.getUsed() + ")"); |
|
160 |
} |
|
161 |
} |
|
162 |
||
163 |
// disable low memory detection and isCollectionUsageThresholdExceeded() |
|
164 |
// should return false |
|
165 |
p.setCollectionUsageThreshold(0); |
|
166 |
if (p.isCollectionUsageThresholdExceeded()) { |
|
167 |
throw new RuntimeException("TEST FAILED: " + |
|
168 |
"Pool " + p.getName() + |
|
169 |
" isCollectionUsageThresholdExceeded() returned true but threshold = 0"); |
|
170 |
} |
|
171 |
} |
|
172 |
} |