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