1 /* 2 * Copyright (c) 2014, 2018, 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 #include "precompiled.hpp" 26 #include "jfr/leakprofiler/utilities/granularTimer.hpp" 27 #include "trace/traceBackend.hpp" 28 29 long GranularTimer::_granularity = 0; 30 long GranularTimer::_counter = 0; 31 JfrTraceTime GranularTimer::_finish_time_ticks = 0; 32 JfrTraceTime GranularTimer::_start_time_ticks = 0; 33 bool GranularTimer::_finished = false; 34 35 void GranularTimer::start(jlong duration_ticks, long granularity) { 36 assert(granularity > 0, "granularity must be at least 1"); 37 _granularity = granularity; 38 _counter = granularity; 39 _start_time_ticks = JfrTraceTime::now(); 40 const jlong end_time_ticks = _start_time_ticks.value() + duration_ticks; 41 _finish_time_ticks = end_time_ticks < 0 ? JfrTraceTime(max_jlong) : JfrTraceTime(end_time_ticks); 42 _finished = _finish_time_ticks == _start_time_ticks; 43 assert(_finish_time_ticks >= 0, "invariant"); 44 assert(_finish_time_ticks >= _start_time_ticks, "invariant"); 45 } 46 void GranularTimer::stop() { 47 if (!_finished) { 48 _finish_time_ticks = JfrTraceTime::now(); 49 } 50 } 51 const JfrTraceTime& GranularTimer::start_time() { 52 return _start_time_ticks; 53 } 54 55 const JfrTraceTime& GranularTimer::end_time() { 56 return _finish_time_ticks; 57 } 58 59 bool GranularTimer::is_finished() { 60 assert(_granularity != 0, "GranularTimer::is_finished must be called after GranularTimer::start"); 61 if (--_counter == 0) { 62 if (_finished) { 63 // reset so we decrease to zero at next iteration 64 _counter = 1; 65 return true; 66 } 67 if (JfrTraceTime::now() > _finish_time_ticks) { 68 _finished = true; 69 _counter = 1; 70 return true; 71 } 72 assert(_counter == 0, "invariant"); 73 _counter = _granularity; // restore next batch 74 } 75 return false; 76 }