Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4)

Side by Side Diff: chrome/browser/metrics/chrome_stability_metrics_provider.cc

Issue 289283011: Introduce ChromeStabilityMetricsProvider (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright 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.
4
5 #include "chrome/browser/metrics/chrome_stability_metrics_provider.h"
6
7 #include <vector>
8
9 #include "base/logging.h"
10 #include "base/metrics/histogram.h"
11 #include "base/metrics/sparse_histogram.h"
12 #include "base/prefs/pref_service.h"
13 #include "base/strings/string16.h"
Alexei Svitkine (slow) 2014/05/21 15:02:29 Nit: Is this needed?
blundell 2014/05/21 15:14:17 Done.
14 #include "base/strings/string_util.h"
Alexei Svitkine (slow) 2014/05/21 15:02:29 Nit: ditto
blundell 2014/05/21 15:14:17 Done.
Alexei Svitkine (slow) 2014/05/21 15:18:54 Do you mean it is actually needed? (Just making su
blundell 2014/05/21 15:23:18 oops, removed both but forgot to save the file. Re
15 #include "chrome/browser/browser_process.h"
16 #include "chrome/browser/chrome_notification_types.h"
17 #include "chrome/common/pref_names.h"
18 #include "content/public/browser/notification_service.h"
19 #include "content/public/browser/render_process_host.h"
20 #include "content/public/browser/user_metrics.h"
21 #include "content/public/browser/web_contents.h"
22 #include "extensions/browser/process_map.h"
23
24 #if defined(OS_WIN)
25 #include <windows.h> // Needed for STATUS_* codes
26 #endif
27
28 namespace {
29
30 void IncrementPrefValue(const char* path) {
31 PrefService* pref = g_browser_process->local_state();
32 DCHECK(pref);
33 int value = pref->GetInteger(path);
34 pref->SetInteger(path, value + 1);
35 }
36
37 void IncrementLongPrefsValue(const char* path) {
38 PrefService* pref = g_browser_process->local_state();
39 DCHECK(pref);
40 int64 value = pref->GetInt64(path);
41 pref->SetInt64(path, value + 1);
42 }
43
44 // Converts an exit code into something that can be inserted into our
45 // histograms (which expect non-negative numbers less than MAX_INT).
46 int MapCrashExitCodeForHistogram(int exit_code) {
47 #if defined(OS_WIN)
48 // Since |abs(STATUS_GUARD_PAGE_VIOLATION) == MAX_INT| it causes problems in
49 // histograms.cc. Solve this by remapping it to a smaller value, which
50 // hopefully doesn't conflict with other codes.
51 if (exit_code == STATUS_GUARD_PAGE_VIOLATION)
52 return 0x1FCF7EC3; // Randomly picked number.
53 #endif
54
55 return std::abs(exit_code);
56 }
57
58 } // namespace
59
60 // TODO(blundell): Rename this class.
Alexei Svitkine (slow) 2014/05/21 15:02:29 Remove.
blundell 2014/05/21 15:14:17 Done.
61 ChromeStabilityMetricsProvider::ChromeStabilityMetricsProvider() {
62 }
63
64 ChromeStabilityMetricsProvider::~ChromeStabilityMetricsProvider() {
65 }
66
67 void ChromeStabilityMetricsProvider::OnRecordingEnabled() {
68 registrar_.Add(this,
69 content::NOTIFICATION_LOAD_START,
70 content::NotificationService::AllSources());
71 registrar_.Add(this,
72 content::NOTIFICATION_RENDERER_PROCESS_CLOSED,
73 content::NotificationService::AllSources());
74 registrar_.Add(this,
75 content::NOTIFICATION_RENDER_WIDGET_HOST_HANG,
76 content::NotificationService::AllSources());
77 }
78
79 void ChromeStabilityMetricsProvider::OnRecordingDisabled() {
80 registrar_.RemoveAll();
81 }
82
83 void ChromeStabilityMetricsProvider::ProvideStabilityMetrics(
84 metrics::SystemProfileProto_Stability* stability_proto) {
85 PrefService* pref = g_browser_process->local_state();
86
87 int count = pref->GetInteger(prefs::kStabilityPageLoadCount);
88 if (count) {
89 stability_proto->set_page_load_count(count);
90 pref->SetInteger(prefs::kStabilityPageLoadCount, 0);
91 }
92
93 count = pref->GetInteger(prefs::kStabilityRendererCrashCount);
94 if (count) {
95 stability_proto->set_renderer_crash_count(count);
96 pref->SetInteger(prefs::kStabilityRendererCrashCount, 0);
97 }
98
99 count = pref->GetInteger(prefs::kStabilityExtensionRendererCrashCount);
100 if (count) {
101 stability_proto->set_extension_renderer_crash_count(count);
102 pref->SetInteger(prefs::kStabilityExtensionRendererCrashCount, 0);
103 }
104
105 count = pref->GetInteger(prefs::kStabilityRendererHangCount);
106 if (count) {
107 stability_proto->set_renderer_hang_count(count);
108 pref->SetInteger(prefs::kStabilityRendererHangCount, 0);
109 }
110 }
111
112 void ChromeStabilityMetricsProvider::Observe(
113 int type,
114 const content::NotificationSource& source,
115 const content::NotificationDetails& details) {
116 switch (type) {
117 case content::NOTIFICATION_LOAD_START: {
118 content::NavigationController* controller =
119 content::Source<content::NavigationController>(source).ptr();
120 content::WebContents* web_contents = controller->GetWebContents();
121 LogLoadStarted(web_contents);
122 break;
123 }
124
125 case content::NOTIFICATION_RENDERER_PROCESS_CLOSED: {
126 content::RenderProcessHost::RendererClosedDetails* process_details =
127 content::Details<content::RenderProcessHost::RendererClosedDetails>(
128 details).ptr();
129 content::RenderProcessHost* host =
130 content::Source<content::RenderProcessHost>(source).ptr();
131 LogRendererCrash(
132 host, process_details->status, process_details->exit_code);
133 break;
134 }
135
136 case content::NOTIFICATION_RENDER_WIDGET_HOST_HANG:
137 LogRendererHang();
138 break;
139
140 default:
141 NOTREACHED();
142 break;
143 }
144 }
145
146 void ChromeStabilityMetricsProvider::LogLoadStarted(
147 content::WebContents* web_contents) {
148 content::RecordAction(base::UserMetricsAction("PageLoad"));
149 HISTOGRAM_ENUMERATION("Chrome.UmaPageloadCounter", 1, 2);
150 IncrementPrefValue(prefs::kStabilityPageLoadCount);
151 IncrementLongPrefsValue(prefs::kUninstallMetricsPageLoadCount);
152 // We need to save the prefs, as page load count is a critical stat, and it
153 // might be lost due to a crash :-(.
154 }
155
156 void ChromeStabilityMetricsProvider::LogRendererCrash(
157 content::RenderProcessHost* host,
158 base::TerminationStatus status,
159 int exit_code) {
160 bool was_extension_process =
161 extensions::ProcessMap::Get(host->GetBrowserContext())
162 ->Contains(host->GetID());
163 if (status == base::TERMINATION_STATUS_PROCESS_CRASHED ||
164 status == base::TERMINATION_STATUS_ABNORMAL_TERMINATION) {
165 if (was_extension_process) {
166 IncrementPrefValue(prefs::kStabilityExtensionRendererCrashCount);
167
168 UMA_HISTOGRAM_SPARSE_SLOWLY("CrashExitCodes.Extension",
169 MapCrashExitCodeForHistogram(exit_code));
170 } else {
171 IncrementPrefValue(prefs::kStabilityRendererCrashCount);
172
173 UMA_HISTOGRAM_SPARSE_SLOWLY("CrashExitCodes.Renderer",
174 MapCrashExitCodeForHistogram(exit_code));
175 }
176
177 UMA_HISTOGRAM_PERCENTAGE("BrowserRenderProcessHost.ChildCrashes",
178 was_extension_process ? 2 : 1);
179 } else if (status == base::TERMINATION_STATUS_PROCESS_WAS_KILLED) {
180 UMA_HISTOGRAM_PERCENTAGE("BrowserRenderProcessHost.ChildKills",
181 was_extension_process ? 2 : 1);
182 } else if (status == base::TERMINATION_STATUS_STILL_RUNNING) {
183 UMA_HISTOGRAM_PERCENTAGE("BrowserRenderProcessHost.DisconnectedAlive",
184 was_extension_process ? 2 : 1);
185 }
186 }
187
188 void ChromeStabilityMetricsProvider::LogRendererHang() {
189 IncrementPrefValue(prefs::kStabilityRendererHangCount);
190 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698