blob: 8c008d7976a51ba2c3ebf495b2ca53217d722229 [file] [log] [blame]
isherman@chromium.org2e4cd1a2012-01-12 08:51:031// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
initial.commit09911bf2008-07-26 23:55:295//------------------------------------------------------------------------------
6// Description of the life cycle of a instance of MetricsService.
7//
8// OVERVIEW
9//
isherman@chromium.orge3eb0c42013-04-18 06:18:5810// A MetricsService instance is typically created at application startup. It is
11// the central controller for the acquisition of log data, and the automatic
initial.commit09911bf2008-07-26 23:55:2912// transmission of that log data to an external server. Its major job is to
13// manage logs, grouping them for transmission, and transmitting them. As part
14// of its grouping, MS finalizes logs by including some just-in-time gathered
15// memory statistics, snapshotting the current stats of numerous histograms,
isherman@chromium.orge3eb0c42013-04-18 06:18:5816// closing the logs, translating to protocol buffer format, and compressing the
17// results for transmission. Transmission includes submitting a compressed log
18// as data in a URL-post, and retransmitting (or retaining at process
19// termination) if the attempted transmission failed. Retention across process
20// terminations is done using the the PrefServices facilities. The retained logs
21// (the ones that never got transmitted) are compressed and base64-encoded
22// before being persisted.
initial.commit09911bf2008-07-26 23:55:2923//
jar@chromium.org281d2882009-01-20 20:32:4224// Logs fall into one of two categories: "initial logs," and "ongoing logs."
asvitkine@chromium.org80a8f312013-12-16 18:00:3025// There is at most one initial log sent for each complete run of Chrome (from
26// startup, to browser shutdown). An initial log is generally transmitted some
27// short time (1 minute?) after startup, and includes stats such as recent crash
28// info, the number and types of plugins, etc. The external server's response
29// to the initial log conceptually tells this MS if it should continue
30// transmitting logs (during this session). The server response can actually be
31// much more detailed, and always includes (at a minimum) how often additional
32// ongoing logs should be sent.
initial.commit09911bf2008-07-26 23:55:2933//
34// After the above initial log, a series of ongoing logs will be transmitted.
35// The first ongoing log actually begins to accumulate information stating when
36// the MS was first constructed. Note that even though the initial log is
37// commonly sent a full minute after startup, the initial log does not include
38// much in the way of user stats. The most common interlog period (delay)
asharif@chromium.org3a668152013-06-21 23:56:4239// is 30 minutes. That time period starts when the first user action causes a
initial.commit09911bf2008-07-26 23:55:2940// logging event. This means that if there is no user action, there may be long
jar@chromium.org281d2882009-01-20 20:32:4241// periods without any (ongoing) log transmissions. Ongoing logs typically
initial.commit09911bf2008-07-26 23:55:2942// contain very detailed records of user activities (ex: opened tab, closed
43// tab, fetched URL, maximized window, etc.) In addition, just before an
44// ongoing log is closed out, a call is made to gather memory statistics. Those
45// memory statistics are deposited into a histogram, and the log finalization
46// code is then called. In the finalization, a call to a Histogram server
47// acquires a list of all local histograms that have been flagged for upload
asvitkine@chromium.org80a8f312013-12-16 18:00:3048// to the UMA server. The finalization also acquires the most recent number
jar@chromium.org281d2882009-01-20 20:32:4249// of page loads, along with any counts of renderer or plugin crashes.
initial.commit09911bf2008-07-26 23:55:2950//
51// When the browser shuts down, there will typically be a fragment of an ongoing
asvitkine@chromium.org80a8f312013-12-16 18:00:3052// log that has not yet been transmitted. At shutdown time, that fragment is
53// closed (including snapshotting histograms), and persisted, for potential
54// transmission during a future run of the product.
initial.commit09911bf2008-07-26 23:55:2955//
56// There are two slightly abnormal shutdown conditions. There is a
57// "disconnected scenario," and a "really fast startup and shutdown" scenario.
58// In the "never connected" situation, the user has (during the running of the
59// process) never established an internet connection. As a result, attempts to
60// transmit the initial log have failed, and a lot(?) of data has accumulated in
61// the ongoing log (which didn't yet get closed, because there was never even a
62// contemplation of sending it). There is also a kindred "lost connection"
63// situation, where a loss of connection prevented an ongoing log from being
64// transmitted, and a (still open) log was stuck accumulating a lot(?) of data,
65// while the earlier log retried its transmission. In both of these
66// disconnected situations, two logs need to be, and are, persistently stored
67// for future transmission.
68//
69// The other unusual shutdown condition, termed "really fast startup and
70// shutdown," involves the deliberate user termination of the process before
71// the initial log is even formed or transmitted. In that situation, no logging
72// is done, but the historical crash statistics remain (unlogged) for inclusion
73// in a future run's initial log. (i.e., we don't lose crash stats).
74//
75// With the above overview, we can now describe the state machine's various
asvitkine@chromium.org80a8f312013-12-16 18:00:3076// states, based on the State enum specified in the state_ member. Those states
initial.commit09911bf2008-07-26 23:55:2977// are:
78//
asvitkine@chromium.org80a8f312013-12-16 18:00:3079// INITIALIZED, // Constructor was called.
80// INIT_TASK_SCHEDULED, // Waiting for deferred init tasks to finish.
81// INIT_TASK_DONE, // Waiting for timer to send initial log.
82// SENDING_INITIAL_STABILITY_LOG, // Initial stability log being sent.
83// SENDING_INITIAL_METRICS_LOG, // Initial metrics log being sent.
84// SENDING_OLD_LOGS, // Sending unsent logs from previous session.
85// SENDING_CURRENT_LOGS, // Sending ongoing logs as they acrue.
initial.commit09911bf2008-07-26 23:55:2986//
87// In more detail, we have:
88//
89// INITIALIZED, // Constructor was called.
90// The MS has been constructed, but has taken no actions to compose the
91// initial log.
92//
asvitkine@chromium.org80a8f312013-12-16 18:00:3093// INIT_TASK_SCHEDULED, // Waiting for deferred init tasks to finish.
initial.commit09911bf2008-07-26 23:55:2994// Typically about 30 seconds after startup, a task is sent to a second thread
zelidrag@chromium.org85ed9d42010-06-08 22:37:4495// (the file thread) to perform deferred (lower priority and slower)
96// initialization steps such as getting the list of plugins. That task will
97// (when complete) make an async callback (via a Task) to indicate the
98// completion.
initial.commit09911bf2008-07-26 23:55:2999//
zelidrag@chromium.org85ed9d42010-06-08 22:37:44100// INIT_TASK_DONE, // Waiting for timer to send initial log.
initial.commit09911bf2008-07-26 23:55:29101// The callback has arrived, and it is now possible for an initial log to be
102// created. This callback typically arrives back less than one second after
zelidrag@chromium.org85ed9d42010-06-08 22:37:44103// the deferred init task is dispatched.
initial.commit09911bf2008-07-26 23:55:29104//
asvitkine@chromium.org80a8f312013-12-16 18:00:30105// SENDING_INITIAL_STABILITY_LOG, // Initial stability log being sent.
106// During initialization, if a crash occurred during the previous session, an
107// initial stability log will be generated and registered with the log manager.
108// This state will be entered if a stability log was prepared during metrics
109// service initialization (in InitializeMetricsRecordingState()) and is waiting
110// to be transmitted when it's time to send up the first log (per the reporting
111// scheduler). If there is no initial stability log (e.g. there was no previous
112// crash), then this state will be skipped and the state will advance to
113// SENDING_INITIAL_METRICS_LOG.
114//
115// SENDING_INITIAL_METRICS_LOG, // Initial metrics log being sent.
116// This state is entered after the initial metrics log has been composed, and
117// prepared for transmission. This happens after SENDING_INITIAL_STABILITY_LOG
118// if there was an initial stability log (see above). It is also the case that
119// any previously unsent logs have been loaded into instance variables for
120// possible transmission.
initial.commit09911bf2008-07-26 23:55:29121//
initial.commit09911bf2008-07-26 23:55:29122// SENDING_OLD_LOGS, // Sending unsent logs from previous session.
stuartmorgan@chromium.orgcac267c2011-09-29 15:18:10123// This state indicates that the initial log for this session has been
124// successfully sent and it is now time to send any logs that were
125// saved from previous sessions. All such logs will be transmitted before
126// exiting this state, and proceeding with ongoing logs from the current session
127// (see next state).
initial.commit09911bf2008-07-26 23:55:29128//
129// SENDING_CURRENT_LOGS, // Sending standard current logs as they accrue.
jar@google.com0b33f80b2008-12-17 21:34:36130// Current logs are being accumulated. Typically every 20 minutes a log is
initial.commit09911bf2008-07-26 23:55:29131// closed and finalized for transmission, at the same time as a new log is
132// started.
133//
134// The progression through the above states is simple, and sequential, in the
135// most common use cases. States proceed from INITIAL to SENDING_CURRENT_LOGS,
136// and remain in the latter until shutdown.
137//
138// The one unusual case is when the user asks that we stop logging. When that
stuartmorgan@chromium.orgcac267c2011-09-29 15:18:10139// happens, any staged (transmission in progress) log is persisted, and any log
stuartmorgan@chromium.org410938e02012-10-24 16:33:59140// that is currently accumulating is also finalized and persisted. We then
stuartmorgan@chromium.orgcac267c2011-09-29 15:18:10141// regress back to the SEND_OLD_LOGS state in case the user enables log
142// recording again during this session. This way anything we have persisted
143// will be sent automatically if/when we progress back to SENDING_CURRENT_LOG
144// state.
initial.commit09911bf2008-07-26 23:55:29145//
stuartmorgan@chromium.org410938e02012-10-24 16:33:59146// Another similar case is on mobile, when the application is backgrounded and
147// then foregrounded again. Backgrounding created new "old" stored logs, so the
148// state drops back from SENDING_CURRENT_LOGS to SENDING_OLD_LOGS so those logs
149// will be sent.
150//
stuartmorgan@chromium.orgcac267c2011-09-29 15:18:10151// Also note that whenever we successfully send an old log, we mirror the list
152// of logs into the PrefService. This ensures that IF we crash, we won't start
153// up and retransmit our old logs again.
initial.commit09911bf2008-07-26 23:55:29154//
155// Due to race conditions, it is always possible that a log file could be sent
156// twice. For example, if a log file is sent, but not yet acknowledged by
157// the external server, and the user shuts down, then a copy of the log may be
158// saved for re-transmission. These duplicates could be filtered out server
jar@chromium.org281d2882009-01-20 20:32:42159// side, but are not expected to be a significant problem.
initial.commit09911bf2008-07-26 23:55:29160//
161//
162//------------------------------------------------------------------------------
163
maruel@chromium.org40bcc302009-03-02 20:50:39164#include "chrome/browser/metrics/metrics_service.h"
165
eroman@chromium.orgd7c1fa62012-06-15 23:35:30166#include <algorithm>
167
stuartmorgan@chromium.org7f7f1962011-04-20 15:58:16168#include "base/bind.h"
169#include "base/callback.h"
erg@google.com5d91c9e2010-07-28 17:25:28170#include "base/command_line.h"
akalin@chromium.org3dc1bc42012-06-19 08:20:53171#include "base/guid.h"
ziadh@chromium.org46f89e142010-07-19 08:00:42172#include "base/md5.h"
brettw@chromium.org835d7c82010-10-14 04:38:38173#include "base/metrics/histogram.h"
eroman@chromium.org1026afd2013-03-20 14:28:54174#include "base/metrics/sparse_histogram.h"
kaiwang@chromium.org567d30e2012-07-13 21:48:29175#include "base/metrics/statistics_recorder.h"
joi@chromium.org3853a4c2013-02-11 17:15:57176#include "base/prefs/pref_registry_simple.h"
177#include "base/prefs/pref_service.h"
derat@chromium.org9eec53fe2013-10-30 20:21:17178#include "base/prefs/scoped_user_pref_update.h"
stevet@chromium.orge61003a2012-05-24 17:03:19179#include "base/rand_util.h"
brettw@chromium.org3ea1b182013-02-08 22:38:41180#include "base/strings/string_number_conversions.h"
avi@chromium.org112158af2013-06-07 23:46:18181#include "base/strings/utf_string_conversions.h"
brettw@chromium.orgce072a72010-12-31 20:02:16182#include "base/threading/platform_thread.h"
tfarina@chromium.orgb3841c502011-03-09 01:21:31183#include "base/threading/thread.h"
jam@chromium.org3a7b66d2012-04-26 16:34:16184#include "base/threading/thread_restrictions.h"
isherman@chromium.orged0fd002012-04-25 23:10:34185#include "base/tracked_objects.h"
erg@google.com679082052010-07-21 21:30:13186#include "base/values.h"
initial.commit09911bf2008-07-26 23:55:29187#include "chrome/browser/browser_process.h"
jam@chromium.org9ea0cd32013-07-12 01:50:36188#include "chrome/browser/chrome_notification_types.h"
aa@chromium.org6f371442011-11-09 06:45:46189#include "chrome/browser/extensions/extension_service.h"
isherman@chromium.orgb8ddb052012-04-19 02:36:06190#include "chrome/browser/io_thread.h"
sail@chromium.org84c988a2011-04-19 17:56:33191#include "chrome/browser/memory_details.h"
asharif@chromium.org537c638d2013-07-04 00:49:19192#include "chrome/browser/metrics/compression_utils.h"
erg@google.com679082052010-07-21 21:30:13193#include "chrome/browser/metrics/metrics_log.h"
stuartmorgan@chromium.orgcac267c2011-09-29 15:18:10194#include "chrome/browser/metrics/metrics_log_serializer.h"
stuartmorgan@chromium.org7f7f1962011-04-20 15:58:16195#include "chrome/browser/metrics/metrics_reporting_scheduler.h"
simonjam@chromium.orgaa312812013-04-30 19:46:05196#include "chrome/browser/metrics/time_ticks_experiment_win.h"
isherman@chromium.orged0fd002012-04-25 23:10:34197#include "chrome/browser/metrics/tracking_synchronizer.h"
bengr@chromium.org60677562013-11-17 15:52:55198#include "chrome/common/metrics/variations/variations_util.h"
simonjam@chromium.orgadbb3762012-03-09 22:20:08199#include "chrome/browser/net/http_pipelining_compatibility_client.h"
rtenneti@chromium.orgd67d1052011-06-09 05:11:41200#include "chrome/browser/net/network_stats.h"
tfarina@chromium.org0fafc8d2013-06-01 00:09:50201#include "chrome/browser/omnibox/omnibox_log.h"
ben@chromium.org8ecad5e2010-12-02 21:18:33202#include "chrome/browser/profiles/profile.h"
tfarina@chromium.org71b73f02011-04-06 15:57:29203#include "chrome/browser/ui/browser_list.h"
dtrainor@chromium.org10b132b02012-07-27 20:46:18204#include "chrome/browser/ui/browser_otr_state.h"
rlp@chromium.org752a5262013-06-23 14:53:42205#include "chrome/browser/ui/search/search_tab_helper.h"
rogerm@chromium.org261ab7c2013-08-19 15:04:58206#include "chrome/common/chrome_constants.h"
eroman@chromium.orgd7c1fa62012-06-15 23:35:30207#include "chrome/common/chrome_result_codes.h"
jar@chromium.org92745242009-06-12 16:52:21208#include "chrome/common/chrome_switches.h"
rsesek@chromium.org264c0acac2013-10-01 13:33:30209#include "chrome/common/crash_keys.h"
asvitkine@chromium.orgc277e2b2013-08-02 15:41:08210#include "chrome/common/metrics/caching_permuted_entropy_provider.h"
isherman@chromium.org2e4cd1a2012-01-12 08:51:03211#include "chrome/common/metrics/metrics_log_manager.h"
simonjam@chromium.orgb4a72d842012-03-22 20:09:09212#include "chrome/common/net/test_server_locations.h"
initial.commit09911bf2008-07-26 23:55:29213#include "chrome/common/pref_names.h"
jam@chromium.orge09ba552009-02-05 03:26:29214#include "chrome/common/render_messages.h"
asvitkine@chromium.org50ae9f12013-08-29 18:03:22215#include "components/variations/entropy_provider.h"
bengr@chromium.org60677562013-11-17 15:52:55216#include "components/variations/metrics_util.h"
jam@chromium.org4967f792012-01-20 22:14:40217#include "content/public/browser/child_process_data.h"
rtenneti@google.com83ab4a282012-07-12 18:19:45218#include "content/public/browser/histogram_fetcher.h"
tfarina@chromium.org09d31d52012-03-11 22:30:27219#include "content/public/browser/load_notification_details.h"
jam@chromium.orgad50def52011-10-19 23:17:07220#include "content/public/browser/notification_service.h"
jam@chromium.org3a5180ae2011-12-21 02:39:38221#include "content/public/browser/plugin_service.h"
ananta@chromium.orgf3b1a082011-11-18 00:34:30222#include "content/public/browser/render_process_host.h"
mpearson@chromium.org5d490e42012-08-30 05:16:43223#include "content/public/browser/user_metrics.h"
avi@chromium.org459f3502012-09-17 17:08:12224#include "content/public/browser/web_contents.h"
yael.aharon@intel.comd5d383252013-07-04 14:44:32225#include "content/public/common/process_type.h"
jam@chromium.orgd7bd3e52013-07-21 04:29:20226#include "content/public/common/webplugininfo.h"
benwells@chromium.org50de9aa22013-11-14 06:30:34227#include "extensions/browser/process_map.h"
isherman@chromium.orgfe58acc22012-02-29 01:29:58228#include "net/base/load_flags.h"
akalin@chromium.org3dc1bc42012-06-19 08:20:53229#include "net/url_request/url_fetcher.h"
initial.commit09911bf2008-07-26 23:55:29230
phajdan.jr@chromium.orge06131d2010-02-10 18:40:33231// TODO(port): port browser_distribution.h.
232#if !defined(OS_POSIX)
kuchhal@chromium.org79bf0b72009-04-27 21:30:55233#include "chrome/installer/util/browser_distribution.h"
paul@chromium.orgdc6f4962009-02-13 01:25:50234#endif
235
rvargas@google.com5ccaa412009-11-13 22:00:16236#if defined(OS_CHROMEOS)
237#include "chrome/browser/chromeos/external_metrics.h"
stevenjb@chromium.org279690f82013-10-09 08:23:52238#include "chromeos/system/statistics_provider.h"
rvargas@google.com5ccaa412009-11-13 22:00:16239#endif
240
eroman@chromium.orgd7c1fa62012-06-15 23:35:30241#if defined(OS_WIN)
242#include <windows.h> // Needed for STATUS_* codes
rogerm@chromium.org261ab7c2013-08-19 15:04:58243#include "base/win/registry.h"
eroman@chromium.orgd7c1fa62012-06-15 23:35:30244#endif
245
vitalybuka@chromium.orga3079832013-10-24 20:29:36246#if !defined(OS_ANDROID)
jianli@chromium.orgcbf160aa2013-11-05 17:54:55247#include "chrome/browser/service_process/service_process_control.h"
vitalybuka@chromium.orga3079832013-10-24 20:29:36248#endif
249
dsh@google.come1acf6f2008-10-27 20:43:33250using base::Time;
joi@chromium.org631bb742011-11-02 11:29:39251using content::BrowserThread;
jam@chromium.org4967f792012-01-20 22:14:40252using content::ChildProcessData;
tfarina@chromium.org09d31d52012-03-11 22:30:27253using content::LoadNotificationDetails;
jam@chromium.org3a5180ae2011-12-21 02:39:38254using content::PluginService;
dsh@google.come1acf6f2008-10-27 20:43:33255
isherman@chromium.orgfe58acc22012-02-29 01:29:58256namespace {
isherman@chromium.orgb2a4812d2012-02-28 05:31:31257
isherman@chromium.orgfe58acc22012-02-29 01:29:58258// Check to see that we're being called on only one thread.
259bool IsSingleThreaded() {
260 static base::PlatformThreadId thread_id = 0;
261 if (!thread_id)
262 thread_id = base::PlatformThread::CurrentId();
263 return base::PlatformThread::CurrentId() == thread_id;
264}
265
stuartmorgan@chromium.org7f7f1962011-04-20 15:58:16266// The delay, in seconds, after starting recording before doing expensive
267// initialization work.
dfalcantara@chromium.org12180f82012-10-10 21:13:30268#if defined(OS_ANDROID) || defined(OS_IOS)
269// On mobile devices, a significant portion of sessions last less than a minute.
270// Use a shorter timer on these platforms to avoid losing data.
271// TODO(dfalcantara): To avoid delaying startup, tighten up initialization so
272// that it occurs after the user gets their initial page.
273const int kInitializationDelaySeconds = 5;
274#else
isherman@chromium.orgfe58acc22012-02-29 01:29:58275const int kInitializationDelaySeconds = 30;
dfalcantara@chromium.org12180f82012-10-10 21:13:30276#endif
petersont@google.com252873ef2008-08-04 21:59:45277
jar@chromium.orgc9a3ef82009-05-28 22:02:46278// This specifies the amount of time to wait for all renderers to send their
279// data.
isherman@chromium.orgfe58acc22012-02-29 01:29:58280const int kMaxHistogramGatheringWaitDuration = 60000; // 60 seconds.
jar@chromium.orgc9a3ef82009-05-28 22:02:46281
stuartmorgan@chromium.org54702c92011-04-15 15:06:43282// The maximum number of events in a log uploaded to the UMA server.
isherman@chromium.orgfe58acc22012-02-29 01:29:58283const int kEventLimit = 2400;
jar@google.com68475e602008-08-22 03:21:15284
285// If an upload fails, and the transmission was over this byte count, then we
286// will discard the log, and not try to retransmit it. We also don't persist
287// the log to the prefs for transmission during the next chrome session if this
288// limit is exceeded.
isherman@chromium.orgfe58acc22012-02-29 01:29:58289const size_t kUploadLogAvoidRetransmitSize = 50000;
initial.commit09911bf2008-07-26 23:55:29290
tedvessenes@gmail.comfc4252a72012-01-12 21:58:47291// Interval, in minutes, between state saves.
isherman@chromium.orgfe58acc22012-02-29 01:29:58292const int kSaveStateIntervalMinutes = 5;
293
isherman@chromium.org4266def22012-05-17 01:02:40294enum ResponseStatus {
295 UNKNOWN_FAILURE,
296 SUCCESS,
297 BAD_REQUEST, // Invalid syntax or log too large.
isherman@chromium.org9f5c1ce82012-05-23 23:11:28298 NO_RESPONSE,
isherman@chromium.org4266def22012-05-17 01:02:40299 NUM_RESPONSE_STATUSES
300};
301
302ResponseStatus ResponseCodeToStatus(int response_code) {
303 switch (response_code) {
304 case 200:
305 return SUCCESS;
306 case 400:
307 return BAD_REQUEST;
isherman@chromium.org9f5c1ce82012-05-23 23:11:28308 case net::URLFetcher::RESPONSE_CODE_INVALID:
309 return NO_RESPONSE;
isherman@chromium.org4266def22012-05-17 01:02:40310 default:
311 return UNKNOWN_FAILURE;
312 }
313}
314
stevet@chromium.orge61003a2012-05-24 17:03:19315// The argument used to generate a non-identifying entropy source. We want no
asvitkine@chromium.org9556a892013-06-21 16:53:20316// more than 13 bits of entropy, so use this max to return a number in the range
317// [0, 7999] as the entropy source (12.97 bits of entropy).
318const int kMaxLowEntropySize = 8000;
stevet@chromium.orge61003a2012-05-24 17:03:19319
asvitkine@chromium.orge63a9ef2012-08-30 15:29:42320// Default prefs value for prefs::kMetricsLowEntropySource to indicate that the
321// value has not yet been set.
322const int kLowEntropySourceNotSet = -1;
323
stevet@chromium.orge61003a2012-05-24 17:03:19324// Generates a new non-identifying entropy source used to seed persistent
325// activities.
326int GenerateLowEntropySource() {
asvitkine@chromium.org20f999b52012-08-24 22:32:59327 return base::RandInt(0, kMaxLowEntropySize - 1);
stevet@chromium.orge61003a2012-05-24 17:03:19328}
329
eroman@chromium.orgd7c1fa62012-06-15 23:35:30330// Converts an exit code into something that can be inserted into our
331// histograms (which expect non-negative numbers less than MAX_INT).
332int MapCrashExitCodeForHistogram(int exit_code) {
333#if defined(OS_WIN)
334 // Since |abs(STATUS_GUARD_PAGE_VIOLATION) == MAX_INT| it causes problems in
335 // histograms.cc. Solve this by remapping it to a smaller value, which
336 // hopefully doesn't conflict with other codes.
337 if (exit_code == STATUS_GUARD_PAGE_VIOLATION)
338 return 0x1FCF7EC3; // Randomly picked number.
339#endif
340
341 return std::abs(exit_code);
342}
343
nileshagrawal@chromium.org84c384e2013-03-01 23:20:19344void MarkAppCleanShutdownAndCommit() {
345 PrefService* pref = g_browser_process->local_state();
346 pref->SetBoolean(prefs::kStabilityExitedCleanly, true);
rtenneti@chromium.org6a6d0d12013-10-28 15:58:19347 pref->SetInteger(prefs::kStabilityExecutionPhase,
rtenneti@chromium.org6d67ea0d2013-11-14 11:02:21348 MetricsService::SHUTDOWN_COMPLETE);
nileshagrawal@chromium.org84c384e2013-03-01 23:20:19349 // Start writing right away (write happens on a different thread).
350 pref->CommitPendingWrite();
351}
352
asvitkine@chromium.org80a8f312013-12-16 18:00:30353// Returns whether initial stability metrics should be sent in a separate log.
354bool SendSeparateInitialStabilityLog() {
355 return base::FieldTrialList::FindFullName("UMAStability") == "SeparateLog";
356}
357
asvitkine@chromium.org20f999b52012-08-24 22:32:59358} // namespace
initial.commit09911bf2008-07-26 23:55:29359
bengr@chromium.org60677562013-11-17 15:52:55360
361SyntheticTrialGroup::SyntheticTrialGroup(uint32 trial,
362 uint32 group,
363 base::TimeTicks start)
364 : start_time(start) {
365 id.name = trial;
366 id.group = group;
367}
368
369SyntheticTrialGroup::~SyntheticTrialGroup() {
370}
371
jar@chromium.orgc0c55e92011-09-10 18:47:30372// static
373MetricsService::ShutdownCleanliness MetricsService::clean_shutdown_status_ =
374 MetricsService::CLEANLY_SHUTDOWN;
375
rtenneti@chromium.org6a6d0d12013-10-28 15:58:19376MetricsService::ExecutionPhase MetricsService::execution_phase_ =
rtenneti@chromium.org6d67ea0d2013-11-14 11:02:21377 MetricsService::UNINITIALIZED_PHASE;
rtenneti@chromium.org6a6d0d12013-10-28 15:58:19378
erg@google.com679082052010-07-21 21:30:13379// This is used to quickly log stats from child process related notifications in
380// MetricsService::child_stats_buffer_. The buffer's contents are transferred
381// out when Local State is periodically saved. The information is then
382// reported to the UMA server on next launch.
383struct MetricsService::ChildProcessStats {
384 public:
jam@chromium.orgf3b357692013-03-22 05:16:13385 explicit ChildProcessStats(int process_type)
erg@google.com679082052010-07-21 21:30:13386 : process_launches(0),
387 process_crashes(0),
388 instances(0),
bauerb@chromium.orgcd937072012-07-02 09:00:29389 loading_errors(0),
jam@chromium.orgf3b357692013-03-22 05:16:13390 process_type(process_type) {}
erg@google.com679082052010-07-21 21:30:13391
392 // This constructor is only used by the map to return some default value for
393 // an index for which no value has been assigned.
394 ChildProcessStats()
395 : process_launches(0),
pkasting@chromium.orgd88bf0a2011-08-30 23:55:57396 process_crashes(0),
397 instances(0),
bauerb@chromium.orgcd937072012-07-02 09:00:29398 loading_errors(0),
jam@chromium.orgbd5d6cf2011-12-01 00:39:12399 process_type(content::PROCESS_TYPE_UNKNOWN) {}
erg@google.com679082052010-07-21 21:30:13400
401 // The number of times that the given child process has been launched
402 int process_launches;
403
404 // The number of times that the given child process has crashed
405 int process_crashes;
406
407 // The number of instances of this child process that have been created.
408 // An instance is a DOM object rendered by this child process during a page
409 // load.
410 int instances;
411
bauerb@chromium.orgcd937072012-07-02 09:00:29412 // The number of times there was an error loading an instance of this child
413 // process.
414 int loading_errors;
415
jam@chromium.orgf3b357692013-03-22 05:16:13416 int process_type;
erg@google.com679082052010-07-21 21:30:13417};
initial.commit09911bf2008-07-26 23:55:29418
sail@chromium.org84c988a2011-04-19 17:56:33419// Handles asynchronous fetching of memory details.
420// Will run the provided task after finished.
421class MetricsMemoryDetails : public MemoryDetails {
422 public:
dcheng@chromium.org2226c222011-11-22 00:08:40423 explicit MetricsMemoryDetails(const base::Closure& callback)
424 : callback_(callback) {}
sail@chromium.org84c988a2011-04-19 17:56:33425
rsleevi@chromium.orgb94584a2013-02-07 03:02:08426 virtual void OnDetailsAvailable() OVERRIDE {
xhwang@chromium.orgb3a25092013-05-28 22:08:16427 base::MessageLoop::current()->PostTask(FROM_HERE, callback_);
sail@chromium.org84c988a2011-04-19 17:56:33428 }
429
430 private:
rsleevi@chromium.orgb94584a2013-02-07 03:02:08431 virtual ~MetricsMemoryDetails() {}
sail@chromium.org84c988a2011-04-19 17:56:33432
dcheng@chromium.org2226c222011-11-22 00:08:40433 base::Closure callback_;
sail@chromium.org84c988a2011-04-19 17:56:33434 DISALLOW_COPY_AND_ASSIGN(MetricsMemoryDetails);
435};
436
initial.commit09911bf2008-07-26 23:55:29437// static
joi@chromium.orgb1de2c72013-02-06 02:45:47438void MetricsService::RegisterPrefs(PrefRegistrySimple* registry) {
initial.commit09911bf2008-07-26 23:55:29439 DCHECK(IsSingleThreaded());
dcheng@chromium.org007b3f82013-04-09 08:46:45440 registry->RegisterStringPref(prefs::kMetricsClientID, std::string());
joi@chromium.orgb1de2c72013-02-06 02:45:47441 registry->RegisterIntegerPref(prefs::kMetricsLowEntropySource,
442 kLowEntropySourceNotSet);
443 registry->RegisterInt64Pref(prefs::kMetricsClientIDTimestamp, 0);
444 registry->RegisterInt64Pref(prefs::kStabilityLaunchTimeSec, 0);
445 registry->RegisterInt64Pref(prefs::kStabilityLastTimestampSec, 0);
dcheng@chromium.org007b3f82013-04-09 08:46:45446 registry->RegisterStringPref(prefs::kStabilityStatsVersion, std::string());
joi@chromium.orgb1de2c72013-02-06 02:45:47447 registry->RegisterInt64Pref(prefs::kStabilityStatsBuildTime, 0);
448 registry->RegisterBooleanPref(prefs::kStabilityExitedCleanly, true);
rtenneti@chromium.org6a6d0d12013-10-28 15:58:19449 registry->RegisterIntegerPref(prefs::kStabilityExecutionPhase,
rtenneti@chromium.org6d67ea0d2013-11-14 11:02:21450 UNINITIALIZED_PHASE);
joi@chromium.orgb1de2c72013-02-06 02:45:47451 registry->RegisterBooleanPref(prefs::kStabilitySessionEndCompleted, true);
452 registry->RegisterIntegerPref(prefs::kMetricsSessionID, -1);
453 registry->RegisterIntegerPref(prefs::kStabilityLaunchCount, 0);
454 registry->RegisterIntegerPref(prefs::kStabilityCrashCount, 0);
455 registry->RegisterIntegerPref(prefs::kStabilityIncompleteSessionEndCount, 0);
456 registry->RegisterIntegerPref(prefs::kStabilityPageLoadCount, 0);
457 registry->RegisterIntegerPref(prefs::kStabilityRendererCrashCount, 0);
458 registry->RegisterIntegerPref(prefs::kStabilityExtensionRendererCrashCount,
459 0);
460 registry->RegisterIntegerPref(prefs::kStabilityRendererHangCount, 0);
461 registry->RegisterIntegerPref(prefs::kStabilityChildProcessCrashCount, 0);
462 registry->RegisterIntegerPref(prefs::kStabilityBreakpadRegistrationFail, 0);
463 registry->RegisterIntegerPref(prefs::kStabilityBreakpadRegistrationSuccess,
464 0);
465 registry->RegisterIntegerPref(prefs::kStabilityDebuggerPresent, 0);
466 registry->RegisterIntegerPref(prefs::kStabilityDebuggerNotPresent, 0);
petkov@chromium.orgc1834a92011-01-21 18:21:03467#if defined(OS_CHROMEOS)
joi@chromium.orgb1de2c72013-02-06 02:45:47468 registry->RegisterIntegerPref(prefs::kStabilityOtherUserCrashCount, 0);
469 registry->RegisterIntegerPref(prefs::kStabilityKernelCrashCount, 0);
470 registry->RegisterIntegerPref(prefs::kStabilitySystemUncleanShutdownCount, 0);
petkov@chromium.orgc1834a92011-01-21 18:21:03471#endif // OS_CHROMEOS
cpu@google.come73c01972008-08-13 00:18:24472
asvitkine@chromium.org0f2f7792013-11-28 16:09:14473 registry->RegisterStringPref(prefs::kStabilitySavedSystemProfile,
474 std::string());
475 registry->RegisterStringPref(prefs::kStabilitySavedSystemProfileHash,
476 std::string());
477
isherman@chromium.org5f3e1642013-05-05 03:37:34478 registry->RegisterListPref(prefs::kMetricsInitialLogs);
479 registry->RegisterListPref(prefs::kMetricsOngoingLogs);
robertshield@google.com0bb1a622009-03-04 03:22:32480
isherman@chromium.org5c181552013-02-07 09:12:52481 registry->RegisterInt64Pref(prefs::kInstallDate, 0);
joi@chromium.orgb1de2c72013-02-06 02:45:47482 registry->RegisterInt64Pref(prefs::kUninstallMetricsPageLoadCount, 0);
483 registry->RegisterInt64Pref(prefs::kUninstallLaunchCount, 0);
joi@chromium.orgb1de2c72013-02-06 02:45:47484 registry->RegisterInt64Pref(prefs::kUninstallMetricsUptimeSec, 0);
485 registry->RegisterInt64Pref(prefs::kUninstallLastLaunchTimeSec, 0);
486 registry->RegisterInt64Pref(prefs::kUninstallLastObservedRunTimeSec, 0);
initial.commit09911bf2008-07-26 23:55:29487}
488
jar@chromium.org541f77922009-02-23 21:14:38489// static
490void MetricsService::DiscardOldStabilityStats(PrefService* local_state) {
491 local_state->SetBoolean(prefs::kStabilityExitedCleanly, true);
rtenneti@chromium.org6d67ea0d2013-11-14 11:02:21492 local_state->SetInteger(prefs::kStabilityExecutionPhase, UNINITIALIZED_PHASE);
jar@chromium.orgc9abf242009-07-18 06:00:38493 local_state->SetBoolean(prefs::kStabilitySessionEndCompleted, true);
jar@chromium.org541f77922009-02-23 21:14:38494
495 local_state->SetInteger(prefs::kStabilityIncompleteSessionEndCount, 0);
496 local_state->SetInteger(prefs::kStabilityBreakpadRegistrationSuccess, 0);
497 local_state->SetInteger(prefs::kStabilityBreakpadRegistrationFail, 0);
498 local_state->SetInteger(prefs::kStabilityDebuggerPresent, 0);
499 local_state->SetInteger(prefs::kStabilityDebuggerNotPresent, 0);
500
501 local_state->SetInteger(prefs::kStabilityLaunchCount, 0);
502 local_state->SetInteger(prefs::kStabilityCrashCount, 0);
503
504 local_state->SetInteger(prefs::kStabilityPageLoadCount, 0);
505 local_state->SetInteger(prefs::kStabilityRendererCrashCount, 0);
506 local_state->SetInteger(prefs::kStabilityRendererHangCount, 0);
507
jar@chromium.org9165f742010-03-10 22:55:01508 local_state->SetInt64(prefs::kStabilityLaunchTimeSec, 0);
509 local_state->SetInt64(prefs::kStabilityLastTimestampSec, 0);
jar@chromium.org541f77922009-02-23 21:14:38510
511 local_state->ClearPref(prefs::kStabilityPluginStats);
jar@chromium.orgae155cb92009-06-19 06:10:37512
isherman@chromium.org5f3e1642013-05-05 03:37:34513 local_state->ClearPref(prefs::kMetricsInitialLogs);
514 local_state->ClearPref(prefs::kMetricsOngoingLogs);
jar@chromium.org541f77922009-02-23 21:14:38515}
516
initial.commit09911bf2008-07-26 23:55:29517MetricsService::MetricsService()
petersont@google.comd01b8732008-10-16 02:18:07518 : recording_active_(false),
519 reporting_active_(false),
stuartmorgan@chromium.org410938e02012-10-24 16:33:59520 test_mode_active_(false),
petersont@google.comd01b8732008-10-16 02:18:07521 state_(INITIALIZED),
asvitkine@chromium.org80a8f312013-12-16 18:00:30522 has_initial_stability_log_(false),
asvitkine@chromium.orge88be472e2013-04-26 22:36:36523 low_entropy_source_(kLowEntropySourceNotSet),
petersont@google.comd01b8732008-10-16 02:18:07524 idle_since_last_transmission_(false),
asvitkine@chromium.org80a8f312013-12-16 18:00:30525 session_id_(-1),
initial.commit09911bf2008-07-26 23:55:29526 next_window_id_(0),
tfarina@chromium.org9c009092013-05-01 03:14:09527 self_ptr_factory_(this),
528 state_saver_factory_(this),
stevet@chromium.orge5354322012-08-09 23:07:37529 waiting_for_asynchronous_reporting_step_(false),
vitalybuka@chromium.orga3079832013-10-24 20:29:36530 num_async_histogram_fetches_in_progress_(0),
stevet@chromium.orge5354322012-08-09 23:07:37531 entropy_source_returned_(LAST_ENTROPY_NONE) {
initial.commit09911bf2008-07-26 23:55:29532 DCHECK(IsSingleThreaded());
stuartmorgan@chromium.org7f7f1962011-04-20 15:58:16533
asvitkine@chromium.org80a8f312013-12-16 18:00:30534 log_manager_.set_log_serializer(new MetricsLogSerializer);
stuartmorgan@chromium.orgcac267c2011-09-29 15:18:10535 log_manager_.set_max_ongoing_log_store_size(kUploadLogAvoidRetransmitSize);
phajdan.jr@chromium.orgf4eaf7b92013-02-28 22:00:40536
537 BrowserChildProcessObserver::Add(this);
initial.commit09911bf2008-07-26 23:55:29538}
539
540MetricsService::~MetricsService() {
stuartmorgan@chromium.org410938e02012-10-24 16:33:59541 DisableRecording();
phajdan.jr@chromium.orgf4eaf7b92013-02-28 22:00:40542
543 BrowserChildProcessObserver::Remove(this);
initial.commit09911bf2008-07-26 23:55:29544}
545
asvitkine@chromium.org80a8f312013-12-16 18:00:30546void MetricsService::InitializeMetricsRecordingState(
547 ReportingState reporting_state) {
548 InitializeMetricsState(reporting_state);
549
550 base::Closure callback = base::Bind(&MetricsService::StartScheduledUpload,
551 self_ptr_factory_.GetWeakPtr());
552 scheduler_.reset(new MetricsReportingScheduler(callback));
553}
554
petersont@google.comd01b8732008-10-16 02:18:07555void MetricsService::Start() {
stuartmorgan@chromium.orgb1c8dc02011-04-13 18:32:04556 HandleIdleSinceLastTransmission(false);
stuartmorgan@chromium.org410938e02012-10-24 16:33:59557 EnableRecording();
558 EnableReporting();
petersont@google.comd01b8732008-10-16 02:18:07559}
560
stuartmorgan@chromium.org410938e02012-10-24 16:33:59561void MetricsService::StartRecordingForTests() {
562 test_mode_active_ = true;
563 EnableRecording();
564 DisableReporting();
petersont@google.comd01b8732008-10-16 02:18:07565}
566
567void MetricsService::Stop() {
stuartmorgan@chromium.orgb1c8dc02011-04-13 18:32:04568 HandleIdleSinceLastTransmission(false);
stuartmorgan@chromium.org410938e02012-10-24 16:33:59569 DisableReporting();
570 DisableRecording();
571}
572
573void MetricsService::EnableReporting() {
574 if (reporting_active_)
575 return;
576 reporting_active_ = true;
577 StartSchedulerIfNecessary();
578}
579
580void MetricsService::DisableReporting() {
581 reporting_active_ = false;
petersont@google.comd01b8732008-10-16 02:18:07582}
583
joi@chromium.orgedafd4c2011-05-10 17:18:53584std::string MetricsService::GetClientId() {
585 return client_id_;
586}
587
asvitkine@chromium.org20f999b52012-08-24 22:32:59588scoped_ptr<const base::FieldTrial::EntropyProvider>
asvitkine@chromium.org80a8f312013-12-16 18:00:30589MetricsService::CreateEntropyProvider(ReportingState reporting_state) {
stevet@chromium.org29d81ee02012-05-25 05:45:42590 // For metrics reporting-enabled users, we combine the client ID and low
591 // entropy source to get the final entropy source. Otherwise, only use the low
592 // entropy source.
593 // This has two useful properties:
stevet@chromium.orge61003a2012-05-24 17:03:19594 // 1) It makes the entropy source less identifiable for parties that do not
595 // know the low entropy source.
596 // 2) It makes the final entropy source resettable.
asvitkine@chromium.orgd0a82c82013-06-13 16:31:18597 const int low_entropy_source_value = GetLowEntropySource();
598 UMA_HISTOGRAM_SPARSE_SLOWLY("UMA.LowEntropySourceValue",
599 low_entropy_source_value);
asvitkine@chromium.org80a8f312013-12-16 18:00:30600 if (reporting_state == REPORTING_ENABLED) {
stevet@chromium.org5fbffb72012-08-18 01:59:18601 if (entropy_source_returned_ == LAST_ENTROPY_NONE)
602 entropy_source_returned_ = LAST_ENTROPY_HIGH;
603 DCHECK_EQ(LAST_ENTROPY_HIGH, entropy_source_returned_);
asvitkine@chromium.org20f999b52012-08-24 22:32:59604 const std::string high_entropy_source =
asvitkine@chromium.orgd0a82c82013-06-13 16:31:18605 client_id_ + base::IntToString(low_entropy_source_value);
asvitkine@chromium.org20f999b52012-08-24 22:32:59606 return scoped_ptr<const base::FieldTrial::EntropyProvider>(
607 new metrics::SHA1EntropyProvider(high_entropy_source));
stevet@chromium.orge5354322012-08-09 23:07:37608 }
asvitkine@chromium.org20f999b52012-08-24 22:32:59609
stevet@chromium.org5fbffb72012-08-18 01:59:18610 if (entropy_source_returned_ == LAST_ENTROPY_NONE)
611 entropy_source_returned_ = LAST_ENTROPY_LOW;
612 DCHECK_EQ(LAST_ENTROPY_LOW, entropy_source_returned_);
asvitkine@chromium.org9d7c4a82013-05-07 12:10:49613
614#if defined(OS_ANDROID) || defined(OS_IOS)
615 return scoped_ptr<const base::FieldTrial::EntropyProvider>(
616 new metrics::CachingPermutedEntropyProvider(
617 g_browser_process->local_state(),
asvitkine@chromium.orgd0a82c82013-06-13 16:31:18618 low_entropy_source_value,
asvitkine@chromium.org9d7c4a82013-05-07 12:10:49619 kMaxLowEntropySize));
620#else
asvitkine@chromium.org20f999b52012-08-24 22:32:59621 return scoped_ptr<const base::FieldTrial::EntropyProvider>(
asvitkine@chromium.orgd0a82c82013-06-13 16:31:18622 new metrics::PermutedEntropyProvider(low_entropy_source_value,
asvitkine@chromium.org20f999b52012-08-24 22:32:59623 kMaxLowEntropySize));
asvitkine@chromium.org9d7c4a82013-05-07 12:10:49624#endif
stevet@chromium.orge61003a2012-05-24 17:03:19625}
626
jam@chromium.org5cbeeef72012-02-08 02:05:18627void MetricsService::ForceClientIdCreation() {
628 if (!client_id_.empty())
629 return;
630 PrefService* pref = g_browser_process->local_state();
631 client_id_ = pref->GetString(prefs::kMetricsClientID);
632 if (!client_id_.empty())
633 return;
634
635 client_id_ = GenerateClientID();
636 pref->SetString(prefs::kMetricsClientID, client_id_);
637
638 // Might as well make a note of how long this ID has existed
639 pref->SetString(prefs::kMetricsClientIDTimestamp,
640 base::Int64ToString(Time::Now().ToTimeT()));
641}
642
stuartmorgan@chromium.org410938e02012-10-24 16:33:59643void MetricsService::EnableRecording() {
initial.commit09911bf2008-07-26 23:55:29644 DCHECK(IsSingleThreaded());
645
stuartmorgan@chromium.org410938e02012-10-24 16:33:59646 if (recording_active_)
initial.commit09911bf2008-07-26 23:55:29647 return;
stuartmorgan@chromium.org410938e02012-10-24 16:33:59648 recording_active_ = true;
initial.commit09911bf2008-07-26 23:55:29649
stuartmorgan@chromium.org410938e02012-10-24 16:33:59650 ForceClientIdCreation();
rsesek@chromium.org264c0acac2013-10-01 13:33:30651 crash_keys::SetClientID(client_id_);
stuartmorgan@chromium.org410938e02012-10-24 16:33:59652 if (!log_manager_.current_log())
653 OpenNewLog();
pkasting@chromium.org005ef3e2009-05-22 20:55:46654
stuartmorgan@chromium.org410938e02012-10-24 16:33:59655 SetUpNotifications(&registrar_, this);
phajdan.jr@chromium.orgdd98f392013-02-04 13:03:22656 content::RemoveActionCallback(action_callback_);
657 action_callback_ = base::Bind(&MetricsService::OnUserAction,
658 base::Unretained(this));
659 content::AddActionCallback(action_callback_);
stuartmorgan@chromium.org410938e02012-10-24 16:33:59660}
661
662void MetricsService::DisableRecording() {
663 DCHECK(IsSingleThreaded());
664
665 if (!recording_active_)
666 return;
667 recording_active_ = false;
668
phajdan.jr@chromium.orgdd98f392013-02-04 13:03:22669 content::RemoveActionCallback(action_callback_);
stuartmorgan@chromium.org410938e02012-10-24 16:33:59670 registrar_.RemoveAll();
671 PushPendingLogsToPersistentStorage();
672 DCHECK(!log_manager_.has_staged_log());
initial.commit09911bf2008-07-26 23:55:29673}
674
petersont@google.comd01b8732008-10-16 02:18:07675bool MetricsService::recording_active() const {
initial.commit09911bf2008-07-26 23:55:29676 DCHECK(IsSingleThreaded());
petersont@google.comd01b8732008-10-16 02:18:07677 return recording_active_;
initial.commit09911bf2008-07-26 23:55:29678}
679
petersont@google.comd01b8732008-10-16 02:18:07680bool MetricsService::reporting_active() const {
681 DCHECK(IsSingleThreaded());
682 return reporting_active_;
initial.commit09911bf2008-07-26 23:55:29683}
684
rtenneti@chromium.org87ef9ea2011-02-26 03:15:15685// static
jam@chromium.org6c2381d2011-10-19 02:52:53686void MetricsService::SetUpNotifications(
687 content::NotificationRegistrar* registrar,
688 content::NotificationObserver* observer) {
689 registrar->Add(observer, chrome::NOTIFICATION_BROWSER_OPENED,
jam@chromium.orgad50def52011-10-19 23:17:07690 content::NotificationService::AllBrowserContextsAndSources());
jam@chromium.org6c2381d2011-10-19 02:52:53691 registrar->Add(observer, chrome::NOTIFICATION_BROWSER_CLOSED,
jam@chromium.orgad50def52011-10-19 23:17:07692 content::NotificationService::AllSources());
avi@chromium.org884033e2012-04-16 19:38:42693 registrar->Add(observer, chrome::NOTIFICATION_TAB_PARENTED,
jam@chromium.orgad50def52011-10-19 23:17:07694 content::NotificationService::AllSources());
avi@chromium.org884033e2012-04-16 19:38:42695 registrar->Add(observer, chrome::NOTIFICATION_TAB_CLOSING,
jam@chromium.orgad50def52011-10-19 23:17:07696 content::NotificationService::AllSources());
jam@chromium.org6c2381d2011-10-19 02:52:53697 registrar->Add(observer, content::NOTIFICATION_LOAD_START,
jam@chromium.orgad50def52011-10-19 23:17:07698 content::NotificationService::AllSources());
jam@chromium.org6c2381d2011-10-19 02:52:53699 registrar->Add(observer, content::NOTIFICATION_LOAD_STOP,
jam@chromium.orgad50def52011-10-19 23:17:07700 content::NotificationService::AllSources());
jam@chromium.org6c2381d2011-10-19 02:52:53701 registrar->Add(observer, content::NOTIFICATION_RENDERER_PROCESS_CLOSED,
jam@chromium.orgad50def52011-10-19 23:17:07702 content::NotificationService::AllSources());
avi@chromium.org42d8d7582013-11-09 01:24:38703 registrar->Add(observer, content::NOTIFICATION_RENDER_WIDGET_HOST_HANG,
jam@chromium.orgad50def52011-10-19 23:17:07704 content::NotificationService::AllSources());
jam@chromium.org6c2381d2011-10-19 02:52:53705 registrar->Add(observer, chrome::NOTIFICATION_OMNIBOX_OPENED_URL,
jam@chromium.orgad50def52011-10-19 23:17:07706 content::NotificationService::AllSources());
rtenneti@chromium.org87ef9ea2011-02-26 03:15:15707}
708
phajdan.jr@chromium.orgf4eaf7b92013-02-28 22:00:40709void MetricsService::BrowserChildProcessHostConnected(
710 const content::ChildProcessData& data) {
711 GetChildProcessStats(data).process_launches++;
712}
713
714void MetricsService::BrowserChildProcessCrashed(
715 const content::ChildProcessData& data) {
716 GetChildProcessStats(data).process_crashes++;
717 // Exclude plugin crashes from the count below because we report them via
718 // a separate UMA metric.
jam@chromium.orgf3b357692013-03-22 05:16:13719 if (!IsPluginProcess(data.process_type))
phajdan.jr@chromium.orgf4eaf7b92013-02-28 22:00:40720 IncrementPrefValue(prefs::kStabilityChildProcessCrashCount);
721}
722
723void MetricsService::BrowserChildProcessInstanceCreated(
724 const content::ChildProcessData& data) {
725 GetChildProcessStats(data).instances++;
726}
727
ananta@chromium.org432115822011-07-10 15:52:27728void MetricsService::Observe(int type,
jam@chromium.org6c2381d2011-10-19 02:52:53729 const content::NotificationSource& source,
730 const content::NotificationDetails& details) {
stuartmorgan@chromium.orgcac267c2011-09-29 15:18:10731 DCHECK(log_manager_.current_log());
initial.commit09911bf2008-07-26 23:55:29732 DCHECK(IsSingleThreaded());
733
phajdan.jr@chromium.orgdd98f392013-02-04 13:03:22734 if (!CanLogNotification())
initial.commit09911bf2008-07-26 23:55:29735 return;
736
ananta@chromium.org432115822011-07-10 15:52:27737 switch (type) {
ananta@chromium.org432115822011-07-10 15:52:27738 case chrome::NOTIFICATION_BROWSER_OPENED:
isherman@chromium.org46a0efc2013-07-17 15:40:47739 case chrome::NOTIFICATION_BROWSER_CLOSED:
740 case chrome::NOTIFICATION_TAB_PARENTED:
741 case chrome::NOTIFICATION_TAB_CLOSING:
ananta@chromium.org432115822011-07-10 15:52:27742 case content::NOTIFICATION_LOAD_STOP:
isherman@chromium.org46a0efc2013-07-17 15:40:47743 // These notifications are currently used only to break out of idle mode.
initial.commit09911bf2008-07-26 23:55:29744 break;
745
rlp@chromium.org752a5262013-06-23 14:53:42746 case content::NOTIFICATION_LOAD_START: {
747 content::NavigationController* controller =
748 content::Source<content::NavigationController>(source).ptr();
749 content::WebContents* web_contents = controller->GetWebContents();
750 LogLoadStarted(web_contents);
initial.commit09911bf2008-07-26 23:55:29751 break;
rlp@chromium.org752a5262013-06-23 14:53:42752 }
initial.commit09911bf2008-07-26 23:55:29753
ananta@chromium.org432115822011-07-10 15:52:27754 case content::NOTIFICATION_RENDERER_PROCESS_CLOSED: {
ananta@chromium.orgf3b1a082011-11-18 00:34:30755 content::RenderProcessHost::RendererClosedDetails* process_details =
756 content::Details<
757 content::RenderProcessHost::RendererClosedDetails>(
758 details).ptr();
759 content::RenderProcessHost* host =
760 content::Source<content::RenderProcessHost>(source).ptr();
jar@chromium.orgc3721482012-03-23 16:21:48761 LogRendererCrash(
jam@chromium.orgf1675202012-07-09 15:18:00762 host, process_details->status, process_details->exit_code);
asargent@chromium.org1f085622009-12-04 05:33:45763 }
initial.commit09911bf2008-07-26 23:55:29764 break;
765
avi@chromium.org42d8d7582013-11-09 01:24:38766 case content::NOTIFICATION_RENDER_WIDGET_HOST_HANG:
initial.commit09911bf2008-07-26 23:55:29767 LogRendererHang();
768 break;
769
ananta@chromium.org432115822011-07-10 15:52:27770 case chrome::NOTIFICATION_OMNIBOX_OPENED_URL: {
isherman@chromium.org279703f2012-01-20 22:23:26771 MetricsLog* current_log =
772 static_cast<MetricsLog*>(log_manager_.current_log());
ananta@chromium.org1226abb2010-06-10 18:01:28773 DCHECK(current_log);
774 current_log->RecordOmniboxOpenedURL(
tfarina@chromium.org0fafc8d2013-06-01 00:09:50775 *content::Details<OmniboxLog>(details).ptr());
initial.commit09911bf2008-07-26 23:55:29776 break;
ananta@chromium.org1226abb2010-06-10 18:01:28777 }
initial.commit09911bf2008-07-26 23:55:29778
initial.commit09911bf2008-07-26 23:55:29779 default:
jar@chromium.orga063c102010-07-22 22:20:19780 NOTREACHED();
initial.commit09911bf2008-07-26 23:55:29781 break;
782 }
petersont@google.comd01b8732008-10-16 02:18:07783
784 HandleIdleSinceLastTransmission(false);
petersont@google.comd01b8732008-10-16 02:18:07785}
786
787void MetricsService::HandleIdleSinceLastTransmission(bool in_idle) {
788 // If there wasn't a lot of action, maybe the computer was asleep, in which
789 // case, the log transmissions should have stopped. Here we start them up
790 // again.
pkasting@chromium.orgcac78842008-11-27 01:02:20791 if (!in_idle && idle_since_last_transmission_)
stuartmorgan@chromium.org7f7f1962011-04-20 15:58:16792 StartSchedulerIfNecessary();
pkasting@chromium.orgcac78842008-11-27 01:02:20793 idle_since_last_transmission_ = in_idle;
initial.commit09911bf2008-07-26 23:55:29794}
795
initial.commit09911bf2008-07-26 23:55:29796void MetricsService::RecordStartOfSessionEnd() {
stuartmorgan@chromium.org466f3c12011-03-23 21:20:38797 LogCleanShutdown();
initial.commit09911bf2008-07-26 23:55:29798 RecordBooleanPrefValue(prefs::kStabilitySessionEndCompleted, false);
799}
800
801void MetricsService::RecordCompletedSessionEnd() {
stuartmorgan@chromium.org466f3c12011-03-23 21:20:38802 LogCleanShutdown();
initial.commit09911bf2008-07-26 23:55:29803 RecordBooleanPrefValue(prefs::kStabilitySessionEndCompleted, true);
804}
805
stuartmorgan@chromium.org410938e02012-10-24 16:33:59806#if defined(OS_ANDROID) || defined(OS_IOS)
dfalcantara@chromium.org117fbdf22012-06-26 18:36:39807void MetricsService::OnAppEnterBackground() {
808 scheduler_->Stop();
809
nileshagrawal@chromium.org84c384e2013-03-01 23:20:19810 MarkAppCleanShutdownAndCommit();
dfalcantara@chromium.org117fbdf22012-06-26 18:36:39811
812 // At this point, there's no way of knowing when the process will be
813 // killed, so this has to be treated similar to a shutdown, closing and
814 // persisting all logs. Unlinke a shutdown, the state is primed to be ready
815 // to continue logging and uploading if the process does return.
asvitkine@chromium.org80a8f312013-12-16 18:00:30816 if (recording_active() && state_ >= SENDING_INITIAL_STABILITY_LOG) {
dfalcantara@chromium.org117fbdf22012-06-26 18:36:39817 PushPendingLogsToPersistentStorage();
stuartmorgan@chromium.org410938e02012-10-24 16:33:59818 // Persisting logs closes the current log, so start recording a new log
819 // immediately to capture any background work that might be done before the
820 // process is killed.
821 OpenNewLog();
dfalcantara@chromium.org117fbdf22012-06-26 18:36:39822 }
dfalcantara@chromium.org117fbdf22012-06-26 18:36:39823}
824
825void MetricsService::OnAppEnterForeground() {
826 PrefService* pref = g_browser_process->local_state();
827 pref->SetBoolean(prefs::kStabilityExitedCleanly, false);
dfalcantara@chromium.org117fbdf22012-06-26 18:36:39828
829 StartSchedulerIfNecessary();
830}
nileshagrawal@chromium.org84c384e2013-03-01 23:20:19831#else
832void MetricsService::LogNeedForCleanShutdown() {
833 PrefService* pref = g_browser_process->local_state();
834 pref->SetBoolean(prefs::kStabilityExitedCleanly, false);
nileshagrawal@chromium.org84c384e2013-03-01 23:20:19835 // Redundant setting to be sure we call for a clean shutdown.
836 clean_shutdown_status_ = NEED_TO_SHUTDOWN;
837}
838#endif // defined(OS_ANDROID) || defined(OS_IOS)
dfalcantara@chromium.org117fbdf22012-06-26 18:36:39839
rtenneti@chromium.org6d67ea0d2013-11-14 11:02:21840// static
841void MetricsService::SetExecutionPhase(ExecutionPhase execution_phase) {
842 execution_phase_ = execution_phase;
843 PrefService* pref = g_browser_process->local_state();
844 pref->SetInteger(prefs::kStabilityExecutionPhase, execution_phase_);
845}
846
stuartmorgan@chromium.org7f7f1962011-04-20 15:58:16847void MetricsService::RecordBreakpadRegistration(bool success) {
jar@google.com68475e602008-08-22 03:21:15848 if (!success)
cpu@google.come73c01972008-08-13 00:18:24849 IncrementPrefValue(prefs::kStabilityBreakpadRegistrationFail);
850 else
851 IncrementPrefValue(prefs::kStabilityBreakpadRegistrationSuccess);
852}
853
854void MetricsService::RecordBreakpadHasDebugger(bool has_debugger) {
855 if (!has_debugger)
856 IncrementPrefValue(prefs::kStabilityDebuggerNotPresent);
857 else
jar@google.com68475e602008-08-22 03:21:15858 IncrementPrefValue(prefs::kStabilityDebuggerPresent);
cpu@google.come73c01972008-08-13 00:18:24859}
860
rogerm@chromium.org261ab7c2013-08-19 15:04:58861#if defined(OS_WIN)
862void MetricsService::CountBrowserCrashDumpAttempts() {
rogerm@chromium.orga5e0fe5e2013-09-16 06:15:45863 // Open the registry key for iteration.
rogerm@chromium.org261ab7c2013-08-19 15:04:58864 base::win::RegKey regkey;
865 if (regkey.Open(HKEY_CURRENT_USER,
rogerm@chromium.orga5e0fe5e2013-09-16 06:15:45866 chrome::kBrowserCrashDumpAttemptsRegistryPath,
rogerm@chromium.org261ab7c2013-08-19 15:04:58867 KEY_ALL_ACCESS) != ERROR_SUCCESS) {
868 return;
869 }
870
rogerm@chromium.orga5e0fe5e2013-09-16 06:15:45871 // The values we're interested in counting are all prefixed with the version.
872 base::string16 chrome_version(base::ASCIIToUTF16(chrome::kChromeVersion));
873
874 // Track a list of values to delete. We don't modify the registry key while
875 // we're iterating over its values.
876 typedef std::vector<base::string16> StringVector;
877 StringVector to_delete;
878
879 // Iterate over the values in the key counting dumps with and without crashes.
880 // We directly walk the values instead of using RegistryValueIterator in order
881 // to read all of the values as DWORDS instead of strings.
882 base::string16 name;
883 DWORD value = 0;
884 int dumps_with_crash = 0;
885 int dumps_with_no_crash = 0;
rogerm@chromium.org261ab7c2013-08-19 15:04:58886 for (int i = regkey.GetValueCount() - 1; i >= 0; --i) {
rogerm@chromium.orga5e0fe5e2013-09-16 06:15:45887 if (regkey.GetValueNameAt(i, &name) == ERROR_SUCCESS &&
888 StartsWith(name, chrome_version, false) &&
889 regkey.ReadValueDW(name.c_str(), &value) == ERROR_SUCCESS) {
890 to_delete.push_back(name);
891 if (value == 0)
892 ++dumps_with_no_crash;
893 else
894 ++dumps_with_crash;
rogerm@chromium.org261ab7c2013-08-19 15:04:58895 }
896 }
rogerm@chromium.orga5e0fe5e2013-09-16 06:15:45897
898 // Delete the registry keys we've just counted.
899 for (StringVector::iterator i = to_delete.begin(); i != to_delete.end(); ++i)
900 regkey.DeleteValue(i->c_str());
901
902 // Capture the histogram samples.
903 if (dumps_with_crash != 0)
904 UMA_HISTOGRAM_COUNTS("Chrome.BrowserDumpsWithCrash", dumps_with_crash);
905 if (dumps_with_no_crash != 0)
906 UMA_HISTOGRAM_COUNTS("Chrome.BrowserDumpsWithNoCrash", dumps_with_no_crash);
907 int total_dumps = dumps_with_crash + dumps_with_no_crash;
908 if (total_dumps != 0)
909 UMA_HISTOGRAM_COUNTS("Chrome.BrowserCrashDumpAttempts", total_dumps);
rogerm@chromium.org261ab7c2013-08-19 15:04:58910}
911#endif // defined(OS_WIN)
912
initial.commit09911bf2008-07-26 23:55:29913//------------------------------------------------------------------------------
914// private methods
915//------------------------------------------------------------------------------
916
917
918//------------------------------------------------------------------------------
919// Initialization methods
920
asvitkine@chromium.org80a8f312013-12-16 18:00:30921void MetricsService::InitializeMetricsState(ReportingState reporting_state) {
kuchhal@chromium.org79bf0b72009-04-27 21:30:55922#if defined(OS_POSIX)
simonjam@chromium.orgb4a72d842012-03-22 20:09:09923 network_stats_server_ = chrome_common_net::kEchoTestServerLocation;
924 http_pipelining_test_server_ = chrome_common_net::kPipelineTestServerBaseUrl;
kuchhal@chromium.org79bf0b72009-04-27 21:30:55925#else
926 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
rtenneti@chromium.orgd67d1052011-06-09 05:11:41927 network_stats_server_ = dist->GetNetworkStatsServer();
simonjam@chromium.orgadbb3762012-03-09 22:20:08928 http_pipelining_test_server_ = dist->GetHttpPipeliningTestServer();
kuchhal@chromium.org79bf0b72009-04-27 21:30:55929#endif
930
initial.commit09911bf2008-07-26 23:55:29931 PrefService* pref = g_browser_process->local_state();
932 DCHECK(pref);
933
asvitkine@chromium.org80a8f312013-12-16 18:00:30934 // TODO(asvitkine): Kill this logic when SendSeparateInitialStabilityLog() is
935 // is made the default behavior.
jar@chromium.org225c50842010-01-19 21:19:13936 if ((pref->GetInt64(prefs::kStabilityStatsBuildTime)
937 != MetricsLog::GetBuildTime()) ||
estade@chromium.orgddd231e2010-06-29 20:35:19938 (pref->GetString(prefs::kStabilityStatsVersion)
jar@chromium.org225c50842010-01-19 21:19:13939 != MetricsLog::GetVersionString())) {
jar@chromium.org541f77922009-02-23 21:14:38940 // This is a new version, so we don't want to confuse the stats about the
941 // old version with info that we upload.
942 DiscardOldStabilityStats(pref);
943 pref->SetString(prefs::kStabilityStatsVersion,
estade@chromium.orgddd231e2010-06-29 20:35:19944 MetricsLog::GetVersionString());
jar@chromium.org225c50842010-01-19 21:19:13945 pref->SetInt64(prefs::kStabilityStatsBuildTime,
946 MetricsLog::GetBuildTime());
jar@chromium.org541f77922009-02-23 21:14:38947 }
948
initial.commit09911bf2008-07-26 23:55:29949 session_id_ = pref->GetInteger(prefs::kMetricsSessionID);
initial.commit09911bf2008-07-26 23:55:29950
cpu@google.come73c01972008-08-13 00:18:24951 if (!pref->GetBoolean(prefs::kStabilityExitedCleanly)) {
952 IncrementPrefValue(prefs::kStabilityCrashCount);
jar@chromium.orgc0c55e92011-09-10 18:47:30953 // Reset flag, and wait until we call LogNeedForCleanShutdown() before
954 // monitoring.
955 pref->SetBoolean(prefs::kStabilityExitedCleanly, true);
rtenneti@chromium.org6a6d0d12013-10-28 15:58:19956
957 // TODO(rtenneti): On windows, consider saving/getting execution_phase from
958 // the registry.
959 int execution_phase = pref->GetInteger(prefs::kStabilityExecutionPhase);
rtenneti@chromium.org6d67ea0d2013-11-14 11:02:21960 UMA_HISTOGRAM_SPARSE_SLOWLY("Chrome.Browser.CrashedExecutionPhase",
rtenneti@chromium.org6a6d0d12013-10-28 15:58:19961 execution_phase);
asvitkine@chromium.org80a8f312013-12-16 18:00:30962
963 // If the previous session didn't exit cleanly, then prepare an initial
964 // stability log if UMA is enabled.
965 bool reporting_will_be_enabled = (reporting_state == REPORTING_ENABLED);
966 if (reporting_will_be_enabled && SendSeparateInitialStabilityLog())
967 PrepareInitialStabilityLog();
initial.commit09911bf2008-07-26 23:55:29968 }
asvitkine@chromium.org80a8f312013-12-16 18:00:30969
970 // Update session ID.
971 ++session_id_;
972 pref->SetInteger(prefs::kMetricsSessionID, session_id_);
973
974 // Stability bookkeeping
975 IncrementPrefValue(prefs::kStabilityLaunchCount);
976
rtenneti@chromium.org6d67ea0d2013-11-14 11:02:21977 DCHECK_EQ(UNINITIALIZED_PHASE, execution_phase_);
978 SetExecutionPhase(START_METRICS_RECORDING);
cpu@google.come73c01972008-08-13 00:18:24979
rogerm@chromium.org261ab7c2013-08-19 15:04:58980#if defined(OS_WIN)
981 CountBrowserCrashDumpAttempts();
982#endif // defined(OS_WIN)
983
cpu@google.come73c01972008-08-13 00:18:24984 if (!pref->GetBoolean(prefs::kStabilitySessionEndCompleted)) {
985 IncrementPrefValue(prefs::kStabilityIncompleteSessionEndCount);
jar@chromium.orgc9abf242009-07-18 06:00:38986 // This is marked false when we get a WM_ENDSESSION.
987 pref->SetBoolean(prefs::kStabilitySessionEndCompleted, true);
initial.commit09911bf2008-07-26 23:55:29988 }
initial.commit09911bf2008-07-26 23:55:29989
jar@chromium.org9165f742010-03-10 22:55:01990 // Initialize uptime counters.
asvitkine@chromium.orgc68a2b9b2013-10-09 18:16:36991 const base::TimeDelta startup_uptime = GetIncrementalUptime(pref);
992 DCHECK_EQ(0, startup_uptime.InMicroseconds());
jar@chromium.org9165f742010-03-10 22:55:01993 // For backwards compatibility, leave this intact in case Omaha is checking
994 // them. prefs::kStabilityLastTimestampSec may also be useless now.
995 // TODO(jar): Delete these if they have no uses.
robertshield@google.com0bb1a622009-03-04 03:22:32996 pref->SetInt64(prefs::kStabilityLaunchTimeSec, Time::Now().ToTimeT());
997
998 // Bookkeeping for the uninstall metrics.
999 IncrementLongPrefsValue(prefs::kUninstallLaunchCount);
initial.commit09911bf2008-07-26 23:55:291000
jar@chromium.org92745242009-06-12 16:52:211001 // Get stats on use of command line.
1002 const CommandLine* command_line(CommandLine::ForCurrentProcess());
1003 size_t common_commands = 0;
1004 if (command_line->HasSwitch(switches::kUserDataDir)) {
1005 ++common_commands;
1006 UMA_HISTOGRAM_COUNTS_100("Chrome.CommandLineDatDirCount", 1);
1007 }
1008
1009 if (command_line->HasSwitch(switches::kApp)) {
1010 ++common_commands;
1011 UMA_HISTOGRAM_COUNTS_100("Chrome.CommandLineAppModeCount", 1);
1012 }
1013
msw@chromium.org62b4e522011-07-13 21:46:321014 size_t switch_count = command_line->GetSwitches().size();
1015 UMA_HISTOGRAM_COUNTS_100("Chrome.CommandLineFlagCount", switch_count);
jar@chromium.org92745242009-06-12 16:52:211016 UMA_HISTOGRAM_COUNTS_100("Chrome.CommandLineUncommonFlagCount",
msw@chromium.org62b4e522011-07-13 21:46:321017 switch_count - common_commands);
jar@chromium.org92745242009-06-12 16:52:211018
initial.commit09911bf2008-07-26 23:55:291019 // Kick off the process of saving the state (so the uptime numbers keep
1020 // getting updated) every n minutes.
1021 ScheduleNextStateSave();
1022}
1023
dpolukhin@chromium.orgc94d7382012-02-28 08:43:401024// static
rsesek@chromium.orgd33e7cc2011-09-23 01:43:561025void MetricsService::InitTaskGetHardwareClass(
dpolukhin@chromium.orgc94d7382012-02-28 08:43:401026 base::WeakPtr<MetricsService> self,
rsesek@chromium.orgd33e7cc2011-09-23 01:43:561027 base::MessageLoopProxy* target_loop) {
rsesek@chromium.orgd33e7cc2011-09-23 01:43:561028 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
1029
1030 std::string hardware_class;
1031#if defined(OS_CHROMEOS)
1032 chromeos::system::StatisticsProvider::GetInstance()->GetMachineStatistic(
1033 "hardware_class", &hardware_class);
1034#endif // OS_CHROMEOS
1035
1036 target_loop->PostTask(FROM_HERE,
1037 base::Bind(&MetricsService::OnInitTaskGotHardwareClass,
dpolukhin@chromium.orgc94d7382012-02-28 08:43:401038 self, hardware_class));
rsesek@chromium.orgd33e7cc2011-09-23 01:43:561039}
1040
1041void MetricsService::OnInitTaskGotHardwareClass(
1042 const std::string& hardware_class) {
isherman@chromium.orged0fd002012-04-25 23:10:341043 DCHECK_EQ(INIT_TASK_SCHEDULED, state_);
zelidrag@chromium.org85ed9d42010-06-08 22:37:441044 hardware_class_ = hardware_class;
rsesek@chromium.orgd33e7cc2011-09-23 01:43:561045
nileshagrawal@chromium.orgebd71962012-12-20 02:56:551046#if defined(ENABLE_PLUGINS)
rsesek@chromium.orgd33e7cc2011-09-23 01:43:561047 // Start the next part of the init task: loading plugin information.
1048 PluginService::GetInstance()->GetPlugins(
1049 base::Bind(&MetricsService::OnInitTaskGotPluginInfo,
dpolukhin@chromium.orgc94d7382012-02-28 08:43:401050 self_ptr_factory_.GetWeakPtr()));
nileshagrawal@chromium.orgebd71962012-12-20 02:56:551051#else
jam@chromium.orgd7bd3e52013-07-21 04:29:201052 std::vector<content::WebPluginInfo> plugin_list_empty;
nileshagrawal@chromium.orgebd71962012-12-20 02:56:551053 OnInitTaskGotPluginInfo(plugin_list_empty);
1054#endif // defined(ENABLE_PLUGINS)
rsesek@chromium.orgd33e7cc2011-09-23 01:43:561055}
1056
1057void MetricsService::OnInitTaskGotPluginInfo(
jam@chromium.orgd7bd3e52013-07-21 04:29:201058 const std::vector<content::WebPluginInfo>& plugins) {
isherman@chromium.orged0fd002012-04-25 23:10:341059 DCHECK_EQ(INIT_TASK_SCHEDULED, state_);
jam@chromium.org35fa6a22009-08-15 00:04:011060 plugins_ = plugins;
rsesek@chromium.orgd33e7cc2011-09-23 01:43:561061
ryanmyers@chromium.org197c0772012-05-14 23:50:511062 // Schedules a task on a blocking pool thread to gather Google Update
1063 // statistics (requires Registry reads).
1064 BrowserThread::PostBlockingPoolTask(
1065 FROM_HERE,
1066 base::Bind(&MetricsService::InitTaskGetGoogleUpdateData,
1067 self_ptr_factory_.GetWeakPtr(),
xhwang@chromium.orgb3a25092013-05-28 22:08:161068 base::MessageLoop::current()->message_loop_proxy()));
ryanmyers@chromium.org197c0772012-05-14 23:50:511069}
1070
1071// static
1072void MetricsService::InitTaskGetGoogleUpdateData(
1073 base::WeakPtr<MetricsService> self,
1074 base::MessageLoopProxy* target_loop) {
1075 GoogleUpdateMetrics google_update_metrics;
1076
1077#if defined(OS_WIN) && defined(GOOGLE_CHROME_BUILD)
1078 const bool system_install = GoogleUpdateSettings::IsSystemInstall();
1079
1080 google_update_metrics.is_system_install = system_install;
1081 google_update_metrics.last_started_au =
1082 GoogleUpdateSettings::GetGoogleUpdateLastStartedAU(system_install);
1083 google_update_metrics.last_checked =
1084 GoogleUpdateSettings::GetGoogleUpdateLastChecked(system_install);
1085 GoogleUpdateSettings::GetUpdateDetailForGoogleUpdate(
1086 system_install,
1087 &google_update_metrics.google_update_data);
1088 GoogleUpdateSettings::GetUpdateDetail(
1089 system_install,
1090 &google_update_metrics.product_data);
1091#endif // defined(OS_WIN) && defined(GOOGLE_CHROME_BUILD)
1092
1093 target_loop->PostTask(FROM_HERE,
1094 base::Bind(&MetricsService::OnInitTaskGotGoogleUpdateData,
1095 self, google_update_metrics));
1096}
1097
1098void MetricsService::OnInitTaskGotGoogleUpdateData(
1099 const GoogleUpdateMetrics& google_update_metrics) {
1100 DCHECK_EQ(INIT_TASK_SCHEDULED, state_);
1101
1102 google_update_metrics_ = google_update_metrics;
1103
isherman@chromium.orged0fd002012-04-25 23:10:341104 // Start the next part of the init task: fetching performance data. This will
1105 // call into |FinishedReceivingProfilerData()| when the task completes.
1106 chrome_browser_metrics::TrackingSynchronizer::FetchProfilerDataAsynchronously(
1107 self_ptr_factory_.GetWeakPtr());
1108}
1109
phajdan.jr@chromium.orgdd98f392013-02-04 13:03:221110void MetricsService::OnUserAction(const std::string& action) {
1111 if (!CanLogNotification())
1112 return;
1113
1114 log_manager_.current_log()->RecordUserAction(action.c_str());
1115 HandleIdleSinceLastTransmission(false);
1116}
1117
isherman@chromium.orged0fd002012-04-25 23:10:341118void MetricsService::ReceivedProfilerData(
1119 const tracked_objects::ProcessDataSnapshot& process_data,
jam@chromium.orgf3b357692013-03-22 05:16:131120 int process_type) {
isherman@chromium.orged0fd002012-04-25 23:10:341121 DCHECK_EQ(INIT_TASK_SCHEDULED, state_);
1122
1123 // Upon the first callback, create the initial log so that we can immediately
1124 // save the profiler data.
asvitkine@chromium.org80a8f312013-12-16 18:00:301125 if (!initial_metrics_log_.get())
1126 initial_metrics_log_.reset(new MetricsLog(client_id_, session_id_));
isherman@chromium.orged0fd002012-04-25 23:10:341127
asvitkine@chromium.org80a8f312013-12-16 18:00:301128 initial_metrics_log_->RecordProfilerData(process_data, process_type);
isherman@chromium.orged0fd002012-04-25 23:10:341129}
1130
1131void MetricsService::FinishedReceivingProfilerData() {
1132 DCHECK_EQ(INIT_TASK_SCHEDULED, state_);
asvitkine@chromium.orgc68a2b9b2013-10-09 18:16:361133 state_ = INIT_TASK_DONE;
asvitkine@chromium.org70886cd2013-12-04 05:53:421134 scheduler_->InitTaskComplete();
asvitkine@chromium.orgc68a2b9b2013-10-09 18:16:361135}
1136
1137base::TimeDelta MetricsService::GetIncrementalUptime(PrefService* pref) {
1138 base::TimeTicks now = base::TimeTicks::Now();
1139 // If this is the first call, init |last_updated_time_|.
1140 if (last_updated_time_.is_null())
1141 last_updated_time_ = now;
1142 const base::TimeDelta incremental_time = now - last_updated_time_;
1143 last_updated_time_ = now;
1144
1145 const int64 incremental_time_secs = incremental_time.InSeconds();
1146 if (incremental_time_secs > 0) {
1147 int64 metrics_uptime = pref->GetInt64(prefs::kUninstallMetricsUptimeSec);
1148 metrics_uptime += incremental_time_secs;
1149 pref->SetInt64(prefs::kUninstallMetricsUptimeSec, metrics_uptime);
1150 }
1151
1152 return incremental_time;
initial.commit09911bf2008-07-26 23:55:291153}
1154
stevet@chromium.orge61003a2012-05-24 17:03:191155int MetricsService::GetLowEntropySource() {
1156 // Note that the default value for the low entropy source and the default pref
asvitkine@chromium.orge88be472e2013-04-26 22:36:361157 // value are both kLowEntropySourceNotSet, which is used to identify if the
1158 // value has been set or not.
1159 if (low_entropy_source_ != kLowEntropySourceNotSet)
stevet@chromium.orge61003a2012-05-24 17:03:191160 return low_entropy_source_;
1161
asvitkine@chromium.org9d7c4a82013-05-07 12:10:491162 PrefService* local_state = g_browser_process->local_state();
stevet@chromium.orge61003a2012-05-24 17:03:191163 const CommandLine* command_line(CommandLine::ForCurrentProcess());
1164 // Only try to load the value from prefs if the user did not request a reset.
1165 // Otherwise, skip to generating a new value.
stevet@chromium.org1862c0c2013-04-18 06:30:481166 if (!command_line->HasSwitch(switches::kResetVariationState)) {
asvitkine@chromium.org9556a892013-06-21 16:53:201167 int value = local_state->GetInteger(prefs::kMetricsLowEntropySource);
1168 // Old versions of the code would generate values in the range of [1, 8192],
1169 // before the range was switched to [0, 8191] and then to [0, 7999]. Map
1170 // 8192 to 0, so that the 0th bucket remains uniform, while re-generating
1171 // the low entropy source for old values in the [8000, 8191] range.
1172 if (value == 8192)
1173 value = 0;
1174 // If the value is outside the [0, kMaxLowEntropySize) range, re-generate
1175 // it below.
1176 if (value >= 0 && value < kMaxLowEntropySize) {
1177 low_entropy_source_ = value;
stevet@chromium.org0c906e92012-10-18 15:24:131178 UMA_HISTOGRAM_BOOLEAN("UMA.GeneratedLowEntropySource", false);
stevet@chromium.orge61003a2012-05-24 17:03:191179 return low_entropy_source_;
asvitkine@chromium.orge63a9ef2012-08-30 15:29:421180 }
stevet@chromium.orge61003a2012-05-24 17:03:191181 }
1182
stevet@chromium.org0c906e92012-10-18 15:24:131183 UMA_HISTOGRAM_BOOLEAN("UMA.GeneratedLowEntropySource", true);
stevet@chromium.orge61003a2012-05-24 17:03:191184 low_entropy_source_ = GenerateLowEntropySource();
asvitkine@chromium.org9d7c4a82013-05-07 12:10:491185 local_state->SetInteger(prefs::kMetricsLowEntropySource, low_entropy_source_);
1186 metrics::CachingPermutedEntropyProvider::ClearCache(local_state);
stevet@chromium.orge61003a2012-05-24 17:03:191187
1188 return low_entropy_source_;
1189}
1190
1191// static
initial.commit09911bf2008-07-26 23:55:291192std::string MetricsService::GenerateClientID() {
marja@chromium.org7e49ad32012-06-14 14:22:071193 return base::GenerateGUID();
initial.commit09911bf2008-07-26 23:55:291194}
1195
initial.commit09911bf2008-07-26 23:55:291196//------------------------------------------------------------------------------
1197// State save methods
1198
1199void MetricsService::ScheduleNextStateSave() {
isherman@chromium.org8454aeb2011-11-19 23:38:201200 state_saver_factory_.InvalidateWeakPtrs();
initial.commit09911bf2008-07-26 23:55:291201
xhwang@chromium.orgb3a25092013-05-28 22:08:161202 base::MessageLoop::current()->PostDelayedTask(FROM_HERE,
isherman@chromium.org8454aeb2011-11-19 23:38:201203 base::Bind(&MetricsService::SaveLocalState,
1204 state_saver_factory_.GetWeakPtr()),
tedvessenes@gmail.comfc4252a72012-01-12 21:58:471205 base::TimeDelta::FromMinutes(kSaveStateIntervalMinutes));
initial.commit09911bf2008-07-26 23:55:291206}
1207
1208void MetricsService::SaveLocalState() {
1209 PrefService* pref = g_browser_process->local_state();
1210 if (!pref) {
jar@chromium.orga063c102010-07-22 22:20:191211 NOTREACHED();
initial.commit09911bf2008-07-26 23:55:291212 return;
1213 }
1214
1215 RecordCurrentState(pref);
initial.commit09911bf2008-07-26 23:55:291216
tedvessenes@gmail.comfc4252a72012-01-12 21:58:471217 // TODO(jar):110021 Does this run down the batteries????
initial.commit09911bf2008-07-26 23:55:291218 ScheduleNextStateSave();
1219}
1220
1221
1222//------------------------------------------------------------------------------
1223// Recording control methods
1224
stuartmorgan@chromium.org410938e02012-10-24 16:33:591225void MetricsService::OpenNewLog() {
1226 DCHECK(!log_manager_.current_log());
initial.commit09911bf2008-07-26 23:55:291227
stuartmorgan@chromium.org29948262012-03-01 12:15:081228 log_manager_.BeginLoggingWithLog(new MetricsLog(client_id_, session_id_),
asvitkine@chromium.org0edf8762013-11-21 18:33:301229 MetricsLog::ONGOING_LOG);
initial.commit09911bf2008-07-26 23:55:291230 if (state_ == INITIALIZED) {
1231 // We only need to schedule that run once.
zelidrag@chromium.org85ed9d42010-06-08 22:37:441232 state_ = INIT_TASK_SCHEDULED;
initial.commit09911bf2008-07-26 23:55:291233
zelidrag@chromium.org85ed9d42010-06-08 22:37:441234 // Schedules a task on the file thread for execution of slower
1235 // initialization steps (such as plugin list generation) necessary
1236 // for sending the initial log. This avoids blocking the main UI
1237 // thread.
joi@chromium.orged10dd12011-12-07 12:03:421238 BrowserThread::PostDelayedTask(
1239 BrowserThread::FILE,
1240 FROM_HERE,
rsesek@chromium.orgd33e7cc2011-09-23 01:43:561241 base::Bind(&MetricsService::InitTaskGetHardwareClass,
dpolukhin@chromium.orgc94d7382012-02-28 08:43:401242 self_ptr_factory_.GetWeakPtr(),
xhwang@chromium.orgb3a25092013-05-28 22:08:161243 base::MessageLoop::current()->message_loop_proxy()),
tedvessenes@gmail.com7e560102012-03-08 20:58:421244 base::TimeDelta::FromSeconds(kInitializationDelaySeconds));
initial.commit09911bf2008-07-26 23:55:291245 }
1246}
1247
stuartmorgan@chromium.org410938e02012-10-24 16:33:591248void MetricsService::CloseCurrentLog() {
stuartmorgan@chromium.orgcac267c2011-09-29 15:18:101249 if (!log_manager_.current_log())
initial.commit09911bf2008-07-26 23:55:291250 return;
1251
jar@google.com68475e602008-08-22 03:21:151252 // TODO(jar): Integrate bounds on log recording more consistently, so that we
1253 // can stop recording logs that are too big much sooner.
stuartmorgan@chromium.orgcac267c2011-09-29 15:18:101254 if (log_manager_.current_log()->num_events() > kEventLimit) {
dsh@google.com553dba62009-02-24 19:08:231255 UMA_HISTOGRAM_COUNTS("UMA.Discarded Log Events",
stuartmorgan@chromium.orgcac267c2011-09-29 15:18:101256 log_manager_.current_log()->num_events());
1257 log_manager_.DiscardCurrentLog();
stuartmorgan@chromium.org410938e02012-10-24 16:33:591258 OpenNewLog(); // Start trivial log to hold our histograms.
jar@google.com68475e602008-08-22 03:21:151259 }
1260
stuartmorgan@chromium.orgcac267c2011-09-29 15:18:101261 // Adds to ongoing logs.
1262 log_manager_.current_log()->set_hardware_class(hardware_class_);
jar@chromium.orgaccdfa62011-09-20 01:56:521263
jar@google.com0b33f80b2008-12-17 21:34:361264 // Put incremental data (histogram deltas, and realtime stats deltas) at the
jar@google.com147bbc0b2009-01-06 19:37:401265 // end of all log transmissions (initial log handles this separately).
stuartmorgan@chromium.org024b5cd2011-05-27 03:29:381266 // RecordIncrementalStabilityElements only exists on the derived
1267 // MetricsLog class.
isherman@chromium.org279703f2012-01-20 22:23:261268 MetricsLog* current_log =
1269 static_cast<MetricsLog*>(log_manager_.current_log());
stuartmorgan@chromium.org024b5cd2011-05-27 03:29:381270 DCHECK(current_log);
bengr@chromium.org60677562013-11-17 15:52:551271 std::vector<chrome_variations::ActiveGroupId> synthetic_trials;
1272 GetCurrentSyntheticFieldTrials(&synthetic_trials);
asvitkine@chromium.org0edf8762013-11-21 18:33:301273 current_log->RecordEnvironment(plugins_, google_update_metrics_,
1274 synthetic_trials);
asvitkine@chromium.orgc68a2b9b2013-10-09 18:16:361275 PrefService* pref = g_browser_process->local_state();
asvitkine@chromium.org250f7b662013-11-23 02:36:511276 current_log->RecordStabilityMetrics(GetIncrementalUptime(pref),
asvitkine@chromium.org0edf8762013-11-21 18:33:301277 MetricsLog::ONGOING_LOG);
bengr@chromium.org60677562013-11-17 15:52:551278
stuartmorgan@chromium.org024b5cd2011-05-27 03:29:381279 RecordCurrentHistograms();
initial.commit09911bf2008-07-26 23:55:291280
stuartmorgan@chromium.org29948262012-03-01 12:15:081281 log_manager_.FinishCurrentLog();
initial.commit09911bf2008-07-26 23:55:291282}
1283
stuartmorgan@chromium.orgcac267c2011-09-29 15:18:101284void MetricsService::PushPendingLogsToPersistentStorage() {
asvitkine@chromium.org80a8f312013-12-16 18:00:301285 if (state_ < SENDING_INITIAL_STABILITY_LOG)
avi@google.com28ab7f92009-01-06 21:39:041286 return; // We didn't and still don't have time to get plugin list etc.
initial.commit09911bf2008-07-26 23:55:291287
stuartmorgan@chromium.orgcac267c2011-09-29 15:18:101288 if (log_manager_.has_staged_log()) {
stuartmorgan@chromium.org7d41ae6d2012-06-26 08:53:031289 // We may race here, and send second copy of the log later.
isherman@chromium.orgdc61fe92012-06-12 00:13:501290 MetricsLogManager::StoreType store_type;
isherman@chromium.orge3eb0c42013-04-18 06:18:581291 if (current_fetch_.get())
isherman@chromium.orgdc61fe92012-06-12 00:13:501292 store_type = MetricsLogManager::PROVISIONAL_STORE;
1293 else
1294 store_type = MetricsLogManager::NORMAL_STORE;
stuartmorgan@chromium.orge7508d82012-05-03 15:59:531295 log_manager_.StoreStagedLogAsUnsent(store_type);
initial.commit09911bf2008-07-26 23:55:291296 }
stuartmorgan@chromium.orgcac267c2011-09-29 15:18:101297 DCHECK(!log_manager_.has_staged_log());
stuartmorgan@chromium.org410938e02012-10-24 16:33:591298 CloseCurrentLog();
asvitkine@chromium.org80a8f312013-12-16 18:00:301299 log_manager_.PersistUnsentLogs();
stuartmorgan@chromium.org7d41ae6d2012-06-26 08:53:031300
1301 // If there was a staged and/or current log, then there is now at least one
1302 // log waiting to be uploaded.
1303 if (log_manager_.has_unsent_logs())
1304 state_ = SENDING_OLD_LOGS;
initial.commit09911bf2008-07-26 23:55:291305}
1306
1307//------------------------------------------------------------------------------
1308// Transmission of logs methods
1309
stuartmorgan@chromium.org7f7f1962011-04-20 15:58:161310void MetricsService::StartSchedulerIfNecessary() {
stuartmorgan@chromium.org410938e02012-10-24 16:33:591311 // Never schedule cutting or uploading of logs in test mode.
1312 if (test_mode_active_)
1313 return;
1314
1315 // Even if reporting is disabled, the scheduler is needed to trigger the
1316 // creation of the initial log, which must be done in order for any logs to be
1317 // persisted on shutdown or backgrounding.
asvitkine@chromium.org80a8f312013-12-16 18:00:301318 if (recording_active() &&
1319 (reporting_active() || state_ < SENDING_INITIAL_STABILITY_LOG)) {
stuartmorgan@chromium.org7f7f1962011-04-20 15:58:161320 scheduler_->Start();
asvitkine@chromium.org80a8f312013-12-16 18:00:301321 }
initial.commit09911bf2008-07-26 23:55:291322}
1323
stuartmorgan@chromium.org7f7f1962011-04-20 15:58:161324void MetricsService::StartScheduledUpload() {
stuartmorgan@chromium.orgcd1ac712012-06-26 08:26:471325 // If we're getting no notifications, then the log won't have much in it, and
1326 // it's possible the computer is about to go to sleep, so don't upload and
1327 // stop the scheduler.
stuartmorgan@chromium.org410938e02012-10-24 16:33:591328 // If recording has been turned off, the scheduler doesn't need to run.
1329 // If reporting is off, proceed if the initial log hasn't been created, since
1330 // that has to happen in order for logs to be cut and stored when persisting.
stuartmorgan@chromium.orgcd1ac712012-06-26 08:26:471331 // TODO(stuartmorgan): Call Stop() on the schedule when reporting and/or
1332 // recording are turned off instead of letting it fire and then aborting.
1333 if (idle_since_last_transmission_ ||
stuartmorgan@chromium.org410938e02012-10-24 16:33:591334 !recording_active() ||
asvitkine@chromium.org80a8f312013-12-16 18:00:301335 (!reporting_active() && state_ >= SENDING_INITIAL_STABILITY_LOG)) {
stuartmorgan@chromium.org7f7f1962011-04-20 15:58:161336 scheduler_->Stop();
1337 scheduler_->UploadCancelled();
1338 return;
1339 }
1340
stuartmorgan@chromium.orgc15faf372012-07-11 06:01:341341 // If the callback was to upload an old log, but there no longer is one,
1342 // just report success back to the scheduler to begin the ongoing log
1343 // callbacks.
1344 // TODO(stuartmorgan): Consider removing the distinction between
1345 // SENDING_OLD_LOGS and SENDING_CURRENT_LOGS to simplify the state machine
1346 // now that the log upload flow is the same for both modes.
1347 if (state_ == SENDING_OLD_LOGS && !log_manager_.has_unsent_logs()) {
1348 state_ = SENDING_CURRENT_LOGS;
1349 scheduler_->UploadFinished(true /* healthy */, false /* no unsent logs */);
1350 return;
1351 }
stuartmorgan@chromium.orgcd1ac712012-06-26 08:26:471352 // If there are unsent logs, send the next one. If not, start the asynchronous
1353 // process of finalizing the current log for upload.
1354 if (state_ == SENDING_OLD_LOGS) {
1355 DCHECK(log_manager_.has_unsent_logs());
1356 log_manager_.StageNextLogForUpload();
1357 SendStagedLog();
1358 } else {
1359 StartFinalLogInfoCollection();
1360 }
stuartmorgan@chromium.org29948262012-03-01 12:15:081361}
1362
1363void MetricsService::StartFinalLogInfoCollection() {
1364 // Begin the multi-step process of collecting memory usage histograms:
1365 // First spawn a task to collect the memory details; when that task is
1366 // finished, it will call OnMemoryDetailCollectionDone. That will in turn
1367 // call HistogramSynchronization to collect histograms from all renderers and
1368 // then call OnHistogramSynchronizationDone to continue processing.
isherman@chromium.orgd119f222012-06-08 02:33:271369 DCHECK(!waiting_for_asynchronous_reporting_step_);
1370 waiting_for_asynchronous_reporting_step_ = true;
stuartmorgan@chromium.org7f7f1962011-04-20 15:58:161371
dcheng@chromium.org2226c222011-11-22 00:08:401372 base::Closure callback =
1373 base::Bind(&MetricsService::OnMemoryDetailCollectionDone,
dpolukhin@chromium.orgc94d7382012-02-28 08:43:401374 self_ptr_factory_.GetWeakPtr());
sail@chromium.org84c988a2011-04-19 17:56:331375
dcheng@chromium.org2226c222011-11-22 00:08:401376 scoped_refptr<MetricsMemoryDetails> details(
1377 new MetricsMemoryDetails(callback));
jamescook@chromium.org4306df72012-04-20 18:58:571378 details->StartFetch(MemoryDetails::UPDATE_USER_METRICS);
sail@chromium.org84c988a2011-04-19 17:56:331379
1380 // Collect WebCore cache information to put into a histogram.
ananta@chromium.orgf3b1a082011-11-18 00:34:301381 for (content::RenderProcessHost::iterator i(
1382 content::RenderProcessHost::AllHostsIterator());
sail@chromium.org84c988a2011-04-19 17:56:331383 !i.IsAtEnd(); i.Advance())
ananta@chromium.org2ccf45c2011-08-19 23:35:501384 i.GetCurrentValue()->Send(new ChromeViewMsg_GetCacheResourceStats());
sail@chromium.org84c988a2011-04-19 17:56:331385}
1386
1387void MetricsService::OnMemoryDetailCollectionDone() {
jar@chromium.orgc9a3ef82009-05-28 22:02:461388 DCHECK(IsSingleThreaded());
stuartmorgan@chromium.org7f7f1962011-04-20 15:58:161389 // This function should only be called as the callback from an ansynchronous
1390 // step.
isherman@chromium.orgd119f222012-06-08 02:33:271391 DCHECK(waiting_for_asynchronous_reporting_step_);
jar@chromium.orgc9a3ef82009-05-28 22:02:461392
jar@chromium.orgc9a3ef82009-05-28 22:02:461393 // Create a callback_task for OnHistogramSynchronizationDone.
dcheng@chromium.org2226c222011-11-22 00:08:401394 base::Closure callback = base::Bind(
1395 &MetricsService::OnHistogramSynchronizationDone,
dpolukhin@chromium.orgc94d7382012-02-28 08:43:401396 self_ptr_factory_.GetWeakPtr());
jar@chromium.orgc9a3ef82009-05-28 22:02:461397
vitalybuka@chromium.orga3079832013-10-24 20:29:361398 base::TimeDelta timeout =
1399 base::TimeDelta::FromMilliseconds(kMaxHistogramGatheringWaitDuration);
1400
1401 DCHECK_EQ(num_async_histogram_fetches_in_progress_, 0);
1402
1403#if defined(OS_ANDROID)
1404 // Android has no service process.
1405 num_async_histogram_fetches_in_progress_ = 1;
1406#else // OS_ANDROID
1407 num_async_histogram_fetches_in_progress_ = 2;
1408 // Run requests to service and content in parallel.
1409 if (!ServiceProcessControl::GetInstance()->GetHistograms(callback, timeout)) {
1410 // Assume |num_async_histogram_fetches_in_progress_| is not changed by
1411 // |GetHistograms()|.
1412 DCHECK_EQ(num_async_histogram_fetches_in_progress_, 2);
1413 // Assign |num_async_histogram_fetches_in_progress_| above and decrement it
1414 // here to make code work even if |GetHistograms()| fired |callback|.
1415 --num_async_histogram_fetches_in_progress_;
1416 }
1417#endif // OS_ANDROID
1418
jar@chromium.orgc9a3ef82009-05-28 22:02:461419 // Set up the callback to task to call after we receive histograms from all
rtenneti@google.com83ab4a282012-07-12 18:19:451420 // child processes. Wait time specifies how long to wait before absolutely
jar@chromium.orgc9a3ef82009-05-28 22:02:461421 // calling us back on the task.
vitalybuka@chromium.orga3079832013-10-24 20:29:361422 content::FetchHistogramsAsynchronously(base::MessageLoop::current(), callback,
1423 timeout);
jar@chromium.orgc9a3ef82009-05-28 22:02:461424}
1425
1426void MetricsService::OnHistogramSynchronizationDone() {
initial.commit09911bf2008-07-26 23:55:291427 DCHECK(IsSingleThreaded());
stuartmorgan@chromium.org29948262012-03-01 12:15:081428 // This function should only be called as the callback from an ansynchronous
1429 // step.
isherman@chromium.orgd119f222012-06-08 02:33:271430 DCHECK(waiting_for_asynchronous_reporting_step_);
vitalybuka@chromium.orga3079832013-10-24 20:29:361431 DCHECK_GT(num_async_histogram_fetches_in_progress_, 0);
1432
1433 // Check if all expected requests finished.
1434 if (--num_async_histogram_fetches_in_progress_ > 0)
1435 return;
initial.commit09911bf2008-07-26 23:55:291436
isherman@chromium.orgd119f222012-06-08 02:33:271437 waiting_for_asynchronous_reporting_step_ = false;
stuartmorgan@chromium.org29948262012-03-01 12:15:081438 OnFinalLogInfoCollectionDone();
1439}
1440
1441void MetricsService::OnFinalLogInfoCollectionDone() {
stuartmorgan@chromium.org7f7f1962011-04-20 15:58:161442 // If somehow there is a fetch in progress, we return and hope things work
1443 // out. The scheduler isn't informed since if this happens, the scheduler
1444 // will get a response from the upload.
isherman@chromium.orge3eb0c42013-04-18 06:18:581445 DCHECK(!current_fetch_.get());
1446 if (current_fetch_.get())
stuartmorgan@chromium.org7f7f1962011-04-20 15:58:161447 return;
1448
stuartmorgan@chromium.orgcd1ac712012-06-26 08:26:471449 // Abort if metrics were turned off during the final info gathering.
stuartmorgan@chromium.org410938e02012-10-24 16:33:591450 if (!recording_active()) {
stuartmorgan@chromium.org7f7f1962011-04-20 15:58:161451 scheduler_->Stop();
1452 scheduler_->UploadCancelled();
petersont@google.comd01b8732008-10-16 02:18:071453 return;
1454 }
1455
stuartmorgan@chromium.orgcd1ac712012-06-26 08:26:471456 StageNewLog();
stuartmorgan@chromium.org410938e02012-10-24 16:33:591457
1458 // If logs shouldn't be uploaded, stop here. It's important that this check
1459 // be after StageNewLog(), otherwise the previous logs will never be loaded,
1460 // and thus the open log won't be persisted.
1461 // TODO(stuartmorgan): This is unnecessarily complicated; restructure loading
1462 // of previous logs to not require running part of the upload logic.
1463 // http://crbug.com/157337
1464 if (!reporting_active()) {
1465 scheduler_->Stop();
1466 scheduler_->UploadCancelled();
1467 return;
1468 }
1469
stuartmorgan@chromium.org29948262012-03-01 12:15:081470 SendStagedLog();
1471}
1472
stuartmorgan@chromium.orgcd1ac712012-06-26 08:26:471473void MetricsService::StageNewLog() {
stuartmorgan@chromium.org29948262012-03-01 12:15:081474 if (log_manager_.has_staged_log())
1475 return;
1476
1477 switch (state_) {
1478 case INITIALIZED:
1479 case INIT_TASK_SCHEDULED: // We should be further along by now.
isherman@chromium.orgdc61fe92012-06-12 00:13:501480 NOTREACHED();
stuartmorgan@chromium.org29948262012-03-01 12:15:081481 return;
1482
1483 case INIT_TASK_DONE:
asvitkine@chromium.org80a8f312013-12-16 18:00:301484 if (has_initial_stability_log_) {
1485 // There's an initial stability log, ready to send.
1486 log_manager_.StageNextLogForUpload();
1487 has_initial_stability_log_ = false;
1488 state_ = SENDING_INITIAL_STABILITY_LOG;
1489 } else {
1490 // TODO(asvitkine): When the field trial is removed, the |log_type|
1491 // arg should be removed and PrepareInitialMetricsLog() should always
1492 // use ONGOING_LOG. Use INITIAL_LOG only to match to the old behavior
1493 // when the field trial is off.
1494 MetricsLog::LogType log_type = SendSeparateInitialStabilityLog() ?
1495 MetricsLog::ONGOING_LOG : MetricsLog::INITIAL_LOG;
1496 PrepareInitialMetricsLog(log_type);
1497 // If the stability log field trial is off, load unsent logs from local
1498 // state here. Otherwise, they have already been loaded earlier.
1499 if (log_type == MetricsLog::INITIAL_LOG)
1500 log_manager_.LoadPersistedUnsentLogs();
1501 state_ = SENDING_INITIAL_METRICS_LOG;
1502 }
stuartmorgan@chromium.org29948262012-03-01 12:15:081503 break;
1504
1505 case SENDING_OLD_LOGS:
stuartmorgan@chromium.orgcd1ac712012-06-26 08:26:471506 NOTREACHED(); // Shouldn't be staging a new log during old log sending.
1507 return;
stuartmorgan@chromium.org29948262012-03-01 12:15:081508
1509 case SENDING_CURRENT_LOGS:
stuartmorgan@chromium.org410938e02012-10-24 16:33:591510 CloseCurrentLog();
1511 OpenNewLog();
stuartmorgan@chromium.org29948262012-03-01 12:15:081512 log_manager_.StageNextLogForUpload();
1513 break;
1514
1515 default:
1516 NOTREACHED();
1517 return;
1518 }
1519
1520 DCHECK(log_manager_.has_staged_log());
1521}
1522
asvitkine@chromium.org80a8f312013-12-16 18:00:301523void MetricsService::PrepareInitialStabilityLog() {
1524 DCHECK_EQ(INITIALIZED, state_);
1525 PrefService* pref = g_browser_process->local_state();
1526 DCHECK_NE(0, pref->GetInteger(prefs::kStabilityCrashCount));
stuartmorgan@chromium.org29948262012-03-01 12:15:081527
asvitkine@chromium.org80a8f312013-12-16 18:00:301528 scoped_ptr<MetricsLog> initial_stability_log(
1529 new MetricsLog(client_id_, session_id_));
1530 if (!initial_stability_log->LoadSavedEnvironmentFromPrefs())
1531 return;
1532 initial_stability_log->RecordStabilityMetrics(base::TimeDelta(),
1533 MetricsLog::INITIAL_LOG);
1534 log_manager_.LoadPersistedUnsentLogs();
1535
1536 log_manager_.PauseCurrentLog();
1537 log_manager_.BeginLoggingWithLog(initial_stability_log.release(),
1538 MetricsLog::INITIAL_LOG);
1539 log_manager_.FinishCurrentLog();
1540 log_manager_.ResumePausedLog();
1541
1542 // Store unsent logs, including the stability log that was just saved, so
1543 // that they're not lost in case of a crash before upload time.
1544 log_manager_.PersistUnsentLogs();
1545
1546 has_initial_stability_log_ = true;
1547}
1548
1549void MetricsService::PrepareInitialMetricsLog(MetricsLog::LogType log_type) {
1550 DCHECK(state_ == INIT_TASK_DONE || state_ == SENDING_INITIAL_STABILITY_LOG);
1551 initial_metrics_log_->set_hardware_class(hardware_class_);
asvitkine@chromium.org0edf8762013-11-21 18:33:301552
bengr@chromium.org60677562013-11-17 15:52:551553 std::vector<chrome_variations::ActiveGroupId> synthetic_trials;
1554 GetCurrentSyntheticFieldTrials(&synthetic_trials);
asvitkine@chromium.org80a8f312013-12-16 18:00:301555 initial_metrics_log_->RecordEnvironment(plugins_, google_update_metrics_,
1556 synthetic_trials);
asvitkine@chromium.org0edf8762013-11-21 18:33:301557 PrefService* pref = g_browser_process->local_state();
asvitkine@chromium.org80a8f312013-12-16 18:00:301558 initial_metrics_log_->RecordStabilityMetrics(GetIncrementalUptime(pref),
1559 log_type);
stuartmorgan@chromium.org29948262012-03-01 12:15:081560
1561 // Histograms only get written to the current log, so make the new log current
1562 // before writing them.
1563 log_manager_.PauseCurrentLog();
asvitkine@chromium.org80a8f312013-12-16 18:00:301564 log_manager_.BeginLoggingWithLog(initial_metrics_log_.release(), log_type);
stuartmorgan@chromium.org29948262012-03-01 12:15:081565 RecordCurrentHistograms();
1566 log_manager_.FinishCurrentLog();
1567 log_manager_.ResumePausedLog();
1568
1569 DCHECK(!log_manager_.has_staged_log());
1570 log_manager_.StageNextLogForUpload();
1571}
1572
stuartmorgan@chromium.org29948262012-03-01 12:15:081573void MetricsService::SendStagedLog() {
1574 DCHECK(log_manager_.has_staged_log());
1575
stuartmorgan@chromium.orgcac267c2011-09-29 15:18:101576 PrepareFetchWithStagedLog();
petersont@google.comd01b8732008-10-16 02:18:071577
isherman@chromium.orge3eb0c42013-04-18 06:18:581578 bool upload_created = (current_fetch_.get() != NULL);
isherman@chromium.orgd6bebb92012-06-13 23:14:551579 UMA_HISTOGRAM_BOOLEAN("UMA.UploadCreation", upload_created);
1580 if (!upload_created) {
petersont@google.comd01b8732008-10-16 02:18:071581 // Compression failed, and log discarded :-/.
isherman@chromium.orgdc61fe92012-06-12 00:13:501582 // Skip this upload and hope things work out next time.
stuartmorgan@chromium.orgcac267c2011-09-29 15:18:101583 log_manager_.DiscardStagedLog();
stuartmorgan@chromium.org7f7f1962011-04-20 15:58:161584 scheduler_->UploadCancelled();
petersont@google.comd01b8732008-10-16 02:18:071585 return;
1586 }
1587
isherman@chromium.orgd119f222012-06-08 02:33:271588 DCHECK(!waiting_for_asynchronous_reporting_step_);
isherman@chromium.orgd119f222012-06-08 02:33:271589 waiting_for_asynchronous_reporting_step_ = true;
isherman@chromium.orgdc61fe92012-06-12 00:13:501590
isherman@chromium.orge3eb0c42013-04-18 06:18:581591 current_fetch_->Start();
petersont@google.comd01b8732008-10-16 02:18:071592
1593 HandleIdleSinceLastTransmission(true);
1594}
1595
stuartmorgan@chromium.orgcac267c2011-09-29 15:18:101596void MetricsService::PrepareFetchWithStagedLog() {
isherman@chromium.orgdc61fe92012-06-12 00:13:501597 DCHECK(log_manager_.has_staged_log());
pkasting@chromium.orgcac78842008-11-27 01:02:201598
isherman@chromium.orgfe58acc22012-02-29 01:29:581599 // Prepare the protobuf version.
isherman@chromium.orge3eb0c42013-04-18 06:18:581600 DCHECK(!current_fetch_.get());
isherman@chromium.org5f3e1642013-05-05 03:37:341601 if (log_manager_.has_staged_log()) {
isherman@chromium.orge3eb0c42013-04-18 06:18:581602 current_fetch_.reset(net::URLFetcher::Create(
isherman@chromium.org5f3e1642013-05-05 03:37:341603 GURL(kServerUrl), net::URLFetcher::POST, this));
isherman@chromium.orge3eb0c42013-04-18 06:18:581604 current_fetch_->SetRequestContext(
isherman@chromium.orgfe58acc22012-02-29 01:29:581605 g_browser_process->system_request_context());
asharif@chromium.org537c638d2013-07-04 00:49:191606
asharif@chromium.org8df71322013-09-13 18:40:001607 std::string log_text = log_manager_.staged_log_text();
1608 std::string compressed_log_text;
1609 bool compression_successful = chrome::GzipCompress(log_text,
1610 &compressed_log_text);
1611 DCHECK(compression_successful);
1612 if (compression_successful) {
1613 current_fetch_->SetUploadData(kMimeType, compressed_log_text);
1614 // Tell the server that we're uploading gzipped protobufs.
1615 current_fetch_->SetExtraRequestHeaders("content-encoding: gzip");
asvitkine@chromium.orgcfee9aa52013-10-19 17:53:051616 const std::string hash =
1617 base::HexEncode(log_manager_.staged_log_hash().data(),
1618 log_manager_.staged_log_hash().size());
1619 DCHECK(!hash.empty());
1620 current_fetch_->AddExtraRequestHeader("X-Chrome-UMA-Log-SHA1: " + hash);
asharif@chromium.org8df71322013-09-13 18:40:001621 UMA_HISTOGRAM_PERCENTAGE(
1622 "UMA.ProtoCompressionRatio",
1623 100 * compressed_log_text.size() / log_text.size());
1624 UMA_HISTOGRAM_CUSTOM_COUNTS(
1625 "UMA.ProtoGzippedKBSaved",
1626 (log_text.size() - compressed_log_text.size()) / 1024,
1627 1, 2000, 50);
asharif@chromium.org537c638d2013-07-04 00:49:191628 }
asharif@chromium.org537c638d2013-07-04 00:49:191629
isherman@chromium.orgfe58acc22012-02-29 01:29:581630 // We already drop cookies server-side, but we might as well strip them out
1631 // client-side as well.
isherman@chromium.orge3eb0c42013-04-18 06:18:581632 current_fetch_->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES |
1633 net::LOAD_DO_NOT_SEND_COOKIES);
isherman@chromium.orgfe58acc22012-02-29 01:29:581634 }
initial.commit09911bf2008-07-26 23:55:291635}
1636
akalin@chromium.org10c2d692012-05-11 05:32:231637void MetricsService::OnURLFetchComplete(const net::URLFetcher* source) {
isherman@chromium.orgd119f222012-06-08 02:33:271638 DCHECK(waiting_for_asynchronous_reporting_step_);
isherman@chromium.orgfe58acc22012-02-29 01:29:581639
1640 // We're not allowed to re-use the existing |URLFetcher|s, so free them here.
isherman@chromium.orge3eb0c42013-04-18 06:18:581641 // Note however that |source| is aliased to the fetcher, so we should be
isherman@chromium.org4266def22012-05-17 01:02:401642 // careful not to delete it too early.
isherman@chromium.orge3eb0c42013-04-18 06:18:581643 DCHECK_EQ(current_fetch_.get(), source);
1644 scoped_ptr<net::URLFetcher> s(current_fetch_.Pass());
isherman@chromium.orgfe58acc22012-02-29 01:29:581645
isherman@chromium.orgdc61fe92012-06-12 00:13:501646 int response_code = source->GetResponseCode();
isherman@chromium.orgfe58acc22012-02-29 01:29:581647
isherman@chromium.orgdc61fe92012-06-12 00:13:501648 // Log a histogram to track response success vs. failure rates.
isherman@chromium.orge3eb0c42013-04-18 06:18:581649 UMA_HISTOGRAM_ENUMERATION("UMA.UploadResponseStatus.Protobuf",
1650 ResponseCodeToStatus(response_code),
1651 NUM_RESPONSE_STATUSES);
isherman@chromium.orgfe58acc22012-02-29 01:29:581652
stuartmorgan@chromium.orge7508d82012-05-03 15:59:531653 // If the upload was provisionally stored, drop it now that the upload is
1654 // known to have gone through.
1655 log_manager_.DiscardLastProvisionalStore();
initial.commit09911bf2008-07-26 23:55:291656
isherman@chromium.orgdc61fe92012-06-12 00:13:501657 bool upload_succeeded = response_code == 200;
stuartmorgan@chromium.org7f7f1962011-04-20 15:58:161658
jar@chromium.org0eb34fee2009-01-21 08:04:381659 // Provide boolean for error recovery (allow us to ignore response_code).
paul@chromium.orgdc6f4962009-02-13 01:25:501660 bool discard_log = false;
isherman@chromium.org5f3e1642013-05-05 03:37:341661 const size_t log_size = log_manager_.staged_log_text().length();
isherman@chromium.orgdc61fe92012-06-12 00:13:501662 if (!upload_succeeded && log_size > kUploadLogAvoidRetransmitSize) {
1663 UMA_HISTOGRAM_COUNTS("UMA.Large Rejected Log was Discarded",
1664 static_cast<int>(log_size));
jar@chromium.org0eb34fee2009-01-21 08:04:381665 discard_log = true;
isherman@chromium.orgdc61fe92012-06-12 00:13:501666 } else if (response_code == 400) {
jar@chromium.org0eb34fee2009-01-21 08:04:381667 // Bad syntax. Retransmission won't work.
jar@chromium.org0eb34fee2009-01-21 08:04:381668 discard_log = true;
jar@google.com68475e602008-08-22 03:21:151669 }
1670
isherman@chromium.orge3eb0c42013-04-18 06:18:581671 if (upload_succeeded || discard_log)
isherman@chromium.org5f3e1642013-05-05 03:37:341672 log_manager_.DiscardStagedLog();
isherman@chromium.orgdc61fe92012-06-12 00:13:501673
1674 waiting_for_asynchronous_reporting_step_ = false;
1675
1676 if (!log_manager_.has_staged_log()) {
initial.commit09911bf2008-07-26 23:55:291677 switch (state_) {
asvitkine@chromium.org80a8f312013-12-16 18:00:301678 case SENDING_INITIAL_STABILITY_LOG:
1679 // Store the updated list to disk now that the removed log is uploaded.
1680 log_manager_.PersistUnsentLogs();
1681 PrepareInitialMetricsLog(MetricsLog::ONGOING_LOG);
1682 SendStagedLog();
1683 state_ = SENDING_INITIAL_METRICS_LOG;
1684 break;
1685
1686 case SENDING_INITIAL_METRICS_LOG:
1687 // The initial metrics log never gets persisted to local state, so it's
1688 // not necessary to call log_manager_.PersistUnsentLogs() here.
1689 // TODO(asvitkine): It should be persisted like the initial stability
1690 // log and old unsent logs. http://crbug.com/328417
stuartmorgan@chromium.orgcd1ac712012-06-26 08:26:471691 state_ = log_manager_.has_unsent_logs() ? SENDING_OLD_LOGS
1692 : SENDING_CURRENT_LOGS;
initial.commit09911bf2008-07-26 23:55:291693 break;
1694
initial.commit09911bf2008-07-26 23:55:291695 case SENDING_OLD_LOGS:
stuartmorgan@chromium.orgd53e2232011-06-30 15:54:571696 // Store the updated list to disk now that the removed log is uploaded.
asvitkine@chromium.org80a8f312013-12-16 18:00:301697 log_manager_.PersistUnsentLogs();
stuartmorgan@chromium.orgcd1ac712012-06-26 08:26:471698 if (!log_manager_.has_unsent_logs())
1699 state_ = SENDING_CURRENT_LOGS;
initial.commit09911bf2008-07-26 23:55:291700 break;
1701
1702 case SENDING_CURRENT_LOGS:
1703 break;
1704
1705 default:
jar@chromium.orga063c102010-07-22 22:20:191706 NOTREACHED();
initial.commit09911bf2008-07-26 23:55:291707 break;
1708 }
petersont@google.comd01b8732008-10-16 02:18:071709
stuartmorgan@chromium.orgcac267c2011-09-29 15:18:101710 if (log_manager_.has_unsent_logs())
isherman@chromium.orged0fd002012-04-25 23:10:341711 DCHECK_LT(state_, SENDING_CURRENT_LOGS);
initial.commit09911bf2008-07-26 23:55:291712 }
petersont@google.com252873ef2008-08-04 21:59:451713
stuartmorgan@chromium.org7f7f1962011-04-20 15:58:161714 // Error 400 indicates a problem with the log, not with the server, so
1715 // don't consider that a sign that the server is in trouble.
isherman@chromium.orgdc61fe92012-06-12 00:13:501716 bool server_is_healthy = upload_succeeded || response_code == 400;
asvitkine@chromium.org80a8f312013-12-16 18:00:301717 // Don't notify the scheduler that the upload is finished if we've only sent
1718 // the initial stability log, but not yet the initial metrics log (treat the
1719 // two as a single unit of work as far as the scheduler is concerned).
1720 if (state_ != SENDING_INITIAL_METRICS_LOG) {
1721 scheduler_->UploadFinished(server_is_healthy,
1722 log_manager_.has_unsent_logs());
1723 }
rtenneti@chromium.orgd67d1052011-06-09 05:11:411724
1725 // Collect network stats if UMA upload succeeded.
isherman@chromium.orgb8ddb052012-04-19 02:36:061726 IOThread* io_thread = g_browser_process->io_thread();
1727 if (server_is_healthy && io_thread) {
1728 chrome_browser_net::CollectNetworkStats(network_stats_server_, io_thread);
simonjam@chromium.orgadbb3762012-03-09 22:20:081729 chrome_browser_net::CollectPipeliningCapabilityStatsOnUIThread(
isherman@chromium.orgb8ddb052012-04-19 02:36:061730 http_pipelining_test_server_, io_thread);
simonjam@chromium.orgaa312812013-04-30 19:46:051731#if defined(OS_WIN)
1732 chrome::CollectTimeTicksStats();
1733#endif
simonjam@chromium.orgadbb3762012-03-09 22:20:081734 }
initial.commit09911bf2008-07-26 23:55:291735}
1736
viettrungluu@chromium.org57ecc4b2010-08-11 03:02:511737void MetricsService::IncrementPrefValue(const char* path) {
cpu@google.come73c01972008-08-13 00:18:241738 PrefService* pref = g_browser_process->local_state();
1739 DCHECK(pref);
1740 int value = pref->GetInteger(path);
1741 pref->SetInteger(path, value + 1);
1742}
1743
viettrungluu@chromium.org57ecc4b2010-08-11 03:02:511744void MetricsService::IncrementLongPrefsValue(const char* path) {
robertshield@google.com0bb1a622009-03-04 03:22:321745 PrefService* pref = g_browser_process->local_state();
1746 DCHECK(pref);
1747 int64 value = pref->GetInt64(path);
jar@chromium.orgb42c5e42010-06-03 20:43:251748 pref->SetInt64(path, value + 1);
robertshield@google.com0bb1a622009-03-04 03:22:321749}
1750
rlp@chromium.org752a5262013-06-23 14:53:421751void MetricsService::LogLoadStarted(content::WebContents* web_contents) {
mpearson@chromium.org5d490e42012-08-30 05:16:431752 content::RecordAction(content::UserMetricsAction("PageLoad"));
jar@chromium.orgdd8d12a2011-09-02 02:10:151753 HISTOGRAM_ENUMERATION("Chrome.UmaPageloadCounter", 1, 2);
cpu@google.come73c01972008-08-13 00:18:241754 IncrementPrefValue(prefs::kStabilityPageLoadCount);
robertshield@google.com0bb1a622009-03-04 03:22:321755 IncrementLongPrefsValue(prefs::kUninstallMetricsPageLoadCount);
jar@google.com0b33f80b2008-12-17 21:34:361756 // We need to save the prefs, as page load count is a critical stat, and it
1757 // might be lost due to a crash :-(.
initial.commit09911bf2008-07-26 23:55:291758}
1759
jar@chromium.orgc3721482012-03-23 16:21:481760void MetricsService::LogRendererCrash(content::RenderProcessHost* host,
1761 base::TerminationStatus status,
jam@chromium.orgf1675202012-07-09 15:18:001762 int exit_code) {
ananta@chromium.orgf3b1a082011-11-18 00:34:301763 Profile* profile = Profile::FromBrowserContext(host->GetBrowserContext());
aa@chromium.org6f371442011-11-09 06:45:461764 ExtensionService* service = profile->GetExtensionService();
1765 bool was_extension_process =
ananta@chromium.orgf3b1a082011-11-18 00:34:301766 service && service->process_map()->Contains(host->GetID());
jar@chromium.orgc3721482012-03-23 16:21:481767 if (status == base::TERMINATION_STATUS_PROCESS_CRASHED ||
1768 status == base::TERMINATION_STATUS_ABNORMAL_TERMINATION) {
eroman@chromium.orgd7c1fa62012-06-15 23:35:301769 if (was_extension_process) {
jochen@chromium.org718eab62011-10-05 21:16:521770 IncrementPrefValue(prefs::kStabilityExtensionRendererCrashCount);
eroman@chromium.orgd7c1fa62012-06-15 23:35:301771
eroman@chromium.org1026afd2013-03-20 14:28:541772 UMA_HISTOGRAM_SPARSE_SLOWLY("CrashExitCodes.Extension",
1773 MapCrashExitCodeForHistogram(exit_code));
eroman@chromium.orgd7c1fa62012-06-15 23:35:301774 } else {
jochen@chromium.org718eab62011-10-05 21:16:521775 IncrementPrefValue(prefs::kStabilityRendererCrashCount);
initial.commit09911bf2008-07-26 23:55:291776
eroman@chromium.org1026afd2013-03-20 14:28:541777 UMA_HISTOGRAM_SPARSE_SLOWLY("CrashExitCodes.Renderer",
1778 MapCrashExitCodeForHistogram(exit_code));
eroman@chromium.orgd7c1fa62012-06-15 23:35:301779 }
1780
jochen@chromium.org718eab62011-10-05 21:16:521781 UMA_HISTOGRAM_PERCENTAGE("BrowserRenderProcessHost.ChildCrashes",
1782 was_extension_process ? 2 : 1);
jar@chromium.orgc3721482012-03-23 16:21:481783 } else if (status == base::TERMINATION_STATUS_PROCESS_WAS_KILLED) {
jochen@chromium.org718eab62011-10-05 21:16:521784 UMA_HISTOGRAM_PERCENTAGE("BrowserRenderProcessHost.ChildKills",
1785 was_extension_process ? 2 : 1);
jam@chromium.orgf1675202012-07-09 15:18:001786 } else if (status == base::TERMINATION_STATUS_STILL_RUNNING) {
1787 UMA_HISTOGRAM_PERCENTAGE("BrowserRenderProcessHost.DisconnectedAlive",
jochen@chromium.org718eab62011-10-05 21:16:521788 was_extension_process ? 2 : 1);
jochen@chromium.org718eab62011-10-05 21:16:521789 }
asvitkine@chromium.org80a8f312013-12-16 18:00:301790}
asargent@chromium.org1f085622009-12-04 05:33:451791
initial.commit09911bf2008-07-26 23:55:291792void MetricsService::LogRendererHang() {
cpu@google.come73c01972008-08-13 00:18:241793 IncrementPrefValue(prefs::kStabilityRendererHangCount);
initial.commit09911bf2008-07-26 23:55:291794}
1795
jar@chromium.orgc0c55e92011-09-10 18:47:301796bool MetricsService::UmaMetricsProperlyShutdown() {
1797 CHECK(clean_shutdown_status_ == CLEANLY_SHUTDOWN ||
1798 clean_shutdown_status_ == NEED_TO_SHUTDOWN);
1799 return clean_shutdown_status_ == CLEANLY_SHUTDOWN;
1800}
1801
bengr@chromium.org60677562013-11-17 15:52:551802void MetricsService::RegisterSyntheticFieldTrial(
1803 const SyntheticTrialGroup& trial) {
1804 for (size_t i = 0; i < synthetic_trial_groups_.size(); ++i) {
1805 if (synthetic_trial_groups_[i].id.name == trial.id.name) {
1806 if (synthetic_trial_groups_[i].id.group != trial.id.group) {
1807 synthetic_trial_groups_[i].id.group = trial.id.group;
1808 synthetic_trial_groups_[i].start_time = trial.start_time;
1809 }
1810 return;
1811 }
1812 }
1813
1814 SyntheticTrialGroup trial_group(
1815 trial.id.name, trial.id.group, base::TimeTicks::Now());
1816 synthetic_trial_groups_.push_back(trial_group);
1817}
1818
1819void MetricsService::GetCurrentSyntheticFieldTrials(
1820 std::vector<chrome_variations::ActiveGroupId>* synthetic_trials) {
1821 DCHECK(synthetic_trials);
1822 synthetic_trials->clear();
1823 const MetricsLog* current_log =
1824 static_cast<const MetricsLog*>(log_manager_.current_log());
1825 for (size_t i = 0; i < synthetic_trial_groups_.size(); ++i) {
1826 if (synthetic_trial_groups_[i].start_time <= current_log->creation_time())
1827 synthetic_trials->push_back(synthetic_trial_groups_[i].id);
1828 }
1829}
1830
stuartmorgan@chromium.org466f3c12011-03-23 21:20:381831void MetricsService::LogCleanShutdown() {
jar@chromium.orgacd55b32011-09-05 17:35:311832 // Redundant hack to write pref ASAP.
nileshagrawal@chromium.org84c384e2013-03-01 23:20:191833 MarkAppCleanShutdownAndCommit();
1834
jar@chromium.orgc0c55e92011-09-10 18:47:301835 // Redundant setting to assure that we always reset this value at shutdown
1836 // (and that we don't use some alternate path, and not call LogCleanShutdown).
1837 clean_shutdown_status_ = CLEANLY_SHUTDOWN;
jar@chromium.orgacd55b32011-09-05 17:35:311838
stuartmorgan@chromium.org466f3c12011-03-23 21:20:381839 RecordBooleanPrefValue(prefs::kStabilityExitedCleanly, true);
rtenneti@chromium.org6a6d0d12013-10-28 15:58:191840 PrefService* pref = g_browser_process->local_state();
1841 pref->SetInteger(prefs::kStabilityExecutionPhase,
rtenneti@chromium.org6d67ea0d2013-11-14 11:02:211842 MetricsService::SHUTDOWN_COMPLETE);
stuartmorgan@chromium.org466f3c12011-03-23 21:20:381843}
1844
petkov@chromium.orgc1834a92011-01-21 18:21:031845#if defined(OS_CHROMEOS)
1846void MetricsService::LogChromeOSCrash(const std::string &crash_type) {
1847 if (crash_type == "user")
1848 IncrementPrefValue(prefs::kStabilityOtherUserCrashCount);
1849 else if (crash_type == "kernel")
1850 IncrementPrefValue(prefs::kStabilityKernelCrashCount);
1851 else if (crash_type == "uncleanshutdown")
1852 IncrementPrefValue(prefs::kStabilitySystemUncleanShutdownCount);
1853 else
1854 NOTREACHED() << "Unexpected Chrome OS crash type " << crash_type;
1855 // Wake up metrics logs sending if necessary now that new
1856 // log data is available.
1857 HandleIdleSinceLastTransmission(false);
1858}
1859#endif // OS_CHROMEOS
1860
brettw@chromium.org650b2d52013-02-10 03:41:451861void MetricsService::LogPluginLoadingError(const base::FilePath& plugin_path) {
jam@chromium.orgd7bd3e52013-07-21 04:29:201862 content::WebPluginInfo plugin;
bauerb@chromium.orgcd937072012-07-02 09:00:291863 bool success =
1864 content::PluginService::GetInstance()->GetPluginInfoByPath(plugin_path,
1865 &plugin);
1866 DCHECK(success);
1867 ChildProcessStats& stats = child_process_stats_buffer_[plugin.name];
1868 // Initialize the type if this entry is new.
1869 if (stats.process_type == content::PROCESS_TYPE_UNKNOWN) {
1870 // The plug-in process might not actually of type PLUGIN (which means
1871 // NPAPI), but we only care that it is *a* plug-in process.
1872 stats.process_type = content::PROCESS_TYPE_PLUGIN;
1873 } else {
1874 DCHECK(IsPluginProcess(stats.process_type));
1875 }
1876 stats.loading_errors++;
1877}
1878
phajdan.jr@chromium.orgf4eaf7b92013-02-28 22:00:401879MetricsService::ChildProcessStats& MetricsService::GetChildProcessStats(
1880 const content::ChildProcessData& data) {
brettw@chromium.org439f1e32013-12-09 20:09:091881 const base::string16& child_name = data.name;
jam@chromium.orgf3b357692013-03-22 05:16:131882 if (!ContainsKey(child_process_stats_buffer_, child_name)) {
1883 child_process_stats_buffer_[child_name] =
1884 ChildProcessStats(data.process_type);
1885 }
phajdan.jr@chromium.orgf4eaf7b92013-02-28 22:00:401886 return child_process_stats_buffer_[child_name];
initial.commit09911bf2008-07-26 23:55:291887}
1888
initial.commit09911bf2008-07-26 23:55:291889void MetricsService::RecordPluginChanges(PrefService* pref) {
battre@chromium.orgf8628c22011-04-05 12:10:181890 ListPrefUpdate update(pref, prefs::kStabilityPluginStats);
1891 ListValue* plugins = update.Get();
initial.commit09911bf2008-07-26 23:55:291892 DCHECK(plugins);
1893
1894 for (ListValue::iterator value_iter = plugins->begin();
1895 value_iter != plugins->end(); ++value_iter) {
1896 if (!(*value_iter)->IsType(Value::TYPE_DICTIONARY)) {
jar@chromium.orga063c102010-07-22 22:20:191897 NOTREACHED();
initial.commit09911bf2008-07-26 23:55:291898 continue;
1899 }
1900
1901 DictionaryValue* plugin_dict = static_cast<DictionaryValue*>(*value_iter);
viettrungluu@chromium.org57ecc4b2010-08-11 03:02:511902 std::string plugin_name;
nsylvain@chromium.org8e50b602009-03-03 22:59:431903 plugin_dict->GetString(prefs::kStabilityPluginName, &plugin_name);
nsylvain@chromium.org6470ee8f2009-03-03 20:46:401904 if (plugin_name.empty()) {
jar@chromium.orga063c102010-07-22 22:20:191905 NOTREACHED();
initial.commit09911bf2008-07-26 23:55:291906 continue;
1907 }
1908
viettrungluu@chromium.org57ecc4b2010-08-11 03:02:511909 // TODO(viettrungluu): remove conversions
brettw@chromium.org439f1e32013-12-09 20:09:091910 base::string16 name16 = UTF8ToUTF16(plugin_name);
evan@chromium.org68b9e72b2011-08-05 23:08:221911 if (child_process_stats_buffer_.find(name16) ==
1912 child_process_stats_buffer_.end()) {
initial.commit09911bf2008-07-26 23:55:291913 continue;
evan@chromium.org68b9e72b2011-08-05 23:08:221914 }
initial.commit09911bf2008-07-26 23:55:291915
evan@chromium.org68b9e72b2011-08-05 23:08:221916 ChildProcessStats stats = child_process_stats_buffer_[name16];
initial.commit09911bf2008-07-26 23:55:291917 if (stats.process_launches) {
1918 int launches = 0;
nsylvain@chromium.org8e50b602009-03-03 22:59:431919 plugin_dict->GetInteger(prefs::kStabilityPluginLaunches, &launches);
initial.commit09911bf2008-07-26 23:55:291920 launches += stats.process_launches;
nsylvain@chromium.org8e50b602009-03-03 22:59:431921 plugin_dict->SetInteger(prefs::kStabilityPluginLaunches, launches);
initial.commit09911bf2008-07-26 23:55:291922 }
1923 if (stats.process_crashes) {
1924 int crashes = 0;
nsylvain@chromium.org8e50b602009-03-03 22:59:431925 plugin_dict->GetInteger(prefs::kStabilityPluginCrashes, &crashes);
initial.commit09911bf2008-07-26 23:55:291926 crashes += stats.process_crashes;
nsylvain@chromium.org8e50b602009-03-03 22:59:431927 plugin_dict->SetInteger(prefs::kStabilityPluginCrashes, crashes);
initial.commit09911bf2008-07-26 23:55:291928 }
1929 if (stats.instances) {
1930 int instances = 0;
nsylvain@chromium.org8e50b602009-03-03 22:59:431931 plugin_dict->GetInteger(prefs::kStabilityPluginInstances, &instances);
initial.commit09911bf2008-07-26 23:55:291932 instances += stats.instances;
nsylvain@chromium.org8e50b602009-03-03 22:59:431933 plugin_dict->SetInteger(prefs::kStabilityPluginInstances, instances);
initial.commit09911bf2008-07-26 23:55:291934 }
bauerb@chromium.orgcd937072012-07-02 09:00:291935 if (stats.loading_errors) {
1936 int loading_errors = 0;
1937 plugin_dict->GetInteger(prefs::kStabilityPluginLoadingErrors,
1938 &loading_errors);
1939 loading_errors += stats.loading_errors;
1940 plugin_dict->SetInteger(prefs::kStabilityPluginLoadingErrors,
1941 loading_errors);
1942 }
initial.commit09911bf2008-07-26 23:55:291943
evan@chromium.org68b9e72b2011-08-05 23:08:221944 child_process_stats_buffer_.erase(name16);
initial.commit09911bf2008-07-26 23:55:291945 }
1946
1947 // Now go through and add dictionaries for plugins that didn't already have
1948 // reports in Local State.
brettw@chromium.orgd2065e062013-12-12 23:49:521949 for (std::map<base::string16, ChildProcessStats>::iterator cache_iter =
jam@chromium.orga27a9382009-02-11 23:55:101950 child_process_stats_buffer_.begin();
1951 cache_iter != child_process_stats_buffer_.end(); ++cache_iter) {
jam@chromium.orga27a9382009-02-11 23:55:101952 ChildProcessStats stats = cache_iter->second;
gregoryd@google.com0d84c5d2009-10-09 01:10:421953
1954 // Insert only plugins information into the plugins list.
petkov@chromium.org8d5f1dae2011-11-11 14:30:411955 if (!IsPluginProcess(stats.process_type))
gregoryd@google.com0d84c5d2009-10-09 01:10:421956 continue;
1957
viettrungluu@chromium.org57ecc4b2010-08-11 03:02:511958 // TODO(viettrungluu): remove conversion
evan@chromium.org68b9e72b2011-08-05 23:08:221959 std::string plugin_name = UTF16ToUTF8(cache_iter->first);
gregoryd@google.com0d84c5d2009-10-09 01:10:421960
initial.commit09911bf2008-07-26 23:55:291961 DictionaryValue* plugin_dict = new DictionaryValue;
1962
nsylvain@chromium.org8e50b602009-03-03 22:59:431963 plugin_dict->SetString(prefs::kStabilityPluginName, plugin_name);
1964 plugin_dict->SetInteger(prefs::kStabilityPluginLaunches,
initial.commit09911bf2008-07-26 23:55:291965 stats.process_launches);
nsylvain@chromium.org8e50b602009-03-03 22:59:431966 plugin_dict->SetInteger(prefs::kStabilityPluginCrashes,
initial.commit09911bf2008-07-26 23:55:291967 stats.process_crashes);
nsylvain@chromium.org8e50b602009-03-03 22:59:431968 plugin_dict->SetInteger(prefs::kStabilityPluginInstances,
initial.commit09911bf2008-07-26 23:55:291969 stats.instances);
bauerb@chromium.orgcd937072012-07-02 09:00:291970 plugin_dict->SetInteger(prefs::kStabilityPluginLoadingErrors,
1971 stats.loading_errors);
initial.commit09911bf2008-07-26 23:55:291972 plugins->Append(plugin_dict);
1973 }
jam@chromium.orga27a9382009-02-11 23:55:101974 child_process_stats_buffer_.clear();
initial.commit09911bf2008-07-26 23:55:291975}
1976
phajdan.jr@chromium.orgdd98f392013-02-04 13:03:221977bool MetricsService::CanLogNotification() {
akalin@chromium.org2c910b72011-03-08 21:16:321978 // We simply don't log anything to UMA if there is a single incognito
initial.commit09911bf2008-07-26 23:55:291979 // session visible. The problem is that we always notify using the orginal
1980 // profile in order to simplify notification processing.
tfarina@chromium.orge764e582012-08-01 03:01:291981 return !chrome::IsOffTheRecordSessionActive();
initial.commit09911bf2008-07-26 23:55:291982}
1983
viettrungluu@chromium.org57ecc4b2010-08-11 03:02:511984void MetricsService::RecordBooleanPrefValue(const char* path, bool value) {
initial.commit09911bf2008-07-26 23:55:291985 DCHECK(IsSingleThreaded());
1986
1987 PrefService* pref = g_browser_process->local_state();
1988 DCHECK(pref);
1989
1990 pref->SetBoolean(path, value);
1991 RecordCurrentState(pref);
1992}
1993
1994void MetricsService::RecordCurrentState(PrefService* pref) {
robertshield@google.com0bb1a622009-03-04 03:22:321995 pref->SetInt64(prefs::kStabilityLastTimestampSec, Time::Now().ToTimeT());
initial.commit09911bf2008-07-26 23:55:291996
1997 RecordPluginChanges(pref);
1998}
1999
petkov@chromium.org8d5f1dae2011-11-11 14:30:412000// static
jam@chromium.orgf3b357692013-03-22 05:16:132001bool MetricsService::IsPluginProcess(int process_type) {
2002 return (process_type == content::PROCESS_TYPE_PLUGIN ||
2003 process_type == content::PROCESS_TYPE_PPAPI_PLUGIN ||
2004 process_type == content::PROCESS_TYPE_PPAPI_BROKER);
petkov@chromium.org8d5f1dae2011-11-11 14:30:412005}
2006
rvargas@google.com5ccaa412009-11-13 22:00:162007#if defined(OS_CHROMEOS)
sky@chromium.org29cf16772010-04-21 15:13:472008void MetricsService::StartExternalMetrics() {
rvargas@google.com5ccaa412009-11-13 22:00:162009 external_metrics_ = new chromeos::ExternalMetrics;
sky@chromium.org29cf16772010-04-21 15:13:472010 external_metrics_->Start();
rvargas@google.com5ccaa412009-11-13 22:00:162011}
2012#endif
sreeram@chromium.org3819f2ee2011-08-21 09:44:382013
2014// static
2015bool MetricsServiceHelper::IsMetricsReportingEnabled() {
2016 bool result = false;
2017 const PrefService* local_state = g_browser_process->local_state();
2018 if (local_state) {
2019 const PrefService::Preference* uma_pref =
2020 local_state->FindPreference(prefs::kMetricsReportingEnabled);
2021 if (uma_pref) {
2022 bool success = uma_pref->GetValue()->GetAsBoolean(&result);
2023 DCHECK(success);
2024 }
2025 }
2026 return result;
2027}