memTracker.cpp revision 3758:716c64bda5ba
1/*
2 * Copyright (c) 2012, 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#include "precompiled.hpp"
25
26#include "runtime/atomic.hpp"
27#include "runtime/interfaceSupport.hpp"
28#include "runtime/mutexLocker.hpp"
29#include "runtime/safepoint.hpp"
30#include "runtime/threadCritical.hpp"
31#include "services/memPtr.hpp"
32#include "services/memReporter.hpp"
33#include "services/memTracker.hpp"
34#include "utilities/decoder.hpp"
35#include "utilities/globalDefinitions.hpp"
36
37bool NMT_track_callsite = false;
38
39// walk all 'known' threads at NMT sync point, and collect their recorders
40void SyncThreadRecorderClosure::do_thread(Thread* thread) {
41  assert(SafepointSynchronize::is_at_safepoint(), "Safepoint required");
42  if (thread->is_Java_thread()) {
43    JavaThread* javaThread = (JavaThread*)thread;
44    MemRecorder* recorder = javaThread->get_recorder();
45    if (recorder != NULL) {
46      MemTracker::enqueue_pending_recorder(recorder);
47      javaThread->set_recorder(NULL);
48    }
49  }
50  _thread_count ++;
51}
52
53
54MemRecorder*                    MemTracker::_global_recorder = NULL;
55MemSnapshot*                    MemTracker::_snapshot = NULL;
56MemBaseline                     MemTracker::_baseline;
57Mutex*                          MemTracker::_query_lock = NULL;
58volatile MemRecorder*           MemTracker::_merge_pending_queue = NULL;
59volatile MemRecorder*           MemTracker::_pooled_recorders = NULL;
60MemTrackWorker*                 MemTracker::_worker_thread = NULL;
61int                             MemTracker::_sync_point_skip_count = 0;
62MemTracker::NMTLevel            MemTracker::_tracking_level = MemTracker::NMT_off;
63volatile MemTracker::NMTStates  MemTracker::_state = NMT_uninited;
64MemTracker::ShutdownReason      MemTracker::_reason = NMT_shutdown_none;
65int                             MemTracker::_thread_count = 255;
66volatile jint                   MemTracker::_pooled_recorder_count = 0;
67debug_only(intx                 MemTracker::_main_thread_tid = 0;)
68NOT_PRODUCT(volatile jint       MemTracker::_pending_recorder_count = 0;)
69
70void MemTracker::init_tracking_options(const char* option_line) {
71  _tracking_level = NMT_off;
72  if (strncmp(option_line, "=summary", 8) == 0) {
73    _tracking_level = NMT_summary;
74  } else if (strncmp(option_line, "=detail", 7) == 0) {
75    _tracking_level = NMT_detail;
76  } else {
77    char msg[255];
78    //+1 to remove the '=' character
79    jio_snprintf(msg, 255, "Unknown option given to XX:NativeMemoryTracking: %s", option_line+1);
80    vm_exit_during_initialization(msg, NULL);
81  }
82}
83
84// first phase of bootstrapping, when VM is still in single-threaded mode.
85void MemTracker::bootstrap_single_thread() {
86  if (_tracking_level > NMT_off) {
87    assert(_state == NMT_uninited, "wrong state");
88
89    // NMT is not supported with UseMallocOnly is on. NMT can NOT
90    // handle the amount of malloc data without significantly impacting
91    // runtime performance when this flag is on.
92    if (UseMallocOnly) {
93      shutdown(NMT_use_malloc_only);
94      return;
95    }
96
97    _query_lock = new (std::nothrow) Mutex(Monitor::max_nonleaf, "NMT_queryLock");
98    if (_query_lock == NULL) {
99      shutdown(NMT_out_of_memory);
100      return;
101    }
102
103    debug_only(_main_thread_tid = os::current_thread_id();)
104    _state = NMT_bootstrapping_single_thread;
105    NMT_track_callsite = (_tracking_level == NMT_detail && can_walk_stack());
106  }
107}
108
109// second phase of bootstrapping, when VM is about to or already entered multi-theaded mode.
110void MemTracker::bootstrap_multi_thread() {
111  if (_tracking_level > NMT_off && _state == NMT_bootstrapping_single_thread) {
112  // create nmt lock for multi-thread execution
113    assert(_main_thread_tid == os::current_thread_id(), "wrong thread");
114    _state = NMT_bootstrapping_multi_thread;
115    NMT_track_callsite = (_tracking_level == NMT_detail && can_walk_stack());
116  }
117}
118
119// fully start nmt
120void MemTracker::start() {
121  // Native memory tracking is off from command line option
122  if (_tracking_level == NMT_off || shutdown_in_progress()) return;
123
124  assert(_main_thread_tid == os::current_thread_id(), "wrong thread");
125  assert(_state == NMT_bootstrapping_multi_thread, "wrong state");
126
127  _snapshot = new (std::nothrow)MemSnapshot();
128  if (_snapshot != NULL && !_snapshot->out_of_memory()) {
129    if (start_worker()) {
130      _state = NMT_started;
131      NMT_track_callsite = (_tracking_level == NMT_detail && can_walk_stack());
132      return;
133    }
134  }
135
136  // fail to start native memory tracking, shut it down
137  shutdown(NMT_initialization);
138}
139
140/**
141 * Shutting down native memory tracking.
142 * We can not shutdown native memory tracking immediately, so we just
143 * setup shutdown pending flag, every native memory tracking component
144 * should orderly shut itself down.
145 *
146 * The shutdown sequences:
147 *  1. MemTracker::shutdown() sets MemTracker to shutdown pending state
148 *  2. Worker thread calls MemTracker::final_shutdown(), which transites
149 *     MemTracker to final shutdown state.
150 *  3. At sync point, MemTracker does final cleanup, before sets memory
151 *     tracking level to off to complete shutdown.
152 */
153void MemTracker::shutdown(ShutdownReason reason) {
154  if (_tracking_level == NMT_off) return;
155
156  if (_state <= NMT_bootstrapping_single_thread) {
157    // we still in single thread mode, there is not contention
158    _state = NMT_shutdown_pending;
159    _reason = reason;
160  } else {
161    // we want to know who initialized shutdown
162    if ((jint)NMT_started == Atomic::cmpxchg((jint)NMT_shutdown_pending,
163                                       (jint*)&_state, (jint)NMT_started)) {
164        _reason = reason;
165    }
166  }
167}
168
169// final phase of shutdown
170void MemTracker::final_shutdown() {
171  // delete all pending recorders and pooled recorders
172  delete_all_pending_recorders();
173  delete_all_pooled_recorders();
174
175  {
176    // shared baseline and snapshot are the only objects needed to
177    // create query results
178    MutexLockerEx locker(_query_lock, true);
179    // cleanup baseline data and snapshot
180    _baseline.clear();
181    delete _snapshot;
182    _snapshot = NULL;
183  }
184
185  // shutdown shared decoder instance, since it is only
186  // used by native memory tracking so far.
187  Decoder::shutdown();
188
189  MemTrackWorker* worker = NULL;
190  {
191    ThreadCritical tc;
192    // can not delete worker inside the thread critical
193    if (_worker_thread != NULL && Thread::current() == _worker_thread) {
194      worker = _worker_thread;
195      _worker_thread = NULL;
196    }
197  }
198  if (worker != NULL) {
199    delete worker;
200  }
201  _state = NMT_final_shutdown;
202}
203
204// delete all pooled recorders
205void MemTracker::delete_all_pooled_recorders() {
206  // free all pooled recorders
207  volatile MemRecorder* cur_head = _pooled_recorders;
208  if (cur_head != NULL) {
209    MemRecorder* null_ptr = NULL;
210    while (cur_head != NULL && (void*)cur_head != Atomic::cmpxchg_ptr((void*)null_ptr,
211      (void*)&_pooled_recorders, (void*)cur_head)) {
212      cur_head = _pooled_recorders;
213    }
214    if (cur_head != NULL) {
215      delete cur_head;
216      _pooled_recorder_count = 0;
217    }
218  }
219}
220
221// delete all recorders in pending queue
222void MemTracker::delete_all_pending_recorders() {
223  // free all pending recorders
224  MemRecorder* pending_head = get_pending_recorders();
225  if (pending_head != NULL) {
226    delete pending_head;
227  }
228}
229
230/*
231 * retrieve per-thread recorder of specified thread.
232 * if thread == NULL, it means global recorder
233 */
234MemRecorder* MemTracker::get_thread_recorder(JavaThread* thread) {
235  if (shutdown_in_progress()) return NULL;
236
237  MemRecorder* rc;
238  if (thread == NULL) {
239    rc = _global_recorder;
240  } else {
241    rc = thread->get_recorder();
242  }
243
244  if (rc != NULL && rc->is_full()) {
245    enqueue_pending_recorder(rc);
246    rc = NULL;
247  }
248
249  if (rc == NULL) {
250    rc = get_new_or_pooled_instance();
251    if (thread == NULL) {
252      _global_recorder = rc;
253    } else {
254      thread->set_recorder(rc);
255    }
256  }
257  return rc;
258}
259
260/*
261 * get a per-thread recorder from pool, or create a new one if
262 * there is not one available.
263 */
264MemRecorder* MemTracker::get_new_or_pooled_instance() {
265   MemRecorder* cur_head = const_cast<MemRecorder*> (_pooled_recorders);
266   if (cur_head == NULL) {
267     MemRecorder* rec = new (std::nothrow)MemRecorder();
268     if (rec == NULL || rec->out_of_memory()) {
269       shutdown(NMT_out_of_memory);
270       if (rec != NULL) {
271         delete rec;
272         rec = NULL;
273       }
274     }
275     return rec;
276   } else {
277     MemRecorder* next_head = cur_head->next();
278     if ((void*)cur_head != Atomic::cmpxchg_ptr((void*)next_head, (void*)&_pooled_recorders,
279       (void*)cur_head)) {
280       return get_new_or_pooled_instance();
281     }
282     cur_head->set_next(NULL);
283     Atomic::dec(&_pooled_recorder_count);
284     debug_only(cur_head->set_generation();)
285     return cur_head;
286  }
287}
288
289/*
290 * retrieve all recorders in pending queue, and empty the queue
291 */
292MemRecorder* MemTracker::get_pending_recorders() {
293  MemRecorder* cur_head = const_cast<MemRecorder*>(_merge_pending_queue);
294  MemRecorder* null_ptr = NULL;
295  while ((void*)cur_head != Atomic::cmpxchg_ptr((void*)null_ptr, (void*)&_merge_pending_queue,
296    (void*)cur_head)) {
297    cur_head = const_cast<MemRecorder*>(_merge_pending_queue);
298  }
299  NOT_PRODUCT(Atomic::store(0, &_pending_recorder_count));
300  return cur_head;
301}
302
303/*
304 * release a recorder to recorder pool.
305 */
306void MemTracker::release_thread_recorder(MemRecorder* rec) {
307  assert(rec != NULL, "null recorder");
308  // we don't want to pool too many recorders
309  rec->set_next(NULL);
310  if (shutdown_in_progress() || _pooled_recorder_count > _thread_count * 2) {
311    delete rec;
312    return;
313  }
314
315  rec->clear();
316  MemRecorder* cur_head = const_cast<MemRecorder*>(_pooled_recorders);
317  rec->set_next(cur_head);
318  while ((void*)cur_head != Atomic::cmpxchg_ptr((void*)rec, (void*)&_pooled_recorders,
319    (void*)cur_head)) {
320    cur_head = const_cast<MemRecorder*>(_pooled_recorders);
321    rec->set_next(cur_head);
322  }
323  Atomic::inc(&_pooled_recorder_count);
324}
325
326/*
327 * This is the most important method in whole nmt implementation.
328 *
329 * Create a memory record.
330 * 1. When nmt is in single-threaded bootstrapping mode, no lock is needed as VM
331 *    still in single thread mode.
332 * 2. For all threads other than JavaThread, ThreadCritical is needed
333 *    to write to recorders to global recorder.
334 * 3. For JavaThreads that are not longer visible by safepoint, also
335 *    need to take ThreadCritical and records are written to global
336 *    recorders, since these threads are NOT walked by Threads.do_thread().
337 * 4. JavaThreads that are running in native state, have to transition
338 *    to VM state before writing to per-thread recorders.
339 * 5. JavaThreads that are running in VM state do not need any lock and
340 *    records are written to per-thread recorders.
341 * 6. For a thread has yet to attach VM 'Thread', they need to take
342 *    ThreadCritical to write to global recorder.
343 *
344 *    Important note:
345 *    NO LOCK should be taken inside ThreadCritical lock !!!
346 */
347void MemTracker::create_memory_record(address addr, MEMFLAGS flags,
348    size_t size, address pc, Thread* thread) {
349  assert(addr != NULL, "Sanity check");
350  if (!shutdown_in_progress()) {
351    // single thread, we just write records direct to global recorder,'
352    // with any lock
353    if (_state == NMT_bootstrapping_single_thread) {
354      assert(_main_thread_tid == os::current_thread_id(), "wrong thread");
355      thread = NULL;
356    } else {
357      if (thread == NULL) {
358          // don't use Thread::current(), since it is possible that
359          // the calling thread has yet to attach to VM 'Thread',
360          // which will result assertion failure
361          thread = ThreadLocalStorage::thread();
362      }
363    }
364
365    if (thread != NULL) {
366      if (thread->is_Java_thread() && ((JavaThread*)thread)->is_safepoint_visible()) {
367        JavaThread*      java_thread = (JavaThread*)thread;
368        JavaThreadState  state = java_thread->thread_state();
369        if (SafepointSynchronize::safepoint_safe(java_thread, state)) {
370          // JavaThreads that are safepoint safe, can run through safepoint,
371          // so ThreadCritical is needed to ensure no threads at safepoint create
372          // new records while the records are being gathered and the sequence number is changing
373          ThreadCritical tc;
374          create_record_in_recorder(addr, flags, size, pc, java_thread);
375        } else {
376          create_record_in_recorder(addr, flags, size, pc, java_thread);
377        }
378      } else {
379        // other threads, such as worker and watcher threads, etc. need to
380        // take ThreadCritical to write to global recorder
381        ThreadCritical tc;
382        create_record_in_recorder(addr, flags, size, pc, NULL);
383      }
384    } else {
385      if (_state == NMT_bootstrapping_single_thread) {
386        // single thread, no lock needed
387        create_record_in_recorder(addr, flags, size, pc, NULL);
388      } else {
389        // for thread has yet to attach VM 'Thread', we can not use VM mutex.
390        // use native thread critical instead
391        ThreadCritical tc;
392        create_record_in_recorder(addr, flags, size, pc, NULL);
393      }
394    }
395  }
396}
397
398// write a record to proper recorder. No lock can be taken from this method
399// down.
400void MemTracker::create_record_in_recorder(address addr, MEMFLAGS flags,
401    size_t size, address pc, JavaThread* thread) {
402
403    MemRecorder* rc = get_thread_recorder(thread);
404    if (rc != NULL) {
405      rc->record(addr, flags, size, pc);
406    }
407}
408
409/**
410 * enqueue a recorder to pending queue
411 */
412void MemTracker::enqueue_pending_recorder(MemRecorder* rec) {
413  assert(rec != NULL, "null recorder");
414
415  // we are shutting down, so just delete it
416  if (shutdown_in_progress()) {
417    rec->set_next(NULL);
418    delete rec;
419    return;
420  }
421
422  MemRecorder* cur_head = const_cast<MemRecorder*>(_merge_pending_queue);
423  rec->set_next(cur_head);
424  while ((void*)cur_head != Atomic::cmpxchg_ptr((void*)rec, (void*)&_merge_pending_queue,
425    (void*)cur_head)) {
426    cur_head = const_cast<MemRecorder*>(_merge_pending_queue);
427    rec->set_next(cur_head);
428  }
429  NOT_PRODUCT(Atomic::inc(&_pending_recorder_count);)
430}
431
432/*
433 * The method is called at global safepoint
434 * during it synchronization process.
435 *   1. enqueue all JavaThreads' per-thread recorders
436 *   2. enqueue global recorder
437 *   3. retrieve all pending recorders
438 *   4. reset global sequence number generator
439 *   5. call worker's sync
440 */
441#define MAX_SAFEPOINTS_TO_SKIP     128
442#define SAFE_SEQUENCE_THRESHOLD    30
443#define HIGH_GENERATION_THRESHOLD  60
444
445void MemTracker::sync() {
446  assert(_tracking_level > NMT_off, "NMT is not enabled");
447  assert(SafepointSynchronize::is_at_safepoint(), "Safepoint required");
448
449  // Some GC tests hit large number of safepoints in short period of time
450  // without meaningful activities. We should prevent going to
451  // sync point in these cases, which can potentially exhaust generation buffer.
452  // Here is the factots to determine if we should go into sync point:
453  // 1. not to overflow sequence number
454  // 2. if we are in danger to overflow generation buffer
455  // 3. how many safepoints we already skipped sync point
456  if (_state == NMT_started) {
457    // worker thread is not ready, no one can manage generation
458    // buffer, so skip this safepoint
459    if (_worker_thread == NULL) return;
460
461    if (_sync_point_skip_count < MAX_SAFEPOINTS_TO_SKIP) {
462      int per_seq_in_use = SequenceGenerator::peek() * 100 / max_jint;
463      int per_gen_in_use = _worker_thread->generations_in_use() * 100 / MAX_GENERATIONS;
464      if (per_seq_in_use < SAFE_SEQUENCE_THRESHOLD && per_gen_in_use >= HIGH_GENERATION_THRESHOLD) {
465        _sync_point_skip_count ++;
466        return;
467      }
468    }
469    _sync_point_skip_count = 0;
470    {
471      // This method is running at safepoint, with ThreadCritical lock,
472      // it should guarantee that NMT is fully sync-ed.
473      ThreadCritical tc;
474
475      SequenceGenerator::reset();
476
477      // walk all JavaThreads to collect recorders
478      SyncThreadRecorderClosure stc;
479      Threads::threads_do(&stc);
480
481      _thread_count = stc.get_thread_count();
482      MemRecorder* pending_recorders = get_pending_recorders();
483
484      if (_global_recorder != NULL) {
485        _global_recorder->set_next(pending_recorders);
486        pending_recorders = _global_recorder;
487        _global_recorder = NULL;
488      }
489      // check _worker_thread with lock to avoid racing condition
490      if (_worker_thread != NULL) {
491        _worker_thread->at_sync_point(pending_recorders);
492      }
493
494      assert(SequenceGenerator::peek() == 1, "Should not have memory activities during sync-point");
495    }
496  }
497
498  // now, it is the time to shut whole things off
499  if (_state == NMT_final_shutdown) {
500    // walk all JavaThreads to delete all recorders
501    SyncThreadRecorderClosure stc;
502    Threads::threads_do(&stc);
503    // delete global recorder
504    {
505      ThreadCritical tc;
506      if (_global_recorder != NULL) {
507        delete _global_recorder;
508        _global_recorder = NULL;
509      }
510    }
511    MemRecorder* pending_recorders = get_pending_recorders();
512    if (pending_recorders != NULL) {
513      delete pending_recorders;
514    }
515    // try at a later sync point to ensure MemRecorder instance drops to zero to
516    // completely shutdown NMT
517    if (MemRecorder::_instance_count == 0) {
518      _state = NMT_shutdown;
519      _tracking_level = NMT_off;
520    }
521  }
522}
523
524/*
525 * Start worker thread.
526 */
527bool MemTracker::start_worker() {
528  assert(_worker_thread == NULL, "Just Check");
529  _worker_thread = new (std::nothrow) MemTrackWorker();
530  if (_worker_thread == NULL || _worker_thread->has_error()) {
531    shutdown(NMT_initialization);
532    return false;
533  }
534  _worker_thread->start();
535  return true;
536}
537
538/*
539 * We need to collect a JavaThread's per-thread recorder
540 * before it exits.
541 */
542void MemTracker::thread_exiting(JavaThread* thread) {
543  if (is_on()) {
544    MemRecorder* rec = thread->get_recorder();
545    if (rec != NULL) {
546      enqueue_pending_recorder(rec);
547      thread->set_recorder(NULL);
548    }
549  }
550}
551
552// baseline current memory snapshot
553bool MemTracker::baseline() {
554  MutexLockerEx lock(_query_lock, true);
555  MemSnapshot* snapshot = get_snapshot();
556  if (snapshot != NULL) {
557    return _baseline.baseline(*snapshot, false);
558  }
559  return false;
560}
561
562// print memory usage from current snapshot
563bool MemTracker::print_memory_usage(BaselineOutputer& out, size_t unit, bool summary_only) {
564  MemBaseline  baseline;
565  MutexLockerEx lock(_query_lock, true);
566  MemSnapshot* snapshot = get_snapshot();
567  if (snapshot != NULL && baseline.baseline(*snapshot, summary_only)) {
568    BaselineReporter reporter(out, unit);
569    reporter.report_baseline(baseline, summary_only);
570    return true;
571  }
572  return false;
573}
574
575// compare memory usage between current snapshot and baseline
576bool MemTracker::compare_memory_usage(BaselineOutputer& out, size_t unit, bool summary_only) {
577  MutexLockerEx lock(_query_lock, true);
578  if (_baseline.baselined()) {
579    MemBaseline baseline;
580    MemSnapshot* snapshot = get_snapshot();
581    if (snapshot != NULL && baseline.baseline(*snapshot, summary_only)) {
582      BaselineReporter reporter(out, unit);
583      reporter.diff_baselines(baseline, _baseline, summary_only);
584      return true;
585    }
586  }
587  return false;
588}
589
590#ifndef PRODUCT
591void MemTracker::walk_stack(int toSkip, char* buf, int len) {
592  int cur_len = 0;
593  char tmp[1024];
594  address pc;
595
596  while (cur_len < len) {
597    pc = os::get_caller_pc(toSkip + 1);
598    if (pc != NULL && os::dll_address_to_function_name(pc, tmp, sizeof(tmp), NULL)) {
599      jio_snprintf(&buf[cur_len], (len - cur_len), "%s\n", tmp);
600      cur_len = (int)strlen(buf);
601    } else {
602      buf[cur_len] = '\0';
603      break;
604    }
605    toSkip ++;
606  }
607}
608
609void MemTracker::print_tracker_stats(outputStream* st) {
610  st->print_cr("\nMemory Tracker Stats:");
611  st->print_cr("\tMax sequence number = %d", SequenceGenerator::max_seq_num());
612  st->print_cr("\tthead count = %d", _thread_count);
613  st->print_cr("\tArena instance = %d", Arena::_instance_count);
614  st->print_cr("\tpooled recorder count = %d", _pooled_recorder_count);
615  st->print_cr("\tqueued recorder count = %d", _pending_recorder_count);
616  st->print_cr("\tmemory recorder instance count = %d", MemRecorder::_instance_count);
617  if (_worker_thread != NULL) {
618    st->print_cr("\tWorker thread:");
619    st->print_cr("\t\tSync point count = %d", _worker_thread->_sync_point_count);
620    st->print_cr("\t\tpending recorder count = %d", _worker_thread->count_pending_recorders());
621    st->print_cr("\t\tmerge count = %d", _worker_thread->_merge_count);
622  } else {
623    st->print_cr("\tWorker thread is not started");
624  }
625  st->print_cr(" ");
626
627  if (_snapshot != NULL) {
628    _snapshot->print_snapshot_stats(st);
629  } else {
630    st->print_cr("No snapshot");
631  }
632}
633#endif
634
635