OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 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. | |
4 | |
5 #include "content/browser/browser_shutdown_profile_dumper.h" | |
6 | |
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 "content/public/common/content_switches.h" | |
15 | |
16 namespace content { | |
17 | |
18 BrowserShutdownProfileDumper::BrowserShutdownProfileDumper() | |
19 : blocks_(0), | |
20 dump_file_(0) { | |
piman
2013/09/05 17:28:40
nit: NULL
Mr4D (OOO till 08-26)
2013/09/05 18:55:17
Done.
| |
21 } | |
22 | |
23 BrowserShutdownProfileDumper::~BrowserShutdownProfileDumper() { | |
24 WriteTracesToDisc(GetFileName()); | |
25 } | |
26 | |
27 void BrowserShutdownProfileDumper::WriteTracesToDisc( | |
28 const base::FilePath& file_name) { | |
29 // Note: I have seen a usage of 0.000xx% when dumping - which fits easily. | |
30 // Since the tracer stops when the trace buffer is filled, we'd rather save | |
31 // what we have then nothing since we might see from the amount of events | |
piman
2013/09/05 17:28:40
nit: s/then/than/
Mr4D (OOO till 08-26)
2013/09/05 18:55:17
Done.
| |
32 // who caused the problem. | |
piman
2013/09/05 17:28:40
nit: s/who/that/
Don't personify events, they hate
Mr4D (OOO till 08-26)
2013/09/05 18:55:17
Done.
| |
33 DVLOG(1) << "Flushing shutdown traces to disc. The buffer is %" << | |
34 base::debug::TraceLog::GetInstance()->GetBufferPercentFull() << | |
35 " full."; | |
36 DCHECK(!dump_file_); | |
37 dump_file_ = file_util::OpenFile(file_name, "w+"); | |
38 if (!IsFileValid()) { | |
39 LOG(ERROR) << "Failed to open performance trace file: " << | |
40 file_name.value(); | |
41 return; | |
42 } | |
43 WriteString("{\"traceEvents\":"); | |
44 WriteString("["); | |
45 | |
46 base::debug::TraceLog::GetInstance()->Flush( | |
47 base::Bind(&BrowserShutdownProfileDumper::WriteTraceDataCollected, | |
48 base::Unretained(this))); | |
49 | |
50 WriteString("]"); | |
51 WriteString("}"); | |
52 CloseFile(); | |
53 } | |
54 | |
55 base::FilePath BrowserShutdownProfileDumper::GetFileName() { | |
56 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | |
57 base::FilePath trace_file = | |
58 command_line.GetSwitchValuePath(switches::kTraceShutdownFile); | |
59 | |
60 if (!trace_file.empty()) | |
61 return trace_file; | |
62 | |
63 // Default to saving the startup trace into the current dir. | |
64 return base::FilePath().AppendASCII("chrometrace.log"); | |
65 } | |
66 | |
67 void BrowserShutdownProfileDumper::WriteTraceDataCollected( | |
68 const scoped_refptr<base::RefCountedString>& events_str) { | |
69 if (!IsFileValid()) | |
70 return; | |
71 if (blocks_) { | |
72 // Blocks are not comma separated. Beginning with the second block we | |
73 // start therefore to add one in front of the previous block. | |
74 WriteString(","); | |
75 } | |
76 ++blocks_; | |
77 WriteString(events_str->data()); | |
78 } | |
79 | |
80 bool BrowserShutdownProfileDumper::IsFileValid() { | |
81 return dump_file_ && (ferror(dump_file_) == 0); | |
82 } | |
83 | |
84 void BrowserShutdownProfileDumper::WriteString(const std::string& string) { | |
85 WriteChars(string.data(), string.size()); | |
86 } | |
87 | |
88 void BrowserShutdownProfileDumper::WriteChars(const char* chars, size_t size) { | |
89 if (!IsFileValid()) | |
90 return; | |
91 | |
92 size_t written = fwrite(chars, 1, size, dump_file_); | |
93 if (written != size) { | |
94 LOG(ERROR) << "Error " << ferror(dump_file_) << | |
95 " in fwrite() to trace file"; | |
96 CloseFile(); | |
97 } | |
98 } | |
99 | |
100 void BrowserShutdownProfileDumper::CloseFile() { | |
101 if (!dump_file_) | |
102 return; | |
103 file_util::CloseFile(dump_file_); | |
104 dump_file_ = 0; | |
piman
2013/09/05 17:28:40
nit: NULL
Mr4D (OOO till 08-26)
2013/09/05 18:55:17
Done.
| |
105 } | |
106 | |
107 } // namespace content | |
OLD | NEW |