base: Factor task debug annotations out of MessageLoop

The MessageLoop has some code for adding debug annotations such as trace
flows and memory usage tags to posted tasks. Since we want to use these
annotations more generally for tasks that are scheduled outside the base
message loop, this patch factors it out into a standalone class.

TEST=TaskAnnotatorTest
BUG=391005

Review URL: https://codereview.chromium.org/455833004

Cr-Commit-Position: refs/heads/master@{#289560}
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@289560 0039d316-1c4b-4281-b951-d872f2087c98


CrOS-Libchrome-Original-Commit: ad8fb459e07068582588d72fd5dabdb72e70b689
diff --git a/base/message_loop/message_loop.cc b/base/message_loop/message_loop.cc
index c0d9b0e..69a02a3 100644
--- a/base/message_loop/message_loop.cc
+++ b/base/message_loop/message_loop.cc
@@ -8,8 +8,6 @@
 
 #include "base/bind.h"
 #include "base/compiler_specific.h"
-#include "base/debug/alias.h"
-#include "base/debug/trace_event.h"
 #include "base/lazy_instance.h"
 #include "base/logging.h"
 #include "base/memory/scoped_ptr.h"
@@ -423,45 +421,20 @@
 }
 
 void MessageLoop::RunTask(const PendingTask& pending_task) {
-  tracked_objects::TrackedTime start_time =
-      tracked_objects::ThreadData::NowForStartOfRun(pending_task.birth_tally);
-
-  TRACE_EVENT_FLOW_END1(TRACE_DISABLED_BY_DEFAULT("toplevel.flow"),
-      "MessageLoop::PostTask", TRACE_ID_MANGLE(GetTaskTraceID(pending_task)),
-      "queue_duration",
-      (start_time - pending_task.EffectiveTimePosted()).InMilliseconds());
-  // When tracing memory for posted tasks it's more valuable to attribute the
-  // memory allocations to the source function than generically to "RunTask".
-  TRACE_EVENT_WITH_MEMORY_TAG2(
-      "toplevel", "MessageLoop::RunTask",
-      pending_task.posted_from.function_name(),  // Name for memory tracking.
-      "src_file", pending_task.posted_from.file_name(),
-      "src_func", pending_task.posted_from.function_name());
-
   DCHECK(nestable_tasks_allowed_);
+
   // Execute the task and assume the worst: It is probably not reentrant.
   nestable_tasks_allowed_ = false;
 
-  // Before running the task, store the program counter where it was posted
-  // and deliberately alias it to ensure it is on the stack if the task
-  // crashes. Be careful not to assume that the variable itself will have the
-  // expected value when displayed by the optimizer in an optimized build.
-  // Look at a memory dump of the stack.
-  const void* program_counter =
-      pending_task.posted_from.program_counter();
-  debug::Alias(&program_counter);
-
   HistogramEvent(kTaskRunEvent);
 
   FOR_EACH_OBSERVER(TaskObserver, task_observers_,
                     WillProcessTask(pending_task));
-  pending_task.task.Run();
+  task_annotator_.RunTask(
+      "MessageLoop::PostTask", "MessageLoop::RunTask", pending_task);
   FOR_EACH_OBSERVER(TaskObserver, task_observers_,
                     DidProcessTask(pending_task));
 
-  tracked_objects::ThreadData::TallyRunOnNamedThreadIfTracking(pending_task,
-      start_time, tracked_objects::ThreadData::NowForEndOfRun());
-
   nestable_tasks_allowed_ = true;
 }
 
@@ -513,11 +486,6 @@
   return did_work;
 }
 
-uint64 MessageLoop::GetTaskTraceID(const PendingTask& task) {
-  return (static_cast<uint64>(task.sequence_num) << 32) |
-         ((static_cast<uint64>(reinterpret_cast<intptr_t>(this)) << 32) >> 32);
-}
-
 void MessageLoop::ReloadWorkQueue() {
   // We can improve performance of our loading tasks from the incoming queue to
   // |*work_queue| by waiting until the last minute (|*work_queue| is empty) to