1 /*
   2  * Copyright (c) 2018, 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 "memory/metaspace.hpp"
  26 #include "memory/metaspace/metaspaceDCmd.hpp"
  27 #include "memory/resourceArea.hpp"
  28 #include "services/diagnosticCommand.hpp"
  29 #include "services/nmtCommon.hpp"
  30 
  31 namespace metaspace {
  32 
  33 MetaspaceDCmd::MetaspaceDCmd(outputStream* output, bool heap)
  34   : DCmdWithParser(output, heap)
  35   , _basic("basic", "Prints a basic summary (does not need a safepoint).", "BOOLEAN", false, "false")
  36   , _show_loaders("show-loaders", "Shows usage by class loader.", "BOOLEAN", false, "false")
  37   , _by_chunktype("by-chunktype", "Break down numbers by chunk type.", "BOOLEAN", false, "false")
  38   , _by_spacetype("by-spacetype", "Break down numbers by loader type.", "BOOLEAN", false, "false")
  39   , _show_vslist("vslist", "Shows details about the underlying virtual space.", "BOOLEAN", false, "false")
  40   , _show_vsmap("vsmap", "Shows chunk composition of the underlying virtual spaces", "BOOLEAN", false, "false")
  41   , _scale("scale", "Memory usage in which to scale. Valid values are: 1, KB, MB or GB (fixed scale) "
  42            "or \"dynamic\" for a dynamically choosen scale.",
  43      "STRING", false, "dynamic")
  44 {
  45   _dcmdparser.add_dcmd_option(&_basic);
  46   _dcmdparser.add_dcmd_option(&_show_loaders);
  47   _dcmdparser.add_dcmd_option(&_by_chunktype);
  48   _dcmdparser.add_dcmd_option(&_by_spacetype);
  49   _dcmdparser.add_dcmd_option(&_show_vslist);
  50   _dcmdparser.add_dcmd_option(&_show_vsmap);
  51   _dcmdparser.add_dcmd_option(&_scale);
  52 }
  53 
  54 int MetaspaceDCmd::num_arguments() {
  55   ResourceMark rm;
  56   MetaspaceDCmd* dcmd = new MetaspaceDCmd(NULL, false);
  57   if (dcmd != NULL) {
  58     DCmdMark mark(dcmd);
  59     return dcmd->_dcmdparser.num_arguments();
  60   } else {
  61     return 0;
  62   }
  63 }
  64 
  65 void MetaspaceDCmd::execute(DCmdSource source, TRAPS) {
  66   // Parse scale value.
  67   const char* scale_value = _scale.value();
  68   size_t scale = 0;
  69   if (scale_value != NULL) {
  70     if (strcasecmp("dynamic", scale_value) == 0) {
  71       scale = 0;
  72     } else {
  73       scale = NMT_ONLY(NMTUtil::scale_from_name(scale_value)) NOT_NMT(0);
  74       if (scale == 0) {
  75         output()->print_cr("Invalid scale: \"%s\". Will use dynamic scaling.", scale_value);
  76       }
  77     }
  78   }
  79   if (_basic.value() == true) {
  80     if (_show_loaders.value() || _by_chunktype.value() || _by_spacetype.value() ||
  81         _show_vslist.value() || _show_vsmap.value()) {
  82       // Basic mode. Just print essentials. Does not need to be at a safepoint.
  83       output()->print_cr("In basic mode, additional arguments are ignored.");
  84     }
  85     MetaspaceUtils::print_basic_report(output(), scale);
  86   } else {
  87     // Full mode. Requires safepoint.
  88     int flags = 0;
  89     if (_show_loaders.value())         flags |= MetaspaceUtils::rf_show_loaders;
  90     if (_by_chunktype.value())         flags |= MetaspaceUtils::rf_break_down_by_chunktype;
  91     if (_by_spacetype.value())         flags |= MetaspaceUtils::rf_break_down_by_spacetype;
  92     if (_show_vslist.value())          flags |= MetaspaceUtils::rf_show_vslist;
  93     if (_show_vsmap.value())           flags |= MetaspaceUtils::rf_show_vsmap;
  94     VM_PrintMetadata op(output(), scale, flags);
  95     VMThread::execute(&op);
  96   }
  97 }
  98 
  99 } // namespace metaspace
 100