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