Don't preload rarely seen large images
[chromium-blink-merge.git] / components / browser_watcher / watcher_metrics_provider_win.cc
blob2798ae56c42d0fce3d9a17387b60cd78df1b33bc
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.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::Process process =
55 base::Process::Open(static_cast<base::ProcessId>(pid));
56 if (process.IsValid()) {
57 // The fact that it was possible to open the process says it's live.
58 return false;
61 return true;
64 void RecordExitCodes(const base::string16& registry_path) {
65 base::win::RegKey regkey(HKEY_CURRENT_USER,
66 registry_path.c_str(),
67 KEY_QUERY_VALUE | KEY_SET_VALUE);
68 if (!regkey.Valid())
69 return;
71 size_t num = regkey.GetValueCount();
72 if (num == 0)
73 return;
74 std::vector<base::string16> to_delete;
76 // Record the exit codes in a sparse stability histogram, as the range of
77 // values used to report failures is large.
78 base::HistogramBase* exit_code_histogram =
79 base::SparseHistogram::FactoryGet(
80 WatcherMetricsProviderWin::kBrowserExitCodeHistogramName,
81 base::HistogramBase::kUmaStabilityHistogramFlag);
83 for (size_t i = 0; i < num; ++i) {
84 base::string16 name;
85 if (regkey.GetValueNameAt(static_cast<int>(i), &name) == ERROR_SUCCESS) {
86 DWORD exit_code = 0;
87 if (regkey.ReadValueDW(name.c_str(), &exit_code) == ERROR_SUCCESS) {
88 // Do not report exit codes for processes that are still live,
89 // notably for our own process.
90 if (exit_code != STILL_ACTIVE || IsDeadProcess(name)) {
91 to_delete.push_back(name);
92 exit_code_histogram->Add(exit_code);
98 // Delete the values reported above.
99 for (size_t i = 0; i < to_delete.size(); ++i)
100 regkey.DeleteValue(to_delete[i].c_str());
103 void ReadSingleExitFunnel(
104 base::win::RegKey* parent_key, const base::char16* name,
105 std::vector<std::pair<base::string16, int64>>* events_out) {
106 DCHECK(parent_key);
107 DCHECK(name);
108 DCHECK(events_out);
110 base::win::RegKey regkey(parent_key->Handle(), name, KEY_READ | KEY_WRITE);
111 if (!regkey.Valid())
112 return;
114 // Exit early if no work to do.
115 size_t num = regkey.GetValueCount();
116 if (num == 0)
117 return;
119 // Enumerate the recorded events for this process for processing.
120 std::vector<std::pair<base::string16, int64>> events;
121 for (size_t i = 0; i < num; ++i) {
122 base::string16 event_name;
123 LONG res = regkey.GetValueNameAt(static_cast<int>(i), &event_name);
124 if (res == ERROR_SUCCESS) {
125 int64 event_time = 0;
126 res = regkey.ReadInt64(event_name.c_str(), &event_time);
127 if (res == ERROR_SUCCESS)
128 events.push_back(std::make_pair(event_name, event_time));
132 // Attempt to delete the values before reporting anything.
133 // Exit if this fails to make sure there is no double-reporting on e.g.
134 // permission problems or other corruption.
135 for (size_t i = 0; i < events.size(); ++i) {
136 const base::string16& event_name = events[i].first;
137 LONG res = regkey.DeleteValue(event_name.c_str());
138 if (res != ERROR_SUCCESS) {
139 LOG(ERROR) << "Failed to delete value " << event_name;
140 return;
144 events_out->swap(events);
147 void MaybeRecordSingleExitFunnel(base::win::RegKey* parent_key,
148 const base::char16* name,
149 bool report) {
150 std::vector<std::pair<base::string16, int64>> events;
151 ReadSingleExitFunnel(parent_key, name, &events);
152 if (!report)
153 return;
155 // Find the earliest event time.
156 int64 min_time = std::numeric_limits<int64>::max();
157 for (size_t i = 0; i < events.size(); ++i)
158 min_time = std::min(min_time, events[i].second);
160 // Record the exit funnel event times in a sparse stability histogram.
161 for (size_t i = 0; i < events.size(); ++i) {
162 std::string histogram_name(
163 WatcherMetricsProviderWin::kExitFunnelHistogramPrefix);
164 histogram_name.append(base::WideToUTF8(events[i].first));
165 base::TimeDelta event_time =
166 base::Time::FromInternalValue(events[i].second) -
167 base::Time::FromInternalValue(min_time);
168 base::HistogramBase* histogram =
169 base::SparseHistogram::FactoryGet(
170 histogram_name.c_str(),
171 base::HistogramBase::kUmaStabilityHistogramFlag);
173 // Record the time rounded up to the nearest millisecond.
174 histogram->Add(event_time.InMillisecondsRoundedUp());
178 void MaybeRecordExitFunnels(const base::string16& registry_path, bool report) {
179 base::win::RegistryKeyIterator it(HKEY_CURRENT_USER, registry_path.c_str());
180 if (!it.Valid())
181 return;
183 // Exit early if no work to do.
184 if (it.SubkeyCount() == 0)
185 return;
187 // Open the key we use for deletion preemptively to prevent reporting
188 // multiple times on permission problems.
189 base::win::RegKey key(HKEY_CURRENT_USER,
190 registry_path.c_str(),
191 KEY_QUERY_VALUE);
192 if (!key.Valid()) {
193 LOG(ERROR) << "Failed to open " << registry_path << " for writing.";
194 return;
197 std::vector<base::string16> to_delete;
198 for (; it.Valid(); ++it) {
199 // Defer reporting on still-live processes.
200 if (IsDeadProcess(it.Name())) {
201 MaybeRecordSingleExitFunnel(&key, it.Name(), report);
202 to_delete.push_back(it.Name());
206 for (size_t i = 0; i < to_delete.size(); ++i) {
207 LONG res = key.DeleteEmptyKey(to_delete[i].c_str());
208 if (res != ERROR_SUCCESS)
209 LOG(ERROR) << "Failed to delete key " << to_delete[i];
213 } // namespace
215 const char WatcherMetricsProviderWin::kBrowserExitCodeHistogramName[] =
216 "Stability.BrowserExitCodes";
217 const char WatcherMetricsProviderWin::kExitFunnelHistogramPrefix[] =
218 "Stability.ExitFunnel.";
220 WatcherMetricsProviderWin::WatcherMetricsProviderWin(
221 const base::char16* registry_path, bool report_exit_funnels) :
222 registry_path_(registry_path),
223 report_exit_funnels_(report_exit_funnels) {
226 WatcherMetricsProviderWin::~WatcherMetricsProviderWin() {
229 void WatcherMetricsProviderWin::ProvideStabilityMetrics(
230 metrics::SystemProfileProto* /* system_profile_proto */) {
231 // Note that if there are multiple instances of Chrome running in the same
232 // user account, there's a small race that will double-report the exit codes
233 // from both/multiple instances. This ought to be vanishingly rare and will
234 // only manifest as low-level "random" noise. To work around this it would be
235 // necessary to implement some form of global locking, which is not worth it
236 // here.
237 RecordExitCodes(registry_path_);
238 MaybeRecordExitFunnels(registry_path_, report_exit_funnels_);
241 } // namespace browser_watcher