Path: blob/master/test/jdk/java/lang/instrument/NMTHelper.java
41149 views
/*1* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 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 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.File;24import java.io.FileWriter;25import java.util.regex.Matcher;26import java.util.regex.Pattern;27import java.util.Arrays;28import java.util.stream.Collectors;29import java.lang.management.ManagementFactory;30import javax.management.MalformedObjectNameException;31import javax.management.ObjectName;3233public class NMTHelper34{35public static void baseline() {36executeDcmd("vmNativeMemory", "baseline");37}3839// Total: reserved=3484685KB +293KB, committed=266629KB +293KB40private static Pattern totalLine = Pattern.compile("^Total: reserved=\\d+KB .*KB, committed=\\d+KB (.*)KB$");4142public static long committedDiff() throws Exception {43String res = (String) executeDcmd("vmNativeMemory", "detail.diff");44String[] lines = res.split("\n");45for (String line : lines) {46Matcher matcher = totalLine.matcher(line);47if (matcher.matches()) {48String committed = matcher.group(1);49return Long.parseLong(committed);50}51}52throw new Exception("Could not find the Total line in the NMT output.");53}5455private static String executeDcmd(String cmd, String ... args) {56ObjectName oname = null;57try {58oname = ObjectName.getInstance("com.sun.management:type=DiagnosticCommand");59} catch (MalformedObjectNameException mone) {60throw new RuntimeException(mone);61}62Object[] dcmdArgs = {args};63String[] signature = {String[].class.getName()};6465String cmdString = cmd + " " +66Arrays.stream(args).collect(Collectors.joining(" "));67File f = new File("dcmdoutput-" + cmd + "-" + System.currentTimeMillis() + ".txt");68System.out.println("Output from Dcmd '" + cmdString + "' is being written to file " + f);69try (FileWriter fw = new FileWriter(f)) {70fw.write("> " + cmdString + ":");71String result = (String)ManagementFactory.getPlatformMBeanServer().72invoke(oname, cmd, dcmdArgs, signature);73fw.write(result);74return result;75} catch(Exception ex) {76ex.printStackTrace();77}78return null;79}80}818283