Wed, 27 Aug 2014 09:36:55 +0200
Merge
zgu@3900 | 1 | /* |
zgu@5272 | 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 | |
zgu@3900 | 25 | #ifndef SHARE_VM_SERVICES_MEM_TRACKER_HPP |
zgu@3900 | 26 | #define SHARE_VM_SERVICES_MEM_TRACKER_HPP |
zgu@3900 | 27 | |
jprovino@4165 | 28 | #include "utilities/macros.hpp" |
jprovino@4165 | 29 | |
jprovino@4165 | 30 | #if !INCLUDE_NMT |
jprovino@4165 | 31 | |
jprovino@4165 | 32 | #include "utilities/ostream.hpp" |
jprovino@4165 | 33 | |
jprovino@4165 | 34 | class BaselineOutputer : public StackObj { |
jprovino@4165 | 35 | |
jprovino@4165 | 36 | }; |
jprovino@4165 | 37 | |
jprovino@4165 | 38 | class BaselineTTYOutputer : public BaselineOutputer { |
jprovino@4165 | 39 | public: |
jprovino@4165 | 40 | BaselineTTYOutputer(outputStream* st) { } |
jprovino@4165 | 41 | }; |
jprovino@4165 | 42 | |
jprovino@4165 | 43 | class MemTracker : AllStatic { |
jprovino@4165 | 44 | public: |
jprovino@4165 | 45 | enum ShutdownReason { |
jprovino@4165 | 46 | NMT_shutdown_none, // no shutdown requested |
jprovino@4165 | 47 | NMT_shutdown_user, // user requested shutdown |
jprovino@4165 | 48 | NMT_normal, // normal shutdown, process exit |
jprovino@4165 | 49 | NMT_out_of_memory, // shutdown due to out of memory |
jprovino@4165 | 50 | NMT_initialization, // shutdown due to initialization failure |
jprovino@4165 | 51 | NMT_use_malloc_only, // can not combine NMT with UseMallocOnly flag |
jprovino@4165 | 52 | NMT_error_reporting, // shutdown by vmError::report_and_die() |
jprovino@4165 | 53 | NMT_out_of_generation, // running out of generation queue |
jprovino@4165 | 54 | NMT_sequence_overflow // overflow the sequence number |
jprovino@4165 | 55 | }; |
jprovino@4165 | 56 | |
zgu@5272 | 57 | class Tracker { |
zgu@5272 | 58 | public: |
zgu@5272 | 59 | void discard() { } |
zgu@5272 | 60 | |
zgu@5272 | 61 | void record(address addr, size_t size = 0, MEMFLAGS flags = mtNone, address pc = NULL) { } |
zgu@5272 | 62 | void record(address old_addr, address new_addr, size_t size, |
zgu@5272 | 63 | MEMFLAGS flags, address pc = NULL) { } |
zgu@5272 | 64 | }; |
zgu@5272 | 65 | |
zgu@5272 | 66 | private: |
zgu@5272 | 67 | static Tracker _tkr; |
zgu@5272 | 68 | |
jprovino@4165 | 69 | |
jprovino@4165 | 70 | public: |
jprovino@4165 | 71 | static inline void init_tracking_options(const char* option_line) { } |
jprovino@4165 | 72 | static inline bool is_on() { return false; } |
jprovino@4165 | 73 | static const char* reason() { return "Native memory tracking is not implemented"; } |
jprovino@4165 | 74 | static inline bool can_walk_stack() { return false; } |
jprovino@4165 | 75 | |
jprovino@4165 | 76 | static inline void bootstrap_single_thread() { } |
jprovino@4165 | 77 | static inline void bootstrap_multi_thread() { } |
jprovino@4165 | 78 | static inline void start() { } |
jprovino@4165 | 79 | |
jprovino@4165 | 80 | static inline void record_malloc(address addr, size_t size, MEMFLAGS flags, |
jprovino@4165 | 81 | address pc = 0, Thread* thread = NULL) { } |
jprovino@4165 | 82 | static inline void record_free(address addr, MEMFLAGS flags, Thread* thread = NULL) { } |
jprovino@4165 | 83 | static inline void record_arena_size(address addr, size_t size) { } |
jprovino@4165 | 84 | static inline void record_virtual_memory_reserve(address addr, size_t size, |
zgu@5272 | 85 | MEMFLAGS flags, address pc = 0, Thread* thread = NULL) { } |
zgu@5272 | 86 | static inline void record_virtual_memory_reserve_and_commit(address addr, size_t size, |
zgu@5272 | 87 | MEMFLAGS flags, address pc = 0, Thread* thread = NULL) { } |
jprovino@4165 | 88 | static inline void record_virtual_memory_commit(address addr, size_t size, |
jprovino@4165 | 89 | address pc = 0, Thread* thread = NULL) { } |
stefank@5578 | 90 | static inline void record_virtual_memory_release(address addr, size_t size, |
stefank@5578 | 91 | Thread* thread = NULL) { } |
jprovino@4165 | 92 | static inline void record_virtual_memory_type(address base, MEMFLAGS flags, |
jprovino@4165 | 93 | Thread* thread = NULL) { } |
zgu@5272 | 94 | static inline Tracker get_realloc_tracker() { return _tkr; } |
zgu@5272 | 95 | static inline Tracker get_virtual_memory_uncommit_tracker() { return _tkr; } |
zgu@5272 | 96 | static inline Tracker get_virtual_memory_release_tracker() { return _tkr; } |
jprovino@4165 | 97 | static inline bool baseline() { return false; } |
jprovino@4165 | 98 | static inline bool has_baseline() { return false; } |
jprovino@4165 | 99 | |
zgu@4810 | 100 | static inline void set_autoShutdown(bool value) { } |
jprovino@4165 | 101 | static void shutdown(ShutdownReason reason) { } |
zgu@4842 | 102 | static inline bool shutdown_in_progress() { return false; } |
jprovino@4165 | 103 | static bool print_memory_usage(BaselineOutputer& out, size_t unit, |
zgu@4842 | 104 | bool summary_only = true) { return false; } |
jprovino@4165 | 105 | static bool compare_memory_usage(BaselineOutputer& out, size_t unit, |
zgu@4842 | 106 | bool summary_only = true) { return false; } |
jprovino@4165 | 107 | |
zgu@4842 | 108 | static bool wbtest_wait_for_data_merge() { return false; } |
ctornqvi@4512 | 109 | |
jprovino@4165 | 110 | static inline void sync() { } |
jprovino@4165 | 111 | static inline void thread_exiting(JavaThread* thread) { } |
jprovino@4165 | 112 | }; |
jprovino@4165 | 113 | |
jprovino@4165 | 114 | |
jprovino@4165 | 115 | #else // !INCLUDE_NMT |
jprovino@4165 | 116 | |
zgu@3900 | 117 | #include "memory/allocation.hpp" |
zgu@3900 | 118 | #include "runtime/globals.hpp" |
zgu@3900 | 119 | #include "runtime/mutex.hpp" |
zgu@3900 | 120 | #include "runtime/os.hpp" |
zgu@3900 | 121 | #include "runtime/thread.hpp" |
zgu@3900 | 122 | #include "services/memPtr.hpp" |
zgu@3900 | 123 | #include "services/memRecorder.hpp" |
zgu@3900 | 124 | #include "services/memSnapshot.hpp" |
zgu@3900 | 125 | #include "services/memTrackWorker.hpp" |
zgu@3900 | 126 | |
zgu@4193 | 127 | extern bool NMT_track_callsite; |
zgu@4193 | 128 | |
ctornqvi@4512 | 129 | #ifndef MAX_UNSIGNED_LONG |
ctornqvi@4512 | 130 | #define MAX_UNSIGNED_LONG (unsigned long)(-1) |
ctornqvi@4512 | 131 | #endif |
ctornqvi@4512 | 132 | |
zgu@4193 | 133 | #ifdef ASSERT |
zgu@4193 | 134 | #define DEBUG_CALLER_PC (NMT_track_callsite ? os::get_caller_pc(2) : 0) |
zgu@3900 | 135 | #else |
zgu@3900 | 136 | #define DEBUG_CALLER_PC 0 |
zgu@3900 | 137 | #endif |
zgu@3900 | 138 | |
zgu@3900 | 139 | // The thread closure walks threads to collect per-thread |
zgu@3900 | 140 | // memory recorders at NMT sync point |
zgu@3900 | 141 | class SyncThreadRecorderClosure : public ThreadClosure { |
zgu@3900 | 142 | private: |
zgu@3900 | 143 | int _thread_count; |
zgu@3900 | 144 | |
zgu@3900 | 145 | public: |
zgu@3900 | 146 | SyncThreadRecorderClosure() { |
zgu@3900 | 147 | _thread_count =0; |
zgu@3900 | 148 | } |
zgu@3900 | 149 | |
zgu@3900 | 150 | void do_thread(Thread* thread); |
zgu@3900 | 151 | int get_thread_count() const { |
zgu@3900 | 152 | return _thread_count; |
zgu@3900 | 153 | } |
zgu@3900 | 154 | }; |
zgu@3900 | 155 | |
zgu@3900 | 156 | class BaselineOutputer; |
zgu@3900 | 157 | class MemSnapshot; |
zgu@3900 | 158 | class MemTrackWorker; |
zgu@3900 | 159 | class Thread; |
zgu@3900 | 160 | /* |
zgu@3900 | 161 | * MemTracker is the 'gate' class to native memory tracking runtime. |
zgu@3900 | 162 | */ |
zgu@3900 | 163 | class MemTracker : AllStatic { |
zgu@4400 | 164 | friend class GenerationData; |
zgu@3900 | 165 | friend class MemTrackWorker; |
zgu@3900 | 166 | friend class MemSnapshot; |
zgu@3900 | 167 | friend class SyncThreadRecorderClosure; |
zgu@3900 | 168 | |
zgu@3900 | 169 | // NMT state |
zgu@3900 | 170 | enum NMTStates { |
zgu@3900 | 171 | NMT_uninited, // not yet initialized |
zgu@3900 | 172 | NMT_bootstrapping_single_thread, // bootstrapping, VM is in single thread mode |
zgu@3900 | 173 | NMT_bootstrapping_multi_thread, // bootstrapping, VM is about to enter multi-thread mode |
zgu@3900 | 174 | NMT_started, // NMT fully started |
zgu@3900 | 175 | NMT_shutdown_pending, // shutdown pending |
zgu@3900 | 176 | NMT_final_shutdown, // in final phase of shutdown |
zgu@3900 | 177 | NMT_shutdown // shutdown |
zgu@3900 | 178 | }; |
zgu@3900 | 179 | |
zgu@4081 | 180 | public: |
zgu@5272 | 181 | class Tracker : public StackObj { |
zgu@5272 | 182 | friend class MemTracker; |
zgu@5272 | 183 | public: |
zgu@5272 | 184 | enum MemoryOperation { |
zgu@5272 | 185 | NoOp, // no op |
zgu@5272 | 186 | Malloc, // malloc |
zgu@5272 | 187 | Realloc, // realloc |
zgu@5272 | 188 | Free, // free |
zgu@5272 | 189 | Reserve, // virtual memory reserve |
zgu@5272 | 190 | Commit, // virtual memory commit |
zgu@5272 | 191 | ReserveAndCommit, // virtual memory reserve and commit |
zgu@5272 | 192 | StackAlloc = ReserveAndCommit, // allocate thread stack |
zgu@5272 | 193 | Type, // assign virtual memory type |
zgu@5272 | 194 | Uncommit, // virtual memory uncommit |
zgu@5272 | 195 | Release, // virtual memory release |
zgu@5272 | 196 | ArenaSize, // set arena size |
zgu@5272 | 197 | StackRelease // release thread stack |
zgu@5272 | 198 | }; |
zgu@5272 | 199 | |
zgu@5272 | 200 | |
zgu@5272 | 201 | protected: |
zgu@5272 | 202 | Tracker(MemoryOperation op, Thread* thr = NULL); |
zgu@5272 | 203 | |
zgu@5272 | 204 | public: |
zgu@5272 | 205 | void discard(); |
zgu@5272 | 206 | |
zgu@5272 | 207 | void record(address addr, size_t size = 0, MEMFLAGS flags = mtNone, address pc = NULL); |
zgu@5272 | 208 | void record(address old_addr, address new_addr, size_t size, |
zgu@5272 | 209 | MEMFLAGS flags, address pc = NULL); |
zgu@5272 | 210 | |
zgu@5272 | 211 | private: |
zgu@5272 | 212 | bool _need_thread_critical_lock; |
zgu@5272 | 213 | JavaThread* _java_thread; |
zgu@5272 | 214 | MemoryOperation _op; // memory operation |
zgu@5272 | 215 | jint _seq; // reserved sequence number |
zgu@5272 | 216 | }; |
zgu@5272 | 217 | |
zgu@5272 | 218 | |
zgu@5272 | 219 | public: |
zgu@3900 | 220 | // native memory tracking level |
zgu@3900 | 221 | enum NMTLevel { |
zgu@3900 | 222 | NMT_off, // native memory tracking is off |
zgu@3900 | 223 | NMT_summary, // don't track callsite |
zgu@3900 | 224 | NMT_detail // track callsite also |
zgu@3900 | 225 | }; |
zgu@3900 | 226 | |
zgu@3900 | 227 | enum ShutdownReason { |
zgu@3900 | 228 | NMT_shutdown_none, // no shutdown requested |
zgu@3900 | 229 | NMT_shutdown_user, // user requested shutdown |
zgu@3900 | 230 | NMT_normal, // normal shutdown, process exit |
zgu@3900 | 231 | NMT_out_of_memory, // shutdown due to out of memory |
zgu@3900 | 232 | NMT_initialization, // shutdown due to initialization failure |
zgu@3900 | 233 | NMT_use_malloc_only, // can not combine NMT with UseMallocOnly flag |
zgu@3900 | 234 | NMT_error_reporting, // shutdown by vmError::report_and_die() |
zgu@3900 | 235 | NMT_out_of_generation, // running out of generation queue |
zgu@3900 | 236 | NMT_sequence_overflow // overflow the sequence number |
zgu@3900 | 237 | }; |
zgu@3900 | 238 | |
zgu@3900 | 239 | public: |
zgu@3900 | 240 | // initialize NMT tracking level from command line options, called |
zgu@3900 | 241 | // from VM command line parsing code |
zgu@3900 | 242 | static void init_tracking_options(const char* option_line); |
zgu@3900 | 243 | |
zgu@3900 | 244 | // if NMT is enabled to record memory activities |
zgu@3900 | 245 | static inline bool is_on() { |
zgu@3900 | 246 | return (_tracking_level >= NMT_summary && |
zgu@3900 | 247 | _state >= NMT_bootstrapping_single_thread); |
zgu@3900 | 248 | } |
zgu@3900 | 249 | |
zgu@4081 | 250 | static inline enum NMTLevel tracking_level() { |
zgu@4081 | 251 | return _tracking_level; |
zgu@4081 | 252 | } |
zgu@4081 | 253 | |
zgu@3900 | 254 | // user readable reason for shutting down NMT |
zgu@3900 | 255 | static const char* reason() { |
zgu@3900 | 256 | switch(_reason) { |
zgu@3900 | 257 | case NMT_shutdown_none: |
zgu@3900 | 258 | return "Native memory tracking is not enabled"; |
zgu@3900 | 259 | case NMT_shutdown_user: |
zgu@3900 | 260 | return "Native memory tracking has been shutdown by user"; |
zgu@3900 | 261 | case NMT_normal: |
zgu@3900 | 262 | return "Native memory tracking has been shutdown due to process exiting"; |
zgu@3936 | 263 | case NMT_out_of_memory: |
zgu@3936 | 264 | return "Native memory tracking has been shutdown due to out of native memory"; |
zgu@3900 | 265 | case NMT_initialization: |
zgu@3900 | 266 | return "Native memory tracking failed to initialize"; |
zgu@3900 | 267 | case NMT_error_reporting: |
zgu@3900 | 268 | return "Native memory tracking has been shutdown due to error reporting"; |
zgu@3900 | 269 | case NMT_out_of_generation: |
zgu@3900 | 270 | return "Native memory tracking has been shutdown due to running out of generation buffer"; |
zgu@3900 | 271 | case NMT_sequence_overflow: |
zgu@3900 | 272 | return "Native memory tracking has been shutdown due to overflow the sequence number"; |
zgu@3900 | 273 | case NMT_use_malloc_only: |
zgu@3900 | 274 | return "Native memory tracking is not supported when UseMallocOnly is on"; |
zgu@3900 | 275 | default: |
zgu@3900 | 276 | ShouldNotReachHere(); |
zgu@3900 | 277 | return NULL; |
zgu@3900 | 278 | } |
zgu@3900 | 279 | } |
zgu@3900 | 280 | |
zgu@3900 | 281 | // test if we can walk native stack |
zgu@3900 | 282 | static bool can_walk_stack() { |
zgu@3900 | 283 | // native stack is not walkable during bootstrapping on sparc |
zgu@3900 | 284 | #if defined(SPARC) |
zgu@3900 | 285 | return (_state == NMT_started); |
zgu@3900 | 286 | #else |
zgu@3900 | 287 | return (_state >= NMT_bootstrapping_single_thread && _state <= NMT_started); |
zgu@3900 | 288 | #endif |
zgu@3900 | 289 | } |
zgu@3900 | 290 | |
zgu@3900 | 291 | // if native memory tracking tracks callsite |
zgu@3900 | 292 | static inline bool track_callsite() { return _tracking_level == NMT_detail; } |
zgu@3900 | 293 | |
zgu@4810 | 294 | // NMT automatically shuts itself down under extreme situation by default. |
zgu@4810 | 295 | // When the value is set to false, NMT will try its best to stay alive, |
zgu@4810 | 296 | // even it has to slow down VM. |
zgu@4810 | 297 | static inline void set_autoShutdown(bool value) { |
zgu@4810 | 298 | AutoShutdownNMT = value; |
zgu@4810 | 299 | if (AutoShutdownNMT && _slowdown_calling_thread) { |
zgu@4810 | 300 | _slowdown_calling_thread = false; |
zgu@4810 | 301 | } |
zgu@4810 | 302 | } |
zgu@4810 | 303 | |
zgu@3900 | 304 | // shutdown native memory tracking capability. Native memory tracking |
zgu@3900 | 305 | // can be shutdown by VM when it encounters low memory scenarios. |
zgu@3900 | 306 | // Memory tracker should gracefully shutdown itself, and preserve the |
zgu@3900 | 307 | // latest memory statistics for post morten diagnosis. |
zgu@3900 | 308 | static void shutdown(ShutdownReason reason); |
zgu@3900 | 309 | |
zgu@3900 | 310 | // if there is shutdown requested |
zgu@3900 | 311 | static inline bool shutdown_in_progress() { |
zgu@3900 | 312 | return (_state >= NMT_shutdown_pending); |
zgu@3900 | 313 | } |
zgu@3900 | 314 | |
zgu@3900 | 315 | // bootstrap native memory tracking, so it can start to collect raw data |
zgu@3900 | 316 | // before worker thread can start |
zgu@3900 | 317 | |
zgu@3900 | 318 | // the first phase of bootstrapping, when VM still in single-threaded mode |
zgu@3900 | 319 | static void bootstrap_single_thread(); |
zgu@3900 | 320 | // the second phase of bootstrapping, VM is about or already in multi-threaded mode |
zgu@3900 | 321 | static void bootstrap_multi_thread(); |
zgu@3900 | 322 | |
zgu@3900 | 323 | |
zgu@3900 | 324 | // start() has to be called when VM still in single thread mode, but after |
zgu@3900 | 325 | // command line option parsing is done. |
zgu@3900 | 326 | static void start(); |
zgu@3900 | 327 | |
zgu@3900 | 328 | // record a 'malloc' call |
zgu@3900 | 329 | static inline void record_malloc(address addr, size_t size, MEMFLAGS flags, |
zgu@3900 | 330 | address pc = 0, Thread* thread = NULL) { |
zgu@5272 | 331 | Tracker tkr(Tracker::Malloc, thread); |
zgu@5272 | 332 | tkr.record(addr, size, flags, pc); |
zgu@3900 | 333 | } |
zgu@3900 | 334 | // record a 'free' call |
zgu@3900 | 335 | static inline void record_free(address addr, MEMFLAGS flags, Thread* thread = NULL) { |
zgu@5272 | 336 | Tracker tkr(Tracker::Free, thread); |
zgu@5272 | 337 | tkr.record(addr, 0, flags, DEBUG_CALLER_PC); |
zgu@3900 | 338 | } |
zgu@3900 | 339 | |
zgu@3900 | 340 | static inline void record_arena_size(address addr, size_t size) { |
zgu@5272 | 341 | Tracker tkr(Tracker::ArenaSize); |
zgu@5272 | 342 | tkr.record(addr, size); |
zgu@3900 | 343 | } |
zgu@3900 | 344 | |
zgu@3900 | 345 | // record a virtual memory 'reserve' call |
zgu@3900 | 346 | static inline void record_virtual_memory_reserve(address addr, size_t size, |
zgu@5272 | 347 | MEMFLAGS flags, address pc = 0, Thread* thread = NULL) { |
zgu@5272 | 348 | assert(size > 0, "Sanity check"); |
zgu@5272 | 349 | Tracker tkr(Tracker::Reserve, thread); |
zgu@5272 | 350 | tkr.record(addr, size, flags, pc); |
zgu@3900 | 351 | } |
zgu@3900 | 352 | |
zgu@4053 | 353 | static inline void record_thread_stack(address addr, size_t size, Thread* thr, |
zgu@4053 | 354 | address pc = 0) { |
zgu@5272 | 355 | Tracker tkr(Tracker::StackAlloc, thr); |
zgu@5272 | 356 | tkr.record(addr, size, mtThreadStack, pc); |
zgu@4053 | 357 | } |
zgu@4053 | 358 | |
zgu@4053 | 359 | static inline void release_thread_stack(address addr, size_t size, Thread* thr) { |
zgu@5272 | 360 | Tracker tkr(Tracker::StackRelease, thr); |
zgu@5272 | 361 | tkr.record(addr, size, mtThreadStack, DEBUG_CALLER_PC); |
zgu@4053 | 362 | } |
zgu@4053 | 363 | |
zgu@3900 | 364 | // record a virtual memory 'commit' call |
zgu@3900 | 365 | static inline void record_virtual_memory_commit(address addr, size_t size, |
zgu@4193 | 366 | address pc, Thread* thread = NULL) { |
zgu@5272 | 367 | Tracker tkr(Tracker::Commit, thread); |
zgu@5272 | 368 | tkr.record(addr, size, mtNone, pc); |
zgu@3900 | 369 | } |
zgu@3900 | 370 | |
zgu@5272 | 371 | static inline void record_virtual_memory_reserve_and_commit(address addr, size_t size, |
zgu@5272 | 372 | MEMFLAGS flags, address pc, Thread* thread = NULL) { |
zgu@5272 | 373 | Tracker tkr(Tracker::ReserveAndCommit, thread); |
zgu@5272 | 374 | tkr.record(addr, size, flags, pc); |
zgu@3900 | 375 | } |
zgu@3900 | 376 | |
stefank@5578 | 377 | static inline void record_virtual_memory_release(address addr, size_t size, |
stefank@5578 | 378 | Thread* thread = NULL) { |
stefank@5578 | 379 | if (is_on()) { |
stefank@5578 | 380 | Tracker tkr(Tracker::Release, thread); |
stefank@5578 | 381 | tkr.record(addr, size); |
stefank@5578 | 382 | } |
stefank@5578 | 383 | } |
zgu@3900 | 384 | |
zgu@3900 | 385 | // record memory type on virtual memory base address |
zgu@3900 | 386 | static inline void record_virtual_memory_type(address base, MEMFLAGS flags, |
zgu@3900 | 387 | Thread* thread = NULL) { |
zgu@5272 | 388 | Tracker tkr(Tracker::Type); |
zgu@5272 | 389 | tkr.record(base, 0, flags); |
zgu@5272 | 390 | } |
zgu@5272 | 391 | |
zgu@5272 | 392 | // Get memory trackers for memory operations that can result race conditions. |
zgu@5272 | 393 | // The memory tracker has to be obtained before realloc, virtual memory uncommit |
zgu@5272 | 394 | // and virtual memory release, and call tracker.record() method if operation |
zgu@5272 | 395 | // succeeded, or tracker.discard() to abort the tracking. |
zgu@5272 | 396 | static inline Tracker get_realloc_tracker() { |
zgu@5272 | 397 | return Tracker(Tracker::Realloc); |
zgu@5272 | 398 | } |
zgu@5272 | 399 | |
zgu@5272 | 400 | static inline Tracker get_virtual_memory_uncommit_tracker() { |
zgu@5272 | 401 | return Tracker(Tracker::Uncommit); |
zgu@5272 | 402 | } |
zgu@5272 | 403 | |
zgu@5272 | 404 | static inline Tracker get_virtual_memory_release_tracker() { |
zgu@5272 | 405 | return Tracker(Tracker::Release); |
zgu@3900 | 406 | } |
zgu@3900 | 407 | |
zgu@3900 | 408 | |
zgu@3900 | 409 | // create memory baseline of current memory snapshot |
zgu@3900 | 410 | static bool baseline(); |
zgu@3900 | 411 | // is there a memory baseline |
zgu@3900 | 412 | static bool has_baseline() { |
zgu@3900 | 413 | return _baseline.baselined(); |
zgu@3900 | 414 | } |
zgu@3900 | 415 | |
zgu@3900 | 416 | // print memory usage from current snapshot |
zgu@3900 | 417 | static bool print_memory_usage(BaselineOutputer& out, size_t unit, |
zgu@3900 | 418 | bool summary_only = true); |
zgu@3900 | 419 | // compare memory usage between current snapshot and baseline |
zgu@3900 | 420 | static bool compare_memory_usage(BaselineOutputer& out, size_t unit, |
zgu@3900 | 421 | bool summary_only = true); |
zgu@3900 | 422 | |
ctornqvi@4512 | 423 | // the version for whitebox testing support, it ensures that all memory |
ctornqvi@4512 | 424 | // activities before this method call, are reflected in the snapshot |
ctornqvi@4512 | 425 | // database. |
ctornqvi@4512 | 426 | static bool wbtest_wait_for_data_merge(); |
ctornqvi@4512 | 427 | |
zgu@3900 | 428 | // sync is called within global safepoint to synchronize nmt data |
zgu@3900 | 429 | static void sync(); |
zgu@3900 | 430 | |
zgu@3900 | 431 | // called when a thread is about to exit |
zgu@3900 | 432 | static void thread_exiting(JavaThread* thread); |
zgu@3900 | 433 | |
zgu@3900 | 434 | // retrieve global snapshot |
zgu@3900 | 435 | static MemSnapshot* get_snapshot() { |
zgu@3900 | 436 | if (shutdown_in_progress()) { |
zgu@3900 | 437 | return NULL; |
zgu@3900 | 438 | } |
zgu@3900 | 439 | return _snapshot; |
zgu@3900 | 440 | } |
zgu@3900 | 441 | |
zgu@3900 | 442 | // print tracker stats |
zgu@3900 | 443 | NOT_PRODUCT(static void print_tracker_stats(outputStream* st);) |
zgu@3900 | 444 | NOT_PRODUCT(static void walk_stack(int toSkip, char* buf, int len);) |
zgu@3900 | 445 | |
zgu@3900 | 446 | private: |
zgu@3900 | 447 | // start native memory tracking worker thread |
zgu@4927 | 448 | static bool start_worker(MemSnapshot* snapshot); |
zgu@3900 | 449 | |
zgu@3900 | 450 | // called by worker thread to complete shutdown process |
zgu@3900 | 451 | static void final_shutdown(); |
zgu@3900 | 452 | |
zgu@3900 | 453 | protected: |
zgu@3900 | 454 | // retrieve per-thread recorder of the specified thread. |
zgu@3900 | 455 | // if the recorder is full, it will be enqueued to overflow |
zgu@3900 | 456 | // queue, a new recorder is acquired from recorder pool or a |
zgu@3900 | 457 | // new instance is created. |
zgu@3900 | 458 | // when thread == NULL, it means global recorder |
zgu@3900 | 459 | static MemRecorder* get_thread_recorder(JavaThread* thread); |
zgu@3900 | 460 | |
zgu@3900 | 461 | // per-thread recorder pool |
zgu@3900 | 462 | static void release_thread_recorder(MemRecorder* rec); |
zgu@3900 | 463 | static void delete_all_pooled_recorders(); |
zgu@3900 | 464 | |
zgu@3900 | 465 | // pending recorder queue. Recorders are queued to pending queue |
zgu@3900 | 466 | // when they are overflowed or collected at nmt sync point. |
zgu@3900 | 467 | static void enqueue_pending_recorder(MemRecorder* rec); |
zgu@3900 | 468 | static MemRecorder* get_pending_recorders(); |
zgu@3900 | 469 | static void delete_all_pending_recorders(); |
zgu@3900 | 470 | |
zgu@5272 | 471 | // write a memory tracking record in recorder |
zgu@5272 | 472 | static void write_tracking_record(address addr, MEMFLAGS type, |
zgu@5272 | 473 | size_t size, jint seq, address pc, JavaThread* thread); |
zgu@5272 | 474 | |
zgu@5272 | 475 | static bool is_single_threaded_bootstrap() { |
zgu@5272 | 476 | return _state == NMT_bootstrapping_single_thread; |
zgu@5272 | 477 | } |
zgu@5272 | 478 | |
zgu@5272 | 479 | static void check_NMT_load(Thread* thr) { |
zgu@5272 | 480 | assert(thr != NULL, "Sanity check"); |
zgu@5272 | 481 | if (_slowdown_calling_thread && thr != _worker_thread) { |
zgu@5363 | 482 | #ifdef _WINDOWS |
zgu@5363 | 483 | // On Windows, os::NakedYield() does not work as well |
zgu@5363 | 484 | // as os::yield_all() |
zgu@5272 | 485 | os::yield_all(); |
zgu@5363 | 486 | #else |
zgu@5363 | 487 | // On Solaris, os::yield_all() depends on os::sleep() |
zgu@5363 | 488 | // which requires JavaTherad in _thread_in_vm state. |
zgu@5363 | 489 | // Transits thread to _thread_in_vm state can be dangerous |
zgu@5363 | 490 | // if caller holds lock, as it may deadlock with Threads_lock. |
zgu@5363 | 491 | // So use NaKedYield instead. |
zgu@5363 | 492 | // |
zgu@5363 | 493 | // Linux and BSD, NakedYield() and yield_all() implementations |
zgu@5363 | 494 | // are the same. |
zgu@5363 | 495 | os::NakedYield(); |
zgu@5363 | 496 | #endif |
zgu@5272 | 497 | } |
zgu@5272 | 498 | } |
zgu@5272 | 499 | |
zgu@5272 | 500 | static void inc_pending_op_count() { |
zgu@5272 | 501 | Atomic::inc(&_pending_op_count); |
zgu@5272 | 502 | } |
zgu@5272 | 503 | |
zgu@5272 | 504 | static void dec_pending_op_count() { |
zgu@5272 | 505 | Atomic::dec(&_pending_op_count); |
zgu@5272 | 506 | assert(_pending_op_count >= 0, "Sanity check"); |
zgu@5272 | 507 | } |
zgu@5272 | 508 | |
zgu@5272 | 509 | |
zgu@3900 | 510 | private: |
zgu@3900 | 511 | // retrieve a pooled memory record or create new one if there is not |
zgu@3900 | 512 | // one available |
zgu@3900 | 513 | static MemRecorder* get_new_or_pooled_instance(); |
zgu@3900 | 514 | static void create_memory_record(address addr, MEMFLAGS type, |
zgu@3900 | 515 | size_t size, address pc, Thread* thread); |
zgu@3900 | 516 | static void create_record_in_recorder(address addr, MEMFLAGS type, |
zgu@3935 | 517 | size_t size, address pc, JavaThread* thread); |
zgu@3900 | 518 | |
ctornqvi@4512 | 519 | static void set_current_processing_generation(unsigned long generation) { |
ctornqvi@4512 | 520 | _worker_thread_idle = false; |
ctornqvi@4512 | 521 | _processing_generation = generation; |
ctornqvi@4512 | 522 | } |
ctornqvi@4512 | 523 | |
ctornqvi@4512 | 524 | static void report_worker_idle() { |
ctornqvi@4512 | 525 | _worker_thread_idle = true; |
ctornqvi@4512 | 526 | } |
ctornqvi@4512 | 527 | |
zgu@3900 | 528 | private: |
zgu@3900 | 529 | // global memory snapshot |
zgu@3900 | 530 | static MemSnapshot* _snapshot; |
zgu@3900 | 531 | |
zgu@3900 | 532 | // a memory baseline of snapshot |
zgu@3900 | 533 | static MemBaseline _baseline; |
zgu@3900 | 534 | |
zgu@3900 | 535 | // query lock |
zgu@3936 | 536 | static Mutex* _query_lock; |
zgu@3900 | 537 | |
zgu@3900 | 538 | // a thread can start to allocate memory before it is attached |
zgu@3900 | 539 | // to VM 'Thread', those memory activities are recorded here. |
zgu@3900 | 540 | // ThreadCritical is required to guard this global recorder. |
zgu@4927 | 541 | static MemRecorder* volatile _global_recorder; |
zgu@3900 | 542 | |
zgu@3900 | 543 | // main thread id |
zgu@3900 | 544 | debug_only(static intx _main_thread_tid;) |
zgu@3900 | 545 | |
zgu@3900 | 546 | // pending recorders to be merged |
zgu@4927 | 547 | static MemRecorder* volatile _merge_pending_queue; |
zgu@3900 | 548 | |
zgu@3900 | 549 | NOT_PRODUCT(static volatile jint _pending_recorder_count;) |
zgu@3900 | 550 | |
zgu@3900 | 551 | // pooled memory recorders |
zgu@4927 | 552 | static MemRecorder* volatile _pooled_recorders; |
zgu@3900 | 553 | |
zgu@3900 | 554 | // memory recorder pool management, uses following |
zgu@3900 | 555 | // counter to determine if a released memory recorder |
zgu@3900 | 556 | // should be pooled |
zgu@3900 | 557 | |
zgu@3900 | 558 | // latest thread count |
zgu@3900 | 559 | static int _thread_count; |
zgu@3900 | 560 | // pooled recorder count |
zgu@3900 | 561 | static volatile jint _pooled_recorder_count; |
zgu@3900 | 562 | |
zgu@3900 | 563 | |
zgu@3900 | 564 | // worker thread to merge pending recorders into snapshot |
zgu@3900 | 565 | static MemTrackWorker* _worker_thread; |
zgu@3900 | 566 | |
zgu@3900 | 567 | // how many safepoints we skipped without entering sync point |
zgu@3900 | 568 | static int _sync_point_skip_count; |
zgu@3900 | 569 | |
zgu@3900 | 570 | // if the tracker is properly intialized |
zgu@3900 | 571 | static bool _is_tracker_ready; |
zgu@3900 | 572 | // tracking level (off, summary and detail) |
zgu@3900 | 573 | static enum NMTLevel _tracking_level; |
zgu@3900 | 574 | |
zgu@3900 | 575 | // current nmt state |
zgu@3900 | 576 | static volatile enum NMTStates _state; |
zgu@3900 | 577 | // the reason for shutting down nmt |
zgu@3900 | 578 | static enum ShutdownReason _reason; |
ctornqvi@4512 | 579 | // the generation that NMT is processing |
ctornqvi@4512 | 580 | static volatile unsigned long _processing_generation; |
ctornqvi@4512 | 581 | // although NMT is still procesing current generation, but |
ctornqvi@4512 | 582 | // there is not more recorder to process, set idle state |
ctornqvi@4512 | 583 | static volatile bool _worker_thread_idle; |
zgu@4810 | 584 | |
zgu@4810 | 585 | // if NMT should slow down calling thread to allow |
zgu@4810 | 586 | // worker thread to catch up |
zgu@4810 | 587 | static volatile bool _slowdown_calling_thread; |
zgu@5272 | 588 | |
zgu@5272 | 589 | // pending memory op count. |
zgu@5272 | 590 | // Certain memory ops need to pre-reserve sequence number |
zgu@5272 | 591 | // before memory operation can happen to avoid race condition. |
zgu@5272 | 592 | // See MemTracker::Tracker for detail |
zgu@5272 | 593 | static volatile jint _pending_op_count; |
zgu@3900 | 594 | }; |
zgu@3900 | 595 | |
jprovino@4165 | 596 | #endif // !INCLUDE_NMT |
jprovino@4165 | 597 | |
zgu@3900 | 598 | #endif // SHARE_VM_SERVICES_MEM_TRACKER_HPP |