1 /*
2  * Copyright (c) 2014, 2017, 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 
25 #ifndef SHARE_VM_SERVICES_NMT_COMMON_HPP
26 #define SHARE_VM_SERVICES_NMT_COMMON_HPP
27 
28 #include "memory/allocation.hpp"
29 #include "utilities/align.hpp"
30 #include "utilities/globalDefinitions.hpp"
31 
32 #define CALC_OBJ_SIZE_IN_TYPE(obj, type) (align_up_(sizeof(obj), sizeof(type))/sizeof(type))
33 
34 // Native memory tracking level
35 enum NMT_TrackingLevel {
36   NMT_unknown = 0xFF,
37   NMT_off     = 0x00,
38   NMT_minimal = 0x01,
39   NMT_summary = 0x02,
40   NMT_detail  = 0x03
41 };
42 
43 // Number of stack frames to capture. This is a
44 // build time decision.
45 const int NMT_TrackingStackDepth = 4;
46 
47 // A few common utilities for native memory tracking
48 class NMTUtil : AllStatic {
49  public:
50   // Map memory type to index
flag_to_index(MEMFLAGS flag)51   static inline int flag_to_index(MEMFLAGS flag) {
52     const int index = flag & 0xff;
53     assert(index >= 0 && index < (int)mt_number_of_types, "Index out of bounds");
54     return index;
55   }
56 
57   // Map memory type to human readable name
flag_to_name(MEMFLAGS flag)58   static const char* flag_to_name(MEMFLAGS flag) {
59     return _memory_type_names[flag_to_index(flag)];
60   }
61 
62   // Map an index to memory type
index_to_flag(int index)63   static MEMFLAGS index_to_flag(int index) {
64     assert(index >= 0 && index < (int) mt_number_of_types, "Index out of bounds");
65     return (MEMFLAGS)index;
66   }
67 
68   // Memory size scale
69   static const char* scale_name(size_t scale);
70   static size_t scale_from_name(const char* scale);
71 
72   // Translate memory size in specified scale
amount_in_scale(size_t amount,size_t scale)73   static size_t amount_in_scale(size_t amount, size_t scale) {
74     return (amount + scale / 2) / scale;
75   }
76  private:
77   static const char* _memory_type_names[mt_number_of_types];
78 };
79 
80 
81 #endif
82