File: PmemTest.java

package info (click to toggle)
openjdk-21 21.0.8%2B9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 823,976 kB
  • sloc: java: 5,613,338; xml: 1,643,607; cpp: 1,296,296; ansic: 420,291; asm: 404,850; objc: 20,994; sh: 15,271; javascript: 11,245; python: 6,895; makefile: 2,362; perl: 357; awk: 351; sed: 172; jsp: 24; csh: 3
file content (176 lines) | stat: -rw-r--r-- 6,892 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
 * Copyright (c) 2019, 2023, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/*
 * This test is manually run because it requires an NVRAM device to be
 * mapped as DAX file system or, at least, to be simulated by a
 * volatile RAM mapped file system. Also, on AArch64 it requires an
 * ARMV8.2 CPU which implements the dc CVAP instruction (CPU feature
 * dcpop) and an OS that makes it available from user space.
 *
 * If the test runs on such a host without throwing an exception then
 * that confirms that NVRAM-backed byte buffers can be allocated,
 * updated and forced via cache line writeback.
 */

/*
 * How to run this test:
 *
 * Ideally this test should be run on a x86_64/amd64 or aarch64 host
 * fitted with an NVRAM memory device. The NVRAM should appear as
 * /dev/pmem0 or some equivalent DAX file device. The file device
 * should be mounted at /mnt/pmem with a directory tmp created
 * directly under that mount point with a+rwx access.
 *
 * It is possible to run the test on x86_64 using a volatile RAM
 * backed device to simulate NVRAM, even though this does not provide
 * any guarantee of persistence of data across program runs. For the
 * latter case the following instructions explain how to set up the
 * simulated NVRAM device.
 *
 * https://developers.redhat.com/blog/2016/12/05/configuring-and-using-persistent-memory-rhel-7-3/
 * https://nvdimm.wiki.kernel.org/
 * TL;DR: add "memmap=1G!4G" to /etc/default/grub, eg. GRUB_CMDLINE_LINUX="memmap=1G!4G"
 *        then ("sudo" may required)
 *          for RHEL(BIOS-based): grub2-mkconfig -o /boot/grub2/grub.cfg
 *          for RHEL(UEFI-based): grub2-mkconfig -o /boot/efi/EFI/redhat/grub.cfg
 *          for Ubuntu: update-grub2
 *        finally reboot
 *        after the host been rebooted, a new /dev/pmem{N} device should exist,
 *        naming conversion starts at /dev/pmem0
 *
 *  Prepare test directory follow below commands, "sudo" may required
 *  (if ndctl or mkfs.xfs not exist, install ndctl or xfsprogs package first)
 *  (for RHEL8, when call mkfs.xfs, specify the -m reflink=0 option to disable reflink feature)
 *
 *  ndctl create-namespace -f -e namespace0.0 -m memory -M mem
 *  mkdir /mnt/pmem
 *  mkfs.xfs -f /dev/pmem0; mount -o dax /dev/pmem0 /mnt/pmem/
 *  mkdir /mnt/pmem/test; chmod a+rwx /mnt/pmem/test
 *
 * Now run the test program
 *
 *  java PmemTest
 *
 * or
 *
 *  make test TEST=jdk/java/nio/MappedByteBuffer/PmemTest.java
*/

/* @test
 * @summary Testing NVRAM mapped byte buffer support
 * @run main/manual PmemTest
 * @requires (os.family == "linux")
 * @requires (os.arch == "x86_64")
 */

/* @test
 * @summary Testing NVRAM mapped byte buffer support
 * @run main/manual PmemTest
 * @requires (os.family == "linux")
 * @requires ((os.arch == "amd64")|(os.arch == "aarch64")|(os.arch == "ppc64le"))
 * @ignore The test described here is currently disabled on systems that are not
 * x64-based and lack an external NVRAM memory device. In order to re-enable the
 * test, you will need to mount the NVRAM device, which will typically appear as
 * /dev/pmem0, to the directory /mnt/pmem. Once that is done, you can follow the
 * instructions above to create a test directory and remove the ignore tag.
 */

import java.io.File;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.EnumSet;
import java.util.List;
import jdk.nio.mapmode.ExtendedMapMode;

import java.lang.management.ManagementFactory;
import java.lang.management.BufferPoolMXBean;

public class PmemTest {

    public static final int K = 1024;
    public static final int NUM_KBS = 16;

    public static void main(String[] args) throws Exception {

        System.out.println("test");

        String dir = "/tmp"; // mapSync should fail
        dir = "/mnt/pmem/test"; // mapSync should work, since fs mount is -o dax

        Path path = new File(dir, "pmemtest").toPath();

        FileChannel fileChannel = (FileChannel) Files
                .newByteChannel(path, EnumSet.of(
                        StandardOpenOption.READ,
                        StandardOpenOption.WRITE,
                        StandardOpenOption.CREATE));

        MappedByteBuffer mappedByteBuffer = fileChannel.map(ExtendedMapMode.READ_WRITE_SYNC, 0, NUM_KBS * K);


        dumpBufferPoolBeans();

        // for (int loops = 0; loops < 1000; loops++) {
        for (int loops = 0; loops < 100; loops++) {
            int base = K * (loops % NUM_KBS);
            for (int i = 0; i < K ; i++) {
                for (int j = 0; j < K ;j++) {
                    testBuffer(mappedByteBuffer, base, (i << 3) + j);
                    commitBuffer(mappedByteBuffer, base);
                }
            }
        }
        dumpBufferPoolBeans();
    }

    public static void testBuffer(MappedByteBuffer mappedByteBuffer, int base, int start) {
        for (int k = 0; k < 8; k++) {
            int idx = (start + k) % K;
            byte z = mappedByteBuffer.get(base + idx);
            z++;
            mappedByteBuffer.put(base + idx, z);
        }
    }

    public static void commitBuffer(MappedByteBuffer mappedByteBuffer, int base)
    {
        mappedByteBuffer.force(base, K);
    }

    public static void dumpBufferPoolBeans()
    {
        List<BufferPoolMXBean> beansList = ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class);
        for (BufferPoolMXBean bean : beansList) {
            System.out.println("BufferPoolMXBean {" +
                               "\n\tname:          " + bean.getName() +
                               "\n\tcount:         " + bean.getCount() +
                               "\n\ttotalCapacity: " + bean.getTotalCapacity() +
                               "\n\tmemoryUsed:    " + bean.getMemoryUsed() +
                               "\n}");
        }
    }
}