25
#include "precompiled.hpp"
26
#include "gc/shared/oopStorage.hpp"
27
#include "gc/shared/weakProcessor.hpp"
28
#include "gc/shared/weakProcessorTimes.hpp"
29
#include "gc/shared/workerDataArray.inline.hpp"
30
#include "logging/log.hpp"
31
#include "logging/logStream.hpp"
32
#include "utilities/debug.hpp"
33
#include "utilities/enumIterator.hpp"
34
#include "utilities/globalDefinitions.hpp"
35
#include "utilities/ticks.hpp"
37
const double uninitialized_time = -1.0;
40
static bool is_initialized_time(double t) { return t >= 0.0; }
44
WeakProcessorTimes::WeakProcessorTimes(uint max_threads) :
45
_max_threads(max_threads),
47
_total_time_sec(uninitialized_time),
50
assert(_max_threads > 0, "max_threads must not be zero");
52
WorkerDataArray<double>** wpt = _worker_data;
53
for (auto id : EnumRange<OopStorageSet::WeakId>()) {
54
assert(size_t(wpt - _worker_data) < ARRAY_SIZE(_worker_data), "invariant");
55
const char* description = OopStorageSet::storage(id)->name();
56
*wpt = new WorkerDataArray<double>(nullptr, description, _max_threads);
57
(*wpt)->create_thread_work_items("Dead", DeadItems);
58
(*wpt)->create_thread_work_items("Total", TotalItems);
61
assert(size_t(wpt - _worker_data) == ARRAY_SIZE(_worker_data), "invariant");
64
WeakProcessorTimes::~WeakProcessorTimes() {
65
for (size_t i = 0; i < ARRAY_SIZE(_worker_data); ++i) {
66
delete _worker_data[i];
70
uint WeakProcessorTimes::max_threads() const { return _max_threads; }
72
uint WeakProcessorTimes::active_workers() const {
73
assert(_active_workers != 0, "active workers not set");
74
return _active_workers;
77
void WeakProcessorTimes::set_active_workers(uint n) {
78
assert(_active_workers == 0, "active workers already set");
79
assert(n > 0, "active workers must be non-zero");
80
assert(n <= _max_threads, "active workers must not exceed max threads");
84
void WeakProcessorTimes::reset() {
86
_total_time_sec = uninitialized_time;
87
for (size_t i = 0; i < ARRAY_SIZE(_worker_data); ++i) {
88
_worker_data[i]->reset();
92
double WeakProcessorTimes::total_time_sec() const {
93
assert(is_initialized_time(_total_time_sec), "Total time not set");
94
return _total_time_sec;
97
void WeakProcessorTimes::record_total_time_sec(double time_sec) {
98
assert(!is_initialized_time(_total_time_sec), "Already set total time");
99
_total_time_sec = time_sec;
102
WorkerDataArray<double>* WeakProcessorTimes::worker_data(OopStorageSet::WeakId id) const {
103
size_t index = EnumRange<OopStorageSet::WeakId>().index(id);
104
assert(index < ARRAY_SIZE(_worker_data), "invalid phase");
105
return _worker_data[index];
108
double WeakProcessorTimes::worker_time_sec(uint worker_id,
109
OopStorageSet::WeakId id) const {
110
assert(worker_id < active_workers(),
111
"invalid worker id %u for %u", worker_id, active_workers());
112
return worker_data(id)->get(worker_id);
115
void WeakProcessorTimes::record_worker_time_sec(uint worker_id,
116
OopStorageSet::WeakId id,
118
worker_data(id)->set(worker_id, time_sec);
121
void WeakProcessorTimes::record_worker_items(uint worker_id,
122
OopStorageSet::WeakId id,
125
WorkerDataArray<double>* data = worker_data(id);
126
data->set_or_add_thread_work_item(worker_id, num_dead, DeadItems);
127
data->set_or_add_thread_work_item(worker_id, num_total, TotalItems);
130
static double elapsed_time_sec(Ticks start_time, Ticks end_time) {
131
return (end_time - start_time).seconds();
134
WeakProcessorTimeTracker::WeakProcessorTimeTracker(WeakProcessorTimes* times) :
136
_start_time(Ticks::now())
139
WeakProcessorTimeTracker::~WeakProcessorTimeTracker() {
140
if (_times != nullptr) {
141
Ticks end_time = Ticks::now();
142
_times->record_total_time_sec(elapsed_time_sec(_start_time, end_time));
146
WeakProcessorParTimeTracker::WeakProcessorParTimeTracker(WeakProcessorTimes* times,
147
OopStorageSet::WeakId storage_id,
150
_storage_id(storage_id),
151
_worker_id(worker_id),
152
_start_time(Ticks::now())
154
assert(_times == nullptr || worker_id < _times->active_workers(),
155
"Invalid worker_id %u", worker_id);
159
WeakProcessorParTimeTracker::~WeakProcessorParTimeTracker() {
160
if (_times != nullptr) {
161
double time_sec = elapsed_time_sec(_start_time, Ticks::now());
162
_times->record_worker_time_sec(_worker_id, _storage_id, time_sec);
169
const char* const indents[] = {"", " ", " ", " ", " "};
170
const size_t max_indents_index = ARRAY_SIZE(indents) - 1;
172
static const char* indent_str(size_t i) {
173
return indents[MIN2(i, max_indents_index)];
176
#define TIME_FORMAT "%.2lfms"
178
void WeakProcessorTimes::log_summary(OopStorageSet::WeakId id, uint indent) const {
179
LogTarget(Debug, gc, phases) lt;
181
ls.print("%s", indents[indent]);
182
worker_data(id)->print_summary_on(&ls, true);
183
log_details(worker_data(id), indent + 1);
185
for (uint i = 0; i < worker_data(id)->MaxThreadWorkItems; i++) {
186
WorkerDataArray<size_t>* work_items = worker_data(id)->thread_work_items(i);
187
if (work_items != nullptr) {
188
ls.print("%s", indents[indent + 1]);
189
work_items->print_summary_on(&ls, true);
190
log_details(work_items, indent + 1);
196
void WeakProcessorTimes::log_details(WorkerDataArray<T>* data, uint indent) const {
197
LogTarget(Trace, gc, phases) lt;
198
if (lt.is_enabled()) {
200
ls.print("%s", indents[indent]);
201
data->print_details_on(&ls);
205
void WeakProcessorTimes::log_subtotals(uint indent) const {
206
if (log_is_enabled(Debug, gc, phases)) {
207
for (auto id : EnumRange<OopStorageSet::WeakId>()) {
208
log_summary(id, indent);
213
void WeakProcessorTimes::log_total(uint indent) const {
214
log_debug(gc, phases)("%s%s: " TIME_FORMAT,
217
total_time_sec() * MILLIUNITS);