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