1 /* |
1 /* |
2 * Copyright (c) 2011, 2018, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
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 |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. |
7 * published by the Free Software Foundation. |
25 import nsk.share.*; |
25 import nsk.share.*; |
26 import nsk.monitoring.share.*; |
26 import nsk.monitoring.share.*; |
27 import nsk.monitoring.ThreadMXBean.*; |
27 import nsk.monitoring.ThreadMXBean.*; |
28 |
28 |
29 /** |
29 /** |
30 * Tests getThreadAllocatedBytes(long id) and getThreadAllocatedBytes(long[] ids), |
30 * Tests getCurrentThreadAllocatedBytes(), getThreadAllocatedBytes(long id). |
|
31 * and getThreadAllocatedBytes(long[] ids), |
31 * functions of com.sun.management.ThreadMXBean |
32 * functions of com.sun.management.ThreadMXBean |
32 * <p> |
33 * <p> |
33 * All methods should |
34 * All methods should |
34 * <br> |
35 * <br> |
35 * - return -1 if ThreadAllocatedMemory allocation is |
36 * - return -1 if ThreadAllocatedMemory allocation is |
47 * Actually runs the test |
48 * Actually runs the test |
48 */ |
49 */ |
49 public void run() { |
50 public void run() { |
50 if (threadMXBean == null) |
51 if (threadMXBean == null) |
51 return; |
52 return; |
|
53 |
|
54 // Expect -1 if thread allocated memory is disabled |
|
55 threadMXBean.setThreadAllocatedMemoryEnabled(false); |
|
56 long result = threadMXBean.getCurrentThreadAllocatedBytes(); |
|
57 if (result != -1) |
|
58 throw new TestFailure("Failure! getCurrentThreadAllocatedBytes() should " |
|
59 + "return -1 if ThreadAllocatedMemoryEnabled is set to false. " |
|
60 + "Received : " + result); |
|
61 threadMXBean.setThreadAllocatedMemoryEnabled(true); |
|
62 // Expect >= 0 value for current thread |
|
63 result = threadMXBean.getCurrentThreadAllocatedBytes(); |
|
64 if (result < 0) |
|
65 throw new TestFailure("Failure! getCurrentThreadAllocatedBytes() should " |
|
66 + "return >= 0 value for current thread. Received : " + result); |
|
67 // Expect >= 0 value for current thread from getThreadAllocatedBytes(id) |
|
68 result = threadMXBean.getThreadAllocatedBytes(Thread.currentThread().getId()); |
|
69 if (result < 0) |
|
70 throw new TestFailure("Failure! getThreadAllocatedBytes(id) should " |
|
71 + "return >= 0 value for current thread. Received : " + result); |
|
72 |
52 MXBeanTestThread thread = new MXBeanTestThread(); |
73 MXBeanTestThread thread = new MXBeanTestThread(); |
53 long id = thread.getId(); |
74 long id = thread.getId(); |
54 long[] idArr = new long[] { id }; |
75 long[] idArr = new long[] { id }; |
55 long result; |
|
56 long[] resultArr; |
76 long[] resultArr; |
|
77 |
57 // Expect -1 for not started threads |
78 // Expect -1 for not started threads |
58 result = threadMXBean.getThreadAllocatedBytes(id); |
79 result = threadMXBean.getThreadAllocatedBytes(id); |
59 if (result != -1) |
80 if (result != -1) |
60 throw new TestFailure("Failure! getThreadAllocatedBytes(long id) should " |
81 throw new TestFailure("Failure! getThreadAllocatedBytes(long id) should " |
61 + "return -1 for not started threads. Recieved : " + result); |
82 + "return -1 for not started threads. Recieved : " + result); |
78 throw new TestFailure("Failure! getThreadAllocatedBytes(long[] ids) should " |
99 throw new TestFailure("Failure! getThreadAllocatedBytes(long[] ids) should " |
79 + "return -1 if ThreadAllocatedMemoryEnabled is set to false. " |
100 + "return -1 if ThreadAllocatedMemoryEnabled is set to false. " |
80 + "Recieved : " + resultArr[0]); |
101 + "Recieved : " + resultArr[0]); |
81 |
102 |
82 threadMXBean.setThreadAllocatedMemoryEnabled(true); |
103 threadMXBean.setThreadAllocatedMemoryEnabled(true); |
83 // Expect > 0 value for running threads |
104 // Expect >= 0 value for running threads |
84 result = threadMXBean.getThreadAllocatedBytes(id); |
105 result = threadMXBean.getThreadAllocatedBytes(id); |
85 if (result < 0) |
106 if (result < 0) |
86 throw new TestFailure("Failure! getThreadAllocatedBytes(long id) should " |
107 throw new TestFailure("Failure! getThreadAllocatedBytes(long id) should " |
87 + "return > 0 value for RUNNING thread. Recieved : " + result); |
108 + "return > 0 value for RUNNING thread. Recieved : " + result); |
88 resultArr = threadMXBean.getThreadAllocatedBytes(idArr); |
109 resultArr = threadMXBean.getThreadAllocatedBytes(idArr); |