| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <windows.h> | 5 #include <windows.h> |
| 6 #include <mmsystem.h> | 6 #include <mmsystem.h> |
| 7 #include <process.h> | 7 #include <process.h> |
| 8 #include <stdint.h> |
| 8 | 9 |
| 9 #include <cmath> | 10 #include <cmath> |
| 10 #include <limits> | 11 #include <limits> |
| 11 #include <vector> | 12 #include <vector> |
| 12 | 13 |
| 13 #include "base/threading/platform_thread.h" | 14 #include "base/threading/platform_thread.h" |
| 14 #include "base/time/time.h" | 15 #include "base/time/time.h" |
| 15 #include "base/win/registry.h" | 16 #include "base/win/registry.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 17 | 18 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 37 static volatile LONG ticker_; | 38 static volatile LONG ticker_; |
| 38 static TickFunctionType old_tick_function_; | 39 static TickFunctionType old_tick_function_; |
| 39 }; | 40 }; |
| 40 | 41 |
| 41 volatile LONG MockTimeTicks::ticker_; | 42 volatile LONG MockTimeTicks::ticker_; |
| 42 MockTimeTicks::TickFunctionType MockTimeTicks::old_tick_function_; | 43 MockTimeTicks::TickFunctionType MockTimeTicks::old_tick_function_; |
| 43 | 44 |
| 44 HANDLE g_rollover_test_start; | 45 HANDLE g_rollover_test_start; |
| 45 | 46 |
| 46 unsigned __stdcall RolloverTestThreadMain(void* param) { | 47 unsigned __stdcall RolloverTestThreadMain(void* param) { |
| 47 int64 counter = reinterpret_cast<int64>(param); | 48 int64_t counter = reinterpret_cast<int64_t>(param); |
| 48 DWORD rv = WaitForSingleObject(g_rollover_test_start, INFINITE); | 49 DWORD rv = WaitForSingleObject(g_rollover_test_start, INFINITE); |
| 49 EXPECT_EQ(rv, WAIT_OBJECT_0); | 50 EXPECT_EQ(rv, WAIT_OBJECT_0); |
| 50 | 51 |
| 51 TimeTicks last = TimeTicks::Now(); | 52 TimeTicks last = TimeTicks::Now(); |
| 52 for (int index = 0; index < counter; index++) { | 53 for (int index = 0; index < counter; index++) { |
| 53 TimeTicks now = TimeTicks::Now(); | 54 TimeTicks now = TimeTicks::Now(); |
| 54 int64 milliseconds = (now - last).InMilliseconds(); | 55 int64_t milliseconds = (now - last).InMilliseconds(); |
| 55 // This is a tight loop; we could have looped faster than our | 56 // This is a tight loop; we could have looped faster than our |
| 56 // measurements, so the time might be 0 millis. | 57 // measurements, so the time might be 0 millis. |
| 57 EXPECT_GE(milliseconds, 0); | 58 EXPECT_GE(milliseconds, 0); |
| 58 EXPECT_LT(milliseconds, 250); | 59 EXPECT_LT(milliseconds, 250); |
| 59 last = now; | 60 last = now; |
| 60 } | 61 } |
| 61 return 0; | 62 return 0; |
| 62 } | 63 } |
| 63 | 64 |
| 64 } // namespace | 65 } // namespace |
| 65 | 66 |
| 66 // This test spawns many threads, and can occasionally fail due to resource | 67 // This test spawns many threads, and can occasionally fail due to resource |
| 67 // exhaustion in the presence of ASan. | 68 // exhaustion in the presence of ASan. |
| 68 #if defined(ADDRESS_SANITIZER) | 69 #if defined(ADDRESS_SANITIZER) |
| 69 #define MAYBE_WinRollover DISABLED_WinRollover | 70 #define MAYBE_WinRollover DISABLED_WinRollover |
| 70 #else | 71 #else |
| 71 #define MAYBE_WinRollover WinRollover | 72 #define MAYBE_WinRollover WinRollover |
| 72 #endif | 73 #endif |
| 73 TEST(TimeTicks, MAYBE_WinRollover) { | 74 TEST(TimeTicks, MAYBE_WinRollover) { |
| 74 // The internal counter rolls over at ~49days. We'll use a mock | 75 // The internal counter rolls over at ~49days. We'll use a mock |
| 75 // timer to test this case. | 76 // timer to test this case. |
| 76 // Basic test algorithm: | 77 // Basic test algorithm: |
| 77 // 1) Set clock to rollover - N | 78 // 1) Set clock to rollover - N |
| 78 // 2) Create N threads | 79 // 2) Create N threads |
| 79 // 3) Start the threads | 80 // 3) Start the threads |
| 80 // 4) Each thread loops through TimeTicks() N times | 81 // 4) Each thread loops through TimeTicks() N times |
| 81 // 5) Each thread verifies integrity of result. | 82 // 5) Each thread verifies integrity of result. |
| 82 | 83 |
| 83 const int kThreads = 8; | 84 const int kThreads = 8; |
| 84 // Use int64 so we can cast into a void* without a compiler warning. | 85 // Use int64_t so we can cast into a void* without a compiler warning. |
| 85 const int64 kChecks = 10; | 86 const int64_t kChecks = 10; |
| 86 | 87 |
| 87 // It takes a lot of iterations to reproduce the bug! | 88 // It takes a lot of iterations to reproduce the bug! |
| 88 // (See bug 1081395) | 89 // (See bug 1081395) |
| 89 for (int loop = 0; loop < 4096; loop++) { | 90 for (int loop = 0; loop < 4096; loop++) { |
| 90 // Setup | 91 // Setup |
| 91 MockTimeTicks::InstallTicker(); | 92 MockTimeTicks::InstallTicker(); |
| 92 g_rollover_test_start = CreateEvent(0, TRUE, FALSE, 0); | 93 g_rollover_test_start = CreateEvent(0, TRUE, FALSE, 0); |
| 93 HANDLE threads[kThreads]; | 94 HANDLE threads[kThreads]; |
| 94 | 95 |
| 95 for (int index = 0; index < kThreads; index++) { | 96 for (int index = 0; index < kThreads; index++) { |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 233 0.005 * processor_mhz_from_registry); | 234 0.005 * processor_mhz_from_registry); |
| 234 } | 235 } |
| 235 } | 236 } |
| 236 | 237 |
| 237 TEST(TimeTicks, FromQPCValue) { | 238 TEST(TimeTicks, FromQPCValue) { |
| 238 if (!TimeTicks::IsHighResolution()) | 239 if (!TimeTicks::IsHighResolution()) |
| 239 return; | 240 return; |
| 240 | 241 |
| 241 LARGE_INTEGER frequency; | 242 LARGE_INTEGER frequency; |
| 242 ASSERT_TRUE(QueryPerformanceFrequency(&frequency)); | 243 ASSERT_TRUE(QueryPerformanceFrequency(&frequency)); |
| 243 const int64 ticks_per_second = frequency.QuadPart; | 244 const int64_t ticks_per_second = frequency.QuadPart; |
| 244 ASSERT_GT(ticks_per_second, 0); | 245 ASSERT_GT(ticks_per_second, 0); |
| 245 | 246 |
| 246 // Generate the tick values to convert, advancing the tick count by varying | 247 // Generate the tick values to convert, advancing the tick count by varying |
| 247 // amounts. These values will ensure that both the fast and overflow-safe | 248 // amounts. These values will ensure that both the fast and overflow-safe |
| 248 // conversion logic in FromQPCValue() is tested, and across the entire range | 249 // conversion logic in FromQPCValue() is tested, and across the entire range |
| 249 // of possible QPC tick values. | 250 // of possible QPC tick values. |
| 250 std::vector<int64> test_cases; | 251 std::vector<int64_t> test_cases; |
| 251 test_cases.push_back(0); | 252 test_cases.push_back(0); |
| 252 const int kNumAdvancements = 100; | 253 const int kNumAdvancements = 100; |
| 253 int64 ticks = 0; | 254 int64_t ticks = 0; |
| 254 int64 ticks_increment = 10; | 255 int64_t ticks_increment = 10; |
| 255 for (int i = 0; i < kNumAdvancements; ++i) { | 256 for (int i = 0; i < kNumAdvancements; ++i) { |
| 256 test_cases.push_back(ticks); | 257 test_cases.push_back(ticks); |
| 257 ticks += ticks_increment; | 258 ticks += ticks_increment; |
| 258 ticks_increment = ticks_increment * 6 / 5; | 259 ticks_increment = ticks_increment * 6 / 5; |
| 259 } | 260 } |
| 260 test_cases.push_back(Time::kQPCOverflowThreshold - 1); | 261 test_cases.push_back(Time::kQPCOverflowThreshold - 1); |
| 261 test_cases.push_back(Time::kQPCOverflowThreshold); | 262 test_cases.push_back(Time::kQPCOverflowThreshold); |
| 262 test_cases.push_back(Time::kQPCOverflowThreshold + 1); | 263 test_cases.push_back(Time::kQPCOverflowThreshold + 1); |
| 263 ticks = Time::kQPCOverflowThreshold + 10; | 264 ticks = Time::kQPCOverflowThreshold + 10; |
| 264 ticks_increment = 10; | 265 ticks_increment = 10; |
| 265 for (int i = 0; i < kNumAdvancements; ++i) { | 266 for (int i = 0; i < kNumAdvancements; ++i) { |
| 266 test_cases.push_back(ticks); | 267 test_cases.push_back(ticks); |
| 267 ticks += ticks_increment; | 268 ticks += ticks_increment; |
| 268 ticks_increment = ticks_increment * 6 / 5; | 269 ticks_increment = ticks_increment * 6 / 5; |
| 269 } | 270 } |
| 270 test_cases.push_back(std::numeric_limits<int64>::max()); | 271 test_cases.push_back(std::numeric_limits<int64_t>::max()); |
| 271 | 272 |
| 272 // Test that the conversions using FromQPCValue() match those computed here | 273 // Test that the conversions using FromQPCValue() match those computed here |
| 273 // using simple floating-point arithmetic. The floating-point math provides | 274 // using simple floating-point arithmetic. The floating-point math provides |
| 274 // enough precision to confirm the implementation is correct to the | 275 // enough precision to confirm the implementation is correct to the |
| 275 // microsecond for all |test_cases| (though it would be insufficient to | 276 // microsecond for all |test_cases| (though it would be insufficient to |
| 276 // confirm many "very large" tick values which are not being tested here). | 277 // confirm many "very large" tick values which are not being tested here). |
| 277 for (int64 ticks : test_cases) { | 278 for (int64_t ticks : test_cases) { |
| 278 const double expected_microseconds_since_origin = | 279 const double expected_microseconds_since_origin = |
| 279 (static_cast<double>(ticks) * Time::kMicrosecondsPerSecond) / | 280 (static_cast<double>(ticks) * Time::kMicrosecondsPerSecond) / |
| 280 ticks_per_second; | 281 ticks_per_second; |
| 281 const TimeTicks converted_value = TimeTicks::FromQPCValue(ticks); | 282 const TimeTicks converted_value = TimeTicks::FromQPCValue(ticks); |
| 282 const double converted_microseconds_since_origin = | 283 const double converted_microseconds_since_origin = |
| 283 static_cast<double>((converted_value - TimeTicks()).InMicroseconds()); | 284 static_cast<double>((converted_value - TimeTicks()).InMicroseconds()); |
| 284 EXPECT_NEAR(expected_microseconds_since_origin, | 285 EXPECT_NEAR(expected_microseconds_since_origin, |
| 285 converted_microseconds_since_origin, | 286 converted_microseconds_since_origin, |
| 286 1.0) | 287 1.0) |
| 287 << "ticks=" << ticks << ", to be converted via logic path: " | 288 << "ticks=" << ticks << ", to be converted via logic path: " |
| 288 << (ticks < Time::kQPCOverflowThreshold ? "FAST" : "SAFE"); | 289 << (ticks < Time::kQPCOverflowThreshold ? "FAST" : "SAFE"); |
| 289 } | 290 } |
| 290 } | 291 } |
| 291 | 292 |
| 292 } // namespace base | 293 } // namespace base |
| OLD | NEW |