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 "content/browser/tracing/trace_controller_impl.h" | 5 #include "content/browser/tracing/trace_controller_impl.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/debug/trace_event.h" | 9 #include "base/debug/trace_event.h" |
10 #include "base/string_number_conversions.h" | 10 #include "base/string_number_conversions.h" |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
47 TraceController* TraceController::GetInstance() { | 47 TraceController* TraceController::GetInstance() { |
48 return TraceControllerImpl::GetInstance(); | 48 return TraceControllerImpl::GetInstance(); |
49 } | 49 } |
50 | 50 |
51 TraceControllerImpl::TraceControllerImpl() : | 51 TraceControllerImpl::TraceControllerImpl() : |
52 subscriber_(NULL), | 52 subscriber_(NULL), |
53 pending_end_ack_count_(0), | 53 pending_end_ack_count_(0), |
54 pending_bpf_ack_count_(0), | 54 pending_bpf_ack_count_(0), |
55 maximum_bpf_(0.0f), | 55 maximum_bpf_(0.0f), |
56 is_tracing_(false), | 56 is_tracing_(false), |
57 is_get_categories_(false) { | 57 is_get_category_groups_(false) { |
58 TraceLog::GetInstance()->SetNotificationCallback( | 58 TraceLog::GetInstance()->SetNotificationCallback( |
59 base::Bind(&TraceControllerImpl::OnTraceNotification, | 59 base::Bind(&TraceControllerImpl::OnTraceNotification, |
60 base::Unretained(this))); | 60 base::Unretained(this))); |
61 } | 61 } |
62 | 62 |
63 TraceControllerImpl::~TraceControllerImpl() { | 63 TraceControllerImpl::~TraceControllerImpl() { |
64 // No need to SetNotificationCallback(nil) on the TraceLog since this is a | 64 // No need to SetNotificationCallback(nil) on the TraceLog since this is a |
65 // Leaky instance. | 65 // Leaky instance. |
66 NOTREACHED(); | 66 NOTREACHED(); |
67 } | 67 } |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
99 | 99 |
100 OnTracingBegan(subscriber.get()); | 100 OnTracingBegan(subscriber.get()); |
101 BrowserThread::PostDelayedTask( | 101 BrowserThread::PostDelayedTask( |
102 BrowserThread::UI, | 102 BrowserThread::UI, |
103 FROM_HERE, | 103 FROM_HERE, |
104 base::Bind(&AutoStopTraceSubscriberStdio::EndStartupTrace, | 104 base::Bind(&AutoStopTraceSubscriberStdio::EndStartupTrace, |
105 base::Unretained(subscriber.release())), | 105 base::Unretained(subscriber.release())), |
106 base::TimeDelta::FromSeconds(delay_secs)); | 106 base::TimeDelta::FromSeconds(delay_secs)); |
107 } | 107 } |
108 | 108 |
109 bool TraceControllerImpl::GetKnownCategoriesAsync(TraceSubscriber* subscriber) { | 109 bool TraceControllerImpl::GetKnownCategoryGroupsAsync( |
110 TraceSubscriber* subscriber) { | |
dsinclair
2013/02/22 21:13:44
nit: indenting.
rterrazas
2013/02/25 05:55:02
Done.
| |
110 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 111 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
111 | 112 |
112 // Known categories come back from child processes with the EndTracingAck | 113 // Known categories come back from child processes with the EndTracingAck |
113 // message. So to get known categories, just begin and end tracing immediately | 114 // message. So to get known categories, just begin and end tracing immediately |
114 // afterwards. This will ping all the child processes for categories. | 115 // afterwards. This will ping all the child processes for categories. |
115 is_get_categories_ = true; | 116 is_get_category_groups_ = true; |
116 bool success = BeginTracing(subscriber, "*") && | 117 bool success = BeginTracing(subscriber, "*") && |
117 EndTracingAsync(subscriber); | 118 EndTracingAsync(subscriber); |
118 is_get_categories_ = success; | 119 is_get_category_groups_ = success; |
119 return success; | 120 return success; |
120 } | 121 } |
121 | 122 |
122 bool TraceControllerImpl::BeginTracing( | 123 bool TraceControllerImpl::BeginTracing(TraceSubscriber* subscriber, |
123 TraceSubscriber* subscriber, | 124 const std::string& category_patterns) { |
dsinclair
2013/02/22 21:13:44
nit: indenting.
rterrazas
2013/02/25 05:55:02
Done.
rterrazas
2013/02/25 05:55:02
Done.
| |
124 const std::vector<std::string>& included_categories, | |
125 const std::vector<std::string>& excluded_categories) { | |
126 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 125 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
127 | 126 |
128 if (!can_begin_tracing(subscriber)) | 127 if (!can_begin_tracing(subscriber)) |
129 return false; | 128 return false; |
130 | 129 |
131 // Enable tracing | 130 // Enable tracing |
132 TraceLog::GetInstance()->SetEnabled(included_categories, excluded_categories); | 131 base::debug::CategoryFilter category_filter(category_patterns); |
133 OnTracingBegan(subscriber); | 132 TraceLog::GetInstance()->SetEnabled(category_filter); |
134 | |
135 return true; | |
136 } | |
137 | |
138 bool TraceControllerImpl::BeginTracing(TraceSubscriber* subscriber, | |
139 const std::string& categories) { | |
140 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
141 | |
142 if (!can_begin_tracing(subscriber)) | |
143 return false; | |
144 | |
145 // Enable tracing | |
146 TraceLog::GetInstance()->SetEnabled(categories); | |
147 | 133 |
148 OnTracingBegan(subscriber); | 134 OnTracingBegan(subscriber); |
149 | 135 |
150 return true; | 136 return true; |
151 } | 137 } |
152 | 138 |
153 bool TraceControllerImpl::EndTracingAsync(TraceSubscriber* subscriber) { | 139 bool TraceControllerImpl::EndTracingAsync(TraceSubscriber* subscriber) { |
154 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 140 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
155 | 141 |
156 if (!can_end_tracing() || subscriber != subscriber_) | 142 if (!can_end_tracing() || subscriber != subscriber_) |
157 return false; | 143 return false; |
158 | 144 |
159 // There could be a case where there are no child processes and filters_ | 145 // There could be a case where there are no child processes and filters_ |
160 // is empty. In that case we can immediately tell the subscriber that tracing | 146 // is empty. In that case we can immediately tell the subscriber that tracing |
161 // has ended. To avoid recursive calls back to the subscriber, we will just | 147 // has ended. To avoid recursive calls back to the subscriber, we will just |
162 // use the existing asynchronous OnEndTracingAck code. | 148 // use the existing asynchronous OnEndTracingAck code. |
163 // Count myself (local trace) in pending_end_ack_count_, acked below. | 149 // Count myself (local trace) in pending_end_ack_count_, acked below. |
164 pending_end_ack_count_ = filters_.size() + 1; | 150 pending_end_ack_count_ = filters_.size() + 1; |
165 | 151 |
166 // Handle special case of zero child processes. | 152 // Handle special case of zero child processes. |
167 if (pending_end_ack_count_ == 1) { | 153 if (pending_end_ack_count_ == 1) { |
168 // Ack asynchronously now, because we don't have any children to wait for. | 154 // Ack asynchronously now, because we don't have any children to wait for. |
169 std::vector<std::string> categories; | 155 std::vector<std::string> category_groups; |
170 TraceLog::GetInstance()->GetKnownCategories(&categories); | 156 TraceLog::GetInstance()->GetKnownCategoryGroups(&category_groups); |
171 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 157 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
172 base::Bind(&TraceControllerImpl::OnEndTracingAck, | 158 base::Bind(&TraceControllerImpl::OnEndTracingAck, |
173 base::Unretained(this), categories)); | 159 base::Unretained(this), category_groups)); |
174 } | 160 } |
175 | 161 |
176 // Notify all child processes. | 162 // Notify all child processes. |
177 for (FilterMap::iterator it = filters_.begin(); it != filters_.end(); ++it) { | 163 for (FilterMap::iterator it = filters_.begin(); it != filters_.end(); ++it) { |
178 it->get()->SendEndTracing(); | 164 it->get()->SendEndTracing(); |
179 } | 165 } |
180 | 166 |
181 return true; | 167 return true; |
182 } | 168 } |
183 | 169 |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
254 void TraceControllerImpl::AddFilter(TraceMessageFilter* filter) { | 240 void TraceControllerImpl::AddFilter(TraceMessageFilter* filter) { |
255 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 241 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
256 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 242 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
257 base::Bind(&TraceControllerImpl::AddFilter, base::Unretained(this), | 243 base::Bind(&TraceControllerImpl::AddFilter, base::Unretained(this), |
258 make_scoped_refptr(filter))); | 244 make_scoped_refptr(filter))); |
259 return; | 245 return; |
260 } | 246 } |
261 | 247 |
262 filters_.insert(filter); | 248 filters_.insert(filter); |
263 if (is_tracing_enabled()) { | 249 if (is_tracing_enabled()) { |
264 filter->SendBeginTracing(included_categories_, excluded_categories_); | 250 scoped_refptr<base::RefCountedString> category_filter_str_ptr = |
251 new base::RefCountedString(); | |
252 category_filter_.ToString(&(category_filter_str_ptr->data())); | |
253 filter->SendBeginTracing(category_filter_str_ptr->data()); | |
265 if (!watch_category_.empty()) | 254 if (!watch_category_.empty()) |
266 filter->SendSetWatchEvent(watch_category_, watch_name_); | 255 filter->SendSetWatchEvent(watch_category_, watch_name_); |
267 } | 256 } |
268 } | 257 } |
269 | 258 |
270 void TraceControllerImpl::RemoveFilter(TraceMessageFilter* filter) { | 259 void TraceControllerImpl::RemoveFilter(TraceMessageFilter* filter) { |
271 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 260 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
272 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 261 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
273 base::Bind(&TraceControllerImpl::RemoveFilter, base::Unretained(this), | 262 base::Bind(&TraceControllerImpl::RemoveFilter, base::Unretained(this), |
274 make_scoped_refptr(filter))); | 263 make_scoped_refptr(filter))); |
275 return; | 264 return; |
276 } | 265 } |
277 | 266 |
278 filters_.erase(filter); | 267 filters_.erase(filter); |
279 } | 268 } |
280 | 269 |
281 void TraceControllerImpl::OnTracingBegan(TraceSubscriber* subscriber) { | 270 void TraceControllerImpl::OnTracingBegan(TraceSubscriber* subscriber) { |
282 is_tracing_ = true; | 271 is_tracing_ = true; |
283 | 272 |
284 subscriber_ = subscriber; | 273 subscriber_ = subscriber; |
285 | 274 |
286 TraceLog::GetInstance()->GetEnabledTraceCategories(&included_categories_, | 275 category_filter_ = TraceLog::GetInstance()->GetCurrentCategoryFilter(); |
287 &excluded_categories_); | 276 scoped_refptr<base::RefCountedString> category_filter_str_ptr = |
277 new base::RefCountedString(); | |
dsinclair
2013/02/22 21:13:44
nit: indenting.
| |
278 category_filter_.ToString(&(category_filter_str_ptr->data())); | |
288 // Notify all child processes. | 279 // Notify all child processes. |
289 for (FilterMap::iterator it = filters_.begin(); it != filters_.end(); ++it) { | 280 for (FilterMap::iterator it = filters_.begin(); it != filters_.end(); ++it) { |
290 it->get()->SendBeginTracing(included_categories_, excluded_categories_); | 281 it->get()->SendBeginTracing(category_filter_str_ptr->data()); |
291 } | 282 } |
292 } | 283 } |
293 | 284 |
294 void TraceControllerImpl::OnEndTracingAck( | 285 void TraceControllerImpl::OnEndTracingAck( |
295 const std::vector<std::string>& known_categories) { | 286 const std::vector<std::string>& known_category_groups) { |
296 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 287 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
297 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 288 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
298 base::Bind(&TraceControllerImpl::OnEndTracingAck, | 289 base::Bind(&TraceControllerImpl::OnEndTracingAck, |
299 base::Unretained(this), known_categories)); | 290 base::Unretained(this), known_category_groups)); |
300 return; | 291 return; |
301 } | 292 } |
302 | 293 |
303 // Merge known_categories with known_categories_ | 294 // Merge known_category_groups with known_category_groups_ |
304 known_categories_.insert(known_categories.begin(), known_categories.end()); | 295 known_category_groups_.insert(known_category_groups.begin(), |
296 known_category_groups.end()); | |
dsinclair
2013/02/22 21:13:44
nit: indenting.
rterrazas
2013/02/25 05:55:02
Done.
| |
305 | 297 |
306 if (pending_end_ack_count_ == 0) | 298 if (pending_end_ack_count_ == 0) |
307 return; | 299 return; |
308 | 300 |
309 if (--pending_end_ack_count_ == 0) { | 301 if (--pending_end_ack_count_ == 0) { |
310 // All acks have been received. | 302 // All acks have been received. |
311 is_tracing_ = false; | 303 is_tracing_ = false; |
312 | 304 |
313 // Disable local trace. | 305 // Disable local trace. |
314 TraceLog::GetInstance()->SetDisabled(); | 306 TraceLog::GetInstance()->SetDisabled(); |
315 | 307 |
316 // During this call, our OnTraceDataCollected will be | 308 // During this call, our OnTraceDataCollected will be |
317 // called with the last of the local trace data. Since we are on the UI | 309 // called with the last of the local trace data. Since we are on the UI |
318 // thread, the call to OnTraceDataCollected will be synchronous, so we can | 310 // thread, the call to OnTraceDataCollected will be synchronous, so we can |
319 // immediately call OnEndTracingComplete below. | 311 // immediately call OnEndTracingComplete below. |
320 TraceLog::GetInstance()->Flush( | 312 TraceLog::GetInstance()->Flush( |
321 base::Bind(&TraceControllerImpl::OnTraceDataCollected, | 313 base::Bind(&TraceControllerImpl::OnTraceDataCollected, |
322 base::Unretained(this))); | 314 base::Unretained(this))); |
323 | 315 |
324 // Trigger callback if one is set. | 316 // Trigger callback if one is set. |
325 if (subscriber_) { | 317 if (subscriber_) { |
326 if (is_get_categories_) | 318 if (is_get_category_groups_) |
327 subscriber_->OnKnownCategoriesCollected(known_categories_); | 319 subscriber_->OnKnownCategoriesCollected(known_category_groups_); |
328 else | 320 else |
329 subscriber_->OnEndTracingComplete(); | 321 subscriber_->OnEndTracingComplete(); |
330 // Clear subscriber so that others can use TraceController. | 322 // Clear subscriber so that others can use TraceController. |
331 subscriber_ = NULL; | 323 subscriber_ = NULL; |
332 } | 324 } |
333 | 325 |
334 is_get_categories_ = false; | 326 is_get_category_groups_ = false; |
335 } | 327 } |
336 | 328 |
337 if (pending_end_ack_count_ == 1) { | 329 if (pending_end_ack_count_ == 1) { |
338 // The last ack represents local trace, so we need to ack it now. Note that | 330 // The last ack represents local trace, so we need to ack it now. Note that |
339 // this code only executes if there were child processes. | 331 // this code only executes if there were child processes. |
340 std::vector<std::string> categories; | 332 std::vector<std::string> category_groups; |
341 TraceLog::GetInstance()->GetKnownCategories(&categories); | 333 TraceLog::GetInstance()->GetKnownCategoryGroups(&category_groups); |
342 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 334 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
343 base::Bind(&TraceControllerImpl::OnEndTracingAck, | 335 base::Bind(&TraceControllerImpl::OnEndTracingAck, |
344 base::Unretained(this), categories)); | 336 base::Unretained(this), category_groups)); |
345 } | 337 } |
346 } | 338 } |
347 | 339 |
348 void TraceControllerImpl::OnTraceDataCollected( | 340 void TraceControllerImpl::OnTraceDataCollected( |
349 const scoped_refptr<base::RefCountedString>& events_str_ptr) { | 341 const scoped_refptr<base::RefCountedString>& events_str_ptr) { |
350 // OnTraceDataCollected may be called from any browser thread, either by the | 342 // OnTraceDataCollected may be called from any browser thread, either by the |
351 // local event trace system or from child processes via TraceMessageFilter. | 343 // local event trace system or from child processes via TraceMessageFilter. |
352 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 344 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
353 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 345 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
354 base::Bind(&TraceControllerImpl::OnTraceDataCollected, | 346 base::Bind(&TraceControllerImpl::OnTraceDataCollected, |
355 base::Unretained(this), events_str_ptr)); | 347 base::Unretained(this), events_str_ptr)); |
356 return; | 348 return; |
357 } | 349 } |
358 | 350 |
359 // Drop trace events if we are just getting categories. | 351 // Drop trace events if we are just getting categories. |
360 if (subscriber_ && !is_get_categories_) | 352 if (subscriber_ && !is_get_category_groups_) |
361 subscriber_->OnTraceDataCollected(events_str_ptr); | 353 subscriber_->OnTraceDataCollected(events_str_ptr); |
362 } | 354 } |
363 | 355 |
364 void TraceControllerImpl::OnTraceNotification(int notification) { | 356 void TraceControllerImpl::OnTraceNotification(int notification) { |
365 // OnTraceNotification may be called from any browser thread, either by the | 357 // OnTraceNotification may be called from any browser thread, either by the |
366 // local event trace system or from child processes via TraceMessageFilter. | 358 // local event trace system or from child processes via TraceMessageFilter. |
367 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 359 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
368 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 360 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
369 base::Bind(&TraceControllerImpl::OnTraceNotification, | 361 base::Bind(&TraceControllerImpl::OnTraceNotification, |
370 base::Unretained(this), notification)); | 362 base::Unretained(this), notification)); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
405 // The last ack represents local trace, so we need to ack it now. Note that | 397 // The last ack represents local trace, so we need to ack it now. Note that |
406 // this code only executes if there were child processes. | 398 // this code only executes if there were child processes. |
407 float bpf = TraceLog::GetInstance()->GetBufferPercentFull(); | 399 float bpf = TraceLog::GetInstance()->GetBufferPercentFull(); |
408 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 400 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
409 base::Bind(&TraceControllerImpl::OnTraceBufferPercentFullReply, | 401 base::Bind(&TraceControllerImpl::OnTraceBufferPercentFullReply, |
410 base::Unretained(this), bpf)); | 402 base::Unretained(this), bpf)); |
411 } | 403 } |
412 } | 404 } |
413 | 405 |
414 } // namespace content | 406 } // namespace content |
OLD | NEW |