OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "components/startup_metric_utils/startup_metric_utils.h" | 5 #include "components/startup_metric_utils/startup_metric_utils.h" |
6 | 6 |
7 #include "base/containers/hash_tables.h" | 7 #include "base/containers/hash_tables.h" |
8 #include "base/environment.h" | 8 #include "base/environment.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
11 #include "base/metrics/histogram_base.h" | 11 #include "base/metrics/histogram_base.h" |
12 #include "base/metrics/statistics_recorder.h" | 12 #include "base/metrics/statistics_recorder.h" |
13 #include "base/process/process_info.h" | 13 #include "base/process/process_info.h" |
14 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
15 #include "base/synchronization/lock.h" | 15 #include "base/synchronization/lock.h" |
16 #include "base/sys_info.h" | 16 #include "base/sys_info.h" |
17 #include "base/time/time.h" | 17 #include "base/time/time.h" |
18 | 18 |
19 #if defined(OS_WIN) | |
20 #include <winternl.h> | |
21 #endif | |
22 | |
19 namespace { | 23 namespace { |
20 | 24 |
21 // Mark as volatile to defensively make sure usage is thread-safe. | 25 // Mark as volatile to defensively make sure usage is thread-safe. |
22 // Note that at the time of this writing, access is only on the UI thread. | 26 // Note that at the time of this writing, access is only on the UI thread. |
23 volatile bool g_non_browser_ui_displayed = false; | 27 volatile bool g_non_browser_ui_displayed = false; |
24 | 28 |
25 base::Time* MainEntryPointTimeInternal() { | 29 base::Time* MainEntryPointTimeInternal() { |
26 static base::Time main_start_time = base::Time::Now(); | 30 static base::Time main_start_time = base::Time::Now(); |
27 return &main_start_time; | 31 return &main_start_time; |
28 } | 32 } |
29 | 33 |
30 typedef base::hash_map<std::string,base::TimeDelta> SubsystemStartupTimeHash; | 34 typedef base::hash_map<std::string,base::TimeDelta> SubsystemStartupTimeHash; |
31 | 35 |
32 SubsystemStartupTimeHash* GetSubsystemStartupTimeHash() { | 36 SubsystemStartupTimeHash* GetSubsystemStartupTimeHash() { |
33 static SubsystemStartupTimeHash* slow_startup_time_hash = | 37 static SubsystemStartupTimeHash* slow_startup_time_hash = |
34 new SubsystemStartupTimeHash; | 38 new SubsystemStartupTimeHash; |
35 return slow_startup_time_hash; | 39 return slow_startup_time_hash; |
36 } | 40 } |
37 | 41 |
38 base::Lock* GetSubsystemStartupTimeHashLock() { | 42 base::Lock* GetSubsystemStartupTimeHashLock() { |
39 static base::Lock* slow_startup_time_hash_lock = new base::Lock; | 43 static base::Lock* slow_startup_time_hash_lock = new base::Lock; |
40 return slow_startup_time_hash_lock; | 44 return slow_startup_time_hash_lock; |
41 } | 45 } |
42 | 46 |
47 #if defined(OS_WIN) | |
48 | |
49 // The struct used to return system process information via the NT internal | |
50 // QuerySystemInformation call. This is partially documented at | |
51 // http://goo.gl/Ja9MrH and fully documented at http://goo.gl/QJ70rn | |
52 // This structure is laid out in the same format on both 32-bit and 64-bit | |
53 // systems, but has a different size due to the various pointer sized fields. | |
54 struct SYSTEM_PROCESS_INFORMATION_EX { | |
55 ULONG NextEntryOffset; | |
56 ULONG NumberOfThreads; | |
57 LARGE_INTEGER WorkingSetPrivateSize; | |
58 ULONG HardFaultCount; | |
59 BYTE Reserved1[36]; | |
60 PVOID Reserved2[3]; | |
61 // This is labeled a handle so that it expands to the correct size for 32-bit | |
62 // and 64-bit operating systems. However, under the hood it's a 32-bit DWORD | |
63 // containing the process ID. | |
64 HANDLE UniqueProcessId; | |
65 PVOID Reserved3; | |
66 ULONG HandleCount; | |
67 BYTE Reserved4[4]; | |
68 PVOID Reserved5[11]; | |
69 SIZE_T PeakPagefileUsage; | |
70 SIZE_T PrivatePageCount; | |
71 LARGE_INTEGER Reserved6[6]; | |
72 // Array of SYSTEM_THREAD_INFORMATION structs follows. | |
73 }; | |
74 | |
75 // The signature of the NtQuerySystemInformation function. | |
76 typedef NTSTATUS (WINAPI *NtQuerySystemInformationPtr)( | |
77 SYSTEM_INFORMATION_CLASS, PVOID, ULONG, PULONG); | |
78 | |
79 // Gets the hard fault count of the current process, returning it via | |
80 // |hard_fault_count|. Returns true on success, false otherwise. Also returns | |
81 // whether or not the system call was even possible for the current OS version | |
82 // via |has_os_support|. | |
83 bool GetHardFaultCountForCurrentProcess(uint32* hard_fault_count, | |
84 bool* has_os_support) { | |
85 DCHECK(hard_fault_count); | |
86 DCHECK(has_os_support); | |
87 | |
88 *has_os_support = false; | |
89 OSVERSIONINFOEX version_info = {}; | |
90 version_info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX); | |
91 if (!::GetVersionEx(reinterpret_cast<LPOSVERSIONINFOW>(&version_info))) | |
92 return false; | |
93 | |
94 // The hard fault counter is only available as of Windows 7 (version 6.1). | |
95 if (version_info.dwMajorVersion < 6) | |
96 return false; | |
97 if (version_info.dwMajorVersion == 6 && version_info.dwMinorVersion < 1) | |
98 return false; | |
99 | |
100 // At this point the OS supports the required system call. | |
101 *has_os_support = true; | |
102 | |
103 // Get the function pointer. | |
104 NtQuerySystemInformationPtr query_sys_info = | |
105 reinterpret_cast<NtQuerySystemInformationPtr>( | |
106 ::GetProcAddress(GetModuleHandle(L"ntdll.dll"), | |
107 "NtQuerySystemInformation")); | |
108 if (query_sys_info == nullptr) | |
109 return false; | |
110 | |
111 // The output of this system call depends on the number of threads and | |
112 // processes on the entire system, and this can change between calls. Retry | |
113 // a small handful of times growing the buffer along the way. | |
114 std::vector<uint8> buffer(1024); | |
115 for (size_t tries = 0; tries < 3; ++tries) { | |
116 ULONG return_length = 0; | |
117 NTSTATUS status = query_sys_info( | |
118 SystemProcessInformation, | |
119 buffer.data(), | |
120 buffer.size(), | |
121 &return_length); | |
122 // Insufficient space in the buffer. | |
123 if (return_length > buffer.size()) { | |
124 buffer.resize(return_length); | |
125 continue; | |
126 } | |
127 if (status == 0 && return_length <= buffer.size()) | |
128 break; | |
129 return false; | |
130 } | |
131 // The only way out to get here is if the system call was successful. | |
132 | |
133 // Look for the struct housing information for the current process. | |
134 DWORD proc_id = ::GetCurrentProcessId(); | |
135 size_t index = 0; | |
136 while (index < buffer.size()) { | |
137 SYSTEM_PROCESS_INFORMATION_EX* proc_info = | |
138 reinterpret_cast<SYSTEM_PROCESS_INFORMATION_EX*>(buffer.data() + index); | |
139 if (reinterpret_cast<DWORD>(proc_info->UniqueProcessId) == proc_id) { | |
140 *hard_fault_count = proc_info->HardFaultCount; | |
141 return true; | |
142 } | |
143 // The list ends with NextEntryOffset is zero. This also prevents busy | |
144 // looping if the data is in fact invalid. | |
145 if (proc_info->NextEntryOffset == 0) | |
146 return false; | |
147 index += proc_info->NextEntryOffset; | |
148 } | |
149 | |
150 return false; | |
151 } | |
152 | |
153 #endif // defined(OS_WIN) | |
154 | |
155 // On Windows, records the number of hard-faults that have occurred in the | |
156 // current chrome.exe process since it was started. This is a nop on other | |
157 // platforms. | |
158 // crbug.com/476923 | |
159 // TODO(chrisha): If this proves useful, use it to split startup stats in two. | |
160 void RecordHardFaultHistogram(bool is_first_run) { | |
161 #if defined(OS_WIN) | |
162 uint32 hard_fault_count = 0; | |
163 bool has_os_support = false; | |
164 bool success = GetHardFaultCountForCurrentProcess( | |
165 &hard_fault_count, &has_os_support); | |
166 | |
167 // Log whether or not the system call was successful, assuming the OS was | |
168 // detected to support it. | |
169 if (has_os_support) { | |
170 UMA_HISTOGRAM_BOOLEAN( | |
171 "Startup.BrowserMessageLoopStartHardFaultCount.Success", | |
172 success); | |
173 } | |
174 | |
175 // Don't log a histogram value if unable to get the hard fault count. | |
176 if (!success) | |
177 return; | |
178 | |
179 // Hard fault counts are expected to be in the thousands range, | |
180 // corresponding to faulting in ~10s of MBs of code ~10s of KBs at a time. | |
181 // (Observed to vary from 1000 to 10000 on various test machines and | |
erikchen
2015/04/14 23:01:52
If you're observing 10000 on a test machine, it's
chrisha
2015/04/15 13:04:30
I suppose it could go drastically higher, but it w
erikchen
2015/04/15 17:26:59
Sounds reasonable to me. We can always change the
| |
182 // platforms.) | |
183 if (is_first_run) { | |
184 UMA_HISTOGRAM_CUSTOM_COUNTS( | |
185 "Startup.BrowserMessageLoopStartHardFaultCount.FirstRun", | |
186 hard_fault_count, | |
187 0, 20000, 50); | |
188 } else { | |
189 UMA_HISTOGRAM_CUSTOM_COUNTS( | |
190 "Startup.BrowserMessageLoopStartHardFaultCount", | |
erikchen
2015/04/14 23:01:52
The description you give for this metric implies i
chrisha
2015/04/15 13:04:29
I was just being consistent with how Startup.Brows
| |
191 hard_fault_count, | |
192 0, 20000, 50); | |
193 } | |
194 #endif // defined(OS_WIN) | |
195 } | |
196 | |
43 // Record time of main entry so it can be read from Telemetry performance | 197 // Record time of main entry so it can be read from Telemetry performance |
44 // tests. | 198 // tests. |
45 // TODO(jeremy): Remove once crbug.com/317481 is fixed. | 199 // TODO(jeremy): Remove once crbug.com/317481 is fixed. |
46 void RecordMainEntryTimeHistogram() { | 200 void RecordMainEntryTimeHistogram() { |
47 const int kLowWordMask = 0xFFFFFFFF; | 201 const int kLowWordMask = 0xFFFFFFFF; |
48 const int kLower31BitsMask = 0x7FFFFFFF; | 202 const int kLower31BitsMask = 0x7FFFFFFF; |
49 base::TimeDelta browser_main_entry_time_absolute = | 203 base::TimeDelta browser_main_entry_time_absolute = |
50 *MainEntryPointTimeInternal() - base::Time::UnixEpoch(); | 204 *MainEntryPointTimeInternal() - base::Time::UnixEpoch(); |
51 | 205 |
52 uint64 browser_main_entry_time_raw_ms = | 206 uint64 browser_main_entry_time_raw_ms = |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
108 } | 262 } |
109 #endif // OS_ANDROID | 263 #endif // OS_ANDROID |
110 | 264 |
111 // Return the time recorded by RecordMainEntryPointTime(). | 265 // Return the time recorded by RecordMainEntryPointTime(). |
112 const base::Time MainEntryStartTime() { | 266 const base::Time MainEntryStartTime() { |
113 DCHECK(g_main_entry_time_was_recorded); | 267 DCHECK(g_main_entry_time_was_recorded); |
114 return *MainEntryPointTimeInternal(); | 268 return *MainEntryPointTimeInternal(); |
115 } | 269 } |
116 | 270 |
117 void OnBrowserStartupComplete(bool is_first_run) { | 271 void OnBrowserStartupComplete(bool is_first_run) { |
272 RecordHardFaultHistogram(is_first_run); | |
erikchen
2015/04/14 23:01:52
As a quick sanity check, can you confirm that this
chrisha
2015/04/15 13:04:30
This was benchmarked across various versions of th
| |
118 RecordMainEntryTimeHistogram(); | 273 RecordMainEntryTimeHistogram(); |
119 | 274 |
120 // Bail if uptime < 7 minutes, to filter out cases where Chrome may have been | 275 // Bail if uptime < 7 minutes, to filter out cases where Chrome may have been |
121 // autostarted and the machine is under io pressure. | 276 // autostarted and the machine is under io pressure. |
122 const int64 kSevenMinutesInMilliseconds = | 277 const int64 kSevenMinutesInMilliseconds = |
123 base::TimeDelta::FromMinutes(7).InMilliseconds(); | 278 base::TimeDelta::FromMinutes(7).InMilliseconds(); |
124 if (base::SysInfo::Uptime() < kSevenMinutesInMilliseconds) { | 279 if (base::SysInfo::Uptime() < kSevenMinutesInMilliseconds) { |
125 g_startup_stats_collection_finished = true; | 280 g_startup_stats_collection_finished = true; |
126 return; | 281 return; |
127 } | 282 } |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
242 SubsystemStartupTimeHash* hash = GetSubsystemStartupTimeHash(); | 397 SubsystemStartupTimeHash* hash = GetSubsystemStartupTimeHash(); |
243 // Only record the initial sample for a given histogram. | 398 // Only record the initial sample for a given histogram. |
244 if (hash->find(histogram_name_) != hash->end()) | 399 if (hash->find(histogram_name_) != hash->end()) |
245 return; | 400 return; |
246 | 401 |
247 (*hash)[histogram_name_] = | 402 (*hash)[histogram_name_] = |
248 base::TimeTicks::Now() - start_time_; | 403 base::TimeTicks::Now() - start_time_; |
249 } | 404 } |
250 | 405 |
251 } // namespace startup_metric_utils | 406 } // namespace startup_metric_utils |
OLD | NEW |