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

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

Issue 9443020: Use SequencedWorkerPool for disk operations in TraceSubscriberStdio. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
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
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 "base/threading/sequenced_worker_pool.h"
10 #include "content/public/browser/browser_thread.h"
9 11
10 TraceSubscriberStdio::TraceSubscriberStdio() : file_(0) { 12 namespace content {
11 }
12 13
13 TraceSubscriberStdio::TraceSubscriberStdio(const FilePath& path) : file_(0) { 14 // All method calls on this class are done on a SequencedWorkerPool thread.
14 OpenFile(path); 15 class TraceSubscriberStdioImpl
16 : public base::RefCountedThreadSafe<TraceSubscriberStdioImpl> {
17 public:
18 explicit TraceSubscriberStdioImpl(const FilePath& path)
19 : path_(path),
20 file_(0) {}
21
22 ~TraceSubscriberStdioImpl() {
23 CloseFile();
24 }
25
26 void OnStart() {
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>& data_ptr) {
40 trace_buffer_.AddFragment(data_ptr->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::PostBlockingPoolSequencedTask(
78 __FILE__, FROM_HERE,
79 base::Bind(&TraceSubscriberStdioImpl::OnStart, impl_.get()));
15 } 80 }
16 81
17 TraceSubscriberStdio::~TraceSubscriberStdio() { 82 TraceSubscriberStdio::~TraceSubscriberStdio() {
18 CloseFile();
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::PostBlockingPoolSequencedTask(
49 CloseFile(); 87 __FILE__, FROM_HERE,
88 base::Bind(&TraceSubscriberStdioImpl::OnEnd, impl_.get()));
50 } 89 }
51 90
52 void TraceSubscriberStdio::OnTraceDataCollected( 91 void TraceSubscriberStdio::OnTraceDataCollected(
53 const std::string& trace_fragment) { 92 const scoped_refptr<base::RefCountedString>& data_ptr) {
54 trace_buffer_.AddFragment(trace_fragment); 93 BrowserThread::PostBlockingPoolSequencedTask(
94 __FILE__, FROM_HERE,
95 base::Bind(&TraceSubscriberStdioImpl::OnData, impl_.get(), data_ptr));
55 } 96 }
56 97
57 void TraceSubscriberStdio::Write(const std::string& output_str) { 98 } // namespace content
58 if (IsValid()) { 99
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
« no previous file with comments | « content/browser/trace_subscriber_stdio.h ('k') | content/browser/trace_subscriber_stdio_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698