OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 "content/browser/browser_shutdown_profile_dumper.h" | 5 #include "content/browser/browser_shutdown_profile_dumper.h" |
6 | 6 |
7 #include "base/base_switches.h" | 7 #include "base/base_switches.h" |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/debug/trace_event.h" | 9 #include "base/debug/trace_event.h" |
10 #include "base/debug/trace_event_impl.h" | 10 #include "base/debug/trace_event_impl.h" |
11 #include "base/file_util.h" | 11 #include "base/file_util.h" |
12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #include "base/synchronization/waitable_event.h" | 14 #include "base/synchronization/waitable_event.h" |
15 #include "base/threading/thread.h" | 15 #include "base/threading/thread.h" |
16 #include "base/threading/thread_restrictions.h" | 16 #include "base/threading/thread_restrictions.h" |
17 #include "content/public/common/content_switches.h" | 17 #include "content/public/common/content_switches.h" |
18 | 18 |
19 namespace content { | 19 namespace content { |
20 | 20 |
21 BrowserShutdownProfileDumper::BrowserShutdownProfileDumper() | 21 BrowserShutdownProfileDumper::BrowserShutdownProfileDumper( |
22 : blocks_(0), | 22 const base::FilePath& dump_file_name) |
| 23 : dump_file_name_(dump_file_name), |
| 24 blocks_(0), |
23 dump_file_(NULL) { | 25 dump_file_(NULL) { |
24 } | 26 } |
25 | 27 |
26 BrowserShutdownProfileDumper::~BrowserShutdownProfileDumper() { | 28 BrowserShutdownProfileDumper::~BrowserShutdownProfileDumper() { |
27 WriteTracesToDisc(GetFileName()); | 29 WriteTracesToDisc(); |
28 } | 30 } |
29 | 31 |
30 void BrowserShutdownProfileDumper::WriteTracesToDisc( | 32 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 // 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 // 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 // what we have than nothing since we might see from the amount of events |
35 // that caused the problem. | 36 // that caused the problem. |
36 DVLOG(1) << "Flushing shutdown traces to disc. The buffer is %" << | 37 DVLOG(1) << "Flushing shutdown traces to disc. The buffer is %" << |
37 base::debug::TraceLog::GetInstance()->GetBufferPercentFull() << | 38 base::debug::TraceLog::GetInstance()->GetBufferPercentFull() << |
38 " full."; | 39 " full."; |
39 DCHECK(!dump_file_); | 40 DCHECK(!dump_file_); |
40 dump_file_ = base::OpenFile(file_name, "w+"); | 41 dump_file_ = base::OpenFile(dump_file_name_, "w+"); |
41 if (!IsFileValid()) { | 42 if (!IsFileValid()) { |
42 LOG(ERROR) << "Failed to open performance trace file: " << | 43 LOG(ERROR) << "Failed to open performance trace file: " |
43 file_name.value(); | 44 << dump_file_name_.value(); |
44 return; | 45 return; |
45 } | 46 } |
46 WriteString("{\"traceEvents\":"); | 47 WriteString("{\"traceEvents\":"); |
47 WriteString("["); | 48 WriteString("["); |
48 | 49 |
49 // TraceLog::Flush() requires the calling thread to have a message loop. | 50 // 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 // As the message loop of the current thread may have quit, start another |
51 // thread for flushing the trace. | 52 // thread for flushing the trace. |
52 base::WaitableEvent flush_complete_event(false, false); | 53 base::WaitableEvent flush_complete_event(false, false); |
53 base::Thread flush_thread("browser_shutdown_trace_event_flush"); | 54 base::Thread flush_thread("browser_shutdown_trace_event_flush"); |
(...skipping 11 matching lines...) Expand all Loading... |
65 | 66 |
66 void BrowserShutdownProfileDumper::EndTraceAndFlush( | 67 void BrowserShutdownProfileDumper::EndTraceAndFlush( |
67 base::WaitableEvent* flush_complete_event) { | 68 base::WaitableEvent* flush_complete_event) { |
68 base::debug::TraceLog::GetInstance()->SetDisabled(); | 69 base::debug::TraceLog::GetInstance()->SetDisabled(); |
69 base::debug::TraceLog::GetInstance()->Flush( | 70 base::debug::TraceLog::GetInstance()->Flush( |
70 base::Bind(&BrowserShutdownProfileDumper::WriteTraceDataCollected, | 71 base::Bind(&BrowserShutdownProfileDumper::WriteTraceDataCollected, |
71 base::Unretained(this), | 72 base::Unretained(this), |
72 base::Unretained(flush_complete_event))); | 73 base::Unretained(flush_complete_event))); |
73 } | 74 } |
74 | 75 |
75 base::FilePath BrowserShutdownProfileDumper::GetFileName() { | 76 // static |
| 77 base::FilePath BrowserShutdownProfileDumper::GetShutdownProfileFileName() { |
76 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | 78 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
77 base::FilePath trace_file = | 79 base::FilePath trace_file = |
78 command_line.GetSwitchValuePath(switches::kTraceShutdownFile); | 80 command_line.GetSwitchValuePath(switches::kTraceShutdownFile); |
79 | 81 |
80 if (!trace_file.empty()) | 82 if (!trace_file.empty()) |
81 return trace_file; | 83 return trace_file; |
82 | 84 |
83 // Default to saving the startup trace into the current dir. | 85 // Default to saving the startup trace into the current dir. |
84 return base::FilePath().AppendASCII("chrometrace.log"); | 86 return base::FilePath().AppendASCII("chrometrace.log"); |
85 } | 87 } |
(...skipping 29 matching lines...) Expand all Loading... |
115 void BrowserShutdownProfileDumper::WriteString(const std::string& string) { | 117 void BrowserShutdownProfileDumper::WriteString(const std::string& string) { |
116 WriteChars(string.data(), string.size()); | 118 WriteChars(string.data(), string.size()); |
117 } | 119 } |
118 | 120 |
119 void BrowserShutdownProfileDumper::WriteChars(const char* chars, size_t size) { | 121 void BrowserShutdownProfileDumper::WriteChars(const char* chars, size_t size) { |
120 if (!IsFileValid()) | 122 if (!IsFileValid()) |
121 return; | 123 return; |
122 | 124 |
123 size_t written = fwrite(chars, 1, size, dump_file_); | 125 size_t written = fwrite(chars, 1, size, dump_file_); |
124 if (written != size) { | 126 if (written != size) { |
125 LOG(ERROR) << "Error " << ferror(dump_file_) << | 127 LOG(ERROR) << "Error " << ferror(dump_file_) |
126 " in fwrite() to trace file"; | 128 << " in fwrite() to trace file '" << dump_file_name_.value() |
| 129 << "'"; |
127 CloseFile(); | 130 CloseFile(); |
128 } | 131 } |
129 } | 132 } |
130 | 133 |
131 void BrowserShutdownProfileDumper::CloseFile() { | 134 void BrowserShutdownProfileDumper::CloseFile() { |
132 if (!dump_file_) | 135 if (!dump_file_) |
133 return; | 136 return; |
134 base::CloseFile(dump_file_); | 137 base::CloseFile(dump_file_); |
135 dump_file_ = NULL; | 138 dump_file_ = NULL; |
136 } | 139 } |
137 | 140 |
138 } // namespace content | 141 } // namespace content |
OLD | NEW |