zgu@3900: /* zgu@3900: * Copyright (c) 2012, 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: #include "precompiled.hpp" zgu@3900: #include "classfile/systemDictionary.hpp" zgu@3900: #include "memory/allocation.hpp" zgu@3900: #include "services/memBaseline.hpp" zgu@3900: #include "services/memTracker.hpp" zgu@3900: zgu@3900: MemType2Name MemBaseline::MemType2NameMap[NUMBER_OF_MEMORY_TYPE] = { zgu@3900: {mtJavaHeap, "Java Heap"}, zgu@3900: {mtClass, "Class"}, zgu@3900: {mtThreadStack,"Thread Stack"}, zgu@3900: {mtThread, "Thread"}, zgu@3900: {mtCode, "Code"}, zgu@3900: {mtGC, "GC"}, zgu@3900: {mtCompiler, "Compiler"}, zgu@3900: {mtInternal, "Internal"}, zgu@3900: {mtOther, "Other"}, zgu@3900: {mtSymbol, "Symbol"}, zgu@3900: {mtNMT, "Memory Tracking"}, zgu@3900: {mtChunk, "Pooled Free Chunks"}, zgu@3900: {mtNone, "Unknown"} // It can happen when type tagging records are lagging zgu@3900: // behind zgu@3900: }; zgu@3900: zgu@3900: MemBaseline::MemBaseline() { zgu@3900: _baselined = false; zgu@3900: zgu@3900: for (int index = 0; index < NUMBER_OF_MEMORY_TYPE; index ++) { zgu@3900: _malloc_data[index].set_type(MemType2NameMap[index]._flag); zgu@3900: _vm_data[index].set_type(MemType2NameMap[index]._flag); zgu@3900: _arena_data[index].set_type(MemType2NameMap[index]._flag); zgu@3900: } zgu@3900: zgu@3900: _malloc_cs = NULL; zgu@3900: _vm_cs = NULL; zgu@3900: zgu@3900: _number_of_classes = 0; zgu@3900: _number_of_threads = 0; zgu@3900: } zgu@3900: zgu@3900: zgu@3900: void MemBaseline::clear() { zgu@3900: if (_malloc_cs != NULL) { zgu@3900: delete _malloc_cs; zgu@3900: _malloc_cs = NULL; zgu@3900: } zgu@3900: zgu@3900: if (_vm_cs != NULL) { zgu@3900: delete _vm_cs; zgu@3900: _vm_cs = NULL; zgu@3900: } zgu@3900: zgu@3900: reset(); zgu@3900: } zgu@3900: zgu@3900: zgu@3900: void MemBaseline::reset() { zgu@3900: _baselined = false; zgu@3900: _total_vm_reserved = 0; zgu@3900: _total_vm_committed = 0; zgu@3900: _total_malloced = 0; zgu@3900: _number_of_classes = 0; zgu@3900: zgu@3900: if (_malloc_cs != NULL) _malloc_cs->clear(); zgu@3900: if (_vm_cs != NULL) _vm_cs->clear(); zgu@3900: zgu@3900: for (int index = 0; index < NUMBER_OF_MEMORY_TYPE; index ++) { zgu@3900: _malloc_data[index].clear(); zgu@3900: _vm_data[index].clear(); zgu@3900: _arena_data[index].clear(); zgu@3900: } zgu@3900: } zgu@3900: zgu@3900: MemBaseline::~MemBaseline() { zgu@3900: if (_malloc_cs != NULL) { zgu@3900: delete _malloc_cs; zgu@3900: } zgu@3900: zgu@3900: if (_vm_cs != NULL) { zgu@3900: delete _vm_cs; zgu@3900: } zgu@3900: } zgu@3900: zgu@3900: // baseline malloc'd memory records, generate overall summary and summaries by zgu@3900: // memory types zgu@3900: bool MemBaseline::baseline_malloc_summary(const MemPointerArray* malloc_records) { zgu@3900: MemPointerArrayIteratorImpl mItr((MemPointerArray*)malloc_records); zgu@3900: MemPointerRecord* mptr = (MemPointerRecord*)mItr.current(); zgu@3900: size_t used_arena_size = 0; zgu@3900: int index; zgu@3900: while (mptr != NULL) { zgu@3900: index = flag2index(FLAGS_TO_MEMORY_TYPE(mptr->flags())); zgu@3900: size_t size = mptr->size(); zgu@3900: _total_malloced += size; zgu@3900: _malloc_data[index].inc(size); zgu@3900: if (MemPointerRecord::is_arena_record(mptr->flags())) { zgu@3900: // see if arena size record present zgu@3900: MemPointerRecord* next_p = (MemPointerRecordEx*)mItr.peek_next(); zgu@3900: if (MemPointerRecord::is_arena_size_record(next_p->flags())) { zgu@3900: assert(next_p->is_size_record_of_arena(mptr), "arena records do not match"); zgu@3900: size = next_p->size(); zgu@3900: _arena_data[index].inc(size); zgu@3900: used_arena_size += size; zgu@3900: mItr.next(); zgu@3900: } zgu@3900: } zgu@3900: mptr = (MemPointerRecordEx*)mItr.next(); zgu@3900: } zgu@3900: zgu@3900: // substract used arena size to get size of arena chunk in free list zgu@3900: index = flag2index(mtChunk); zgu@3900: _malloc_data[index].reduce(used_arena_size); zgu@3900: // we really don't know how many chunks in free list, so just set to zgu@3900: // 0 zgu@3900: _malloc_data[index].overwrite_counter(0); zgu@3900: zgu@3900: return true; zgu@3900: } zgu@3900: zgu@3900: // baseline mmap'd memory records, generate overall summary and summaries by zgu@3900: // memory types zgu@3900: bool MemBaseline::baseline_vm_summary(const MemPointerArray* vm_records) { zgu@3900: MemPointerArrayIteratorImpl vItr((MemPointerArray*)vm_records); zgu@3900: VMMemRegion* vptr = (VMMemRegion*)vItr.current(); zgu@3900: int index; zgu@3900: while (vptr != NULL) { zgu@3900: index = flag2index(FLAGS_TO_MEMORY_TYPE(vptr->flags())); zgu@3900: zgu@3900: // we use the number of thread stack to count threads zgu@3900: if (IS_MEMORY_TYPE(vptr->flags(), mtThreadStack)) { zgu@3900: _number_of_threads ++; zgu@3900: } zgu@3900: _total_vm_reserved += vptr->reserved_size(); zgu@3900: _total_vm_committed += vptr->committed_size(); zgu@3900: _vm_data[index].inc(vptr->reserved_size(), vptr->committed_size()); zgu@3900: vptr = (VMMemRegion*)vItr.next(); zgu@3900: } zgu@3900: return true; zgu@3900: } zgu@3900: zgu@3900: // baseline malloc'd memory by callsites, but only the callsites with memory allocation zgu@3900: // over 1KB are stored. zgu@3900: bool MemBaseline::baseline_malloc_details(const MemPointerArray* malloc_records) { zgu@3900: assert(MemTracker::track_callsite(), "detail tracking is off"); zgu@3900: zgu@3900: MemPointerArrayIteratorImpl mItr((MemPointerArray*)malloc_records); zgu@3900: MemPointerRecordEx* mptr = (MemPointerRecordEx*)mItr.current(); zgu@3900: MallocCallsitePointer mp; zgu@3900: zgu@3900: if (_malloc_cs == NULL) { zgu@3900: _malloc_cs = new (std::nothrow) MemPointerArrayImpl(64); zgu@3900: // out of native memory zgu@3900: if (_malloc_cs == NULL) { zgu@3900: return false; zgu@3900: } zgu@3900: } else { zgu@3900: _malloc_cs->clear(); zgu@3900: } zgu@3900: zgu@3900: // baseline memory that is totaled over 1 KB zgu@3900: while (mptr != NULL) { zgu@3900: if (!MemPointerRecord::is_arena_size_record(mptr->flags())) { zgu@3900: // skip thread stacks zgu@3900: if (!IS_MEMORY_TYPE(mptr->flags(), mtThreadStack)) { zgu@3900: if (mp.addr() != mptr->pc()) { zgu@3900: if ((mp.amount()/K) > 0) { zgu@3900: if (!_malloc_cs->append(&mp)) { zgu@3900: return false; zgu@3900: } zgu@3900: } zgu@3900: mp = MallocCallsitePointer(mptr->pc()); zgu@3900: } zgu@3900: mp.inc(mptr->size()); zgu@3900: } zgu@3900: } zgu@3900: mptr = (MemPointerRecordEx*)mItr.next(); zgu@3900: } zgu@3900: zgu@3900: if (mp.addr() != 0 && (mp.amount()/K) > 0) { zgu@3900: if (!_malloc_cs->append(&mp)) { zgu@3900: return false; zgu@3900: } zgu@3900: } zgu@3900: return true; zgu@3900: } zgu@3900: zgu@3900: // baseline mmap'd memory by callsites zgu@3900: bool MemBaseline::baseline_vm_details(const MemPointerArray* vm_records) { zgu@3900: assert(MemTracker::track_callsite(), "detail tracking is off"); zgu@3900: zgu@3900: VMCallsitePointer vp; zgu@3900: MemPointerArrayIteratorImpl vItr((MemPointerArray*)vm_records); zgu@3900: VMMemRegionEx* vptr = (VMMemRegionEx*)vItr.current(); zgu@3900: zgu@3900: if (_vm_cs == NULL) { zgu@3900: _vm_cs = new (std::nothrow) MemPointerArrayImpl(64); zgu@3900: if (_vm_cs == NULL) { zgu@3900: return false; zgu@3900: } zgu@3900: } else { zgu@3900: _vm_cs->clear(); zgu@3900: } zgu@3900: zgu@3900: while (vptr != NULL) { zgu@3900: if (vp.addr() != vptr->pc()) { zgu@3900: if (!_vm_cs->append(&vp)) { zgu@3900: return false; zgu@3900: } zgu@3900: vp = VMCallsitePointer(vptr->pc()); zgu@3900: } zgu@3900: vp.inc(vptr->size(), vptr->committed_size()); zgu@3900: vptr = (VMMemRegionEx*)vItr.next(); zgu@3900: } zgu@3900: if (vp.addr() != 0) { zgu@3900: if (!_vm_cs->append(&vp)) { zgu@3900: return false; zgu@3900: } zgu@3900: } zgu@3900: return true; zgu@3900: } zgu@3900: zgu@3900: // baseline a snapshot. If summary_only = false, memory usages aggregated by zgu@3900: // callsites are also baselined. zgu@3900: bool MemBaseline::baseline(MemSnapshot& snapshot, bool summary_only) { zgu@3900: MutexLockerEx snapshot_locker(snapshot._lock, true); zgu@3900: reset(); zgu@3900: _baselined = baseline_malloc_summary(snapshot._alloc_ptrs) && zgu@3900: baseline_vm_summary(snapshot._vm_ptrs); zgu@3900: _number_of_classes = SystemDictionary::number_of_classes(); zgu@3900: zgu@3900: if (!summary_only && MemTracker::track_callsite() && _baselined) { zgu@3900: ((MemPointerArray*)snapshot._alloc_ptrs)->sort((FN_SORT)malloc_sort_by_pc); zgu@3900: ((MemPointerArray*)snapshot._vm_ptrs)->sort((FN_SORT)vm_sort_by_pc); zgu@3900: _baselined = baseline_malloc_details(snapshot._alloc_ptrs) && zgu@3900: baseline_vm_details(snapshot._vm_ptrs); zgu@3900: ((MemPointerArray*)snapshot._alloc_ptrs)->sort((FN_SORT)malloc_sort_by_addr); zgu@3900: ((MemPointerArray*)snapshot._vm_ptrs)->sort((FN_SORT)vm_sort_by_addr); zgu@3900: } zgu@3900: return _baselined; zgu@3900: } zgu@3900: zgu@3900: zgu@3900: int MemBaseline::flag2index(MEMFLAGS flag) const { zgu@3900: for (int index = 0; index < NUMBER_OF_MEMORY_TYPE; index ++) { zgu@3900: if (MemType2NameMap[index]._flag == flag) { zgu@3900: return index; zgu@3900: } zgu@3900: } zgu@3900: assert(false, "no type"); zgu@3900: return -1; zgu@3900: } zgu@3900: zgu@3900: const char* MemBaseline::type2name(MEMFLAGS type) { zgu@3900: for (int index = 0; index < NUMBER_OF_MEMORY_TYPE; index ++) { zgu@3900: if (MemType2NameMap[index]._flag == type) { zgu@3900: return MemType2NameMap[index]._name; zgu@3900: } zgu@3900: } zgu@3900: assert(false, "no type"); zgu@3900: return NULL; zgu@3900: } zgu@3900: zgu@3900: zgu@3900: MemBaseline& MemBaseline::operator=(const MemBaseline& other) { zgu@3900: _total_malloced = other._total_malloced; zgu@3900: _total_vm_reserved = other._total_vm_reserved; zgu@3900: _total_vm_committed = other._total_vm_committed; zgu@3900: zgu@3900: _baselined = other._baselined; zgu@3900: _number_of_classes = other._number_of_classes; zgu@3900: zgu@3900: for (int index = 0; index < NUMBER_OF_MEMORY_TYPE; index ++) { zgu@3900: _malloc_data[index] = other._malloc_data[index]; zgu@3900: _vm_data[index] = other._vm_data[index]; zgu@3900: _arena_data[index] = other._arena_data[index]; zgu@3900: } zgu@3900: zgu@3900: if (MemTracker::track_callsite()) { zgu@3900: assert(_malloc_cs != NULL && _vm_cs != NULL, "out of memory"); zgu@3900: assert(other._malloc_cs != NULL && other._vm_cs != NULL, zgu@3900: "not properly baselined"); zgu@3900: _malloc_cs->clear(); zgu@3900: _vm_cs->clear(); zgu@3900: int index; zgu@3900: for (index = 0; index < other._malloc_cs->length(); index ++) { zgu@3900: _malloc_cs->append(other._malloc_cs->at(index)); zgu@3900: } zgu@3900: zgu@3900: for (index = 0; index < other._vm_cs->length(); index ++) { zgu@3900: _vm_cs->append(other._vm_cs->at(index)); zgu@3900: } zgu@3900: } zgu@3900: return *this; zgu@3900: } zgu@3900: zgu@3900: /* compare functions for sorting */ zgu@3900: zgu@3900: // sort snapshot malloc'd records in callsite pc order zgu@3900: int MemBaseline::malloc_sort_by_pc(const void* p1, const void* p2) { zgu@3900: assert(MemTracker::track_callsite(),"Just check"); zgu@3900: const MemPointerRecordEx* mp1 = (const MemPointerRecordEx*)p1; zgu@3900: const MemPointerRecordEx* mp2 = (const MemPointerRecordEx*)p2; zgu@3900: return UNSIGNED_COMPARE(mp1->pc(), mp2->pc()); zgu@3900: } zgu@3900: zgu@3900: // sort baselined malloc'd records in size order zgu@3900: int MemBaseline::bl_malloc_sort_by_size(const void* p1, const void* p2) { zgu@3900: assert(MemTracker::is_on(), "Just check"); zgu@3900: const MallocCallsitePointer* mp1 = (const MallocCallsitePointer*)p1; zgu@3900: const MallocCallsitePointer* mp2 = (const MallocCallsitePointer*)p2; zgu@3900: return UNSIGNED_COMPARE(mp2->amount(), mp1->amount()); zgu@3900: } zgu@3900: zgu@3900: // sort baselined malloc'd records in callsite pc order zgu@3900: int MemBaseline::bl_malloc_sort_by_pc(const void* p1, const void* p2) { zgu@3900: assert(MemTracker::is_on(), "Just check"); zgu@3900: const MallocCallsitePointer* mp1 = (const MallocCallsitePointer*)p1; zgu@3900: const MallocCallsitePointer* mp2 = (const MallocCallsitePointer*)p2; zgu@3900: return UNSIGNED_COMPARE(mp1->addr(), mp2->addr()); zgu@3900: } zgu@3900: zgu@3900: // sort snapshot mmap'd records in callsite pc order zgu@3900: int MemBaseline::vm_sort_by_pc(const void* p1, const void* p2) { zgu@3900: assert(MemTracker::track_callsite(),"Just check"); zgu@3900: const VMMemRegionEx* mp1 = (const VMMemRegionEx*)p1; zgu@3900: const VMMemRegionEx* mp2 = (const VMMemRegionEx*)p2; zgu@3900: return UNSIGNED_COMPARE(mp1->pc(), mp2->pc()); zgu@3900: } zgu@3900: zgu@3900: // sort baselined mmap'd records in size (reserved size) order zgu@3900: int MemBaseline::bl_vm_sort_by_size(const void* p1, const void* p2) { zgu@3900: assert(MemTracker::is_on(), "Just check"); zgu@3900: const VMCallsitePointer* mp1 = (const VMCallsitePointer*)p1; zgu@3900: const VMCallsitePointer* mp2 = (const VMCallsitePointer*)p2; zgu@3900: return UNSIGNED_COMPARE(mp2->reserved_amount(), mp1->reserved_amount()); zgu@3900: } zgu@3900: zgu@3900: // sort baselined mmap'd records in callsite pc order zgu@3900: int MemBaseline::bl_vm_sort_by_pc(const void* p1, const void* p2) { zgu@3900: assert(MemTracker::is_on(), "Just check"); zgu@3900: const VMCallsitePointer* mp1 = (const VMCallsitePointer*)p1; zgu@3900: const VMCallsitePointer* mp2 = (const VMCallsitePointer*)p2; zgu@3900: return UNSIGNED_COMPARE(mp1->addr(), mp2->addr()); zgu@3900: } zgu@3900: zgu@3900: zgu@3900: // sort snapshot malloc'd records in memory block address order zgu@3900: int MemBaseline::malloc_sort_by_addr(const void* p1, const void* p2) { zgu@3900: assert(MemTracker::is_on(), "Just check"); zgu@3900: const MemPointerRecord* mp1 = (const MemPointerRecord*)p1; zgu@3900: const MemPointerRecord* mp2 = (const MemPointerRecord*)p2; zgu@3900: int delta = UNSIGNED_COMPARE(mp1->addr(), mp2->addr()); zgu@3900: assert(delta != 0, "dup pointer"); zgu@3900: return delta; zgu@3900: } zgu@3900: zgu@3900: // sort snapshot mmap'd records in memory block address order zgu@3900: int MemBaseline::vm_sort_by_addr(const void* p1, const void* p2) { zgu@3900: assert(MemTracker::is_on(), "Just check"); zgu@3900: const VMMemRegion* mp1 = (const VMMemRegion*)p1; zgu@3900: const VMMemRegion* mp2 = (const VMMemRegion*)p2; zgu@3900: int delta = UNSIGNED_COMPARE(mp1->addr(), mp2->addr()); zgu@3900: assert(delta != 0, "dup pointer"); zgu@3900: return delta; zgu@3900: }