Path: blob/master/src/hotspot/share/jfr/leakprofiler/utilities/granularTimer.cpp
41155 views
/*1* Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 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 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "jfr/leakprofiler/utilities/granularTimer.hpp"2627long GranularTimer::_granularity = 0;28long GranularTimer::_counter = 0;29JfrTicks GranularTimer::_finish_time_ticks = 0;30JfrTicks GranularTimer::_start_time_ticks = 0;31bool GranularTimer::_finished = false;3233void GranularTimer::start(jlong duration_ticks, long granularity) {34assert(granularity > 0, "granularity must be at least 1");35_granularity = granularity;36_counter = granularity;37_start_time_ticks = JfrTicks::now();38const jlong end_time_ticks = _start_time_ticks.value() + duration_ticks;39_finish_time_ticks = end_time_ticks < 0 ? JfrTicks(max_jlong) : JfrTicks(end_time_ticks);40_finished = _finish_time_ticks == _start_time_ticks;41assert(_finish_time_ticks.value() >= 0, "invariant");42assert(_finish_time_ticks >= _start_time_ticks, "invariant");43}44void GranularTimer::stop() {45if (!_finished) {46_finish_time_ticks = JfrTicks::now();47}48}49const JfrTicks& GranularTimer::start_time() {50return _start_time_ticks;51}5253const JfrTicks& GranularTimer::end_time() {54return _finish_time_ticks;55}5657bool GranularTimer::is_finished() {58assert(_granularity != 0, "GranularTimer::is_finished must be called after GranularTimer::start");59if (--_counter == 0) {60if (_finished) {61// reset so we decrease to zero at next iteration62_counter = 1;63return true;64}65if (JfrTicks::now() > _finish_time_ticks) {66_finished = true;67_counter = 1;68return true;69}70assert(_counter == 0, "invariant");71_counter = _granularity; // restore next batch72}73return false;74}757677