memSnapshot.hpp revision 4068
0N/A/*
0N/A * Copyright (c) 2012, 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
0N/A#ifndef SHARE_VM_SERVICES_MEM_SNAPSHOT_HPP
0N/A#define SHARE_VM_SERVICES_MEM_SNAPSHOT_HPP
0N/A
0N/A#include "memory/allocation.hpp"
0N/A#include "runtime/mutex.hpp"
0N/A#include "runtime/mutexLocker.hpp"
0N/A#include "services/memBaseline.hpp"
0N/A#include "services/memPtrArray.hpp"
0N/A
0N/A// Snapshot pointer array iterator
0N/A
0N/A// The pointer array contains malloc-ed pointers
0N/Aclass MemPointerIterator : public MemPointerArrayIteratorImpl {
0N/A public:
0N/A MemPointerIterator(MemPointerArray* arr):
0N/A MemPointerArrayIteratorImpl(arr) {
0N/A assert(arr != NULL, "null array");
0N/A }
0N/A
0N/A#ifdef ASSERT
0N/A virtual bool is_dup_pointer(const MemPointer* ptr1,
0N/A const MemPointer* ptr2) const {
0N/A MemPointerRecord* p1 = (MemPointerRecord*)ptr1;
0N/A MemPointerRecord* p2 = (MemPointerRecord*)ptr2;
0N/A
0N/A if (p1->addr() != p2->addr()) return false;
0N/A if ((p1->flags() & MemPointerRecord::tag_masks) !=
0N/A (p2->flags() & MemPointerRecord::tag_masks)) {
0N/A return false;
0N/A }
0N/A // we do see multiple commit/uncommit on the same memory, it is ok
0N/A return (p1->flags() & MemPointerRecord::tag_masks) == MemPointerRecord::tag_alloc ||
0N/A (p1->flags() & MemPointerRecord::tag_masks) == MemPointerRecord::tag_release;
0N/A }
0N/A
0N/A virtual bool insert(MemPointer* ptr) {
0N/A if (_pos > 0) {
0N/A MemPointer* p1 = (MemPointer*)ptr;
0N/A MemPointer* p2 = (MemPointer*)_array->at(_pos - 1);
0N/A assert(!is_dup_pointer(p1, p2),
0N/A err_msg("duplicated pointer, flag = [%x]", (unsigned int)((MemPointerRecord*)p1)->flags()));
0N/A }
0N/A if (_pos < _array->length() -1) {
0N/A MemPointer* p1 = (MemPointer*)ptr;
0N/A MemPointer* p2 = (MemPointer*)_array->at(_pos + 1);
0N/A assert(!is_dup_pointer(p1, p2),
0N/A err_msg("duplicated pointer, flag = [%x]", (unsigned int)((MemPointerRecord*)p1)->flags()));
0N/A }
0N/A return _array->insert_at(ptr, _pos);
0N/A }
0N/A
0N/A virtual bool insert_after(MemPointer* ptr) {
0N/A if (_pos > 0) {
0N/A MemPointer* p1 = (MemPointer*)ptr;
0N/A MemPointer* p2 = (MemPointer*)_array->at(_pos - 1);
0N/A assert(!is_dup_pointer(p1, p2),
0N/A err_msg("duplicated pointer, flag = [%x]", (unsigned int)((MemPointerRecord*)p1)->flags()));
0N/A }
0N/A if (_pos < _array->length() - 1) {
0N/A MemPointer* p1 = (MemPointer*)ptr;
0N/A MemPointer* p2 = (MemPointer*)_array->at(_pos + 1);
0N/A
0N/A assert(!is_dup_pointer(p1, p2),
0N/A err_msg("duplicated pointer, flag = [%x]", (unsigned int)((MemPointerRecord*)p1)->flags()));
0N/A }
0N/A if (_array->insert_at(ptr, _pos + 1)) {
0N/A _pos ++;
0N/A return true;
0N/A }
0N/A return false;
0N/A }
0N/A#endif
0N/A
0N/A virtual MemPointer* locate(address addr) {
0N/A MemPointer* cur = current();
0N/A while (cur != NULL && cur->addr() < addr) {
0N/A cur = next();
0N/A }
0N/A return cur;
0N/A }
0N/A};
0N/A
0N/Aclass VMMemPointerIterator : public MemPointerIterator {
0N/A public:
0N/A VMMemPointerIterator(MemPointerArray* arr):
0N/A MemPointerIterator(arr) {
0N/A }
0N/A
0N/A // locate an existing reserved memory region that contains specified address,
0N/A // or the reserved region just above this address, where the incoming
0N/A // reserved region should be inserted.
0N/A virtual MemPointer* locate(address addr) {
0N/A reset();
0N/A VMMemRegion* reg = (VMMemRegion*)current();
0N/A while (reg != NULL) {
0N/A if (reg->is_reserved_region()) {
0N/A if (reg->contains_address(addr) || addr < reg->base()) {
0N/A return reg;
0N/A }
0N/A }
0N/A reg = (VMMemRegion*)next();
0N/A }
0N/A return NULL;
0N/A }
0N/A
0N/A // following methods update virtual memory in the context
0N/A // of 'current' position, which is properly positioned by
0N/A // callers via locate method.
0N/A bool add_reserved_region(MemPointerRecord* rec);
0N/A bool add_committed_region(MemPointerRecord* rec);
0N/A bool remove_uncommitted_region(MemPointerRecord* rec);
0N/A bool remove_released_region(MemPointerRecord* rec);
0N/A
0N/A // split a reserved region to create a new memory region with specified base and size
0N/A bool split_reserved_region(VMMemRegion* rgn, address new_rgn_addr, size_t new_rgn_size);
0N/A private:
0N/A bool insert_record(MemPointerRecord* rec);
0N/A bool insert_record_after(MemPointerRecord* rec);
0N/A
0N/A bool insert_reserved_region(MemPointerRecord* rec);
0N/A
0N/A // reset current position
0N/A inline void reset() { _pos = 0; }
0N/A#ifdef ASSERT
0N/A // check integrity of records on current reserved memory region.
0N/A bool check_reserved_region() {
0N/A VMMemRegion* reserved_region = (VMMemRegion*)current();
0N/A assert(reserved_region != NULL && reserved_region->is_reserved_region(),
0N/A "Sanity check");
0N/A // all committed regions that follow current reserved region, should all
0N/A // belong to the reserved region.
0N/A VMMemRegion* next_region = (VMMemRegion*)next();
0N/A for (; next_region != NULL && next_region->is_committed_region();
0N/A next_region = (VMMemRegion*)next() ) {
0N/A if(!reserved_region->contains_region(next_region)) {
0N/A return false;
0N/A }
0N/A }
0N/A return true;
0N/A }
0N/A
0N/A virtual bool is_dup_pointer(const MemPointer* ptr1,
0N/A const MemPointer* ptr2) const {
0N/A VMMemRegion* p1 = (VMMemRegion*)ptr1;
0N/A VMMemRegion* p2 = (VMMemRegion*)ptr2;
0N/A
0N/A if (p1->addr() != p2->addr()) return false;
0N/A if ((p1->flags() & MemPointerRecord::tag_masks) !=
0N/A (p2->flags() & MemPointerRecord::tag_masks)) {
0N/A return false;
0N/A }
0N/A // we do see multiple commit/uncommit on the same memory, it is ok
0N/A return (p1->flags() & MemPointerRecord::tag_masks) == MemPointerRecord::tag_alloc ||
0N/A (p1->flags() & MemPointerRecord::tag_masks) == MemPointerRecord::tag_release;
0N/A }
0N/A#endif
0N/A};
0N/A
0N/Aclass MallocRecordIterator : public MemPointerArrayIterator {
0N/A private:
0N/A MemPointerArrayIteratorImpl _itr;
0N/A
0N/A
0N/A
0N/A public:
0N/A MallocRecordIterator(MemPointerArray* arr) : _itr(arr) {
0N/A }
0N/A
0N/A virtual MemPointer* current() const {
0N/A#ifdef ASSERT
0N/A MemPointer* cur_rec = _itr.current();
0N/A if (cur_rec != NULL) {
0N/A MemPointer* prev_rec = _itr.peek_prev();
0N/A MemPointer* next_rec = _itr.peek_next();
0N/A assert(prev_rec == NULL || prev_rec->addr() < cur_rec->addr(), "Sorting order");
0N/A assert(next_rec == NULL || next_rec->addr() > cur_rec->addr(), "Sorting order");
0N/A }
0N/A#endif
0N/A return _itr.current();
0N/A }
0N/A virtual MemPointer* next() {
0N/A MemPointerRecord* next_rec = (MemPointerRecord*)_itr.next();
0N/A // arena memory record is a special case, which we have to compare
0N/A // sequence number against its associated arena record.
0N/A if (next_rec != NULL && next_rec->is_arena_memory_record()) {
0N/A MemPointerRecord* prev_rec = (MemPointerRecord*)_itr.peek_prev();
0N/A // if there is an associated arena record, it has to be previous
0N/A // record because of sorting order (by address) - NMT generates a pseudo address
0N/A // for arena's size record by offsetting arena's address, that guarantees
0N/A // the order of arena record and it's size record.
0N/A if (prev_rec != NULL && prev_rec->is_arena_record() &&
0N/A next_rec->is_memory_record_of_arena(prev_rec)) {
0N/A if (prev_rec->seq() > next_rec->seq()) {
0N/A // Skip this arena memory record
0N/A // Two scenarios:
0N/A // - if the arena record is an allocation record, this early
0N/A // size record must be leftover by previous arena,
0N/A // and the last size record should have size = 0.
0N/A // - if the arena record is a deallocation record, this
0N/A // size record should be its cleanup record, which should
0N/A // also have size = 0. In other world, arena alway reset
0N/A // its size before gone (see Arena's destructor)
0N/A assert(next_rec->size() == 0, "size not reset");
0N/A return _itr.next();
0N/A } else {
0N/A assert(prev_rec->is_allocation_record(),
0N/A "Arena size record ahead of allocation record");
0N/A }
0N/A }
0N/A }
0N/A return next_rec;
0N/A }
0N/A
0N/A MemPointer* peek_next() const { ShouldNotReachHere(); return NULL; }
0N/A MemPointer* peek_prev() const { ShouldNotReachHere(); return NULL; }
0N/A void remove() { ShouldNotReachHere(); }
0N/A bool insert(MemPointer* ptr) { ShouldNotReachHere(); return false; }
0N/A bool insert_after(MemPointer* ptr) { ShouldNotReachHere(); return false; }
0N/A};
0N/A
0N/A// collapse duplicated records. Eliminating duplicated records here, is much
0N/A// cheaper than during promotion phase. However, it does have limitation - it
0N/A// can only eliminate duplicated records within the generation, there are
0N/A// still chances seeing duplicated records during promotion.
0N/A// We want to use the record with higher sequence number, because it has
0N/A// more accurate callsite pc.
0N/Aclass VMRecordIterator : public MemPointerArrayIterator {
0N/A private:
0N/A MemPointerArrayIteratorImpl _itr;
0N/A
0N/A public:
0N/A VMRecordIterator(MemPointerArray* arr) : _itr(arr) {
0N/A MemPointerRecord* cur = (MemPointerRecord*)_itr.current();
0N/A MemPointerRecord* next = (MemPointerRecord*)_itr.peek_next();
0N/A while (next != NULL) {
0N/A assert(cur != NULL, "Sanity check");
0N/A assert(((SeqMemPointerRecord*)next)->seq() > ((SeqMemPointerRecord*)cur)->seq(),
0N/A "pre-sort order");
0N/A
0N/A if (is_duplicated_record(cur, next)) {
0N/A _itr.next();
0N/A next = (MemPointerRecord*)_itr.peek_next();
0N/A } else {
0N/A break;
0N/A }
0N/A }
0N/A }
0N/A
0N/A virtual MemPointer* current() const {
0N/A return _itr.current();
0N/A }
0N/A
0N/A // get next record, but skip the duplicated records
0N/A virtual MemPointer* next() {
0N/A MemPointerRecord* cur = (MemPointerRecord*)_itr.next();
0N/A MemPointerRecord* next = (MemPointerRecord*)_itr.peek_next();
0N/A while (next != NULL) {
0N/A assert(cur != NULL, "Sanity check");
0N/A assert(((SeqMemPointerRecord*)next)->seq() > ((SeqMemPointerRecord*)cur)->seq(),
0N/A "pre-sort order");
0N/A
0N/A if (is_duplicated_record(cur, next)) {
0N/A _itr.next();
0N/A cur = next;
0N/A next = (MemPointerRecord*)_itr.peek_next();
0N/A } else {
0N/A break;
0N/A }
0N/A }
0N/A return cur;
0N/A }
0N/A
0N/A MemPointer* peek_next() const { ShouldNotReachHere(); return NULL; }
0N/A MemPointer* peek_prev() const { ShouldNotReachHere(); return NULL; }
0N/A void remove() { ShouldNotReachHere(); }
0N/A bool insert(MemPointer* ptr) { ShouldNotReachHere(); return false; }
0N/A bool insert_after(MemPointer* ptr) { ShouldNotReachHere(); return false; }
0N/A
0N/A private:
0N/A bool is_duplicated_record(MemPointerRecord* p1, MemPointerRecord* p2) const {
0N/A bool ret = (p1->addr() == p2->addr() && p1->size() == p2->size() && p1->flags() == p2->flags());
0N/A assert(!(ret && FLAGS_TO_MEMORY_TYPE(p1->flags()) == mtThreadStack), "dup on stack record");
0N/A return ret;
0N/A }
0N/A};
0N/A
0N/Aclass StagingArea : public _ValueObj {
0N/A private:
0N/A MemPointerArray* _malloc_data;
0N/A MemPointerArray* _vm_data;
0N/A
0N/A public:
0N/A StagingArea() : _malloc_data(NULL), _vm_data(NULL) {
0N/A init();
0N/A }
0N/A
0N/A ~StagingArea() {
0N/A if (_malloc_data != NULL) delete _malloc_data;
0N/A if (_vm_data != NULL) delete _vm_data;
0N/A }
0N/A
0N/A MallocRecordIterator malloc_record_walker() {
0N/A return MallocRecordIterator(malloc_data());
0N/A }
0N/A
0N/A VMRecordIterator virtual_memory_record_walker();
0N/A
0N/A bool init();
0N/A void clear() {
0N/A assert(_malloc_data != NULL && _vm_data != NULL, "Just check");
0N/A _malloc_data->shrink();
0N/A _malloc_data->clear();
0N/A _vm_data->clear();
0N/A }
0N/A
0N/A inline MemPointerArray* malloc_data() { return _malloc_data; }
0N/A inline MemPointerArray* vm_data() { return _vm_data; }
0N/A};
0N/A
0N/Aclass MemBaseline;
0N/Aclass MemSnapshot : public CHeapObj<mtNMT> {
0N/A private:
0N/A // the following two arrays contain records of all known lived memory blocks
0N/A // live malloc-ed memory pointers
0N/A MemPointerArray* _alloc_ptrs;
0N/A // live virtual memory pointers
0N/A MemPointerArray* _vm_ptrs;
0N/A
0N/A StagingArea _staging_area;
0N/A
0N/A // the lock to protect this snapshot
0N/A Monitor* _lock;
0N/A
0N/A NOT_PRODUCT(size_t _untracked_count;)
0N/A friend class MemBaseline;
0N/A
0N/A public:
0N/A MemSnapshot();
0N/A virtual ~MemSnapshot();
0N/A
0N/A // if we are running out of native memory
0N/A bool out_of_memory() {
0N/A return (_alloc_ptrs == NULL ||
0N/A _staging_area.malloc_data() == NULL ||
0N/A _staging_area.vm_data() == NULL ||
0N/A _vm_ptrs == NULL || _lock == NULL ||
0N/A _alloc_ptrs->out_of_memory() ||
0N/A _vm_ptrs->out_of_memory());
0N/A }
0N/A
// merge a per-thread memory recorder into staging area
bool merge(MemRecorder* rec);
// promote staged data to snapshot
bool promote();
void wait(long timeout) {
assert(_lock != NULL, "Just check");
MonitorLockerEx locker(_lock);
locker.wait(true, timeout);
}
NOT_PRODUCT(void print_snapshot_stats(outputStream* st);)
NOT_PRODUCT(void check_staging_data();)
NOT_PRODUCT(void check_malloc_pointers();)
NOT_PRODUCT(bool has_allocation_record(address addr);)
// dump all virtual memory pointers in snapshot
DEBUG_ONLY( void dump_all_vm_pointers();)
private:
// copy sequenced pointer from src to dest
void copy_seq_pointer(MemPointerRecord* dest, const MemPointerRecord* src);
// assign a sequenced pointer to non-sequenced pointer
void assign_pointer(MemPointerRecord*dest, const MemPointerRecord* src);
bool promote_malloc_records(MemPointerArrayIterator* itr);
bool promote_virtual_memory_records(MemPointerArrayIterator* itr);
};
#endif // SHARE_VM_SERVICES_MEM_SNAPSHOT_HPP