-
Notifications
You must be signed in to change notification settings - Fork 17
/
heapObjectStatistics.cpp
168 lines (146 loc) · 5.46 KB
/
heapObjectStatistics.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
* Copyright (c) 2021, Red Hat, Inc. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/
#include "precompiled.hpp"
#include "gc/shared/collectedHeap.hpp"
#include "logging/logStream.hpp"
#include "logging/logTag.hpp"
#include "memory/allocation.hpp"
#include "memory/iterator.hpp"
#include "memory/resourceArea.hpp"
#include "memory/universe.hpp"
#include "oops/oop.inline.hpp"
#include "runtime/vmThread.hpp"
#include "services/heapObjectStatistics.hpp"
#include "utilities/copy.hpp"
#include "utilities/globalDefinitions.hpp"
#include "utilities/ostream.hpp"
HeapObjectStatistics* HeapObjectStatistics::_instance = NULL;
class HeapObjectStatsObjectClosure : public ObjectClosure {
private:
HeapObjectStatistics* const _stats;
public:
HeapObjectStatsObjectClosure() : _stats(HeapObjectStatistics::instance()) {}
void do_object(oop obj) {
_stats->visit_object(obj);
}
};
class VM_HeapObjectStatistics : public VM_Operation {
public:
VMOp_Type type() const { return VMOp_HeapObjectStatistics; }
bool doit_prologue() {
Heap_lock->lock();
return true;
}
void doit_epilogue() {
Heap_lock->unlock();
}
void doit() {
assert(SafepointSynchronize::is_at_safepoint(), "all threads are stopped");
assert(Heap_lock->is_locked(), "should have the Heap_lock");
CollectedHeap* heap = Universe::heap();
heap->ensure_parsability(false);
HeapObjectStatistics* stats = HeapObjectStatistics::instance();
stats->begin_sample();
HeapObjectStatsObjectClosure cl;
heap->object_iterate(&cl);
}
};
HeapObjectStatisticsTask::HeapObjectStatisticsTask() : PeriodicTask(HeapObjectStatsSamplingInterval) {}
void HeapObjectStatisticsTask::task() {
VM_HeapObjectStatistics vmop;
VMThread::execute(&vmop);
}
void HeapObjectStatistics::initialize() {
assert(_instance == NULL, "Don't init twice");
if (HeapObjectStats) {
_instance = new HeapObjectStatistics();
_instance->start();
}
}
void HeapObjectStatistics::shutdown() {
if (HeapObjectStats) {
assert(_instance != NULL, "Must be initialized");
LogTarget(Info, heap, stats) lt;
if (lt.is_enabled()) {
LogStream ls(lt);
ResourceMark rm;
_instance->print(&ls);
}
_instance->stop();
delete _instance;
_instance = NULL;
}
}
HeapObjectStatistics* HeapObjectStatistics::instance() {
assert(_instance != NULL, "Must be initialized");
return _instance;
}
void HeapObjectStatistics::increase_counter(uint64_t& counter, uint64_t val) {
uint64_t oldval = counter;
uint64_t newval = counter + val;
if (newval < oldval) {
log_warning(heap, stats)("HeapObjectStats counter overflow: resulting statistics will be useless");
}
counter = newval;
}
HeapObjectStatistics::HeapObjectStatistics() :
_task(), _num_samples(0), _num_objects(0), _num_ihashed(0), _num_locked(0), _lds(0) { }
void HeapObjectStatistics::start() {
_task.enroll();
}
void HeapObjectStatistics::stop() {
_task.disenroll();
}
void HeapObjectStatistics::begin_sample() {
_num_samples++;
}
void HeapObjectStatistics::visit_object(oop obj) {
increase_counter(_num_objects);
if (!obj->mark().has_no_hash()) {
increase_counter(_num_ihashed);
if (obj->mark().age() > 0) {
increase_counter(_num_ihashed_moved);
}
}
if (obj->mark().is_locked()) {
increase_counter(_num_locked);
}
increase_counter(_lds, obj->size());
}
void HeapObjectStatistics::print(outputStream* out) const {
if (!HeapObjectStats) {
return;
}
if (_num_samples == 0 || _num_objects == 0) {
return;
}
out->print_cr("Number of samples: " UINT64_FORMAT, _num_samples);
out->print_cr("Average number of objects: " UINT64_FORMAT, _num_objects / _num_samples);
out->print_cr("Average object size: " UINT64_FORMAT " bytes, %.1f words", (_lds * HeapWordSize) / _num_objects, (float) _lds / _num_objects);
out->print_cr("Average number of hashed objects: " UINT64_FORMAT " (%.2f%%)", _num_ihashed / _num_samples, (float) (_num_ihashed * 100.0) / _num_objects);
out->print_cr("Average number of moved hashed objects: " UINT64_FORMAT " (%.2f%%)", _num_ihashed_moved / _num_samples, (float) (_num_ihashed_moved * 100.0) / _num_objects);
out->print_cr("Average number of locked objects: " UINT64_FORMAT " (%.2f%%)", _num_locked / _num_samples, (float) (_num_locked * 100) / _num_objects);
out->print_cr("Average LDS: " UINT64_FORMAT " bytes", _lds * HeapWordSize / _num_samples);
out->print_cr("Avg LDS with (assumed) 64bit header: " UINT64_FORMAT " bytes (%.1f%%)", (_lds - _num_objects) * HeapWordSize / _num_samples, ((float) _lds - _num_objects) * 100.0 / _lds);
}