OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/test/trace_event_analyzer.h" | 5 #include "base/test/trace_event_analyzer.h" |
6 | 6 |
7 #include <math.h> | 7 #include <math.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <set> | 10 #include <set> |
11 | 11 |
12 #include "base/json/json_reader.h" | 12 #include "base/json/json_reader.h" |
13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
14 #include "base/strings/pattern.h" | 14 #include "base/strings/pattern.h" |
15 #include "base/values.h" | 15 #include "base/values.h" |
16 | 16 |
17 namespace trace_analyzer { | 17 namespace trace_analyzer { |
18 | 18 |
19 // TraceEvent | 19 // TraceEvent |
20 | 20 |
21 TraceEvent::TraceEvent() | 21 TraceEvent::TraceEvent() |
22 : thread(0, 0), | 22 : thread(0, 0), |
23 timestamp(0), | 23 timestamp(0), |
24 duration(0), | 24 duration(0), |
25 phase(TRACE_EVENT_PHASE_BEGIN), | 25 phase(TRACE_EVENT_PHASE_BEGIN), |
26 other_event(NULL) { | 26 other_event(NULL) { |
27 } | 27 } |
28 | 28 |
29 TraceEvent::TraceEvent(const TraceEvent& other) = default; | 29 TraceEvent::TraceEvent(TraceEvent&& other) = default; |
30 | 30 |
31 TraceEvent::~TraceEvent() { | 31 TraceEvent::~TraceEvent() { |
32 } | 32 } |
33 | 33 |
| 34 TraceEvent& TraceEvent::operator=(TraceEvent&& rhs) = default; |
| 35 |
34 bool TraceEvent::SetFromJSON(const base::Value* event_value) { | 36 bool TraceEvent::SetFromJSON(const base::Value* event_value) { |
35 if (event_value->GetType() != base::Value::TYPE_DICTIONARY) { | 37 if (event_value->GetType() != base::Value::TYPE_DICTIONARY) { |
36 LOG(ERROR) << "Value must be TYPE_DICTIONARY"; | 38 LOG(ERROR) << "Value must be TYPE_DICTIONARY"; |
37 return false; | 39 return false; |
38 } | 40 } |
39 const base::DictionaryValue* dictionary = | 41 const base::DictionaryValue* dictionary = |
40 static_cast<const base::DictionaryValue*>(event_value); | 42 static_cast<const base::DictionaryValue*>(event_value); |
41 | 43 |
42 std::string phase_str; | 44 std::string phase_str; |
43 const base::DictionaryValue* args = NULL; | 45 const base::DictionaryValue* args = NULL; |
44 | 46 |
45 if (!dictionary->GetString("ph", &phase_str)) { | 47 if (!dictionary->GetString("ph", &phase_str)) { |
46 LOG(ERROR) << "ph is missing from TraceEvent JSON"; | 48 LOG(ERROR) << "ph is missing from TraceEvent JSON"; |
47 return false; | 49 return false; |
48 } | 50 } |
49 | 51 |
50 phase = *phase_str.data(); | 52 phase = *phase_str.data(); |
51 | 53 |
52 bool may_have_duration = (phase == TRACE_EVENT_PHASE_COMPLETE); | 54 bool may_have_duration = (phase == TRACE_EVENT_PHASE_COMPLETE); |
53 bool require_origin = (phase != TRACE_EVENT_PHASE_METADATA); | 55 bool require_origin = (phase != TRACE_EVENT_PHASE_METADATA); |
54 bool require_id = (phase == TRACE_EVENT_PHASE_ASYNC_BEGIN || | 56 bool require_id = (phase == TRACE_EVENT_PHASE_ASYNC_BEGIN || |
55 phase == TRACE_EVENT_PHASE_ASYNC_STEP_INTO || | 57 phase == TRACE_EVENT_PHASE_ASYNC_STEP_INTO || |
56 phase == TRACE_EVENT_PHASE_ASYNC_STEP_PAST || | 58 phase == TRACE_EVENT_PHASE_ASYNC_STEP_PAST || |
| 59 phase == TRACE_EVENT_PHASE_MEMORY_DUMP || |
| 60 phase == TRACE_EVENT_PHASE_ENTER_CONTEXT || |
| 61 phase == TRACE_EVENT_PHASE_LEAVE_CONTEXT || |
| 62 phase == TRACE_EVENT_PHASE_CREATE_OBJECT || |
| 63 phase == TRACE_EVENT_PHASE_DELETE_OBJECT || |
| 64 phase == TRACE_EVENT_PHASE_SNAPSHOT_OBJECT || |
57 phase == TRACE_EVENT_PHASE_ASYNC_END); | 65 phase == TRACE_EVENT_PHASE_ASYNC_END); |
58 | 66 |
59 if (require_origin && !dictionary->GetInteger("pid", &thread.process_id)) { | 67 if (require_origin && !dictionary->GetInteger("pid", &thread.process_id)) { |
60 LOG(ERROR) << "pid is missing from TraceEvent JSON"; | 68 LOG(ERROR) << "pid is missing from TraceEvent JSON"; |
61 return false; | 69 return false; |
62 } | 70 } |
63 if (require_origin && !dictionary->GetInteger("tid", &thread.thread_id)) { | 71 if (require_origin && !dictionary->GetInteger("tid", &thread.thread_id)) { |
64 LOG(ERROR) << "tid is missing from TraceEvent JSON"; | 72 LOG(ERROR) << "tid is missing from TraceEvent JSON"; |
65 return false; | 73 return false; |
66 } | 74 } |
(...skipping 29 matching lines...) Expand all Loading... |
96 int int_num = 0; | 104 int int_num = 0; |
97 double double_num = 0.0; | 105 double double_num = 0.0; |
98 if (it.value().GetAsString(&str)) { | 106 if (it.value().GetAsString(&str)) { |
99 arg_strings[it.key()] = str; | 107 arg_strings[it.key()] = str; |
100 } else if (it.value().GetAsInteger(&int_num)) { | 108 } else if (it.value().GetAsInteger(&int_num)) { |
101 arg_numbers[it.key()] = static_cast<double>(int_num); | 109 arg_numbers[it.key()] = static_cast<double>(int_num); |
102 } else if (it.value().GetAsBoolean(&boolean)) { | 110 } else if (it.value().GetAsBoolean(&boolean)) { |
103 arg_numbers[it.key()] = static_cast<double>(boolean ? 1 : 0); | 111 arg_numbers[it.key()] = static_cast<double>(boolean ? 1 : 0); |
104 } else if (it.value().GetAsDouble(&double_num)) { | 112 } else if (it.value().GetAsDouble(&double_num)) { |
105 arg_numbers[it.key()] = double_num; | 113 arg_numbers[it.key()] = double_num; |
106 } else { | |
107 LOG(WARNING) << "Value type of argument is not supported: " << | |
108 static_cast<int>(it.value().GetType()); | |
109 continue; // Skip non-supported arguments. | |
110 } | 114 } |
| 115 // Record all arguments as values. |
| 116 arg_values[it.key()] = it.value().CreateDeepCopy(); |
111 } | 117 } |
112 | 118 |
113 return true; | 119 return true; |
114 } | 120 } |
115 | 121 |
116 double TraceEvent::GetAbsTimeToOtherEvent() const { | 122 double TraceEvent::GetAbsTimeToOtherEvent() const { |
117 return fabs(other_event->timestamp - timestamp); | 123 return fabs(other_event->timestamp - timestamp); |
118 } | 124 } |
119 | 125 |
120 bool TraceEvent::GetArgAsString(const std::string& name, | 126 bool TraceEvent::GetArgAsString(const std::string& name, |
121 std::string* arg) const { | 127 std::string* arg) const { |
122 std::map<std::string, std::string>::const_iterator i = arg_strings.find(name); | 128 const auto it = arg_strings.find(name); |
123 if (i != arg_strings.end()) { | 129 if (it != arg_strings.end()) { |
124 *arg = i->second; | 130 *arg = it->second; |
125 return true; | 131 return true; |
126 } | 132 } |
127 return false; | 133 return false; |
128 } | 134 } |
129 | 135 |
130 bool TraceEvent::GetArgAsNumber(const std::string& name, | 136 bool TraceEvent::GetArgAsNumber(const std::string& name, |
131 double* arg) const { | 137 double* arg) const { |
132 std::map<std::string, double>::const_iterator i = arg_numbers.find(name); | 138 const auto it = arg_numbers.find(name); |
133 if (i != arg_numbers.end()) { | 139 if (it != arg_numbers.end()) { |
134 *arg = i->second; | 140 *arg = it->second; |
135 return true; | 141 return true; |
136 } | 142 } |
137 return false; | 143 return false; |
| 144 } |
| 145 |
| 146 bool TraceEvent::GetArgAsValue(const std::string& name, |
| 147 scoped_ptr<base::Value>* arg) const { |
| 148 const auto it = arg_values.find(name); |
| 149 if (it != arg_values.end()) { |
| 150 *arg = it->second->CreateDeepCopy(); |
| 151 return true; |
| 152 } |
| 153 return false; |
138 } | 154 } |
139 | 155 |
140 bool TraceEvent::HasStringArg(const std::string& name) const { | 156 bool TraceEvent::HasStringArg(const std::string& name) const { |
141 return (arg_strings.find(name) != arg_strings.end()); | 157 return (arg_strings.find(name) != arg_strings.end()); |
142 } | 158 } |
143 | 159 |
144 bool TraceEvent::HasNumberArg(const std::string& name) const { | 160 bool TraceEvent::HasNumberArg(const std::string& name) const { |
145 return (arg_numbers.find(name) != arg_numbers.end()); | 161 return (arg_numbers.find(name) != arg_numbers.end()); |
146 } | 162 } |
147 | 163 |
| 164 bool TraceEvent::HasArg(const std::string& name) const { |
| 165 return (arg_values.find(name) != arg_values.end()); |
| 166 } |
| 167 |
148 std::string TraceEvent::GetKnownArgAsString(const std::string& name) const { | 168 std::string TraceEvent::GetKnownArgAsString(const std::string& name) const { |
149 std::string arg_string; | 169 std::string arg_string; |
150 bool result = GetArgAsString(name, &arg_string); | 170 bool result = GetArgAsString(name, &arg_string); |
151 DCHECK(result); | 171 DCHECK(result); |
152 return arg_string; | 172 return arg_string; |
153 } | 173 } |
154 | 174 |
155 double TraceEvent::GetKnownArgAsDouble(const std::string& name) const { | 175 double TraceEvent::GetKnownArgAsDouble(const std::string& name) const { |
156 double arg_double = 0; | 176 double arg_double = 0; |
157 bool result = GetArgAsNumber(name, &arg_double); | 177 bool result = GetArgAsNumber(name, &arg_double); |
158 DCHECK(result); | 178 DCHECK(result); |
159 return arg_double; | 179 return arg_double; |
160 } | 180 } |
161 | 181 |
162 int TraceEvent::GetKnownArgAsInt(const std::string& name) const { | 182 int TraceEvent::GetKnownArgAsInt(const std::string& name) const { |
163 double arg_double = 0; | 183 double arg_double = 0; |
164 bool result = GetArgAsNumber(name, &arg_double); | 184 bool result = GetArgAsNumber(name, &arg_double); |
165 DCHECK(result); | 185 DCHECK(result); |
166 return static_cast<int>(arg_double); | 186 return static_cast<int>(arg_double); |
167 } | 187 } |
168 | 188 |
169 bool TraceEvent::GetKnownArgAsBool(const std::string& name) const { | 189 bool TraceEvent::GetKnownArgAsBool(const std::string& name) const { |
170 double arg_double = 0; | 190 double arg_double = 0; |
171 bool result = GetArgAsNumber(name, &arg_double); | 191 bool result = GetArgAsNumber(name, &arg_double); |
172 DCHECK(result); | 192 DCHECK(result); |
173 return (arg_double != 0.0); | 193 return (arg_double != 0.0); |
174 } | 194 } |
175 | 195 |
| 196 scoped_ptr<base::Value> TraceEvent::GetKnownArgAsValue( |
| 197 const std::string& name) const { |
| 198 scoped_ptr<base::Value> arg_value; |
| 199 bool result = GetArgAsValue(name, &arg_value); |
| 200 DCHECK(result); |
| 201 return arg_value; |
| 202 } |
| 203 |
176 // QueryNode | 204 // QueryNode |
177 | 205 |
178 QueryNode::QueryNode(const Query& query) : query_(query) { | 206 QueryNode::QueryNode(const Query& query) : query_(query) { |
179 } | 207 } |
180 | 208 |
181 QueryNode::~QueryNode() { | 209 QueryNode::~QueryNode() { |
182 } | 210 } |
183 | 211 |
184 // Query | 212 // Query |
185 | 213 |
(...skipping 469 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
655 | 683 |
656 base::ListValue* root_list = NULL; | 684 base::ListValue* root_list = NULL; |
657 if (!root.get() || !root->GetAsList(&root_list)) | 685 if (!root.get() || !root->GetAsList(&root_list)) |
658 return false; | 686 return false; |
659 | 687 |
660 for (size_t i = 0; i < root_list->GetSize(); ++i) { | 688 for (size_t i = 0; i < root_list->GetSize(); ++i) { |
661 base::Value* item = NULL; | 689 base::Value* item = NULL; |
662 if (root_list->Get(i, &item)) { | 690 if (root_list->Get(i, &item)) { |
663 TraceEvent event; | 691 TraceEvent event; |
664 if (event.SetFromJSON(item)) | 692 if (event.SetFromJSON(item)) |
665 output->push_back(event); | 693 output->push_back(std::move(event)); |
666 else | 694 else |
667 return false; | 695 return false; |
668 } | 696 } |
669 } | 697 } |
670 | 698 |
671 return true; | 699 return true; |
672 } | 700 } |
673 | 701 |
674 } // namespace | 702 } // namespace |
675 | 703 |
(...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
962 end_position = (end_position < events.size()) ? end_position : events.size(); | 990 end_position = (end_position < events.size()) ? end_position : events.size(); |
963 size_t count = 0u; | 991 size_t count = 0u; |
964 for (size_t i = begin_position; i < end_position; ++i) { | 992 for (size_t i = begin_position; i < end_position; ++i) { |
965 if (query.Evaluate(*events.at(i))) | 993 if (query.Evaluate(*events.at(i))) |
966 ++count; | 994 ++count; |
967 } | 995 } |
968 return count; | 996 return count; |
969 } | 997 } |
970 | 998 |
971 } // namespace trace_analyzer | 999 } // namespace trace_analyzer |
OLD | NEW |