blob: b6bb8f548f8a2e2ddf5d74eacab5b7f5d0f68d6e [file] [log] [blame]
[email protected]d6147bd2014-06-11 01:58:191// Copyright 2014 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//
[email protected]e3eb0c42013-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,
[email protected]e3eb0c42013-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//
[email protected]281d2882009-01-20 20:32:4224// Logs fall into one of two categories: "initial logs," and "ongoing logs."
[email protected]80a8f312013-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)
[email protected]3a668152013-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
[email protected]281d2882009-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
[email protected]80a8f312013-12-16 18:00:3048// to the UMA server. The finalization also acquires the most recent number
[email protected]281d2882009-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
[email protected]80a8f312013-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
[email protected]80a8f312013-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//
[email protected]80a8f312013-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//
[email protected]80a8f312013-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
[email protected]85ed9d42010-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//
[email protected]85ed9d42010-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
[email protected]85ed9d42010-06-08 22:37:44103// the deferred init task is dispatched.
initial.commit09911bf2008-07-26 23:55:29104//
[email protected]80a8f312013-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.
[email protected]cac267c2011-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.
[email protected]0b33f80b2008-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
[email protected]cac267c2011-09-29 15:18:10139// happens, any staged (transmission in progress) log is persisted, and any log
[email protected]410938e02012-10-24 16:33:59140// that is currently accumulating is also finalized and persisted. We then
[email protected]cac267c2011-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//
[email protected]410938e02012-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//
[email protected]cac267c2011-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
[email protected]281d2882009-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
[email protected]d6147bd2014-06-11 01:58:19164#include "components/metrics/metrics_service.h"
[email protected]40bcc302009-03-02 20:50:39165
[email protected]d7c1fa62012-06-15 23:35:30166#include <algorithm>
167
[email protected]7f7f1962011-04-20 15:58:16168#include "base/bind.h"
169#include "base/callback.h"
[email protected]835d7c82010-10-14 04:38:38170#include "base/metrics/histogram.h"
[email protected]acc2ce5512014-05-22 18:29:13171#include "base/metrics/histogram_base.h"
172#include "base/metrics/histogram_samples.h"
[email protected]1026afd2013-03-20 14:28:54173#include "base/metrics/sparse_histogram.h"
[email protected]567d30e2012-07-13 21:48:29174#include "base/metrics/statistics_recorder.h"
[email protected]3853a4c2013-02-11 17:15:57175#include "base/prefs/pref_registry_simple.h"
176#include "base/prefs/pref_service.h"
[email protected]3ea1b182013-02-08 22:38:41177#include "base/strings/string_number_conversions.h"
[email protected]112158af2013-06-07 23:46:18178#include "base/strings/utf_string_conversions.h"
[email protected]ce072a72010-12-31 20:02:16179#include "base/threading/platform_thread.h"
[email protected]b3841c502011-03-09 01:21:31180#include "base/threading/thread.h"
[email protected]3a7b66d2012-04-26 16:34:16181#include "base/threading/thread_restrictions.h"
[email protected]64b8652c2014-07-16 19:14:28182#include "base/time/time.h"
[email protected]ed0fd002012-04-25 23:10:34183#include "base/tracked_objects.h"
[email protected]679082052010-07-21 21:30:13184#include "base/values.h"
[email protected]91b1d912014-06-05 10:52:08185#include "components/metrics/metrics_log.h"
[email protected]064107e2014-05-02 00:59:06186#include "components/metrics/metrics_log_manager.h"
[email protected]0d5a61a82014-05-31 22:28:34187#include "components/metrics/metrics_log_uploader.h"
[email protected]7f07db62014-05-15 01:12:45188#include "components/metrics/metrics_pref_names.h"
[email protected]14bb46692014-05-20 17:16:45189#include "components/metrics/metrics_reporting_scheduler.h"
[email protected]73929422014-05-22 08:19:05190#include "components/metrics/metrics_service_client.h"
[email protected]16a30912014-06-04 00:20:04191#include "components/metrics/metrics_state_manager.h"
[email protected]50ae9f12013-08-29 18:03:22192#include "components/variations/entropy_provider.h"
initial.commit09911bf2008-07-26 23:55:29193
asvitkinecbd420732014-08-26 22:15:40194namespace metrics {
[email protected]e1acf6f2008-10-27 20:43:33195
[email protected]fe58acc22012-02-29 01:29:58196namespace {
[email protected]b2a4812d2012-02-28 05:31:31197
[email protected]fe58acc22012-02-29 01:29:58198// Check to see that we're being called on only one thread.
199bool IsSingleThreaded() {
200 static base::PlatformThreadId thread_id = 0;
201 if (!thread_id)
202 thread_id = base::PlatformThread::CurrentId();
203 return base::PlatformThread::CurrentId() == thread_id;
204}
205
[email protected]7f7f1962011-04-20 15:58:16206// The delay, in seconds, after starting recording before doing expensive
207// initialization work.
[email protected]12180f82012-10-10 21:13:30208#if defined(OS_ANDROID) || defined(OS_IOS)
209// On mobile devices, a significant portion of sessions last less than a minute.
210// Use a shorter timer on these platforms to avoid losing data.
211// TODO(dfalcantara): To avoid delaying startup, tighten up initialization so
212// that it occurs after the user gets their initial page.
213const int kInitializationDelaySeconds = 5;
214#else
[email protected]fe58acc22012-02-29 01:29:58215const int kInitializationDelaySeconds = 30;
[email protected]12180f82012-10-10 21:13:30216#endif
[email protected]252873ef2008-08-04 21:59:45217
[email protected]54702c92011-04-15 15:06:43218// The maximum number of events in a log uploaded to the UMA server.
[email protected]fe58acc22012-02-29 01:29:58219const int kEventLimit = 2400;
[email protected]68475e602008-08-22 03:21:15220
221// If an upload fails, and the transmission was over this byte count, then we
222// will discard the log, and not try to retransmit it. We also don't persist
223// the log to the prefs for transmission during the next chrome session if this
224// limit is exceeded.
[email protected]a63006e2014-06-20 05:22:32225const size_t kUploadLogAvoidRetransmitSize = 100 * 1024;
initial.commit09911bf2008-07-26 23:55:29226
[email protected]fc4252a72012-01-12 21:58:47227// Interval, in minutes, between state saves.
[email protected]fe58acc22012-02-29 01:29:58228const int kSaveStateIntervalMinutes = 5;
229
[email protected]acc2ce5512014-05-22 18:29:13230// The metrics server's URL.
231const char kServerUrl[] = "https://clients4.google.com/uma/v2";
232
233// The MIME type for the uploaded metrics data.
234const char kMimeType[] = "application/vnd.chrome.uma";
235
[email protected]4266def22012-05-17 01:02:40236enum ResponseStatus {
237 UNKNOWN_FAILURE,
238 SUCCESS,
239 BAD_REQUEST, // Invalid syntax or log too large.
[email protected]9f5c1ce82012-05-23 23:11:28240 NO_RESPONSE,
[email protected]4266def22012-05-17 01:02:40241 NUM_RESPONSE_STATUSES
242};
243
244ResponseStatus ResponseCodeToStatus(int response_code) {
245 switch (response_code) {
[email protected]0d5a61a82014-05-31 22:28:34246 case -1:
247 return NO_RESPONSE;
[email protected]4266def22012-05-17 01:02:40248 case 200:
249 return SUCCESS;
250 case 400:
251 return BAD_REQUEST;
252 default:
253 return UNKNOWN_FAILURE;
254 }
255}
256
holted1843d42014-10-09 18:38:52257bool NewInitialMetricsTimingEnabled() {
258 return base::FieldTrialList::FindFullName("UMAInitialMetricsTiming") ==
259 "Enabled";
260}
261
erikwright65b58df2014-09-12 00:05:28262void MarkAppCleanShutdownAndCommit(CleanExitBeacon* clean_exit_beacon,
263 PrefService* local_state) {
264 clean_exit_beacon->WriteBeaconValue(true);
[email protected]d6147bd2014-06-11 01:58:19265 local_state->SetInteger(metrics::prefs::kStabilityExecutionPhase,
[email protected]24f81ca2014-05-26 15:59:34266 MetricsService::SHUTDOWN_COMPLETE);
[email protected]84c384e2013-03-01 23:20:19267 // Start writing right away (write happens on a different thread).
[email protected]24f81ca2014-05-26 15:59:34268 local_state->CommitPendingWrite();
[email protected]84c384e2013-03-01 23:20:19269}
270
[email protected]20f999b52012-08-24 22:32:59271} // namespace
initial.commit09911bf2008-07-26 23:55:29272
[email protected]60677562013-11-17 15:52:55273
[email protected]7a5c07812014-02-26 11:45:41274SyntheticTrialGroup::SyntheticTrialGroup(uint32 trial, uint32 group) {
[email protected]60677562013-11-17 15:52:55275 id.name = trial;
276 id.group = group;
277}
278
279SyntheticTrialGroup::~SyntheticTrialGroup() {
280}
281
[email protected]c0c55e92011-09-10 18:47:30282// static
283MetricsService::ShutdownCleanliness MetricsService::clean_shutdown_status_ =
284 MetricsService::CLEANLY_SHUTDOWN;
285
[email protected]6a6d0d12013-10-28 15:58:19286MetricsService::ExecutionPhase MetricsService::execution_phase_ =
[email protected]6d67ea0d2013-11-14 11:02:21287 MetricsService::UNINITIALIZED_PHASE;
[email protected]6a6d0d12013-10-28 15:58:19288
initial.commit09911bf2008-07-26 23:55:29289// static
[email protected]b1de2c72013-02-06 02:45:47290void MetricsService::RegisterPrefs(PrefRegistrySimple* registry) {
initial.commit09911bf2008-07-26 23:55:29291 DCHECK(IsSingleThreaded());
[email protected]39076642014-05-05 20:32:55292 metrics::MetricsStateManager::RegisterPrefs(registry);
[email protected]91b1d912014-06-05 10:52:08293 MetricsLog::RegisterPrefs(registry);
[email protected]39076642014-05-05 20:32:55294
[email protected]65801452014-07-09 05:42:41295 registry->RegisterInt64Pref(metrics::prefs::kInstallDate, 0);
296
[email protected]d6147bd2014-06-11 01:58:19297 registry->RegisterInt64Pref(metrics::prefs::kStabilityLaunchTimeSec, 0);
298 registry->RegisterInt64Pref(metrics::prefs::kStabilityLastTimestampSec, 0);
299 registry->RegisterStringPref(metrics::prefs::kStabilityStatsVersion,
300 std::string());
301 registry->RegisterInt64Pref(metrics::prefs::kStabilityStatsBuildTime, 0);
302 registry->RegisterBooleanPref(metrics::prefs::kStabilityExitedCleanly, true);
303 registry->RegisterIntegerPref(metrics::prefs::kStabilityExecutionPhase,
[email protected]6d67ea0d2013-11-14 11:02:21304 UNINITIALIZED_PHASE);
[email protected]d6147bd2014-06-11 01:58:19305 registry->RegisterBooleanPref(metrics::prefs::kStabilitySessionEndCompleted,
306 true);
[email protected]16a30912014-06-04 00:20:04307 registry->RegisterIntegerPref(metrics::prefs::kMetricsSessionID, -1);
[email protected]0f2f7792013-11-28 16:09:14308
[email protected]7f07db62014-05-15 01:12:45309 registry->RegisterListPref(metrics::prefs::kMetricsInitialLogs);
310 registry->RegisterListPref(metrics::prefs::kMetricsOngoingLogs);
[email protected]0bb1a622009-03-04 03:22:32311
[email protected]d6147bd2014-06-11 01:58:19312 registry->RegisterInt64Pref(metrics::prefs::kUninstallLaunchCount, 0);
313 registry->RegisterInt64Pref(metrics::prefs::kUninstallMetricsUptimeSec, 0);
initial.commit09911bf2008-07-26 23:55:29314}
315
[email protected]728de072014-05-21 09:20:32316MetricsService::MetricsService(metrics::MetricsStateManager* state_manager,
[email protected]24f81ca2014-05-26 15:59:34317 metrics::MetricsServiceClient* client,
318 PrefService* local_state)
319 : log_manager_(local_state, kUploadLogAvoidRetransmitSize),
[email protected]acc2ce5512014-05-22 18:29:13320 histogram_snapshot_manager_(this),
[email protected]7f07db62014-05-15 01:12:45321 state_manager_(state_manager),
[email protected]728de072014-05-21 09:20:32322 client_(client),
[email protected]24f81ca2014-05-26 15:59:34323 local_state_(local_state),
erikwright65b58df2014-09-12 00:05:28324 clean_exit_beacon_(client->GetRegistryBackupKey(), local_state),
[email protected]37d4709a2014-03-29 03:07:40325 recording_active_(false),
[email protected]d01b8732008-10-16 02:18:07326 reporting_active_(false),
[email protected]410938e02012-10-24 16:33:59327 test_mode_active_(false),
[email protected]d01b8732008-10-16 02:18:07328 state_(INITIALIZED),
[email protected]80a8f312013-12-16 18:00:30329 has_initial_stability_log_(false),
[email protected]0d5a61a82014-05-31 22:28:34330 log_upload_in_progress_(false),
[email protected]d01b8732008-10-16 02:18:07331 idle_since_last_transmission_(false),
[email protected]80a8f312013-12-16 18:00:30332 session_id_(-1),
[email protected]9c009092013-05-01 03:14:09333 self_ptr_factory_(this),
[email protected]0d5a61a82014-05-31 22:28:34334 state_saver_factory_(this) {
initial.commit09911bf2008-07-26 23:55:29335 DCHECK(IsSingleThreaded());
[email protected]39076642014-05-05 20:32:55336 DCHECK(state_manager_);
[email protected]728de072014-05-21 09:20:32337 DCHECK(client_);
[email protected]24f81ca2014-05-26 15:59:34338 DCHECK(local_state_);
[email protected]64b8652c2014-07-16 19:14:28339
340 // Set the install date if this is our first run.
341 int64 install_date = local_state_->GetInt64(metrics::prefs::kInstallDate);
342 if (install_date == 0) {
343 local_state_->SetInt64(metrics::prefs::kInstallDate,
344 base::Time::Now().ToTimeT());
345 }
initial.commit09911bf2008-07-26 23:55:29346}
347
348MetricsService::~MetricsService() {
[email protected]410938e02012-10-24 16:33:59349 DisableRecording();
initial.commit09911bf2008-07-26 23:55:29350}
351
[email protected]39076642014-05-05 20:32:55352void MetricsService::InitializeMetricsRecordingState() {
353 InitializeMetricsState();
[email protected]80a8f312013-12-16 18:00:30354
355 base::Closure callback = base::Bind(&MetricsService::StartScheduledUpload,
356 self_ptr_factory_.GetWeakPtr());
357 scheduler_.reset(new MetricsReportingScheduler(callback));
358}
359
[email protected]d01b8732008-10-16 02:18:07360void MetricsService::Start() {
[email protected]b1c8dc02011-04-13 18:32:04361 HandleIdleSinceLastTransmission(false);
[email protected]410938e02012-10-24 16:33:59362 EnableRecording();
363 EnableReporting();
[email protected]d01b8732008-10-16 02:18:07364}
365
[email protected]39076642014-05-05 20:32:55366bool MetricsService::StartIfMetricsReportingEnabled() {
367 const bool enabled = state_manager_->IsMetricsReportingEnabled();
368 if (enabled)
369 Start();
370 return enabled;
371}
372
[email protected]410938e02012-10-24 16:33:59373void MetricsService::StartRecordingForTests() {
374 test_mode_active_ = true;
375 EnableRecording();
376 DisableReporting();
[email protected]d01b8732008-10-16 02:18:07377}
378
379void MetricsService::Stop() {
[email protected]b1c8dc02011-04-13 18:32:04380 HandleIdleSinceLastTransmission(false);
[email protected]410938e02012-10-24 16:33:59381 DisableReporting();
382 DisableRecording();
383}
384
385void MetricsService::EnableReporting() {
386 if (reporting_active_)
387 return;
388 reporting_active_ = true;
389 StartSchedulerIfNecessary();
390}
391
392void MetricsService::DisableReporting() {
393 reporting_active_ = false;
[email protected]d01b8732008-10-16 02:18:07394}
395
[email protected]edafd4c2011-05-10 17:18:53396std::string MetricsService::GetClientId() {
[email protected]39076642014-05-05 20:32:55397 return state_manager_->client_id();
[email protected]edafd4c2011-05-10 17:18:53398}
399
[email protected]65801452014-07-09 05:42:41400int64 MetricsService::GetInstallDate() {
401 return local_state_->GetInt64(metrics::prefs::kInstallDate);
402}
403
[email protected]20f999b52012-08-24 22:32:59404scoped_ptr<const base::FieldTrial::EntropyProvider>
[email protected]39076642014-05-05 20:32:55405MetricsService::CreateEntropyProvider() {
406 // TODO(asvitkine): Refactor the code so that MetricsService does not expose
407 // this method.
408 return state_manager_->CreateEntropyProvider();
[email protected]5cbeeef72012-02-08 02:05:18409}
410
[email protected]410938e02012-10-24 16:33:59411void MetricsService::EnableRecording() {
initial.commit09911bf2008-07-26 23:55:29412 DCHECK(IsSingleThreaded());
413
[email protected]410938e02012-10-24 16:33:59414 if (recording_active_)
initial.commit09911bf2008-07-26 23:55:29415 return;
[email protected]410938e02012-10-24 16:33:59416 recording_active_ = true;
initial.commit09911bf2008-07-26 23:55:29417
[email protected]39076642014-05-05 20:32:55418 state_manager_->ForceClientIdCreation();
[email protected]9d1b0152014-07-09 18:53:22419 client_->SetMetricsClientId(state_manager_->client_id());
[email protected]410938e02012-10-24 16:33:59420 if (!log_manager_.current_log())
421 OpenNewLog();
[email protected]005ef3e2009-05-22 20:55:46422
[email protected]85791b0b2014-05-20 15:18:58423 for (size_t i = 0; i < metrics_providers_.size(); ++i)
424 metrics_providers_[i]->OnRecordingEnabled();
425
[email protected]e6e30ac2014-01-13 21:24:39426 base::RemoveActionCallback(action_callback_);
[email protected]dd98f392013-02-04 13:03:22427 action_callback_ = base::Bind(&MetricsService::OnUserAction,
428 base::Unretained(this));
[email protected]e6e30ac2014-01-13 21:24:39429 base::AddActionCallback(action_callback_);
[email protected]410938e02012-10-24 16:33:59430}
431
432void MetricsService::DisableRecording() {
433 DCHECK(IsSingleThreaded());
434
435 if (!recording_active_)
436 return;
437 recording_active_ = false;
438
[email protected]e6e30ac2014-01-13 21:24:39439 base::RemoveActionCallback(action_callback_);
[email protected]85791b0b2014-05-20 15:18:58440
441 for (size_t i = 0; i < metrics_providers_.size(); ++i)
442 metrics_providers_[i]->OnRecordingDisabled();
443
[email protected]410938e02012-10-24 16:33:59444 PushPendingLogsToPersistentStorage();
initial.commit09911bf2008-07-26 23:55:29445}
446
[email protected]d01b8732008-10-16 02:18:07447bool MetricsService::recording_active() const {
initial.commit09911bf2008-07-26 23:55:29448 DCHECK(IsSingleThreaded());
[email protected]d01b8732008-10-16 02:18:07449 return recording_active_;
initial.commit09911bf2008-07-26 23:55:29450}
451
[email protected]d01b8732008-10-16 02:18:07452bool MetricsService::reporting_active() const {
453 DCHECK(IsSingleThreaded());
454 return reporting_active_;
initial.commit09911bf2008-07-26 23:55:29455}
456
[email protected]acc2ce5512014-05-22 18:29:13457void MetricsService::RecordDelta(const base::HistogramBase& histogram,
458 const base::HistogramSamples& snapshot) {
459 log_manager_.current_log()->RecordHistogramDelta(histogram.histogram_name(),
460 snapshot);
461}
462
463void MetricsService::InconsistencyDetected(
464 base::HistogramBase::Inconsistency problem) {
465 UMA_HISTOGRAM_ENUMERATION("Histogram.InconsistenciesBrowser",
466 problem, base::HistogramBase::NEVER_EXCEEDED_VALUE);
467}
468
469void MetricsService::UniqueInconsistencyDetected(
470 base::HistogramBase::Inconsistency problem) {
471 UMA_HISTOGRAM_ENUMERATION("Histogram.InconsistenciesBrowserUnique",
472 problem, base::HistogramBase::NEVER_EXCEEDED_VALUE);
473}
474
475void MetricsService::InconsistencyDetectedInLoggedCount(int amount) {
476 UMA_HISTOGRAM_COUNTS("Histogram.InconsistentSnapshotBrowser",
477 std::abs(amount));
478}
479
[email protected]d01b8732008-10-16 02:18:07480void MetricsService::HandleIdleSinceLastTransmission(bool in_idle) {
481 // If there wasn't a lot of action, maybe the computer was asleep, in which
482 // case, the log transmissions should have stopped. Here we start them up
483 // again.
[email protected]cac78842008-11-27 01:02:20484 if (!in_idle && idle_since_last_transmission_)
[email protected]7f7f1962011-04-20 15:58:16485 StartSchedulerIfNecessary();
[email protected]cac78842008-11-27 01:02:20486 idle_since_last_transmission_ = in_idle;
initial.commit09911bf2008-07-26 23:55:29487}
488
[email protected]d7ea39e2014-05-22 03:59:18489void MetricsService::OnApplicationNotIdle() {
490 if (recording_active_)
491 HandleIdleSinceLastTransmission(false);
492}
493
initial.commit09911bf2008-07-26 23:55:29494void MetricsService::RecordStartOfSessionEnd() {
[email protected]466f3c12011-03-23 21:20:38495 LogCleanShutdown();
[email protected]d6147bd2014-06-11 01:58:19496 RecordBooleanPrefValue(metrics::prefs::kStabilitySessionEndCompleted, false);
initial.commit09911bf2008-07-26 23:55:29497}
498
499void MetricsService::RecordCompletedSessionEnd() {
[email protected]466f3c12011-03-23 21:20:38500 LogCleanShutdown();
[email protected]d6147bd2014-06-11 01:58:19501 RecordBooleanPrefValue(metrics::prefs::kStabilitySessionEndCompleted, true);
initial.commit09911bf2008-07-26 23:55:29502}
503
[email protected]410938e02012-10-24 16:33:59504#if defined(OS_ANDROID) || defined(OS_IOS)
[email protected]117fbdf22012-06-26 18:36:39505void MetricsService::OnAppEnterBackground() {
506 scheduler_->Stop();
507
erikwright65b58df2014-09-12 00:05:28508 MarkAppCleanShutdownAndCommit(&clean_exit_beacon_, local_state_);
[email protected]117fbdf22012-06-26 18:36:39509
510 // At this point, there's no way of knowing when the process will be
511 // killed, so this has to be treated similar to a shutdown, closing and
512 // persisting all logs. Unlinke a shutdown, the state is primed to be ready
513 // to continue logging and uploading if the process does return.
[email protected]80a8f312013-12-16 18:00:30514 if (recording_active() && state_ >= SENDING_INITIAL_STABILITY_LOG) {
[email protected]117fbdf22012-06-26 18:36:39515 PushPendingLogsToPersistentStorage();
[email protected]410938e02012-10-24 16:33:59516 // Persisting logs closes the current log, so start recording a new log
517 // immediately to capture any background work that might be done before the
518 // process is killed.
519 OpenNewLog();
[email protected]117fbdf22012-06-26 18:36:39520 }
[email protected]117fbdf22012-06-26 18:36:39521}
522
523void MetricsService::OnAppEnterForeground() {
erikwright65b58df2014-09-12 00:05:28524 clean_exit_beacon_.WriteBeaconValue(false);
[email protected]117fbdf22012-06-26 18:36:39525 StartSchedulerIfNecessary();
526}
[email protected]84c384e2013-03-01 23:20:19527#else
erikwrightcc98a7e02014-09-09 22:05:12528void MetricsService::LogNeedForCleanShutdown() {
erikwright65b58df2014-09-12 00:05:28529 clean_exit_beacon_.WriteBeaconValue(false);
[email protected]84c384e2013-03-01 23:20:19530 // Redundant setting to be sure we call for a clean shutdown.
531 clean_shutdown_status_ = NEED_TO_SHUTDOWN;
532}
533#endif // defined(OS_ANDROID) || defined(OS_IOS)
[email protected]117fbdf22012-06-26 18:36:39534
[email protected]6d67ea0d2013-11-14 11:02:21535// static
[email protected]24f81ca2014-05-26 15:59:34536void MetricsService::SetExecutionPhase(ExecutionPhase execution_phase,
537 PrefService* local_state) {
[email protected]6d67ea0d2013-11-14 11:02:21538 execution_phase_ = execution_phase;
[email protected]d6147bd2014-06-11 01:58:19539 local_state->SetInteger(metrics::prefs::kStabilityExecutionPhase,
540 execution_phase_);
[email protected]6d67ea0d2013-11-14 11:02:21541}
542
[email protected]7f7f1962011-04-20 15:58:16543void MetricsService::RecordBreakpadRegistration(bool success) {
[email protected]68475e602008-08-22 03:21:15544 if (!success)
[email protected]91b1d912014-06-05 10:52:08545 IncrementPrefValue(metrics::prefs::kStabilityBreakpadRegistrationFail);
[email protected]e73c01972008-08-13 00:18:24546 else
[email protected]91b1d912014-06-05 10:52:08547 IncrementPrefValue(metrics::prefs::kStabilityBreakpadRegistrationSuccess);
[email protected]e73c01972008-08-13 00:18:24548}
549
550void MetricsService::RecordBreakpadHasDebugger(bool has_debugger) {
551 if (!has_debugger)
[email protected]91b1d912014-06-05 10:52:08552 IncrementPrefValue(metrics::prefs::kStabilityDebuggerNotPresent);
[email protected]e73c01972008-08-13 00:18:24553 else
[email protected]91b1d912014-06-05 10:52:08554 IncrementPrefValue(metrics::prefs::kStabilityDebuggerPresent);
[email protected]e73c01972008-08-13 00:18:24555}
556
initial.commit09911bf2008-07-26 23:55:29557//------------------------------------------------------------------------------
558// private methods
559//------------------------------------------------------------------------------
560
561
562//------------------------------------------------------------------------------
563// Initialization methods
564
[email protected]39076642014-05-05 20:32:55565void MetricsService::InitializeMetricsState() {
asvitkineff3e2a62014-09-18 22:01:49566 const int64 buildtime = MetricsLog::GetBuildTime();
567 const std::string version = client_->GetVersionString();
568 bool version_changed = false;
569 if (local_state_->GetInt64(prefs::kStabilityStatsBuildTime) != buildtime ||
570 local_state_->GetString(prefs::kStabilityStatsVersion) != version) {
571 local_state_->SetString(metrics::prefs::kStabilityStatsVersion, version);
572 local_state_->SetInt64(metrics::prefs::kStabilityStatsBuildTime, buildtime);
573 version_changed = true;
574 }
initial.commit09911bf2008-07-26 23:55:29575
[email protected]94dce122014-07-16 04:20:12576 log_manager_.LoadPersistedUnsentLogs();
577
[email protected]16a30912014-06-04 00:20:04578 session_id_ = local_state_->GetInteger(metrics::prefs::kMetricsSessionID);
erikwright65b58df2014-09-12 00:05:28579
580 if (!clean_exit_beacon_.exited_cleanly()) {
[email protected]91b1d912014-06-05 10:52:08581 IncrementPrefValue(metrics::prefs::kStabilityCrashCount);
[email protected]c0c55e92011-09-10 18:47:30582 // Reset flag, and wait until we call LogNeedForCleanShutdown() before
583 // monitoring.
erikwright65b58df2014-09-12 00:05:28584 clean_exit_beacon_.WriteBeaconValue(true);
siggic179dd062014-09-10 17:02:31585 }
[email protected]6a6d0d12013-10-28 15:58:19586
erikwright65b58df2014-09-12 00:05:28587 if (!clean_exit_beacon_.exited_cleanly() || ProvidersHaveStabilityMetrics()) {
[email protected]6a6d0d12013-10-28 15:58:19588 // TODO(rtenneti): On windows, consider saving/getting execution_phase from
589 // the registry.
[email protected]24f81ca2014-05-26 15:59:34590 int execution_phase =
[email protected]d6147bd2014-06-11 01:58:19591 local_state_->GetInteger(metrics::prefs::kStabilityExecutionPhase);
[email protected]6d67ea0d2013-11-14 11:02:21592 UMA_HISTOGRAM_SPARSE_SLOWLY("Chrome.Browser.CrashedExecutionPhase",
[email protected]6a6d0d12013-10-28 15:58:19593 execution_phase);
[email protected]80a8f312013-12-16 18:00:30594
siggic179dd062014-09-10 17:02:31595 // If the previous session didn't exit cleanly, or if any provider
596 // explicitly requests it, prepare an initial stability log -
597 // provided UMA is enabled.
[email protected]39076642014-05-05 20:32:55598 if (state_manager_->IsMetricsReportingEnabled())
[email protected]80a8f312013-12-16 18:00:30599 PrepareInitialStabilityLog();
initial.commit09911bf2008-07-26 23:55:29600 }
[email protected]80a8f312013-12-16 18:00:30601
asvitkineff3e2a62014-09-18 22:01:49602 // If no initial stability log was generated and there was a version upgrade,
603 // clear the stability stats from the previous version (so that they don't get
604 // attributed to the current version). This could otherwise happen due to a
605 // number of different edge cases, such as if the last version crashed before
606 // it could save off a system profile or if UMA reporting is disabled (which
607 // normally results in stats being accumulated).
608 if (!has_initial_stability_log_ && version_changed) {
609 for (size_t i = 0; i < metrics_providers_.size(); ++i)
610 metrics_providers_[i]->ClearSavedStabilityMetrics();
611
612 // Reset the prefs that are managed by MetricsService/MetricsLog directly.
613 local_state_->SetInteger(prefs::kStabilityCrashCount, 0);
614 local_state_->SetInteger(prefs::kStabilityExecutionPhase,
615 UNINITIALIZED_PHASE);
616 local_state_->SetInteger(prefs::kStabilityIncompleteSessionEndCount, 0);
617 local_state_->SetInteger(prefs::kStabilityLaunchCount, 0);
618 local_state_->SetBoolean(prefs::kStabilitySessionEndCompleted, true);
619 }
620
[email protected]80a8f312013-12-16 18:00:30621 // Update session ID.
622 ++session_id_;
[email protected]16a30912014-06-04 00:20:04623 local_state_->SetInteger(metrics::prefs::kMetricsSessionID, session_id_);
[email protected]80a8f312013-12-16 18:00:30624
625 // Stability bookkeeping
[email protected]91b1d912014-06-05 10:52:08626 IncrementPrefValue(metrics::prefs::kStabilityLaunchCount);
[email protected]80a8f312013-12-16 18:00:30627
[email protected]6d67ea0d2013-11-14 11:02:21628 DCHECK_EQ(UNINITIALIZED_PHASE, execution_phase_);
[email protected]24f81ca2014-05-26 15:59:34629 SetExecutionPhase(START_METRICS_RECORDING, local_state_);
[email protected]e73c01972008-08-13 00:18:24630
[email protected]d6147bd2014-06-11 01:58:19631 if (!local_state_->GetBoolean(
632 metrics::prefs::kStabilitySessionEndCompleted)) {
[email protected]91b1d912014-06-05 10:52:08633 IncrementPrefValue(metrics::prefs::kStabilityIncompleteSessionEndCount);
[email protected]c9abf242009-07-18 06:00:38634 // This is marked false when we get a WM_ENDSESSION.
[email protected]d6147bd2014-06-11 01:58:19635 local_state_->SetBoolean(metrics::prefs::kStabilitySessionEndCompleted,
636 true);
initial.commit09911bf2008-07-26 23:55:29637 }
initial.commit09911bf2008-07-26 23:55:29638
[email protected]076961c2014-03-12 22:23:56639 // Call GetUptimes() for the first time, thus allowing all later calls
640 // to record incremental uptimes accurately.
641 base::TimeDelta ignored_uptime_parameter;
642 base::TimeDelta startup_uptime;
[email protected]24f81ca2014-05-26 15:59:34643 GetUptimes(local_state_, &startup_uptime, &ignored_uptime_parameter);
[email protected]c68a2b9b2013-10-09 18:16:36644 DCHECK_EQ(0, startup_uptime.InMicroseconds());
[email protected]9165f742010-03-10 22:55:01645 // For backwards compatibility, leave this intact in case Omaha is checking
[email protected]d6147bd2014-06-11 01:58:19646 // them. metrics::prefs::kStabilityLastTimestampSec may also be useless now.
[email protected]9165f742010-03-10 22:55:01647 // TODO(jar): Delete these if they have no uses.
[email protected]d6147bd2014-06-11 01:58:19648 local_state_->SetInt64(metrics::prefs::kStabilityLaunchTimeSec,
asvitkinecbd420732014-08-26 22:15:40649 base::Time::Now().ToTimeT());
[email protected]0bb1a622009-03-04 03:22:32650
651 // Bookkeeping for the uninstall metrics.
[email protected]d6147bd2014-06-11 01:58:19652 IncrementLongPrefsValue(metrics::prefs::kUninstallLaunchCount);
initial.commit09911bf2008-07-26 23:55:29653
initial.commit09911bf2008-07-26 23:55:29654 // Kick off the process of saving the state (so the uptime numbers keep
655 // getting updated) every n minutes.
656 ScheduleNextStateSave();
657}
658
[email protected]dd98f392013-02-04 13:03:22659void MetricsService::OnUserAction(const std::string& action) {
[email protected]e5ad60a2014-03-11 03:54:04660 if (!ShouldLogEvents())
[email protected]dd98f392013-02-04 13:03:22661 return;
662
[email protected]4426d2d2014-04-09 12:33:00663 log_manager_.current_log()->RecordUserAction(action);
[email protected]dd98f392013-02-04 13:03:22664 HandleIdleSinceLastTransmission(false);
665}
666
[email protected]4a55a712014-06-08 16:50:34667void MetricsService::FinishedGatheringInitialMetrics() {
[email protected]ed0fd002012-04-25 23:10:34668 DCHECK_EQ(INIT_TASK_SCHEDULED, state_);
[email protected]c68a2b9b2013-10-09 18:16:36669 state_ = INIT_TASK_DONE;
[email protected]83d09f92014-06-03 14:58:26670
671 // Create the initial log.
672 if (!initial_metrics_log_.get()) {
673 initial_metrics_log_ = CreateLog(MetricsLog::ONGOING_LOG);
674 NotifyOnDidCreateMetricsLog();
675 }
676
[email protected]70886cd2013-12-04 05:53:42677 scheduler_->InitTaskComplete();
[email protected]c68a2b9b2013-10-09 18:16:36678}
679
[email protected]076961c2014-03-12 22:23:56680void MetricsService::GetUptimes(PrefService* pref,
681 base::TimeDelta* incremental_uptime,
682 base::TimeDelta* uptime) {
[email protected]c68a2b9b2013-10-09 18:16:36683 base::TimeTicks now = base::TimeTicks::Now();
[email protected]076961c2014-03-12 22:23:56684 // If this is the first call, init |first_updated_time_| and
685 // |last_updated_time_|.
686 if (last_updated_time_.is_null()) {
687 first_updated_time_ = now;
[email protected]c68a2b9b2013-10-09 18:16:36688 last_updated_time_ = now;
[email protected]076961c2014-03-12 22:23:56689 }
690 *incremental_uptime = now - last_updated_time_;
691 *uptime = now - first_updated_time_;
[email protected]c68a2b9b2013-10-09 18:16:36692 last_updated_time_ = now;
693
[email protected]076961c2014-03-12 22:23:56694 const int64 incremental_time_secs = incremental_uptime->InSeconds();
[email protected]c68a2b9b2013-10-09 18:16:36695 if (incremental_time_secs > 0) {
[email protected]d6147bd2014-06-11 01:58:19696 int64 metrics_uptime =
697 pref->GetInt64(metrics::prefs::kUninstallMetricsUptimeSec);
[email protected]c68a2b9b2013-10-09 18:16:36698 metrics_uptime += incremental_time_secs;
[email protected]d6147bd2014-06-11 01:58:19699 pref->SetInt64(metrics::prefs::kUninstallMetricsUptimeSec, metrics_uptime);
[email protected]c68a2b9b2013-10-09 18:16:36700 }
initial.commit09911bf2008-07-26 23:55:29701}
702
[email protected]2a321de32014-05-10 19:59:06703void MetricsService::NotifyOnDidCreateMetricsLog() {
asvitkinebbde62b2014-09-03 16:33:21704 DCHECK(IsSingleThreaded());
[email protected]8304f61a2014-05-24 12:17:33705 for (size_t i = 0; i < metrics_providers_.size(); ++i)
706 metrics_providers_[i]->OnDidCreateMetricsLog();
[email protected]2a321de32014-05-10 19:59:06707}
708
initial.commit09911bf2008-07-26 23:55:29709//------------------------------------------------------------------------------
710// State save methods
711
712void MetricsService::ScheduleNextStateSave() {
[email protected]8454aeb2011-11-19 23:38:20713 state_saver_factory_.InvalidateWeakPtrs();
initial.commit09911bf2008-07-26 23:55:29714
[email protected]b3a25092013-05-28 22:08:16715 base::MessageLoop::current()->PostDelayedTask(FROM_HERE,
[email protected]8454aeb2011-11-19 23:38:20716 base::Bind(&MetricsService::SaveLocalState,
717 state_saver_factory_.GetWeakPtr()),
[email protected]fc4252a72012-01-12 21:58:47718 base::TimeDelta::FromMinutes(kSaveStateIntervalMinutes));
initial.commit09911bf2008-07-26 23:55:29719}
720
721void MetricsService::SaveLocalState() {
[email protected]24f81ca2014-05-26 15:59:34722 RecordCurrentState(local_state_);
initial.commit09911bf2008-07-26 23:55:29723
[email protected]fc4252a72012-01-12 21:58:47724 // TODO(jar):110021 Does this run down the batteries????
initial.commit09911bf2008-07-26 23:55:29725 ScheduleNextStateSave();
726}
727
728
729//------------------------------------------------------------------------------
730// Recording control methods
731
[email protected]410938e02012-10-24 16:33:59732void MetricsService::OpenNewLog() {
733 DCHECK(!log_manager_.current_log());
initial.commit09911bf2008-07-26 23:55:29734
[email protected]bfb77b52014-06-07 01:54:01735 log_manager_.BeginLoggingWithLog(CreateLog(MetricsLog::ONGOING_LOG));
[email protected]2a321de32014-05-10 19:59:06736 NotifyOnDidCreateMetricsLog();
initial.commit09911bf2008-07-26 23:55:29737 if (state_ == INITIALIZED) {
738 // We only need to schedule that run once.
[email protected]85ed9d42010-06-08 22:37:44739 state_ = INIT_TASK_SCHEDULED;
initial.commit09911bf2008-07-26 23:55:29740
[email protected]d6147bd2014-06-11 01:58:19741 base::MessageLoop::current()->PostDelayedTask(
[email protected]ed10dd12011-12-07 12:03:42742 FROM_HERE,
[email protected]51994b22014-05-30 13:24:21743 base::Bind(&MetricsService::StartGatheringMetrics,
744 self_ptr_factory_.GetWeakPtr()),
[email protected]7e560102012-03-08 20:58:42745 base::TimeDelta::FromSeconds(kInitializationDelaySeconds));
initial.commit09911bf2008-07-26 23:55:29746 }
747}
748
[email protected]51994b22014-05-30 13:24:21749void MetricsService::StartGatheringMetrics() {
[email protected]51994b22014-05-30 13:24:21750 client_->StartGatheringMetrics(
[email protected]4a55a712014-06-08 16:50:34751 base::Bind(&MetricsService::FinishedGatheringInitialMetrics,
[email protected]51994b22014-05-30 13:24:21752 self_ptr_factory_.GetWeakPtr()));
753}
754
[email protected]410938e02012-10-24 16:33:59755void MetricsService::CloseCurrentLog() {
[email protected]cac267c2011-09-29 15:18:10756 if (!log_manager_.current_log())
initial.commit09911bf2008-07-26 23:55:29757 return;
758
[email protected]68475e602008-08-22 03:21:15759 // TODO(jar): Integrate bounds on log recording more consistently, so that we
760 // can stop recording logs that are too big much sooner.
[email protected]cac267c2011-09-29 15:18:10761 if (log_manager_.current_log()->num_events() > kEventLimit) {
[email protected]553dba62009-02-24 19:08:23762 UMA_HISTOGRAM_COUNTS("UMA.Discarded Log Events",
[email protected]cac267c2011-09-29 15:18:10763 log_manager_.current_log()->num_events());
764 log_manager_.DiscardCurrentLog();
[email protected]410938e02012-10-24 16:33:59765 OpenNewLog(); // Start trivial log to hold our histograms.
[email protected]68475e602008-08-22 03:21:15766 }
767
[email protected]0b33f80b2008-12-17 21:34:36768 // Put incremental data (histogram deltas, and realtime stats deltas) at the
[email protected]147bbc0b2009-01-06 19:37:40769 // end of all log transmissions (initial log handles this separately).
[email protected]024b5cd2011-05-27 03:29:38770 // RecordIncrementalStabilityElements only exists on the derived
771 // MetricsLog class.
[email protected]94dce122014-07-16 04:20:12772 MetricsLog* current_log = log_manager_.current_log();
[email protected]024b5cd2011-05-27 03:29:38773 DCHECK(current_log);
asvitkine88aa9332014-09-29 23:29:17774 RecordCurrentEnvironment(current_log);
[email protected]076961c2014-03-12 22:23:56775 base::TimeDelta incremental_uptime;
776 base::TimeDelta uptime;
[email protected]24f81ca2014-05-26 15:59:34777 GetUptimes(local_state_, &incremental_uptime, &uptime);
[email protected]85791b0b2014-05-20 15:18:58778 current_log->RecordStabilityMetrics(metrics_providers_.get(),
779 incremental_uptime, uptime);
[email protected]60677562013-11-17 15:52:55780
[email protected]024b5cd2011-05-27 03:29:38781 RecordCurrentHistograms();
[email protected]85791b0b2014-05-20 15:18:58782 current_log->RecordGeneralMetrics(metrics_providers_.get());
initial.commit09911bf2008-07-26 23:55:29783
[email protected]29948262012-03-01 12:15:08784 log_manager_.FinishCurrentLog();
initial.commit09911bf2008-07-26 23:55:29785}
786
[email protected]cac267c2011-09-29 15:18:10787void MetricsService::PushPendingLogsToPersistentStorage() {
[email protected]80a8f312013-12-16 18:00:30788 if (state_ < SENDING_INITIAL_STABILITY_LOG)
[email protected]28ab7f92009-01-06 21:39:04789 return; // We didn't and still don't have time to get plugin list etc.
initial.commit09911bf2008-07-26 23:55:29790
[email protected]410938e02012-10-24 16:33:59791 CloseCurrentLog();
[email protected]80a8f312013-12-16 18:00:30792 log_manager_.PersistUnsentLogs();
[email protected]7d41ae6d2012-06-26 08:53:03793
794 // If there was a staged and/or current log, then there is now at least one
795 // log waiting to be uploaded.
796 if (log_manager_.has_unsent_logs())
797 state_ = SENDING_OLD_LOGS;
initial.commit09911bf2008-07-26 23:55:29798}
799
800//------------------------------------------------------------------------------
801// Transmission of logs methods
802
[email protected]7f7f1962011-04-20 15:58:16803void MetricsService::StartSchedulerIfNecessary() {
[email protected]410938e02012-10-24 16:33:59804 // Never schedule cutting or uploading of logs in test mode.
805 if (test_mode_active_)
806 return;
807
808 // Even if reporting is disabled, the scheduler is needed to trigger the
809 // creation of the initial log, which must be done in order for any logs to be
810 // persisted on shutdown or backgrounding.
[email protected]80a8f312013-12-16 18:00:30811 if (recording_active() &&
812 (reporting_active() || state_ < SENDING_INITIAL_STABILITY_LOG)) {
[email protected]7f7f1962011-04-20 15:58:16813 scheduler_->Start();
[email protected]80a8f312013-12-16 18:00:30814 }
initial.commit09911bf2008-07-26 23:55:29815}
816
[email protected]7f7f1962011-04-20 15:58:16817void MetricsService::StartScheduledUpload() {
[email protected]cd1ac712012-06-26 08:26:47818 // If we're getting no notifications, then the log won't have much in it, and
819 // it's possible the computer is about to go to sleep, so don't upload and
820 // stop the scheduler.
[email protected]410938e02012-10-24 16:33:59821 // If recording has been turned off, the scheduler doesn't need to run.
822 // If reporting is off, proceed if the initial log hasn't been created, since
823 // that has to happen in order for logs to be cut and stored when persisting.
[email protected]d7ea39e2014-05-22 03:59:18824 // TODO(stuartmorgan): Call Stop() on the scheduler when reporting and/or
[email protected]cd1ac712012-06-26 08:26:47825 // recording are turned off instead of letting it fire and then aborting.
826 if (idle_since_last_transmission_ ||
[email protected]410938e02012-10-24 16:33:59827 !recording_active() ||
[email protected]80a8f312013-12-16 18:00:30828 (!reporting_active() && state_ >= SENDING_INITIAL_STABILITY_LOG)) {
[email protected]7f7f1962011-04-20 15:58:16829 scheduler_->Stop();
830 scheduler_->UploadCancelled();
831 return;
832 }
833
[email protected]c15faf372012-07-11 06:01:34834 // If the callback was to upload an old log, but there no longer is one,
835 // just report success back to the scheduler to begin the ongoing log
836 // callbacks.
837 // TODO(stuartmorgan): Consider removing the distinction between
838 // SENDING_OLD_LOGS and SENDING_CURRENT_LOGS to simplify the state machine
839 // now that the log upload flow is the same for both modes.
840 if (state_ == SENDING_OLD_LOGS && !log_manager_.has_unsent_logs()) {
841 state_ = SENDING_CURRENT_LOGS;
842 scheduler_->UploadFinished(true /* healthy */, false /* no unsent logs */);
843 return;
844 }
[email protected]cd1ac712012-06-26 08:26:47845 // If there are unsent logs, send the next one. If not, start the asynchronous
846 // process of finalizing the current log for upload.
847 if (state_ == SENDING_OLD_LOGS) {
848 DCHECK(log_manager_.has_unsent_logs());
849 log_manager_.StageNextLogForUpload();
850 SendStagedLog();
851 } else {
[email protected]4b4892b2014-05-22 15:06:15852 client_->CollectFinalMetrics(
853 base::Bind(&MetricsService::OnFinalLogInfoCollectionDone,
854 self_ptr_factory_.GetWeakPtr()));
[email protected]cd1ac712012-06-26 08:26:47855 }
[email protected]29948262012-03-01 12:15:08856}
857
[email protected]29948262012-03-01 12:15:08858void MetricsService::OnFinalLogInfoCollectionDone() {
[email protected]0d5a61a82014-05-31 22:28:34859 // If somehow there is a log upload in progress, we return and hope things
860 // work out. The scheduler isn't informed since if this happens, the scheduler
[email protected]7f7f1962011-04-20 15:58:16861 // will get a response from the upload.
[email protected]0d5a61a82014-05-31 22:28:34862 DCHECK(!log_upload_in_progress_);
863 if (log_upload_in_progress_)
[email protected]7f7f1962011-04-20 15:58:16864 return;
865
[email protected]cd1ac712012-06-26 08:26:47866 // Abort if metrics were turned off during the final info gathering.
[email protected]410938e02012-10-24 16:33:59867 if (!recording_active()) {
[email protected]7f7f1962011-04-20 15:58:16868 scheduler_->Stop();
869 scheduler_->UploadCancelled();
[email protected]d01b8732008-10-16 02:18:07870 return;
871 }
872
[email protected]cd1ac712012-06-26 08:26:47873 StageNewLog();
[email protected]410938e02012-10-24 16:33:59874
875 // If logs shouldn't be uploaded, stop here. It's important that this check
876 // be after StageNewLog(), otherwise the previous logs will never be loaded,
877 // and thus the open log won't be persisted.
878 // TODO(stuartmorgan): This is unnecessarily complicated; restructure loading
879 // of previous logs to not require running part of the upload logic.
880 // http://crbug.com/157337
881 if (!reporting_active()) {
882 scheduler_->Stop();
883 scheduler_->UploadCancelled();
884 return;
885 }
886
[email protected]29948262012-03-01 12:15:08887 SendStagedLog();
888}
889
[email protected]cd1ac712012-06-26 08:26:47890void MetricsService::StageNewLog() {
[email protected]29948262012-03-01 12:15:08891 if (log_manager_.has_staged_log())
892 return;
893
894 switch (state_) {
895 case INITIALIZED:
896 case INIT_TASK_SCHEDULED: // We should be further along by now.
[email protected]dc61fe92012-06-12 00:13:50897 NOTREACHED();
[email protected]29948262012-03-01 12:15:08898 return;
899
900 case INIT_TASK_DONE:
holted1843d42014-10-09 18:38:52901 if (NewInitialMetricsTimingEnabled()) {
[email protected]b58b8b22014-04-08 22:40:33902 PrepareInitialMetricsLog();
holted1843d42014-10-09 18:38:52903 // Stage the first log, which could be a stability log (either one
904 // for created in this session or from a previous session) or the
905 // initial metrics log that was just created.
906 log_manager_.StageNextLogForUpload();
907 if (has_initial_stability_log_) {
908 // The initial stability log was just staged.
909 has_initial_stability_log_ = false;
910 state_ = SENDING_INITIAL_STABILITY_LOG;
911 } else {
912 state_ = SENDING_INITIAL_METRICS_LOG;
913 }
914 } else {
915 if (has_initial_stability_log_) {
916 // There's an initial stability log, ready to send.
917 log_manager_.StageNextLogForUpload();
918 has_initial_stability_log_ = false;
919 state_ = SENDING_INITIAL_STABILITY_LOG;
920 } else {
921 PrepareInitialMetricsLog();
922 log_manager_.StageNextLogForUpload();
923 state_ = SENDING_INITIAL_METRICS_LOG;
924 }
[email protected]80a8f312013-12-16 18:00:30925 }
[email protected]29948262012-03-01 12:15:08926 break;
927
928 case SENDING_OLD_LOGS:
[email protected]cd1ac712012-06-26 08:26:47929 NOTREACHED(); // Shouldn't be staging a new log during old log sending.
930 return;
[email protected]29948262012-03-01 12:15:08931
932 case SENDING_CURRENT_LOGS:
[email protected]410938e02012-10-24 16:33:59933 CloseCurrentLog();
934 OpenNewLog();
[email protected]29948262012-03-01 12:15:08935 log_manager_.StageNextLogForUpload();
936 break;
937
938 default:
939 NOTREACHED();
940 return;
941 }
942
943 DCHECK(log_manager_.has_staged_log());
944}
945
siggic179dd062014-09-10 17:02:31946bool MetricsService::ProvidersHaveStabilityMetrics() {
947 // Check whether any metrics provider has stability metrics.
948 for (size_t i = 0; i < metrics_providers_.size(); ++i) {
949 if (metrics_providers_[i]->HasStabilityMetrics())
950 return true;
951 }
952
953 return false;
954}
955
[email protected]80a8f312013-12-16 18:00:30956void MetricsService::PrepareInitialStabilityLog() {
957 DCHECK_EQ(INITIALIZED, state_);
[email protected]29948262012-03-01 12:15:08958
[email protected]80a8f312013-12-16 18:00:30959 scoped_ptr<MetricsLog> initial_stability_log(
[email protected]09dee82d2014-05-22 14:00:53960 CreateLog(MetricsLog::INITIAL_STABILITY_LOG));
[email protected]2a321de32014-05-10 19:59:06961
962 // Do not call NotifyOnDidCreateMetricsLog here because the stability
963 // log describes stats from the _previous_ session.
964
[email protected]80a8f312013-12-16 18:00:30965 if (!initial_stability_log->LoadSavedEnvironmentFromPrefs())
966 return;
[email protected]85791b0b2014-05-20 15:18:58967
[email protected]80a8f312013-12-16 18:00:30968 log_manager_.PauseCurrentLog();
[email protected]bfb77b52014-06-07 01:54:01969 log_manager_.BeginLoggingWithLog(initial_stability_log.Pass());
[email protected]85791b0b2014-05-20 15:18:58970
971 // Note: Some stability providers may record stability stats via histograms,
972 // so this call has to be after BeginLoggingWithLog().
[email protected]bfb77b52014-06-07 01:54:01973 log_manager_.current_log()->RecordStabilityMetrics(
974 metrics_providers_.get(), base::TimeDelta(), base::TimeDelta());
[email protected]c778687a2014-02-11 14:46:45975 RecordCurrentStabilityHistograms();
[email protected]85791b0b2014-05-20 15:18:58976
977 // Note: RecordGeneralMetrics() intentionally not called since this log is for
978 // stability stats from a previous session only.
979
[email protected]80a8f312013-12-16 18:00:30980 log_manager_.FinishCurrentLog();
981 log_manager_.ResumePausedLog();
982
983 // Store unsent logs, including the stability log that was just saved, so
984 // that they're not lost in case of a crash before upload time.
985 log_manager_.PersistUnsentLogs();
986
987 has_initial_stability_log_ = true;
988}
989
[email protected]b58b8b22014-04-08 22:40:33990void MetricsService::PrepareInitialMetricsLog() {
[email protected]80a8f312013-12-16 18:00:30991 DCHECK(state_ == INIT_TASK_DONE || state_ == SENDING_INITIAL_STABILITY_LOG);
[email protected]0edf8762013-11-21 18:33:30992
asvitkine88aa9332014-09-29 23:29:17993 RecordCurrentEnvironment(initial_metrics_log_.get());
[email protected]076961c2014-03-12 22:23:56994 base::TimeDelta incremental_uptime;
995 base::TimeDelta uptime;
[email protected]24f81ca2014-05-26 15:59:34996 GetUptimes(local_state_, &incremental_uptime, &uptime);
[email protected]29948262012-03-01 12:15:08997
998 // Histograms only get written to the current log, so make the new log current
999 // before writing them.
1000 log_manager_.PauseCurrentLog();
[email protected]bfb77b52014-06-07 01:54:011001 log_manager_.BeginLoggingWithLog(initial_metrics_log_.Pass());
[email protected]85791b0b2014-05-20 15:18:581002
1003 // Note: Some stability providers may record stability stats via histograms,
1004 // so this call has to be after BeginLoggingWithLog().
[email protected]94dce122014-07-16 04:20:121005 MetricsLog* current_log = log_manager_.current_log();
[email protected]85791b0b2014-05-20 15:18:581006 current_log->RecordStabilityMetrics(metrics_providers_.get(),
1007 base::TimeDelta(), base::TimeDelta());
[email protected]29948262012-03-01 12:15:081008 RecordCurrentHistograms();
[email protected]85791b0b2014-05-20 15:18:581009
1010 current_log->RecordGeneralMetrics(metrics_providers_.get());
1011
[email protected]29948262012-03-01 12:15:081012 log_manager_.FinishCurrentLog();
1013 log_manager_.ResumePausedLog();
1014
[email protected]94dce122014-07-16 04:20:121015 // Store unsent logs, including the initial log that was just saved, so
1016 // that they're not lost in case of a crash before upload time.
1017 log_manager_.PersistUnsentLogs();
[email protected]29948262012-03-01 12:15:081018}
1019
[email protected]29948262012-03-01 12:15:081020void MetricsService::SendStagedLog() {
1021 DCHECK(log_manager_.has_staged_log());
[email protected]0d5a61a82014-05-31 22:28:341022 if (!log_manager_.has_staged_log())
1023 return;
[email protected]29948262012-03-01 12:15:081024
[email protected]0d5a61a82014-05-31 22:28:341025 DCHECK(!log_upload_in_progress_);
1026 log_upload_in_progress_ = true;
[email protected]d01b8732008-10-16 02:18:071027
[email protected]0d5a61a82014-05-31 22:28:341028 if (!log_uploader_) {
1029 log_uploader_ = client_->CreateUploader(
1030 kServerUrl, kMimeType,
1031 base::Bind(&MetricsService::OnLogUploadComplete,
1032 self_ptr_factory_.GetWeakPtr()));
1033 }
1034
1035 const std::string hash =
1036 base::HexEncode(log_manager_.staged_log_hash().data(),
1037 log_manager_.staged_log_hash().size());
1038 bool success = log_uploader_->UploadLog(log_manager_.staged_log(), hash);
1039 UMA_HISTOGRAM_BOOLEAN("UMA.UploadCreation", success);
1040 if (!success) {
[email protected]dc61fe92012-06-12 00:13:501041 // Skip this upload and hope things work out next time.
[email protected]cac267c2011-09-29 15:18:101042 log_manager_.DiscardStagedLog();
[email protected]7f7f1962011-04-20 15:58:161043 scheduler_->UploadCancelled();
[email protected]0d5a61a82014-05-31 22:28:341044 log_upload_in_progress_ = false;
[email protected]d01b8732008-10-16 02:18:071045 return;
1046 }
1047
[email protected]d01b8732008-10-16 02:18:071048 HandleIdleSinceLastTransmission(true);
1049}
1050
[email protected]cac78842008-11-27 01:02:201051
[email protected]0d5a61a82014-05-31 22:28:341052void MetricsService::OnLogUploadComplete(int response_code) {
1053 DCHECK(log_upload_in_progress_);
1054 log_upload_in_progress_ = false;
[email protected]fe58acc22012-02-29 01:29:581055
[email protected]dc61fe92012-06-12 00:13:501056 // Log a histogram to track response success vs. failure rates.
[email protected]e3eb0c42013-04-18 06:18:581057 UMA_HISTOGRAM_ENUMERATION("UMA.UploadResponseStatus.Protobuf",
1058 ResponseCodeToStatus(response_code),
1059 NUM_RESPONSE_STATUSES);
[email protected]fe58acc22012-02-29 01:29:581060
[email protected]dc61fe92012-06-12 00:13:501061 bool upload_succeeded = response_code == 200;
[email protected]7f7f1962011-04-20 15:58:161062
[email protected]0eb34fee2009-01-21 08:04:381063 // Provide boolean for error recovery (allow us to ignore response_code).
[email protected]dc6f4962009-02-13 01:25:501064 bool discard_log = false;
[email protected]7f07db62014-05-15 01:12:451065 const size_t log_size = log_manager_.staged_log().length();
asvitkined80ebf6c2014-09-02 22:29:111066 if (upload_succeeded) {
1067 UMA_HISTOGRAM_COUNTS_10000("UMA.LogSize.OnSuccess", log_size / 1024);
1068 } else if (log_size > kUploadLogAvoidRetransmitSize) {
[email protected]dc61fe92012-06-12 00:13:501069 UMA_HISTOGRAM_COUNTS("UMA.Large Rejected Log was Discarded",
1070 static_cast<int>(log_size));
[email protected]0eb34fee2009-01-21 08:04:381071 discard_log = true;
[email protected]dc61fe92012-06-12 00:13:501072 } else if (response_code == 400) {
[email protected]0eb34fee2009-01-21 08:04:381073 // Bad syntax. Retransmission won't work.
[email protected]0eb34fee2009-01-21 08:04:381074 discard_log = true;
[email protected]68475e602008-08-22 03:21:151075 }
1076
[email protected]94dce122014-07-16 04:20:121077 if (upload_succeeded || discard_log) {
[email protected]5f3e1642013-05-05 03:37:341078 log_manager_.DiscardStagedLog();
[email protected]94dce122014-07-16 04:20:121079 // Store the updated list to disk now that the removed log is uploaded.
1080 log_manager_.PersistUnsentLogs();
1081 }
[email protected]dc61fe92012-06-12 00:13:501082
[email protected]dc61fe92012-06-12 00:13:501083 if (!log_manager_.has_staged_log()) {
initial.commit09911bf2008-07-26 23:55:291084 switch (state_) {
[email protected]80a8f312013-12-16 18:00:301085 case SENDING_INITIAL_STABILITY_LOG:
holted1843d42014-10-09 18:38:521086 if (NewInitialMetricsTimingEnabled()) {
1087 // The initial metrics log is already in the queue of unsent logs.
1088 state_ = SENDING_OLD_LOGS;
1089 } else {
1090 PrepareInitialMetricsLog();
1091 log_manager_.StageNextLogForUpload();
1092 SendStagedLog();
1093 state_ = SENDING_INITIAL_METRICS_LOG;
1094 }
[email protected]80a8f312013-12-16 18:00:301095 break;
1096
1097 case SENDING_INITIAL_METRICS_LOG:
[email protected]cd1ac712012-06-26 08:26:471098 state_ = log_manager_.has_unsent_logs() ? SENDING_OLD_LOGS
1099 : SENDING_CURRENT_LOGS;
initial.commit09911bf2008-07-26 23:55:291100 break;
1101
initial.commit09911bf2008-07-26 23:55:291102 case SENDING_OLD_LOGS:
[email protected]cd1ac712012-06-26 08:26:471103 if (!log_manager_.has_unsent_logs())
1104 state_ = SENDING_CURRENT_LOGS;
initial.commit09911bf2008-07-26 23:55:291105 break;
1106
1107 case SENDING_CURRENT_LOGS:
1108 break;
1109
1110 default:
[email protected]a063c102010-07-22 22:20:191111 NOTREACHED();
initial.commit09911bf2008-07-26 23:55:291112 break;
1113 }
[email protected]d01b8732008-10-16 02:18:071114
[email protected]cac267c2011-09-29 15:18:101115 if (log_manager_.has_unsent_logs())
[email protected]ed0fd002012-04-25 23:10:341116 DCHECK_LT(state_, SENDING_CURRENT_LOGS);
initial.commit09911bf2008-07-26 23:55:291117 }
[email protected]252873ef2008-08-04 21:59:451118
[email protected]7f7f1962011-04-20 15:58:161119 // Error 400 indicates a problem with the log, not with the server, so
1120 // don't consider that a sign that the server is in trouble.
[email protected]dc61fe92012-06-12 00:13:501121 bool server_is_healthy = upload_succeeded || response_code == 400;
[email protected]80a8f312013-12-16 18:00:301122 // Don't notify the scheduler that the upload is finished if we've only sent
1123 // the initial stability log, but not yet the initial metrics log (treat the
1124 // two as a single unit of work as far as the scheduler is concerned).
1125 if (state_ != SENDING_INITIAL_METRICS_LOG) {
1126 scheduler_->UploadFinished(server_is_healthy,
1127 log_manager_.has_unsent_logs());
1128 }
[email protected]d67d1052011-06-09 05:11:411129
[email protected]73929422014-05-22 08:19:051130 if (server_is_healthy)
1131 client_->OnLogUploadComplete();
initial.commit09911bf2008-07-26 23:55:291132}
1133
[email protected]57ecc4b2010-08-11 03:02:511134void MetricsService::IncrementPrefValue(const char* path) {
[email protected]24f81ca2014-05-26 15:59:341135 int value = local_state_->GetInteger(path);
1136 local_state_->SetInteger(path, value + 1);
[email protected]e73c01972008-08-13 00:18:241137}
1138
[email protected]57ecc4b2010-08-11 03:02:511139void MetricsService::IncrementLongPrefsValue(const char* path) {
[email protected]24f81ca2014-05-26 15:59:341140 int64 value = local_state_->GetInt64(path);
1141 local_state_->SetInt64(path, value + 1);
[email protected]0bb1a622009-03-04 03:22:321142}
1143
[email protected]c0c55e92011-09-10 18:47:301144bool MetricsService::UmaMetricsProperlyShutdown() {
1145 CHECK(clean_shutdown_status_ == CLEANLY_SHUTDOWN ||
1146 clean_shutdown_status_ == NEED_TO_SHUTDOWN);
1147 return clean_shutdown_status_ == CLEANLY_SHUTDOWN;
1148}
1149
[email protected]60677562013-11-17 15:52:551150void MetricsService::RegisterSyntheticFieldTrial(
1151 const SyntheticTrialGroup& trial) {
1152 for (size_t i = 0; i < synthetic_trial_groups_.size(); ++i) {
1153 if (synthetic_trial_groups_[i].id.name == trial.id.name) {
1154 if (synthetic_trial_groups_[i].id.group != trial.id.group) {
1155 synthetic_trial_groups_[i].id.group = trial.id.group;
[email protected]7a5c07812014-02-26 11:45:411156 synthetic_trial_groups_[i].start_time = base::TimeTicks::Now();
[email protected]60677562013-11-17 15:52:551157 }
1158 return;
1159 }
1160 }
1161
[email protected]7a5c07812014-02-26 11:45:411162 SyntheticTrialGroup trial_group = trial;
1163 trial_group.start_time = base::TimeTicks::Now();
[email protected]60677562013-11-17 15:52:551164 synthetic_trial_groups_.push_back(trial_group);
1165}
1166
[email protected]85791b0b2014-05-20 15:18:581167void MetricsService::RegisterMetricsProvider(
1168 scoped_ptr<metrics::MetricsProvider> provider) {
1169 DCHECK_EQ(INITIALIZED, state_);
1170 metrics_providers_.push_back(provider.release());
1171}
1172
[email protected]61b0d482014-05-20 14:49:101173void MetricsService::CheckForClonedInstall(
1174 scoped_refptr<base::SingleThreadTaskRunner> task_runner) {
1175 state_manager_->CheckForClonedInstall(task_runner);
[email protected]99c892d2014-03-24 18:11:211176}
1177
[email protected]60677562013-11-17 15:52:551178void MetricsService::GetCurrentSyntheticFieldTrials(
[email protected]b3610d42014-05-19 18:07:231179 std::vector<variations::ActiveGroupId>* synthetic_trials) {
[email protected]60677562013-11-17 15:52:551180 DCHECK(synthetic_trials);
1181 synthetic_trials->clear();
[email protected]94dce122014-07-16 04:20:121182 const MetricsLog* current_log = log_manager_.current_log();
[email protected]60677562013-11-17 15:52:551183 for (size_t i = 0; i < synthetic_trial_groups_.size(); ++i) {
1184 if (synthetic_trial_groups_[i].start_time <= current_log->creation_time())
1185 synthetic_trials->push_back(synthetic_trial_groups_[i].id);
1186 }
1187}
1188
[email protected]09dee82d2014-05-22 14:00:531189scoped_ptr<MetricsLog> MetricsService::CreateLog(MetricsLog::LogType log_type) {
[email protected]24f81ca2014-05-26 15:59:341190 return make_scoped_ptr(new MetricsLog(state_manager_->client_id(),
1191 session_id_,
1192 log_type,
1193 client_,
1194 local_state_));
[email protected]09dee82d2014-05-22 14:00:531195}
1196
asvitkine88aa9332014-09-29 23:29:171197void MetricsService::RecordCurrentEnvironment(MetricsLog* log) {
1198 std::vector<variations::ActiveGroupId> synthetic_trials;
1199 GetCurrentSyntheticFieldTrials(&synthetic_trials);
1200 log->RecordEnvironment(metrics_providers_.get(), synthetic_trials,
1201 GetInstallDate());
1202 UMA_HISTOGRAM_COUNTS_100("UMA.SyntheticTrials.Count",
1203 synthetic_trials.size());
1204}
1205
[email protected]acc2ce5512014-05-22 18:29:131206void MetricsService::RecordCurrentHistograms() {
1207 DCHECK(log_manager_.current_log());
1208 histogram_snapshot_manager_.PrepareDeltas(
1209 base::Histogram::kNoFlags, base::Histogram::kUmaTargetedHistogramFlag);
1210}
1211
1212void MetricsService::RecordCurrentStabilityHistograms() {
1213 DCHECK(log_manager_.current_log());
1214 histogram_snapshot_manager_.PrepareDeltas(
1215 base::Histogram::kNoFlags, base::Histogram::kUmaStabilityHistogramFlag);
1216}
1217
[email protected]466f3c12011-03-23 21:20:381218void MetricsService::LogCleanShutdown() {
[email protected]acd55b32011-09-05 17:35:311219 // Redundant hack to write pref ASAP.
erikwright65b58df2014-09-12 00:05:281220 MarkAppCleanShutdownAndCommit(&clean_exit_beacon_, local_state_);
[email protected]84c384e2013-03-01 23:20:191221
[email protected]c0c55e92011-09-10 18:47:301222 // Redundant setting to assure that we always reset this value at shutdown
1223 // (and that we don't use some alternate path, and not call LogCleanShutdown).
1224 clean_shutdown_status_ = CLEANLY_SHUTDOWN;
[email protected]acd55b32011-09-05 17:35:311225
erikwright65b58df2014-09-12 00:05:281226 clean_exit_beacon_.WriteBeaconValue(true);
1227 RecordCurrentState(local_state_);
[email protected]d6147bd2014-06-11 01:58:191228 local_state_->SetInteger(metrics::prefs::kStabilityExecutionPhase,
[email protected]24f81ca2014-05-26 15:59:341229 MetricsService::SHUTDOWN_COMPLETE);
[email protected]466f3c12011-03-23 21:20:381230}
1231
[email protected]e5ad60a2014-03-11 03:54:041232bool MetricsService::ShouldLogEvents() {
1233 // We simply don't log events to UMA if there is a single incognito
initial.commit09911bf2008-07-26 23:55:291234 // session visible. The problem is that we always notify using the orginal
1235 // profile in order to simplify notification processing.
[email protected]7d000322014-05-23 07:16:021236 return !client_->IsOffTheRecordSessionActive();
initial.commit09911bf2008-07-26 23:55:291237}
1238
[email protected]57ecc4b2010-08-11 03:02:511239void MetricsService::RecordBooleanPrefValue(const char* path, bool value) {
initial.commit09911bf2008-07-26 23:55:291240 DCHECK(IsSingleThreaded());
[email protected]24f81ca2014-05-26 15:59:341241 local_state_->SetBoolean(path, value);
1242 RecordCurrentState(local_state_);
initial.commit09911bf2008-07-26 23:55:291243}
1244
1245void MetricsService::RecordCurrentState(PrefService* pref) {
[email protected]d6147bd2014-06-11 01:58:191246 pref->SetInt64(metrics::prefs::kStabilityLastTimestampSec,
asvitkinecbd420732014-08-26 22:15:401247 base::Time::Now().ToTimeT());
initial.commit09911bf2008-07-26 23:55:291248}
asvitkinecbd420732014-08-26 22:15:401249
1250} // namespace metrics