| 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(const TraceEvent& other) { |
| 30 *this = other; |
| 31 } |
| 30 | 32 |
| 31 TraceEvent::~TraceEvent() { | 33 TraceEvent::~TraceEvent() { |
| 32 } | 34 } |
| 33 | 35 |
| 36 void TraceEvent::operator=(const TraceEvent& rhs) { |
| 37 thread = rhs.thread; |
| 38 timestamp = rhs.timestamp; |
| 39 phase = rhs.phase; |
| 40 category = rhs.category; |
| 41 name = rhs.name; |
| 42 id = rhs.id; |
| 43 arg_numbers = rhs.arg_numbers; |
| 44 arg_strings = rhs.arg_strings; |
| 45 for (auto it = rhs.arg_values.cbegin(); it != rhs.arg_values.cend(); ++it) { |
| 46 arg_values[it->first] = it->second->CreateDeepCopy(); |
| 47 } |
| 48 } |
| 49 |
| 34 bool TraceEvent::SetFromJSON(const base::Value* event_value) { | 50 bool TraceEvent::SetFromJSON(const base::Value* event_value) { |
| 35 if (event_value->GetType() != base::Value::TYPE_DICTIONARY) { | 51 if (event_value->GetType() != base::Value::TYPE_DICTIONARY) { |
| 36 LOG(ERROR) << "Value must be TYPE_DICTIONARY"; | 52 LOG(ERROR) << "Value must be TYPE_DICTIONARY"; |
| 37 return false; | 53 return false; |
| 38 } | 54 } |
| 39 const base::DictionaryValue* dictionary = | 55 const base::DictionaryValue* dictionary = |
| 40 static_cast<const base::DictionaryValue*>(event_value); | 56 static_cast<const base::DictionaryValue*>(event_value); |
| 41 | 57 |
| 42 std::string phase_str; | 58 std::string phase_str; |
| 43 const base::DictionaryValue* args = NULL; | 59 const base::DictionaryValue* args = NULL; |
| 44 | 60 |
| 45 if (!dictionary->GetString("ph", &phase_str)) { | 61 if (!dictionary->GetString("ph", &phase_str)) { |
| 46 LOG(ERROR) << "ph is missing from TraceEvent JSON"; | 62 LOG(ERROR) << "ph is missing from TraceEvent JSON"; |
| 47 return false; | 63 return false; |
| 48 } | 64 } |
| 49 | 65 |
| 50 phase = *phase_str.data(); | 66 phase = *phase_str.data(); |
| 51 | 67 |
| 52 bool may_have_duration = (phase == TRACE_EVENT_PHASE_COMPLETE); | 68 bool may_have_duration = (phase == TRACE_EVENT_PHASE_COMPLETE); |
| 53 bool require_origin = (phase != TRACE_EVENT_PHASE_METADATA); | 69 bool require_origin = (phase != TRACE_EVENT_PHASE_METADATA); |
| 54 bool require_id = (phase == TRACE_EVENT_PHASE_ASYNC_BEGIN || | 70 bool require_id = (phase == TRACE_EVENT_PHASE_ASYNC_BEGIN || |
| 55 phase == TRACE_EVENT_PHASE_ASYNC_STEP_INTO || | 71 phase == TRACE_EVENT_PHASE_ASYNC_STEP_INTO || |
| 56 phase == TRACE_EVENT_PHASE_ASYNC_STEP_PAST || | 72 phase == TRACE_EVENT_PHASE_ASYNC_STEP_PAST || |
| 73 phase == TRACE_EVENT_PHASE_MEMORY_DUMP || |
| 74 phase == TRACE_EVENT_PHASE_ENTER_CONTEXT || |
| 75 phase == TRACE_EVENT_PHASE_LEAVE_CONTEXT || |
| 76 phase == TRACE_EVENT_PHASE_CREATE_OBJECT || |
| 77 phase == TRACE_EVENT_PHASE_DELETE_OBJECT || |
| 78 phase == TRACE_EVENT_PHASE_SNAPSHOT_OBJECT || |
| 57 phase == TRACE_EVENT_PHASE_ASYNC_END); | 79 phase == TRACE_EVENT_PHASE_ASYNC_END); |
| 58 | 80 |
| 59 if (require_origin && !dictionary->GetInteger("pid", &thread.process_id)) { | 81 if (require_origin && !dictionary->GetInteger("pid", &thread.process_id)) { |
| 60 LOG(ERROR) << "pid is missing from TraceEvent JSON"; | 82 LOG(ERROR) << "pid is missing from TraceEvent JSON"; |
| 61 return false; | 83 return false; |
| 62 } | 84 } |
| 63 if (require_origin && !dictionary->GetInteger("tid", &thread.thread_id)) { | 85 if (require_origin && !dictionary->GetInteger("tid", &thread.thread_id)) { |
| 64 LOG(ERROR) << "tid is missing from TraceEvent JSON"; | 86 LOG(ERROR) << "tid is missing from TraceEvent JSON"; |
| 65 return false; | 87 return false; |
| 66 } | 88 } |
| 67 if (require_origin && !dictionary->GetDouble("ts", ×tamp)) { | 89 if (require_origin && !dictionary->GetDouble("ts", ×tamp)) { |
| 68 LOG(ERROR) << "ts is missing from TraceEvent JSON"; | 90 LOG(ERROR) << "ts is missing from TraceEvent JSON"; |
| 69 return false; | 91 return false; |
| 70 } | 92 } |
| 71 if (may_have_duration) { | 93 if (may_have_duration) { |
| 72 dictionary->GetDouble("dur", &duration); | 94 dictionary->GetDouble("dur", &duration); |
| 73 } | 95 } |
| 74 if (!dictionary->GetString("cat", &category)) { | 96 if (!dictionary->GetString("cat", &category)) { |
| 75 LOG(ERROR) << "cat is missing from TraceEvent JSON"; | 97 LOG(ERROR) << "cat is missing from TraceEvent JSON"; |
| 76 return false; | 98 return false; |
| 77 } | 99 } |
| 78 if (!dictionary->GetString("name", &name)) { | 100 if (!dictionary->GetString("name", &name)) { |
| 79 LOG(ERROR) << "name is missing from TraceEvent JSON"; | 101 LOG(ERROR) << "name is missing from TraceEvent JSON"; |
| 80 return false; | 102 return false; |
| 81 } | 103 } |
| 82 if (!dictionary->GetDictionary("args", &args)) { | 104 if (!dictionary->GetDictionary("args", &args)) { |
| 83 LOG(ERROR) << "args is missing from TraceEvent JSON"; | 105 LOG(ERROR) << "args is missing from TraceEvent JSON"; |
| 84 return false; | 106 return false; |
| 85 } | 107 } |
| 108 |
| 86 if (require_id && !dictionary->GetString("id", &id)) { | 109 if (require_id && !dictionary->GetString("id", &id)) { |
| 87 LOG(ERROR) << "id is missing from ASYNC_BEGIN/ASYNC_END TraceEvent JSON"; | 110 LOG(ERROR) << "id is missing from ASYNC_BEGIN/ASYNC_END TraceEvent JSON"; |
| 88 return false; | 111 return false; |
| 89 } | 112 } |
| 90 | 113 |
| 91 // For each argument, copy the type and create a trace_analyzer::TraceValue. | 114 // For each argument, copy the type and create a trace_analyzer::TraceValue. |
| 92 for (base::DictionaryValue::Iterator it(*args); !it.IsAtEnd(); | 115 for (base::DictionaryValue::Iterator it(*args); !it.IsAtEnd(); |
| 93 it.Advance()) { | 116 it.Advance()) { |
| 94 std::string str; | 117 std::string str; |
| 95 bool boolean = false; | 118 bool boolean = false; |
| 96 int int_num = 0; | 119 int int_num = 0; |
| 97 double double_num = 0.0; | 120 double double_num = 0.0; |
| 98 if (it.value().GetAsString(&str)) { | 121 if (it.value().GetAsString(&str)) { |
| 99 arg_strings[it.key()] = str; | 122 arg_strings[it.key()] = str; |
| 100 } else if (it.value().GetAsInteger(&int_num)) { | 123 } else if (it.value().GetAsInteger(&int_num)) { |
| 101 arg_numbers[it.key()] = static_cast<double>(int_num); | 124 arg_numbers[it.key()] = static_cast<double>(int_num); |
| 102 } else if (it.value().GetAsBoolean(&boolean)) { | 125 } else if (it.value().GetAsBoolean(&boolean)) { |
| 103 arg_numbers[it.key()] = static_cast<double>(boolean ? 1 : 0); | 126 arg_numbers[it.key()] = static_cast<double>(boolean ? 1 : 0); |
| 104 } else if (it.value().GetAsDouble(&double_num)) { | 127 } else if (it.value().GetAsDouble(&double_num)) { |
| 105 arg_numbers[it.key()] = double_num; | 128 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 } | 129 } |
| 130 // Record all arguments as values. |
| 131 arg_values[it.key()] = it.value().CreateDeepCopy(); |
| 111 } | 132 } |
| 112 | 133 |
| 113 return true; | 134 return true; |
| 114 } | 135 } |
| 115 | 136 |
| 116 double TraceEvent::GetAbsTimeToOtherEvent() const { | 137 double TraceEvent::GetAbsTimeToOtherEvent() const { |
| 117 return fabs(other_event->timestamp - timestamp); | 138 return fabs(other_event->timestamp - timestamp); |
| 118 } | 139 } |
| 119 | 140 |
| 120 bool TraceEvent::GetArgAsString(const std::string& name, | 141 bool TraceEvent::GetArgAsString(const std::string& name, |
| 121 std::string* arg) const { | 142 std::string* arg) const { |
| 122 std::map<std::string, std::string>::const_iterator i = arg_strings.find(name); | 143 std::map<std::string, std::string>::const_iterator i = arg_strings.find(name); |
| 123 if (i != arg_strings.end()) { | 144 if (i != arg_strings.end()) { |
| 124 *arg = i->second; | 145 *arg = i->second; |
| 125 return true; | 146 return true; |
| 126 } | 147 } |
| 127 return false; | 148 return false; |
| 128 } | 149 } |
| 129 | 150 |
| 130 bool TraceEvent::GetArgAsNumber(const std::string& name, | 151 bool TraceEvent::GetArgAsNumber(const std::string& name, |
| 131 double* arg) const { | 152 double* arg) const { |
| 132 std::map<std::string, double>::const_iterator i = arg_numbers.find(name); | 153 std::map<std::string, double>::const_iterator i = arg_numbers.find(name); |
| 133 if (i != arg_numbers.end()) { | 154 if (i != arg_numbers.end()) { |
| 134 *arg = i->second; | 155 *arg = i->second; |
| 135 return true; | 156 return true; |
| 136 } | 157 } |
| 137 return false; | 158 return false; |
| 138 } | 159 } |
| 139 | 160 |
| 161 bool TraceEvent::GetArgAsValue(const std::string& name, |
| 162 scoped_ptr<base::Value>* arg) const { |
| 163 std::map<std::string, scoped_ptr<base::Value>>::const_iterator i = |
| 164 arg_values.find(name); |
| 165 if (i != arg_values.end()) { |
| 166 *arg = i->second->CreateDeepCopy(); |
| 167 return true; |
| 168 } |
| 169 return false; |
| 170 } |
| 171 |
| 140 bool TraceEvent::HasStringArg(const std::string& name) const { | 172 bool TraceEvent::HasStringArg(const std::string& name) const { |
| 141 return (arg_strings.find(name) != arg_strings.end()); | 173 return (arg_strings.find(name) != arg_strings.end()); |
| 142 } | 174 } |
| 143 | 175 |
| 144 bool TraceEvent::HasNumberArg(const std::string& name) const { | 176 bool TraceEvent::HasNumberArg(const std::string& name) const { |
| 145 return (arg_numbers.find(name) != arg_numbers.end()); | 177 return (arg_numbers.find(name) != arg_numbers.end()); |
| 146 } | 178 } |
| 147 | 179 |
| 180 bool TraceEvent::HasArg(const std::string& name) const { |
| 181 return (arg_values.find(name) != arg_values.end()); |
| 182 } |
| 183 |
| 148 std::string TraceEvent::GetKnownArgAsString(const std::string& name) const { | 184 std::string TraceEvent::GetKnownArgAsString(const std::string& name) const { |
| 149 std::string arg_string; | 185 std::string arg_string; |
| 150 bool result = GetArgAsString(name, &arg_string); | 186 bool result = GetArgAsString(name, &arg_string); |
| 151 DCHECK(result); | 187 DCHECK(result); |
| 152 return arg_string; | 188 return arg_string; |
| 153 } | 189 } |
| 154 | 190 |
| 155 double TraceEvent::GetKnownArgAsDouble(const std::string& name) const { | 191 double TraceEvent::GetKnownArgAsDouble(const std::string& name) const { |
| 156 double arg_double = 0; | 192 double arg_double = 0; |
| 157 bool result = GetArgAsNumber(name, &arg_double); | 193 bool result = GetArgAsNumber(name, &arg_double); |
| 158 DCHECK(result); | 194 DCHECK(result); |
| 159 return arg_double; | 195 return arg_double; |
| 160 } | 196 } |
| 161 | 197 |
| 162 int TraceEvent::GetKnownArgAsInt(const std::string& name) const { | 198 int TraceEvent::GetKnownArgAsInt(const std::string& name) const { |
| 163 double arg_double = 0; | 199 double arg_double = 0; |
| 164 bool result = GetArgAsNumber(name, &arg_double); | 200 bool result = GetArgAsNumber(name, &arg_double); |
| 165 DCHECK(result); | 201 DCHECK(result); |
| 166 return static_cast<int>(arg_double); | 202 return static_cast<int>(arg_double); |
| 167 } | 203 } |
| 168 | 204 |
| 169 bool TraceEvent::GetKnownArgAsBool(const std::string& name) const { | 205 bool TraceEvent::GetKnownArgAsBool(const std::string& name) const { |
| 170 double arg_double = 0; | 206 double arg_double = 0; |
| 171 bool result = GetArgAsNumber(name, &arg_double); | 207 bool result = GetArgAsNumber(name, &arg_double); |
| 172 DCHECK(result); | 208 DCHECK(result); |
| 173 return (arg_double != 0.0); | 209 return (arg_double != 0.0); |
| 174 } | 210 } |
| 175 | 211 |
| 212 scoped_ptr<base::Value> TraceEvent::GetKnownArgAsValue( |
| 213 const std::string& name) const { |
| 214 scoped_ptr<base::Value> arg_value; |
| 215 bool result = GetArgAsValue(name, &arg_value); |
| 216 DCHECK(result); |
| 217 return arg_value; |
| 218 } |
| 219 |
| 176 // QueryNode | 220 // QueryNode |
| 177 | 221 |
| 178 QueryNode::QueryNode(const Query& query) : query_(query) { | 222 QueryNode::QueryNode(const Query& query) : query_(query) { |
| 179 } | 223 } |
| 180 | 224 |
| 181 QueryNode::~QueryNode() { | 225 QueryNode::~QueryNode() { |
| 182 } | 226 } |
| 183 | 227 |
| 184 // Query | 228 // Query |
| 185 | 229 |
| (...skipping 776 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 962 end_position = (end_position < events.size()) ? end_position : events.size(); | 1006 end_position = (end_position < events.size()) ? end_position : events.size(); |
| 963 size_t count = 0u; | 1007 size_t count = 0u; |
| 964 for (size_t i = begin_position; i < end_position; ++i) { | 1008 for (size_t i = begin_position; i < end_position; ++i) { |
| 965 if (query.Evaluate(*events.at(i))) | 1009 if (query.Evaluate(*events.at(i))) |
| 966 ++count; | 1010 ++count; |
| 967 } | 1011 } |
| 968 return count; | 1012 return count; |
| 969 } | 1013 } |
| 970 | 1014 |
| 971 } // namespace trace_analyzer | 1015 } // namespace trace_analyzer |
| OLD | NEW |