1 // Copyright 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 "content/browser/browser_shutdown_profile_dumper.h"
7 #include "base/base_switches.h"
8 #include "base/command_line.h"
9 #include "base/debug/trace_event.h"
10 #include "base/debug/trace_event_impl.h"
11 #include "base/file_util.h"
12 #include "base/files/file_path.h"
13 #include "base/logging.h"
14 #include "base/synchronization/waitable_event.h"
15 #include "base/threading/thread.h"
16 #include "base/threading/thread_restrictions.h"
17 #include "content/public/common/content_switches.h"
21 BrowserShutdownProfileDumper::BrowserShutdownProfileDumper()
26 BrowserShutdownProfileDumper::~BrowserShutdownProfileDumper() {
27 WriteTracesToDisc(GetFileName());
30 void BrowserShutdownProfileDumper::WriteTracesToDisc(
31 const base::FilePath
& file_name
) {
32 // Note: I have seen a usage of 0.000xx% when dumping - which fits easily.
33 // Since the tracer stops when the trace buffer is filled, we'd rather save
34 // what we have than nothing since we might see from the amount of events
35 // that caused the problem.
36 DVLOG(1) << "Flushing shutdown traces to disc. The buffer is %" <<
37 base::debug::TraceLog::GetInstance()->GetBufferPercentFull() <<
40 dump_file_
= file_util::OpenFile(file_name
, "w+");
42 LOG(ERROR
) << "Failed to open performance trace file: " <<
46 WriteString("{\"traceEvents\":");
49 // TraceLog::Flush() requires the calling thread to have a message loop.
50 // As the message loop of the current thread may have quit, start another
51 // thread for flushing the trace.
52 base::WaitableEvent
flush_complete_event(false, false);
53 base::Thread
flush_thread("browser_shutdown_trace_event_flush");
55 flush_thread
.message_loop()->PostTask(
57 base::Bind(&BrowserShutdownProfileDumper::EndTraceAndFlush
,
58 base::Unretained(this),
59 base::Unretained(&flush_complete_event
)));
61 bool original_wait_allowed
= base::ThreadRestrictions::SetWaitAllowed(true);
62 flush_complete_event
.Wait();
63 base::ThreadRestrictions::SetWaitAllowed(original_wait_allowed
);
66 void BrowserShutdownProfileDumper::EndTraceAndFlush(
67 base::WaitableEvent
* flush_complete_event
) {
68 while (base::debug::TraceLog::GetInstance()->IsEnabled())
69 base::debug::TraceLog::GetInstance()->SetDisabled();
70 base::debug::TraceLog::GetInstance()->Flush(
71 base::Bind(&BrowserShutdownProfileDumper::WriteTraceDataCollected
,
72 base::Unretained(this),
73 base::Unretained(flush_complete_event
)));
76 base::FilePath
BrowserShutdownProfileDumper::GetFileName() {
77 const CommandLine
& command_line
= *CommandLine::ForCurrentProcess();
78 base::FilePath trace_file
=
79 command_line
.GetSwitchValuePath(switches::kTraceShutdownFile
);
81 if (!trace_file
.empty())
84 // Default to saving the startup trace into the current dir.
85 return base::FilePath().AppendASCII("chrometrace.log");
88 void BrowserShutdownProfileDumper::WriteTraceDataCollected(
89 base::WaitableEvent
* flush_complete_event
,
90 const scoped_refptr
<base::RefCountedString
>& events_str
,
91 bool has_more_events
) {
93 flush_complete_event
->Signal();
97 // Blocks are not comma separated. Beginning with the second block we
98 // start therefore to add one in front of the previous block.
102 WriteString(events_str
->data());
104 if (!has_more_events
) {
108 flush_complete_event
->Signal();
112 bool BrowserShutdownProfileDumper::IsFileValid() {
113 return dump_file_
&& (ferror(dump_file_
) == 0);
116 void BrowserShutdownProfileDumper::WriteString(const std::string
& string
) {
117 WriteChars(string
.data(), string
.size());
120 void BrowserShutdownProfileDumper::WriteChars(const char* chars
, size_t size
) {
124 size_t written
= fwrite(chars
, 1, size
, dump_file_
);
125 if (written
!= size
) {
126 LOG(ERROR
) << "Error " << ferror(dump_file_
) <<
127 " in fwrite() to trace file";
132 void BrowserShutdownProfileDumper::CloseFile() {
135 file_util::CloseFile(dump_file_
);
139 } // namespace content