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 "base/message_loop/message_loop.h" | 5 #include "base/message_loop/message_loop.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
11 #include "base/debug/alias.h" | |
12 #include "base/debug/trace_event.h" | |
13 #include "base/lazy_instance.h" | 11 #include "base/lazy_instance.h" |
14 #include "base/logging.h" | 12 #include "base/logging.h" |
15 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
16 #include "base/message_loop/message_pump_default.h" | 14 #include "base/message_loop/message_pump_default.h" |
17 #include "base/metrics/histogram.h" | 15 #include "base/metrics/histogram.h" |
18 #include "base/metrics/statistics_recorder.h" | 16 #include "base/metrics/statistics_recorder.h" |
19 #include "base/run_loop.h" | 17 #include "base/run_loop.h" |
20 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" | 18 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" |
21 #include "base/thread_task_runner_handle.h" | 19 #include "base/thread_task_runner_handle.h" |
22 #include "base/threading/thread_local.h" | 20 #include "base/threading/thread_local.h" |
(...skipping 393 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
416 return false; | 414 return false; |
417 | 415 |
418 PendingTask pending_task = deferred_non_nestable_work_queue_.front(); | 416 PendingTask pending_task = deferred_non_nestable_work_queue_.front(); |
419 deferred_non_nestable_work_queue_.pop(); | 417 deferred_non_nestable_work_queue_.pop(); |
420 | 418 |
421 RunTask(pending_task); | 419 RunTask(pending_task); |
422 return true; | 420 return true; |
423 } | 421 } |
424 | 422 |
425 void MessageLoop::RunTask(const PendingTask& pending_task) { | 423 void MessageLoop::RunTask(const PendingTask& pending_task) { |
426 tracked_objects::TrackedTime start_time = | 424 DCHECK(nestable_tasks_allowed_); |
427 tracked_objects::ThreadData::NowForStartOfRun(pending_task.birth_tally); | |
428 | 425 |
429 TRACE_EVENT_FLOW_END1(TRACE_DISABLED_BY_DEFAULT("toplevel.flow"), | |
430 "MessageLoop::PostTask", TRACE_ID_MANGLE(GetTaskTraceID(pending_task)), | |
431 "queue_duration", | |
432 (start_time - pending_task.EffectiveTimePosted()).InMilliseconds()); | |
433 // When tracing memory for posted tasks it's more valuable to attribute the | |
434 // memory allocations to the source function than generically to "RunTask". | |
435 TRACE_EVENT_WITH_MEMORY_TAG2( | |
436 "toplevel", "MessageLoop::RunTask", | |
437 pending_task.posted_from.function_name(), // Name for memory tracking. | |
438 "src_file", pending_task.posted_from.file_name(), | |
439 "src_func", pending_task.posted_from.function_name()); | |
440 | |
441 DCHECK(nestable_tasks_allowed_); | |
442 // Execute the task and assume the worst: It is probably not reentrant. | 426 // Execute the task and assume the worst: It is probably not reentrant. |
443 nestable_tasks_allowed_ = false; | 427 nestable_tasks_allowed_ = false; |
444 | 428 |
445 // Before running the task, store the program counter where it was posted | |
446 // and deliberately alias it to ensure it is on the stack if the task | |
447 // crashes. Be careful not to assume that the variable itself will have the | |
448 // expected value when displayed by the optimizer in an optimized build. | |
449 // Look at a memory dump of the stack. | |
450 const void* program_counter = | |
451 pending_task.posted_from.program_counter(); | |
452 debug::Alias(&program_counter); | |
453 | |
454 HistogramEvent(kTaskRunEvent); | 429 HistogramEvent(kTaskRunEvent); |
455 | 430 |
456 FOR_EACH_OBSERVER(TaskObserver, task_observers_, | 431 FOR_EACH_OBSERVER(TaskObserver, task_observers_, |
457 WillProcessTask(pending_task)); | 432 WillProcessTask(pending_task)); |
458 pending_task.task.Run(); | 433 task_annotator_.RunTask( |
| 434 "MessageLoop::PostTask", "MessageLoop::RunTask", pending_task); |
459 FOR_EACH_OBSERVER(TaskObserver, task_observers_, | 435 FOR_EACH_OBSERVER(TaskObserver, task_observers_, |
460 DidProcessTask(pending_task)); | 436 DidProcessTask(pending_task)); |
461 | 437 |
462 tracked_objects::ThreadData::TallyRunOnNamedThreadIfTracking(pending_task, | |
463 start_time, tracked_objects::ThreadData::NowForEndOfRun()); | |
464 | |
465 nestable_tasks_allowed_ = true; | 438 nestable_tasks_allowed_ = true; |
466 } | 439 } |
467 | 440 |
468 bool MessageLoop::DeferOrRunPendingTask(const PendingTask& pending_task) { | 441 bool MessageLoop::DeferOrRunPendingTask(const PendingTask& pending_task) { |
469 if (pending_task.nestable || run_loop_->run_depth_ == 1) { | 442 if (pending_task.nestable || run_loop_->run_depth_ == 1) { |
470 RunTask(pending_task); | 443 RunTask(pending_task); |
471 // Show that we ran a task (Note: a new one might arrive as a | 444 // Show that we ran a task (Note: a new one might arrive as a |
472 // consequence!). | 445 // consequence!). |
473 return true; | 446 return true; |
474 } | 447 } |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
506 // not completely clear why we want to leak them in the loops above. This | 479 // not completely clear why we want to leak them in the loops above. This |
507 // code is replicating legacy behavior, and should not be considered | 480 // code is replicating legacy behavior, and should not be considered |
508 // absolutely "correct" behavior. See TODO above about deleting all tasks | 481 // absolutely "correct" behavior. See TODO above about deleting all tasks |
509 // when it's safe. | 482 // when it's safe. |
510 while (!delayed_work_queue_.empty()) { | 483 while (!delayed_work_queue_.empty()) { |
511 delayed_work_queue_.pop(); | 484 delayed_work_queue_.pop(); |
512 } | 485 } |
513 return did_work; | 486 return did_work; |
514 } | 487 } |
515 | 488 |
516 uint64 MessageLoop::GetTaskTraceID(const PendingTask& task) { | |
517 return (static_cast<uint64>(task.sequence_num) << 32) | | |
518 ((static_cast<uint64>(reinterpret_cast<intptr_t>(this)) << 32) >> 32); | |
519 } | |
520 | |
521 void MessageLoop::ReloadWorkQueue() { | 489 void MessageLoop::ReloadWorkQueue() { |
522 // We can improve performance of our loading tasks from the incoming queue to | 490 // We can improve performance of our loading tasks from the incoming queue to |
523 // |*work_queue| by waiting until the last minute (|*work_queue| is empty) to | 491 // |*work_queue| by waiting until the last minute (|*work_queue| is empty) to |
524 // load. That reduces the number of locks-per-task significantly when our | 492 // load. That reduces the number of locks-per-task significantly when our |
525 // queues get large. | 493 // queues get large. |
526 if (work_queue_.empty()) | 494 if (work_queue_.empty()) |
527 incoming_task_queue_->ReloadWorkQueue(&work_queue_); | 495 incoming_task_queue_->ReloadWorkQueue(&work_queue_); |
528 } | 496 } |
529 | 497 |
530 void MessageLoop::ScheduleWork(bool was_empty) { | 498 void MessageLoop::ScheduleWork(bool was_empty) { |
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
717 persistent, | 685 persistent, |
718 mode, | 686 mode, |
719 controller, | 687 controller, |
720 delegate); | 688 delegate); |
721 } | 689 } |
722 #endif | 690 #endif |
723 | 691 |
724 #endif // !defined(OS_NACL) | 692 #endif // !defined(OS_NACL) |
725 | 693 |
726 } // namespace base | 694 } // namespace base |
OLD | NEW |