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