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 "base/cpu.h" | |
10 #include "base/metrics/histogram.h" | |
11 #include "base/win/windows_version.h" | |
12 | |
13 #include <windows.h> | |
14 | |
15 namespace chrome_browser_metrics { | |
16 | |
17 namespace { | |
18 | |
19 const int kNumIterations = 1000; | |
20 | |
21 } // anonymous namespace | |
22 | |
23 void CollectTimeTicksStats() { | |
24 // This bit is supposed to indicate that rdtsc is safe across cores. If so, we | |
25 // can use QPC as long as it uses rdtsc. | |
26 // TODO(simonjam): We should look for other signals that QPC might be safe and | |
27 // test them out here. | |
28 base::CPU cpu; | |
29 UMA_HISTOGRAM_BOOLEAN("WinTimeTicks.NonStopTsc", | |
30 cpu.has_non_stop_time_stamp_counter()); | |
31 if (!cpu.has_non_stop_time_stamp_counter()) { | |
32 return; | |
33 } | |
34 | |
35 SYSTEM_INFO sys_info; | |
36 GetSystemInfo(&sys_info); | |
37 DWORD num_cores = sys_info.dwNumberOfProcessors; | |
38 DWORD current_core = 0; | |
39 | |
40 typedef DWORD (WINAPI *GetCurrentProcessorFunction)(); | |
41 GetCurrentProcessorFunction get_current_processor_func = NULL; | |
42 | |
43 if (num_cores > 1) { | |
44 // Windows XP doesn't have a function to tell which core we're currently | |
45 // running on. Here, we determine if the function is available and use it if | |
46 // so. If the function is not available, we bail. | |
47 HMODULE module = GetModuleHandle(TEXT("kernel32.dll")); | |
48 if (!module) { | |
49 return; | |
50 } | |
51 get_current_processor_func = | |
52 reinterpret_cast<GetCurrentProcessorFunction>( | |
53 GetProcAddress(module, "GetCurrentProcessorNumber")); | |
54 if (!get_current_processor_func) { | |
Sigurður Ásgeirsson
2013/04/17 13:36:16
since this function is unavailable on Windows XP,
| |
55 return; | |
56 } | |
57 current_core = get_current_processor_func(); | |
58 } | |
59 | |
60 base::win::OSInfo* info = base::win::OSInfo::GetInstance(); | |
61 UMA_HISTOGRAM_ENUMERATION("WinTimeTicks.VersionTotal", info->version(), | |
62 base::win::VERSION_WIN_LAST); | |
63 | |
64 DWORD starting_core = get_current_processor_func(); | |
65 bool did_change_cores = false; | |
66 | |
67 LARGE_INTEGER qpc_frequency; | |
68 QueryPerformanceFrequency(&qpc_frequency); | |
69 | |
70 int min_delta = 1e9; | |
71 LARGE_INTEGER qpc_last; | |
72 QueryPerformanceCounter(&qpc_last); | |
73 for (int i = 0; i < kNumIterations; ++i) { | |
74 LARGE_INTEGER qpc_now; | |
75 QueryPerformanceCounter(&qpc_now); | |
76 int delta = static_cast<int>(qpc_now.QuadPart - qpc_last.QuadPart); | |
77 if (delta != 0) { | |
78 min_delta = std::min(min_delta, delta); | |
79 } | |
80 qpc_last = qpc_now; | |
81 | |
82 if (num_cores > 1 && (i % 100) == 0) { | |
83 ++current_core; | |
84 if (current_core > num_cores) { | |
85 current_core = 0; | |
86 } | |
87 SetThreadAffinityMask(GetCurrentThread(), 1 << current_core); | |
Sigurður Ásgeirsson
2013/04/17 13:36:16
This function can fail and a failure in this funct
| |
88 if (!did_change_cores && | |
Sigurður Ásgeirsson
2013/04/17 13:36:16
My earlier comment on this code was to the effect
| |
89 get_current_processor_func() != starting_core) { | |
90 did_change_cores = true; | |
91 } | |
92 } | |
93 } | |
94 | |
95 if (num_cores > 1) { | |
96 if (did_change_cores) { | |
97 UMA_HISTOGRAM_ENUMERATION("WinTimeTicks.ChangedCores", info->version(), | |
98 base::win::VERSION_WIN_LAST); | |
99 } else { | |
100 UMA_HISTOGRAM_ENUMERATION("WinTimeTicks.FailedToChangeCores", | |
101 info->version(), base::win::VERSION_WIN_LAST); | |
102 } | |
103 } | |
104 | |
105 if (min_delta >= 0) { | |
106 UMA_HISTOGRAM_ENUMERATION("WinTimeTicks.NonDecreasing", info->version(), | |
107 base::win::VERSION_WIN_LAST); | |
108 } else { | |
109 return; | |
110 } | |
111 | |
112 int min_delta_ns = static_cast<int>( | |
113 min_delta * (1e9 / qpc_frequency.QuadPart)); | |
114 UMA_HISTOGRAM_CUSTOM_COUNTS("WinTimeTicks.MinResolutionNanoseconds", | |
115 min_delta_ns, 1, 1000000, 50); | |
116 | |
117 bool success = min_delta_ns <= 10000; | |
118 if (success) { | |
119 UMA_HISTOGRAM_ENUMERATION("WinTimeTicks.VersionSuccessful", | |
120 info->version(), base::win::VERSION_WIN_LAST); | |
121 } | |
122 } | |
123 | |
124 } // namespace chrome_browser_net | |
125 | |
126 #endif // defined(OS_WIN) | |
OLD | NEW |