4437N/A/*
4437N/A * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
4437N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4437N/A *
4437N/A * This code is free software; you can redistribute it and/or modify it
4437N/A * under the terms of the GNU General Public License version 2 only, as
4437N/A * published by the Free Software Foundation.
4437N/A *
4437N/A * This code is distributed in the hope that it will be useful, but WITHOUT
4437N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4437N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
4437N/A * version 2 for more details (a copy is included in the LICENSE file that
4437N/A * accompanied this code).
4437N/A *
4437N/A * You should have received a copy of the GNU General Public License version
4437N/A * 2 along with this work; if not, write to the Free Software Foundation,
4437N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4437N/A *
4437N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4437N/A * or visit www.oracle.com if you need additional information or have any
4437N/A * questions.
4437N/A */
4437N/A
4437N/A/*
4437N/A * @test
4437N/A * @key nmt jcmd
4437N/A * @library /testlibrary /testlibrary/whitebox
4437N/A * @build ThreadedMallocTestType
4437N/A * @run main ClassFileInstaller sun.hotspot.WhiteBox
4437N/A * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -XX:NativeMemoryTracking=detail ThreadedMallocTestType
4437N/A */
4437N/A
4437N/Aimport com.oracle.java.testlibrary.*;
4437N/Aimport sun.hotspot.WhiteBox;
4437N/A
4437N/Apublic class ThreadedMallocTestType {
4437N/A public static long memAlloc1;
4437N/A public static long memAlloc2;
4437N/A public static long memAlloc3;
4437N/A
4437N/A public static void main(String args[]) throws Exception {
4437N/A OutputAnalyzer output;
4437N/A final WhiteBox wb = WhiteBox.getWhiteBox();
4437N/A
4437N/A // Grab my own PID
4437N/A String pid = Integer.toString(ProcessTools.getProcessId());
4437N/A ProcessBuilder pb = new ProcessBuilder();
4437N/A
4437N/A Thread allocThread = new Thread() {
4437N/A public void run() {
4437N/A // Alloc memory using the WB api
4437N/A memAlloc1 = wb.NMTMalloc(128 * 1024);
4437N/A memAlloc2 = wb.NMTMalloc(256 * 1024);
4437N/A memAlloc3 = wb.NMTMalloc(512 * 1024);
4437N/A }
4437N/A };
4437N/A
4437N/A allocThread.start();
4437N/A allocThread.join();
4437N/A
4437N/A // Use WB API to ensure that all data has been merged before we continue
4437N/A if (!wb.NMTWaitForDataMerge()) {
4437N/A throw new Exception("Call to WB API NMTWaitForDataMerge() failed");
4437N/A }
4437N/A
4437N/A // Run 'jcmd <pid> VM.native_memory summary'
4437N/A pb.command(new String[] { JDKToolFinder.getJDKTool("jcmd"), pid, "VM.native_memory", "summary"});
4437N/A output = new OutputAnalyzer(pb.start());
4437N/A output.shouldContain("Test (reserved=896KB, committed=896KB)");
4437N/A
4437N/A Thread freeThread = new Thread() {
4437N/A public void run() {
4437N/A // Free the memory allocated by NMTMalloc
4437N/A wb.NMTFree(memAlloc1);
4437N/A wb.NMTFree(memAlloc2);
4437N/A wb.NMTFree(memAlloc3);
4437N/A }
4437N/A };
4437N/A
4437N/A freeThread.start();
4437N/A freeThread.join();
4437N/A
4437N/A // Use WB API to ensure that all data has been merged before we continue
4437N/A if (!wb.NMTWaitForDataMerge()) {
4437N/A throw new Exception("Call to WB API NMTWaitForDataMerge() failed");
4437N/A }
4437N/A
4437N/A output = new OutputAnalyzer(pb.start());
4437N/A output.shouldNotContain("Test (reserved=");
4437N/A }
4437N/A}