Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/services/mallocSiteTable.hpp
32285 views
/*1* Copyright (c) 2014, 2018, 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*/2324#ifndef SHARE_VM_SERVICES_MALLOC_SITE_TABLE_HPP25#define SHARE_VM_SERVICES_MALLOC_SITE_TABLE_HPP2627#if INCLUDE_NMT2829#include "memory/allocation.hpp"30#include "runtime/atomic.hpp"31#include "services/allocationSite.hpp"32#include "services/mallocTracker.hpp"33#include "services/nmtCommon.hpp"34#include "utilities/nativeCallStack.hpp"3536// MallocSite represents a code path that eventually calls37// os::malloc() to allocate memory38class MallocSite : public AllocationSite<MemoryCounter> {39public:40MallocSite() :41AllocationSite<MemoryCounter>(NativeCallStack::empty_stack(), mtNone) {}4243MallocSite(const NativeCallStack& stack, MEMFLAGS flags) :44AllocationSite<MemoryCounter>(stack, flags) {}454647void allocate(size_t size) { data()->allocate(size); }48void deallocate(size_t size) { data()->deallocate(size); }4950// Memory allocated from this code path51size_t size() const { return peek()->size(); }52// The number of calls were made53size_t count() const { return peek()->count(); }54};5556// Malloc site hashtable entry57class MallocSiteHashtableEntry : public CHeapObj<mtNMT> {58private:59MallocSite _malloc_site;60MallocSiteHashtableEntry* _next;6162public:63MallocSiteHashtableEntry() : _next(NULL) { }6465MallocSiteHashtableEntry(NativeCallStack stack, MEMFLAGS flags):66_malloc_site(stack, flags), _next(NULL) {67assert(flags != mtNone, "Expect a real memory type");68}6970inline const MallocSiteHashtableEntry* next() const {71return _next;72}7374// Insert an entry atomically.75// Return true if the entry is inserted successfully.76// The operation can be failed due to contention from other thread.77bool atomic_insert(const MallocSiteHashtableEntry* entry) {78return (Atomic::cmpxchg_ptr((void*)entry, (volatile void*)&_next,79NULL) == NULL);80}8182void set_callsite(const MallocSite& site) {83_malloc_site = site;84}8586inline const MallocSite* peek() const { return &_malloc_site; }87inline MallocSite* data() { return &_malloc_site; }8889inline long hash() const { return _malloc_site.hash(); }90inline bool equals(const NativeCallStack& stack) const {91return _malloc_site.equals(stack);92}93// Allocation/deallocation on this allocation site94inline void allocate(size_t size) { _malloc_site.allocate(size); }95inline void deallocate(size_t size) { _malloc_site.deallocate(size); }96// Memory counters97inline size_t size() const { return _malloc_site.size(); }98inline size_t count() const { return _malloc_site.count(); }99};100101// The walker walks every entry on MallocSiteTable102class MallocSiteWalker : public StackObj {103public:104virtual bool do_malloc_site(const MallocSite* e) { return false; }105};106107/*108* Native memory tracking call site table.109* The table is only needed when detail tracking is enabled.110*/111class MallocSiteTable : AllStatic {112private:113// The number of hash bucket in this hashtable. The number should114// be tuned if malloc activities changed significantly.115// The statistics data can be obtained via Jcmd116// jcmd <pid> VM.native_memory statistics.117118// Currently, (number of buckets / number of entires) ratio is119// about 1 / 6120enum {121table_base_size = 128, // The base size is calculated from statistics to give122// table ratio around 1:6123table_size = (table_base_size * NMT_TrackingStackDepth - 1)124};125126127// This is a very special lock, that allows multiple shared accesses (sharedLock), but128// once exclusive access (exclusiveLock) is requested, all shared accesses are129// rejected forever.130class AccessLock : public StackObj {131enum LockState {132NoLock,133SharedLock,134ExclusiveLock135};136137private:138// A very large negative number. The only possibility to "overflow"139// this number is when there are more than -min_jint threads in140// this process, which is not going to happen in foreseeable future.141const static int _MAGIC_ = min_jint;142143LockState _lock_state;144volatile int* _lock;145public:146AccessLock(volatile int* lock) :147_lock(lock), _lock_state(NoLock) {148}149150~AccessLock() {151if (_lock_state == SharedLock) {152Atomic::dec((volatile jint*)_lock);153}154}155// Acquire shared lock.156// Return true if shared access is granted.157inline bool sharedLock() {158jint res = Atomic::add(1, _lock);159if (res < 0) {160Atomic::add(-1, _lock);161return false;162}163_lock_state = SharedLock;164return true;165}166// Acquire exclusive lock167void exclusiveLock();168};169170public:171static bool initialize();172static void shutdown();173174NOT_PRODUCT(static int access_peak_count() { return _peak_count; })175176// Number of hash buckets177static inline int hash_buckets() { return (int)table_size; }178179// Access and copy a call stack from this table. Shared lock should be180// acquired before access the entry.181static inline bool access_stack(NativeCallStack& stack, size_t bucket_idx,182size_t pos_idx) {183AccessLock locker(&_access_count);184if (locker.sharedLock()) {185NOT_PRODUCT(_peak_count = MAX2(_peak_count, _access_count);)186MallocSite* site = malloc_site(bucket_idx, pos_idx);187if (site != NULL) {188stack = *site->call_stack();189return true;190}191}192return false;193}194195// Record a new allocation from specified call path.196// Return true if the allocation is recorded successfully, bucket_idx197// and pos_idx are also updated to indicate the entry where the allocation198// information was recorded.199// Return false only occurs under rare scenarios:200// 1. out of memory201// 2. overflow hash bucket202static inline bool allocation_at(const NativeCallStack& stack, size_t size,203size_t* bucket_idx, size_t* pos_idx, MEMFLAGS flags) {204AccessLock locker(&_access_count);205if (locker.sharedLock()) {206NOT_PRODUCT(_peak_count = MAX2(_peak_count, _access_count);)207MallocSite* site = lookup_or_add(stack, bucket_idx, pos_idx, flags);208if (site != NULL) site->allocate(size);209return site != NULL;210}211return false;212}213214// Record memory deallocation. bucket_idx and pos_idx indicate where the allocation215// information was recorded.216static inline bool deallocation_at(size_t size, size_t bucket_idx, size_t pos_idx) {217AccessLock locker(&_access_count);218if (locker.sharedLock()) {219NOT_PRODUCT(_peak_count = MAX2(_peak_count, _access_count);)220MallocSite* site = malloc_site(bucket_idx, pos_idx);221if (site != NULL) {222site->deallocate(size);223return true;224}225}226return false;227}228229// Walk this table.230static bool walk_malloc_site(MallocSiteWalker* walker);231232private:233static MallocSiteHashtableEntry* new_entry(const NativeCallStack& key, MEMFLAGS flags);234static void reset();235236// Delete a bucket linked list237static void delete_linked_list(MallocSiteHashtableEntry* head);238239static MallocSite* lookup_or_add(const NativeCallStack& key, size_t* bucket_idx, size_t* pos_idx, MEMFLAGS flags);240static MallocSite* malloc_site(size_t bucket_idx, size_t pos_idx);241static bool walk(MallocSiteWalker* walker);242243static inline unsigned int hash_to_index(unsigned int hash) {244return (hash % table_size);245}246247static inline const NativeCallStack* hash_entry_allocation_stack() {248return (NativeCallStack*)_hash_entry_allocation_stack;249}250251private:252// Counter for counting concurrent access253static volatile int _access_count;254255// The callsite hashtable. It has to be a static table,256// since malloc call can come from C runtime linker.257static MallocSiteHashtableEntry* _table[table_size];258259260// Reserve enough memory for placing the objects261262// The memory for hashtable entry allocation stack object263static size_t _hash_entry_allocation_stack[CALC_OBJ_SIZE_IN_TYPE(NativeCallStack, size_t)];264// The memory for hashtable entry allocation callsite object265static size_t _hash_entry_allocation_site[CALC_OBJ_SIZE_IN_TYPE(MallocSiteHashtableEntry, size_t)];266NOT_PRODUCT(static int _peak_count;)267};268269#endif // INCLUDE_NMT270#endif // SHARE_VM_SERVICES_MALLOC_SITE_TABLE_HPP271272273