Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/debug/activity_analyzer.h" | |
| 6 | |
| 7 #include <memory> | |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/files/file.h" | |
| 11 #include "base/files/file_util.h" | |
| 12 #include "base/files/memory_mapped_file.h" | |
| 13 #include "base/files/scoped_temp_dir.h" | |
| 14 #include "base/memory/ptr_util.h" | |
| 15 #include "base/pending_task.h" | |
| 16 #include "base/process/process.h" | |
| 17 #include "base/synchronization/condition_variable.h" | |
| 18 #include "base/synchronization/lock.h" | |
| 19 #include "base/synchronization/spin_wait.h" | |
| 20 #include "base/threading/platform_thread.h" | |
| 21 #include "base/threading/simple_thread.h" | |
| 22 #include "base/time/time.h" | |
| 23 #include "testing/gtest/include/gtest/gtest.h" | |
| 24 | |
| 25 namespace base { | |
| 26 namespace debug { | |
| 27 | |
| 28 namespace { | |
| 29 | |
| 30 class TestActivityTracker : public ThreadActivityTracker { | |
| 31 public: | |
| 32 TestActivityTracker(std::unique_ptr<char[]> memory, size_t mem_size) | |
| 33 : ThreadActivityTracker(memset(memory.get(), 0, mem_size), mem_size), | |
| 34 mem_segment_(std::move(memory)) {} | |
| 35 | |
| 36 ~TestActivityTracker() override {} | |
| 37 | |
| 38 private: | |
| 39 std::unique_ptr<char[]> mem_segment_; | |
| 40 }; | |
| 41 | |
| 42 } // namespace | |
| 43 | |
| 44 | |
| 45 class ActivityAnalyzerTest : public testing::Test { | |
| 46 public: | |
| 47 const int kMemorySize = 1 << 10; // 1KiB | |
|
Sigurður Ásgeirsson
2016/06/14 15:28:13
static?
bcwhite
2016/06/14 19:48:45
In general it doesn't matter since the compiler wi
| |
| 48 const int kStackSize = 256; | |
| 49 | |
| 50 using Activity = ThreadActivityAnalyzer::Activity; | |
| 51 | |
| 52 ActivityAnalyzerTest() {} | |
| 53 | |
| 54 ~ActivityAnalyzerTest() override { | |
| 55 GlobalActivityTracker* global_tracker = GlobalActivityTracker::Get(); | |
| 56 if (global_tracker) { | |
| 57 global_tracker->ReleaseTrackerForCurrentThreadForTesting(); | |
| 58 delete global_tracker; | |
| 59 } | |
| 60 } | |
| 61 | |
| 62 std::unique_ptr<ThreadActivityTracker> CreateActivityTracker() { | |
| 63 std::unique_ptr<char[]> memory(new char[kStackSize]); | |
| 64 return WrapUnique(new TestActivityTracker(std::move(memory), kStackSize)); | |
| 65 } | |
| 66 | |
| 67 static void DoNothing() {} | |
| 68 }; | |
| 69 | |
| 70 TEST_F(ActivityAnalyzerTest, ThreadAnalyzerConstruction) { | |
| 71 std::unique_ptr<ThreadActivityTracker> tracker = CreateActivityTracker(); | |
| 72 { | |
| 73 ThreadActivityAnalyzer analyzer(tracker.get()); | |
| 74 EXPECT_TRUE(analyzer.IsValid()); | |
| 75 EXPECT_EQ(PlatformThread::GetName(), analyzer.GetThreadName()); | |
| 76 } | |
| 77 | |
| 78 // TODO(bcwhite): More tests. | |
|
Sigurður Ásgeirsson
2016/06/14 15:28:13
Are there already cross-process tests?
bcwhite
2016/06/14 19:48:45
No but in practice it doesn't matter since there a
| |
| 79 } | |
| 80 | |
| 81 TEST_F(ActivityAnalyzerTest, GlobalAnalyzerConstruction) { | |
| 82 GlobalActivityTracker::CreateWithLocalMemory(kMemorySize, 0, "", 3); | |
| 83 // ThreadActivityTracker* tracker = | |
| 84 // GlobalActivityTracker::Get()->GetOrCreateTrackerForCurrentThread(); | |
| 85 | |
| 86 PersistentMemoryAllocator* allocator = | |
| 87 GlobalActivityTracker::Get()->allocator(); | |
| 88 GlobalActivityAnalyzer analyzer(WrapUnique( | |
| 89 new PersistentMemoryAllocator(const_cast<void*>(allocator->data()), | |
| 90 allocator->size(), 0, 0, "", true))); | |
| 91 | |
| 92 // The only thread is the test thread. | |
| 93 ThreadActivityAnalyzer* ta1 = analyzer.GetFirstAnalyzer(); | |
|
Sigurður Ásgeirsson
2016/06/14 15:28:13
I'm not sure this is a safe assumption, given the
bcwhite
2016/06/14 19:48:45
I'll look into it.
| |
| 94 ASSERT_TRUE(ta1); | |
| 95 EXPECT_FALSE(analyzer.GetNextAnalyzer()); | |
| 96 EXPECT_EQ(ta1, analyzer.GetAnalyzerForThread(ta1->GetThreadKey())); | |
| 97 } | |
| 98 | |
| 99 } // namespace debug | |
| 100 } // namespace base | |
| OLD | NEW |