author | stefank |
Mon, 25 Nov 2019 12:33:15 +0100 | |
changeset 59252 | 623722a6aeb9 |
parent 53685 | df83034c9275 |
permissions | -rw-r--r-- |
25946 | 1 |
/* |
53685
df83034c9275
8218558: NMT stack traces in output should show mt component for virtual memory allocations
zgu
parents:
51038
diff
changeset
|
2 |
* Copyright (c) 2014, 2019, 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 |
||
27 |
#include "memory/allocation.inline.hpp" |
|
28 |
#include "runtime/atomic.hpp" |
|
29 |
#include "services/mallocSiteTable.hpp" |
|
30 |
||
31 |
// Malloc site hashtable buckets |
|
32 |
MallocSiteHashtableEntry* MallocSiteTable::_table[MallocSiteTable::table_size]; |
|
50965
29eaf3feab30
8206183: Possible construct EMPTY_STACK and allocation stack, etc. on first use
zgu
parents:
48488
diff
changeset
|
33 |
const NativeCallStack* MallocSiteTable::_hash_entry_allocation_stack = NULL; |
29eaf3feab30
8206183: Possible construct EMPTY_STACK and allocation stack, etc. on first use
zgu
parents:
48488
diff
changeset
|
34 |
const MallocSiteHashtableEntry* MallocSiteTable::_hash_entry_allocation_site = NULL; |
25946 | 35 |
|
36 |
// concurrent access counter |
|
37 |
volatile int MallocSiteTable::_access_count = 0; |
|
38 |
||
39 |
// Tracking hashtable contention |
|
40 |
NOT_PRODUCT(int MallocSiteTable::_peak_count = 0;) |
|
41 |
||
42 |
||
43 |
/* |
|
44 |
* Initialize malloc site table. |
|
45 |
* Hashtable entry is malloc'd, so it can cause infinite recursion. |
|
46 |
* To avoid above problem, we pre-initialize a hash entry for |
|
47 |
* this allocation site. |
|
48 |
* The method is called during C runtime static variable initialization |
|
49 |
* time, it is in single-threaded mode from JVM perspective. |
|
50 |
*/ |
|
51 |
bool MallocSiteTable::initialize() { |
|
52 |
assert((size_t)table_size <= MAX_MALLOCSITE_TABLE_SIZE, "Hashtable overflow"); |
|
53 |
||
54 |
// Fake the call stack for hashtable entry allocation |
|
55 |
assert(NMT_TrackingStackDepth > 1, "At least one tracking stack"); |
|
56 |
||
57 |
// Create pseudo call stack for hashtable entry allocation |
|
58 |
address pc[3]; |
|
59 |
if (NMT_TrackingStackDepth >= 3) { |
|
50976
59ee619866c3
8206173: MallocSiteTable::initialize() doesn't take function descriptors into account
simonis
parents:
48488
diff
changeset
|
60 |
uintx *fp = (uintx*)MallocSiteTable::allocation_at; |
59ee619866c3
8206173: MallocSiteTable::initialize() doesn't take function descriptors into account
simonis
parents:
48488
diff
changeset
|
61 |
// On ppc64, 'fp' is a pointer to a function descriptor which is a struct of |
59ee619866c3
8206173: MallocSiteTable::initialize() doesn't take function descriptors into account
simonis
parents:
48488
diff
changeset
|
62 |
// three native pointers where the first pointer is the real function address. |
59ee619866c3
8206173: MallocSiteTable::initialize() doesn't take function descriptors into account
simonis
parents:
48488
diff
changeset
|
63 |
// See: http://refspecs.linuxfoundation.org/ELF/ppc64/PPC-elf64abi-1.9.html#FUNC-DES |
59ee619866c3
8206173: MallocSiteTable::initialize() doesn't take function descriptors into account
simonis
parents:
48488
diff
changeset
|
64 |
pc[2] = (address)(fp PPC64_ONLY(BIG_ENDIAN_ONLY([0]))); |
25946 | 65 |
} |
66 |
if (NMT_TrackingStackDepth >= 2) { |
|
50976
59ee619866c3
8206173: MallocSiteTable::initialize() doesn't take function descriptors into account
simonis
parents:
48488
diff
changeset
|
67 |
uintx *fp = (uintx*)MallocSiteTable::lookup_or_add; |
59ee619866c3
8206173: MallocSiteTable::initialize() doesn't take function descriptors into account
simonis
parents:
48488
diff
changeset
|
68 |
pc[1] = (address)(fp PPC64_ONLY(BIG_ENDIAN_ONLY([0]))); |
25946 | 69 |
} |
50976
59ee619866c3
8206173: MallocSiteTable::initialize() doesn't take function descriptors into account
simonis
parents:
48488
diff
changeset
|
70 |
uintx *fp = (uintx*)MallocSiteTable::new_entry; |
59ee619866c3
8206173: MallocSiteTable::initialize() doesn't take function descriptors into account
simonis
parents:
48488
diff
changeset
|
71 |
pc[0] = (address)(fp PPC64_ONLY(BIG_ENDIAN_ONLY([0]))); |
25946 | 72 |
|
50965
29eaf3feab30
8206183: Possible construct EMPTY_STACK and allocation stack, etc. on first use
zgu
parents:
48488
diff
changeset
|
73 |
static const NativeCallStack stack(pc, MIN2(((int)(sizeof(pc) / sizeof(address))), ((int)NMT_TrackingStackDepth))); |
29eaf3feab30
8206183: Possible construct EMPTY_STACK and allocation stack, etc. on first use
zgu
parents:
48488
diff
changeset
|
74 |
static const MallocSiteHashtableEntry entry(stack, mtNMT); |
25946 | 75 |
|
50965
29eaf3feab30
8206183: Possible construct EMPTY_STACK and allocation stack, etc. on first use
zgu
parents:
48488
diff
changeset
|
76 |
assert(_hash_entry_allocation_stack == NULL && |
29eaf3feab30
8206183: Possible construct EMPTY_STACK and allocation stack, etc. on first use
zgu
parents:
48488
diff
changeset
|
77 |
_hash_entry_allocation_site == NULL, |
29eaf3feab30
8206183: Possible construct EMPTY_STACK and allocation stack, etc. on first use
zgu
parents:
48488
diff
changeset
|
78 |
"Already initailized"); |
29eaf3feab30
8206183: Possible construct EMPTY_STACK and allocation stack, etc. on first use
zgu
parents:
48488
diff
changeset
|
79 |
|
29eaf3feab30
8206183: Possible construct EMPTY_STACK and allocation stack, etc. on first use
zgu
parents:
48488
diff
changeset
|
80 |
_hash_entry_allocation_stack = &stack; |
29eaf3feab30
8206183: Possible construct EMPTY_STACK and allocation stack, etc. on first use
zgu
parents:
48488
diff
changeset
|
81 |
_hash_entry_allocation_site = &entry; |
25946 | 82 |
|
83 |
// Add the allocation site to hashtable. |
|
50965
29eaf3feab30
8206183: Possible construct EMPTY_STACK and allocation stack, etc. on first use
zgu
parents:
48488
diff
changeset
|
84 |
int index = hash_to_index(stack.hash()); |
29eaf3feab30
8206183: Possible construct EMPTY_STACK and allocation stack, etc. on first use
zgu
parents:
48488
diff
changeset
|
85 |
_table[index] = const_cast<MallocSiteHashtableEntry*>(&entry); |
25946 | 86 |
|
87 |
return true; |
|
88 |
} |
|
89 |
||
90 |
// Walks entries in the hashtable. |
|
91 |
// It stops walk if the walker returns false. |
|
92 |
bool MallocSiteTable::walk(MallocSiteWalker* walker) { |
|
93 |
MallocSiteHashtableEntry* head; |
|
94 |
for (int index = 0; index < table_size; index ++) { |
|
95 |
head = _table[index]; |
|
96 |
while (head != NULL) { |
|
97 |
if (!walker->do_malloc_site(head->peek())) { |
|
98 |
return false; |
|
99 |
} |
|
100 |
head = (MallocSiteHashtableEntry*)head->next(); |
|
101 |
} |
|
102 |
} |
|
103 |
return true; |
|
104 |
} |
|
105 |
||
106 |
/* |
|
107 |
* The hashtable does not have deletion policy on individual entry, |
|
108 |
* and each linked list node is inserted via compare-and-swap, |
|
109 |
* so each linked list is stable, the contention only happens |
|
110 |
* at the end of linked list. |
|
111 |
* This method should not return NULL under normal circumstance. |
|
112 |
* If NULL is returned, it indicates: |
|
113 |
* 1. Out of memory, it cannot allocate new hash entry. |
|
114 |
* 2. Overflow hash bucket. |
|
115 |
* Under any of above circumstances, caller should handle the situation. |
|
116 |
*/ |
|
117 |
MallocSite* MallocSiteTable::lookup_or_add(const NativeCallStack& key, size_t* bucket_idx, |
|
46489
40abcea5a9d5
8139673: NMT stack traces in output should show mtcomponent
zgu
parents:
29462
diff
changeset
|
118 |
size_t* pos_idx, MEMFLAGS flags) { |
40abcea5a9d5
8139673: NMT stack traces in output should show mtcomponent
zgu
parents:
29462
diff
changeset
|
119 |
assert(flags != mtNone, "Should have a real memory type"); |
29462
f2e2922222a4
8069124: runtime/NMT/MallocSiteHashOverflow.java failing in nightlies
ctornqvi
parents:
28953
diff
changeset
|
120 |
unsigned int index = hash_to_index(key.hash()); |
25946 | 121 |
*bucket_idx = (size_t)index; |
122 |
*pos_idx = 0; |
|
123 |
||
124 |
// First entry for this hash bucket |
|
125 |
if (_table[index] == NULL) { |
|
46489
40abcea5a9d5
8139673: NMT stack traces in output should show mtcomponent
zgu
parents:
29462
diff
changeset
|
126 |
MallocSiteHashtableEntry* entry = new_entry(key, flags); |
25946 | 127 |
// OOM check |
128 |
if (entry == NULL) return NULL; |
|
129 |
||
130 |
// swap in the head |
|
59252
623722a6aeb9
8234740: Harmonize parameter order in Atomic - cmpxchg
stefank
parents:
53685
diff
changeset
|
131 |
if (Atomic::replace_if_null(&_table[index], entry)) { |
25946 | 132 |
return entry->data(); |
133 |
} |
|
134 |
||
135 |
delete entry; |
|
136 |
} |
|
137 |
||
138 |
MallocSiteHashtableEntry* head = _table[index]; |
|
139 |
while (head != NULL && (*pos_idx) <= MAX_BUCKET_LENGTH) { |
|
140 |
MallocSite* site = head->data(); |
|
53685
df83034c9275
8218558: NMT stack traces in output should show mt component for virtual memory allocations
zgu
parents:
51038
diff
changeset
|
141 |
if (site->flag() == flags && site->equals(key)) { |
25946 | 142 |
return head->data(); |
143 |
} |
|
144 |
||
145 |
if (head->next() == NULL && (*pos_idx) < MAX_BUCKET_LENGTH) { |
|
46489
40abcea5a9d5
8139673: NMT stack traces in output should show mtcomponent
zgu
parents:
29462
diff
changeset
|
146 |
MallocSiteHashtableEntry* entry = new_entry(key, flags); |
25946 | 147 |
// OOM check |
148 |
if (entry == NULL) return NULL; |
|
149 |
if (head->atomic_insert(entry)) { |
|
150 |
(*pos_idx) ++; |
|
151 |
return entry->data(); |
|
152 |
} |
|
153 |
// contended, other thread won |
|
154 |
delete entry; |
|
155 |
} |
|
156 |
head = (MallocSiteHashtableEntry*)head->next(); |
|
157 |
(*pos_idx) ++; |
|
158 |
} |
|
159 |
return NULL; |
|
160 |
} |
|
161 |
||
162 |
// Access malloc site |
|
163 |
MallocSite* MallocSiteTable::malloc_site(size_t bucket_idx, size_t pos_idx) { |
|
164 |
assert(bucket_idx < table_size, "Invalid bucket index"); |
|
165 |
MallocSiteHashtableEntry* head = _table[bucket_idx]; |
|
46630
75aa3e39d02c
8182299: Enable disabled clang warnings, build on OSX 10 + Xcode 8
jwilhelm
parents:
46489
diff
changeset
|
166 |
for (size_t index = 0; |
75aa3e39d02c
8182299: Enable disabled clang warnings, build on OSX 10 + Xcode 8
jwilhelm
parents:
46489
diff
changeset
|
167 |
index < pos_idx && head != NULL; |
75aa3e39d02c
8182299: Enable disabled clang warnings, build on OSX 10 + Xcode 8
jwilhelm
parents:
46489
diff
changeset
|
168 |
index++, head = (MallocSiteHashtableEntry*)head->next()) {} |
25946 | 169 |
assert(head != NULL, "Invalid position index"); |
170 |
return head->data(); |
|
171 |
} |
|
172 |
||
173 |
// Allocates MallocSiteHashtableEntry object. Special call stack |
|
174 |
// (pre-installed allocation site) has to be used to avoid infinite |
|
175 |
// recursion. |
|
46489
40abcea5a9d5
8139673: NMT stack traces in output should show mtcomponent
zgu
parents:
29462
diff
changeset
|
176 |
MallocSiteHashtableEntry* MallocSiteTable::new_entry(const NativeCallStack& key, MEMFLAGS flags) { |
25946 | 177 |
void* p = AllocateHeap(sizeof(MallocSiteHashtableEntry), mtNMT, |
178 |
*hash_entry_allocation_stack(), AllocFailStrategy::RETURN_NULL); |
|
46489
40abcea5a9d5
8139673: NMT stack traces in output should show mtcomponent
zgu
parents:
29462
diff
changeset
|
179 |
return ::new (p) MallocSiteHashtableEntry(key, flags); |
25946 | 180 |
} |
181 |
||
182 |
void MallocSiteTable::reset() { |
|
183 |
for (int index = 0; index < table_size; index ++) { |
|
184 |
MallocSiteHashtableEntry* head = _table[index]; |
|
185 |
_table[index] = NULL; |
|
186 |
delete_linked_list(head); |
|
187 |
} |
|
50965
29eaf3feab30
8206183: Possible construct EMPTY_STACK and allocation stack, etc. on first use
zgu
parents:
48488
diff
changeset
|
188 |
|
29eaf3feab30
8206183: Possible construct EMPTY_STACK and allocation stack, etc. on first use
zgu
parents:
48488
diff
changeset
|
189 |
_hash_entry_allocation_stack = NULL; |
29eaf3feab30
8206183: Possible construct EMPTY_STACK and allocation stack, etc. on first use
zgu
parents:
48488
diff
changeset
|
190 |
_hash_entry_allocation_site = NULL; |
25946 | 191 |
} |
192 |
||
193 |
void MallocSiteTable::delete_linked_list(MallocSiteHashtableEntry* head) { |
|
194 |
MallocSiteHashtableEntry* p; |
|
195 |
while (head != NULL) { |
|
196 |
p = head; |
|
197 |
head = (MallocSiteHashtableEntry*)head->next(); |
|
50965
29eaf3feab30
8206183: Possible construct EMPTY_STACK and allocation stack, etc. on first use
zgu
parents:
48488
diff
changeset
|
198 |
if (p != hash_entry_allocation_site()) { |
25946 | 199 |
delete p; |
200 |
} |
|
201 |
} |
|
202 |
} |
|
203 |
||
204 |
void MallocSiteTable::shutdown() { |
|
205 |
AccessLock locker(&_access_count); |
|
206 |
locker.exclusiveLock(); |
|
207 |
reset(); |
|
208 |
} |
|
209 |
||
210 |
bool MallocSiteTable::walk_malloc_site(MallocSiteWalker* walker) { |
|
211 |
assert(walker != NULL, "NuLL walker"); |
|
212 |
AccessLock locker(&_access_count); |
|
213 |
if (locker.sharedLock()) { |
|
214 |
NOT_PRODUCT(_peak_count = MAX2(_peak_count, _access_count);) |
|
215 |
return walk(walker); |
|
216 |
} |
|
217 |
return false; |
|
218 |
} |
|
219 |
||
220 |
||
221 |
void MallocSiteTable::AccessLock::exclusiveLock() { |
|
47765
b7c7428eaab9
8189610: Reconcile jvm.h and all jvm_md.h between java.base and hotspot
coleenp
parents:
47634
diff
changeset
|
222 |
int target; |
b7c7428eaab9
8189610: Reconcile jvm.h and all jvm_md.h between java.base and hotspot
coleenp
parents:
47634
diff
changeset
|
223 |
int val; |
25946 | 224 |
|
225 |
assert(_lock_state != ExclusiveLock, "Can only call once"); |
|
226 |
assert(*_lock >= 0, "Can not content exclusive lock"); |
|
227 |
||
228 |
// make counter negative to block out shared locks |
|
229 |
do { |
|
230 |
val = *_lock; |
|
231 |
target = _MAGIC_ + *_lock; |
|
59252
623722a6aeb9
8234740: Harmonize parameter order in Atomic - cmpxchg
stefank
parents:
53685
diff
changeset
|
232 |
} while (Atomic::cmpxchg(_lock, val, target) != val); |
25946 | 233 |
|
234 |
// wait for all readers to exit |
|
235 |
while (*_lock != _MAGIC_) { |
|
236 |
#ifdef _WINDOWS |
|
237 |
os::naked_short_sleep(1); |
|
238 |
#else |
|
239 |
os::naked_yield(); |
|
240 |
#endif |
|
241 |
} |
|
242 |
_lock_state = ExclusiveLock; |
|
243 |
} |
|
47634
6a0c42c40cd1
8188220: Remove Atomic::*_ptr() uses and overloads from hotspot
coleenp
parents:
47216
diff
changeset
|
244 |
|
6a0c42c40cd1
8188220: Remove Atomic::*_ptr() uses and overloads from hotspot
coleenp
parents:
47216
diff
changeset
|
245 |
bool MallocSiteHashtableEntry::atomic_insert(MallocSiteHashtableEntry* entry) { |
59252
623722a6aeb9
8234740: Harmonize parameter order in Atomic - cmpxchg
stefank
parents:
53685
diff
changeset
|
246 |
return Atomic::replace_if_null(&_next, entry); |
47634
6a0c42c40cd1
8188220: Remove Atomic::*_ptr() uses and overloads from hotspot
coleenp
parents:
47216
diff
changeset
|
247 |
} |