1 /*
2  * Copyright (c) 2003, 2019, 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_SERVICES_MEMORYSERVICE_HPP
26 #define SHARE_SERVICES_MEMORYSERVICE_HPP
27 
28 #include "gc/shared/gcCause.hpp"
29 #include "logging/log.hpp"
30 #include "memory/allocation.hpp"
31 #include "runtime/handles.hpp"
32 #include "services/memoryUsage.hpp"
33 #include "utilities/growableArray.hpp"
34 
35 // Forward declaration
36 class MemoryPool;
37 class MemoryManager;
38 class GCMemoryManager;
39 class CollectedHeap;
40 class CodeHeap;
41 
42 // VM Monitoring and Management Support
43 
44 class MemoryService : public AllStatic {
45 private:
46   enum {
47     init_pools_list_size = 10,
48     init_managers_list_size = 5,
49     init_code_heap_pools_size = 9
50   };
51 
52   static GrowableArray<MemoryPool*>*    _pools_list;
53   static GrowableArray<MemoryManager*>* _managers_list;
54 
55   // memory manager and code heap pools for the CodeCache
56   static MemoryManager*                 _code_cache_manager;
57   static GrowableArray<MemoryPool*>*    _code_heap_pools;
58 
59   static MemoryPool*                    _metaspace_pool;
60   static MemoryPool*                    _compressed_class_pool;
61 
62 public:
63   static void set_universe_heap(CollectedHeap* heap);
64   static void add_code_heap_memory_pool(CodeHeap* heap, const char* name);
65   static void add_metaspace_memory_pools();
66 
67   static MemoryPool*    get_memory_pool(instanceHandle pool);
68   static MemoryManager* get_memory_manager(instanceHandle mgr);
69 
num_memory_pools()70   static const int num_memory_pools() {
71     return _pools_list->length();
72   }
num_memory_managers()73   static const int num_memory_managers() {
74     return _managers_list->length();
75   }
76 
get_memory_pool(int index)77   static MemoryPool* get_memory_pool(int index) {
78     return _pools_list->at(index);
79   }
80 
get_memory_manager(int index)81   static MemoryManager* get_memory_manager(int index) {
82     return _managers_list->at(index);
83   }
84 
85   static void track_memory_usage();
track_code_cache_memory_usage()86   static void track_code_cache_memory_usage() {
87     // Track memory pool usage of all CodeCache memory pools
88     for (int i = 0; i < _code_heap_pools->length(); ++i) {
89       track_memory_pool_usage(_code_heap_pools->at(i));
90     }
91   }
track_metaspace_memory_usage()92   static void track_metaspace_memory_usage() {
93     track_memory_pool_usage(_metaspace_pool);
94   }
track_compressed_class_memory_usage()95   static void track_compressed_class_memory_usage() {
96     track_memory_pool_usage(_compressed_class_pool);
97   }
98   static void track_memory_pool_usage(MemoryPool* pool);
99 
100   static void gc_begin(GCMemoryManager* manager, bool recordGCBeginTime,
101                        bool recordAccumulatedGCTime,
102                        bool recordPreGCUsage, bool recordPeakUsage);
103   static void gc_end(GCMemoryManager* manager, bool recordPostGCUsage,
104                      bool recordAccumulatedGCTime,
105                      bool recordGCEndTime, bool countCollection,
106                      GCCause::Cause cause,
107                      bool allMemoryPoolsAffected);
108 
get_verbose()109   static bool get_verbose() { return log_is_enabled(Info, gc); }
110   static bool set_verbose(bool verbose);
111 
112   // Create an instance of java/lang/management/MemoryUsage
113   static Handle create_MemoryUsage_obj(MemoryUsage usage, TRAPS);
114 };
115 
116 class TraceMemoryManagerStats : public StackObj {
117 private:
118   GCMemoryManager* _gc_memory_manager;
119   bool         _allMemoryPoolsAffected;
120   bool         _recordGCBeginTime;
121   bool         _recordPreGCUsage;
122   bool         _recordPeakUsage;
123   bool         _recordPostGCUsage;
124   bool         _recordAccumulatedGCTime;
125   bool         _recordGCEndTime;
126   bool         _countCollection;
127   GCCause::Cause _cause;
128 public:
TraceMemoryManagerStats()129   TraceMemoryManagerStats() {}
130   TraceMemoryManagerStats(GCMemoryManager* gc_memory_manager,
131                           GCCause::Cause cause,
132                           bool allMemoryPoolsAffected = true,
133                           bool recordGCBeginTime = true,
134                           bool recordPreGCUsage = true,
135                           bool recordPeakUsage = true,
136                           bool recordPostGCUsage = true,
137                           bool recordAccumulatedGCTime = true,
138                           bool recordGCEndTime = true,
139                           bool countCollection = true);
140 
141   void initialize(GCMemoryManager* gc_memory_manager,
142                   GCCause::Cause cause,
143                   bool allMemoryPoolsAffected,
144                   bool recordGCBeginTime,
145                   bool recordPreGCUsage,
146                   bool recordPeakUsage,
147                   bool recordPostGCUsage,
148                   bool recordAccumulatedGCTime,
149                   bool recordGCEndTime,
150                   bool countCollection);
151 
152   ~TraceMemoryManagerStats();
153 };
154 
155 #endif // SHARE_SERVICES_MEMORYSERVICE_HPP
156