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(
22 const base::FilePath
& dump_file_name
)
23 : dump_file_name_(dump_file_name
),
28 BrowserShutdownProfileDumper::~BrowserShutdownProfileDumper() {
32 void BrowserShutdownProfileDumper::WriteTracesToDisc() {
33 // Note: I have seen a usage of 0.000xx% when dumping - which fits easily.
34 // Since the tracer stops when the trace buffer is filled, we'd rather save
35 // what we have than nothing since we might see from the amount of events
36 // that caused the problem.
37 DVLOG(1) << "Flushing shutdown traces to disc. The buffer is %" <<
38 base::debug::TraceLog::GetInstance()->GetBufferPercentFull() <<
41 dump_file_
= base::OpenFile(dump_file_name_
, "w+");
43 LOG(ERROR
) << "Failed to open performance trace file: "
44 << dump_file_name_
.value();
47 WriteString("{\"traceEvents\":");
50 // TraceLog::Flush() requires the calling thread to have a message loop.
51 // As the message loop of the current thread may have quit, start another
52 // thread for flushing the trace.
53 base::WaitableEvent
flush_complete_event(false, false);
54 base::Thread
flush_thread("browser_shutdown_trace_event_flush");
56 flush_thread
.message_loop()->PostTask(
58 base::Bind(&BrowserShutdownProfileDumper::EndTraceAndFlush
,
59 base::Unretained(this),
60 base::Unretained(&flush_complete_event
)));
62 bool original_wait_allowed
= base::ThreadRestrictions::SetWaitAllowed(true);
63 flush_complete_event
.Wait();
64 base::ThreadRestrictions::SetWaitAllowed(original_wait_allowed
);
67 void BrowserShutdownProfileDumper::EndTraceAndFlush(
68 base::WaitableEvent
* flush_complete_event
) {
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
)));
77 base::FilePath
BrowserShutdownProfileDumper::GetShutdownProfileFileName() {
78 const CommandLine
& command_line
= *CommandLine::ForCurrentProcess();
79 base::FilePath trace_file
=
80 command_line
.GetSwitchValuePath(switches::kTraceShutdownFile
);
82 if (!trace_file
.empty())
85 // Default to saving the startup trace into the current dir.
86 return base::FilePath().AppendASCII("chrometrace.log");
89 void BrowserShutdownProfileDumper::WriteTraceDataCollected(
90 base::WaitableEvent
* flush_complete_event
,
91 const scoped_refptr
<base::RefCountedString
>& events_str
,
92 bool has_more_events
) {
94 flush_complete_event
->Signal();
98 // Blocks are not comma separated. Beginning with the second block we
99 // start therefore to add one in front of the previous block.
103 WriteString(events_str
->data());
105 if (!has_more_events
) {
109 flush_complete_event
->Signal();
113 bool BrowserShutdownProfileDumper::IsFileValid() {
114 return dump_file_
&& (ferror(dump_file_
) == 0);
117 void BrowserShutdownProfileDumper::WriteString(const std::string
& string
) {
118 WriteChars(string
.data(), string
.size());
121 void BrowserShutdownProfileDumper::WriteChars(const char* chars
, size_t size
) {
125 size_t written
= fwrite(chars
, 1, size
, dump_file_
);
126 if (written
!= size
) {
127 LOG(ERROR
) << "Error " << ferror(dump_file_
)
128 << " in fwrite() to trace file '" << dump_file_name_
.value()
134 void BrowserShutdownProfileDumper::CloseFile() {
137 base::CloseFile(dump_file_
);
141 } // namespace content