perfetto_service.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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_PERFETTO_SERVICE_H_
  5. #define SERVICES_TRACING_PERFETTO_PERFETTO_SERVICE_H_
  6. #include <map>
  7. #include <memory>
  8. #include <set>
  9. #include "base/tracing/perfetto_task_runner.h"
  10. #include "mojo/public/cpp/bindings/receiver_set.h"
  11. #include "mojo/public/cpp/bindings/unique_receiver_set.h"
  12. #include "services/tracing/perfetto/consumer_host.h"
  13. #include "services/tracing/public/mojom/perfetto_service.mojom.h"
  14. namespace perfetto {
  15. class TracingService;
  16. } // namespace perfetto
  17. namespace tracing {
  18. // This class serves two purposes: It wraps the use of the system-wide
  19. // perfetto::TracingService instance, and serves as the main Mojo interface for
  20. // connecting per-process ProducerClient with corresponding service-side
  21. // ProducerHost.
  22. class PerfettoService : public mojom::PerfettoService {
  23. public:
  24. explicit PerfettoService(scoped_refptr<base::SequencedTaskRunner>
  25. task_runner_for_testing = nullptr);
  26. PerfettoService(const PerfettoService&) = delete;
  27. PerfettoService& operator=(const PerfettoService&) = delete;
  28. ~PerfettoService() override;
  29. static PerfettoService* GetInstance();
  30. static bool ParsePidFromProducerName(const std::string& producer_name,
  31. base::ProcessId* pid);
  32. void BindReceiver(mojo::PendingReceiver<mojom::PerfettoService> receiver,
  33. uint32_t pid);
  34. // mojom::PerfettoService implementation.
  35. void ConnectToProducerHost(
  36. mojo::PendingRemote<mojom::ProducerClient> producer_client,
  37. mojo::PendingReceiver<mojom::ProducerHost> producer_host_receiver,
  38. base::UnsafeSharedMemoryRegion shared_memory,
  39. uint64_t shared_memory_buffer_page_size_bytes) override;
  40. perfetto::TracingService* GetService() const;
  41. // Called when a ConsumerHost::TracingSession is created/destroyed (i.e. when
  42. // a consumer starts/finishes tracing.
  43. void RegisterTracingSession(ConsumerHost::TracingSession* consumer_host);
  44. void UnregisterTracingSession(ConsumerHost::TracingSession* consumer_host);
  45. // Make a request of the service for whether or not a TracingSession
  46. // should be allowed to start tracing, in case of pre-existing sessions.
  47. // |callback| will eventually be called once a session is allowed, or it
  48. // will be destroyed.
  49. void RequestTracingSession(mojom::TracingClientPriority priority,
  50. base::OnceClosure callback);
  51. // Called by TracingService to notify the perfetto service of the PIDs of
  52. // actively running services (whenever a service starts or stops).
  53. void AddActiveServicePid(base::ProcessId pid);
  54. void RemoveActiveServicePid(base::ProcessId pid);
  55. void RemoveActiveServicePidIfNoActiveConnections(base::ProcessId pid);
  56. void SetActiveServicePidsInitialized();
  57. std::set<base::ProcessId> active_service_pids() const {
  58. return active_service_pids_;
  59. }
  60. bool active_service_pids_initialized() const {
  61. return active_service_pids_initialized_;
  62. }
  63. base::tracing::PerfettoTaskRunner* perfetto_task_runner() {
  64. return &perfetto_task_runner_;
  65. }
  66. private:
  67. void BindOnSequence(mojo::PendingReceiver<mojom::PerfettoService> receiver);
  68. void CreateServiceOnSequence();
  69. void OnProducerHostDisconnect();
  70. void OnServiceDisconnect();
  71. void OnDisconnectFromProcess(base::ProcessId pid);
  72. base::tracing::PerfettoTaskRunner perfetto_task_runner_;
  73. std::unique_ptr<perfetto::TracingService> service_;
  74. mojo::ReceiverSet<mojom::PerfettoService, uint32_t> receivers_;
  75. mojo::UniqueReceiverSet<mojom::ProducerHost, uint32_t> producer_receivers_;
  76. std::set<ConsumerHost::TracingSession*> tracing_sessions_; // Not owned.
  77. std::set<base::ProcessId> active_service_pids_;
  78. std::map<base::ProcessId, int> num_active_connections_;
  79. bool active_service_pids_initialized_ = false;
  80. };
  81. } // namespace tracing
  82. #endif // SERVICES_TRACING_PERFETTO_PERFETTO_SERVICE_H_