Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 // Activity tracking provides a low-overhead method of collecting information | 5 // Activity tracking provides a low-overhead method of collecting information |
| 6 // about the state of the application for analysis both while it is running | 6 // about the state of the application for analysis both while it is running |
| 7 // and after it has terminated unexpectedly. Its primary purpose is to help | 7 // and after it has terminated unexpectedly. Its primary purpose is to help |
| 8 // locate reasons the browser becomes unresponsive by providing insight into | 8 // locate reasons the browser becomes unresponsive by providing insight into |
| 9 // what all the various threads and processes are (or were) doing. | 9 // what all the various threads and processes are (or were) doing. |
| 10 | 10 |
| 11 #ifndef BASE_DEBUG_ACTIVITY_TRACKER_H_ | 11 #ifndef BASE_DEBUG_ACTIVITY_TRACKER_H_ |
| 12 #define BASE_DEBUG_ACTIVITY_TRACKER_H_ | 12 #define BASE_DEBUG_ACTIVITY_TRACKER_H_ |
| 13 | 13 |
| 14 // std::atomic is undesired due to performance issues when used as global | 14 // std::atomic is undesired due to performance issues when used as global |
| 15 // variables. There are no such instances here. This module uses the | 15 // variables. There are no such instances here. This module uses the |
| 16 // PersistentMemoryAllocator which also uses std::atomic and is written | 16 // PersistentMemoryAllocator which also uses std::atomic and is written |
| 17 // by the same author. | 17 // by the same author. |
| 18 #include <atomic> | 18 #include <atomic> |
| 19 #include <memory> | 19 #include <memory> |
| 20 #include <string> | 20 #include <string> |
| 21 #include <vector> | 21 #include <vector> |
| 22 | 22 |
| 23 #include "base/base_export.h" | 23 #include "base/base_export.h" |
| 24 #include "base/location.h" | 24 #include "base/location.h" |
| 25 #include "base/metrics/persistent_memory_allocator.h" | 25 #include "base/metrics/persistent_memory_allocator.h" |
| 26 #include "base/threading/platform_thread.h" | |
| 26 #include "base/threading/thread_checker.h" | 27 #include "base/threading/thread_checker.h" |
| 27 #include "base/threading/thread_local_storage.h" | 28 #include "base/threading/thread_local_storage.h" |
| 28 | 29 |
| 29 namespace base { | 30 namespace base { |
| 30 | 31 |
| 31 struct PendingTask; | 32 struct PendingTask; |
| 32 | 33 |
| 33 class FilePath; | 34 class FilePath; |
| 34 class Lock; | 35 class Lock; |
| 35 class MemoryMappedFile; | 36 class MemoryMappedFile; |
| 36 class PlatformThreadHandle; | 37 class PlatformThreadHandle; |
| 37 class Process; | 38 class Process; |
| 38 class WaitableEvent; | 39 class WaitableEvent; |
| 39 | 40 |
| 40 namespace debug { | 41 namespace debug { |
| 41 | 42 |
| 42 class ThreadActivityTracker; | 43 class ThreadActivityTracker; |
| 43 | 44 |
| 45 | |
| 46 //============================================================================= | |
| 47 // This class provides a lock-free stack of any atomic type with the | |
| 48 // limitation that there must be at least one "invalid" value. This is | |
| 49 // built as a completely generic type and can (hopefully) be moved to a | |
| 50 // more generally useful place in the future. | |
| 51 template <typename T> | |
| 52 class LockFreeSimpleStack { | |
| 53 public: | |
| 54 // Construct a simple lock-free stack with the specified |size| but | |
| 55 // not alowed to hold the |invalid_value|. | |
| 56 LockFreeSimpleStack(size_t size, T invalid_value) | |
| 57 : size_(size), invalid_value_(invalid_value), head_(0) { | |
| 58 DCHECK_LE(1U, size); | |
| 59 | |
| 60 // Allocate memory for the stack value. | |
| 61 values_.reset(new std::atomic<T>[size_]); | |
| 62 | |
| 63 // Ensure that the underlying atomics are also lock-free. This should | |
| 64 // evaluate to a constant at compile time and so produce no code, but | |
| 65 // a static_assert will not compile. | |
| 66 CHECK(head_.is_lock_free()); | |
| 67 CHECK(values_[0].is_lock_free()); | |
| 68 | |
| 69 // All elements must be "invalid" to start in order for the push/pop | |
| 70 // operations to work. | |
| 71 for (size_t i = 0; i < size_; ++i) | |
| 72 values_[i].store(invalid_value_, std::memory_order_relaxed); | |
| 73 } | |
| 74 | |
| 75 T invalid_value() { return invalid_value_; } | |
| 76 size_t size() { return size_; } | |
| 77 size_t used() { return (head_.load(std::memory_order_relaxed)); } | |
| 78 bool empty() { return used() == 0; } | |
| 79 bool full() { return used() == size_; } | |
| 80 | |
| 81 // Adds a new |value| to the top of the stack and returns true on success | |
| 82 // or false if the stack was full. | |
| 83 bool push(T value); | |
| 84 | |
| 85 // Retrieves the top value off the stack and returns it or the "invalid" | |
| 86 // value if the stack is empty. | |
| 87 T pop(); | |
| 88 | |
| 89 private: | |
| 90 const size_t size_; // Size of the internal |values_| | |
| 91 const T invalid_value_; // A value not allowed to be stored. | |
| 92 | |
| 93 std::atomic<size_t> head_; // One past the newest value; where to push. | |
|
manzagop (departed)
2016/08/25 15:26:55
nit: is "index of the first empty slot" clearer?
bcwhite
2016/08/25 15:53:19
Done.
| |
| 94 | |
| 95 // Array holding pushed values. | |
| 96 std::unique_ptr<std::atomic<T>[]> values_; | |
| 97 | |
| 98 DISALLOW_COPY_AND_ASSIGN(LockFreeSimpleStack); | |
| 99 }; | |
| 100 | |
| 101 template <typename T> | |
| 102 bool LockFreeSimpleStack<T>::push(T value) { | |
| 103 // Pushing the "invalid" value is not allowed; it would cause an infinite | |
| 104 // loop in pop. | |
| 105 CHECK_NE(invalid_value_, value); | |
| 106 | |
| 107 // Get the head of the stack. | |
| 108 size_t head = head_.load(std::memory_order_relaxed); | |
| 109 | |
| 110 // In short: allocate a slot at the head of the stack, write the value to | |
| 111 // it, try again if anything gets in the way. | |
| 112 while (true) { | |
| 113 DCHECK_LE(0U, head); | |
| 114 DCHECK_GE(size_, head); | |
| 115 | |
| 116 // If the stack is full, fail. | |
| 117 if (head == size_) | |
| 118 return false; | |
| 119 | |
| 120 // The "head" is the critical resource so allocate a slot from the | |
| 121 // |values_| buffer at its current location, acquiring the value there. | |
| 122 // A "weak" operation is used because it's relatively trivial to try | |
| 123 // this operation again. | |
| 124 size_t slot = head; | |
| 125 if (!head_.compare_exchange_weak(slot, head + 1, | |
| 126 std::memory_order_acquire, | |
| 127 std::memory_order_relaxed)) { | |
| 128 // The exchange will have loaded the latest |head_| into |slot|. | |
| 129 head = slot; | |
| 130 continue; | |
| 131 } | |
| 132 | |
| 133 // Save the value being pushed to the reserved slot, overwriting the | |
| 134 // "invalid" value that should be there. If it's not, it's because the | |
| 135 // slot was released by a pop() but that method hasn't yet extracted | |
| 136 // the value. Wait for it to do so. Use a "strong" exchange to avoid | |
| 137 // mistakenly releasing the CPU. | |
| 138 T expected_value = invalid_value_; | |
| 139 while (!values_[slot].compare_exchange_strong(expected_value, value, | |
| 140 std::memory_order_relaxed, | |
| 141 std::memory_order_relaxed)) { | |
| 142 PlatformThread::YieldCurrentThread(); | |
| 143 expected_value = invalid_value_; | |
| 144 } | |
| 145 | |
| 146 // Success! | |
| 147 return true; | |
| 148 } | |
| 149 } | |
| 150 | |
| 151 template <typename T> | |
| 152 T LockFreeSimpleStack<T>::pop() { | |
| 153 // Get the head of the stack. | |
| 154 size_t head = head_.load(std::memory_order_relaxed); | |
| 155 | |
| 156 // In short: deallocate a slot at the head of the stack, read the value from | |
| 157 // it, try again if anything gets in the way. | |
| 158 while (true) { | |
| 159 DCHECK_LE(0U, head); | |
| 160 DCHECK_GE(size_, head); | |
| 161 | |
| 162 // If the stack is full, fail. | |
|
manzagop (departed)
2016/08/25 15:26:55
nit: full -> empty
bcwhite
2016/08/25 15:53:19
Done.
| |
| 163 if (head == 0) | |
| 164 return invalid_value_; | |
| 165 | |
| 166 // The "head" is the critical resource so deallocate a slot from the | |
| 167 // |values_| buffer at its current location, acquiring the value there. | |
| 168 // A "weak" operation is used because it's relatively trivial to try | |
| 169 // this operation again. | |
| 170 size_t slot = head; | |
| 171 if (!head_.compare_exchange_weak(slot, head - 1, | |
| 172 std::memory_order_acquire, | |
| 173 std::memory_order_relaxed)) { | |
| 174 // The exchange will have loaded the latest |head_| into |slot|. | |
| 175 head = slot; | |
| 176 continue; | |
| 177 } | |
| 178 --slot; // "head" is past-the-top | |
| 179 | |
| 180 // Read a value from the top of the stack, writing the "invalid" value | |
| 181 // in its place. If the retrieved value is invalid then the slot was | |
| 182 // acquired by push() but that method hasn't yet written the value. Wait | |
| 183 // for it to do so. | |
| 184 T value; | |
| 185 while ((value = values_[slot].exchange( | |
| 186 invalid_value_, std::memory_order_relaxed)) == invalid_value_) { | |
| 187 PlatformThread::YieldCurrentThread(); | |
| 188 } | |
| 189 | |
| 190 // Success! | |
| 191 DCHECK_NE(invalid_value_, value); | |
| 192 return value; | |
| 193 } | |
| 194 } | |
| 195 //============================================================================= | |
| 196 | |
| 197 | |
| 44 enum : int { | 198 enum : int { |
| 45 // The maximum number of call-stack addresses stored per activity. This | 199 // The maximum number of call-stack addresses stored per activity. This |
| 46 // cannot be changed without also changing the version number of the | 200 // cannot be changed without also changing the version number of the |
| 47 // structure. See kTypeIdActivityTracker in GlobalActivityTracker. | 201 // structure. See kTypeIdActivityTracker in GlobalActivityTracker. |
| 48 kActivityCallStackSize = 10, | 202 kActivityCallStackSize = 10, |
| 49 }; | 203 }; |
| 50 | 204 |
| 51 // The data associated with an activity is dependent upon the activity type. | 205 // The data associated with an activity is dependent upon the activity type. |
| 52 // This union defines all of the various fields. All fields must be explicitly | 206 // This union defines all of the various fields. All fields must be explicitly |
| 53 // sized types to ensure no interoperability problems between 32-bit and | 207 // sized types to ensure no interoperability problems between 32-bit and |
| (...skipping 447 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 501 // The size (in bytes) of memory required by a ThreadActivityTracker to | 655 // The size (in bytes) of memory required by a ThreadActivityTracker to |
| 502 // provide the stack-depth requested during construction. | 656 // provide the stack-depth requested during construction. |
| 503 const size_t stack_memory_size_; | 657 const size_t stack_memory_size_; |
| 504 | 658 |
| 505 // The activity tracker for the currently executing thread. | 659 // The activity tracker for the currently executing thread. |
| 506 base::ThreadLocalStorage::Slot this_thread_tracker_; | 660 base::ThreadLocalStorage::Slot this_thread_tracker_; |
| 507 | 661 |
| 508 // These have to be lock-free because lock activity is tracked and causes | 662 // These have to be lock-free because lock activity is tracked and causes |
| 509 // re-entry problems. | 663 // re-entry problems. |
| 510 std::atomic<int> thread_tracker_count_; | 664 std::atomic<int> thread_tracker_count_; |
| 511 std::atomic<int> available_memories_count_; | 665 LockFreeSimpleStack<PersistentMemoryAllocator::Reference> available_memories_; |
| 512 std::atomic<PersistentMemoryAllocator::Reference> | |
| 513 available_memories_[kMaxThreadCount]; | |
| 514 | 666 |
| 515 // The active global activity tracker. | 667 // The active global activity tracker. |
| 516 static GlobalActivityTracker* g_tracker_; | 668 static GlobalActivityTracker* g_tracker_; |
| 517 | 669 |
| 518 DISALLOW_COPY_AND_ASSIGN(GlobalActivityTracker); | 670 DISALLOW_COPY_AND_ASSIGN(GlobalActivityTracker); |
| 519 }; | 671 }; |
| 520 | 672 |
| 521 | 673 |
| 522 // Record entry in to and out of an arbitrary block of code. | 674 // Record entry in to and out of an arbitrary block of code. |
| 523 class BASE_EXPORT ScopedActivity | 675 class BASE_EXPORT ScopedActivity |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 607 explicit ScopedProcessWaitActivity(const base::Process* process); | 759 explicit ScopedProcessWaitActivity(const base::Process* process); |
| 608 private: | 760 private: |
| 609 DISALLOW_COPY_AND_ASSIGN(ScopedProcessWaitActivity); | 761 DISALLOW_COPY_AND_ASSIGN(ScopedProcessWaitActivity); |
| 610 }; | 762 }; |
| 611 #endif | 763 #endif |
| 612 | 764 |
| 613 } // namespace debug | 765 } // namespace debug |
| 614 } // namespace base | 766 } // namespace base |
| 615 | 767 |
| 616 #endif // BASE_DEBUG_ACTIVITY_TRACKER_H_ | 768 #endif // BASE_DEBUG_ACTIVITY_TRACKER_H_ |
| OLD | NEW |