1 /*
  2  * Copyright (c) 2017, 2021, Red Hat, Inc. 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 "jfr/jfrEvents.hpp"
 28 #include "gc/shared/gcCause.hpp"
 29 #include "gc/shared/gcTrace.hpp"
 30 #include "gc/shared/gcWhen.hpp"
 31 #include "gc/shared/referenceProcessorStats.hpp"
 32 #include "gc/shenandoah/heuristics/shenandoahHeuristics.hpp"
 33 #include "gc/shenandoah/shenandoahCollectorPolicy.hpp"
 34 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
 35 #include "gc/shenandoah/shenandoahReferenceProcessor.hpp"
 36 #include "gc/shenandoah/shenandoahUtils.hpp"
 37 #include "utilities/debug.hpp"
 38 
 39 ShenandoahPhaseTimings::Phase ShenandoahTimingsTracker::_current_phase = ShenandoahPhaseTimings::_invalid_phase;
 40 
 41 ShenandoahGCSession::ShenandoahGCSession(GCCause::Cause cause) :
 42   _heap(ShenandoahHeap::heap()),
 43   _timer(_heap->gc_timer()),
 44   _tracer(_heap->tracer()) {
 45   assert(!ShenandoahGCPhase::is_current_phase_valid(), "No current GC phase");
 46 
 47   _heap->set_gc_cause(cause);
 48   _timer->register_gc_start();
 49   _tracer->report_gc_start(cause, _timer->gc_start());
 50   _heap->trace_heap_before_gc(_tracer);
 51 
 52   _heap->shenandoah_policy()->record_cycle_start();
 53   _heap->heuristics()->record_cycle_start();
 54   _trace_cycle.initialize(_heap->cycle_memory_manager(), cause,
 55           /* allMemoryPoolsAffected */    true,
 56           /* recordGCBeginTime = */       true,
 57           /* recordPreGCUsage = */        true,
 58           /* recordPeakUsage = */         true,
 59           /* recordPostGCUsage = */       true,
 60           /* recordAccumulatedGCTime = */ true,
 61           /* recordGCEndTime = */         true,
 62           /* countCollection = */         true
 63   );
 64 }
 65 
 66 ShenandoahGCSession::~ShenandoahGCSession() {
 67   _heap->heuristics()->record_cycle_end();
 68   _timer->register_gc_end();
 69   _heap->trace_heap_after_gc(_tracer);
 70   _tracer->report_gc_reference_stats(_heap->ref_processor()->reference_process_stats());
 71   _tracer->report_gc_end(_timer->gc_end(), _timer->time_partitions());
 72   assert(!ShenandoahGCPhase::is_current_phase_valid(), "No current GC phase");
 73   _heap->set_gc_cause(GCCause::_no_gc);
 74 }
 75 
 76 ShenandoahGCPauseMark::ShenandoahGCPauseMark(uint gc_id, SvcGCMarker::reason_type type) :
 77   _heap(ShenandoahHeap::heap()), _gc_id_mark(gc_id), _svc_gc_mark(type), _is_gc_active_mark() {
 78   _trace_pause.initialize(_heap->stw_memory_manager(), _heap->gc_cause(),
 79           /* allMemoryPoolsAffected */    true,
 80           /* recordGCBeginTime = */       true,
 81           /* recordPreGCUsage = */        false,
 82           /* recordPeakUsage = */         false,
 83           /* recordPostGCUsage = */       false,
 84           /* recordAccumulatedGCTime = */ true,
 85           /* recordGCEndTime = */         true,
 86           /* countCollection = */         true
 87   );
 88 }
 89 
 90 ShenandoahPausePhase::ShenandoahPausePhase(const char* title, ShenandoahPhaseTimings::Phase phase, bool log_heap_usage) :
 91   ShenandoahTimingsTracker(phase),
 92   _tracer(title, NULL, GCCause::_no_gc, log_heap_usage),
 93   _timer(ShenandoahHeap::heap()->gc_timer()) {
 94   _timer->register_gc_pause_start(title);
 95 }
 96 
 97 ShenandoahPausePhase::~ShenandoahPausePhase() {
 98   _timer->register_gc_pause_end();
 99 }
100 
101 ShenandoahConcurrentPhase::ShenandoahConcurrentPhase(const char* title, ShenandoahPhaseTimings::Phase phase, bool log_heap_usage) :
102   ShenandoahTimingsTracker(phase),
103   _tracer(title, NULL, GCCause::_no_gc, log_heap_usage),
104   _timer(ShenandoahHeap::heap()->gc_timer()) {
105   _timer->register_gc_concurrent_start(title);
106 }
107 
108 ShenandoahConcurrentPhase::~ShenandoahConcurrentPhase() {
109   _timer->register_gc_concurrent_end();
110 }
111 
112 ShenandoahTimingsTracker::ShenandoahTimingsTracker(ShenandoahPhaseTimings::Phase phase) :
113   _timings(ShenandoahHeap::heap()->phase_timings()), _phase(phase) {
114   assert(!Thread::current()->is_Worker_thread() &&
115               (Thread::current()->is_VM_thread() ||
116                Thread::current()->is_ConcurrentGC_thread()),
117           "Must be set by these threads");
118   _parent_phase = _current_phase;
119   _current_phase = phase;
120   _start = os::elapsedTime();
121 }
122 
123 ShenandoahTimingsTracker::~ShenandoahTimingsTracker() {
124   _timings->record_phase_time(_phase, os::elapsedTime() - _start);
125   _current_phase = _parent_phase;
126 }
127 
128 bool ShenandoahTimingsTracker::is_current_phase_valid() {
129   return _current_phase < ShenandoahPhaseTimings::_num_phases;
130 }
131 
132 ShenandoahGCPhase::ShenandoahGCPhase(ShenandoahPhaseTimings::Phase phase) :
133   ShenandoahTimingsTracker(phase),
134   _timer(ShenandoahHeap::heap()->gc_timer()) {
135   _timer->register_gc_phase_start(ShenandoahPhaseTimings::phase_name(phase), Ticks::now());
136 }
137 
138 ShenandoahGCPhase::~ShenandoahGCPhase() {
139   _timer->register_gc_phase_end(Ticks::now());
140 }
141 
142 ShenandoahGCWorkerPhase::ShenandoahGCWorkerPhase(const ShenandoahPhaseTimings::Phase phase) :
143     _timings(ShenandoahHeap::heap()->phase_timings()), _phase(phase) {
144   _timings->record_workers_start(_phase);
145 }
146 
147 ShenandoahGCWorkerPhase::~ShenandoahGCWorkerPhase() {
148   _timings->record_workers_end(_phase);
149 }
150 
151 ShenandoahWorkerSession::ShenandoahWorkerSession(uint worker_id) : _worker_id(worker_id) {
152   Thread* thr = Thread::current();
153   assert(ShenandoahThreadLocalData::worker_id(thr) == ShenandoahThreadLocalData::INVALID_WORKER_ID, "Already set");
154   ShenandoahThreadLocalData::set_worker_id(thr, worker_id);
155 }
156 
157 ShenandoahConcurrentWorkerSession::~ShenandoahConcurrentWorkerSession() {
158   _event.commit(GCId::current(), ShenandoahPhaseTimings::phase_name(ShenandoahGCPhase::current_phase()));
159 }
160 
161 ShenandoahParallelWorkerSession::~ShenandoahParallelWorkerSession() {
162   _event.commit(GCId::current(), _worker_id, ShenandoahPhaseTimings::phase_name(ShenandoahGCPhase::current_phase()));
163 }
164 ShenandoahWorkerSession::~ShenandoahWorkerSession() {
165 #ifdef ASSERT
166   Thread* thr = Thread::current();
167   assert(ShenandoahThreadLocalData::worker_id(thr) != ShenandoahThreadLocalData::INVALID_WORKER_ID, "Must be set");
168   ShenandoahThreadLocalData::set_worker_id(thr, ShenandoahThreadLocalData::INVALID_WORKER_ID);
169 #endif
170 }