Path: blob/master/src/hotspot/share/services/memTracker.hpp
64441 views
/*1* Copyright (c) 2013, 2021, 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_SERVICES_MEMTRACKER_HPP25#define SHARE_SERVICES_MEMTRACKER_HPP2627#include "services/nmtCommon.hpp"28#include "utilities/nativeCallStack.hpp"293031#if !INCLUDE_NMT3233#define CURRENT_PC NativeCallStack::empty_stack()34#define CALLER_PC NativeCallStack::empty_stack()3536class Tracker : public StackObj {37public:38enum TrackerType {39uncommit,40release41};42Tracker(enum TrackerType type) : _type(type) { }43void record(address addr, size_t size) { }44private:45enum TrackerType _type;46};4748class MemTracker : AllStatic {49public:50static inline NMT_TrackingLevel tracking_level() { return NMT_off; }51static inline void shutdown() { }52static inline void init() { }53static bool check_launcher_nmt_support(const char* value) { return true; }54static bool verify_nmt_option() { return true; }5556static inline void* record_malloc(void* mem_base, size_t size, MEMFLAGS flag,57const NativeCallStack& stack, NMT_TrackingLevel level) { return mem_base; }58static inline size_t malloc_header_size(NMT_TrackingLevel level) { return 0; }59static inline size_t malloc_header_size(void* memblock) { return 0; }60static inline void* malloc_base(void* memblock) { return memblock; }61static inline void* record_free(void* memblock, NMT_TrackingLevel level) { return memblock; }6263static inline void record_new_arena(MEMFLAGS flag) { }64static inline void record_arena_free(MEMFLAGS flag) { }65static inline void record_arena_size_change(ssize_t diff, MEMFLAGS flag) { }66static inline void record_virtual_memory_reserve(void* addr, size_t size, const NativeCallStack& stack,67MEMFLAGS flag = mtNone) { }68static inline void record_virtual_memory_reserve_and_commit(void* addr, size_t size,69const NativeCallStack& stack, MEMFLAGS flag = mtNone) { }70static inline void record_virtual_memory_split_reserved(void* addr, size_t size, size_t split) { }71static inline void record_virtual_memory_commit(void* addr, size_t size, const NativeCallStack& stack) { }72static inline void record_virtual_memory_type(void* addr, MEMFLAGS flag) { }73static inline void record_thread_stack(void* addr, size_t size) { }74static inline void release_thread_stack(void* addr, size_t size) { }7576static void final_report(outputStream*) { }77static void error_report(outputStream*) { }78};7980#else8182#include "runtime/mutexLocker.hpp"83#include "runtime/threadCritical.hpp"84#include "services/mallocTracker.hpp"85#include "services/threadStackTracker.hpp"86#include "services/virtualMemoryTracker.hpp"8788#define CURRENT_PC ((MemTracker::tracking_level() == NMT_detail) ? \89NativeCallStack(0) : NativeCallStack::empty_stack())90#define CALLER_PC ((MemTracker::tracking_level() == NMT_detail) ? \91NativeCallStack(1) : NativeCallStack::empty_stack())9293class MemBaseline;9495// Tracker is used for guarding 'release' semantics of virtual memory operation, to avoid96// the other thread obtains and records the same region that is just 'released' by current97// thread but before it can record the operation.98class Tracker : public StackObj {99public:100enum TrackerType {101uncommit,102release103};104105public:106Tracker(enum TrackerType type) : _type(type) { }107void record(address addr, size_t size);108private:109enum TrackerType _type;110// Virtual memory tracking data structures are protected by ThreadCritical lock.111ThreadCritical _tc;112};113114class MemTracker : AllStatic {115friend class VirtualMemoryTrackerTest;116117// Helper; asserts that we are in post-NMT-init phase118static void assert_post_init() {119assert(is_initialized(), "NMT not yet initialized.");120}121122public:123124// Initializes NMT to whatever -XX:NativeMemoryTracking says.125// - Can only be called once.126// - NativeMemoryTracking must be validated beforehand.127static void initialize();128129// Returns true if NMT had been initialized.130static bool is_initialized() {131return _tracking_level != NMT_unknown;132}133134static inline NMT_TrackingLevel tracking_level() {135return _tracking_level;136}137138// Shutdown native memory tracking.139// This transitions the tracking level:140// summary -> minimal141// detail -> minimal142static void shutdown();143144// Transition the tracking level to specified level145static bool transition_to(NMT_TrackingLevel level);146147static inline void* record_malloc(void* mem_base, size_t size, MEMFLAGS flag,148const NativeCallStack& stack, NMT_TrackingLevel level) {149if (level != NMT_off) {150return MallocTracker::record_malloc(mem_base, size, flag, stack, level);151}152return mem_base;153}154155static inline size_t malloc_header_size(NMT_TrackingLevel level) {156return MallocTracker::malloc_header_size(level);157}158159static size_t malloc_header_size(void* memblock) {160if (tracking_level() != NMT_off) {161return MallocTracker::get_header_size(memblock);162}163return 0;164}165166// To malloc base address, which is the starting address167// of malloc tracking header if tracking is enabled.168// Otherwise, it returns the same address.169static void* malloc_base(void* memblock);170171// Record malloc free and return malloc base address172static inline void* record_free(void* memblock, NMT_TrackingLevel level) {173// Never turned on174if (level == NMT_off || memblock == NULL) {175return memblock;176}177return MallocTracker::record_free(memblock);178}179180181// Record creation of an arena182static inline void record_new_arena(MEMFLAGS flag) {183if (tracking_level() < NMT_summary) return;184MallocTracker::record_new_arena(flag);185}186187// Record destruction of an arena188static inline void record_arena_free(MEMFLAGS flag) {189if (tracking_level() < NMT_summary) return;190MallocTracker::record_arena_free(flag);191}192193// Record arena size change. Arena size is the size of all arena194// chuncks that backing up the arena.195static inline void record_arena_size_change(ssize_t diff, MEMFLAGS flag) {196if (tracking_level() < NMT_summary) return;197MallocTracker::record_arena_size_change(diff, flag);198}199200// Note: virtual memory operations should only ever be called after NMT initialization201// (we do not do any reservations before that).202203static inline void record_virtual_memory_reserve(void* addr, size_t size, const NativeCallStack& stack,204MEMFLAGS flag = mtNone) {205assert_post_init();206if (tracking_level() < NMT_summary) return;207if (addr != NULL) {208ThreadCritical tc;209// Recheck to avoid potential racing during NMT shutdown210if (tracking_level() < NMT_summary) return;211VirtualMemoryTracker::add_reserved_region((address)addr, size, stack, flag);212}213}214215static inline void record_virtual_memory_reserve_and_commit(void* addr, size_t size,216const NativeCallStack& stack, MEMFLAGS flag = mtNone) {217assert_post_init();218if (tracking_level() < NMT_summary) return;219if (addr != NULL) {220ThreadCritical tc;221if (tracking_level() < NMT_summary) return;222VirtualMemoryTracker::add_reserved_region((address)addr, size, stack, flag);223VirtualMemoryTracker::add_committed_region((address)addr, size, stack);224}225}226227static inline void record_virtual_memory_commit(void* addr, size_t size,228const NativeCallStack& stack) {229assert_post_init();230if (tracking_level() < NMT_summary) return;231if (addr != NULL) {232ThreadCritical tc;233if (tracking_level() < NMT_summary) return;234VirtualMemoryTracker::add_committed_region((address)addr, size, stack);235}236}237238// Given an existing memory mapping registered with NMT and a splitting239// address, split the mapping in two. The memory region is supposed to240// be fully uncommitted.241//242// The two new memory regions will be both registered under stack and243// memory flags of the original region.244static inline void record_virtual_memory_split_reserved(void* addr, size_t size, size_t split) {245assert_post_init();246if (tracking_level() < NMT_summary) return;247if (addr != NULL) {248ThreadCritical tc;249// Recheck to avoid potential racing during NMT shutdown250if (tracking_level() < NMT_summary) return;251VirtualMemoryTracker::split_reserved_region((address)addr, size, split);252}253}254255static inline void record_virtual_memory_type(void* addr, MEMFLAGS flag) {256assert_post_init();257if (tracking_level() < NMT_summary) return;258if (addr != NULL) {259ThreadCritical tc;260if (tracking_level() < NMT_summary) return;261VirtualMemoryTracker::set_reserved_region_type((address)addr, flag);262}263}264265static void record_thread_stack(void* addr, size_t size) {266assert_post_init();267if (tracking_level() < NMT_summary) return;268if (addr != NULL) {269ThreadStackTracker::new_thread_stack((address)addr, size, CALLER_PC);270}271}272273static inline void release_thread_stack(void* addr, size_t size) {274assert_post_init();275if (tracking_level() < NMT_summary) return;276if (addr != NULL) {277ThreadStackTracker::delete_thread_stack((address)addr, size);278}279}280281// Query lock is used to synchronize the access to tracking data.282// So far, it is only used by JCmd query, but it may be used by283// other tools.284static inline Mutex* query_lock() {285assert(NMTQuery_lock != NULL, "not initialized!");286return NMTQuery_lock;287}288289// Report during error reporting.290static void error_report(outputStream* output);291292// Report when handling PrintNMTStatistics before VM shutdown.293static void final_report(outputStream* output);294295// Stored baseline296static inline MemBaseline& get_baseline() {297return _baseline;298}299300static NMT_TrackingLevel cmdline_tracking_level() {301return _cmdline_tracking_level;302}303304static void tuning_statistics(outputStream* out);305306private:307static NMT_TrackingLevel init_tracking_level();308static void report(bool summary_only, outputStream* output, size_t scale);309310private:311// Tracking level312static volatile NMT_TrackingLevel _tracking_level;313// If NMT option value passed by launcher through environment314// variable is valid315static bool _is_nmt_env_valid;316// command line tracking level317static NMT_TrackingLevel _cmdline_tracking_level;318// Stored baseline319static MemBaseline _baseline;320// Query lock321static Mutex* _query_lock;322};323324#endif // INCLUDE_NMT325326#endif // SHARE_SERVICES_MEMTRACKER_HPP327328329