1 /*
   2  * Copyright (c) 2019, Red Hat, Inc. All rights reserved.
   3  *
   4  * This code is free software; you can redistribute it and/or modify it
   5  * under the terms of the GNU General Public License version 2 only, as
   6  * published by the Free Software Foundation.
   7  *
   8  * This code is distributed in the hope that it will be useful, but WITHOUT
   9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  11  * version 2 for more details (a copy is included in the LICENSE file that
  12  * accompanied this code).
  13  *
  14  * You should have received a copy of the GNU General Public License version
  15  * 2 along with this work; if not, write to the Free Software Foundation,
  16  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  17  *
  18  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  19  * or visit www.oracle.com if you need additional information or have any
  20  * questions.
  21  *
  22  */
  23 
  24 /*
  25  * @test
  26  * @key nmt jcmd
  27  * @library /test/lib
  28  * @modules java.base/jdk.internal.misc
  29  *          java.management
  30  * @build sun.hotspot.WhiteBox
  31  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
  32  *                              sun.hotspot.WhiteBox$WhiteBoxPermission
  33  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -XX:NativeMemoryTracking=detail HugeArenaTracking
  34  */
  35 
  36 import java.util.Random;
  37 import jdk.test.lib.process.ProcessTools;
  38 import jdk.test.lib.process.OutputAnalyzer;
  39 import jdk.test.lib.JDKToolFinder;
  40 import sun.hotspot.WhiteBox;
  41 
  42 public class HugeArenaTracking {
  43   private static final long GB = 1024 * 1024 * 1024;
  44 
  45   public static void main(String args[]) throws Exception {
  46     OutputAnalyzer output;
  47     final WhiteBox wb = WhiteBox.getWhiteBox();
  48 
  49     // Grab my own PID
  50     String pid = Long.toString(ProcessTools.getProcessId());
  51     ProcessBuilder pb = new ProcessBuilder();
  52 
  53     long arena1 = wb.NMTNewArena(1024);
  54     long arena2 = wb.NMTNewArena(1024);
  55 
  56     // Run 'jcmd <pid> VM.native_memory summary'
  57     pb.command(new String[] { JDKToolFinder.getJDKTool("jcmd"), pid, "VM.native_memory", "summary"});
  58     output = new OutputAnalyzer(pb.start());
  59     output.shouldContain("Test (reserved=2KB, committed=2KB)");
  60 
  61     Random rand = new Random();
  62 
  63     // Allocate 2GB+ from arena
  64     long total = 0;
  65     while (total < 2 * GB) {
  66       // Cap to 10M
  67       long inc = rand.nextInt(10 * 1024 * 1024);
  68       wb.NMTArenaMalloc(arena1, inc);
  69       total += inc;
  70     }
  71 
  72     ProcessBuilder pb2 = new ProcessBuilder();
  73     // Run 'jcmd <pid> VM.native_memory summary'
  74     pb2.command(new String[] { JDKToolFinder.getJDKTool("jcmd"), pid, "VM.native_memory", "summary", "scale=GB"});
  75     output = new OutputAnalyzer(pb2.start());
  76     output.shouldContain("Test (reserved=2GB, committed=2GB)");
  77 
  78     wb.NMTFreeArena(arena1);
  79 
  80     output = new OutputAnalyzer(pb.start());
  81     output.shouldContain("Test (reserved=1KB, committed=1KB)");
  82     wb.NMTFreeArena(arena2);
  83 
  84     output = new OutputAnalyzer(pb.start());
  85     output.shouldNotContain("Test (reserved");
  86   }
  87 }