src/share/vm/services/memTracker.hpp

Mon, 17 Sep 2012 10:20:04 -0400

author
zgu
date
Mon, 17 Sep 2012 10:20:04 -0400
changeset 4079
716e6ef4482a
parent 4053
33143ee07800
child 4081
9a86ddfc6c8f
permissions
-rw-r--r--

7190089: NMT ON: NMT failed assertion on thread's stack base address
Summary: Solaris only, record stack info to NMT after stack size adjustment was made for primordial threads
Reviewed-by: kvn, acorn, coleenp

zgu@3900 1 /*
zgu@3900 2 * Copyright (c) 2012, 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
zgu@3900 25 #ifndef SHARE_VM_SERVICES_MEM_TRACKER_HPP
zgu@3900 26 #define SHARE_VM_SERVICES_MEM_TRACKER_HPP
zgu@3900 27
zgu@3900 28 #include "memory/allocation.hpp"
zgu@3900 29 #include "runtime/globals.hpp"
zgu@3900 30 #include "runtime/mutex.hpp"
zgu@3900 31 #include "runtime/os.hpp"
zgu@3900 32 #include "runtime/thread.hpp"
zgu@3900 33 #include "services/memPtr.hpp"
zgu@3900 34 #include "services/memRecorder.hpp"
zgu@3900 35 #include "services/memSnapshot.hpp"
zgu@3900 36 #include "services/memTrackWorker.hpp"
zgu@3900 37
zgu@3900 38 #ifdef SOLARIS
zgu@3900 39 #include "thread_solaris.inline.hpp"
zgu@3900 40 #endif
zgu@3900 41
zgu@4053 42 #ifdef _DEBUG
zgu@3900 43 #define DEBUG_CALLER_PC os::get_caller_pc(3)
zgu@3900 44 #else
zgu@3900 45 #define DEBUG_CALLER_PC 0
zgu@3900 46 #endif
zgu@3900 47
zgu@3900 48 // The thread closure walks threads to collect per-thread
zgu@3900 49 // memory recorders at NMT sync point
zgu@3900 50 class SyncThreadRecorderClosure : public ThreadClosure {
zgu@3900 51 private:
zgu@3900 52 int _thread_count;
zgu@3900 53
zgu@3900 54 public:
zgu@3900 55 SyncThreadRecorderClosure() {
zgu@3900 56 _thread_count =0;
zgu@3900 57 }
zgu@3900 58
zgu@3900 59 void do_thread(Thread* thread);
zgu@3900 60 int get_thread_count() const {
zgu@3900 61 return _thread_count;
zgu@3900 62 }
zgu@3900 63 };
zgu@3900 64
zgu@3900 65 class BaselineOutputer;
zgu@3900 66 class MemSnapshot;
zgu@3900 67 class MemTrackWorker;
zgu@3900 68 class Thread;
zgu@3900 69 /*
zgu@3900 70 * MemTracker is the 'gate' class to native memory tracking runtime.
zgu@3900 71 */
zgu@3900 72 class MemTracker : AllStatic {
zgu@3900 73 friend class MemTrackWorker;
zgu@3900 74 friend class MemSnapshot;
zgu@3900 75 friend class SyncThreadRecorderClosure;
zgu@3900 76
zgu@3900 77 // NMT state
zgu@3900 78 enum NMTStates {
zgu@3900 79 NMT_uninited, // not yet initialized
zgu@3900 80 NMT_bootstrapping_single_thread, // bootstrapping, VM is in single thread mode
zgu@3900 81 NMT_bootstrapping_multi_thread, // bootstrapping, VM is about to enter multi-thread mode
zgu@3900 82 NMT_started, // NMT fully started
zgu@3900 83 NMT_shutdown_pending, // shutdown pending
zgu@3900 84 NMT_final_shutdown, // in final phase of shutdown
zgu@3900 85 NMT_shutdown // shutdown
zgu@3900 86 };
zgu@3900 87
zgu@3900 88
zgu@3900 89 // native memory tracking level
zgu@3900 90 enum NMTLevel {
zgu@3900 91 NMT_off, // native memory tracking is off
zgu@3900 92 NMT_summary, // don't track callsite
zgu@3900 93 NMT_detail // track callsite also
zgu@3900 94 };
zgu@3900 95
zgu@3900 96 public:
zgu@3900 97 enum ShutdownReason {
zgu@3900 98 NMT_shutdown_none, // no shutdown requested
zgu@3900 99 NMT_shutdown_user, // user requested shutdown
zgu@3900 100 NMT_normal, // normal shutdown, process exit
zgu@3900 101 NMT_out_of_memory, // shutdown due to out of memory
zgu@3900 102 NMT_initialization, // shutdown due to initialization failure
zgu@3900 103 NMT_use_malloc_only, // can not combine NMT with UseMallocOnly flag
zgu@3900 104 NMT_error_reporting, // shutdown by vmError::report_and_die()
zgu@3900 105 NMT_out_of_generation, // running out of generation queue
zgu@3900 106 NMT_sequence_overflow // overflow the sequence number
zgu@3900 107 };
zgu@3900 108
zgu@3900 109 public:
zgu@3900 110 // initialize NMT tracking level from command line options, called
zgu@3900 111 // from VM command line parsing code
zgu@3900 112 static void init_tracking_options(const char* option_line);
zgu@3900 113
zgu@3900 114 // if NMT is enabled to record memory activities
zgu@3900 115 static inline bool is_on() {
zgu@3900 116 return (_tracking_level >= NMT_summary &&
zgu@3900 117 _state >= NMT_bootstrapping_single_thread);
zgu@3900 118 }
zgu@3900 119
zgu@3900 120 // user readable reason for shutting down NMT
zgu@3900 121 static const char* reason() {
zgu@3900 122 switch(_reason) {
zgu@3900 123 case NMT_shutdown_none:
zgu@3900 124 return "Native memory tracking is not enabled";
zgu@3900 125 case NMT_shutdown_user:
zgu@3900 126 return "Native memory tracking has been shutdown by user";
zgu@3900 127 case NMT_normal:
zgu@3900 128 return "Native memory tracking has been shutdown due to process exiting";
zgu@3936 129 case NMT_out_of_memory:
zgu@3936 130 return "Native memory tracking has been shutdown due to out of native memory";
zgu@3900 131 case NMT_initialization:
zgu@3900 132 return "Native memory tracking failed to initialize";
zgu@3900 133 case NMT_error_reporting:
zgu@3900 134 return "Native memory tracking has been shutdown due to error reporting";
zgu@3900 135 case NMT_out_of_generation:
zgu@3900 136 return "Native memory tracking has been shutdown due to running out of generation buffer";
zgu@3900 137 case NMT_sequence_overflow:
zgu@3900 138 return "Native memory tracking has been shutdown due to overflow the sequence number";
zgu@3900 139 case NMT_use_malloc_only:
zgu@3900 140 return "Native memory tracking is not supported when UseMallocOnly is on";
zgu@3900 141 default:
zgu@3900 142 ShouldNotReachHere();
zgu@3900 143 return NULL;
zgu@3900 144 }
zgu@3900 145 }
zgu@3900 146
zgu@3900 147 // test if we can walk native stack
zgu@3900 148 static bool can_walk_stack() {
zgu@3900 149 // native stack is not walkable during bootstrapping on sparc
zgu@3900 150 #if defined(SPARC)
zgu@3900 151 return (_state == NMT_started);
zgu@3900 152 #else
zgu@3900 153 return (_state >= NMT_bootstrapping_single_thread && _state <= NMT_started);
zgu@3900 154 #endif
zgu@3900 155 }
zgu@3900 156
zgu@3900 157 // if native memory tracking tracks callsite
zgu@3900 158 static inline bool track_callsite() { return _tracking_level == NMT_detail; }
zgu@3900 159
zgu@3900 160 // shutdown native memory tracking capability. Native memory tracking
zgu@3900 161 // can be shutdown by VM when it encounters low memory scenarios.
zgu@3900 162 // Memory tracker should gracefully shutdown itself, and preserve the
zgu@3900 163 // latest memory statistics for post morten diagnosis.
zgu@3900 164 static void shutdown(ShutdownReason reason);
zgu@3900 165
zgu@3900 166 // if there is shutdown requested
zgu@3900 167 static inline bool shutdown_in_progress() {
zgu@3900 168 return (_state >= NMT_shutdown_pending);
zgu@3900 169 }
zgu@3900 170
zgu@3900 171 // bootstrap native memory tracking, so it can start to collect raw data
zgu@3900 172 // before worker thread can start
zgu@3900 173
zgu@3900 174 // the first phase of bootstrapping, when VM still in single-threaded mode
zgu@3900 175 static void bootstrap_single_thread();
zgu@3900 176 // the second phase of bootstrapping, VM is about or already in multi-threaded mode
zgu@3900 177 static void bootstrap_multi_thread();
zgu@3900 178
zgu@3900 179
zgu@3900 180 // start() has to be called when VM still in single thread mode, but after
zgu@3900 181 // command line option parsing is done.
zgu@3900 182 static void start();
zgu@3900 183
zgu@3900 184 // record a 'malloc' call
zgu@3900 185 static inline void record_malloc(address addr, size_t size, MEMFLAGS flags,
zgu@3900 186 address pc = 0, Thread* thread = NULL) {
zgu@3900 187 if (NMT_CAN_TRACK(flags)) {
zgu@4079 188 assert(size > 0, "Sanity check");
zgu@3900 189 create_memory_record(addr, (flags|MemPointerRecord::malloc_tag()), size, pc, thread);
zgu@3900 190 }
zgu@3900 191 }
zgu@3900 192 // record a 'free' call
zgu@3900 193 static inline void record_free(address addr, MEMFLAGS flags, Thread* thread = NULL) {
zgu@3900 194 if (is_on() && NMT_CAN_TRACK(flags)) {
zgu@3900 195 create_memory_record(addr, MemPointerRecord::free_tag(), 0, 0, thread);
zgu@3900 196 }
zgu@3900 197 }
zgu@3900 198 // record a 'realloc' call
zgu@3900 199 static inline void record_realloc(address old_addr, address new_addr, size_t size,
zgu@3900 200 MEMFLAGS flags, address pc = 0, Thread* thread = NULL) {
zgu@3900 201 if (is_on()) {
zgu@4079 202 assert(size > 0, "Sanity check");
zgu@3900 203 record_free(old_addr, flags, thread);
zgu@3900 204 record_malloc(new_addr, size, flags, pc, thread);
zgu@3900 205 }
zgu@3900 206 }
zgu@3900 207
zgu@3900 208 // record arena size
zgu@3900 209 static inline void record_arena_size(address addr, size_t size) {
zgu@3900 210 // we add a positive offset to arena address, so we can have arena size record
zgu@3900 211 // sorted after arena record
zgu@3900 212 if (is_on() && !UseMallocOnly) {
zgu@4079 213 assert(addr != NULL, "Sanity check");
zgu@3900 214 create_memory_record((addr + sizeof(void*)), MemPointerRecord::arena_size_tag(), size,
zgu@3900 215 0, NULL);
zgu@3900 216 }
zgu@3900 217 }
zgu@3900 218
zgu@3900 219 // record a virtual memory 'reserve' call
zgu@3900 220 static inline void record_virtual_memory_reserve(address addr, size_t size,
zgu@3900 221 address pc = 0, Thread* thread = NULL) {
zgu@3900 222 if (is_on()) {
zgu@4079 223 assert(size > 0, "Sanity check");
zgu@3900 224 create_memory_record(addr, MemPointerRecord::virtual_memory_reserve_tag(),
zgu@3900 225 size, pc, thread);
zgu@3900 226 }
zgu@3900 227 }
zgu@3900 228
zgu@4053 229 static inline void record_thread_stack(address addr, size_t size, Thread* thr,
zgu@4053 230 address pc = 0) {
zgu@4053 231 if (is_on()) {
zgu@4053 232 assert(size > 0 && thr != NULL, "Sanity check");
zgu@4053 233 create_memory_record(addr, MemPointerRecord::virtual_memory_reserve_tag() | mtThreadStack,
zgu@4053 234 size, pc, thr);
zgu@4053 235 create_memory_record(addr, MemPointerRecord::virtual_memory_commit_tag() | mtThreadStack,
zgu@4053 236 size, pc, thr);
zgu@4053 237 }
zgu@4053 238 }
zgu@4053 239
zgu@4053 240 static inline void release_thread_stack(address addr, size_t size, Thread* thr) {
zgu@4053 241 if (is_on()) {
zgu@4053 242 assert(size > 0 && thr != NULL, "Sanity check");
zgu@4053 243 create_memory_record(addr, MemPointerRecord::virtual_memory_uncommit_tag() | mtThreadStack,
zgu@4053 244 size, DEBUG_CALLER_PC, thr);
zgu@4053 245 create_memory_record(addr, MemPointerRecord::virtual_memory_release_tag() | mtThreadStack,
zgu@4053 246 size, DEBUG_CALLER_PC, thr);
zgu@4053 247 }
zgu@4053 248 }
zgu@4053 249
zgu@3900 250 // record a virtual memory 'commit' call
zgu@3900 251 static inline void record_virtual_memory_commit(address addr, size_t size,
zgu@3900 252 address pc = 0, Thread* thread = NULL) {
zgu@3900 253 if (is_on()) {
zgu@4079 254 assert(size > 0, "Sanity check");
zgu@3900 255 create_memory_record(addr, MemPointerRecord::virtual_memory_commit_tag(),
zgu@4053 256 size, DEBUG_CALLER_PC, thread);
zgu@3900 257 }
zgu@3900 258 }
zgu@3900 259
zgu@3900 260 // record a virtual memory 'uncommit' call
zgu@3900 261 static inline void record_virtual_memory_uncommit(address addr, size_t size,
zgu@3900 262 Thread* thread = NULL) {
zgu@3900 263 if (is_on()) {
zgu@4079 264 assert(size > 0, "Sanity check");
zgu@3900 265 create_memory_record(addr, MemPointerRecord::virtual_memory_uncommit_tag(),
zgu@4053 266 size, DEBUG_CALLER_PC, thread);
zgu@3900 267 }
zgu@3900 268 }
zgu@3900 269
zgu@3900 270 // record a virtual memory 'release' call
zgu@3900 271 static inline void record_virtual_memory_release(address addr, size_t size,
zgu@3900 272 Thread* thread = NULL) {
zgu@3900 273 if (is_on()) {
zgu@4079 274 assert(size > 0, "Sanity check");
zgu@3900 275 create_memory_record(addr, MemPointerRecord::virtual_memory_release_tag(),
zgu@4053 276 size, DEBUG_CALLER_PC, thread);
zgu@3900 277 }
zgu@3900 278 }
zgu@3900 279
zgu@3900 280 // record memory type on virtual memory base address
zgu@3900 281 static inline void record_virtual_memory_type(address base, MEMFLAGS flags,
zgu@3900 282 Thread* thread = NULL) {
zgu@3900 283 if (is_on()) {
zgu@3900 284 assert(base > 0, "wrong base address");
zgu@3900 285 assert((flags & (~mt_masks)) == 0, "memory type only");
zgu@3900 286 create_memory_record(base, (flags | MemPointerRecord::virtual_memory_type_tag()),
zgu@4053 287 0, DEBUG_CALLER_PC, thread);
zgu@3900 288 }
zgu@3900 289 }
zgu@3900 290
zgu@3900 291
zgu@3900 292 // create memory baseline of current memory snapshot
zgu@3900 293 static bool baseline();
zgu@3900 294 // is there a memory baseline
zgu@3900 295 static bool has_baseline() {
zgu@3900 296 return _baseline.baselined();
zgu@3900 297 }
zgu@3900 298
zgu@3900 299 // print memory usage from current snapshot
zgu@3900 300 static bool print_memory_usage(BaselineOutputer& out, size_t unit,
zgu@3900 301 bool summary_only = true);
zgu@3900 302 // compare memory usage between current snapshot and baseline
zgu@3900 303 static bool compare_memory_usage(BaselineOutputer& out, size_t unit,
zgu@3900 304 bool summary_only = true);
zgu@3900 305
zgu@3900 306 // sync is called within global safepoint to synchronize nmt data
zgu@3900 307 static void sync();
zgu@3900 308
zgu@3900 309 // called when a thread is about to exit
zgu@3900 310 static void thread_exiting(JavaThread* thread);
zgu@3900 311
zgu@3900 312 // retrieve global snapshot
zgu@3900 313 static MemSnapshot* get_snapshot() {
zgu@3900 314 if (shutdown_in_progress()) {
zgu@3900 315 return NULL;
zgu@3900 316 }
zgu@3900 317 return _snapshot;
zgu@3900 318 }
zgu@3900 319
zgu@3900 320 // print tracker stats
zgu@3900 321 NOT_PRODUCT(static void print_tracker_stats(outputStream* st);)
zgu@3900 322 NOT_PRODUCT(static void walk_stack(int toSkip, char* buf, int len);)
zgu@3900 323
zgu@3900 324 private:
zgu@3900 325 // start native memory tracking worker thread
zgu@3900 326 static bool start_worker();
zgu@3900 327
zgu@3900 328 // called by worker thread to complete shutdown process
zgu@3900 329 static void final_shutdown();
zgu@3900 330
zgu@3900 331 protected:
zgu@3900 332 // retrieve per-thread recorder of the specified thread.
zgu@3900 333 // if the recorder is full, it will be enqueued to overflow
zgu@3900 334 // queue, a new recorder is acquired from recorder pool or a
zgu@3900 335 // new instance is created.
zgu@3900 336 // when thread == NULL, it means global recorder
zgu@3900 337 static MemRecorder* get_thread_recorder(JavaThread* thread);
zgu@3900 338
zgu@3900 339 // per-thread recorder pool
zgu@3900 340 static void release_thread_recorder(MemRecorder* rec);
zgu@3900 341 static void delete_all_pooled_recorders();
zgu@3900 342
zgu@3900 343 // pending recorder queue. Recorders are queued to pending queue
zgu@3900 344 // when they are overflowed or collected at nmt sync point.
zgu@3900 345 static void enqueue_pending_recorder(MemRecorder* rec);
zgu@3900 346 static MemRecorder* get_pending_recorders();
zgu@3900 347 static void delete_all_pending_recorders();
zgu@3900 348
zgu@3900 349 private:
zgu@3900 350 // retrieve a pooled memory record or create new one if there is not
zgu@3900 351 // one available
zgu@3900 352 static MemRecorder* get_new_or_pooled_instance();
zgu@3900 353 static void create_memory_record(address addr, MEMFLAGS type,
zgu@3900 354 size_t size, address pc, Thread* thread);
zgu@3900 355 static void create_record_in_recorder(address addr, MEMFLAGS type,
zgu@3935 356 size_t size, address pc, JavaThread* thread);
zgu@3900 357
zgu@3900 358 private:
zgu@3900 359 // global memory snapshot
zgu@3900 360 static MemSnapshot* _snapshot;
zgu@3900 361
zgu@3900 362 // a memory baseline of snapshot
zgu@3900 363 static MemBaseline _baseline;
zgu@3900 364
zgu@3900 365 // query lock
zgu@3936 366 static Mutex* _query_lock;
zgu@3900 367
zgu@3900 368 // a thread can start to allocate memory before it is attached
zgu@3900 369 // to VM 'Thread', those memory activities are recorded here.
zgu@3900 370 // ThreadCritical is required to guard this global recorder.
zgu@3900 371 static MemRecorder* _global_recorder;
zgu@3900 372
zgu@3900 373 // main thread id
zgu@3900 374 debug_only(static intx _main_thread_tid;)
zgu@3900 375
zgu@3900 376 // pending recorders to be merged
zgu@3900 377 static volatile MemRecorder* _merge_pending_queue;
zgu@3900 378
zgu@3900 379 NOT_PRODUCT(static volatile jint _pending_recorder_count;)
zgu@3900 380
zgu@3900 381 // pooled memory recorders
zgu@3900 382 static volatile MemRecorder* _pooled_recorders;
zgu@3900 383
zgu@3900 384 // memory recorder pool management, uses following
zgu@3900 385 // counter to determine if a released memory recorder
zgu@3900 386 // should be pooled
zgu@3900 387
zgu@3900 388 // latest thread count
zgu@3900 389 static int _thread_count;
zgu@3900 390 // pooled recorder count
zgu@3900 391 static volatile jint _pooled_recorder_count;
zgu@3900 392
zgu@3900 393
zgu@3900 394 // worker thread to merge pending recorders into snapshot
zgu@3900 395 static MemTrackWorker* _worker_thread;
zgu@3900 396
zgu@3900 397 // how many safepoints we skipped without entering sync point
zgu@3900 398 static int _sync_point_skip_count;
zgu@3900 399
zgu@3900 400 // if the tracker is properly intialized
zgu@3900 401 static bool _is_tracker_ready;
zgu@3900 402 // tracking level (off, summary and detail)
zgu@3900 403 static enum NMTLevel _tracking_level;
zgu@3900 404
zgu@3900 405 // current nmt state
zgu@3900 406 static volatile enum NMTStates _state;
zgu@3900 407 // the reason for shutting down nmt
zgu@3900 408 static enum ShutdownReason _reason;
zgu@3900 409 };
zgu@3900 410
zgu@3900 411 #endif // SHARE_VM_SERVICES_MEM_TRACKER_HPP

mercurial