Przemyslaw Pietrzkiewicz | 1d90994 | 2015-08-28 16:27:56 +0200 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "apps/benchmark/event.h" |
| 6 | |
Przemyslaw Pietrzkiewicz | 421c1f7 | 2015-09-08 14:52:36 +0200 | [diff] [blame] | 7 | #include <map> |
| 8 | #include <stack> |
| 9 | |
Przemyslaw Pietrzkiewicz | 1d90994 | 2015-08-28 16:27:56 +0200 | [diff] [blame] | 10 | #include "base/json/json_reader.h" |
Przemyslaw Pietrzkiewicz | 421c1f7 | 2015-09-08 14:52:36 +0200 | [diff] [blame] | 11 | #include "base/macros.h" |
Przemyslaw Pietrzkiewicz | 1d90994 | 2015-08-28 16:27:56 +0200 | [diff] [blame] | 12 | #include "base/memory/scoped_ptr.h" |
| 13 | #include "base/values.h" |
| 14 | |
| 15 | namespace benchmark { |
| 16 | |
| 17 | Event::Event() {} |
| 18 | |
Przemyslaw Pietrzkiewicz | 421c1f7 | 2015-09-08 14:52:36 +0200 | [diff] [blame] | 19 | Event::Event(EventType type, |
| 20 | std::string name, |
| 21 | std::string categories, |
Przemyslaw Pietrzkiewicz | 1d90994 | 2015-08-28 16:27:56 +0200 | [diff] [blame] | 22 | base::TimeTicks timestamp, |
| 23 | base::TimeDelta duration) |
Przemyslaw Pietrzkiewicz | 421c1f7 | 2015-09-08 14:52:36 +0200 | [diff] [blame] | 24 | : type(type), |
| 25 | name(name), |
| 26 | categories(categories), |
Przemyslaw Pietrzkiewicz | 1d90994 | 2015-08-28 16:27:56 +0200 | [diff] [blame] | 27 | timestamp(timestamp), |
| 28 | duration(duration) {} |
| 29 | |
| 30 | Event::~Event() {} |
| 31 | |
Przemyslaw Pietrzkiewicz | 421c1f7 | 2015-09-08 14:52:36 +0200 | [diff] [blame] | 32 | namespace { |
Przemyslaw Pietrzkiewicz | 1d90994 | 2015-08-28 16:27:56 +0200 | [diff] [blame] | 33 | |
Przemyslaw Pietrzkiewicz | 421c1f7 | 2015-09-08 14:52:36 +0200 | [diff] [blame] | 34 | // Finds the matching "duration end" event for each "duration begin" event and |
| 35 | // rewrites the "begin event" as a "complete" event with a duration. Leaves all |
| 36 | // events that are not "duration begin" unchanged. |
| 37 | bool JoinDurationEvents(base::ListValue* event_list) { |
| 38 | // Maps thread ids to stacks of unmatched duration begin events on the given |
| 39 | // thread. |
| 40 | std::map<int, std::stack<base::DictionaryValue*>> open_begin_events; |
Przemyslaw Pietrzkiewicz | 1d90994 | 2015-08-28 16:27:56 +0200 | [diff] [blame] | 41 | |
| 42 | for (base::Value* val : *event_list) { |
Przemyslaw Pietrzkiewicz | 421c1f7 | 2015-09-08 14:52:36 +0200 | [diff] [blame] | 43 | base::DictionaryValue* event_dict; |
| 44 | if (!val->GetAsDictionary(&event_dict)) |
| 45 | return false; |
| 46 | |
| 47 | std::string phase; |
| 48 | if (!event_dict->GetString("ph", &phase)) { |
| 49 | LOG(ERROR) << "Incorrect trace event (missing phase)"; |
| 50 | return false; |
| 51 | } |
| 52 | |
| 53 | if (phase != "B" && phase != "E") |
| 54 | continue; |
| 55 | |
| 56 | int tid; |
| 57 | if (!event_dict->GetInteger("tid", &tid)) { |
| 58 | LOG(ERROR) << "Incorrect trace event (missing tid)."; |
| 59 | return false; |
| 60 | } |
| 61 | |
| 62 | if (phase == "B") { |
| 63 | open_begin_events[tid].push(event_dict); |
| 64 | } else if (phase == "E") { |
| 65 | if (!open_begin_events.count(tid) || open_begin_events[tid].empty()) { |
| 66 | LOG(ERROR) << "Incorrect trace event (duration end without begin)."; |
| 67 | return false; |
| 68 | } |
| 69 | |
| 70 | base::DictionaryValue* begin_event_dict = open_begin_events[tid].top(); |
| 71 | open_begin_events[tid].pop(); |
| 72 | double begin_ts; |
| 73 | if (!begin_event_dict->GetDouble("ts", &begin_ts)) { |
| 74 | LOG(ERROR) << "Incorrect trace event (no timestamp)"; |
| 75 | return false; |
| 76 | } |
| 77 | |
| 78 | double end_ts; |
| 79 | if (!event_dict->GetDouble("ts", &end_ts)) { |
| 80 | LOG(ERROR) << "Incorrect trace event (no timestamp)"; |
| 81 | return false; |
| 82 | } |
| 83 | |
| 84 | if (end_ts < begin_ts) { |
| 85 | LOG(ERROR) << "Incorrect trace event (duration timestamps decreasing)"; |
| 86 | return false; |
| 87 | } |
| 88 | |
| 89 | begin_event_dict->SetDouble("dur", end_ts - begin_ts); |
| 90 | begin_event_dict->SetString("ph", "X"); |
| 91 | } else { |
| 92 | NOTREACHED(); |
| 93 | } |
| 94 | } |
| 95 | return true; |
| 96 | } |
| 97 | |
| 98 | bool ParseEvents(base::ListValue* event_list, std::vector<Event>* result) { |
| 99 | result->clear(); |
| 100 | |
| 101 | for (base::Value* val : *event_list) { |
| 102 | base::DictionaryValue* event_dict; |
Przemyslaw Pietrzkiewicz | 447d2f9 | 2015-09-16 15:57:36 +0200 | [diff] [blame] | 103 | if (!val->GetAsDictionary(&event_dict)) { |
| 104 | LOG(WARNING) << "Ignoring incorrect trace event (not a dictionary)"; |
| 105 | continue; |
| 106 | } |
Przemyslaw Pietrzkiewicz | 421c1f7 | 2015-09-08 14:52:36 +0200 | [diff] [blame] | 107 | |
Przemyslaw Pietrzkiewicz | 1d90994 | 2015-08-28 16:27:56 +0200 | [diff] [blame] | 108 | Event event; |
Przemyslaw Pietrzkiewicz | 1d90994 | 2015-08-28 16:27:56 +0200 | [diff] [blame] | 109 | |
Przemyslaw Pietrzkiewicz | 421c1f7 | 2015-09-08 14:52:36 +0200 | [diff] [blame] | 110 | std::string phase; |
| 111 | if (!event_dict->GetString("ph", &phase)) { |
Przemyslaw Pietrzkiewicz | 447d2f9 | 2015-09-16 15:57:36 +0200 | [diff] [blame] | 112 | LOG(WARNING) << "Ignoring incorrect trace event (missing phase)"; |
| 113 | continue; |
Przemyslaw Pietrzkiewicz | 421c1f7 | 2015-09-08 14:52:36 +0200 | [diff] [blame] | 114 | } |
| 115 | if (phase == "X") { |
| 116 | event.type = EventType::COMPLETE; |
| 117 | } else if (phase == "I") { |
| 118 | event.type = EventType::INSTANT; |
| 119 | } else { |
| 120 | // Skip all event types we do not handle. |
| 121 | continue; |
| 122 | } |
Przemyslaw Pietrzkiewicz | 1d90994 | 2015-08-28 16:27:56 +0200 | [diff] [blame] | 123 | |
Przemyslaw Pietrzkiewicz | 421c1f7 | 2015-09-08 14:52:36 +0200 | [diff] [blame] | 124 | if (!event_dict->GetString("name", &event.name)) { |
| 125 | LOG(ERROR) << "Incorrect trace event (no name)"; |
Przemyslaw Pietrzkiewicz | 1d90994 | 2015-08-28 16:27:56 +0200 | [diff] [blame] | 126 | return false; |
Przemyslaw Pietrzkiewicz | 421c1f7 | 2015-09-08 14:52:36 +0200 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | if (!event_dict->GetString("cat", &event.categories)) { |
Przemyslaw Pietrzkiewicz | 447d2f9 | 2015-09-16 15:57:36 +0200 | [diff] [blame] | 130 | LOG(WARNING) << "Ignoring incorrect trace event (no categories)"; |
| 131 | continue; |
Przemyslaw Pietrzkiewicz | 421c1f7 | 2015-09-08 14:52:36 +0200 | [diff] [blame] | 132 | } |
Przemyslaw Pietrzkiewicz | 1d90994 | 2015-08-28 16:27:56 +0200 | [diff] [blame] | 133 | |
| 134 | double timestamp; |
Przemyslaw Pietrzkiewicz | 421c1f7 | 2015-09-08 14:52:36 +0200 | [diff] [blame] | 135 | if (!event_dict->GetDouble("ts", ×tamp)) { |
Przemyslaw Pietrzkiewicz | 447d2f9 | 2015-09-16 15:57:36 +0200 | [diff] [blame] | 136 | LOG(WARNING) << "Ingoring incorrect trace event (no timestamp)"; |
| 137 | continue; |
Przemyslaw Pietrzkiewicz | 421c1f7 | 2015-09-08 14:52:36 +0200 | [diff] [blame] | 138 | } |
Przemyslaw Pietrzkiewicz | 1d90994 | 2015-08-28 16:27:56 +0200 | [diff] [blame] | 139 | event.timestamp = base::TimeTicks::FromInternalValue(timestamp); |
| 140 | |
Przemyslaw Pietrzkiewicz | 421c1f7 | 2015-09-08 14:52:36 +0200 | [diff] [blame] | 141 | if (event.type == EventType::COMPLETE) { |
| 142 | double duration; |
| 143 | if (!event_dict->GetDouble("dur", &duration)) { |
Przemyslaw Pietrzkiewicz | 447d2f9 | 2015-09-16 15:57:36 +0200 | [diff] [blame] | 144 | LOG(WARNING) << "Ignoring incorrect complete event (no duration)"; |
| 145 | continue; |
Przemyslaw Pietrzkiewicz | 421c1f7 | 2015-09-08 14:52:36 +0200 | [diff] [blame] | 146 | } |
| 147 | |
Przemyslaw Pietrzkiewicz | 1d90994 | 2015-08-28 16:27:56 +0200 | [diff] [blame] | 148 | event.duration = base::TimeDelta::FromInternalValue(duration); |
Przemyslaw Pietrzkiewicz | 421c1f7 | 2015-09-08 14:52:36 +0200 | [diff] [blame] | 149 | } else { |
| 150 | event.duration = base::TimeDelta(); |
Przemyslaw Pietrzkiewicz | 1d90994 | 2015-08-28 16:27:56 +0200 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | result->push_back(event); |
| 154 | } |
| 155 | return true; |
| 156 | } |
Przemyslaw Pietrzkiewicz | 421c1f7 | 2015-09-08 14:52:36 +0200 | [diff] [blame] | 157 | |
| 158 | } // namespace |
| 159 | |
| 160 | bool GetEvents(const std::string& trace_json, std::vector<Event>* result) { |
| 161 | // Parse the JSON string describing the events. |
| 162 | base::JSONReader reader; |
| 163 | scoped_ptr<base::Value> trace_data = reader.ReadToValue(trace_json); |
| 164 | if (!trace_data) { |
| 165 | LOG(ERROR) << "Failed to parse the JSON string: " |
| 166 | << reader.GetErrorMessage(); |
| 167 | return false; |
| 168 | } |
| 169 | |
| 170 | base::ListValue* event_list; |
| 171 | if (!trace_data->GetAsList(&event_list)) { |
| 172 | LOG(ERROR) << "Incorrect format of the trace data."; |
| 173 | return false; |
| 174 | } |
| 175 | |
| 176 | if (!JoinDurationEvents(event_list)) |
| 177 | return false; |
| 178 | |
| 179 | if (!ParseEvents(event_list, result)) |
| 180 | return false; |
| 181 | return true; |
| 182 | } |
Przemyslaw Pietrzkiewicz | 1d90994 | 2015-08-28 16:27:56 +0200 | [diff] [blame] | 183 | } // namespace benchmark |