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