author | zgu |
Mon, 05 Feb 2018 13:23:26 -0500 | |
changeset 48865 | 53427ddce0a0 |
parent 47216 | 71c04702a3d5 |
child 48872 | c7774afc93e3 |
permissions | -rw-r--r-- |
13195 | 1 |
/* |
48865
53427ddce0a0
8194934: NMT: Remove MemTracker::get_virtual_memory_xxx_tracker(), create Tracker in place
zgu
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved. |
13195 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
22 |
* |
|
23 |
*/ |
|
24 |
||
25 |
#ifndef SHARE_VM_SERVICES_MEM_TRACKER_HPP |
|
26 |
#define SHARE_VM_SERVICES_MEM_TRACKER_HPP |
|
27 |
||
25946 | 28 |
#include "services/nmtCommon.hpp" |
26144 | 29 |
#include "utilities/nativeCallStack.hpp" |
25946 | 30 |
|
13975
2f7431485cfa
7189254: Change makefiles for more flexibility to override defaults
jprovino
parents:
13861
diff
changeset
|
31 |
|
2f7431485cfa
7189254: Change makefiles for more flexibility to override defaults
jprovino
parents:
13861
diff
changeset
|
32 |
#if !INCLUDE_NMT |
2f7431485cfa
7189254: Change makefiles for more flexibility to override defaults
jprovino
parents:
13861
diff
changeset
|
33 |
|
26144 | 34 |
#define CURRENT_PC NativeCallStack::EMPTY_STACK |
35 |
#define CALLER_PC NativeCallStack::EMPTY_STACK |
|
13975
2f7431485cfa
7189254: Change makefiles for more flexibility to override defaults
jprovino
parents:
13861
diff
changeset
|
36 |
|
25946 | 37 |
class Tracker : public StackObj { |
38 |
public: |
|
39 |
Tracker() { } |
|
40 |
void record(address addr, size_t size) { } |
|
13975
2f7431485cfa
7189254: Change makefiles for more flexibility to override defaults
jprovino
parents:
13861
diff
changeset
|
41 |
}; |
2f7431485cfa
7189254: Change makefiles for more flexibility to override defaults
jprovino
parents:
13861
diff
changeset
|
42 |
|
25946 | 43 |
class MemTracker : AllStatic { |
44 |
public: |
|
45 |
static inline NMT_TrackingLevel tracking_level() { return NMT_off; } |
|
46 |
static inline void shutdown() { } |
|
47 |
static inline void init() { } |
|
48 |
static bool check_launcher_nmt_support(const char* value) { return true; } |
|
49 |
static bool verify_nmt_option() { return true; } |
|
50 |
||
51 |
static inline void* record_malloc(void* mem_base, size_t size, MEMFLAGS flag, |
|
52 |
const NativeCallStack& stack, NMT_TrackingLevel level) { return mem_base; } |
|
53 |
static inline size_t malloc_header_size(NMT_TrackingLevel level) { return 0; } |
|
54 |
static inline size_t malloc_header_size(void* memblock) { return 0; } |
|
55 |
static inline void* malloc_base(void* memblock) { return memblock; } |
|
56 |
static inline void* record_free(void* memblock) { return memblock; } |
|
57 |
||
58 |
static inline void record_new_arena(MEMFLAGS flag) { } |
|
59 |
static inline void record_arena_free(MEMFLAGS flag) { } |
|
60 |
static inline void record_arena_size_change(int diff, MEMFLAGS flag) { } |
|
61 |
static inline void record_virtual_memory_reserve(void* addr, size_t size, const NativeCallStack& stack, |
|
62 |
MEMFLAGS flag = mtNone) { } |
|
63 |
static inline void record_virtual_memory_reserve_and_commit(void* addr, size_t size, |
|
64 |
const NativeCallStack& stack, MEMFLAGS flag = mtNone) { } |
|
65 |
static inline void record_virtual_memory_commit(void* addr, size_t size, const NativeCallStack& stack) { } |
|
66 |
static inline void record_virtual_memory_type(void* addr, MEMFLAGS flag) { } |
|
67 |
static inline void record_thread_stack(void* addr, size_t size) { } |
|
68 |
static inline void release_thread_stack(void* addr, size_t size) { } |
|
69 |
||
70 |
static void final_report(outputStream*) { } |
|
27162
0a4a7276949b
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
26144
diff
changeset
|
71 |
static void error_report(outputStream*) { } |
25946 | 72 |
}; |
73 |
||
74 |
#else |
|
75 |
||
76 |
#include "runtime/threadCritical.hpp" |
|
77 |
#include "services/mallocTracker.hpp" |
|
78 |
#include "services/virtualMemoryTracker.hpp" |
|
79 |
||
80 |
extern volatile bool NMT_stack_walkable; |
|
81 |
||
82 |
#define CURRENT_PC ((MemTracker::tracking_level() == NMT_detail && NMT_stack_walkable) ? \ |
|
26144 | 83 |
NativeCallStack(0, true) : NativeCallStack::EMPTY_STACK) |
25946 | 84 |
#define CALLER_PC ((MemTracker::tracking_level() == NMT_detail && NMT_stack_walkable) ? \ |
26144 | 85 |
NativeCallStack(1, true) : NativeCallStack::EMPTY_STACK) |
25946 | 86 |
|
87 |
class MemBaseline; |
|
88 |
class Mutex; |
|
89 |
||
90 |
// Tracker is used for guarding 'release' semantics of virtual memory operation, to avoid |
|
91 |
// the other thread obtains and records the same region that is just 'released' by current |
|
92 |
// thread but before it can record the operation. |
|
93 |
class Tracker : public StackObj { |
|
94 |
public: |
|
95 |
enum TrackerType { |
|
96 |
uncommit, |
|
97 |
release |
|
98 |
}; |
|
99 |
||
100 |
public: |
|
101 |
Tracker(enum TrackerType type) : _type(type) { } |
|
102 |
void record(address addr, size_t size); |
|
103 |
private: |
|
104 |
enum TrackerType _type; |
|
105 |
// Virtual memory tracking data structures are protected by ThreadCritical lock. |
|
106 |
ThreadCritical _tc; |
|
13975
2f7431485cfa
7189254: Change makefiles for more flexibility to override defaults
jprovino
parents:
13861
diff
changeset
|
107 |
}; |
2f7431485cfa
7189254: Change makefiles for more flexibility to override defaults
jprovino
parents:
13861
diff
changeset
|
108 |
|
2f7431485cfa
7189254: Change makefiles for more flexibility to override defaults
jprovino
parents:
13861
diff
changeset
|
109 |
class MemTracker : AllStatic { |
13195 | 110 |
public: |
25946 | 111 |
static inline NMT_TrackingLevel tracking_level() { |
112 |
if (_tracking_level == NMT_unknown) { |
|
113 |
// No fencing is needed here, since JVM is in single-threaded |
|
114 |
// mode. |
|
115 |
_tracking_level = init_tracking_level(); |
|
116 |
_cmdline_tracking_level = _tracking_level; |
|
117 |
} |
|
13861
619cff4bfb8b
7188594: Print statistic collected by NMT with VM flag
zgu
parents:
13859
diff
changeset
|
118 |
return _tracking_level; |
619cff4bfb8b
7188594: Print statistic collected by NMT with VM flag
zgu
parents:
13859
diff
changeset
|
119 |
} |
619cff4bfb8b
7188594: Print statistic collected by NMT with VM flag
zgu
parents:
13859
diff
changeset
|
120 |
|
25946 | 121 |
// A late initialization, for the stuff(s) can not be |
122 |
// done in init_tracking_level(), which can NOT malloc |
|
123 |
// any memory. |
|
124 |
static void init(); |
|
125 |
||
126 |
// Shutdown native memory tracking |
|
127 |
static void shutdown(); |
|
128 |
||
129 |
// Verify native memory tracking command line option. |
|
130 |
// This check allows JVM to detect if compatible launcher |
|
131 |
// is used. |
|
132 |
// If an incompatible launcher is used, NMT may not be |
|
133 |
// able to start, even it is enabled by command line option. |
|
134 |
// A warning message should be given if it is encountered. |
|
135 |
static bool check_launcher_nmt_support(const char* value); |
|
136 |
||
137 |
// This method checks native memory tracking environment |
|
138 |
// variable value passed by launcher. |
|
139 |
// Launcher only obligated to pass native memory tracking |
|
140 |
// option value, but not obligated to validate the value, |
|
141 |
// and launcher has option to discard native memory tracking |
|
142 |
// option from the command line once it sets up the environment |
|
143 |
// variable, so NMT has to catch the bad value here. |
|
144 |
static bool verify_nmt_option(); |
|
145 |
||
146 |
// Transition the tracking level to specified level |
|
147 |
static bool transition_to(NMT_TrackingLevel level); |
|
148 |
||
149 |
static inline void* record_malloc(void* mem_base, size_t size, MEMFLAGS flag, |
|
150 |
const NativeCallStack& stack, NMT_TrackingLevel level) { |
|
151 |
return MallocTracker::record_malloc(mem_base, size, flag, stack, level); |
|
152 |
} |
|
153 |
||
154 |
static inline size_t malloc_header_size(NMT_TrackingLevel level) { |
|
155 |
return MallocTracker::malloc_header_size(level); |
|
13195 | 156 |
} |
157 |
||
25946 | 158 |
static size_t malloc_header_size(void* memblock) { |
159 |
if (tracking_level() != NMT_off) { |
|
160 |
return MallocTracker::get_header_size(memblock); |
|
161 |
} |
|
162 |
return 0; |
|
163 |
} |
|
164 |
||
165 |
// To malloc base address, which is the starting address |
|
166 |
// of malloc tracking header if tracking is enabled. |
|
167 |
// Otherwise, it returns the same address. |
|
168 |
static void* malloc_base(void* memblock); |
|
169 |
||
170 |
// Record malloc free and return malloc base address |
|
171 |
static inline void* record_free(void* memblock) { |
|
172 |
return MallocTracker::record_free(memblock); |
|
13195 | 173 |
} |
174 |
||
25946 | 175 |
|
176 |
// Record creation of an arena |
|
177 |
static inline void record_new_arena(MEMFLAGS flag) { |
|
178 |
if (tracking_level() < NMT_summary) return; |
|
179 |
MallocTracker::record_new_arena(flag); |
|
180 |
} |
|
181 |
||
182 |
// Record destruction of an arena |
|
183 |
static inline void record_arena_free(MEMFLAGS flag) { |
|
184 |
if (tracking_level() < NMT_summary) return; |
|
185 |
MallocTracker::record_arena_free(flag); |
|
186 |
} |
|
13195 | 187 |
|
25946 | 188 |
// Record arena size change. Arena size is the size of all arena |
189 |
// chuncks that backing up the arena. |
|
190 |
static inline void record_arena_size_change(int diff, MEMFLAGS flag) { |
|
191 |
if (tracking_level() < NMT_summary) return; |
|
192 |
MallocTracker::record_arena_size_change(diff, flag); |
|
193 |
} |
|
194 |
||
195 |
static inline void record_virtual_memory_reserve(void* addr, size_t size, const NativeCallStack& stack, |
|
196 |
MEMFLAGS flag = mtNone) { |
|
197 |
if (tracking_level() < NMT_summary) return; |
|
198 |
if (addr != NULL) { |
|
199 |
ThreadCritical tc; |
|
200 |
// Recheck to avoid potential racing during NMT shutdown |
|
201 |
if (tracking_level() < NMT_summary) return; |
|
202 |
VirtualMemoryTracker::add_reserved_region((address)addr, size, stack, flag); |
|
16436
6b67e51e3cfb
8009298: NMT: Special version of class loading/unloading with runThese stresses out NMT
zgu
parents:
15452
diff
changeset
|
203 |
} |
6b67e51e3cfb
8009298: NMT: Special version of class loading/unloading with runThese stresses out NMT
zgu
parents:
15452
diff
changeset
|
204 |
} |
6b67e51e3cfb
8009298: NMT: Special version of class loading/unloading with runThese stresses out NMT
zgu
parents:
15452
diff
changeset
|
205 |
|
25946 | 206 |
static inline void record_virtual_memory_reserve_and_commit(void* addr, size_t size, |
207 |
const NativeCallStack& stack, MEMFLAGS flag = mtNone) { |
|
208 |
if (tracking_level() < NMT_summary) return; |
|
209 |
if (addr != NULL) { |
|
210 |
ThreadCritical tc; |
|
211 |
if (tracking_level() < NMT_summary) return; |
|
212 |
VirtualMemoryTracker::add_reserved_region((address)addr, size, |
|
213 |
stack, flag, true); |
|
46976
6d4058ac744f
8186748: NMT: memTracker::record_virtual_memory_reserve_and_commit() does not tag the memory as committed
zgu
parents:
40655
diff
changeset
|
214 |
VirtualMemoryTracker::add_committed_region((address)addr, size, stack); |
19546
f6b7c9e96ea3
8007074: SIGSEGV at ParMarkBitMap::verify_clear()
stefank
parents:
18681
diff
changeset
|
215 |
} |
f6b7c9e96ea3
8007074: SIGSEGV at ParMarkBitMap::verify_clear()
stefank
parents:
18681
diff
changeset
|
216 |
} |
13195 | 217 |
|
25946 | 218 |
static inline void record_virtual_memory_commit(void* addr, size_t size, |
219 |
const NativeCallStack& stack) { |
|
220 |
if (tracking_level() < NMT_summary) return; |
|
221 |
if (addr != NULL) { |
|
222 |
ThreadCritical tc; |
|
223 |
if (tracking_level() < NMT_summary) return; |
|
224 |
VirtualMemoryTracker::add_committed_region((address)addr, size, stack); |
|
225 |
} |
|
18086
f44cf213a775
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
16991
diff
changeset
|
226 |
} |
f44cf213a775
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
16991
diff
changeset
|
227 |
|
25946 | 228 |
static inline void record_virtual_memory_type(void* addr, MEMFLAGS flag) { |
229 |
if (tracking_level() < NMT_summary) return; |
|
230 |
if (addr != NULL) { |
|
231 |
ThreadCritical tc; |
|
232 |
if (tracking_level() < NMT_summary) return; |
|
233 |
VirtualMemoryTracker::set_reserved_region_type((address)addr, flag); |
|
13195 | 234 |
} |
235 |
} |
|
236 |
||
25946 | 237 |
static inline void record_thread_stack(void* addr, size_t size) { |
238 |
if (tracking_level() < NMT_summary) return; |
|
239 |
if (addr != NULL) { |
|
240 |
// uses thread stack malloc slot for book keeping number of threads |
|
241 |
MallocMemorySummary::record_malloc(0, mtThreadStack); |
|
242 |
record_virtual_memory_reserve_and_commit(addr, size, CALLER_PC, mtThreadStack); |
|
243 |
} |
|
18086
f44cf213a775
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
16991
diff
changeset
|
244 |
} |
f44cf213a775
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
16991
diff
changeset
|
245 |
|
25946 | 246 |
static inline void release_thread_stack(void* addr, size_t size) { |
247 |
if (tracking_level() < NMT_summary) return; |
|
248 |
if (addr != NULL) { |
|
249 |
// uses thread stack malloc slot for book keeping number of threads |
|
250 |
MallocMemorySummary::record_free(0, mtThreadStack); |
|
251 |
ThreadCritical tc; |
|
252 |
if (tracking_level() < NMT_summary) return; |
|
253 |
VirtualMemoryTracker::remove_released_region((address)addr, size); |
|
18086
f44cf213a775
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
16991
diff
changeset
|
254 |
} |
f44cf213a775
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
16991
diff
changeset
|
255 |
} |
f44cf213a775
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
16991
diff
changeset
|
256 |
|
25946 | 257 |
// Query lock is used to synchronize the access to tracking data. |
258 |
// So far, it is only used by JCmd query, but it may be used by |
|
259 |
// other tools. |
|
260 |
static inline Mutex* query_lock() { return _query_lock; } |
|
18086
f44cf213a775
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
16991
diff
changeset
|
261 |
|
27162
0a4a7276949b
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
26144
diff
changeset
|
262 |
// Make a final report or report for hs_err file. |
0a4a7276949b
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
26144
diff
changeset
|
263 |
static void error_report(outputStream* output) { |
0a4a7276949b
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
26144
diff
changeset
|
264 |
if (tracking_level() >= NMT_summary) { |
0a4a7276949b
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
26144
diff
changeset
|
265 |
report(true, output); // just print summary for error case. |
0a4a7276949b
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
26144
diff
changeset
|
266 |
} |
0a4a7276949b
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
26144
diff
changeset
|
267 |
} |
0a4a7276949b
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
26144
diff
changeset
|
268 |
|
0a4a7276949b
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
26144
diff
changeset
|
269 |
static void final_report(outputStream* output) { |
0a4a7276949b
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
26144
diff
changeset
|
270 |
NMT_TrackingLevel level = tracking_level(); |
0a4a7276949b
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
26144
diff
changeset
|
271 |
if (level >= NMT_summary) { |
0a4a7276949b
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
26144
diff
changeset
|
272 |
report(level == NMT_summary, output); |
0a4a7276949b
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
26144
diff
changeset
|
273 |
} |
0a4a7276949b
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
26144
diff
changeset
|
274 |
} |
0a4a7276949b
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
26144
diff
changeset
|
275 |
|
25946 | 276 |
|
277 |
// Stored baseline |
|
278 |
static inline MemBaseline& get_baseline() { |
|
279 |
return _baseline; |
|
18086
f44cf213a775
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
16991
diff
changeset
|
280 |
} |
f44cf213a775
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
16991
diff
changeset
|
281 |
|
25946 | 282 |
static NMT_TrackingLevel cmdline_tracking_level() { |
283 |
return _cmdline_tracking_level; |
|
15452
3bfde2dea09d
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
15104
diff
changeset
|
284 |
} |
3bfde2dea09d
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
15104
diff
changeset
|
285 |
|
25946 | 286 |
static void tuning_statistics(outputStream* out); |
15452
3bfde2dea09d
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
15104
diff
changeset
|
287 |
|
13195 | 288 |
private: |
25946 | 289 |
static NMT_TrackingLevel init_tracking_level(); |
27162
0a4a7276949b
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
26144
diff
changeset
|
290 |
static void report(bool summary_only, outputStream* output); |
13195 | 291 |
|
25946 | 292 |
private: |
293 |
// Tracking level |
|
294 |
static volatile NMT_TrackingLevel _tracking_level; |
|
295 |
// If NMT option value passed by launcher through environment |
|
296 |
// variable is valid |
|
297 |
static bool _is_nmt_env_valid; |
|
298 |
// command line tracking level |
|
299 |
static NMT_TrackingLevel _cmdline_tracking_level; |
|
300 |
// Stored baseline |
|
301 |
static MemBaseline _baseline; |
|
302 |
// Query lock |
|
303 |
static Mutex* _query_lock; |
|
13195 | 304 |
}; |
305 |
||
25946 | 306 |
#endif // INCLUDE_NMT |
13975
2f7431485cfa
7189254: Change makefiles for more flexibility to override defaults
jprovino
parents:
13861
diff
changeset
|
307 |
|
13195 | 308 |
#endif // SHARE_VM_SERVICES_MEM_TRACKER_HPP |
25946 | 309 |