author | sherman |
Wed, 04 Mar 2009 09:26:41 -0800 | |
changeset 2178 | 5a730fff4d0d |
parent 1143 | 645d4b930f93 |
child 5506 | 202f599c92aa |
permissions | -rw-r--r-- |
1143
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
1 |
/* |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
2 |
* Copyright 2007-2008 Sun Microsystems, Inc. All Rights Reserved. |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
4 |
* |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
8 |
* |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
13 |
* accompanied this code). |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
14 |
* |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
18 |
* |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
19 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
20 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
21 |
* have any questions. |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
22 |
*/ |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
23 |
|
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
24 |
/* @test |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
25 |
* @bug 6606598 |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
26 |
* @summary Unit test for java.nio.BufferPoolMXBean |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
27 |
*/ |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
28 |
|
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
29 |
import java.nio.ByteBuffer; |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
30 |
import java.nio.MappedByteBuffer; |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
31 |
import java.nio.BufferPoolMXBean; |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
32 |
import java.nio.channels.FileChannel; |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
33 |
import java.io.File; |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
34 |
import java.io.RandomAccessFile; |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
35 |
import java.lang.management.ManagementFactory; |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
36 |
import javax.management.MBeanServer; |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
37 |
import javax.management.ObjectName; |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
38 |
import java.util.*; |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
39 |
|
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
40 |
public class Basic { |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
41 |
|
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
42 |
// static fields to ensure buffers aren't GC'ed |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
43 |
static List<ByteBuffer> buffers; |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
44 |
static MappedByteBuffer mbb; |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
45 |
|
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
46 |
// check counters |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
47 |
static void check(List<BufferPoolMXBean> pools, |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
48 |
int minBufferCount, |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
49 |
long minTotalCapacity) |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
50 |
{ |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
51 |
int bufferCount = 0; |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
52 |
long totalCap = 0; |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
53 |
long totalMem = 0; |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
54 |
for (BufferPoolMXBean pool: pools) { |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
55 |
bufferCount += pool.getCount(); |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
56 |
totalCap += pool.getTotalCapacity(); |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
57 |
totalMem += pool.getMemoryUsed(); |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
58 |
} |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
59 |
if (bufferCount < minBufferCount) |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
60 |
throw new RuntimeException("Count less than expected"); |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
61 |
if (totalMem < minTotalCapacity) |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
62 |
throw new RuntimeException("Memory usage less than expected"); |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
63 |
if (totalCap < minTotalCapacity) |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
64 |
throw new RuntimeException("Total capacity less than expected"); |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
65 |
} |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
66 |
|
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
67 |
public static void main(String[] args) throws Exception { |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
68 |
Random rand = new Random(); |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
69 |
|
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
70 |
// allocate a few direct buffers |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
71 |
int bufferCount = 5 + rand.nextInt(20); |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
72 |
buffers = new ArrayList<ByteBuffer>(bufferCount); |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
73 |
long totalCapacity = 0L; |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
74 |
for (int i=0; i<bufferCount; i++) { |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
75 |
int cap = 1024 + rand.nextInt(4096); |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
76 |
buffers.add( ByteBuffer.allocateDirect(cap) ); |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
77 |
totalCapacity += cap; |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
78 |
} |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
79 |
|
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
80 |
// map a file |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
81 |
File f = File.createTempFile("blah", null); |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
82 |
f.deleteOnExit(); |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
83 |
RandomAccessFile raf = new RandomAccessFile(f, "rw"); |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
84 |
FileChannel fc = raf.getChannel(); |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
85 |
mbb = fc.map(FileChannel.MapMode.READ_WRITE, 10, 100); |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
86 |
bufferCount++; |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
87 |
totalCapacity += mbb.capacity(); |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
88 |
|
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
89 |
// direct |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
90 |
List<BufferPoolMXBean> pools = |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
91 |
ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class); |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
92 |
check(pools, bufferCount, totalCapacity); |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
93 |
|
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
94 |
// using MBeanServer |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
95 |
MBeanServer server = ManagementFactory.getPlatformMBeanServer(); |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
96 |
Set<ObjectName> mbeans = server.queryNames( |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
97 |
new ObjectName("java.nio:type=BufferPool,*"), null); |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
98 |
pools = new ArrayList<BufferPoolMXBean>(); |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
99 |
for (ObjectName name: mbeans) { |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
100 |
BufferPoolMXBean pool = ManagementFactory |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
101 |
.newPlatformMXBeanProxy(server, name.toString(), BufferPoolMXBean.class); |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
102 |
pools.add(pool); |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
103 |
} |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
104 |
check(pools, bufferCount, totalCapacity); |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
105 |
} |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
diff
changeset
|
106 |
} |