OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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/win/sampling_profiler.h" |
| 6 |
| 7 #include "base/test/test_timeouts.h" |
| 8 #include "base/win/pe_image.h" |
| 9 #include "base/win/scoped_handle.h" |
| 10 #include "gtest/gtest.h" |
| 11 |
| 12 // The address of our image base. |
| 13 extern "C" IMAGE_DOS_HEADER __ImageBase; |
| 14 |
| 15 namespace base { |
| 16 namespace win { |
| 17 |
| 18 namespace { |
| 19 |
| 20 class SamplingProfilerTest : public testing::Test { |
| 21 public: |
| 22 SamplingProfilerTest() : code_start(NULL), code_size(0) { |
| 23 } |
| 24 |
| 25 virtual void SetUp() { |
| 26 process.Set(::OpenProcess(PROCESS_QUERY_INFORMATION, |
| 27 FALSE, |
| 28 ::GetCurrentProcessId())); |
| 29 ASSERT_TRUE(process.IsValid()); |
| 30 |
| 31 PEImage image(&__ImageBase); |
| 32 |
| 33 // Get the address of the .text section, which is the first section output |
| 34 // by the VS tools. |
| 35 ASSERT_TRUE(image.GetNumSections() > 0); |
| 36 const IMAGE_SECTION_HEADER* text_section = image.GetSectionHeader(0); |
| 37 ASSERT_EQ(0, strncmp(".text", |
| 38 reinterpret_cast<const char*>(text_section->Name), |
| 39 arraysize(text_section->Name))); |
| 40 ASSERT_NE(0U, text_section->Characteristics & IMAGE_SCN_MEM_EXECUTE); |
| 41 |
| 42 code_start = reinterpret_cast<uint8*>(&__ImageBase) + |
| 43 text_section->VirtualAddress; |
| 44 code_size = text_section->Misc.VirtualSize; |
| 45 } |
| 46 |
| 47 protected: |
| 48 ScopedHandle process; |
| 49 void* code_start; |
| 50 size_t code_size; |
| 51 }; |
| 52 |
| 53 } // namespace |
| 54 |
| 55 TEST_F(SamplingProfilerTest, Initialize) { |
| 56 SamplingProfiler profiler; |
| 57 |
| 58 ASSERT_TRUE(profiler.Initialize(process.Get(), code_start, code_size, 8)); |
| 59 } |
| 60 |
| 61 TEST_F(SamplingProfilerTest, StartStop) { |
| 62 SamplingProfiler profiler; |
| 63 |
| 64 // Initialize with a huge bucket size, aiming for a single bucket. |
| 65 ASSERT_TRUE( |
| 66 profiler.Initialize(process.Get(), code_start, code_size, 31)); |
| 67 |
| 68 ASSERT_EQ(1, profiler.buckets().size()); |
| 69 ASSERT_EQ(0, profiler.buckets()[0]); |
| 70 |
| 71 FILETIME ignore = {}; |
| 72 FILETIME user_time = {}; |
| 73 ASSERT_TRUE(::GetThreadTimes( |
| 74 ::GetCurrentThread(), &ignore, &ignore, &ignore, &user_time)); |
| 75 |
| 76 base::Time start = base::Time::FromFileTime(user_time); |
| 77 base::TimeDelta spin_time = |
| 78 base::TimeDelta::FromMilliseconds(TestTimeouts::tiny_timeout_ms()); |
| 79 |
| 80 // Start the profiler. |
| 81 ASSERT_TRUE(profiler.Start()); |
| 82 |
| 83 // Spin for spin_time CPU seconds to get some samples. |
| 84 base::Time now; |
| 85 do { |
| 86 ASSERT_TRUE(::GetThreadTimes( |
| 87 ::GetCurrentThread(), &ignore, &ignore, &ignore, &user_time)); |
| 88 |
| 89 now = base::Time::FromFileTime(user_time); |
| 90 } while(now - start < spin_time); |
| 91 |
| 92 // Stop the profiler. |
| 93 ASSERT_TRUE(profiler.Stop()); |
| 94 |
| 95 // Check that we got some samples. |
| 96 ASSERT_NE(0U, profiler.buckets()[0]); |
| 97 } |
| 98 |
| 99 TEST_F(SamplingProfilerTest, GetSetInterval) { |
| 100 // Get the original sampling interval. |
| 101 base::TimeDelta original_interval; |
| 102 ASSERT_TRUE(SamplingProfiler::GetSamplingInterval(&original_interval)); |
| 103 |
| 104 // Set a new interval that's double the old one. |
| 105 base::TimeDelta new_interval = original_interval * 2; |
| 106 ASSERT_TRUE(SamplingProfiler::SetSamplingInterval(new_interval)); |
| 107 |
| 108 // And verify that we can fetch the new one. |
| 109 base::TimeDelta set_interval; |
| 110 ASSERT_TRUE(SamplingProfiler::GetSamplingInterval(&set_interval)); |
| 111 EXPECT_EQ(new_interval.InMicroseconds(), set_interval.InMicroseconds()); |
| 112 |
| 113 // Restore the original interval. |
| 114 ASSERT_TRUE(SamplingProfiler::SetSamplingInterval(original_interval)); |
| 115 } |
| 116 |
| 117 } // namespace win |
| 118 } // namespace base |
OLD | NEW |