1 /*
   2  * Copyright (c) 2013, 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 "runtime/os.hpp"
  27 #include "utilities/ticks.inline.hpp"
  28 
  29 #ifdef ASSERT
  30  const jlong Ticks::invalid_time_stamp = -2; // 0xFFFF FFFF`FFFF FFFE
  31 #endif
  32 
  33 void Ticks::stamp() {
  34   _stamp_ticks = os::elapsed_counter();
  35 }
  36 
  37 const Ticks Ticks::now() {
  38   Ticks t;
  39   t.stamp();
  40   return t;
  41 }
  42 
  43 Tickspan::Tickspan(const Ticks& end, const Ticks& start) {
  44   assert(end.value() != Ticks::invalid_time_stamp, "end is unstamped!");
  45   assert(start.value() != Ticks::invalid_time_stamp, "start is unstamped!");
  46 
  47   assert(end >= start, "negative time!");
  48 
  49   _span_ticks = end.value() - start.value();
  50 }
  51 
  52 template <typename ReturnType>
  53 static ReturnType time_conversion(const Tickspan& span, TicksToTimeHelper::Unit unit) {
  54   assert(TicksToTimeHelper::SECONDS == unit ||
  55          TicksToTimeHelper::MILLISECONDS == unit, "invalid unit!");
  56 
  57   ReturnType frequency_per_unit = (ReturnType)os::elapsed_frequency() / (ReturnType)unit;
  58 
  59   return (ReturnType) ((ReturnType)span.value() / frequency_per_unit);
  60 }
  61 
  62 double TicksToTimeHelper::seconds(const Tickspan& span) {
  63   return time_conversion<double>(span, SECONDS);
  64 }
  65 
  66 jlong TicksToTimeHelper::milliseconds(const Tickspan& span) {
  67   return time_conversion<jlong>(span, MILLISECONDS);
  68 }