6197
|
1 /*
|
|
2 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
|
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 *
|
|
5 * This code is free software; you can redistribute it and/or modify it
|
|
6 * under the terms of the GNU General Public License version 2 only, as
|
|
7 * published by the Free Software Foundation.
|
|
8 *
|
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 * version 2 for more details (a copy is included in the LICENSE file that
|
|
13 * accompanied this code).
|
|
14 *
|
|
15 * You should have received a copy of the GNU General Public License version
|
|
16 * 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 *
|
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
20 * or visit www.oracle.com if you need additional information or have any
|
|
21 * questions.
|
|
22 *
|
|
23 */
|
|
24
|
|
25 #include "precompiled.hpp"
|
|
26
|
|
27 #include "runtime/atomic.hpp"
|
|
28 #include "services/memBaseline.hpp"
|
|
29 #include "services/memRecorder.hpp"
|
|
30 #include "services/memPtr.hpp"
|
|
31 #include "services/memTracker.hpp"
|
|
32
|
|
33 MemPointer* SequencedRecordIterator::next_record() {
|
|
34 MemPointer* itr_cur = _itr.current();
|
|
35 if (itr_cur == NULL) return NULL;
|
|
36 MemPointer* itr_next = _itr.next();
|
|
37
|
|
38 while (itr_next != NULL &&
|
|
39 same_kind((MemPointerRecord*)itr_cur, (MemPointerRecord*)itr_next)) {
|
|
40 itr_cur = itr_next;
|
|
41 itr_next = _itr.next();
|
|
42 }
|
|
43
|
|
44 return itr_cur;
|
|
45 }
|
|
46
|
|
47
|
6231
|
48 volatile jint MemRecorder::_instance_count = 0;
|
6197
|
49
|
|
50 MemRecorder::MemRecorder() {
|
|
51 assert(MemTracker::is_on(), "Native memory tracking is off");
|
6231
|
52 Atomic::inc(&_instance_count);
|
6197
|
53 debug_only(set_generation();)
|
|
54
|
|
55 if (MemTracker::track_callsite()) {
|
|
56 _pointer_records = new (std::nothrow)FixedSizeMemPointerArray<SeqMemPointerRecordEx,
|
|
57 DEFAULT_RECORDER_PTR_ARRAY_SIZE>();
|
|
58 } else {
|
|
59 _pointer_records = new (std::nothrow)FixedSizeMemPointerArray<SeqMemPointerRecord,
|
|
60 DEFAULT_RECORDER_PTR_ARRAY_SIZE>();
|
|
61 }
|
|
62 _next = NULL;
|
|
63
|
|
64
|
|
65 if (_pointer_records != NULL) {
|
|
66 // recode itself
|
|
67 record((address)this, (MemPointerRecord::malloc_tag()|mtNMT|otNMTRecorder),
|
|
68 sizeof(MemRecorder), CALLER_PC);
|
|
69 record((address)_pointer_records, (MemPointerRecord::malloc_tag()|mtNMT|otNMTRecorder),
|
|
70 _pointer_records->instance_size(),CURRENT_PC);
|
|
71 }
|
|
72 }
|
|
73
|
|
74 MemRecorder::~MemRecorder() {
|
|
75 if (_pointer_records != NULL) {
|
|
76 if (MemTracker::is_on()) {
|
|
77 MemTracker::record_free((address)_pointer_records, mtNMT);
|
|
78 MemTracker::record_free((address)this, mtNMT);
|
|
79 }
|
|
80 delete _pointer_records;
|
|
81 }
|
|
82 if (_next != NULL) {
|
|
83 delete _next;
|
|
84 }
|
|
85
|
|
86 Atomic::dec(&_instance_count);
|
|
87 }
|
|
88
|
|
89 // Sorting order:
|
|
90 // 1. memory block address
|
|
91 // 2. mem pointer record tags
|
|
92 // 3. sequence number
|
|
93 int MemRecorder::sort_record_fn(const void* e1, const void* e2) {
|
|
94 const MemPointerRecord* p1 = (const MemPointerRecord*)e1;
|
|
95 const MemPointerRecord* p2 = (const MemPointerRecord*)e2;
|
|
96 int delta = UNSIGNED_COMPARE(p1->addr(), p2->addr());
|
|
97 if (delta == 0) {
|
|
98 int df = UNSIGNED_COMPARE((p1->flags() & MemPointerRecord::tag_masks),
|
|
99 (p2->flags() & MemPointerRecord::tag_masks));
|
|
100 if (df == 0) {
|
|
101 assert(p1->seq() != p2->seq(), "dup seq");
|
|
102 return p1->seq() - p2->seq();
|
|
103 } else {
|
|
104 return df;
|
|
105 }
|
|
106 } else {
|
|
107 return delta;
|
|
108 }
|
|
109 }
|
|
110
|
|
111 bool MemRecorder::record(address p, MEMFLAGS flags, size_t size, address pc) {
|
|
112 #ifdef ASSERT
|
|
113 if (MemPointerRecord::is_virtual_memory_record(flags)) {
|
|
114 assert((flags & MemPointerRecord::tag_masks) != 0, "bad virtual memory record");
|
|
115 } else {
|
|
116 assert((flags & MemPointerRecord::tag_masks) == MemPointerRecord::malloc_tag() ||
|
|
117 (flags & MemPointerRecord::tag_masks) == MemPointerRecord::free_tag() ||
|
|
118 IS_ARENA_OBJ(flags),
|
|
119 "bad malloc record");
|
|
120 }
|
|
121 // a recorder should only hold records within the same generation
|
|
122 unsigned long cur_generation = SequenceGenerator::current_generation();
|
|
123 assert(cur_generation == _generation,
|
|
124 "this thread did not enter sync point");
|
|
125 #endif
|
|
126
|
|
127 if (MemTracker::track_callsite()) {
|
|
128 SeqMemPointerRecordEx ap(p, flags, size, pc);
|
|
129 debug_only(check_dup_seq(ap.seq());)
|
|
130 return _pointer_records->append(&ap);
|
|
131 } else {
|
|
132 SeqMemPointerRecord ap(p, flags, size);
|
|
133 debug_only(check_dup_seq(ap.seq());)
|
|
134 return _pointer_records->append(&ap);
|
|
135 }
|
|
136 }
|
|
137
|
|
138 // iterator for alloc pointers
|
|
139 SequencedRecordIterator MemRecorder::pointer_itr() {
|
|
140 assert(_pointer_records != NULL, "just check");
|
|
141 _pointer_records->sort((FN_SORT)sort_record_fn);
|
|
142 return SequencedRecordIterator(_pointer_records);
|
|
143 }
|
|
144
|
|
145
|
|
146 #ifdef ASSERT
|
|
147 void MemRecorder::set_generation() {
|
|
148 _generation = SequenceGenerator::current_generation();
|
|
149 }
|
|
150
|
|
151 void MemRecorder::check_dup_seq(jint seq) const {
|
|
152 MemPointerArrayIteratorImpl itr(_pointer_records);
|
|
153 MemPointerRecord* rc = (MemPointerRecord*)itr.current();
|
|
154 while (rc != NULL) {
|
|
155 assert(rc->seq() != seq, "dup seq");
|
|
156 rc = (MemPointerRecord*)itr.next();
|
|
157 }
|
|
158 }
|
|
159
|
|
160 #endif
|