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