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) { | |
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 TraceLog::GetInstance()->trace_options()) && | 118 TraceLog::GetInstance()->trace_options()) && |
118 EndTracingAsync(subscriber); | 119 EndTracingAsync(subscriber); |
119 is_get_categories_ = success; | 120 is_get_category_groups_ = success; |
120 return success; | 121 return success; |
121 } | 122 } |
122 | 123 |
123 bool TraceControllerImpl::BeginTracing( | |
124 TraceSubscriber* subscriber, | |
125 const std::vector<std::string>& included_categories, | |
126 const std::vector<std::string>& excluded_categories, | |
127 base::debug::TraceLog::Options options) { | |
128 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
129 | |
130 if (!can_begin_tracing(subscriber)) | |
131 return false; | |
132 | |
133 // Enable tracing | |
134 TraceLog::GetInstance()->SetEnabled(included_categories, excluded_categories, | |
135 options); | |
136 OnTracingBegan(subscriber); | |
137 | |
138 return true; | |
139 } | |
140 | |
141 bool TraceControllerImpl::BeginTracing(TraceSubscriber* subscriber, | 124 bool TraceControllerImpl::BeginTracing(TraceSubscriber* subscriber, |
142 const std::string& categories, | 125 const std::string& category_patterns, |
143 base::debug::TraceLog::Options options) { | 126 base::debug::TraceLog::Options options) { |
144 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 127 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
145 | 128 |
146 if (!can_begin_tracing(subscriber)) | 129 if (!can_begin_tracing(subscriber)) |
147 return false; | 130 return false; |
148 | 131 |
149 // Enable tracing | 132 // Enable tracing |
150 TraceLog::GetInstance()->SetEnabled(categories, options); | 133 TraceLog::GetInstance()->SetEnabled( |
134 base::debug::CategoryFilter(category_patterns), options); | |
151 | 135 |
152 OnTracingBegan(subscriber); | 136 OnTracingBegan(subscriber); |
153 | 137 |
154 return true; | 138 return true; |
155 } | 139 } |
156 | 140 |
157 bool TraceControllerImpl::EndTracingAsync(TraceSubscriber* subscriber) { | 141 bool TraceControllerImpl::EndTracingAsync(TraceSubscriber* subscriber) { |
158 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 142 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
159 | 143 |
160 if (!can_end_tracing() || subscriber != subscriber_) | 144 if (!can_end_tracing() || subscriber != subscriber_) |
161 return false; | 145 return false; |
162 | 146 |
163 // There could be a case where there are no child processes and filters_ | 147 // There could be a case where there are no child processes and filters_ |
164 // is empty. In that case we can immediately tell the subscriber that tracing | 148 // is empty. In that case we can immediately tell the subscriber that tracing |
165 // has ended. To avoid recursive calls back to the subscriber, we will just | 149 // has ended. To avoid recursive calls back to the subscriber, we will just |
166 // use the existing asynchronous OnEndTracingAck code. | 150 // use the existing asynchronous OnEndTracingAck code. |
167 // Count myself (local trace) in pending_end_ack_count_, acked below. | 151 // Count myself (local trace) in pending_end_ack_count_, acked below. |
168 pending_end_ack_count_ = filters_.size() + 1; | 152 pending_end_ack_count_ = filters_.size() + 1; |
169 | 153 |
170 // Handle special case of zero child processes. | 154 // Handle special case of zero child processes. |
171 if (pending_end_ack_count_ == 1) { | 155 if (pending_end_ack_count_ == 1) { |
172 // Ack asynchronously now, because we don't have any children to wait for. | 156 // Ack asynchronously now, because we don't have any children to wait for. |
173 std::vector<std::string> categories; | 157 std::vector<std::string> category_groups; |
174 TraceLog::GetInstance()->GetKnownCategories(&categories); | 158 TraceLog::GetInstance()->GetKnownCategoryGroups(&category_groups); |
175 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 159 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
176 base::Bind(&TraceControllerImpl::OnEndTracingAck, | 160 base::Bind(&TraceControllerImpl::OnEndTracingAck, |
177 base::Unretained(this), categories)); | 161 base::Unretained(this), category_groups)); |
178 } | 162 } |
179 | 163 |
180 // Notify all child processes. | 164 // Notify all child processes. |
181 for (FilterMap::iterator it = filters_.begin(); it != filters_.end(); ++it) { | 165 for (FilterMap::iterator it = filters_.begin(); it != filters_.end(); ++it) { |
182 it->get()->SendEndTracing(); | 166 it->get()->SendEndTracing(); |
183 } | 167 } |
184 | 168 |
185 return true; | 169 return true; |
186 } | 170 } |
187 | 171 |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
258 void TraceControllerImpl::AddFilter(TraceMessageFilter* filter) { | 242 void TraceControllerImpl::AddFilter(TraceMessageFilter* filter) { |
259 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 243 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
260 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 244 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
261 base::Bind(&TraceControllerImpl::AddFilter, base::Unretained(this), | 245 base::Bind(&TraceControllerImpl::AddFilter, base::Unretained(this), |
262 make_scoped_refptr(filter))); | 246 make_scoped_refptr(filter))); |
263 return; | 247 return; |
264 } | 248 } |
265 | 249 |
266 filters_.insert(filter); | 250 filters_.insert(filter); |
267 if (is_tracing_enabled()) { | 251 if (is_tracing_enabled()) { |
268 filter->SendBeginTracing(included_categories_, excluded_categories_, | 252 std::string cf_str = category_filter_.ToString(); |
269 trace_options_); | 253 filter->SendBeginTracing(cf_str, trace_options_); |
dsinclair
2013/03/12 21:27:24
Do you need to store cf_str or can you do filter->
rterrazas
2013/03/20 08:48:49
Nope, left it there by mistake. Done.
| |
270 if (!watch_category_.empty()) | 254 if (!watch_category_.empty()) |
271 filter->SendSetWatchEvent(watch_category_, watch_name_); | 255 filter->SendSetWatchEvent(watch_category_, watch_name_); |
272 } | 256 } |
273 } | 257 } |
274 | 258 |
275 void TraceControllerImpl::RemoveFilter(TraceMessageFilter* filter) { | 259 void TraceControllerImpl::RemoveFilter(TraceMessageFilter* filter) { |
276 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 260 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
277 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 261 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
278 base::Bind(&TraceControllerImpl::RemoveFilter, base::Unretained(this), | 262 base::Bind(&TraceControllerImpl::RemoveFilter, base::Unretained(this), |
279 make_scoped_refptr(filter))); | 263 make_scoped_refptr(filter))); |
280 return; | 264 return; |
281 } | 265 } |
282 | 266 |
283 filters_.erase(filter); | 267 filters_.erase(filter); |
284 } | 268 } |
285 | 269 |
286 void TraceControllerImpl::OnTracingBegan(TraceSubscriber* subscriber) { | 270 void TraceControllerImpl::OnTracingBegan(TraceSubscriber* subscriber) { |
287 is_tracing_ = true; | 271 is_tracing_ = true; |
288 | 272 |
289 subscriber_ = subscriber; | 273 subscriber_ = subscriber; |
290 | 274 |
291 TraceLog::GetInstance()->GetEnabledTraceCategories(&included_categories_, | 275 category_filter_ = TraceLog::GetInstance()->GetCurrentCategoryFilter(); |
292 &excluded_categories_); | 276 std::string cf_str = category_filter_.ToString(); |
293 trace_options_ = TraceLog::GetInstance()->trace_options(); | 277 trace_options_ = TraceLog::GetInstance()->trace_options(); |
294 | 278 |
295 // Notify all child processes. | 279 // Notify all child processes. |
296 for (FilterMap::iterator it = filters_.begin(); it != filters_.end(); ++it) { | 280 for (FilterMap::iterator it = filters_.begin(); it != filters_.end(); ++it) { |
297 it->get()->SendBeginTracing(included_categories_, excluded_categories_, | 281 it->get()->SendBeginTracing(cf_str, trace_options_); |
298 trace_options_); | |
299 } | 282 } |
300 } | 283 } |
301 | 284 |
302 void TraceControllerImpl::OnEndTracingAck( | 285 void TraceControllerImpl::OnEndTracingAck( |
303 const std::vector<std::string>& known_categories) { | 286 const std::vector<std::string>& known_category_groups) { |
304 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 287 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
305 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 288 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
306 base::Bind(&TraceControllerImpl::OnEndTracingAck, | 289 base::Bind(&TraceControllerImpl::OnEndTracingAck, |
307 base::Unretained(this), known_categories)); | 290 base::Unretained(this), known_category_groups)); |
308 return; | 291 return; |
309 } | 292 } |
310 | 293 |
311 // Merge known_categories with known_categories_ | 294 // Merge known_category_groups with known_category_groups_ |
312 known_categories_.insert(known_categories.begin(), known_categories.end()); | 295 known_category_groups_.insert(known_category_groups.begin(), |
296 known_category_groups.end()); | |
313 | 297 |
314 if (pending_end_ack_count_ == 0) | 298 if (pending_end_ack_count_ == 0) |
315 return; | 299 return; |
316 | 300 |
317 if (--pending_end_ack_count_ == 0) { | 301 if (--pending_end_ack_count_ == 0) { |
318 // All acks have been received. | 302 // All acks have been received. |
319 is_tracing_ = false; | 303 is_tracing_ = false; |
320 | 304 |
321 // Disable local trace. | 305 // Disable local trace. |
322 TraceLog::GetInstance()->SetDisabled(); | 306 TraceLog::GetInstance()->SetDisabled(); |
323 | 307 |
324 // During this call, our OnTraceDataCollected will be | 308 // During this call, our OnTraceDataCollected will be |
325 // 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 |
326 // thread, the call to OnTraceDataCollected will be synchronous, so we can | 310 // thread, the call to OnTraceDataCollected will be synchronous, so we can |
327 // immediately call OnEndTracingComplete below. | 311 // immediately call OnEndTracingComplete below. |
328 TraceLog::GetInstance()->Flush( | 312 TraceLog::GetInstance()->Flush( |
329 base::Bind(&TraceControllerImpl::OnTraceDataCollected, | 313 base::Bind(&TraceControllerImpl::OnTraceDataCollected, |
330 base::Unretained(this))); | 314 base::Unretained(this))); |
331 | 315 |
332 // Trigger callback if one is set. | 316 // Trigger callback if one is set. |
333 if (subscriber_) { | 317 if (subscriber_) { |
334 if (is_get_categories_) | 318 if (is_get_category_groups_) |
335 subscriber_->OnKnownCategoriesCollected(known_categories_); | 319 subscriber_->OnKnownCategoriesCollected(known_category_groups_); |
336 else | 320 else |
337 subscriber_->OnEndTracingComplete(); | 321 subscriber_->OnEndTracingComplete(); |
338 // Clear subscriber so that others can use TraceController. | 322 // Clear subscriber so that others can use TraceController. |
339 subscriber_ = NULL; | 323 subscriber_ = NULL; |
340 } | 324 } |
341 | 325 |
342 is_get_categories_ = false; | 326 is_get_category_groups_ = false; |
343 } | 327 } |
344 | 328 |
345 if (pending_end_ack_count_ == 1) { | 329 if (pending_end_ack_count_ == 1) { |
346 // 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 |
347 // this code only executes if there were child processes. | 331 // this code only executes if there were child processes. |
348 std::vector<std::string> categories; | 332 std::vector<std::string> category_groups; |
349 TraceLog::GetInstance()->GetKnownCategories(&categories); | 333 TraceLog::GetInstance()->GetKnownCategoryGroups(&category_groups); |
350 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 334 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
351 base::Bind(&TraceControllerImpl::OnEndTracingAck, | 335 base::Bind(&TraceControllerImpl::OnEndTracingAck, |
352 base::Unretained(this), categories)); | 336 base::Unretained(this), category_groups)); |
353 } | 337 } |
354 } | 338 } |
355 | 339 |
356 void TraceControllerImpl::OnTraceDataCollected( | 340 void TraceControllerImpl::OnTraceDataCollected( |
357 const scoped_refptr<base::RefCountedString>& events_str_ptr) { | 341 const scoped_refptr<base::RefCountedString>& events_str_ptr) { |
358 // OnTraceDataCollected may be called from any browser thread, either by the | 342 // OnTraceDataCollected may be called from any browser thread, either by the |
359 // local event trace system or from child processes via TraceMessageFilter. | 343 // local event trace system or from child processes via TraceMessageFilter. |
360 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 344 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
361 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 345 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
362 base::Bind(&TraceControllerImpl::OnTraceDataCollected, | 346 base::Bind(&TraceControllerImpl::OnTraceDataCollected, |
363 base::Unretained(this), events_str_ptr)); | 347 base::Unretained(this), events_str_ptr)); |
364 return; | 348 return; |
365 } | 349 } |
366 | 350 |
367 // Drop trace events if we are just getting categories. | 351 // Drop trace events if we are just getting categories. |
368 if (subscriber_ && !is_get_categories_) | 352 if (subscriber_ && !is_get_category_groups_) |
369 subscriber_->OnTraceDataCollected(events_str_ptr); | 353 subscriber_->OnTraceDataCollected(events_str_ptr); |
370 } | 354 } |
371 | 355 |
372 void TraceControllerImpl::OnTraceNotification(int notification) { | 356 void TraceControllerImpl::OnTraceNotification(int notification) { |
373 // OnTraceNotification may be called from any browser thread, either by the | 357 // OnTraceNotification may be called from any browser thread, either by the |
374 // local event trace system or from child processes via TraceMessageFilter. | 358 // local event trace system or from child processes via TraceMessageFilter. |
375 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 359 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
376 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 360 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
377 base::Bind(&TraceControllerImpl::OnTraceNotification, | 361 base::Bind(&TraceControllerImpl::OnTraceNotification, |
378 base::Unretained(this), notification)); | 362 base::Unretained(this), notification)); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
413 // 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 |
414 // this code only executes if there were child processes. | 398 // this code only executes if there were child processes. |
415 float bpf = TraceLog::GetInstance()->GetBufferPercentFull(); | 399 float bpf = TraceLog::GetInstance()->GetBufferPercentFull(); |
416 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 400 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
417 base::Bind(&TraceControllerImpl::OnTraceBufferPercentFullReply, | 401 base::Bind(&TraceControllerImpl::OnTraceBufferPercentFullReply, |
418 base::Unretained(this), bpf)); | 402 base::Unretained(this), bpf)); |
419 } | 403 } |
420 } | 404 } |
421 | 405 |
422 } // namespace content | 406 } // namespace content |
OLD | NEW |