OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 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 | 4 |
5 #include "content/browser/tracing/tracing_controller_impl.h" | 5 #include "content/browser/tracing/tracing_controller_impl.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/debug/trace_event.h" | 8 #include "base/debug/trace_event.h" |
9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/json/string_escape.h" | 10 #include "base/json/string_escape.h" |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
143 EnableRecordingDoneCallback())) { | 143 EnableRecordingDoneCallback())) { |
144 pending_get_categories_done_callback_.Reset(); | 144 pending_get_categories_done_callback_.Reset(); |
145 return false; | 145 return false; |
146 } | 146 } |
147 | 147 |
148 bool ok = DisableRecording(base::FilePath(), TracingFileResultCallback()); | 148 bool ok = DisableRecording(base::FilePath(), TracingFileResultCallback()); |
149 DCHECK(ok); | 149 DCHECK(ok); |
150 return true; | 150 return true; |
151 } | 151 } |
152 | 152 |
| 153 void TracingControllerImpl::SetEnabledOnFileThread( |
| 154 const std::string& category_filter, |
| 155 int trace_options, |
| 156 const base::Closure& callback) { |
| 157 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 158 |
| 159 TraceLog::GetInstance()->SetEnabled( |
| 160 base::debug::CategoryFilter(category_filter), |
| 161 static_cast<TraceLog::Options>(trace_options)); |
| 162 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, callback); |
| 163 } |
| 164 |
| 165 void TracingControllerImpl::SetDisabledOnFileThread( |
| 166 const base::Closure& callback) { |
| 167 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 168 |
| 169 TraceLog::GetInstance()->SetDisabled(); |
| 170 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, callback); |
| 171 } |
| 172 |
153 bool TracingControllerImpl::EnableRecording( | 173 bool TracingControllerImpl::EnableRecording( |
154 const std::string& category_filter, | 174 const std::string& category_filter, |
155 TracingController::Options options, | 175 TracingController::Options options, |
156 const EnableRecordingDoneCallback& callback) { | 176 const EnableRecordingDoneCallback& callback) { |
157 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 177 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
158 | 178 |
159 if (!can_enable_recording()) | 179 if (!can_enable_recording()) |
160 return false; | 180 return false; |
| 181 is_recording_ = true; |
161 | 182 |
162 #if defined(OS_ANDROID) | 183 #if defined(OS_ANDROID) |
163 if (pending_get_categories_done_callback_.is_null()) | 184 if (pending_get_categories_done_callback_.is_null()) |
164 TraceLog::GetInstance()->AddClockSyncMetadataEvent(); | 185 TraceLog::GetInstance()->AddClockSyncMetadataEvent(); |
165 #endif | 186 #endif |
166 | 187 |
167 TraceLog::Options trace_options = (options & RECORD_CONTINUOUSLY) ? | 188 int trace_options = (options & RECORD_CONTINUOUSLY) ? |
168 TraceLog::RECORD_CONTINUOUSLY : TraceLog::RECORD_UNTIL_FULL; | 189 TraceLog::RECORD_CONTINUOUSLY : TraceLog::RECORD_UNTIL_FULL; |
169 if (options & ENABLE_SAMPLING) { | 190 if (options & ENABLE_SAMPLING) { |
170 trace_options = static_cast<TraceLog::Options>( | 191 trace_options |= TraceLog::ENABLE_SAMPLING; |
171 trace_options | TraceLog::ENABLE_SAMPLING); | |
172 } | 192 } |
173 // TODO(haraken): How to handle ENABLE_SYSTRACE? | 193 // TODO(haraken): How to handle ENABLE_SYSTRACE? |
174 | 194 |
175 TraceLog::GetInstance()->SetEnabled( | 195 base::Closure on_enable_recording_done_callback = |
176 base::debug::CategoryFilter(category_filter), trace_options); | 196 base::Bind(&TracingControllerImpl::OnEnableRecordingDone, |
177 is_recording_ = true; | 197 base::Unretained(this), |
| 198 category_filter, trace_options, callback); |
| 199 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 200 base::Bind(&TracingControllerImpl::SetEnabledOnFileThread, |
| 201 base::Unretained(this), |
| 202 category_filter, trace_options, |
| 203 on_enable_recording_done_callback)); |
| 204 return true; |
| 205 } |
| 206 |
| 207 void TracingControllerImpl::OnEnableRecordingDone( |
| 208 const std::string& category_filter, |
| 209 int trace_options, |
| 210 const EnableRecordingDoneCallback& callback) { |
| 211 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
178 | 212 |
179 // Notify all child processes. | 213 // Notify all child processes. |
180 for (TraceMessageFilterMap::iterator it = trace_message_filters_.begin(); | 214 for (TraceMessageFilterMap::iterator it = trace_message_filters_.begin(); |
181 it != trace_message_filters_.end(); ++it) { | 215 it != trace_message_filters_.end(); ++it) { |
182 it->get()->SendBeginTracing(category_filter, trace_options); | 216 it->get()->SendBeginTracing(category_filter, |
| 217 static_cast<TraceLog::Options>(trace_options)); |
183 } | 218 } |
184 | 219 |
185 if (!callback.is_null()) | 220 if (!callback.is_null()) |
186 callback.Run(); | 221 callback.Run(); |
187 return true; | |
188 } | 222 } |
189 | 223 |
190 bool TracingControllerImpl::DisableRecording( | 224 bool TracingControllerImpl::DisableRecording( |
191 const base::FilePath& result_file_path, | 225 const base::FilePath& result_file_path, |
192 const TracingFileResultCallback& callback) { | 226 const TracingFileResultCallback& callback) { |
193 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 227 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
194 | 228 |
195 if (!can_disable_recording()) | 229 if (!can_disable_recording()) |
196 return false; | 230 return false; |
197 | 231 |
198 pending_disable_recording_done_callback_ = callback; | |
199 | |
200 // Disable local trace early to avoid traces during end-tracing process from | 232 // Disable local trace early to avoid traces during end-tracing process from |
201 // interfering with the process. | 233 // interfering with the process. |
202 TraceLog::GetInstance()->SetDisabled(); | 234 base::Closure on_disable_recording_done_callback = |
| 235 base::Bind(&TracingControllerImpl::OnDisableRecordingDone, |
| 236 base::Unretained(this), |
| 237 result_file_path, callback); |
| 238 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 239 base::Bind(&TracingControllerImpl::SetDisabledOnFileThread, |
| 240 base::Unretained(this), |
| 241 on_disable_recording_done_callback)); |
| 242 return true; |
| 243 } |
| 244 |
| 245 void TracingControllerImpl::OnDisableRecordingDone( |
| 246 const base::FilePath& result_file_path, |
| 247 const TracingFileResultCallback& callback) { |
| 248 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 249 |
| 250 pending_disable_recording_done_callback_ = callback; |
203 | 251 |
204 #if defined(OS_ANDROID) | 252 #if defined(OS_ANDROID) |
205 if (pending_get_categories_done_callback_.is_null()) | 253 if (pending_get_categories_done_callback_.is_null()) |
206 TraceLog::GetInstance()->AddClockSyncMetadataEvent(); | 254 TraceLog::GetInstance()->AddClockSyncMetadataEvent(); |
207 #endif | 255 #endif |
208 | 256 |
209 if (!callback.is_null() || !result_file_path.empty()) | 257 if (!callback.is_null() || !result_file_path.empty()) |
210 result_file_.reset(new ResultFile(result_file_path)); | 258 result_file_.reset(new ResultFile(result_file_path)); |
211 | 259 |
212 // Count myself (local trace) in pending_disable_recording_ack_count_, | 260 // Count myself (local trace) in pending_disable_recording_ack_count_, |
213 // acked below. | 261 // acked below. |
214 pending_disable_recording_ack_count_ = trace_message_filters_.size() + 1; | 262 pending_disable_recording_ack_count_ = trace_message_filters_.size() + 1; |
215 | 263 |
216 // Handle special case of zero child processes by immediately flushing the | 264 // Handle special case of zero child processes by immediately flushing the |
217 // trace log. Once the flush has completed the caller will be notified that | 265 // trace log. Once the flush has completed the caller will be notified that |
218 // tracing has ended. | 266 // tracing has ended. |
219 if (pending_disable_recording_ack_count_ == 1) { | 267 if (pending_disable_recording_ack_count_ == 1) { |
220 // Flush asynchronously now, because we don't have any children to wait for. | 268 // Flush asynchronously now, because we don't have any children to wait for. |
221 TraceLog::GetInstance()->Flush( | 269 TraceLog::GetInstance()->Flush( |
222 base::Bind(&TracingControllerImpl::OnLocalTraceDataCollected, | 270 base::Bind(&TracingControllerImpl::OnLocalTraceDataCollected, |
223 base::Unretained(this))); | 271 base::Unretained(this))); |
224 } | 272 } |
225 | 273 |
226 // Notify all child processes. | 274 // Notify all child processes. |
227 for (TraceMessageFilterMap::iterator it = trace_message_filters_.begin(); | 275 for (TraceMessageFilterMap::iterator it = trace_message_filters_.begin(); |
228 it != trace_message_filters_.end(); ++it) { | 276 it != trace_message_filters_.end(); ++it) { |
229 it->get()->SendEndTracing(); | 277 it->get()->SendEndTracing(); |
230 } | 278 } |
231 return true; | |
232 } | 279 } |
233 | 280 |
234 bool TracingControllerImpl::EnableMonitoring( | 281 bool TracingControllerImpl::EnableMonitoring( |
235 const std::string& category_filter, | 282 const std::string& category_filter, |
236 TracingController::Options options, | 283 TracingController::Options options, |
237 const EnableMonitoringDoneCallback& callback) { | 284 const EnableMonitoringDoneCallback& callback) { |
238 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 285 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
239 | 286 |
240 if (!can_enable_monitoring()) | 287 if (!can_enable_monitoring()) |
241 return false; | 288 return false; |
242 is_monitoring_ = true; | 289 is_monitoring_ = true; |
243 | 290 |
244 #if defined(OS_ANDROID) | 291 #if defined(OS_ANDROID) |
245 TraceLog::GetInstance()->AddClockSyncMetadataEvent(); | 292 TraceLog::GetInstance()->AddClockSyncMetadataEvent(); |
246 #endif | 293 #endif |
247 | 294 |
248 int monitoring_tracing_options = 0; | 295 int trace_options = 0; |
249 if (options & ENABLE_SAMPLING) | 296 if (options & ENABLE_SAMPLING) |
250 monitoring_tracing_options |= base::debug::TraceLog::MONITOR_SAMPLING; | 297 trace_options |= TraceLog::MONITOR_SAMPLING; |
251 | 298 |
252 TraceLog::GetInstance()->SetEnabled( | 299 base::Closure on_enable_monitoring_done_callback = |
253 base::debug::CategoryFilter(category_filter), | 300 base::Bind(&TracingControllerImpl::OnEnableMonitoringDone, |
254 static_cast<TraceLog::Options>(monitoring_tracing_options)); | 301 base::Unretained(this), |
| 302 category_filter, trace_options, callback); |
| 303 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 304 base::Bind(&TracingControllerImpl::SetEnabledOnFileThread, |
| 305 base::Unretained(this), |
| 306 category_filter, trace_options, |
| 307 on_enable_monitoring_done_callback)); |
| 308 return true; |
| 309 } |
| 310 |
| 311 void TracingControllerImpl::OnEnableMonitoringDone( |
| 312 const std::string& category_filter, |
| 313 int trace_options, |
| 314 const EnableMonitoringDoneCallback& callback) { |
| 315 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
255 | 316 |
256 // Notify all child processes. | 317 // Notify all child processes. |
257 for (TraceMessageFilterMap::iterator it = trace_message_filters_.begin(); | 318 for (TraceMessageFilterMap::iterator it = trace_message_filters_.begin(); |
258 it != trace_message_filters_.end(); ++it) { | 319 it != trace_message_filters_.end(); ++it) { |
259 it->get()->SendEnableMonitoring(category_filter, | 320 it->get()->SendEnableMonitoring(category_filter, |
260 static_cast<TraceLog::Options>(monitoring_tracing_options)); | 321 static_cast<TraceLog::Options>(trace_options)); |
261 } | 322 } |
262 | 323 |
263 if (!callback.is_null()) | 324 if (!callback.is_null()) |
264 callback.Run(); | 325 callback.Run(); |
265 return true; | |
266 } | 326 } |
267 | 327 |
268 bool TracingControllerImpl::DisableMonitoring( | 328 bool TracingControllerImpl::DisableMonitoring( |
269 const DisableMonitoringDoneCallback& callback) { | 329 const DisableMonitoringDoneCallback& callback) { |
270 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 330 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
271 | 331 |
272 if (!can_disable_monitoring()) | 332 if (!can_disable_monitoring()) |
273 return false; | 333 return false; |
| 334 |
| 335 base::Closure on_disable_monitoring_done_callback = |
| 336 base::Bind(&TracingControllerImpl::OnDisableMonitoringDone, |
| 337 base::Unretained(this), callback); |
| 338 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 339 base::Bind(&TracingControllerImpl::SetDisabledOnFileThread, |
| 340 base::Unretained(this), |
| 341 on_disable_monitoring_done_callback)); |
| 342 return true; |
| 343 } |
| 344 |
| 345 void TracingControllerImpl::OnDisableMonitoringDone( |
| 346 const DisableMonitoringDoneCallback& callback) { |
| 347 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 348 |
274 is_monitoring_ = false; | 349 is_monitoring_ = false; |
275 | 350 |
276 TraceLog::GetInstance()->SetDisabled(); | |
277 | |
278 // Notify all child processes. | 351 // Notify all child processes. |
279 for (TraceMessageFilterMap::iterator it = trace_message_filters_.begin(); | 352 for (TraceMessageFilterMap::iterator it = trace_message_filters_.begin(); |
280 it != trace_message_filters_.end(); ++it) { | 353 it != trace_message_filters_.end(); ++it) { |
281 it->get()->SendDisableMonitoring(); | 354 it->get()->SendDisableMonitoring(); |
282 } | 355 } |
283 | 356 |
284 if (!callback.is_null()) | 357 if (!callback.is_null()) |
285 callback.Run(); | 358 callback.Run(); |
286 return true; | |
287 } | 359 } |
288 | 360 |
289 void TracingControllerImpl::GetMonitoringStatus( | 361 void TracingControllerImpl::GetMonitoringStatus( |
290 bool* out_enabled, | 362 bool* out_enabled, |
291 std::string* out_category_filter, | 363 std::string* out_category_filter, |
292 TracingController::Options* out_options) { | 364 TracingController::Options* out_options) { |
293 NOTIMPLEMENTED(); | 365 NOTIMPLEMENTED(); |
294 } | 366 } |
295 | 367 |
296 bool TracingControllerImpl::CaptureMonitoringSnapshot( | 368 bool TracingControllerImpl::CaptureMonitoringSnapshot( |
(...skipping 338 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
635 base::Bind(&TracingControllerImpl::OnWatchEventMatched, | 707 base::Bind(&TracingControllerImpl::OnWatchEventMatched, |
636 base::Unretained(this))); | 708 base::Unretained(this))); |
637 return; | 709 return; |
638 } | 710 } |
639 | 711 |
640 if (!watch_event_callback_.is_null()) | 712 if (!watch_event_callback_.is_null()) |
641 watch_event_callback_.Run(); | 713 watch_event_callback_.Run(); |
642 } | 714 } |
643 | 715 |
644 } // namespace content | 716 } // namespace content |
OLD | NEW |