| 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 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | |
| 77 base::FilePath trace_file = | |
| 78 command_line.GetSwitchValuePath(switches::kTraceShutdownFile); | |
| 79 | |
| 80 if (!trace_file.empty()) | |
| 81 return trace_file; | |
| 82 | |
| 83 // Default to saving the startup trace into the current dir. | |
| 84 return base::FilePath().AppendASCII("chrometrace.log"); | |
| 85 } | |
| 86 | |
| 87 void BrowserShutdownProfileDumper::WriteTraceDataCollected( | 76 void BrowserShutdownProfileDumper::WriteTraceDataCollected( |
| 88 base::WaitableEvent* flush_complete_event, | 77 base::WaitableEvent* flush_complete_event, |
| 89 const scoped_refptr<base::RefCountedString>& events_str, | 78 const scoped_refptr<base::RefCountedString>& events_str, |
| 90 bool has_more_events) { | 79 bool has_more_events) { |
| 91 if (!IsFileValid()) { | 80 if (!IsFileValid()) { |
| 92 flush_complete_event->Signal(); | 81 flush_complete_event->Signal(); |
| 93 return; | 82 return; |
| 94 } | 83 } |
| 95 if (blocks_) { | 84 if (blocks_) { |
| 96 // Blocks are not comma separated. Beginning with the second block we | 85 // Blocks are not comma separated. Beginning with the second block we |
| (...skipping 18 matching lines...) Expand all Loading... |
| 115 void BrowserShutdownProfileDumper::WriteString(const std::string& string) { | 104 void BrowserShutdownProfileDumper::WriteString(const std::string& string) { |
| 116 WriteChars(string.data(), string.size()); | 105 WriteChars(string.data(), string.size()); |
| 117 } | 106 } |
| 118 | 107 |
| 119 void BrowserShutdownProfileDumper::WriteChars(const char* chars, size_t size) { | 108 void BrowserShutdownProfileDumper::WriteChars(const char* chars, size_t size) { |
| 120 if (!IsFileValid()) | 109 if (!IsFileValid()) |
| 121 return; | 110 return; |
| 122 | 111 |
| 123 size_t written = fwrite(chars, 1, size, dump_file_); | 112 size_t written = fwrite(chars, 1, size, dump_file_); |
| 124 if (written != size) { | 113 if (written != size) { |
| 125 LOG(ERROR) << "Error " << ferror(dump_file_) << | 114 LOG(ERROR) << "Error " << ferror(dump_file_) |
| 126 " in fwrite() to trace file"; | 115 << " in fwrite() to trace file '" << dump_file_name_.value() |
| 116 << "'"; |
| 127 CloseFile(); | 117 CloseFile(); |
| 128 } | 118 } |
| 129 } | 119 } |
| 130 | 120 |
| 131 void BrowserShutdownProfileDumper::CloseFile() { | 121 void BrowserShutdownProfileDumper::CloseFile() { |
| 132 if (!dump_file_) | 122 if (!dump_file_) |
| 133 return; | 123 return; |
| 134 base::CloseFile(dump_file_); | 124 base::CloseFile(dump_file_); |
| 135 dump_file_ = NULL; | 125 dump_file_ = NULL; |
| 136 } | 126 } |
| 137 | 127 |
| 138 } // namespace content | 128 } // namespace content |
| OLD | NEW |