1 /*
   2  * Copyright (c) 2013 Oracle and/or its affiliates. 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 import java.io.File;
  25 import java.io.FileWriter;
  26 import java.util.regex.Matcher;
  27 import java.util.regex.Pattern;
  28 import java.util.Arrays;
  29 import java.util.stream.Collectors;
  30 import sun.management.ManagementFactoryHelper;
  31 import com.sun.management.DiagnosticCommandMBean;
  32 
  33 public class NMTHelper
  34 {
  35     public static void baseline() {
  36         executeDcmd("vmNativeMemory", "baseline");
  37     }
  38 
  39     // Total: reserved=3484685KB +293KB, committed=266629KB +293KB
  40     private static Pattern totalLine = Pattern.compile("^Total: reserved=\\d+KB .*KB, committed=\\d+KB (.*)KB$");
  41 
  42     public static long committedDiff() throws Exception {
  43         String res = (String) executeDcmd("vmNativeMemory", "detail.diff");
  44         String[] lines = res.split("\n");
  45         for (String line : lines) {
  46             Matcher matcher = totalLine.matcher(line);
  47             if (matcher.matches()) {
  48                 String committed = matcher.group(1);
  49                 return Long.parseLong(committed);
  50             }
  51         }
  52         throw new Exception("Could not find the Total line in the NMT output.");
  53     }
  54 
  55     private static String executeDcmd(String cmd, String ... args) {
  56         DiagnosticCommandMBean dcmd = ManagementFactoryHelper.getDiagnosticCommandMBean();
  57         Object[] dcmdArgs = {args};
  58         String[] signature = {String[].class.getName()};
  59 
  60         String cmdString = cmd + " " +
  61             Arrays.stream(args).collect(Collectors.joining(" "));
  62         File f = new File("dcmdoutput-" + cmd + "-" + System.currentTimeMillis() + ".txt");
  63         System.out.println("Output from Dcmd '" + cmdString + "' is being written to file " + f);
  64         try (FileWriter fw = new FileWriter(f)) {
  65             fw.write("> " + cmdString + ":");
  66             String result = (String) dcmd.invoke(cmd, dcmdArgs, signature);
  67             fw.write(result);
  68             return result;
  69         } catch(Exception ex) {
  70             ex.printStackTrace();
  71         }
  72         return null;
  73     }
  74 }