perfetto-consumer.h 2.57 KB
Newer Older
1 2 3 4
// 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.

5 6
#ifndef V8_LIBPLATFORM_TRACING_PERFETTO_CONSUMER_H_
#define V8_LIBPLATFORM_TRACING_PERFETTO_CONSUMER_H_
7 8 9 10 11 12 13

#include <memory>

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

14 15 16 17 18 19
namespace perfetto {
namespace protos {
class ChromeTracePacket;
}  // namespace protos
}  // namespace perfetto

20
namespace v8 {
21 22 23 24 25

namespace base {
class Semaphore;
}

26 27 28
namespace platform {
namespace tracing {

29 30
class TraceEventListener;

31 32 33 34
// 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
35
// output the system should have.
36 37 38 39

// 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 {
40
 public:
41 42
  explicit PerfettoConsumer(base::Semaphore* finished);

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

45 46 47 48 49
  // 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);

50 51 52 53 54 55 56 57 58 59 60
  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,
61
                   bool has_more) override;
62 63 64 65 66 67 68 69 70 71
  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_;
72
  base::Semaphore* finished_semaphore_;
73
  std::vector<TraceEventListener*> listeners_;
74 75 76 77 78 79
};

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

80
#endif  // V8_LIBPLATFORM_TRACING_PERFETTO_CONSUMER_H_