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