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

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

Issue 9333003: Use FILE thread for disk operations in TraceSubscriberStdio (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 8 years, 10 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
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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/trace_subscriber_stdio.h" 5 #include "content/browser/trace_subscriber_stdio.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "content/public/browser/browser_thread.h"
9 10
10 TraceSubscriberStdio::TraceSubscriberStdio() : file_(0) { 11 namespace content {
11 }
12 12
13 TraceSubscriberStdio::TraceSubscriberStdio(const FilePath& path) : file_(0) { 13 // All method calls on this class should be done on the FILE thread.
joth 2012/02/08 13:22:20 FWIW the new hotness is to use SequencedWorkerPool
Iain Merrick 2012/02/08 14:11:24 Huh, really? The new hotness needs better advertis
14 OpenFile(path); 14 class TraceSubscriberStdioImpl
15 : public base::RefCountedThreadSafe<TraceSubscriberStdioImpl> {
16 public:
17 explicit TraceSubscriberStdioImpl(const FilePath& path)
18 : path_(path),
19 file_(0) {}
20
21 ~TraceSubscriberStdioImpl() {
22 CloseFile();
23 }
24
25 void OnStart() {
26 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
27 DCHECK(!file_);
28 file_ = file_util::OpenFile(path_, "w+");
29 if (IsValid()) {
30 LOG(INFO) << "Logging performance trace to file: " << path_.value();
31 trace_buffer_.SetOutputCallback(
32 base::Bind(&TraceSubscriberStdioImpl::Write, this));
33 trace_buffer_.Start();
34 } else {
35 LOG(ERROR) << "Failed to open performance trace file: " << path_.value();
36 }
37 }
38
39 void OnData(const scoped_refptr<base::RefCountedString>& trace_fragment) {
40 trace_buffer_.AddFragment(trace_fragment->data());
41 }
42
43 void OnEnd() {
44 trace_buffer_.Finish();
45 CloseFile();
46 }
47
48 private:
49 bool IsValid() {
50 return file_ && (0 == ferror(file_));
51 }
52
53 void CloseFile() {
54 if (file_) {
55 fclose(file_);
56 file_ = 0;
57 }
58 }
59
60 void Write(const std::string& output_str) {
61 if (IsValid()) {
62 size_t written = fwrite(output_str.data(), 1, output_str.size(), file_);
63 if (written != output_str.size()) {
64 LOG(ERROR) << "Error " << ferror(file_) << " in fwrite() to trace file";
65 CloseFile();
66 }
67 }
68 }
69
70 FilePath path_;
71 FILE* file_;
72 base::debug::TraceResultBuffer trace_buffer_;
73 };
74
75 TraceSubscriberStdio::TraceSubscriberStdio(const FilePath& path)
76 : impl_(new TraceSubscriberStdioImpl(path)) {
77 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, base::Bind(
78 &TraceSubscriberStdioImpl::OnStart, impl_.get()));
15 } 79 }
16 80
17 TraceSubscriberStdio::~TraceSubscriberStdio() { 81 TraceSubscriberStdio::~TraceSubscriberStdio() {
18 CloseFile(); 82 // This can't be inlined because TraceSubscriberStdioImpl isn't public.
joth 2012/02/08 13:22:20 no need to comment this: it is convention to *alwa
Iain Merrick 2012/02/08 14:11:24 Done.
19 }
20
21 bool TraceSubscriberStdio::OpenFile(const FilePath& path) {
22 LOG(INFO) << "Logging performance trace to file: " << path.value();
23 CloseFile();
24 file_ = file_util::OpenFile(path, "w+");
25 if (IsValid()) {
26 trace_buffer_.SetOutputCallback(base::Bind(&TraceSubscriberStdio::Write,
27 base::Unretained(this)));
28 trace_buffer_.Start();
29 return true;
30 } else {
31 LOG(ERROR) << "Failed to open performance trace file: " << path.value();
32 return false;
33 }
34 }
35
36 void TraceSubscriberStdio::CloseFile() {
37 if (file_) {
38 fclose(file_);
39 file_ = 0;
40 }
41 }
42
43 bool TraceSubscriberStdio::IsValid() {
44 return file_ && (0 == ferror(file_));
45 } 83 }
46 84
47 void TraceSubscriberStdio::OnEndTracingComplete() { 85 void TraceSubscriberStdio::OnEndTracingComplete() {
48 trace_buffer_.Finish(); 86 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, base::Bind(
joth 2012/02/08 13:22:20 I think we'd normally break line before bind and t
Iain Merrick 2012/02/08 14:11:24 Done.
49 CloseFile(); 87 &TraceSubscriberStdioImpl::OnEnd, impl_.get()));
50 } 88 }
51 89
52 void TraceSubscriberStdio::OnTraceDataCollected( 90 void TraceSubscriberStdio::OnTraceDataCollected(
53 const std::string& trace_fragment) { 91 const scoped_refptr<base::RefCountedString>& trace_fragment) {
54 trace_buffer_.AddFragment(trace_fragment); 92 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, base::Bind(
93 &TraceSubscriberStdioImpl::OnData, impl_.get(), trace_fragment));
55 } 94 }
56 95
57 void TraceSubscriberStdio::Write(const std::string& output_str) { 96 } // namespace content
58 if (IsValid()) {
59 size_t written = fwrite(output_str.data(), 1, output_str.size(), file_);
60 if (written != output_str.size()) {
61 LOG(ERROR) << "Error " << ferror(file_) << " when writing to trace file";
62 CloseFile();
63 }
64 }
65 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698