1 /*
 2  * Copyright (c) 2014, 2024, Alibaba Group Holding Limited. 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 package gc.parallel;
25 
26 /**
27  * @test TestAlwaysPreTouchBehavior
28  * @summary Tests AlwaysPreTouch Bahavior, pages of java heap should be pretouched with AlwaysPreTouch enabled. This test reads RSS of test process, which should be bigger than heap size(1g) with AlwaysPreTouch enabled.
29  * @requires vm.gc.Parallel
30  * @requires vm.debug != true
31  * @requires os.family == "linux"
32  * @requires os.maxMemory > 2G
33  * @library /test/lib
34  * @run main/othervm -Xmx1g -Xms1g -XX:+UseParallelGC -XX:+AlwaysPreTouch gc.parallel.TestAlwaysPreTouchBehavior
35  */
36 import java.lang.management.ManagementFactory;
37 import java.lang.management.ThreadInfo;
38 import java.lang.management.ThreadMXBean;
39 import java.nio.file.Files;
40 import java.nio.file.Path;
41 import java.nio.file.Paths;
42 import java.io.IOException;
43 import java.util.*;
44 import javax.management.*;
45 import java.lang.management.*;
46 import jdk.test.lib.Utils;
47 import jdk.test.lib.Asserts;
48 import java.lang.management.*;
49 import java.util.stream.*;
50 import java.io.*;
51 
52 public class TestAlwaysPreTouchBehavior {
53     public static long getProcessRssInKb() throws IOException {
54         String pid = ManagementFactory.getRuntimeMXBean().getName().split("@")[0];
55         // Read RSS from /proc/$pid/status. Only available on Linux.
56         String processStatusFile = "/proc/" + pid + "/status";
57         BufferedReader reader = new BufferedReader(new FileReader(processStatusFile));
58         String line = null;
59         while ((line = reader.readLine()) != null) {
60             if (line.startsWith("VmRSS:")) {
61                 break;
62             }
63         }
64         reader.close();
65         return Long.valueOf(line.split("\\s+")[1].trim());
66     }
67     public static void main(String [] args) {
68     long rss = 0;
69     Runtime runtime = Runtime.getRuntime();
70     long committedMemory = runtime.totalMemory() / 1024; // in kb
71     try {
72         rss = getProcessRssInKb();
73     } catch (Exception e) {
74         System.out.println("cannot get RSS, just skip");
75         return; // Did not get avaiable RSS, just ignore this test
76     }
77     Asserts.assertGreaterThanOrEqual(rss, committedMemory, "RSS of this process(" + rss + "kb) should be bigger than or equal to committed heap mem(" + committedMemory + "kb)");
78    }
79 }
80