| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 #include "content/browser/tracing/tracing_controller_impl.h" | 4 #include "content/browser/tracing/tracing_controller_impl.h" |
| 5 | 5 |
| 6 #include "base/bind.h" | 6 #include "base/bind.h" |
| 7 #include "base/debug/trace_event.h" | 7 #include "base/debug/trace_event.h" |
| 8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
| 9 #include "base/json/string_escape.h" | 9 #include "base/json/string_escape.h" |
| 10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 using base::debug::TraceOptions; | 27 using base::debug::TraceOptions; |
| 28 using base::debug::CategoryFilter; | 28 using base::debug::CategoryFilter; |
| 29 | 29 |
| 30 namespace content { | 30 namespace content { |
| 31 | 31 |
| 32 namespace { | 32 namespace { |
| 33 | 33 |
| 34 base::LazyInstance<TracingControllerImpl>::Leaky g_controller = | 34 base::LazyInstance<TracingControllerImpl>::Leaky g_controller = |
| 35 LAZY_INSTANCE_INITIALIZER; | 35 LAZY_INSTANCE_INITIALIZER; |
| 36 | 36 |
| 37 class FileTraceDataSink : public TracingController::TraceDataSink { |
| 38 public: |
| 39 explicit FileTraceDataSink(const base::FilePath& trace_file_path, |
| 40 const base::Closure& callback) |
| 41 : file_path_(trace_file_path), |
| 42 completion_callback_(callback), |
| 43 file_(NULL) {} |
| 44 |
| 45 virtual void AddTraceChunk(const std::string& chunk) OVERRIDE { |
| 46 std::string tmp = chunk; |
| 47 scoped_refptr<base::RefCountedString> chunk_ptr = |
| 48 base::RefCountedString::TakeString(&tmp); |
| 49 BrowserThread::PostTask( |
| 50 BrowserThread::FILE, |
| 51 FROM_HERE, |
| 52 base::Bind( |
| 53 &FileTraceDataSink::AddTraceChunkOnFileThread, this, chunk_ptr)); |
| 54 } |
| 55 virtual void SetSystemTrace(const std::string& data) OVERRIDE { |
| 56 system_trace_ = data; |
| 57 } |
| 58 virtual void Close() OVERRIDE { |
| 59 BrowserThread::PostTask( |
| 60 BrowserThread::FILE, |
| 61 FROM_HERE, |
| 62 base::Bind(&FileTraceDataSink::CloseOnFileThread, this)); |
| 63 } |
| 64 |
| 65 private: |
| 66 virtual ~FileTraceDataSink() { DCHECK(file_ == NULL); } |
| 67 |
| 68 void AddTraceChunkOnFileThread( |
| 69 const scoped_refptr<base::RefCountedString> chunk) { |
| 70 if (!OpenFileIfNeededOnFileThread()) |
| 71 return; |
| 72 fwrite(chunk->data().c_str(), strlen(chunk->data().c_str()), 1, file_); |
| 73 } |
| 74 |
| 75 bool OpenFileIfNeededOnFileThread() { |
| 76 if (file_ != NULL) |
| 77 return true; |
| 78 file_ = base::OpenFile(file_path_, "w"); |
| 79 if (file_ == NULL) { |
| 80 LOG(ERROR) << "Failed to open " << file_path_.value(); |
| 81 return false; |
| 82 } |
| 83 const char preamble[] = "{\"traceEvents\": ["; |
| 84 fwrite(preamble, strlen(preamble), 1, file_); |
| 85 return true; |
| 86 } |
| 87 |
| 88 void CloseOnFileThread() { |
| 89 if (OpenFileIfNeededOnFileThread()) { |
| 90 fputc(']', file_); |
| 91 if (!system_trace_.empty()) { |
| 92 const char systemTraceEvents[] = ",\"systemTraceEvents\": "; |
| 93 fwrite(systemTraceEvents, strlen(systemTraceEvents), 1, file_); |
| 94 fwrite(system_trace_.c_str(), strlen(system_trace_.c_str()), 1, file_); |
| 95 } |
| 96 fputc('}', file_); |
| 97 base::CloseFile(file_); |
| 98 file_ = NULL; |
| 99 } |
| 100 BrowserThread::PostTask( |
| 101 BrowserThread::UI, |
| 102 FROM_HERE, |
| 103 base::Bind(&FileTraceDataSink::FinalizeOnUIThread, this)); |
| 104 } |
| 105 |
| 106 void FinalizeOnUIThread() { completion_callback_.Run(); } |
| 107 |
| 108 base::FilePath file_path_; |
| 109 base::Closure completion_callback_; |
| 110 FILE* file_; |
| 111 std::string system_trace_; |
| 112 |
| 113 DISALLOW_COPY_AND_ASSIGN(FileTraceDataSink); |
| 114 }; |
| 115 |
| 116 class StringTraceDataSink : public TracingController::TraceDataSink { |
| 117 public: |
| 118 typedef base::Callback<void(base::RefCountedString*)> CompletionCallback; |
| 119 |
| 120 explicit StringTraceDataSink(CompletionCallback callback) |
| 121 : completion_callback_(callback) {} |
| 122 |
| 123 // TracingController::TraceDataSink implementation |
| 124 virtual void AddTraceChunk(const std::string& chunk) OVERRIDE { |
| 125 if (!trace_.empty()) |
| 126 trace_ += ","; |
| 127 trace_ += chunk; |
| 128 } |
| 129 virtual void SetSystemTrace(const std::string& data) OVERRIDE { |
| 130 system_trace_ = data; |
| 131 } |
| 132 virtual void Close() OVERRIDE { |
| 133 std::string result = "{\"traceEvents\":[" + trace_ + "]"; |
| 134 if (!system_trace_.empty()) |
| 135 result += ",\"systemTraceEvents\": " + system_trace_; |
| 136 result += "}"; |
| 137 |
| 138 completion_callback_.Run(base::RefCountedString::TakeString(&result)); |
| 139 } |
| 140 |
| 141 private: |
| 142 virtual ~StringTraceDataSink() {} |
| 143 |
| 144 std::string trace_; |
| 145 std::string system_trace_; |
| 146 CompletionCallback completion_callback_; |
| 147 |
| 148 DISALLOW_COPY_AND_ASSIGN(StringTraceDataSink); |
| 149 }; |
| 150 |
| 37 } // namespace | 151 } // namespace |
| 38 | 152 |
| 39 TracingController* TracingController::GetInstance() { | 153 TracingController* TracingController::GetInstance() { |
| 40 return TracingControllerImpl::GetInstance(); | 154 return TracingControllerImpl::GetInstance(); |
| 41 } | 155 } |
| 42 | 156 |
| 43 class TracingControllerImpl::ResultFile { | |
| 44 public: | |
| 45 explicit ResultFile(const base::FilePath& path); | |
| 46 void Write(const scoped_refptr<base::RefCountedString>& events_str_ptr) { | |
| 47 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | |
| 48 base::Bind(&TracingControllerImpl::ResultFile::WriteTask, | |
| 49 base::Unretained(this), events_str_ptr)); | |
| 50 } | |
| 51 void Close(const base::Closure& callback) { | |
| 52 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | |
| 53 base::Bind(&TracingControllerImpl::ResultFile::CloseTask, | |
| 54 base::Unretained(this), callback)); | |
| 55 } | |
| 56 void WriteSystemTrace( | |
| 57 const scoped_refptr<base::RefCountedString>& events_str_ptr) { | |
| 58 BrowserThread::PostTask( | |
| 59 BrowserThread::FILE, | |
| 60 FROM_HERE, | |
| 61 base::Bind(&TracingControllerImpl::ResultFile::WriteSystemTraceTask, | |
| 62 base::Unretained(this), events_str_ptr)); | |
| 63 } | |
| 64 | |
| 65 const base::FilePath& path() const { return path_; } | |
| 66 | |
| 67 private: | |
| 68 void OpenTask(); | |
| 69 void WriteTask(const scoped_refptr<base::RefCountedString>& events_str_ptr); | |
| 70 void WriteSystemTraceTask( | |
| 71 const scoped_refptr<base::RefCountedString>& events_str_ptr); | |
| 72 void CloseTask(const base::Closure& callback); | |
| 73 | |
| 74 FILE* file_; | |
| 75 base::FilePath path_; | |
| 76 bool has_at_least_one_result_; | |
| 77 scoped_refptr<base::RefCountedString> system_trace_; | |
| 78 | |
| 79 DISALLOW_COPY_AND_ASSIGN(ResultFile); | |
| 80 }; | |
| 81 | |
| 82 TracingControllerImpl::ResultFile::ResultFile(const base::FilePath& path) | |
| 83 : file_(NULL), | |
| 84 path_(path), | |
| 85 has_at_least_one_result_(false) { | |
| 86 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | |
| 87 base::Bind(&TracingControllerImpl::ResultFile::OpenTask, | |
| 88 base::Unretained(this))); | |
| 89 } | |
| 90 | |
| 91 void TracingControllerImpl::ResultFile::OpenTask() { | |
| 92 if (path_.empty()) | |
| 93 base::CreateTemporaryFile(&path_); | |
| 94 file_ = base::OpenFile(path_, "w"); | |
| 95 if (!file_) { | |
| 96 LOG(ERROR) << "Failed to open " << path_.value(); | |
| 97 return; | |
| 98 } | |
| 99 const char* preamble = "{\"traceEvents\": ["; | |
| 100 size_t written = fwrite(preamble, strlen(preamble), 1, file_); | |
| 101 DCHECK(written == 1); | |
| 102 } | |
| 103 | |
| 104 void TracingControllerImpl::ResultFile::WriteTask( | |
| 105 const scoped_refptr<base::RefCountedString>& events_str_ptr) { | |
| 106 if (!file_ || !events_str_ptr->data().size()) | |
| 107 return; | |
| 108 | |
| 109 // If there is already a result in the file, then put a comma | |
| 110 // before the next batch of results. | |
| 111 if (has_at_least_one_result_) { | |
| 112 size_t written = fwrite(",", 1, 1, file_); | |
| 113 DCHECK(written == 1); | |
| 114 } | |
| 115 has_at_least_one_result_ = true; | |
| 116 size_t written = fwrite(events_str_ptr->data().c_str(), | |
| 117 events_str_ptr->data().size(), 1, | |
| 118 file_); | |
| 119 DCHECK(written == 1); | |
| 120 } | |
| 121 | |
| 122 void TracingControllerImpl::ResultFile::WriteSystemTraceTask( | |
| 123 const scoped_refptr<base::RefCountedString>& events_str_ptr) { | |
| 124 system_trace_ = events_str_ptr; | |
| 125 } | |
| 126 | |
| 127 void TracingControllerImpl::ResultFile::CloseTask( | |
| 128 const base::Closure& callback) { | |
| 129 if (!file_) | |
| 130 return; | |
| 131 | |
| 132 const char* trailevents = "]"; | |
| 133 size_t written = fwrite(trailevents, strlen(trailevents), 1, file_); | |
| 134 DCHECK(written == 1); | |
| 135 | |
| 136 if (system_trace_.get()) { | |
| 137 #if defined(OS_WIN) | |
| 138 // The Windows kernel events are kept into a JSon format stored as string | |
| 139 // and must not be escaped. | |
| 140 std::string json_string = system_trace_->data(); | |
| 141 #else | |
| 142 std::string json_string = base::GetQuotedJSONString(system_trace_->data()); | |
| 143 #endif | |
| 144 | |
| 145 const char* systemTraceHead = ",\n\"systemTraceEvents\": "; | |
| 146 written = fwrite(systemTraceHead, strlen(systemTraceHead), 1, file_); | |
| 147 DCHECK(written == 1); | |
| 148 | |
| 149 written = fwrite(json_string.data(), json_string.size(), 1, file_); | |
| 150 DCHECK(written == 1); | |
| 151 | |
| 152 system_trace_ = NULL; | |
| 153 } | |
| 154 | |
| 155 const char* trailout = "}"; | |
| 156 written = fwrite(trailout, strlen(trailout), 1, file_); | |
| 157 DCHECK(written == 1); | |
| 158 base::CloseFile(file_); | |
| 159 file_ = NULL; | |
| 160 | |
| 161 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, callback); | |
| 162 } | |
| 163 | |
| 164 | |
| 165 TracingControllerImpl::TracingControllerImpl() : | 157 TracingControllerImpl::TracingControllerImpl() : |
| 166 pending_disable_recording_ack_count_(0), | 158 pending_disable_recording_ack_count_(0), |
| 167 pending_capture_monitoring_snapshot_ack_count_(0), | 159 pending_capture_monitoring_snapshot_ack_count_(0), |
| 168 pending_trace_buffer_percent_full_ack_count_(0), | 160 pending_trace_buffer_percent_full_ack_count_(0), |
| 169 maximum_trace_buffer_percent_full_(0), | 161 maximum_trace_buffer_percent_full_(0), |
| 170 // Tracing may have been enabled by ContentMainRunner if kTraceStartup | 162 // Tracing may have been enabled by ContentMainRunner if kTraceStartup |
| 171 // is specified in command line. | 163 // is specified in command line. |
| 172 #if defined(OS_CHROMEOS) || defined(OS_WIN) | 164 #if defined(OS_CHROMEOS) || defined(OS_WIN) |
| 173 is_system_tracing_(false), | 165 is_system_tracing_(false), |
| 174 #endif | 166 #endif |
| (...skipping 17 matching lines...) Expand all Loading... |
| 192 // Known categories come back from child processes with the EndTracingAck | 184 // Known categories come back from child processes with the EndTracingAck |
| 193 // message. So to get known categories, just begin and end tracing immediately | 185 // message. So to get known categories, just begin and end tracing immediately |
| 194 // afterwards. This will ping all the child processes for categories. | 186 // afterwards. This will ping all the child processes for categories. |
| 195 pending_get_categories_done_callback_ = callback; | 187 pending_get_categories_done_callback_ = callback; |
| 196 if (!EnableRecording( | 188 if (!EnableRecording( |
| 197 CategoryFilter("*"), TraceOptions(), EnableRecordingDoneCallback())) { | 189 CategoryFilter("*"), TraceOptions(), EnableRecordingDoneCallback())) { |
| 198 pending_get_categories_done_callback_.Reset(); | 190 pending_get_categories_done_callback_.Reset(); |
| 199 return false; | 191 return false; |
| 200 } | 192 } |
| 201 | 193 |
| 202 bool ok = DisableRecording(base::FilePath(), TracingFileResultCallback()); | 194 bool ok = DisableRecording(NULL); |
| 203 DCHECK(ok); | 195 DCHECK(ok); |
| 204 return true; | 196 return true; |
| 205 } | 197 } |
| 206 | 198 |
| 207 void TracingControllerImpl::SetEnabledOnFileThread( | 199 void TracingControllerImpl::SetEnabledOnFileThread( |
| 208 const CategoryFilter& category_filter, | 200 const CategoryFilter& category_filter, |
| 209 int mode, | 201 int mode, |
| 210 const TraceOptions& trace_options, | 202 const TraceOptions& trace_options, |
| 211 const base::Closure& callback) { | 203 const base::Closure& callback) { |
| 212 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 204 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 279 for (TraceMessageFilterSet::iterator it = trace_message_filters_.begin(); | 271 for (TraceMessageFilterSet::iterator it = trace_message_filters_.begin(); |
| 280 it != trace_message_filters_.end(); ++it) { | 272 it != trace_message_filters_.end(); ++it) { |
| 281 it->get()->SendBeginTracing(category_filter, trace_options); | 273 it->get()->SendBeginTracing(category_filter, trace_options); |
| 282 } | 274 } |
| 283 | 275 |
| 284 if (!callback.is_null()) | 276 if (!callback.is_null()) |
| 285 callback.Run(); | 277 callback.Run(); |
| 286 } | 278 } |
| 287 | 279 |
| 288 bool TracingControllerImpl::DisableRecording( | 280 bool TracingControllerImpl::DisableRecording( |
| 289 const base::FilePath& result_file_path, | 281 const scoped_refptr<TraceDataSink>& trace_data_sink) { |
| 290 const TracingFileResultCallback& callback) { | |
| 291 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 282 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 292 | 283 |
| 293 if (!can_disable_recording()) | 284 if (!can_disable_recording()) |
| 294 return false; | 285 return false; |
| 295 | 286 |
| 287 trace_data_sink_ = trace_data_sink; |
| 296 trace_options_ = TraceOptions(); | 288 trace_options_ = TraceOptions(); |
| 297 // Disable local trace early to avoid traces during end-tracing process from | 289 // Disable local trace early to avoid traces during end-tracing process from |
| 298 // interfering with the process. | 290 // interfering with the process. |
| 299 base::Closure on_disable_recording_done_callback = | 291 base::Closure on_disable_recording_done_callback = base::Bind( |
| 300 base::Bind(&TracingControllerImpl::OnDisableRecordingDone, | 292 &TracingControllerImpl::OnDisableRecordingDone, base::Unretained(this)); |
| 301 base::Unretained(this), | |
| 302 result_file_path, callback); | |
| 303 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 293 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 304 base::Bind(&TracingControllerImpl::SetDisabledOnFileThread, | 294 base::Bind(&TracingControllerImpl::SetDisabledOnFileThread, |
| 305 base::Unretained(this), | 295 base::Unretained(this), |
| 306 on_disable_recording_done_callback)); | 296 on_disable_recording_done_callback)); |
| 307 return true; | 297 return true; |
| 308 } | 298 } |
| 309 | 299 |
| 310 void TracingControllerImpl::OnDisableRecordingDone( | 300 void TracingControllerImpl::OnDisableRecordingDone() { |
| 311 const base::FilePath& result_file_path, | |
| 312 const TracingFileResultCallback& callback) { | |
| 313 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 301 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 314 | 302 |
| 315 pending_disable_recording_done_callback_ = callback; | |
| 316 | |
| 317 #if defined(OS_ANDROID) | 303 #if defined(OS_ANDROID) |
| 318 if (pending_get_categories_done_callback_.is_null()) | 304 if (pending_get_categories_done_callback_.is_null()) |
| 319 TraceLog::GetInstance()->AddClockSyncMetadataEvent(); | 305 TraceLog::GetInstance()->AddClockSyncMetadataEvent(); |
| 320 #endif | 306 #endif |
| 321 | 307 |
| 322 if (!callback.is_null() || !result_file_path.empty()) | |
| 323 result_file_.reset(new ResultFile(result_file_path)); | |
| 324 | |
| 325 // Count myself (local trace) in pending_disable_recording_ack_count_, | 308 // Count myself (local trace) in pending_disable_recording_ack_count_, |
| 326 // acked below. | 309 // acked below. |
| 327 pending_disable_recording_ack_count_ = trace_message_filters_.size() + 1; | 310 pending_disable_recording_ack_count_ = trace_message_filters_.size() + 1; |
| 328 pending_disable_recording_filters_ = trace_message_filters_; | 311 pending_disable_recording_filters_ = trace_message_filters_; |
| 329 | 312 |
| 330 #if defined(OS_CHROMEOS) || defined(OS_WIN) | 313 #if defined(OS_CHROMEOS) || defined(OS_WIN) |
| 331 if (is_system_tracing_) { | 314 if (is_system_tracing_) { |
| 332 // Disable system tracing. | 315 // Disable system tracing. |
| 333 is_system_tracing_ = false; | 316 is_system_tracing_ = false; |
| 334 ++pending_disable_recording_ack_count_; | 317 ++pending_disable_recording_ack_count_; |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 424 base::Closure on_disable_monitoring_done_callback = | 407 base::Closure on_disable_monitoring_done_callback = |
| 425 base::Bind(&TracingControllerImpl::OnDisableMonitoringDone, | 408 base::Bind(&TracingControllerImpl::OnDisableMonitoringDone, |
| 426 base::Unretained(this), callback); | 409 base::Unretained(this), callback); |
| 427 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 410 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 428 base::Bind(&TracingControllerImpl::SetDisabledOnFileThread, | 411 base::Bind(&TracingControllerImpl::SetDisabledOnFileThread, |
| 429 base::Unretained(this), | 412 base::Unretained(this), |
| 430 on_disable_monitoring_done_callback)); | 413 on_disable_monitoring_done_callback)); |
| 431 return true; | 414 return true; |
| 432 } | 415 } |
| 433 | 416 |
| 417 scoped_refptr<TracingController::TraceDataSink> |
| 418 TracingController::CreateStringSink( |
| 419 const base::Callback<void(base::RefCountedString*)>& callback) { |
| 420 return new StringTraceDataSink(callback); |
| 421 } |
| 422 |
| 423 scoped_refptr<TracingController::TraceDataSink> |
| 424 TracingController::CreateFileSink(const base::FilePath& file_path, |
| 425 const base::Closure& callback) { |
| 426 return new FileTraceDataSink(file_path, callback); |
| 427 } |
| 428 |
| 434 void TracingControllerImpl::OnDisableMonitoringDone( | 429 void TracingControllerImpl::OnDisableMonitoringDone( |
| 435 const DisableMonitoringDoneCallback& callback) { | 430 const DisableMonitoringDoneCallback& callback) { |
| 436 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 431 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 437 | 432 |
| 438 OnMonitoringStateChanged(false); | 433 OnMonitoringStateChanged(false); |
| 439 | 434 |
| 440 // Notify all child processes. | 435 // Notify all child processes. |
| 441 for (TraceMessageFilterSet::iterator it = trace_message_filters_.begin(); | 436 for (TraceMessageFilterSet::iterator it = trace_message_filters_.begin(); |
| 442 it != trace_message_filters_.end(); ++it) { | 437 it != trace_message_filters_.end(); ++it) { |
| 443 it->get()->SendDisableMonitoring(); | 438 it->get()->SendDisableMonitoring(); |
| 444 } | 439 } |
| 445 | |
| 446 if (!callback.is_null()) | 440 if (!callback.is_null()) |
| 447 callback.Run(); | 441 callback.Run(); |
| 448 } | 442 } |
| 449 | 443 |
| 450 void TracingControllerImpl::GetMonitoringStatus( | 444 void TracingControllerImpl::GetMonitoringStatus( |
| 451 bool* out_enabled, | 445 bool* out_enabled, |
| 452 CategoryFilter* out_category_filter, | 446 CategoryFilter* out_category_filter, |
| 453 TraceOptions* out_trace_options) { | 447 TraceOptions* out_trace_options) { |
| 454 *out_enabled = is_monitoring_; | 448 *out_enabled = is_monitoring_; |
| 455 *out_category_filter = TraceLog::GetInstance()->GetCurrentCategoryFilter(); | 449 *out_category_filter = TraceLog::GetInstance()->GetCurrentCategoryFilter(); |
| 456 *out_trace_options = trace_options_; | 450 *out_trace_options = trace_options_; |
| 457 } | 451 } |
| 458 | 452 |
| 459 bool TracingControllerImpl::CaptureMonitoringSnapshot( | 453 bool TracingControllerImpl::CaptureMonitoringSnapshot( |
| 460 const base::FilePath& result_file_path, | 454 const scoped_refptr<TraceDataSink>& monitoring_data_sink) { |
| 461 const TracingFileResultCallback& callback) { | |
| 462 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 455 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 463 | 456 |
| 464 if (!can_disable_monitoring()) | 457 if (!can_disable_monitoring()) |
| 465 return false; | 458 return false; |
| 466 | 459 |
| 467 if (callback.is_null() && result_file_path.empty()) | 460 if (!monitoring_data_sink.get()) |
| 468 return false; | 461 return false; |
| 469 | 462 |
| 470 pending_capture_monitoring_snapshot_done_callback_ = callback; | 463 monitoring_data_sink_ = monitoring_data_sink; |
| 471 monitoring_snapshot_file_.reset(new ResultFile(result_file_path)); | |
| 472 | 464 |
| 473 // Count myself in pending_capture_monitoring_snapshot_ack_count_, | 465 // Count myself in pending_capture_monitoring_snapshot_ack_count_, |
| 474 // acked below. | 466 // acked below. |
| 475 pending_capture_monitoring_snapshot_ack_count_ = | 467 pending_capture_monitoring_snapshot_ack_count_ = |
| 476 trace_message_filters_.size() + 1; | 468 trace_message_filters_.size() + 1; |
| 477 pending_capture_monitoring_filters_ = trace_message_filters_; | 469 pending_capture_monitoring_filters_ = trace_message_filters_; |
| 478 | 470 |
| 479 // Handle special case of zero child processes by immediately flushing the | 471 // Handle special case of zero child processes by immediately flushing the |
| 480 // trace log. Once the flush has completed the caller will be notified that | 472 // trace log. Once the flush has completed the caller will be notified that |
| 481 // the capture snapshot has ended. | 473 // the capture snapshot has ended. |
| (...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 678 // called with the last of the local trace data. | 670 // called with the last of the local trace data. |
| 679 TraceLog::GetInstance()->Flush( | 671 TraceLog::GetInstance()->Flush( |
| 680 base::Bind(&TracingControllerImpl::OnLocalTraceDataCollected, | 672 base::Bind(&TracingControllerImpl::OnLocalTraceDataCollected, |
| 681 base::Unretained(this))); | 673 base::Unretained(this))); |
| 682 return; | 674 return; |
| 683 } | 675 } |
| 684 | 676 |
| 685 if (pending_disable_recording_ack_count_ != 0) | 677 if (pending_disable_recording_ack_count_ != 0) |
| 686 return; | 678 return; |
| 687 | 679 |
| 688 OnDisableRecordingComplete(); | |
| 689 } | |
| 690 | |
| 691 void TracingControllerImpl::OnDisableRecordingComplete() { | |
| 692 // All acks (including from the subprocesses and the local trace) have been | 680 // All acks (including from the subprocesses and the local trace) have been |
| 693 // received. | 681 // received. |
| 694 is_recording_ = false; | 682 is_recording_ = false; |
| 695 | 683 |
| 696 // Trigger callback if one is set. | 684 // Trigger callback if one is set. |
| 697 if (!pending_get_categories_done_callback_.is_null()) { | 685 if (!pending_get_categories_done_callback_.is_null()) { |
| 698 pending_get_categories_done_callback_.Run(known_category_groups_); | 686 pending_get_categories_done_callback_.Run(known_category_groups_); |
| 699 pending_get_categories_done_callback_.Reset(); | 687 pending_get_categories_done_callback_.Reset(); |
| 700 } else if (result_file_) { | 688 } else if (trace_data_sink_.get()) { |
| 701 result_file_->Close( | 689 trace_data_sink_->Close(); |
| 702 base::Bind(&TracingControllerImpl::OnResultFileClosed, | 690 trace_data_sink_ = NULL; |
| 703 base::Unretained(this))); | |
| 704 } | 691 } |
| 705 } | 692 } |
| 706 | 693 |
| 707 void TracingControllerImpl::OnResultFileClosed() { | |
| 708 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 709 | |
| 710 if (!result_file_) | |
| 711 return; | |
| 712 | |
| 713 if (!pending_disable_recording_done_callback_.is_null()) { | |
| 714 pending_disable_recording_done_callback_.Run(result_file_->path()); | |
| 715 pending_disable_recording_done_callback_.Reset(); | |
| 716 } | |
| 717 result_file_.reset(); | |
| 718 } | |
| 719 | |
| 720 #if defined(OS_CHROMEOS) || defined(OS_WIN) | 694 #if defined(OS_CHROMEOS) || defined(OS_WIN) |
| 721 void TracingControllerImpl::OnEndSystemTracingAcked( | 695 void TracingControllerImpl::OnEndSystemTracingAcked( |
| 722 const scoped_refptr<base::RefCountedString>& events_str_ptr) { | 696 const scoped_refptr<base::RefCountedString>& events_str_ptr) { |
| 723 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 697 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 724 | 698 |
| 725 if (result_file_) | 699 if (trace_data_sink_.get()) { |
| 726 result_file_->WriteSystemTrace(events_str_ptr); | 700 #if defined(OS_WIN) |
| 727 | 701 // The Windows kernel events are kept into a JSon format stored as string |
| 702 // and must not be escaped. |
| 703 std::string json_string = events_str_ptr->data(); |
| 704 #else |
| 705 std::string json_string = |
| 706 base::GetQuotedJSONString(events_str_ptr->data()); |
| 707 #endif |
| 708 trace_data_sink_->SetSystemTrace(json_string); |
| 709 } |
| 728 DCHECK(!is_system_tracing_); | 710 DCHECK(!is_system_tracing_); |
| 729 std::vector<std::string> category_groups; | 711 std::vector<std::string> category_groups; |
| 730 OnDisableRecordingAcked(NULL, category_groups); | 712 OnDisableRecordingAcked(NULL, category_groups); |
| 731 } | 713 } |
| 732 #endif | 714 #endif |
| 733 | 715 |
| 734 void TracingControllerImpl::OnCaptureMonitoringSnapshotAcked( | 716 void TracingControllerImpl::OnCaptureMonitoringSnapshotAcked( |
| 735 TraceMessageFilter* trace_message_filter) { | 717 TraceMessageFilter* trace_message_filter) { |
| 736 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 718 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
| 737 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 719 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| (...skipping 18 matching lines...) Expand all Loading... |
| 756 // will be called with the last of the local trace data. | 738 // will be called with the last of the local trace data. |
| 757 TraceLog::GetInstance()->FlushButLeaveBufferIntact( | 739 TraceLog::GetInstance()->FlushButLeaveBufferIntact( |
| 758 base::Bind(&TracingControllerImpl::OnLocalMonitoringTraceDataCollected, | 740 base::Bind(&TracingControllerImpl::OnLocalMonitoringTraceDataCollected, |
| 759 base::Unretained(this))); | 741 base::Unretained(this))); |
| 760 return; | 742 return; |
| 761 } | 743 } |
| 762 | 744 |
| 763 if (pending_capture_monitoring_snapshot_ack_count_ != 0) | 745 if (pending_capture_monitoring_snapshot_ack_count_ != 0) |
| 764 return; | 746 return; |
| 765 | 747 |
| 766 if (monitoring_snapshot_file_) { | 748 if (monitoring_data_sink_.get()) { |
| 767 monitoring_snapshot_file_->Close( | 749 monitoring_data_sink_->Close(); |
| 768 base::Bind(&TracingControllerImpl::OnMonitoringSnapshotFileClosed, | 750 monitoring_data_sink_ = NULL; |
| 769 base::Unretained(this))); | |
| 770 } | 751 } |
| 771 } | 752 } |
| 772 | 753 |
| 773 void TracingControllerImpl::OnMonitoringSnapshotFileClosed() { | |
| 774 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 775 | |
| 776 if (!monitoring_snapshot_file_) | |
| 777 return; | |
| 778 | |
| 779 if (!pending_capture_monitoring_snapshot_done_callback_.is_null()) { | |
| 780 pending_capture_monitoring_snapshot_done_callback_.Run( | |
| 781 monitoring_snapshot_file_->path()); | |
| 782 pending_capture_monitoring_snapshot_done_callback_.Reset(); | |
| 783 } | |
| 784 monitoring_snapshot_file_.reset(); | |
| 785 } | |
| 786 | |
| 787 void TracingControllerImpl::OnTraceDataCollected( | 754 void TracingControllerImpl::OnTraceDataCollected( |
| 788 const scoped_refptr<base::RefCountedString>& events_str_ptr) { | 755 const scoped_refptr<base::RefCountedString>& events_str_ptr) { |
| 789 // OnTraceDataCollected may be called from any browser thread, either by the | 756 // OnTraceDataCollected may be called from any browser thread, either by the |
| 790 // local event trace system or from child processes via TraceMessageFilter. | 757 // local event trace system or from child processes via TraceMessageFilter. |
| 791 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 758 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
| 792 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 759 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 793 base::Bind(&TracingControllerImpl::OnTraceDataCollected, | 760 base::Bind(&TracingControllerImpl::OnTraceDataCollected, |
| 794 base::Unretained(this), events_str_ptr)); | 761 base::Unretained(this), events_str_ptr)); |
| 795 return; | 762 return; |
| 796 } | 763 } |
| 797 | 764 |
| 798 if (result_file_) | 765 if (trace_data_sink_.get()) |
| 799 result_file_->Write(events_str_ptr); | 766 trace_data_sink_->AddTraceChunk(events_str_ptr->data()); |
| 800 } | 767 } |
| 801 | 768 |
| 802 void TracingControllerImpl::OnMonitoringTraceDataCollected( | 769 void TracingControllerImpl::OnMonitoringTraceDataCollected( |
| 803 const scoped_refptr<base::RefCountedString>& events_str_ptr) { | 770 const scoped_refptr<base::RefCountedString>& events_str_ptr) { |
| 804 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 771 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
| 805 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 772 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 806 base::Bind(&TracingControllerImpl::OnMonitoringTraceDataCollected, | 773 base::Bind(&TracingControllerImpl::OnMonitoringTraceDataCollected, |
| 807 base::Unretained(this), events_str_ptr)); | 774 base::Unretained(this), events_str_ptr)); |
| 808 return; | 775 return; |
| 809 } | 776 } |
| 810 | 777 |
| 811 if (monitoring_snapshot_file_) | 778 if (monitoring_data_sink_.get()) |
| 812 monitoring_snapshot_file_->Write(events_str_ptr); | 779 monitoring_data_sink_->AddTraceChunk(events_str_ptr->data()); |
| 813 } | 780 } |
| 814 | 781 |
| 815 void TracingControllerImpl::OnLocalTraceDataCollected( | 782 void TracingControllerImpl::OnLocalTraceDataCollected( |
| 816 const scoped_refptr<base::RefCountedString>& events_str_ptr, | 783 const scoped_refptr<base::RefCountedString>& events_str_ptr, |
| 817 bool has_more_events) { | 784 bool has_more_events) { |
| 818 if (events_str_ptr->data().size()) | 785 if (events_str_ptr->data().size()) |
| 819 OnTraceDataCollected(events_str_ptr); | 786 OnTraceDataCollected(events_str_ptr); |
| 820 | 787 |
| 821 if (has_more_events) | 788 if (has_more_events) |
| 822 return; | 789 return; |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 902 is_monitoring_ = is_monitoring; | 869 is_monitoring_ = is_monitoring; |
| 903 #if !defined(OS_ANDROID) | 870 #if !defined(OS_ANDROID) |
| 904 for (std::set<TracingUI*>::iterator it = tracing_uis_.begin(); | 871 for (std::set<TracingUI*>::iterator it = tracing_uis_.begin(); |
| 905 it != tracing_uis_.end(); it++) { | 872 it != tracing_uis_.end(); it++) { |
| 906 (*it)->OnMonitoringStateChanged(is_monitoring); | 873 (*it)->OnMonitoringStateChanged(is_monitoring); |
| 907 } | 874 } |
| 908 #endif | 875 #endif |
| 909 } | 876 } |
| 910 | 877 |
| 911 } // namespace content | 878 } // namespace content |
| OLD | NEW |