1 /*
   2  * Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2018, 2020 SAP SE. All rights reserved.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  *
  24  */
  25 
  26 #include "precompiled.hpp"
  27 #include "memory/metaspace/metaspaceReport.hpp"
  28 #include "memory/metaspace/metaspaceDCmd.hpp"
  29 #include "memory/resourceArea.hpp"
  30 #include "services/diagnosticCommand.hpp"
  31 #include "services/nmtCommon.hpp"
  32 
  33 namespace metaspace {
  34 
  35 MetaspaceDCmd::MetaspaceDCmd(outputStream* output, bool heap)
  36   : DCmdWithParser(output, heap)
  37   , _basic("basic", "Prints a basic summary (does not need a safepoint).", "BOOLEAN", false, "false")
  38   , _show_loaders("show-loaders", "Shows usage by class loader.", "BOOLEAN", false, "false")
  39   , _by_spacetype("by-spacetype", "Break down numbers by loader type.", "BOOLEAN", false, "false")
  40   , _by_chunktype("by-chunktype", "Break down numbers by chunk type.", "BOOLEAN", false, "false")
  41   , _show_vslist("vslist", "Shows details about the underlying virtual space.", "BOOLEAN", false, "false")
  42   , _scale("scale", "Memory usage in which to scale. Valid values are: 1, KB, MB or GB (fixed scale) "
  43            "or \"dynamic\" for a dynamically choosen scale.",
  44            "STRING", false, "dynamic")
  45   , _show_classes("show-classes", "If show-loaders is set, shows loaded classes for each loader.", "BOOLEAN", false, "false")
  46 {
  47   _dcmdparser.add_dcmd_option(&_basic);
  48   _dcmdparser.add_dcmd_option(&_show_loaders);
  49   _dcmdparser.add_dcmd_option(&_show_classes);
  50   _dcmdparser.add_dcmd_option(&_by_chunktype);
  51   _dcmdparser.add_dcmd_option(&_by_spacetype);
  52   _dcmdparser.add_dcmd_option(&_show_vslist);
  53   _dcmdparser.add_dcmd_option(&_scale);
  54 }
  55 
  56 int MetaspaceDCmd::num_arguments() {
  57   ResourceMark rm;
  58   MetaspaceDCmd* dcmd = new MetaspaceDCmd(NULL, false);
  59   if (dcmd != NULL) {
  60     DCmdMark mark(dcmd);
  61     return dcmd->_dcmdparser.num_arguments();
  62   } else {
  63     return 0;
  64   }
  65 }
  66 
  67 void MetaspaceDCmd::execute(DCmdSource source, TRAPS) {
  68   // Parse scale value.
  69   const char* scale_value = _scale.value();
  70   size_t scale = 0;
  71   if (scale_value != NULL) {
  72     if (strcasecmp("dynamic", scale_value) == 0) {
  73       scale = 0;
  74     } else {
  75       scale = NMT_ONLY(NMTUtil::scale_from_name(scale_value)) NOT_NMT(0);
  76       if (scale == 0) {
  77         output()->print_cr("Invalid scale: \"%s\". Will use dynamic scaling.", scale_value);
  78       }
  79     }
  80   }
  81   if (_basic.value() == true) {
  82     if (_show_loaders.value() || _by_chunktype.value() || _by_spacetype.value() ||
  83         _show_vslist.value()) {
  84       // Basic mode. Just print essentials. Does not need to be at a safepoint.
  85       output()->print_cr("In basic mode, additional arguments are ignored.");
  86     }
  87     MetaspaceUtils::print_basic_report(output(), scale);
  88   } else {
  89     // Full mode. Requires safepoint.
  90     int flags = 0;
  91     if (_show_loaders.value())         flags |= MetaspaceReporter::rf_show_loaders;
  92     if (_show_classes.value())         flags |= MetaspaceReporter::rf_show_classes;
  93     if (_by_chunktype.value())         flags |= MetaspaceReporter::rf_break_down_by_chunktype;
  94     if (_by_spacetype.value())         flags |= MetaspaceReporter::rf_break_down_by_spacetype;
  95     if (_show_vslist.value())          flags |= MetaspaceReporter::rf_show_vslist;
  96     VM_PrintMetadata op(output(), scale, flags);
  97     VMThread::execute(&op);
  98   }
  99 }
 100 
 101 } // namespace metaspace
 102