• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef V8_LOGGING_COUNTERS_INL_H_
6 #define V8_LOGGING_COUNTERS_INL_H_
7 
8 #include "src/logging/counters.h"
9 #include "src/logging/tracing-flags.h"
10 
11 namespace v8 {
12 namespace internal {
13 
Start(RuntimeCallCounter * counter,RuntimeCallTimer * parent)14 void RuntimeCallTimer::Start(RuntimeCallCounter* counter,
15                              RuntimeCallTimer* parent) {
16   DCHECK(!IsStarted());
17   counter_ = counter;
18   parent_.SetValue(parent);
19   if (TracingFlags::runtime_stats.load(std::memory_order_relaxed) ==
20       v8::tracing::TracingCategoryObserver::ENABLED_BY_SAMPLING) {
21     return;
22   }
23   base::TimeTicks now = RuntimeCallTimer::Now();
24   if (parent) parent->Pause(now);
25   Resume(now);
26   DCHECK(IsStarted());
27 }
28 
Pause(base::TimeTicks now)29 void RuntimeCallTimer::Pause(base::TimeTicks now) {
30   DCHECK(IsStarted());
31   elapsed_ += (now - start_ticks_);
32   start_ticks_ = base::TimeTicks();
33 }
34 
Resume(base::TimeTicks now)35 void RuntimeCallTimer::Resume(base::TimeTicks now) {
36   DCHECK(!IsStarted());
37   start_ticks_ = now;
38 }
39 
Stop()40 RuntimeCallTimer* RuntimeCallTimer::Stop() {
41   if (!IsStarted()) return parent();
42   base::TimeTicks now = RuntimeCallTimer::Now();
43   Pause(now);
44   counter_->Increment();
45   CommitTimeToCounter();
46 
47   RuntimeCallTimer* parent_timer = parent();
48   if (parent_timer) {
49     parent_timer->Resume(now);
50   }
51   return parent_timer;
52 }
53 
CommitTimeToCounter()54 void RuntimeCallTimer::CommitTimeToCounter() {
55   counter_->Add(elapsed_);
56   elapsed_ = base::TimeDelta();
57 }
58 
IsStarted()59 bool RuntimeCallTimer::IsStarted() { return start_ticks_ != base::TimeTicks(); }
60 
61 }  // namespace internal
62 }  // namespace v8
63 
64 #endif  // V8_LOGGING_COUNTERS_INL_H_
65