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_HPP 26 #define SHARE_VM_GC_SHARED_GCTRACETIME_HPP 27 28 #include "gc/shared/collectedHeap.hpp" 29 #include "gc/shared/gcTimer.hpp" 30 #include "gc/shared/gcTrace.hpp" 31 #include "logging/log.hpp" 32 #include "memory/allocation.hpp" 33 #include "memory/resourceArea.hpp" 34 #include "memory/universe.hpp" 35 #include "prims/jni_md.h" 36 #include "utilities/ticks.hpp" 37 #include "runtime/timer.hpp" 38 #include "utilities/ticks.hpp" 39 40 class GCTraceCPUTime : public StackObj { 41 bool _active; // true if times will be measured and printed 42 double _starting_user_time; // user time at start of measurement 43 double _starting_system_time; // system time at start of measurement 44 double _starting_real_time; // real time at start of measurement 45 public: 46 GCTraceCPUTime(); 47 ~GCTraceCPUTime(); 48 }; 49 50 template <LogLevelType Level, LogTagType T0, LogTagType T1 = LogTag::__NO_TAG, LogTagType T2 = LogTag::__NO_TAG, LogTagType T3 = LogTag::__NO_TAG, 51 LogTagType T4 = LogTag::__NO_TAG, LogTagType GuardTag = LogTag::__NO_TAG> 52 class GCTraceTimeImpl : public StackObj { 53 private: 54 bool _enabled; 55 Ticks _start_ticks; 56 const char* _title; 57 GCCause::Cause _gc_cause; 58 GCTimer* _timer; 59 size_t _heap_usage_before; 60 61 void log_start(jlong start_counter) { 62 if (Log<LogTag::start, T0, T1, T2, T3>::is_level(Level)) { 63 FormatBuffer<> start_msg("%s", _title); 64 if (_gc_cause != GCCause::_no_gc) { 65 start_msg.append(" (%s)", GCCause::to_string(_gc_cause)); 66 } 67 start_msg.append(" (%.3fs)", TimeHelper::counter_to_seconds(start_counter)); 68 // Make sure to put the "start" tag last in the tag set 69 STATIC_ASSERT(T0 != LogTag::__NO_TAG); // Need some tag to log on. 70 STATIC_ASSERT(T4 == LogTag::__NO_TAG); // Need to leave at least the last tag for the "start" tag in log_start() 71 if (T1 == LogTag::__NO_TAG) { 72 Log<T0, LogTag::start>::template write<Level>("%s", start_msg.buffer()); 73 } else if (T2 == LogTag::__NO_TAG) { 74 Log<T0, T1, LogTag::start>::template write<Level>("%s", start_msg.buffer()); 75 } else if (T3 == LogTag::__NO_TAG) { 76 Log<T0, T1, T2, LogTag::start>::template write<Level>("%s", start_msg.buffer()); 77 } else { 78 Log<T0, T1, T2, T3, LogTag::start>::template write<Level>("%s", start_msg.buffer()); 79 } 80 } 81 } 82 83 #define LOG_STOP_TIME_FORMAT "(%.3fs, %.3fs) %.3fms" 84 #define LOG_STOP_HEAP_FORMAT SIZE_FORMAT "M->" SIZE_FORMAT "M(" SIZE_FORMAT "M)" 85 86 void log_stop(jlong start_counter, jlong stop_counter) { 87 double duration_in_ms = TimeHelper::counter_to_millis(stop_counter - start_counter); 88 double start_time_in_secs = TimeHelper::counter_to_seconds(start_counter); 89 double stop_time_in_secs = TimeHelper::counter_to_seconds(stop_counter); 90 FormatBuffer<> stop_msg("%s", _title); 91 if (_gc_cause != GCCause::_no_gc) { 92 stop_msg.append(" (%s)", GCCause::to_string(_gc_cause)); 93 } 94 if (_heap_usage_before == SIZE_MAX) { 95 Log<T0, T1, T2, T3, T4>::template write<Level>("%s " LOG_STOP_TIME_FORMAT, 96 stop_msg.buffer(), start_time_in_secs, stop_time_in_secs, duration_in_ms); 97 } else { 98 CollectedHeap* heap = Universe::heap(); 99 size_t used_before_m = _heap_usage_before / M; 100 size_t used_m = heap->used() / M; 101 size_t capacity_m = heap->capacity() / M; 102 Log<T0, T1, T2, T3, T4>::template write<Level>("%s " LOG_STOP_HEAP_FORMAT " " LOG_STOP_TIME_FORMAT, 103 stop_msg.buffer(), used_before_m, used_m, capacity_m, start_time_in_secs, stop_time_in_secs, duration_in_ms); 104 } 105 } 106 107 public: 108 GCTraceTimeImpl(const char* title, GCTimer* timer = NULL, GCCause::Cause gc_cause = GCCause::_no_gc, bool log_heap_usage = false) 109 : _enabled(Log<T0, T1, T2, T3, T4, GuardTag>::is_level(Level)), _start_ticks(), _title(title), _gc_cause(gc_cause), _timer(timer) { 110 if (_enabled) { 111 _start_ticks.stamp(); 112 if (log_heap_usage) { 113 _heap_usage_before = Universe::heap()->used(); 114 } else { 115 _heap_usage_before = SIZE_MAX; 116 } 117 log_start(_start_ticks.value()); 118 } 119 if (_timer != NULL) { 120 if (!_enabled) { 121 _start_ticks.stamp(); 122 } 123 _timer->register_gc_phase_start(_title, _start_ticks); 124 } 125 } 126 127 ~GCTraceTimeImpl() { 128 Ticks stop_ticks; 129 if (_enabled) { 130 stop_ticks.stamp(); 131 log_stop(_start_ticks.value(), stop_ticks.value()); 132 } 133 if (_timer != NULL) { 134 if (!_enabled) { 135 stop_ticks.stamp(); 136 } 137 _timer->register_gc_phase_end(stop_ticks); 138 } 139 } 140 }; 141 142 #define GCTraceTime(Level, ...) GCTraceTimeImpl<LogLevel::Level, LOG_TAGS(__VA_ARGS__)> 143 144 #endif // SHARE_VM_GC_SHARED_GCTRACETIME_HPP