author | zgu |
Wed, 11 Jul 2018 13:28:07 -0400 | |
changeset 50965 | 29eaf3feab30 |
parent 48488 | 51825789dd89 |
child 51038 | 3312e730c791 |
permissions | -rw-r--r-- |
25946 | 1 |
/* |
46489
40abcea5a9d5
8139673: NMT stack traces in output should show mtcomponent
zgu
parents:
29462
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 |
||
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) { |
|
60 |
pc[2] = (address)MallocSiteTable::allocation_at; |
|
61 |
} |
|
62 |
if (NMT_TrackingStackDepth >= 2) { |
|
63 |
pc[1] = (address)MallocSiteTable::lookup_or_add; |
|
64 |
} |
|
65 |
pc[0] = (address)MallocSiteTable::new_entry; |
|
66 |
||
50965
29eaf3feab30
8206183: Possible construct EMPTY_STACK and allocation stack, etc. on first use
zgu
parents:
48488
diff
changeset
|
67 |
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
|
68 |
static const MallocSiteHashtableEntry entry(stack, mtNMT); |
25946 | 69 |
|
50965
29eaf3feab30
8206183: Possible construct EMPTY_STACK and allocation stack, etc. on first use
zgu
parents:
48488
diff
changeset
|
70 |
assert(_hash_entry_allocation_stack == NULL && |
29eaf3feab30
8206183: Possible construct EMPTY_STACK and allocation stack, etc. on first use
zgu
parents:
48488
diff
changeset
|
71 |
_hash_entry_allocation_site == NULL, |
29eaf3feab30
8206183: Possible construct EMPTY_STACK and allocation stack, etc. on first use
zgu
parents:
48488
diff
changeset
|
72 |
"Already initailized"); |
29eaf3feab30
8206183: Possible construct EMPTY_STACK and allocation stack, etc. on first use
zgu
parents:
48488
diff
changeset
|
73 |
|
29eaf3feab30
8206183: Possible construct EMPTY_STACK and allocation stack, etc. on first use
zgu
parents:
48488
diff
changeset
|
74 |
_hash_entry_allocation_stack = &stack; |
29eaf3feab30
8206183: Possible construct EMPTY_STACK and allocation stack, etc. on first use
zgu
parents:
48488
diff
changeset
|
75 |
_hash_entry_allocation_site = &entry; |
25946 | 76 |
|
77 |
// 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
|
78 |
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
|
79 |
_table[index] = const_cast<MallocSiteHashtableEntry*>(&entry); |
25946 | 80 |
|
81 |
return true; |
|
82 |
} |
|
83 |
||
84 |
// Walks entries in the hashtable. |
|
85 |
// It stops walk if the walker returns false. |
|
86 |
bool MallocSiteTable::walk(MallocSiteWalker* walker) { |
|
87 |
MallocSiteHashtableEntry* head; |
|
88 |
for (int index = 0; index < table_size; index ++) { |
|
89 |
head = _table[index]; |
|
90 |
while (head != NULL) { |
|
91 |
if (!walker->do_malloc_site(head->peek())) { |
|
92 |
return false; |
|
93 |
} |
|
94 |
head = (MallocSiteHashtableEntry*)head->next(); |
|
95 |
} |
|
96 |
} |
|
97 |
return true; |
|
98 |
} |
|
99 |
||
100 |
/* |
|
101 |
* The hashtable does not have deletion policy on individual entry, |
|
102 |
* and each linked list node is inserted via compare-and-swap, |
|
103 |
* so each linked list is stable, the contention only happens |
|
104 |
* at the end of linked list. |
|
105 |
* This method should not return NULL under normal circumstance. |
|
106 |
* If NULL is returned, it indicates: |
|
107 |
* 1. Out of memory, it cannot allocate new hash entry. |
|
108 |
* 2. Overflow hash bucket. |
|
109 |
* Under any of above circumstances, caller should handle the situation. |
|
110 |
*/ |
|
111 |
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
|
112 |
size_t* pos_idx, MEMFLAGS flags) { |
40abcea5a9d5
8139673: NMT stack traces in output should show mtcomponent
zgu
parents:
29462
diff
changeset
|
113 |
assert(flags != mtNone, "Should have a real memory type"); |
29462
f2e2922222a4
8069124: runtime/NMT/MallocSiteHashOverflow.java failing in nightlies
ctornqvi
parents:
28953
diff
changeset
|
114 |
unsigned int index = hash_to_index(key.hash()); |
25946 | 115 |
*bucket_idx = (size_t)index; |
116 |
*pos_idx = 0; |
|
117 |
||
118 |
// First entry for this hash bucket |
|
119 |
if (_table[index] == NULL) { |
|
46489
40abcea5a9d5
8139673: NMT stack traces in output should show mtcomponent
zgu
parents:
29462
diff
changeset
|
120 |
MallocSiteHashtableEntry* entry = new_entry(key, flags); |
25946 | 121 |
// OOM check |
122 |
if (entry == NULL) return NULL; |
|
123 |
||
124 |
// swap in the head |
|
48488 | 125 |
if (Atomic::replace_if_null(entry, &_table[index])) { |
25946 | 126 |
return entry->data(); |
127 |
} |
|
128 |
||
129 |
delete entry; |
|
130 |
} |
|
131 |
||
132 |
MallocSiteHashtableEntry* head = _table[index]; |
|
133 |
while (head != NULL && (*pos_idx) <= MAX_BUCKET_LENGTH) { |
|
134 |
MallocSite* site = head->data(); |
|
46489
40abcea5a9d5
8139673: NMT stack traces in output should show mtcomponent
zgu
parents:
29462
diff
changeset
|
135 |
if (site->flags() == flags && site->equals(key)) { |
25946 | 136 |
return head->data(); |
137 |
} |
|
138 |
||
139 |
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
|
140 |
MallocSiteHashtableEntry* entry = new_entry(key, flags); |
25946 | 141 |
// OOM check |
142 |
if (entry == NULL) return NULL; |
|
143 |
if (head->atomic_insert(entry)) { |
|
144 |
(*pos_idx) ++; |
|
145 |
return entry->data(); |
|
146 |
} |
|
147 |
// contended, other thread won |
|
148 |
delete entry; |
|
149 |
} |
|
150 |
head = (MallocSiteHashtableEntry*)head->next(); |
|
151 |
(*pos_idx) ++; |
|
152 |
} |
|
153 |
return NULL; |
|
154 |
} |
|
155 |
||
156 |
// Access malloc site |
|
157 |
MallocSite* MallocSiteTable::malloc_site(size_t bucket_idx, size_t pos_idx) { |
|
158 |
assert(bucket_idx < table_size, "Invalid bucket index"); |
|
159 |
MallocSiteHashtableEntry* head = _table[bucket_idx]; |
|
46630
75aa3e39d02c
8182299: Enable disabled clang warnings, build on OSX 10 + Xcode 8
jwilhelm
parents:
46489
diff
changeset
|
160 |
for (size_t index = 0; |
75aa3e39d02c
8182299: Enable disabled clang warnings, build on OSX 10 + Xcode 8
jwilhelm
parents:
46489
diff
changeset
|
161 |
index < pos_idx && head != NULL; |
75aa3e39d02c
8182299: Enable disabled clang warnings, build on OSX 10 + Xcode 8
jwilhelm
parents:
46489
diff
changeset
|
162 |
index++, head = (MallocSiteHashtableEntry*)head->next()) {} |
25946 | 163 |
assert(head != NULL, "Invalid position index"); |
164 |
return head->data(); |
|
165 |
} |
|
166 |
||
167 |
// Allocates MallocSiteHashtableEntry object. Special call stack |
|
168 |
// (pre-installed allocation site) has to be used to avoid infinite |
|
169 |
// recursion. |
|
46489
40abcea5a9d5
8139673: NMT stack traces in output should show mtcomponent
zgu
parents:
29462
diff
changeset
|
170 |
MallocSiteHashtableEntry* MallocSiteTable::new_entry(const NativeCallStack& key, MEMFLAGS flags) { |
25946 | 171 |
void* p = AllocateHeap(sizeof(MallocSiteHashtableEntry), mtNMT, |
172 |
*hash_entry_allocation_stack(), AllocFailStrategy::RETURN_NULL); |
|
46489
40abcea5a9d5
8139673: NMT stack traces in output should show mtcomponent
zgu
parents:
29462
diff
changeset
|
173 |
return ::new (p) MallocSiteHashtableEntry(key, flags); |
25946 | 174 |
} |
175 |
||
176 |
void MallocSiteTable::reset() { |
|
177 |
for (int index = 0; index < table_size; index ++) { |
|
178 |
MallocSiteHashtableEntry* head = _table[index]; |
|
179 |
_table[index] = NULL; |
|
180 |
delete_linked_list(head); |
|
181 |
} |
|
50965
29eaf3feab30
8206183: Possible construct EMPTY_STACK and allocation stack, etc. on first use
zgu
parents:
48488
diff
changeset
|
182 |
|
29eaf3feab30
8206183: Possible construct EMPTY_STACK and allocation stack, etc. on first use
zgu
parents:
48488
diff
changeset
|
183 |
_hash_entry_allocation_stack = NULL; |
29eaf3feab30
8206183: Possible construct EMPTY_STACK and allocation stack, etc. on first use
zgu
parents:
48488
diff
changeset
|
184 |
_hash_entry_allocation_site = NULL; |
25946 | 185 |
} |
186 |
||
187 |
void MallocSiteTable::delete_linked_list(MallocSiteHashtableEntry* head) { |
|
188 |
MallocSiteHashtableEntry* p; |
|
189 |
while (head != NULL) { |
|
190 |
p = head; |
|
191 |
head = (MallocSiteHashtableEntry*)head->next(); |
|
50965
29eaf3feab30
8206183: Possible construct EMPTY_STACK and allocation stack, etc. on first use
zgu
parents:
48488
diff
changeset
|
192 |
if (p != hash_entry_allocation_site()) { |
25946 | 193 |
delete p; |
194 |
} |
|
195 |
} |
|
196 |
} |
|
197 |
||
198 |
void MallocSiteTable::shutdown() { |
|
199 |
AccessLock locker(&_access_count); |
|
200 |
locker.exclusiveLock(); |
|
201 |
reset(); |
|
202 |
} |
|
203 |
||
204 |
bool MallocSiteTable::walk_malloc_site(MallocSiteWalker* walker) { |
|
205 |
assert(walker != NULL, "NuLL walker"); |
|
206 |
AccessLock locker(&_access_count); |
|
207 |
if (locker.sharedLock()) { |
|
208 |
NOT_PRODUCT(_peak_count = MAX2(_peak_count, _access_count);) |
|
209 |
return walk(walker); |
|
210 |
} |
|
211 |
return false; |
|
212 |
} |
|
213 |
||
214 |
||
215 |
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
|
216 |
int target; |
b7c7428eaab9
8189610: Reconcile jvm.h and all jvm_md.h between java.base and hotspot
coleenp
parents:
47634
diff
changeset
|
217 |
int val; |
25946 | 218 |
|
219 |
assert(_lock_state != ExclusiveLock, "Can only call once"); |
|
220 |
assert(*_lock >= 0, "Can not content exclusive lock"); |
|
221 |
||
222 |
// make counter negative to block out shared locks |
|
223 |
do { |
|
224 |
val = *_lock; |
|
225 |
target = _MAGIC_ + *_lock; |
|
226 |
} while (Atomic::cmpxchg(target, _lock, val) != val); |
|
227 |
||
228 |
// wait for all readers to exit |
|
229 |
while (*_lock != _MAGIC_) { |
|
230 |
#ifdef _WINDOWS |
|
231 |
os::naked_short_sleep(1); |
|
232 |
#else |
|
233 |
os::naked_yield(); |
|
234 |
#endif |
|
235 |
} |
|
236 |
_lock_state = ExclusiveLock; |
|
237 |
} |
|
47634
6a0c42c40cd1
8188220: Remove Atomic::*_ptr() uses and overloads from hotspot
coleenp
parents:
47216
diff
changeset
|
238 |
|
6a0c42c40cd1
8188220: Remove Atomic::*_ptr() uses and overloads from hotspot
coleenp
parents:
47216
diff
changeset
|
239 |
bool MallocSiteHashtableEntry::atomic_insert(MallocSiteHashtableEntry* entry) { |
48488 | 240 |
return Atomic::replace_if_null(entry, &_next); |
47634
6a0c42c40cd1
8188220: Remove Atomic::*_ptr() uses and overloads from hotspot
coleenp
parents:
47216
diff
changeset
|
241 |
} |