1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/vm/services/memRecorder.hpp Thu Jun 28 17:03:16 2012 -0400 1.3 @@ -0,0 +1,267 @@ 1.4 +/* 1.5 + * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + * 1.26 + */ 1.27 + 1.28 +#ifndef SHARE_VM_SERVICES_MEM_RECORDER_HPP 1.29 +#define SHARE_VM_SERVICES_MEM_RECORDER_HPP 1.30 + 1.31 +#include "memory/allocation.hpp" 1.32 +#include "runtime/os.hpp" 1.33 +#include "services/memPtrArray.hpp" 1.34 + 1.35 +class MemSnapshot; 1.36 +class MemTracker; 1.37 +class MemTrackWorker; 1.38 + 1.39 +// Fixed size memory pointer array implementation 1.40 +template <class E, int SIZE> class FixedSizeMemPointerArray : 1.41 + public MemPointerArray { 1.42 + // This implementation is for memory recorder only 1.43 + friend class MemRecorder; 1.44 + 1.45 + private: 1.46 + E _data[SIZE]; 1.47 + int _size; 1.48 + 1.49 + protected: 1.50 + FixedSizeMemPointerArray(bool init_elements = false): 1.51 + _size(0){ 1.52 + if (init_elements) { 1.53 + for (int index = 0; index < SIZE; index ++) { 1.54 + ::new ((void*)&_data[index]) E(); 1.55 + } 1.56 + } 1.57 + } 1.58 + 1.59 + void* operator new(size_t size, const std::nothrow_t& nothrow_constant) { 1.60 + // the instance is part of memRecorder, needs to be tagged with 'otNMTRecorder' 1.61 + // to avoid recursion 1.62 + return os::malloc(size, (mtNMT | otNMTRecorder)); 1.63 + } 1.64 + 1.65 + void* operator new(size_t size) { 1.66 + assert(false, "use nothrow version"); 1.67 + return NULL; 1.68 + } 1.69 + 1.70 + void operator delete(void* p) { 1.71 + os::free(p, (mtNMT | otNMTRecorder)); 1.72 + } 1.73 + 1.74 + // instance size 1.75 + inline size_t instance_size() const { 1.76 + return sizeof(FixedSizeMemPointerArray<E, SIZE>); 1.77 + } 1.78 + 1.79 + debug_only(int capacity() const { return SIZE; }) 1.80 + 1.81 + public: 1.82 + // implementation of public interface 1.83 + bool out_of_memory() const { return false; } 1.84 + bool is_empty() const { return _size == 0; } 1.85 + bool is_full() { return length() >= SIZE; } 1.86 + int length() const { return _size; } 1.87 + 1.88 + void clear() { 1.89 + _size = 0; 1.90 + } 1.91 + 1.92 + bool append(MemPointer* ptr) { 1.93 + if (is_full()) return false; 1.94 + _data[_size ++] = *(E*)ptr; 1.95 + return true; 1.96 + } 1.97 + 1.98 + virtual bool insert_at(MemPointer* p, int pos) { 1.99 + assert(false, "append only"); 1.100 + return false; 1.101 + } 1.102 + 1.103 + virtual bool remove_at(int pos) { 1.104 + assert(false, "not supported"); 1.105 + return false; 1.106 + } 1.107 + 1.108 + MemPointer* at(int index) const { 1.109 + assert(index >= 0 && index < length(), 1.110 + "parameter check"); 1.111 + return ((E*)&_data[index]); 1.112 + } 1.113 + 1.114 + void sort(FN_SORT fn) { 1.115 + qsort((void*)_data, _size, sizeof(E), fn); 1.116 + } 1.117 + 1.118 + bool shrink() { 1.119 + return false; 1.120 + } 1.121 +}; 1.122 + 1.123 + 1.124 +// This iterator requires pre-sorted MemPointerArray, which is sorted by: 1.125 +// 1. address 1.126 +// 2. allocation type 1.127 +// 3. sequence number 1.128 +// During the array walking, iterator collapses pointers with the same 1.129 +// address and allocation type, and only returns the one with highest 1.130 +// sequence number. 1.131 +// 1.132 +// This is read-only iterator, update methods are asserted. 1.133 +class SequencedRecordIterator : public MemPointerArrayIterator { 1.134 + private: 1.135 + MemPointerArrayIteratorImpl _itr; 1.136 + MemPointer* _cur; 1.137 + 1.138 + public: 1.139 + SequencedRecordIterator(const MemPointerArray* arr): 1.140 + _itr(const_cast<MemPointerArray*>(arr)) { 1.141 + _cur = next_record(); 1.142 + } 1.143 + 1.144 + SequencedRecordIterator(const SequencedRecordIterator& itr): 1.145 + _itr(itr._itr) { 1.146 + _cur = next_record(); 1.147 + } 1.148 + 1.149 + // return the pointer at current position 1.150 + virtual MemPointer* current() const { 1.151 + return _cur; 1.152 + }; 1.153 + 1.154 + // return the next pointer and advance current position 1.155 + virtual MemPointer* next() { 1.156 + _cur = next_record(); 1.157 + return _cur; 1.158 + } 1.159 + 1.160 + // return the next pointer without advancing current position 1.161 + virtual MemPointer* peek_next() const { 1.162 + assert(false, "not implemented"); 1.163 + return NULL; 1.164 + 1.165 + } 1.166 + // return the previous pointer without changing current position 1.167 + virtual MemPointer* peek_prev() const { 1.168 + assert(false, "not implemented"); 1.169 + return NULL; 1.170 + } 1.171 + 1.172 + // remove the pointer at current position 1.173 + virtual void remove() { 1.174 + assert(false, "read-only iterator"); 1.175 + }; 1.176 + // insert the pointer at current position 1.177 + virtual bool insert(MemPointer* ptr) { 1.178 + assert(false, "read-only iterator"); 1.179 + return false; 1.180 + } 1.181 + 1.182 + virtual bool insert_after(MemPointer* ptr) { 1.183 + assert(false, "read-only iterator"); 1.184 + return false; 1.185 + } 1.186 + private: 1.187 + // collapse the 'same kind' of records, and return this 'kind' of 1.188 + // record with highest sequence number 1.189 + MemPointer* next_record(); 1.190 + 1.191 + // Test if the two records are the same kind: the same memory block and allocation 1.192 + // type. 1.193 + inline bool same_kind(const MemPointerRecord* p1, const MemPointerRecord* p2) const { 1.194 + return (p1->addr() == p2->addr() && 1.195 + (p1->flags() &MemPointerRecord::tag_masks) == 1.196 + (p2->flags() & MemPointerRecord::tag_masks)); 1.197 + } 1.198 +}; 1.199 + 1.200 + 1.201 + 1.202 +#define DEFAULT_RECORDER_PTR_ARRAY_SIZE 512 1.203 + 1.204 +class MemRecorder : public CHeapObj<mtNMT|otNMTRecorder> { 1.205 + friend class MemSnapshot; 1.206 + friend class MemTracker; 1.207 + friend class MemTrackWorker; 1.208 + 1.209 + protected: 1.210 + // the array that holds memory records 1.211 + MemPointerArray* _pointer_records; 1.212 + 1.213 + private: 1.214 + // used for linked list 1.215 + MemRecorder* _next; 1.216 + // active recorder can only record a certain generation data 1.217 + debug_only(unsigned long _generation;) 1.218 + 1.219 + protected: 1.220 + _NOINLINE_ MemRecorder(); 1.221 + ~MemRecorder(); 1.222 + 1.223 + // record a memory operation 1.224 + bool record(address addr, MEMFLAGS flags, size_t size, address caller_pc = 0); 1.225 + 1.226 + // linked list support 1.227 + inline void set_next(MemRecorder* rec) { 1.228 + _next = rec; 1.229 + } 1.230 + 1.231 + inline MemRecorder* next() const { 1.232 + return _next; 1.233 + } 1.234 + 1.235 + // if the recorder is full 1.236 + inline bool is_full() const { 1.237 + assert(_pointer_records != NULL, "just check"); 1.238 + return _pointer_records->is_full(); 1.239 + } 1.240 + 1.241 + // if running out of memory when initializing recorder's internal 1.242 + // data 1.243 + inline bool out_of_memory() const { 1.244 + return (_pointer_records == NULL || 1.245 + _pointer_records->out_of_memory()); 1.246 + } 1.247 + 1.248 + inline void clear() { 1.249 + assert(_pointer_records != NULL, "Just check"); 1.250 + _pointer_records->clear(); 1.251 + } 1.252 + 1.253 + SequencedRecordIterator pointer_itr(); 1.254 + 1.255 + public: 1.256 + // number of MemRecorder instance 1.257 + debug_only(static volatile jint _instance_count;) 1.258 + 1.259 + private: 1.260 + // sorting function, sort records into following order 1.261 + // 1. memory address 1.262 + // 2. allocation type 1.263 + // 3. sequence number 1.264 + static int sort_record_fn(const void* e1, const void* e2); 1.265 + 1.266 + debug_only(void check_dup_seq(jint seq) const;) 1.267 + debug_only(void set_generation();) 1.268 +}; 1.269 + 1.270 +#endif // SHARE_VM_SERVICES_MEM_RECORDER_HPP