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