Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / base / debug / task_annotator.cc
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/debug/task_annotator.h"
6
7 #include "base/debug/alias.h"
8 #include "base/debug/trace_event.h"
9 #include "base/pending_task.h"
10 #include "base/tracked_objects.h"
11
12 namespace base {
13 namespace debug {
14
15 TaskAnnotator::TaskAnnotator() {
16 }
17
18 TaskAnnotator::~TaskAnnotator() {
19 }
20
21 void TaskAnnotator::DidQueueTask(const char* queue_function,
22                                  const PendingTask& pending_task) {
23   TRACE_EVENT_FLOW_BEGIN0(TRACE_DISABLED_BY_DEFAULT("toplevel.flow"),
24                           queue_function,
25                           TRACE_ID_MANGLE(GetTaskTraceID(pending_task)));
26 }
27
28 void TaskAnnotator::RunTask(const char* queue_function,
29                             const char* run_function,
30                             const PendingTask& pending_task) {
31   tracked_objects::ThreadData::PrepareForStartOfRun(pending_task.birth_tally);
32   tracked_objects::TaskStopwatch stopwatch;
33   stopwatch.Start();
34   tracked_objects::Duration queue_duration =
35       stopwatch.StartTime() - pending_task.EffectiveTimePosted();
36
37   TRACE_EVENT_FLOW_END1(TRACE_DISABLED_BY_DEFAULT("toplevel.flow"),
38                         queue_function,
39                         TRACE_ID_MANGLE(GetTaskTraceID(pending_task)),
40                         "queue_duration",
41                         queue_duration.InMilliseconds());
42
43   // When tracing memory for posted tasks it's more valuable to attribute the
44   // memory allocations to the source function than generically to the task
45   // runner.
46   TRACE_EVENT_WITH_MEMORY_TAG2(
47       "toplevel",
48       run_function,
49       pending_task.posted_from.function_name(),  // Name for memory tracking.
50       "src_file",
51       pending_task.posted_from.file_name(),
52       "src_func",
53       pending_task.posted_from.function_name());
54
55   // Before running the task, store the program counter where it was posted
56   // and deliberately alias it to ensure it is on the stack if the task
57   // crashes. Be careful not to assume that the variable itself will have the
58   // expected value when displayed by the optimizer in an optimized build.
59   // Look at a memory dump of the stack.
60   const void* program_counter = pending_task.posted_from.program_counter();
61   debug::Alias(&program_counter);
62
63   pending_task.task.Run();
64
65   stopwatch.Stop();
66   tracked_objects::ThreadData::TallyRunOnNamedThreadIfTracking(
67       pending_task, stopwatch);
68 }
69
70 uint64 TaskAnnotator::GetTaskTraceID(const PendingTask& task) const {
71   return (static_cast<uint64>(task.sequence_num) << 32) |
72          ((static_cast<uint64>(reinterpret_cast<intptr_t>(this)) << 32) >> 32);
73 }
74
75 }  // namespace debug
76 }  // namespace base