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