memBaseline.cpp revision 3863
3863N/A/*
3863N/A * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
3863N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3863N/A *
3863N/A * This code is free software; you can redistribute it and/or modify it
3863N/A * under the terms of the GNU General Public License version 2 only, as
3863N/A * published by the Free Software Foundation.
3863N/A *
3863N/A * This code is distributed in the hope that it will be useful, but WITHOUT
3863N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3863N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
3863N/A * version 2 for more details (a copy is included in the LICENSE file that
3863N/A * accompanied this code).
3863N/A *
3863N/A * You should have received a copy of the GNU General Public License version
3863N/A * 2 along with this work; if not, write to the Free Software Foundation,
3863N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3863N/A *
3863N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3863N/A * or visit www.oracle.com if you need additional information or have any
3863N/A * questions.
3863N/A *
3863N/A */
3863N/A#include "precompiled.hpp"
3863N/A#include "classfile/systemDictionary.hpp"
3863N/A#include "memory/allocation.hpp"
3863N/A#include "services/memBaseline.hpp"
3863N/A#include "services/memTracker.hpp"
3863N/A
3863N/AMemType2Name MemBaseline::MemType2NameMap[NUMBER_OF_MEMORY_TYPE] = {
3863N/A {mtJavaHeap, "Java Heap"},
3863N/A {mtClass, "Class"},
3863N/A {mtThreadStack,"Thread Stack"},
3863N/A {mtThread, "Thread"},
3863N/A {mtCode, "Code"},
3863N/A {mtGC, "GC"},
3863N/A {mtCompiler, "Compiler"},
3863N/A {mtInternal, "Internal"},
3863N/A {mtOther, "Other"},
3863N/A {mtSymbol, "Symbol"},
3863N/A {mtNMT, "Memory Tracking"},
3863N/A {mtChunk, "Pooled Free Chunks"},
3863N/A {mtNone, "Unknown"} // It can happen when type tagging records are lagging
3863N/A // behind
3863N/A};
3863N/A
3863N/AMemBaseline::MemBaseline() {
3863N/A _baselined = false;
3863N/A
3863N/A for (int index = 0; index < NUMBER_OF_MEMORY_TYPE; index ++) {
3863N/A _malloc_data[index].set_type(MemType2NameMap[index]._flag);
3863N/A _vm_data[index].set_type(MemType2NameMap[index]._flag);
3863N/A _arena_data[index].set_type(MemType2NameMap[index]._flag);
3863N/A }
3863N/A
3863N/A _malloc_cs = NULL;
3863N/A _vm_cs = NULL;
3863N/A
3863N/A _number_of_classes = 0;
3863N/A _number_of_threads = 0;
3863N/A}
3863N/A
3863N/A
3863N/Avoid MemBaseline::clear() {
3863N/A if (_malloc_cs != NULL) {
3863N/A delete _malloc_cs;
3863N/A _malloc_cs = NULL;
3863N/A }
3863N/A
3863N/A if (_vm_cs != NULL) {
3863N/A delete _vm_cs;
3863N/A _vm_cs = NULL;
3863N/A }
3863N/A
3863N/A reset();
3863N/A}
3863N/A
3863N/A
3863N/Avoid MemBaseline::reset() {
3863N/A _baselined = false;
3863N/A _total_vm_reserved = 0;
3863N/A _total_vm_committed = 0;
3863N/A _total_malloced = 0;
3863N/A _number_of_classes = 0;
3863N/A
3863N/A if (_malloc_cs != NULL) _malloc_cs->clear();
3863N/A if (_vm_cs != NULL) _vm_cs->clear();
3863N/A
3863N/A for (int index = 0; index < NUMBER_OF_MEMORY_TYPE; index ++) {
3863N/A _malloc_data[index].clear();
3863N/A _vm_data[index].clear();
3863N/A _arena_data[index].clear();
3863N/A }
3863N/A}
3863N/A
3863N/AMemBaseline::~MemBaseline() {
3863N/A if (_malloc_cs != NULL) {
3863N/A delete _malloc_cs;
3863N/A }
3863N/A
3863N/A if (_vm_cs != NULL) {
3863N/A delete _vm_cs;
3863N/A }
3863N/A}
3863N/A
3863N/A// baseline malloc'd memory records, generate overall summary and summaries by
3863N/A// memory types
3863N/Abool MemBaseline::baseline_malloc_summary(const MemPointerArray* malloc_records) {
3863N/A MemPointerArrayIteratorImpl mItr((MemPointerArray*)malloc_records);
3863N/A MemPointerRecord* mptr = (MemPointerRecord*)mItr.current();
3863N/A size_t used_arena_size = 0;
3863N/A int index;
3863N/A while (mptr != NULL) {
3863N/A index = flag2index(FLAGS_TO_MEMORY_TYPE(mptr->flags()));
3863N/A size_t size = mptr->size();
3863N/A _total_malloced += size;
3863N/A _malloc_data[index].inc(size);
3863N/A if (MemPointerRecord::is_arena_record(mptr->flags())) {
3863N/A // see if arena size record present
3863N/A MemPointerRecord* next_p = (MemPointerRecordEx*)mItr.peek_next();
3863N/A if (MemPointerRecord::is_arena_size_record(next_p->flags())) {
3863N/A assert(next_p->is_size_record_of_arena(mptr), "arena records do not match");
3863N/A size = next_p->size();
3863N/A _arena_data[index].inc(size);
3863N/A used_arena_size += size;
3863N/A mItr.next();
3863N/A }
3863N/A }
3863N/A mptr = (MemPointerRecordEx*)mItr.next();
3863N/A }
3863N/A
3863N/A // substract used arena size to get size of arena chunk in free list
3863N/A index = flag2index(mtChunk);
3863N/A _malloc_data[index].reduce(used_arena_size);
3863N/A // we really don't know how many chunks in free list, so just set to
3863N/A // 0
3863N/A _malloc_data[index].overwrite_counter(0);
3863N/A
3863N/A return true;
3863N/A}
3863N/A
3863N/A// baseline mmap'd memory records, generate overall summary and summaries by
3863N/A// memory types
3863N/Abool MemBaseline::baseline_vm_summary(const MemPointerArray* vm_records) {
3863N/A MemPointerArrayIteratorImpl vItr((MemPointerArray*)vm_records);
3863N/A VMMemRegion* vptr = (VMMemRegion*)vItr.current();
3863N/A int index;
3863N/A while (vptr != NULL) {
3863N/A index = flag2index(FLAGS_TO_MEMORY_TYPE(vptr->flags()));
3863N/A
3863N/A // we use the number of thread stack to count threads
3863N/A if (IS_MEMORY_TYPE(vptr->flags(), mtThreadStack)) {
3863N/A _number_of_threads ++;
3863N/A }
3863N/A _total_vm_reserved += vptr->reserved_size();
3863N/A _total_vm_committed += vptr->committed_size();
3863N/A _vm_data[index].inc(vptr->reserved_size(), vptr->committed_size());
3863N/A vptr = (VMMemRegion*)vItr.next();
3863N/A }
3863N/A return true;
3863N/A}
3863N/A
3863N/A// baseline malloc'd memory by callsites, but only the callsites with memory allocation
3863N/A// over 1KB are stored.
3863N/Abool MemBaseline::baseline_malloc_details(const MemPointerArray* malloc_records) {
3863N/A assert(MemTracker::track_callsite(), "detail tracking is off");
3863N/A
3863N/A MemPointerArrayIteratorImpl mItr((MemPointerArray*)malloc_records);
3863N/A MemPointerRecordEx* mptr = (MemPointerRecordEx*)mItr.current();
3863N/A MallocCallsitePointer mp;
3863N/A
3863N/A if (_malloc_cs == NULL) {
3863N/A _malloc_cs = new (std::nothrow) MemPointerArrayImpl<MallocCallsitePointer>(64);
3863N/A // out of native memory
3863N/A if (_malloc_cs == NULL) {
3863N/A return false;
3863N/A }
3863N/A } else {
3863N/A _malloc_cs->clear();
3863N/A }
3863N/A
3863N/A // baseline memory that is totaled over 1 KB
3863N/A while (mptr != NULL) {
3863N/A if (!MemPointerRecord::is_arena_size_record(mptr->flags())) {
3863N/A // skip thread stacks
3863N/A if (!IS_MEMORY_TYPE(mptr->flags(), mtThreadStack)) {
3863N/A if (mp.addr() != mptr->pc()) {
3863N/A if ((mp.amount()/K) > 0) {
3863N/A if (!_malloc_cs->append(&mp)) {
3863N/A return false;
3863N/A }
3863N/A }
3863N/A mp = MallocCallsitePointer(mptr->pc());
3863N/A }
3863N/A mp.inc(mptr->size());
3863N/A }
3863N/A }
3863N/A mptr = (MemPointerRecordEx*)mItr.next();
3863N/A }
3863N/A
3863N/A if (mp.addr() != 0 && (mp.amount()/K) > 0) {
3863N/A if (!_malloc_cs->append(&mp)) {
3863N/A return false;
3863N/A }
3863N/A }
3863N/A return true;
3863N/A}
3863N/A
3863N/A// baseline mmap'd memory by callsites
3863N/Abool MemBaseline::baseline_vm_details(const MemPointerArray* vm_records) {
3863N/A assert(MemTracker::track_callsite(), "detail tracking is off");
3863N/A
3863N/A VMCallsitePointer vp;
3863N/A MemPointerArrayIteratorImpl vItr((MemPointerArray*)vm_records);
3863N/A VMMemRegionEx* vptr = (VMMemRegionEx*)vItr.current();
3863N/A
3863N/A if (_vm_cs == NULL) {
3863N/A _vm_cs = new (std::nothrow) MemPointerArrayImpl<VMCallsitePointer>(64);
3863N/A if (_vm_cs == NULL) {
3863N/A return false;
3863N/A }
3863N/A } else {
3863N/A _vm_cs->clear();
3863N/A }
3863N/A
3863N/A while (vptr != NULL) {
3863N/A if (vp.addr() != vptr->pc()) {
3863N/A if (!_vm_cs->append(&vp)) {
3863N/A return false;
3863N/A }
3863N/A vp = VMCallsitePointer(vptr->pc());
3863N/A }
3863N/A vp.inc(vptr->size(), vptr->committed_size());
3863N/A vptr = (VMMemRegionEx*)vItr.next();
3863N/A }
3863N/A if (vp.addr() != 0) {
3863N/A if (!_vm_cs->append(&vp)) {
3863N/A return false;
3863N/A }
3863N/A }
3863N/A return true;
3863N/A}
3863N/A
3863N/A// baseline a snapshot. If summary_only = false, memory usages aggregated by
3863N/A// callsites are also baselined.
3863N/Abool MemBaseline::baseline(MemSnapshot& snapshot, bool summary_only) {
3863N/A MutexLockerEx snapshot_locker(snapshot._lock, true);
3863N/A reset();
3863N/A _baselined = baseline_malloc_summary(snapshot._alloc_ptrs) &&
3863N/A baseline_vm_summary(snapshot._vm_ptrs);
3863N/A _number_of_classes = SystemDictionary::number_of_classes();
3863N/A
3863N/A if (!summary_only && MemTracker::track_callsite() && _baselined) {
3863N/A ((MemPointerArray*)snapshot._alloc_ptrs)->sort((FN_SORT)malloc_sort_by_pc);
3863N/A ((MemPointerArray*)snapshot._vm_ptrs)->sort((FN_SORT)vm_sort_by_pc);
3863N/A _baselined = baseline_malloc_details(snapshot._alloc_ptrs) &&
3863N/A baseline_vm_details(snapshot._vm_ptrs);
3863N/A ((MemPointerArray*)snapshot._alloc_ptrs)->sort((FN_SORT)malloc_sort_by_addr);
3863N/A ((MemPointerArray*)snapshot._vm_ptrs)->sort((FN_SORT)vm_sort_by_addr);
3863N/A }
3863N/A return _baselined;
3863N/A}
3863N/A
3863N/A
3863N/Aint MemBaseline::flag2index(MEMFLAGS flag) const {
3863N/A for (int index = 0; index < NUMBER_OF_MEMORY_TYPE; index ++) {
3863N/A if (MemType2NameMap[index]._flag == flag) {
3863N/A return index;
3863N/A }
3863N/A }
3863N/A assert(false, "no type");
3863N/A return -1;
3863N/A}
3863N/A
3863N/Aconst char* MemBaseline::type2name(MEMFLAGS type) {
3863N/A for (int index = 0; index < NUMBER_OF_MEMORY_TYPE; index ++) {
3863N/A if (MemType2NameMap[index]._flag == type) {
3863N/A return MemType2NameMap[index]._name;
3863N/A }
3863N/A }
3863N/A assert(false, "no type");
3863N/A return NULL;
3863N/A}
3863N/A
3863N/A
3863N/AMemBaseline& MemBaseline::operator=(const MemBaseline& other) {
3863N/A _total_malloced = other._total_malloced;
3863N/A _total_vm_reserved = other._total_vm_reserved;
3863N/A _total_vm_committed = other._total_vm_committed;
3863N/A
3863N/A _baselined = other._baselined;
3863N/A _number_of_classes = other._number_of_classes;
3863N/A
3863N/A for (int index = 0; index < NUMBER_OF_MEMORY_TYPE; index ++) {
3863N/A _malloc_data[index] = other._malloc_data[index];
3863N/A _vm_data[index] = other._vm_data[index];
3863N/A _arena_data[index] = other._arena_data[index];
3863N/A }
3863N/A
3863N/A if (MemTracker::track_callsite()) {
3863N/A assert(_malloc_cs != NULL && _vm_cs != NULL, "out of memory");
3863N/A assert(other._malloc_cs != NULL && other._vm_cs != NULL,
3863N/A "not properly baselined");
3863N/A _malloc_cs->clear();
3863N/A _vm_cs->clear();
3863N/A int index;
3863N/A for (index = 0; index < other._malloc_cs->length(); index ++) {
3863N/A _malloc_cs->append(other._malloc_cs->at(index));
3863N/A }
3863N/A
3863N/A for (index = 0; index < other._vm_cs->length(); index ++) {
3863N/A _vm_cs->append(other._vm_cs->at(index));
3863N/A }
3863N/A }
3863N/A return *this;
3863N/A}
3863N/A
3863N/A/* compare functions for sorting */
3863N/A
3863N/A// sort snapshot malloc'd records in callsite pc order
3863N/Aint MemBaseline::malloc_sort_by_pc(const void* p1, const void* p2) {
3863N/A assert(MemTracker::track_callsite(),"Just check");
3863N/A const MemPointerRecordEx* mp1 = (const MemPointerRecordEx*)p1;
3863N/A const MemPointerRecordEx* mp2 = (const MemPointerRecordEx*)p2;
3863N/A return UNSIGNED_COMPARE(mp1->pc(), mp2->pc());
3863N/A}
3863N/A
3863N/A// sort baselined malloc'd records in size order
3863N/Aint MemBaseline::bl_malloc_sort_by_size(const void* p1, const void* p2) {
3863N/A assert(MemTracker::is_on(), "Just check");
3863N/A const MallocCallsitePointer* mp1 = (const MallocCallsitePointer*)p1;
3863N/A const MallocCallsitePointer* mp2 = (const MallocCallsitePointer*)p2;
3863N/A return UNSIGNED_COMPARE(mp2->amount(), mp1->amount());
3863N/A}
3863N/A
3863N/A// sort baselined malloc'd records in callsite pc order
3863N/Aint MemBaseline::bl_malloc_sort_by_pc(const void* p1, const void* p2) {
3863N/A assert(MemTracker::is_on(), "Just check");
3863N/A const MallocCallsitePointer* mp1 = (const MallocCallsitePointer*)p1;
3863N/A const MallocCallsitePointer* mp2 = (const MallocCallsitePointer*)p2;
3863N/A return UNSIGNED_COMPARE(mp1->addr(), mp2->addr());
3863N/A}
3863N/A
3863N/A// sort snapshot mmap'd records in callsite pc order
3863N/Aint MemBaseline::vm_sort_by_pc(const void* p1, const void* p2) {
3863N/A assert(MemTracker::track_callsite(),"Just check");
3863N/A const VMMemRegionEx* mp1 = (const VMMemRegionEx*)p1;
3863N/A const VMMemRegionEx* mp2 = (const VMMemRegionEx*)p2;
3863N/A return UNSIGNED_COMPARE(mp1->pc(), mp2->pc());
3863N/A}
3863N/A
3863N/A// sort baselined mmap'd records in size (reserved size) order
3863N/Aint MemBaseline::bl_vm_sort_by_size(const void* p1, const void* p2) {
3863N/A assert(MemTracker::is_on(), "Just check");
3863N/A const VMCallsitePointer* mp1 = (const VMCallsitePointer*)p1;
3863N/A const VMCallsitePointer* mp2 = (const VMCallsitePointer*)p2;
3863N/A return UNSIGNED_COMPARE(mp2->reserved_amount(), mp1->reserved_amount());
3863N/A}
3863N/A
3863N/A// sort baselined mmap'd records in callsite pc order
3863N/Aint MemBaseline::bl_vm_sort_by_pc(const void* p1, const void* p2) {
3863N/A assert(MemTracker::is_on(), "Just check");
3863N/A const VMCallsitePointer* mp1 = (const VMCallsitePointer*)p1;
3863N/A const VMCallsitePointer* mp2 = (const VMCallsitePointer*)p2;
3863N/A return UNSIGNED_COMPARE(mp1->addr(), mp2->addr());
3863N/A}
3863N/A
3863N/A
3863N/A// sort snapshot malloc'd records in memory block address order
3863N/Aint MemBaseline::malloc_sort_by_addr(const void* p1, const void* p2) {
3863N/A assert(MemTracker::is_on(), "Just check");
3863N/A const MemPointerRecord* mp1 = (const MemPointerRecord*)p1;
3863N/A const MemPointerRecord* mp2 = (const MemPointerRecord*)p2;
3863N/A int delta = UNSIGNED_COMPARE(mp1->addr(), mp2->addr());
3863N/A assert(delta != 0, "dup pointer");
3863N/A return delta;
3863N/A}
3863N/A
3863N/A// sort snapshot mmap'd records in memory block address order
3863N/Aint MemBaseline::vm_sort_by_addr(const void* p1, const void* p2) {
3863N/A assert(MemTracker::is_on(), "Just check");
3863N/A const VMMemRegion* mp1 = (const VMMemRegion*)p1;
3863N/A const VMMemRegion* mp2 = (const VMMemRegion*)p2;
3863N/A int delta = UNSIGNED_COMPARE(mp1->addr(), mp2->addr());
3863N/A assert(delta != 0, "dup pointer");
3863N/A return delta;
3863N/A}