1 /*
 2  * Copyright (c) 2013, 2021, 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/nmtCommon.hpp"
26 #include "utilities/globalDefinitions.hpp"
27 
28 #define MEMORY_TYPE_DECLARE_NAME(type, human_readable) \
29   human_readable,
30 
31 STATIC_ASSERT(NMT_off > NMT_unknown);
32 STATIC_ASSERT(NMT_summary > NMT_off);
33 STATIC_ASSERT(NMT_detail > NMT_summary);
34 
35 const char* NMTUtil::_memory_type_names[] = {
36   MEMORY_TYPES_DO(MEMORY_TYPE_DECLARE_NAME)
37   "Value Types",
38 };
39 
40 const char* NMTUtil::scale_name(size_t scale) {
41   switch(scale) {
42     case 1: return "";
43     case K: return "KB";
44     case M: return "MB";
45     case G: return "GB";
46   }
47   ShouldNotReachHere();
48   return NULL;
49 }
50 
51 size_t NMTUtil::scale_from_name(const char* scale) {
52   assert(scale != NULL, "Null pointer check");
53   if (strcasecmp(scale, "1") == 0 || strcasecmp(scale, "b") == 0) {
54     return 1;
55   } else if (strcasecmp(scale, "kb") == 0 || strcasecmp(scale, "k") == 0) {
56     return K;
57   } else if (strcasecmp(scale, "mb") == 0 || strcasecmp(scale, "m") == 0) {
58     return M;
59   } else if (strcasecmp(scale, "gb") == 0 || strcasecmp(scale, "g") == 0) {
60     return G;
61   } else {
62     return 0; // Invalid value
63   }
64   return K;
65 }
66 
67 const char* NMTUtil::tracking_level_to_string(NMT_TrackingLevel lvl) {
68   switch(lvl) {
69     case NMT_unknown: return "unknown"; break;
70     case NMT_off:     return "off"; break;
71     case NMT_summary: return "summary"; break;
72     case NMT_detail:  return "detail"; break;
73     default:          return "invalid"; break;
74   }
75 }
76 
77 // Returns the parsed level; NMT_unknown if string is invalid
78 NMT_TrackingLevel NMTUtil::parse_tracking_level(const char* s) {
79   if (s != NULL) {
80     if (strcmp(s, "summary") == 0) {
81       return NMT_summary;
82     } else if (strcmp(s, "detail") == 0) {
83       return NMT_detail;
84     } else if (strcmp(s, "off") == 0) {
85       return NMT_off;
86     }
87   }
88   return NMT_unknown;
89 }