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