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 #include "base/debug/activity_analyzer.h" | 5 #include "base/debug/activity_analyzer.h" |
| 6 | 6 |
| 7 #include <atomic> | |
| 7 #include <memory> | 8 #include <memory> |
| 8 | 9 |
| 9 #include "base/bind.h" | 10 #include "base/bind.h" |
| 10 #include "base/debug/activity_tracker.h" | 11 #include "base/debug/activity_tracker.h" |
| 11 #include "base/files/file.h" | 12 #include "base/files/file.h" |
| 12 #include "base/files/file_util.h" | 13 #include "base/files/file_util.h" |
| 13 #include "base/files/memory_mapped_file.h" | 14 #include "base/files/memory_mapped_file.h" |
| 14 #include "base/files/scoped_temp_dir.h" | 15 #include "base/files/scoped_temp_dir.h" |
| 15 #include "base/memory/ptr_util.h" | 16 #include "base/memory/ptr_util.h" |
| 16 #include "base/pending_task.h" | 17 #include "base/pending_task.h" |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 83 class SimpleActivityThread : public SimpleThread { | 84 class SimpleActivityThread : public SimpleThread { |
| 84 public: | 85 public: |
| 85 SimpleActivityThread(const std::string& name, | 86 SimpleActivityThread(const std::string& name, |
| 86 const void* source, | 87 const void* source, |
| 87 Activity::Type activity, | 88 Activity::Type activity, |
| 88 const ActivityData& data) | 89 const ActivityData& data) |
| 89 : SimpleThread(name, Options()), | 90 : SimpleThread(name, Options()), |
| 90 source_(source), | 91 source_(source), |
| 91 activity_(activity), | 92 activity_(activity), |
| 92 data_(data), | 93 data_(data), |
| 94 ready_(false), | |
| 95 exit_(false), | |
| 93 exit_condition_(&lock_) {} | 96 exit_condition_(&lock_) {} |
| 94 | 97 |
| 95 ~SimpleActivityThread() override {} | 98 ~SimpleActivityThread() override {} |
| 96 | 99 |
| 97 void Run() override { | 100 void Run() override { |
| 98 GlobalActivityTracker::Get() | 101 GlobalActivityTracker::Get() |
| 99 ->GetOrCreateTrackerForCurrentThread() | 102 ->GetOrCreateTrackerForCurrentThread() |
| 100 ->PushActivity(source_, activity_, data_); | 103 ->PushActivity(source_, activity_, data_); |
| 101 | 104 |
| 102 { | 105 { |
| 103 AutoLock auto_lock(lock_); | 106 AutoLock auto_lock(lock_); |
| 104 ready_ = true; | 107 ready_.store(true, std::memory_order_relaxed); |
| 105 while (!exit_) | 108 while (!exit_.load(std::memory_order_relaxed)) |
| 106 exit_condition_.Wait(); | 109 exit_condition_.Wait(); |
| 107 } | 110 } |
| 108 | 111 |
| 109 GlobalActivityTracker::Get() | 112 GlobalActivityTracker::Get() |
| 110 ->GetOrCreateTrackerForCurrentThread() | 113 ->GetOrCreateTrackerForCurrentThread() |
| 111 ->PopActivity(); | 114 ->PopActivity(); |
| 112 } | 115 } |
| 113 | 116 |
| 114 void Exit() { | 117 void Exit() { |
| 115 AutoLock auto_lock(lock_); | 118 AutoLock auto_lock(lock_); |
| 116 exit_ = true; | 119 exit_.store(true, std::memory_order_relaxed); |
| 117 exit_condition_.Signal(); | 120 exit_condition_.Signal(); |
| 118 } | 121 } |
| 119 | 122 |
| 120 void WaitReady() { | 123 void WaitReady() { |
| 121 SPIN_FOR_1_SECOND_OR_UNTIL_TRUE(ready_); | 124 SPIN_FOR_1_SECOND_OR_UNTIL_TRUE(ready_.load(std::memory_order_relaxed)); |
|
Alexander Potapenko
2016/10/14 10:07:33
You're waiting for another thread here, but do not
| |
| 122 } | 125 } |
| 123 | 126 |
| 124 private: | 127 private: |
| 125 const void* source_; | 128 const void* source_; |
| 126 Activity::Type activity_; | 129 Activity::Type activity_; |
| 127 ActivityData data_; | 130 ActivityData data_; |
| 128 | 131 |
| 129 bool ready_ = false; | 132 std::atomic<bool> ready_; |
| 130 bool exit_ = false; | 133 std::atomic<bool> exit_; |
| 131 Lock lock_; | 134 Lock lock_; |
| 132 ConditionVariable exit_condition_; | 135 ConditionVariable exit_condition_; |
| 133 | 136 |
| 134 DISALLOW_COPY_AND_ASSIGN(SimpleActivityThread); | 137 DISALLOW_COPY_AND_ASSIGN(SimpleActivityThread); |
| 135 }; | 138 }; |
| 136 | 139 |
| 137 TEST_F(ActivityAnalyzerTest, GlobalAnalyzerConstruction) { | 140 TEST_F(ActivityAnalyzerTest, GlobalAnalyzerConstruction) { |
| 138 GlobalActivityTracker::CreateWithLocalMemory(kMemorySize, 0, "", 3); | 141 GlobalActivityTracker::CreateWithLocalMemory(kMemorySize, 0, "", 3); |
| 139 | 142 |
| 140 PersistentMemoryAllocator* allocator = | 143 PersistentMemoryAllocator* allocator = |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 170 ThreadActivityAnalyzer* ta2 = analyzer.GetFirstAnalyzer(); | 173 ThreadActivityAnalyzer* ta2 = analyzer.GetFirstAnalyzer(); |
| 171 ASSERT_TRUE(ta2); | 174 ASSERT_TRUE(ta2); |
| 172 EXPECT_FALSE(analyzer.GetNextAnalyzer()); | 175 EXPECT_FALSE(analyzer.GetNextAnalyzer()); |
| 173 ThreadActivityAnalyzer::ThreadKey tk2 = ta2->GetThreadKey(); | 176 ThreadActivityAnalyzer::ThreadKey tk2 = ta2->GetThreadKey(); |
| 174 EXPECT_EQ(ta2, analyzer.GetAnalyzerForThread(tk2)); | 177 EXPECT_EQ(ta2, analyzer.GetAnalyzerForThread(tk2)); |
| 175 EXPECT_EQ(tk1, tk2); | 178 EXPECT_EQ(tk1, tk2); |
| 176 } | 179 } |
| 177 | 180 |
| 178 } // namespace debug | 181 } // namespace debug |
| 179 } // namespace base | 182 } // namespace base |
| OLD | NEW |