| 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/trace_controller_impl.h" | 5 #include "content/browser/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 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 45 return TraceControllerImpl::GetInstance(); | 45 return TraceControllerImpl::GetInstance(); |
| 46 } | 46 } |
| 47 | 47 |
| 48 TraceControllerImpl::TraceControllerImpl() : | 48 TraceControllerImpl::TraceControllerImpl() : |
| 49 subscriber_(NULL), | 49 subscriber_(NULL), |
| 50 pending_end_ack_count_(0), | 50 pending_end_ack_count_(0), |
| 51 pending_bpf_ack_count_(0), | 51 pending_bpf_ack_count_(0), |
| 52 maximum_bpf_(0.0f), | 52 maximum_bpf_(0.0f), |
| 53 is_tracing_(false), | 53 is_tracing_(false), |
| 54 is_get_categories_(false) { | 54 is_get_categories_(false) { |
| 55 TraceLog::GetInstance()->SetOutputCallback( | 55 TraceLog::GetInstance()->SetNotificationCallback( |
| 56 base::Bind(&TraceControllerImpl::OnTraceDataCollected, | 56 base::Bind(&TraceControllerImpl::OnTraceNotification, |
| 57 base::Unretained(this))); | 57 base::Unretained(this))); |
| 58 } | 58 } |
| 59 | 59 |
| 60 TraceControllerImpl::~TraceControllerImpl() { | 60 TraceControllerImpl::~TraceControllerImpl() { |
| 61 if (TraceLog* trace_log = TraceLog::GetInstance()) | 61 if (TraceLog* trace_log = TraceLog::GetInstance()) |
| 62 trace_log->SetOutputCallback(TraceLog::OutputCallback()); | 62 trace_log->SetNotificationCallback(TraceLog::NotificationCallback()); |
| 63 } | 63 } |
| 64 | 64 |
| 65 TraceControllerImpl* TraceControllerImpl::GetInstance() { | 65 TraceControllerImpl* TraceControllerImpl::GetInstance() { |
| 66 return Singleton<TraceControllerImpl>::get(); | 66 return Singleton<TraceControllerImpl>::get(); |
| 67 } | 67 } |
| 68 | 68 |
| 69 void TraceControllerImpl::InitStartupTracing(const CommandLine& command_line) { | 69 void TraceControllerImpl::InitStartupTracing(const CommandLine& command_line) { |
| 70 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 70 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 71 FilePath trace_file = command_line.GetSwitchValuePath( | 71 FilePath trace_file = command_line.GetSwitchValuePath( |
| 72 switches::kTraceStartupFile); | 72 switches::kTraceStartupFile); |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 197 } | 197 } |
| 198 | 198 |
| 199 // Message all child processes. | 199 // Message all child processes. |
| 200 for (FilterMap::iterator it = filters_.begin(); it != filters_.end(); ++it) { | 200 for (FilterMap::iterator it = filters_.begin(); it != filters_.end(); ++it) { |
| 201 it->get()->SendGetTraceBufferPercentFull(); | 201 it->get()->SendGetTraceBufferPercentFull(); |
| 202 } | 202 } |
| 203 | 203 |
| 204 return true; | 204 return true; |
| 205 } | 205 } |
| 206 | 206 |
| 207 bool TraceControllerImpl::SetWatchEvent(TraceSubscriber* subscriber, |
| 208 const std::string& category_name, |
| 209 const std::string& event_name) { |
| 210 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 211 if (subscriber != subscriber_) |
| 212 return false; |
| 213 |
| 214 watch_category_ = category_name; |
| 215 watch_name_ = event_name; |
| 216 |
| 217 TraceLog::GetInstance()->SetWatchEvent(category_name, event_name); |
| 218 for (FilterMap::iterator it = filters_.begin(); it != filters_.end(); ++it) |
| 219 it->get()->SendSetWatchEvent(category_name, event_name); |
| 220 |
| 221 return true; |
| 222 } |
| 223 |
| 224 bool TraceControllerImpl::CancelWatchEvent(TraceSubscriber* subscriber) { |
| 225 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 226 if (subscriber != subscriber_) |
| 227 return false; |
| 228 |
| 229 watch_category_.clear(); |
| 230 watch_name_.clear(); |
| 231 |
| 232 TraceLog::GetInstance()->CancelWatchEvent(); |
| 233 for (FilterMap::iterator it = filters_.begin(); it != filters_.end(); ++it) |
| 234 it->get()->SendCancelWatchEvent(); |
| 235 |
| 236 return true; |
| 237 } |
| 238 |
| 207 void TraceControllerImpl::CancelSubscriber(TraceSubscriber* subscriber) { | 239 void TraceControllerImpl::CancelSubscriber(TraceSubscriber* subscriber) { |
| 208 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 240 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 209 | 241 |
| 210 if (subscriber == subscriber_) { | 242 if (subscriber == subscriber_) { |
| 211 subscriber_ = NULL; | 243 subscriber_ = NULL; |
| 212 // End tracing if necessary. | 244 // End tracing if necessary. |
| 213 if (is_tracing_ && pending_end_ack_count_ == 0) | 245 if (is_tracing_ && pending_end_ack_count_ == 0) |
| 214 EndTracingAsync(NULL); | 246 EndTracingAsync(NULL); |
| 215 } | 247 } |
| 216 } | 248 } |
| 217 | 249 |
| 218 void TraceControllerImpl::AddFilter(TraceMessageFilter* filter) { | 250 void TraceControllerImpl::AddFilter(TraceMessageFilter* filter) { |
| 219 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 251 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
| 220 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 252 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 221 base::Bind(&TraceControllerImpl::AddFilter, base::Unretained(this), | 253 base::Bind(&TraceControllerImpl::AddFilter, base::Unretained(this), |
| 222 make_scoped_refptr(filter))); | 254 make_scoped_refptr(filter))); |
| 223 return; | 255 return; |
| 224 } | 256 } |
| 225 | 257 |
| 226 filters_.insert(filter); | 258 filters_.insert(filter); |
| 227 if (is_tracing_enabled()) { | 259 if (is_tracing_enabled()) { |
| 228 filter->SendBeginTracing(included_categories_, excluded_categories_); | 260 filter->SendBeginTracing(included_categories_, excluded_categories_); |
| 261 if (!watch_category_.empty()) |
| 262 filter->SendSetWatchEvent(watch_category_, watch_name_); |
| 229 } | 263 } |
| 230 } | 264 } |
| 231 | 265 |
| 232 void TraceControllerImpl::RemoveFilter(TraceMessageFilter* filter) { | 266 void TraceControllerImpl::RemoveFilter(TraceMessageFilter* filter) { |
| 233 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 267 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
| 234 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 268 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 235 base::Bind(&TraceControllerImpl::RemoveFilter, base::Unretained(this), | 269 base::Bind(&TraceControllerImpl::RemoveFilter, base::Unretained(this), |
| 236 make_scoped_refptr(filter))); | 270 make_scoped_refptr(filter))); |
| 237 return; | 271 return; |
| 238 } | 272 } |
| (...skipping 26 matching lines...) Expand all Loading... |
| 265 // Merge known_categories with known_categories_ | 299 // Merge known_categories with known_categories_ |
| 266 known_categories_.insert(known_categories.begin(), known_categories.end()); | 300 known_categories_.insert(known_categories.begin(), known_categories.end()); |
| 267 | 301 |
| 268 if (pending_end_ack_count_ == 0) | 302 if (pending_end_ack_count_ == 0) |
| 269 return; | 303 return; |
| 270 | 304 |
| 271 if (--pending_end_ack_count_ == 0) { | 305 if (--pending_end_ack_count_ == 0) { |
| 272 // All acks have been received. | 306 // All acks have been received. |
| 273 is_tracing_ = false; | 307 is_tracing_ = false; |
| 274 | 308 |
| 275 // Disable local trace. During this call, our OnTraceDataCollected will be | 309 // Disable local trace. |
| 310 TraceLog::GetInstance()->SetDisabled(); |
| 311 |
| 312 // During this call, our OnTraceDataCollected will be |
| 276 // called with the last of the local trace data. Since we are on the UI | 313 // called with the last of the local trace data. Since we are on the UI |
| 277 // thread, the call to OnTraceDataCollected will be synchronous, so we can | 314 // thread, the call to OnTraceDataCollected will be synchronous, so we can |
| 278 // immediately call OnEndTracingComplete below. | 315 // immediately call OnEndTracingComplete below. |
| 279 TraceLog::GetInstance()->SetEnabled(false); | 316 TraceLog::GetInstance()->Flush( |
| 317 base::Bind(&TraceControllerImpl::OnTraceDataCollected, |
| 318 base::Unretained(this))); |
| 280 | 319 |
| 281 // Trigger callback if one is set. | 320 // Trigger callback if one is set. |
| 282 if (subscriber_) { | 321 if (subscriber_) { |
| 283 if (is_get_categories_) | 322 if (is_get_categories_) |
| 284 subscriber_->OnKnownCategoriesCollected(known_categories_); | 323 subscriber_->OnKnownCategoriesCollected(known_categories_); |
| 285 else | 324 else |
| 286 subscriber_->OnEndTracingComplete(); | 325 subscriber_->OnEndTracingComplete(); |
| 287 // Clear subscriber so that others can use TraceController. | 326 // Clear subscriber so that others can use TraceController. |
| 288 subscriber_ = NULL; | 327 subscriber_ = NULL; |
| 289 } | 328 } |
| (...skipping 21 matching lines...) Expand all Loading... |
| 311 base::Bind(&TraceControllerImpl::OnTraceDataCollected, | 350 base::Bind(&TraceControllerImpl::OnTraceDataCollected, |
| 312 base::Unretained(this), events_str_ptr)); | 351 base::Unretained(this), events_str_ptr)); |
| 313 return; | 352 return; |
| 314 } | 353 } |
| 315 | 354 |
| 316 // Drop trace events if we are just getting categories. | 355 // Drop trace events if we are just getting categories. |
| 317 if (subscriber_ && !is_get_categories_) | 356 if (subscriber_ && !is_get_categories_) |
| 318 subscriber_->OnTraceDataCollected(events_str_ptr); | 357 subscriber_->OnTraceDataCollected(events_str_ptr); |
| 319 } | 358 } |
| 320 | 359 |
| 321 void TraceControllerImpl::OnTraceBufferFull() { | 360 void TraceControllerImpl::OnTraceNotification(int notification) { |
| 322 // OnTraceBufferFull may be called from any browser thread, either by the | 361 // OnTraceNotification may be called from any browser thread, either by the |
| 323 // local event trace system or from child processes via TraceMessageFilter. | 362 // local event trace system or from child processes via TraceMessageFilter. |
| 324 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 363 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
| 325 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 364 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 326 base::Bind(&TraceControllerImpl::OnTraceBufferFull, | 365 base::Bind(&TraceControllerImpl::OnTraceNotification, |
| 327 base::Unretained(this))); | 366 base::Unretained(this), notification)); |
| 328 return; | 367 return; |
| 329 } | 368 } |
| 330 | 369 |
| 331 // EndTracingAsync may return false if tracing is already in the process of | 370 if (notification & base::debug::TraceLog::TRACE_BUFFER_FULL) { |
| 332 // being ended. That is ok. | 371 // EndTracingAsync may return false if tracing is already in the process |
| 333 EndTracingAsync(subscriber_); | 372 // of being ended. That is ok. |
| 373 EndTracingAsync(subscriber_); |
| 374 } |
| 375 if (notification & base::debug::TraceLog::EVENT_WATCH_NOTIFICATION) { |
| 376 if (subscriber_) |
| 377 subscriber_->OnEventWatchNotification(); |
| 378 } |
| 334 } | 379 } |
| 335 | 380 |
| 336 void TraceControllerImpl::OnTraceBufferPercentFullReply(float percent_full) { | 381 void TraceControllerImpl::OnTraceBufferPercentFullReply(float percent_full) { |
| 337 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 382 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
| 338 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 383 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 339 base::Bind(&TraceControllerImpl::OnTraceBufferPercentFullReply, | 384 base::Bind(&TraceControllerImpl::OnTraceBufferPercentFullReply, |
| 340 base::Unretained(this), percent_full)); | 385 base::Unretained(this), percent_full)); |
| 341 return; | 386 return; |
| 342 } | 387 } |
| 343 | 388 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 356 // The last ack represents local trace, so we need to ack it now. Note that | 401 // The last ack represents local trace, so we need to ack it now. Note that |
| 357 // this code only executes if there were child processes. | 402 // this code only executes if there were child processes. |
| 358 float bpf = TraceLog::GetInstance()->GetBufferPercentFull(); | 403 float bpf = TraceLog::GetInstance()->GetBufferPercentFull(); |
| 359 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 404 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 360 base::Bind(&TraceControllerImpl::OnTraceBufferPercentFullReply, | 405 base::Bind(&TraceControllerImpl::OnTraceBufferPercentFullReply, |
| 361 base::Unretained(this), bpf)); | 406 base::Unretained(this), bpf)); |
| 362 } | 407 } |
| 363 } | 408 } |
| 364 | 409 |
| 365 } // namespace content | 410 } // namespace content |
| OLD | NEW |