author | stefank |
Tue, 26 Nov 2019 10:47:46 +0100 | |
changeset 59290 | 97d13893ec3c |
parent 58927 | d8aced36e872 |
permissions | -rw-r--r-- |
25946 | 1 |
/* |
46489
40abcea5a9d5
8139673: NMT stack traces in output should show mtcomponent
zgu
parents:
40655
diff
changeset
|
2 |
* Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved. |
25946 | 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 |
#include "precompiled.hpp" |
|
25 |
||
26 |
#include "services/mallocSiteTable.hpp" |
|
27 |
#include "services/mallocTracker.hpp" |
|
28 |
#include "services/mallocTracker.inline.hpp" |
|
29 |
#include "services/memTracker.hpp" |
|
30 |
||
31 |
size_t MallocMemorySummary::_snapshot[CALC_OBJ_SIZE_IN_TYPE(MallocMemorySnapshot, size_t)]; |
|
32 |
||
33 |
// Total malloc'd memory amount |
|
34 |
size_t MallocMemorySnapshot::total() const { |
|
35 |
size_t amount = 0; |
|
36 |
for (int index = 0; index < mt_number_of_types; index ++) { |
|
37 |
amount += _malloc[index].malloc_size(); |
|
38 |
} |
|
39 |
amount += _tracking_header.size() + total_arena(); |
|
40 |
return amount; |
|
41 |
} |
|
42 |
||
43 |
// Total malloc'd memory used by arenas |
|
44 |
size_t MallocMemorySnapshot::total_arena() const { |
|
45 |
size_t amount = 0; |
|
46 |
for (int index = 0; index < mt_number_of_types; index ++) { |
|
47 |
amount += _malloc[index].arena_size(); |
|
48 |
} |
|
49 |
return amount; |
|
50 |
} |
|
51 |
||
52 |
// Make adjustment by subtracting chunks used by arenas |
|
28953
75f0bfa6ea14
8068451: Kitchensink fails with assert(_size >= sz) failed: Negative size
coleenp
parents:
27469
diff
changeset
|
53 |
// from total chunks to get total free chunk size |
25946 | 54 |
void MallocMemorySnapshot::make_adjustment() { |
55 |
size_t arena_size = total_arena(); |
|
56 |
int chunk_idx = NMTUtil::flag_to_index(mtChunk); |
|
57 |
_malloc[chunk_idx].record_free(arena_size); |
|
58 |
} |
|
59 |
||
60 |
||
61 |
void MallocMemorySummary::initialize() { |
|
62 |
assert(sizeof(_snapshot) >= sizeof(MallocMemorySnapshot), "Sanity Check"); |
|
63 |
// Uses placement new operator to initialize static area. |
|
64 |
::new ((void*)_snapshot)MallocMemorySnapshot(); |
|
65 |
} |
|
66 |
||
67 |
void MallocHeader::release() const { |
|
68 |
// Tracking already shutdown, no housekeeping is needed anymore |
|
69 |
if (MemTracker::tracking_level() <= NMT_minimal) return; |
|
70 |
||
71 |
MallocMemorySummary::record_free(size(), flags()); |
|
72 |
MallocMemorySummary::record_free_malloc_header(sizeof(MallocHeader)); |
|
27469
34008cfb8994
8058251: assert(_count > 0) failed: Negative counter when running runtime/NMT/MallocTrackingVerify.java
ctornqvi
parents:
27162
diff
changeset
|
73 |
if (MemTracker::tracking_level() == NMT_detail) { |
25946 | 74 |
MallocSiteTable::deallocation_at(size(), _bucket_idx, _pos_idx); |
75 |
} |
|
76 |
} |
|
77 |
||
78 |
bool MallocHeader::record_malloc_site(const NativeCallStack& stack, size_t size, |
|
46489
40abcea5a9d5
8139673: NMT stack traces in output should show mtcomponent
zgu
parents:
40655
diff
changeset
|
79 |
size_t* bucket_idx, size_t* pos_idx, MEMFLAGS flags) const { |
40abcea5a9d5
8139673: NMT stack traces in output should show mtcomponent
zgu
parents:
40655
diff
changeset
|
80 |
bool ret = MallocSiteTable::allocation_at(stack, size, bucket_idx, pos_idx, flags); |
25946 | 81 |
|
82 |
// Something went wrong, could be OOM or overflow malloc site table. |
|
83 |
// We want to keep tracking data under OOM circumstance, so transition to |
|
84 |
// summary tracking. |
|
85 |
if (!ret) { |
|
86 |
MemTracker::transition_to(NMT_summary); |
|
87 |
} |
|
88 |
return ret; |
|
89 |
} |
|
90 |
||
91 |
bool MallocHeader::get_stack(NativeCallStack& stack) const { |
|
92 |
return MallocSiteTable::access_stack(stack, _bucket_idx, _pos_idx); |
|
93 |
} |
|
94 |
||
95 |
bool MallocTracker::initialize(NMT_TrackingLevel level) { |
|
96 |
if (level >= NMT_summary) { |
|
97 |
MallocMemorySummary::initialize(); |
|
98 |
} |
|
99 |
||
100 |
if (level == NMT_detail) { |
|
101 |
return MallocSiteTable::initialize(); |
|
102 |
} |
|
103 |
return true; |
|
104 |
} |
|
105 |
||
106 |
bool MallocTracker::transition(NMT_TrackingLevel from, NMT_TrackingLevel to) { |
|
107 |
assert(from != NMT_off, "Can not transition from off state"); |
|
108 |
assert(to != NMT_off, "Can not transition to off state"); |
|
27162
0a4a7276949b
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
26925
diff
changeset
|
109 |
assert (from != NMT_minimal, "cannot transition from minimal state"); |
25946 | 110 |
|
27162
0a4a7276949b
8059100: SIGSEGV VirtualMemoryTracker::remove_released_region
coleenp
parents:
26925
diff
changeset
|
111 |
if (from == NMT_detail) { |
25946 | 112 |
assert(to == NMT_minimal || to == NMT_summary, "Just check"); |
113 |
MallocSiteTable::shutdown(); |
|
114 |
} |
|
115 |
return true; |
|
116 |
} |
|
117 |
||
118 |
// Record a malloc memory allocation |
|
119 |
void* MallocTracker::record_malloc(void* malloc_base, size_t size, MEMFLAGS flags, |
|
120 |
const NativeCallStack& stack, NMT_TrackingLevel level) { |
|
58927
d8aced36e872
8233494: Avoid calling MallocTracker::record_malloc and record_free when NMT is off
redestad
parents:
47216
diff
changeset
|
121 |
assert(level != NMT_off, "precondition"); |
25946 | 122 |
void* memblock; // the address for user data |
123 |
MallocHeader* header = NULL; |
|
124 |
||
125 |
if (malloc_base == NULL) { |
|
126 |
return NULL; |
|
127 |
} |
|
128 |
||
129 |
// Uses placement global new operator to initialize malloc header |
|
27469
34008cfb8994
8058251: assert(_count > 0) failed: Negative counter when running runtime/NMT/MallocTrackingVerify.java
ctornqvi
parents:
27162
diff
changeset
|
130 |
|
34008cfb8994
8058251: assert(_count > 0) failed: Negative counter when running runtime/NMT/MallocTrackingVerify.java
ctornqvi
parents:
27162
diff
changeset
|
131 |
header = ::new (malloc_base)MallocHeader(size, flags, stack, level); |
25946 | 132 |
memblock = (void*)((char*)malloc_base + sizeof(MallocHeader)); |
133 |
||
134 |
// The alignment check: 8 bytes alignment for 32 bit systems. |
|
135 |
// 16 bytes alignment for 64-bit systems. |
|
136 |
assert(((size_t)memblock & (sizeof(size_t) * 2 - 1)) == 0, "Alignment check"); |
|
137 |
||
138 |
#ifdef ASSERT |
|
139 |
if (level > NMT_minimal) { |
|
140 |
// Read back |
|
141 |
assert(get_size(memblock) == size, "Wrong size"); |
|
142 |
assert(get_flags(memblock) == flags, "Wrong flags"); |
|
143 |
} |
|
144 |
#endif |
|
145 |
||
146 |
return memblock; |
|
147 |
} |
|
148 |
||
149 |
void* MallocTracker::record_free(void* memblock) { |
|
58927
d8aced36e872
8233494: Avoid calling MallocTracker::record_malloc and record_free when NMT is off
redestad
parents:
47216
diff
changeset
|
150 |
assert(MemTracker::tracking_level() != NMT_off && memblock != NULL, "precondition"); |
25946 | 151 |
MallocHeader* header = malloc_header(memblock); |
152 |
header->release(); |
|
153 |
return (void*)header; |
|
154 |
} |