Update V8 to version 4.6.62.
[chromium-blink-merge.git] / components / metrics / metrics_service.cc
blob89c36d236c44afac6a69e24cff7849842ba45e7e
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 //------------------------------------------------------------------------------
6 // Description of the life cycle of a instance of MetricsService.
7 //
8 // OVERVIEW
9 //
10 // A MetricsService instance is typically created at application startup. It is
11 // the central controller for the acquisition of log data, and the automatic
12 // 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,
16 // 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.
24 // Logs fall into one of two categories: "initial logs," and "ongoing logs."
25 // 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.
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)
39 // is 30 minutes. That time period starts when the first user action causes a
40 // logging event. This means that if there is no user action, there may be long
41 // periods without any (ongoing) log transmissions. Ongoing logs typically
42 // 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
48 // to the UMA server. The finalization also acquires the most recent number
49 // of page loads, along with any counts of renderer or plugin crashes.
51 // When the browser shuts down, there will typically be a fragment of an ongoing
52 // 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.
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.
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).
75 // With the above overview, we can now describe the state machine's various
76 // states, based on the State enum specified in the state_ member. Those states
77 // are:
79 // 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_LOGS, // Sending logs and creating new ones when we run out.
84 // In more detail, we have:
86 // INITIALIZED, // Constructor was called.
87 // The MS has been constructed, but has taken no actions to compose the
88 // initial log.
90 // INIT_TASK_SCHEDULED, // Waiting for deferred init tasks to finish.
91 // Typically about 30 seconds after startup, a task is sent to a second thread
92 // (the file thread) to perform deferred (lower priority and slower)
93 // initialization steps such as getting the list of plugins. That task will
94 // (when complete) make an async callback (via a Task) to indicate the
95 // completion.
97 // INIT_TASK_DONE, // Waiting for timer to send initial log.
98 // The callback has arrived, and it is now possible for an initial log to be
99 // created. This callback typically arrives back less than one second after
100 // the deferred init task is dispatched.
102 // SENDING_LOGS, // Sending logs an creating new ones when we run out.
103 // Logs from previous sessions have been loaded, and initial logs have been
104 // created (an optional stability log and the first metrics log). We will
105 // send all of these logs, and when run out, we will start cutting new logs
106 // to send. We will also cut a new log if we expect a shutdown.
108 // The progression through the above states is simple, and sequential.
109 // States proceed from INITIAL to SENDING_LOGS, and remain in the latter until
110 // shutdown.
112 // Also note that whenever we successfully send a log, we mirror the list
113 // of logs into the PrefService. This ensures that IF we crash, we won't start
114 // up and retransmit our old logs again.
116 // Due to race conditions, it is always possible that a log file could be sent
117 // twice. For example, if a log file is sent, but not yet acknowledged by
118 // the external server, and the user shuts down, then a copy of the log may be
119 // saved for re-transmission. These duplicates could be filtered out server
120 // side, but are not expected to be a significant problem.
123 //------------------------------------------------------------------------------
125 #include "components/metrics/metrics_service.h"
127 #include <algorithm>
129 #include "base/bind.h"
130 #include "base/callback.h"
131 #include "base/location.h"
132 #include "base/metrics/histogram_base.h"
133 #include "base/metrics/histogram_macros.h"
134 #include "base/metrics/histogram_samples.h"
135 #include "base/metrics/sparse_histogram.h"
136 #include "base/metrics/statistics_recorder.h"
137 #include "base/prefs/pref_registry_simple.h"
138 #include "base/prefs/pref_service.h"
139 #include "base/rand_util.h"
140 #include "base/single_thread_task_runner.h"
141 #include "base/strings/string_number_conversions.h"
142 #include "base/strings/utf_string_conversions.h"
143 #include "base/thread_task_runner_handle.h"
144 #include "base/threading/platform_thread.h"
145 #include "base/threading/thread.h"
146 #include "base/threading/thread_restrictions.h"
147 #include "base/time/time.h"
148 #include "base/tracked_objects.h"
149 #include "base/values.h"
150 #include "components/metrics/metrics_log.h"
151 #include "components/metrics/metrics_log_manager.h"
152 #include "components/metrics/metrics_log_uploader.h"
153 #include "components/metrics/metrics_pref_names.h"
154 #include "components/metrics/metrics_reporting_scheduler.h"
155 #include "components/metrics/metrics_service_client.h"
156 #include "components/metrics/metrics_state_manager.h"
157 #include "components/variations/entropy_provider.h"
158 #include "components/variations/variations_associated_data.h"
160 namespace metrics {
162 namespace {
164 // Check to see that we're being called on only one thread.
165 bool IsSingleThreaded() {
166 static base::PlatformThreadId thread_id = 0;
167 if (!thread_id)
168 thread_id = base::PlatformThread::CurrentId();
169 return base::PlatformThread::CurrentId() == thread_id;
172 // The delay, in seconds, after starting recording before doing expensive
173 // initialization work.
174 #if defined(OS_ANDROID) || defined(OS_IOS)
175 // On mobile devices, a significant portion of sessions last less than a minute.
176 // Use a shorter timer on these platforms to avoid losing data.
177 // TODO(dfalcantara): To avoid delaying startup, tighten up initialization so
178 // that it occurs after the user gets their initial page.
179 const int kInitializationDelaySeconds = 5;
180 #else
181 const int kInitializationDelaySeconds = 30;
182 #endif
184 // The maximum number of events in a log uploaded to the UMA server.
185 const int kEventLimit = 2400;
187 // If an upload fails, and the transmission was over this byte count, then we
188 // will discard the log, and not try to retransmit it. We also don't persist
189 // the log to the prefs for transmission during the next chrome session if this
190 // limit is exceeded.
191 const size_t kUploadLogAvoidRetransmitSize = 100 * 1024;
193 // Interval, in minutes, between state saves.
194 const int kSaveStateIntervalMinutes = 5;
196 enum ResponseStatus {
197 UNKNOWN_FAILURE,
198 SUCCESS,
199 BAD_REQUEST, // Invalid syntax or log too large.
200 NO_RESPONSE,
201 NUM_RESPONSE_STATUSES
204 ResponseStatus ResponseCodeToStatus(int response_code) {
205 switch (response_code) {
206 case -1:
207 return NO_RESPONSE;
208 case 200:
209 return SUCCESS;
210 case 400:
211 return BAD_REQUEST;
212 default:
213 return UNKNOWN_FAILURE;
217 #if defined(OS_ANDROID) || defined(OS_IOS)
218 void MarkAppCleanShutdownAndCommit(CleanExitBeacon* clean_exit_beacon,
219 PrefService* local_state) {
220 clean_exit_beacon->WriteBeaconValue(true);
221 local_state->SetInteger(prefs::kStabilityExecutionPhase,
222 MetricsService::SHUTDOWN_COMPLETE);
223 // Start writing right away (write happens on a different thread).
224 local_state->CommitPendingWrite();
226 #endif // defined(OS_ANDROID) || defined(OS_IOS)
228 // Determines if current log should be sent based on sampling rate. Returns true
229 // if the sampling rate is not set.
230 bool ShouldUploadLog() {
231 std::string probability_str = variations::GetVariationParamValue(
232 "UMA_EnableCellularLogUpload", "Sample_Probability");
233 if (probability_str.empty())
234 return true;
236 int probability;
237 // In case specified sampling rate is invalid.
238 if (!base::StringToInt(probability_str, &probability))
239 return true;
240 return base::RandInt(1, 100) <= probability;
243 } // namespace
246 SyntheticTrialGroup::SyntheticTrialGroup(uint32 trial, uint32 group) {
247 id.name = trial;
248 id.group = group;
251 SyntheticTrialGroup::~SyntheticTrialGroup() {
254 // static
255 MetricsService::ShutdownCleanliness MetricsService::clean_shutdown_status_ =
256 MetricsService::CLEANLY_SHUTDOWN;
258 MetricsService::ExecutionPhase MetricsService::execution_phase_ =
259 MetricsService::UNINITIALIZED_PHASE;
261 // static
262 void MetricsService::RegisterPrefs(PrefRegistrySimple* registry) {
263 DCHECK(IsSingleThreaded());
264 MetricsStateManager::RegisterPrefs(registry);
265 MetricsLog::RegisterPrefs(registry);
267 registry->RegisterInt64Pref(prefs::kInstallDate, 0);
269 registry->RegisterInt64Pref(prefs::kStabilityLaunchTimeSec, 0);
270 registry->RegisterInt64Pref(prefs::kStabilityLastTimestampSec, 0);
271 registry->RegisterStringPref(prefs::kStabilityStatsVersion, std::string());
272 registry->RegisterInt64Pref(prefs::kStabilityStatsBuildTime, 0);
273 registry->RegisterBooleanPref(prefs::kStabilityExitedCleanly, true);
274 registry->RegisterIntegerPref(prefs::kStabilityExecutionPhase,
275 UNINITIALIZED_PHASE);
276 registry->RegisterBooleanPref(prefs::kStabilitySessionEndCompleted, true);
277 registry->RegisterIntegerPref(prefs::kMetricsSessionID, -1);
279 registry->RegisterListPref(prefs::kMetricsInitialLogs);
280 registry->RegisterListPref(prefs::kMetricsOngoingLogs);
282 registry->RegisterInt64Pref(prefs::kUninstallLaunchCount, 0);
283 registry->RegisterInt64Pref(prefs::kUninstallMetricsUptimeSec, 0);
286 MetricsService::MetricsService(MetricsStateManager* state_manager,
287 MetricsServiceClient* client,
288 PrefService* local_state)
289 : log_manager_(local_state, kUploadLogAvoidRetransmitSize),
290 histogram_snapshot_manager_(this),
291 state_manager_(state_manager),
292 client_(client),
293 local_state_(local_state),
294 clean_exit_beacon_(client->GetRegistryBackupKey(), local_state),
295 recording_state_(UNSET),
296 reporting_active_(false),
297 test_mode_active_(false),
298 state_(INITIALIZED),
299 log_upload_in_progress_(false),
300 idle_since_last_transmission_(false),
301 session_id_(-1),
302 self_ptr_factory_(this),
303 state_saver_factory_(this) {
304 DCHECK(IsSingleThreaded());
305 DCHECK(state_manager_);
306 DCHECK(client_);
307 DCHECK(local_state_);
309 // Set the install date if this is our first run.
310 int64 install_date = local_state_->GetInt64(prefs::kInstallDate);
311 if (install_date == 0)
312 local_state_->SetInt64(prefs::kInstallDate, base::Time::Now().ToTimeT());
315 MetricsService::~MetricsService() {
316 DisableRecording();
319 void MetricsService::InitializeMetricsRecordingState() {
320 InitializeMetricsState();
322 base::Closure upload_callback =
323 base::Bind(&MetricsService::StartScheduledUpload,
324 self_ptr_factory_.GetWeakPtr());
325 scheduler_.reset(
326 new MetricsReportingScheduler(
327 upload_callback,
328 // MetricsServiceClient outlives MetricsService, and
329 // MetricsReportingScheduler is tied to the lifetime of |this|.
330 base::Bind(&MetricsServiceClient::GetStandardUploadInterval,
331 base::Unretained(client_))));
334 void MetricsService::Start() {
335 HandleIdleSinceLastTransmission(false);
336 EnableRecording();
337 EnableReporting();
340 void MetricsService::StartRecordingForTests() {
341 test_mode_active_ = true;
342 EnableRecording();
343 DisableReporting();
346 void MetricsService::Stop() {
347 HandleIdleSinceLastTransmission(false);
348 DisableReporting();
349 DisableRecording();
352 void MetricsService::EnableReporting() {
353 if (reporting_active_)
354 return;
355 reporting_active_ = true;
356 StartSchedulerIfNecessary();
359 void MetricsService::DisableReporting() {
360 reporting_active_ = false;
363 std::string MetricsService::GetClientId() {
364 return state_manager_->client_id();
367 int64 MetricsService::GetInstallDate() {
368 return local_state_->GetInt64(prefs::kInstallDate);
371 int64 MetricsService::GetMetricsReportingEnabledDate() {
372 return local_state_->GetInt64(prefs::kMetricsReportingEnabledTimestamp);
375 bool MetricsService::WasLastShutdownClean() const {
376 return clean_exit_beacon_.exited_cleanly();
379 scoped_ptr<const base::FieldTrial::EntropyProvider>
380 MetricsService::CreateEntropyProvider() {
381 // TODO(asvitkine): Refactor the code so that MetricsService does not expose
382 // this method.
383 return state_manager_->CreateEntropyProvider();
386 void MetricsService::EnableRecording() {
387 DCHECK(IsSingleThreaded());
389 if (recording_state_ == ACTIVE)
390 return;
391 recording_state_ = ACTIVE;
393 state_manager_->ForceClientIdCreation();
394 client_->SetMetricsClientId(state_manager_->client_id());
395 if (!log_manager_.current_log())
396 OpenNewLog();
398 for (size_t i = 0; i < metrics_providers_.size(); ++i)
399 metrics_providers_[i]->OnRecordingEnabled();
401 base::RemoveActionCallback(action_callback_);
402 action_callback_ = base::Bind(&MetricsService::OnUserAction,
403 base::Unretained(this));
404 base::AddActionCallback(action_callback_);
407 void MetricsService::DisableRecording() {
408 DCHECK(IsSingleThreaded());
410 if (recording_state_ == INACTIVE)
411 return;
412 recording_state_ = INACTIVE;
414 client_->OnRecordingDisabled();
416 base::RemoveActionCallback(action_callback_);
418 for (size_t i = 0; i < metrics_providers_.size(); ++i)
419 metrics_providers_[i]->OnRecordingDisabled();
421 PushPendingLogsToPersistentStorage();
424 bool MetricsService::recording_active() const {
425 DCHECK(IsSingleThreaded());
426 return recording_state_ == ACTIVE;
429 bool MetricsService::reporting_active() const {
430 DCHECK(IsSingleThreaded());
431 return reporting_active_;
434 void MetricsService::RecordDelta(const base::HistogramBase& histogram,
435 const base::HistogramSamples& snapshot) {
436 log_manager_.current_log()->RecordHistogramDelta(histogram.histogram_name(),
437 snapshot);
440 void MetricsService::InconsistencyDetected(
441 base::HistogramBase::Inconsistency problem) {
442 UMA_HISTOGRAM_ENUMERATION("Histogram.InconsistenciesBrowser",
443 problem, base::HistogramBase::NEVER_EXCEEDED_VALUE);
446 void MetricsService::UniqueInconsistencyDetected(
447 base::HistogramBase::Inconsistency problem) {
448 UMA_HISTOGRAM_ENUMERATION("Histogram.InconsistenciesBrowserUnique",
449 problem, base::HistogramBase::NEVER_EXCEEDED_VALUE);
452 void MetricsService::InconsistencyDetectedInLoggedCount(int amount) {
453 UMA_HISTOGRAM_COUNTS("Histogram.InconsistentSnapshotBrowser",
454 std::abs(amount));
457 void MetricsService::HandleIdleSinceLastTransmission(bool in_idle) {
458 // If there wasn't a lot of action, maybe the computer was asleep, in which
459 // case, the log transmissions should have stopped. Here we start them up
460 // again.
461 if (!in_idle && idle_since_last_transmission_)
462 StartSchedulerIfNecessary();
463 idle_since_last_transmission_ = in_idle;
466 void MetricsService::OnApplicationNotIdle() {
467 if (recording_state_ == ACTIVE)
468 HandleIdleSinceLastTransmission(false);
471 void MetricsService::RecordStartOfSessionEnd() {
472 LogCleanShutdown();
473 RecordBooleanPrefValue(prefs::kStabilitySessionEndCompleted, false);
476 void MetricsService::RecordCompletedSessionEnd() {
477 LogCleanShutdown();
478 RecordBooleanPrefValue(prefs::kStabilitySessionEndCompleted, true);
481 #if defined(OS_ANDROID) || defined(OS_IOS)
482 void MetricsService::OnAppEnterBackground() {
483 scheduler_->Stop();
485 MarkAppCleanShutdownAndCommit(&clean_exit_beacon_, local_state_);
487 // At this point, there's no way of knowing when the process will be
488 // killed, so this has to be treated similar to a shutdown, closing and
489 // persisting all logs. Unlinke a shutdown, the state is primed to be ready
490 // to continue logging and uploading if the process does return.
491 if (recording_active() && state_ >= SENDING_LOGS) {
492 PushPendingLogsToPersistentStorage();
493 // Persisting logs closes the current log, so start recording a new log
494 // immediately to capture any background work that might be done before the
495 // process is killed.
496 OpenNewLog();
500 void MetricsService::OnAppEnterForeground() {
501 clean_exit_beacon_.WriteBeaconValue(false);
502 StartSchedulerIfNecessary();
504 #else
505 void MetricsService::LogNeedForCleanShutdown() {
506 clean_exit_beacon_.WriteBeaconValue(false);
507 // Redundant setting to be sure we call for a clean shutdown.
508 clean_shutdown_status_ = NEED_TO_SHUTDOWN;
510 #endif // defined(OS_ANDROID) || defined(OS_IOS)
512 // static
513 void MetricsService::SetExecutionPhase(ExecutionPhase execution_phase,
514 PrefService* local_state) {
515 execution_phase_ = execution_phase;
516 local_state->SetInteger(prefs::kStabilityExecutionPhase, execution_phase_);
519 void MetricsService::RecordBreakpadRegistration(bool success) {
520 if (!success)
521 IncrementPrefValue(prefs::kStabilityBreakpadRegistrationFail);
522 else
523 IncrementPrefValue(prefs::kStabilityBreakpadRegistrationSuccess);
526 void MetricsService::RecordBreakpadHasDebugger(bool has_debugger) {
527 if (!has_debugger)
528 IncrementPrefValue(prefs::kStabilityDebuggerNotPresent);
529 else
530 IncrementPrefValue(prefs::kStabilityDebuggerPresent);
533 void MetricsService::ClearSavedStabilityMetrics() {
534 for (size_t i = 0; i < metrics_providers_.size(); ++i)
535 metrics_providers_[i]->ClearSavedStabilityMetrics();
537 // Reset the prefs that are managed by MetricsService/MetricsLog directly.
538 local_state_->SetInteger(prefs::kStabilityCrashCount, 0);
539 local_state_->SetInteger(prefs::kStabilityExecutionPhase,
540 UNINITIALIZED_PHASE);
541 local_state_->SetInteger(prefs::kStabilityIncompleteSessionEndCount, 0);
542 local_state_->SetInteger(prefs::kStabilityLaunchCount, 0);
543 local_state_->SetBoolean(prefs::kStabilitySessionEndCompleted, true);
546 void MetricsService::PushExternalLog(const std::string& log) {
547 log_manager_.StoreLog(log, MetricsLog::ONGOING_LOG);
550 //------------------------------------------------------------------------------
551 // private methods
552 //------------------------------------------------------------------------------
555 //------------------------------------------------------------------------------
556 // Initialization methods
558 void MetricsService::InitializeMetricsState() {
559 const int64 buildtime = MetricsLog::GetBuildTime();
560 const std::string version = client_->GetVersionString();
561 bool version_changed = false;
562 if (local_state_->GetInt64(prefs::kStabilityStatsBuildTime) != buildtime ||
563 local_state_->GetString(prefs::kStabilityStatsVersion) != version) {
564 local_state_->SetString(prefs::kStabilityStatsVersion, version);
565 local_state_->SetInt64(prefs::kStabilityStatsBuildTime, buildtime);
566 version_changed = true;
569 log_manager_.LoadPersistedUnsentLogs();
571 session_id_ = local_state_->GetInteger(prefs::kMetricsSessionID);
573 if (!clean_exit_beacon_.exited_cleanly()) {
574 IncrementPrefValue(prefs::kStabilityCrashCount);
575 // Reset flag, and wait until we call LogNeedForCleanShutdown() before
576 // monitoring.
577 clean_exit_beacon_.WriteBeaconValue(true);
580 bool has_initial_stability_log = false;
581 if (!clean_exit_beacon_.exited_cleanly() ||
582 ProvidersHaveInitialStabilityMetrics()) {
583 // TODO(rtenneti): On windows, consider saving/getting execution_phase from
584 // the registry.
585 int execution_phase =
586 local_state_->GetInteger(prefs::kStabilityExecutionPhase);
587 UMA_HISTOGRAM_SPARSE_SLOWLY("Chrome.Browser.CrashedExecutionPhase",
588 execution_phase);
590 // If the previous session didn't exit cleanly, or if any provider
591 // explicitly requests it, prepare an initial stability log -
592 // provided UMA is enabled.
593 if (state_manager_->IsMetricsReportingEnabled())
594 has_initial_stability_log = PrepareInitialStabilityLog();
597 // If no initial stability log was generated and there was a version upgrade,
598 // clear the stability stats from the previous version (so that they don't get
599 // attributed to the current version). This could otherwise happen due to a
600 // number of different edge cases, such as if the last version crashed before
601 // it could save off a system profile or if UMA reporting is disabled (which
602 // normally results in stats being accumulated).
603 if (!has_initial_stability_log && version_changed)
604 ClearSavedStabilityMetrics();
606 // Update session ID.
607 ++session_id_;
608 local_state_->SetInteger(prefs::kMetricsSessionID, session_id_);
610 // Stability bookkeeping
611 IncrementPrefValue(prefs::kStabilityLaunchCount);
613 DCHECK_EQ(UNINITIALIZED_PHASE, execution_phase_);
614 SetExecutionPhase(START_METRICS_RECORDING, local_state_);
616 if (!local_state_->GetBoolean(prefs::kStabilitySessionEndCompleted)) {
617 IncrementPrefValue(prefs::kStabilityIncompleteSessionEndCount);
618 // This is marked false when we get a WM_ENDSESSION.
619 local_state_->SetBoolean(prefs::kStabilitySessionEndCompleted, true);
622 // Call GetUptimes() for the first time, thus allowing all later calls
623 // to record incremental uptimes accurately.
624 base::TimeDelta ignored_uptime_parameter;
625 base::TimeDelta startup_uptime;
626 GetUptimes(local_state_, &startup_uptime, &ignored_uptime_parameter);
627 DCHECK_EQ(0, startup_uptime.InMicroseconds());
628 // For backwards compatibility, leave this intact in case Omaha is checking
629 // them. prefs::kStabilityLastTimestampSec may also be useless now.
630 // TODO(jar): Delete these if they have no uses.
631 local_state_->SetInt64(prefs::kStabilityLaunchTimeSec,
632 base::Time::Now().ToTimeT());
634 // Bookkeeping for the uninstall metrics.
635 IncrementLongPrefsValue(prefs::kUninstallLaunchCount);
637 // Kick off the process of saving the state (so the uptime numbers keep
638 // getting updated) every n minutes.
639 ScheduleNextStateSave();
642 void MetricsService::OnUserAction(const std::string& action) {
643 if (!ShouldLogEvents())
644 return;
646 log_manager_.current_log()->RecordUserAction(action);
647 HandleIdleSinceLastTransmission(false);
650 void MetricsService::FinishedGatheringInitialMetrics() {
651 DCHECK_EQ(INIT_TASK_SCHEDULED, state_);
652 state_ = INIT_TASK_DONE;
654 // Create the initial log.
655 if (!initial_metrics_log_.get()) {
656 initial_metrics_log_ = CreateLog(MetricsLog::ONGOING_LOG);
657 NotifyOnDidCreateMetricsLog();
660 scheduler_->InitTaskComplete();
663 void MetricsService::GetUptimes(PrefService* pref,
664 base::TimeDelta* incremental_uptime,
665 base::TimeDelta* uptime) {
666 base::TimeTicks now = base::TimeTicks::Now();
667 // If this is the first call, init |first_updated_time_| and
668 // |last_updated_time_|.
669 if (last_updated_time_.is_null()) {
670 first_updated_time_ = now;
671 last_updated_time_ = now;
673 *incremental_uptime = now - last_updated_time_;
674 *uptime = now - first_updated_time_;
675 last_updated_time_ = now;
677 const int64 incremental_time_secs = incremental_uptime->InSeconds();
678 if (incremental_time_secs > 0) {
679 int64 metrics_uptime = pref->GetInt64(prefs::kUninstallMetricsUptimeSec);
680 metrics_uptime += incremental_time_secs;
681 pref->SetInt64(prefs::kUninstallMetricsUptimeSec, metrics_uptime);
685 void MetricsService::NotifyOnDidCreateMetricsLog() {
686 DCHECK(IsSingleThreaded());
687 for (size_t i = 0; i < metrics_providers_.size(); ++i)
688 metrics_providers_[i]->OnDidCreateMetricsLog();
691 //------------------------------------------------------------------------------
692 // State save methods
694 void MetricsService::ScheduleNextStateSave() {
695 state_saver_factory_.InvalidateWeakPtrs();
697 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
698 FROM_HERE, base::Bind(&MetricsService::SaveLocalState,
699 state_saver_factory_.GetWeakPtr()),
700 base::TimeDelta::FromMinutes(kSaveStateIntervalMinutes));
703 void MetricsService::SaveLocalState() {
704 RecordCurrentState(local_state_);
706 // TODO(jar):110021 Does this run down the batteries????
707 ScheduleNextStateSave();
711 //------------------------------------------------------------------------------
712 // Recording control methods
714 void MetricsService::OpenNewLog() {
715 DCHECK(!log_manager_.current_log());
717 log_manager_.BeginLoggingWithLog(CreateLog(MetricsLog::ONGOING_LOG));
718 NotifyOnDidCreateMetricsLog();
719 if (state_ == INITIALIZED) {
720 // We only need to schedule that run once.
721 state_ = INIT_TASK_SCHEDULED;
723 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
724 FROM_HERE, base::Bind(&MetricsService::StartGatheringMetrics,
725 self_ptr_factory_.GetWeakPtr()),
726 base::TimeDelta::FromSeconds(kInitializationDelaySeconds));
730 void MetricsService::StartGatheringMetrics() {
731 client_->StartGatheringMetrics(
732 base::Bind(&MetricsService::FinishedGatheringInitialMetrics,
733 self_ptr_factory_.GetWeakPtr()));
736 void MetricsService::CloseCurrentLog() {
737 if (!log_manager_.current_log())
738 return;
740 // TODO(jar): Integrate bounds on log recording more consistently, so that we
741 // can stop recording logs that are too big much sooner.
742 if (log_manager_.current_log()->num_events() > kEventLimit) {
743 UMA_HISTOGRAM_COUNTS("UMA.Discarded Log Events",
744 log_manager_.current_log()->num_events());
745 log_manager_.DiscardCurrentLog();
746 OpenNewLog(); // Start trivial log to hold our histograms.
749 // Put incremental data (histogram deltas, and realtime stats deltas) at the
750 // end of all log transmissions (initial log handles this separately).
751 // RecordIncrementalStabilityElements only exists on the derived
752 // MetricsLog class.
753 MetricsLog* current_log = log_manager_.current_log();
754 DCHECK(current_log);
755 RecordCurrentEnvironment(current_log);
756 base::TimeDelta incremental_uptime;
757 base::TimeDelta uptime;
758 GetUptimes(local_state_, &incremental_uptime, &uptime);
759 current_log->RecordStabilityMetrics(metrics_providers_.get(),
760 incremental_uptime, uptime);
762 current_log->RecordGeneralMetrics(metrics_providers_.get());
763 RecordCurrentHistograms();
765 log_manager_.FinishCurrentLog();
768 void MetricsService::PushPendingLogsToPersistentStorage() {
769 if (state_ < SENDING_LOGS)
770 return; // We didn't and still don't have time to get plugin list etc.
772 CloseCurrentLog();
773 log_manager_.PersistUnsentLogs();
776 //------------------------------------------------------------------------------
777 // Transmission of logs methods
779 void MetricsService::StartSchedulerIfNecessary() {
780 // Never schedule cutting or uploading of logs in test mode.
781 if (test_mode_active_)
782 return;
784 // Even if reporting is disabled, the scheduler is needed to trigger the
785 // creation of the initial log, which must be done in order for any logs to be
786 // persisted on shutdown or backgrounding.
787 if (recording_active() &&
788 (reporting_active() || state_ < SENDING_LOGS)) {
789 scheduler_->Start();
793 void MetricsService::StartScheduledUpload() {
794 DCHECK(state_ >= INIT_TASK_DONE);
795 // If we're getting no notifications, then the log won't have much in it, and
796 // it's possible the computer is about to go to sleep, so don't upload and
797 // stop the scheduler.
798 // If recording has been turned off, the scheduler doesn't need to run.
799 // If reporting is off, proceed if the initial log hasn't been created, since
800 // that has to happen in order for logs to be cut and stored when persisting.
801 // TODO(stuartmorgan): Call Stop() on the scheduler when reporting and/or
802 // recording are turned off instead of letting it fire and then aborting.
803 if (idle_since_last_transmission_ ||
804 !recording_active() ||
805 (!reporting_active() && state_ >= SENDING_LOGS)) {
806 scheduler_->Stop();
807 scheduler_->UploadCancelled();
808 return;
811 // If there are unsent logs, send the next one. If not, start the asynchronous
812 // process of finalizing the current log for upload.
813 if (state_ == SENDING_LOGS && log_manager_.has_unsent_logs()) {
814 SendNextLog();
815 } else {
816 // There are no logs left to send, so start creating a new one.
817 client_->CollectFinalMetrics(
818 base::Bind(&MetricsService::OnFinalLogInfoCollectionDone,
819 self_ptr_factory_.GetWeakPtr()));
823 void MetricsService::OnFinalLogInfoCollectionDone() {
824 // If somehow there is a log upload in progress, we return and hope things
825 // work out. The scheduler isn't informed since if this happens, the scheduler
826 // will get a response from the upload.
827 DCHECK(!log_upload_in_progress_);
828 if (log_upload_in_progress_)
829 return;
831 // Abort if metrics were turned off during the final info gathering.
832 if (!recording_active()) {
833 scheduler_->Stop();
834 scheduler_->UploadCancelled();
835 return;
838 if (state_ == INIT_TASK_DONE) {
839 PrepareInitialMetricsLog();
840 } else {
841 DCHECK_EQ(SENDING_LOGS, state_);
842 CloseCurrentLog();
843 OpenNewLog();
845 SendNextLog();
848 void MetricsService::SendNextLog() {
849 DCHECK_EQ(SENDING_LOGS, state_);
850 if (!reporting_active()) {
851 scheduler_->Stop();
852 scheduler_->UploadCancelled();
853 return;
855 if (!log_manager_.has_unsent_logs()) {
856 // Should only get here if serializing the log failed somehow.
857 // Just tell the scheduler it was uploaded and wait for the next log
858 // interval.
859 scheduler_->UploadFinished(true, log_manager_.has_unsent_logs());
860 return;
862 if (!log_manager_.has_staged_log())
863 log_manager_.StageNextLogForUpload();
864 SendStagedLog();
867 bool MetricsService::ProvidersHaveInitialStabilityMetrics() {
868 // Check whether any metrics provider has initial stability metrics.
869 for (size_t i = 0; i < metrics_providers_.size(); ++i) {
870 if (metrics_providers_[i]->HasInitialStabilityMetrics())
871 return true;
874 return false;
877 bool MetricsService::PrepareInitialStabilityLog() {
878 DCHECK_EQ(INITIALIZED, state_);
880 scoped_ptr<MetricsLog> initial_stability_log(
881 CreateLog(MetricsLog::INITIAL_STABILITY_LOG));
883 // Do not call NotifyOnDidCreateMetricsLog here because the stability
884 // log describes stats from the _previous_ session.
886 if (!initial_stability_log->LoadSavedEnvironmentFromPrefs())
887 return false;
889 log_manager_.PauseCurrentLog();
890 log_manager_.BeginLoggingWithLog(initial_stability_log.Pass());
892 // Note: Some stability providers may record stability stats via histograms,
893 // so this call has to be after BeginLoggingWithLog().
894 log_manager_.current_log()->RecordStabilityMetrics(
895 metrics_providers_.get(), base::TimeDelta(), base::TimeDelta());
896 RecordCurrentStabilityHistograms();
898 // Note: RecordGeneralMetrics() intentionally not called since this log is for
899 // stability stats from a previous session only.
901 log_manager_.FinishCurrentLog();
902 log_manager_.ResumePausedLog();
904 // Store unsent logs, including the stability log that was just saved, so
905 // that they're not lost in case of a crash before upload time.
906 log_manager_.PersistUnsentLogs();
908 return true;
911 void MetricsService::PrepareInitialMetricsLog() {
912 DCHECK_EQ(INIT_TASK_DONE, state_);
914 RecordCurrentEnvironment(initial_metrics_log_.get());
915 base::TimeDelta incremental_uptime;
916 base::TimeDelta uptime;
917 GetUptimes(local_state_, &incremental_uptime, &uptime);
919 // Histograms only get written to the current log, so make the new log current
920 // before writing them.
921 log_manager_.PauseCurrentLog();
922 log_manager_.BeginLoggingWithLog(initial_metrics_log_.Pass());
924 // Note: Some stability providers may record stability stats via histograms,
925 // so this call has to be after BeginLoggingWithLog().
926 MetricsLog* current_log = log_manager_.current_log();
927 current_log->RecordStabilityMetrics(metrics_providers_.get(),
928 base::TimeDelta(), base::TimeDelta());
929 current_log->RecordGeneralMetrics(metrics_providers_.get());
930 RecordCurrentHistograms();
932 log_manager_.FinishCurrentLog();
933 log_manager_.ResumePausedLog();
935 // Store unsent logs, including the initial log that was just saved, so
936 // that they're not lost in case of a crash before upload time.
937 log_manager_.PersistUnsentLogs();
939 state_ = SENDING_LOGS;
942 void MetricsService::SendStagedLog() {
943 DCHECK(log_manager_.has_staged_log());
944 if (!log_manager_.has_staged_log())
945 return;
947 DCHECK(!log_upload_in_progress_);
948 log_upload_in_progress_ = true;
950 if (!ShouldUploadLog()) {
951 SkipAndDiscardUpload();
952 return;
955 if (!log_uploader_) {
956 log_uploader_ = client_->CreateUploader(
957 base::Bind(&MetricsService::OnLogUploadComplete,
958 self_ptr_factory_.GetWeakPtr()));
961 const std::string hash =
962 base::HexEncode(log_manager_.staged_log_hash().data(),
963 log_manager_.staged_log_hash().size());
964 log_uploader_->UploadLog(log_manager_.staged_log(), hash);
966 HandleIdleSinceLastTransmission(true);
970 void MetricsService::OnLogUploadComplete(int response_code) {
971 DCHECK_EQ(SENDING_LOGS, state_);
972 DCHECK(log_upload_in_progress_);
973 log_upload_in_progress_ = false;
975 // Log a histogram to track response success vs. failure rates.
976 UMA_HISTOGRAM_ENUMERATION("UMA.UploadResponseStatus.Protobuf",
977 ResponseCodeToStatus(response_code),
978 NUM_RESPONSE_STATUSES);
980 bool upload_succeeded = response_code == 200;
982 // Provide boolean for error recovery (allow us to ignore response_code).
983 bool discard_log = false;
984 const size_t log_size = log_manager_.staged_log().length();
985 if (upload_succeeded) {
986 UMA_HISTOGRAM_COUNTS_10000("UMA.LogSize.OnSuccess", log_size / 1024);
987 } else if (log_size > kUploadLogAvoidRetransmitSize) {
988 UMA_HISTOGRAM_COUNTS("UMA.Large Rejected Log was Discarded",
989 static_cast<int>(log_size));
990 discard_log = true;
991 } else if (response_code == 400) {
992 // Bad syntax. Retransmission won't work.
993 discard_log = true;
996 if (upload_succeeded || discard_log) {
997 log_manager_.DiscardStagedLog();
998 // Store the updated list to disk now that the removed log is uploaded.
999 log_manager_.PersistUnsentLogs();
1002 // Error 400 indicates a problem with the log, not with the server, so
1003 // don't consider that a sign that the server is in trouble.
1004 bool server_is_healthy = upload_succeeded || response_code == 400;
1005 scheduler_->UploadFinished(server_is_healthy, log_manager_.has_unsent_logs());
1007 if (server_is_healthy)
1008 client_->OnLogUploadComplete();
1011 void MetricsService::IncrementPrefValue(const char* path) {
1012 int value = local_state_->GetInteger(path);
1013 local_state_->SetInteger(path, value + 1);
1016 void MetricsService::IncrementLongPrefsValue(const char* path) {
1017 int64 value = local_state_->GetInt64(path);
1018 local_state_->SetInt64(path, value + 1);
1021 bool MetricsService::UmaMetricsProperlyShutdown() {
1022 CHECK(clean_shutdown_status_ == CLEANLY_SHUTDOWN ||
1023 clean_shutdown_status_ == NEED_TO_SHUTDOWN);
1024 return clean_shutdown_status_ == CLEANLY_SHUTDOWN;
1027 void MetricsService::AddSyntheticTrialObserver(
1028 SyntheticTrialObserver* observer) {
1029 synthetic_trial_observer_list_.AddObserver(observer);
1030 if (!synthetic_trial_groups_.empty())
1031 observer->OnSyntheticTrialsChanged(synthetic_trial_groups_);
1034 void MetricsService::RemoveSyntheticTrialObserver(
1035 SyntheticTrialObserver* observer) {
1036 synthetic_trial_observer_list_.RemoveObserver(observer);
1039 void MetricsService::RegisterSyntheticFieldTrial(
1040 const SyntheticTrialGroup& trial) {
1041 for (size_t i = 0; i < synthetic_trial_groups_.size(); ++i) {
1042 if (synthetic_trial_groups_[i].id.name == trial.id.name) {
1043 if (synthetic_trial_groups_[i].id.group != trial.id.group) {
1044 synthetic_trial_groups_[i].id.group = trial.id.group;
1045 synthetic_trial_groups_[i].start_time = base::TimeTicks::Now();
1046 NotifySyntheticTrialObservers();
1048 return;
1052 SyntheticTrialGroup trial_group = trial;
1053 trial_group.start_time = base::TimeTicks::Now();
1054 synthetic_trial_groups_.push_back(trial_group);
1055 NotifySyntheticTrialObservers();
1058 void MetricsService::RegisterMetricsProvider(
1059 scoped_ptr<MetricsProvider> provider) {
1060 DCHECK_EQ(INITIALIZED, state_);
1061 metrics_providers_.push_back(provider.Pass());
1064 void MetricsService::CheckForClonedInstall(
1065 scoped_refptr<base::SingleThreadTaskRunner> task_runner) {
1066 state_manager_->CheckForClonedInstall(task_runner);
1069 void MetricsService::NotifySyntheticTrialObservers() {
1070 FOR_EACH_OBSERVER(SyntheticTrialObserver, synthetic_trial_observer_list_,
1071 OnSyntheticTrialsChanged(synthetic_trial_groups_));
1074 void MetricsService::GetCurrentSyntheticFieldTrials(
1075 std::vector<variations::ActiveGroupId>* synthetic_trials) {
1076 DCHECK(synthetic_trials);
1077 synthetic_trials->clear();
1078 const MetricsLog* current_log = log_manager_.current_log();
1079 for (size_t i = 0; i < synthetic_trial_groups_.size(); ++i) {
1080 if (synthetic_trial_groups_[i].start_time <= current_log->creation_time())
1081 synthetic_trials->push_back(synthetic_trial_groups_[i].id);
1085 scoped_ptr<MetricsLog> MetricsService::CreateLog(MetricsLog::LogType log_type) {
1086 return make_scoped_ptr(new MetricsLog(state_manager_->client_id(),
1087 session_id_,
1088 log_type,
1089 client_,
1090 local_state_));
1093 void MetricsService::RecordCurrentEnvironment(MetricsLog* log) {
1094 std::vector<variations::ActiveGroupId> synthetic_trials;
1095 GetCurrentSyntheticFieldTrials(&synthetic_trials);
1096 log->RecordEnvironment(metrics_providers_.get(), synthetic_trials,
1097 GetInstallDate(), GetMetricsReportingEnabledDate());
1100 void MetricsService::RecordCurrentHistograms() {
1101 DCHECK(log_manager_.current_log());
1102 histogram_snapshot_manager_.PrepareDeltas(
1103 base::Histogram::kNoFlags, base::Histogram::kUmaTargetedHistogramFlag);
1106 void MetricsService::RecordCurrentStabilityHistograms() {
1107 DCHECK(log_manager_.current_log());
1108 histogram_snapshot_manager_.PrepareDeltas(
1109 base::Histogram::kNoFlags, base::Histogram::kUmaStabilityHistogramFlag);
1112 void MetricsService::LogCleanShutdown() {
1113 // Redundant setting to assure that we always reset this value at shutdown
1114 // (and that we don't use some alternate path, and not call LogCleanShutdown).
1115 clean_shutdown_status_ = CLEANLY_SHUTDOWN;
1117 clean_exit_beacon_.WriteBeaconValue(true);
1118 RecordCurrentState(local_state_);
1119 local_state_->SetInteger(prefs::kStabilityExecutionPhase,
1120 MetricsService::SHUTDOWN_COMPLETE);
1123 bool MetricsService::ShouldLogEvents() {
1124 // We simply don't log events to UMA if there is a single incognito
1125 // session visible. The problem is that we always notify using the original
1126 // profile in order to simplify notification processing.
1127 return !client_->IsOffTheRecordSessionActive();
1130 void MetricsService::RecordBooleanPrefValue(const char* path, bool value) {
1131 DCHECK(IsSingleThreaded());
1132 local_state_->SetBoolean(path, value);
1133 RecordCurrentState(local_state_);
1136 void MetricsService::RecordCurrentState(PrefService* pref) {
1137 pref->SetInt64(prefs::kStabilityLastTimestampSec,
1138 base::Time::Now().ToTimeT());
1141 void MetricsService::SkipAndDiscardUpload() {
1142 log_manager_.DiscardStagedLog();
1143 scheduler_->UploadCancelled();
1144 log_upload_in_progress_ = false;
1147 } // namespace metrics