consumer_host.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. // Copyright 2019 The Chromium 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. #ifndef SERVICES_TRACING_PERFETTO_CONSUMER_HOST_H_
  5. #define SERVICES_TRACING_PERFETTO_CONSUMER_HOST_H_
  6. #include <memory>
  7. #include <set>
  8. #include <string>
  9. #include <vector>
  10. #include "base/callback.h"
  11. #include "base/memory/raw_ptr.h"
  12. #include "base/memory/weak_ptr.h"
  13. #include "base/process/process_handle.h"
  14. #include "base/sequence_checker.h"
  15. #include "base/threading/sequence_bound.h"
  16. #include "base/timer/timer.h"
  17. #include "mojo/public/cpp/bindings/remote.h"
  18. #include "services/tracing/public/mojom/perfetto_service.mojom.h"
  19. #include "third_party/perfetto/include/perfetto/ext/tracing/core/consumer.h"
  20. #include "third_party/perfetto/include/perfetto/ext/tracing/core/tracing_service.h"
  21. namespace perfetto {
  22. namespace trace_processor {
  23. class TraceProcessorStorage;
  24. } // namespace trace_processor
  25. } // namespace perfetto
  26. namespace tracing {
  27. class PerfettoService;
  28. // This is a Mojo interface which enables any client
  29. // to act as a Perfetto consumer.
  30. class ConsumerHost : public perfetto::Consumer, public mojom::ConsumerHost {
  31. public:
  32. static void BindConsumerReceiver(
  33. PerfettoService* service,
  34. mojo::PendingReceiver<mojom::ConsumerHost> receiver);
  35. class StreamWriter;
  36. class TracingSession : public mojom::TracingSessionHost {
  37. public:
  38. TracingSession(
  39. ConsumerHost* host,
  40. mojo::PendingReceiver<mojom::TracingSessionHost> tracing_session_host,
  41. mojo::PendingRemote<mojom::TracingSessionClient> tracing_session_client,
  42. const perfetto::TraceConfig& trace_config,
  43. perfetto::base::ScopedFile output_file,
  44. mojom::TracingClientPriority priority);
  45. TracingSession(const TracingSession&) = delete;
  46. TracingSession& operator=(const TracingSession&) = delete;
  47. ~TracingSession() override;
  48. void OnPerfettoEvents(const perfetto::ObservableEvents&);
  49. void OnTraceData(std::vector<perfetto::TracePacket> packets, bool has_more);
  50. void OnTraceStats(bool success, const perfetto::TraceStats&);
  51. void OnTracingDisabled(const std::string& error);
  52. void OnConsumerClientDisconnected();
  53. void Flush(uint32_t timeout, base::OnceCallback<void(bool)> callback);
  54. mojom::TracingClientPriority tracing_priority() const {
  55. return tracing_priority_;
  56. }
  57. bool tracing_enabled() const { return tracing_enabled_; }
  58. ConsumerHost* host() const { return host_; }
  59. // Called by TracingService.
  60. void OnActiveServicePidAdded(base::ProcessId pid);
  61. void OnActiveServicePidRemoved(base::ProcessId pid);
  62. void OnActiveServicePidsInitialized();
  63. void RequestDisableTracing(base::OnceClosure on_disabled_callback);
  64. // mojom::TracingSessionHost implementation.
  65. void ChangeTraceConfig(const perfetto::TraceConfig& config) override;
  66. void DisableTracing() override;
  67. void ReadBuffers(mojo::ScopedDataPipeProducerHandle stream,
  68. ReadBuffersCallback callback) override;
  69. void RequestBufferUsage(RequestBufferUsageCallback callback) override;
  70. void DisableTracingAndEmitJson(
  71. const std::string& agent_label_filter,
  72. mojo::ScopedDataPipeProducerHandle stream,
  73. bool privacy_filtering_enabled,
  74. DisableTracingAndEmitJsonCallback callback) override;
  75. private:
  76. void ExportJson();
  77. void OnJSONTraceData(std::string json, bool has_more);
  78. void OnEnableTracingTimeout();
  79. void MaybeSendEnableTracingAck();
  80. bool IsExpectedPid(base::ProcessId pid) const;
  81. const raw_ptr<ConsumerHost> host_;
  82. mojo::Remote<mojom::TracingSessionClient> tracing_session_client_;
  83. mojo::Receiver<mojom::TracingSessionHost> receiver_;
  84. bool privacy_filtering_enabled_ = false;
  85. bool convert_to_legacy_json_ = false;
  86. base::SequenceBound<StreamWriter> read_buffers_stream_writer_;
  87. RequestBufferUsageCallback request_buffer_usage_callback_;
  88. std::unique_ptr<perfetto::trace_processor::TraceProcessorStorage>
  89. trace_processor_;
  90. std::string json_agent_label_filter_;
  91. base::OnceCallback<void(bool)> flush_callback_;
  92. const mojom::TracingClientPriority tracing_priority_;
  93. base::OnceClosure on_disabled_callback_;
  94. std::set<base::ProcessId> filtered_pids_;
  95. bool tracing_enabled_ = true;
  96. // If set, we didn't issue OnTracingEnabled() on the session yet. If set and
  97. // empty, no more pids are pending and we should issue OnTracingEnabled().
  98. absl::optional<std::set<base::ProcessId>> pending_enable_tracing_ack_pids_;
  99. base::OneShotTimer enable_tracing_ack_timer_;
  100. struct DataSourceHandle : public std::pair<std::string, std::string> {
  101. using std::pair<std::string, std::string>::pair;
  102. const std::string& producer_name() const { return first; }
  103. const std::string& data_source_name() const { return second; }
  104. };
  105. std::map<DataSourceHandle, bool /*started*/> data_source_states_;
  106. SEQUENCE_CHECKER(sequence_checker_);
  107. base::WeakPtrFactory<TracingSession> weak_factory_{this};
  108. };
  109. // The owner of ConsumerHost should make sure to destroy
  110. // |service| after destroying this.
  111. explicit ConsumerHost(PerfettoService* service);
  112. ConsumerHost(const ConsumerHost&) = delete;
  113. ConsumerHost& operator=(const ConsumerHost&) = delete;
  114. ~ConsumerHost() override;
  115. PerfettoService* service() const { return service_; }
  116. perfetto::TracingService::ConsumerEndpoint* consumer_endpoint() const {
  117. return consumer_endpoint_.get();
  118. }
  119. // mojom::ConsumerHost implementation.
  120. void EnableTracing(
  121. mojo::PendingReceiver<mojom::TracingSessionHost> tracing_session_host,
  122. mojo::PendingRemote<mojom::TracingSessionClient> tracing_session_client,
  123. const perfetto::TraceConfig& config,
  124. base::File output_file) override;
  125. // perfetto::Consumer implementation.
  126. // This gets called by the Perfetto service as control signals,
  127. // and to send finished protobufs over.
  128. void OnConnect() override;
  129. void OnDisconnect() override;
  130. void OnTracingDisabled(const std::string& error) override;
  131. void OnTraceData(std::vector<perfetto::TracePacket> packets,
  132. bool has_more) override;
  133. void OnObservableEvents(const perfetto::ObservableEvents&) override;
  134. void OnTraceStats(bool success, const perfetto::TraceStats&) override;
  135. // Unused in Chrome.
  136. void OnDetach(bool success) override {}
  137. void OnAttach(bool success, const perfetto::TraceConfig&) override {}
  138. TracingSession* tracing_session_for_testing() {
  139. return tracing_session_.get();
  140. }
  141. private:
  142. void DestructTracingSession();
  143. const raw_ptr<PerfettoService> service_;
  144. std::unique_ptr<TracingSession> tracing_session_;
  145. SEQUENCE_CHECKER(sequence_checker_);
  146. // Keep last to avoid edge-cases where its callbacks come in mid-destruction.
  147. std::unique_ptr<perfetto::TracingService::ConsumerEndpoint>
  148. consumer_endpoint_;
  149. base::WeakPtrFactory<ConsumerHost> weak_factory_{this};
  150. };
  151. } // namespace tracing
  152. #endif // SERVICES_TRACING_PERFETTO_CONSUMER_HOST_H_