| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "content/browser/trace_controller.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/command_line.h" | |
| 9 #include "base/debug/trace_event.h" | |
| 10 #include "base/string_number_conversions.h" | |
| 11 #include "content/browser/trace_message_filter.h" | |
| 12 #include "content/browser/trace_subscriber_stdio.h" | |
| 13 #include "content/common/child_process_messages.h" | |
| 14 #include "content/public/browser/browser_message_filter.h" | |
| 15 #include "content/public/common/content_switches.h" | |
| 16 | |
| 17 using base::debug::TraceLog; | |
| 18 using content::BrowserMessageFilter; | |
| 19 using content::BrowserThread; | |
| 20 | |
| 21 namespace { | |
| 22 | |
| 23 class AutoStopTraceSubscriberStdio : public content::TraceSubscriberStdio { | |
| 24 public: | |
| 25 AutoStopTraceSubscriberStdio(const FilePath& file_path) | |
| 26 : TraceSubscriberStdio(file_path) {} | |
| 27 | |
| 28 static void EndStartupTrace(TraceSubscriberStdio* subscriber) { | |
| 29 if (!TraceController::GetInstance()->EndTracingAsync(subscriber)) | |
| 30 delete subscriber; | |
| 31 // else, the tracing will end asynchronously in OnEndTracingComplete(). | |
| 32 } | |
| 33 | |
| 34 virtual void OnEndTracingComplete() { | |
| 35 TraceSubscriberStdio::OnEndTracingComplete(); | |
| 36 delete this; | |
| 37 // TODO(joth): this would be the time to automatically open up | |
| 38 // chrome://tracing/ and load up the trace data collected. | |
| 39 } | |
| 40 }; | |
| 41 | |
| 42 } // namespace | |
| 43 | |
| 44 void TraceSubscriber::OnKnownCategoriesCollected( | |
| 45 const std::set<std::string>& known_categories) {} | |
| 46 | |
| 47 void TraceSubscriber::OnTraceBufferPercentFullReply(float percent_full) {} | |
| 48 | |
| 49 TraceSubscriber::~TraceSubscriber() {} | |
| 50 | |
| 51 TraceController::TraceController() : | |
| 52 subscriber_(NULL), | |
| 53 pending_end_ack_count_(0), | |
| 54 pending_bpf_ack_count_(0), | |
| 55 maximum_bpf_(0.0f), | |
| 56 is_tracing_(false), | |
| 57 is_get_categories_(false) { | |
| 58 TraceLog::GetInstance()->SetOutputCallback( | |
| 59 base::Bind(&TraceController::OnTraceDataCollected, | |
| 60 base::Unretained(this))); | |
| 61 } | |
| 62 | |
| 63 TraceController::~TraceController() { | |
| 64 if (TraceLog* trace_log = TraceLog::GetInstance()) | |
| 65 trace_log->SetOutputCallback(TraceLog::OutputCallback()); | |
| 66 } | |
| 67 | |
| 68 TraceController* TraceController::GetInstance() { | |
| 69 return Singleton<TraceController>::get(); | |
| 70 } | |
| 71 | |
| 72 void TraceController::InitStartupTracing(const CommandLine& command_line) { | |
| 73 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 74 FilePath trace_file = command_line.GetSwitchValuePath( | |
| 75 switches::kTraceStartupFile); | |
| 76 // trace_file = "none" means that startup events will show up for the next | |
| 77 // begin/end tracing (via about:tracing or AutomationProxy::BeginTracing/ | |
| 78 // EndTracing, for example). | |
| 79 if (trace_file == FilePath().AppendASCII("none")) | |
| 80 return; | |
| 81 | |
| 82 if (trace_file.empty()) { | |
| 83 // Default to saving the startup trace into the current dir. | |
| 84 trace_file = FilePath().AppendASCII("chrometrace.log"); | |
| 85 } | |
| 86 scoped_ptr<AutoStopTraceSubscriberStdio> subscriber( | |
| 87 new AutoStopTraceSubscriberStdio(trace_file)); | |
| 88 DCHECK(can_begin_tracing(subscriber.get())); | |
| 89 | |
| 90 std::string delay_str = command_line.GetSwitchValueASCII( | |
| 91 switches::kTraceStartupDuration); | |
| 92 int delay_secs = 5; | |
| 93 if (!delay_str.empty() && !base::StringToInt(delay_str, &delay_secs)) { | |
| 94 DLOG(WARNING) << "Could not parse --" << switches::kTraceStartupDuration | |
| 95 << "=" << delay_str << " defaulting to 5 (secs)"; | |
| 96 delay_secs = 5; | |
| 97 } | |
| 98 | |
| 99 OnTracingBegan(subscriber.get()); | |
| 100 BrowserThread::PostDelayedTask( | |
| 101 BrowserThread::UI, | |
| 102 FROM_HERE, | |
| 103 base::Bind(&AutoStopTraceSubscriberStdio::EndStartupTrace, | |
| 104 base::Unretained(subscriber.release())), | |
| 105 base::TimeDelta::FromSeconds(delay_secs)); | |
| 106 } | |
| 107 | |
| 108 bool TraceController::GetKnownCategoriesAsync(TraceSubscriber* subscriber) { | |
| 109 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 110 | |
| 111 // Known categories come back from child processes with the EndTracingAck | |
| 112 // message. So to get known categories, just begin and end tracing immediately | |
| 113 // afterwards. This will ping all the child processes for categories. | |
| 114 is_get_categories_ = true; | |
| 115 bool success = BeginTracing(subscriber) && EndTracingAsync(subscriber); | |
| 116 is_get_categories_ = success; | |
| 117 return success; | |
| 118 } | |
| 119 | |
| 120 bool TraceController::BeginTracing(TraceSubscriber* subscriber) { | |
| 121 std::vector<std::string> include, exclude; | |
| 122 // By default, exclude all categories that begin with test_ | |
| 123 exclude.push_back("test_*"); | |
| 124 return BeginTracing(subscriber, include, exclude); | |
| 125 } | |
| 126 | |
| 127 bool TraceController::BeginTracing( | |
| 128 TraceSubscriber* subscriber, | |
| 129 const std::vector<std::string>& included_categories, | |
| 130 const std::vector<std::string>& excluded_categories) { | |
| 131 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 132 | |
| 133 if (!can_begin_tracing(subscriber)) | |
| 134 return false; | |
| 135 | |
| 136 // Enable tracing | |
| 137 TraceLog::GetInstance()->SetEnabled(included_categories, excluded_categories); | |
| 138 OnTracingBegan(subscriber); | |
| 139 | |
| 140 return true; | |
| 141 } | |
| 142 | |
| 143 bool TraceController::BeginTracing(TraceSubscriber* subscriber, | |
| 144 const std::string& categories) { | |
| 145 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 146 | |
| 147 if (!can_begin_tracing(subscriber)) | |
| 148 return false; | |
| 149 | |
| 150 // Enable tracing | |
| 151 TraceLog::GetInstance()->SetEnabled(categories); | |
| 152 | |
| 153 OnTracingBegan(subscriber); | |
| 154 | |
| 155 return true; | |
| 156 } | |
| 157 | |
| 158 bool TraceController::EndTracingAsync(TraceSubscriber* subscriber) { | |
| 159 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 160 | |
| 161 if (!can_end_tracing() || subscriber != subscriber_) | |
| 162 return false; | |
| 163 | |
| 164 // There could be a case where there are no child processes and filters_ | |
| 165 // is empty. In that case we can immediately tell the subscriber that tracing | |
| 166 // has ended. To avoid recursive calls back to the subscriber, we will just | |
| 167 // use the existing asynchronous OnEndTracingAck code. | |
| 168 // Count myself (local trace) in pending_end_ack_count_, acked below. | |
| 169 pending_end_ack_count_ = filters_.size() + 1; | |
| 170 | |
| 171 // Handle special case of zero child processes. | |
| 172 if (pending_end_ack_count_ == 1) { | |
| 173 // Ack asynchronously now, because we don't have any children to wait for. | |
| 174 std::vector<std::string> categories; | |
| 175 TraceLog::GetInstance()->GetKnownCategories(&categories); | |
| 176 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
| 177 base::Bind(&TraceController::OnEndTracingAck, base::Unretained(this), | |
| 178 categories)); | |
| 179 } | |
| 180 | |
| 181 // Notify all child processes. | |
| 182 for (FilterMap::iterator it = filters_.begin(); it != filters_.end(); ++it) { | |
| 183 it->get()->SendEndTracing(); | |
| 184 } | |
| 185 | |
| 186 return true; | |
| 187 } | |
| 188 | |
| 189 bool TraceController::GetTraceBufferPercentFullAsync( | |
| 190 TraceSubscriber* subscriber) { | |
| 191 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 192 | |
| 193 if (!can_get_buffer_percent_full() || subscriber != subscriber_) | |
| 194 return false; | |
| 195 | |
| 196 maximum_bpf_ = 0.0f; | |
| 197 pending_bpf_ack_count_ = filters_.size() + 1; | |
| 198 | |
| 199 // Handle special case of zero child processes. | |
| 200 if (pending_bpf_ack_count_ == 1) { | |
| 201 // Ack asynchronously now, because we don't have any children to wait for. | |
| 202 float bpf = TraceLog::GetInstance()->GetBufferPercentFull(); | |
| 203 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
| 204 base::Bind(&TraceController::OnTraceBufferPercentFullReply, | |
| 205 base::Unretained(this), bpf)); | |
| 206 } | |
| 207 | |
| 208 // Message all child processes. | |
| 209 for (FilterMap::iterator it = filters_.begin(); it != filters_.end(); ++it) { | |
| 210 it->get()->SendGetTraceBufferPercentFull(); | |
| 211 } | |
| 212 | |
| 213 return true; | |
| 214 } | |
| 215 | |
| 216 void TraceController::CancelSubscriber(TraceSubscriber* subscriber) { | |
| 217 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 218 | |
| 219 if (subscriber == subscriber_) { | |
| 220 subscriber_ = NULL; | |
| 221 // End tracing if necessary. | |
| 222 if (is_tracing_ && pending_end_ack_count_ == 0) | |
| 223 EndTracingAsync(NULL); | |
| 224 } | |
| 225 } | |
| 226 | |
| 227 void TraceController::AddFilter(TraceMessageFilter* filter) { | |
| 228 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | |
| 229 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
| 230 base::Bind(&TraceController::AddFilter, base::Unretained(this), | |
| 231 make_scoped_refptr(filter))); | |
| 232 return; | |
| 233 } | |
| 234 | |
| 235 filters_.insert(filter); | |
| 236 if (is_tracing_enabled()) { | |
| 237 filter->SendBeginTracing(included_categories_, excluded_categories_); | |
| 238 } | |
| 239 } | |
| 240 | |
| 241 void TraceController::RemoveFilter(TraceMessageFilter* filter) { | |
| 242 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | |
| 243 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
| 244 base::Bind(&TraceController::RemoveFilter, base::Unretained(this), | |
| 245 make_scoped_refptr(filter))); | |
| 246 return; | |
| 247 } | |
| 248 | |
| 249 filters_.erase(filter); | |
| 250 } | |
| 251 | |
| 252 void TraceController::OnTracingBegan(TraceSubscriber* subscriber) { | |
| 253 is_tracing_ = true; | |
| 254 | |
| 255 subscriber_ = subscriber; | |
| 256 | |
| 257 TraceLog::GetInstance()->GetEnabledTraceCategories(&included_categories_, | |
| 258 &excluded_categories_); | |
| 259 // Notify all child processes. | |
| 260 for (FilterMap::iterator it = filters_.begin(); it != filters_.end(); ++it) { | |
| 261 it->get()->SendBeginTracing(included_categories_, excluded_categories_); | |
| 262 } | |
| 263 } | |
| 264 | |
| 265 void TraceController::OnEndTracingAck( | |
| 266 const std::vector<std::string>& known_categories) { | |
| 267 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | |
| 268 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
| 269 base::Bind(&TraceController::OnEndTracingAck, base::Unretained(this), | |
| 270 known_categories)); | |
| 271 return; | |
| 272 } | |
| 273 | |
| 274 // Merge known_categories with known_categories_ | |
| 275 known_categories_.insert(known_categories.begin(), known_categories.end()); | |
| 276 | |
| 277 if (pending_end_ack_count_ == 0) | |
| 278 return; | |
| 279 | |
| 280 if (--pending_end_ack_count_ == 0) { | |
| 281 // All acks have been received. | |
| 282 is_tracing_ = false; | |
| 283 | |
| 284 // Disable local trace. During this call, our OnTraceDataCollected will be | |
| 285 // called with the last of the local trace data. Since we are on the UI | |
| 286 // thread, the call to OnTraceDataCollected will be synchronous, so we can | |
| 287 // immediately call OnEndTracingComplete below. | |
| 288 TraceLog::GetInstance()->SetEnabled(false); | |
| 289 | |
| 290 // Trigger callback if one is set. | |
| 291 if (subscriber_) { | |
| 292 if (is_get_categories_) | |
| 293 subscriber_->OnKnownCategoriesCollected(known_categories_); | |
| 294 else | |
| 295 subscriber_->OnEndTracingComplete(); | |
| 296 // Clear subscriber so that others can use TraceController. | |
| 297 subscriber_ = NULL; | |
| 298 } | |
| 299 | |
| 300 is_get_categories_ = false; | |
| 301 } | |
| 302 | |
| 303 if (pending_end_ack_count_ == 1) { | |
| 304 // The last ack represents local trace, so we need to ack it now. Note that | |
| 305 // this code only executes if there were child processes. | |
| 306 std::vector<std::string> categories; | |
| 307 TraceLog::GetInstance()->GetKnownCategories(&categories); | |
| 308 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
| 309 base::Bind(&TraceController::OnEndTracingAck, base::Unretained(this), | |
| 310 categories)); | |
| 311 } | |
| 312 } | |
| 313 | |
| 314 void TraceController::OnTraceDataCollected( | |
| 315 const scoped_refptr<base::RefCountedString>& events_str_ptr) { | |
| 316 // OnTraceDataCollected may be called from any browser thread, either by the | |
| 317 // local event trace system or from child processes via TraceMessageFilter. | |
| 318 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | |
| 319 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
| 320 base::Bind(&TraceController::OnTraceDataCollected, | |
| 321 base::Unretained(this), events_str_ptr)); | |
| 322 return; | |
| 323 } | |
| 324 | |
| 325 // Drop trace events if we are just getting categories. | |
| 326 if (subscriber_ && !is_get_categories_) | |
| 327 subscriber_->OnTraceDataCollected(events_str_ptr); | |
| 328 } | |
| 329 | |
| 330 void TraceController::OnTraceBufferFull() { | |
| 331 // OnTraceBufferFull may be called from any browser thread, either by the | |
| 332 // local event trace system or from child processes via TraceMessageFilter. | |
| 333 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | |
| 334 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
| 335 base::Bind(&TraceController::OnTraceBufferFull, | |
| 336 base::Unretained(this))); | |
| 337 return; | |
| 338 } | |
| 339 | |
| 340 // EndTracingAsync may return false if tracing is already in the process of | |
| 341 // being ended. That is ok. | |
| 342 EndTracingAsync(subscriber_); | |
| 343 } | |
| 344 | |
| 345 void TraceController::OnTraceBufferPercentFullReply(float percent_full) { | |
| 346 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | |
| 347 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
| 348 base::Bind(&TraceController::OnTraceBufferPercentFullReply, | |
| 349 base::Unretained(this), percent_full)); | |
| 350 return; | |
| 351 } | |
| 352 | |
| 353 if (pending_bpf_ack_count_ == 0) | |
| 354 return; | |
| 355 | |
| 356 maximum_bpf_ = (maximum_bpf_ > percent_full)? maximum_bpf_ : percent_full; | |
| 357 | |
| 358 if (--pending_bpf_ack_count_ == 0) { | |
| 359 // Trigger callback if one is set. | |
| 360 if (subscriber_) | |
| 361 subscriber_->OnTraceBufferPercentFullReply(maximum_bpf_); | |
| 362 } | |
| 363 | |
| 364 if (pending_bpf_ack_count_ == 1) { | |
| 365 // The last ack represents local trace, so we need to ack it now. Note that | |
| 366 // this code only executes if there were child processes. | |
| 367 float bpf = TraceLog::GetInstance()->GetBufferPercentFull(); | |
| 368 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
| 369 base::Bind(&TraceController::OnTraceBufferPercentFullReply, | |
| 370 base::Unretained(this), bpf)); | |
| 371 } | |
| 372 } | |
| 373 | |
| OLD | NEW |