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 SetEmbedderRecorder(Isolate * isolate,const std::shared_ptr<v8::metrics::Recorder> & embedder_recorder)34void Recorder::SetEmbedderRecorder( 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 HasEmbedderRecorder() const43bool Recorder::HasEmbedderRecorder() const { return embedder_recorder_.get(); } 44 NotifyIsolateDisposal()45void Recorder::NotifyIsolateDisposal() { 46 if (embedder_recorder_) { 47 embedder_recorder_->NotifyIsolateDisposal(); 48 } 49 } 50 Delay(std::unique_ptr<Recorder::DelayedEventBase> && event)51void Recorder::Delay(std::unique_ptr<Recorder::DelayedEventBase>&& event) { 52 base::MutexGuard lock_scope(&lock_); 53 bool was_empty = delayed_events_.empty(); 54 delayed_events_.push(std::move(event)); 55 if (was_empty) { 56 foreground_task_runner_->PostDelayedTask( 57 std::make_unique<Task>(shared_from_this()), 1.0); 58 } 59 } 60 61 } // namespace metrics 62 } // namespace internal 63 } // namespace v8 64