tracing_agent.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright 2015 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 BASE_TRACE_EVENT_TRACING_AGENT_H_
  5. #define BASE_TRACE_EVENT_TRACING_AGENT_H_
  6. #include "base/base_export.h"
  7. #include "base/callback.h"
  8. #include "base/memory/ref_counted_memory.h"
  9. #include "base/values.h"
  10. namespace base {
  11. class TimeTicks;
  12. namespace trace_event {
  13. class TraceConfig;
  14. // A tracing agent is an entity that records its own sort of trace. Each
  15. // tracing method that produces its own trace log should implement this
  16. // interface. All tracing agents must only be controlled by TracingController.
  17. // Some existing examples include TracingControllerImpl for Chrome trace events,
  18. // DebugDaemonClient for CrOs system trace, and EtwTracingAgent for Windows
  19. // system.
  20. class BASE_EXPORT TracingAgent {
  21. public:
  22. using StartAgentTracingCallback =
  23. base::OnceCallback<void(const std::string& agent_name, bool success)>;
  24. // Passing a null or empty events_str_ptr indicates that no trace data is
  25. // available for the specified agent.
  26. using StopAgentTracingCallback = base::OnceCallback<void(
  27. const std::string& agent_name,
  28. const std::string& events_label,
  29. const scoped_refptr<base::RefCountedString>& events_str_ptr)>;
  30. using RecordClockSyncMarkerCallback =
  31. base::OnceCallback<void(const std::string& sync_id,
  32. const TimeTicks& issue_ts,
  33. const TimeTicks& issue_end_ts)>;
  34. virtual ~TracingAgent();
  35. // Gets the name of the tracing agent. Each tracing agent's name should be
  36. // unique.
  37. virtual std::string GetTracingAgentName() = 0;
  38. // Gets the trace event label of this tracing agent. The label will be used to
  39. // label this agent's trace when all traces from different tracing agents are
  40. // combined. Multiple tracing agents could have the same label. The tracing
  41. // agents using the same label should not be able to run at the same time. For
  42. // example, ETW on Windows and CrOS system tracing both use
  43. // "systemTraceEvents" as the label. Those two agents never run at the same
  44. // time because they are for different platforms.
  45. virtual std::string GetTraceEventLabel() = 0;
  46. // Starts tracing on the tracing agent with the trace configuration.
  47. virtual void StartAgentTracing(const TraceConfig& trace_config,
  48. StartAgentTracingCallback callback) = 0;
  49. // Stops tracing on the tracing agent. The trace data will be passed back to
  50. // the TracingController via the callback.
  51. virtual void StopAgentTracing(StopAgentTracingCallback callback) = 0;
  52. // Checks if the tracing agent supports explicit clock synchronization.
  53. virtual bool SupportsExplicitClockSync();
  54. // Records a clock sync marker issued by another tracing agent. This is only
  55. // used if the tracing agent supports explicit clock synchronization.
  56. //
  57. // Two things need to be done:
  58. // 1. The issuer asks the receiver to record the clock sync marker.
  59. // 2. The issuer records how long the receiver takes to do the recording.
  60. //
  61. // In Chrome, the receiver thread also runs in Chrome and it will talk to the
  62. // real receiver entity, e.g., power monitor or Android device system, via
  63. // different communication methods, e.g., through USB or file reading/writing.
  64. // The 2nd task measures that communication latency.
  65. //
  66. // Having a reliable timing measurement for the 2nd task requires synchronous
  67. // function call without any cross-thread or cross-process activity. However,
  68. // tracing agents in Chrome run in their own threads. Therefore, the issuer
  69. // needs to dedicate the 2nd task to the receiver to take time measurements
  70. // in the receiver thread, and the receiver thread needs to pass them back to
  71. // the issuer in the callback.
  72. //
  73. // The assumption is that the receiver thread knows the issuer's clock, which
  74. // is true in Chrome because all agent threads' clocks are Chrome clock.
  75. virtual void RecordClockSyncMarker(const std::string& sync_id,
  76. RecordClockSyncMarkerCallback callback);
  77. };
  78. } // namespace trace_event
  79. } // namespace base
  80. #endif // BASE_TRACE_EVENT_TRACING_AGENT_H_