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"
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
{
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
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.
43 if (!base::StringToInt(key_or_value_name
, &pid
) || pid
== 0)
46 // This is a very inexpensive check for the common case of our own PID.
47 if (static_cast<base::ProcessId
>(pid
) == base::GetCurrentProcId())
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.
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
);
72 size_t num
= regkey
.GetValueCount();
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
) {
86 if (regkey
.GetValueNameAt(static_cast<int>(i
), &name
) == ERROR_SUCCESS
) {
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
) {
111 base::win::RegKey
regkey(parent_key
->Handle(), name
, KEY_READ
| KEY_WRITE
);
115 // Exit early if no work to do.
116 size_t num
= regkey
.GetValueCount();
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
;
145 events_out
->swap(events
);
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
);
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
);
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
);
171 // Record the time rounded up to the nearest millisecond.
172 histogram
->Add(event_time
.InMillisecondsRoundedUp());
176 void RecordExitFunnels(const base::string16
& registry_path
) {
177 base::win::RegistryKeyIterator
it(HKEY_CURRENT_USER
, registry_path
.c_str());
181 // Exit early if no work to do.
182 if (it
.SubkeyCount() == 0)
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(),
191 LOG(ERROR
) << "Failed to open " << registry_path
<< " for writing.";
195 std::vector
<base::string16
> to_delete
;
196 for (; it
.Valid(); ++it
) {
197 // Defer reporting on still-live processes.
198 if (IsDeadProcess(it
.Name())) {
199 RecordSingleExitFunnel(&key
, it
.Name());
200 to_delete
.push_back(it
.Name());
204 for (size_t i
= 0; i
< to_delete
.size(); ++i
) {
205 LONG res
= key
.DeleteEmptyKey(to_delete
[i
].c_str());
206 if (res
!= ERROR_SUCCESS
)
207 LOG(ERROR
) << "Failed to delete key " << to_delete
[i
];
213 const char WatcherMetricsProviderWin::kBrowserExitCodeHistogramName
[] =
214 "Stability.BrowserExitCodes";
215 const char WatcherMetricsProviderWin::kExitFunnelHistogramPrefix
[] =
216 "Stability.ExitFunnel.";
218 WatcherMetricsProviderWin::WatcherMetricsProviderWin(
219 const base::char16
* registry_path
) : registry_path_(registry_path
) {
222 WatcherMetricsProviderWin::~WatcherMetricsProviderWin() {
225 void WatcherMetricsProviderWin::ProvideStabilityMetrics(
226 metrics::SystemProfileProto
* /* system_profile_proto */) {
227 // Note that if there are multiple instances of Chrome running in the same
228 // user account, there's a small race that will double-report the exit codes
229 // from both/multiple instances. This ought to be vanishingly rare and will
230 // only manifest as low-level "random" noise. To work around this it would be
231 // necessary to implement some form of global locking, which is not worth it
233 RecordExitCodes(registry_path_
);
234 RecordExitFunnels(registry_path_
);
237 } // namespace browser_watcher