OLD | NEW |
(Empty) | |
| 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 #include "src/compiler-dispatcher/compiler-dispatcher-tracer.h" |
| 6 |
| 7 #include "src/isolate.h" |
| 8 |
| 9 namespace v8 { |
| 10 namespace internal { |
| 11 |
| 12 namespace { |
| 13 |
| 14 double MonotonicallyIncreasingTimeInMs() { |
| 15 return V8::GetCurrentPlatform()->MonotonicallyIncreasingTime() * |
| 16 static_cast<double>(base::Time::kMillisecondsPerSecond); |
| 17 } |
| 18 |
| 19 } // namespace |
| 20 |
| 21 CompilerDispatcherTracer::Scope::Scope(CompilerDispatcherTracer* tracer, |
| 22 ScopeID scope_id, size_t num) |
| 23 : tracer_(tracer), scope_id_(scope_id), num_(num) { |
| 24 start_time_ = MonotonicallyIncreasingTimeInMs(); |
| 25 // TODO(cbruni): remove once we fully moved to a trace-based system. |
| 26 if (TRACE_EVENT_RUNTIME_CALL_STATS_TRACING_ENABLED() || |
| 27 FLAG_runtime_call_stats) { |
| 28 RuntimeCallStats::Enter(tracer_->runtime_call_stats_, &timer_, |
| 29 &RuntimeCallStats::CompilerDispatcher); |
| 30 } |
| 31 } |
| 32 |
| 33 CompilerDispatcherTracer::Scope::~Scope() { |
| 34 double elapsed = MonotonicallyIncreasingTimeInMs() - start_time_; |
| 35 switch (scope_id_) { |
| 36 case ScopeID::kPrepareToParse: |
| 37 tracer_->RecordPrepareToParse(elapsed); |
| 38 break; |
| 39 case ScopeID::kParse: |
| 40 tracer_->RecordParse(elapsed, num_); |
| 41 break; |
| 42 case ScopeID::kFinalizeParsing: |
| 43 tracer_->RecordFinalizeParsing(elapsed); |
| 44 break; |
| 45 case ScopeID::kPrepareToCompile: |
| 46 tracer_->RecordPrepareToCompile(elapsed); |
| 47 break; |
| 48 case ScopeID::kCompile: |
| 49 tracer_->RecordCompile(elapsed, num_); |
| 50 break; |
| 51 case ScopeID::kFinalizeCompiling: |
| 52 tracer_->RecordFinalizeCompiling(elapsed); |
| 53 break; |
| 54 } |
| 55 // TODO(cbruni): remove once we fully moved to a trace-based system. |
| 56 if (TRACE_EVENT_RUNTIME_CALL_STATS_TRACING_ENABLED() || |
| 57 FLAG_runtime_call_stats) { |
| 58 RuntimeCallStats::Leave(tracer_->runtime_call_stats_, &timer_); |
| 59 } |
| 60 } |
| 61 |
| 62 // static |
| 63 const char* CompilerDispatcherTracer::Scope::Name(ScopeID scope_id) { |
| 64 switch (scope_id) { |
| 65 case ScopeID::kPrepareToParse: |
| 66 return "V8.BackgroundCompile_PrepareToParse"; |
| 67 case ScopeID::kParse: |
| 68 return "V8.BackgroundCompile_Parse"; |
| 69 case ScopeID::kFinalizeParsing: |
| 70 return "V8.BackgroundCompile_FinalizeParsing"; |
| 71 case ScopeID::kPrepareToCompile: |
| 72 return "V8.BackgroundCompile_PrepareToCompile"; |
| 73 case ScopeID::kCompile: |
| 74 return "V8.BackgroundCompile_Compile"; |
| 75 case ScopeID::kFinalizeCompiling: |
| 76 return "V8.BackgroundCompile_FinalizeCompiling"; |
| 77 } |
| 78 UNREACHABLE(); |
| 79 return nullptr; |
| 80 } |
| 81 |
| 82 CompilerDispatcherTracer::CompilerDispatcherTracer(Isolate* isolate) |
| 83 : runtime_call_stats_(nullptr) { |
| 84 // isolate might be nullptr during unittests. |
| 85 if (isolate) { |
| 86 runtime_call_stats_ = isolate->counters()->runtime_call_stats(); |
| 87 } |
| 88 } |
| 89 |
| 90 CompilerDispatcherTracer::~CompilerDispatcherTracer() {} |
| 91 |
| 92 void CompilerDispatcherTracer::RecordPrepareToParse(double duration_ms) { |
| 93 base::LockGuard<base::Mutex> lock(&mutex_); |
| 94 prepare_parse_events_.Push(duration_ms); |
| 95 } |
| 96 |
| 97 void CompilerDispatcherTracer::RecordParse(double duration_ms, |
| 98 size_t source_length) { |
| 99 base::LockGuard<base::Mutex> lock(&mutex_); |
| 100 parse_events_.Push(std::make_pair(source_length, duration_ms)); |
| 101 } |
| 102 |
| 103 void CompilerDispatcherTracer::RecordFinalizeParsing(double duration_ms) { |
| 104 base::LockGuard<base::Mutex> lock(&mutex_); |
| 105 finalize_parsing_events_.Push(duration_ms); |
| 106 } |
| 107 |
| 108 void CompilerDispatcherTracer::RecordPrepareToCompile(double duration_ms) { |
| 109 base::LockGuard<base::Mutex> lock(&mutex_); |
| 110 prepare_compile_events_.Push(duration_ms); |
| 111 } |
| 112 |
| 113 void CompilerDispatcherTracer::RecordCompile(double duration_ms, |
| 114 size_t ast_size_in_bytes) { |
| 115 base::LockGuard<base::Mutex> lock(&mutex_); |
| 116 compile_events_.Push(std::make_pair(ast_size_in_bytes, duration_ms)); |
| 117 } |
| 118 |
| 119 void CompilerDispatcherTracer::RecordFinalizeCompiling(double duration_ms) { |
| 120 base::LockGuard<base::Mutex> lock(&mutex_); |
| 121 finalize_compiling_events_.Push(duration_ms); |
| 122 } |
| 123 |
| 124 double CompilerDispatcherTracer::EstimatePrepareToParse() const { |
| 125 base::LockGuard<base::Mutex> lock(&mutex_); |
| 126 return Average(prepare_parse_events_); |
| 127 } |
| 128 |
| 129 double CompilerDispatcherTracer::EstimateParse(size_t source_length) const { |
| 130 base::LockGuard<base::Mutex> lock(&mutex_); |
| 131 return Estimate(parse_events_, source_length); |
| 132 } |
| 133 |
| 134 double CompilerDispatcherTracer::EstimateFinalizeParsing() { |
| 135 base::LockGuard<base::Mutex> lock(&mutex_); |
| 136 return Average(finalize_parsing_events_); |
| 137 } |
| 138 |
| 139 double CompilerDispatcherTracer::EstimatePrepareToCompile() { |
| 140 base::LockGuard<base::Mutex> lock(&mutex_); |
| 141 return Average(prepare_compile_events_); |
| 142 } |
| 143 |
| 144 double CompilerDispatcherTracer::EstimateCompile(size_t ast_size_in_bytes) { |
| 145 base::LockGuard<base::Mutex> lock(&mutex_); |
| 146 return Estimate(compile_events_, ast_size_in_bytes); |
| 147 } |
| 148 |
| 149 double CompilerDispatcherTracer::EstimateFinalizeCompiling() { |
| 150 base::LockGuard<base::Mutex> lock(&mutex_); |
| 151 return Average(finalize_compiling_events_); |
| 152 } |
| 153 |
| 154 double CompilerDispatcherTracer::Average( |
| 155 const base::RingBuffer<double>& buffer) { |
| 156 if (buffer.Count() == 0) return 0.0; |
| 157 double sum = buffer.Sum([](double a, double b) { return a + b; }, 0.0); |
| 158 return sum / buffer.Count(); |
| 159 } |
| 160 |
| 161 double CompilerDispatcherTracer::Estimate( |
| 162 const base::RingBuffer<std::pair<size_t, double>>& buffer, size_t num) { |
| 163 if (buffer.Count() == 0) return 0.0; |
| 164 std::pair<size_t, double> sum = buffer.Sum( |
| 165 [](std::pair<size_t, double> a, std::pair<size_t, double> b) { |
| 166 return std::make_pair(a.first + b.first, a.second + b.second); |
| 167 }, |
| 168 std::make_pair(0, 0.0)); |
| 169 return num * (sum.second / sum.first); |
| 170 } |
| 171 |
| 172 } // namespace internal |
| 173 } // namespace v8 |
OLD | NEW |