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