memBaseline.cpp revision 4186
0N/A/*
53N/A * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
0N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0N/A * or visit www.oracle.com if you need additional information or have any
0N/A * questions.
0N/A *
0N/A */
0N/A#include "precompiled.hpp"
0N/A#include "memory/allocation.hpp"
0N/A#include "services/memBaseline.hpp"
0N/A#include "services/memTracker.hpp"
0N/A
0N/AMemType2Name MemBaseline::MemType2NameMap[NUMBER_OF_MEMORY_TYPE] = {
49N/A {mtJavaHeap, "Java Heap"},
49N/A {mtClass, "Class"},
0N/A {mtThreadStack,"Thread Stack"},
0N/A {mtThread, "Thread"},
0N/A {mtCode, "Code"},
0N/A {mtGC, "GC"},
0N/A {mtCompiler, "Compiler"},
0N/A {mtInternal, "Internal"},
0N/A {mtOther, "Other"},
0N/A {mtSymbol, "Symbol"},
0N/A {mtNMT, "Memory Tracking"},
0N/A {mtTracing, "Tracing"},
0N/A {mtChunk, "Pooled Free Chunks"},
0N/A {mtClassShared,"Shared spaces for classes"},
0N/A {mtTest, "Test"},
0N/A {mtNone, "Unknown"} // It can happen when type tagging records are lagging
0N/A // behind
0N/A};
0N/A
0N/AMemBaseline::MemBaseline() {
0N/A _baselined = false;
0N/A
0N/A for (int index = 0; index < NUMBER_OF_MEMORY_TYPE; index ++) {
0N/A _malloc_data[index].set_type(MemType2NameMap[index]._flag);
0N/A _vm_data[index].set_type(MemType2NameMap[index]._flag);
0N/A _arena_data[index].set_type(MemType2NameMap[index]._flag);
0N/A }
0N/A
0N/A _malloc_cs = NULL;
0N/A _vm_cs = NULL;
0N/A _vm_map = NULL;
0N/A
0N/A _number_of_classes = 0;
0N/A _number_of_threads = 0;
0N/A}
112N/A
0N/A
0N/Avoid MemBaseline::clear() {
0N/A if (_malloc_cs != NULL) {
0N/A delete _malloc_cs;
0N/A _malloc_cs = NULL;
0N/A }
0N/A
112N/A if (_vm_cs != NULL) {
0N/A delete _vm_cs;
0N/A _vm_cs = NULL;
0N/A }
0N/A
0N/A if (_vm_map != NULL) {
0N/A delete _vm_map;
0N/A _vm_map = NULL;
0N/A }
0N/A
0N/A reset();
0N/A}
0N/A
0N/A
0N/Avoid MemBaseline::reset() {
0N/A _baselined = false;
0N/A _total_vm_reserved = 0;
0N/A _total_vm_committed = 0;
0N/A _total_malloced = 0;
0N/A _number_of_classes = 0;
0N/A
0N/A if (_malloc_cs != NULL) _malloc_cs->clear();
0N/A if (_vm_cs != NULL) _vm_cs->clear();
0N/A if (_vm_map != NULL) _vm_map->clear();
0N/A
0N/A for (int index = 0; index < NUMBER_OF_MEMORY_TYPE; index ++) {
0N/A _malloc_data[index].clear();
0N/A _vm_data[index].clear();
0N/A _arena_data[index].clear();
0N/A }
408N/A}
408N/A
408N/AMemBaseline::~MemBaseline() {
408N/A clear();
408N/A}
408N/A
408N/A// baseline malloc'd memory records, generate overall summary and summaries by
408N/A// memory types
408N/Abool MemBaseline::baseline_malloc_summary(const MemPointerArray* malloc_records) {
408N/A MemPointerArrayIteratorImpl malloc_itr((MemPointerArray*)malloc_records);
408N/A MemPointerRecord* malloc_ptr = (MemPointerRecord*)malloc_itr.current();
408N/A size_t used_arena_size = 0;
0N/A int index;
0N/A while (malloc_ptr != NULL) {
0N/A index = flag2index(FLAGS_TO_MEMORY_TYPE(malloc_ptr->flags()));
0N/A size_t size = malloc_ptr->size();
0N/A if (malloc_ptr->is_arena_memory_record()) {
0N/A // We do have anonymous arenas, they are either used as value objects,
0N/A // which are embedded inside other objects, or used as stack objects.
0N/A _arena_data[index].inc(size);
0N/A used_arena_size += size;
0N/A } else {
0N/A _total_malloced += size;
0N/A _malloc_data[index].inc(size);
0N/A if (malloc_ptr->is_arena_record()) {
0N/A // see if arena memory record present
0N/A MemPointerRecord* next_malloc_ptr = (MemPointerRecordEx*)malloc_itr.peek_next();
0N/A if (next_malloc_ptr->is_arena_memory_record()) {
0N/A assert(next_malloc_ptr->is_memory_record_of_arena(malloc_ptr),
0N/A "Arena records do not match");
0N/A size = next_malloc_ptr->size();
0N/A _arena_data[index].inc(size);
0N/A used_arena_size += size;
0N/A malloc_itr.next();
0N/A }
0N/A }
0N/A }
0N/A malloc_ptr = (MemPointerRecordEx*)malloc_itr.next();
0N/A }
0N/A
0N/A // substract used arena size to get size of arena chunk in free list
0N/A index = flag2index(mtChunk);
0N/A _malloc_data[index].reduce(used_arena_size);
0N/A // we really don't know how many chunks in free list, so just set to
0N/A // 0
0N/A _malloc_data[index].overwrite_counter(0);
0N/A
0N/A return true;
0N/A}
0N/A
0N/A// baseline mmap'd memory records, generate overall summary and summaries by
0N/A// memory types
0N/Abool MemBaseline::baseline_vm_summary(const MemPointerArray* vm_records) {
0N/A MemPointerArrayIteratorImpl vm_itr((MemPointerArray*)vm_records);
0N/A VMMemRegion* vm_ptr = (VMMemRegion*)vm_itr.current();
0N/A int index;
0N/A while (vm_ptr != NULL) {
0N/A if (vm_ptr->is_reserved_region()) {
0N/A index = flag2index(FLAGS_TO_MEMORY_TYPE(vm_ptr->flags()));
0N/A // we use the number of thread stack to count threads
0N/A if (IS_MEMORY_TYPE(vm_ptr->flags(), mtThreadStack)) {
0N/A _number_of_threads ++;
0N/A }
0N/A _total_vm_reserved += vm_ptr->size();
0N/A _vm_data[index].inc(vm_ptr->size(), 0);
0N/A } else {
0N/A _total_vm_committed += vm_ptr->size();
112N/A _vm_data[index].inc(0, vm_ptr->size());
0N/A }
0N/A vm_ptr = (VMMemRegion*)vm_itr.next();
0N/A }
0N/A return true;
0N/A}
0N/A
408N/A// baseline malloc'd memory by callsites, but only the callsites with memory allocation
408N/A// over 1KB are stored.
408N/Abool MemBaseline::baseline_malloc_details(const MemPointerArray* malloc_records) {
408N/A assert(MemTracker::track_callsite(), "detail tracking is off");
408N/A
408N/A MemPointerArrayIteratorImpl malloc_itr(const_cast<MemPointerArray*>(malloc_records));
408N/A MemPointerRecordEx* malloc_ptr = (MemPointerRecordEx*)malloc_itr.current();
0N/A MallocCallsitePointer malloc_callsite;
0N/A
0N/A // initailize malloc callsite array
0N/A if (_malloc_cs == NULL) {
0N/A _malloc_cs = new (std::nothrow) MemPointerArrayImpl<MallocCallsitePointer>(64);
0N/A // out of native memory
0N/A if (_malloc_cs == NULL || _malloc_cs->out_of_memory()) {
0N/A return false;
0N/A }
0N/A } else {
0N/A _malloc_cs->clear();
0N/A }
0N/A
0N/A MemPointerArray* malloc_data = const_cast<MemPointerArray*>(malloc_records);
0N/A
0N/A // sort into callsite pc order. Details are aggregated by callsites
0N/A malloc_data->sort((FN_SORT)malloc_sort_by_pc);
0N/A bool ret = true;
0N/A
0N/A // baseline memory that is totaled over 1 KB
0N/A while (malloc_ptr != NULL) {
0N/A if (!MemPointerRecord::is_arena_memory_record(malloc_ptr->flags())) {
0N/A // skip thread stacks
0N/A if (!IS_MEMORY_TYPE(malloc_ptr->flags(), mtThreadStack)) {
0N/A if (malloc_callsite.addr() != malloc_ptr->pc()) {
0N/A if ((malloc_callsite.amount()/K) > 0) {
0N/A if (!_malloc_cs->append(&malloc_callsite)) {
0N/A ret = false;
0N/A break;
0N/A }
0N/A }
0N/A malloc_callsite = MallocCallsitePointer(malloc_ptr->pc());
0N/A }
0N/A malloc_callsite.inc(malloc_ptr->size());
0N/A }
0N/A }
0N/A malloc_ptr = (MemPointerRecordEx*)malloc_itr.next();
0N/A }
0N/A
0N/A // restore to address order. Snapshot malloc data is maintained in memory
0N/A // address order.
0N/A malloc_data->sort((FN_SORT)malloc_sort_by_addr);
0N/A
0N/A if (!ret) {
0N/A return false;
0N/A }
0N/A // deal with last record
0N/A if (malloc_callsite.addr() != 0 && (malloc_callsite.amount()/K) > 0) {
0N/A if (!_malloc_cs->append(&malloc_callsite)) {
0N/A return false;
0N/A }
0N/A }
0N/A return true;
0N/A}
0N/A
0N/A// baseline mmap'd memory by callsites
0N/Abool MemBaseline::baseline_vm_details(const MemPointerArray* vm_records) {
0N/A assert(MemTracker::track_callsite(), "detail tracking is off");
0N/A
0N/A VMCallsitePointer vm_callsite;
0N/A VMCallsitePointer* cur_callsite = NULL;
0N/A MemPointerArrayIteratorImpl vm_itr((MemPointerArray*)vm_records);
0N/A VMMemRegionEx* vm_ptr = (VMMemRegionEx*)vm_itr.current();
0N/A
0N/A // initialize virtual memory map array
0N/A if (_vm_map == NULL) {
0N/A _vm_map = new (std::nothrow) MemPointerArrayImpl<VMMemRegionEx>(vm_records->length());
0N/A if (_vm_map == NULL || _vm_map->out_of_memory()) {
0N/A return false;
0N/A }
0N/A } else {
0N/A _vm_map->clear();
0N/A }
0N/A
0N/A // initialize virtual memory callsite array
0N/A if (_vm_cs == NULL) {
0N/A _vm_cs = new (std::nothrow) MemPointerArrayImpl<VMCallsitePointer>(64);
0N/A if (_vm_cs == NULL || _vm_cs->out_of_memory()) {
0N/A return false;
0N/A }
0N/A } else {
0N/A _vm_cs->clear();
0N/A }
0N/A
0N/A // consolidate virtual memory data
0N/A VMMemRegionEx* reserved_rec = NULL;
0N/A VMMemRegionEx* committed_rec = NULL;
0N/A
0N/A // vm_ptr is coming in increasing base address order
0N/A while (vm_ptr != NULL) {
0N/A if (vm_ptr->is_reserved_region()) {
0N/A // consolidate reserved memory regions for virtual memory map.
0N/A // The criteria for consolidation is:
0N/A // 1. two adjacent reserved memory regions
0N/A // 2. belong to the same memory type
0N/A // 3. reserved from the same callsite
0N/A if (reserved_rec == NULL ||
0N/A reserved_rec->base() + reserved_rec->size() != vm_ptr->addr() ||
0N/A FLAGS_TO_MEMORY_TYPE(reserved_rec->flags()) != FLAGS_TO_MEMORY_TYPE(vm_ptr->flags()) ||
0N/A reserved_rec->pc() != vm_ptr->pc()) {
0N/A if (!_vm_map->append(vm_ptr)) {
0N/A return false;
0N/A }
0N/A // inserted reserved region, we need the pointer to the element in virtual
0N/A // memory map array.
0N/A reserved_rec = (VMMemRegionEx*)_vm_map->at(_vm_map->length() - 1);
0N/A } else {
0N/A reserved_rec->expand_region(vm_ptr->addr(), vm_ptr->size());
0N/A }
0N/A
0N/A if (cur_callsite != NULL && !_vm_cs->append(cur_callsite)) {
0N/A return false;
0N/A }
0N/A vm_callsite = VMCallsitePointer(vm_ptr->pc());
0N/A cur_callsite = &vm_callsite;
0N/A vm_callsite.inc(vm_ptr->size(), 0);
0N/A } else {
0N/A // consolidate committed memory regions for virtual memory map
0N/A // The criterial is:
0N/A // 1. two adjacent committed memory regions
0N/A // 2. committed from the same callsite
0N/A if (committed_rec == NULL ||
0N/A committed_rec->base() + committed_rec->size() != vm_ptr->addr() ||
0N/A committed_rec->pc() != vm_ptr->pc()) {
0N/A if (!_vm_map->append(vm_ptr)) {
0N/A return false;
0N/A }
0N/A committed_rec = (VMMemRegionEx*)_vm_map->at(_vm_map->length() - 1);
0N/A } else {
0N/A committed_rec->expand_region(vm_ptr->addr(), vm_ptr->size());
0N/A }
0N/A vm_callsite.inc(0, vm_ptr->size());
0N/A }
0N/A vm_ptr = (VMMemRegionEx*)vm_itr.next();
0N/A }
0N/A // deal with last record
0N/A if (cur_callsite != NULL && !_vm_cs->append(cur_callsite)) {
0N/A return false;
0N/A }
0N/A
0N/A // sort it into callsite pc order. Details are aggregated by callsites
0N/A _vm_cs->sort((FN_SORT)bl_vm_sort_by_pc);
0N/A
0N/A // walk the array to consolidate record by pc
0N/A MemPointerArrayIteratorImpl itr(_vm_cs);
0N/A VMCallsitePointer* callsite_rec = (VMCallsitePointer*)itr.current();
0N/A VMCallsitePointer* next_rec = (VMCallsitePointer*)itr.next();
0N/A while (next_rec != NULL) {
0N/A assert(callsite_rec != NULL, "Sanity check");
0N/A if (next_rec->addr() == callsite_rec->addr()) {
0N/A callsite_rec->inc(next_rec->reserved_amount(), next_rec->committed_amount());
266N/A itr.remove();
0N/A next_rec = (VMCallsitePointer*)itr.current();
0N/A } else {
0N/A callsite_rec = next_rec;
0N/A next_rec = (VMCallsitePointer*)itr.next();
0N/A }
0N/A }
0N/A
0N/A return true;
0N/A}
0N/A
0N/A// baseline a snapshot. If summary_only = false, memory usages aggregated by
0N/A// callsites are also baselined.
0N/Abool MemBaseline::baseline(MemSnapshot& snapshot, bool summary_only) {
0N/A MutexLockerEx snapshot_locker(snapshot._lock, true);
0N/A reset();
0N/A _baselined = baseline_malloc_summary(snapshot._alloc_ptrs) &&
0N/A baseline_vm_summary(snapshot._vm_ptrs);
0N/A _number_of_classes = snapshot.number_of_classes();
0N/A
0N/A if (!summary_only && MemTracker::track_callsite() && _baselined) {
0N/A _baselined = baseline_malloc_details(snapshot._alloc_ptrs) &&
0N/A baseline_vm_details(snapshot._vm_ptrs);
0N/A }
0N/A return _baselined;
0N/A}
0N/A
0N/A
0N/Aint MemBaseline::flag2index(MEMFLAGS flag) const {
0N/A for (int index = 0; index < NUMBER_OF_MEMORY_TYPE; index ++) {
0N/A if (MemType2NameMap[index]._flag == flag) {
0N/A return index;
0N/A }
0N/A }
0N/A assert(false, "no type");
0N/A return -1;
0N/A}
0N/A
0N/Aconst char* MemBaseline::type2name(MEMFLAGS type) {
0N/A for (int index = 0; index < NUMBER_OF_MEMORY_TYPE; index ++) {
266N/A if (MemType2NameMap[index]._flag == type) {
266N/A return MemType2NameMap[index]._name;
266N/A }
266N/A }
266N/A assert(false, err_msg("bad type %x", type));
266N/A return NULL;
266N/A}
266N/A
266N/A
266N/AMemBaseline& MemBaseline::operator=(const MemBaseline& other) {
266N/A _total_malloced = other._total_malloced;
266N/A _total_vm_reserved = other._total_vm_reserved;
0N/A _total_vm_committed = other._total_vm_committed;
0N/A
0N/A _baselined = other._baselined;
0N/A _number_of_classes = other._number_of_classes;
0N/A
0N/A for (int index = 0; index < NUMBER_OF_MEMORY_TYPE; index ++) {
0N/A _malloc_data[index] = other._malloc_data[index];
0N/A _vm_data[index] = other._vm_data[index];
266N/A _arena_data[index] = other._arena_data[index];
266N/A }
266N/A
266N/A if (MemTracker::track_callsite()) {
266N/A assert(_malloc_cs != NULL && _vm_cs != NULL, "out of memory");
266N/A assert(other._malloc_cs != NULL && other._vm_cs != NULL,
266N/A "not properly baselined");
266N/A _malloc_cs->clear();
266N/A _vm_cs->clear();
266N/A int index;
266N/A for (index = 0; index < other._malloc_cs->length(); index ++) {
266N/A _malloc_cs->append(other._malloc_cs->at(index));
266N/A }
266N/A
266N/A for (index = 0; index < other._vm_cs->length(); index ++) {
266N/A _vm_cs->append(other._vm_cs->at(index));
266N/A }
266N/A }
0N/A return *this;
408N/A}
408N/A
408N/A/* compare functions for sorting */
408N/A
408N/A// sort snapshot malloc'd records in callsite pc order
408N/Aint MemBaseline::malloc_sort_by_pc(const void* p1, const void* p2) {
408N/A assert(MemTracker::track_callsite(),"Just check");
408N/A const MemPointerRecordEx* mp1 = (const MemPointerRecordEx*)p1;
408N/A const MemPointerRecordEx* mp2 = (const MemPointerRecordEx*)p2;
408N/A return UNSIGNED_COMPARE(mp1->pc(), mp2->pc());
408N/A}
408N/A
408N/A// sort baselined malloc'd records in size order
408N/Aint MemBaseline::bl_malloc_sort_by_size(const void* p1, const void* p2) {
408N/A assert(MemTracker::is_on(), "Just check");
408N/A const MallocCallsitePointer* mp1 = (const MallocCallsitePointer*)p1;
408N/A const MallocCallsitePointer* mp2 = (const MallocCallsitePointer*)p2;
408N/A return UNSIGNED_COMPARE(mp2->amount(), mp1->amount());
408N/A}
408N/A
0N/A// sort baselined malloc'd records in callsite pc order
0N/Aint MemBaseline::bl_malloc_sort_by_pc(const void* p1, const void* p2) {
0N/A assert(MemTracker::is_on(), "Just check");
0N/A const MallocCallsitePointer* mp1 = (const MallocCallsitePointer*)p1;
0N/A const MallocCallsitePointer* mp2 = (const MallocCallsitePointer*)p2;
0N/A return UNSIGNED_COMPARE(mp1->addr(), mp2->addr());
408N/A}
0N/A
0N/A
0N/A// sort baselined mmap'd records in size (reserved size) order
0N/Aint MemBaseline::bl_vm_sort_by_size(const void* p1, const void* p2) {
408N/A assert(MemTracker::is_on(), "Just check");
0N/A const VMCallsitePointer* mp1 = (const VMCallsitePointer*)p1;
408N/A const VMCallsitePointer* mp2 = (const VMCallsitePointer*)p2;
0N/A return UNSIGNED_COMPARE(mp2->reserved_amount(), mp1->reserved_amount());
408N/A}
0N/A
0N/A// sort baselined mmap'd records in callsite pc order
0N/Aint MemBaseline::bl_vm_sort_by_pc(const void* p1, const void* p2) {
0N/A assert(MemTracker::is_on(), "Just check");
0N/A const VMCallsitePointer* mp1 = (const VMCallsitePointer*)p1;
0N/A const VMCallsitePointer* mp2 = (const VMCallsitePointer*)p2;
0N/A return UNSIGNED_COMPARE(mp1->addr(), mp2->addr());
0N/A}
0N/A
0N/A
0N/A// sort snapshot malloc'd records in memory block address order
0N/Aint MemBaseline::malloc_sort_by_addr(const void* p1, const void* p2) {
0N/A assert(MemTracker::is_on(), "Just check");
0N/A const MemPointerRecord* mp1 = (const MemPointerRecord*)p1;
0N/A const MemPointerRecord* mp2 = (const MemPointerRecord*)p2;
0N/A int delta = UNSIGNED_COMPARE(mp1->addr(), mp2->addr());
0N/A assert(delta != 0, "dup pointer");
0N/A return delta;
0N/A}
0N/A
0N/A