| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2013 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 "chrome/browser/metrics/time_ticks_experiment_win.h" | |
| 6 | |
| 7 #if defined(OS_WIN) | |
| 8 | |
| 9 #include <windows.h> | |
| 10 #include <algorithm> | |
| 11 | |
| 12 #include "base/cpu.h" | |
| 13 #include "base/metrics/histogram_macros.h" | |
| 14 #include "base/win/windows_version.h" | |
| 15 #include "build/build_config.h" | |
| 16 | |
| 17 namespace chrome { | |
| 18 | |
| 19 namespace { | |
| 20 | |
| 21 const int kNumIterations = 1000; | |
| 22 | |
| 23 } // anonymous namespace | |
| 24 | |
| 25 void CollectTimeTicksStats() { | |
| 26 // This bit is supposed to indicate that rdtsc is safe across cores. If so, we | |
| 27 // can use QPC as long as it uses rdtsc. | |
| 28 // TODO(simonjam): We should look for other signals that QPC might be safe and | |
| 29 // test them out here. | |
| 30 base::CPU cpu; | |
| 31 UMA_HISTOGRAM_BOOLEAN("WinTimeTicks.NonStopTsc", | |
| 32 cpu.has_non_stop_time_stamp_counter()); | |
| 33 if (!cpu.has_non_stop_time_stamp_counter()) { | |
| 34 return; | |
| 35 } | |
| 36 | |
| 37 DWORD_PTR default_mask; | |
| 38 DWORD_PTR system_mask; | |
| 39 if (!GetProcessAffinityMask(GetCurrentProcess(), | |
| 40 &default_mask, &system_mask)) { | |
| 41 return; | |
| 42 } | |
| 43 if (!default_mask) { | |
| 44 return; | |
| 45 } | |
| 46 | |
| 47 DWORD_PTR current_mask = 1; | |
| 48 bool failed_to_change_cores = false; | |
| 49 | |
| 50 base::win::OSInfo* info = base::win::OSInfo::GetInstance(); | |
| 51 UMA_HISTOGRAM_ENUMERATION("WinTimeTicks.VersionTotal", info->version(), | |
| 52 base::win::VERSION_WIN_LAST); | |
| 53 | |
| 54 LARGE_INTEGER qpc_frequency; | |
| 55 QueryPerformanceFrequency(&qpc_frequency); | |
| 56 | |
| 57 int min_delta = 1e9; | |
| 58 LARGE_INTEGER qpc_last; | |
| 59 QueryPerformanceCounter(&qpc_last); | |
| 60 for (int i = 0; i < kNumIterations; ++i) { | |
| 61 LARGE_INTEGER qpc_now; | |
| 62 QueryPerformanceCounter(&qpc_now); | |
| 63 int delta = static_cast<int>(qpc_now.QuadPart - qpc_last.QuadPart); | |
| 64 if (delta != 0) { | |
| 65 min_delta = std::min(min_delta, delta); | |
| 66 } | |
| 67 qpc_last = qpc_now; | |
| 68 | |
| 69 // Change cores every 10 iterations. | |
| 70 if (i % 10 == 0) { | |
| 71 DWORD_PTR old_mask = current_mask; | |
| 72 current_mask <<= 1; | |
| 73 while ((current_mask & default_mask) == 0) { | |
| 74 current_mask <<= 1; | |
| 75 if (!current_mask) { | |
| 76 current_mask = 1; | |
| 77 } | |
| 78 if (current_mask == old_mask) { | |
| 79 break; | |
| 80 } | |
| 81 } | |
| 82 if (!SetThreadAffinityMask(GetCurrentThread(), current_mask)) { | |
| 83 failed_to_change_cores = true; | |
| 84 break; | |
| 85 } | |
| 86 } | |
| 87 } | |
| 88 | |
| 89 SetThreadAffinityMask(GetCurrentThread(), default_mask); | |
| 90 if (failed_to_change_cores) { | |
| 91 UMA_HISTOGRAM_ENUMERATION("WinTimeTicks.FailedToChangeCores", | |
| 92 info->version(), base::win::VERSION_WIN_LAST); | |
| 93 return; | |
| 94 } | |
| 95 | |
| 96 if (min_delta < 0) { | |
| 97 UMA_HISTOGRAM_ENUMERATION("WinTimeTicks.TickedBackwards", info->version(), | |
| 98 base::win::VERSION_WIN_LAST); | |
| 99 return; | |
| 100 } | |
| 101 | |
| 102 int min_delta_ns = static_cast<int>( | |
| 103 min_delta * (1e9 / qpc_frequency.QuadPart)); | |
| 104 UMA_HISTOGRAM_CUSTOM_COUNTS("WinTimeTicks.MinResolutionNanoseconds", | |
| 105 min_delta_ns, 1, 1000000, 50); | |
| 106 | |
| 107 bool success = min_delta_ns <= 10000; | |
| 108 if (success) { | |
| 109 UMA_HISTOGRAM_ENUMERATION("WinTimeTicks.VersionSuccessful", | |
| 110 info->version(), base::win::VERSION_WIN_LAST); | |
| 111 } | |
| 112 } | |
| 113 | |
| 114 } // namespace chrome | |
| 115 | |
| 116 #endif // defined(OS_WIN) | |
| OLD | NEW |