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