tracing_agent.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 COMPONENTS_UI_DEVTOOLS_TRACING_AGENT_H_
  5. #define COMPONENTS_UI_DEVTOOLS_TRACING_AGENT_H_
  6. #include <memory>
  7. #include <string>
  8. #include <utility>
  9. #include "base/callback.h"
  10. #include "base/memory/weak_ptr.h"
  11. #include "base/trace_event/trace_config.h"
  12. #include "components/ui_devtools/devtools_base_agent.h"
  13. #include "components/ui_devtools/tracing.h"
  14. namespace base {
  15. class RepeatingTimer;
  16. }
  17. namespace ui_devtools {
  18. class ConnectorDelegate;
  19. // This class is used for tracing in the ui_devtools performance panel.
  20. // A lot of the code is based on TracingHandler from
  21. // content/browser/devtools/protocol/tracing_handler.h.
  22. class UI_DEVTOOLS_EXPORT TracingAgent
  23. : public UiDevToolsBaseAgent<protocol::Tracing::Metainfo> {
  24. public:
  25. explicit TracingAgent(std::unique_ptr<ConnectorDelegate> connector);
  26. TracingAgent(const TracingAgent&) = delete;
  27. TracingAgent& operator=(const TracingAgent&) = delete;
  28. ~TracingAgent() override;
  29. void set_gpu_pid(base::ProcessId pid) { gpu_pid_ = pid; }
  30. // Sends the Tracing JSON data in the form of CBOR to the frontend.
  31. void OnTraceDataCollected(std::unique_ptr<std::string> trace_fragment);
  32. // Signals that tracing is complete and notifies any data loss to the
  33. // frontend.
  34. void OnTraceComplete();
  35. // Tracing::Backend:
  36. void start(protocol::Maybe<std::string> categories,
  37. protocol::Maybe<std::string> options,
  38. protocol::Maybe<double> buffer_usage_reporting_interval,
  39. std::unique_ptr<StartCallback> callback) override;
  40. protocol::Response end() override;
  41. private:
  42. class DevToolsTraceEndpointProxy;
  43. class PerfettoTracingSession;
  44. struct TraceDataBufferState {
  45. public:
  46. std::string data;
  47. size_t pos = 0;
  48. int open_braces = 0;
  49. bool in_string = false;
  50. bool slashed = false;
  51. size_t offset = 0;
  52. };
  53. // Returns the longest prefix of |trace_fragment| that is a valid list and
  54. // stores the rest (tail) to be used in subsequent calls. This returns the
  55. // longest prefix of the tail prepended to |trace_fragment| next time. Assumes
  56. // that the input is a potentially incomplete string representation of a comma
  57. // separated list of JSON objects.
  58. std::string UpdateTraceDataBuffer(const std::string& trace_fragment);
  59. // Sets TraceConfig to only collect trace events for the specified processes
  60. // in this method. Currently, only the browser process is specified. Starts
  61. // tracing by attempting to enable tracing via perfetto.
  62. void StartTracing(std::unique_ptr<StartCallback>);
  63. // Called when we have successfully started tracing with Perfetto session.
  64. void OnRecordingEnabled(std::unique_ptr<StartCallback> callback);
  65. // Edits tracing data to use the normal devtools frontend logic to display
  66. // the performance metrics. Without this, it will use the devtools generic
  67. // trace logic to display the performance metrics.
  68. void EditTraceDataForFrontend();
  69. // Sets up repeating timer to request the trace buffer status from the
  70. // perfetto tracing session. If usage_reporting_interval is too small, it will
  71. // be clipped to a minimum value.
  72. void SetupTimer(double usage_reporting_interval);
  73. // Sends frontend information on buffer usage such as how much of the buffer
  74. // is used and the approximate number of events in the trace log.
  75. void OnBufferUsage(float percent_full, size_t approximate_event_count);
  76. // Gets updated buffer usage from the perfetto tracing session.
  77. void UpdateBufferUsage();
  78. // Resets buffer usage and passes the trace data to the trace data endpoint
  79. // consumer.
  80. void StopTracing(const scoped_refptr<DevToolsTraceEndpointProxy>& endpoint,
  81. const std::string& agent_label);
  82. std::unique_ptr<base::RepeatingTimer> buffer_usage_poll_timer_;
  83. std::unique_ptr<ConnectorDelegate> connector_;
  84. base::ProcessId gpu_pid_ = base::kNullProcessId;
  85. bool did_initiate_recording_ = false;
  86. double buffer_usage_reporting_interval_ = 0;
  87. base::trace_event::TraceConfig trace_config_;
  88. std::unique_ptr<PerfettoTracingSession> perfetto_session_;
  89. TraceDataBufferState trace_data_buffer_state_;
  90. base::WeakPtrFactory<TracingAgent> weak_factory_{this};
  91. };
  92. } // namespace ui_devtools
  93. #endif // COMPONENTS_UI_DEVTOOLS_TRACING_AGENT_H_