author | stefank |
Wed, 11 Apr 2018 13:52:23 +0200 | |
changeset 49746 | b17256b5c047 |
parent 49349 | 7194eb9e8f19 |
child 49968 | 33a76b934213 |
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: |
|
48872 | 39 |
enum TrackerType { |
40 |
uncommit, |
|
41 |
release |
|
42 |
}; |
|
43 |
Tracker(enum TrackerType type) : _type(type) { } |
|
49746 | 44 |
void record(address addr, size_t size) { } |
48872 | 45 |
private: |
46 |
enum TrackerType _type; |
|
13975
2f7431485cfa
7189254: Change makefiles for more flexibility to override defaults
jprovino
parents:
13861
diff
changeset
|
47 |
}; |
2f7431485cfa
7189254: Change makefiles for more flexibility to override defaults
jprovino
parents:
13861
diff
changeset
|
48 |
|
25946 | 49 |
class MemTracker : AllStatic { |
50 |
public: |
|
51 |
static inline NMT_TrackingLevel tracking_level() { return NMT_off; } |
|
52 |
static inline void shutdown() { } |
|
53 |
static inline void init() { } |
|
54 |
static bool check_launcher_nmt_support(const char* value) { return true; } |
|
55 |
static bool verify_nmt_option() { return true; } |
|
56 |
||
57 |
static inline void* record_malloc(void* mem_base, size_t size, MEMFLAGS flag, |
|
58 |
const NativeCallStack& stack, NMT_TrackingLevel level) { return mem_base; } |
|
59 |
static inline size_t malloc_header_size(NMT_TrackingLevel level) { return 0; } |
|
60 |
static inline size_t malloc_header_size(void* memblock) { return 0; } |
|
61 |
static inline void* malloc_base(void* memblock) { return memblock; } |
|
62 |
static inline void* record_free(void* memblock) { return memblock; } |
|
63 |
||
64 |
static inline void record_new_arena(MEMFLAGS flag) { } |
|
65 |
static inline void record_arena_free(MEMFLAGS flag) { } |
|
66 |
static inline void record_arena_size_change(int diff, MEMFLAGS flag) { } |
|
67 |
static inline void record_virtual_memory_reserve(void* addr, size_t size, const NativeCallStack& stack, |
|
68 |
MEMFLAGS flag = mtNone) { } |
|
69 |
static inline void record_virtual_memory_reserve_and_commit(void* addr, size_t size, |
|
70 |
const NativeCallStack& stack, MEMFLAGS flag = mtNone) { } |
|
71 |
static inline void record_virtual_memory_commit(void* addr, size_t size, const NativeCallStack& stack) { } |
|
72 |
static inline void record_virtual_memory_type(void* addr, MEMFLAGS flag) { } |
|
73 |
static inline void record_thread_stack(void* addr, size_t size) { } |
|
74 |
static inline void release_thread_stack(void* addr, size_t size) { } |
|
75 |
||
76 |
static void final_report(outputStream*) { } |
|
27162
0a4a7276949b
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
26144
diff
changeset
|
77 |
static void error_report(outputStream*) { } |
25946 | 78 |
}; |
79 |
||
80 |
#else |
|
81 |
||
82 |
#include "runtime/threadCritical.hpp" |
|
83 |
#include "services/mallocTracker.hpp" |
|
84 |
#include "services/virtualMemoryTracker.hpp" |
|
85 |
||
86 |
extern volatile bool NMT_stack_walkable; |
|
87 |
||
88 |
#define CURRENT_PC ((MemTracker::tracking_level() == NMT_detail && NMT_stack_walkable) ? \ |
|
26144 | 89 |
NativeCallStack(0, true) : NativeCallStack::EMPTY_STACK) |
25946 | 90 |
#define CALLER_PC ((MemTracker::tracking_level() == NMT_detail && NMT_stack_walkable) ? \ |
26144 | 91 |
NativeCallStack(1, true) : NativeCallStack::EMPTY_STACK) |
25946 | 92 |
|
93 |
class MemBaseline; |
|
94 |
class Mutex; |
|
95 |
||
96 |
// Tracker is used for guarding 'release' semantics of virtual memory operation, to avoid |
|
97 |
// the other thread obtains and records the same region that is just 'released' by current |
|
98 |
// thread but before it can record the operation. |
|
99 |
class Tracker : public StackObj { |
|
100 |
public: |
|
101 |
enum TrackerType { |
|
102 |
uncommit, |
|
103 |
release |
|
104 |
}; |
|
105 |
||
106 |
public: |
|
107 |
Tracker(enum TrackerType type) : _type(type) { } |
|
108 |
void record(address addr, size_t size); |
|
109 |
private: |
|
110 |
enum TrackerType _type; |
|
111 |
// Virtual memory tracking data structures are protected by ThreadCritical lock. |
|
112 |
ThreadCritical _tc; |
|
13975
2f7431485cfa
7189254: Change makefiles for more flexibility to override defaults
jprovino
parents:
13861
diff
changeset
|
113 |
}; |
2f7431485cfa
7189254: Change makefiles for more flexibility to override defaults
jprovino
parents:
13861
diff
changeset
|
114 |
|
2f7431485cfa
7189254: Change makefiles for more flexibility to override defaults
jprovino
parents:
13861
diff
changeset
|
115 |
class MemTracker : AllStatic { |
49033
3acc342c0738
8196405: [REDO] NMT: add_committed_regions doesn't merge succeeding regions
stefank
parents:
48872
diff
changeset
|
116 |
friend class VirtualMemoryTrackerTest; |
3acc342c0738
8196405: [REDO] NMT: add_committed_regions doesn't merge succeeding regions
stefank
parents:
48872
diff
changeset
|
117 |
|
13195 | 118 |
public: |
25946 | 119 |
static inline NMT_TrackingLevel tracking_level() { |
120 |
if (_tracking_level == NMT_unknown) { |
|
121 |
// No fencing is needed here, since JVM is in single-threaded |
|
122 |
// mode. |
|
123 |
_tracking_level = init_tracking_level(); |
|
124 |
_cmdline_tracking_level = _tracking_level; |
|
125 |
} |
|
13861
619cff4bfb8b
7188594: Print statistic collected by NMT with VM flag
zgu
parents:
13859
diff
changeset
|
126 |
return _tracking_level; |
619cff4bfb8b
7188594: Print statistic collected by NMT with VM flag
zgu
parents:
13859
diff
changeset
|
127 |
} |
619cff4bfb8b
7188594: Print statistic collected by NMT with VM flag
zgu
parents:
13859
diff
changeset
|
128 |
|
25946 | 129 |
// A late initialization, for the stuff(s) can not be |
130 |
// done in init_tracking_level(), which can NOT malloc |
|
131 |
// any memory. |
|
132 |
static void init(); |
|
133 |
||
134 |
// Shutdown native memory tracking |
|
135 |
static void shutdown(); |
|
136 |
||
137 |
// Verify native memory tracking command line option. |
|
138 |
// This check allows JVM to detect if compatible launcher |
|
139 |
// is used. |
|
140 |
// If an incompatible launcher is used, NMT may not be |
|
141 |
// able to start, even it is enabled by command line option. |
|
142 |
// A warning message should be given if it is encountered. |
|
143 |
static bool check_launcher_nmt_support(const char* value); |
|
144 |
||
145 |
// This method checks native memory tracking environment |
|
146 |
// variable value passed by launcher. |
|
147 |
// Launcher only obligated to pass native memory tracking |
|
148 |
// option value, but not obligated to validate the value, |
|
149 |
// and launcher has option to discard native memory tracking |
|
150 |
// option from the command line once it sets up the environment |
|
151 |
// variable, so NMT has to catch the bad value here. |
|
152 |
static bool verify_nmt_option(); |
|
153 |
||
154 |
// Transition the tracking level to specified level |
|
155 |
static bool transition_to(NMT_TrackingLevel level); |
|
156 |
||
157 |
static inline void* record_malloc(void* mem_base, size_t size, MEMFLAGS flag, |
|
158 |
const NativeCallStack& stack, NMT_TrackingLevel level) { |
|
159 |
return MallocTracker::record_malloc(mem_base, size, flag, stack, level); |
|
160 |
} |
|
161 |
||
162 |
static inline size_t malloc_header_size(NMT_TrackingLevel level) { |
|
163 |
return MallocTracker::malloc_header_size(level); |
|
13195 | 164 |
} |
165 |
||
25946 | 166 |
static size_t malloc_header_size(void* memblock) { |
167 |
if (tracking_level() != NMT_off) { |
|
168 |
return MallocTracker::get_header_size(memblock); |
|
169 |
} |
|
170 |
return 0; |
|
171 |
} |
|
172 |
||
173 |
// To malloc base address, which is the starting address |
|
174 |
// of malloc tracking header if tracking is enabled. |
|
175 |
// Otherwise, it returns the same address. |
|
176 |
static void* malloc_base(void* memblock); |
|
177 |
||
178 |
// Record malloc free and return malloc base address |
|
179 |
static inline void* record_free(void* memblock) { |
|
180 |
return MallocTracker::record_free(memblock); |
|
13195 | 181 |
} |
182 |
||
25946 | 183 |
|
184 |
// Record creation of an arena |
|
185 |
static inline void record_new_arena(MEMFLAGS flag) { |
|
186 |
if (tracking_level() < NMT_summary) return; |
|
187 |
MallocTracker::record_new_arena(flag); |
|
188 |
} |
|
189 |
||
190 |
// Record destruction of an arena |
|
191 |
static inline void record_arena_free(MEMFLAGS flag) { |
|
192 |
if (tracking_level() < NMT_summary) return; |
|
193 |
MallocTracker::record_arena_free(flag); |
|
194 |
} |
|
13195 | 195 |
|
25946 | 196 |
// Record arena size change. Arena size is the size of all arena |
197 |
// chuncks that backing up the arena. |
|
198 |
static inline void record_arena_size_change(int diff, MEMFLAGS flag) { |
|
199 |
if (tracking_level() < NMT_summary) return; |
|
200 |
MallocTracker::record_arena_size_change(diff, flag); |
|
201 |
} |
|
202 |
||
203 |
static inline void record_virtual_memory_reserve(void* addr, size_t size, const NativeCallStack& stack, |
|
204 |
MEMFLAGS flag = mtNone) { |
|
205 |
if (tracking_level() < NMT_summary) return; |
|
206 |
if (addr != NULL) { |
|
207 |
ThreadCritical tc; |
|
208 |
// Recheck to avoid potential racing during NMT shutdown |
|
209 |
if (tracking_level() < NMT_summary) return; |
|
210 |
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
|
211 |
} |
6b67e51e3cfb
8009298: NMT: Special version of class loading/unloading with runThese stresses out NMT
zgu
parents:
15452
diff
changeset
|
212 |
} |
6b67e51e3cfb
8009298: NMT: Special version of class loading/unloading with runThese stresses out NMT
zgu
parents:
15452
diff
changeset
|
213 |
|
25946 | 214 |
static inline void record_virtual_memory_reserve_and_commit(void* addr, size_t size, |
215 |
const NativeCallStack& stack, MEMFLAGS flag = mtNone) { |
|
216 |
if (tracking_level() < NMT_summary) return; |
|
217 |
if (addr != NULL) { |
|
218 |
ThreadCritical tc; |
|
219 |
if (tracking_level() < NMT_summary) return; |
|
49033
3acc342c0738
8196405: [REDO] NMT: add_committed_regions doesn't merge succeeding regions
stefank
parents:
48872
diff
changeset
|
220 |
VirtualMemoryTracker::add_reserved_region((address)addr, size, stack, flag); |
46976
6d4058ac744f
8186748: NMT: memTracker::record_virtual_memory_reserve_and_commit() does not tag the memory as committed
zgu
parents:
40655
diff
changeset
|
221 |
VirtualMemoryTracker::add_committed_region((address)addr, size, stack); |
19546
f6b7c9e96ea3
8007074: SIGSEGV at ParMarkBitMap::verify_clear()
stefank
parents:
18681
diff
changeset
|
222 |
} |
f6b7c9e96ea3
8007074: SIGSEGV at ParMarkBitMap::verify_clear()
stefank
parents:
18681
diff
changeset
|
223 |
} |
13195 | 224 |
|
25946 | 225 |
static inline void record_virtual_memory_commit(void* addr, size_t size, |
226 |
const NativeCallStack& stack) { |
|
227 |
if (tracking_level() < NMT_summary) return; |
|
228 |
if (addr != NULL) { |
|
229 |
ThreadCritical tc; |
|
230 |
if (tracking_level() < NMT_summary) return; |
|
231 |
VirtualMemoryTracker::add_committed_region((address)addr, size, stack); |
|
232 |
} |
|
18086
f44cf213a775
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
16991
diff
changeset
|
233 |
} |
f44cf213a775
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
16991
diff
changeset
|
234 |
|
25946 | 235 |
static inline void record_virtual_memory_type(void* addr, MEMFLAGS flag) { |
236 |
if (tracking_level() < NMT_summary) return; |
|
237 |
if (addr != NULL) { |
|
238 |
ThreadCritical tc; |
|
239 |
if (tracking_level() < NMT_summary) return; |
|
240 |
VirtualMemoryTracker::set_reserved_region_type((address)addr, flag); |
|
13195 | 241 |
} |
242 |
} |
|
243 |
||
25946 | 244 |
static inline void record_thread_stack(void* addr, size_t size) { |
245 |
if (tracking_level() < NMT_summary) return; |
|
246 |
if (addr != NULL) { |
|
247 |
// uses thread stack malloc slot for book keeping number of threads |
|
248 |
MallocMemorySummary::record_malloc(0, mtThreadStack); |
|
49349
7194eb9e8f19
8199133: [BACKOUT] NMT: Enhance thread stack tracking
coleenp
parents:
49193
diff
changeset
|
249 |
record_virtual_memory_reserve_and_commit(addr, size, CALLER_PC, mtThreadStack); |
25946 | 250 |
} |
18086
f44cf213a775
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
16991
diff
changeset
|
251 |
} |
f44cf213a775
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
16991
diff
changeset
|
252 |
|
25946 | 253 |
static inline void release_thread_stack(void* addr, size_t size) { |
254 |
if (tracking_level() < NMT_summary) return; |
|
255 |
if (addr != NULL) { |
|
256 |
// uses thread stack malloc slot for book keeping number of threads |
|
257 |
MallocMemorySummary::record_free(0, mtThreadStack); |
|
258 |
ThreadCritical tc; |
|
259 |
if (tracking_level() < NMT_summary) return; |
|
260 |
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
|
261 |
} |
f44cf213a775
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
16991
diff
changeset
|
262 |
} |
f44cf213a775
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
16991
diff
changeset
|
263 |
|
25946 | 264 |
// Query lock is used to synchronize the access to tracking data. |
265 |
// So far, it is only used by JCmd query, but it may be used by |
|
266 |
// other tools. |
|
267 |
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
|
268 |
|
27162
0a4a7276949b
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
26144
diff
changeset
|
269 |
// Make a final report or report for hs_err file. |
0a4a7276949b
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
26144
diff
changeset
|
270 |
static void error_report(outputStream* output) { |
0a4a7276949b
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
26144
diff
changeset
|
271 |
if (tracking_level() >= NMT_summary) { |
0a4a7276949b
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
26144
diff
changeset
|
272 |
report(true, output); // just print summary for error case. |
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 |
|
0a4a7276949b
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
26144
diff
changeset
|
276 |
static void final_report(outputStream* output) { |
0a4a7276949b
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
26144
diff
changeset
|
277 |
NMT_TrackingLevel level = tracking_level(); |
0a4a7276949b
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
26144
diff
changeset
|
278 |
if (level >= NMT_summary) { |
0a4a7276949b
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
26144
diff
changeset
|
279 |
report(level == NMT_summary, output); |
0a4a7276949b
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
26144
diff
changeset
|
280 |
} |
0a4a7276949b
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
26144
diff
changeset
|
281 |
} |
0a4a7276949b
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
26144
diff
changeset
|
282 |
|
25946 | 283 |
|
284 |
// Stored baseline |
|
285 |
static inline MemBaseline& get_baseline() { |
|
286 |
return _baseline; |
|
18086
f44cf213a775
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
16991
diff
changeset
|
287 |
} |
f44cf213a775
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
16991
diff
changeset
|
288 |
|
25946 | 289 |
static NMT_TrackingLevel cmdline_tracking_level() { |
290 |
return _cmdline_tracking_level; |
|
15452
3bfde2dea09d
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
15104
diff
changeset
|
291 |
} |
3bfde2dea09d
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
15104
diff
changeset
|
292 |
|
25946 | 293 |
static void tuning_statistics(outputStream* out); |
15452
3bfde2dea09d
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
15104
diff
changeset
|
294 |
|
13195 | 295 |
private: |
25946 | 296 |
static NMT_TrackingLevel init_tracking_level(); |
27162
0a4a7276949b
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
26144
diff
changeset
|
297 |
static void report(bool summary_only, outputStream* output); |
13195 | 298 |
|
25946 | 299 |
private: |
300 |
// Tracking level |
|
301 |
static volatile NMT_TrackingLevel _tracking_level; |
|
302 |
// If NMT option value passed by launcher through environment |
|
303 |
// variable is valid |
|
304 |
static bool _is_nmt_env_valid; |
|
305 |
// command line tracking level |
|
306 |
static NMT_TrackingLevel _cmdline_tracking_level; |
|
307 |
// Stored baseline |
|
308 |
static MemBaseline _baseline; |
|
309 |
// Query lock |
|
310 |
static Mutex* _query_lock; |
|
13195 | 311 |
}; |
312 |
||
25946 | 313 |
#endif // INCLUDE_NMT |
13975
2f7431485cfa
7189254: Change makefiles for more flexibility to override defaults
jprovino
parents:
13861
diff
changeset
|
314 |
|
13195 | 315 |
#endif // SHARE_VM_SERVICES_MEM_TRACKER_HPP |