1 /*
   2  * Copyright (c) 2012, 2015, Oracle and/or its affiliates. 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 #ifndef SHARE_VM_GC_SHARED_GCTRACETIME_INLINE_HPP
  26 #define SHARE_VM_GC_SHARED_GCTRACETIME_INLINE_HPP
  27 
  28 #include "gc/shared/collectedHeap.hpp"
  29 #include "gc/shared/gcTimer.hpp"
  30 #include "gc/shared/gcTrace.hpp"
  31 #include "gc/shared/gcTraceTime.hpp"
  32 #include "logging/log.hpp"
  33 #include "logging/logStream.inline.hpp"
  34 #include "memory/universe.hpp"
  35 #include "prims/jni_md.h"
  36 #include "utilities/ticks.hpp"
  37 
  38 #define LOG_STOP_HEAP_FORMAT SIZE_FORMAT "M->" SIZE_FORMAT "M("  SIZE_FORMAT "M)"
  39 
  40 inline void GCTraceTimeImpl::log_start(jlong start_counter) {
  41   if (_out_start.is_enabled()) {
  42     LogStream out(_out_start);
  43 
  44     out.print("%s", _title);
  45     if (_gc_cause != GCCause::_no_gc) {
  46       out.print(" (%s)", GCCause::to_string(_gc_cause));
  47     }
  48     out.cr();
  49   }
  50 }
  51 
  52 inline void GCTraceTimeImpl::log_stop(jlong start_counter, jlong stop_counter) {
  53   double duration_in_ms = TimeHelper::counter_to_millis(stop_counter - start_counter);
  54   double start_time_in_secs = TimeHelper::counter_to_seconds(start_counter);
  55   double stop_time_in_secs = TimeHelper::counter_to_seconds(stop_counter);
  56 
  57   LogStream out(_out_stop);
  58 
  59   out.print("%s", _title);
  60 
  61   if (_gc_cause != GCCause::_no_gc) {
  62     out.print(" (%s)", GCCause::to_string(_gc_cause));
  63   }
  64 
  65   if (_heap_usage_before != SIZE_MAX) {
  66     CollectedHeap* heap = GC::gc()->heap();
  67     size_t used_before_m = _heap_usage_before / M;
  68     size_t used_m = heap->used() / M;
  69     size_t capacity_m = heap->capacity() / M;
  70     out.print(" " LOG_STOP_HEAP_FORMAT, used_before_m, used_m, capacity_m);
  71   }
  72 
  73   out.print_cr(" %.3fms", duration_in_ms);
  74 }
  75 
  76 inline void GCTraceTimeImpl::time_stamp(Ticks& ticks) {
  77   if (_enabled || _timer != NULL) {
  78     ticks.stamp();
  79   }
  80 }
  81 
  82 inline GCTraceTimeImpl::GCTraceTimeImpl(LogTargetHandle out_start, LogTargetHandle out_stop, const char* title, GCTimer* timer, GCCause::Cause gc_cause, bool log_heap_usage) :
  83   _enabled(out_stop.is_enabled()),
  84   _out_start(out_start),
  85   _out_stop(out_stop),
  86   _start_ticks(),
  87   _heap_usage_before(SIZE_MAX),
  88   _title(title),
  89   _gc_cause(gc_cause),
  90   _timer(timer) {
  91 
  92   time_stamp(_start_ticks);
  93   if (_enabled) {
  94     if (log_heap_usage) {
  95       _heap_usage_before = GC::gc()->heap()->used();
  96     }
  97     log_start(_start_ticks.value());
  98   }
  99   if (_timer != NULL) {
 100     _timer->register_gc_phase_start(_title, _start_ticks);
 101   }
 102 }
 103 
 104 inline GCTraceTimeImpl::~GCTraceTimeImpl() {
 105   Ticks stop_ticks;
 106   time_stamp(stop_ticks);
 107   if (_enabled) {
 108     log_stop(_start_ticks.value(), stop_ticks.value());
 109   }
 110   if (_timer != NULL) {
 111     _timer->register_gc_phase_end(stop_ticks);
 112   }
 113 }
 114 
 115 template <LogLevelType Level, LogTagType T0, LogTagType T1, LogTagType T2, LogTagType T3, LogTagType T4, LogTagType GuardTag >
 116 GCTraceConcTimeImpl<Level, T0, T1, T2, T3, T4, GuardTag>::GCTraceConcTimeImpl(const char* title) :
 117   _enabled(LogImpl<T0, T1, T2, T3, T4, GuardTag>::is_level(Level)), _start_time(os::elapsed_counter()), _title(title) {
 118   if (_enabled) {
 119     LogImpl<T0, T1, T2, T3, T4>::template write<Level>("%s", _title);
 120   }
 121 }
 122 
 123 template <LogLevelType Level, LogTagType T0, LogTagType T1, LogTagType T2, LogTagType T3, LogTagType T4, LogTagType GuardTag >
 124 GCTraceConcTimeImpl<Level, T0, T1, T2, T3, T4, GuardTag>::~GCTraceConcTimeImpl() {
 125   if (_enabled) {
 126     jlong stop_time = os::elapsed_counter();
 127     LogImpl<T0, T1, T2, T3, T4>::template write<Level>("%s %0.3fms", _title,
 128                                                        TimeHelper::counter_to_millis(stop_time - _start_time));
 129   }
 130 }
 131 
 132 // Figure out the first __NO_TAG position and replace it with 'start'.
 133 #define INJECT_START_TAG(T1, T2, T3, T4) \
 134     ((                          T1 == LogTag::__NO_TAG) ? PREFIX_LOG_TAG(start) : T1), \
 135     ((T1 != LogTag::__NO_TAG && T2 == LogTag::__NO_TAG) ? PREFIX_LOG_TAG(start) : T2), \
 136     ((T2 != LogTag::__NO_TAG && T3 == LogTag::__NO_TAG) ? PREFIX_LOG_TAG(start) : T3), \
 137     ((T3 != LogTag::__NO_TAG && T4 == LogTag::__NO_TAG) ? PREFIX_LOG_TAG(start) : T4)
 138 
 139 template <LogLevelType level, LogTagType T0, LogTagType T1, LogTagType T2, LogTagType T3, LogTagType T4, LogTagType GuardTag>
 140 GCTraceTimeImplWrapper<level, T0, T1, T2, T3, T4, GuardTag>::GCTraceTimeImplWrapper(
 141     const char* title, GCTimer* timer, GCCause::Cause gc_cause, bool log_heap_usage)
 142     : _impl(
 143         LogTargetHandle::create<level, T0, INJECT_START_TAG(T1, T2, T3, T4), GuardTag>(),
 144         LogTargetHandle::create<level, T0, T1, T2, T3, T4, GuardTag>(),
 145         title,
 146         timer,
 147         gc_cause,
 148         log_heap_usage) {
 149 
 150   STATIC_ASSERT(T0 != LogTag::__NO_TAG); // Need some tag to log on.
 151   STATIC_ASSERT(T4 == LogTag::__NO_TAG); // Need to leave at least the last tag for the "start" tag in log_start()
 152 }
 153 
 154 #undef INJECT_START_TAG
 155 
 156 template <LogLevelType Level, LogTagType T0, LogTagType T1, LogTagType T2, LogTagType T3, LogTagType T4, LogTagType GuardTag>
 157 GCTraceTimeImplWrapper<Level, T0, T1, T2, T3, T4, GuardTag>::~GCTraceTimeImplWrapper() {}
 158 
 159 #define GCTraceTime(Level, ...) GCTraceTimeImplWrapper<LogLevel::Level, LOG_TAGS(__VA_ARGS__)>
 160 #define GCTraceConcTime(Level, ...) GCTraceConcTimeImpl<LogLevel::Level, LOG_TAGS(__VA_ARGS__)>
 161 
 162 #endif // SHARE_VM_GC_SHARED_GCTRACETIME_INLINE_HPP