1 // Copyright 2020 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/logging/metrics.h" 6 7 #include "include/v8-platform.h" 8 9 namespace v8 { 10 namespace internal { 11 namespace metrics { 12 13 class Recorder::Task : public v8::Task { 14 public: Task(const std::shared_ptr<Recorder> & recorder)15 explicit Task(const std::shared_ptr<Recorder>& recorder) 16 : recorder_(recorder) {} 17 Run()18 void Run() override { 19 std::queue<std::unique_ptr<Recorder::DelayedEventBase>> delayed_events; 20 { 21 base::MutexGuard lock_scope(&recorder_->lock_); 22 delayed_events.swap(recorder_->delayed_events_); 23 } 24 while (!delayed_events.empty()) { 25 delayed_events.front()->Run(recorder_); 26 delayed_events.pop(); 27 } 28 } 29 30 private: 31 std::shared_ptr<Recorder> recorder_; 32 }; 33 SetRecorder(Isolate * isolate,const std::shared_ptr<v8::metrics::Recorder> & embedder_recorder)34void Recorder::SetRecorder( 35 Isolate* isolate, 36 const std::shared_ptr<v8::metrics::Recorder>& embedder_recorder) { 37 foreground_task_runner_ = V8::GetCurrentPlatform()->GetForegroundTaskRunner( 38 reinterpret_cast<v8::Isolate*>(isolate)); 39 CHECK_NULL(embedder_recorder_); 40 embedder_recorder_ = embedder_recorder; 41 } 42 NotifyIsolateDisposal()43void Recorder::NotifyIsolateDisposal() { 44 if (embedder_recorder_) { 45 embedder_recorder_->NotifyIsolateDisposal(); 46 } 47 } 48 Delay(std::unique_ptr<Recorder::DelayedEventBase> && event)49void Recorder::Delay(std::unique_ptr<Recorder::DelayedEventBase>&& event) { 50 base::MutexGuard lock_scope(&lock_); 51 bool was_empty = delayed_events_.empty(); 52 delayed_events_.push(std::move(event)); 53 if (was_empty) { 54 foreground_task_runner_->PostDelayedTask( 55 std::make_unique<Task>(shared_from_this()), 1.0); 56 } 57 } 58 59 } // namespace metrics 60 } // namespace internal 61 } // namespace v8 62