Add ICU message format support
[chromium-blink-merge.git] / content / browser / browser_shutdown_profile_dumper.cc
blobc23d8d16000d0f1e20167a071bdc9277ea9a0056
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/files/file_path.h"
10 #include "base/files/file_util.h"
11 #include "base/location.h"
12 #include "base/logging.h"
13 #include "base/single_thread_task_runner.h"
14 #include "base/synchronization/waitable_event.h"
15 #include "base/threading/thread.h"
16 #include "base/threading/thread_restrictions.h"
17 #include "base/trace_event/trace_event.h"
18 #include "base/trace_event/trace_event_impl.h"
19 #include "content/public/common/content_switches.h"
21 namespace content {
23 BrowserShutdownProfileDumper::BrowserShutdownProfileDumper(
24 const base::FilePath& dump_file_name)
25 : dump_file_name_(dump_file_name),
26 blocks_(0),
27 dump_file_(NULL) {
30 BrowserShutdownProfileDumper::~BrowserShutdownProfileDumper() {
31 WriteTracesToDisc();
34 static float GetTraceBufferPercentFull() {
35 base::trace_event::TraceLogStatus status =
36 base::trace_event::TraceLog::GetInstance()->GetStatus();
37 return 100 * static_cast<float>(static_cast<double>(status.event_count) /
38 status.event_capacity);
41 void BrowserShutdownProfileDumper::WriteTracesToDisc() {
42 // Note: I have seen a usage of 0.000xx% when dumping - which fits easily.
43 // Since the tracer stops when the trace buffer is filled, we'd rather save
44 // what we have than nothing since we might see from the amount of events
45 // that caused the problem.
46 DVLOG(1) << "Flushing shutdown traces to disc. The buffer is "
47 << GetTraceBufferPercentFull() << "% full.";
48 DCHECK(!dump_file_);
49 dump_file_ = base::OpenFile(dump_file_name_, "w+");
50 if (!IsFileValid()) {
51 LOG(ERROR) << "Failed to open performance trace file: "
52 << dump_file_name_.value();
53 return;
55 WriteString("{\"traceEvents\":");
56 WriteString("[");
58 // TraceLog::Flush() requires the calling thread to have a message loop.
59 // As the message loop of the current thread may have quit, start another
60 // thread for flushing the trace.
61 base::WaitableEvent flush_complete_event(false, false);
62 base::Thread flush_thread("browser_shutdown_trace_event_flush");
63 flush_thread.Start();
64 flush_thread.task_runner()->PostTask(
65 FROM_HERE, base::Bind(&BrowserShutdownProfileDumper::EndTraceAndFlush,
66 base::Unretained(this),
67 base::Unretained(&flush_complete_event)));
69 bool original_wait_allowed = base::ThreadRestrictions::SetWaitAllowed(true);
70 flush_complete_event.Wait();
71 base::ThreadRestrictions::SetWaitAllowed(original_wait_allowed);
74 void BrowserShutdownProfileDumper::EndTraceAndFlush(
75 base::WaitableEvent* flush_complete_event) {
76 base::trace_event::TraceLog::GetInstance()->SetDisabled();
77 base::trace_event::TraceLog::GetInstance()->Flush(
78 base::Bind(&BrowserShutdownProfileDumper::WriteTraceDataCollected,
79 base::Unretained(this),
80 base::Unretained(flush_complete_event)));
83 // static
84 base::FilePath BrowserShutdownProfileDumper::GetShutdownProfileFileName() {
85 const base::CommandLine& command_line =
86 *base::CommandLine::ForCurrentProcess();
87 base::FilePath trace_file =
88 command_line.GetSwitchValuePath(switches::kTraceShutdownFile);
90 if (!trace_file.empty())
91 return trace_file;
93 // Default to saving the startup trace into the current dir.
94 return base::FilePath().AppendASCII("chrometrace.log");
97 void BrowserShutdownProfileDumper::WriteTraceDataCollected(
98 base::WaitableEvent* flush_complete_event,
99 const scoped_refptr<base::RefCountedString>& events_str,
100 bool has_more_events) {
101 if (!IsFileValid()) {
102 flush_complete_event->Signal();
103 return;
105 if (blocks_) {
106 // Blocks are not comma separated. Beginning with the second block we
107 // start therefore to add one in front of the previous block.
108 WriteString(",");
110 ++blocks_;
111 WriteString(events_str->data());
113 if (!has_more_events) {
114 WriteString("]");
115 WriteString("}");
116 CloseFile();
117 flush_complete_event->Signal();
121 bool BrowserShutdownProfileDumper::IsFileValid() {
122 return dump_file_ && (ferror(dump_file_) == 0);
125 void BrowserShutdownProfileDumper::WriteString(const std::string& string) {
126 WriteChars(string.data(), string.size());
129 void BrowserShutdownProfileDumper::WriteChars(const char* chars, size_t size) {
130 if (!IsFileValid())
131 return;
133 size_t written = fwrite(chars, 1, size, dump_file_);
134 if (written != size) {
135 LOG(ERROR) << "Error " << ferror(dump_file_)
136 << " in fwrite() to trace file '" << dump_file_name_.value()
137 << "'";
138 CloseFile();
142 void BrowserShutdownProfileDumper::CloseFile() {
143 if (!dump_file_)
144 return;
145 base::CloseFile(dump_file_);
146 dump_file_ = NULL;
149 } // namespace content