1 /*
2 * Copyright (c) 2003, 2018, 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 #include "precompiled.hpp"
26 #include "classfile/systemDictionary.hpp"
27 #include "classfile/vmSymbols.hpp"
28 #include "gc/shared/collectedHeap.hpp"
29 #include "logging/logConfiguration.hpp"
30 #include "memory/heap.hpp"
31 #include "memory/memRegion.hpp"
32 #include "memory/resourceArea.hpp"
33 #include "oops/oop.inline.hpp"
34 #include "runtime/globals.hpp"
35 #include "runtime/handles.inline.hpp"
36 #include "runtime/javaCalls.hpp"
37 #include "services/classLoadingService.hpp"
38 #include "services/lowMemoryDetector.hpp"
39 #include "services/management.hpp"
40 #include "services/memoryManager.hpp"
41 #include "services/memoryPool.hpp"
42 #include "services/memoryService.hpp"
43 #include "utilities/growableArray.hpp"
44 #include "utilities/macros.hpp"
45
46 GrowableArray<MemoryPool*>* MemoryService::_pools_list =
47 new (ResourceObj::C_HEAP, mtServiceability) GrowableArray<MemoryPool*>(init_pools_list_size, mtServiceability);
48 GrowableArray<MemoryManager*>* MemoryService::_managers_list =
49 new (ResourceObj::C_HEAP, mtServiceability) GrowableArray<MemoryManager*>(init_managers_list_size, mtServiceability);
50
51 MemoryManager* MemoryService::_code_cache_manager = NULL;
52 GrowableArray<MemoryPool*>* MemoryService::_code_heap_pools =
53 new (ResourceObj::C_HEAP, mtServiceability) GrowableArray<MemoryPool*>(init_code_heap_pools_size, mtServiceability);
54 MemoryPool* MemoryService::_metaspace_pool = NULL;
55 MemoryPool* MemoryService::_compressed_class_pool = NULL;
56
57 class GcThreadCountClosure: public ThreadClosure {
58 private:
59 int _count;
60 public:
GcThreadCountClosure()61 GcThreadCountClosure() : _count(0) {};
62 void do_thread(Thread* thread);
count()63 int count() { return _count; }
64 };
65
do_thread(Thread * thread)66 void GcThreadCountClosure::do_thread(Thread* thread) {
67 _count++;
68 }
69
set_universe_heap(CollectedHeap * heap)70 void MemoryService::set_universe_heap(CollectedHeap* heap) {
71 ResourceMark rm; // For internal allocations in GrowableArray.
72
73 GrowableArray<MemoryPool*> gc_mem_pools = heap->memory_pools();
74 _pools_list->appendAll(&gc_mem_pools);
75
76 // set the GC thread count
77 GcThreadCountClosure gctcc;
78 heap->gc_threads_do(&gctcc);
79 int count = gctcc.count();
80
81 GrowableArray<GCMemoryManager*> gc_memory_managers = heap->memory_managers();
82 for (int i = 0; i < gc_memory_managers.length(); i++) {
83 GCMemoryManager* gc_manager = gc_memory_managers.at(i);
84
85 if (count > 0) {
86 gc_manager->set_num_gc_threads(count);
87 }
88 gc_manager->initialize_gc_stat_info();
89 _managers_list->append(gc_manager);
90 }
91 }
92
add_code_heap_memory_pool(CodeHeap * heap,const char * name)93 void MemoryService::add_code_heap_memory_pool(CodeHeap* heap, const char* name) {
94 // Create new memory pool for this heap
95 MemoryPool* code_heap_pool = new CodeHeapPool(heap, name, true /* support_usage_threshold */);
96
97 // Append to lists
98 _code_heap_pools->append(code_heap_pool);
99 _pools_list->append(code_heap_pool);
100
101 if (_code_cache_manager == NULL) {
102 // Create CodeCache memory manager
103 _code_cache_manager = MemoryManager::get_code_cache_memory_manager();
104 _managers_list->append(_code_cache_manager);
105 }
106
107 _code_cache_manager->add_pool(code_heap_pool);
108 }
109
add_metaspace_memory_pools()110 void MemoryService::add_metaspace_memory_pools() {
111 MemoryManager* mgr = MemoryManager::get_metaspace_memory_manager();
112
113 _metaspace_pool = new MetaspacePool();
114 mgr->add_pool(_metaspace_pool);
115 _pools_list->append(_metaspace_pool);
116
117 if (UseCompressedClassPointers) {
118 _compressed_class_pool = new CompressedKlassSpacePool();
119 mgr->add_pool(_compressed_class_pool);
120 _pools_list->append(_compressed_class_pool);
121 }
122
123 _managers_list->append(mgr);
124 }
125
get_memory_manager(instanceHandle mh)126 MemoryManager* MemoryService::get_memory_manager(instanceHandle mh) {
127 for (int i = 0; i < _managers_list->length(); i++) {
128 MemoryManager* mgr = _managers_list->at(i);
129 if (mgr->is_manager(mh)) {
130 return mgr;
131 }
132 }
133 return NULL;
134 }
135
get_memory_pool(instanceHandle ph)136 MemoryPool* MemoryService::get_memory_pool(instanceHandle ph) {
137 for (int i = 0; i < _pools_list->length(); i++) {
138 MemoryPool* pool = _pools_list->at(i);
139 if (pool->is_pool(ph)) {
140 return pool;
141 }
142 }
143 return NULL;
144 }
145
track_memory_usage()146 void MemoryService::track_memory_usage() {
147 // Track the peak memory usage
148 for (int i = 0; i < _pools_list->length(); i++) {
149 MemoryPool* pool = _pools_list->at(i);
150 pool->record_peak_memory_usage();
151 }
152
153 // Detect low memory
154 LowMemoryDetector::detect_low_memory();
155 }
156
track_memory_pool_usage(MemoryPool * pool)157 void MemoryService::track_memory_pool_usage(MemoryPool* pool) {
158 // Track the peak memory usage
159 pool->record_peak_memory_usage();
160
161 // Detect low memory
162 if (LowMemoryDetector::is_enabled(pool)) {
163 LowMemoryDetector::detect_low_memory(pool);
164 }
165 }
166
gc_begin(GCMemoryManager * manager,bool recordGCBeginTime,bool recordAccumulatedGCTime,bool recordPreGCUsage,bool recordPeakUsage)167 void MemoryService::gc_begin(GCMemoryManager* manager, bool recordGCBeginTime,
168 bool recordAccumulatedGCTime,
169 bool recordPreGCUsage, bool recordPeakUsage) {
170
171 manager->gc_begin(recordGCBeginTime, recordPreGCUsage, recordAccumulatedGCTime);
172
173 // Track the peak memory usage when GC begins
174 if (recordPeakUsage) {
175 for (int i = 0; i < _pools_list->length(); i++) {
176 MemoryPool* pool = _pools_list->at(i);
177 pool->record_peak_memory_usage();
178 }
179 }
180 }
181
gc_end(GCMemoryManager * manager,bool recordPostGCUsage,bool recordAccumulatedGCTime,bool recordGCEndTime,bool countCollection,GCCause::Cause cause,bool allMemoryPoolsAffected)182 void MemoryService::gc_end(GCMemoryManager* manager, bool recordPostGCUsage,
183 bool recordAccumulatedGCTime,
184 bool recordGCEndTime, bool countCollection,
185 GCCause::Cause cause,
186 bool allMemoryPoolsAffected) {
187 // register the GC end statistics and memory usage
188 manager->gc_end(recordPostGCUsage, recordAccumulatedGCTime, recordGCEndTime,
189 countCollection, cause, allMemoryPoolsAffected);
190 }
191
set_verbose(bool verbose)192 bool MemoryService::set_verbose(bool verbose) {
193 MutexLocker m(Management_lock);
194 // verbose will be set to the previous value
195 if (verbose) {
196 LogConfiguration::configure_stdout(LogLevel::Info, true, LOG_TAGS(gc));
197 } else {
198 LogConfiguration::configure_stdout(LogLevel::Off, true, LOG_TAGS(gc));
199 }
200 ClassLoadingService::reset_trace_class_unloading();
201
202 return verbose;
203 }
204
create_MemoryUsage_obj(MemoryUsage usage,TRAPS)205 Handle MemoryService::create_MemoryUsage_obj(MemoryUsage usage, TRAPS) {
206 InstanceKlass* ik = Management::java_lang_management_MemoryUsage_klass(CHECK_NH);
207
208 JavaCallArguments args(10);
209 args.push_long(usage.init_size_as_jlong());
210 args.push_long(usage.used_as_jlong());
211 args.push_long(usage.committed_as_jlong());
212 args.push_long(usage.max_size_as_jlong());
213
214 return JavaCalls::construct_new_instance(
215 ik,
216 vmSymbols::long_long_long_long_void_signature(),
217 &args,
218 CHECK_NH);
219 }
220
TraceMemoryManagerStats(GCMemoryManager * gc_memory_manager,GCCause::Cause cause,bool allMemoryPoolsAffected,bool recordGCBeginTime,bool recordPreGCUsage,bool recordPeakUsage,bool recordPostGCUsage,bool recordAccumulatedGCTime,bool recordGCEndTime,bool countCollection)221 TraceMemoryManagerStats::TraceMemoryManagerStats(GCMemoryManager* gc_memory_manager,
222 GCCause::Cause cause,
223 bool allMemoryPoolsAffected,
224 bool recordGCBeginTime,
225 bool recordPreGCUsage,
226 bool recordPeakUsage,
227 bool recordPostGCUsage,
228 bool recordAccumulatedGCTime,
229 bool recordGCEndTime,
230 bool countCollection) {
231 initialize(gc_memory_manager, cause, allMemoryPoolsAffected,
232 recordGCBeginTime, recordPreGCUsage, recordPeakUsage,
233 recordPostGCUsage, recordAccumulatedGCTime, recordGCEndTime,
234 countCollection);
235 }
236
237 // for a subclass to create then initialize an instance before invoking
238 // the MemoryService
initialize(GCMemoryManager * gc_memory_manager,GCCause::Cause cause,bool allMemoryPoolsAffected,bool recordGCBeginTime,bool recordPreGCUsage,bool recordPeakUsage,bool recordPostGCUsage,bool recordAccumulatedGCTime,bool recordGCEndTime,bool countCollection)239 void TraceMemoryManagerStats::initialize(GCMemoryManager* gc_memory_manager,
240 GCCause::Cause cause,
241 bool allMemoryPoolsAffected,
242 bool recordGCBeginTime,
243 bool recordPreGCUsage,
244 bool recordPeakUsage,
245 bool recordPostGCUsage,
246 bool recordAccumulatedGCTime,
247 bool recordGCEndTime,
248 bool countCollection) {
249 _gc_memory_manager = gc_memory_manager;
250 _allMemoryPoolsAffected = allMemoryPoolsAffected;
251 _recordGCBeginTime = recordGCBeginTime;
252 _recordPreGCUsage = recordPreGCUsage;
253 _recordPeakUsage = recordPeakUsage;
254 _recordPostGCUsage = recordPostGCUsage;
255 _recordAccumulatedGCTime = recordAccumulatedGCTime;
256 _recordGCEndTime = recordGCEndTime;
257 _countCollection = countCollection;
258 _cause = cause;
259
260 MemoryService::gc_begin(_gc_memory_manager, _recordGCBeginTime, _recordAccumulatedGCTime,
261 _recordPreGCUsage, _recordPeakUsage);
262 }
263
~TraceMemoryManagerStats()264 TraceMemoryManagerStats::~TraceMemoryManagerStats() {
265 MemoryService::gc_end(_gc_memory_manager, _recordPostGCUsage, _recordAccumulatedGCTime,
266 _recordGCEndTime, _countCollection, _cause, _allMemoryPoolsAffected);
267 }
268