Add an OWNERS file for //chrome/browser/chromeos/login/easy_unlock
[chromium-blink-merge.git] / components / browser_watcher / watcher_metrics_provider_win.cc
blob0848046ef79cc7559a5f950824966eb59447bd07
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
3 // found in the LICENSE file.
5 #include "components/browser_watcher/watcher_metrics_provider_win.h"
7 #include <limits>
8 #include <vector>
10 #include "base/metrics/sparse_histogram.h"
11 #include "base/process/process_handle.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/string_piece.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "base/win/registry.h"
17 namespace browser_watcher {
19 namespace {
21 void CompileAsserts() {
22 // Process ID APIs on Windows talk in DWORDs, whereas for string formatting
23 // and parsing, this code uses int. In practice there are no process IDs with
24 // the high bit set on Windows, so there's no danger of overflow if this is
25 // done consistently.
26 static_assert(sizeof(DWORD) == sizeof(int),
27 "process ids are expected to be no larger than int");
30 // This function does soft matching on the PID recorded in the key only.
31 // Due to PID reuse, the possibility exists that the process that's now live
32 // with the given PID is not the same process the data was recorded for.
33 // This doesn't matter for the purpose, as eventually the data will be
34 // scavenged and reported.
35 bool IsDeadProcess(base::StringPiece16 key_or_value_name) {
36 // Truncate the input string to the first occurrence of '-', if one exists.
37 size_t num_end = key_or_value_name.find(L'-');
38 if (num_end != base::StringPiece16::npos)
39 key_or_value_name = key_or_value_name.substr(0, num_end);
41 // Convert to the numeric PID.
42 int pid = 0;
43 if (!base::StringToInt(key_or_value_name, &pid) || pid == 0)
44 return true;
46 // This is a very inexpensive check for the common case of our own PID.
47 if (static_cast<base::ProcessId>(pid) == base::GetCurrentProcId())
48 return false;
50 // The process is not our own - see whether a process with this PID exists.
51 // This is more expensive than the above check, but should also be very rare,
52 // as this only happens more than once for a given PID if a user is running
53 // multiple Chrome instances concurrently.
54 base::ProcessHandle process = base::kNullProcessHandle;
55 if (base::OpenProcessHandle(static_cast<base::ProcessId>(pid), &process)) {
56 base::CloseProcessHandle(process);
58 // The fact that it was possible to open the process says it's live.
59 return false;
62 return true;
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;
72 size_t num = regkey.GetValueCount();
73 if (num == 0)
74 return;
75 std::vector<base::string16> to_delete;
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);
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);
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());
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);
111 base::win::RegKey regkey(parent_key->Handle(), name, KEY_READ | KEY_WRITE);
112 if (!regkey.Valid())
113 return;
115 // Exit early if no work to do.
116 size_t num = regkey.GetValueCount();
117 if (num == 0)
118 return;
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));
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) {
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;
145 events_out->swap(events);
148 void MaybeRecordSingleExitFunnel(base::win::RegKey* parent_key,
149 const base::char16* name,
150 bool report) {
151 std::vector<std::pair<base::string16, int64>> events;
152 ReadSingleExitFunnel(parent_key, name, &events);
153 if (!report)
154 return;
156 // Find the earliest event time.
157 int64 min_time = std::numeric_limits<int64>::max();
158 for (size_t i = 0; i < events.size(); ++i)
159 min_time = std::min(min_time, events[i].second);
161 // Record the exit funnel event times in a sparse stability histogram.
162 for (size_t i = 0; i < events.size(); ++i) {
163 std::string histogram_name(
164 WatcherMetricsProviderWin::kExitFunnelHistogramPrefix);
165 histogram_name.append(base::WideToUTF8(events[i].first));
166 base::TimeDelta event_time =
167 base::Time::FromInternalValue(events[i].second) -
168 base::Time::FromInternalValue(min_time);
169 base::HistogramBase* histogram =
170 base::SparseHistogram::FactoryGet(
171 histogram_name.c_str(),
172 base::HistogramBase::kUmaStabilityHistogramFlag);
174 // Record the time rounded up to the nearest millisecond.
175 histogram->Add(event_time.InMillisecondsRoundedUp());
179 void MaybeRecordExitFunnels(const base::string16& registry_path, bool report) {
180 base::win::RegistryKeyIterator it(HKEY_CURRENT_USER, registry_path.c_str());
181 if (!it.Valid())
182 return;
184 // Exit early if no work to do.
185 if (it.SubkeyCount() == 0)
186 return;
188 // Open the key we use for deletion preemptively to prevent reporting
189 // multiple times on permission problems.
190 base::win::RegKey key(HKEY_CURRENT_USER,
191 registry_path.c_str(),
192 KEY_QUERY_VALUE);
193 if (!key.Valid()) {
194 LOG(ERROR) << "Failed to open " << registry_path << " for writing.";
195 return;
198 std::vector<base::string16> to_delete;
199 for (; it.Valid(); ++it) {
200 // Defer reporting on still-live processes.
201 if (IsDeadProcess(it.Name())) {
202 MaybeRecordSingleExitFunnel(&key, it.Name(), report);
203 to_delete.push_back(it.Name());
207 for (size_t i = 0; i < to_delete.size(); ++i) {
208 LONG res = key.DeleteEmptyKey(to_delete[i].c_str());
209 if (res != ERROR_SUCCESS)
210 LOG(ERROR) << "Failed to delete key " << to_delete[i];
214 } // namespace
216 const char WatcherMetricsProviderWin::kBrowserExitCodeHistogramName[] =
217 "Stability.BrowserExitCodes";
218 const char WatcherMetricsProviderWin::kExitFunnelHistogramPrefix[] =
219 "Stability.ExitFunnel.";
221 WatcherMetricsProviderWin::WatcherMetricsProviderWin(
222 const base::char16* registry_path, bool report_exit_funnels) :
223 registry_path_(registry_path),
224 report_exit_funnels_(report_exit_funnels) {
227 WatcherMetricsProviderWin::~WatcherMetricsProviderWin() {
230 void WatcherMetricsProviderWin::ProvideStabilityMetrics(
231 metrics::SystemProfileProto* /* system_profile_proto */) {
232 // Note that if there are multiple instances of Chrome running in the same
233 // user account, there's a small race that will double-report the exit codes
234 // from both/multiple instances. This ought to be vanishingly rare and will
235 // only manifest as low-level "random" noise. To work around this it would be
236 // necessary to implement some form of global locking, which is not worth it
237 // here.
238 RecordExitCodes(registry_path_);
239 MaybeRecordExitFunnels(registry_path_, report_exit_funnels_);
242 } // namespace browser_watcher