Add support for writing system traces at startup

BUG=275822
TEST=Included in  cl
[email protected], [email protected]

Review URL: https://codereview.chromium.org/23125009

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@218498 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/content/browser/android/tracing_intent_handler.cc b/content/browser/android/tracing_intent_handler.cc
index 2760916..324f805 100644
--- a/content/browser/android/tracing_intent_handler.cc
+++ b/content/browser/android/tracing_intent_handler.cc
@@ -16,7 +16,7 @@
 TracingIntentHandler* g_trace_intent_handler = NULL;
 
 TracingIntentHandler::TracingIntentHandler(const base::FilePath& path)
-    : TraceSubscriberStdio(path) {
+    : TraceSubscriberStdio(path, FILE_TYPE_ARRAY, false) {
   TraceController::GetInstance()->BeginTracing(
       this,
       std::string("-test*"),
diff --git a/content/browser/tracing/trace_controller_impl.cc b/content/browser/tracing/trace_controller_impl.cc
index 54e0c86..53e4ea5c 100644
--- a/content/browser/tracing/trace_controller_impl.cc
+++ b/content/browser/tracing/trace_controller_impl.cc
@@ -26,9 +26,11 @@
 class AutoStopTraceSubscriberStdio : public TraceSubscriberStdio {
  public:
   AutoStopTraceSubscriberStdio(const base::FilePath& file_path)
-      : TraceSubscriberStdio(file_path) {}
+      : TraceSubscriberStdio(file_path,
+                             FILE_TYPE_PROPERTY_LIST,
+                             false) {}
 
-  static void EndStartupTrace(TraceSubscriberStdio* subscriber) {
+  static void EndStartupTrace(AutoStopTraceSubscriberStdio* subscriber) {
     if (!TraceControllerImpl::GetInstance()->EndTracingAsync(subscriber))
       delete subscriber;
     // else, the tracing will end asynchronously in OnEndTracingComplete().
diff --git a/content/browser/tracing/trace_subscriber_stdio.cc b/content/browser/tracing/trace_subscriber_stdio.cc
index b5f6e82..35b96464 100644
--- a/content/browser/tracing/trace_subscriber_stdio.cc
+++ b/content/browser/tracing/trace_subscriber_stdio.cc
@@ -14,39 +14,78 @@
 namespace content {
 
 // All method calls on this class are done on a SequencedWorkerPool thread.
-class TraceSubscriberStdioImpl
-    : public base::RefCountedThreadSafe<TraceSubscriberStdioImpl> {
+class TraceSubscriberStdio::TraceSubscriberStdioWorker
+    : public base::RefCountedThreadSafe<TraceSubscriberStdioWorker> {
  public:
-  explicit TraceSubscriberStdioImpl(const base::FilePath& path)
+  TraceSubscriberStdioWorker(const base::FilePath& path,
+                             FileType file_type,
+                             bool has_system_trace)
       : path_(path),
-        file_(0) {}
+        file_type_(file_type),
+        has_system_trace_(has_system_trace),
+        file_(0),
+        needs_comma_(false),
+        wrote_trace_(false),
+        has_pending_system_trace_(false),
+        wrote_system_trace_(false) {}
 
-  void OnStart() {
+  void OnTraceStart() {
     DCHECK(!file_);
-    trace_buffer_.SetOutputCallback(
-        base::Bind(&TraceSubscriberStdioImpl::Write, this));
     file_ = file_util::OpenFile(path_, "w+");
-    if (IsValid()) {
-      LOG(INFO) << "Logging performance trace to file: " << path_.value();
-      trace_buffer_.Start();
-    } else {
+    if (!IsValid()) {
       LOG(ERROR) << "Failed to open performance trace file: " << path_.value();
+      return;
+    }
+
+    LOG(INFO) << "Logging performance trace to file: " << path_.value();
+    if (file_type_ == FILE_TYPE_PROPERTY_LIST)
+      WriteString("{\"traceEvents\":");
+    WriteString("[");
+  }
+
+  void OnTraceData(const scoped_refptr<base::RefCountedString>& data_ptr) {
+    if (!IsValid())
+      return;
+    if (needs_comma_)
+      WriteString(",");
+    WriteString(data_ptr->data());
+    needs_comma_ = true;
+  }
+
+  void OnSystemTraceData(
+      const scoped_refptr<base::RefCountedString>& data_ptr) {
+    if (wrote_trace_) {
+      WriteSystemTrace(data_ptr);
+      End();
+    } else {
+      pending_system_trace_ = data_ptr;
+      has_pending_system_trace_ = true;
     }
   }
 
-  void OnData(const scoped_refptr<base::RefCountedString>& data_ptr) {
-    trace_buffer_.AddFragment(data_ptr->data());
-  }
+  void OnTraceEnd() {
+    if (!IsValid())
+      return;
+    WriteString("]");
 
-  void OnEnd() {
-    trace_buffer_.Finish();
-    CloseFile();
+    wrote_trace_ = true;
+
+    if (!has_system_trace_ || wrote_system_trace_) {
+      End();
+      return;
+    }
+
+    WriteString(",");
+    if (has_pending_system_trace_) {
+      WriteSystemTrace(pending_system_trace_);
+      End();
+    }
   }
 
  private:
-  friend class base::RefCountedThreadSafe<TraceSubscriberStdioImpl>;
+  friend class base::RefCountedThreadSafe<TraceSubscriberStdioWorker>;
 
-  ~TraceSubscriberStdioImpl() {
+  ~TraceSubscriberStdioWorker() {
     CloseFile();
   }
 
@@ -58,32 +97,79 @@
     if (file_) {
       fclose(file_);
       file_ = 0;
+
     }
-    // This is important, as it breaks a reference cycle.
-    trace_buffer_.SetOutputCallback(
-        base::debug::TraceResultBuffer::OutputCallback());
   }
 
-  void Write(const std::string& output_str) {
+  void End() {
+    if (file_type_ == FILE_TYPE_PROPERTY_LIST)
+      WriteString("}");
+    CloseFile();
+  }
+
+  void WriteSystemTrace(const scoped_refptr<base::RefCountedString>& data_ptr) {
+    // Newlines need to be replaced with the string "\n" to be parsed correctly.
+    // Double quotes need to be replaced with the string "\"".
+    // System logs are ASCII.
+    const std::string& data = data_ptr->data();
+    const char* chars = data.c_str();
+    WriteString("\"systemTraceEvents\":\"");
+    size_t old_index = 0;
+    for (size_t new_index = data.find_first_of("\n\"");
+         std::string::npos != new_index;
+         old_index = new_index + 1,
+         new_index = data.find_first_of("\n\"", old_index)) {
+      WriteChars(chars + old_index, new_index - old_index);
+      if (chars[new_index] == '\n')
+        WriteChars("\\n", 2);
+      else
+        WriteChars("\\\"", 2);
+    }
+    WriteChars(chars + old_index, data.size() - old_index);
+    WriteString("\"");
+    wrote_system_trace_ = true;
+  }
+
+  void WriteChars(const char* output_chars, size_t size) {
+    if (size == 0)
+      return;
+
     if (IsValid()) {
-      size_t written = fwrite(output_str.data(), 1, output_str.size(), file_);
-      if (written != output_str.size()) {
+      size_t written = fwrite(output_chars, 1, size, file_);
+      if (written != size) {
         LOG(ERROR) << "Error " << ferror(file_) << " in fwrite() to trace file";
         CloseFile();
       }
     }
   }
 
+  void WriteString(const std::string& output_str) {
+    WriteChars(output_str.data(), output_str.size());
+  }
+
   base::FilePath path_;
+  const FileType file_type_;
+  const bool has_system_trace_;
   FILE* file_;
-  base::debug::TraceResultBuffer trace_buffer_;
+  bool needs_comma_;
+  bool wrote_trace_;
+  bool has_pending_system_trace_;
+  bool wrote_system_trace_;
+  scoped_refptr<base::RefCountedString> pending_system_trace_;
+  DISALLOW_COPY_AND_ASSIGN(TraceSubscriberStdioWorker);
 };
 
-TraceSubscriberStdio::TraceSubscriberStdio(const base::FilePath& path)
-    : impl_(new TraceSubscriberStdioImpl(path)) {
+TraceSubscriberStdio::TraceSubscriberStdio(const base::FilePath& path,
+                                           FileType file_type,
+                                           bool has_system_trace)
+    : worker_(new TraceSubscriberStdioWorker(path,
+                                             file_type,
+                                             has_system_trace)) {
+  if (has_system_trace)
+    CHECK_EQ(FILE_TYPE_PROPERTY_LIST, file_type);
   BrowserThread::PostBlockingPoolSequencedTask(
       __FILE__, FROM_HERE,
-      base::Bind(&TraceSubscriberStdioImpl::OnStart, impl_));
+      base::Bind(&TraceSubscriberStdioWorker::OnTraceStart, worker_));
 }
 
 TraceSubscriberStdio::~TraceSubscriberStdio() {
@@ -92,14 +178,23 @@
 void TraceSubscriberStdio::OnEndTracingComplete() {
   BrowserThread::PostBlockingPoolSequencedTask(
       __FILE__, FROM_HERE,
-      base::Bind(&TraceSubscriberStdioImpl::OnEnd, impl_));
+      base::Bind(&TraceSubscriberStdioWorker::OnTraceEnd, worker_));
 }
 
 void TraceSubscriberStdio::OnTraceDataCollected(
     const scoped_refptr<base::RefCountedString>& data_ptr) {
   BrowserThread::PostBlockingPoolSequencedTask(
       __FILE__, FROM_HERE,
-      base::Bind(&TraceSubscriberStdioImpl::OnData, impl_, data_ptr));
+      base::Bind(&TraceSubscriberStdioWorker::OnTraceData, worker_, data_ptr));
+}
+
+void TraceSubscriberStdio::OnEndSystemTracing(
+    const scoped_refptr<base::RefCountedString>& events_str_ptr) {
+  BrowserThread::PostBlockingPoolSequencedTask(
+      __FILE__, FROM_HERE,
+      base::Bind(&TraceSubscriberStdioWorker::OnSystemTraceData,
+                 worker_,
+                 events_str_ptr));
 }
 
 }  // namespace content
diff --git a/content/browser/tracing/trace_subscriber_stdio.h b/content/browser/tracing/trace_subscriber_stdio.h
index 06a70db..b9fc4f74 100644
--- a/content/browser/tracing/trace_subscriber_stdio.h
+++ b/content/browser/tracing/trace_subscriber_stdio.h
@@ -17,14 +17,24 @@
 
 namespace content {
 
-class TraceSubscriberStdioImpl;
-
 // Stdio implementation of TraceSubscriber. Use this to write traces to a file.
 class CONTENT_EXPORT TraceSubscriberStdio
     : NON_EXPORTED_BASE(public TraceSubscriber) {
  public:
-  // Creates or overwrites the specified file. Check IsValid() for success.
-  explicit TraceSubscriberStdio(const base::FilePath& path);
+  enum FileType {
+    // Output file as array, representing trace events:
+    // [event1, event2, ...]
+    FILE_TYPE_ARRAY,
+    // Output file as property list with one or two items:
+    // {traceEvents: [event1, event2, ...],
+    //  systemTraceEvents: "event1\nevent2\n..." // optional}
+    FILE_TYPE_PROPERTY_LIST
+  };
+
+  // has_system_trace indicates whether system trace events are expected.
+  TraceSubscriberStdio(const base::FilePath& path,
+                       FileType file_type,
+                       bool has_system_trace);
   virtual ~TraceSubscriberStdio();
 
   // Implementation of TraceSubscriber
@@ -32,8 +42,13 @@
   virtual void OnTraceDataCollected(
       const scoped_refptr<base::RefCountedString>& data_ptr) OVERRIDE;
 
+  // To be used as callback to DebugDaemonClient::RequestStopSystemTracing().
+  virtual void OnEndSystemTracing(
+      const scoped_refptr<base::RefCountedString>& events_str_ptr);
+
  private:
-  scoped_refptr<TraceSubscriberStdioImpl> impl_;
+  class TraceSubscriberStdioWorker;
+  scoped_refptr<TraceSubscriberStdioWorker> worker_;
   DISALLOW_COPY_AND_ASSIGN(TraceSubscriberStdio);
 };
 
diff --git a/content/browser/tracing/trace_subscriber_stdio_unittest.cc b/content/browser/tracing/trace_subscriber_stdio_unittest.cc
index 0b0e7c2e..0a3be3a 100644
--- a/content/browser/tracing/trace_subscriber_stdio_unittest.cc
+++ b/content/browser/tracing/trace_subscriber_stdio_unittest.cc
@@ -14,12 +14,14 @@
 
 class TraceSubscriberStdioTest : public ::testing::Test {};
 
-TEST_F(TraceSubscriberStdioTest, CanWriteDataToFile) {
+TEST_F(TraceSubscriberStdioTest, CanWriteArray) {
   base::ScopedTempDir trace_dir;
   ASSERT_TRUE(trace_dir.CreateUniqueTempDir());
   base::FilePath trace_file(trace_dir.path().AppendASCII("trace.txt"));
   {
-    TraceSubscriberStdio subscriber(trace_file);
+    TraceSubscriberStdio subscriber(trace_file,
+                                    TraceSubscriberStdio::FILE_TYPE_ARRAY,
+                                    false);
 
     std::string foo("foo");
     subscriber.OnTraceDataCollected(
@@ -37,4 +39,94 @@
   EXPECT_EQ("[foo,bar]", result);
 }
 
+TEST_F(TraceSubscriberStdioTest, CanWritePropertyList) {
+  base::ScopedTempDir trace_dir;
+  ASSERT_TRUE(trace_dir.CreateUniqueTempDir());
+  base::FilePath trace_file(trace_dir.path().AppendASCII("trace.txt"));
+  {
+    TraceSubscriberStdio subscriber(
+        trace_file,
+        TraceSubscriberStdio::FILE_TYPE_PROPERTY_LIST,
+        false);
+
+    std::string foo("foo");
+    subscriber.OnTraceDataCollected(
+        make_scoped_refptr(base::RefCountedString::TakeString(&foo)));
+
+    std::string bar("bar");
+    subscriber.OnTraceDataCollected(
+        make_scoped_refptr(base::RefCountedString::TakeString(&bar)));
+
+    subscriber.OnEndTracingComplete();
+  }
+  BrowserThread::GetBlockingPool()->FlushForTesting();
+  std::string result;
+  EXPECT_TRUE(file_util::ReadFileToString(trace_file, &result));
+  EXPECT_EQ("{\"traceEvents\":[foo,bar]}", result);
+}
+
+TEST_F(TraceSubscriberStdioTest, CanWriteSystemDataFirst) {
+  base::ScopedTempDir trace_dir;
+  ASSERT_TRUE(trace_dir.CreateUniqueTempDir());
+  base::FilePath trace_file(trace_dir.path().AppendASCII("trace.txt"));
+  {
+    TraceSubscriberStdio subscriber(
+        trace_file,
+        TraceSubscriberStdio::FILE_TYPE_PROPERTY_LIST,
+        true);
+
+    std::string foo("foo");
+    subscriber.OnTraceDataCollected(
+        make_scoped_refptr(base::RefCountedString::TakeString(&foo)));
+
+    std::string bar("bar");
+    subscriber.OnTraceDataCollected(
+        make_scoped_refptr(base::RefCountedString::TakeString(&bar)));
+
+    std::string systemTrace("event1\nev\"ent\"2\n");
+    subscriber.OnEndSystemTracing(
+        make_scoped_refptr(base::RefCountedString::TakeString(&systemTrace)));
+    subscriber.OnEndTracingComplete();
+  }
+  BrowserThread::GetBlockingPool()->FlushForTesting();
+  std::string result;
+  EXPECT_TRUE(file_util::ReadFileToString(trace_file, &result));
+  EXPECT_EQ(
+    "{\"traceEvents\":[foo,bar],\""
+    "systemTraceEvents\":\"event1\\nev\\\"ent\\\"2\\n\"}",
+    result);
+}
+
+TEST_F(TraceSubscriberStdioTest, CanWriteSystemDataLast) {
+  base::ScopedTempDir trace_dir;
+  ASSERT_TRUE(trace_dir.CreateUniqueTempDir());
+  base::FilePath trace_file(trace_dir.path().AppendASCII("trace.txt"));
+  {
+    TraceSubscriberStdio subscriber(
+        trace_file,
+        TraceSubscriberStdio::FILE_TYPE_PROPERTY_LIST,
+        true);
+
+    std::string foo("foo");
+    subscriber.OnTraceDataCollected(
+        make_scoped_refptr(base::RefCountedString::TakeString(&foo)));
+
+    std::string bar("bar");
+    subscriber.OnTraceDataCollected(
+        make_scoped_refptr(base::RefCountedString::TakeString(&bar)));
+
+    std::string systemTrace("event1\nev\"ent\"2\n");
+    subscriber.OnEndTracingComplete();
+    subscriber.OnEndSystemTracing(
+        make_scoped_refptr(base::RefCountedString::TakeString(&systemTrace)));
+  }
+  BrowserThread::GetBlockingPool()->FlushForTesting();
+  std::string result;
+  EXPECT_TRUE(file_util::ReadFileToString(trace_file, &result));
+  EXPECT_EQ(
+    "{\"traceEvents\":[foo,bar],\""
+    "systemTraceEvents\":\"event1\\nev\\\"ent\\\"2\\n\"}",
+    result);
+}
+
 }  // namespace content