producer_host.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright 2018 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_PRODUCER_HOST_H_
  5. #define SERVICES_TRACING_PERFETTO_PRODUCER_HOST_H_
  6. #include <memory>
  7. #include <string>
  8. #include <utility>
  9. #include <vector>
  10. #include "base/memory/raw_ptr.h"
  11. #include "base/memory/unsafe_shared_memory_region.h"
  12. #include "mojo/public/cpp/bindings/remote.h"
  13. #include "services/tracing/public/mojom/perfetto_service.mojom.h"
  14. #include "third_party/perfetto/include/perfetto/ext/tracing/core/producer.h"
  15. #include "third_party/perfetto/include/perfetto/ext/tracing/core/tracing_service.h"
  16. #include "third_party/perfetto/include/perfetto/tracing/core/forward_decls.h"
  17. namespace base {
  18. namespace tracing {
  19. class PerfettoTaskRunner;
  20. } // namespace tracing
  21. } // namespace base
  22. namespace tracing {
  23. // This class is the service-side part of the Perfetto Producer pair
  24. // and is responsible for registering any available DataSources
  25. // with Perfetto (like ChromeTracing) in OnConnect(). It will forward
  26. // control messages from Perfetto to its per-process ProducerClient
  27. // counterpart, like starting tracing with a specific shared memory buffer,
  28. // create/teardown instances of specific data sources, etc.
  29. // It's managed by PerfettoService which is responsible for
  30. // creating a ProducerHost when a ProducerClient registers itself
  31. // and connects them together.
  32. class ProducerHost : public tracing::mojom::ProducerHost,
  33. public perfetto::Producer {
  34. public:
  35. explicit ProducerHost(base::tracing::PerfettoTaskRunner*);
  36. ProducerHost(const ProducerHost&) = delete;
  37. ProducerHost& operator=(const ProducerHost&) = delete;
  38. ~ProducerHost() override;
  39. // Keep in sync with tools/metrics/histograms/enums.xml. These values are
  40. // persisted to logs. Entries should not be renumbered and numeric values
  41. // should never be reused.
  42. enum class InitializationResult {
  43. kSuccess = 0,
  44. kSmbMappingFailed = 1,
  45. kSmbNotAdopted = 2,
  46. kProducerEndpointConstructionFailed = 3,
  47. kMaxValue = kProducerEndpointConstructionFailed
  48. };
  49. // Called by the ProducerService to register the Producer with Perfetto,
  50. // connect to the corresponding remote ProducerClient, and setup the provided
  51. // shared memory buffer for tracing data exchange.
  52. InitializationResult Initialize(
  53. mojo::PendingRemote<mojom::ProducerClient> producer_client,
  54. perfetto::TracingService* service,
  55. const std::string& name,
  56. base::UnsafeSharedMemoryRegion shared_memory,
  57. uint64_t shared_memory_buffer_page_size_bytes);
  58. // perfetto::Producer implementation.
  59. // Gets called by perfetto::TracingService to toggle specific data sources
  60. // when requested by a Perfetto Consumer.
  61. void OnConnect() override;
  62. void OnDisconnect() override;
  63. void SetupDataSource(perfetto::DataSourceInstanceID id,
  64. const perfetto::DataSourceConfig& config) override;
  65. void StartDataSource(perfetto::DataSourceInstanceID id,
  66. const perfetto::DataSourceConfig& config) override;
  67. void StopDataSource(perfetto::DataSourceInstanceID) override;
  68. void OnTracingSetup() override;
  69. void Flush(perfetto::FlushRequestID,
  70. const perfetto::DataSourceInstanceID* raw_data_source_ids,
  71. size_t num_data_sources) override;
  72. void ClearIncrementalState(
  73. const perfetto::DataSourceInstanceID* data_source_ids,
  74. size_t num_data_sources) override;
  75. // mojom::ProducerHost implementation.
  76. // This interface gets called by the per-process ProducerClients
  77. // to signal that there's changes to be committed to the
  78. // Shared Memory buffer (like finished chunks).
  79. void CommitData(const perfetto::CommitDataRequest& data_request,
  80. CommitDataCallback callback) override;
  81. // Called by the ProducerClient to signal the Host that it can
  82. // provide a specific data source.
  83. void RegisterDataSource(
  84. const perfetto::DataSourceDescriptor& registration_info) override;
  85. // Called by the ProducerClient to associate a TraceWriter with a target
  86. // buffer, which is required to support scraping of the SMB by the service.
  87. void RegisterTraceWriter(uint32_t writer_id, uint32_t target_buffer) override;
  88. void UnregisterTraceWriter(uint32_t writer_id) override;
  89. protected:
  90. base::RepeatingCallback<void(const perfetto::CommitDataRequest&)>
  91. on_commit_callback_for_testing_;
  92. private:
  93. mojo::Remote<mojom::ProducerClient> producer_client_;
  94. raw_ptr<base::tracing::PerfettoTaskRunner> task_runner_;
  95. protected:
  96. // Perfetto guarantees that no OnXX callbacks are invoked on |this|
  97. // immediately after |producer_endpoint_| is destroyed.
  98. std::unique_ptr<perfetto::TracingService::ProducerEndpoint>
  99. producer_endpoint_;
  100. };
  101. } // namespace tracing
  102. #endif // SERVICES_TRACING_PERFETTO_PRODUCER_HOST_H_