memSnapshot.hpp revision 4064
1008N/A/*
1008N/A * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
1008N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1008N/A *
1008N/A * This code is free software; you can redistribute it and/or modify it
1008N/A * under the terms of the GNU General Public License version 2 only, as
1008N/A * published by the Free Software Foundation.
1008N/A *
1008N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1008N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1008N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1008N/A * version 2 for more details (a copy is included in the LICENSE file that
1008N/A * accompanied this code).
1008N/A *
1008N/A * You should have received a copy of the GNU General Public License version
1008N/A * 2 along with this work; if not, write to the Free Software Foundation,
1008N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1008N/A *
1008N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1008N/A * or visit www.oracle.com if you need additional information or have any
1008N/A * questions.
1008N/A *
1008N/A */
1008N/A
1008N/A#ifndef SHARE_VM_SERVICES_MEM_SNAPSHOT_HPP
1008N/A#define SHARE_VM_SERVICES_MEM_SNAPSHOT_HPP
1008N/A
1008N/A#include "memory/allocation.hpp"
1008N/A#include "runtime/mutex.hpp"
1008N/A#include "runtime/mutexLocker.hpp"
1008N/A#include "services/memBaseline.hpp"
1008N/A#include "services/memPtrArray.hpp"
1008N/A
1008N/A
1008N/A// Snapshot pointer array iterator
1008N/A
1008N/A// The pointer array contains malloc-ed pointers
1008N/Aclass MemPointerIterator : public MemPointerArrayIteratorImpl {
1008N/A public:
1008N/A MemPointerIterator(MemPointerArray* arr):
1008N/A MemPointerArrayIteratorImpl(arr) {
1008N/A assert(arr != NULL, "null array");
1008N/A }
1008N/A
1008N/A#ifdef ASSERT
1008N/A virtual bool is_dup_pointer(const MemPointer* ptr1,
1008N/A const MemPointer* ptr2) const {
1008N/A MemPointerRecord* p1 = (MemPointerRecord*)ptr1;
1008N/A MemPointerRecord* p2 = (MemPointerRecord*)ptr2;
1008N/A
1008N/A if (p1->addr() != p2->addr()) return false;
1008N/A if ((p1->flags() & MemPointerRecord::tag_masks) !=
1008N/A (p2->flags() & MemPointerRecord::tag_masks)) {
1008N/A return false;
1008N/A }
1008N/A // we do see multiple commit/uncommit on the same memory, it is ok
1008N/A return (p1->flags() & MemPointerRecord::tag_masks) == MemPointerRecord::tag_alloc ||
1008N/A (p1->flags() & MemPointerRecord::tag_masks) == MemPointerRecord::tag_release;
1008N/A }
1008N/A
1008N/A virtual bool insert(MemPointer* ptr) {
1008N/A if (_pos > 0) {
1008N/A MemPointer* p1 = (MemPointer*)ptr;
1008N/A MemPointer* p2 = (MemPointer*)_array->at(_pos - 1);
1008N/A assert(!is_dup_pointer(p1, p2),
1008N/A err_msg("duplicated pointer, flag = [%x]", (unsigned int)((MemPointerRecord*)p1)->flags()));
1008N/A }
1008N/A if (_pos < _array->length() -1) {
1008N/A MemPointer* p1 = (MemPointer*)ptr;
1008N/A MemPointer* p2 = (MemPointer*)_array->at(_pos + 1);
1008N/A assert(!is_dup_pointer(p1, p2),
1008N/A err_msg("duplicated pointer, flag = [%x]", (unsigned int)((MemPointerRecord*)p1)->flags()));
1008N/A }
1008N/A return _array->insert_at(ptr, _pos);
1008N/A }
1008N/A
1008N/A virtual bool insert_after(MemPointer* ptr) {
1008N/A if (_pos > 0) {
1008N/A MemPointer* p1 = (MemPointer*)ptr;
1008N/A MemPointer* p2 = (MemPointer*)_array->at(_pos - 1);
1008N/A assert(!is_dup_pointer(p1, p2),
1008N/A err_msg("duplicated pointer, flag = [%x]", (unsigned int)((MemPointerRecord*)p1)->flags()));
1008N/A }
1008N/A if (_pos < _array->length() - 1) {
1008N/A MemPointer* p1 = (MemPointer*)ptr;
1008N/A MemPointer* p2 = (MemPointer*)_array->at(_pos + 1);
1008N/A
1008N/A assert(!is_dup_pointer(p1, p2),
1008N/A err_msg("duplicated pointer, flag = [%x]", (unsigned int)((MemPointerRecord*)p1)->flags()));
1008N/A }
1008N/A if (_array->insert_at(ptr, _pos + 1)) {
1008N/A _pos ++;
1008N/A return true;
1008N/A }
1008N/A return false;
1008N/A }
1008N/A#endif
1008N/A
1008N/A virtual MemPointer* locate(address addr) {
1008N/A MemPointer* cur = current();
1008N/A while (cur != NULL && cur->addr() < addr) {
1008N/A cur = next();
1008N/A }
1008N/A return cur;
1008N/A }
1008N/A};
1008N/A
1008N/Aclass VMMemPointerIterator : public MemPointerIterator {
1008N/A public:
1008N/A VMMemPointerIterator(MemPointerArray* arr):
1008N/A MemPointerIterator(arr) {
1008N/A }
1008N/A
1008N/A // locate an existing reserved memory region that contains specified address,
1008N/A // or the reserved region just above this address, where the incoming
1008N/A // reserved region should be inserted.
1008N/A virtual MemPointer* locate(address addr) {
1008N/A reset();
1008N/A VMMemRegion* reg = (VMMemRegion*)current();
1008N/A while (reg != NULL) {
1008N/A if (reg->is_reserved_region()) {
1008N/A if (reg->contains_address(addr) || addr < reg->base()) {
1008N/A return reg;
1008N/A }
1008N/A }
1008N/A reg = (VMMemRegion*)next();
1008N/A }
1008N/A return NULL;
1008N/A }
1008N/A
1008N/A // following methods update virtual memory in the context
1008N/A // of 'current' position, which is properly positioned by
1008N/A // callers via locate method.
1008N/A bool add_reserved_region(MemPointerRecord* rec);
1008N/A bool add_committed_region(MemPointerRecord* rec);
1008N/A bool remove_uncommitted_region(MemPointerRecord* rec);
1008N/A bool remove_released_region(MemPointerRecord* rec);
1008N/A
1008N/A // split a reserved region to create a new memory region with specified base and size
1008N/A bool split_reserved_region(VMMemRegion* rgn, address new_rgn_addr, size_t new_rgn_size);
1008N/A private:
1008N/A bool insert_record(MemPointerRecord* rec);
1008N/A bool insert_record_after(MemPointerRecord* rec);
1008N/A
1008N/A bool insert_reserved_region(MemPointerRecord* rec);
1008N/A
1008N/A // reset current position
1008N/A inline void reset() { _pos = 0; }
1008N/A#ifdef ASSERT
1008N/A virtual bool is_dup_pointer(const MemPointer* ptr1,
1008N/A const MemPointer* ptr2) const {
1008N/A VMMemRegion* p1 = (VMMemRegion*)ptr1;
1008N/A VMMemRegion* p2 = (VMMemRegion*)ptr2;
1008N/A
1008N/A if (p1->addr() != p2->addr()) return false;
1008N/A if ((p1->flags() & MemPointerRecord::tag_masks) !=
1008N/A (p2->flags() & MemPointerRecord::tag_masks)) {
1008N/A return false;
1008N/A }
1008N/A // we do see multiple commit/uncommit on the same memory, it is ok
1008N/A return (p1->flags() & MemPointerRecord::tag_masks) == MemPointerRecord::tag_alloc ||
1008N/A (p1->flags() & MemPointerRecord::tag_masks) == MemPointerRecord::tag_release;
1008N/A }
1008N/A#endif
1008N/A};
1008N/A
1008N/Aclass MallocRecordIterator : public MemPointerArrayIterator {
1008N/A protected:
1008N/A MemPointerArrayIteratorImpl _itr;
1008N/A
1008N/A public:
1008N/A MallocRecordIterator(MemPointerArray* arr) : _itr(arr) {
1008N/A }
1008N/A
1008N/A virtual MemPointer* current() const {
1008N/A MemPointerRecord* cur = (MemPointerRecord*)_itr.current();
1008N/A assert(cur == NULL || !cur->is_vm_pointer(), "seek error");
1008N/A MemPointerRecord* next = (MemPointerRecord*)_itr.peek_next();
1008N/A if (next == NULL || next->addr() != cur->addr()) {
1008N/A return cur;
1008N/A } else {
1008N/A assert(!cur->is_vm_pointer(), "Sanity check");
1008N/A assert(cur->is_allocation_record() && next->is_deallocation_record(),
1008N/A "sorting order");
1008N/A assert(cur->seq() != next->seq(), "Sanity check");
1008N/A return cur->seq() > next->seq() ? cur : next;
1008N/A }
1008N/A }
1008N/A
1008N/A virtual MemPointer* next() {
1008N/A MemPointerRecord* cur = (MemPointerRecord*)_itr.current();
1008N/A assert(cur == NULL || !cur->is_vm_pointer(), "Sanity check");
1008N/A MemPointerRecord* next = (MemPointerRecord*)_itr.next();
1008N/A if (next == NULL) {
1008N/A return NULL;
1008N/A }
1008N/A if (cur->addr() == next->addr()) {
1008N/A next = (MemPointerRecord*)_itr.next();
1008N/A }
1008N/A return current();
1008N/A }
1008N/A
1008N/A MemPointer* peek_next() const { ShouldNotReachHere(); return NULL; }
1008N/A MemPointer* peek_prev() const { ShouldNotReachHere(); return NULL; }
1008N/A void remove() { ShouldNotReachHere(); }
1008N/A bool insert(MemPointer* ptr) { ShouldNotReachHere(); return false; }
1008N/A bool insert_after(MemPointer* ptr) { ShouldNotReachHere(); return false; }
1008N/A};
1008N/A
1008N/A// collapse duplicated records. Eliminating duplicated records here, is much
1008N/A// cheaper than during promotion phase. However, it does have limitation - it
1008N/A// can only eliminate duplicated records within the generation, there are
1008N/A// still chances seeing duplicated records during promotion.
1008N/A// We want to use the record with higher sequence number, because it has
1008N/A// more accurate callsite pc.
1008N/Aclass VMRecordIterator : public MallocRecordIterator {
1008N/A public:
1008N/A VMRecordIterator(MemPointerArray* arr) : MallocRecordIterator(arr) {
1008N/A MemPointerRecord* cur = (MemPointerRecord*)_itr.current();
1008N/A MemPointerRecord* next = (MemPointerRecord*)_itr.peek_next();
1008N/A while (next != NULL) {
1008N/A assert(cur != NULL, "Sanity check");
1008N/A assert(((SeqMemPointerRecord*)next)->seq() > ((SeqMemPointerRecord*)cur)->seq(),
1008N/A "pre-sort order");
1008N/A
1008N/A if (is_duplicated_record(cur, next)) {
1008N/A _itr.next();
1008N/A next = (MemPointerRecord*)_itr.peek_next();
1008N/A } else {
1008N/A break;
1008N/A }
1008N/A }
1008N/A }
1008N/A
1008N/A virtual MemPointer* current() const {
1008N/A return _itr.current();
1008N/A }
1008N/A
1008N/A // get next record, but skip the duplicated records
1008N/A virtual MemPointer* next() {
1008N/A MemPointerRecord* cur = (MemPointerRecord*)_itr.next();
1008N/A MemPointerRecord* next = (MemPointerRecord*)_itr.peek_next();
1008N/A while (next != NULL) {
1008N/A assert(cur != NULL, "Sanity check");
1008N/A assert(((SeqMemPointerRecord*)next)->seq() > ((SeqMemPointerRecord*)cur)->seq(),
1008N/A "pre-sort order");
1008N/A
1008N/A if (is_duplicated_record(cur, next)) {
1008N/A _itr.next();
1008N/A cur = next;
1008N/A next = (MemPointerRecord*)_itr.peek_next();
1008N/A } else {
1008N/A break;
1008N/A }
1008N/A }
1008N/A return cur;
1008N/A }
1008N/A
1008N/A private:
1008N/A bool is_duplicated_record(MemPointerRecord* p1, MemPointerRecord* p2) const {
1008N/A bool ret = (p1->addr() == p2->addr() && p1->size() == p2->size() && p1->flags() == p2->flags());
1008N/A assert(!(ret && FLAGS_TO_MEMORY_TYPE(p1->flags()) == mtThreadStack), "dup on stack record");
1008N/A return ret;
1008N/A }
1008N/A};
1008N/A
1008N/Aclass StagingArea : public _ValueObj {
1008N/A private:
1008N/A MemPointerArray* _malloc_data;
1008N/A MemPointerArray* _vm_data;
1008N/A
1008N/A public:
1008N/A StagingArea() : _malloc_data(NULL), _vm_data(NULL) {
1008N/A init();
1008N/A }
1008N/A
1008N/A ~StagingArea() {
1008N/A if (_malloc_data != NULL) delete _malloc_data;
1008N/A if (_vm_data != NULL) delete _vm_data;
1008N/A }
1008N/A
1008N/A MallocRecordIterator malloc_record_walker() {
1008N/A return MallocRecordIterator(malloc_data());
1008N/A }
1008N/A
1008N/A VMRecordIterator virtual_memory_record_walker();
1008N/A
1008N/A bool init();
1008N/A void clear() {
1008N/A assert(_malloc_data != NULL && _vm_data != NULL, "Just check");
1008N/A _malloc_data->shrink();
1008N/A _malloc_data->clear();
1008N/A _vm_data->clear();
1008N/A }
1008N/A
1008N/A inline MemPointerArray* malloc_data() { return _malloc_data; }
1008N/A inline MemPointerArray* vm_data() { return _vm_data; }
1008N/A};
1008N/A
1008N/Aclass MemBaseline;
1008N/Aclass MemSnapshot : public CHeapObj<mtNMT> {
1008N/A private:
1008N/A // the following two arrays contain records of all known lived memory blocks
1008N/A // live malloc-ed memory pointers
1008N/A MemPointerArray* _alloc_ptrs;
1008N/A // live virtual memory pointers
1008N/A MemPointerArray* _vm_ptrs;
1008N/A
1008N/A StagingArea _staging_area;
1008N/A
1008N/A // the lock to protect this snapshot
1008N/A Monitor* _lock;
1008N/A
1008N/A NOT_PRODUCT(size_t _untracked_count;)
1008N/A friend class MemBaseline;
1008N/A
1008N/A public:
1008N/A MemSnapshot();
1008N/A virtual ~MemSnapshot();
1008N/A
1008N/A // if we are running out of native memory
1008N/A bool out_of_memory() {
1008N/A return (_alloc_ptrs == NULL ||
1008N/A _staging_area.malloc_data() == NULL ||
1008N/A _staging_area.vm_data() == NULL ||
1008N/A _vm_ptrs == NULL || _lock == NULL ||
1008N/A _alloc_ptrs->out_of_memory() ||
1008N/A _vm_ptrs->out_of_memory());
1008N/A }
1008N/A
1008N/A // merge a per-thread memory recorder into staging area
1008N/A bool merge(MemRecorder* rec);
1008N/A // promote staged data to snapshot
1008N/A bool promote();
1008N/A
1008N/A
1008N/A void wait(long timeout) {
1008N/A assert(_lock != NULL, "Just check");
1008N/A MonitorLockerEx locker(_lock);
1008N/A locker.wait(true, timeout);
1008N/A }
1008N/A
1008N/A NOT_PRODUCT(void print_snapshot_stats(outputStream* st);)
1008N/A NOT_PRODUCT(void check_staging_data();)
1008N/A NOT_PRODUCT(void check_malloc_pointers();)
1008N/A NOT_PRODUCT(bool has_allocation_record(address addr);)
1008N/A // dump all virtual memory pointers in snapshot
1008N/A DEBUG_ONLY( void dump_all_vm_pointers();)
1008N/A
1008N/A private:
1008N/A // copy pointer data from src to dest
1008N/A void copy_pointer(MemPointerRecord* dest, const MemPointerRecord* src);
1008N/A
2499N/A bool promote_malloc_records(MemPointerArrayIterator* itr);
2499N/A bool promote_virtual_memory_records(MemPointerArrayIterator* itr);
2499N/A};
1008N/A
1008N/A#endif // SHARE_VM_SERVICES_MEM_SNAPSHOT_HPP
1008N/A