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 
5 #ifndef CHROME_BROWSER_CHROMEOS_ARC_TRACING_ARC_TRACING_EVENT_H_
6 #define CHROME_BROWSER_CHROMEOS_ARC_TRACING_ARC_TRACING_EVENT_H_
7 
8 #include <stddef.h>
9 
10 #include <memory>
11 #include <string>
12 #include <vector>
13 
14 #include "base/macros.h"
15 #include "base/values.h"
16 
17 namespace arc {
18 
19 // |ArcTracingEvent| is a wrapper over |base::DictionaryValue| that is used to
20 // represent trace event in Chrome. |ArcTracingEvent| is hierarchical and
21 // can contain children. Setter methods are used to convert system trace events
22 // that are not dictionary based to the common Chrome format.
23 class ArcTracingEvent {
24  public:
25   enum class Position {
26     kBefore,   // event is before the compared event
27     kInside,   // event is inside the compared event.
28     kAfter,    // event is after the compared event.
29     kOverlap,  // event overlaps with compared event.
30   };
31 
32   explicit ArcTracingEvent(base::Value dictionary);
33   ~ArcTracingEvent();
34 
35   ArcTracingEvent(ArcTracingEvent&&);
36   ArcTracingEvent& operator=(ArcTracingEvent&&);
37 
38   // Gets process id of the event. Returns 0 if not set.
39   int GetPid() const;
40   // Sets process id of the event.
41   void SetPid(int pid);
42 
43   // Gets thread id of the event. Returns 0 if not set.
44   int GetTid() const;
45   // Sets thread id of the event.
46   void SetTid(int tid);
47 
48   // Gets id of the group of events. Returns empty string if not set.
49   std::string GetId() const;
50   // Sets id of the event.
51   void SetId(const std::string& id);
52 
53   // Gets category of the event. Returns empty string if not set.
54   std::string GetCategory() const;
55   // Sets category of the event.
56   void SetCategory(const std::string& category);
57 
58   // Gets name of the event. Returns empty string if not set.
59   std::string GetName() const;
60   // Sets name of the event.
61   void SetName(const std::string& name);
62 
63   // Gets phase of the event. Returns 0 if not set.
64   char GetPhase() const;
65   // Sets phase of the event.
66   void SetPhase(char phase);
67 
68   // Gets timestamp of the start of the event. Return 0 if not set.
69   uint64_t GetTimestamp() const;
70   // Sets timestamp of the start of the event.
71   void SetTimestamp(uint64_t timestamp);
72 
73   // Gets duration of the event.  Return 0 if not set. It is optional for some
74   // events.
75   uint64_t GetDuration() const;
76   // Sets duration of the event.
77   void SetDuration(uint64_t duration);
78 
79   // Gets timestamp of the end of the event.
80   uint64_t GetEndTimestamp() const;
81 
82   // Returns base representation of the event as a |base::DictionaryValue|.
83   const base::DictionaryValue* GetDictionary() const;
84 
85   // Returns set of arguments as a |base::DictionaryValue|.
86   const base::DictionaryValue* GetArgs() const;
87 
88   // Gets argument as string. Return |default_value| if not found.
89   std::string GetArgAsString(const std::string& name,
90                              const std::string& default_value) const;
91 
92   // Gets argument as integer. Returns |default_value| if not found.
93   int GetArgAsInteger(const std::string& name, int default_value) const;
94 
95   // Gets argument as double. Returns |default_value| if not found.
96   double GetArgAsDouble(const std::string& name, double default_value) const;
97 
98   // Classifies the position of another event relative to the current event.
99   Position ClassifyPositionOf(const ArcTracingEvent& other) const;
100 
101   // Recursively adds child trace event. If child event is not inside the
102   // current event than child event is not added and false is returned.
103   // Based on building constraints, child element can be appended to the end
104   // of the list of child events or to the last child event.
105   bool AppendChild(std::unique_ptr<ArcTracingEvent> child);
106 
107   // Validates that event contains correct information.
108   bool Validate() const;
109 
110   // Returns string representation of this event.
111   std::string ToString() const;
112 
113   // Dumps this event and its children to |stream|. |prefix| is used for
114   // formatting.
115   void Dump(const std::string& prefix, std::ostream& stream) const;
116 
children()117   const std::vector<std::unique_ptr<ArcTracingEvent>>& children() const {
118     return children_;
119   }
120 
121  private:
122   std::vector<std::unique_ptr<ArcTracingEvent>> children_;
123   base::Value dictionary_;
124 
125   DISALLOW_COPY_AND_ASSIGN(ArcTracingEvent);
126 };
127 
128 }  // namespace arc
129 
130 #endif  // CHROME_BROWSER_CHROMEOS_ARC_TRACING_ARC_TRACING_EVENT_H_
131