| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 "tools/gn/trace.h" | 5 #include "tools/gn/trace.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <map> | 8 #include <map> |
| 9 #include <sstream> | 9 #include <sstream> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 127 | 127 |
| 128 ScopedTrace::ScopedTrace(TraceItem::Type t, const std::string& name) | 128 ScopedTrace::ScopedTrace(TraceItem::Type t, const std::string& name) |
| 129 : item_(NULL), | 129 : item_(NULL), |
| 130 done_(false) { | 130 done_(false) { |
| 131 if (trace_log) { | 131 if (trace_log) { |
| 132 item_ = new TraceItem(t, name, base::PlatformThread::CurrentId()); | 132 item_ = new TraceItem(t, name, base::PlatformThread::CurrentId()); |
| 133 item_->set_begin(base::TimeTicks::HighResNow()); | 133 item_->set_begin(base::TimeTicks::HighResNow()); |
| 134 } | 134 } |
| 135 } | 135 } |
| 136 | 136 |
| 137 ScopedTrace::ScopedTrace(TraceItem::Type t, const Label& label) |
| 138 : item_(NULL), |
| 139 done_(false) { |
| 140 if (trace_log) { |
| 141 item_ = new TraceItem(t, label.GetUserVisibleName(false), |
| 142 base::PlatformThread::CurrentId()); |
| 143 item_->set_begin(base::TimeTicks::HighResNow()); |
| 144 } |
| 145 } |
| 146 |
| 137 ScopedTrace::~ScopedTrace() { | 147 ScopedTrace::~ScopedTrace() { |
| 138 Done(); | 148 Done(); |
| 139 } | 149 } |
| 140 | 150 |
| 141 void ScopedTrace::SetToolchain(const Label& label) { | 151 void ScopedTrace::SetToolchain(const Label& label) { |
| 142 if (item_) | 152 if (item_) |
| 143 item_->set_toolchain(label.GetUserVisibleName(false)); | 153 item_->set_toolchain(label.GetUserVisibleName(false)); |
| 144 } | 154 } |
| 145 | 155 |
| 146 void ScopedTrace::SetCommandLine(const CommandLine& cmdline) { | 156 void ScopedTrace::SetCommandLine(const CommandLine& cmdline) { |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 183 parses.push_back(events[i]); | 193 parses.push_back(events[i]); |
| 184 break; | 194 break; |
| 185 case TraceItem::TRACE_FILE_EXECUTE: | 195 case TraceItem::TRACE_FILE_EXECUTE: |
| 186 file_execs.push_back(events[i]); | 196 file_execs.push_back(events[i]); |
| 187 break; | 197 break; |
| 188 case TraceItem::TRACE_SCRIPT_EXECUTE: | 198 case TraceItem::TRACE_SCRIPT_EXECUTE: |
| 189 script_execs.push_back(events[i]); | 199 script_execs.push_back(events[i]); |
| 190 break; | 200 break; |
| 191 case TraceItem::TRACE_FILE_LOAD: | 201 case TraceItem::TRACE_FILE_LOAD: |
| 192 case TraceItem::TRACE_FILE_WRITE: | 202 case TraceItem::TRACE_FILE_WRITE: |
| 203 case TraceItem::TRACE_DEFINE_TARGET: |
| 193 break; // Ignore these for the summary. | 204 break; // Ignore these for the summary. |
| 194 } | 205 } |
| 195 } | 206 } |
| 196 | 207 |
| 197 std::ostringstream out; | 208 std::ostringstream out; |
| 198 SummarizeParses(parses, out); | 209 SummarizeParses(parses, out); |
| 199 out << std::endl; | 210 out << std::endl; |
| 200 SummarizeFileExecs(file_execs, out); | 211 SummarizeFileExecs(file_execs, out); |
| 201 out << std::endl; | 212 out << std::endl; |
| 202 SummarizeScriptExecs(script_execs, out); | 213 SummarizeScriptExecs(script_execs, out); |
| 203 out << std::endl; | 214 out << std::endl; |
| 204 | 215 |
| 205 return out.str(); | 216 return out.str(); |
| 206 } | 217 } |
| 207 | 218 |
| 208 void SaveTraces(const base::FilePath& file_name) { | 219 void SaveTraces(const base::FilePath& file_name) { |
| 209 std::ostringstream out; | 220 std::ostringstream out; |
| 210 | 221 |
| 211 out << "{\"traceEvents\":["; | 222 out << "{\"traceEvents\":["; |
| 212 | 223 |
| 213 std::string quote_buffer; // Allocate outside loop to prevent reallocationg. | 224 std::string quote_buffer; // Allocate outside loop to prevent reallocationg. |
| 214 | 225 |
| 226 // Write main thread metadata (assume this is being written on the main |
| 227 // thread). |
| 228 out << "{\"pid\":0,\"tid\":" << base::PlatformThread::CurrentId(); |
| 229 out << ",\"ts\":0,\"ph\":\"M\","; |
| 230 out << "\"name\":\"thread_name\",\"args\":{\"name\":\"Main thread\"}},"; |
| 231 |
| 215 std::vector<TraceItem*> events = trace_log->events(); | 232 std::vector<TraceItem*> events = trace_log->events(); |
| 216 for (size_t i = 0; i < events.size(); i++) { | 233 for (size_t i = 0; i < events.size(); i++) { |
| 217 const TraceItem& item = *events[i]; | 234 const TraceItem& item = *events[i]; |
| 218 | 235 |
| 219 if (i != 0) | 236 if (i != 0) |
| 220 out << ","; | 237 out << ","; |
| 221 out << "{\"pid\":0,\"tid\":" << item.thread_id(); | 238 out << "{\"pid\":0,\"tid\":" << item.thread_id(); |
| 222 out << ",\"ts\":" << item.begin().ToInternalValue(); | 239 out << ",\"ts\":" << item.begin().ToInternalValue(); |
| 223 out << ",\"ph\":\"X\""; // "X" = complete event with begin & duration. | 240 out << ",\"ph\":\"X\""; // "X" = complete event with begin & duration. |
| 224 out << ",\"dur\":" << item.delta().InMicroseconds(); | 241 out << ",\"dur\":" << item.delta().InMicroseconds(); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 237 break; | 254 break; |
| 238 case TraceItem::TRACE_FILE_EXECUTE: | 255 case TraceItem::TRACE_FILE_EXECUTE: |
| 239 out << "\"file_exec\""; | 256 out << "\"file_exec\""; |
| 240 break; | 257 break; |
| 241 case TraceItem::TRACE_FILE_WRITE: | 258 case TraceItem::TRACE_FILE_WRITE: |
| 242 out << "\"file_write\""; | 259 out << "\"file_write\""; |
| 243 break; | 260 break; |
| 244 case TraceItem::TRACE_SCRIPT_EXECUTE: | 261 case TraceItem::TRACE_SCRIPT_EXECUTE: |
| 245 out << "\"script_exec\""; | 262 out << "\"script_exec\""; |
| 246 break; | 263 break; |
| 264 case TraceItem::TRACE_DEFINE_TARGET: |
| 265 out << "\"define\""; |
| 247 } | 266 } |
| 248 | 267 |
| 249 if (!item.toolchain().empty() || !item.cmdline().empty()) { | 268 if (!item.toolchain().empty() || !item.cmdline().empty()) { |
| 250 out << ",\"args\":{"; | 269 out << ",\"args\":{"; |
| 251 bool needs_comma = false; | 270 bool needs_comma = false; |
| 252 if (!item.toolchain().empty()) { | 271 if (!item.toolchain().empty()) { |
| 253 quote_buffer.resize(0); | 272 quote_buffer.resize(0); |
| 254 base::JsonDoubleQuote(item.toolchain(), true, "e_buffer); | 273 base::JsonDoubleQuote(item.toolchain(), true, "e_buffer); |
| 255 out << "\"toolchain\":" << quote_buffer; | 274 out << "\"toolchain\":" << quote_buffer; |
| 256 needs_comma = true; | 275 needs_comma = true; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 267 } | 286 } |
| 268 out << "}"; | 287 out << "}"; |
| 269 } | 288 } |
| 270 | 289 |
| 271 out << "]}"; | 290 out << "]}"; |
| 272 | 291 |
| 273 std::string out_str = out.str(); | 292 std::string out_str = out.str(); |
| 274 file_util::WriteFile(file_name, out_str.data(), | 293 file_util::WriteFile(file_name, out_str.data(), |
| 275 static_cast<int>(out_str.size())); | 294 static_cast<int>(out_str.size())); |
| 276 } | 295 } |
| OLD | NEW |