Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(101)

Side by Side Diff: content/browser/browser_shutdown_profile_dumper.cc

Issue 23691025: Adding shutdown tracing capabilities (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Moved the trace starting for OS'ses other then ChromeOS to a later point where is no user intervent… Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012 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) {
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 %" <<
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_++;
DaveMoore 2013/09/03 17:02:48 Nit: ++blocks_;
Mr4D (OOO till 08-26) 2013/09/03 17:34:34 Done.
76 WriteString(events_str->data());
DaveMoore 2013/09/03 17:02:48 What you're doing currently is taking the char* re
Mr4D (OOO till 08-26) 2013/09/03 17:34:34 Actually... RefCountedString does return it's std:
DaveMoore 2013/09/03 20:24:11 You are right. On 2013/09/03 17:34:34, Mr4D wrote:
77 }
78
79 bool BrowserShutdownProfileDumper::IsFileValid() {
80 return dump_file_ && (ferror(dump_file_) == 0);
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())
101 return;
102 file_util::CloseFile(dump_file_);
103 dump_file_ = 0;
104 }
105
106 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698