src/share/vm/services/memTracker.cpp

changeset 7074
833b0f92429a
parent 6911
ce8f6bb717c9
child 7077
36c9011aaead
equal deleted inserted replaced
7073:4d3a43351904 7074:833b0f92429a
1 /* 1 /*
2 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. 2 * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 * 4 *
5 * This code is free software; you can redistribute it and/or modify it 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 6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
21 * questions. 21 * questions.
22 * 22 *
23 */ 23 */
24 #include "precompiled.hpp" 24 #include "precompiled.hpp"
25 25
26 #include "oops/instanceKlass.hpp" 26 #include "runtime/mutex.hpp"
27 #include "runtime/atomic.hpp" 27 #include "services/memBaseline.hpp"
28 #include "runtime/interfaceSupport.hpp"
29 #include "runtime/mutexLocker.hpp"
30 #include "runtime/safepoint.hpp"
31 #include "runtime/threadCritical.hpp"
32 #include "runtime/thread.inline.hpp"
33 #include "runtime/vm_operations.hpp"
34 #include "services/memPtr.hpp"
35 #include "services/memReporter.hpp" 28 #include "services/memReporter.hpp"
29 #include "services/mallocTracker.inline.hpp"
36 #include "services/memTracker.hpp" 30 #include "services/memTracker.hpp"
37 #include "utilities/decoder.hpp"
38 #include "utilities/defaultStream.hpp" 31 #include "utilities/defaultStream.hpp"
39 #include "utilities/globalDefinitions.hpp" 32
40 33 #ifdef SOLARIS
41 bool NMT_track_callsite = false; 34 volatile bool NMT_stack_walkable = false;
42 35 #else
43 // walk all 'known' threads at NMT sync point, and collect their recorders 36 volatile bool NMT_stack_walkable = true;
44 void SyncThreadRecorderClosure::do_thread(Thread* thread) { 37 #endif
45 assert(SafepointSynchronize::is_at_safepoint(), "Safepoint required"); 38
46 if (thread->is_Java_thread()) { 39 volatile NMT_TrackingLevel MemTracker::_tracking_level = NMT_unknown;
47 JavaThread* javaThread = (JavaThread*)thread; 40 NMT_TrackingLevel MemTracker::_cmdline_tracking_level = NMT_unknown;
48 MemRecorder* recorder = javaThread->get_recorder(); 41
49 if (recorder != NULL) { 42 NativeCallStack emptyStack(0, false);
50 MemTracker::enqueue_pending_recorder(recorder); 43
51 javaThread->set_recorder(NULL); 44 MemBaseline MemTracker::_baseline;
52 } 45 Mutex* MemTracker::_query_lock = NULL;
53 } 46 bool MemTracker::_is_nmt_env_valid = true;
54 _thread_count ++; 47
55 } 48
56 49 NMT_TrackingLevel MemTracker::init_tracking_level() {
57 50 NMT_TrackingLevel level = NMT_off;
58 MemRecorder* volatile MemTracker::_global_recorder = NULL; 51 char buf[64];
59 MemSnapshot* MemTracker::_snapshot = NULL; 52 char nmt_option[64];
60 MemBaseline MemTracker::_baseline; 53 jio_snprintf(buf, sizeof(buf), "NMT_LEVEL_%d", os::current_process_id());
61 Mutex* MemTracker::_query_lock = NULL; 54 if (os::getenv(buf, nmt_option, sizeof(nmt_option))) {
62 MemRecorder* volatile MemTracker::_merge_pending_queue = NULL; 55 if (strcmp(nmt_option, "summary") == 0) {
63 MemRecorder* volatile MemTracker::_pooled_recorders = NULL; 56 level = NMT_summary;
64 MemTrackWorker* MemTracker::_worker_thread = NULL; 57 } else if (strcmp(nmt_option, "detail") == 0) {
65 int MemTracker::_sync_point_skip_count = 0;
66 MemTracker::NMTLevel MemTracker::_tracking_level = MemTracker::NMT_off;
67 volatile MemTracker::NMTStates MemTracker::_state = NMT_uninited;
68 MemTracker::ShutdownReason MemTracker::_reason = NMT_shutdown_none;
69 int MemTracker::_thread_count = 255;
70 volatile jint MemTracker::_pooled_recorder_count = 0;
71 volatile unsigned long MemTracker::_processing_generation = 0;
72 volatile bool MemTracker::_worker_thread_idle = false;
73 volatile jint MemTracker::_pending_op_count = 0;
74 volatile bool MemTracker::_slowdown_calling_thread = false;
75 debug_only(intx MemTracker::_main_thread_tid = 0;)
76 NOT_PRODUCT(volatile jint MemTracker::_pending_recorder_count = 0;)
77
78 void MemTracker::init_tracking_options(const char* option_line) {
79 _tracking_level = NMT_off;
80 if (strcmp(option_line, "=summary") == 0) {
81 _tracking_level = NMT_summary;
82 } else if (strcmp(option_line, "=detail") == 0) {
83 // detail relies on a stack-walking ability that may not
84 // be available depending on platform and/or compiler flags
85 #if PLATFORM_NATIVE_STACK_WALKING_SUPPORTED 58 #if PLATFORM_NATIVE_STACK_WALKING_SUPPORTED
86 _tracking_level = NMT_detail; 59 level = NMT_detail;
87 #else 60 #else
61 level = NMT_summary;
62 #endif // PLATFORM_NATIVE_STACK_WALKING_SUPPORTED
63 } else if (strcmp(nmt_option, "off") != 0) {
64 // The option value is invalid
65 _is_nmt_env_valid = false;
66 }
67
68 // Remove the environment variable to avoid leaking to child processes
69 os::unsetenv(buf);
70 }
71
72 if (!MallocTracker::initialize(level) ||
73 !VirtualMemoryTracker::initialize(level)) {
74 level = NMT_off;
75 }
76 return level;
77 }
78
79 void MemTracker::init() {
80 if (tracking_level() >= NMT_summary) {
81 _query_lock = new (std::nothrow) Mutex(Monitor::max_nonleaf, "NMT_queryLock");
82 // Already OOM. It is unlikely, but still have to handle it.
83 if (_query_lock == NULL) {
84 shutdown();
85 }
86 }
87 }
88
89 bool MemTracker::check_launcher_nmt_support(const char* value) {
90 if (strcmp(value, "=detail") == 0) {
91 #if !PLATFORM_NATIVE_STACK_WALKING_SUPPORTED
88 jio_fprintf(defaultStream::error_stream(), 92 jio_fprintf(defaultStream::error_stream(),
89 "NMT detail is not supported on this platform. Using NMT summary instead.\n"); 93 "NMT detail is not supported on this platform. Using NMT summary instead.\n");
90 _tracking_level = NMT_summary; 94 if (MemTracker::tracking_level() != NMT_summary) {
95 return false;
96 }
97 #else
98 if (MemTracker::tracking_level() != NMT_detail) {
99 return false;
100 }
91 #endif 101 #endif
92 } else if (strcmp(option_line, "=off") != 0) { 102 } else if (strcmp(value, "=summary") == 0) {
93 vm_exit_during_initialization("Syntax error, expecting -XX:NativeMemoryTracking=[off|summary|detail]", NULL); 103 if (MemTracker::tracking_level() != NMT_summary) {
94 } 104 return false;
95 } 105 }
96 106 } else if (strcmp(value, "=off") == 0) {
97 // first phase of bootstrapping, when VM is still in single-threaded mode. 107 if (MemTracker::tracking_level() != NMT_off) {
98 void MemTracker::bootstrap_single_thread() { 108 return false;
99 if (_tracking_level > NMT_off) { 109 }
100 assert(_state == NMT_uninited, "wrong state");
101
102 // NMT is not supported with UseMallocOnly is on. NMT can NOT
103 // handle the amount of malloc data without significantly impacting
104 // runtime performance when this flag is on.
105 if (UseMallocOnly) {
106 shutdown(NMT_use_malloc_only);
107 return;
108 }
109
110 _query_lock = new (std::nothrow) Mutex(Monitor::max_nonleaf, "NMT_queryLock");
111 if (_query_lock == NULL) {
112 shutdown(NMT_out_of_memory);
113 return;
114 }
115
116 debug_only(_main_thread_tid = os::current_thread_id();)
117 _state = NMT_bootstrapping_single_thread;
118 NMT_track_callsite = (_tracking_level == NMT_detail && can_walk_stack());
119 }
120 }
121
122 // second phase of bootstrapping, when VM is about to or already entered multi-theaded mode.
123 void MemTracker::bootstrap_multi_thread() {
124 if (_tracking_level > NMT_off && _state == NMT_bootstrapping_single_thread) {
125 // create nmt lock for multi-thread execution
126 assert(_main_thread_tid == os::current_thread_id(), "wrong thread");
127 _state = NMT_bootstrapping_multi_thread;
128 NMT_track_callsite = (_tracking_level == NMT_detail && can_walk_stack());
129 }
130 }
131
132 // fully start nmt
133 void MemTracker::start() {
134 // Native memory tracking is off from command line option
135 if (_tracking_level == NMT_off || shutdown_in_progress()) return;
136
137 assert(_main_thread_tid == os::current_thread_id(), "wrong thread");
138 assert(_state == NMT_bootstrapping_multi_thread, "wrong state");
139
140 _snapshot = new (std::nothrow)MemSnapshot();
141 if (_snapshot != NULL) {
142 if (!_snapshot->out_of_memory() && start_worker(_snapshot)) {
143 _state = NMT_started;
144 NMT_track_callsite = (_tracking_level == NMT_detail && can_walk_stack());
145 return;
146 }
147
148 delete _snapshot;
149 _snapshot = NULL;
150 }
151
152 // fail to start native memory tracking, shut it down
153 shutdown(NMT_initialization);
154 }
155
156 /**
157 * Shutting down native memory tracking.
158 * We can not shutdown native memory tracking immediately, so we just
159 * setup shutdown pending flag, every native memory tracking component
160 * should orderly shut itself down.
161 *
162 * The shutdown sequences:
163 * 1. MemTracker::shutdown() sets MemTracker to shutdown pending state
164 * 2. Worker thread calls MemTracker::final_shutdown(), which transites
165 * MemTracker to final shutdown state.
166 * 3. At sync point, MemTracker does final cleanup, before sets memory
167 * tracking level to off to complete shutdown.
168 */
169 void MemTracker::shutdown(ShutdownReason reason) {
170 if (_tracking_level == NMT_off) return;
171
172 if (_state <= NMT_bootstrapping_single_thread) {
173 // we still in single thread mode, there is not contention
174 _state = NMT_shutdown_pending;
175 _reason = reason;
176 } else { 110 } else {
177 // we want to know who initialized shutdown 111 _is_nmt_env_valid = false;
178 if ((jint)NMT_started == Atomic::cmpxchg((jint)NMT_shutdown_pending, 112 }
179 (jint*)&_state, (jint)NMT_started)) { 113
180 _reason = reason; 114 return true;
181 } 115 }
182 } 116
183 } 117 bool MemTracker::verify_nmt_option() {
184 118 return _is_nmt_env_valid;
185 // final phase of shutdown 119 }
186 void MemTracker::final_shutdown() { 120
187 // delete all pending recorders and pooled recorders 121 void* MemTracker::malloc_base(void* memblock) {
188 delete_all_pending_recorders(); 122 return MallocTracker::get_base(memblock);
189 delete_all_pooled_recorders(); 123 }
190 124
191 { 125 void Tracker::record(address addr, size_t size) {
192 // shared baseline and snapshot are the only objects needed to 126 if (MemTracker::tracking_level() < NMT_summary) return;
193 // create query results 127 switch(_type) {
194 MutexLockerEx locker(_query_lock, true); 128 case uncommit:
195 // cleanup baseline data and snapshot 129 VirtualMemoryTracker::remove_uncommitted_region(addr, size);
196 _baseline.clear(); 130 break;
197 delete _snapshot; 131 case release:
198 _snapshot = NULL; 132 VirtualMemoryTracker::remove_released_region(addr, size);
199 } 133 break;
200 134 default:
201 // shutdown shared decoder instance, since it is only 135 ShouldNotReachHere();
202 // used by native memory tracking so far. 136 }
203 Decoder::shutdown(); 137 }
204 138
205 MemTrackWorker* worker = NULL; 139
206 { 140 // Shutdown can only be issued via JCmd, and NMT JCmd is serialized
207 ThreadCritical tc; 141 // by lock
208 // can not delete worker inside the thread critical 142 void MemTracker::shutdown() {
209 if (_worker_thread != NULL && Thread::current() == _worker_thread) { 143 // We can only shutdown NMT to minimal tracking level if it is
210 worker = _worker_thread; 144 // ever on.
211 _worker_thread = NULL; 145 if (tracking_level () > NMT_minimal) {
212 } 146 transition_to(NMT_minimal);
213 } 147 }
214 if (worker != NULL) { 148 }
215 delete worker; 149
216 } 150 bool MemTracker::transition_to(NMT_TrackingLevel level) {
217 _state = NMT_final_shutdown; 151 NMT_TrackingLevel current_level = tracking_level();
218 } 152
219 153 if (current_level == level) {
220 // delete all pooled recorders 154 return true;
221 void MemTracker::delete_all_pooled_recorders() { 155 } else if (current_level > level) {
222 // free all pooled recorders 156 // Downgrade tracking level, we want to lower the tracking
223 MemRecorder* volatile cur_head = _pooled_recorders; 157 // level first
224 if (cur_head != NULL) { 158 _tracking_level = level;
225 MemRecorder* null_ptr = NULL; 159 // Make _tracking_level visible immediately.
226 while (cur_head != NULL && (void*)cur_head != Atomic::cmpxchg_ptr((void*)null_ptr, 160 OrderAccess::fence();
227 (void*)&_pooled_recorders, (void*)cur_head)) { 161 VirtualMemoryTracker::transition(current_level, level);
228 cur_head = _pooled_recorders; 162 MallocTracker::transition(current_level, level);
229 } 163
230 if (cur_head != NULL) { 164 if (level == NMT_minimal) _baseline.reset();
231 delete cur_head;
232 _pooled_recorder_count = 0;
233 }
234 }
235 }
236
237 // delete all recorders in pending queue
238 void MemTracker::delete_all_pending_recorders() {
239 // free all pending recorders
240 MemRecorder* pending_head = get_pending_recorders();
241 if (pending_head != NULL) {
242 delete pending_head;
243 }
244 }
245
246 /*
247 * retrieve per-thread recorder of specified thread.
248 * if thread == NULL, it means global recorder
249 */
250 MemRecorder* MemTracker::get_thread_recorder(JavaThread* thread) {
251 if (shutdown_in_progress()) return NULL;
252
253 MemRecorder* rc;
254 if (thread == NULL) {
255 rc = _global_recorder;
256 } else { 165 } else {
257 rc = thread->get_recorder(); 166 VirtualMemoryTracker::transition(current_level, level);
258 } 167 MallocTracker::transition(current_level, level);
259 168
260 if (rc != NULL && rc->is_full()) { 169 _tracking_level = level;
261 enqueue_pending_recorder(rc); 170 // Make _tracking_level visible immediately.
262 rc = NULL; 171 OrderAccess::fence();
263 } 172 }
264 173
265 if (rc == NULL) { 174 return true;
266 rc = get_new_or_pooled_instance(); 175 }
267 if (thread == NULL) { 176
268 _global_recorder = rc; 177 void MemTracker::final_report(outputStream* output) {
178 assert(output != NULL, "No output stream");
179 if (tracking_level() >= NMT_summary) {
180 MallocMemorySnapshot* malloc_memory_snapshot =
181 MallocMemorySummary::as_snapshot();
182 malloc_memory_snapshot->make_adjustment();
183
184 VirtualMemorySnapshot* virtual_memory_snapshot =
185 VirtualMemorySummary::as_snapshot();
186
187 MemSummaryReporter rptr(malloc_memory_snapshot,
188 virtual_memory_snapshot, output);
189 rptr.report();
190 // shutdown NMT, the data no longer accurate
191 shutdown();
192 }
193 }
194
195 // This is a walker to gather malloc site hashtable statistics,
196 // the result is used for tuning.
197 class StatisticsWalker : public MallocSiteWalker {
198 private:
199 enum Threshold {
200 // aggregates statistics over this threshold into one
201 // line item.
202 report_threshold = 20
203 };
204
205 private:
206 // Number of allocation sites that have all memory freed
207 int _empty_entries;
208 // Total number of allocation sites, include empty sites
209 int _total_entries;
210 // Number of captured call stack distribution
211 int _stack_depth_distribution[NMT_TrackingStackDepth];
212 // Hash distribution
213 int _hash_distribution[report_threshold];
214 // Number of hash buckets that have entries over the threshold
215 int _bucket_over_threshold;
216
217 // The hash bucket that walker is currently walking
218 int _current_hash_bucket;
219 // The length of current hash bucket
220 int _current_bucket_length;
221 // Number of hash buckets that are not empty
222 int _used_buckets;
223 // Longest hash bucket length
224 int _longest_bucket_length;
225
226 public:
227 StatisticsWalker() : _empty_entries(0), _total_entries(0) {
228 int index = 0;
229 for (index = 0; index < NMT_TrackingStackDepth; index ++) {
230 _stack_depth_distribution[index] = 0;
231 }
232 for (index = 0; index < report_threshold; index ++) {
233 _hash_distribution[index] = 0;
234 }
235 _bucket_over_threshold = 0;
236 _longest_bucket_length = 0;
237 _current_hash_bucket = -1;
238 _current_bucket_length = 0;
239 _used_buckets = 0;
240 }
241
242 virtual bool at(const MallocSite* e) {
243 if (e->size() == 0) _empty_entries ++;
244 _total_entries ++;
245
246 // stack depth distrubution
247 int frames = e->call_stack()->frames();
248 _stack_depth_distribution[frames - 1] ++;
249
250 // hash distribution
251 int hash_bucket = e->hash() % MallocSiteTable::hash_buckets();
252 if (_current_hash_bucket == -1) {
253 _current_hash_bucket = hash_bucket;
254 _current_bucket_length = 1;
255 } else if (_current_hash_bucket == hash_bucket) {
256 _current_bucket_length ++;
269 } else { 257 } else {
270 thread->set_recorder(rc); 258 record_bucket_length(_current_bucket_length);
271 } 259 _current_hash_bucket = hash_bucket;
272 } 260 _current_bucket_length = 1;
273 return rc; 261 }
274 } 262 return true;
275 263 }
276 /* 264
277 * get a per-thread recorder from pool, or create a new one if 265 // walk completed
278 * there is not one available. 266 void completed() {
279 */ 267 record_bucket_length(_current_bucket_length);
280 MemRecorder* MemTracker::get_new_or_pooled_instance() { 268 }
281 MemRecorder* cur_head = const_cast<MemRecorder*> (_pooled_recorders); 269
282 if (cur_head == NULL) { 270 void report_statistics(outputStream* out) {
283 MemRecorder* rec = new (std::nothrow)MemRecorder(); 271 int index;
284 if (rec == NULL || rec->out_of_memory()) { 272 out->print_cr("Malloc allocation site table:");
285 shutdown(NMT_out_of_memory); 273 out->print_cr("\tTotal entries: %d", _total_entries);
286 if (rec != NULL) { 274 out->print_cr("\tEmpty entries: %d (%2.2f%%)", _empty_entries, ((float)_empty_entries * 100) / _total_entries);
287 delete rec; 275 out->print_cr(" ");
288 rec = NULL; 276 out->print_cr("Hash distribution:");
289 } 277 if (_used_buckets < MallocSiteTable::hash_buckets()) {
290 } 278 out->print_cr("empty bucket: %d", (MallocSiteTable::hash_buckets() - _used_buckets));
291 return rec; 279 }
292 } else { 280 for (index = 0; index < report_threshold; index ++) {
293 MemRecorder* next_head = cur_head->next(); 281 if (_hash_distribution[index] != 0) {
294 if ((void*)cur_head != Atomic::cmpxchg_ptr((void*)next_head, (void*)&_pooled_recorders, 282 if (index == 0) {
295 (void*)cur_head)) { 283 out->print_cr(" %d entry: %d", 1, _hash_distribution[0]);
296 return get_new_or_pooled_instance(); 284 } else if (index < 9) { // single digit
297 } 285 out->print_cr(" %d entries: %d", (index + 1), _hash_distribution[index]);
298 cur_head->set_next(NULL); 286 } else {
299 Atomic::dec(&_pooled_recorder_count); 287 out->print_cr(" %d entries: %d", (index + 1), _hash_distribution[index]);
300 cur_head->set_generation(); 288 }
301 return cur_head;
302 }
303 }
304
305 /*
306 * retrieve all recorders in pending queue, and empty the queue
307 */
308 MemRecorder* MemTracker::get_pending_recorders() {
309 MemRecorder* cur_head = const_cast<MemRecorder*>(_merge_pending_queue);
310 MemRecorder* null_ptr = NULL;
311 while ((void*)cur_head != Atomic::cmpxchg_ptr((void*)null_ptr, (void*)&_merge_pending_queue,
312 (void*)cur_head)) {
313 cur_head = const_cast<MemRecorder*>(_merge_pending_queue);
314 }
315 NOT_PRODUCT(Atomic::store(0, &_pending_recorder_count));
316 return cur_head;
317 }
318
319 /*
320 * release a recorder to recorder pool.
321 */
322 void MemTracker::release_thread_recorder(MemRecorder* rec) {
323 assert(rec != NULL, "null recorder");
324 // we don't want to pool too many recorders
325 rec->set_next(NULL);
326 if (shutdown_in_progress() || _pooled_recorder_count > _thread_count * 2) {
327 delete rec;
328 return;
329 }
330
331 rec->clear();
332 MemRecorder* cur_head = const_cast<MemRecorder*>(_pooled_recorders);
333 rec->set_next(cur_head);
334 while ((void*)cur_head != Atomic::cmpxchg_ptr((void*)rec, (void*)&_pooled_recorders,
335 (void*)cur_head)) {
336 cur_head = const_cast<MemRecorder*>(_pooled_recorders);
337 rec->set_next(cur_head);
338 }
339 Atomic::inc(&_pooled_recorder_count);
340 }
341
342 // write a record to proper recorder. No lock can be taken from this method
343 // down.
344 void MemTracker::write_tracking_record(address addr, MEMFLAGS flags,
345 size_t size, jint seq, address pc, JavaThread* thread) {
346
347 MemRecorder* rc = get_thread_recorder(thread);
348 if (rc != NULL) {
349 rc->record(addr, flags, size, seq, pc);
350 }
351 }
352
353 /**
354 * enqueue a recorder to pending queue
355 */
356 void MemTracker::enqueue_pending_recorder(MemRecorder* rec) {
357 assert(rec != NULL, "null recorder");
358
359 // we are shutting down, so just delete it
360 if (shutdown_in_progress()) {
361 rec->set_next(NULL);
362 delete rec;
363 return;
364 }
365
366 MemRecorder* cur_head = const_cast<MemRecorder*>(_merge_pending_queue);
367 rec->set_next(cur_head);
368 while ((void*)cur_head != Atomic::cmpxchg_ptr((void*)rec, (void*)&_merge_pending_queue,
369 (void*)cur_head)) {
370 cur_head = const_cast<MemRecorder*>(_merge_pending_queue);
371 rec->set_next(cur_head);
372 }
373 NOT_PRODUCT(Atomic::inc(&_pending_recorder_count);)
374 }
375
376 /*
377 * The method is called at global safepoint
378 * during it synchronization process.
379 * 1. enqueue all JavaThreads' per-thread recorders
380 * 2. enqueue global recorder
381 * 3. retrieve all pending recorders
382 * 4. reset global sequence number generator
383 * 5. call worker's sync
384 */
385 #define MAX_SAFEPOINTS_TO_SKIP 128
386 #define SAFE_SEQUENCE_THRESHOLD 30
387 #define HIGH_GENERATION_THRESHOLD 60
388 #define MAX_RECORDER_THREAD_RATIO 30
389 #define MAX_RECORDER_PER_THREAD 100
390
391 void MemTracker::sync() {
392 assert(_tracking_level > NMT_off, "NMT is not enabled");
393 assert(SafepointSynchronize::is_at_safepoint(), "Safepoint required");
394
395 // Some GC tests hit large number of safepoints in short period of time
396 // without meaningful activities. We should prevent going to
397 // sync point in these cases, which can potentially exhaust generation buffer.
398 // Here is the factots to determine if we should go into sync point:
399 // 1. not to overflow sequence number
400 // 2. if we are in danger to overflow generation buffer
401 // 3. how many safepoints we already skipped sync point
402 if (_state == NMT_started) {
403 // worker thread is not ready, no one can manage generation
404 // buffer, so skip this safepoint
405 if (_worker_thread == NULL) return;
406
407 if (_sync_point_skip_count < MAX_SAFEPOINTS_TO_SKIP) {
408 int per_seq_in_use = SequenceGenerator::peek() * 100 / max_jint;
409 int per_gen_in_use = _worker_thread->generations_in_use() * 100 / MAX_GENERATIONS;
410 if (per_seq_in_use < SAFE_SEQUENCE_THRESHOLD && per_gen_in_use >= HIGH_GENERATION_THRESHOLD) {
411 _sync_point_skip_count ++;
412 return;
413 } 289 }
414 } 290 }
415 { 291 if (_bucket_over_threshold > 0) {
416 // This method is running at safepoint, with ThreadCritical lock, 292 out->print_cr(" >%d entries: %d", report_threshold, _bucket_over_threshold);
417 // it should guarantee that NMT is fully sync-ed. 293 }
418 ThreadCritical tc; 294 out->print_cr("most entries: %d", _longest_bucket_length);
419 295 out->print_cr(" ");
420 // We can NOT execute NMT sync-point if there are pending tracking ops. 296 out->print_cr("Call stack depth distribution:");
421 if (_pending_op_count == 0) { 297 for (index = 0; index < NMT_TrackingStackDepth; index ++) {
422 SequenceGenerator::reset(); 298 if (_stack_depth_distribution[index] > 0) {
423 _sync_point_skip_count = 0; 299 out->print_cr("\t%d: %d", index + 1, _stack_depth_distribution[index]);
424
425 // walk all JavaThreads to collect recorders
426 SyncThreadRecorderClosure stc;
427 Threads::threads_do(&stc);
428
429 _thread_count = stc.get_thread_count();
430 MemRecorder* pending_recorders = get_pending_recorders();
431
432 if (_global_recorder != NULL) {
433 _global_recorder->set_next(pending_recorders);
434 pending_recorders = _global_recorder;
435 _global_recorder = NULL;
436 }
437
438 // see if NMT has too many outstanding recorder instances, it usually
439 // means that worker thread is lagging behind in processing them.
440 if (!AutoShutdownNMT) {
441 _slowdown_calling_thread = (MemRecorder::_instance_count > MAX_RECORDER_THREAD_RATIO * _thread_count);
442 } else {
443 // If auto shutdown is on, enforce MAX_RECORDER_PER_THREAD threshold to prevent OOM
444 if (MemRecorder::_instance_count >= _thread_count * MAX_RECORDER_PER_THREAD) {
445 shutdown(NMT_out_of_memory);
446 }
447 }
448
449 // check _worker_thread with lock to avoid racing condition
450 if (_worker_thread != NULL) {
451 _worker_thread->at_sync_point(pending_recorders, InstanceKlass::number_of_instance_classes());
452 }
453 assert(SequenceGenerator::peek() == 1, "Should not have memory activities during sync-point");
454 } else {
455 _sync_point_skip_count ++;
456 } 300 }
457 } 301 }
458 } 302 }
459 303
460 // now, it is the time to shut whole things off 304 private:
461 if (_state == NMT_final_shutdown) { 305 void record_bucket_length(int length) {
462 // walk all JavaThreads to delete all recorders 306 _used_buckets ++;
463 SyncThreadRecorderClosure stc; 307 if (length <= report_threshold) {
464 Threads::threads_do(&stc); 308 _hash_distribution[length - 1] ++;
465 // delete global recorder
466 {
467 ThreadCritical tc;
468 if (_global_recorder != NULL) {
469 delete _global_recorder;
470 _global_recorder = NULL;
471 }
472 }
473 MemRecorder* pending_recorders = get_pending_recorders();
474 if (pending_recorders != NULL) {
475 delete pending_recorders;
476 }
477 // try at a later sync point to ensure MemRecorder instance drops to zero to
478 // completely shutdown NMT
479 if (MemRecorder::_instance_count == 0) {
480 _state = NMT_shutdown;
481 _tracking_level = NMT_off;
482 }
483 }
484 }
485
486 /*
487 * Start worker thread.
488 */
489 bool MemTracker::start_worker(MemSnapshot* snapshot) {
490 assert(_worker_thread == NULL && _snapshot != NULL, "Just Check");
491 _worker_thread = new (std::nothrow) MemTrackWorker(snapshot);
492 if (_worker_thread == NULL) {
493 return false;
494 } else if (_worker_thread->has_error()) {
495 delete _worker_thread;
496 _worker_thread = NULL;
497 return false;
498 }
499 _worker_thread->start();
500 return true;
501 }
502
503 /*
504 * We need to collect a JavaThread's per-thread recorder
505 * before it exits.
506 */
507 void MemTracker::thread_exiting(JavaThread* thread) {
508 if (is_on()) {
509 MemRecorder* rec = thread->get_recorder();
510 if (rec != NULL) {
511 enqueue_pending_recorder(rec);
512 thread->set_recorder(NULL);
513 }
514 }
515 }
516
517 // baseline current memory snapshot
518 bool MemTracker::baseline() {
519 MutexLocker lock(_query_lock);
520 MemSnapshot* snapshot = get_snapshot();
521 if (snapshot != NULL) {
522 return _baseline.baseline(*snapshot, false);
523 }
524 return false;
525 }
526
527 // print memory usage from current snapshot
528 bool MemTracker::print_memory_usage(BaselineOutputer& out, size_t unit, bool summary_only) {
529 MemBaseline baseline;
530 MutexLocker lock(_query_lock);
531 MemSnapshot* snapshot = get_snapshot();
532 if (snapshot != NULL && baseline.baseline(*snapshot, summary_only)) {
533 BaselineReporter reporter(out, unit);
534 reporter.report_baseline(baseline, summary_only);
535 return true;
536 }
537 return false;
538 }
539
540 // Whitebox API for blocking until the current generation of NMT data has been merged
541 bool MemTracker::wbtest_wait_for_data_merge() {
542 // NMT can't be shutdown while we're holding _query_lock
543 MutexLocker lock(_query_lock);
544 assert(_worker_thread != NULL, "Invalid query");
545 // the generation at query time, so NMT will spin till this generation is processed
546 unsigned long generation_at_query_time = SequenceGenerator::current_generation();
547 unsigned long current_processing_generation = _processing_generation;
548 // if generation counter overflown
549 bool generation_overflown = (generation_at_query_time < current_processing_generation);
550 long generations_to_wrap = MAX_UNSIGNED_LONG - current_processing_generation;
551 // spin
552 while (!shutdown_in_progress()) {
553 if (!generation_overflown) {
554 if (current_processing_generation > generation_at_query_time) {
555 return true;
556 }
557 } else { 309 } else {
558 assert(generations_to_wrap >= 0, "Sanity check"); 310 _bucket_over_threshold ++;
559 long current_generations_to_wrap = MAX_UNSIGNED_LONG - current_processing_generation; 311 }
560 assert(current_generations_to_wrap >= 0, "Sanity check"); 312 _longest_bucket_length = MAX2(_longest_bucket_length, length);
561 // to overflow an unsigned long should take long time, so to_wrap check should be sufficient 313 }
562 if (current_generations_to_wrap > generations_to_wrap && 314 };
563 current_processing_generation > generation_at_query_time) { 315
564 return true; 316
565 } 317 void MemTracker::tuning_statistics(outputStream* out) {
566 } 318 // NMT statistics
567 319 StatisticsWalker walker;
568 // if worker thread is idle, but generation is not advancing, that means 320 MallocSiteTable::walk_malloc_site(&walker);
569 // there is not safepoint to let NMT advance generation, force one. 321 walker.completed();
570 if (_worker_thread_idle) { 322
571 VM_ForceSafepoint vfs; 323 out->print_cr("Native Memory Tracking Statistics:");
572 VMThread::execute(&vfs); 324 out->print_cr("Malloc allocation site table size: %d", MallocSiteTable::hash_buckets());
573 } 325 out->print_cr(" Tracking stack depth: %d", NMT_TrackingStackDepth);
574 MemSnapshot* snapshot = get_snapshot(); 326 NOT_PRODUCT(out->print_cr("Peak concurrent access: %d", MallocSiteTable::access_peak_count());)
575 if (snapshot == NULL) { 327 out->print_cr(" ");
576 return false; 328 walker.report_statistics(out);
577 } 329 }
578 snapshot->wait(1000); 330
579 current_processing_generation = _processing_generation;
580 }
581 // We end up here if NMT is shutting down before our data has been merged
582 return false;
583 }
584
585 // compare memory usage between current snapshot and baseline
586 bool MemTracker::compare_memory_usage(BaselineOutputer& out, size_t unit, bool summary_only) {
587 MutexLocker lock(_query_lock);
588 if (_baseline.baselined()) {
589 MemBaseline baseline;
590 MemSnapshot* snapshot = get_snapshot();
591 if (snapshot != NULL && baseline.baseline(*snapshot, summary_only)) {
592 BaselineReporter reporter(out, unit);
593 reporter.diff_baselines(baseline, _baseline, summary_only);
594 return true;
595 }
596 }
597 return false;
598 }
599
600 #ifndef PRODUCT
601 void MemTracker::walk_stack(int toSkip, char* buf, int len) {
602 int cur_len = 0;
603 char tmp[1024];
604 address pc;
605
606 while (cur_len < len) {
607 pc = os::get_caller_pc(toSkip + 1);
608 if (pc != NULL && os::dll_address_to_function_name(pc, tmp, sizeof(tmp), NULL)) {
609 jio_snprintf(&buf[cur_len], (len - cur_len), "%s\n", tmp);
610 cur_len = (int)strlen(buf);
611 } else {
612 buf[cur_len] = '\0';
613 break;
614 }
615 toSkip ++;
616 }
617 }
618
619 void MemTracker::print_tracker_stats(outputStream* st) {
620 st->print_cr("\nMemory Tracker Stats:");
621 st->print_cr("\tMax sequence number = %d", SequenceGenerator::max_seq_num());
622 st->print_cr("\tthead count = %d", _thread_count);
623 st->print_cr("\tArena instance = %d", Arena::_instance_count);
624 st->print_cr("\tpooled recorder count = %d", _pooled_recorder_count);
625 st->print_cr("\tqueued recorder count = %d", _pending_recorder_count);
626 st->print_cr("\tmemory recorder instance count = %d", MemRecorder::_instance_count);
627 if (_worker_thread != NULL) {
628 st->print_cr("\tWorker thread:");
629 st->print_cr("\t\tSync point count = %d", _worker_thread->_sync_point_count);
630 st->print_cr("\t\tpending recorder count = %d", _worker_thread->count_pending_recorders());
631 st->print_cr("\t\tmerge count = %d", _worker_thread->_merge_count);
632 } else {
633 st->print_cr("\tWorker thread is not started");
634 }
635 st->print_cr(" ");
636
637 if (_snapshot != NULL) {
638 _snapshot->print_snapshot_stats(st);
639 } else {
640 st->print_cr("No snapshot");
641 }
642 }
643 #endif
644
645
646 // Tracker Implementation
647
648 /*
649 * Create a tracker.
650 * This is a fairly complicated constructor, as it has to make two important decisions:
651 * 1) Does it need to take ThreadCritical lock to write tracking record
652 * 2) Does it need to pre-reserve a sequence number for the tracking record
653 *
654 * The rules to determine if ThreadCritical is needed:
655 * 1. When nmt is in single-threaded bootstrapping mode, no lock is needed as VM
656 * still in single thread mode.
657 * 2. For all threads other than JavaThread, ThreadCritical is needed
658 * to write to recorders to global recorder.
659 * 3. For JavaThreads that are no longer visible by safepoint, also
660 * need to take ThreadCritical and records are written to global
661 * recorders, since these threads are NOT walked by Threads.do_thread().
662 * 4. JavaThreads that are running in safepoint-safe states do not stop
663 * for safepoints, ThreadCritical lock should be taken to write
664 * memory records.
665 * 5. JavaThreads that are running in VM state do not need any lock and
666 * records are written to per-thread recorders.
667 * 6. For a thread has yet to attach VM 'Thread', they need to take
668 * ThreadCritical to write to global recorder.
669 *
670 * The memory operations that need pre-reserve sequence numbers:
671 * The memory operations that "release" memory blocks and the
672 * operations can fail, need to pre-reserve sequence number. They
673 * are realloc, uncommit and release.
674 *
675 * The reason for pre-reserve sequence number, is to prevent race condition:
676 * Thread 1 Thread 2
677 * <release>
678 * <allocate>
679 * <write allocate record>
680 * <write release record>
681 * if Thread 2 happens to obtain the memory address Thread 1 just released,
682 * then NMT can mistakenly report the memory is free.
683 *
684 * Noticeably, free() does not need pre-reserve sequence number, because the call
685 * does not fail, so we can alway write "release" record before the memory is actaully
686 * freed.
687 *
688 * For realloc, uncommit and release, following coding pattern should be used:
689 *
690 * MemTracker::Tracker tkr = MemTracker::get_realloc_tracker();
691 * ptr = ::realloc(...);
692 * if (ptr == NULL) {
693 * tkr.record(...)
694 * } else {
695 * tkr.discard();
696 * }
697 *
698 * MemTracker::Tracker tkr = MemTracker::get_virtual_memory_uncommit_tracker();
699 * if (uncommit(...)) {
700 * tkr.record(...);
701 * } else {
702 * tkr.discard();
703 * }
704 *
705 * MemTracker::Tracker tkr = MemTracker::get_virtual_memory_release_tracker();
706 * if (release(...)) {
707 * tkr.record(...);
708 * } else {
709 * tkr.discard();
710 * }
711 *
712 * Since pre-reserved sequence number is only good for the generation that it is acquired,
713 * when there is pending Tracker that reserved sequence number, NMT sync-point has
714 * to be skipped to prevent from advancing generation. This is done by inc and dec
715 * MemTracker::_pending_op_count, when MemTracker::_pending_op_count > 0, NMT sync-point is skipped.
716 * Not all pre-reservation of sequence number will increment pending op count. For JavaThreads
717 * that honor safepoints, safepoint can not occur during the memory operations, so the
718 * pre-reserved sequence number won't cross the generation boundry.
719 */
720 MemTracker::Tracker::Tracker(MemoryOperation op, Thread* thr) {
721 _op = NoOp;
722 _seq = 0;
723 if (MemTracker::is_on()) {
724 _java_thread = NULL;
725 _op = op;
726
727 // figure out if ThreadCritical lock is needed to write this operation
728 // to MemTracker
729 if (MemTracker::is_single_threaded_bootstrap()) {
730 thr = NULL;
731 } else if (thr == NULL) {
732 // don't use Thread::current(), since it is possible that
733 // the calling thread has yet to attach to VM 'Thread',
734 // which will result assertion failure
735 thr = ThreadLocalStorage::thread();
736 }
737
738 if (thr != NULL) {
739 // Check NMT load
740 MemTracker::check_NMT_load(thr);
741
742 if (thr->is_Java_thread() && ((JavaThread*)thr)->is_safepoint_visible()) {
743 _java_thread = (JavaThread*)thr;
744 JavaThreadState state = _java_thread->thread_state();
745 // JavaThreads that are safepoint safe, can run through safepoint,
746 // so ThreadCritical is needed to ensure no threads at safepoint create
747 // new records while the records are being gathered and the sequence number is changing
748 _need_thread_critical_lock =
749 SafepointSynchronize::safepoint_safe(_java_thread, state);
750 } else {
751 _need_thread_critical_lock = true;
752 }
753 } else {
754 _need_thread_critical_lock
755 = !MemTracker::is_single_threaded_bootstrap();
756 }
757
758 // see if we need to pre-reserve sequence number for this operation
759 if (_op == Realloc || _op == Uncommit || _op == Release) {
760 if (_need_thread_critical_lock) {
761 ThreadCritical tc;
762 MemTracker::inc_pending_op_count();
763 _seq = SequenceGenerator::next();
764 } else {
765 // for the threads that honor safepoints, no safepoint can occur
766 // during the lifespan of tracker, so we don't need to increase
767 // pending op count.
768 _seq = SequenceGenerator::next();
769 }
770 }
771 }
772 }
773
774 void MemTracker::Tracker::discard() {
775 if (MemTracker::is_on() && _seq != 0) {
776 if (_need_thread_critical_lock) {
777 ThreadCritical tc;
778 MemTracker::dec_pending_op_count();
779 }
780 _seq = 0;
781 }
782 }
783
784
785 void MemTracker::Tracker::record(address old_addr, address new_addr, size_t size,
786 MEMFLAGS flags, address pc) {
787 assert(old_addr != NULL && new_addr != NULL, "Sanity check");
788 assert(_op == Realloc || _op == NoOp, "Wrong call");
789 if (MemTracker::is_on() && NMT_CAN_TRACK(flags) && _op != NoOp) {
790 assert(_seq > 0, "Need pre-reserve sequence number");
791 if (_need_thread_critical_lock) {
792 ThreadCritical tc;
793 // free old address, use pre-reserved sequence number
794 MemTracker::write_tracking_record(old_addr, MemPointerRecord::free_tag(),
795 0, _seq, pc, _java_thread);
796 MemTracker::write_tracking_record(new_addr, flags | MemPointerRecord::malloc_tag(),
797 size, SequenceGenerator::next(), pc, _java_thread);
798 // decrement MemTracker pending_op_count
799 MemTracker::dec_pending_op_count();
800 } else {
801 // free old address, use pre-reserved sequence number
802 MemTracker::write_tracking_record(old_addr, MemPointerRecord::free_tag(),
803 0, _seq, pc, _java_thread);
804 MemTracker::write_tracking_record(new_addr, flags | MemPointerRecord::malloc_tag(),
805 size, SequenceGenerator::next(), pc, _java_thread);
806 }
807 _seq = 0;
808 }
809 }
810
811 void MemTracker::Tracker::record(address addr, size_t size, MEMFLAGS flags, address pc) {
812 // OOM already?
813 if (addr == NULL) return;
814
815 if (MemTracker::is_on() && NMT_CAN_TRACK(flags) && _op != NoOp) {
816 bool pre_reserved_seq = (_seq != 0);
817 address pc = CALLER_CALLER_PC;
818 MEMFLAGS orig_flags = flags;
819
820 // or the tagging flags
821 switch(_op) {
822 case Malloc:
823 flags |= MemPointerRecord::malloc_tag();
824 break;
825 case Free:
826 flags = MemPointerRecord::free_tag();
827 break;
828 case Realloc:
829 fatal("Use the other Tracker::record()");
830 break;
831 case Reserve:
832 case ReserveAndCommit:
833 flags |= MemPointerRecord::virtual_memory_reserve_tag();
834 break;
835 case Commit:
836 flags = MemPointerRecord::virtual_memory_commit_tag();
837 break;
838 case Type:
839 flags |= MemPointerRecord::virtual_memory_type_tag();
840 break;
841 case Uncommit:
842 assert(pre_reserved_seq, "Need pre-reserve sequence number");
843 flags = MemPointerRecord::virtual_memory_uncommit_tag();
844 break;
845 case Release:
846 assert(pre_reserved_seq, "Need pre-reserve sequence number");
847 flags = MemPointerRecord::virtual_memory_release_tag();
848 break;
849 case ArenaSize:
850 // a bit of hack here, add a small postive offset to arena
851 // address for its size record, so the size record is sorted
852 // right after arena record.
853 flags = MemPointerRecord::arena_size_tag();
854 addr += sizeof(void*);
855 break;
856 case StackRelease:
857 flags = MemPointerRecord::virtual_memory_release_tag();
858 break;
859 default:
860 ShouldNotReachHere();
861 }
862
863 // write memory tracking record
864 if (_need_thread_critical_lock) {
865 ThreadCritical tc;
866 if (_seq == 0) _seq = SequenceGenerator::next();
867 MemTracker::write_tracking_record(addr, flags, size, _seq, pc, _java_thread);
868 if (_op == ReserveAndCommit) {
869 MemTracker::write_tracking_record(addr, orig_flags | MemPointerRecord::virtual_memory_commit_tag(),
870 size, SequenceGenerator::next(), pc, _java_thread);
871 }
872 if (pre_reserved_seq) MemTracker::dec_pending_op_count();
873 } else {
874 if (_seq == 0) _seq = SequenceGenerator::next();
875 MemTracker::write_tracking_record(addr, flags, size, _seq, pc, _java_thread);
876 if (_op == ReserveAndCommit) {
877 MemTracker::write_tracking_record(addr, orig_flags | MemPointerRecord::virtual_memory_commit_tag(),
878 size, SequenceGenerator::next(), pc, _java_thread);
879 }
880 }
881 _seq = 0;
882 }
883 }
884

mercurial