src/share/vm/services/nmtDCmd.cpp

Mon, 17 Dec 2012 13:14:02 -0500

author
zgu
date
Mon, 17 Dec 2012 13:14:02 -0500
changeset 4348
3f84e17b6bca
parent 3900
d2a62e0f25eb
child 4810
06db4c0afbf3
permissions
-rw-r--r--

8004802: jcmd VM.native_memory baseline=false crashes VM
Summary: NMT has to check option's value also to determine which command to execute
Reviewed-by: acorn, coleenp, hseigel

     1 /*
     2  * Copyright (c) 2012, 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 #include "precompiled.hpp"
    25 #include "services/nmtDCmd.hpp"
    26 #include "services/memReporter.hpp"
    27 #include "services/memTracker.hpp"
    28 #include "utilities/globalDefinitions.hpp"
    30 NMTDCmd::NMTDCmd(outputStream* output,
    31   bool heap): DCmdWithParser(output, heap),
    32   _summary("summary", "request runtime to report current memory summary, " \
    33            "which includes total reserved and committed memory, along " \
    34            "with memory usage summary by each subsytem.",
    35            "BOOLEAN", false, "false"),
    36   _detail("detail", "request runtime to report memory allocation >= "
    37            "1K by each callsite.",
    38            "BOOLEAN", false, "false"),
    39   _baseline("baseline", "request runtime to baseline current memory usage, " \
    40             "so it can be compared against in later time.",
    41             "BOOLEAN", false, "false"),
    42   _summary_diff("summary.diff", "request runtime to report memory summary " \
    43             "comparison against previous baseline.",
    44             "BOOLEAN", false, "false"),
    45   _detail_diff("detail.diff", "request runtime to report memory detail " \
    46             "comparison against previous baseline, which shows the memory " \
    47             "allocation activities at different callsites.",
    48             "BOOLEAN", false, "false"),
    49   _shutdown("shutdown", "request runtime to shutdown itself and free the " \
    50             "memory used by runtime.",
    51             "BOOLEAN", false, "false"),
    52 #ifndef PRODUCT
    53   _debug("debug", "print tracker statistics. Debug only, not thread safe", \
    54             "BOOLEAN", false, "false"),
    55 #endif
    56   _scale("scale", "Memory usage in which scale, KB, MB or GB",
    57        "STRING", false, "KB") {
    58   _dcmdparser.add_dcmd_option(&_summary);
    59   _dcmdparser.add_dcmd_option(&_detail);
    60   _dcmdparser.add_dcmd_option(&_baseline);
    61   _dcmdparser.add_dcmd_option(&_summary_diff);
    62   _dcmdparser.add_dcmd_option(&_detail_diff);
    63   _dcmdparser.add_dcmd_option(&_shutdown);
    64 #ifndef PRODUCT
    65   _dcmdparser.add_dcmd_option(&_debug);
    66 #endif
    67   _dcmdparser.add_dcmd_option(&_scale);
    68 }
    70 void NMTDCmd::execute(TRAPS) {
    71   const char* scale_value = _scale.value();
    72   size_t scale_unit;
    73   if (strcmp(scale_value, "KB") == 0 || strcmp(scale_value, "kb") == 0) {
    74     scale_unit = K;
    75   } else if (strcmp(scale_value, "MB") == 0 ||
    76              strcmp(scale_value, "mb") == 0) {
    77     scale_unit = M;
    78   } else if (strcmp(scale_value, "GB") == 0 ||
    79              strcmp(scale_value, "gb") == 0) {
    80     scale_unit = G;
    81   } else {
    82     output()->print_cr("Incorrect scale value: %s", scale_value);
    83     return;
    84   }
    86   int nopt = 0;
    87   if(_summary.is_set() && _summary.value()) { ++nopt; }
    88   if(_detail.is_set() && _detail.value()) { ++nopt; }
    89   if(_baseline.is_set() && _baseline.value()) { ++nopt; }
    90   if(_summary_diff.is_set() && _summary_diff.value()) { ++nopt; }
    91   if(_detail_diff.is_set() && _detail_diff.value()) { ++nopt; }
    92   if(_shutdown.is_set() && _shutdown.value()) { ++nopt; }
    93 #ifndef PRODUCT
    94   if(_debug.is_set() && _debug.value()) { ++nopt; }
    95 #endif
    97   if(nopt > 1) {
    98       output()->print_cr("At most one of the following option can be specified: " \
    99         "summary, detail, baseline, summary.diff, detail.diff, shutdown"
   100 #ifndef PRODUCT
   101         ", debug"
   102 #endif
   103       );
   104       return;
   105   } else if (nopt == 0) {
   106     if (_summary.is_set()) {
   107       output()->print_cr("No command to execute");
   108       return;
   109     } else {
   110       _summary.set_value(true);
   111     }
   112   }
   114 #ifndef PRODUCT
   115   if (_debug.value()) {
   116     output()->print_cr("debug command is NOT thread-safe, may cause crash");
   117     MemTracker::print_tracker_stats(output());
   118     return;
   119   }
   120 #endif
   122   // native memory tracking has to be on
   123   if (!MemTracker::is_on() || MemTracker::shutdown_in_progress()) {
   124     // if it is not on, what's the reason?
   125     output()->print_cr(MemTracker::reason());
   126     return;
   127   }
   129   if (_summary.value()) {
   130     BaselineTTYOutputer outputer(output());
   131     MemTracker::print_memory_usage(outputer, scale_unit, true);
   132   } else if (_detail.value()) {
   133     BaselineTTYOutputer outputer(output());
   134     MemTracker::print_memory_usage(outputer, scale_unit, false);
   135   } else if (_baseline.value()) {
   136     if (MemTracker::baseline()) {
   137       output()->print_cr("Successfully baselined.");
   138     } else {
   139       output()->print_cr("Baseline failed.");
   140     }
   141   } else if (_summary_diff.value()) {
   142     if (MemTracker::has_baseline()) {
   143       BaselineTTYOutputer outputer(output());
   144       MemTracker::compare_memory_usage(outputer, scale_unit, true);
   145     } else {
   146       output()->print_cr("No baseline to compare, run 'baseline' command first");
   147     }
   148   } else if (_detail_diff.value()) {
   149     if (MemTracker::has_baseline()) {
   150       BaselineTTYOutputer outputer(output());
   151       MemTracker::compare_memory_usage(outputer, scale_unit, false);
   152     } else {
   153       output()->print_cr("No baseline to compare to, run 'baseline' command first");
   154     }
   155   } else if (_shutdown.value()) {
   156     MemTracker::shutdown(MemTracker::NMT_shutdown_user);
   157     output()->print_cr("Shutdown is in progress, it will take a few moments to " \
   158       "completely shutdown");
   159   } else {
   160     ShouldNotReachHere();
   161     output()->print_cr("Unknown command");
   162   }
   163 }
   165 int NMTDCmd::num_arguments() {
   166   ResourceMark rm;
   167   NMTDCmd* dcmd = new NMTDCmd(NULL, false);
   168   if (dcmd != NULL) {
   169     DCmdMark mark(dcmd);
   170     return dcmd->_dcmdparser.num_arguments();
   171   } else {
   172     return 0;
   173   }
   174 }

mercurial