Thu, 07 Nov 2019 17:56:14 -0500
8206173: MallocSiteTable::initialize() doesn't take function descriptors into account
Reviewed-by: stuefe, zgu
1 /*
2 * Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved.
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"
27 #include "memory/allocation.inline.hpp"
28 #include "runtime/atomic.hpp"
29 #include "services/mallocSiteTable.hpp"
31 /*
32 * Early os::malloc() calls come from initializations of static variables, long before entering any
33 * VM code. Upon the arrival of the first os::malloc() call, malloc site hashtable has to be
34 * initialized, along with the allocation site for the hashtable entries.
35 * To ensure that malloc site hashtable can be initialized without triggering any additional os::malloc()
36 * call, the hashtable bucket array and hashtable entry allocation site have to be static.
37 * It is not a problem for hashtable bucket, since it is an array of pointer type, C runtime just
38 * allocates a block memory and zero the memory for it.
39 * But for hashtable entry allocation site object, things get tricky. C runtime not only allocates
40 * memory for it, but also calls its constructor at some later time. If we initialize the allocation site
41 * at the first os::malloc() call, the object will be reinitialized when its constructor is called
42 * by C runtime.
43 * To workaround above issue, we declare a static size_t array with the size of the CallsiteHashtableEntry,
44 * the memory is used to instantiate CallsiteHashtableEntry for the hashtable entry allocation site.
45 * Given it is a primitive type array, C runtime will do nothing other than assign the memory block for the variable,
46 * which is exactly what we want.
47 * The same trick is also applied to create NativeCallStack object for CallsiteHashtableEntry memory allocation.
48 *
49 * Note: C++ object usually aligns to particular alignment, depends on compiler implementation, we declare
50 * the memory as size_t arrays, to ensure the memory is aligned to native machine word alignment.
51 */
53 // Reserve enough memory for NativeCallStack and MallocSiteHashtableEntry objects
54 size_t MallocSiteTable::_hash_entry_allocation_stack[CALC_OBJ_SIZE_IN_TYPE(NativeCallStack, size_t)];
55 size_t MallocSiteTable::_hash_entry_allocation_site[CALC_OBJ_SIZE_IN_TYPE(MallocSiteHashtableEntry, size_t)];
57 // Malloc site hashtable buckets
58 MallocSiteHashtableEntry* MallocSiteTable::_table[MallocSiteTable::table_size];
60 // concurrent access counter
61 volatile int MallocSiteTable::_access_count = 0;
63 // Tracking hashtable contention
64 NOT_PRODUCT(int MallocSiteTable::_peak_count = 0;)
67 /*
68 * Initialize malloc site table.
69 * Hashtable entry is malloc'd, so it can cause infinite recursion.
70 * To avoid above problem, we pre-initialize a hash entry for
71 * this allocation site.
72 * The method is called during C runtime static variable initialization
73 * time, it is in single-threaded mode from JVM perspective.
74 */
75 bool MallocSiteTable::initialize() {
76 assert(sizeof(_hash_entry_allocation_stack) >= sizeof(NativeCallStack), "Sanity Check");
77 assert(sizeof(_hash_entry_allocation_site) >= sizeof(MallocSiteHashtableEntry),
78 "Sanity Check");
79 assert((size_t)table_size <= MAX_MALLOCSITE_TABLE_SIZE, "Hashtable overflow");
81 // Fake the call stack for hashtable entry allocation
82 assert(NMT_TrackingStackDepth > 1, "At least one tracking stack");
84 // Create pseudo call stack for hashtable entry allocation
85 address pc[3];
86 if (NMT_TrackingStackDepth >= 3) {
87 uintx *fp = (uintx*)MallocSiteTable::allocation_at;
88 // On ppc64, 'fp' is a pointer to a function descriptor which is a struct of
89 // three native pointers where the first pointer is the real function address.
90 // See: http://refspecs.linuxfoundation.org/ELF/ppc64/PPC-elf64abi-1.9.html#FUNC-DES
91 pc[2] = (address)(fp PPC64_ONLY(BIG_ENDIAN_ONLY([0])));
92 }
93 if (NMT_TrackingStackDepth >= 2) {
94 uintx *fp = (uintx*)MallocSiteTable::lookup_or_add;
95 pc[1] = (address)(fp PPC64_ONLY(BIG_ENDIAN_ONLY([0])));
96 }
97 uintx *fp = (uintx*)MallocSiteTable::new_entry;
98 pc[0] = (address)(fp PPC64_ONLY(BIG_ENDIAN_ONLY([0])));
100 // Instantiate NativeCallStack object, have to use placement new operator. (see comments above)
101 NativeCallStack* stack = ::new ((void*)_hash_entry_allocation_stack)
102 NativeCallStack(pc, MIN2(((int)(sizeof(pc) / sizeof(address))), ((int)NMT_TrackingStackDepth)));
104 // Instantiate hash entry for hashtable entry allocation callsite
105 MallocSiteHashtableEntry* entry = ::new ((void*)_hash_entry_allocation_site)
106 MallocSiteHashtableEntry(*stack, mtNMT);
108 // Add the allocation site to hashtable.
109 int index = hash_to_index(stack->hash());
110 _table[index] = entry;
112 return true;
113 }
115 // Walks entries in the hashtable.
116 // It stops walk if the walker returns false.
117 bool MallocSiteTable::walk(MallocSiteWalker* walker) {
118 MallocSiteHashtableEntry* head;
119 for (int index = 0; index < table_size; index ++) {
120 head = _table[index];
121 while (head != NULL) {
122 if (!walker->do_malloc_site(head->peek())) {
123 return false;
124 }
125 head = (MallocSiteHashtableEntry*)head->next();
126 }
127 }
128 return true;
129 }
131 /*
132 * The hashtable does not have deletion policy on individual entry,
133 * and each linked list node is inserted via compare-and-swap,
134 * so each linked list is stable, the contention only happens
135 * at the end of linked list.
136 * This method should not return NULL under normal circumstance.
137 * If NULL is returned, it indicates:
138 * 1. Out of memory, it cannot allocate new hash entry.
139 * 2. Overflow hash bucket.
140 * Under any of above circumstances, caller should handle the situation.
141 */
142 MallocSite* MallocSiteTable::lookup_or_add(const NativeCallStack& key, size_t* bucket_idx,
143 size_t* pos_idx, MEMFLAGS flags) {
144 assert(flags != mtNone, "Should have a real memory type");
145 unsigned int index = hash_to_index(key.hash());
146 assert(index >= 0, "Negative index");
147 *bucket_idx = (size_t)index;
148 *pos_idx = 0;
150 // First entry for this hash bucket
151 if (_table[index] == NULL) {
152 MallocSiteHashtableEntry* entry = new_entry(key, flags);
153 // OOM check
154 if (entry == NULL) return NULL;
156 // swap in the head
157 if (Atomic::cmpxchg_ptr((void*)entry, (volatile void *)&_table[index], NULL) == NULL) {
158 return entry->data();
159 }
161 delete entry;
162 }
164 MallocSiteHashtableEntry* head = _table[index];
165 while (head != NULL && (*pos_idx) <= MAX_BUCKET_LENGTH) {
166 MallocSite* site = head->data();
167 if (site->flag() == flags && site->equals(key)) {
168 return head->data();
169 }
171 if (head->next() == NULL && (*pos_idx) < MAX_BUCKET_LENGTH) {
172 MallocSiteHashtableEntry* entry = new_entry(key, flags);
173 // OOM check
174 if (entry == NULL) return NULL;
175 if (head->atomic_insert(entry)) {
176 (*pos_idx) ++;
177 return entry->data();
178 }
179 // contended, other thread won
180 delete entry;
181 }
182 head = (MallocSiteHashtableEntry*)head->next();
183 (*pos_idx) ++;
184 }
185 return NULL;
186 }
188 // Access malloc site
189 MallocSite* MallocSiteTable::malloc_site(size_t bucket_idx, size_t pos_idx) {
190 assert(bucket_idx < table_size, "Invalid bucket index");
191 MallocSiteHashtableEntry* head = _table[bucket_idx];
192 for (size_t index = 0; index < pos_idx && head != NULL;
193 index ++, head = (MallocSiteHashtableEntry*)head->next());
194 assert(head != NULL, "Invalid position index");
195 return head->data();
196 }
198 // Allocates MallocSiteHashtableEntry object. Special call stack
199 // (pre-installed allocation site) has to be used to avoid infinite
200 // recursion.
201 MallocSiteHashtableEntry* MallocSiteTable::new_entry(const NativeCallStack& key, MEMFLAGS flags) {
202 void* p = AllocateHeap(sizeof(MallocSiteHashtableEntry), mtNMT,
203 *hash_entry_allocation_stack(), AllocFailStrategy::RETURN_NULL);
204 return ::new (p) MallocSiteHashtableEntry(key, flags);
205 }
207 void MallocSiteTable::reset() {
208 for (int index = 0; index < table_size; index ++) {
209 MallocSiteHashtableEntry* head = _table[index];
210 _table[index] = NULL;
211 delete_linked_list(head);
212 }
213 }
215 void MallocSiteTable::delete_linked_list(MallocSiteHashtableEntry* head) {
216 MallocSiteHashtableEntry* p;
217 while (head != NULL) {
218 p = head;
219 head = (MallocSiteHashtableEntry*)head->next();
220 if (p != (MallocSiteHashtableEntry*)_hash_entry_allocation_site) {
221 delete p;
222 }
223 }
224 }
226 void MallocSiteTable::shutdown() {
227 AccessLock locker(&_access_count);
228 locker.exclusiveLock();
229 reset();
230 }
232 bool MallocSiteTable::walk_malloc_site(MallocSiteWalker* walker) {
233 assert(walker != NULL, "NuLL walker");
234 AccessLock locker(&_access_count);
235 if (locker.sharedLock()) {
236 NOT_PRODUCT(_peak_count = MAX2(_peak_count, _access_count);)
237 return walk(walker);
238 }
239 return false;
240 }
243 void MallocSiteTable::AccessLock::exclusiveLock() {
244 jint target;
245 jint val;
247 assert(_lock_state != ExclusiveLock, "Can only call once");
248 assert(*_lock >= 0, "Can not content exclusive lock");
250 // make counter negative to block out shared locks
251 do {
252 val = *_lock;
253 target = _MAGIC_ + *_lock;
254 } while (Atomic::cmpxchg(target, _lock, val) != val);
256 // wait for all readers to exit
257 while (*_lock != _MAGIC_) {
258 #ifdef _WINDOWS
259 os::naked_short_sleep(1);
260 #else
261 os::NakedYield();
262 #endif
263 }
264 _lock_state = ExclusiveLock;
265 }