Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "apps/benchmark/event.h" | 5 #include "apps/benchmark/event.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <stack> | 8 #include <stack> |
| 9 | 9 |
| 10 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
| 11 #include "base/macros.h" | 11 #include "base/macros.h" |
| 12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/strings/string_number_conversions.h" | |
| 13 #include "base/values.h" | 14 #include "base/values.h" |
| 14 | 15 |
| 15 namespace benchmark { | 16 namespace benchmark { |
| 16 | 17 |
| 17 Event::Event() {} | 18 Event::Event() {} |
| 18 | 19 |
| 19 Event::Event(EventType type, | 20 Event::Event(EventType type, |
| 20 std::string name, | 21 std::string name, |
| 21 std::string categories, | 22 std::string categories, |
| 22 base::TimeTicks timestamp, | 23 base::TimeTicks timestamp, |
| 23 base::TimeDelta duration) | 24 base::TimeDelta duration) |
| 24 : type(type), | 25 : type(type), |
| 25 name(name), | 26 name(name), |
| 26 categories(categories), | 27 categories(categories), |
| 27 timestamp(timestamp), | 28 timestamp(timestamp), |
| 28 duration(duration) {} | 29 duration(duration) {} |
| 29 | 30 |
| 30 Event::~Event() {} | 31 Event::~Event() {} |
| 31 | 32 |
| 32 namespace { | 33 namespace { |
| 33 // ID uniquely identifying an asynchronous event stack. | 34 // ID uniquely identifying an asynchronous event stack. |
| 34 struct AsyncEventStackId { | 35 struct AsyncEventStackId { |
| 35 int id; | 36 std::string id; |
| 36 std::string cat; | 37 std::string cat; |
| 37 }; | 38 }; |
| 38 | 39 |
| 39 bool operator<(const AsyncEventStackId& t, const AsyncEventStackId& o) { | 40 bool operator<(const AsyncEventStackId& t, const AsyncEventStackId& o) { |
| 40 return t.id < o.id || (t.id == o.id && t.cat < o.cat); | 41 return t.id < o.id || (t.id == o.id && t.cat < o.cat); |
| 41 } | 42 } |
| 42 | 43 |
| 43 // ID uniquely identifying a duration event stack. | 44 // ID uniquely identifying a duration event stack. |
| 44 typedef int DurationEventStackId; | 45 typedef std::string DurationEventStackId; |
| 45 | 46 |
| 46 // Makes a unique id for a duration event stack. | 47 bool ExtractKeyAsString(base::DictionaryValue* event_dict, |
| 47 bool GetDurationEventStackId(base::DictionaryValue* event_dict, | 48 const std::string& key, |
| 48 DurationEventStackId* durationId) { | 49 std::string* output) { |
| 49 if (!event_dict->GetInteger("tid", durationId)) { | 50 const base::Value* value; |
| 50 LOG(ERROR) << "Incorrect trace event (missing tid)."; | 51 if (!event_dict->Get(key, &value)) { |
| 52 LOG(ERROR) << "Incorrect trace event (missing " + key + "): " | |
| 53 << *event_dict; | |
| 54 return false; | |
| 55 } | |
| 56 | |
| 57 if (value->IsType(base::Value::TYPE_INTEGER)) { | |
| 58 int id_int; | |
| 59 // We already verified the type, so it should be an integer. | |
| 60 DCHECK(value->GetAsInteger(&id_int)); | |
| 61 *output = base::IntToString(id_int); | |
| 62 } else if (value->IsType(base::Value::TYPE_STRING)) { | |
| 63 DCHECK(value->GetAsString(output)); | |
| 64 } else { | |
| 65 LOG(ERROR) << "Incorrect trace event (" + key + | |
| 66 " not a string or integer): " | |
| 67 << *event_dict; | |
| 51 return false; | 68 return false; |
| 52 } | 69 } |
| 53 return true; | 70 return true; |
| 54 } | 71 } |
| 55 | 72 |
| 73 // Makes a unique id for a duration event stack. | |
| 74 bool GetDurationEventStackId(base::DictionaryValue* event_dict, | |
| 75 DurationEventStackId* durationId) { | |
| 76 return ExtractKeyAsString(event_dict, "tid", durationId); | |
| 77 } | |
| 78 | |
| 56 // Makes a unique key for an async event stack. | 79 // Makes a unique key for an async event stack. |
| 57 bool GetAsyncEventStackId(base::DictionaryValue* event_dict, | 80 bool GetAsyncEventStackId(base::DictionaryValue* event_dict, |
| 58 AsyncEventStackId* asyncId) { | 81 AsyncEventStackId* asyncId) { |
| 59 if (!event_dict->GetInteger("id", &asyncId->id)) { | 82 if (!ExtractKeyAsString(event_dict, "id", &asyncId->id)) { |
| 60 LOG(ERROR) << "Incorrect async trace event (missing id)."; | |
| 61 return false; | 83 return false; |
| 62 } | 84 } |
| 63 | 85 |
| 64 // We can have an empty category, but it is still relevant for event merging, | 86 // We can have an empty category, but it is still relevant for event |
| 65 // per the documentation. | 87 // merging per the documentation. |
| 66 std::string cat; | 88 std::string cat; |
| 67 event_dict->GetString("cat", &asyncId->cat); | 89 event_dict->GetString("cat", &asyncId->cat); |
| 68 | 90 |
| 69 return true; | 91 return true; |
| 70 } | 92 } |
| 71 | 93 |
| 72 // Given a beginning event, registers its beginning for future merging. | 94 // Given a beginning event, registers its beginning for future merging. |
| 73 template <typename T> | 95 template <typename T> |
| 74 void RegisterEventBegin( | 96 void RegisterEventBegin( |
| 75 T key, | 97 T key, |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 187 event.type = EventType::INSTANT; | 209 event.type = EventType::INSTANT; |
| 188 } else { | 210 } else { |
| 189 // Skip all event types we do not handle. | 211 // Skip all event types we do not handle. |
| 190 continue; | 212 continue; |
| 191 } | 213 } |
| 192 | 214 |
| 193 if (!event_dict->GetString("name", &event.name)) { | 215 if (!event_dict->GetString("name", &event.name)) { |
| 194 LOG(ERROR) << "Incorrect trace event (no name)"; | 216 LOG(ERROR) << "Incorrect trace event (no name)"; |
| 195 return false; | 217 return false; |
| 196 } | 218 } |
| 197 | 219 |
|
ppi
2015/10/01 13:59:10
please add a comment explaining that this will fai
etiennej
2015/10/01 14:03:36
Done.
| |
| 198 if (!event_dict->GetString("cat", &event.categories)) { | 220 event_dict->GetString("cat", &event.categories); |
| 199 LOG(WARNING) << "Ignoring incorrect trace event (no categories)"; | |
| 200 continue; | |
| 201 } | |
| 202 | 221 |
| 203 double timestamp; | 222 double timestamp; |
| 204 if (!event_dict->GetDouble("ts", ×tamp)) { | 223 if (!event_dict->GetDouble("ts", ×tamp)) { |
| 205 LOG(WARNING) << "Ingoring incorrect trace event (no timestamp)"; | 224 LOG(WARNING) << "Ingoring incorrect trace event (no timestamp)"; |
| 206 continue; | 225 continue; |
| 207 } | 226 } |
| 208 event.timestamp = base::TimeTicks::FromInternalValue(timestamp); | 227 event.timestamp = base::TimeTicks::FromInternalValue(timestamp); |
| 209 | 228 |
| 210 if (event.type == EventType::COMPLETE) { | 229 if (event.type == EventType::COMPLETE) { |
| 211 double duration; | 230 double duration; |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 243 } | 262 } |
| 244 | 263 |
| 245 if (!JoinEvents(event_list)) | 264 if (!JoinEvents(event_list)) |
| 246 return false; | 265 return false; |
| 247 | 266 |
| 248 if (!ParseEvents(event_list, result)) | 267 if (!ParseEvents(event_list, result)) |
| 249 return false; | 268 return false; |
| 250 return true; | 269 return true; |
| 251 } | 270 } |
| 252 } // namespace benchmark | 271 } // namespace benchmark |
| OLD | NEW |