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"
9 #include "base/metrics/sparse_histogram.h"
10 #include "base/process/process_handle.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/strings/string_piece.h"
13 #include "base/win/registry.h"
15 namespace browser_watcher
{
19 void CompileAsserts() {
20 // 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
22 // the high bit set on Windows, so there's no danger of overflow if this is
24 COMPILE_ASSERT(sizeof(DWORD
) == sizeof(int),
25 process_ids_have_outgrown_an_int
);
28 // This function does soft matching on the PID recorded in the key only.
29 // Due to PID reuse, the possibility exists that the process that's now live
30 // with the given PID is not the same process the data was recorded for.
31 // This doesn't matter for the purpose, as eventually the data will be
32 // scavenged and reported.
33 bool IsDeadProcess(base::StringPiece16 key_name
) {
34 // Truncate the input string to the first occurrence of '-', if one exists.
35 size_t num_end
= key_name
.find(L
'-');
36 if (num_end
!= base::StringPiece16::npos
)
37 key_name
= key_name
.substr(0, num_end
);
39 // Convert to the numeric PID.
41 if (!base::StringToInt(key_name
, &pid
) || pid
== 0)
44 // This is a very inexpensive check for the common case of our own PID.
45 if (static_cast<base::ProcessId
>(pid
) == base::GetCurrentProcId())
48 // The process is not our own - see whether a process with this PID exists.
49 // This is more expensive than the above check, but should also be very rare,
50 // as this only happens more than once for a given PID if a user is running
51 // multiple Chrome instances concurrently.
52 base::ProcessHandle process
= base::kNullProcessHandle
;
53 if (base::OpenProcessHandle(static_cast<base::ProcessId
>(pid
), &process
)) {
54 base::CloseProcessHandle(process
);
56 // The fact that it was possible to open the process says it's live.
65 const char WatcherMetricsProviderWin::kBrowserExitCodeHistogramName
[] =
66 "Stability.BrowserExitCodes";
68 WatcherMetricsProviderWin::WatcherMetricsProviderWin(
69 const base::char16
* registry_path
) : registry_path_(registry_path
) {
72 WatcherMetricsProviderWin::~WatcherMetricsProviderWin() {
75 void WatcherMetricsProviderWin::ProvideStabilityMetrics(
76 metrics::SystemProfileProto
* /* system_profile_proto */) {
77 // 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
79 // 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
81 // necessary to implement some form of global locking, which is not worth it
83 base::win::RegKey
regkey(HKEY_CURRENT_USER
,
84 registry_path_
.c_str(),
85 KEY_QUERY_VALUE
| KEY_SET_VALUE
);
87 size_t num
= regkey
.GetValueCount();
89 std::vector
<base::string16
> to_delete
;
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
);
97 for (size_t i
= 0; i
< num
; ++i
) {
99 if (regkey
.GetValueNameAt(static_cast<int>(i
), &name
) == ERROR_SUCCESS
) {
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
);
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());
118 } // namespace browser_watcher