summaryrefslogtreecommitdiff
path: root/deps/v8/src/libplatform/tracing/perfetto-consumer.h
blob: 83d0c48c1bd2ab0c62bd51275def97faa87749e7 (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// 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.

#ifndef V8_LIBPLATFORM_TRACING_PERFETTO_CONSUMER_H_
#define V8_LIBPLATFORM_TRACING_PERFETTO_CONSUMER_H_

#include <memory>

#include "perfetto/tracing/core/consumer.h"
#include "perfetto/tracing/core/tracing_service.h"
#include "src/base/logging.h"

namespace perfetto {
namespace protos {
class ChromeTracePacket;
}  // namespace protos
}  // namespace perfetto

namespace v8 {

namespace base {
class Semaphore;
}

namespace platform {
namespace tracing {

class TraceEventListener;

// A Perfetto Consumer gets streamed trace events from the Service via
// OnTraceData(). A Consumer can be configured (via
// service_endpoint()->EnableTracing()) to listen to various different types of
// trace events. The Consumer is responsible for producing whatever tracing
// output the system should have.

// Implements the V8-specific logic for interacting with the tracing controller
// and directs trace events to the added TraceEventListeners.
class PerfettoConsumer final : public ::perfetto::Consumer {
 public:
  explicit PerfettoConsumer(base::Semaphore* finished);

  using ServiceEndpoint = ::perfetto::TracingService::ConsumerEndpoint;

  // Register a trace event listener that will receive trace events from this
  // consumer. This can be called multiple times to register multiple listeners,
  // but must be called before starting tracing.
  void AddTraceEventListener(TraceEventListener* listener);

  ServiceEndpoint* service_endpoint() const { return service_endpoint_.get(); }
  void set_service_endpoint(std::unique_ptr<ServiceEndpoint> endpoint) {
    service_endpoint_ = std::move(endpoint);
  }

 private:
  // ::perfetto::Consumer implementation
  void OnConnect() override {}
  void OnDisconnect() override {}
  void OnTracingDisabled() override {}
  void OnTraceData(std::vector<::perfetto::TracePacket> packets,
                   bool has_more) override;
  void OnDetach(bool success) override {}
  void OnAttach(bool success, const ::perfetto::TraceConfig&) override {}
  void OnTraceStats(bool success, const ::perfetto::TraceStats&) override {
    UNREACHABLE();
  }
  void OnObservableEvents(const ::perfetto::ObservableEvents&) override {
    UNREACHABLE();
  }

  std::unique_ptr<ServiceEndpoint> service_endpoint_;
  base::Semaphore* finished_semaphore_;
  std::vector<TraceEventListener*> listeners_;
};

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

#endif  // V8_LIBPLATFORM_TRACING_PERFETTO_CONSUMER_H_