OLD | NEW |
---|---|
1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2014 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/browser_watcher/watcher_metrics_provider_win.h" | 5 #include "components/browser_watcher/watcher_metrics_provider_win.h" |
6 | 6 |
7 #include <limits> | |
7 #include <vector> | 8 #include <vector> |
8 | 9 |
9 #include "base/metrics/sparse_histogram.h" | 10 #include "base/metrics/sparse_histogram.h" |
10 #include "base/process/process_handle.h" | 11 #include "base/process/process_handle.h" |
11 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
12 #include "base/strings/string_piece.h" | 13 #include "base/strings/string_piece.h" |
14 #include "base/strings/utf_string_conversions.h" | |
13 #include "base/win/registry.h" | 15 #include "base/win/registry.h" |
14 | 16 |
15 namespace browser_watcher { | 17 namespace browser_watcher { |
16 | 18 |
17 namespace { | 19 namespace { |
18 | 20 |
19 void CompileAsserts() { | 21 void CompileAsserts() { |
20 // Process ID APIs on Windows talk in DWORDs, whereas for string formatting | 22 // Process ID APIs on Windows talk in DWORDs, whereas for string formatting |
21 // and parsing, this code uses int. In practice there are no process IDs with | 23 // and parsing, this code uses int. In practice there are no process IDs with |
22 // the high bit set on Windows, so there's no danger of overflow if this is | 24 // the high bit set on Windows, so there's no danger of overflow if this is |
(...skipping 30 matching lines...) Expand all Loading... | |
53 if (base::OpenProcessHandle(static_cast<base::ProcessId>(pid), &process)) { | 55 if (base::OpenProcessHandle(static_cast<base::ProcessId>(pid), &process)) { |
54 base::CloseProcessHandle(process); | 56 base::CloseProcessHandle(process); |
55 | 57 |
56 // The fact that it was possible to open the process says it's live. | 58 // The fact that it was possible to open the process says it's live. |
57 return false; | 59 return false; |
58 } | 60 } |
59 | 61 |
60 return true; | 62 return true; |
61 } | 63 } |
62 | 64 |
65 void RecordExitCodes(const base::string16& registry_path) { | |
66 base::win::RegKey regkey(HKEY_CURRENT_USER, | |
67 registry_path.c_str(), | |
68 KEY_QUERY_VALUE | KEY_SET_VALUE); | |
69 if (!regkey.Valid()) | |
70 return; | |
71 | |
72 size_t num = regkey.GetValueCount(); | |
73 if (num == 0) | |
74 return; | |
75 std::vector<base::string16> to_delete; | |
76 | |
77 // Record the exit codes in a sparse stability histogram, as the range of | |
78 // values used to report failures is large. | |
79 base::HistogramBase* exit_code_histogram = | |
80 base::SparseHistogram::FactoryGet( | |
81 WatcherMetricsProviderWin::kBrowserExitCodeHistogramName, | |
82 base::HistogramBase::kUmaStabilityHistogramFlag); | |
83 | |
84 for (size_t i = 0; i < num; ++i) { | |
85 base::string16 name; | |
86 if (regkey.GetValueNameAt(static_cast<int>(i), &name) == ERROR_SUCCESS) { | |
87 DWORD exit_code = 0; | |
88 if (regkey.ReadValueDW(name.c_str(), &exit_code) == ERROR_SUCCESS) { | |
89 // Do not report exit codes for processes that are still live, | |
90 // notably for our own process. | |
91 if (exit_code != STILL_ACTIVE || IsDeadProcess(name)) { | |
92 to_delete.push_back(name); | |
93 exit_code_histogram->Add(exit_code); | |
94 } | |
95 } | |
96 } | |
97 } | |
98 | |
99 // Delete the values reported above. | |
100 for (size_t i = 0; i < to_delete.size(); ++i) | |
101 regkey.DeleteValue(to_delete[i].c_str()); | |
102 } | |
103 | |
104 void ReadSingleExitFunnel( | |
105 base::win::RegKey* parent_key, const base::char16* name, | |
106 std::vector<std::pair<base::string16, int64>>* events_out) { | |
107 DCHECK(parent_key); | |
108 DCHECK(name); | |
109 DCHECK(events_out); | |
110 | |
111 base::win::RegKey regkey(parent_key->Handle(), name, KEY_READ | KEY_WRITE); | |
112 if (!regkey.Valid()) | |
113 return; | |
114 | |
115 // Exit early if no work to do. | |
116 size_t num = regkey.GetValueCount(); | |
117 if (num == 0) | |
118 return; | |
119 | |
120 // Enumerate the recorded events for this process for processing. | |
121 std::vector<std::pair<base::string16, int64>> events; | |
122 for (size_t i = 0; i < num; ++i) { | |
123 base::string16 event_name; | |
124 LONG res = regkey.GetValueNameAt(static_cast<int>(i), &event_name); | |
125 if (res == ERROR_SUCCESS) { | |
126 int64 event_time = 0; | |
127 res = regkey.ReadInt64(event_name.c_str(), &event_time); | |
128 if (res == ERROR_SUCCESS) | |
129 events.push_back(std::make_pair(event_name, event_time)); | |
130 } | |
131 } | |
132 | |
133 // Attempt to delete the values before reporting anything. | |
134 // Exit if this fails to make sure there is no double-reporting on e.g. | |
135 // permission problems or other corruption. | |
136 for (size_t i = 0; i < events.size(); ++i) { | |
erikwright (departed)
2014/12/12 16:58:29
Since you are recording the duration from the firs
erikwright (departed)
2014/12/12 17:17:19
This is optional, FYI. Up to you to decide if it c
Sigurður Ásgeirsson
2014/12/12 20:25:13
Thanks - I figure failure to delete a value will b
Sigurður Ásgeirsson
2014/12/12 20:25:13
Acknowledged.
| |
137 const base::string16& event_name = events[i].first; | |
138 LONG res = regkey.DeleteValue(event_name.c_str()); | |
139 if (res != ERROR_SUCCESS) { | |
140 LOG(ERROR) << "Failed to delete value " << event_name; | |
141 return; | |
142 } | |
143 } | |
144 | |
145 events_out->swap(events); | |
146 } | |
147 | |
148 void RecordSingleExitFunnel(base::win::RegKey* parent_key, | |
149 const base::char16* name) { | |
150 std::vector<std::pair<base::string16, int64>> events; | |
151 ReadSingleExitFunnel(parent_key, name, &events); | |
152 | |
153 // Find the earliest event time. | |
154 int64 min_time = std::numeric_limits<int64>::max(); | |
155 for (size_t i = 0; i < events.size(); ++i) | |
156 min_time = std::min(min_time, events[i].second); | |
157 | |
158 // Record the exit funnel event times in a sparse stability histogram. | |
159 for (size_t i = 0; i < events.size(); ++i) { | |
160 std::string histogram_name( | |
161 WatcherMetricsProviderWin::kExitFunnelHistogramPrefix); | |
162 histogram_name.append(base::WideToUTF8(events[i].first)); | |
163 base::TimeDelta event_time = | |
164 base::Time::FromInternalValue(events[i].second) - | |
165 base::Time::FromInternalValue(min_time); | |
166 base::HistogramBase* histogram = | |
167 base::SparseHistogram::FactoryGet( | |
168 histogram_name.c_str(), | |
169 base::HistogramBase::kUmaStabilityHistogramFlag); | |
170 | |
171 // Record the time rounded up to the nearest millisecond. | |
172 histogram->Add(event_time.InMillisecondsRoundedUp()); | |
173 } | |
174 } | |
175 | |
176 void RecordExitFunnels(const base::string16& registry_path) { | |
177 base::win::RegistryKeyIterator it(HKEY_CURRENT_USER, registry_path.c_str()); | |
178 if (!it.Valid()) | |
179 return; | |
180 | |
181 // Exit early if no work to do. | |
182 if (it.SubkeyCount() == 0) | |
183 return; | |
184 | |
185 // Open the key we use for deletion preemptively to prevent reporting | |
186 // multiple times on permission problems. | |
187 base::win::RegKey key(HKEY_CURRENT_USER, | |
188 registry_path.c_str(), | |
189 KEY_QUERY_VALUE); | |
190 if (!key.Valid()) { | |
191 LOG(ERROR) << "Failed to open " << registry_path << " for writing."; | |
192 return; | |
193 } | |
194 | |
195 std::vector<base::string16> to_delete; | |
196 for (; it.Valid(); ++it) { | |
197 RecordSingleExitFunnel(&key, it.Name()); | |
198 to_delete.push_back(it.Name()); | |
199 } | |
200 | |
201 for (size_t i = 0; i < to_delete.size(); ++i) { | |
202 LONG res = key.DeleteEmptyKey(to_delete[i].c_str()); | |
203 if (res != ERROR_SUCCESS) | |
204 LOG(ERROR) << "Failed to delete key " << to_delete[i]; | |
205 } | |
206 } | |
207 | |
63 } // namespace | 208 } // namespace |
64 | 209 |
65 const char WatcherMetricsProviderWin::kBrowserExitCodeHistogramName[] = | 210 const char WatcherMetricsProviderWin::kBrowserExitCodeHistogramName[] = |
66 "Stability.BrowserExitCodes"; | 211 "Stability.BrowserExitCodes"; |
212 const char WatcherMetricsProviderWin::kExitFunnelHistogramPrefix[] = | |
213 "Stability.ExitFunnel."; | |
67 | 214 |
68 WatcherMetricsProviderWin::WatcherMetricsProviderWin( | 215 WatcherMetricsProviderWin::WatcherMetricsProviderWin( |
69 const base::char16* registry_path) : registry_path_(registry_path) { | 216 const base::char16* registry_path) : registry_path_(registry_path) { |
70 } | 217 } |
71 | 218 |
72 WatcherMetricsProviderWin::~WatcherMetricsProviderWin() { | 219 WatcherMetricsProviderWin::~WatcherMetricsProviderWin() { |
73 } | 220 } |
74 | 221 |
75 void WatcherMetricsProviderWin::ProvideStabilityMetrics( | 222 void WatcherMetricsProviderWin::ProvideStabilityMetrics( |
76 metrics::SystemProfileProto* /* system_profile_proto */) { | 223 metrics::SystemProfileProto* /* system_profile_proto */) { |
77 // Note that if there are multiple instances of Chrome running in the same | 224 // Note that if there are multiple instances of Chrome running in the same |
78 // user account, there's a small race that will double-report the exit codes | 225 // user account, there's a small race that will double-report the exit codes |
79 // from both/multiple instances. This ought to be vanishingly rare and will | 226 // from both/multiple instances. This ought to be vanishingly rare and will |
80 // only manifest as low-level "random" noise. To work around this it would be | 227 // only manifest as low-level "random" noise. To work around this it would be |
81 // necessary to implement some form of global locking, which is not worth it | 228 // necessary to implement some form of global locking, which is not worth it |
82 // here. | 229 // here. |
83 base::win::RegKey regkey(HKEY_CURRENT_USER, | 230 RecordExitCodes(registry_path_); |
84 registry_path_.c_str(), | 231 RecordExitFunnels(registry_path_); |
85 KEY_QUERY_VALUE | KEY_SET_VALUE); | |
86 | |
87 size_t num = regkey.GetValueCount(); | |
88 if (num != 0) { | |
89 std::vector<base::string16> to_delete; | |
90 | |
91 // Record the exit codes in a sparse stability histogram, as the range of | |
92 // values used to report failures is large. | |
93 base::HistogramBase* exit_code_histogram = | |
94 base::SparseHistogram::FactoryGet(kBrowserExitCodeHistogramName, | |
95 base::HistogramBase::kUmaStabilityHistogramFlag); | |
96 | |
97 for (size_t i = 0; i < num; ++i) { | |
98 base::string16 name; | |
99 if (regkey.GetValueNameAt(static_cast<int>(i), &name) == ERROR_SUCCESS) { | |
100 DWORD exit_code = 0; | |
101 if (regkey.ReadValueDW(name.c_str(), &exit_code) == ERROR_SUCCESS) { | |
102 // Do not report exit codes for processes that are still live, | |
103 // notably for our own process. | |
104 if (exit_code != STILL_ACTIVE || IsDeadProcess(name)) { | |
105 to_delete.push_back(name); | |
106 exit_code_histogram->Add(exit_code); | |
107 } | |
108 } | |
109 } | |
110 } | |
111 | |
112 // Delete the values reported above. | |
113 for (size_t i = 0; i < to_delete.size(); ++i) | |
114 regkey.DeleteValue(to_delete[i].c_str()); | |
115 } | |
116 } | 232 } |
117 | 233 |
118 } // namespace browser_watcher | 234 } // namespace browser_watcher |
OLD | NEW |