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

Unified 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 side-by-side diff with in-line comments
Download patch
« 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 »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: content/browser/trace_subscriber_stdio.cc
diff --git a/content/browser/trace_subscriber_stdio.cc b/content/browser/trace_subscriber_stdio.cc
index 0ecd51ed73612f2b1b0744652f4fd72765eef47f..6a632be46d18df23ad7e19e94030683cdc4eaf65 100644
--- a/content/browser/trace_subscriber_stdio.cc
+++ b/content/browser/trace_subscriber_stdio.cc
@@ -6,60 +6,94 @@
#include "base/bind.h"
#include "base/logging.h"
+#include "base/threading/sequenced_worker_pool.h"
+#include "content/public/browser/browser_thread.h"
-TraceSubscriberStdio::TraceSubscriberStdio() : file_(0) {
-}
+namespace content {
-TraceSubscriberStdio::TraceSubscriberStdio(const FilePath& path) : file_(0) {
- OpenFile(path);
-}
+// All method calls on this class are done on a SequencedWorkerPool thread.
+class TraceSubscriberStdioImpl
+ : public base::RefCountedThreadSafe<TraceSubscriberStdioImpl> {
+ public:
+ explicit TraceSubscriberStdioImpl(const FilePath& path)
+ : path_(path),
+ file_(0) {}
-TraceSubscriberStdio::~TraceSubscriberStdio() {
- CloseFile();
-}
+ ~TraceSubscriberStdioImpl() {
+ CloseFile();
+ }
+
+ void OnStart() {
+ DCHECK(!file_);
+ file_ = file_util::OpenFile(path_, "w+");
+ if (IsValid()) {
+ LOG(INFO) << "Logging performance trace to file: " << path_.value();
+ trace_buffer_.SetOutputCallback(
+ base::Bind(&TraceSubscriberStdioImpl::Write, this));
+ trace_buffer_.Start();
+ } else {
+ LOG(ERROR) << "Failed to open performance trace file: " << path_.value();
+ }
+ }
-bool TraceSubscriberStdio::OpenFile(const FilePath& path) {
- LOG(INFO) << "Logging performance trace to file: " << path.value();
- CloseFile();
- file_ = file_util::OpenFile(path, "w+");
- if (IsValid()) {
- trace_buffer_.SetOutputCallback(base::Bind(&TraceSubscriberStdio::Write,
- base::Unretained(this)));
- trace_buffer_.Start();
- return true;
- } else {
- LOG(ERROR) << "Failed to open performance trace file: " << path.value();
- return false;
+ void OnData(const scoped_refptr<base::RefCountedString>& data_ptr) {
+ trace_buffer_.AddFragment(data_ptr->data());
+ }
+
+ void OnEnd() {
+ trace_buffer_.Finish();
+ CloseFile();
+ }
+
+ private:
+ bool IsValid() {
+ return file_ && (0 == ferror(file_));
+ }
+
+ void CloseFile() {
+ if (file_) {
+ fclose(file_);
+ file_ = 0;
+ }
}
-}
-void TraceSubscriberStdio::CloseFile() {
- if (file_) {
- fclose(file_);
- file_ = 0;
+ void Write(const std::string& output_str) {
+ if (IsValid()) {
+ size_t written = fwrite(output_str.data(), 1, output_str.size(), file_);
+ if (written != output_str.size()) {
+ LOG(ERROR) << "Error " << ferror(file_) << " in fwrite() to trace file";
+ CloseFile();
+ }
+ }
}
+
+ FilePath path_;
+ FILE* file_;
+ base::debug::TraceResultBuffer trace_buffer_;
+};
+
+TraceSubscriberStdio::TraceSubscriberStdio(const FilePath& path)
+ : impl_(new TraceSubscriberStdioImpl(path)) {
+ BrowserThread::PostBlockingPoolSequencedTask(
+ __FILE__, FROM_HERE,
+ base::Bind(&TraceSubscriberStdioImpl::OnStart, impl_.get()));
}
-bool TraceSubscriberStdio::IsValid() {
- return file_ && (0 == ferror(file_));
+TraceSubscriberStdio::~TraceSubscriberStdio() {
}
void TraceSubscriberStdio::OnEndTracingComplete() {
- trace_buffer_.Finish();
- CloseFile();
+ BrowserThread::PostBlockingPoolSequencedTask(
+ __FILE__, FROM_HERE,
+ base::Bind(&TraceSubscriberStdioImpl::OnEnd, impl_.get()));
}
void TraceSubscriberStdio::OnTraceDataCollected(
- const std::string& trace_fragment) {
- trace_buffer_.AddFragment(trace_fragment);
+ const scoped_refptr<base::RefCountedString>& data_ptr) {
+ BrowserThread::PostBlockingPoolSequencedTask(
+ __FILE__, FROM_HERE,
+ base::Bind(&TraceSubscriberStdioImpl::OnData, impl_.get(), data_ptr));
}
-void TraceSubscriberStdio::Write(const std::string& output_str) {
- if (IsValid()) {
- size_t written = fwrite(output_str.data(), 1, output_str.size(), file_);
- if (written != output_str.size()) {
- LOG(ERROR) << "Error " << ferror(file_) << " when writing to trace file";
- CloseFile();
- }
- }
-}
+} // namespace content
+
« 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