src/share/vm/services/memRecorder.hpp

Wed, 27 Apr 2016 01:25:04 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:25:04 +0800
changeset 0
f90c822e73f8
child 6876
710a3c8b516e
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/
changeset: 6782:28b50d07f6f8
tag: jdk8u25-b17

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #ifndef SHARE_VM_SERVICES_MEM_RECORDER_HPP
aoqi@0 26 #define SHARE_VM_SERVICES_MEM_RECORDER_HPP
aoqi@0 27
aoqi@0 28 #include "memory/allocation.hpp"
aoqi@0 29 #include "runtime/os.hpp"
aoqi@0 30 #include "services/memPtrArray.hpp"
aoqi@0 31
aoqi@0 32 class MemSnapshot;
aoqi@0 33 class MemTracker;
aoqi@0 34 class MemTrackWorker;
aoqi@0 35
aoqi@0 36 // Fixed size memory pointer array implementation
aoqi@0 37 template <class E, int SIZE> class FixedSizeMemPointerArray :
aoqi@0 38 public MemPointerArray {
aoqi@0 39 // This implementation is for memory recorder only
aoqi@0 40 friend class MemRecorder;
aoqi@0 41
aoqi@0 42 private:
aoqi@0 43 E _data[SIZE];
aoqi@0 44 int _size;
aoqi@0 45
aoqi@0 46 protected:
aoqi@0 47 FixedSizeMemPointerArray(bool init_elements = false):
aoqi@0 48 _size(0){
aoqi@0 49 if (init_elements) {
aoqi@0 50 for (int index = 0; index < SIZE; index ++) {
aoqi@0 51 ::new ((void*)&_data[index]) E();
aoqi@0 52 }
aoqi@0 53 }
aoqi@0 54 }
aoqi@0 55
aoqi@0 56 void* operator new(size_t size, const std::nothrow_t& nothrow_constant) throw() {
aoqi@0 57 // the instance is part of memRecorder, needs to be tagged with 'otNMTRecorder'
aoqi@0 58 // to avoid recursion
aoqi@0 59 return os::malloc(size, (mtNMT | otNMTRecorder));
aoqi@0 60 }
aoqi@0 61
aoqi@0 62 void* operator new(size_t size) throw() {
aoqi@0 63 assert(false, "use nothrow version");
aoqi@0 64 return NULL;
aoqi@0 65 }
aoqi@0 66
aoqi@0 67 void operator delete(void* p) {
aoqi@0 68 os::free(p, (mtNMT | otNMTRecorder));
aoqi@0 69 }
aoqi@0 70
aoqi@0 71 // instance size
aoqi@0 72 inline size_t instance_size() const {
aoqi@0 73 return sizeof(FixedSizeMemPointerArray<E, SIZE>);
aoqi@0 74 }
aoqi@0 75
aoqi@0 76 NOT_PRODUCT(int capacity() const { return SIZE; })
aoqi@0 77
aoqi@0 78 public:
aoqi@0 79 // implementation of public interface
aoqi@0 80 bool out_of_memory() const { return false; }
aoqi@0 81 bool is_empty() const { return _size == 0; }
aoqi@0 82 bool is_full() { return length() >= SIZE; }
aoqi@0 83 int length() const { return _size; }
aoqi@0 84
aoqi@0 85 void clear() {
aoqi@0 86 _size = 0;
aoqi@0 87 }
aoqi@0 88
aoqi@0 89 bool append(MemPointer* ptr) {
aoqi@0 90 if (is_full()) return false;
aoqi@0 91 _data[_size ++] = *(E*)ptr;
aoqi@0 92 return true;
aoqi@0 93 }
aoqi@0 94
aoqi@0 95 virtual bool insert_at(MemPointer* p, int pos) {
aoqi@0 96 assert(false, "append only");
aoqi@0 97 return false;
aoqi@0 98 }
aoqi@0 99
aoqi@0 100 virtual bool remove_at(int pos) {
aoqi@0 101 assert(false, "not supported");
aoqi@0 102 return false;
aoqi@0 103 }
aoqi@0 104
aoqi@0 105 MemPointer* at(int index) const {
aoqi@0 106 assert(index >= 0 && index < length(),
aoqi@0 107 "parameter check");
aoqi@0 108 return ((E*)&_data[index]);
aoqi@0 109 }
aoqi@0 110
aoqi@0 111 void sort(FN_SORT fn) {
aoqi@0 112 qsort((void*)_data, _size, sizeof(E), fn);
aoqi@0 113 }
aoqi@0 114
aoqi@0 115 bool shrink() {
aoqi@0 116 return false;
aoqi@0 117 }
aoqi@0 118 };
aoqi@0 119
aoqi@0 120
aoqi@0 121 // This iterator requires pre-sorted MemPointerArray, which is sorted by:
aoqi@0 122 // 1. address
aoqi@0 123 // 2. allocation type
aoqi@0 124 // 3. sequence number
aoqi@0 125 // During the array walking, iterator collapses pointers with the same
aoqi@0 126 // address and allocation type, and only returns the one with highest
aoqi@0 127 // sequence number.
aoqi@0 128 //
aoqi@0 129 // This is read-only iterator, update methods are asserted.
aoqi@0 130 class SequencedRecordIterator : public MemPointerArrayIterator {
aoqi@0 131 private:
aoqi@0 132 MemPointerArrayIteratorImpl _itr;
aoqi@0 133 MemPointer* _cur;
aoqi@0 134
aoqi@0 135 public:
aoqi@0 136 SequencedRecordIterator(const MemPointerArray* arr):
aoqi@0 137 _itr(const_cast<MemPointerArray*>(arr)) {
aoqi@0 138 _cur = next_record();
aoqi@0 139 }
aoqi@0 140
aoqi@0 141 SequencedRecordIterator(const SequencedRecordIterator& itr):
aoqi@0 142 _itr(itr._itr) {
aoqi@0 143 _cur = next_record();
aoqi@0 144 }
aoqi@0 145
aoqi@0 146 // return the pointer at current position
aoqi@0 147 virtual MemPointer* current() const {
aoqi@0 148 return _cur;
aoqi@0 149 };
aoqi@0 150
aoqi@0 151 // return the next pointer and advance current position
aoqi@0 152 virtual MemPointer* next() {
aoqi@0 153 _cur = next_record();
aoqi@0 154 return _cur;
aoqi@0 155 }
aoqi@0 156
aoqi@0 157 // return the next pointer without advancing current position
aoqi@0 158 virtual MemPointer* peek_next() const {
aoqi@0 159 assert(false, "not implemented");
aoqi@0 160 return NULL;
aoqi@0 161
aoqi@0 162 }
aoqi@0 163 // return the previous pointer without changing current position
aoqi@0 164 virtual MemPointer* peek_prev() const {
aoqi@0 165 assert(false, "not implemented");
aoqi@0 166 return NULL;
aoqi@0 167 }
aoqi@0 168
aoqi@0 169 // remove the pointer at current position
aoqi@0 170 virtual void remove() {
aoqi@0 171 assert(false, "read-only iterator");
aoqi@0 172 };
aoqi@0 173 // insert the pointer at current position
aoqi@0 174 virtual bool insert(MemPointer* ptr) {
aoqi@0 175 assert(false, "read-only iterator");
aoqi@0 176 return false;
aoqi@0 177 }
aoqi@0 178
aoqi@0 179 virtual bool insert_after(MemPointer* ptr) {
aoqi@0 180 assert(false, "read-only iterator");
aoqi@0 181 return false;
aoqi@0 182 }
aoqi@0 183 private:
aoqi@0 184 // collapse the 'same kind' of records, and return this 'kind' of
aoqi@0 185 // record with highest sequence number
aoqi@0 186 MemPointer* next_record();
aoqi@0 187
aoqi@0 188 // Test if the two records are the same kind: the same memory block and allocation
aoqi@0 189 // type.
aoqi@0 190 inline bool same_kind(const MemPointerRecord* p1, const MemPointerRecord* p2) const {
aoqi@0 191 assert(!p1->is_vm_pointer() && !p2->is_vm_pointer(), "malloc pointer only");
aoqi@0 192 return (p1->addr() == p2->addr() &&
aoqi@0 193 (p1->flags() &MemPointerRecord::tag_masks) ==
aoqi@0 194 (p2->flags() & MemPointerRecord::tag_masks));
aoqi@0 195 }
aoqi@0 196 };
aoqi@0 197
aoqi@0 198
aoqi@0 199
aoqi@0 200 #define DEFAULT_RECORDER_PTR_ARRAY_SIZE 512
aoqi@0 201
aoqi@0 202 class MemRecorder : public CHeapObj<mtNMT|otNMTRecorder> {
aoqi@0 203 friend class MemSnapshot;
aoqi@0 204 friend class MemTracker;
aoqi@0 205 friend class MemTrackWorker;
aoqi@0 206 friend class GenerationData;
aoqi@0 207
aoqi@0 208 protected:
aoqi@0 209 // the array that holds memory records
aoqi@0 210 MemPointerArray* _pointer_records;
aoqi@0 211
aoqi@0 212 private:
aoqi@0 213 // used for linked list
aoqi@0 214 MemRecorder* _next;
aoqi@0 215 // active recorder can only record a certain generation data
aoqi@0 216 unsigned long _generation;
aoqi@0 217
aoqi@0 218 protected:
aoqi@0 219 _NOINLINE_ MemRecorder();
aoqi@0 220 ~MemRecorder();
aoqi@0 221
aoqi@0 222 // record a memory operation
aoqi@0 223 bool record(address addr, MEMFLAGS flags, size_t size, jint seq, address caller_pc = 0);
aoqi@0 224
aoqi@0 225 // linked list support
aoqi@0 226 inline void set_next(MemRecorder* rec) {
aoqi@0 227 _next = rec;
aoqi@0 228 }
aoqi@0 229
aoqi@0 230 inline MemRecorder* next() const {
aoqi@0 231 return _next;
aoqi@0 232 }
aoqi@0 233
aoqi@0 234 // if the recorder is full
aoqi@0 235 inline bool is_full() const {
aoqi@0 236 assert(_pointer_records != NULL, "just check");
aoqi@0 237 return _pointer_records->is_full();
aoqi@0 238 }
aoqi@0 239
aoqi@0 240 // if running out of memory when initializing recorder's internal
aoqi@0 241 // data
aoqi@0 242 inline bool out_of_memory() const {
aoqi@0 243 return (_pointer_records == NULL ||
aoqi@0 244 _pointer_records->out_of_memory());
aoqi@0 245 }
aoqi@0 246
aoqi@0 247 inline void clear() {
aoqi@0 248 assert(_pointer_records != NULL, "Just check");
aoqi@0 249 _pointer_records->clear();
aoqi@0 250 }
aoqi@0 251
aoqi@0 252 SequencedRecordIterator pointer_itr();
aoqi@0 253
aoqi@0 254 // return the generation of this recorder which it belongs to
aoqi@0 255 unsigned long get_generation() const { return _generation; }
aoqi@0 256 protected:
aoqi@0 257 // number of MemRecorder instance
aoqi@0 258 static volatile jint _instance_count;
aoqi@0 259
aoqi@0 260 private:
aoqi@0 261 // sorting function, sort records into following order
aoqi@0 262 // 1. memory address
aoqi@0 263 // 2. allocation type
aoqi@0 264 // 3. sequence number
aoqi@0 265 static int sort_record_fn(const void* e1, const void* e2);
aoqi@0 266
aoqi@0 267 debug_only(void check_dup_seq(jint seq) const;)
aoqi@0 268 void set_generation();
aoqi@0 269 };
aoqi@0 270
aoqi@0 271 #endif // SHARE_VM_SERVICES_MEM_RECORDER_HPP

mercurial