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