zgu@3900: /* zgu@4980: * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. zgu@3900: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. zgu@3900: * zgu@3900: * This code is free software; you can redistribute it and/or modify it zgu@3900: * under the terms of the GNU General Public License version 2 only, as zgu@3900: * published by the Free Software Foundation. zgu@3900: * zgu@3900: * This code is distributed in the hope that it will be useful, but WITHOUT zgu@3900: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or zgu@3900: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License zgu@3900: * version 2 for more details (a copy is included in the LICENSE file that zgu@3900: * accompanied this code). zgu@3900: * zgu@3900: * You should have received a copy of the GNU General Public License version zgu@3900: * 2 along with this work; if not, write to the Free Software Foundation, zgu@3900: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. zgu@3900: * zgu@3900: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA zgu@3900: * or visit www.oracle.com if you need additional information or have any zgu@3900: * questions. zgu@3900: * zgu@3900: */ zgu@3900: zgu@3900: #ifndef SHARE_VM_SERVICES_MEM_BASELINE_HPP zgu@3900: #define SHARE_VM_SERVICES_MEM_BASELINE_HPP zgu@3900: zgu@3900: #include "memory/allocation.hpp" zgu@3900: #include "runtime/mutex.hpp" zgu@3900: #include "services/memPtr.hpp" zgu@3900: #include "services/memSnapshot.hpp" zgu@3900: zgu@3900: // compare unsigned number zgu@3900: #define UNSIGNED_COMPARE(a, b) ((a > b) ? 1 : ((a == b) ? 0 : -1)) zgu@3900: zgu@3900: /* zgu@3900: * MallocCallsitePointer and VMCallsitePointer are used zgu@3900: * to baseline memory blocks with their callsite information. zgu@3900: * They are only available when detail tracking is turned zgu@3900: * on. zgu@3900: */ zgu@3900: zgu@3900: /* baselined malloc record aggregated by callsite */ zgu@3900: class MallocCallsitePointer : public MemPointer { zgu@3900: private: zgu@3900: size_t _count; // number of malloc invocation from this callsite zgu@3900: size_t _amount; // total amount of memory malloc-ed from this callsite zgu@3900: zgu@3900: public: zgu@3900: MallocCallsitePointer() { zgu@3900: _count = 0; zgu@3900: _amount = 0; zgu@3900: } zgu@3900: zgu@3900: MallocCallsitePointer(address pc) : MemPointer(pc) { zgu@3900: _count = 0; zgu@3900: _amount = 0; zgu@3900: } zgu@3900: zgu@3900: MallocCallsitePointer& operator=(const MallocCallsitePointer& p) { zgu@3900: MemPointer::operator=(p); zgu@3900: _count = p.count(); zgu@3900: _amount = p.amount(); zgu@3900: return *this; zgu@3900: } zgu@3900: zgu@3900: inline void inc(size_t size) { zgu@3900: _count ++; zgu@3900: _amount += size; zgu@3900: }; zgu@3900: zgu@3900: inline size_t count() const { zgu@3900: return _count; zgu@3900: } zgu@3900: zgu@3900: inline size_t amount() const { zgu@3900: return _amount; zgu@3900: } zgu@3900: }; zgu@3900: zgu@3900: // baselined virtual memory record aggregated by callsite zgu@3900: class VMCallsitePointer : public MemPointer { zgu@3900: private: zgu@3900: size_t _count; // number of invocation from this callsite zgu@3900: size_t _reserved_amount; // total reserved amount zgu@3900: size_t _committed_amount; // total committed amount zgu@3900: zgu@3900: public: zgu@3900: VMCallsitePointer() { zgu@3900: _count = 0; zgu@3900: _reserved_amount = 0; zgu@3900: _committed_amount = 0; zgu@3900: } zgu@3900: zgu@3900: VMCallsitePointer(address pc) : MemPointer(pc) { zgu@3900: _count = 0; zgu@3900: _reserved_amount = 0; zgu@3900: _committed_amount = 0; zgu@3900: } zgu@3900: zgu@3900: VMCallsitePointer& operator=(const VMCallsitePointer& p) { zgu@3900: MemPointer::operator=(p); zgu@3900: _count = p.count(); zgu@3900: _reserved_amount = p.reserved_amount(); zgu@3900: _committed_amount = p.committed_amount(); zgu@3900: return *this; zgu@3900: } zgu@3900: zgu@3900: inline void inc(size_t reserved, size_t committed) { zgu@3900: _count ++; zgu@3900: _reserved_amount += reserved; zgu@3900: _committed_amount += committed; zgu@3900: } zgu@3900: zgu@3900: inline size_t count() const { zgu@3900: return _count; zgu@3900: } zgu@3900: zgu@3900: inline size_t reserved_amount() const { zgu@3900: return _reserved_amount; zgu@3900: } zgu@3900: zgu@3900: inline size_t committed_amount() const { zgu@3900: return _committed_amount; zgu@3900: } zgu@3900: }; zgu@3900: zgu@3900: // maps a memory type flag to readable name zgu@3900: typedef struct _memType2Name { zgu@3900: MEMFLAGS _flag; zgu@3900: const char* _name; zgu@3900: } MemType2Name; zgu@3900: zgu@3900: zgu@3900: // This class aggregates malloc'd records by memory type zgu@4959: class MallocMem VALUE_OBJ_CLASS_SPEC { zgu@3900: private: zgu@3900: MEMFLAGS _type; zgu@3900: zgu@3900: size_t _count; zgu@3900: size_t _amount; zgu@3900: zgu@3900: public: zgu@3900: MallocMem() { zgu@3900: _type = mtNone; zgu@3900: _count = 0; zgu@3900: _amount = 0; zgu@3900: } zgu@3900: zgu@3900: MallocMem(MEMFLAGS flags) { zgu@3900: assert(HAS_VALID_MEMORY_TYPE(flags), "no type"); zgu@3900: _type = FLAGS_TO_MEMORY_TYPE(flags); zgu@3900: _count = 0; zgu@3900: _amount = 0; zgu@3900: } zgu@3900: zgu@3900: inline void set_type(MEMFLAGS flag) { zgu@3900: _type = flag; zgu@3900: } zgu@3900: zgu@3900: inline void clear() { zgu@3900: _count = 0; zgu@3900: _amount = 0; zgu@3900: _type = mtNone; zgu@3900: } zgu@3900: zgu@3900: MallocMem& operator=(const MallocMem& m) { zgu@3900: assert(_type == m.type(), "different type"); zgu@3900: _count = m.count(); zgu@3900: _amount = m.amount(); zgu@3900: return *this; zgu@3900: } zgu@3900: zgu@3900: inline void inc(size_t amt) { zgu@3900: _amount += amt; zgu@3900: _count ++; zgu@3900: } zgu@3900: zgu@3900: inline void reduce(size_t amt) { zgu@3900: assert(_amount >= amt, "Just check"); zgu@3900: _amount -= amt; zgu@3900: } zgu@3900: zgu@3900: inline void overwrite_counter(size_t count) { zgu@3900: _count = count; zgu@3900: } zgu@3900: zgu@3900: inline MEMFLAGS type() const { zgu@3900: return _type; zgu@3900: } zgu@3900: zgu@3900: inline bool is_type(MEMFLAGS flags) const { zgu@3900: return FLAGS_TO_MEMORY_TYPE(flags) == _type; zgu@3900: } zgu@3900: zgu@3900: inline size_t count() const { zgu@3900: return _count; zgu@3900: } zgu@3900: zgu@3900: inline size_t amount() const { zgu@3900: return _amount; zgu@3900: } zgu@3900: }; zgu@3900: zgu@3900: // This class records live arena's memory usage zgu@3900: class ArenaMem : public MallocMem { zgu@3900: public: zgu@3900: ArenaMem(MEMFLAGS typeflag): MallocMem(typeflag) { zgu@3900: } zgu@3900: ArenaMem() { } zgu@3900: }; zgu@3900: zgu@3900: // This class aggregates virtual memory by its memory type zgu@4959: class VMMem VALUE_OBJ_CLASS_SPEC { zgu@3900: private: zgu@3900: MEMFLAGS _type; zgu@3900: zgu@3900: size_t _count; zgu@3900: size_t _reserved_amount; zgu@3900: size_t _committed_amount; zgu@3900: zgu@3900: public: zgu@3900: VMMem() { zgu@3900: _type = mtNone; zgu@3900: _count = 0; zgu@3900: _reserved_amount = 0; zgu@3900: _committed_amount = 0; zgu@3900: } zgu@3900: zgu@3900: VMMem(MEMFLAGS flags) { zgu@3900: assert(HAS_VALID_MEMORY_TYPE(flags), "no type"); zgu@3900: _type = FLAGS_TO_MEMORY_TYPE(flags); zgu@3900: _count = 0; zgu@3900: _reserved_amount = 0; zgu@3900: _committed_amount = 0; zgu@3900: } zgu@3900: zgu@3900: inline void clear() { zgu@3900: _type = mtNone; zgu@3900: _count = 0; zgu@3900: _reserved_amount = 0; zgu@3900: _committed_amount = 0; zgu@3900: } zgu@3900: zgu@3900: inline void set_type(MEMFLAGS flags) { zgu@3900: _type = FLAGS_TO_MEMORY_TYPE(flags); zgu@3900: } zgu@3900: zgu@3900: VMMem& operator=(const VMMem& m) { zgu@3900: assert(_type == m.type(), "different type"); zgu@3900: zgu@3900: _count = m.count(); zgu@3900: _reserved_amount = m.reserved_amount(); zgu@3900: _committed_amount = m.committed_amount(); zgu@3900: return *this; zgu@3900: } zgu@3900: zgu@3900: zgu@3900: inline MEMFLAGS type() const { zgu@3900: return _type; zgu@3900: } zgu@3900: zgu@3900: inline bool is_type(MEMFLAGS flags) const { zgu@3900: return FLAGS_TO_MEMORY_TYPE(flags) == _type; zgu@3900: } zgu@3900: zgu@3900: inline void inc(size_t reserved_amt, size_t committed_amt) { zgu@3900: _reserved_amount += reserved_amt; zgu@3900: _committed_amount += committed_amt; zgu@3900: _count ++; zgu@3900: } zgu@3900: zgu@3900: inline size_t count() const { zgu@3900: return _count; zgu@3900: } zgu@3900: zgu@3900: inline size_t reserved_amount() const { zgu@3900: return _reserved_amount; zgu@3900: } zgu@3900: zgu@3900: inline size_t committed_amount() const { zgu@3900: return _committed_amount; zgu@3900: } zgu@3900: }; zgu@3900: zgu@3900: zgu@3900: zgu@3900: #define NUMBER_OF_MEMORY_TYPE (mt_number_of_types + 1) zgu@3900: zgu@3900: class BaselineReporter; zgu@3900: class BaselineComparisonReporter; zgu@3900: zgu@3900: /* zgu@3900: * This class baselines current memory snapshot. zgu@3900: * A memory baseline summarizes memory usage by memory type, zgu@3900: * aggregates memory usage by callsites when detail tracking zgu@3900: * is on. zgu@3900: */ zgu@4959: class MemBaseline VALUE_OBJ_CLASS_SPEC { zgu@3900: friend class BaselineReporter; zgu@3900: friend class BaselineComparisonReporter; zgu@3900: zgu@3900: private: zgu@3900: // overall summaries zgu@3900: size_t _total_malloced; zgu@3900: size_t _total_vm_reserved; zgu@3900: size_t _total_vm_committed; zgu@3900: size_t _number_of_classes; zgu@3900: size_t _number_of_threads; zgu@3900: zgu@3900: // if it has properly baselined zgu@3900: bool _baselined; zgu@3900: zgu@3900: // we categorize memory into three categories within the memory type zgu@3900: MallocMem _malloc_data[NUMBER_OF_MEMORY_TYPE]; zgu@3900: VMMem _vm_data[NUMBER_OF_MEMORY_TYPE]; zgu@3900: ArenaMem _arena_data[NUMBER_OF_MEMORY_TYPE]; zgu@3900: zgu@3900: // memory records that aggregate memory usage by callsites. zgu@3900: // only available when detail tracking is on. zgu@3900: MemPointerArray* _malloc_cs; zgu@3900: MemPointerArray* _vm_cs; zgu@4193: // virtual memory map zgu@4193: MemPointerArray* _vm_map; zgu@3900: zgu@3900: private: zgu@3900: static MemType2Name MemType2NameMap[NUMBER_OF_MEMORY_TYPE]; zgu@3900: zgu@3900: private: zgu@3900: // should not use copy constructor zgu@3900: MemBaseline(MemBaseline& copy) { ShouldNotReachHere(); } zgu@3900: zgu@4980: // check and block at a safepoint zgu@4980: static inline void check_safepoint(JavaThread* thr); zgu@4980: zgu@3900: public: zgu@3900: // create a memory baseline zgu@3900: MemBaseline(); zgu@3900: brutisso@4370: ~MemBaseline(); zgu@3900: zgu@3900: inline bool baselined() const { zgu@3900: return _baselined; zgu@3900: } zgu@3900: zgu@3900: MemBaseline& operator=(const MemBaseline& other); zgu@3900: zgu@3900: // reset the baseline for reuse zgu@3900: void clear(); zgu@3900: zgu@3900: // baseline the snapshot zgu@3900: bool baseline(MemSnapshot& snapshot, bool summary_only = true); zgu@3900: zgu@3900: bool baseline(const MemPointerArray* malloc_records, zgu@3900: const MemPointerArray* vm_records, zgu@3900: bool summary_only = true); zgu@3900: zgu@3900: // total malloc'd memory of specified memory type zgu@3900: inline size_t malloc_amount(MEMFLAGS flag) const { zgu@3900: return _malloc_data[flag2index(flag)].amount(); zgu@3900: } zgu@3900: // number of malloc'd memory blocks of specified memory type zgu@3900: inline size_t malloc_count(MEMFLAGS flag) const { zgu@3900: return _malloc_data[flag2index(flag)].count(); zgu@3900: } zgu@3900: // total memory used by arenas of specified memory type zgu@3900: inline size_t arena_amount(MEMFLAGS flag) const { zgu@3900: return _arena_data[flag2index(flag)].amount(); zgu@3900: } zgu@3900: // number of arenas of specified memory type zgu@3900: inline size_t arena_count(MEMFLAGS flag) const { zgu@3900: return _arena_data[flag2index(flag)].count(); zgu@3900: } zgu@3900: // total reserved memory of specified memory type zgu@3900: inline size_t reserved_amount(MEMFLAGS flag) const { zgu@3900: return _vm_data[flag2index(flag)].reserved_amount(); zgu@3900: } zgu@3900: // total committed memory of specified memory type zgu@3900: inline size_t committed_amount(MEMFLAGS flag) const { zgu@3900: return _vm_data[flag2index(flag)].committed_amount(); zgu@3900: } zgu@3900: // total memory (malloc'd + mmap'd + arena) of specified zgu@3900: // memory type zgu@3900: inline size_t total_amount(MEMFLAGS flag) const { zgu@3900: int index = flag2index(flag); zgu@3900: return _malloc_data[index].amount() + zgu@3900: _vm_data[index].reserved_amount() + zgu@3900: _arena_data[index].amount(); zgu@3900: } zgu@3900: zgu@3900: /* overall summaries */ zgu@3900: zgu@3900: // total malloc'd memory in snapshot zgu@3900: inline size_t total_malloc_amount() const { zgu@3900: return _total_malloced; zgu@3900: } zgu@3900: // total mmap'd memory in snapshot zgu@3900: inline size_t total_reserved_amount() const { zgu@3900: return _total_vm_reserved; zgu@3900: } zgu@3900: // total committed memory in snapshot zgu@3900: inline size_t total_committed_amount() const { zgu@3900: return _total_vm_committed; zgu@3900: } zgu@3900: // number of loaded classes zgu@3900: inline size_t number_of_classes() const { zgu@3900: return _number_of_classes; zgu@3900: } zgu@3900: // number of running threads zgu@3900: inline size_t number_of_threads() const { zgu@3900: return _number_of_threads; zgu@3900: } zgu@3900: // lookup human readable name of a memory type zgu@3900: static const char* type2name(MEMFLAGS type); zgu@3900: zgu@3900: private: zgu@3900: // convert memory flag to the index to mapping table zgu@3900: int flag2index(MEMFLAGS flag) const; zgu@3900: zgu@3900: // reset baseline values zgu@3900: void reset(); zgu@3900: zgu@3900: // summarize the records in global snapshot zgu@3900: bool baseline_malloc_summary(const MemPointerArray* malloc_records); zgu@3900: bool baseline_vm_summary(const MemPointerArray* vm_records); zgu@3900: bool baseline_malloc_details(const MemPointerArray* malloc_records); zgu@3900: bool baseline_vm_details(const MemPointerArray* vm_records); zgu@3900: zgu@3900: // print a line of malloc'd memory aggregated by callsite zgu@3900: void print_malloc_callsite(outputStream* st, address pc, size_t size, zgu@3900: size_t count, int diff_amt, int diff_count) const; zgu@3900: // print a line of mmap'd memory aggregated by callsite zgu@3900: void print_vm_callsite(outputStream* st, address pc, size_t rsz, zgu@3900: size_t csz, int diff_rsz, int diff_csz) const; zgu@3900: zgu@3900: // sorting functions for raw records zgu@3900: static int malloc_sort_by_pc(const void* p1, const void* p2); zgu@3900: static int malloc_sort_by_addr(const void* p1, const void* p2); zgu@3900: zgu@3900: private: zgu@3900: // sorting functions for baselined records zgu@3900: static int bl_malloc_sort_by_size(const void* p1, const void* p2); zgu@3900: static int bl_vm_sort_by_size(const void* p1, const void* p2); zgu@3900: static int bl_malloc_sort_by_pc(const void* p1, const void* p2); zgu@3900: static int bl_vm_sort_by_pc(const void* p1, const void* p2); zgu@3900: }; zgu@3900: zgu@3900: zgu@3900: #endif // SHARE_VM_SERVICES_MEM_BASELINE_HPP