| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/test/base/tracing.h" | 5 #include "chrome/test/base/tracing.h" |
| 6 | 6 |
| 7 #include "base/debug/trace_event.h" | 7 #include "base/file_util.h" |
| 8 #include "base/files/file_path.h" |
| 8 #include "base/memory/singleton.h" | 9 #include "base/memory/singleton.h" |
| 9 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| 11 #include "base/strings/string_util.h" |
| 12 #include "base/timer/timer.h" |
| 10 #include "content/public/browser/browser_thread.h" | 13 #include "content/public/browser/browser_thread.h" |
| 11 #include "content/public/browser/trace_controller.h" | 14 #include "content/public/browser/tracing_controller.h" |
| 12 #include "content/public/browser/trace_subscriber.h" | |
| 13 #include "content/public/test/test_utils.h" | 15 #include "content/public/test/test_utils.h" |
| 14 | 16 |
| 15 namespace { | 17 namespace { |
| 16 | 18 |
| 17 using content::BrowserThread; | 19 using content::BrowserThread; |
| 18 | 20 |
| 19 class InProcessTraceController : public content::TraceSubscriber { | 21 class InProcessTraceController { |
| 20 public: | 22 public: |
| 21 static InProcessTraceController* GetInstance() { | 23 static InProcessTraceController* GetInstance() { |
| 22 return Singleton<InProcessTraceController>::get(); | 24 return Singleton<InProcessTraceController>::get(); |
| 23 } | 25 } |
| 24 | 26 |
| 25 InProcessTraceController() | 27 InProcessTraceController() |
| 26 : is_waiting_on_watch_(false), | 28 : is_waiting_on_watch_(false), |
| 27 watch_notification_count_(0) {} | 29 watch_notification_count_(0) {} |
| 28 virtual ~InProcessTraceController() {} | 30 virtual ~InProcessTraceController() {} |
| 29 | 31 |
| 30 bool BeginTracing(const std::string& category_patterns) { | 32 bool BeginTracing(const std::string& category_patterns) { |
| 31 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 33 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 32 return content::TraceController::GetInstance()->BeginTracing( | 34 return content::TracingController::GetInstance()->EnableRecording( |
| 33 this, category_patterns, base::debug::TraceLog::RECORD_UNTIL_FULL); | 35 category_patterns, content::TracingController::DEFAULT_OPTIONS, |
| 36 content::TracingController::EnableRecordingDoneCallback()); |
| 37 return true; |
| 34 } | 38 } |
| 35 | 39 |
| 36 bool BeginTracingWithWatch(const std::string& category_patterns, | 40 bool BeginTracingWithWatch(const std::string& category_patterns, |
| 37 const std::string& category_name, | 41 const std::string& category_name, |
| 38 const std::string& event_name, | 42 const std::string& event_name, |
| 39 int num_occurrences) { | 43 int num_occurrences) { |
| 40 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 44 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 41 DCHECK(num_occurrences > 0); | 45 DCHECK(num_occurrences > 0); |
| 42 watch_notification_count_ = num_occurrences; | 46 watch_notification_count_ = num_occurrences; |
| 43 return BeginTracing(category_patterns) && | 47 return content::TracingController::GetInstance()->SetWatchEvent( |
| 44 content::TraceController::GetInstance()->SetWatchEvent( | 48 category_name, event_name, |
| 45 this, category_name, event_name); | 49 base::Bind(&InProcessTraceController::OnWatchEventMatched, |
| 50 base::Unretained(this))) && |
| 51 BeginTracing(category_patterns); |
| 46 } | 52 } |
| 47 | 53 |
| 48 bool WaitForWatchEvent(base::TimeDelta timeout) { | 54 bool WaitForWatchEvent(base::TimeDelta timeout) { |
| 49 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 55 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 50 if (watch_notification_count_ == 0) | 56 if (watch_notification_count_ == 0) |
| 51 return true; | 57 return true; |
| 52 | 58 |
| 53 if (timeout != base::TimeDelta()) { | 59 if (timeout != base::TimeDelta()) { |
| 54 timer_.Start(FROM_HERE, timeout, this, | 60 timer_.Start(FROM_HERE, timeout, this, |
| 55 &InProcessTraceController::Timeout); | 61 &InProcessTraceController::Timeout); |
| 56 } | 62 } |
| 57 | 63 |
| 58 is_waiting_on_watch_ = true; | 64 is_waiting_on_watch_ = true; |
| 59 message_loop_runner_ = new content::MessageLoopRunner; | 65 message_loop_runner_ = new content::MessageLoopRunner; |
| 60 message_loop_runner_->Run(); | 66 message_loop_runner_->Run(); |
| 61 is_waiting_on_watch_ = false; | 67 is_waiting_on_watch_ = false; |
| 62 | 68 |
| 63 return watch_notification_count_ == 0; | 69 return watch_notification_count_ == 0; |
| 64 } | 70 } |
| 65 | 71 |
| 66 bool EndTracing(std::string* json_trace_output) { | 72 bool EndTracing(std::string* json_trace_output) { |
| 67 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 73 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 68 using namespace base::debug; | 74 using namespace base::debug; |
| 69 | 75 |
| 70 TraceResultBuffer::SimpleOutput output; | 76 if (!content::TracingController::GetInstance()->DisableRecording( |
| 71 trace_buffer_.SetOutputCallback(output.GetCallback()); | 77 base::FilePath(), |
| 78 base::Bind(&InProcessTraceController::OnTraceDataCollected, |
| 79 base::Unretained(this), |
| 80 base::Unretained(json_trace_output)))) |
| 81 return false; |
| 72 | 82 |
| 73 trace_buffer_.Start(); | |
| 74 if (!content::TraceController::GetInstance()->EndTracingAsync(this)) | |
| 75 return false; | |
| 76 // Wait for OnEndTracingComplete() to quit the message loop. | 83 // Wait for OnEndTracingComplete() to quit the message loop. |
| 77 // OnTraceDataCollected may be called multiple times while blocking here. | |
| 78 message_loop_runner_ = new content::MessageLoopRunner; | 84 message_loop_runner_ = new content::MessageLoopRunner; |
| 79 message_loop_runner_->Run(); | 85 message_loop_runner_->Run(); |
| 80 trace_buffer_.Finish(); | |
| 81 trace_buffer_.SetOutputCallback(TraceResultBuffer::OutputCallback()); | |
| 82 | |
| 83 *json_trace_output = output.json_output; | |
| 84 | 86 |
| 85 // Watch notifications can occur during this method's message loop run, but | 87 // Watch notifications can occur during this method's message loop run, but |
| 86 // not after, so clear them here. | 88 // not after, so clear them here. |
| 87 watch_notification_count_ = 0; | 89 watch_notification_count_ = 0; |
| 88 return true; | 90 return true; |
| 89 } | 91 } |
| 90 | 92 |
| 91 private: | 93 private: |
| 92 friend struct DefaultSingletonTraits<InProcessTraceController>; | 94 friend struct DefaultSingletonTraits<InProcessTraceController>; |
| 93 | 95 |
| 94 // TraceSubscriber implementation | 96 void OnEndTracingComplete() { |
| 95 virtual void OnEndTracingComplete() OVERRIDE { | |
| 96 message_loop_runner_->Quit(); | 97 message_loop_runner_->Quit(); |
| 97 } | 98 } |
| 98 | 99 |
| 99 // TraceSubscriber implementation | 100 void OnTraceDataCollected(std::string* json_trace_output, |
| 100 virtual void OnTraceDataCollected( | 101 const base::FilePath& path) { |
| 101 const scoped_refptr<base::RefCountedString>& trace_fragment) OVERRIDE { | 102 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 102 trace_buffer_.AddFragment(trace_fragment->data()); | 103 base::Bind(&InProcessTraceController::ReadTraceData, |
| 104 base::Unretained(this), |
| 105 base::Unretained(json_trace_output), |
| 106 path)); |
| 103 } | 107 } |
| 104 | 108 |
| 105 // TraceSubscriber implementation | 109 void ReadTraceData(std::string* json_trace_output, |
| 106 virtual void OnEventWatchNotification() OVERRIDE { | 110 const base::FilePath& path) { |
| 111 json_trace_output->clear(); |
| 112 bool ok = base::ReadFileToString(path, json_trace_output); |
| 113 DCHECK(ok); |
| 114 base::DeleteFile(path, false); |
| 115 |
| 116 // The callers expect an array of trace events. |
| 117 const char* preamble = "{\"traceEvents\": "; |
| 118 const char* trailout = "}"; |
| 119 DCHECK(StartsWithASCII(*json_trace_output, preamble, true)); |
| 120 DCHECK(EndsWith(*json_trace_output, trailout, true)); |
| 121 json_trace_output->erase(0, strlen(preamble)); |
| 122 json_trace_output->erase(json_trace_output->end() - 1); |
| 123 |
| 124 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 125 base::Bind(&InProcessTraceController::OnEndTracingComplete, |
| 126 base::Unretained(this))); |
| 127 } |
| 128 |
| 129 void OnWatchEventMatched() { |
| 107 if (watch_notification_count_ == 0) | 130 if (watch_notification_count_ == 0) |
| 108 return; | 131 return; |
| 109 if (--watch_notification_count_ == 0) { | 132 if (--watch_notification_count_ == 0) { |
| 110 timer_.Stop(); | 133 timer_.Stop(); |
| 111 if (is_waiting_on_watch_) | 134 if (is_waiting_on_watch_) |
| 112 message_loop_runner_->Quit(); | 135 message_loop_runner_->Quit(); |
| 113 } | 136 } |
| 114 } | 137 } |
| 115 | 138 |
| 116 void Timeout() { | 139 void Timeout() { |
| 117 DCHECK(is_waiting_on_watch_); | 140 DCHECK(is_waiting_on_watch_); |
| 118 message_loop_runner_->Quit(); | 141 message_loop_runner_->Quit(); |
| 119 } | 142 } |
| 120 | 143 |
| 121 // For collecting trace data asynchronously. | |
| 122 base::debug::TraceResultBuffer trace_buffer_; | |
| 123 | |
| 124 scoped_refptr<content::MessageLoopRunner> message_loop_runner_; | 144 scoped_refptr<content::MessageLoopRunner> message_loop_runner_; |
| 125 | 145 |
| 126 base::OneShotTimer<InProcessTraceController> timer_; | 146 base::OneShotTimer<InProcessTraceController> timer_; |
| 127 | 147 |
| 128 bool is_waiting_on_watch_; | 148 bool is_waiting_on_watch_; |
| 129 int watch_notification_count_; | 149 int watch_notification_count_; |
| 130 | 150 |
| 131 DISALLOW_COPY_AND_ASSIGN(InProcessTraceController); | 151 DISALLOW_COPY_AND_ASSIGN(InProcessTraceController); |
| 132 }; | 152 }; |
| 133 | 153 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 151 bool WaitForWatchEvent(base::TimeDelta timeout) { | 171 bool WaitForWatchEvent(base::TimeDelta timeout) { |
| 152 return InProcessTraceController::GetInstance()->WaitForWatchEvent(timeout); | 172 return InProcessTraceController::GetInstance()->WaitForWatchEvent(timeout); |
| 153 } | 173 } |
| 154 | 174 |
| 155 bool EndTracing(std::string* json_trace_output) { | 175 bool EndTracing(std::string* json_trace_output) { |
| 156 return InProcessTraceController::GetInstance()->EndTracing(json_trace_output); | 176 return InProcessTraceController::GetInstance()->EndTracing(json_trace_output); |
| 157 } | 177 } |
| 158 | 178 |
| 159 } // namespace tracing | 179 } // namespace tracing |
| 160 | 180 |
| OLD | NEW |