| 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" |
| 15 #include "base/threading/thread.h" |
| 16 #include "base/threading/thread_restrictions.h" |
| 14 #include "content/public/common/content_switches.h" | 17 #include "content/public/common/content_switches.h" |
| 15 | 18 |
| 16 namespace content { | 19 namespace content { |
| 17 | 20 |
| 18 BrowserShutdownProfileDumper::BrowserShutdownProfileDumper() | 21 BrowserShutdownProfileDumper::BrowserShutdownProfileDumper() |
| 19 : blocks_(0), | 22 : blocks_(0), |
| 20 dump_file_(NULL) { | 23 dump_file_(NULL) { |
| 21 } | 24 } |
| 22 | 25 |
| 23 BrowserShutdownProfileDumper::~BrowserShutdownProfileDumper() { | 26 BrowserShutdownProfileDumper::~BrowserShutdownProfileDumper() { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 36 DCHECK(!dump_file_); | 39 DCHECK(!dump_file_); |
| 37 dump_file_ = file_util::OpenFile(file_name, "w+"); | 40 dump_file_ = file_util::OpenFile(file_name, "w+"); |
| 38 if (!IsFileValid()) { | 41 if (!IsFileValid()) { |
| 39 LOG(ERROR) << "Failed to open performance trace file: " << | 42 LOG(ERROR) << "Failed to open performance trace file: " << |
| 40 file_name.value(); | 43 file_name.value(); |
| 41 return; | 44 return; |
| 42 } | 45 } |
| 43 WriteString("{\"traceEvents\":"); | 46 WriteString("{\"traceEvents\":"); |
| 44 WriteString("["); | 47 WriteString("["); |
| 45 | 48 |
| 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"); |
| 54 flush_thread.Start(); |
| 55 flush_thread.message_loop()->PostTask( |
| 56 FROM_HERE, |
| 57 base::Bind(&BrowserShutdownProfileDumper::EndTraceAndFlush, |
| 58 base::Unretained(this), |
| 59 base::Unretained(&flush_complete_event))); |
| 60 |
| 61 bool original_wait_allowed = base::ThreadRestrictions::SetWaitAllowed(true); |
| 62 flush_complete_event.Wait(); |
| 63 base::ThreadRestrictions::SetWaitAllowed(original_wait_allowed); |
| 64 } |
| 65 |
| 66 void BrowserShutdownProfileDumper::EndTraceAndFlush( |
| 67 base::WaitableEvent* flush_complete_event) { |
| 68 while (base::debug::TraceLog::GetInstance()->IsEnabled()) |
| 69 base::debug::TraceLog::GetInstance()->SetDisabled(); |
| 46 base::debug::TraceLog::GetInstance()->Flush( | 70 base::debug::TraceLog::GetInstance()->Flush( |
| 47 base::Bind(&BrowserShutdownProfileDumper::WriteTraceDataCollected, | 71 base::Bind(&BrowserShutdownProfileDumper::WriteTraceDataCollected, |
| 48 base::Unretained(this))); | 72 base::Unretained(this), |
| 73 base::Unretained(flush_complete_event))); |
| 49 } | 74 } |
| 50 | 75 |
| 51 base::FilePath BrowserShutdownProfileDumper::GetFileName() { | 76 base::FilePath BrowserShutdownProfileDumper::GetFileName() { |
| 52 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | 77 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
| 53 base::FilePath trace_file = | 78 base::FilePath trace_file = |
| 54 command_line.GetSwitchValuePath(switches::kTraceShutdownFile); | 79 command_line.GetSwitchValuePath(switches::kTraceShutdownFile); |
| 55 | 80 |
| 56 if (!trace_file.empty()) | 81 if (!trace_file.empty()) |
| 57 return trace_file; | 82 return trace_file; |
| 58 | 83 |
| 59 // Default to saving the startup trace into the current dir. | 84 // Default to saving the startup trace into the current dir. |
| 60 return base::FilePath().AppendASCII("chrometrace.log"); | 85 return base::FilePath().AppendASCII("chrometrace.log"); |
| 61 } | 86 } |
| 62 | 87 |
| 63 void BrowserShutdownProfileDumper::WriteTraceDataCollected( | 88 void BrowserShutdownProfileDumper::WriteTraceDataCollected( |
| 89 base::WaitableEvent* flush_complete_event, |
| 64 const scoped_refptr<base::RefCountedString>& events_str, | 90 const scoped_refptr<base::RefCountedString>& events_str, |
| 65 bool has_more_events) { | 91 bool has_more_events) { |
| 66 if (!IsFileValid()) | 92 if (!IsFileValid()) { |
| 93 flush_complete_event->Signal(); |
| 67 return; | 94 return; |
| 95 } |
| 68 if (blocks_) { | 96 if (blocks_) { |
| 69 // Blocks are not comma separated. Beginning with the second block we | 97 // Blocks are not comma separated. Beginning with the second block we |
| 70 // start therefore to add one in front of the previous block. | 98 // start therefore to add one in front of the previous block. |
| 71 WriteString(","); | 99 WriteString(","); |
| 72 } | 100 } |
| 73 ++blocks_; | 101 ++blocks_; |
| 74 WriteString(events_str->data()); | 102 WriteString(events_str->data()); |
| 75 | 103 |
| 76 if (!has_more_events) { | 104 if (!has_more_events) { |
| 77 WriteString("]"); | 105 WriteString("]"); |
| 78 WriteString("}"); | 106 WriteString("}"); |
| 79 CloseFile(); | 107 CloseFile(); |
| 108 flush_complete_event->Signal(); |
| 80 } | 109 } |
| 81 } | 110 } |
| 82 | 111 |
| 83 bool BrowserShutdownProfileDumper::IsFileValid() { | 112 bool BrowserShutdownProfileDumper::IsFileValid() { |
| 84 return dump_file_ && (ferror(dump_file_) == 0); | 113 return dump_file_ && (ferror(dump_file_) == 0); |
| 85 } | 114 } |
| 86 | 115 |
| 87 void BrowserShutdownProfileDumper::WriteString(const std::string& string) { | 116 void BrowserShutdownProfileDumper::WriteString(const std::string& string) { |
| 88 WriteChars(string.data(), string.size()); | 117 WriteChars(string.data(), string.size()); |
| 89 } | 118 } |
| (...skipping 11 matching lines...) Expand all Loading... |
| 101 } | 130 } |
| 102 | 131 |
| 103 void BrowserShutdownProfileDumper::CloseFile() { | 132 void BrowserShutdownProfileDumper::CloseFile() { |
| 104 if (!dump_file_) | 133 if (!dump_file_) |
| 105 return; | 134 return; |
| 106 file_util::CloseFile(dump_file_); | 135 file_util::CloseFile(dump_file_); |
| 107 dump_file_ = NULL; | 136 dump_file_ = NULL; |
| 108 } | 137 } |
| 109 | 138 |
| 110 } // namespace content | 139 } // namespace content |
| OLD | NEW |