| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 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 "mojo/common/task_tracker.h" | |
| 6 | |
| 7 #include <vector> | |
| 8 | |
| 9 #include "base/location.h" | |
| 10 #include "base/logging.h" | |
| 11 #include "base/macros.h" | |
| 12 #include "base/threading/thread_local.h" | |
| 13 #include "base/tracked_objects.h" | |
| 14 | |
| 15 namespace mojo { | |
| 16 namespace common { | |
| 17 | |
| 18 namespace { | |
| 19 | |
| 20 class TrackingActivation { | |
| 21 public: | |
| 22 TrackingActivation() : birth_(nullptr) {} | |
| 23 | |
| 24 bool Start(const char* function_name, | |
| 25 const char* file_name, | |
| 26 int line_number, | |
| 27 const void* program_counter); | |
| 28 void End(); | |
| 29 bool IsAlive() const { return birth_ != nullptr; } | |
| 30 | |
| 31 private: | |
| 32 tracked_objects::TaskStopwatch* stopwatch() { | |
| 33 return reinterpret_cast<tracked_objects::TaskStopwatch*>(stopwatch_heap_); | |
| 34 } | |
| 35 | |
| 36 tracked_objects::Births* birth_; | |
| 37 // TaskStopwatch isn't copyable, but we don't want to allocate it on the heap | |
| 38 // so as not to slow things down, hence replacement new. | |
| 39 char stopwatch_heap_[sizeof(tracked_objects::TaskStopwatch)]; | |
| 40 | |
| 41 DISALLOW_COPY_AND_ASSIGN(TrackingActivation); | |
| 42 }; | |
| 43 | |
| 44 bool TrackingActivation::Start(const char* function_name, | |
| 45 const char* file_name, | |
| 46 int line_number, | |
| 47 const void* program_counter) { | |
| 48 // So far we don't track nested invocations. | |
| 49 if (IsAlive()) | |
| 50 return false; | |
| 51 birth_ = tracked_objects::ThreadData::TallyABirthIfActive( | |
| 52 tracked_objects::Location(function_name, file_name, line_number, | |
| 53 program_counter)); | |
| 54 if (!birth_) | |
| 55 return false; | |
| 56 | |
| 57 (new (stopwatch()) tracked_objects::TaskStopwatch())->Start(); | |
| 58 return true; | |
| 59 } | |
| 60 | |
| 61 void TrackingActivation::End() { | |
| 62 DCHECK(IsAlive()); | |
| 63 stopwatch()->Stop(); | |
| 64 tracked_objects::ThreadData::TallyRunInAScopedRegionIfTracking(birth_, | |
| 65 *stopwatch()); | |
| 66 stopwatch()->tracked_objects::TaskStopwatch::~TaskStopwatch(); | |
| 67 birth_ = nullptr; | |
| 68 } | |
| 69 | |
| 70 base::ThreadLocalPointer<TrackingActivation> g_activation; | |
| 71 | |
| 72 } // namespace | |
| 73 | |
| 74 // static | |
| 75 intptr_t TaskTracker::StartTracking(const char* function_name, | |
| 76 const char* file_name, | |
| 77 int line_number, | |
| 78 const void* program_counter) { | |
| 79 TrackingActivation* activation = g_activation.Get(); | |
| 80 if (!activation) { | |
| 81 // Leak this. | |
| 82 activation = new TrackingActivation(); | |
| 83 g_activation.Set(activation); | |
| 84 } | |
| 85 | |
| 86 if (!activation->Start(function_name, file_name, line_number, | |
| 87 program_counter)) | |
| 88 return 0; | |
| 89 return reinterpret_cast<intptr_t>(activation); | |
| 90 } | |
| 91 | |
| 92 // static | |
| 93 void TaskTracker::EndTracking(intptr_t id) { | |
| 94 if (0 == id) | |
| 95 return; | |
| 96 // |EndTracking()| must be called from the same thread of |StartTracking()|. | |
| 97 DCHECK_EQ(reinterpret_cast<TrackingActivation*>(id), g_activation.Get()); | |
| 98 reinterpret_cast<TrackingActivation*>(id)->End(); | |
| 99 } | |
| 100 | |
| 101 } // namespace common | |
| 102 } // namespace mojo | |
| OLD | NEW |