summaryrefslogtreecommitdiff
path: root/deps/v8/src/libplatform/tracing/perfetto-tasks.cc
blob: 70d00ed626945abf592d766e70331fd1da66314e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Copyright 2019 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "src/libplatform/tracing/perfetto-tasks.h"

#include "src/base/platform/semaphore.h"
#include "src/base/platform/time.h"

namespace v8 {
namespace platform {
namespace tracing {

PerfettoTaskRunner::PerfettoTaskRunner() : runner_(1, DefaultTimeFunction) {}

PerfettoTaskRunner::~PerfettoTaskRunner() { runner_.Terminate(); }

// static
double PerfettoTaskRunner::DefaultTimeFunction() {
  return (base::TimeTicks::HighResolutionNow() - base::TimeTicks())
      .InSecondsF();
}

void PerfettoTaskRunner::PostTask(std::function<void()> f) {
  runner_.PostTask(base::make_unique<TracingTask>(std::move(f)));
}

void PerfettoTaskRunner::PostDelayedTask(std::function<void()> f,
                                         uint32_t delay_ms) {
  double delay_in_seconds =
      delay_ms / static_cast<double>(base::Time::kMillisecondsPerSecond);
  runner_.PostDelayedTask(base::make_unique<TracingTask>(std::move(f)),
                          delay_in_seconds);
}

bool PerfettoTaskRunner::RunsTasksOnCurrentThread() const {
  return runner_.RunsTasksOnCurrentThread();
}

void PerfettoTaskRunner::FinishImmediateTasks() {
  DCHECK(!RunsTasksOnCurrentThread());
  base::Semaphore semaphore(0);
  // PostTask has guaranteed ordering so this will be the last task executed.
  runner_.PostTask(
      base::make_unique<TracingTask>([&semaphore] { semaphore.Signal(); }));

  semaphore.Wait();
}

}  // namespace tracing
}  // namespace platform
}  // namespace v8