[email protected] | d6147bd | 2014-06-11 01:58:19 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 5 | //------------------------------------------------------------------------------ |
| 6 | // Description of the life cycle of a instance of MetricsService. |
| 7 | // |
| 8 | // OVERVIEW |
| 9 | // |
[email protected] | e3eb0c4 | 2013-04-18 06:18:58 | [diff] [blame] | 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 |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 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, |
[email protected] | e3eb0c4 | 2013-04-18 06:18:58 | [diff] [blame] | 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. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 23 | // |
[email protected] | 281d288 | 2009-01-20 20:32:42 | [diff] [blame] | 24 | // Logs fall into one of two categories: "initial logs," and "ongoing logs." |
[email protected] | 80a8f31 | 2013-12-16 18:00:30 | [diff] [blame] | 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. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 33 | // |
| 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] | 3a66815 | 2013-06-21 23:56:42 | [diff] [blame] | 39 | // is 30 minutes. That time period starts when the first user action causes a |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 40 | // logging event. This means that if there is no user action, there may be long |
[email protected] | 281d288 | 2009-01-20 20:32:42 | [diff] [blame] | 41 | // periods without any (ongoing) log transmissions. Ongoing logs typically |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 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 |
[email protected] | 80a8f31 | 2013-12-16 18:00:30 | [diff] [blame] | 48 | // to the UMA server. The finalization also acquires the most recent number |
[email protected] | 281d288 | 2009-01-20 20:32:42 | [diff] [blame] | 49 | // of page loads, along with any counts of renderer or plugin crashes. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 50 | // |
| 51 | // When the browser shuts down, there will typically be a fragment of an ongoing |
[email protected] | 80a8f31 | 2013-12-16 18:00:30 | [diff] [blame] | 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. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 55 | // |
| 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] | 80a8f31 | 2013-12-16 18:00:30 | [diff] [blame] | 76 | // states, based on the State enum specified in the state_ member. Those states |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 77 | // are: |
| 78 | // |
[email protected] | 80a8f31 | 2013-12-16 18:00:30 | [diff] [blame] | 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_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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 86 | // |
| 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] | 80a8f31 | 2013-12-16 18:00:30 | [diff] [blame] | 93 | // INIT_TASK_SCHEDULED, // Waiting for deferred init tasks to finish. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 94 | // Typically about 30 seconds after startup, a task is sent to a second thread |
[email protected] | 85ed9d4 | 2010-06-08 22:37:44 | [diff] [blame] | 95 | // (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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 99 | // |
[email protected] | 85ed9d4 | 2010-06-08 22:37:44 | [diff] [blame] | 100 | // INIT_TASK_DONE, // Waiting for timer to send initial log. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 101 | // 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] | 85ed9d4 | 2010-06-08 22:37:44 | [diff] [blame] | 103 | // the deferred init task is dispatched. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 104 | // |
[email protected] | 80a8f31 | 2013-12-16 18:00:30 | [diff] [blame] | 105 | // 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 121 | // |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 122 | // SENDING_OLD_LOGS, // Sending unsent logs from previous session. |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 123 | // 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 128 | // |
| 129 | // SENDING_CURRENT_LOGS, // Sending standard current logs as they accrue. |
[email protected] | 0b33f80b | 2008-12-17 21:34:36 | [diff] [blame] | 130 | // Current logs are being accumulated. Typically every 20 minutes a log is |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 131 | // 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] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 139 | // happens, any staged (transmission in progress) log is persisted, and any log |
[email protected] | 410938e0 | 2012-10-24 16:33:59 | [diff] [blame] | 140 | // that is currently accumulating is also finalized and persisted. We then |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 141 | // 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 145 | // |
[email protected] | 410938e0 | 2012-10-24 16:33:59 | [diff] [blame] | 146 | // 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] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 151 | // 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 154 | // |
| 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] | 281d288 | 2009-01-20 20:32:42 | [diff] [blame] | 159 | // side, but are not expected to be a significant problem. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 160 | // |
| 161 | // |
| 162 | //------------------------------------------------------------------------------ |
| 163 | |
[email protected] | d6147bd | 2014-06-11 01:58:19 | [diff] [blame] | 164 | #include "components/metrics/metrics_service.h" |
[email protected] | 40bcc30 | 2009-03-02 20:50:39 | [diff] [blame] | 165 | |
[email protected] | d7c1fa6 | 2012-06-15 23:35:30 | [diff] [blame] | 166 | #include <algorithm> |
| 167 | |
[email protected] | 7f7f196 | 2011-04-20 15:58:16 | [diff] [blame] | 168 | #include "base/bind.h" |
| 169 | #include "base/callback.h" |
[email protected] | 835d7c8 | 2010-10-14 04:38:38 | [diff] [blame] | 170 | #include "base/metrics/histogram.h" |
[email protected] | acc2ce551 | 2014-05-22 18:29:13 | [diff] [blame] | 171 | #include "base/metrics/histogram_base.h" |
| 172 | #include "base/metrics/histogram_samples.h" |
[email protected] | 1026afd | 2013-03-20 14:28:54 | [diff] [blame] | 173 | #include "base/metrics/sparse_histogram.h" |
[email protected] | 567d30e | 2012-07-13 21:48:29 | [diff] [blame] | 174 | #include "base/metrics/statistics_recorder.h" |
[email protected] | 3853a4c | 2013-02-11 17:15:57 | [diff] [blame] | 175 | #include "base/prefs/pref_registry_simple.h" |
| 176 | #include "base/prefs/pref_service.h" |
[email protected] | 3ea1b18 | 2013-02-08 22:38:41 | [diff] [blame] | 177 | #include "base/strings/string_number_conversions.h" |
[email protected] | 112158af | 2013-06-07 23:46:18 | [diff] [blame] | 178 | #include "base/strings/utf_string_conversions.h" |
[email protected] | ce072a7 | 2010-12-31 20:02:16 | [diff] [blame] | 179 | #include "base/threading/platform_thread.h" |
[email protected] | b3841c50 | 2011-03-09 01:21:31 | [diff] [blame] | 180 | #include "base/threading/thread.h" |
[email protected] | 3a7b66d | 2012-04-26 16:34:16 | [diff] [blame] | 181 | #include "base/threading/thread_restrictions.h" |
[email protected] | ed0fd00 | 2012-04-25 23:10:34 | [diff] [blame] | 182 | #include "base/tracked_objects.h" |
[email protected] | 67908205 | 2010-07-21 21:30:13 | [diff] [blame] | 183 | #include "base/values.h" |
[email protected] | 91b1d91 | 2014-06-05 10:52:08 | [diff] [blame] | 184 | #include "components/metrics/metrics_log.h" |
[email protected] | 064107e | 2014-05-02 00:59:06 | [diff] [blame] | 185 | #include "components/metrics/metrics_log_manager.h" |
[email protected] | 0d5a61a8 | 2014-05-31 22:28:34 | [diff] [blame] | 186 | #include "components/metrics/metrics_log_uploader.h" |
[email protected] | 7f07db6 | 2014-05-15 01:12:45 | [diff] [blame] | 187 | #include "components/metrics/metrics_pref_names.h" |
[email protected] | 14bb4669 | 2014-05-20 17:16:45 | [diff] [blame] | 188 | #include "components/metrics/metrics_reporting_scheduler.h" |
[email protected] | 7392942 | 2014-05-22 08:19:05 | [diff] [blame] | 189 | #include "components/metrics/metrics_service_client.h" |
[email protected] | 16a3091 | 2014-06-04 00:20:04 | [diff] [blame] | 190 | #include "components/metrics/metrics_state_manager.h" |
[email protected] | 50ae9f1 | 2013-08-29 18:03:22 | [diff] [blame] | 191 | #include "components/variations/entropy_provider.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 192 | |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 193 | using base::Time; |
[email protected] | 064107e | 2014-05-02 00:59:06 | [diff] [blame] | 194 | using metrics::MetricsLogManager; |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 195 | |
[email protected] | fe58acc2 | 2012-02-29 01:29:58 | [diff] [blame] | 196 | namespace { |
[email protected] | b2a4812d | 2012-02-28 05:31:31 | [diff] [blame] | 197 | |
[email protected] | fe58acc2 | 2012-02-29 01:29:58 | [diff] [blame] | 198 | // Check to see that we're being called on only one thread. |
| 199 | bool 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] | 7f7f196 | 2011-04-20 15:58:16 | [diff] [blame] | 206 | // The delay, in seconds, after starting recording before doing expensive |
| 207 | // initialization work. |
[email protected] | 12180f8 | 2012-10-10 21:13:30 | [diff] [blame] | 208 | #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. |
| 213 | const int kInitializationDelaySeconds = 5; |
| 214 | #else |
[email protected] | fe58acc2 | 2012-02-29 01:29:58 | [diff] [blame] | 215 | const int kInitializationDelaySeconds = 30; |
[email protected] | 12180f8 | 2012-10-10 21:13:30 | [diff] [blame] | 216 | #endif |
[email protected] | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 217 | |
[email protected] | 54702c9 | 2011-04-15 15:06:43 | [diff] [blame] | 218 | // The maximum number of events in a log uploaded to the UMA server. |
[email protected] | fe58acc2 | 2012-02-29 01:29:58 | [diff] [blame] | 219 | const int kEventLimit = 2400; |
[email protected] | 68475e60 | 2008-08-22 03:21:15 | [diff] [blame] | 220 | |
| 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] | a63006e | 2014-06-20 05:22:32 | [diff] [blame] | 225 | const size_t kUploadLogAvoidRetransmitSize = 100 * 1024; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 226 | |
[email protected] | fc4252a7 | 2012-01-12 21:58:47 | [diff] [blame] | 227 | // Interval, in minutes, between state saves. |
[email protected] | fe58acc2 | 2012-02-29 01:29:58 | [diff] [blame] | 228 | const int kSaveStateIntervalMinutes = 5; |
| 229 | |
[email protected] | acc2ce551 | 2014-05-22 18:29:13 | [diff] [blame] | 230 | // The metrics server's URL. |
| 231 | const char kServerUrl[] = "https://clients4.google.com/uma/v2"; |
| 232 | |
| 233 | // The MIME type for the uploaded metrics data. |
| 234 | const char kMimeType[] = "application/vnd.chrome.uma"; |
| 235 | |
[email protected] | 4266def2 | 2012-05-17 01:02:40 | [diff] [blame] | 236 | enum ResponseStatus { |
| 237 | UNKNOWN_FAILURE, |
| 238 | SUCCESS, |
| 239 | BAD_REQUEST, // Invalid syntax or log too large. |
[email protected] | 9f5c1ce8 | 2012-05-23 23:11:28 | [diff] [blame] | 240 | NO_RESPONSE, |
[email protected] | 4266def2 | 2012-05-17 01:02:40 | [diff] [blame] | 241 | NUM_RESPONSE_STATUSES |
| 242 | }; |
| 243 | |
| 244 | ResponseStatus ResponseCodeToStatus(int response_code) { |
| 245 | switch (response_code) { |
[email protected] | 0d5a61a8 | 2014-05-31 22:28:34 | [diff] [blame] | 246 | case -1: |
| 247 | return NO_RESPONSE; |
[email protected] | 4266def2 | 2012-05-17 01:02:40 | [diff] [blame] | 248 | case 200: |
| 249 | return SUCCESS; |
| 250 | case 400: |
| 251 | return BAD_REQUEST; |
| 252 | default: |
| 253 | return UNKNOWN_FAILURE; |
| 254 | } |
| 255 | } |
| 256 | |
[email protected] | 24f81ca | 2014-05-26 15:59:34 | [diff] [blame] | 257 | void MarkAppCleanShutdownAndCommit(PrefService* local_state) { |
[email protected] | d6147bd | 2014-06-11 01:58:19 | [diff] [blame] | 258 | local_state->SetBoolean(metrics::prefs::kStabilityExitedCleanly, true); |
| 259 | local_state->SetInteger(metrics::prefs::kStabilityExecutionPhase, |
[email protected] | 24f81ca | 2014-05-26 15:59:34 | [diff] [blame] | 260 | MetricsService::SHUTDOWN_COMPLETE); |
[email protected] | 84c384e | 2013-03-01 23:20:19 | [diff] [blame] | 261 | // Start writing right away (write happens on a different thread). |
[email protected] | 24f81ca | 2014-05-26 15:59:34 | [diff] [blame] | 262 | local_state->CommitPendingWrite(); |
[email protected] | 84c384e | 2013-03-01 23:20:19 | [diff] [blame] | 263 | } |
| 264 | |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 265 | } // namespace |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 266 | |
[email protected] | 6067756 | 2013-11-17 15:52:55 | [diff] [blame] | 267 | |
[email protected] | 7a5c0781 | 2014-02-26 11:45:41 | [diff] [blame] | 268 | SyntheticTrialGroup::SyntheticTrialGroup(uint32 trial, uint32 group) { |
[email protected] | 6067756 | 2013-11-17 15:52:55 | [diff] [blame] | 269 | id.name = trial; |
| 270 | id.group = group; |
| 271 | } |
| 272 | |
| 273 | SyntheticTrialGroup::~SyntheticTrialGroup() { |
| 274 | } |
| 275 | |
[email protected] | c0c55e9 | 2011-09-10 18:47:30 | [diff] [blame] | 276 | // static |
| 277 | MetricsService::ShutdownCleanliness MetricsService::clean_shutdown_status_ = |
| 278 | MetricsService::CLEANLY_SHUTDOWN; |
| 279 | |
[email protected] | 6a6d0d1 | 2013-10-28 15:58:19 | [diff] [blame] | 280 | MetricsService::ExecutionPhase MetricsService::execution_phase_ = |
[email protected] | 6d67ea0d | 2013-11-14 11:02:21 | [diff] [blame] | 281 | MetricsService::UNINITIALIZED_PHASE; |
[email protected] | 6a6d0d1 | 2013-10-28 15:58:19 | [diff] [blame] | 282 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 283 | // static |
[email protected] | b1de2c7 | 2013-02-06 02:45:47 | [diff] [blame] | 284 | void MetricsService::RegisterPrefs(PrefRegistrySimple* registry) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 285 | DCHECK(IsSingleThreaded()); |
[email protected] | 3907664 | 2014-05-05 20:32:55 | [diff] [blame] | 286 | metrics::MetricsStateManager::RegisterPrefs(registry); |
[email protected] | 91b1d91 | 2014-06-05 10:52:08 | [diff] [blame] | 287 | MetricsLog::RegisterPrefs(registry); |
[email protected] | 3907664 | 2014-05-05 20:32:55 | [diff] [blame] | 288 | |
[email protected] | 6580145 | 2014-07-09 05:42:41 | [diff] [blame] | 289 | registry->RegisterInt64Pref(metrics::prefs::kInstallDate, 0); |
| 290 | |
[email protected] | d6147bd | 2014-06-11 01:58:19 | [diff] [blame] | 291 | registry->RegisterInt64Pref(metrics::prefs::kStabilityLaunchTimeSec, 0); |
| 292 | registry->RegisterInt64Pref(metrics::prefs::kStabilityLastTimestampSec, 0); |
| 293 | registry->RegisterStringPref(metrics::prefs::kStabilityStatsVersion, |
| 294 | std::string()); |
| 295 | registry->RegisterInt64Pref(metrics::prefs::kStabilityStatsBuildTime, 0); |
| 296 | registry->RegisterBooleanPref(metrics::prefs::kStabilityExitedCleanly, true); |
| 297 | registry->RegisterIntegerPref(metrics::prefs::kStabilityExecutionPhase, |
[email protected] | 6d67ea0d | 2013-11-14 11:02:21 | [diff] [blame] | 298 | UNINITIALIZED_PHASE); |
[email protected] | d6147bd | 2014-06-11 01:58:19 | [diff] [blame] | 299 | registry->RegisterBooleanPref(metrics::prefs::kStabilitySessionEndCompleted, |
| 300 | true); |
[email protected] | 16a3091 | 2014-06-04 00:20:04 | [diff] [blame] | 301 | registry->RegisterIntegerPref(metrics::prefs::kMetricsSessionID, -1); |
[email protected] | 0f2f779 | 2013-11-28 16:09:14 | [diff] [blame] | 302 | |
[email protected] | 7f07db6 | 2014-05-15 01:12:45 | [diff] [blame] | 303 | registry->RegisterListPref(metrics::prefs::kMetricsInitialLogs); |
| 304 | registry->RegisterListPref(metrics::prefs::kMetricsOngoingLogs); |
[email protected] | 9706e1b | 2014-06-11 16:31:24 | [diff] [blame] | 305 | registry->RegisterListPref(metrics::prefs::kMetricsInitialLogsOld); |
| 306 | registry->RegisterListPref(metrics::prefs::kMetricsOngoingLogsOld); |
[email protected] | 0bb1a62 | 2009-03-04 03:22:32 | [diff] [blame] | 307 | |
[email protected] | d6147bd | 2014-06-11 01:58:19 | [diff] [blame] | 308 | registry->RegisterInt64Pref(metrics::prefs::kUninstallLaunchCount, 0); |
| 309 | registry->RegisterInt64Pref(metrics::prefs::kUninstallMetricsUptimeSec, 0); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 310 | } |
| 311 | |
[email protected] | 728de07 | 2014-05-21 09:20:32 | [diff] [blame] | 312 | MetricsService::MetricsService(metrics::MetricsStateManager* state_manager, |
[email protected] | 24f81ca | 2014-05-26 15:59:34 | [diff] [blame] | 313 | metrics::MetricsServiceClient* client, |
| 314 | PrefService* local_state) |
| 315 | : log_manager_(local_state, kUploadLogAvoidRetransmitSize), |
[email protected] | acc2ce551 | 2014-05-22 18:29:13 | [diff] [blame] | 316 | histogram_snapshot_manager_(this), |
[email protected] | 7f07db6 | 2014-05-15 01:12:45 | [diff] [blame] | 317 | state_manager_(state_manager), |
[email protected] | 728de07 | 2014-05-21 09:20:32 | [diff] [blame] | 318 | client_(client), |
[email protected] | 24f81ca | 2014-05-26 15:59:34 | [diff] [blame] | 319 | local_state_(local_state), |
[email protected] | 37d4709a | 2014-03-29 03:07:40 | [diff] [blame] | 320 | recording_active_(false), |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 321 | reporting_active_(false), |
[email protected] | 410938e0 | 2012-10-24 16:33:59 | [diff] [blame] | 322 | test_mode_active_(false), |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 323 | state_(INITIALIZED), |
[email protected] | 80a8f31 | 2013-12-16 18:00:30 | [diff] [blame] | 324 | has_initial_stability_log_(false), |
[email protected] | 0d5a61a8 | 2014-05-31 22:28:34 | [diff] [blame] | 325 | log_upload_in_progress_(false), |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 326 | idle_since_last_transmission_(false), |
[email protected] | 80a8f31 | 2013-12-16 18:00:30 | [diff] [blame] | 327 | session_id_(-1), |
[email protected] | 9c00909 | 2013-05-01 03:14:09 | [diff] [blame] | 328 | self_ptr_factory_(this), |
[email protected] | 0d5a61a8 | 2014-05-31 22:28:34 | [diff] [blame] | 329 | state_saver_factory_(this) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 330 | DCHECK(IsSingleThreaded()); |
[email protected] | 3907664 | 2014-05-05 20:32:55 | [diff] [blame] | 331 | DCHECK(state_manager_); |
[email protected] | 728de07 | 2014-05-21 09:20:32 | [diff] [blame] | 332 | DCHECK(client_); |
[email protected] | 24f81ca | 2014-05-26 15:59:34 | [diff] [blame] | 333 | DCHECK(local_state_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | MetricsService::~MetricsService() { |
[email protected] | 410938e0 | 2012-10-24 16:33:59 | [diff] [blame] | 337 | DisableRecording(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 338 | } |
| 339 | |
[email protected] | 3907664 | 2014-05-05 20:32:55 | [diff] [blame] | 340 | void MetricsService::InitializeMetricsRecordingState() { |
| 341 | InitializeMetricsState(); |
[email protected] | 80a8f31 | 2013-12-16 18:00:30 | [diff] [blame] | 342 | |
| 343 | base::Closure callback = base::Bind(&MetricsService::StartScheduledUpload, |
| 344 | self_ptr_factory_.GetWeakPtr()); |
| 345 | scheduler_.reset(new MetricsReportingScheduler(callback)); |
| 346 | } |
| 347 | |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 348 | void MetricsService::Start() { |
[email protected] | b1c8dc0 | 2011-04-13 18:32:04 | [diff] [blame] | 349 | HandleIdleSinceLastTransmission(false); |
[email protected] | 410938e0 | 2012-10-24 16:33:59 | [diff] [blame] | 350 | EnableRecording(); |
| 351 | EnableReporting(); |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 352 | } |
| 353 | |
[email protected] | 3907664 | 2014-05-05 20:32:55 | [diff] [blame] | 354 | bool MetricsService::StartIfMetricsReportingEnabled() { |
| 355 | const bool enabled = state_manager_->IsMetricsReportingEnabled(); |
| 356 | if (enabled) |
| 357 | Start(); |
| 358 | return enabled; |
| 359 | } |
| 360 | |
[email protected] | 410938e0 | 2012-10-24 16:33:59 | [diff] [blame] | 361 | void MetricsService::StartRecordingForTests() { |
| 362 | test_mode_active_ = true; |
| 363 | EnableRecording(); |
| 364 | DisableReporting(); |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | void MetricsService::Stop() { |
[email protected] | b1c8dc0 | 2011-04-13 18:32:04 | [diff] [blame] | 368 | HandleIdleSinceLastTransmission(false); |
[email protected] | 410938e0 | 2012-10-24 16:33:59 | [diff] [blame] | 369 | DisableReporting(); |
| 370 | DisableRecording(); |
| 371 | } |
| 372 | |
| 373 | void MetricsService::EnableReporting() { |
| 374 | if (reporting_active_) |
| 375 | return; |
| 376 | reporting_active_ = true; |
| 377 | StartSchedulerIfNecessary(); |
| 378 | } |
| 379 | |
| 380 | void MetricsService::DisableReporting() { |
| 381 | reporting_active_ = false; |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 382 | } |
| 383 | |
[email protected] | edafd4c | 2011-05-10 17:18:53 | [diff] [blame] | 384 | std::string MetricsService::GetClientId() { |
[email protected] | 3907664 | 2014-05-05 20:32:55 | [diff] [blame] | 385 | return state_manager_->client_id(); |
[email protected] | edafd4c | 2011-05-10 17:18:53 | [diff] [blame] | 386 | } |
| 387 | |
[email protected] | 6580145 | 2014-07-09 05:42:41 | [diff] [blame] | 388 | int64 MetricsService::GetInstallDate() { |
| 389 | return local_state_->GetInt64(metrics::prefs::kInstallDate); |
| 390 | } |
| 391 | |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 392 | scoped_ptr<const base::FieldTrial::EntropyProvider> |
[email protected] | 3907664 | 2014-05-05 20:32:55 | [diff] [blame] | 393 | MetricsService::CreateEntropyProvider() { |
| 394 | // TODO(asvitkine): Refactor the code so that MetricsService does not expose |
| 395 | // this method. |
| 396 | return state_manager_->CreateEntropyProvider(); |
[email protected] | 5cbeeef7 | 2012-02-08 02:05:18 | [diff] [blame] | 397 | } |
| 398 | |
[email protected] | 410938e0 | 2012-10-24 16:33:59 | [diff] [blame] | 399 | void MetricsService::EnableRecording() { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 400 | DCHECK(IsSingleThreaded()); |
| 401 | |
[email protected] | 410938e0 | 2012-10-24 16:33:59 | [diff] [blame] | 402 | if (recording_active_) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 403 | return; |
[email protected] | 410938e0 | 2012-10-24 16:33:59 | [diff] [blame] | 404 | recording_active_ = true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 405 | |
[email protected] | 3907664 | 2014-05-05 20:32:55 | [diff] [blame] | 406 | state_manager_->ForceClientIdCreation(); |
[email protected] | 9d1b015 | 2014-07-09 18:53:22 | [diff] [blame^] | 407 | client_->SetMetricsClientId(state_manager_->client_id()); |
[email protected] | 410938e0 | 2012-10-24 16:33:59 | [diff] [blame] | 408 | if (!log_manager_.current_log()) |
| 409 | OpenNewLog(); |
[email protected] | 005ef3e | 2009-05-22 20:55:46 | [diff] [blame] | 410 | |
[email protected] | 85791b0b | 2014-05-20 15:18:58 | [diff] [blame] | 411 | for (size_t i = 0; i < metrics_providers_.size(); ++i) |
| 412 | metrics_providers_[i]->OnRecordingEnabled(); |
| 413 | |
[email protected] | e6e30ac | 2014-01-13 21:24:39 | [diff] [blame] | 414 | base::RemoveActionCallback(action_callback_); |
[email protected] | dd98f39 | 2013-02-04 13:03:22 | [diff] [blame] | 415 | action_callback_ = base::Bind(&MetricsService::OnUserAction, |
| 416 | base::Unretained(this)); |
[email protected] | e6e30ac | 2014-01-13 21:24:39 | [diff] [blame] | 417 | base::AddActionCallback(action_callback_); |
[email protected] | 410938e0 | 2012-10-24 16:33:59 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | void MetricsService::DisableRecording() { |
| 421 | DCHECK(IsSingleThreaded()); |
| 422 | |
| 423 | if (!recording_active_) |
| 424 | return; |
| 425 | recording_active_ = false; |
| 426 | |
[email protected] | e6e30ac | 2014-01-13 21:24:39 | [diff] [blame] | 427 | base::RemoveActionCallback(action_callback_); |
[email protected] | 85791b0b | 2014-05-20 15:18:58 | [diff] [blame] | 428 | |
| 429 | for (size_t i = 0; i < metrics_providers_.size(); ++i) |
| 430 | metrics_providers_[i]->OnRecordingDisabled(); |
| 431 | |
[email protected] | 410938e0 | 2012-10-24 16:33:59 | [diff] [blame] | 432 | PushPendingLogsToPersistentStorage(); |
| 433 | DCHECK(!log_manager_.has_staged_log()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 434 | } |
| 435 | |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 436 | bool MetricsService::recording_active() const { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 437 | DCHECK(IsSingleThreaded()); |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 438 | return recording_active_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 439 | } |
| 440 | |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 441 | bool MetricsService::reporting_active() const { |
| 442 | DCHECK(IsSingleThreaded()); |
| 443 | return reporting_active_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 444 | } |
| 445 | |
[email protected] | acc2ce551 | 2014-05-22 18:29:13 | [diff] [blame] | 446 | void MetricsService::RecordDelta(const base::HistogramBase& histogram, |
| 447 | const base::HistogramSamples& snapshot) { |
| 448 | log_manager_.current_log()->RecordHistogramDelta(histogram.histogram_name(), |
| 449 | snapshot); |
| 450 | } |
| 451 | |
| 452 | void MetricsService::InconsistencyDetected( |
| 453 | base::HistogramBase::Inconsistency problem) { |
| 454 | UMA_HISTOGRAM_ENUMERATION("Histogram.InconsistenciesBrowser", |
| 455 | problem, base::HistogramBase::NEVER_EXCEEDED_VALUE); |
| 456 | } |
| 457 | |
| 458 | void MetricsService::UniqueInconsistencyDetected( |
| 459 | base::HistogramBase::Inconsistency problem) { |
| 460 | UMA_HISTOGRAM_ENUMERATION("Histogram.InconsistenciesBrowserUnique", |
| 461 | problem, base::HistogramBase::NEVER_EXCEEDED_VALUE); |
| 462 | } |
| 463 | |
| 464 | void MetricsService::InconsistencyDetectedInLoggedCount(int amount) { |
| 465 | UMA_HISTOGRAM_COUNTS("Histogram.InconsistentSnapshotBrowser", |
| 466 | std::abs(amount)); |
| 467 | } |
| 468 | |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 469 | void MetricsService::HandleIdleSinceLastTransmission(bool in_idle) { |
| 470 | // If there wasn't a lot of action, maybe the computer was asleep, in which |
| 471 | // case, the log transmissions should have stopped. Here we start them up |
| 472 | // again. |
[email protected] | cac7884 | 2008-11-27 01:02:20 | [diff] [blame] | 473 | if (!in_idle && idle_since_last_transmission_) |
[email protected] | 7f7f196 | 2011-04-20 15:58:16 | [diff] [blame] | 474 | StartSchedulerIfNecessary(); |
[email protected] | cac7884 | 2008-11-27 01:02:20 | [diff] [blame] | 475 | idle_since_last_transmission_ = in_idle; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 476 | } |
| 477 | |
[email protected] | d7ea39e | 2014-05-22 03:59:18 | [diff] [blame] | 478 | void MetricsService::OnApplicationNotIdle() { |
| 479 | if (recording_active_) |
| 480 | HandleIdleSinceLastTransmission(false); |
| 481 | } |
| 482 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 483 | void MetricsService::RecordStartOfSessionEnd() { |
[email protected] | 466f3c1 | 2011-03-23 21:20:38 | [diff] [blame] | 484 | LogCleanShutdown(); |
[email protected] | d6147bd | 2014-06-11 01:58:19 | [diff] [blame] | 485 | RecordBooleanPrefValue(metrics::prefs::kStabilitySessionEndCompleted, false); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | void MetricsService::RecordCompletedSessionEnd() { |
[email protected] | 466f3c1 | 2011-03-23 21:20:38 | [diff] [blame] | 489 | LogCleanShutdown(); |
[email protected] | d6147bd | 2014-06-11 01:58:19 | [diff] [blame] | 490 | RecordBooleanPrefValue(metrics::prefs::kStabilitySessionEndCompleted, true); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 491 | } |
| 492 | |
[email protected] | 410938e0 | 2012-10-24 16:33:59 | [diff] [blame] | 493 | #if defined(OS_ANDROID) || defined(OS_IOS) |
[email protected] | 117fbdf2 | 2012-06-26 18:36:39 | [diff] [blame] | 494 | void MetricsService::OnAppEnterBackground() { |
| 495 | scheduler_->Stop(); |
| 496 | |
[email protected] | 24f81ca | 2014-05-26 15:59:34 | [diff] [blame] | 497 | MarkAppCleanShutdownAndCommit(local_state_); |
[email protected] | 117fbdf2 | 2012-06-26 18:36:39 | [diff] [blame] | 498 | |
| 499 | // At this point, there's no way of knowing when the process will be |
| 500 | // killed, so this has to be treated similar to a shutdown, closing and |
| 501 | // persisting all logs. Unlinke a shutdown, the state is primed to be ready |
| 502 | // to continue logging and uploading if the process does return. |
[email protected] | 80a8f31 | 2013-12-16 18:00:30 | [diff] [blame] | 503 | if (recording_active() && state_ >= SENDING_INITIAL_STABILITY_LOG) { |
[email protected] | 117fbdf2 | 2012-06-26 18:36:39 | [diff] [blame] | 504 | PushPendingLogsToPersistentStorage(); |
[email protected] | 410938e0 | 2012-10-24 16:33:59 | [diff] [blame] | 505 | // Persisting logs closes the current log, so start recording a new log |
| 506 | // immediately to capture any background work that might be done before the |
| 507 | // process is killed. |
| 508 | OpenNewLog(); |
[email protected] | 117fbdf2 | 2012-06-26 18:36:39 | [diff] [blame] | 509 | } |
[email protected] | 117fbdf2 | 2012-06-26 18:36:39 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | void MetricsService::OnAppEnterForeground() { |
[email protected] | d6147bd | 2014-06-11 01:58:19 | [diff] [blame] | 513 | local_state_->SetBoolean(metrics::prefs::kStabilityExitedCleanly, false); |
[email protected] | 117fbdf2 | 2012-06-26 18:36:39 | [diff] [blame] | 514 | StartSchedulerIfNecessary(); |
| 515 | } |
[email protected] | 84c384e | 2013-03-01 23:20:19 | [diff] [blame] | 516 | #else |
[email protected] | 24f81ca | 2014-05-26 15:59:34 | [diff] [blame] | 517 | void MetricsService::LogNeedForCleanShutdown(PrefService* local_state) { |
[email protected] | d6147bd | 2014-06-11 01:58:19 | [diff] [blame] | 518 | local_state->SetBoolean(metrics::prefs::kStabilityExitedCleanly, false); |
[email protected] | 84c384e | 2013-03-01 23:20:19 | [diff] [blame] | 519 | // Redundant setting to be sure we call for a clean shutdown. |
| 520 | clean_shutdown_status_ = NEED_TO_SHUTDOWN; |
| 521 | } |
| 522 | #endif // defined(OS_ANDROID) || defined(OS_IOS) |
[email protected] | 117fbdf2 | 2012-06-26 18:36:39 | [diff] [blame] | 523 | |
[email protected] | 6d67ea0d | 2013-11-14 11:02:21 | [diff] [blame] | 524 | // static |
[email protected] | 24f81ca | 2014-05-26 15:59:34 | [diff] [blame] | 525 | void MetricsService::SetExecutionPhase(ExecutionPhase execution_phase, |
| 526 | PrefService* local_state) { |
[email protected] | 6d67ea0d | 2013-11-14 11:02:21 | [diff] [blame] | 527 | execution_phase_ = execution_phase; |
[email protected] | d6147bd | 2014-06-11 01:58:19 | [diff] [blame] | 528 | local_state->SetInteger(metrics::prefs::kStabilityExecutionPhase, |
| 529 | execution_phase_); |
[email protected] | 6d67ea0d | 2013-11-14 11:02:21 | [diff] [blame] | 530 | } |
| 531 | |
[email protected] | 7f7f196 | 2011-04-20 15:58:16 | [diff] [blame] | 532 | void MetricsService::RecordBreakpadRegistration(bool success) { |
[email protected] | 68475e60 | 2008-08-22 03:21:15 | [diff] [blame] | 533 | if (!success) |
[email protected] | 91b1d91 | 2014-06-05 10:52:08 | [diff] [blame] | 534 | IncrementPrefValue(metrics::prefs::kStabilityBreakpadRegistrationFail); |
[email protected] | e73c0197 | 2008-08-13 00:18:24 | [diff] [blame] | 535 | else |
[email protected] | 91b1d91 | 2014-06-05 10:52:08 | [diff] [blame] | 536 | IncrementPrefValue(metrics::prefs::kStabilityBreakpadRegistrationSuccess); |
[email protected] | e73c0197 | 2008-08-13 00:18:24 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | void MetricsService::RecordBreakpadHasDebugger(bool has_debugger) { |
| 540 | if (!has_debugger) |
[email protected] | 91b1d91 | 2014-06-05 10:52:08 | [diff] [blame] | 541 | IncrementPrefValue(metrics::prefs::kStabilityDebuggerNotPresent); |
[email protected] | e73c0197 | 2008-08-13 00:18:24 | [diff] [blame] | 542 | else |
[email protected] | 91b1d91 | 2014-06-05 10:52:08 | [diff] [blame] | 543 | IncrementPrefValue(metrics::prefs::kStabilityDebuggerPresent); |
[email protected] | e73c0197 | 2008-08-13 00:18:24 | [diff] [blame] | 544 | } |
| 545 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 546 | //------------------------------------------------------------------------------ |
| 547 | // private methods |
| 548 | //------------------------------------------------------------------------------ |
| 549 | |
| 550 | |
| 551 | //------------------------------------------------------------------------------ |
| 552 | // Initialization methods |
| 553 | |
[email protected] | 3907664 | 2014-05-05 20:32:55 | [diff] [blame] | 554 | void MetricsService::InitializeMetricsState() { |
[email protected] | d6147bd | 2014-06-11 01:58:19 | [diff] [blame] | 555 | local_state_->SetString(metrics::prefs::kStabilityStatsVersion, |
[email protected] | 24f81ca | 2014-05-26 15:59:34 | [diff] [blame] | 556 | client_->GetVersionString()); |
[email protected] | d6147bd | 2014-06-11 01:58:19 | [diff] [blame] | 557 | local_state_->SetInt64(metrics::prefs::kStabilityStatsBuildTime, |
[email protected] | 24f81ca | 2014-05-26 15:59:34 | [diff] [blame] | 558 | MetricsLog::GetBuildTime()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 559 | |
[email protected] | 16a3091 | 2014-06-04 00:20:04 | [diff] [blame] | 560 | session_id_ = local_state_->GetInteger(metrics::prefs::kMetricsSessionID); |
[email protected] | 541f7792 | 2009-02-23 21:14:38 | [diff] [blame] | 561 | |
[email protected] | d6147bd | 2014-06-11 01:58:19 | [diff] [blame] | 562 | if (!local_state_->GetBoolean(metrics::prefs::kStabilityExitedCleanly)) { |
[email protected] | 91b1d91 | 2014-06-05 10:52:08 | [diff] [blame] | 563 | IncrementPrefValue(metrics::prefs::kStabilityCrashCount); |
[email protected] | c0c55e9 | 2011-09-10 18:47:30 | [diff] [blame] | 564 | // Reset flag, and wait until we call LogNeedForCleanShutdown() before |
| 565 | // monitoring. |
[email protected] | d6147bd | 2014-06-11 01:58:19 | [diff] [blame] | 566 | local_state_->SetBoolean(metrics::prefs::kStabilityExitedCleanly, true); |
[email protected] | 6a6d0d1 | 2013-10-28 15:58:19 | [diff] [blame] | 567 | |
| 568 | // TODO(rtenneti): On windows, consider saving/getting execution_phase from |
| 569 | // the registry. |
[email protected] | 24f81ca | 2014-05-26 15:59:34 | [diff] [blame] | 570 | int execution_phase = |
[email protected] | d6147bd | 2014-06-11 01:58:19 | [diff] [blame] | 571 | local_state_->GetInteger(metrics::prefs::kStabilityExecutionPhase); |
[email protected] | 6d67ea0d | 2013-11-14 11:02:21 | [diff] [blame] | 572 | UMA_HISTOGRAM_SPARSE_SLOWLY("Chrome.Browser.CrashedExecutionPhase", |
[email protected] | 6a6d0d1 | 2013-10-28 15:58:19 | [diff] [blame] | 573 | execution_phase); |
[email protected] | 80a8f31 | 2013-12-16 18:00:30 | [diff] [blame] | 574 | |
| 575 | // If the previous session didn't exit cleanly, then prepare an initial |
| 576 | // stability log if UMA is enabled. |
[email protected] | 3907664 | 2014-05-05 20:32:55 | [diff] [blame] | 577 | if (state_manager_->IsMetricsReportingEnabled()) |
[email protected] | 80a8f31 | 2013-12-16 18:00:30 | [diff] [blame] | 578 | PrepareInitialStabilityLog(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 579 | } |
[email protected] | 80a8f31 | 2013-12-16 18:00:30 | [diff] [blame] | 580 | |
| 581 | // Update session ID. |
| 582 | ++session_id_; |
[email protected] | 16a3091 | 2014-06-04 00:20:04 | [diff] [blame] | 583 | local_state_->SetInteger(metrics::prefs::kMetricsSessionID, session_id_); |
[email protected] | 80a8f31 | 2013-12-16 18:00:30 | [diff] [blame] | 584 | |
| 585 | // Stability bookkeeping |
[email protected] | 91b1d91 | 2014-06-05 10:52:08 | [diff] [blame] | 586 | IncrementPrefValue(metrics::prefs::kStabilityLaunchCount); |
[email protected] | 80a8f31 | 2013-12-16 18:00:30 | [diff] [blame] | 587 | |
[email protected] | 6d67ea0d | 2013-11-14 11:02:21 | [diff] [blame] | 588 | DCHECK_EQ(UNINITIALIZED_PHASE, execution_phase_); |
[email protected] | 24f81ca | 2014-05-26 15:59:34 | [diff] [blame] | 589 | SetExecutionPhase(START_METRICS_RECORDING, local_state_); |
[email protected] | e73c0197 | 2008-08-13 00:18:24 | [diff] [blame] | 590 | |
[email protected] | d6147bd | 2014-06-11 01:58:19 | [diff] [blame] | 591 | if (!local_state_->GetBoolean( |
| 592 | metrics::prefs::kStabilitySessionEndCompleted)) { |
[email protected] | 91b1d91 | 2014-06-05 10:52:08 | [diff] [blame] | 593 | IncrementPrefValue(metrics::prefs::kStabilityIncompleteSessionEndCount); |
[email protected] | c9abf24 | 2009-07-18 06:00:38 | [diff] [blame] | 594 | // This is marked false when we get a WM_ENDSESSION. |
[email protected] | d6147bd | 2014-06-11 01:58:19 | [diff] [blame] | 595 | local_state_->SetBoolean(metrics::prefs::kStabilitySessionEndCompleted, |
| 596 | true); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 597 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 598 | |
[email protected] | 076961c | 2014-03-12 22:23:56 | [diff] [blame] | 599 | // Call GetUptimes() for the first time, thus allowing all later calls |
| 600 | // to record incremental uptimes accurately. |
| 601 | base::TimeDelta ignored_uptime_parameter; |
| 602 | base::TimeDelta startup_uptime; |
[email protected] | 24f81ca | 2014-05-26 15:59:34 | [diff] [blame] | 603 | GetUptimes(local_state_, &startup_uptime, &ignored_uptime_parameter); |
[email protected] | c68a2b9b | 2013-10-09 18:16:36 | [diff] [blame] | 604 | DCHECK_EQ(0, startup_uptime.InMicroseconds()); |
[email protected] | 9165f74 | 2010-03-10 22:55:01 | [diff] [blame] | 605 | // For backwards compatibility, leave this intact in case Omaha is checking |
[email protected] | d6147bd | 2014-06-11 01:58:19 | [diff] [blame] | 606 | // them. metrics::prefs::kStabilityLastTimestampSec may also be useless now. |
[email protected] | 9165f74 | 2010-03-10 22:55:01 | [diff] [blame] | 607 | // TODO(jar): Delete these if they have no uses. |
[email protected] | d6147bd | 2014-06-11 01:58:19 | [diff] [blame] | 608 | local_state_->SetInt64(metrics::prefs::kStabilityLaunchTimeSec, |
| 609 | Time::Now().ToTimeT()); |
[email protected] | 0bb1a62 | 2009-03-04 03:22:32 | [diff] [blame] | 610 | |
| 611 | // Bookkeeping for the uninstall metrics. |
[email protected] | d6147bd | 2014-06-11 01:58:19 | [diff] [blame] | 612 | IncrementLongPrefsValue(metrics::prefs::kUninstallLaunchCount); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 613 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 614 | // Kick off the process of saving the state (so the uptime numbers keep |
| 615 | // getting updated) every n minutes. |
| 616 | ScheduleNextStateSave(); |
| 617 | } |
| 618 | |
[email protected] | dd98f39 | 2013-02-04 13:03:22 | [diff] [blame] | 619 | void MetricsService::OnUserAction(const std::string& action) { |
[email protected] | e5ad60a | 2014-03-11 03:54:04 | [diff] [blame] | 620 | if (!ShouldLogEvents()) |
[email protected] | dd98f39 | 2013-02-04 13:03:22 | [diff] [blame] | 621 | return; |
| 622 | |
[email protected] | 4426d2d | 2014-04-09 12:33:00 | [diff] [blame] | 623 | log_manager_.current_log()->RecordUserAction(action); |
[email protected] | dd98f39 | 2013-02-04 13:03:22 | [diff] [blame] | 624 | HandleIdleSinceLastTransmission(false); |
| 625 | } |
| 626 | |
[email protected] | 4a55a71 | 2014-06-08 16:50:34 | [diff] [blame] | 627 | void MetricsService::FinishedGatheringInitialMetrics() { |
[email protected] | ed0fd00 | 2012-04-25 23:10:34 | [diff] [blame] | 628 | DCHECK_EQ(INIT_TASK_SCHEDULED, state_); |
[email protected] | c68a2b9b | 2013-10-09 18:16:36 | [diff] [blame] | 629 | state_ = INIT_TASK_DONE; |
[email protected] | 83d09f9 | 2014-06-03 14:58:26 | [diff] [blame] | 630 | |
| 631 | // Create the initial log. |
| 632 | if (!initial_metrics_log_.get()) { |
| 633 | initial_metrics_log_ = CreateLog(MetricsLog::ONGOING_LOG); |
| 634 | NotifyOnDidCreateMetricsLog(); |
| 635 | } |
| 636 | |
[email protected] | 70886cd | 2013-12-04 05:53:42 | [diff] [blame] | 637 | scheduler_->InitTaskComplete(); |
[email protected] | c68a2b9b | 2013-10-09 18:16:36 | [diff] [blame] | 638 | } |
| 639 | |
[email protected] | 076961c | 2014-03-12 22:23:56 | [diff] [blame] | 640 | void MetricsService::GetUptimes(PrefService* pref, |
| 641 | base::TimeDelta* incremental_uptime, |
| 642 | base::TimeDelta* uptime) { |
[email protected] | c68a2b9b | 2013-10-09 18:16:36 | [diff] [blame] | 643 | base::TimeTicks now = base::TimeTicks::Now(); |
[email protected] | 076961c | 2014-03-12 22:23:56 | [diff] [blame] | 644 | // If this is the first call, init |first_updated_time_| and |
| 645 | // |last_updated_time_|. |
| 646 | if (last_updated_time_.is_null()) { |
| 647 | first_updated_time_ = now; |
[email protected] | c68a2b9b | 2013-10-09 18:16:36 | [diff] [blame] | 648 | last_updated_time_ = now; |
[email protected] | 076961c | 2014-03-12 22:23:56 | [diff] [blame] | 649 | } |
| 650 | *incremental_uptime = now - last_updated_time_; |
| 651 | *uptime = now - first_updated_time_; |
[email protected] | c68a2b9b | 2013-10-09 18:16:36 | [diff] [blame] | 652 | last_updated_time_ = now; |
| 653 | |
[email protected] | 076961c | 2014-03-12 22:23:56 | [diff] [blame] | 654 | const int64 incremental_time_secs = incremental_uptime->InSeconds(); |
[email protected] | c68a2b9b | 2013-10-09 18:16:36 | [diff] [blame] | 655 | if (incremental_time_secs > 0) { |
[email protected] | d6147bd | 2014-06-11 01:58:19 | [diff] [blame] | 656 | int64 metrics_uptime = |
| 657 | pref->GetInt64(metrics::prefs::kUninstallMetricsUptimeSec); |
[email protected] | c68a2b9b | 2013-10-09 18:16:36 | [diff] [blame] | 658 | metrics_uptime += incremental_time_secs; |
[email protected] | d6147bd | 2014-06-11 01:58:19 | [diff] [blame] | 659 | pref->SetInt64(metrics::prefs::kUninstallMetricsUptimeSec, metrics_uptime); |
[email protected] | c68a2b9b | 2013-10-09 18:16:36 | [diff] [blame] | 660 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 661 | } |
| 662 | |
[email protected] | 2a321de3 | 2014-05-10 19:59:06 | [diff] [blame] | 663 | void MetricsService::AddObserver(MetricsServiceObserver* observer) { |
| 664 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 665 | observers_.AddObserver(observer); |
| 666 | } |
| 667 | |
| 668 | void MetricsService::RemoveObserver(MetricsServiceObserver* observer) { |
| 669 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 670 | observers_.RemoveObserver(observer); |
| 671 | } |
| 672 | |
| 673 | void MetricsService::NotifyOnDidCreateMetricsLog() { |
| 674 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 675 | FOR_EACH_OBSERVER( |
| 676 | MetricsServiceObserver, observers_, OnDidCreateMetricsLog()); |
[email protected] | 8304f61a | 2014-05-24 12:17:33 | [diff] [blame] | 677 | for (size_t i = 0; i < metrics_providers_.size(); ++i) |
| 678 | metrics_providers_[i]->OnDidCreateMetricsLog(); |
[email protected] | 2a321de3 | 2014-05-10 19:59:06 | [diff] [blame] | 679 | } |
| 680 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 681 | //------------------------------------------------------------------------------ |
| 682 | // State save methods |
| 683 | |
| 684 | void MetricsService::ScheduleNextStateSave() { |
[email protected] | 8454aeb | 2011-11-19 23:38:20 | [diff] [blame] | 685 | state_saver_factory_.InvalidateWeakPtrs(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 686 | |
[email protected] | b3a2509 | 2013-05-28 22:08:16 | [diff] [blame] | 687 | base::MessageLoop::current()->PostDelayedTask(FROM_HERE, |
[email protected] | 8454aeb | 2011-11-19 23:38:20 | [diff] [blame] | 688 | base::Bind(&MetricsService::SaveLocalState, |
| 689 | state_saver_factory_.GetWeakPtr()), |
[email protected] | fc4252a7 | 2012-01-12 21:58:47 | [diff] [blame] | 690 | base::TimeDelta::FromMinutes(kSaveStateIntervalMinutes)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | void MetricsService::SaveLocalState() { |
[email protected] | 24f81ca | 2014-05-26 15:59:34 | [diff] [blame] | 694 | RecordCurrentState(local_state_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 695 | |
[email protected] | fc4252a7 | 2012-01-12 21:58:47 | [diff] [blame] | 696 | // TODO(jar):110021 Does this run down the batteries???? |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 697 | ScheduleNextStateSave(); |
| 698 | } |
| 699 | |
| 700 | |
| 701 | //------------------------------------------------------------------------------ |
| 702 | // Recording control methods |
| 703 | |
[email protected] | 410938e0 | 2012-10-24 16:33:59 | [diff] [blame] | 704 | void MetricsService::OpenNewLog() { |
| 705 | DCHECK(!log_manager_.current_log()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 706 | |
[email protected] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 707 | log_manager_.BeginLoggingWithLog(CreateLog(MetricsLog::ONGOING_LOG)); |
[email protected] | 2a321de3 | 2014-05-10 19:59:06 | [diff] [blame] | 708 | NotifyOnDidCreateMetricsLog(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 709 | if (state_ == INITIALIZED) { |
| 710 | // We only need to schedule that run once. |
[email protected] | 85ed9d4 | 2010-06-08 22:37:44 | [diff] [blame] | 711 | state_ = INIT_TASK_SCHEDULED; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 712 | |
[email protected] | d6147bd | 2014-06-11 01:58:19 | [diff] [blame] | 713 | base::MessageLoop::current()->PostDelayedTask( |
[email protected] | ed10dd1 | 2011-12-07 12:03:42 | [diff] [blame] | 714 | FROM_HERE, |
[email protected] | 51994b2 | 2014-05-30 13:24:21 | [diff] [blame] | 715 | base::Bind(&MetricsService::StartGatheringMetrics, |
| 716 | self_ptr_factory_.GetWeakPtr()), |
[email protected] | 7e56010 | 2012-03-08 20:58:42 | [diff] [blame] | 717 | base::TimeDelta::FromSeconds(kInitializationDelaySeconds)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 718 | } |
| 719 | } |
| 720 | |
[email protected] | 51994b2 | 2014-05-30 13:24:21 | [diff] [blame] | 721 | void MetricsService::StartGatheringMetrics() { |
[email protected] | 51994b2 | 2014-05-30 13:24:21 | [diff] [blame] | 722 | client_->StartGatheringMetrics( |
[email protected] | 4a55a71 | 2014-06-08 16:50:34 | [diff] [blame] | 723 | base::Bind(&MetricsService::FinishedGatheringInitialMetrics, |
[email protected] | 51994b2 | 2014-05-30 13:24:21 | [diff] [blame] | 724 | self_ptr_factory_.GetWeakPtr())); |
| 725 | } |
| 726 | |
[email protected] | 410938e0 | 2012-10-24 16:33:59 | [diff] [blame] | 727 | void MetricsService::CloseCurrentLog() { |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 728 | if (!log_manager_.current_log()) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 729 | return; |
| 730 | |
[email protected] | 68475e60 | 2008-08-22 03:21:15 | [diff] [blame] | 731 | // TODO(jar): Integrate bounds on log recording more consistently, so that we |
| 732 | // can stop recording logs that are too big much sooner. |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 733 | if (log_manager_.current_log()->num_events() > kEventLimit) { |
[email protected] | 553dba6 | 2009-02-24 19:08:23 | [diff] [blame] | 734 | UMA_HISTOGRAM_COUNTS("UMA.Discarded Log Events", |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 735 | log_manager_.current_log()->num_events()); |
| 736 | log_manager_.DiscardCurrentLog(); |
[email protected] | 410938e0 | 2012-10-24 16:33:59 | [diff] [blame] | 737 | OpenNewLog(); // Start trivial log to hold our histograms. |
[email protected] | 68475e60 | 2008-08-22 03:21:15 | [diff] [blame] | 738 | } |
| 739 | |
[email protected] | 0b33f80b | 2008-12-17 21:34:36 | [diff] [blame] | 740 | // Put incremental data (histogram deltas, and realtime stats deltas) at the |
[email protected] | 147bbc0b | 2009-01-06 19:37:40 | [diff] [blame] | 741 | // end of all log transmissions (initial log handles this separately). |
[email protected] | 024b5cd | 2011-05-27 03:29:38 | [diff] [blame] | 742 | // RecordIncrementalStabilityElements only exists on the derived |
| 743 | // MetricsLog class. |
[email protected] | 279703f | 2012-01-20 22:23:26 | [diff] [blame] | 744 | MetricsLog* current_log = |
| 745 | static_cast<MetricsLog*>(log_manager_.current_log()); |
[email protected] | 024b5cd | 2011-05-27 03:29:38 | [diff] [blame] | 746 | DCHECK(current_log); |
[email protected] | b3610d4 | 2014-05-19 18:07:23 | [diff] [blame] | 747 | std::vector<variations::ActiveGroupId> synthetic_trials; |
[email protected] | 6067756 | 2013-11-17 15:52:55 | [diff] [blame] | 748 | GetCurrentSyntheticFieldTrials(&synthetic_trials); |
[email protected] | 6580145 | 2014-07-09 05:42:41 | [diff] [blame] | 749 | current_log->RecordEnvironment( |
| 750 | metrics_providers_.get(), synthetic_trials, GetInstallDate()); |
[email protected] | 076961c | 2014-03-12 22:23:56 | [diff] [blame] | 751 | base::TimeDelta incremental_uptime; |
| 752 | base::TimeDelta uptime; |
[email protected] | 24f81ca | 2014-05-26 15:59:34 | [diff] [blame] | 753 | GetUptimes(local_state_, &incremental_uptime, &uptime); |
[email protected] | 85791b0b | 2014-05-20 15:18:58 | [diff] [blame] | 754 | current_log->RecordStabilityMetrics(metrics_providers_.get(), |
| 755 | incremental_uptime, uptime); |
[email protected] | 6067756 | 2013-11-17 15:52:55 | [diff] [blame] | 756 | |
[email protected] | 024b5cd | 2011-05-27 03:29:38 | [diff] [blame] | 757 | RecordCurrentHistograms(); |
[email protected] | 85791b0b | 2014-05-20 15:18:58 | [diff] [blame] | 758 | current_log->RecordGeneralMetrics(metrics_providers_.get()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 759 | |
[email protected] | 2994826 | 2012-03-01 12:15:08 | [diff] [blame] | 760 | log_manager_.FinishCurrentLog(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 761 | } |
| 762 | |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 763 | void MetricsService::PushPendingLogsToPersistentStorage() { |
[email protected] | 80a8f31 | 2013-12-16 18:00:30 | [diff] [blame] | 764 | if (state_ < SENDING_INITIAL_STABILITY_LOG) |
[email protected] | 28ab7f9 | 2009-01-06 21:39:04 | [diff] [blame] | 765 | return; // We didn't and still don't have time to get plugin list etc. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 766 | |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 767 | if (log_manager_.has_staged_log()) { |
[email protected] | 7d41ae6d | 2012-06-26 08:53:03 | [diff] [blame] | 768 | // We may race here, and send second copy of the log later. |
[email protected] | 7f07db6 | 2014-05-15 01:12:45 | [diff] [blame] | 769 | metrics::PersistedLogs::StoreType store_type; |
[email protected] | 0d5a61a8 | 2014-05-31 22:28:34 | [diff] [blame] | 770 | if (log_upload_in_progress_) |
[email protected] | 7f07db6 | 2014-05-15 01:12:45 | [diff] [blame] | 771 | store_type = metrics::PersistedLogs::PROVISIONAL_STORE; |
[email protected] | dc61fe9 | 2012-06-12 00:13:50 | [diff] [blame] | 772 | else |
[email protected] | 7f07db6 | 2014-05-15 01:12:45 | [diff] [blame] | 773 | store_type = metrics::PersistedLogs::NORMAL_STORE; |
[email protected] | e7508d8 | 2012-05-03 15:59:53 | [diff] [blame] | 774 | log_manager_.StoreStagedLogAsUnsent(store_type); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 775 | } |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 776 | DCHECK(!log_manager_.has_staged_log()); |
[email protected] | 410938e0 | 2012-10-24 16:33:59 | [diff] [blame] | 777 | CloseCurrentLog(); |
[email protected] | 80a8f31 | 2013-12-16 18:00:30 | [diff] [blame] | 778 | log_manager_.PersistUnsentLogs(); |
[email protected] | 7d41ae6d | 2012-06-26 08:53:03 | [diff] [blame] | 779 | |
| 780 | // If there was a staged and/or current log, then there is now at least one |
| 781 | // log waiting to be uploaded. |
| 782 | if (log_manager_.has_unsent_logs()) |
| 783 | state_ = SENDING_OLD_LOGS; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | //------------------------------------------------------------------------------ |
| 787 | // Transmission of logs methods |
| 788 | |
[email protected] | 7f7f196 | 2011-04-20 15:58:16 | [diff] [blame] | 789 | void MetricsService::StartSchedulerIfNecessary() { |
[email protected] | 410938e0 | 2012-10-24 16:33:59 | [diff] [blame] | 790 | // Never schedule cutting or uploading of logs in test mode. |
| 791 | if (test_mode_active_) |
| 792 | return; |
| 793 | |
| 794 | // Even if reporting is disabled, the scheduler is needed to trigger the |
| 795 | // creation of the initial log, which must be done in order for any logs to be |
| 796 | // persisted on shutdown or backgrounding. |
[email protected] | 80a8f31 | 2013-12-16 18:00:30 | [diff] [blame] | 797 | if (recording_active() && |
| 798 | (reporting_active() || state_ < SENDING_INITIAL_STABILITY_LOG)) { |
[email protected] | 7f7f196 | 2011-04-20 15:58:16 | [diff] [blame] | 799 | scheduler_->Start(); |
[email protected] | 80a8f31 | 2013-12-16 18:00:30 | [diff] [blame] | 800 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 801 | } |
| 802 | |
[email protected] | 7f7f196 | 2011-04-20 15:58:16 | [diff] [blame] | 803 | void MetricsService::StartScheduledUpload() { |
[email protected] | cd1ac71 | 2012-06-26 08:26:47 | [diff] [blame] | 804 | // If we're getting no notifications, then the log won't have much in it, and |
| 805 | // it's possible the computer is about to go to sleep, so don't upload and |
| 806 | // stop the scheduler. |
[email protected] | 410938e0 | 2012-10-24 16:33:59 | [diff] [blame] | 807 | // If recording has been turned off, the scheduler doesn't need to run. |
| 808 | // If reporting is off, proceed if the initial log hasn't been created, since |
| 809 | // that has to happen in order for logs to be cut and stored when persisting. |
[email protected] | d7ea39e | 2014-05-22 03:59:18 | [diff] [blame] | 810 | // TODO(stuartmorgan): Call Stop() on the scheduler when reporting and/or |
[email protected] | cd1ac71 | 2012-06-26 08:26:47 | [diff] [blame] | 811 | // recording are turned off instead of letting it fire and then aborting. |
| 812 | if (idle_since_last_transmission_ || |
[email protected] | 410938e0 | 2012-10-24 16:33:59 | [diff] [blame] | 813 | !recording_active() || |
[email protected] | 80a8f31 | 2013-12-16 18:00:30 | [diff] [blame] | 814 | (!reporting_active() && state_ >= SENDING_INITIAL_STABILITY_LOG)) { |
[email protected] | 7f7f196 | 2011-04-20 15:58:16 | [diff] [blame] | 815 | scheduler_->Stop(); |
| 816 | scheduler_->UploadCancelled(); |
| 817 | return; |
| 818 | } |
| 819 | |
[email protected] | c15faf37 | 2012-07-11 06:01:34 | [diff] [blame] | 820 | // If the callback was to upload an old log, but there no longer is one, |
| 821 | // just report success back to the scheduler to begin the ongoing log |
| 822 | // callbacks. |
| 823 | // TODO(stuartmorgan): Consider removing the distinction between |
| 824 | // SENDING_OLD_LOGS and SENDING_CURRENT_LOGS to simplify the state machine |
| 825 | // now that the log upload flow is the same for both modes. |
| 826 | if (state_ == SENDING_OLD_LOGS && !log_manager_.has_unsent_logs()) { |
| 827 | state_ = SENDING_CURRENT_LOGS; |
| 828 | scheduler_->UploadFinished(true /* healthy */, false /* no unsent logs */); |
| 829 | return; |
| 830 | } |
[email protected] | cd1ac71 | 2012-06-26 08:26:47 | [diff] [blame] | 831 | // If there are unsent logs, send the next one. If not, start the asynchronous |
| 832 | // process of finalizing the current log for upload. |
| 833 | if (state_ == SENDING_OLD_LOGS) { |
| 834 | DCHECK(log_manager_.has_unsent_logs()); |
| 835 | log_manager_.StageNextLogForUpload(); |
| 836 | SendStagedLog(); |
| 837 | } else { |
[email protected] | 4b4892b | 2014-05-22 15:06:15 | [diff] [blame] | 838 | client_->CollectFinalMetrics( |
| 839 | base::Bind(&MetricsService::OnFinalLogInfoCollectionDone, |
| 840 | self_ptr_factory_.GetWeakPtr())); |
[email protected] | cd1ac71 | 2012-06-26 08:26:47 | [diff] [blame] | 841 | } |
[email protected] | 2994826 | 2012-03-01 12:15:08 | [diff] [blame] | 842 | } |
| 843 | |
[email protected] | 2994826 | 2012-03-01 12:15:08 | [diff] [blame] | 844 | void MetricsService::OnFinalLogInfoCollectionDone() { |
[email protected] | 0d5a61a8 | 2014-05-31 22:28:34 | [diff] [blame] | 845 | // If somehow there is a log upload in progress, we return and hope things |
| 846 | // work out. The scheduler isn't informed since if this happens, the scheduler |
[email protected] | 7f7f196 | 2011-04-20 15:58:16 | [diff] [blame] | 847 | // will get a response from the upload. |
[email protected] | 0d5a61a8 | 2014-05-31 22:28:34 | [diff] [blame] | 848 | DCHECK(!log_upload_in_progress_); |
| 849 | if (log_upload_in_progress_) |
[email protected] | 7f7f196 | 2011-04-20 15:58:16 | [diff] [blame] | 850 | return; |
| 851 | |
[email protected] | cd1ac71 | 2012-06-26 08:26:47 | [diff] [blame] | 852 | // Abort if metrics were turned off during the final info gathering. |
[email protected] | 410938e0 | 2012-10-24 16:33:59 | [diff] [blame] | 853 | if (!recording_active()) { |
[email protected] | 7f7f196 | 2011-04-20 15:58:16 | [diff] [blame] | 854 | scheduler_->Stop(); |
| 855 | scheduler_->UploadCancelled(); |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 856 | return; |
| 857 | } |
| 858 | |
[email protected] | cd1ac71 | 2012-06-26 08:26:47 | [diff] [blame] | 859 | StageNewLog(); |
[email protected] | 410938e0 | 2012-10-24 16:33:59 | [diff] [blame] | 860 | |
| 861 | // If logs shouldn't be uploaded, stop here. It's important that this check |
| 862 | // be after StageNewLog(), otherwise the previous logs will never be loaded, |
| 863 | // and thus the open log won't be persisted. |
| 864 | // TODO(stuartmorgan): This is unnecessarily complicated; restructure loading |
| 865 | // of previous logs to not require running part of the upload logic. |
| 866 | // http://crbug.com/157337 |
| 867 | if (!reporting_active()) { |
| 868 | scheduler_->Stop(); |
| 869 | scheduler_->UploadCancelled(); |
| 870 | return; |
| 871 | } |
| 872 | |
[email protected] | 2994826 | 2012-03-01 12:15:08 | [diff] [blame] | 873 | SendStagedLog(); |
| 874 | } |
| 875 | |
[email protected] | cd1ac71 | 2012-06-26 08:26:47 | [diff] [blame] | 876 | void MetricsService::StageNewLog() { |
[email protected] | 2994826 | 2012-03-01 12:15:08 | [diff] [blame] | 877 | if (log_manager_.has_staged_log()) |
| 878 | return; |
| 879 | |
| 880 | switch (state_) { |
| 881 | case INITIALIZED: |
| 882 | case INIT_TASK_SCHEDULED: // We should be further along by now. |
[email protected] | dc61fe9 | 2012-06-12 00:13:50 | [diff] [blame] | 883 | NOTREACHED(); |
[email protected] | 2994826 | 2012-03-01 12:15:08 | [diff] [blame] | 884 | return; |
| 885 | |
| 886 | case INIT_TASK_DONE: |
[email protected] | 80a8f31 | 2013-12-16 18:00:30 | [diff] [blame] | 887 | if (has_initial_stability_log_) { |
| 888 | // There's an initial stability log, ready to send. |
| 889 | log_manager_.StageNextLogForUpload(); |
| 890 | has_initial_stability_log_ = false; |
[email protected] | f61eb84 | 2014-01-22 10:59:13 | [diff] [blame] | 891 | // Note: No need to call LoadPersistedUnsentLogs() here because unsent |
| 892 | // logs have already been loaded by PrepareInitialStabilityLog(). |
[email protected] | 80a8f31 | 2013-12-16 18:00:30 | [diff] [blame] | 893 | state_ = SENDING_INITIAL_STABILITY_LOG; |
| 894 | } else { |
[email protected] | b58b8b2 | 2014-04-08 22:40:33 | [diff] [blame] | 895 | PrepareInitialMetricsLog(); |
[email protected] | f61eb84 | 2014-01-22 10:59:13 | [diff] [blame] | 896 | // Load unsent logs (if any) from local state. |
| 897 | log_manager_.LoadPersistedUnsentLogs(); |
[email protected] | 80a8f31 | 2013-12-16 18:00:30 | [diff] [blame] | 898 | state_ = SENDING_INITIAL_METRICS_LOG; |
| 899 | } |
[email protected] | 2994826 | 2012-03-01 12:15:08 | [diff] [blame] | 900 | break; |
| 901 | |
| 902 | case SENDING_OLD_LOGS: |
[email protected] | cd1ac71 | 2012-06-26 08:26:47 | [diff] [blame] | 903 | NOTREACHED(); // Shouldn't be staging a new log during old log sending. |
| 904 | return; |
[email protected] | 2994826 | 2012-03-01 12:15:08 | [diff] [blame] | 905 | |
| 906 | case SENDING_CURRENT_LOGS: |
[email protected] | 410938e0 | 2012-10-24 16:33:59 | [diff] [blame] | 907 | CloseCurrentLog(); |
| 908 | OpenNewLog(); |
[email protected] | 2994826 | 2012-03-01 12:15:08 | [diff] [blame] | 909 | log_manager_.StageNextLogForUpload(); |
| 910 | break; |
| 911 | |
| 912 | default: |
| 913 | NOTREACHED(); |
| 914 | return; |
| 915 | } |
| 916 | |
| 917 | DCHECK(log_manager_.has_staged_log()); |
| 918 | } |
| 919 | |
[email protected] | 80a8f31 | 2013-12-16 18:00:30 | [diff] [blame] | 920 | void MetricsService::PrepareInitialStabilityLog() { |
| 921 | DCHECK_EQ(INITIALIZED, state_); |
[email protected] | 91b1d91 | 2014-06-05 10:52:08 | [diff] [blame] | 922 | DCHECK_NE(0, local_state_->GetInteger(metrics::prefs::kStabilityCrashCount)); |
[email protected] | 2994826 | 2012-03-01 12:15:08 | [diff] [blame] | 923 | |
[email protected] | 80a8f31 | 2013-12-16 18:00:30 | [diff] [blame] | 924 | scoped_ptr<MetricsLog> initial_stability_log( |
[email protected] | 09dee82d | 2014-05-22 14:00:53 | [diff] [blame] | 925 | CreateLog(MetricsLog::INITIAL_STABILITY_LOG)); |
[email protected] | 2a321de3 | 2014-05-10 19:59:06 | [diff] [blame] | 926 | |
| 927 | // Do not call NotifyOnDidCreateMetricsLog here because the stability |
| 928 | // log describes stats from the _previous_ session. |
| 929 | |
[email protected] | 80a8f31 | 2013-12-16 18:00:30 | [diff] [blame] | 930 | if (!initial_stability_log->LoadSavedEnvironmentFromPrefs()) |
| 931 | return; |
[email protected] | 85791b0b | 2014-05-20 15:18:58 | [diff] [blame] | 932 | |
[email protected] | 80a8f31 | 2013-12-16 18:00:30 | [diff] [blame] | 933 | log_manager_.LoadPersistedUnsentLogs(); |
| 934 | |
| 935 | log_manager_.PauseCurrentLog(); |
[email protected] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 936 | log_manager_.BeginLoggingWithLog(initial_stability_log.Pass()); |
[email protected] | 85791b0b | 2014-05-20 15:18:58 | [diff] [blame] | 937 | |
| 938 | // Note: Some stability providers may record stability stats via histograms, |
| 939 | // so this call has to be after BeginLoggingWithLog(). |
[email protected] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 940 | log_manager_.current_log()->RecordStabilityMetrics( |
| 941 | metrics_providers_.get(), base::TimeDelta(), base::TimeDelta()); |
[email protected] | c778687a | 2014-02-11 14:46:45 | [diff] [blame] | 942 | RecordCurrentStabilityHistograms(); |
[email protected] | 85791b0b | 2014-05-20 15:18:58 | [diff] [blame] | 943 | |
| 944 | // Note: RecordGeneralMetrics() intentionally not called since this log is for |
| 945 | // stability stats from a previous session only. |
| 946 | |
[email protected] | 80a8f31 | 2013-12-16 18:00:30 | [diff] [blame] | 947 | log_manager_.FinishCurrentLog(); |
| 948 | log_manager_.ResumePausedLog(); |
| 949 | |
| 950 | // Store unsent logs, including the stability log that was just saved, so |
| 951 | // that they're not lost in case of a crash before upload time. |
| 952 | log_manager_.PersistUnsentLogs(); |
| 953 | |
| 954 | has_initial_stability_log_ = true; |
| 955 | } |
| 956 | |
[email protected] | b58b8b2 | 2014-04-08 22:40:33 | [diff] [blame] | 957 | void MetricsService::PrepareInitialMetricsLog() { |
[email protected] | 80a8f31 | 2013-12-16 18:00:30 | [diff] [blame] | 958 | DCHECK(state_ == INIT_TASK_DONE || state_ == SENDING_INITIAL_STABILITY_LOG); |
[email protected] | 0edf876 | 2013-11-21 18:33:30 | [diff] [blame] | 959 | |
[email protected] | b3610d4 | 2014-05-19 18:07:23 | [diff] [blame] | 960 | std::vector<variations::ActiveGroupId> synthetic_trials; |
[email protected] | 6067756 | 2013-11-17 15:52:55 | [diff] [blame] | 961 | GetCurrentSyntheticFieldTrials(&synthetic_trials); |
[email protected] | 48ff2c7f | 2014-05-23 09:57:45 | [diff] [blame] | 962 | initial_metrics_log_->RecordEnvironment(metrics_providers_.get(), |
[email protected] | 6580145 | 2014-07-09 05:42:41 | [diff] [blame] | 963 | synthetic_trials, |
| 964 | GetInstallDate()); |
[email protected] | 076961c | 2014-03-12 22:23:56 | [diff] [blame] | 965 | base::TimeDelta incremental_uptime; |
| 966 | base::TimeDelta uptime; |
[email protected] | 24f81ca | 2014-05-26 15:59:34 | [diff] [blame] | 967 | GetUptimes(local_state_, &incremental_uptime, &uptime); |
[email protected] | 2994826 | 2012-03-01 12:15:08 | [diff] [blame] | 968 | |
| 969 | // Histograms only get written to the current log, so make the new log current |
| 970 | // before writing them. |
| 971 | log_manager_.PauseCurrentLog(); |
[email protected] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 972 | log_manager_.BeginLoggingWithLog(initial_metrics_log_.Pass()); |
[email protected] | 85791b0b | 2014-05-20 15:18:58 | [diff] [blame] | 973 | |
| 974 | // Note: Some stability providers may record stability stats via histograms, |
| 975 | // so this call has to be after BeginLoggingWithLog(). |
| 976 | MetricsLog* current_log = |
| 977 | static_cast<MetricsLog*>(log_manager_.current_log()); |
| 978 | current_log->RecordStabilityMetrics(metrics_providers_.get(), |
| 979 | base::TimeDelta(), base::TimeDelta()); |
[email protected] | 2994826 | 2012-03-01 12:15:08 | [diff] [blame] | 980 | RecordCurrentHistograms(); |
[email protected] | 85791b0b | 2014-05-20 15:18:58 | [diff] [blame] | 981 | |
| 982 | current_log->RecordGeneralMetrics(metrics_providers_.get()); |
| 983 | |
[email protected] | 2994826 | 2012-03-01 12:15:08 | [diff] [blame] | 984 | log_manager_.FinishCurrentLog(); |
| 985 | log_manager_.ResumePausedLog(); |
| 986 | |
| 987 | DCHECK(!log_manager_.has_staged_log()); |
| 988 | log_manager_.StageNextLogForUpload(); |
| 989 | } |
| 990 | |
[email protected] | 2994826 | 2012-03-01 12:15:08 | [diff] [blame] | 991 | void MetricsService::SendStagedLog() { |
| 992 | DCHECK(log_manager_.has_staged_log()); |
[email protected] | 0d5a61a8 | 2014-05-31 22:28:34 | [diff] [blame] | 993 | if (!log_manager_.has_staged_log()) |
| 994 | return; |
[email protected] | 2994826 | 2012-03-01 12:15:08 | [diff] [blame] | 995 | |
[email protected] | 0d5a61a8 | 2014-05-31 22:28:34 | [diff] [blame] | 996 | DCHECK(!log_upload_in_progress_); |
| 997 | log_upload_in_progress_ = true; |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 998 | |
[email protected] | 0d5a61a8 | 2014-05-31 22:28:34 | [diff] [blame] | 999 | if (!log_uploader_) { |
| 1000 | log_uploader_ = client_->CreateUploader( |
| 1001 | kServerUrl, kMimeType, |
| 1002 | base::Bind(&MetricsService::OnLogUploadComplete, |
| 1003 | self_ptr_factory_.GetWeakPtr())); |
| 1004 | } |
| 1005 | |
| 1006 | const std::string hash = |
| 1007 | base::HexEncode(log_manager_.staged_log_hash().data(), |
| 1008 | log_manager_.staged_log_hash().size()); |
| 1009 | bool success = log_uploader_->UploadLog(log_manager_.staged_log(), hash); |
| 1010 | UMA_HISTOGRAM_BOOLEAN("UMA.UploadCreation", success); |
| 1011 | if (!success) { |
[email protected] | dc61fe9 | 2012-06-12 00:13:50 | [diff] [blame] | 1012 | // Skip this upload and hope things work out next time. |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 1013 | log_manager_.DiscardStagedLog(); |
[email protected] | 7f7f196 | 2011-04-20 15:58:16 | [diff] [blame] | 1014 | scheduler_->UploadCancelled(); |
[email protected] | 0d5a61a8 | 2014-05-31 22:28:34 | [diff] [blame] | 1015 | log_upload_in_progress_ = false; |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1016 | return; |
| 1017 | } |
| 1018 | |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1019 | HandleIdleSinceLastTransmission(true); |
| 1020 | } |
| 1021 | |
[email protected] | cac7884 | 2008-11-27 01:02:20 | [diff] [blame] | 1022 | |
[email protected] | 0d5a61a8 | 2014-05-31 22:28:34 | [diff] [blame] | 1023 | void MetricsService::OnLogUploadComplete(int response_code) { |
| 1024 | DCHECK(log_upload_in_progress_); |
| 1025 | log_upload_in_progress_ = false; |
[email protected] | fe58acc2 | 2012-02-29 01:29:58 | [diff] [blame] | 1026 | |
[email protected] | dc61fe9 | 2012-06-12 00:13:50 | [diff] [blame] | 1027 | // Log a histogram to track response success vs. failure rates. |
[email protected] | e3eb0c4 | 2013-04-18 06:18:58 | [diff] [blame] | 1028 | UMA_HISTOGRAM_ENUMERATION("UMA.UploadResponseStatus.Protobuf", |
| 1029 | ResponseCodeToStatus(response_code), |
| 1030 | NUM_RESPONSE_STATUSES); |
[email protected] | fe58acc2 | 2012-02-29 01:29:58 | [diff] [blame] | 1031 | |
[email protected] | e7508d8 | 2012-05-03 15:59:53 | [diff] [blame] | 1032 | // If the upload was provisionally stored, drop it now that the upload is |
| 1033 | // known to have gone through. |
| 1034 | log_manager_.DiscardLastProvisionalStore(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1035 | |
[email protected] | dc61fe9 | 2012-06-12 00:13:50 | [diff] [blame] | 1036 | bool upload_succeeded = response_code == 200; |
[email protected] | 7f7f196 | 2011-04-20 15:58:16 | [diff] [blame] | 1037 | |
[email protected] | 0eb34fee | 2009-01-21 08:04:38 | [diff] [blame] | 1038 | // Provide boolean for error recovery (allow us to ignore response_code). |
[email protected] | dc6f496 | 2009-02-13 01:25:50 | [diff] [blame] | 1039 | bool discard_log = false; |
[email protected] | 7f07db6 | 2014-05-15 01:12:45 | [diff] [blame] | 1040 | const size_t log_size = log_manager_.staged_log().length(); |
[email protected] | dc61fe9 | 2012-06-12 00:13:50 | [diff] [blame] | 1041 | if (!upload_succeeded && log_size > kUploadLogAvoidRetransmitSize) { |
| 1042 | UMA_HISTOGRAM_COUNTS("UMA.Large Rejected Log was Discarded", |
| 1043 | static_cast<int>(log_size)); |
[email protected] | 0eb34fee | 2009-01-21 08:04:38 | [diff] [blame] | 1044 | discard_log = true; |
[email protected] | dc61fe9 | 2012-06-12 00:13:50 | [diff] [blame] | 1045 | } else if (response_code == 400) { |
[email protected] | 0eb34fee | 2009-01-21 08:04:38 | [diff] [blame] | 1046 | // Bad syntax. Retransmission won't work. |
[email protected] | 0eb34fee | 2009-01-21 08:04:38 | [diff] [blame] | 1047 | discard_log = true; |
[email protected] | 68475e60 | 2008-08-22 03:21:15 | [diff] [blame] | 1048 | } |
| 1049 | |
[email protected] | e3eb0c4 | 2013-04-18 06:18:58 | [diff] [blame] | 1050 | if (upload_succeeded || discard_log) |
[email protected] | 5f3e164 | 2013-05-05 03:37:34 | [diff] [blame] | 1051 | log_manager_.DiscardStagedLog(); |
[email protected] | dc61fe9 | 2012-06-12 00:13:50 | [diff] [blame] | 1052 | |
[email protected] | dc61fe9 | 2012-06-12 00:13:50 | [diff] [blame] | 1053 | if (!log_manager_.has_staged_log()) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1054 | switch (state_) { |
[email protected] | 80a8f31 | 2013-12-16 18:00:30 | [diff] [blame] | 1055 | case SENDING_INITIAL_STABILITY_LOG: |
| 1056 | // Store the updated list to disk now that the removed log is uploaded. |
| 1057 | log_manager_.PersistUnsentLogs(); |
[email protected] | b58b8b2 | 2014-04-08 22:40:33 | [diff] [blame] | 1058 | PrepareInitialMetricsLog(); |
[email protected] | 80a8f31 | 2013-12-16 18:00:30 | [diff] [blame] | 1059 | SendStagedLog(); |
| 1060 | state_ = SENDING_INITIAL_METRICS_LOG; |
| 1061 | break; |
| 1062 | |
| 1063 | case SENDING_INITIAL_METRICS_LOG: |
| 1064 | // The initial metrics log never gets persisted to local state, so it's |
| 1065 | // not necessary to call log_manager_.PersistUnsentLogs() here. |
| 1066 | // TODO(asvitkine): It should be persisted like the initial stability |
| 1067 | // log and old unsent logs. http://crbug.com/328417 |
[email protected] | cd1ac71 | 2012-06-26 08:26:47 | [diff] [blame] | 1068 | state_ = log_manager_.has_unsent_logs() ? SENDING_OLD_LOGS |
| 1069 | : SENDING_CURRENT_LOGS; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1070 | break; |
| 1071 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1072 | case SENDING_OLD_LOGS: |
[email protected] | d53e223 | 2011-06-30 15:54:57 | [diff] [blame] | 1073 | // Store the updated list to disk now that the removed log is uploaded. |
[email protected] | 80a8f31 | 2013-12-16 18:00:30 | [diff] [blame] | 1074 | log_manager_.PersistUnsentLogs(); |
[email protected] | cd1ac71 | 2012-06-26 08:26:47 | [diff] [blame] | 1075 | if (!log_manager_.has_unsent_logs()) |
| 1076 | state_ = SENDING_CURRENT_LOGS; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1077 | break; |
| 1078 | |
| 1079 | case SENDING_CURRENT_LOGS: |
| 1080 | break; |
| 1081 | |
| 1082 | default: |
[email protected] | a063c10 | 2010-07-22 22:20:19 | [diff] [blame] | 1083 | NOTREACHED(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1084 | break; |
| 1085 | } |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1086 | |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 1087 | if (log_manager_.has_unsent_logs()) |
[email protected] | ed0fd00 | 2012-04-25 23:10:34 | [diff] [blame] | 1088 | DCHECK_LT(state_, SENDING_CURRENT_LOGS); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1089 | } |
[email protected] | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 1090 | |
[email protected] | 7f7f196 | 2011-04-20 15:58:16 | [diff] [blame] | 1091 | // Error 400 indicates a problem with the log, not with the server, so |
| 1092 | // don't consider that a sign that the server is in trouble. |
[email protected] | dc61fe9 | 2012-06-12 00:13:50 | [diff] [blame] | 1093 | bool server_is_healthy = upload_succeeded || response_code == 400; |
[email protected] | 80a8f31 | 2013-12-16 18:00:30 | [diff] [blame] | 1094 | // Don't notify the scheduler that the upload is finished if we've only sent |
| 1095 | // the initial stability log, but not yet the initial metrics log (treat the |
| 1096 | // two as a single unit of work as far as the scheduler is concerned). |
| 1097 | if (state_ != SENDING_INITIAL_METRICS_LOG) { |
| 1098 | scheduler_->UploadFinished(server_is_healthy, |
| 1099 | log_manager_.has_unsent_logs()); |
| 1100 | } |
[email protected] | d67d105 | 2011-06-09 05:11:41 | [diff] [blame] | 1101 | |
[email protected] | 7392942 | 2014-05-22 08:19:05 | [diff] [blame] | 1102 | if (server_is_healthy) |
| 1103 | client_->OnLogUploadComplete(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1104 | } |
| 1105 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 1106 | void MetricsService::IncrementPrefValue(const char* path) { |
[email protected] | 24f81ca | 2014-05-26 15:59:34 | [diff] [blame] | 1107 | int value = local_state_->GetInteger(path); |
| 1108 | local_state_->SetInteger(path, value + 1); |
[email protected] | e73c0197 | 2008-08-13 00:18:24 | [diff] [blame] | 1109 | } |
| 1110 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 1111 | void MetricsService::IncrementLongPrefsValue(const char* path) { |
[email protected] | 24f81ca | 2014-05-26 15:59:34 | [diff] [blame] | 1112 | int64 value = local_state_->GetInt64(path); |
| 1113 | local_state_->SetInt64(path, value + 1); |
[email protected] | 0bb1a62 | 2009-03-04 03:22:32 | [diff] [blame] | 1114 | } |
| 1115 | |
[email protected] | c0c55e9 | 2011-09-10 18:47:30 | [diff] [blame] | 1116 | bool MetricsService::UmaMetricsProperlyShutdown() { |
| 1117 | CHECK(clean_shutdown_status_ == CLEANLY_SHUTDOWN || |
| 1118 | clean_shutdown_status_ == NEED_TO_SHUTDOWN); |
| 1119 | return clean_shutdown_status_ == CLEANLY_SHUTDOWN; |
| 1120 | } |
| 1121 | |
[email protected] | 6067756 | 2013-11-17 15:52:55 | [diff] [blame] | 1122 | void MetricsService::RegisterSyntheticFieldTrial( |
| 1123 | const SyntheticTrialGroup& trial) { |
| 1124 | for (size_t i = 0; i < synthetic_trial_groups_.size(); ++i) { |
| 1125 | if (synthetic_trial_groups_[i].id.name == trial.id.name) { |
| 1126 | if (synthetic_trial_groups_[i].id.group != trial.id.group) { |
| 1127 | synthetic_trial_groups_[i].id.group = trial.id.group; |
[email protected] | 7a5c0781 | 2014-02-26 11:45:41 | [diff] [blame] | 1128 | synthetic_trial_groups_[i].start_time = base::TimeTicks::Now(); |
[email protected] | 6067756 | 2013-11-17 15:52:55 | [diff] [blame] | 1129 | } |
| 1130 | return; |
| 1131 | } |
| 1132 | } |
| 1133 | |
[email protected] | 7a5c0781 | 2014-02-26 11:45:41 | [diff] [blame] | 1134 | SyntheticTrialGroup trial_group = trial; |
| 1135 | trial_group.start_time = base::TimeTicks::Now(); |
[email protected] | 6067756 | 2013-11-17 15:52:55 | [diff] [blame] | 1136 | synthetic_trial_groups_.push_back(trial_group); |
| 1137 | } |
| 1138 | |
[email protected] | 85791b0b | 2014-05-20 15:18:58 | [diff] [blame] | 1139 | void MetricsService::RegisterMetricsProvider( |
| 1140 | scoped_ptr<metrics::MetricsProvider> provider) { |
| 1141 | DCHECK_EQ(INITIALIZED, state_); |
| 1142 | metrics_providers_.push_back(provider.release()); |
| 1143 | } |
| 1144 | |
[email protected] | 61b0d48 | 2014-05-20 14:49:10 | [diff] [blame] | 1145 | void MetricsService::CheckForClonedInstall( |
| 1146 | scoped_refptr<base::SingleThreadTaskRunner> task_runner) { |
| 1147 | state_manager_->CheckForClonedInstall(task_runner); |
[email protected] | 99c892d | 2014-03-24 18:11:21 | [diff] [blame] | 1148 | } |
| 1149 | |
[email protected] | 6067756 | 2013-11-17 15:52:55 | [diff] [blame] | 1150 | void MetricsService::GetCurrentSyntheticFieldTrials( |
[email protected] | b3610d4 | 2014-05-19 18:07:23 | [diff] [blame] | 1151 | std::vector<variations::ActiveGroupId>* synthetic_trials) { |
[email protected] | 6067756 | 2013-11-17 15:52:55 | [diff] [blame] | 1152 | DCHECK(synthetic_trials); |
| 1153 | synthetic_trials->clear(); |
| 1154 | const MetricsLog* current_log = |
| 1155 | static_cast<const MetricsLog*>(log_manager_.current_log()); |
| 1156 | for (size_t i = 0; i < synthetic_trial_groups_.size(); ++i) { |
| 1157 | if (synthetic_trial_groups_[i].start_time <= current_log->creation_time()) |
| 1158 | synthetic_trials->push_back(synthetic_trial_groups_[i].id); |
| 1159 | } |
| 1160 | } |
| 1161 | |
[email protected] | 09dee82d | 2014-05-22 14:00:53 | [diff] [blame] | 1162 | scoped_ptr<MetricsLog> MetricsService::CreateLog(MetricsLog::LogType log_type) { |
[email protected] | 24f81ca | 2014-05-26 15:59:34 | [diff] [blame] | 1163 | return make_scoped_ptr(new MetricsLog(state_manager_->client_id(), |
| 1164 | session_id_, |
| 1165 | log_type, |
| 1166 | client_, |
| 1167 | local_state_)); |
[email protected] | 09dee82d | 2014-05-22 14:00:53 | [diff] [blame] | 1168 | } |
| 1169 | |
[email protected] | acc2ce551 | 2014-05-22 18:29:13 | [diff] [blame] | 1170 | void MetricsService::RecordCurrentHistograms() { |
| 1171 | DCHECK(log_manager_.current_log()); |
| 1172 | histogram_snapshot_manager_.PrepareDeltas( |
| 1173 | base::Histogram::kNoFlags, base::Histogram::kUmaTargetedHistogramFlag); |
| 1174 | } |
| 1175 | |
| 1176 | void MetricsService::RecordCurrentStabilityHistograms() { |
| 1177 | DCHECK(log_manager_.current_log()); |
| 1178 | histogram_snapshot_manager_.PrepareDeltas( |
| 1179 | base::Histogram::kNoFlags, base::Histogram::kUmaStabilityHistogramFlag); |
| 1180 | } |
| 1181 | |
[email protected] | 466f3c1 | 2011-03-23 21:20:38 | [diff] [blame] | 1182 | void MetricsService::LogCleanShutdown() { |
[email protected] | acd55b3 | 2011-09-05 17:35:31 | [diff] [blame] | 1183 | // Redundant hack to write pref ASAP. |
[email protected] | 24f81ca | 2014-05-26 15:59:34 | [diff] [blame] | 1184 | MarkAppCleanShutdownAndCommit(local_state_); |
[email protected] | 84c384e | 2013-03-01 23:20:19 | [diff] [blame] | 1185 | |
[email protected] | c0c55e9 | 2011-09-10 18:47:30 | [diff] [blame] | 1186 | // Redundant setting to assure that we always reset this value at shutdown |
| 1187 | // (and that we don't use some alternate path, and not call LogCleanShutdown). |
| 1188 | clean_shutdown_status_ = CLEANLY_SHUTDOWN; |
[email protected] | acd55b3 | 2011-09-05 17:35:31 | [diff] [blame] | 1189 | |
[email protected] | d6147bd | 2014-06-11 01:58:19 | [diff] [blame] | 1190 | RecordBooleanPrefValue(metrics::prefs::kStabilityExitedCleanly, true); |
| 1191 | local_state_->SetInteger(metrics::prefs::kStabilityExecutionPhase, |
[email protected] | 24f81ca | 2014-05-26 15:59:34 | [diff] [blame] | 1192 | MetricsService::SHUTDOWN_COMPLETE); |
[email protected] | 466f3c1 | 2011-03-23 21:20:38 | [diff] [blame] | 1193 | } |
| 1194 | |
[email protected] | e5ad60a | 2014-03-11 03:54:04 | [diff] [blame] | 1195 | bool MetricsService::ShouldLogEvents() { |
| 1196 | // We simply don't log events to UMA if there is a single incognito |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1197 | // session visible. The problem is that we always notify using the orginal |
| 1198 | // profile in order to simplify notification processing. |
[email protected] | 7d00032 | 2014-05-23 07:16:02 | [diff] [blame] | 1199 | return !client_->IsOffTheRecordSessionActive(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1200 | } |
| 1201 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 1202 | void MetricsService::RecordBooleanPrefValue(const char* path, bool value) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1203 | DCHECK(IsSingleThreaded()); |
[email protected] | 24f81ca | 2014-05-26 15:59:34 | [diff] [blame] | 1204 | local_state_->SetBoolean(path, value); |
| 1205 | RecordCurrentState(local_state_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1206 | } |
| 1207 | |
| 1208 | void MetricsService::RecordCurrentState(PrefService* pref) { |
[email protected] | d6147bd | 2014-06-11 01:58:19 | [diff] [blame] | 1209 | pref->SetInt64(metrics::prefs::kStabilityLastTimestampSec, |
| 1210 | Time::Now().ToTimeT()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1211 | |
[email protected] | 544246e | 2014-06-06 11:22:28 | [diff] [blame] | 1212 | for (size_t i = 0; i < metrics_providers_.size(); ++i) |
| 1213 | metrics_providers_[i]->RecordCurrentState(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1214 | } |