1*635a8641SAndroid Build Coastguard Worker // Copyright (c) 2012 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 // Use trace_analyzer::Query and trace_analyzer::TraceAnalyzer to search for
6*635a8641SAndroid Build Coastguard Worker // specific trace events that were generated by the trace_event.h API.
7*635a8641SAndroid Build Coastguard Worker //
8*635a8641SAndroid Build Coastguard Worker // Basic procedure:
9*635a8641SAndroid Build Coastguard Worker // - Get trace events JSON string from base::trace_event::TraceLog.
10*635a8641SAndroid Build Coastguard Worker // - Create TraceAnalyzer with JSON string.
11*635a8641SAndroid Build Coastguard Worker // - Call TraceAnalyzer::AssociateBeginEndEvents (optional).
12*635a8641SAndroid Build Coastguard Worker // - Call TraceAnalyzer::AssociateEvents (zero or more times).
13*635a8641SAndroid Build Coastguard Worker // - Call TraceAnalyzer::FindEvents with queries to find specific events.
14*635a8641SAndroid Build Coastguard Worker //
15*635a8641SAndroid Build Coastguard Worker // A Query is a boolean expression tree that evaluates to true or false for a
16*635a8641SAndroid Build Coastguard Worker // given trace event. Queries can be combined into a tree using boolean,
17*635a8641SAndroid Build Coastguard Worker // arithmetic and comparison operators that refer to data of an individual trace
18*635a8641SAndroid Build Coastguard Worker // event.
19*635a8641SAndroid Build Coastguard Worker //
20*635a8641SAndroid Build Coastguard Worker // The events are returned as trace_analyzer::TraceEvent objects.
21*635a8641SAndroid Build Coastguard Worker // TraceEvent contains a single trace event's data, as well as a pointer to
22*635a8641SAndroid Build Coastguard Worker // a related trace event. The related trace event is typically the matching end
23*635a8641SAndroid Build Coastguard Worker // of a begin event or the matching begin of an end event.
24*635a8641SAndroid Build Coastguard Worker //
25*635a8641SAndroid Build Coastguard Worker // The following examples use this basic setup code to construct TraceAnalyzer
26*635a8641SAndroid Build Coastguard Worker // with the json trace string retrieved from TraceLog and construct an event
27*635a8641SAndroid Build Coastguard Worker // vector for retrieving events:
28*635a8641SAndroid Build Coastguard Worker //
29*635a8641SAndroid Build Coastguard Worker // TraceAnalyzer analyzer(json_events);
30*635a8641SAndroid Build Coastguard Worker // TraceEventVector events;
31*635a8641SAndroid Build Coastguard Worker //
32*635a8641SAndroid Build Coastguard Worker // EXAMPLE 1: Find events named "my_event".
33*635a8641SAndroid Build Coastguard Worker //
34*635a8641SAndroid Build Coastguard Worker // analyzer.FindEvents(Query(EVENT_NAME) == "my_event", &events);
35*635a8641SAndroid Build Coastguard Worker //
36*635a8641SAndroid Build Coastguard Worker // EXAMPLE 2: Find begin events named "my_event" with duration > 1 second.
37*635a8641SAndroid Build Coastguard Worker //
38*635a8641SAndroid Build Coastguard Worker // Query q = (Query(EVENT_NAME) == Query::String("my_event") &&
39*635a8641SAndroid Build Coastguard Worker // Query(EVENT_PHASE) == Query::Phase(TRACE_EVENT_PHASE_BEGIN) &&
40*635a8641SAndroid Build Coastguard Worker // Query(EVENT_DURATION) > Query::Double(1000000.0));
41*635a8641SAndroid Build Coastguard Worker // analyzer.FindEvents(q, &events);
42*635a8641SAndroid Build Coastguard Worker //
43*635a8641SAndroid Build Coastguard Worker // EXAMPLE 3: Associating event pairs across threads.
44*635a8641SAndroid Build Coastguard Worker //
45*635a8641SAndroid Build Coastguard Worker // If the test needs to analyze something that starts and ends on different
46*635a8641SAndroid Build Coastguard Worker // threads, the test needs to use INSTANT events. The typical procedure is to
47*635a8641SAndroid Build Coastguard Worker // specify the same unique ID as a TRACE_EVENT argument on both the start and
48*635a8641SAndroid Build Coastguard Worker // finish INSTANT events. Then use the following procedure to associate those
49*635a8641SAndroid Build Coastguard Worker // events.
50*635a8641SAndroid Build Coastguard Worker //
51*635a8641SAndroid Build Coastguard Worker // Step 1: instrument code with custom begin/end trace events.
52*635a8641SAndroid Build Coastguard Worker // [Thread 1 tracing code]
53*635a8641SAndroid Build Coastguard Worker // TRACE_EVENT_INSTANT1("test_latency", "timing1_begin", "id", 3);
54*635a8641SAndroid Build Coastguard Worker // [Thread 2 tracing code]
55*635a8641SAndroid Build Coastguard Worker // TRACE_EVENT_INSTANT1("test_latency", "timing1_end", "id", 3);
56*635a8641SAndroid Build Coastguard Worker //
57*635a8641SAndroid Build Coastguard Worker // Step 2: associate these custom begin/end pairs.
58*635a8641SAndroid Build Coastguard Worker // Query begin(Query(EVENT_NAME) == Query::String("timing1_begin"));
59*635a8641SAndroid Build Coastguard Worker // Query end(Query(EVENT_NAME) == Query::String("timing1_end"));
60*635a8641SAndroid Build Coastguard Worker // Query match(Query(EVENT_ARG, "id") == Query(OTHER_ARG, "id"));
61*635a8641SAndroid Build Coastguard Worker // analyzer.AssociateEvents(begin, end, match);
62*635a8641SAndroid Build Coastguard Worker //
63*635a8641SAndroid Build Coastguard Worker // Step 3: search for "timing1_begin" events with existing other event.
64*635a8641SAndroid Build Coastguard Worker // Query q = (Query(EVENT_NAME) == Query::String("timing1_begin") &&
65*635a8641SAndroid Build Coastguard Worker // Query(EVENT_HAS_OTHER));
66*635a8641SAndroid Build Coastguard Worker // analyzer.FindEvents(q, &events);
67*635a8641SAndroid Build Coastguard Worker //
68*635a8641SAndroid Build Coastguard Worker // Step 4: analyze events, such as checking durations.
69*635a8641SAndroid Build Coastguard Worker // for (size_t i = 0; i < events.size(); ++i) {
70*635a8641SAndroid Build Coastguard Worker // double duration;
71*635a8641SAndroid Build Coastguard Worker // EXPECT_TRUE(events[i].GetAbsTimeToOtherEvent(&duration));
72*635a8641SAndroid Build Coastguard Worker // EXPECT_LT(duration, 1000000.0/60.0); // expect less than 1/60 second.
73*635a8641SAndroid Build Coastguard Worker // }
74*635a8641SAndroid Build Coastguard Worker //
75*635a8641SAndroid Build Coastguard Worker // There are two helper functions, Start(category_filter_string) and Stop(), for
76*635a8641SAndroid Build Coastguard Worker // facilitating the collection of process-local traces and building a
77*635a8641SAndroid Build Coastguard Worker // TraceAnalyzer from them. A typical test, that uses the helper functions,
78*635a8641SAndroid Build Coastguard Worker // looks like the following:
79*635a8641SAndroid Build Coastguard Worker //
80*635a8641SAndroid Build Coastguard Worker // TEST_F(...) {
81*635a8641SAndroid Build Coastguard Worker // Start("*");
82*635a8641SAndroid Build Coastguard Worker // [Invoke the functions you want to test their traces]
83*635a8641SAndroid Build Coastguard Worker // auto analyzer = Stop();
84*635a8641SAndroid Build Coastguard Worker //
85*635a8641SAndroid Build Coastguard Worker // [Use the analyzer to verify produced traces, as explained above]
86*635a8641SAndroid Build Coastguard Worker // }
87*635a8641SAndroid Build Coastguard Worker //
88*635a8641SAndroid Build Coastguard Worker // Note: The Stop() function needs a SingleThreadTaskRunner.
89*635a8641SAndroid Build Coastguard Worker
90*635a8641SAndroid Build Coastguard Worker #ifndef BASE_TEST_TRACE_EVENT_ANALYZER_H_
91*635a8641SAndroid Build Coastguard Worker #define BASE_TEST_TRACE_EVENT_ANALYZER_H_
92*635a8641SAndroid Build Coastguard Worker
93*635a8641SAndroid Build Coastguard Worker #include <stddef.h>
94*635a8641SAndroid Build Coastguard Worker #include <stdint.h>
95*635a8641SAndroid Build Coastguard Worker
96*635a8641SAndroid Build Coastguard Worker #include <map>
97*635a8641SAndroid Build Coastguard Worker #include <memory>
98*635a8641SAndroid Build Coastguard Worker #include <string>
99*635a8641SAndroid Build Coastguard Worker #include <vector>
100*635a8641SAndroid Build Coastguard Worker
101*635a8641SAndroid Build Coastguard Worker #include "base/macros.h"
102*635a8641SAndroid Build Coastguard Worker #include "base/memory/ref_counted.h"
103*635a8641SAndroid Build Coastguard Worker #include "base/trace_event/trace_event.h"
104*635a8641SAndroid Build Coastguard Worker
105*635a8641SAndroid Build Coastguard Worker namespace base {
106*635a8641SAndroid Build Coastguard Worker class Value;
107*635a8641SAndroid Build Coastguard Worker }
108*635a8641SAndroid Build Coastguard Worker
109*635a8641SAndroid Build Coastguard Worker namespace trace_analyzer {
110*635a8641SAndroid Build Coastguard Worker class QueryNode;
111*635a8641SAndroid Build Coastguard Worker
112*635a8641SAndroid Build Coastguard Worker // trace_analyzer::TraceEvent is a more convenient form of the
113*635a8641SAndroid Build Coastguard Worker // base::trace_event::TraceEvent class to make tracing-based tests easier to
114*635a8641SAndroid Build Coastguard Worker // write.
115*635a8641SAndroid Build Coastguard Worker struct TraceEvent {
116*635a8641SAndroid Build Coastguard Worker // ProcessThreadID contains a Process ID and Thread ID.
117*635a8641SAndroid Build Coastguard Worker struct ProcessThreadID {
ProcessThreadIDTraceEvent::ProcessThreadID118*635a8641SAndroid Build Coastguard Worker ProcessThreadID() : process_id(0), thread_id(0) {}
ProcessThreadIDTraceEvent::ProcessThreadID119*635a8641SAndroid Build Coastguard Worker ProcessThreadID(int process_id, int thread_id)
120*635a8641SAndroid Build Coastguard Worker : process_id(process_id), thread_id(thread_id) {}
121*635a8641SAndroid Build Coastguard Worker bool operator< (const ProcessThreadID& rhs) const {
122*635a8641SAndroid Build Coastguard Worker if (process_id != rhs.process_id)
123*635a8641SAndroid Build Coastguard Worker return process_id < rhs.process_id;
124*635a8641SAndroid Build Coastguard Worker return thread_id < rhs.thread_id;
125*635a8641SAndroid Build Coastguard Worker }
126*635a8641SAndroid Build Coastguard Worker int process_id;
127*635a8641SAndroid Build Coastguard Worker int thread_id;
128*635a8641SAndroid Build Coastguard Worker };
129*635a8641SAndroid Build Coastguard Worker
130*635a8641SAndroid Build Coastguard Worker TraceEvent();
131*635a8641SAndroid Build Coastguard Worker TraceEvent(TraceEvent&& other);
132*635a8641SAndroid Build Coastguard Worker ~TraceEvent();
133*635a8641SAndroid Build Coastguard Worker
134*635a8641SAndroid Build Coastguard Worker bool SetFromJSON(const base::Value* event_value) WARN_UNUSED_RESULT;
135*635a8641SAndroid Build Coastguard Worker
136*635a8641SAndroid Build Coastguard Worker bool operator< (const TraceEvent& rhs) const {
137*635a8641SAndroid Build Coastguard Worker return timestamp < rhs.timestamp;
138*635a8641SAndroid Build Coastguard Worker }
139*635a8641SAndroid Build Coastguard Worker
140*635a8641SAndroid Build Coastguard Worker TraceEvent& operator=(TraceEvent&& rhs);
141*635a8641SAndroid Build Coastguard Worker
has_other_eventTraceEvent142*635a8641SAndroid Build Coastguard Worker bool has_other_event() const { return other_event; }
143*635a8641SAndroid Build Coastguard Worker
144*635a8641SAndroid Build Coastguard Worker // Returns absolute duration in microseconds between this event and other
145*635a8641SAndroid Build Coastguard Worker // event. Must have already verified that other_event exists by
146*635a8641SAndroid Build Coastguard Worker // Query(EVENT_HAS_OTHER) or by calling has_other_event().
147*635a8641SAndroid Build Coastguard Worker double GetAbsTimeToOtherEvent() const;
148*635a8641SAndroid Build Coastguard Worker
149*635a8641SAndroid Build Coastguard Worker // Return the argument value if it exists and it is a string.
150*635a8641SAndroid Build Coastguard Worker bool GetArgAsString(const std::string& name, std::string* arg) const;
151*635a8641SAndroid Build Coastguard Worker // Return the argument value if it exists and it is a number.
152*635a8641SAndroid Build Coastguard Worker bool GetArgAsNumber(const std::string& name, double* arg) const;
153*635a8641SAndroid Build Coastguard Worker // Return the argument value if it exists.
154*635a8641SAndroid Build Coastguard Worker bool GetArgAsValue(const std::string& name,
155*635a8641SAndroid Build Coastguard Worker std::unique_ptr<base::Value>* arg) const;
156*635a8641SAndroid Build Coastguard Worker
157*635a8641SAndroid Build Coastguard Worker // Check if argument exists and is string.
158*635a8641SAndroid Build Coastguard Worker bool HasStringArg(const std::string& name) const;
159*635a8641SAndroid Build Coastguard Worker // Check if argument exists and is number (double, int or bool).
160*635a8641SAndroid Build Coastguard Worker bool HasNumberArg(const std::string& name) const;
161*635a8641SAndroid Build Coastguard Worker // Check if argument exists.
162*635a8641SAndroid Build Coastguard Worker bool HasArg(const std::string& name) const;
163*635a8641SAndroid Build Coastguard Worker
164*635a8641SAndroid Build Coastguard Worker // Get known existing arguments as specific types.
165*635a8641SAndroid Build Coastguard Worker // Useful when you have already queried the argument with
166*635a8641SAndroid Build Coastguard Worker // Query(HAS_NUMBER_ARG) or Query(HAS_STRING_ARG).
167*635a8641SAndroid Build Coastguard Worker std::string GetKnownArgAsString(const std::string& name) const;
168*635a8641SAndroid Build Coastguard Worker double GetKnownArgAsDouble(const std::string& name) const;
169*635a8641SAndroid Build Coastguard Worker int GetKnownArgAsInt(const std::string& name) const;
170*635a8641SAndroid Build Coastguard Worker bool GetKnownArgAsBool(const std::string& name) const;
171*635a8641SAndroid Build Coastguard Worker std::unique_ptr<base::Value> GetKnownArgAsValue(
172*635a8641SAndroid Build Coastguard Worker const std::string& name) const;
173*635a8641SAndroid Build Coastguard Worker
174*635a8641SAndroid Build Coastguard Worker // Process ID and Thread ID.
175*635a8641SAndroid Build Coastguard Worker ProcessThreadID thread;
176*635a8641SAndroid Build Coastguard Worker
177*635a8641SAndroid Build Coastguard Worker // Time since epoch in microseconds.
178*635a8641SAndroid Build Coastguard Worker // Stored as double to match its JSON representation.
179*635a8641SAndroid Build Coastguard Worker double timestamp;
180*635a8641SAndroid Build Coastguard Worker double duration;
181*635a8641SAndroid Build Coastguard Worker char phase;
182*635a8641SAndroid Build Coastguard Worker std::string category;
183*635a8641SAndroid Build Coastguard Worker std::string name;
184*635a8641SAndroid Build Coastguard Worker std::string id;
185*635a8641SAndroid Build Coastguard Worker double thread_duration = 0.0;
186*635a8641SAndroid Build Coastguard Worker double thread_timestamp = 0.0;
187*635a8641SAndroid Build Coastguard Worker std::string scope;
188*635a8641SAndroid Build Coastguard Worker std::string bind_id;
189*635a8641SAndroid Build Coastguard Worker bool flow_out = false;
190*635a8641SAndroid Build Coastguard Worker bool flow_in = false;
191*635a8641SAndroid Build Coastguard Worker std::string global_id2;
192*635a8641SAndroid Build Coastguard Worker std::string local_id2;
193*635a8641SAndroid Build Coastguard Worker
194*635a8641SAndroid Build Coastguard Worker // All numbers and bool values from TraceEvent args are cast to double.
195*635a8641SAndroid Build Coastguard Worker // bool becomes 1.0 (true) or 0.0 (false).
196*635a8641SAndroid Build Coastguard Worker std::map<std::string, double> arg_numbers;
197*635a8641SAndroid Build Coastguard Worker std::map<std::string, std::string> arg_strings;
198*635a8641SAndroid Build Coastguard Worker std::map<std::string, std::unique_ptr<base::Value>> arg_values;
199*635a8641SAndroid Build Coastguard Worker
200*635a8641SAndroid Build Coastguard Worker // The other event associated with this event (or NULL).
201*635a8641SAndroid Build Coastguard Worker const TraceEvent* other_event;
202*635a8641SAndroid Build Coastguard Worker
203*635a8641SAndroid Build Coastguard Worker // A back-link for |other_event|. That is, if other_event is not null, then
204*635a8641SAndroid Build Coastguard Worker // |event->other_event->prev_event == event| is always true.
205*635a8641SAndroid Build Coastguard Worker const TraceEvent* prev_event;
206*635a8641SAndroid Build Coastguard Worker };
207*635a8641SAndroid Build Coastguard Worker
208*635a8641SAndroid Build Coastguard Worker typedef std::vector<const TraceEvent*> TraceEventVector;
209*635a8641SAndroid Build Coastguard Worker
210*635a8641SAndroid Build Coastguard Worker class Query {
211*635a8641SAndroid Build Coastguard Worker public:
212*635a8641SAndroid Build Coastguard Worker Query(const Query& query);
213*635a8641SAndroid Build Coastguard Worker
214*635a8641SAndroid Build Coastguard Worker ~Query();
215*635a8641SAndroid Build Coastguard Worker
216*635a8641SAndroid Build Coastguard Worker ////////////////////////////////////////////////////////////////
217*635a8641SAndroid Build Coastguard Worker // Query literal values
218*635a8641SAndroid Build Coastguard Worker
219*635a8641SAndroid Build Coastguard Worker // Compare with the given string.
220*635a8641SAndroid Build Coastguard Worker static Query String(const std::string& str);
221*635a8641SAndroid Build Coastguard Worker
222*635a8641SAndroid Build Coastguard Worker // Compare with the given number.
223*635a8641SAndroid Build Coastguard Worker static Query Double(double num);
224*635a8641SAndroid Build Coastguard Worker static Query Int(int32_t num);
225*635a8641SAndroid Build Coastguard Worker static Query Uint(uint32_t num);
226*635a8641SAndroid Build Coastguard Worker
227*635a8641SAndroid Build Coastguard Worker // Compare with the given bool.
228*635a8641SAndroid Build Coastguard Worker static Query Bool(bool boolean);
229*635a8641SAndroid Build Coastguard Worker
230*635a8641SAndroid Build Coastguard Worker // Compare with the given phase.
231*635a8641SAndroid Build Coastguard Worker static Query Phase(char phase);
232*635a8641SAndroid Build Coastguard Worker
233*635a8641SAndroid Build Coastguard Worker // Compare with the given string pattern. Only works with == and != operators.
234*635a8641SAndroid Build Coastguard Worker // Example: Query(EVENT_NAME) == Query::Pattern("MyEvent*")
235*635a8641SAndroid Build Coastguard Worker static Query Pattern(const std::string& pattern);
236*635a8641SAndroid Build Coastguard Worker
237*635a8641SAndroid Build Coastguard Worker ////////////////////////////////////////////////////////////////
238*635a8641SAndroid Build Coastguard Worker // Query event members
239*635a8641SAndroid Build Coastguard Worker
EventPid()240*635a8641SAndroid Build Coastguard Worker static Query EventPid() { return Query(EVENT_PID); }
241*635a8641SAndroid Build Coastguard Worker
EventTid()242*635a8641SAndroid Build Coastguard Worker static Query EventTid() { return Query(EVENT_TID); }
243*635a8641SAndroid Build Coastguard Worker
244*635a8641SAndroid Build Coastguard Worker // Return the timestamp of the event in microseconds since epoch.
EventTime()245*635a8641SAndroid Build Coastguard Worker static Query EventTime() { return Query(EVENT_TIME); }
246*635a8641SAndroid Build Coastguard Worker
247*635a8641SAndroid Build Coastguard Worker // Return the absolute time between event and other event in microseconds.
248*635a8641SAndroid Build Coastguard Worker // Only works if Query::EventHasOther() == true.
EventDuration()249*635a8641SAndroid Build Coastguard Worker static Query EventDuration() { return Query(EVENT_DURATION); }
250*635a8641SAndroid Build Coastguard Worker
251*635a8641SAndroid Build Coastguard Worker // Return the duration of a COMPLETE event.
EventCompleteDuration()252*635a8641SAndroid Build Coastguard Worker static Query EventCompleteDuration() {
253*635a8641SAndroid Build Coastguard Worker return Query(EVENT_COMPLETE_DURATION);
254*635a8641SAndroid Build Coastguard Worker }
255*635a8641SAndroid Build Coastguard Worker
EventPhase()256*635a8641SAndroid Build Coastguard Worker static Query EventPhase() { return Query(EVENT_PHASE); }
257*635a8641SAndroid Build Coastguard Worker
EventCategory()258*635a8641SAndroid Build Coastguard Worker static Query EventCategory() { return Query(EVENT_CATEGORY); }
259*635a8641SAndroid Build Coastguard Worker
EventName()260*635a8641SAndroid Build Coastguard Worker static Query EventName() { return Query(EVENT_NAME); }
261*635a8641SAndroid Build Coastguard Worker
EventId()262*635a8641SAndroid Build Coastguard Worker static Query EventId() { return Query(EVENT_ID); }
263*635a8641SAndroid Build Coastguard Worker
EventPidIs(int process_id)264*635a8641SAndroid Build Coastguard Worker static Query EventPidIs(int process_id) {
265*635a8641SAndroid Build Coastguard Worker return Query(EVENT_PID) == Query::Int(process_id);
266*635a8641SAndroid Build Coastguard Worker }
267*635a8641SAndroid Build Coastguard Worker
EventTidIs(int thread_id)268*635a8641SAndroid Build Coastguard Worker static Query EventTidIs(int thread_id) {
269*635a8641SAndroid Build Coastguard Worker return Query(EVENT_TID) == Query::Int(thread_id);
270*635a8641SAndroid Build Coastguard Worker }
271*635a8641SAndroid Build Coastguard Worker
EventThreadIs(const TraceEvent::ProcessThreadID & thread)272*635a8641SAndroid Build Coastguard Worker static Query EventThreadIs(const TraceEvent::ProcessThreadID& thread) {
273*635a8641SAndroid Build Coastguard Worker return EventPidIs(thread.process_id) && EventTidIs(thread.thread_id);
274*635a8641SAndroid Build Coastguard Worker }
275*635a8641SAndroid Build Coastguard Worker
EventTimeIs(double timestamp)276*635a8641SAndroid Build Coastguard Worker static Query EventTimeIs(double timestamp) {
277*635a8641SAndroid Build Coastguard Worker return Query(EVENT_TIME) == Query::Double(timestamp);
278*635a8641SAndroid Build Coastguard Worker }
279*635a8641SAndroid Build Coastguard Worker
EventDurationIs(double duration)280*635a8641SAndroid Build Coastguard Worker static Query EventDurationIs(double duration) {
281*635a8641SAndroid Build Coastguard Worker return Query(EVENT_DURATION) == Query::Double(duration);
282*635a8641SAndroid Build Coastguard Worker }
283*635a8641SAndroid Build Coastguard Worker
EventPhaseIs(char phase)284*635a8641SAndroid Build Coastguard Worker static Query EventPhaseIs(char phase) {
285*635a8641SAndroid Build Coastguard Worker return Query(EVENT_PHASE) == Query::Phase(phase);
286*635a8641SAndroid Build Coastguard Worker }
287*635a8641SAndroid Build Coastguard Worker
EventCategoryIs(const std::string & category)288*635a8641SAndroid Build Coastguard Worker static Query EventCategoryIs(const std::string& category) {
289*635a8641SAndroid Build Coastguard Worker return Query(EVENT_CATEGORY) == Query::String(category);
290*635a8641SAndroid Build Coastguard Worker }
291*635a8641SAndroid Build Coastguard Worker
EventNameIs(const std::string & name)292*635a8641SAndroid Build Coastguard Worker static Query EventNameIs(const std::string& name) {
293*635a8641SAndroid Build Coastguard Worker return Query(EVENT_NAME) == Query::String(name);
294*635a8641SAndroid Build Coastguard Worker }
295*635a8641SAndroid Build Coastguard Worker
EventIdIs(const std::string & id)296*635a8641SAndroid Build Coastguard Worker static Query EventIdIs(const std::string& id) {
297*635a8641SAndroid Build Coastguard Worker return Query(EVENT_ID) == Query::String(id);
298*635a8641SAndroid Build Coastguard Worker }
299*635a8641SAndroid Build Coastguard Worker
300*635a8641SAndroid Build Coastguard Worker // Evaluates to true if arg exists and is a string.
EventHasStringArg(const std::string & arg_name)301*635a8641SAndroid Build Coastguard Worker static Query EventHasStringArg(const std::string& arg_name) {
302*635a8641SAndroid Build Coastguard Worker return Query(EVENT_HAS_STRING_ARG, arg_name);
303*635a8641SAndroid Build Coastguard Worker }
304*635a8641SAndroid Build Coastguard Worker
305*635a8641SAndroid Build Coastguard Worker // Evaluates to true if arg exists and is a number.
306*635a8641SAndroid Build Coastguard Worker // Number arguments include types double, int and bool.
EventHasNumberArg(const std::string & arg_name)307*635a8641SAndroid Build Coastguard Worker static Query EventHasNumberArg(const std::string& arg_name) {
308*635a8641SAndroid Build Coastguard Worker return Query(EVENT_HAS_NUMBER_ARG, arg_name);
309*635a8641SAndroid Build Coastguard Worker }
310*635a8641SAndroid Build Coastguard Worker
311*635a8641SAndroid Build Coastguard Worker // Evaluates to arg value (string or number).
EventArg(const std::string & arg_name)312*635a8641SAndroid Build Coastguard Worker static Query EventArg(const std::string& arg_name) {
313*635a8641SAndroid Build Coastguard Worker return Query(EVENT_ARG, arg_name);
314*635a8641SAndroid Build Coastguard Worker }
315*635a8641SAndroid Build Coastguard Worker
316*635a8641SAndroid Build Coastguard Worker // Return true if associated event exists.
EventHasOther()317*635a8641SAndroid Build Coastguard Worker static Query EventHasOther() { return Query(EVENT_HAS_OTHER); }
318*635a8641SAndroid Build Coastguard Worker
319*635a8641SAndroid Build Coastguard Worker // Access the associated other_event's members:
320*635a8641SAndroid Build Coastguard Worker
OtherPid()321*635a8641SAndroid Build Coastguard Worker static Query OtherPid() { return Query(OTHER_PID); }
322*635a8641SAndroid Build Coastguard Worker
OtherTid()323*635a8641SAndroid Build Coastguard Worker static Query OtherTid() { return Query(OTHER_TID); }
324*635a8641SAndroid Build Coastguard Worker
OtherTime()325*635a8641SAndroid Build Coastguard Worker static Query OtherTime() { return Query(OTHER_TIME); }
326*635a8641SAndroid Build Coastguard Worker
OtherPhase()327*635a8641SAndroid Build Coastguard Worker static Query OtherPhase() { return Query(OTHER_PHASE); }
328*635a8641SAndroid Build Coastguard Worker
OtherCategory()329*635a8641SAndroid Build Coastguard Worker static Query OtherCategory() { return Query(OTHER_CATEGORY); }
330*635a8641SAndroid Build Coastguard Worker
OtherName()331*635a8641SAndroid Build Coastguard Worker static Query OtherName() { return Query(OTHER_NAME); }
332*635a8641SAndroid Build Coastguard Worker
OtherId()333*635a8641SAndroid Build Coastguard Worker static Query OtherId() { return Query(OTHER_ID); }
334*635a8641SAndroid Build Coastguard Worker
OtherPidIs(int process_id)335*635a8641SAndroid Build Coastguard Worker static Query OtherPidIs(int process_id) {
336*635a8641SAndroid Build Coastguard Worker return Query(OTHER_PID) == Query::Int(process_id);
337*635a8641SAndroid Build Coastguard Worker }
338*635a8641SAndroid Build Coastguard Worker
OtherTidIs(int thread_id)339*635a8641SAndroid Build Coastguard Worker static Query OtherTidIs(int thread_id) {
340*635a8641SAndroid Build Coastguard Worker return Query(OTHER_TID) == Query::Int(thread_id);
341*635a8641SAndroid Build Coastguard Worker }
342*635a8641SAndroid Build Coastguard Worker
OtherThreadIs(const TraceEvent::ProcessThreadID & thread)343*635a8641SAndroid Build Coastguard Worker static Query OtherThreadIs(const TraceEvent::ProcessThreadID& thread) {
344*635a8641SAndroid Build Coastguard Worker return OtherPidIs(thread.process_id) && OtherTidIs(thread.thread_id);
345*635a8641SAndroid Build Coastguard Worker }
346*635a8641SAndroid Build Coastguard Worker
OtherTimeIs(double timestamp)347*635a8641SAndroid Build Coastguard Worker static Query OtherTimeIs(double timestamp) {
348*635a8641SAndroid Build Coastguard Worker return Query(OTHER_TIME) == Query::Double(timestamp);
349*635a8641SAndroid Build Coastguard Worker }
350*635a8641SAndroid Build Coastguard Worker
OtherPhaseIs(char phase)351*635a8641SAndroid Build Coastguard Worker static Query OtherPhaseIs(char phase) {
352*635a8641SAndroid Build Coastguard Worker return Query(OTHER_PHASE) == Query::Phase(phase);
353*635a8641SAndroid Build Coastguard Worker }
354*635a8641SAndroid Build Coastguard Worker
OtherCategoryIs(const std::string & category)355*635a8641SAndroid Build Coastguard Worker static Query OtherCategoryIs(const std::string& category) {
356*635a8641SAndroid Build Coastguard Worker return Query(OTHER_CATEGORY) == Query::String(category);
357*635a8641SAndroid Build Coastguard Worker }
358*635a8641SAndroid Build Coastguard Worker
OtherNameIs(const std::string & name)359*635a8641SAndroid Build Coastguard Worker static Query OtherNameIs(const std::string& name) {
360*635a8641SAndroid Build Coastguard Worker return Query(OTHER_NAME) == Query::String(name);
361*635a8641SAndroid Build Coastguard Worker }
362*635a8641SAndroid Build Coastguard Worker
OtherIdIs(const std::string & id)363*635a8641SAndroid Build Coastguard Worker static Query OtherIdIs(const std::string& id) {
364*635a8641SAndroid Build Coastguard Worker return Query(OTHER_ID) == Query::String(id);
365*635a8641SAndroid Build Coastguard Worker }
366*635a8641SAndroid Build Coastguard Worker
367*635a8641SAndroid Build Coastguard Worker // Evaluates to true if arg exists and is a string.
OtherHasStringArg(const std::string & arg_name)368*635a8641SAndroid Build Coastguard Worker static Query OtherHasStringArg(const std::string& arg_name) {
369*635a8641SAndroid Build Coastguard Worker return Query(OTHER_HAS_STRING_ARG, arg_name);
370*635a8641SAndroid Build Coastguard Worker }
371*635a8641SAndroid Build Coastguard Worker
372*635a8641SAndroid Build Coastguard Worker // Evaluates to true if arg exists and is a number.
373*635a8641SAndroid Build Coastguard Worker // Number arguments include types double, int and bool.
OtherHasNumberArg(const std::string & arg_name)374*635a8641SAndroid Build Coastguard Worker static Query OtherHasNumberArg(const std::string& arg_name) {
375*635a8641SAndroid Build Coastguard Worker return Query(OTHER_HAS_NUMBER_ARG, arg_name);
376*635a8641SAndroid Build Coastguard Worker }
377*635a8641SAndroid Build Coastguard Worker
378*635a8641SAndroid Build Coastguard Worker // Evaluates to arg value (string or number).
OtherArg(const std::string & arg_name)379*635a8641SAndroid Build Coastguard Worker static Query OtherArg(const std::string& arg_name) {
380*635a8641SAndroid Build Coastguard Worker return Query(OTHER_ARG, arg_name);
381*635a8641SAndroid Build Coastguard Worker }
382*635a8641SAndroid Build Coastguard Worker
383*635a8641SAndroid Build Coastguard Worker // Access the associated prev_event's members:
384*635a8641SAndroid Build Coastguard Worker
PrevPid()385*635a8641SAndroid Build Coastguard Worker static Query PrevPid() { return Query(PREV_PID); }
386*635a8641SAndroid Build Coastguard Worker
PrevTid()387*635a8641SAndroid Build Coastguard Worker static Query PrevTid() { return Query(PREV_TID); }
388*635a8641SAndroid Build Coastguard Worker
PrevTime()389*635a8641SAndroid Build Coastguard Worker static Query PrevTime() { return Query(PREV_TIME); }
390*635a8641SAndroid Build Coastguard Worker
PrevPhase()391*635a8641SAndroid Build Coastguard Worker static Query PrevPhase() { return Query(PREV_PHASE); }
392*635a8641SAndroid Build Coastguard Worker
PrevCategory()393*635a8641SAndroid Build Coastguard Worker static Query PrevCategory() { return Query(PREV_CATEGORY); }
394*635a8641SAndroid Build Coastguard Worker
PrevName()395*635a8641SAndroid Build Coastguard Worker static Query PrevName() { return Query(PREV_NAME); }
396*635a8641SAndroid Build Coastguard Worker
PrevId()397*635a8641SAndroid Build Coastguard Worker static Query PrevId() { return Query(PREV_ID); }
398*635a8641SAndroid Build Coastguard Worker
PrevPidIs(int process_id)399*635a8641SAndroid Build Coastguard Worker static Query PrevPidIs(int process_id) {
400*635a8641SAndroid Build Coastguard Worker return Query(PREV_PID) == Query::Int(process_id);
401*635a8641SAndroid Build Coastguard Worker }
402*635a8641SAndroid Build Coastguard Worker
PrevTidIs(int thread_id)403*635a8641SAndroid Build Coastguard Worker static Query PrevTidIs(int thread_id) {
404*635a8641SAndroid Build Coastguard Worker return Query(PREV_TID) == Query::Int(thread_id);
405*635a8641SAndroid Build Coastguard Worker }
406*635a8641SAndroid Build Coastguard Worker
PrevThreadIs(const TraceEvent::ProcessThreadID & thread)407*635a8641SAndroid Build Coastguard Worker static Query PrevThreadIs(const TraceEvent::ProcessThreadID& thread) {
408*635a8641SAndroid Build Coastguard Worker return PrevPidIs(thread.process_id) && PrevTidIs(thread.thread_id);
409*635a8641SAndroid Build Coastguard Worker }
410*635a8641SAndroid Build Coastguard Worker
PrevTimeIs(double timestamp)411*635a8641SAndroid Build Coastguard Worker static Query PrevTimeIs(double timestamp) {
412*635a8641SAndroid Build Coastguard Worker return Query(PREV_TIME) == Query::Double(timestamp);
413*635a8641SAndroid Build Coastguard Worker }
414*635a8641SAndroid Build Coastguard Worker
PrevPhaseIs(char phase)415*635a8641SAndroid Build Coastguard Worker static Query PrevPhaseIs(char phase) {
416*635a8641SAndroid Build Coastguard Worker return Query(PREV_PHASE) == Query::Phase(phase);
417*635a8641SAndroid Build Coastguard Worker }
418*635a8641SAndroid Build Coastguard Worker
PrevCategoryIs(const std::string & category)419*635a8641SAndroid Build Coastguard Worker static Query PrevCategoryIs(const std::string& category) {
420*635a8641SAndroid Build Coastguard Worker return Query(PREV_CATEGORY) == Query::String(category);
421*635a8641SAndroid Build Coastguard Worker }
422*635a8641SAndroid Build Coastguard Worker
PrevNameIs(const std::string & name)423*635a8641SAndroid Build Coastguard Worker static Query PrevNameIs(const std::string& name) {
424*635a8641SAndroid Build Coastguard Worker return Query(PREV_NAME) == Query::String(name);
425*635a8641SAndroid Build Coastguard Worker }
426*635a8641SAndroid Build Coastguard Worker
PrevIdIs(const std::string & id)427*635a8641SAndroid Build Coastguard Worker static Query PrevIdIs(const std::string& id) {
428*635a8641SAndroid Build Coastguard Worker return Query(PREV_ID) == Query::String(id);
429*635a8641SAndroid Build Coastguard Worker }
430*635a8641SAndroid Build Coastguard Worker
431*635a8641SAndroid Build Coastguard Worker // Evaluates to true if arg exists and is a string.
PrevHasStringArg(const std::string & arg_name)432*635a8641SAndroid Build Coastguard Worker static Query PrevHasStringArg(const std::string& arg_name) {
433*635a8641SAndroid Build Coastguard Worker return Query(PREV_HAS_STRING_ARG, arg_name);
434*635a8641SAndroid Build Coastguard Worker }
435*635a8641SAndroid Build Coastguard Worker
436*635a8641SAndroid Build Coastguard Worker // Evaluates to true if arg exists and is a number.
437*635a8641SAndroid Build Coastguard Worker // Number arguments include types double, int and bool.
PrevHasNumberArg(const std::string & arg_name)438*635a8641SAndroid Build Coastguard Worker static Query PrevHasNumberArg(const std::string& arg_name) {
439*635a8641SAndroid Build Coastguard Worker return Query(PREV_HAS_NUMBER_ARG, arg_name);
440*635a8641SAndroid Build Coastguard Worker }
441*635a8641SAndroid Build Coastguard Worker
442*635a8641SAndroid Build Coastguard Worker // Evaluates to arg value (string or number).
PrevArg(const std::string & arg_name)443*635a8641SAndroid Build Coastguard Worker static Query PrevArg(const std::string& arg_name) {
444*635a8641SAndroid Build Coastguard Worker return Query(PREV_ARG, arg_name);
445*635a8641SAndroid Build Coastguard Worker }
446*635a8641SAndroid Build Coastguard Worker
447*635a8641SAndroid Build Coastguard Worker ////////////////////////////////////////////////////////////////
448*635a8641SAndroid Build Coastguard Worker // Common queries:
449*635a8641SAndroid Build Coastguard Worker
450*635a8641SAndroid Build Coastguard Worker // Find BEGIN events that have a corresponding END event.
MatchBeginWithEnd()451*635a8641SAndroid Build Coastguard Worker static Query MatchBeginWithEnd() {
452*635a8641SAndroid Build Coastguard Worker return (Query(EVENT_PHASE) == Query::Phase(TRACE_EVENT_PHASE_BEGIN)) &&
453*635a8641SAndroid Build Coastguard Worker Query(EVENT_HAS_OTHER);
454*635a8641SAndroid Build Coastguard Worker }
455*635a8641SAndroid Build Coastguard Worker
456*635a8641SAndroid Build Coastguard Worker // Find COMPLETE events.
MatchComplete()457*635a8641SAndroid Build Coastguard Worker static Query MatchComplete() {
458*635a8641SAndroid Build Coastguard Worker return (Query(EVENT_PHASE) == Query::Phase(TRACE_EVENT_PHASE_COMPLETE));
459*635a8641SAndroid Build Coastguard Worker }
460*635a8641SAndroid Build Coastguard Worker
461*635a8641SAndroid Build Coastguard Worker // Find ASYNC_BEGIN events that have a corresponding ASYNC_END event.
MatchAsyncBeginWithNext()462*635a8641SAndroid Build Coastguard Worker static Query MatchAsyncBeginWithNext() {
463*635a8641SAndroid Build Coastguard Worker return (Query(EVENT_PHASE) ==
464*635a8641SAndroid Build Coastguard Worker Query::Phase(TRACE_EVENT_PHASE_ASYNC_BEGIN)) &&
465*635a8641SAndroid Build Coastguard Worker Query(EVENT_HAS_OTHER);
466*635a8641SAndroid Build Coastguard Worker }
467*635a8641SAndroid Build Coastguard Worker
468*635a8641SAndroid Build Coastguard Worker // Find BEGIN events of given |name| which also have associated END events.
MatchBeginName(const std::string & name)469*635a8641SAndroid Build Coastguard Worker static Query MatchBeginName(const std::string& name) {
470*635a8641SAndroid Build Coastguard Worker return (Query(EVENT_NAME) == Query(name)) && MatchBeginWithEnd();
471*635a8641SAndroid Build Coastguard Worker }
472*635a8641SAndroid Build Coastguard Worker
473*635a8641SAndroid Build Coastguard Worker // Find COMPLETE events of given |name|.
MatchCompleteName(const std::string & name)474*635a8641SAndroid Build Coastguard Worker static Query MatchCompleteName(const std::string& name) {
475*635a8641SAndroid Build Coastguard Worker return (Query(EVENT_NAME) == Query(name)) && MatchComplete();
476*635a8641SAndroid Build Coastguard Worker }
477*635a8641SAndroid Build Coastguard Worker
478*635a8641SAndroid Build Coastguard Worker // Match given Process ID and Thread ID.
MatchThread(const TraceEvent::ProcessThreadID & thread)479*635a8641SAndroid Build Coastguard Worker static Query MatchThread(const TraceEvent::ProcessThreadID& thread) {
480*635a8641SAndroid Build Coastguard Worker return (Query(EVENT_PID) == Query::Int(thread.process_id)) &&
481*635a8641SAndroid Build Coastguard Worker (Query(EVENT_TID) == Query::Int(thread.thread_id));
482*635a8641SAndroid Build Coastguard Worker }
483*635a8641SAndroid Build Coastguard Worker
484*635a8641SAndroid Build Coastguard Worker // Match event pair that spans multiple threads.
MatchCrossThread()485*635a8641SAndroid Build Coastguard Worker static Query MatchCrossThread() {
486*635a8641SAndroid Build Coastguard Worker return (Query(EVENT_PID) != Query(OTHER_PID)) ||
487*635a8641SAndroid Build Coastguard Worker (Query(EVENT_TID) != Query(OTHER_TID));
488*635a8641SAndroid Build Coastguard Worker }
489*635a8641SAndroid Build Coastguard Worker
490*635a8641SAndroid Build Coastguard Worker ////////////////////////////////////////////////////////////////
491*635a8641SAndroid Build Coastguard Worker // Operators:
492*635a8641SAndroid Build Coastguard Worker
493*635a8641SAndroid Build Coastguard Worker // Boolean operators:
494*635a8641SAndroid Build Coastguard Worker Query operator==(const Query& rhs) const;
495*635a8641SAndroid Build Coastguard Worker Query operator!=(const Query& rhs) const;
496*635a8641SAndroid Build Coastguard Worker Query operator< (const Query& rhs) const;
497*635a8641SAndroid Build Coastguard Worker Query operator<=(const Query& rhs) const;
498*635a8641SAndroid Build Coastguard Worker Query operator> (const Query& rhs) const;
499*635a8641SAndroid Build Coastguard Worker Query operator>=(const Query& rhs) const;
500*635a8641SAndroid Build Coastguard Worker Query operator&&(const Query& rhs) const;
501*635a8641SAndroid Build Coastguard Worker Query operator||(const Query& rhs) const;
502*635a8641SAndroid Build Coastguard Worker Query operator!() const;
503*635a8641SAndroid Build Coastguard Worker
504*635a8641SAndroid Build Coastguard Worker // Arithmetic operators:
505*635a8641SAndroid Build Coastguard Worker // Following operators are applied to double arguments:
506*635a8641SAndroid Build Coastguard Worker Query operator+(const Query& rhs) const;
507*635a8641SAndroid Build Coastguard Worker Query operator-(const Query& rhs) const;
508*635a8641SAndroid Build Coastguard Worker Query operator*(const Query& rhs) const;
509*635a8641SAndroid Build Coastguard Worker Query operator/(const Query& rhs) const;
510*635a8641SAndroid Build Coastguard Worker Query operator-() const;
511*635a8641SAndroid Build Coastguard Worker // Mod operates on int64_t args (doubles are casted to int64_t beforehand):
512*635a8641SAndroid Build Coastguard Worker Query operator%(const Query& rhs) const;
513*635a8641SAndroid Build Coastguard Worker
514*635a8641SAndroid Build Coastguard Worker // Return true if the given event matches this query tree.
515*635a8641SAndroid Build Coastguard Worker // This is a recursive method that walks the query tree.
516*635a8641SAndroid Build Coastguard Worker bool Evaluate(const TraceEvent& event) const;
517*635a8641SAndroid Build Coastguard Worker
518*635a8641SAndroid Build Coastguard Worker enum TraceEventMember {
519*635a8641SAndroid Build Coastguard Worker EVENT_INVALID,
520*635a8641SAndroid Build Coastguard Worker EVENT_PID,
521*635a8641SAndroid Build Coastguard Worker EVENT_TID,
522*635a8641SAndroid Build Coastguard Worker EVENT_TIME,
523*635a8641SAndroid Build Coastguard Worker EVENT_DURATION,
524*635a8641SAndroid Build Coastguard Worker EVENT_COMPLETE_DURATION,
525*635a8641SAndroid Build Coastguard Worker EVENT_PHASE,
526*635a8641SAndroid Build Coastguard Worker EVENT_CATEGORY,
527*635a8641SAndroid Build Coastguard Worker EVENT_NAME,
528*635a8641SAndroid Build Coastguard Worker EVENT_ID,
529*635a8641SAndroid Build Coastguard Worker EVENT_HAS_STRING_ARG,
530*635a8641SAndroid Build Coastguard Worker EVENT_HAS_NUMBER_ARG,
531*635a8641SAndroid Build Coastguard Worker EVENT_ARG,
532*635a8641SAndroid Build Coastguard Worker EVENT_HAS_OTHER,
533*635a8641SAndroid Build Coastguard Worker EVENT_HAS_PREV,
534*635a8641SAndroid Build Coastguard Worker
535*635a8641SAndroid Build Coastguard Worker OTHER_PID,
536*635a8641SAndroid Build Coastguard Worker OTHER_TID,
537*635a8641SAndroid Build Coastguard Worker OTHER_TIME,
538*635a8641SAndroid Build Coastguard Worker OTHER_PHASE,
539*635a8641SAndroid Build Coastguard Worker OTHER_CATEGORY,
540*635a8641SAndroid Build Coastguard Worker OTHER_NAME,
541*635a8641SAndroid Build Coastguard Worker OTHER_ID,
542*635a8641SAndroid Build Coastguard Worker OTHER_HAS_STRING_ARG,
543*635a8641SAndroid Build Coastguard Worker OTHER_HAS_NUMBER_ARG,
544*635a8641SAndroid Build Coastguard Worker OTHER_ARG,
545*635a8641SAndroid Build Coastguard Worker
546*635a8641SAndroid Build Coastguard Worker PREV_PID,
547*635a8641SAndroid Build Coastguard Worker PREV_TID,
548*635a8641SAndroid Build Coastguard Worker PREV_TIME,
549*635a8641SAndroid Build Coastguard Worker PREV_PHASE,
550*635a8641SAndroid Build Coastguard Worker PREV_CATEGORY,
551*635a8641SAndroid Build Coastguard Worker PREV_NAME,
552*635a8641SAndroid Build Coastguard Worker PREV_ID,
553*635a8641SAndroid Build Coastguard Worker PREV_HAS_STRING_ARG,
554*635a8641SAndroid Build Coastguard Worker PREV_HAS_NUMBER_ARG,
555*635a8641SAndroid Build Coastguard Worker PREV_ARG,
556*635a8641SAndroid Build Coastguard Worker
557*635a8641SAndroid Build Coastguard Worker OTHER_FIRST_MEMBER = OTHER_PID,
558*635a8641SAndroid Build Coastguard Worker OTHER_LAST_MEMBER = OTHER_ARG,
559*635a8641SAndroid Build Coastguard Worker
560*635a8641SAndroid Build Coastguard Worker PREV_FIRST_MEMBER = PREV_PID,
561*635a8641SAndroid Build Coastguard Worker PREV_LAST_MEMBER = PREV_ARG,
562*635a8641SAndroid Build Coastguard Worker };
563*635a8641SAndroid Build Coastguard Worker
564*635a8641SAndroid Build Coastguard Worker enum Operator {
565*635a8641SAndroid Build Coastguard Worker OP_INVALID,
566*635a8641SAndroid Build Coastguard Worker // Boolean operators:
567*635a8641SAndroid Build Coastguard Worker OP_EQ,
568*635a8641SAndroid Build Coastguard Worker OP_NE,
569*635a8641SAndroid Build Coastguard Worker OP_LT,
570*635a8641SAndroid Build Coastguard Worker OP_LE,
571*635a8641SAndroid Build Coastguard Worker OP_GT,
572*635a8641SAndroid Build Coastguard Worker OP_GE,
573*635a8641SAndroid Build Coastguard Worker OP_AND,
574*635a8641SAndroid Build Coastguard Worker OP_OR,
575*635a8641SAndroid Build Coastguard Worker OP_NOT,
576*635a8641SAndroid Build Coastguard Worker // Arithmetic operators:
577*635a8641SAndroid Build Coastguard Worker OP_ADD,
578*635a8641SAndroid Build Coastguard Worker OP_SUB,
579*635a8641SAndroid Build Coastguard Worker OP_MUL,
580*635a8641SAndroid Build Coastguard Worker OP_DIV,
581*635a8641SAndroid Build Coastguard Worker OP_MOD,
582*635a8641SAndroid Build Coastguard Worker OP_NEGATE
583*635a8641SAndroid Build Coastguard Worker };
584*635a8641SAndroid Build Coastguard Worker
585*635a8641SAndroid Build Coastguard Worker enum QueryType {
586*635a8641SAndroid Build Coastguard Worker QUERY_BOOLEAN_OPERATOR,
587*635a8641SAndroid Build Coastguard Worker QUERY_ARITHMETIC_OPERATOR,
588*635a8641SAndroid Build Coastguard Worker QUERY_EVENT_MEMBER,
589*635a8641SAndroid Build Coastguard Worker QUERY_NUMBER,
590*635a8641SAndroid Build Coastguard Worker QUERY_STRING
591*635a8641SAndroid Build Coastguard Worker };
592*635a8641SAndroid Build Coastguard Worker
593*635a8641SAndroid Build Coastguard Worker // Compare with the given member.
594*635a8641SAndroid Build Coastguard Worker explicit Query(TraceEventMember member);
595*635a8641SAndroid Build Coastguard Worker
596*635a8641SAndroid Build Coastguard Worker // Compare with the given member argument value.
597*635a8641SAndroid Build Coastguard Worker Query(TraceEventMember member, const std::string& arg_name);
598*635a8641SAndroid Build Coastguard Worker
599*635a8641SAndroid Build Coastguard Worker // Compare with the given string.
600*635a8641SAndroid Build Coastguard Worker explicit Query(const std::string& str);
601*635a8641SAndroid Build Coastguard Worker
602*635a8641SAndroid Build Coastguard Worker // Compare with the given number.
603*635a8641SAndroid Build Coastguard Worker explicit Query(double num);
604*635a8641SAndroid Build Coastguard Worker
605*635a8641SAndroid Build Coastguard Worker // Construct a boolean Query that returns (left <binary_op> right).
606*635a8641SAndroid Build Coastguard Worker Query(const Query& left, const Query& right, Operator binary_op);
607*635a8641SAndroid Build Coastguard Worker
608*635a8641SAndroid Build Coastguard Worker // Construct a boolean Query that returns (<binary_op> left).
609*635a8641SAndroid Build Coastguard Worker Query(const Query& left, Operator unary_op);
610*635a8641SAndroid Build Coastguard Worker
611*635a8641SAndroid Build Coastguard Worker // Try to compare left_ against right_ based on operator_.
612*635a8641SAndroid Build Coastguard Worker // If either left or right does not convert to double, false is returned.
613*635a8641SAndroid Build Coastguard Worker // Otherwise, true is returned and |result| is set to the comparison result.
614*635a8641SAndroid Build Coastguard Worker bool CompareAsDouble(const TraceEvent& event, bool* result) const;
615*635a8641SAndroid Build Coastguard Worker
616*635a8641SAndroid Build Coastguard Worker // Try to compare left_ against right_ based on operator_.
617*635a8641SAndroid Build Coastguard Worker // If either left or right does not convert to string, false is returned.
618*635a8641SAndroid Build Coastguard Worker // Otherwise, true is returned and |result| is set to the comparison result.
619*635a8641SAndroid Build Coastguard Worker bool CompareAsString(const TraceEvent& event, bool* result) const;
620*635a8641SAndroid Build Coastguard Worker
621*635a8641SAndroid Build Coastguard Worker // Attempt to convert this Query to a double. On success, true is returned
622*635a8641SAndroid Build Coastguard Worker // and the double value is stored in |num|.
623*635a8641SAndroid Build Coastguard Worker bool GetAsDouble(const TraceEvent& event, double* num) const;
624*635a8641SAndroid Build Coastguard Worker
625*635a8641SAndroid Build Coastguard Worker // Attempt to convert this Query to a string. On success, true is returned
626*635a8641SAndroid Build Coastguard Worker // and the string value is stored in |str|.
627*635a8641SAndroid Build Coastguard Worker bool GetAsString(const TraceEvent& event, std::string* str) const;
628*635a8641SAndroid Build Coastguard Worker
629*635a8641SAndroid Build Coastguard Worker // Evaluate this Query as an arithmetic operator on left_ and right_.
630*635a8641SAndroid Build Coastguard Worker bool EvaluateArithmeticOperator(const TraceEvent& event,
631*635a8641SAndroid Build Coastguard Worker double* num) const;
632*635a8641SAndroid Build Coastguard Worker
633*635a8641SAndroid Build Coastguard Worker // For QUERY_EVENT_MEMBER Query: attempt to get the double value of the Query.
634*635a8641SAndroid Build Coastguard Worker bool GetMemberValueAsDouble(const TraceEvent& event, double* num) const;
635*635a8641SAndroid Build Coastguard Worker
636*635a8641SAndroid Build Coastguard Worker // For QUERY_EVENT_MEMBER Query: attempt to get the string value of the Query.
637*635a8641SAndroid Build Coastguard Worker bool GetMemberValueAsString(const TraceEvent& event, std::string* num) const;
638*635a8641SAndroid Build Coastguard Worker
639*635a8641SAndroid Build Coastguard Worker // Does this Query represent a value?
is_value()640*635a8641SAndroid Build Coastguard Worker bool is_value() const { return type_ != QUERY_BOOLEAN_OPERATOR; }
641*635a8641SAndroid Build Coastguard Worker
is_unary_operator()642*635a8641SAndroid Build Coastguard Worker bool is_unary_operator() const {
643*635a8641SAndroid Build Coastguard Worker return operator_ == OP_NOT || operator_ == OP_NEGATE;
644*635a8641SAndroid Build Coastguard Worker }
645*635a8641SAndroid Build Coastguard Worker
is_comparison_operator()646*635a8641SAndroid Build Coastguard Worker bool is_comparison_operator() const {
647*635a8641SAndroid Build Coastguard Worker return operator_ != OP_INVALID && operator_ < OP_AND;
648*635a8641SAndroid Build Coastguard Worker }
649*635a8641SAndroid Build Coastguard Worker
650*635a8641SAndroid Build Coastguard Worker static const TraceEvent* SelectTargetEvent(const TraceEvent* ev,
651*635a8641SAndroid Build Coastguard Worker TraceEventMember member);
652*635a8641SAndroid Build Coastguard Worker
653*635a8641SAndroid Build Coastguard Worker const Query& left() const;
654*635a8641SAndroid Build Coastguard Worker const Query& right() const;
655*635a8641SAndroid Build Coastguard Worker
656*635a8641SAndroid Build Coastguard Worker private:
657*635a8641SAndroid Build Coastguard Worker QueryType type_;
658*635a8641SAndroid Build Coastguard Worker Operator operator_;
659*635a8641SAndroid Build Coastguard Worker scoped_refptr<QueryNode> left_;
660*635a8641SAndroid Build Coastguard Worker scoped_refptr<QueryNode> right_;
661*635a8641SAndroid Build Coastguard Worker TraceEventMember member_;
662*635a8641SAndroid Build Coastguard Worker double number_;
663*635a8641SAndroid Build Coastguard Worker std::string string_;
664*635a8641SAndroid Build Coastguard Worker bool is_pattern_;
665*635a8641SAndroid Build Coastguard Worker };
666*635a8641SAndroid Build Coastguard Worker
667*635a8641SAndroid Build Coastguard Worker // Implementation detail:
668*635a8641SAndroid Build Coastguard Worker // QueryNode allows Query to store a ref-counted query tree.
669*635a8641SAndroid Build Coastguard Worker class QueryNode : public base::RefCounted<QueryNode> {
670*635a8641SAndroid Build Coastguard Worker public:
671*635a8641SAndroid Build Coastguard Worker explicit QueryNode(const Query& query);
query()672*635a8641SAndroid Build Coastguard Worker const Query& query() const { return query_; }
673*635a8641SAndroid Build Coastguard Worker
674*635a8641SAndroid Build Coastguard Worker private:
675*635a8641SAndroid Build Coastguard Worker friend class base::RefCounted<QueryNode>;
676*635a8641SAndroid Build Coastguard Worker ~QueryNode();
677*635a8641SAndroid Build Coastguard Worker
678*635a8641SAndroid Build Coastguard Worker Query query_;
679*635a8641SAndroid Build Coastguard Worker };
680*635a8641SAndroid Build Coastguard Worker
681*635a8641SAndroid Build Coastguard Worker // TraceAnalyzer helps tests search for trace events.
682*635a8641SAndroid Build Coastguard Worker class TraceAnalyzer {
683*635a8641SAndroid Build Coastguard Worker public:
684*635a8641SAndroid Build Coastguard Worker ~TraceAnalyzer();
685*635a8641SAndroid Build Coastguard Worker
686*635a8641SAndroid Build Coastguard Worker // Use trace events from JSON string generated by tracing API.
687*635a8641SAndroid Build Coastguard Worker // Returns non-NULL if the JSON is successfully parsed.
688*635a8641SAndroid Build Coastguard Worker static TraceAnalyzer* Create(const std::string& json_events)
689*635a8641SAndroid Build Coastguard Worker WARN_UNUSED_RESULT;
690*635a8641SAndroid Build Coastguard Worker
SetIgnoreMetadataEvents(bool ignore)691*635a8641SAndroid Build Coastguard Worker void SetIgnoreMetadataEvents(bool ignore) {
692*635a8641SAndroid Build Coastguard Worker ignore_metadata_events_ = ignore;
693*635a8641SAndroid Build Coastguard Worker }
694*635a8641SAndroid Build Coastguard Worker
695*635a8641SAndroid Build Coastguard Worker // Associate BEGIN and END events with each other. This allows Query(OTHER_*)
696*635a8641SAndroid Build Coastguard Worker // to access the associated event and enables Query(EVENT_DURATION).
697*635a8641SAndroid Build Coastguard Worker // An end event will match the most recent begin event with the same name,
698*635a8641SAndroid Build Coastguard Worker // category, process ID and thread ID. This matches what is shown in
699*635a8641SAndroid Build Coastguard Worker // about:tracing. After association, the BEGIN event will point to the
700*635a8641SAndroid Build Coastguard Worker // matching END event, but the END event will not point to the BEGIN event.
701*635a8641SAndroid Build Coastguard Worker void AssociateBeginEndEvents();
702*635a8641SAndroid Build Coastguard Worker
703*635a8641SAndroid Build Coastguard Worker // Associate ASYNC_BEGIN, ASYNC_STEP and ASYNC_END events with each other.
704*635a8641SAndroid Build Coastguard Worker // An ASYNC_END event will match the most recent ASYNC_BEGIN or ASYNC_STEP
705*635a8641SAndroid Build Coastguard Worker // event with the same name, category, and ID. This creates a singly linked
706*635a8641SAndroid Build Coastguard Worker // list of ASYNC_BEGIN->ASYNC_STEP...->ASYNC_END.
707*635a8641SAndroid Build Coastguard Worker // |match_pid| - If true, will only match async events which are running
708*635a8641SAndroid Build Coastguard Worker // under the same process ID, otherwise will allow linking
709*635a8641SAndroid Build Coastguard Worker // async events from different processes.
710*635a8641SAndroid Build Coastguard Worker void AssociateAsyncBeginEndEvents(bool match_pid = true);
711*635a8641SAndroid Build Coastguard Worker
712*635a8641SAndroid Build Coastguard Worker // AssociateEvents can be used to customize event associations by setting the
713*635a8641SAndroid Build Coastguard Worker // other_event member of TraceEvent. This should be used to associate two
714*635a8641SAndroid Build Coastguard Worker // INSTANT events.
715*635a8641SAndroid Build Coastguard Worker //
716*635a8641SAndroid Build Coastguard Worker // The assumptions are:
717*635a8641SAndroid Build Coastguard Worker // - |first| events occur before |second| events.
718*635a8641SAndroid Build Coastguard Worker // - the closest matching |second| event is the correct match.
719*635a8641SAndroid Build Coastguard Worker //
720*635a8641SAndroid Build Coastguard Worker // |first| - Eligible |first| events match this query.
721*635a8641SAndroid Build Coastguard Worker // |second| - Eligible |second| events match this query.
722*635a8641SAndroid Build Coastguard Worker // |match| - This query is run on the |first| event. The OTHER_* EventMember
723*635a8641SAndroid Build Coastguard Worker // queries will point to an eligible |second| event. The query
724*635a8641SAndroid Build Coastguard Worker // should evaluate to true if the |first|/|second| pair is a match.
725*635a8641SAndroid Build Coastguard Worker //
726*635a8641SAndroid Build Coastguard Worker // When a match is found, the pair will be associated by having the first
727*635a8641SAndroid Build Coastguard Worker // event's other_event member point to the other. AssociateEvents does not
728*635a8641SAndroid Build Coastguard Worker // clear previous associations, so it is possible to associate multiple pairs
729*635a8641SAndroid Build Coastguard Worker // of events by calling AssociateEvents more than once with different queries.
730*635a8641SAndroid Build Coastguard Worker //
731*635a8641SAndroid Build Coastguard Worker // NOTE: AssociateEvents will overwrite existing other_event associations if
732*635a8641SAndroid Build Coastguard Worker // the queries pass for events that already had a previous association.
733*635a8641SAndroid Build Coastguard Worker //
734*635a8641SAndroid Build Coastguard Worker // After calling any Find* method, it is not allowed to call AssociateEvents
735*635a8641SAndroid Build Coastguard Worker // again.
736*635a8641SAndroid Build Coastguard Worker void AssociateEvents(const Query& first,
737*635a8641SAndroid Build Coastguard Worker const Query& second,
738*635a8641SAndroid Build Coastguard Worker const Query& match);
739*635a8641SAndroid Build Coastguard Worker
740*635a8641SAndroid Build Coastguard Worker // For each event, copy its arguments to the other_event argument map. If
741*635a8641SAndroid Build Coastguard Worker // argument name already exists, it will not be overwritten.
742*635a8641SAndroid Build Coastguard Worker void MergeAssociatedEventArgs();
743*635a8641SAndroid Build Coastguard Worker
744*635a8641SAndroid Build Coastguard Worker // Find all events that match query and replace output vector.
745*635a8641SAndroid Build Coastguard Worker size_t FindEvents(const Query& query, TraceEventVector* output);
746*635a8641SAndroid Build Coastguard Worker
747*635a8641SAndroid Build Coastguard Worker // Find first event that matches query or NULL if not found.
748*635a8641SAndroid Build Coastguard Worker const TraceEvent* FindFirstOf(const Query& query);
749*635a8641SAndroid Build Coastguard Worker
750*635a8641SAndroid Build Coastguard Worker // Find last event that matches query or NULL if not found.
751*635a8641SAndroid Build Coastguard Worker const TraceEvent* FindLastOf(const Query& query);
752*635a8641SAndroid Build Coastguard Worker
753*635a8641SAndroid Build Coastguard Worker const std::string& GetThreadName(const TraceEvent::ProcessThreadID& thread);
754*635a8641SAndroid Build Coastguard Worker
755*635a8641SAndroid Build Coastguard Worker private:
756*635a8641SAndroid Build Coastguard Worker TraceAnalyzer();
757*635a8641SAndroid Build Coastguard Worker
758*635a8641SAndroid Build Coastguard Worker bool SetEvents(const std::string& json_events) WARN_UNUSED_RESULT;
759*635a8641SAndroid Build Coastguard Worker
760*635a8641SAndroid Build Coastguard Worker // Read metadata (thread names, etc) from events.
761*635a8641SAndroid Build Coastguard Worker void ParseMetadata();
762*635a8641SAndroid Build Coastguard Worker
763*635a8641SAndroid Build Coastguard Worker std::map<TraceEvent::ProcessThreadID, std::string> thread_names_;
764*635a8641SAndroid Build Coastguard Worker std::vector<TraceEvent> raw_events_;
765*635a8641SAndroid Build Coastguard Worker bool ignore_metadata_events_;
766*635a8641SAndroid Build Coastguard Worker bool allow_association_changes_;
767*635a8641SAndroid Build Coastguard Worker
768*635a8641SAndroid Build Coastguard Worker DISALLOW_COPY_AND_ASSIGN(TraceAnalyzer);
769*635a8641SAndroid Build Coastguard Worker };
770*635a8641SAndroid Build Coastguard Worker
771*635a8641SAndroid Build Coastguard Worker // Utility functions for collecting process-local traces and creating a
772*635a8641SAndroid Build Coastguard Worker // |TraceAnalyzer| from the result. Please see comments in trace_config.h to
773*635a8641SAndroid Build Coastguard Worker // understand how the |category_filter_string| works. Use "*" to enable all
774*635a8641SAndroid Build Coastguard Worker // default categories.
775*635a8641SAndroid Build Coastguard Worker void Start(const std::string& category_filter_string);
776*635a8641SAndroid Build Coastguard Worker std::unique_ptr<TraceAnalyzer> Stop();
777*635a8641SAndroid Build Coastguard Worker
778*635a8641SAndroid Build Coastguard Worker // Utility functions for TraceEventVector.
779*635a8641SAndroid Build Coastguard Worker
780*635a8641SAndroid Build Coastguard Worker struct RateStats {
781*635a8641SAndroid Build Coastguard Worker double min_us;
782*635a8641SAndroid Build Coastguard Worker double max_us;
783*635a8641SAndroid Build Coastguard Worker double mean_us;
784*635a8641SAndroid Build Coastguard Worker double standard_deviation_us;
785*635a8641SAndroid Build Coastguard Worker };
786*635a8641SAndroid Build Coastguard Worker
787*635a8641SAndroid Build Coastguard Worker struct RateStatsOptions {
RateStatsOptionsRateStatsOptions788*635a8641SAndroid Build Coastguard Worker RateStatsOptions() : trim_min(0u), trim_max(0u) {}
789*635a8641SAndroid Build Coastguard Worker // After the times between events are sorted, the number of specified elements
790*635a8641SAndroid Build Coastguard Worker // will be trimmed before calculating the RateStats. This is useful in cases
791*635a8641SAndroid Build Coastguard Worker // where extreme outliers are tolerable and should not skew the overall
792*635a8641SAndroid Build Coastguard Worker // average.
793*635a8641SAndroid Build Coastguard Worker size_t trim_min; // Trim this many minimum times.
794*635a8641SAndroid Build Coastguard Worker size_t trim_max; // Trim this many maximum times.
795*635a8641SAndroid Build Coastguard Worker };
796*635a8641SAndroid Build Coastguard Worker
797*635a8641SAndroid Build Coastguard Worker // Calculate min/max/mean and standard deviation from the times between
798*635a8641SAndroid Build Coastguard Worker // adjacent events.
799*635a8641SAndroid Build Coastguard Worker bool GetRateStats(const TraceEventVector& events,
800*635a8641SAndroid Build Coastguard Worker RateStats* stats,
801*635a8641SAndroid Build Coastguard Worker const RateStatsOptions* options);
802*635a8641SAndroid Build Coastguard Worker
803*635a8641SAndroid Build Coastguard Worker // Starting from |position|, find the first event that matches |query|.
804*635a8641SAndroid Build Coastguard Worker // Returns true if found, false otherwise.
805*635a8641SAndroid Build Coastguard Worker bool FindFirstOf(const TraceEventVector& events,
806*635a8641SAndroid Build Coastguard Worker const Query& query,
807*635a8641SAndroid Build Coastguard Worker size_t position,
808*635a8641SAndroid Build Coastguard Worker size_t* return_index);
809*635a8641SAndroid Build Coastguard Worker
810*635a8641SAndroid Build Coastguard Worker // Starting from |position|, find the last event that matches |query|.
811*635a8641SAndroid Build Coastguard Worker // Returns true if found, false otherwise.
812*635a8641SAndroid Build Coastguard Worker bool FindLastOf(const TraceEventVector& events,
813*635a8641SAndroid Build Coastguard Worker const Query& query,
814*635a8641SAndroid Build Coastguard Worker size_t position,
815*635a8641SAndroid Build Coastguard Worker size_t* return_index);
816*635a8641SAndroid Build Coastguard Worker
817*635a8641SAndroid Build Coastguard Worker // Find the closest events to |position| in time that match |query|.
818*635a8641SAndroid Build Coastguard Worker // return_second_closest may be NULL. Closeness is determined by comparing
819*635a8641SAndroid Build Coastguard Worker // with the event timestamp.
820*635a8641SAndroid Build Coastguard Worker // Returns true if found, false otherwise. If both return parameters are
821*635a8641SAndroid Build Coastguard Worker // requested, both must be found for a successful result.
822*635a8641SAndroid Build Coastguard Worker bool FindClosest(const TraceEventVector& events,
823*635a8641SAndroid Build Coastguard Worker const Query& query,
824*635a8641SAndroid Build Coastguard Worker size_t position,
825*635a8641SAndroid Build Coastguard Worker size_t* return_closest,
826*635a8641SAndroid Build Coastguard Worker size_t* return_second_closest);
827*635a8641SAndroid Build Coastguard Worker
828*635a8641SAndroid Build Coastguard Worker // Count matches, inclusive of |begin_position|, exclusive of |end_position|.
829*635a8641SAndroid Build Coastguard Worker size_t CountMatches(const TraceEventVector& events,
830*635a8641SAndroid Build Coastguard Worker const Query& query,
831*635a8641SAndroid Build Coastguard Worker size_t begin_position,
832*635a8641SAndroid Build Coastguard Worker size_t end_position);
833*635a8641SAndroid Build Coastguard Worker
834*635a8641SAndroid Build Coastguard Worker // Count all matches.
CountMatches(const TraceEventVector & events,const Query & query)835*635a8641SAndroid Build Coastguard Worker static inline size_t CountMatches(const TraceEventVector& events,
836*635a8641SAndroid Build Coastguard Worker const Query& query) {
837*635a8641SAndroid Build Coastguard Worker return CountMatches(events, query, 0u, events.size());
838*635a8641SAndroid Build Coastguard Worker }
839*635a8641SAndroid Build Coastguard Worker
840*635a8641SAndroid Build Coastguard Worker } // namespace trace_analyzer
841*635a8641SAndroid Build Coastguard Worker
842*635a8641SAndroid Build Coastguard Worker #endif // BASE_TEST_TRACE_EVENT_ANALYZER_H_
843