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
3 // found in the LICENSE file.
5 #include "tools/gn/trace.h"
12 #include "base/files/file_util.h"
13 #include "base/json/string_escape.h"
14 #include "base/logging.h"
15 #include "base/strings/stringprintf.h"
16 #include "base/synchronization/lock.h"
17 #include "tools/gn/filesystem_utils.h"
18 #include "tools/gn/label.h"
25 events_
.reserve(16384);
27 // Trace items leaked intentionally.
29 void Add(TraceItem
* item
) {
30 base::AutoLock
lock(lock_
);
31 events_
.push_back(item
);
34 // Returns a copy for threadsafety.
35 std::vector
<TraceItem
*> events() const { return events_
; }
40 std::vector
<TraceItem
*> events_
;
42 DISALLOW_COPY_AND_ASSIGN(TraceLog
);
45 TraceLog
* trace_log
= nullptr;
48 Coalesced() : name_ptr(nullptr), total_duration(0.0), count(0) {}
50 const std::string
* name_ptr
; // Pointer to a string with the name in it.
51 double total_duration
;
55 bool DurationGreater(const TraceItem
* a
, const TraceItem
* b
) {
56 return a
->delta() > b
->delta();
59 bool CoalescedDurationGreater(const Coalesced
& a
, const Coalesced
& b
) {
60 return a
.total_duration
> b
.total_duration
;
63 void SummarizeParses(std::vector
<const TraceItem
*>& loads
,
65 out
<< "File parse times: (time in ms, name)\n";
67 std::sort(loads
.begin(), loads
.end(), &DurationGreater
);
68 for (const auto& load
: loads
) {
69 out
<< base::StringPrintf(" %8.2f ", load
->delta().InMillisecondsF());
70 out
<< load
->name() << std::endl
;
74 void SummarizeCoalesced(std::vector
<const TraceItem
*>& items
,
76 // Group by file name.
77 std::map
<std::string
, Coalesced
> coalesced
;
78 for (const auto& item
: items
) {
79 Coalesced
& c
= coalesced
[item
->name()];
80 c
.name_ptr
= &item
->name();
81 c
.total_duration
+= item
->delta().InMillisecondsF();
86 std::vector
<Coalesced
> sorted
;
87 for (const auto& pair
: coalesced
)
88 sorted
.push_back(pair
.second
);
89 std::sort(sorted
.begin(), sorted
.end(), &CoalescedDurationGreater
);
91 for (const auto& cur
: sorted
) {
92 out
<< base::StringPrintf(" %8.2f %d ", cur
.total_duration
, cur
.count
);
93 out
<< *cur
.name_ptr
<< std::endl
;
97 void SummarizeFileExecs(std::vector
<const TraceItem
*>& execs
,
99 out
<< "File execute times: (total time in ms, # executions, name)\n";
100 SummarizeCoalesced(execs
, out
);
103 void SummarizeScriptExecs(std::vector
<const TraceItem
*>& execs
,
105 out
<< "Script execute times: (total time in ms, # executions, name)\n";
106 SummarizeCoalesced(execs
, out
);
111 TraceItem::TraceItem(Type type
,
112 const std::string
& name
,
113 base::PlatformThreadId thread_id
)
116 thread_id_(thread_id
) {
119 TraceItem::~TraceItem() {
122 ScopedTrace::ScopedTrace(TraceItem::Type t
, const std::string
& name
)
123 : item_(nullptr), done_(false) {
125 item_
= new TraceItem(t
, name
, base::PlatformThread::CurrentId());
126 item_
->set_begin(base::TimeTicks::Now());
130 ScopedTrace::ScopedTrace(TraceItem::Type t
, const Label
& label
)
131 : item_(nullptr), done_(false) {
133 item_
= new TraceItem(t
, label
.GetUserVisibleName(false),
134 base::PlatformThread::CurrentId());
135 item_
->set_begin(base::TimeTicks::Now());
139 ScopedTrace::~ScopedTrace() {
143 void ScopedTrace::SetToolchain(const Label
& label
) {
145 item_
->set_toolchain(label
.GetUserVisibleName(false));
148 void ScopedTrace::SetCommandLine(const base::CommandLine
& cmdline
) {
150 item_
->set_cmdline(FilePathToUTF8(cmdline
.GetArgumentsString()));
153 void ScopedTrace::Done() {
157 item_
->set_end(base::TimeTicks::Now());
163 void EnableTracing() {
165 trace_log
= new TraceLog
;
168 void AddTrace(TraceItem
* item
) {
169 trace_log
->Add(item
);
172 std::string
SummarizeTraces() {
174 return std::string();
176 std::vector
<TraceItem
*> events
= trace_log
->events();
178 // Classify all events.
179 std::vector
<const TraceItem
*> parses
;
180 std::vector
<const TraceItem
*> file_execs
;
181 std::vector
<const TraceItem
*> script_execs
;
182 std::vector
<const TraceItem
*> check_headers
;
183 int headers_checked
= 0;
184 for (const auto& event
: events
) {
185 switch (event
->type()) {
186 case TraceItem::TRACE_FILE_PARSE
:
187 parses
.push_back(event
);
189 case TraceItem::TRACE_FILE_EXECUTE
:
190 file_execs
.push_back(event
);
192 case TraceItem::TRACE_SCRIPT_EXECUTE
:
193 script_execs
.push_back(event
);
195 case TraceItem::TRACE_CHECK_HEADERS
:
196 check_headers
.push_back(event
);
198 case TraceItem::TRACE_CHECK_HEADER
:
201 case TraceItem::TRACE_SETUP
:
202 case TraceItem::TRACE_FILE_LOAD
:
203 case TraceItem::TRACE_FILE_WRITE
:
204 case TraceItem::TRACE_DEFINE_TARGET
:
205 break; // Ignore these for the summary.
209 std::ostringstream out
;
210 SummarizeParses(parses
, out
);
212 SummarizeFileExecs(file_execs
, out
);
214 SummarizeScriptExecs(script_execs
, out
);
217 // Generally there will only be one header check, but it's theoretically
218 // possible for more than one to run if more than one build is going in
219 // parallel. Just report the total of all of them.
220 if (!check_headers
.empty()) {
221 double check_headers_time
= 0;
222 for (const auto& cur
: check_headers
)
223 check_headers_time
+= cur
->delta().InMillisecondsF();
225 out
<< "Header check time: (total time in ms, files checked)\n";
226 out
<< base::StringPrintf(" %8.2f %d\n",
227 check_headers_time
, headers_checked
);
233 void SaveTraces(const base::FilePath
& file_name
) {
234 std::ostringstream out
;
236 out
<< "{\"traceEvents\":[";
238 std::string quote_buffer
; // Allocate outside loop to prevent reallocationg.
240 // Write main thread metadata (assume this is being written on the main
242 out
<< "{\"pid\":0,\"tid\":" << base::PlatformThread::CurrentId();
243 out
<< ",\"ts\":0,\"ph\":\"M\",";
244 out
<< "\"name\":\"thread_name\",\"args\":{\"name\":\"Main thread\"}},";
246 std::vector
<TraceItem
*> events
= trace_log
->events();
247 for (size_t i
= 0; i
< events
.size(); i
++) {
248 const TraceItem
& item
= *events
[i
];
252 out
<< "{\"pid\":0,\"tid\":" << item
.thread_id();
253 out
<< ",\"ts\":" << item
.begin().ToInternalValue();
254 out
<< ",\"ph\":\"X\""; // "X" = complete event with begin & duration.
255 out
<< ",\"dur\":" << item
.delta().InMicroseconds();
257 quote_buffer
.resize(0);
258 base::EscapeJSONString(item
.name(), true, "e_buffer
);
259 out
<< ",\"name\":" << quote_buffer
;
262 switch (item
.type()) {
263 case TraceItem::TRACE_SETUP
:
266 case TraceItem::TRACE_FILE_LOAD
:
269 case TraceItem::TRACE_FILE_PARSE
:
272 case TraceItem::TRACE_FILE_EXECUTE
:
273 out
<< "\"file_exec\"";
275 case TraceItem::TRACE_FILE_WRITE
:
276 out
<< "\"file_write\"";
278 case TraceItem::TRACE_SCRIPT_EXECUTE
:
279 out
<< "\"script_exec\"";
281 case TraceItem::TRACE_DEFINE_TARGET
:
284 case TraceItem::TRACE_CHECK_HEADER
:
287 case TraceItem::TRACE_CHECK_HEADERS
:
288 out
<< "\"header_check\"";
292 if (!item
.toolchain().empty() || !item
.cmdline().empty()) {
293 out
<< ",\"args\":{";
294 bool needs_comma
= false;
295 if (!item
.toolchain().empty()) {
296 quote_buffer
.resize(0);
297 base::EscapeJSONString(item
.toolchain(), true, "e_buffer
);
298 out
<< "\"toolchain\":" << quote_buffer
;
301 if (!item
.cmdline().empty()) {
302 quote_buffer
.resize(0);
303 base::EscapeJSONString(item
.cmdline(), true, "e_buffer
);
306 out
<< "\"cmdline\":" << quote_buffer
;
316 std::string out_str
= out
.str();
317 base::WriteFile(file_name
, out_str
.data(),
318 static_cast<int>(out_str
.size()));