Chromium Code Reviews| 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/debug/trace_event.h" |
| 8 #include "base/memory/singleton.h" | 8 #include "base/memory/singleton.h" |
| 9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "content/public/browser/trace_controller.h" | 10 #include "content/public/browser/trace_controller.h" |
| 11 #include "content/public/browser/trace_subscriber.h" | 11 #include "content/public/browser/trace_subscriber.h" |
| 12 #include "content/public/test/test_utils.h" | 12 #include "content/public/test/test_utils.h" |
| 13 | 13 |
| 14 namespace { | 14 namespace { |
| 15 | 15 |
| 16 class InProcessTraceController : public content::TraceSubscriber { | 16 class InProcessTraceController : public content::TraceSubscriber { |
| 17 public: | 17 public: |
| 18 static InProcessTraceController* GetInstance() { | 18 static InProcessTraceController* GetInstance() { |
| 19 return Singleton<InProcessTraceController>::get(); | 19 return Singleton<InProcessTraceController>::get(); |
| 20 } | 20 } |
| 21 | 21 |
| 22 InProcessTraceController() {} | 22 InProcessTraceController() |
| 23 : is_waiting_on_watch_(false) | |
| 24 , got_watch_notification_(false) {} | |
| 23 virtual ~InProcessTraceController() {} | 25 virtual ~InProcessTraceController() {} |
| 24 | 26 |
| 25 bool BeginTracing(const std::string& categories) { | 27 bool BeginTracing(const std::string& categories) { |
| 26 return content::TraceController::GetInstance()->BeginTracing( | 28 return content::TraceController::GetInstance()->BeginTracing( |
| 27 this, categories); | 29 this, categories); |
| 28 } | 30 } |
| 29 | 31 |
| 32 bool BeginTracingWithWatch(const std::string& categories, | |
| 33 const char* category_name, | |
| 34 const char* event_name, | |
| 35 int num_occurrences) { | |
| 36 return BeginTracing(categories) && | |
| 37 content::TraceController::GetInstance()->SetWatchEvent( | |
| 38 this, category_name, event_name, num_occurrences); | |
| 39 } | |
| 40 | |
| 41 bool WaitForWatchEvent(base::TimeDelta timeout) { | |
| 42 if (got_watch_notification_) | |
| 43 return true; | |
| 44 | |
| 45 if (timeout != base::TimeDelta()) { | |
| 46 timer_.Start(FROM_HERE, timeout, this, | |
| 47 &InProcessTraceController::Timeout); | |
| 48 } | |
| 49 | |
| 50 is_waiting_on_watch_ = true; | |
|
ccameron
2012/08/21 01:06:34
Race: we will use is_waiting_on_watch to see if it
jbates
2012/08/23 22:45:28
As discussed, the missing detail is that these met
| |
| 51 message_loop_runner_ = new content::MessageLoopRunner; | |
| 52 message_loop_runner_->Run(); | |
| 53 is_waiting_on_watch_ = false; | |
| 54 | |
| 55 return got_watch_notification_; | |
| 56 } | |
| 57 | |
| 30 bool EndTracing(std::string* json_trace_output) { | 58 bool EndTracing(std::string* json_trace_output) { |
| 31 using namespace base::debug; | 59 using namespace base::debug; |
| 32 | 60 |
| 33 TraceResultBuffer::SimpleOutput output; | 61 TraceResultBuffer::SimpleOutput output; |
| 34 trace_buffer_.SetOutputCallback(output.GetCallback()); | 62 trace_buffer_.SetOutputCallback(output.GetCallback()); |
| 35 | 63 |
| 36 trace_buffer_.Start(); | 64 trace_buffer_.Start(); |
| 37 if (!content::TraceController::GetInstance()->EndTracingAsync(this)) | 65 if (!content::TraceController::GetInstance()->EndTracingAsync(this)) |
| 38 return false; | 66 return false; |
| 39 // Wait for OnEndTracingComplete() to quit the message loop. | 67 // Wait for OnEndTracingComplete() to quit the message loop. |
| 40 // OnTraceDataCollected may be called multiple times while blocking here. | 68 // OnTraceDataCollected may be called multiple times while blocking here. |
| 41 message_loop_runner_ = new content::MessageLoopRunner; | 69 message_loop_runner_ = new content::MessageLoopRunner; |
| 42 message_loop_runner_->Run(); | 70 message_loop_runner_->Run(); |
| 43 trace_buffer_.Finish(); | 71 trace_buffer_.Finish(); |
| 44 trace_buffer_.SetOutputCallback(TraceResultBuffer::OutputCallback()); | 72 trace_buffer_.SetOutputCallback(TraceResultBuffer::OutputCallback()); |
| 45 | 73 |
| 46 *json_trace_output = output.json_output; | 74 *json_trace_output = output.json_output; |
| 75 | |
| 76 // Watch notifications can occur during this method's message loop run, but | |
| 77 // not after, so clear them here. | |
| 78 got_watch_notification_ = false; | |
| 47 return true; | 79 return true; |
| 48 } | 80 } |
| 49 | 81 |
| 50 private: | 82 private: |
| 51 friend struct DefaultSingletonTraits<InProcessTraceController>; | 83 friend struct DefaultSingletonTraits<InProcessTraceController>; |
| 52 | 84 |
| 53 // TraceSubscriber | 85 // TraceSubscriber implementation |
| 54 virtual void OnEndTracingComplete() OVERRIDE { | 86 virtual void OnEndTracingComplete() OVERRIDE { |
| 55 message_loop_runner_->Quit(); | 87 message_loop_runner_->Quit(); |
| 56 } | 88 } |
| 57 | 89 |
| 58 // TraceSubscriber | 90 // TraceSubscriber implementation |
| 59 virtual void OnTraceDataCollected( | 91 virtual void OnTraceDataCollected( |
| 60 const scoped_refptr<base::RefCountedString>& trace_fragment) OVERRIDE { | 92 const scoped_refptr<base::RefCountedString>& trace_fragment) OVERRIDE { |
| 61 trace_buffer_.AddFragment(trace_fragment->data()); | 93 trace_buffer_.AddFragment(trace_fragment->data()); |
| 62 } | 94 } |
| 63 | 95 |
| 96 // TraceSubscriber implementation | |
| 97 virtual void OnEventWatchNotification() { | |
| 98 got_watch_notification_ = true; | |
| 99 timer_.Stop(); | |
| 100 if (is_waiting_on_watch_) | |
|
ccameron
2012/08/21 01:06:34
Race: is_waiting_in_watch_ can be true while messa
| |
| 101 message_loop_runner_->Quit(); | |
| 102 } | |
| 103 | |
| 104 void Timeout() { | |
| 105 DCHECK(is_waiting_on_watch_); | |
| 106 message_loop_runner_->Quit(); | |
| 107 } | |
| 108 | |
| 64 // For collecting trace data asynchronously. | 109 // For collecting trace data asynchronously. |
| 65 base::debug::TraceResultBuffer trace_buffer_; | 110 base::debug::TraceResultBuffer trace_buffer_; |
| 66 | 111 |
| 67 scoped_refptr<content::MessageLoopRunner> message_loop_runner_; | 112 scoped_refptr<content::MessageLoopRunner> message_loop_runner_; |
| 68 | 113 |
| 114 base::OneShotTimer<InProcessTraceController> timer_; | |
| 115 | |
| 116 bool is_waiting_on_watch_; | |
| 117 bool got_watch_notification_; | |
| 118 | |
| 69 DISALLOW_COPY_AND_ASSIGN(InProcessTraceController); | 119 DISALLOW_COPY_AND_ASSIGN(InProcessTraceController); |
| 70 }; | 120 }; |
| 71 | 121 |
| 72 } // namespace | 122 } // namespace |
| 73 | 123 |
| 74 namespace tracing { | 124 namespace tracing { |
| 75 | 125 |
| 76 bool BeginTracing(const std::string& categories) { | 126 bool BeginTracing(const std::string& categories) { |
| 77 return InProcessTraceController::GetInstance()->BeginTracing(categories); | 127 return InProcessTraceController::GetInstance()->BeginTracing(categories); |
| 78 } | 128 } |
| 79 | 129 |
| 130 bool BeginTracingWithWatch(const std::string& categories, | |
| 131 const char* category_name, | |
| 132 const char* event_name, | |
| 133 int num_occurrences) { | |
| 134 return InProcessTraceController::GetInstance()->BeginTracingWithWatch( | |
| 135 categories, category_name, event_name, num_occurrences); | |
| 136 } | |
| 137 | |
| 138 bool WaitForWatchEvent(base::TimeDelta timeout) { | |
| 139 return InProcessTraceController::GetInstance()->WaitForWatchEvent(timeout); | |
| 140 } | |
| 141 | |
| 80 bool EndTracing(std::string* json_trace_output) { | 142 bool EndTracing(std::string* json_trace_output) { |
| 81 return InProcessTraceController::GetInstance()->EndTracing(json_trace_output); | 143 return InProcessTraceController::GetInstance()->EndTracing(json_trace_output); |
| 82 } | 144 } |
| 83 | 145 |
| 84 } // namespace tracing | 146 } // namespace tracing |
| 85 | 147 |
| OLD | NEW |