< prev index next >

src/hotspot/share/runtime/timer.cpp

Print this page
@@ -24,10 +24,11 @@
  
  #include "precompiled.hpp"
  #include "logging/log.hpp"
  #include "oops/oop.inline.hpp"
  #include "runtime/os.hpp"
+ #include "runtime/thread.hpp"
  #include "runtime/timer.hpp"
  #include "utilities/ostream.hpp"
  
  double TimeHelper::counter_to_seconds(jlong counter) {
    double freq  = (double) os::elapsed_frequency();

@@ -46,49 +47,78 @@
  jlong TimeHelper::micros_to_counter(jlong micros) {
    jlong freq = os::elapsed_frequency() / MICROUNITS;
    return micros * freq;
  }
  
- void elapsedTimer::add(elapsedTimer t) {
-   _counter += t._counter;
+ void BaseTimer::add(BaseTimer* t) {
+   _counter += t->_counter;
  }
  
- void elapsedTimer::add_nanoseconds(jlong ns) {
+ void BaseTimer::add_nanoseconds(jlong ns) {
    jlong freq = os::elapsed_frequency() / NANOUNITS;
    _counter += ns * freq;
  }
  
- void elapsedTimer::start() {
+ void BaseTimer::start() {
    if (!_active) {
      _active = true;
-     _start_counter = os::elapsed_counter();
+     _start_counter = read_counter();
    }
  }
  
- void elapsedTimer::stop() {
+ void BaseTimer::stop() {
    if (_active) {
-     _counter += os::elapsed_counter() - _start_counter;
+     _counter += read_counter() - _start_counter;
      _active = false;
    }
  }
  
- double elapsedTimer::seconds() const {
+ double BaseTimer::seconds() const {
   return TimeHelper::counter_to_seconds(_counter);
  }
  
- jlong elapsedTimer::milliseconds() const {
+ jlong BaseTimer::milliseconds() const {
    return (jlong)TimeHelper::counter_to_millis(_counter);
  }
  
- jlong elapsedTimer::active_ticks() const {
+ jlong BaseTimer::active_ticks() const {
    if (!_active) {
      return ticks();
    }
-   jlong counter = _counter + os::elapsed_counter() - _start_counter;
+   jlong counter = _counter + read_counter() - _start_counter;
    return counter;
  }
  
+ jlong elapsedTimer::read_counter() const {
+   return os::elapsed_counter();
+ }
+ 
+ ThreadTimer::ThreadTimer() {
+   _owner = Thread::current();
+ }
+ 
+ void ThreadTimer::start() {
+   assert(_owner != nullptr, "timer must be bound to a thread");
+   Thread* current = Thread::current();
+   assert(current == _owner, "timer can only be started by the thread that owns it");
+   BaseTimer::start();
+ }
+ 
+ void ThreadTimer::stop() {
+   assert(_owner != nullptr, "sanity check");
+   Thread* current = Thread::current();
+   assert(current == _owner, "timer can only be stopped by the thread that owns it");
+   if (_start_counter != -1) {
+     BaseTimer::stop();
+   }
+ }
+ 
+ jlong ThreadTimer::read_counter() const {
+   assert(_owner != nullptr, "sanity check");
+   return os::thread_cpu_time(_owner);
+ }
+ 
  void TimeStamp::update_to(jlong ticks) {
    _counter = ticks;
    if (_counter == 0)  _counter = 1;
    assert(is_updated(), "must not look clear");
  }
< prev index next >