Print this page
rev 6898 : 8062870: src/share/vm/services/mallocTracker.hpp:64 assert(_count > 0) failed: Negative counter
Summary: Signed bitfield size y can only have (1 << y)-1 values.
Reviewed-by: shade, dholmes, jrose, ctornqvi, gtriantafill
Split |
Split |
Close |
Expand all |
Collapse all |
--- old/hotspot/src/share/vm/services/mallocTracker.hpp
+++ new/hotspot/src/share/vm/services/mallocTracker.hpp
1 1 /*
2 2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
3 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 4 *
5 5 * This code is free software; you can redistribute it and/or modify it
6 6 * under the terms of the GNU General Public License version 2 only, as
7 7 * published by the Free Software Foundation.
8 8 *
9 9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 12 * version 2 for more details (a copy is included in the LICENSE file that
13 13 * accompanied this code).
14 14 *
15 15 * You should have received a copy of the GNU General Public License version
16 16 * 2 along with this work; if not, write to the Free Software Foundation,
17 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 18 *
19 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 20 * or visit www.oracle.com if you need additional information or have any
21 21 * questions.
22 22 *
23 23 */
24 24
25 25 #ifndef SHARE_VM_SERVICES_MALLOC_TRACKER_HPP
26 26 #define SHARE_VM_SERVICES_MALLOC_TRACKER_HPP
27 27
28 28 #if INCLUDE_NMT
29 29
30 30 #include "memory/allocation.hpp"
31 31 #include "runtime/atomic.hpp"
32 32 #include "services/nmtCommon.hpp"
33 33 #include "utilities/nativeCallStack.hpp"
34 34
35 35 /*
36 36 * This counter class counts memory allocation and deallocation,
37 37 * records total memory allocation size and number of allocations.
38 38 * The counters are updated atomically.
39 39 */
40 40 class MemoryCounter VALUE_OBJ_CLASS_SPEC {
41 41 private:
42 42 size_t _count;
43 43 size_t _size;
44 44
45 45 DEBUG_ONLY(size_t _peak_count;)
46 46 DEBUG_ONLY(size_t _peak_size; )
47 47
48 48 public:
49 49 MemoryCounter() : _count(0), _size(0) {
50 50 DEBUG_ONLY(_peak_count = 0;)
51 51 DEBUG_ONLY(_peak_size = 0;)
52 52 }
53 53
54 54 inline void allocate(size_t sz) {
55 55 Atomic::add(1, (volatile MemoryCounterType*)&_count);
56 56 if (sz > 0) {
57 57 Atomic::add((MemoryCounterType)sz, (volatile MemoryCounterType*)&_size);
58 58 DEBUG_ONLY(_peak_size = MAX2(_peak_size, _size));
59 59 }
60 60 DEBUG_ONLY(_peak_count = MAX2(_peak_count, _count);)
61 61 }
62 62
63 63 inline void deallocate(size_t sz) {
64 64 assert(_count > 0, "Negative counter");
65 65 assert(_size >= sz, "Negative size");
66 66 Atomic::add(-1, (volatile MemoryCounterType*)&_count);
67 67 if (sz > 0) {
68 68 Atomic::add(-(MemoryCounterType)sz, (volatile MemoryCounterType*)&_size);
69 69 }
70 70 }
71 71
72 72 inline void resize(long sz) {
73 73 if (sz != 0) {
74 74 Atomic::add((MemoryCounterType)sz, (volatile MemoryCounterType*)&_size);
75 75 DEBUG_ONLY(_peak_size = MAX2(_size, _peak_size);)
76 76 }
77 77 }
78 78
79 79 inline size_t count() const { return _count; }
80 80 inline size_t size() const { return _size; }
81 81 DEBUG_ONLY(inline size_t peak_count() const { return _peak_count; })
82 82 DEBUG_ONLY(inline size_t peak_size() const { return _peak_size; })
83 83
84 84 };
85 85
86 86 /*
87 87 * Malloc memory used by a particular subsystem.
88 88 * It includes the memory acquired through os::malloc()
89 89 * call and arena's backing memory.
90 90 */
91 91 class MallocMemory VALUE_OBJ_CLASS_SPEC {
92 92 private:
93 93 MemoryCounter _malloc;
94 94 MemoryCounter _arena;
95 95
96 96 public:
97 97 MallocMemory() { }
98 98
99 99 inline void record_malloc(size_t sz) {
100 100 _malloc.allocate(sz);
101 101 }
102 102
103 103 inline void record_free(size_t sz) {
104 104 _malloc.deallocate(sz);
105 105 }
106 106
107 107 inline void record_new_arena() {
108 108 _arena.allocate(0);
109 109 }
110 110
111 111 inline void record_arena_free() {
112 112 _arena.deallocate(0);
113 113 }
114 114
115 115 inline void record_arena_size_change(long sz) {
116 116 _arena.resize(sz);
117 117 }
118 118
119 119 inline size_t malloc_size() const { return _malloc.size(); }
120 120 inline size_t malloc_count() const { return _malloc.count();}
121 121 inline size_t arena_size() const { return _arena.size(); }
122 122 inline size_t arena_count() const { return _arena.count(); }
123 123
124 124 DEBUG_ONLY(inline const MemoryCounter& malloc_counter() const { return _malloc; })
125 125 DEBUG_ONLY(inline const MemoryCounter& arena_counter() const { return _arena; })
126 126 };
127 127
128 128 class MallocMemorySummary;
129 129
130 130 // A snapshot of malloc'd memory, includes malloc memory
131 131 // usage by types and memory used by tracking itself.
132 132 class MallocMemorySnapshot : public ResourceObj {
133 133 friend class MallocMemorySummary;
134 134
135 135 private:
136 136 MallocMemory _malloc[mt_number_of_types];
137 137 MemoryCounter _tracking_header;
138 138
139 139
140 140 public:
141 141 inline MallocMemory* by_type(MEMFLAGS flags) {
142 142 int index = NMTUtil::flag_to_index(flags);
143 143 return &_malloc[index];
144 144 }
145 145
146 146 inline MallocMemory* by_index(int index) {
147 147 assert(index >= 0, "Index out of bound");
148 148 assert(index < mt_number_of_types, "Index out of bound");
149 149 return &_malloc[index];
150 150 }
151 151
152 152 inline MemoryCounter* malloc_overhead() {
153 153 return &_tracking_header;
154 154 }
155 155
156 156 // Total malloc'd memory amount
157 157 size_t total() const;
158 158 // Total malloc'd memory used by arenas
159 159 size_t total_arena() const;
160 160
161 161 inline size_t thread_count() const {
162 162 MallocMemorySnapshot* s = const_cast<MallocMemorySnapshot*>(this);
163 163 return s->by_type(mtThreadStack)->malloc_count();
164 164 }
165 165
166 166 void copy_to(MallocMemorySnapshot* s) {
167 167 s->_tracking_header = _tracking_header;
168 168 for (int index = 0; index < mt_number_of_types; index ++) {
169 169 s->_malloc[index] = _malloc[index];
170 170 }
171 171 }
172 172
173 173 // Make adjustment by subtracting chunks used by arenas
174 174 // from total chunks to get total free chunk size
175 175 void make_adjustment();
176 176 };
177 177
178 178 /*
179 179 * This class is for collecting malloc statistics at summary level
180 180 */
181 181 class MallocMemorySummary : AllStatic {
182 182 private:
183 183 // Reserve memory for placement of MallocMemorySnapshot object
184 184 static size_t _snapshot[CALC_OBJ_SIZE_IN_TYPE(MallocMemorySnapshot, size_t)];
185 185
186 186 public:
187 187 static void initialize();
188 188
189 189 static inline void record_malloc(size_t size, MEMFLAGS flag) {
190 190 as_snapshot()->by_type(flag)->record_malloc(size);
191 191 }
192 192
193 193 static inline void record_free(size_t size, MEMFLAGS flag) {
194 194 as_snapshot()->by_type(flag)->record_free(size);
195 195 }
196 196
197 197 static inline void record_new_arena(MEMFLAGS flag) {
198 198 as_snapshot()->by_type(flag)->record_new_arena();
199 199 }
200 200
201 201 static inline void record_arena_free(MEMFLAGS flag) {
202 202 as_snapshot()->by_type(flag)->record_arena_free();
203 203 }
204 204
205 205 static inline void record_arena_size_change(long size, MEMFLAGS flag) {
206 206 as_snapshot()->by_type(flag)->record_arena_size_change(size);
207 207 }
208 208
209 209 static void snapshot(MallocMemorySnapshot* s) {
210 210 as_snapshot()->copy_to(s);
211 211 s->make_adjustment();
212 212 }
213 213
214 214 // Record memory used by malloc tracking header
215 215 static inline void record_new_malloc_header(size_t sz) {
216 216 as_snapshot()->malloc_overhead()->allocate(sz);
217 217 }
218 218
219 219 static inline void record_free_malloc_header(size_t sz) {
220 220 as_snapshot()->malloc_overhead()->deallocate(sz);
221 221 }
222 222
223 223 // The memory used by malloc tracking headers
224 224 static inline size_t tracking_overhead() {
225 225 return as_snapshot()->malloc_overhead()->size();
226 226 }
227 227
228 228 static MallocMemorySnapshot* as_snapshot() {
229 229 return (MallocMemorySnapshot*)_snapshot;
230 230 }
231 231 };
↓ open down ↓ |
231 lines elided |
↑ open up ↑ |
232 232
233 233
234 234 /*
235 235 * Malloc tracking header.
236 236 * To satisfy malloc alignment requirement, NMT uses 2 machine words for tracking purpose,
237 237 * which ensures 8-bytes alignment on 32-bit systems and 16-bytes on 64-bit systems (Product build).
238 238 */
239 239
240 240 class MallocHeader VALUE_OBJ_CLASS_SPEC {
241 241 #ifdef _LP64
242 - size_t _size : 62;
243 - size_t _level : 2;
242 + size_t _size : 64;
244 243 size_t _flags : 8;
245 244 size_t _pos_idx : 16;
246 245 size_t _bucket_idx: 40;
247 -#define MAX_MALLOCSITE_TABLE_SIZE ((size_t)1 << 40)
248 -#define MAX_BUCKET_LENGTH ((size_t)(1 << 16))
249 -#define MAX_MALLOC_SIZE (((size_t)1 << 62) - 1)
246 +#define MAX_MALLOCSITE_TABLE_SIZE right_n_bits(40)
247 +#define MAX_BUCKET_LENGTH right_n_bits(16)
250 248 #else
251 - size_t _size : 30;
252 - size_t _level : 2;
249 + size_t _size : 32;
253 250 size_t _flags : 8;
254 251 size_t _pos_idx : 8;
255 252 size_t _bucket_idx: 16;
256 -#define MAX_MALLOCSITE_TABLE_SIZE ((size_t)(1 << 16))
257 -#define MAX_BUCKET_LENGTH ((size_t)(1 << 8))
258 -// Max malloc size = 1GB - 1 on 32 bit system, such has total 4GB memory
259 -#define MAX_MALLOC_SIZE ((size_t)(1 << 30) - 1)
253 +#define MAX_MALLOCSITE_TABLE_SIZE right_n_bits(16)
254 +#define MAX_BUCKET_LENGTH right_n_bits(8)
260 255 #endif // _LP64
261 256
262 257 public:
263 - // Summary tracking header
264 - MallocHeader(size_t size, MEMFLAGS flags) {
258 + MallocHeader(size_t size, MEMFLAGS flags, const NativeCallStack& stack, NMT_TrackingLevel level) {
265 259 assert(sizeof(MallocHeader) == sizeof(void*) * 2,
266 260 "Wrong header size");
267 261
268 - _level = NMT_summary;
269 - _flags = flags;
270 - set_size(size);
271 - MallocMemorySummary::record_malloc(size, flags);
272 - MallocMemorySummary::record_new_malloc_header(sizeof(MallocHeader));
273 - }
274 - // Detail tracking header
275 - MallocHeader(size_t size, MEMFLAGS flags, const NativeCallStack& stack) {
276 - assert(sizeof(MallocHeader) == sizeof(void*) * 2,
277 - "Wrong header size");
262 + if (level == NMT_minimal) {
263 + return;
264 + }
278 265
279 - _level = NMT_detail;
280 266 _flags = flags;
281 267 set_size(size);
282 - size_t bucket_idx;
283 - size_t pos_idx;
284 - if (record_malloc_site(stack, size, &bucket_idx, &pos_idx)) {
285 - assert(bucket_idx <= MAX_MALLOCSITE_TABLE_SIZE, "Overflow bucket index");
286 - assert(pos_idx <= MAX_BUCKET_LENGTH, "Overflow bucket position index");
287 - _bucket_idx = bucket_idx;
288 - _pos_idx = pos_idx;
268 + if (level == NMT_detail) {
269 + size_t bucket_idx;
270 + size_t pos_idx;
271 + if (record_malloc_site(stack, size, &bucket_idx, &pos_idx)) {
272 + assert(bucket_idx <= MAX_MALLOCSITE_TABLE_SIZE, "Overflow bucket index");
273 + assert(pos_idx <= MAX_BUCKET_LENGTH, "Overflow bucket position index");
274 + _bucket_idx = bucket_idx;
275 + _pos_idx = pos_idx;
276 + }
289 277 }
278 +
290 279 MallocMemorySummary::record_malloc(size, flags);
291 280 MallocMemorySummary::record_new_malloc_header(sizeof(MallocHeader));
292 281 }
293 - // Minimal tracking header
294 - MallocHeader() {
295 - assert(sizeof(MallocHeader) == sizeof(void*) * 2,
296 - "Wrong header size");
297 -
298 - _level = (unsigned short)NMT_minimal;
299 - }
300 -
301 - inline NMT_TrackingLevel tracking_level() const {
302 - return (NMT_TrackingLevel)_level;
303 - }
304 282
305 283 inline size_t size() const { return _size; }
306 284 inline MEMFLAGS flags() const { return (MEMFLAGS)_flags; }
307 285 bool get_stack(NativeCallStack& stack) const;
308 286
309 287 // Cleanup tracking information before the memory is released.
310 288 void release() const;
311 289
312 290 private:
313 291 inline void set_size(size_t size) {
314 - assert(size <= MAX_MALLOC_SIZE, "Malloc size too large, should use virtual memory?");
315 292 _size = size;
316 293 }
317 294 bool record_malloc_site(const NativeCallStack& stack, size_t size,
318 295 size_t* bucket_idx, size_t* pos_idx) const;
319 296 };
320 297
321 298
322 299 // Main class called from MemTracker to track malloc activities
323 300 class MallocTracker : AllStatic {
324 301 public:
325 302 // Initialize malloc tracker for specific tracking level
326 303 static bool initialize(NMT_TrackingLevel level);
327 304
328 305 static bool transition(NMT_TrackingLevel from, NMT_TrackingLevel to);
329 306
330 307 // malloc tracking header size for specific tracking level
331 308 static inline size_t malloc_header_size(NMT_TrackingLevel level) {
332 309 return (level == NMT_off) ? 0 : sizeof(MallocHeader);
333 310 }
334 311
335 312 // Parameter name convention:
336 313 // memblock : the beginning address for user data
337 314 // malloc_base: the beginning address that includes malloc tracking header
338 315 //
339 316 // The relationship:
↓ open down ↓ |
15 lines elided |
↑ open up ↑ |
340 317 // memblock = (char*)malloc_base + sizeof(nmt header)
341 318 //
342 319
343 320 // Record malloc on specified memory block
344 321 static void* record_malloc(void* malloc_base, size_t size, MEMFLAGS flags,
345 322 const NativeCallStack& stack, NMT_TrackingLevel level);
346 323
347 324 // Record free on specified memory block
348 325 static void* record_free(void* memblock);
349 326
350 - // Get tracking level of specified memory block
351 - static inline NMT_TrackingLevel get_memory_tracking_level(void* memblock);
352 -
353 -
354 327 // Offset memory address to header address
355 328 static inline void* get_base(void* memblock);
356 329 static inline void* get_base(void* memblock, NMT_TrackingLevel level) {
357 330 if (memblock == NULL || level == NMT_off) return memblock;
358 331 return (char*)memblock - malloc_header_size(level);
359 332 }
360 333
361 334 // Get memory size
362 335 static inline size_t get_size(void* memblock) {
363 336 MallocHeader* header = malloc_header(memblock);
364 - assert(header->tracking_level() >= NMT_summary,
365 - "Wrong tracking level");
366 337 return header->size();
367 338 }
368 339
369 340 // Get memory type
370 341 static inline MEMFLAGS get_flags(void* memblock) {
371 342 MallocHeader* header = malloc_header(memblock);
372 - assert(header->tracking_level() >= NMT_summary,
373 - "Wrong tracking level");
374 343 return header->flags();
375 344 }
376 345
377 346 // Get header size
378 347 static inline size_t get_header_size(void* memblock) {
379 348 return (memblock == NULL) ? 0 : sizeof(MallocHeader);
380 349 }
381 350
382 351 static inline void record_new_arena(MEMFLAGS flags) {
383 352 MallocMemorySummary::record_new_arena(flags);
384 353 }
385 354
386 355 static inline void record_arena_free(MEMFLAGS flags) {
↓ open down ↓ |
3 lines elided |
↑ open up ↑ |
387 356 MallocMemorySummary::record_arena_free(flags);
388 357 }
389 358
390 359 static inline void record_arena_size_change(int size, MEMFLAGS flags) {
391 360 MallocMemorySummary::record_arena_size_change(size, flags);
392 361 }
393 362 private:
394 363 static inline MallocHeader* malloc_header(void *memblock) {
395 364 assert(memblock != NULL, "NULL pointer");
396 365 MallocHeader* header = (MallocHeader*)((char*)memblock - sizeof(MallocHeader));
397 - assert(header->tracking_level() >= NMT_minimal, "Bad header");
398 366 return header;
399 367 }
400 368 };
401 369
402 370 #endif // INCLUDE_NMT
403 371
404 372
405 373 #endif //SHARE_VM_SERVICES_MALLOC_TRACKER_HPP
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX