|
1 /* |
|
2 * Copyright (c) 2011, 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.monitoring.ThreadMXBean.GetThreadAllocatedBytes; |
|
24 |
|
25 import nsk.share.*; |
|
26 import nsk.monitoring.share.*; |
|
27 import nsk.monitoring.ThreadMXBean.*; |
|
28 |
|
29 /** |
|
30 * Tests getThreadAllocatedBytes(long id) and getThreadAllocatedBytes(long[] ids), |
|
31 * functions of com.sun.management.ThreadMXBean |
|
32 * <p> |
|
33 * All methods should |
|
34 * <br> |
|
35 * - return -1 if ThreadAllocatedMemory allocation is |
|
36 * not enabled |
|
37 * <br> |
|
38 * - return -1 for not started thread |
|
39 * <br> |
|
40 * - return > 0 value for any running thread |
|
41 * <br> |
|
42 * - return -1 for finished thread |
|
43 */ |
|
44 public class BaseBehaviorTest extends ThreadMXBeanTestBase { |
|
45 |
|
46 /** |
|
47 * Actually runs the test |
|
48 */ |
|
49 public void run() { |
|
50 if (threadMXBean == null) |
|
51 return; |
|
52 MXBeanTestThread thread = new MXBeanTestThread(); |
|
53 long id = thread.getId(); |
|
54 long[] idArr = new long[] { id }; |
|
55 long result; |
|
56 long[] resultArr; |
|
57 // Expect -1 for not started threads |
|
58 result = threadMXBean.getThreadAllocatedBytes(id); |
|
59 if (result != -1) |
|
60 throw new TestFailure("Failure! getThreadAllocatedBytes(long id) should " |
|
61 + "return -1 for not started threads. Recieved : " + result); |
|
62 resultArr = threadMXBean.getThreadAllocatedBytes(idArr); |
|
63 if (resultArr[0] != -1) |
|
64 throw new TestFailure("Failure! getThreadAllocatedBytes(long[] ids) should " |
|
65 + "return -1 for not started threads. Recieved : " + resultArr[0]); |
|
66 BarrierHandler handler = startThreads(thread); |
|
67 try { |
|
68 handler.proceed(); |
|
69 // Expect -1 for running thread if ThreadAllocatedMemory (CpuTime) is disabled |
|
70 threadMXBean.setThreadAllocatedMemoryEnabled(false); |
|
71 result = threadMXBean.getThreadAllocatedBytes(id); |
|
72 if (result != -1) |
|
73 throw new TestFailure("Failure! getThreadAllocatedBytes(long id) should " |
|
74 + "return -1 if ThreadAllocatedMemoryEnabled is set to false. " |
|
75 + "Recieved : " + result); |
|
76 resultArr = threadMXBean.getThreadAllocatedBytes(idArr); |
|
77 if (resultArr[0] != -1) |
|
78 throw new TestFailure("Failure! getThreadAllocatedBytes(long[] ids) should " |
|
79 + "return -1 if ThreadAllocatedMemoryEnabled is set to false. " |
|
80 + "Recieved : " + resultArr[0]); |
|
81 |
|
82 threadMXBean.setThreadAllocatedMemoryEnabled(true); |
|
83 // Expect > 0 value for running threads |
|
84 result = threadMXBean.getThreadAllocatedBytes(id); |
|
85 if (result < 0) |
|
86 throw new TestFailure("Failure! getThreadAllocatedBytes(long id) should " |
|
87 + "return > 0 value for RUNNING thread. Recieved : " + result); |
|
88 resultArr = threadMXBean.getThreadAllocatedBytes(idArr); |
|
89 if (resultArr[0] < 0) |
|
90 throw new TestFailure("Failure! getThreadAllocatedBytes(long[] ids) should " |
|
91 + "return > 0 value for RUNNING thread. Recieved : " + resultArr[0]); |
|
92 } finally { |
|
93 // Let thread finish |
|
94 handler.finish(); |
|
95 } |
|
96 try { |
|
97 thread.join(); |
|
98 } catch (InterruptedException e) {} |
|
99 // Expect -1 for finished thread |
|
100 result = threadMXBean.getThreadAllocatedBytes(id); |
|
101 if (result != -1) |
|
102 throw new TestFailure("Failure! getThreadAllocatedBytes(long id) should " |
|
103 + "return -1 for finished threads. Recieved : " + result); |
|
104 resultArr = threadMXBean.getThreadAllocatedBytes(idArr); |
|
105 if (resultArr[0] != -1) |
|
106 throw new TestFailure("Failure! getThreadAllocatedBytes(long[] ids) should " |
|
107 + "return -1 for finished threads. Recieved : " + resultArr[0]); |
|
108 log.info("BaseBehaviorTest passed."); |
|
109 } |
|
110 |
|
111 /** |
|
112 * Entry point for java program |
|
113 * @param args sets the test configuration |
|
114 */ |
|
115 public static void main(String[] args) { |
|
116 ThreadMXBeanTestBase test = new BaseBehaviorTest(); |
|
117 Monitoring.runTest(test, test.setGarbageProducer(args)); |
|
118 } |
|
119 } |