1 /*
   2  * Copyright (c) 2017, Red Hat, Inc. and/or its affiliates.
   3  *
   4  * This code is free software; you can redistribute it and/or modify it
   5  * under the terms of the GNU General Public License version 2 only, as
   6  * published by the Free Software Foundation.
   7  *
   8  * This code is distributed in the hope that it will be useful, but WITHOUT
   9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  11  * version 2 for more details (a copy is included in the LICENSE file that
  12  * accompanied this code).
  13  *
  14  * You should have received a copy of the GNU General Public License version
  15  * 2 along with this work; if not, write to the Free Software Foundation,
  16  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  17  *
  18  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  19  * or visit www.oracle.com if you need additional information or have any
  20  * questions.
  21  *
  22  */
  23 
  24 #include "precompiled.hpp"
  25 
  26 #include "gc_implementation/shenandoah/shenandoahAllocTracker.hpp"
  27 #include "gc_implementation/shenandoah/shenandoahCollectorPolicy.hpp"
  28 #include "gc_implementation/shenandoah/shenandoahMarkCompact.hpp"
  29 #include "gc_implementation/shenandoah/shenandoahHeap.hpp"
  30 #include "gc_implementation/shenandoah/shenandoahHeuristics.hpp"
  31 #include "gc_implementation/shenandoah/shenandoahUtils.hpp"
  32 #include "gc_implementation/shenandoah/shenandoahLogging.hpp"
  33 #include "gc_interface/gcCause.hpp"
  34 #include "gc_implementation/shared/gcTimer.hpp"
  35 #include "gc_implementation/shared/gcWhen.hpp"
  36 #include "gc_implementation/shared/gcTrace.hpp"
  37 
  38 
  39 ShenandoahGCSession::ShenandoahGCSession(GCCause::Cause cause) :
  40   _timer(ShenandoahHeap::heap()->gc_timer()),
  41   _tracer(ShenandoahHeap::heap()->tracer()) {
  42   ShenandoahHeap* sh = ShenandoahHeap::heap();
  43 
  44   _timer->register_gc_start();
  45   _tracer->report_gc_start(cause, _timer->gc_start());
  46   sh->trace_heap(GCWhen::BeforeGC, _tracer);
  47 
  48   sh->shenandoahPolicy()->record_cycle_start();
  49   sh->heuristics()->record_cycle_start();
  50   _trace_cycle.initialize(false, sh->gc_cause(),
  51           /* recordGCBeginTime = */       true,
  52           /* recordPreGCUsage = */        true,
  53           /* recordPeakUsage = */         true,
  54           /* recordPostGCUsage = */       true,
  55           /* recordAccumulatedGCTime = */ true,
  56           /* recordGCEndTime = */         true,
  57           /* countCollection = */         true
  58   );
  59 }
  60 
  61 ShenandoahGCSession::~ShenandoahGCSession() {
  62   ShenandoahHeap::heap()->heuristics()->record_cycle_end();
  63   _tracer->report_gc_end(_timer->gc_end(), _timer->time_partitions());
  64   _timer->register_gc_end();
  65 }
  66 
  67 ShenandoahGCPauseMark::ShenandoahGCPauseMark(SvcGCMarker::reason_type type) :
  68         _svc_gc_mark(type), _is_gc_active_mark() {
  69   ShenandoahHeap* sh = ShenandoahHeap::heap();
  70 
  71   // FIXME: It seems that JMC throws away level 0 events, which are the Shenandoah
  72   // pause events. Create this pseudo level 0 event to push real events to level 1.
  73   sh->gc_timer()->register_gc_phase_start("Shenandoah", Ticks::now());
  74   _trace_pause.initialize(true, sh->gc_cause(),
  75           /* recordGCBeginTime = */       true,
  76           /* recordPreGCUsage = */        false,
  77           /* recordPeakUsage = */         false,
  78           /* recordPostGCUsage = */       false,
  79           /* recordAccumulatedGCTime = */ true,
  80           /* recordGCEndTime = */         true,
  81           /* countCollection = */         true
  82   );
  83 
  84   sh->heuristics()->record_gc_start();
  85 }
  86 
  87 ShenandoahGCPauseMark::~ShenandoahGCPauseMark() {
  88   ShenandoahHeap* sh = ShenandoahHeap::heap();
  89   sh->gc_timer()->register_gc_phase_end(Ticks::now());
  90   sh->heuristics()->record_gc_end();
  91 }
  92 
  93 ShenandoahGCPhase::ShenandoahGCPhase(const ShenandoahPhaseTimings::Phase phase) :
  94   _phase(phase) {
  95   ShenandoahHeap::heap()->phase_timings()->record_phase_start(_phase);
  96 }
  97 
  98 ShenandoahGCPhase::~ShenandoahGCPhase() {
  99   ShenandoahHeap::heap()->phase_timings()->record_phase_end(_phase);
 100 }
 101 
 102 ShenandoahAllocTrace::ShenandoahAllocTrace(size_t words_size, ShenandoahHeap::AllocType alloc_type) {
 103   if (ShenandoahAllocationTrace) {
 104     _start = os::elapsedTime();
 105     _size = words_size;
 106     _alloc_type = alloc_type;
 107   } else {
 108     _start = 0;
 109     _size = 0;
 110     _alloc_type = ShenandoahHeap::AllocType(0);
 111   }
 112 }
 113 
 114 ShenandoahAllocTrace::~ShenandoahAllocTrace() {
 115   if (ShenandoahAllocationTrace) {
 116     double stop = os::elapsedTime();
 117     double duration_sec = stop - _start;
 118     double duration_us = duration_sec * 1000000;
 119     ShenandoahAllocTracker* tracker = ShenandoahHeap::heap()->alloc_tracker();
 120     assert(tracker != NULL, "Must be");
 121     tracker->record_alloc_latency(_size, _alloc_type, duration_us);
 122     if (duration_us > ShenandoahAllocationStallThreshold) {
 123       log_warning(gc)("Allocation stall: %.0f us (threshold: " INTX_FORMAT " us)",
 124                       duration_us, ShenandoahAllocationStallThreshold);
 125     }
 126   }
 127 }
 128 
 129 ShenandoahWorkerSession::ShenandoahWorkerSession(uint worker_id) {
 130   Thread* thr = Thread::current();
 131   assert(thr->worker_id() == INVALID_WORKER_ID, "Already set");
 132   thr->set_worker_id(worker_id);
 133 }
 134 
 135 ShenandoahWorkerSession::~ShenandoahWorkerSession() {
 136 #ifdef ASSERT
 137   Thread* thr = Thread::current();
 138   assert(thr->worker_id() != INVALID_WORKER_ID, "Must be set");
 139   thr->set_worker_id(INVALID_WORKER_ID);
 140 #endif
 141 }
 142