1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/metrics/chrome_stability_metrics_provider.h"
#include <vector>
#include "base/logging.h"
#include "base/metrics/histogram.h"
#include "base/metrics/sparse_histogram.h"
#include "base/prefs/pref_registry_simple.h"
#include "base/prefs/pref_service.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/common/pref_names.h"
#include "components/metrics/proto/system_profile.pb.h"
#include "content/public/browser/child_process_data.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/user_metrics.h"
#include "content/public/browser/web_contents.h"
#if defined(ENABLE_EXTENSIONS)
#include "extensions/browser/process_map.h"
#endif
#if defined(ENABLE_PLUGINS)
#include "chrome/browser/metrics/plugin_metrics_provider.h"
#endif
#if defined(OS_WIN)
#include <windows.h> // Needed for STATUS_* codes
#include "chrome/installer/util/install_util.h"
#include "components/browser_watcher/crash_reporting_metrics_win.h"
#endif
#if defined(OS_CHROMEOS)
#include "chrome/browser/memory/system_memory_stats_recorder.h"
#endif
namespace {
void IncrementPrefValue(const char* path) {
PrefService* pref = g_browser_process->local_state();
DCHECK(pref);
int value = pref->GetInteger(path);
pref->SetInteger(path, value + 1);
}
void IncrementLongPrefsValue(const char* path) {
PrefService* pref = g_browser_process->local_state();
DCHECK(pref);
int64 value = pref->GetInt64(path);
pref->SetInt64(path, value + 1);
}
// Converts an exit code into something that can be inserted into our
// histograms (which expect non-negative numbers less than MAX_INT).
int MapCrashExitCodeForHistogram(int exit_code) {
#if defined(OS_WIN)
// Since |abs(STATUS_GUARD_PAGE_VIOLATION) == MAX_INT| it causes problems in
// histograms.cc. Solve this by remapping it to a smaller value, which
// hopefully doesn't conflict with other codes.
if (exit_code == STATUS_GUARD_PAGE_VIOLATION)
return 0x1FCF7EC3; // Randomly picked number.
#endif
return std::abs(exit_code);
}
#if defined(OS_WIN)
void CountBrowserCrashDumpAttempts() {
enum Outcome {
OUTCOME_SUCCESS,
OUTCOME_FAILURE,
OUTCOME_UNKNOWN,
OUTCOME_MAX_VALUE
};
browser_watcher::CrashReportingMetrics::Values metrics =
browser_watcher::CrashReportingMetrics(
InstallUtil::IsChromeSxSProcess()
? chrome::kBrowserCrashDumpAttemptsRegistryPathSxS
: chrome::kBrowserCrashDumpAttemptsRegistryPath)
.RetrieveAndResetMetrics();
for (int i = 0; i < metrics.crash_dump_attempts; ++i) {
Outcome outcome = OUTCOME_UNKNOWN;
if (i < metrics.successful_crash_dumps)
outcome = OUTCOME_SUCCESS;
else if (i < metrics.successful_crash_dumps + metrics.failed_crash_dumps)
outcome = OUTCOME_FAILURE;
UMA_STABILITY_HISTOGRAM_ENUMERATION("CrashReport.BreakpadCrashDumpOutcome",
outcome, OUTCOME_MAX_VALUE);
}
for (int i = 0; i < metrics.dump_without_crash_attempts; ++i) {
Outcome outcome = OUTCOME_UNKNOWN;
if (i < metrics.successful_dumps_without_crash) {
outcome = OUTCOME_SUCCESS;
} else if (i < metrics.successful_dumps_without_crash +
metrics.failed_dumps_without_crash) {
outcome = OUTCOME_FAILURE;
}
UMA_STABILITY_HISTOGRAM_ENUMERATION(
"CrashReport.BreakpadDumpWithoutCrashOutcome", outcome,
OUTCOME_MAX_VALUE);
}
}
#endif // defined(OS_WIN)
void RecordChildKills(bool was_extension_process) {
UMA_HISTOGRAM_PERCENTAGE("BrowserRenderProcessHost.ChildKills",
was_extension_process ? 2 : 1);
}
} // namespace
ChromeStabilityMetricsProvider::ChromeStabilityMetricsProvider() {
BrowserChildProcessObserver::Add(this);
}
ChromeStabilityMetricsProvider::~ChromeStabilityMetricsProvider() {
BrowserChildProcessObserver::Remove(this);
}
void ChromeStabilityMetricsProvider::OnRecordingEnabled() {
registrar_.Add(this,
content::NOTIFICATION_LOAD_START,
content::NotificationService::AllSources());
registrar_.Add(this,
content::NOTIFICATION_RENDERER_PROCESS_CLOSED,
content::NotificationService::AllSources());
registrar_.Add(this,
content::NOTIFICATION_RENDER_WIDGET_HOST_HANG,
content::NotificationService::AllSources());
}
void ChromeStabilityMetricsProvider::OnRecordingDisabled() {
registrar_.RemoveAll();
}
void ChromeStabilityMetricsProvider::ProvideStabilityMetrics(
metrics::SystemProfileProto* system_profile_proto) {
PrefService* pref = g_browser_process->local_state();
metrics::SystemProfileProto_Stability* stability_proto =
system_profile_proto->mutable_stability();
int count = pref->GetInteger(prefs::kStabilityPageLoadCount);
if (count) {
stability_proto->set_page_load_count(count);
pref->SetInteger(prefs::kStabilityPageLoadCount, 0);
}
count = pref->GetInteger(prefs::kStabilityChildProcessCrashCount);
if (count) {
stability_proto->set_child_process_crash_count(count);
pref->SetInteger(prefs::kStabilityChildProcessCrashCount, 0);
}
count = pref->GetInteger(prefs::kStabilityRendererCrashCount);
if (count) {
stability_proto->set_renderer_crash_count(count);
pref->SetInteger(prefs::kStabilityRendererCrashCount, 0);
}
count = pref->GetInteger(prefs::kStabilityExtensionRendererCrashCount);
if (count) {
stability_proto->set_extension_renderer_crash_count(count);
pref->SetInteger(prefs::kStabilityExtensionRendererCrashCount, 0);
}
count = pref->GetInteger(prefs::kStabilityRendererHangCount);
if (count) {
stability_proto->set_renderer_hang_count(count);
pref->SetInteger(prefs::kStabilityRendererHangCount, 0);
}
#if defined(OS_WIN)
CountBrowserCrashDumpAttempts();
#endif // defined(OS_WIN)
}
void ChromeStabilityMetricsProvider::ClearSavedStabilityMetrics() {
PrefService* local_state = g_browser_process->local_state();
// Clear all the prefs used in this class in UMA reports (which doesn't
// include |kUninstallMetricsPageLoadCount| as it's not sent up by UMA).
local_state->SetInteger(prefs::kStabilityChildProcessCrashCount, 0);
local_state->SetInteger(prefs::kStabilityExtensionRendererCrashCount, 0);
local_state->SetInteger(prefs::kStabilityPageLoadCount, 0);
local_state->SetInteger(prefs::kStabilityRendererCrashCount, 0);
local_state->SetInteger(prefs::kStabilityRendererHangCount, 0);
}
// static
void ChromeStabilityMetricsProvider::RegisterPrefs(
PrefRegistrySimple* registry) {
registry->RegisterIntegerPref(prefs::kStabilityChildProcessCrashCount, 0);
registry->RegisterIntegerPref(prefs::kStabilityExtensionRendererCrashCount,
0);
registry->RegisterIntegerPref(prefs::kStabilityPageLoadCount, 0);
registry->RegisterIntegerPref(prefs::kStabilityRendererCrashCount, 0);
registry->RegisterIntegerPref(prefs::kStabilityRendererHangCount, 0);
registry->RegisterInt64Pref(prefs::kUninstallMetricsPageLoadCount, 0);
}
void ChromeStabilityMetricsProvider::Observe(
int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
switch (type) {
case content::NOTIFICATION_LOAD_START: {
content::NavigationController* controller =
content::Source<content::NavigationController>(source).ptr();
content::WebContents* web_contents = controller->GetWebContents();
LogLoadStarted(web_contents);
break;
}
case content::NOTIFICATION_RENDERER_PROCESS_CLOSED: {
content::RenderProcessHost::RendererClosedDetails* process_details =
content::Details<content::RenderProcessHost::RendererClosedDetails>(
details).ptr();
content::RenderProcessHost* host =
content::Source<content::RenderProcessHost>(source).ptr();
LogRendererCrash(
host, process_details->status, process_details->exit_code);
break;
}
case content::NOTIFICATION_RENDER_WIDGET_HOST_HANG:
LogRendererHang();
break;
default:
NOTREACHED();
break;
}
}
void ChromeStabilityMetricsProvider::BrowserChildProcessCrashed(
const content::ChildProcessData& data,
int exit_code) {
#if defined(ENABLE_PLUGINS)
// Exclude plugin crashes from the count below because we report them via
// a separate UMA metric.
if (PluginMetricsProvider::IsPluginProcess(data.process_type))
return;
#endif
IncrementPrefValue(prefs::kStabilityChildProcessCrashCount);
}
void ChromeStabilityMetricsProvider::LogLoadStarted(
content::WebContents* web_contents) {
content::RecordAction(base::UserMetricsAction("PageLoad"));
// TODO(asvitkine): Check if this is used for anything and if not, remove.
LOCAL_HISTOGRAM_BOOLEAN("Chrome.UmaPageloadCounter", true);
IncrementPrefValue(prefs::kStabilityPageLoadCount);
IncrementLongPrefsValue(prefs::kUninstallMetricsPageLoadCount);
// We need to save the prefs, as page load count is a critical stat, and it
// might be lost due to a crash :-(.
}
void ChromeStabilityMetricsProvider::LogRendererCrash(
content::RenderProcessHost* host,
base::TerminationStatus status,
int exit_code) {
bool was_extension_process = false;
#if defined(ENABLE_EXTENSIONS)
was_extension_process =
extensions::ProcessMap::Get(host->GetBrowserContext())->Contains(
host->GetID());
#endif
if (status == base::TERMINATION_STATUS_PROCESS_CRASHED ||
status == base::TERMINATION_STATUS_ABNORMAL_TERMINATION) {
if (was_extension_process) {
IncrementPrefValue(prefs::kStabilityExtensionRendererCrashCount);
UMA_HISTOGRAM_SPARSE_SLOWLY("CrashExitCodes.Extension",
MapCrashExitCodeForHistogram(exit_code));
} else {
IncrementPrefValue(prefs::kStabilityRendererCrashCount);
UMA_HISTOGRAM_SPARSE_SLOWLY("CrashExitCodes.Renderer",
MapCrashExitCodeForHistogram(exit_code));
}
UMA_HISTOGRAM_PERCENTAGE("BrowserRenderProcessHost.ChildCrashes",
was_extension_process ? 2 : 1);
} else if (status == base::TERMINATION_STATUS_PROCESS_WAS_KILLED) {
RecordChildKills(was_extension_process);
#if defined(OS_CHROMEOS)
} else if (status == base::TERMINATION_STATUS_PROCESS_WAS_KILLED_BY_OOM) {
RecordChildKills(was_extension_process);
UMA_HISTOGRAM_ENUMERATION("BrowserRenderProcessHost.ChildKills.OOM",
was_extension_process ? 2 : 1,
3);
memory::RecordMemoryStats(
was_extension_process
? memory::RECORD_MEMORY_STATS_EXTENSIONS_OOM_KILLED
: memory::RECORD_MEMORY_STATS_CONTENTS_OOM_KILLED);
#endif
} else if (status == base::TERMINATION_STATUS_STILL_RUNNING) {
UMA_HISTOGRAM_PERCENTAGE("BrowserRenderProcessHost.DisconnectedAlive",
was_extension_process ? 2 : 1);
}
}
void ChromeStabilityMetricsProvider::LogRendererHang() {
IncrementPrefValue(prefs::kStabilityRendererHangCount);
}
|