57804
|
1 |
/*
|
|
2 |
* Copyright (c) 2019, 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 |
|
|
24 |
/*
|
|
25 |
* This test is manually run because it requires an NVRAM device to be
|
|
26 |
* mapped as DAX file system or, at least, to be simulated by a
|
|
27 |
* volatile RAM mapped file system. Also, on AArch64 it requires an
|
|
28 |
* ARMV8.2 CPU which implements the dc CVAP instruction (CPU feature
|
|
29 |
* dcpop) and an OS that makes it available from user space.
|
|
30 |
*
|
|
31 |
* If the test runs on such a host without throwing an exception then
|
|
32 |
* that confirms that NVRAM-backed byte buffers can be allocated,
|
|
33 |
* updated and forced via cache line writeback.
|
|
34 |
*/
|
|
35 |
|
|
36 |
/*
|
|
37 |
* How to run this test:
|
|
38 |
*
|
|
39 |
* Ideally this test should be run on a x86_64/amd64 or aarch64 host
|
|
40 |
* fitted with an NVRAM memory device. The NVRAM should appear as
|
|
41 |
* /dev/pmem0 or some equivalent DAX file device. The file device
|
|
42 |
* should be mounted at /mnt/pmem with a directory tmp created
|
|
43 |
* directly under that mount point with a+rwx access.
|
|
44 |
*
|
|
45 |
* It is possible to run the test on x86_64 using a volatile RAM
|
|
46 |
* backed device to simulate NVRAM, even though this does not provide
|
|
47 |
* any guarantee of persistence of data across program runs. For the
|
|
48 |
* latter case the following instructions explain how to set up the
|
|
49 |
* simulated NVRAM device.
|
|
50 |
*
|
|
51 |
* https://developers.redhat.com/blog/2016/12/05/configuring-and-using-persistent-memory-rhel-7-3/
|
|
52 |
* https://nvdimm.wiki.kernel.org/
|
|
53 |
* TL;DR: add "memmap=1G!4G" to /etc/default/grub,
|
|
54 |
* then grub2-mkconfig -o /boot/grub2/grub.cfg and reboot
|
|
55 |
*
|
|
56 |
* ndctl create-namespace * -f -e namespace0.0 -m memory -M mem
|
|
57 |
* mkdir /mnt/pmem
|
|
58 |
* mkfs.xfs -f /dev/pmem0; mount -o dax /dev/pmem0 /mnt/pmem/
|
|
59 |
* mkdir /mnt/pmem/test; chmod a+rwx /mnt/pmem/test
|
|
60 |
*
|
|
61 |
* Now run the test program
|
|
62 |
*
|
|
63 |
* java PmemTest
|
|
64 |
*
|
|
65 |
* or
|
|
66 |
*
|
|
67 |
* make test TEST=jdk/java/nio/MappedByteBuffer/PmemTest.java
|
|
68 |
*/
|
|
69 |
|
|
70 |
/* @test
|
|
71 |
* @summary Testing NVRAM mapped byte buffer support
|
|
72 |
* @run main/manual PmemTest
|
|
73 |
* @requires (os.family == "linux")
|
|
74 |
* @requires ((os.arch == "x86_64")|(os.arch == "amd64")|(os.arch == "aarch64"))
|
|
75 |
*/
|
|
76 |
|
|
77 |
import java.io.File;
|
|
78 |
import java.nio.MappedByteBuffer;
|
|
79 |
import java.nio.channels.FileChannel;
|
|
80 |
import java.nio.file.Files;
|
|
81 |
import java.nio.file.Path;
|
|
82 |
import java.nio.file.StandardOpenOption;
|
|
83 |
import java.util.EnumSet;
|
|
84 |
import java.util.List;
|
|
85 |
import jdk.nio.mapmode.ExtendedMapMode;
|
|
86 |
|
|
87 |
import java.lang.management.ManagementFactory;
|
|
88 |
import java.lang.management.BufferPoolMXBean;
|
|
89 |
|
|
90 |
public class PmemTest {
|
|
91 |
|
|
92 |
public static final int K = 1024;
|
|
93 |
public static final int NUM_KBS = 16;
|
|
94 |
|
|
95 |
public static void main(String[] args) throws Exception {
|
|
96 |
|
|
97 |
System.out.println("test");
|
|
98 |
|
|
99 |
String dir = "/tmp"; // mapSync should fail
|
|
100 |
dir = "/mnt/pmem/test"; // mapSync should work, since fs mount is -o dax
|
|
101 |
|
|
102 |
Path path = new File(dir, "pmemtest").toPath();
|
|
103 |
|
|
104 |
FileChannel fileChannel = (FileChannel) Files
|
|
105 |
.newByteChannel(path, EnumSet.of(
|
|
106 |
StandardOpenOption.READ,
|
|
107 |
StandardOpenOption.WRITE,
|
|
108 |
StandardOpenOption.CREATE));
|
|
109 |
|
|
110 |
MappedByteBuffer mappedByteBuffer = fileChannel.map(ExtendedMapMode.READ_WRITE_SYNC, 0, NUM_KBS * K);
|
|
111 |
|
|
112 |
|
|
113 |
dumpBufferPoolBeans();
|
|
114 |
|
|
115 |
// for (int loops = 0; loops < 1000; loops++) {
|
|
116 |
for (int loops = 0; loops < 100; loops++) {
|
|
117 |
int base = K * (loops % NUM_KBS);
|
|
118 |
for (int i = 0; i < K ; i++) {
|
|
119 |
for (int j = 0; j < K ;j++) {
|
|
120 |
testBuffer(mappedByteBuffer, base, (i << 3) + j);
|
|
121 |
commitBuffer(mappedByteBuffer, base);
|
|
122 |
}
|
|
123 |
}
|
|
124 |
}
|
|
125 |
dumpBufferPoolBeans();
|
|
126 |
}
|
|
127 |
|
|
128 |
public static void testBuffer(MappedByteBuffer mappedByteBuffer, int base, int start) {
|
|
129 |
for (int k = 0; k < 8; k++) {
|
|
130 |
int idx = (start + k) % K;
|
|
131 |
byte z = mappedByteBuffer.get(base + idx);
|
|
132 |
z++;
|
|
133 |
mappedByteBuffer.put(base + idx, z);
|
|
134 |
}
|
|
135 |
}
|
|
136 |
|
|
137 |
public static void commitBuffer(MappedByteBuffer mappedByteBuffer, int base)
|
|
138 |
{
|
|
139 |
mappedByteBuffer.force(base, K);
|
|
140 |
}
|
|
141 |
|
|
142 |
public static void dumpBufferPoolBeans()
|
|
143 |
{
|
|
144 |
List<BufferPoolMXBean> beansList = ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class);
|
|
145 |
for (BufferPoolMXBean bean : beansList) {
|
|
146 |
System.out.println("BufferPoolMXBean {" +
|
|
147 |
"\n\tname: " + bean.getName() +
|
|
148 |
"\n\tcount: " + bean.getCount() +
|
|
149 |
"\n\ttotalCapacity: " + bean.getTotalCapacity() +
|
|
150 |
"\n\tmemoryUsed: " + bean.getMemoryUsed() +
|
|
151 |
"\n}");
|
|
152 |
}
|
|
153 |
}
|
|
154 |
}
|