memTracker.cpp revision 4060
3863N/A * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. 3863N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3863N/A * This code is free software; you can redistribute it and/or modify it 3863N/A * under the terms of the GNU General Public License version 2 only, as 3863N/A * published by the Free Software Foundation. 3863N/A * This code is distributed in the hope that it will be useful, but WITHOUT 3863N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 3863N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 3863N/A * version 2 for more details (a copy is included in the LICENSE file that 3863N/A * You should have received a copy of the GNU General Public License version 3863N/A * 2 along with this work; if not, write to the Free Software Foundation, 3863N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 3863N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 3863N/A// walk all 'known' threads at NMT sync point, and collect their recorders 3863N/A// first phase of bootstrapping, when VM is still in single-threaded mode. 3863N/A // NMT is not supported with UseMallocOnly is on. NMT can NOT 3863N/A // handle the amount of malloc data without significantly impacting 3863N/A // runtime performance when this flag is on. 3863N/A// second phase of bootstrapping, when VM is about to or already entered multi-theaded mode. 3863N/A // create nmt lock for multi-thread execution 3863N/A // Native memory tracking is off from command line option 3863N/A // fail to start native memory tracking, shut it down 3863N/A * Shutting down native memory tracking. 3863N/A * We can not shutdown native memory tracking immediately, so we just 3863N/A * setup shutdown pending flag, every native memory tracking component 3863N/A * should orderly shut itself down. 3863N/A * 1. MemTracker::shutdown() sets MemTracker to shutdown pending state 3863N/A * 2. Worker thread calls MemTracker::final_shutdown(), which transites 3863N/A * MemTracker to final shutdown state. 3863N/A * 3. At sync point, MemTracker does final cleanup, before sets memory 3863N/A * tracking level to off to complete shutdown. 3863N/A // we still in single thread mode, there is not contention 3863N/A // we want to know who initialized shutdown 3863N/A // delete all pending recorders and pooled recorders 3863N/A // shared baseline and snapshot are the only objects needed to 3863N/A // cleanup baseline data and snapshot 3863N/A // shutdown shared decoder instance, since it is only 3863N/A // used by native memory tracking so far. 3863N/A // can not delete worker inside the thread critical 3863N/A// delete all pooled recorders 3863N/A // free all pooled recorders 3863N/A// delete all recorders in pending queue 3863N/A // free all pending recorders 3863N/A * retrieve per-thread recorder of specified thread. 3863N/A * if thread == NULL, it means global recorder 3863N/A * get a per-thread recorder from pool, or create a new one if 3863N/A * there is not one available. 3863N/A * retrieve all recorders in pending queue, and empty the queue 3863N/A * release a recorder to recorder pool. 3863N/A // we don't want to pool too many recorders 3863N/A * This is the most important method in whole nmt implementation. 3863N/A * 1. When nmt is in single-threaded bootstrapping mode, no lock is needed as VM 3863N/A * still in single thread mode. 3863N/A * 2. For all threads other than JavaThread, ThreadCritical is needed 3863N/A * to write to recorders to global recorder. 3863N/A * 3. For JavaThreads that are not longer visible by safepoint, also 3863N/A * need to take ThreadCritical and records are written to global 3863N/A * recorders, since these threads are NOT walked by Threads.do_thread(). 3863N/A * 4. JavaThreads that are running in native state, have to transition 3863N/A * to VM state before writing to per-thread recorders. 3863N/A * 5. JavaThreads that are running in VM state do not need any lock and 3863N/A * records are written to per-thread recorders. 3863N/A * 6. For a thread has yet to attach VM 'Thread', they need to take 3863N/A * ThreadCritical to write to global recorder. 3863N/A * NO LOCK should be taken inside ThreadCritical lock !!! 3863N/A // single thread, we just write records direct to global recorder,' 3863N/A // don't use Thread::current(), since it is possible that 3863N/A // the calling thread has yet to attach to VM 'Thread', 3863N/A // which will result assertion failure 3897N/A // JavaThreads that are safepoint safe, can run through safepoint, 3897N/A // so ThreadCritical is needed to ensure no threads at safepoint create 3897N/A // new records while the records are being gathered and the sequence number is changing 3863N/A // other threads, such as worker and watcher threads, etc. need to 3863N/A // take ThreadCritical to write to global recorder 3863N/A // single thread, no lock needed 3863N/A // for thread has yet to attach VM 'Thread', we can not use VM mutex. 3863N/A // use native thread critical instead 3863N/A// write a record to proper recorder. No lock can be taken from this method 3863N/A * enqueue a recorder to pending queue 3863N/A // we are shutting down, so just delete it 3863N/A * The method is called at global safepoint 3863N/A * during it synchronization process. 3863N/A * 1. enqueue all JavaThreads' per-thread recorders 3863N/A * 2. enqueue global recorder 3863N/A * 3. retrieve all pending recorders 3863N/A * 4. reset global sequence number generator 3863N/A // Some GC tests hit large number of safepoints in short period of time 3863N/A // without meaningful activities. We should prevent going to 3863N/A // sync point in these cases, which can potentially exhaust generation buffer. 3863N/A // Here is the factots to determine if we should go into sync point: 3863N/A // 1. not to overflow sequence number 3863N/A // 2. if we are in danger to overflow generation buffer 3863N/A // 3. how many safepoints we already skipped sync point 3863N/A // worker thread is not ready, no one can manage generation 3863N/A // buffer, so skip this safepoint 3863N/A // This method is running at safepoint, with ThreadCritical lock, 3863N/A // it should guarantee that NMT is fully sync-ed. 3897N/A // walk all JavaThreads to collect recorders 3863N/A // check _worker_thread with lock to avoid racing condition 3863N/A // now, it is the time to shut whole things off 3863N/A // walk all JavaThreads to delete all recorders 3897N/A // try at a later sync point to ensure MemRecorder instance drops to zero to 3863N/A * We need to collect a JavaThread's per-thread recorder 3863N/A// baseline current memory snapshot 3863N/A// print memory usage from current snapshot 3863N/A// compare memory usage between current snapshot and baseline