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 // Makes a unique id for a duration event stack. |
| 47 bool GetDurationEventStackId(base::DictionaryValue* event_dict, | 48 bool GetDurationEventStackId(base::DictionaryValue* event_dict, |
| 48 DurationEventStackId* durationId) { | 49 DurationEventStackId* durationId) { |
| 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("tid", &value)) { |
| 52 LOG(ERROR) << "Incorrect duration trace event (missing tid): " | |
| 53 << *event_dict; | |
| 51 return false; | 54 return false; |
| 52 } | 55 } |
| 56 | |
| 57 if (value->IsType(base::Value::TYPE_INTEGER)) { | |
| 58 int tid_int; | |
| 59 // We already verified the type, so it should be an integer. | |
| 60 DCHECK(value->GetAsInteger(&tid_int)); | |
|
ppi
2015/10/01 13:31:16
please extract the common part between this and th
etiennej
2015/10/01 13:41:47
Done.
| |
| 61 *durationId = base::IntToString(tid_int); | |
| 62 } else if (value->IsType(base::Value::TYPE_STRING)) { | |
| 63 DCHECK(value->GetAsString(durationId)); | |
| 64 } else { | |
| 65 LOG(ERROR) | |
| 66 << "Incorrect duration trace event (tid not a string or integer): " | |
| 67 << *event_dict; | |
| 68 } | |
| 53 return true; | 69 return true; |
| 54 } | 70 } |
| 55 | 71 |
| 56 // Makes a unique key for an async event stack. | 72 // Makes a unique key for an async event stack. |
| 57 bool GetAsyncEventStackId(base::DictionaryValue* event_dict, | 73 bool GetAsyncEventStackId(base::DictionaryValue* event_dict, |
| 58 AsyncEventStackId* asyncId) { | 74 AsyncEventStackId* asyncId) { |
| 59 if (!event_dict->GetInteger("id", &asyncId->id)) { | 75 const base::Value* value; |
| 60 LOG(ERROR) << "Incorrect async trace event (missing id)."; | 76 if (!event_dict->Get("id", &value)) { |
| 77 LOG(ERROR) << "Incorrect async trace event (missing id): " << *event_dict; | |
| 61 return false; | 78 return false; |
| 62 } | 79 } |
| 63 | 80 |
| 64 // We can have an empty category, but it is still relevant for event merging, | 81 if (value->IsType(base::Value::TYPE_INTEGER)) { |
| 65 // per the documentation. | 82 int id_int; |
| 83 // We already verified the type, so it should be an integer. | |
| 84 DCHECK(value->GetAsInteger(&id_int)); | |
| 85 asyncId->id = base::IntToString(id_int); | |
| 86 } else if (value->IsType(base::Value::TYPE_STRING)) { | |
| 87 DCHECK(value->GetAsString(&asyncId->id)); | |
| 88 } else { | |
| 89 LOG(ERROR) << "Incorrect async trace event (id not a string or integer): " | |
| 90 << *event_dict; | |
| 91 } | |
| 92 | |
| 93 // We can have an empty category, but it is still relevant for event | |
| 94 // merging per the documentation. | |
| 66 std::string cat; | 95 std::string cat; |
| 67 event_dict->GetString("cat", &asyncId->cat); | 96 event_dict->GetString("cat", &asyncId->cat); |
| 68 | 97 |
| 69 return true; | 98 return true; |
| 70 } | 99 } |
| 71 | 100 |
| 72 // Given a beginning event, registers its beginning for future merging. | 101 // Given a beginning event, registers its beginning for future merging. |
| 73 template <typename T> | 102 template <typename T> |
| 74 void RegisterEventBegin( | 103 void RegisterEventBegin( |
| 75 T key, | 104 T key, |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 188 } else { | 217 } else { |
| 189 // Skip all event types we do not handle. | 218 // Skip all event types we do not handle. |
| 190 continue; | 219 continue; |
| 191 } | 220 } |
| 192 | 221 |
| 193 if (!event_dict->GetString("name", &event.name)) { | 222 if (!event_dict->GetString("name", &event.name)) { |
| 194 LOG(ERROR) << "Incorrect trace event (no name)"; | 223 LOG(ERROR) << "Incorrect trace event (no name)"; |
| 195 return false; | 224 return false; |
| 196 } | 225 } |
| 197 | 226 |
| 198 if (!event_dict->GetString("cat", &event.categories)) { | |
|
ppi
2015/10/01 13:31:16
Why this change?
etiennej
2015/10/01 13:41:47
Some real-world events do not have a category, yet
| |
| 199 LOG(WARNING) << "Ignoring incorrect trace event (no categories)"; | |
| 200 continue; | |
| 201 } | |
| 202 | |
| 203 double timestamp; | 227 double timestamp; |
| 204 if (!event_dict->GetDouble("ts", ×tamp)) { | 228 if (!event_dict->GetDouble("ts", ×tamp)) { |
| 205 LOG(WARNING) << "Ingoring incorrect trace event (no timestamp)"; | 229 LOG(WARNING) << "Ingoring incorrect trace event (no timestamp)"; |
| 206 continue; | 230 continue; |
| 207 } | 231 } |
| 208 event.timestamp = base::TimeTicks::FromInternalValue(timestamp); | 232 event.timestamp = base::TimeTicks::FromInternalValue(timestamp); |
| 209 | 233 |
| 210 if (event.type == EventType::COMPLETE) { | 234 if (event.type == EventType::COMPLETE) { |
| 211 double duration; | 235 double duration; |
| 212 if (!event_dict->GetDouble("dur", &duration)) { | 236 if (!event_dict->GetDouble("dur", &duration)) { |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 243 } | 267 } |
| 244 | 268 |
| 245 if (!JoinEvents(event_list)) | 269 if (!JoinEvents(event_list)) |
| 246 return false; | 270 return false; |
| 247 | 271 |
| 248 if (!ParseEvents(event_list, result)) | 272 if (!ParseEvents(event_list, result)) |
| 249 return false; | 273 return false; |
| 250 return true; | 274 return true; |
| 251 } | 275 } |
| 252 } // namespace benchmark | 276 } // namespace benchmark |
| OLD | NEW |