Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/services/mallocSiteTable.cpp
32285 views
/*1* Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/23#include "precompiled.hpp"242526#include "memory/allocation.inline.hpp"27#include "runtime/atomic.hpp"28#include "services/mallocSiteTable.hpp"2930/*31* Early os::malloc() calls come from initializations of static variables, long before entering any32* VM code. Upon the arrival of the first os::malloc() call, malloc site hashtable has to be33* initialized, along with the allocation site for the hashtable entries.34* To ensure that malloc site hashtable can be initialized without triggering any additional os::malloc()35* call, the hashtable bucket array and hashtable entry allocation site have to be static.36* It is not a problem for hashtable bucket, since it is an array of pointer type, C runtime just37* allocates a block memory and zero the memory for it.38* But for hashtable entry allocation site object, things get tricky. C runtime not only allocates39* memory for it, but also calls its constructor at some later time. If we initialize the allocation site40* at the first os::malloc() call, the object will be reinitialized when its constructor is called41* by C runtime.42* To workaround above issue, we declare a static size_t array with the size of the CallsiteHashtableEntry,43* the memory is used to instantiate CallsiteHashtableEntry for the hashtable entry allocation site.44* Given it is a primitive type array, C runtime will do nothing other than assign the memory block for the variable,45* which is exactly what we want.46* The same trick is also applied to create NativeCallStack object for CallsiteHashtableEntry memory allocation.47*48* Note: C++ object usually aligns to particular alignment, depends on compiler implementation, we declare49* the memory as size_t arrays, to ensure the memory is aligned to native machine word alignment.50*/5152// Reserve enough memory for NativeCallStack and MallocSiteHashtableEntry objects53size_t MallocSiteTable::_hash_entry_allocation_stack[CALC_OBJ_SIZE_IN_TYPE(NativeCallStack, size_t)];54size_t MallocSiteTable::_hash_entry_allocation_site[CALC_OBJ_SIZE_IN_TYPE(MallocSiteHashtableEntry, size_t)];5556// Malloc site hashtable buckets57MallocSiteHashtableEntry* MallocSiteTable::_table[MallocSiteTable::table_size];5859// concurrent access counter60volatile int MallocSiteTable::_access_count = 0;6162// Tracking hashtable contention63NOT_PRODUCT(int MallocSiteTable::_peak_count = 0;)646566/*67* Initialize malloc site table.68* Hashtable entry is malloc'd, so it can cause infinite recursion.69* To avoid above problem, we pre-initialize a hash entry for70* this allocation site.71* The method is called during C runtime static variable initialization72* time, it is in single-threaded mode from JVM perspective.73*/74bool MallocSiteTable::initialize() {75assert(sizeof(_hash_entry_allocation_stack) >= sizeof(NativeCallStack), "Sanity Check");76assert(sizeof(_hash_entry_allocation_site) >= sizeof(MallocSiteHashtableEntry),77"Sanity Check");78assert((size_t)table_size <= MAX_MALLOCSITE_TABLE_SIZE, "Hashtable overflow");7980// Fake the call stack for hashtable entry allocation81assert(NMT_TrackingStackDepth > 1, "At least one tracking stack");8283// Create pseudo call stack for hashtable entry allocation84address pc[3];85if (NMT_TrackingStackDepth >= 3) {86uintx *fp = (uintx*)MallocSiteTable::allocation_at;87// On ppc64, 'fp' is a pointer to a function descriptor which is a struct of88// three native pointers where the first pointer is the real function address.89// See: http://refspecs.linuxfoundation.org/ELF/ppc64/PPC-elf64abi-1.9.html#FUNC-DES90pc[2] = (address)(fp PPC64_ONLY(BIG_ENDIAN_ONLY([0])));91}92if (NMT_TrackingStackDepth >= 2) {93uintx *fp = (uintx*)MallocSiteTable::lookup_or_add;94pc[1] = (address)(fp PPC64_ONLY(BIG_ENDIAN_ONLY([0])));95}96uintx *fp = (uintx*)MallocSiteTable::new_entry;97pc[0] = (address)(fp PPC64_ONLY(BIG_ENDIAN_ONLY([0])));9899// Instantiate NativeCallStack object, have to use placement new operator. (see comments above)100NativeCallStack* stack = ::new ((void*)_hash_entry_allocation_stack)101NativeCallStack(pc, MIN2(((int)(sizeof(pc) / sizeof(address))), ((int)NMT_TrackingStackDepth)));102103// Instantiate hash entry for hashtable entry allocation callsite104MallocSiteHashtableEntry* entry = ::new ((void*)_hash_entry_allocation_site)105MallocSiteHashtableEntry(*stack, mtNMT);106107// Add the allocation site to hashtable.108int index = hash_to_index(stack->hash());109_table[index] = entry;110111return true;112}113114// Walks entries in the hashtable.115// It stops walk if the walker returns false.116bool MallocSiteTable::walk(MallocSiteWalker* walker) {117MallocSiteHashtableEntry* head;118for (int index = 0; index < table_size; index ++) {119head = _table[index];120while (head != NULL) {121if (!walker->do_malloc_site(head->peek())) {122return false;123}124head = (MallocSiteHashtableEntry*)head->next();125}126}127return true;128}129130/*131* The hashtable does not have deletion policy on individual entry,132* and each linked list node is inserted via compare-and-swap,133* so each linked list is stable, the contention only happens134* at the end of linked list.135* This method should not return NULL under normal circumstance.136* If NULL is returned, it indicates:137* 1. Out of memory, it cannot allocate new hash entry.138* 2. Overflow hash bucket.139* Under any of above circumstances, caller should handle the situation.140*/141MallocSite* MallocSiteTable::lookup_or_add(const NativeCallStack& key, size_t* bucket_idx,142size_t* pos_idx, MEMFLAGS flags) {143assert(flags != mtNone, "Should have a real memory type");144unsigned int index = hash_to_index(key.hash());145assert(index >= 0, "Negative index");146*bucket_idx = (size_t)index;147*pos_idx = 0;148149// First entry for this hash bucket150if (_table[index] == NULL) {151MallocSiteHashtableEntry* entry = new_entry(key, flags);152// OOM check153if (entry == NULL) return NULL;154155// swap in the head156if (Atomic::cmpxchg_ptr((void*)entry, (volatile void *)&_table[index], NULL) == NULL) {157return entry->data();158}159160delete entry;161}162163MallocSiteHashtableEntry* head = _table[index];164while (head != NULL && (*pos_idx) <= MAX_BUCKET_LENGTH) {165MallocSite* site = head->data();166if (site->flag() == flags && site->equals(key)) {167return head->data();168}169170if (head->next() == NULL && (*pos_idx) < MAX_BUCKET_LENGTH) {171MallocSiteHashtableEntry* entry = new_entry(key, flags);172// OOM check173if (entry == NULL) return NULL;174if (head->atomic_insert(entry)) {175(*pos_idx) ++;176return entry->data();177}178// contended, other thread won179delete entry;180}181head = (MallocSiteHashtableEntry*)head->next();182(*pos_idx) ++;183}184return NULL;185}186187// Access malloc site188MallocSite* MallocSiteTable::malloc_site(size_t bucket_idx, size_t pos_idx) {189assert(bucket_idx < table_size, "Invalid bucket index");190MallocSiteHashtableEntry* head = _table[bucket_idx];191for (size_t index = 0; index < pos_idx && head != NULL;192index ++, head = (MallocSiteHashtableEntry*)head->next());193assert(head != NULL, "Invalid position index");194return head->data();195}196197// Allocates MallocSiteHashtableEntry object. Special call stack198// (pre-installed allocation site) has to be used to avoid infinite199// recursion.200MallocSiteHashtableEntry* MallocSiteTable::new_entry(const NativeCallStack& key, MEMFLAGS flags) {201void* p = AllocateHeap(sizeof(MallocSiteHashtableEntry), mtNMT,202*hash_entry_allocation_stack(), AllocFailStrategy::RETURN_NULL);203return ::new (p) MallocSiteHashtableEntry(key, flags);204}205206void MallocSiteTable::reset() {207for (int index = 0; index < table_size; index ++) {208MallocSiteHashtableEntry* head = _table[index];209_table[index] = NULL;210delete_linked_list(head);211}212}213214void MallocSiteTable::delete_linked_list(MallocSiteHashtableEntry* head) {215MallocSiteHashtableEntry* p;216while (head != NULL) {217p = head;218head = (MallocSiteHashtableEntry*)head->next();219if (p != (MallocSiteHashtableEntry*)_hash_entry_allocation_site) {220delete p;221}222}223}224225void MallocSiteTable::shutdown() {226AccessLock locker(&_access_count);227locker.exclusiveLock();228reset();229}230231bool MallocSiteTable::walk_malloc_site(MallocSiteWalker* walker) {232assert(walker != NULL, "NuLL walker");233AccessLock locker(&_access_count);234if (locker.sharedLock()) {235NOT_PRODUCT(_peak_count = MAX2(_peak_count, _access_count);)236return walk(walker);237}238return false;239}240241242void MallocSiteTable::AccessLock::exclusiveLock() {243jint target;244jint val;245246assert(_lock_state != ExclusiveLock, "Can only call once");247assert(*_lock >= 0, "Can not content exclusive lock");248249// make counter negative to block out shared locks250do {251val = *_lock;252target = _MAGIC_ + *_lock;253} while (Atomic::cmpxchg(target, _lock, val) != val);254255// wait for all readers to exit256while (*_lock != _MAGIC_) {257#ifdef _WINDOWS258os::naked_short_sleep(1);259#else260os::NakedYield();261#endif262}263_lock_state = ExclusiveLock;264}265266267268269