[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 | // |
Steven Holte | 2c294a3 | 2015-03-12 21:45:03 | [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_LOGS, // Sending logs and creating new ones when we run out. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 83 | // |
| 84 | // In more detail, we have: |
| 85 | // |
| 86 | // INITIALIZED, // Constructor was called. |
| 87 | // The MS has been constructed, but has taken no actions to compose the |
| 88 | // initial log. |
| 89 | // |
[email protected] | 80a8f31 | 2013-12-16 18:00:30 | [diff] [blame] | 90 | // INIT_TASK_SCHEDULED, // Waiting for deferred init tasks to finish. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 91 | // 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] | 92 | // (the file thread) to perform deferred (lower priority and slower) |
| 93 | // initialization steps such as getting the list of plugins. That task will |
| 94 | // (when complete) make an async callback (via a Task) to indicate the |
| 95 | // completion. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 96 | // |
[email protected] | 85ed9d4 | 2010-06-08 22:37:44 | [diff] [blame] | 97 | // INIT_TASK_DONE, // Waiting for timer to send initial log. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 98 | // The callback has arrived, and it is now possible for an initial log to be |
| 99 | // created. This callback typically arrives back less than one second after |
[email protected] | 85ed9d4 | 2010-06-08 22:37:44 | [diff] [blame] | 100 | // the deferred init task is dispatched. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 101 | // |
Steven Holte | 2c294a3 | 2015-03-12 21:45:03 | [diff] [blame] | 102 | // SENDING_LOGS, // Sending logs an creating new ones when we run out. |
| 103 | // Logs from previous sessions have been loaded, and initial logs have been |
| 104 | // created (an optional stability log and the first metrics log). We will |
| 105 | // send all of these logs, and when run out, we will start cutting new logs |
| 106 | // to send. We will also cut a new log if we expect a shutdown. |
[email protected] | 80a8f31 | 2013-12-16 18:00:30 | [diff] [blame] | 107 | // |
Steven Holte | 2c294a3 | 2015-03-12 21:45:03 | [diff] [blame] | 108 | // The progression through the above states is simple, and sequential. |
| 109 | // States proceed from INITIAL to SENDING_LOGS, and remain in the latter until |
| 110 | // shutdown. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 111 | // |
Steven Holte | 2c294a3 | 2015-03-12 21:45:03 | [diff] [blame] | 112 | // Also note that whenever we successfully send a log, we mirror the list |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 113 | // of logs into the PrefService. This ensures that IF we crash, we won't start |
| 114 | // up and retransmit our old logs again. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 115 | // |
| 116 | // Due to race conditions, it is always possible that a log file could be sent |
| 117 | // twice. For example, if a log file is sent, but not yet acknowledged by |
| 118 | // the external server, and the user shuts down, then a copy of the log may be |
| 119 | // saved for re-transmission. These duplicates could be filtered out server |
[email protected] | 281d288 | 2009-01-20 20:32:42 | [diff] [blame] | 120 | // side, but are not expected to be a significant problem. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 121 | // |
| 122 | // |
| 123 | //------------------------------------------------------------------------------ |
| 124 | |
[email protected] | d6147bd | 2014-06-11 01:58:19 | [diff] [blame] | 125 | #include "components/metrics/metrics_service.h" |
[email protected] | 40bcc30 | 2009-03-02 20:50:39 | [diff] [blame] | 126 | |
avi | 2606292 | 2015-12-26 00:14:18 | [diff] [blame] | 127 | #include <stddef.h> |
dcheng | d99c42a | 2016-04-21 21:54:13 | [diff] [blame] | 128 | |
dcheng | 5160635 | 2015-12-26 21:16:23 | [diff] [blame] | 129 | #include <algorithm> |
| 130 | #include <utility> |
avi | 2606292 | 2015-12-26 00:14:18 | [diff] [blame] | 131 | |
[email protected] | 7f7f196 | 2011-04-20 15:58:16 | [diff] [blame] | 132 | #include "base/bind.h" |
| 133 | #include "base/callback.h" |
holte | 6839585 | 2017-01-10 20:40:21 | [diff] [blame] | 134 | #include "base/feature_list.h" |
skyostil | b0daa01 | 2015-06-02 19:03:48 | [diff] [blame] | 135 | #include "base/location.h" |
dcheng | d99c42a | 2016-04-21 21:54:13 | [diff] [blame] | 136 | #include "base/memory/ptr_util.h" |
[email protected] | acc2ce551 | 2014-05-22 18:29:13 | [diff] [blame] | 137 | #include "base/metrics/histogram_base.h" |
asvitkine | 454600f | 2015-06-16 16:34:50 | [diff] [blame] | 138 | #include "base/metrics/histogram_macros.h" |
[email protected] | acc2ce551 | 2014-05-22 18:29:13 | [diff] [blame] | 139 | #include "base/metrics/histogram_samples.h" |
bcwhite | 33d95806a | 2016-03-16 02:37:45 | [diff] [blame] | 140 | #include "base/metrics/persistent_histogram_allocator.h" |
[email protected] | 1026afd | 2013-03-20 14:28:54 | [diff] [blame] | 141 | #include "base/metrics/sparse_histogram.h" |
[email protected] | 567d30e | 2012-07-13 21:48:29 | [diff] [blame] | 142 | #include "base/metrics/statistics_recorder.h" |
skyostil | b0daa01 | 2015-06-02 19:03:48 | [diff] [blame] | 143 | #include "base/single_thread_task_runner.h" |
[email protected] | 3ea1b18 | 2013-02-08 22:38:41 | [diff] [blame] | 144 | #include "base/strings/string_number_conversions.h" |
[email protected] | 112158af | 2013-06-07 23:46:18 | [diff] [blame] | 145 | #include "base/strings/utf_string_conversions.h" |
gab | 7966d31 | 2016-05-11 20:35:01 | [diff] [blame] | 146 | #include "base/threading/thread_task_runner_handle.h" |
[email protected] | 64b8652c | 2014-07-16 19:14:28 | [diff] [blame] | 147 | #include "base/time/time.h" |
[email protected] | ed0fd00 | 2012-04-25 23:10:34 | [diff] [blame] | 148 | #include "base/tracked_objects.h" |
avi | 2606292 | 2015-12-26 00:14:18 | [diff] [blame] | 149 | #include "build/build_config.h" |
gayane | 0b46091c | 2016-04-07 21:01:05 | [diff] [blame] | 150 | #include "components/metrics/data_use_tracker.h" |
[email protected] | 91b1d91 | 2014-06-05 10:52:08 | [diff] [blame] | 151 | #include "components/metrics/metrics_log.h" |
[email protected] | 064107e | 2014-05-02 00:59:06 | [diff] [blame] | 152 | #include "components/metrics/metrics_log_manager.h" |
[email protected] | 0d5a61a8 | 2014-05-31 22:28:34 | [diff] [blame] | 153 | #include "components/metrics/metrics_log_uploader.h" |
[email protected] | 7f07db6 | 2014-05-15 01:12:45 | [diff] [blame] | 154 | #include "components/metrics/metrics_pref_names.h" |
[email protected] | 14bb4669 | 2014-05-20 17:16:45 | [diff] [blame] | 155 | #include "components/metrics/metrics_reporting_scheduler.h" |
holte | 6839585 | 2017-01-10 20:40:21 | [diff] [blame] | 156 | #include "components/metrics/metrics_rotation_scheduler.h" |
[email protected] | 7392942 | 2014-05-22 08:19:05 | [diff] [blame] | 157 | #include "components/metrics/metrics_service_client.h" |
[email protected] | 16a3091 | 2014-06-04 00:20:04 | [diff] [blame] | 158 | #include "components/metrics/metrics_state_manager.h" |
holte | 6839585 | 2017-01-10 20:40:21 | [diff] [blame] | 159 | #include "components/metrics/metrics_upload_scheduler.h" |
holte | 567a16f2 | 2017-01-06 01:53:45 | [diff] [blame] | 160 | #include "components/metrics/url_constants.h" |
brettw | f00b9b4 | 2016-02-01 22:11:38 | [diff] [blame] | 161 | #include "components/prefs/pref_registry_simple.h" |
| 162 | #include "components/prefs/pref_service.h" |
[email protected] | 50ae9f1 | 2013-08-29 18:03:22 | [diff] [blame] | 163 | #include "components/variations/entropy_provider.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 164 | |
asvitkine | cbd42073 | 2014-08-26 22:15:40 | [diff] [blame] | 165 | namespace metrics { |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 166 | |
[email protected] | fe58acc2 | 2012-02-29 01:29:58 | [diff] [blame] | 167 | namespace { |
[email protected] | b2a4812d | 2012-02-28 05:31:31 | [diff] [blame] | 168 | |
holte | 6839585 | 2017-01-10 20:40:21 | [diff] [blame] | 169 | // This feature moves the upload schedule to a seperate schedule from the |
| 170 | // log rotation schedule. This may change upload timing slightly, but |
| 171 | // would allow some compartmentalization of uploader logic to allow more |
| 172 | // code reuse between different metrics services. |
| 173 | const base::Feature kUploadSchedulerFeature{"UMAUploadScheduler", |
| 174 | base::FEATURE_DISABLED_BY_DEFAULT}; |
| 175 | |
rkaplow | 02e24843 | 2017-01-20 23:39:41 | [diff] [blame] | 176 | // This drops records if the number of events (user action and omnibox) exceeds |
| 177 | // the kEventLimit. |
| 178 | const base::Feature kUMAThrottleEvents{"UMAThrottleEvents", |
| 179 | base::FEATURE_ENABLED_BY_DEFAULT}; |
| 180 | |
holte | 6839585 | 2017-01-10 20:40:21 | [diff] [blame] | 181 | bool HasUploadScheduler() { |
| 182 | return base::FeatureList::IsEnabled(kUploadSchedulerFeature); |
| 183 | } |
| 184 | |
[email protected] | 7f7f196 | 2011-04-20 15:58:16 | [diff] [blame] | 185 | // The delay, in seconds, after starting recording before doing expensive |
| 186 | // initialization work. |
[email protected] | 12180f8 | 2012-10-10 21:13:30 | [diff] [blame] | 187 | #if defined(OS_ANDROID) || defined(OS_IOS) |
| 188 | // On mobile devices, a significant portion of sessions last less than a minute. |
| 189 | // Use a shorter timer on these platforms to avoid losing data. |
| 190 | // TODO(dfalcantara): To avoid delaying startup, tighten up initialization so |
| 191 | // that it occurs after the user gets their initial page. |
| 192 | const int kInitializationDelaySeconds = 5; |
| 193 | #else |
[email protected] | fe58acc2 | 2012-02-29 01:29:58 | [diff] [blame] | 194 | const int kInitializationDelaySeconds = 30; |
[email protected] | 12180f8 | 2012-10-10 21:13:30 | [diff] [blame] | 195 | #endif |
[email protected] | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 196 | |
[email protected] | 54702c9 | 2011-04-15 15:06:43 | [diff] [blame] | 197 | // The maximum number of events in a log uploaded to the UMA server. |
[email protected] | fe58acc2 | 2012-02-29 01:29:58 | [diff] [blame] | 198 | const int kEventLimit = 2400; |
[email protected] | 68475e60 | 2008-08-22 03:21:15 | [diff] [blame] | 199 | |
| 200 | // If an upload fails, and the transmission was over this byte count, then we |
| 201 | // will discard the log, and not try to retransmit it. We also don't persist |
| 202 | // the log to the prefs for transmission during the next chrome session if this |
| 203 | // limit is exceeded. |
[email protected] | a63006e | 2014-06-20 05:22:32 | [diff] [blame] | 204 | const size_t kUploadLogAvoidRetransmitSize = 100 * 1024; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 205 | |
[email protected] | 4266def2 | 2012-05-17 01:02:40 | [diff] [blame] | 206 | enum ResponseStatus { |
| 207 | UNKNOWN_FAILURE, |
| 208 | SUCCESS, |
| 209 | BAD_REQUEST, // Invalid syntax or log too large. |
[email protected] | 9f5c1ce8 | 2012-05-23 23:11:28 | [diff] [blame] | 210 | NO_RESPONSE, |
[email protected] | 4266def2 | 2012-05-17 01:02:40 | [diff] [blame] | 211 | NUM_RESPONSE_STATUSES |
| 212 | }; |
| 213 | |
| 214 | ResponseStatus ResponseCodeToStatus(int response_code) { |
| 215 | switch (response_code) { |
[email protected] | 0d5a61a8 | 2014-05-31 22:28:34 | [diff] [blame] | 216 | case -1: |
| 217 | return NO_RESPONSE; |
[email protected] | 4266def2 | 2012-05-17 01:02:40 | [diff] [blame] | 218 | case 200: |
| 219 | return SUCCESS; |
| 220 | case 400: |
| 221 | return BAD_REQUEST; |
| 222 | default: |
| 223 | return UNKNOWN_FAILURE; |
| 224 | } |
| 225 | } |
| 226 | |
hashimoto | a5c00e28d | 2015-03-27 06:28:37 | [diff] [blame] | 227 | #if defined(OS_ANDROID) || defined(OS_IOS) |
erikwright | 65b58df | 2014-09-12 00:05:28 | [diff] [blame] | 228 | void MarkAppCleanShutdownAndCommit(CleanExitBeacon* clean_exit_beacon, |
| 229 | PrefService* local_state) { |
| 230 | clean_exit_beacon->WriteBeaconValue(true); |
holte | a27eadf | 2017-01-26 01:58:59 | [diff] [blame^] | 231 | ExecutionPhaseManager(local_state).OnAppEnterBackground(); |
[email protected] | 84c384e | 2013-03-01 23:20:19 | [diff] [blame] | 232 | // Start writing right away (write happens on a different thread). |
[email protected] | 24f81ca | 2014-05-26 15:59:34 | [diff] [blame] | 233 | local_state->CommitPendingWrite(); |
[email protected] | 84c384e | 2013-03-01 23:20:19 | [diff] [blame] | 234 | } |
hashimoto | a5c00e28d | 2015-03-27 06:28:37 | [diff] [blame] | 235 | #endif // defined(OS_ANDROID) || defined(OS_IOS) |
[email protected] | 84c384e | 2013-03-01 23:20:19 | [diff] [blame] | 236 | |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 237 | } // namespace |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 238 | |
[email protected] | c0c55e9 | 2011-09-10 18:47:30 | [diff] [blame] | 239 | // static |
| 240 | MetricsService::ShutdownCleanliness MetricsService::clean_shutdown_status_ = |
| 241 | MetricsService::CLEANLY_SHUTDOWN; |
| 242 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 243 | // static |
[email protected] | b1de2c7 | 2013-02-06 02:45:47 | [diff] [blame] | 244 | void MetricsService::RegisterPrefs(PrefRegistrySimple* registry) { |
asvitkine | a63d19e | 2014-10-24 16:19:39 | [diff] [blame] | 245 | MetricsStateManager::RegisterPrefs(registry); |
[email protected] | 91b1d91 | 2014-06-05 10:52:08 | [diff] [blame] | 246 | MetricsLog::RegisterPrefs(registry); |
gayane | 0b46091c | 2016-04-07 21:01:05 | [diff] [blame] | 247 | DataUseTracker::RegisterPrefs(registry); |
[email protected] | 3907664 | 2014-05-05 20:32:55 | [diff] [blame] | 248 | |
asvitkine | a63d19e | 2014-10-24 16:19:39 | [diff] [blame] | 249 | registry->RegisterInt64Pref(prefs::kInstallDate, 0); |
[email protected] | 6580145 | 2014-07-09 05:42:41 | [diff] [blame] | 250 | |
asvitkine | a63d19e | 2014-10-24 16:19:39 | [diff] [blame] | 251 | registry->RegisterInt64Pref(prefs::kStabilityLaunchTimeSec, 0); |
| 252 | registry->RegisterInt64Pref(prefs::kStabilityLastTimestampSec, 0); |
| 253 | registry->RegisterStringPref(prefs::kStabilityStatsVersion, std::string()); |
| 254 | registry->RegisterInt64Pref(prefs::kStabilityStatsBuildTime, 0); |
| 255 | registry->RegisterBooleanPref(prefs::kStabilityExitedCleanly, true); |
holte | a27eadf | 2017-01-26 01:58:59 | [diff] [blame^] | 256 | ExecutionPhaseManager::RegisterPrefs(registry); |
asvitkine | a63d19e | 2014-10-24 16:19:39 | [diff] [blame] | 257 | registry->RegisterBooleanPref(prefs::kStabilitySessionEndCompleted, true); |
| 258 | registry->RegisterIntegerPref(prefs::kMetricsSessionID, -1); |
[email protected] | 0f2f779 | 2013-11-28 16:09:14 | [diff] [blame] | 259 | |
asvitkine | a63d19e | 2014-10-24 16:19:39 | [diff] [blame] | 260 | registry->RegisterListPref(prefs::kMetricsInitialLogs); |
| 261 | registry->RegisterListPref(prefs::kMetricsOngoingLogs); |
[email protected] | 0bb1a62 | 2009-03-04 03:22:32 | [diff] [blame] | 262 | |
asvitkine | a63d19e | 2014-10-24 16:19:39 | [diff] [blame] | 263 | registry->RegisterInt64Pref(prefs::kUninstallLaunchCount, 0); |
| 264 | registry->RegisterInt64Pref(prefs::kUninstallMetricsUptimeSec, 0); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 265 | } |
| 266 | |
asvitkine | a63d19e | 2014-10-24 16:19:39 | [diff] [blame] | 267 | MetricsService::MetricsService(MetricsStateManager* state_manager, |
| 268 | MetricsServiceClient* client, |
[email protected] | 24f81ca | 2014-05-26 15:59:34 | [diff] [blame] | 269 | PrefService* local_state) |
| 270 | : log_manager_(local_state, kUploadLogAvoidRetransmitSize), |
[email protected] | acc2ce551 | 2014-05-22 18:29:13 | [diff] [blame] | 271 | histogram_snapshot_manager_(this), |
[email protected] | 7f07db6 | 2014-05-15 01:12:45 | [diff] [blame] | 272 | state_manager_(state_manager), |
[email protected] | 728de07 | 2014-05-21 09:20:32 | [diff] [blame] | 273 | client_(client), |
[email protected] | 24f81ca | 2014-05-26 15:59:34 | [diff] [blame] | 274 | local_state_(local_state), |
erikwright | 65b58df | 2014-09-12 00:05:28 | [diff] [blame] | 275 | clean_exit_beacon_(client->GetRegistryBackupKey(), local_state), |
wittman | 622851e | 2015-07-31 18:13:40 | [diff] [blame] | 276 | recording_state_(UNSET), |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 277 | reporting_active_(false), |
[email protected] | 410938e0 | 2012-10-24 16:33:59 | [diff] [blame] | 278 | test_mode_active_(false), |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 279 | state_(INITIALIZED), |
[email protected] | 0d5a61a8 | 2014-05-31 22:28:34 | [diff] [blame] | 280 | log_upload_in_progress_(false), |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 281 | idle_since_last_transmission_(false), |
[email protected] | 80a8f31 | 2013-12-16 18:00:30 | [diff] [blame] | 282 | session_id_(-1), |
gayane | 0b46091c | 2016-04-07 21:01:05 | [diff] [blame] | 283 | data_use_tracker_(DataUseTracker::Create(local_state_)), |
holte | 57fcaed | 2017-01-23 22:14:31 | [diff] [blame] | 284 | self_ptr_factory_(this) { |
holte | a07de206 | 2017-01-21 03:43:15 | [diff] [blame] | 285 | DCHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | 3907664 | 2014-05-05 20:32:55 | [diff] [blame] | 286 | DCHECK(state_manager_); |
[email protected] | 728de07 | 2014-05-21 09:20:32 | [diff] [blame] | 287 | DCHECK(client_); |
[email protected] | 24f81ca | 2014-05-26 15:59:34 | [diff] [blame] | 288 | DCHECK(local_state_); |
[email protected] | 64b8652c | 2014-07-16 19:14:28 | [diff] [blame] | 289 | |
| 290 | // Set the install date if this is our first run. |
avi | 2606292 | 2015-12-26 00:14:18 | [diff] [blame] | 291 | int64_t install_date = local_state_->GetInt64(prefs::kInstallDate); |
asvitkine | a63d19e | 2014-10-24 16:19:39 | [diff] [blame] | 292 | if (install_date == 0) |
| 293 | local_state_->SetInt64(prefs::kInstallDate, base::Time::Now().ToTimeT()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | MetricsService::~MetricsService() { |
[email protected] | 410938e0 | 2012-10-24 16:33:59 | [diff] [blame] | 297 | DisableRecording(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 298 | } |
| 299 | |
[email protected] | 3907664 | 2014-05-05 20:32:55 | [diff] [blame] | 300 | void MetricsService::InitializeMetricsRecordingState() { |
| 301 | InitializeMetricsState(); |
[email protected] | 80a8f31 | 2013-12-16 18:00:30 | [diff] [blame] | 302 | |
gayane | d52ca40 | 2015-02-23 21:23:06 | [diff] [blame] | 303 | base::Closure upload_callback = |
| 304 | base::Bind(&MetricsService::StartScheduledUpload, |
| 305 | self_ptr_factory_.GetWeakPtr()); |
holte | 6839585 | 2017-01-10 20:40:21 | [diff] [blame] | 306 | |
| 307 | if (HasUploadScheduler()) { |
| 308 | rotation_scheduler_.reset(new MetricsRotationScheduler( |
| 309 | upload_callback, |
| 310 | // MetricsServiceClient outlives MetricsService, and |
| 311 | // MetricsRotationScheduler is tied to the lifetime of |this|. |
| 312 | base::Bind(&MetricsServiceClient::GetStandardUploadInterval, |
| 313 | base::Unretained(client_)))); |
| 314 | base::Closure send_next_log_callback = base::Bind( |
| 315 | &MetricsService::SendNextLog, self_ptr_factory_.GetWeakPtr()); |
| 316 | upload_scheduler_.reset(new MetricsUploadScheduler(send_next_log_callback)); |
| 317 | } else { |
| 318 | scheduler_.reset(new MetricsReportingScheduler( |
| 319 | upload_callback, |
| 320 | // MetricsServiceClient outlives MetricsService, and |
| 321 | // MetricsReportingScheduler is tied to the lifetime of |this|. |
| 322 | base::Bind(&MetricsServiceClient::GetStandardUploadInterval, |
| 323 | base::Unretained(client_)))); |
| 324 | } |
dhsharp | b0073a2 | 2016-02-18 21:54:09 | [diff] [blame] | 325 | |
bcwhite | 34c6bbf | 2016-02-19 22:14:46 | [diff] [blame] | 326 | for (MetricsProvider* provider : metrics_providers_) |
| 327 | provider->Init(); |
[email protected] | 80a8f31 | 2013-12-16 18:00:30 | [diff] [blame] | 328 | } |
| 329 | |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 330 | void MetricsService::Start() { |
[email protected] | b1c8dc0 | 2011-04-13 18:32:04 | [diff] [blame] | 331 | HandleIdleSinceLastTransmission(false); |
[email protected] | 410938e0 | 2012-10-24 16:33:59 | [diff] [blame] | 332 | EnableRecording(); |
| 333 | EnableReporting(); |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 334 | } |
| 335 | |
[email protected] | 410938e0 | 2012-10-24 16:33:59 | [diff] [blame] | 336 | void MetricsService::StartRecordingForTests() { |
| 337 | test_mode_active_ = true; |
| 338 | EnableRecording(); |
| 339 | DisableReporting(); |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | void MetricsService::Stop() { |
[email protected] | b1c8dc0 | 2011-04-13 18:32:04 | [diff] [blame] | 343 | HandleIdleSinceLastTransmission(false); |
[email protected] | 410938e0 | 2012-10-24 16:33:59 | [diff] [blame] | 344 | DisableReporting(); |
| 345 | DisableRecording(); |
| 346 | } |
| 347 | |
| 348 | void MetricsService::EnableReporting() { |
| 349 | if (reporting_active_) |
| 350 | return; |
| 351 | reporting_active_ = true; |
| 352 | StartSchedulerIfNecessary(); |
| 353 | } |
| 354 | |
| 355 | void MetricsService::DisableReporting() { |
| 356 | reporting_active_ = false; |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 357 | } |
| 358 | |
[email protected] | edafd4c | 2011-05-10 17:18:53 | [diff] [blame] | 359 | std::string MetricsService::GetClientId() { |
[email protected] | 3907664 | 2014-05-05 20:32:55 | [diff] [blame] | 360 | return state_manager_->client_id(); |
[email protected] | edafd4c | 2011-05-10 17:18:53 | [diff] [blame] | 361 | } |
| 362 | |
avi | 2606292 | 2015-12-26 00:14:18 | [diff] [blame] | 363 | int64_t MetricsService::GetInstallDate() { |
asvitkine | a63d19e | 2014-10-24 16:19:39 | [diff] [blame] | 364 | return local_state_->GetInt64(prefs::kInstallDate); |
[email protected] | 6580145 | 2014-07-09 05:42:41 | [diff] [blame] | 365 | } |
| 366 | |
avi | 2606292 | 2015-12-26 00:14:18 | [diff] [blame] | 367 | int64_t MetricsService::GetMetricsReportingEnabledDate() { |
olivierrobin | c3dfc5b | 2015-04-07 19:12:00 | [diff] [blame] | 368 | return local_state_->GetInt64(prefs::kMetricsReportingEnabledTimestamp); |
| 369 | } |
| 370 | |
droger | b8d286e4 | 2015-07-08 09:07:09 | [diff] [blame] | 371 | bool MetricsService::WasLastShutdownClean() const { |
| 372 | return clean_exit_beacon_.exited_cleanly(); |
| 373 | } |
| 374 | |
[email protected] | 410938e0 | 2012-10-24 16:33:59 | [diff] [blame] | 375 | void MetricsService::EnableRecording() { |
holte | a07de206 | 2017-01-21 03:43:15 | [diff] [blame] | 376 | DCHECK(thread_checker_.CalledOnValidThread()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 377 | |
wittman | 622851e | 2015-07-31 18:13:40 | [diff] [blame] | 378 | if (recording_state_ == ACTIVE) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 379 | return; |
wittman | 622851e | 2015-07-31 18:13:40 | [diff] [blame] | 380 | recording_state_ = ACTIVE; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 381 | |
[email protected] | 3907664 | 2014-05-05 20:32:55 | [diff] [blame] | 382 | state_manager_->ForceClientIdCreation(); |
[email protected] | 9d1b015 | 2014-07-09 18:53:22 | [diff] [blame] | 383 | client_->SetMetricsClientId(state_manager_->client_id()); |
[email protected] | 410938e0 | 2012-10-24 16:33:59 | [diff] [blame] | 384 | if (!log_manager_.current_log()) |
| 385 | OpenNewLog(); |
[email protected] | 005ef3e | 2009-05-22 20:55:46 | [diff] [blame] | 386 | |
bcwhite | 34c6bbf | 2016-02-19 22:14:46 | [diff] [blame] | 387 | for (MetricsProvider* provider : metrics_providers_) |
| 388 | provider->OnRecordingEnabled(); |
[email protected] | 85791b0b | 2014-05-20 15:18:58 | [diff] [blame] | 389 | |
[email protected] | e6e30ac | 2014-01-13 21:24:39 | [diff] [blame] | 390 | base::RemoveActionCallback(action_callback_); |
[email protected] | dd98f39 | 2013-02-04 13:03:22 | [diff] [blame] | 391 | action_callback_ = base::Bind(&MetricsService::OnUserAction, |
| 392 | base::Unretained(this)); |
[email protected] | e6e30ac | 2014-01-13 21:24:39 | [diff] [blame] | 393 | base::AddActionCallback(action_callback_); |
[email protected] | 410938e0 | 2012-10-24 16:33:59 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | void MetricsService::DisableRecording() { |
holte | a07de206 | 2017-01-21 03:43:15 | [diff] [blame] | 397 | DCHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | 410938e0 | 2012-10-24 16:33:59 | [diff] [blame] | 398 | |
wittman | 622851e | 2015-07-31 18:13:40 | [diff] [blame] | 399 | if (recording_state_ == INACTIVE) |
[email protected] | 410938e0 | 2012-10-24 16:33:59 | [diff] [blame] | 400 | return; |
wittman | 622851e | 2015-07-31 18:13:40 | [diff] [blame] | 401 | recording_state_ = INACTIVE; |
[email protected] | 410938e0 | 2012-10-24 16:33:59 | [diff] [blame] | 402 | |
[email protected] | e6e30ac | 2014-01-13 21:24:39 | [diff] [blame] | 403 | base::RemoveActionCallback(action_callback_); |
[email protected] | 85791b0b | 2014-05-20 15:18:58 | [diff] [blame] | 404 | |
bcwhite | 34c6bbf | 2016-02-19 22:14:46 | [diff] [blame] | 405 | for (MetricsProvider* provider : metrics_providers_) |
| 406 | provider->OnRecordingDisabled(); |
[email protected] | 85791b0b | 2014-05-20 15:18:58 | [diff] [blame] | 407 | |
[email protected] | 410938e0 | 2012-10-24 16:33:59 | [diff] [blame] | 408 | PushPendingLogsToPersistentStorage(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 409 | } |
| 410 | |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 411 | bool MetricsService::recording_active() const { |
holte | a07de206 | 2017-01-21 03:43:15 | [diff] [blame] | 412 | DCHECK(thread_checker_.CalledOnValidThread()); |
wittman | 622851e | 2015-07-31 18:13:40 | [diff] [blame] | 413 | return recording_state_ == ACTIVE; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 414 | } |
| 415 | |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 416 | bool MetricsService::reporting_active() const { |
holte | a07de206 | 2017-01-21 03:43:15 | [diff] [blame] | 417 | DCHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 418 | return reporting_active_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 419 | } |
| 420 | |
[email protected] | acc2ce551 | 2014-05-22 18:29:13 | [diff] [blame] | 421 | void MetricsService::RecordDelta(const base::HistogramBase& histogram, |
| 422 | const base::HistogramSamples& snapshot) { |
| 423 | log_manager_.current_log()->RecordHistogramDelta(histogram.histogram_name(), |
| 424 | snapshot); |
| 425 | } |
| 426 | |
| 427 | void MetricsService::InconsistencyDetected( |
| 428 | base::HistogramBase::Inconsistency problem) { |
| 429 | UMA_HISTOGRAM_ENUMERATION("Histogram.InconsistenciesBrowser", |
| 430 | problem, base::HistogramBase::NEVER_EXCEEDED_VALUE); |
| 431 | } |
| 432 | |
| 433 | void MetricsService::UniqueInconsistencyDetected( |
| 434 | base::HistogramBase::Inconsistency problem) { |
| 435 | UMA_HISTOGRAM_ENUMERATION("Histogram.InconsistenciesBrowserUnique", |
| 436 | problem, base::HistogramBase::NEVER_EXCEEDED_VALUE); |
| 437 | } |
| 438 | |
| 439 | void MetricsService::InconsistencyDetectedInLoggedCount(int amount) { |
| 440 | UMA_HISTOGRAM_COUNTS("Histogram.InconsistentSnapshotBrowser", |
| 441 | std::abs(amount)); |
| 442 | } |
| 443 | |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 444 | void MetricsService::HandleIdleSinceLastTransmission(bool in_idle) { |
| 445 | // If there wasn't a lot of action, maybe the computer was asleep, in which |
| 446 | // case, the log transmissions should have stopped. Here we start them up |
| 447 | // again. |
[email protected] | cac7884 | 2008-11-27 01:02:20 | [diff] [blame] | 448 | if (!in_idle && idle_since_last_transmission_) |
[email protected] | 7f7f196 | 2011-04-20 15:58:16 | [diff] [blame] | 449 | StartSchedulerIfNecessary(); |
[email protected] | cac7884 | 2008-11-27 01:02:20 | [diff] [blame] | 450 | idle_since_last_transmission_ = in_idle; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 451 | } |
| 452 | |
[email protected] | d7ea39e | 2014-05-22 03:59:18 | [diff] [blame] | 453 | void MetricsService::OnApplicationNotIdle() { |
wittman | 622851e | 2015-07-31 18:13:40 | [diff] [blame] | 454 | if (recording_state_ == ACTIVE) |
[email protected] | d7ea39e | 2014-05-22 03:59:18 | [diff] [blame] | 455 | HandleIdleSinceLastTransmission(false); |
| 456 | } |
| 457 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 458 | void MetricsService::RecordStartOfSessionEnd() { |
holte | 6d34116 | 2016-12-19 21:42:42 | [diff] [blame] | 459 | LogCleanShutdown(false); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | void MetricsService::RecordCompletedSessionEnd() { |
holte | 6d34116 | 2016-12-19 21:42:42 | [diff] [blame] | 463 | LogCleanShutdown(true); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 464 | } |
| 465 | |
[email protected] | 410938e0 | 2012-10-24 16:33:59 | [diff] [blame] | 466 | #if defined(OS_ANDROID) || defined(OS_IOS) |
[email protected] | 117fbdf2 | 2012-06-26 18:36:39 | [diff] [blame] | 467 | void MetricsService::OnAppEnterBackground() { |
holte | 6839585 | 2017-01-10 20:40:21 | [diff] [blame] | 468 | if (upload_scheduler_) { |
| 469 | rotation_scheduler_->Stop(); |
| 470 | upload_scheduler_->Stop(); |
| 471 | } else { |
| 472 | scheduler_->Stop(); |
| 473 | } |
[email protected] | 117fbdf2 | 2012-06-26 18:36:39 | [diff] [blame] | 474 | |
erikwright | 65b58df | 2014-09-12 00:05:28 | [diff] [blame] | 475 | MarkAppCleanShutdownAndCommit(&clean_exit_beacon_, local_state_); |
[email protected] | 117fbdf2 | 2012-06-26 18:36:39 | [diff] [blame] | 476 | |
bmcquade | 6a49bed | 2016-07-27 19:25:56 | [diff] [blame] | 477 | // Give providers a chance to persist histograms as part of being |
| 478 | // backgrounded. |
| 479 | for (MetricsProvider* provider : metrics_providers_) |
| 480 | provider->OnAppEnterBackground(); |
| 481 | |
[email protected] | 117fbdf2 | 2012-06-26 18:36:39 | [diff] [blame] | 482 | // At this point, there's no way of knowing when the process will be |
| 483 | // killed, so this has to be treated similar to a shutdown, closing and |
| 484 | // persisting all logs. Unlinke a shutdown, the state is primed to be ready |
| 485 | // to continue logging and uploading if the process does return. |
Steven Holte | 2c294a3 | 2015-03-12 21:45:03 | [diff] [blame] | 486 | if (recording_active() && state_ >= SENDING_LOGS) { |
[email protected] | 117fbdf2 | 2012-06-26 18:36:39 | [diff] [blame] | 487 | PushPendingLogsToPersistentStorage(); |
[email protected] | 410938e0 | 2012-10-24 16:33:59 | [diff] [blame] | 488 | // Persisting logs closes the current log, so start recording a new log |
| 489 | // immediately to capture any background work that might be done before the |
| 490 | // process is killed. |
| 491 | OpenNewLog(); |
[email protected] | 117fbdf2 | 2012-06-26 18:36:39 | [diff] [blame] | 492 | } |
[email protected] | 117fbdf2 | 2012-06-26 18:36:39 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | void MetricsService::OnAppEnterForeground() { |
erikwright | 65b58df | 2014-09-12 00:05:28 | [diff] [blame] | 496 | clean_exit_beacon_.WriteBeaconValue(false); |
holte | a27eadf | 2017-01-26 01:58:59 | [diff] [blame^] | 497 | ExecutionPhaseManager(local_state_).OnAppEnterForeground(); |
[email protected] | 117fbdf2 | 2012-06-26 18:36:39 | [diff] [blame] | 498 | StartSchedulerIfNecessary(); |
| 499 | } |
[email protected] | 84c384e | 2013-03-01 23:20:19 | [diff] [blame] | 500 | #else |
erikwright | cc98a7e0 | 2014-09-09 22:05:12 | [diff] [blame] | 501 | void MetricsService::LogNeedForCleanShutdown() { |
erikwright | 65b58df | 2014-09-12 00:05:28 | [diff] [blame] | 502 | clean_exit_beacon_.WriteBeaconValue(false); |
[email protected] | 84c384e | 2013-03-01 23:20:19 | [diff] [blame] | 503 | // Redundant setting to be sure we call for a clean shutdown. |
| 504 | clean_shutdown_status_ = NEED_TO_SHUTDOWN; |
| 505 | } |
| 506 | #endif // defined(OS_ANDROID) || defined(OS_IOS) |
[email protected] | 117fbdf2 | 2012-06-26 18:36:39 | [diff] [blame] | 507 | |
[email protected] | 6d67ea0d | 2013-11-14 11:02:21 | [diff] [blame] | 508 | // static |
[email protected] | 24f81ca | 2014-05-26 15:59:34 | [diff] [blame] | 509 | void MetricsService::SetExecutionPhase(ExecutionPhase execution_phase, |
| 510 | PrefService* local_state) { |
holte | a27eadf | 2017-01-26 01:58:59 | [diff] [blame^] | 511 | ExecutionPhaseManager(local_state).SetExecutionPhase(execution_phase); |
[email protected] | 6d67ea0d | 2013-11-14 11:02:21 | [diff] [blame] | 512 | } |
| 513 | |
[email protected] | 7f7f196 | 2011-04-20 15:58:16 | [diff] [blame] | 514 | void MetricsService::RecordBreakpadRegistration(bool success) { |
[email protected] | 68475e60 | 2008-08-22 03:21:15 | [diff] [blame] | 515 | if (!success) |
asvitkine | a63d19e | 2014-10-24 16:19:39 | [diff] [blame] | 516 | IncrementPrefValue(prefs::kStabilityBreakpadRegistrationFail); |
[email protected] | e73c0197 | 2008-08-13 00:18:24 | [diff] [blame] | 517 | else |
asvitkine | a63d19e | 2014-10-24 16:19:39 | [diff] [blame] | 518 | IncrementPrefValue(prefs::kStabilityBreakpadRegistrationSuccess); |
[email protected] | e73c0197 | 2008-08-13 00:18:24 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | void MetricsService::RecordBreakpadHasDebugger(bool has_debugger) { |
| 522 | if (!has_debugger) |
asvitkine | a63d19e | 2014-10-24 16:19:39 | [diff] [blame] | 523 | IncrementPrefValue(prefs::kStabilityDebuggerNotPresent); |
[email protected] | e73c0197 | 2008-08-13 00:18:24 | [diff] [blame] | 524 | else |
asvitkine | a63d19e | 2014-10-24 16:19:39 | [diff] [blame] | 525 | IncrementPrefValue(prefs::kStabilityDebuggerPresent); |
[email protected] | e73c0197 | 2008-08-13 00:18:24 | [diff] [blame] | 526 | } |
| 527 | |
yiyaoliu | 14ef3ead | 2014-11-19 02:36:50 | [diff] [blame] | 528 | void MetricsService::ClearSavedStabilityMetrics() { |
bcwhite | 34c6bbf | 2016-02-19 22:14:46 | [diff] [blame] | 529 | for (MetricsProvider* provider : metrics_providers_) |
| 530 | provider->ClearSavedStabilityMetrics(); |
yiyaoliu | 14ef3ead | 2014-11-19 02:36:50 | [diff] [blame] | 531 | |
| 532 | // Reset the prefs that are managed by MetricsService/MetricsLog directly. |
manzagop | 5415700 | 2016-09-01 02:43:59 | [diff] [blame] | 533 | local_state_->SetInteger(prefs::kStabilityBreakpadRegistrationSuccess, 0); |
| 534 | local_state_->SetInteger(prefs::kStabilityBreakpadRegistrationFail, 0); |
yiyaoliu | 14ef3ead | 2014-11-19 02:36:50 | [diff] [blame] | 535 | local_state_->SetInteger(prefs::kStabilityCrashCount, 0); |
manzagop | 5415700 | 2016-09-01 02:43:59 | [diff] [blame] | 536 | local_state_->SetInteger(prefs::kStabilityDebuggerPresent, 0); |
| 537 | local_state_->SetInteger(prefs::kStabilityDebuggerNotPresent, 0); |
yiyaoliu | 14ef3ead | 2014-11-19 02:36:50 | [diff] [blame] | 538 | local_state_->SetInteger(prefs::kStabilityIncompleteSessionEndCount, 0); |
| 539 | local_state_->SetInteger(prefs::kStabilityLaunchCount, 0); |
| 540 | local_state_->SetBoolean(prefs::kStabilitySessionEndCompleted, true); |
manzagop | ea99d195 | 2016-09-08 23:40:05 | [diff] [blame] | 541 | local_state_->SetInteger(prefs::kStabilityDeferredCount, 0); |
| 542 | // Note: kStabilityDiscardCount is not cleared as its intent is to measure |
| 543 | // the number of times data is discarded, even across versions. |
| 544 | local_state_->SetInteger(prefs::kStabilityVersionMismatchCount, 0); |
yiyaoliu | 14ef3ead | 2014-11-19 02:36:50 | [diff] [blame] | 545 | } |
| 546 | |
olivierrobin | c3dfc5b | 2015-04-07 19:12:00 | [diff] [blame] | 547 | void MetricsService::PushExternalLog(const std::string& log) { |
| 548 | log_manager_.StoreLog(log, MetricsLog::ONGOING_LOG); |
| 549 | } |
| 550 | |
robliao | 7253fd2 | 2016-12-01 18:41:38 | [diff] [blame] | 551 | void MetricsService::UpdateMetricsUsagePrefs(const std::string& service_name, |
| 552 | int message_size, |
| 553 | bool is_cellular) { |
holte | a07de206 | 2017-01-21 03:43:15 | [diff] [blame] | 554 | DCHECK(thread_checker_.CalledOnValidThread()); |
gayane | 0b46091c | 2016-04-07 21:01:05 | [diff] [blame] | 555 | if (data_use_tracker_) { |
robliao | 7253fd2 | 2016-12-01 18:41:38 | [diff] [blame] | 556 | data_use_tracker_->UpdateMetricsUsagePrefs(service_name, |
| 557 | message_size, |
| 558 | is_cellular); |
gayane | 0b46091c | 2016-04-07 21:01:05 | [diff] [blame] | 559 | } |
gayane | 0b46091c | 2016-04-07 21:01:05 | [diff] [blame] | 560 | } |
| 561 | |
bcwhite | 81e1485 | 2016-08-01 19:30:23 | [diff] [blame] | 562 | void MetricsService::MergeHistogramDeltas() { |
| 563 | for (MetricsProvider* provider : metrics_providers_) |
| 564 | provider->MergeHistogramDeltas(); |
| 565 | } |
| 566 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 567 | //------------------------------------------------------------------------------ |
| 568 | // private methods |
| 569 | //------------------------------------------------------------------------------ |
| 570 | |
| 571 | |
| 572 | //------------------------------------------------------------------------------ |
| 573 | // Initialization methods |
| 574 | |
[email protected] | 3907664 | 2014-05-05 20:32:55 | [diff] [blame] | 575 | void MetricsService::InitializeMetricsState() { |
avi | 2606292 | 2015-12-26 00:14:18 | [diff] [blame] | 576 | const int64_t buildtime = MetricsLog::GetBuildTime(); |
asvitkine | ff3e2a6 | 2014-09-18 22:01:49 | [diff] [blame] | 577 | const std::string version = client_->GetVersionString(); |
holte | 57fcaed | 2017-01-23 22:14:31 | [diff] [blame] | 578 | |
| 579 | // Delete deprecated prefs |
| 580 | // TODO(holte): Remove these in M58 |
| 581 | local_state_->ClearPref(prefs::kStabilityLaunchTimeSec); |
| 582 | local_state_->ClearPref(prefs::kStabilityLastTimestampSec); |
| 583 | |
asvitkine | ff3e2a6 | 2014-09-18 22:01:49 | [diff] [blame] | 584 | bool version_changed = false; |
manzagop | ea99d195 | 2016-09-08 23:40:05 | [diff] [blame] | 585 | int64_t previous_buildtime = |
| 586 | local_state_->GetInt64(prefs::kStabilityStatsBuildTime); |
| 587 | std::string previous_version = |
| 588 | local_state_->GetString(prefs::kStabilityStatsVersion); |
| 589 | if (previous_buildtime != buildtime || previous_version != version) { |
asvitkine | a63d19e | 2014-10-24 16:19:39 | [diff] [blame] | 590 | local_state_->SetString(prefs::kStabilityStatsVersion, version); |
| 591 | local_state_->SetInt64(prefs::kStabilityStatsBuildTime, buildtime); |
asvitkine | ff3e2a6 | 2014-09-18 22:01:49 | [diff] [blame] | 592 | version_changed = true; |
| 593 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 594 | |
[email protected] | 94dce12 | 2014-07-16 04:20:12 | [diff] [blame] | 595 | log_manager_.LoadPersistedUnsentLogs(); |
| 596 | |
asvitkine | a63d19e | 2014-10-24 16:19:39 | [diff] [blame] | 597 | session_id_ = local_state_->GetInteger(prefs::kMetricsSessionID); |
erikwright | 65b58df | 2014-09-12 00:05:28 | [diff] [blame] | 598 | |
| 599 | if (!clean_exit_beacon_.exited_cleanly()) { |
asvitkine | a63d19e | 2014-10-24 16:19:39 | [diff] [blame] | 600 | IncrementPrefValue(prefs::kStabilityCrashCount); |
[email protected] | c0c55e9 | 2011-09-10 18:47:30 | [diff] [blame] | 601 | // Reset flag, and wait until we call LogNeedForCleanShutdown() before |
| 602 | // monitoring. |
erikwright | 65b58df | 2014-09-12 00:05:28 | [diff] [blame] | 603 | clean_exit_beacon_.WriteBeaconValue(true); |
holte | a27eadf | 2017-01-26 01:58:59 | [diff] [blame^] | 604 | ExecutionPhaseManager manager(local_state_); |
manzagop | 7c14de2 | 2016-11-25 16:19:34 | [diff] [blame] | 605 | UMA_HISTOGRAM_SPARSE_SLOWLY("Chrome.Browser.CrashedExecutionPhase", |
holte | a27eadf | 2017-01-26 01:58:59 | [diff] [blame^] | 606 | static_cast<int>(manager.GetExecutionPhase())); |
| 607 | manager.SetExecutionPhase(ExecutionPhase::UNINITIALIZED_PHASE); |
siggi | c179dd06 | 2014-09-10 17:02:31 | [diff] [blame] | 608 | } |
[email protected] | 6a6d0d1 | 2013-10-28 15:58:19 | [diff] [blame] | 609 | |
manzagop | 780bb8b | 2016-10-21 14:21:16 | [diff] [blame] | 610 | // ProvidersHaveInitialStabilityMetrics is called first to ensure it is never |
| 611 | // bypassed. |
| 612 | const bool is_initial_stability_log_required = |
| 613 | ProvidersHaveInitialStabilityMetrics() || |
| 614 | !clean_exit_beacon_.exited_cleanly(); |
Steven Holte | 2c294a3 | 2015-03-12 21:45:03 | [diff] [blame] | 615 | bool has_initial_stability_log = false; |
manzagop | 780bb8b | 2016-10-21 14:21:16 | [diff] [blame] | 616 | if (is_initial_stability_log_required) { |
siggi | c179dd06 | 2014-09-10 17:02:31 | [diff] [blame] | 617 | // If the previous session didn't exit cleanly, or if any provider |
| 618 | // explicitly requests it, prepare an initial stability log - |
| 619 | // provided UMA is enabled. |
manzagop | ea99d195 | 2016-09-08 23:40:05 | [diff] [blame] | 620 | if (state_manager_->IsMetricsReportingEnabled()) { |
| 621 | has_initial_stability_log = PrepareInitialStabilityLog(previous_version); |
| 622 | if (!has_initial_stability_log) |
| 623 | IncrementPrefValue(prefs::kStabilityDeferredCount); |
| 624 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 625 | } |
[email protected] | 80a8f31 | 2013-12-16 18:00:30 | [diff] [blame] | 626 | |
manzagop | 780bb8b | 2016-10-21 14:21:16 | [diff] [blame] | 627 | // If the version changed, but no initial stability log was generated, clear |
| 628 | // the stability stats from the previous version (so that they don't get |
asvitkine | ff3e2a6 | 2014-09-18 22:01:49 | [diff] [blame] | 629 | // attributed to the current version). This could otherwise happen due to a |
| 630 | // number of different edge cases, such as if the last version crashed before |
| 631 | // it could save off a system profile or if UMA reporting is disabled (which |
| 632 | // normally results in stats being accumulated). |
manzagop | 780bb8b | 2016-10-21 14:21:16 | [diff] [blame] | 633 | if (version_changed && !has_initial_stability_log) { |
yiyaoliu | 14ef3ead | 2014-11-19 02:36:50 | [diff] [blame] | 634 | ClearSavedStabilityMetrics(); |
manzagop | ea99d195 | 2016-09-08 23:40:05 | [diff] [blame] | 635 | IncrementPrefValue(prefs::kStabilityDiscardCount); |
| 636 | } |
asvitkine | ff3e2a6 | 2014-09-18 22:01:49 | [diff] [blame] | 637 | |
manzagop | 780bb8b | 2016-10-21 14:21:16 | [diff] [blame] | 638 | // If the version changed, the system profile is obsolete and needs to be |
| 639 | // cleared. This is to avoid the stability data misattribution that could |
| 640 | // occur if the current version crashed before saving its own system profile. |
| 641 | // Note however this clearing occurs only after preparing the initial |
| 642 | // stability log, an operation that requires the previous version's system |
| 643 | // profile. At this point, stability metrics pertaining to the previous |
| 644 | // version have been cleared. |
| 645 | if (version_changed) { |
| 646 | local_state_->ClearPref(prefs::kStabilitySavedSystemProfile); |
| 647 | local_state_->ClearPref(prefs::kStabilitySavedSystemProfileHash); |
| 648 | } |
| 649 | |
[email protected] | 80a8f31 | 2013-12-16 18:00:30 | [diff] [blame] | 650 | // Update session ID. |
| 651 | ++session_id_; |
asvitkine | a63d19e | 2014-10-24 16:19:39 | [diff] [blame] | 652 | local_state_->SetInteger(prefs::kMetricsSessionID, session_id_); |
[email protected] | 80a8f31 | 2013-12-16 18:00:30 | [diff] [blame] | 653 | |
| 654 | // Stability bookkeeping |
asvitkine | a63d19e | 2014-10-24 16:19:39 | [diff] [blame] | 655 | IncrementPrefValue(prefs::kStabilityLaunchCount); |
[email protected] | 80a8f31 | 2013-12-16 18:00:30 | [diff] [blame] | 656 | |
holte | a27eadf | 2017-01-26 01:58:59 | [diff] [blame^] | 657 | SetExecutionPhase(ExecutionPhase::START_METRICS_RECORDING, local_state_); |
[email protected] | e73c0197 | 2008-08-13 00:18:24 | [diff] [blame] | 658 | |
asvitkine | a63d19e | 2014-10-24 16:19:39 | [diff] [blame] | 659 | if (!local_state_->GetBoolean(prefs::kStabilitySessionEndCompleted)) { |
| 660 | IncrementPrefValue(prefs::kStabilityIncompleteSessionEndCount); |
[email protected] | c9abf24 | 2009-07-18 06:00:38 | [diff] [blame] | 661 | // This is marked false when we get a WM_ENDSESSION. |
asvitkine | a63d19e | 2014-10-24 16:19:39 | [diff] [blame] | 662 | local_state_->SetBoolean(prefs::kStabilitySessionEndCompleted, true); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 663 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 664 | |
[email protected] | 076961c | 2014-03-12 22:23:56 | [diff] [blame] | 665 | // Call GetUptimes() for the first time, thus allowing all later calls |
| 666 | // to record incremental uptimes accurately. |
| 667 | base::TimeDelta ignored_uptime_parameter; |
| 668 | base::TimeDelta startup_uptime; |
[email protected] | 24f81ca | 2014-05-26 15:59:34 | [diff] [blame] | 669 | GetUptimes(local_state_, &startup_uptime, &ignored_uptime_parameter); |
[email protected] | c68a2b9b | 2013-10-09 18:16:36 | [diff] [blame] | 670 | DCHECK_EQ(0, startup_uptime.InMicroseconds()); |
[email protected] | 0bb1a62 | 2009-03-04 03:22:32 | [diff] [blame] | 671 | |
| 672 | // Bookkeeping for the uninstall metrics. |
asvitkine | a63d19e | 2014-10-24 16:19:39 | [diff] [blame] | 673 | IncrementLongPrefsValue(prefs::kUninstallLaunchCount); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 674 | } |
| 675 | |
[email protected] | dd98f39 | 2013-02-04 13:03:22 | [diff] [blame] | 676 | void MetricsService::OnUserAction(const std::string& action) { |
[email protected] | 4426d2d | 2014-04-09 12:33:00 | [diff] [blame] | 677 | log_manager_.current_log()->RecordUserAction(action); |
[email protected] | dd98f39 | 2013-02-04 13:03:22 | [diff] [blame] | 678 | HandleIdleSinceLastTransmission(false); |
| 679 | } |
| 680 | |
isherman | b670568 | 2015-08-29 00:01:00 | [diff] [blame] | 681 | void MetricsService::FinishedInitTask() { |
[email protected] | ed0fd00 | 2012-04-25 23:10:34 | [diff] [blame] | 682 | DCHECK_EQ(INIT_TASK_SCHEDULED, state_); |
[email protected] | c68a2b9b | 2013-10-09 18:16:36 | [diff] [blame] | 683 | state_ = INIT_TASK_DONE; |
[email protected] | 83d09f9 | 2014-06-03 14:58:26 | [diff] [blame] | 684 | |
| 685 | // Create the initial log. |
| 686 | if (!initial_metrics_log_.get()) { |
| 687 | initial_metrics_log_ = CreateLog(MetricsLog::ONGOING_LOG); |
| 688 | NotifyOnDidCreateMetricsLog(); |
| 689 | } |
| 690 | |
holte | 6839585 | 2017-01-10 20:40:21 | [diff] [blame] | 691 | if (upload_scheduler_) { |
| 692 | rotation_scheduler_->InitTaskComplete(); |
| 693 | } else { |
| 694 | scheduler_->InitTaskComplete(); |
| 695 | } |
[email protected] | c68a2b9b | 2013-10-09 18:16:36 | [diff] [blame] | 696 | } |
| 697 | |
[email protected] | 076961c | 2014-03-12 22:23:56 | [diff] [blame] | 698 | void MetricsService::GetUptimes(PrefService* pref, |
| 699 | base::TimeDelta* incremental_uptime, |
| 700 | base::TimeDelta* uptime) { |
[email protected] | c68a2b9b | 2013-10-09 18:16:36 | [diff] [blame] | 701 | base::TimeTicks now = base::TimeTicks::Now(); |
[email protected] | 076961c | 2014-03-12 22:23:56 | [diff] [blame] | 702 | // If this is the first call, init |first_updated_time_| and |
| 703 | // |last_updated_time_|. |
| 704 | if (last_updated_time_.is_null()) { |
| 705 | first_updated_time_ = now; |
[email protected] | c68a2b9b | 2013-10-09 18:16:36 | [diff] [blame] | 706 | last_updated_time_ = now; |
[email protected] | 076961c | 2014-03-12 22:23:56 | [diff] [blame] | 707 | } |
| 708 | *incremental_uptime = now - last_updated_time_; |
| 709 | *uptime = now - first_updated_time_; |
[email protected] | c68a2b9b | 2013-10-09 18:16:36 | [diff] [blame] | 710 | last_updated_time_ = now; |
| 711 | |
avi | 2606292 | 2015-12-26 00:14:18 | [diff] [blame] | 712 | const int64_t incremental_time_secs = incremental_uptime->InSeconds(); |
[email protected] | c68a2b9b | 2013-10-09 18:16:36 | [diff] [blame] | 713 | if (incremental_time_secs > 0) { |
avi | 2606292 | 2015-12-26 00:14:18 | [diff] [blame] | 714 | int64_t metrics_uptime = pref->GetInt64(prefs::kUninstallMetricsUptimeSec); |
[email protected] | c68a2b9b | 2013-10-09 18:16:36 | [diff] [blame] | 715 | metrics_uptime += incremental_time_secs; |
asvitkine | a63d19e | 2014-10-24 16:19:39 | [diff] [blame] | 716 | pref->SetInt64(prefs::kUninstallMetricsUptimeSec, metrics_uptime); |
[email protected] | c68a2b9b | 2013-10-09 18:16:36 | [diff] [blame] | 717 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 718 | } |
| 719 | |
[email protected] | 2a321de3 | 2014-05-10 19:59:06 | [diff] [blame] | 720 | void MetricsService::NotifyOnDidCreateMetricsLog() { |
holte | a07de206 | 2017-01-21 03:43:15 | [diff] [blame] | 721 | DCHECK(thread_checker_.CalledOnValidThread()); |
bcwhite | 34c6bbf | 2016-02-19 22:14:46 | [diff] [blame] | 722 | for (MetricsProvider* provider : metrics_providers_) |
| 723 | provider->OnDidCreateMetricsLog(); |
[email protected] | 2a321de3 | 2014-05-10 19:59:06 | [diff] [blame] | 724 | } |
| 725 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 726 | |
| 727 | //------------------------------------------------------------------------------ |
| 728 | // Recording control methods |
| 729 | |
[email protected] | 410938e0 | 2012-10-24 16:33:59 | [diff] [blame] | 730 | void MetricsService::OpenNewLog() { |
| 731 | DCHECK(!log_manager_.current_log()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 732 | |
[email protected] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 733 | log_manager_.BeginLoggingWithLog(CreateLog(MetricsLog::ONGOING_LOG)); |
[email protected] | 2a321de3 | 2014-05-10 19:59:06 | [diff] [blame] | 734 | NotifyOnDidCreateMetricsLog(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 735 | if (state_ == INITIALIZED) { |
| 736 | // We only need to schedule that run once. |
[email protected] | 85ed9d4 | 2010-06-08 22:37:44 | [diff] [blame] | 737 | state_ = INIT_TASK_SCHEDULED; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 738 | |
skyostil | b0daa01 | 2015-06-02 19:03:48 | [diff] [blame] | 739 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
isherman | b670568 | 2015-08-29 00:01:00 | [diff] [blame] | 740 | FROM_HERE, base::Bind(&MetricsService::StartInitTask, |
skyostil | b0daa01 | 2015-06-02 19:03:48 | [diff] [blame] | 741 | self_ptr_factory_.GetWeakPtr()), |
[email protected] | 7e56010 | 2012-03-08 20:58:42 | [diff] [blame] | 742 | base::TimeDelta::FromSeconds(kInitializationDelaySeconds)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 743 | } |
| 744 | } |
| 745 | |
isherman | b670568 | 2015-08-29 00:01:00 | [diff] [blame] | 746 | void MetricsService::StartInitTask() { |
| 747 | client_->InitializeSystemProfileMetrics( |
| 748 | base::Bind(&MetricsService::FinishedInitTask, |
[email protected] | 51994b2 | 2014-05-30 13:24:21 | [diff] [blame] | 749 | self_ptr_factory_.GetWeakPtr())); |
| 750 | } |
| 751 | |
[email protected] | 410938e0 | 2012-10-24 16:33:59 | [diff] [blame] | 752 | void MetricsService::CloseCurrentLog() { |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 753 | if (!log_manager_.current_log()) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 754 | return; |
| 755 | |
rkaplow | 02e24843 | 2017-01-20 23:39:41 | [diff] [blame] | 756 | // TODO(rkaplow): Evaluate if this is needed. |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 757 | if (log_manager_.current_log()->num_events() > kEventLimit) { |
[email protected] | 553dba6 | 2009-02-24 19:08:23 | [diff] [blame] | 758 | UMA_HISTOGRAM_COUNTS("UMA.Discarded Log Events", |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 759 | log_manager_.current_log()->num_events()); |
rkaplow | 02e24843 | 2017-01-20 23:39:41 | [diff] [blame] | 760 | if (base::FeatureList::IsEnabled(kUMAThrottleEvents)) { |
| 761 | log_manager_.DiscardCurrentLog(); |
| 762 | OpenNewLog(); // Start trivial log to hold our histograms. |
| 763 | } |
[email protected] | 68475e60 | 2008-08-22 03:21:15 | [diff] [blame] | 764 | } |
| 765 | |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 766 | // If a persistent allocator is in use, update its internal histograms (such |
| 767 | // as how much memory is being used) before reporting. |
bcwhite | 33d95806a | 2016-03-16 02:37:45 | [diff] [blame] | 768 | base::PersistentHistogramAllocator* allocator = |
bcwhite | 5e748c6 | 2016-04-06 02:03:53 | [diff] [blame] | 769 | base::GlobalHistogramAllocator::Get(); |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 770 | if (allocator) |
| 771 | allocator->UpdateTrackingHistograms(); |
| 772 | |
[email protected] | 0b33f80b | 2008-12-17 21:34:36 | [diff] [blame] | 773 | // Put incremental data (histogram deltas, and realtime stats deltas) at the |
[email protected] | 147bbc0b | 2009-01-06 19:37:40 | [diff] [blame] | 774 | // end of all log transmissions (initial log handles this separately). |
[email protected] | 024b5cd | 2011-05-27 03:29:38 | [diff] [blame] | 775 | // RecordIncrementalStabilityElements only exists on the derived |
| 776 | // MetricsLog class. |
[email protected] | 94dce12 | 2014-07-16 04:20:12 | [diff] [blame] | 777 | MetricsLog* current_log = log_manager_.current_log(); |
[email protected] | 024b5cd | 2011-05-27 03:29:38 | [diff] [blame] | 778 | DCHECK(current_log); |
asvitkine | 88aa933 | 2014-09-29 23:29:17 | [diff] [blame] | 779 | RecordCurrentEnvironment(current_log); |
[email protected] | 076961c | 2014-03-12 22:23:56 | [diff] [blame] | 780 | base::TimeDelta incremental_uptime; |
| 781 | base::TimeDelta uptime; |
[email protected] | 24f81ca | 2014-05-26 15:59:34 | [diff] [blame] | 782 | GetUptimes(local_state_, &incremental_uptime, &uptime); |
[email protected] | 85791b0b | 2014-05-20 15:18:58 | [diff] [blame] | 783 | current_log->RecordStabilityMetrics(metrics_providers_.get(), |
| 784 | incremental_uptime, uptime); |
[email protected] | 6067756 | 2013-11-17 15:52:55 | [diff] [blame] | 785 | |
[email protected] | 85791b0b | 2014-05-20 15:18:58 | [diff] [blame] | 786 | current_log->RecordGeneralMetrics(metrics_providers_.get()); |
mariakhomenko | 19102898 | 2014-10-20 23:22:56 | [diff] [blame] | 787 | RecordCurrentHistograms(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 788 | |
[email protected] | 2994826 | 2012-03-01 12:15:08 | [diff] [blame] | 789 | log_manager_.FinishCurrentLog(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 790 | } |
| 791 | |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 792 | void MetricsService::PushPendingLogsToPersistentStorage() { |
Steven Holte | 2c294a3 | 2015-03-12 21:45:03 | [diff] [blame] | 793 | if (state_ < SENDING_LOGS) |
[email protected] | 28ab7f9 | 2009-01-06 21:39:04 | [diff] [blame] | 794 | 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] | 795 | |
[email protected] | 410938e0 | 2012-10-24 16:33:59 | [diff] [blame] | 796 | CloseCurrentLog(); |
[email protected] | 80a8f31 | 2013-12-16 18:00:30 | [diff] [blame] | 797 | log_manager_.PersistUnsentLogs(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 798 | } |
| 799 | |
| 800 | //------------------------------------------------------------------------------ |
| 801 | // Transmission of logs methods |
| 802 | |
[email protected] | 7f7f196 | 2011-04-20 15:58:16 | [diff] [blame] | 803 | void MetricsService::StartSchedulerIfNecessary() { |
[email protected] | 410938e0 | 2012-10-24 16:33:59 | [diff] [blame] | 804 | // Never schedule cutting or uploading of logs in test mode. |
| 805 | if (test_mode_active_) |
| 806 | return; |
| 807 | |
| 808 | // Even if reporting is disabled, the scheduler is needed to trigger the |
| 809 | // creation of the initial log, which must be done in order for any logs to be |
| 810 | // persisted on shutdown or backgrounding. |
[email protected] | 80a8f31 | 2013-12-16 18:00:30 | [diff] [blame] | 811 | if (recording_active() && |
Steven Holte | 2c294a3 | 2015-03-12 21:45:03 | [diff] [blame] | 812 | (reporting_active() || state_ < SENDING_LOGS)) { |
holte | 6839585 | 2017-01-10 20:40:21 | [diff] [blame] | 813 | if (upload_scheduler_) { |
| 814 | rotation_scheduler_->Start(); |
| 815 | upload_scheduler_->Start(); |
| 816 | } else { |
| 817 | scheduler_->Start(); |
| 818 | } |
[email protected] | 80a8f31 | 2013-12-16 18:00:30 | [diff] [blame] | 819 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 820 | } |
| 821 | |
[email protected] | 7f7f196 | 2011-04-20 15:58:16 | [diff] [blame] | 822 | void MetricsService::StartScheduledUpload() { |
holte | a6a3917 | 2015-03-25 19:57:20 | [diff] [blame] | 823 | DCHECK(state_ >= INIT_TASK_DONE); |
[email protected] | cd1ac71 | 2012-06-26 08:26:47 | [diff] [blame] | 824 | // If we're getting no notifications, then the log won't have much in it, and |
| 825 | // it's possible the computer is about to go to sleep, so don't upload and |
| 826 | // stop the scheduler. |
[email protected] | 410938e0 | 2012-10-24 16:33:59 | [diff] [blame] | 827 | // If recording has been turned off, the scheduler doesn't need to run. |
| 828 | // If reporting is off, proceed if the initial log hasn't been created, since |
| 829 | // 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] | 830 | // TODO(stuartmorgan): Call Stop() on the scheduler when reporting and/or |
[email protected] | cd1ac71 | 2012-06-26 08:26:47 | [diff] [blame] | 831 | // recording are turned off instead of letting it fire and then aborting. |
| 832 | if (idle_since_last_transmission_ || |
[email protected] | 410938e0 | 2012-10-24 16:33:59 | [diff] [blame] | 833 | !recording_active() || |
Steven Holte | 2c294a3 | 2015-03-12 21:45:03 | [diff] [blame] | 834 | (!reporting_active() && state_ >= SENDING_LOGS)) { |
holte | 6839585 | 2017-01-10 20:40:21 | [diff] [blame] | 835 | if (upload_scheduler_) { |
| 836 | rotation_scheduler_->Stop(); |
| 837 | rotation_scheduler_->RotationFinished(); |
| 838 | } else { |
| 839 | scheduler_->Stop(); |
| 840 | scheduler_->UploadCancelled(); |
| 841 | } |
[email protected] | 7f7f196 | 2011-04-20 15:58:16 | [diff] [blame] | 842 | return; |
| 843 | } |
| 844 | |
[email protected] | cd1ac71 | 2012-06-26 08:26:47 | [diff] [blame] | 845 | // If there are unsent logs, send the next one. If not, start the asynchronous |
| 846 | // process of finalizing the current log for upload. |
Steven Holte | 2c294a3 | 2015-03-12 21:45:03 | [diff] [blame] | 847 | if (state_ == SENDING_LOGS && log_manager_.has_unsent_logs()) { |
holte | 6839585 | 2017-01-10 20:40:21 | [diff] [blame] | 848 | if (upload_scheduler_) { |
| 849 | upload_scheduler_->Start(); |
| 850 | rotation_scheduler_->RotationFinished(); |
| 851 | } else { |
| 852 | SendNextLog(); |
| 853 | } |
[email protected] | cd1ac71 | 2012-06-26 08:26:47 | [diff] [blame] | 854 | } else { |
holte | a6a3917 | 2015-03-25 19:57:20 | [diff] [blame] | 855 | // There are no logs left to send, so start creating a new one. |
isherman | b670568 | 2015-08-29 00:01:00 | [diff] [blame] | 856 | client_->CollectFinalMetricsForLog( |
[email protected] | 4b4892b | 2014-05-22 15:06:15 | [diff] [blame] | 857 | base::Bind(&MetricsService::OnFinalLogInfoCollectionDone, |
| 858 | self_ptr_factory_.GetWeakPtr())); |
[email protected] | cd1ac71 | 2012-06-26 08:26:47 | [diff] [blame] | 859 | } |
[email protected] | 2994826 | 2012-03-01 12:15:08 | [diff] [blame] | 860 | } |
| 861 | |
[email protected] | 2994826 | 2012-03-01 12:15:08 | [diff] [blame] | 862 | void MetricsService::OnFinalLogInfoCollectionDone() { |
[email protected] | 0d5a61a8 | 2014-05-31 22:28:34 | [diff] [blame] | 863 | // If somehow there is a log upload in progress, we return and hope things |
| 864 | // work out. The scheduler isn't informed since if this happens, the scheduler |
[email protected] | 7f7f196 | 2011-04-20 15:58:16 | [diff] [blame] | 865 | // will get a response from the upload. |
[email protected] | 0d5a61a8 | 2014-05-31 22:28:34 | [diff] [blame] | 866 | DCHECK(!log_upload_in_progress_); |
| 867 | if (log_upload_in_progress_) |
[email protected] | 7f7f196 | 2011-04-20 15:58:16 | [diff] [blame] | 868 | return; |
| 869 | |
[email protected] | cd1ac71 | 2012-06-26 08:26:47 | [diff] [blame] | 870 | // Abort if metrics were turned off during the final info gathering. |
[email protected] | 410938e0 | 2012-10-24 16:33:59 | [diff] [blame] | 871 | if (!recording_active()) { |
holte | 6839585 | 2017-01-10 20:40:21 | [diff] [blame] | 872 | if (upload_scheduler_) { |
| 873 | rotation_scheduler_->Stop(); |
| 874 | rotation_scheduler_->RotationFinished(); |
| 875 | } else { |
| 876 | scheduler_->Stop(); |
| 877 | scheduler_->UploadCancelled(); |
| 878 | } |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 879 | return; |
| 880 | } |
| 881 | |
holte | a6a3917 | 2015-03-25 19:57:20 | [diff] [blame] | 882 | if (state_ == INIT_TASK_DONE) { |
| 883 | PrepareInitialMetricsLog(); |
| 884 | } else { |
| 885 | DCHECK_EQ(SENDING_LOGS, state_); |
| 886 | CloseCurrentLog(); |
| 887 | OpenNewLog(); |
| 888 | } |
holte | 6839585 | 2017-01-10 20:40:21 | [diff] [blame] | 889 | if (upload_scheduler_) { |
| 890 | upload_scheduler_->Start(); |
| 891 | rotation_scheduler_->RotationFinished(); |
| 892 | } else { |
| 893 | SendNextLog(); |
| 894 | } |
holte | a6a3917 | 2015-03-25 19:57:20 | [diff] [blame] | 895 | } |
[email protected] | 410938e0 | 2012-10-24 16:33:59 | [diff] [blame] | 896 | |
holte | a6a3917 | 2015-03-25 19:57:20 | [diff] [blame] | 897 | void MetricsService::SendNextLog() { |
| 898 | DCHECK_EQ(SENDING_LOGS, state_); |
[email protected] | 410938e0 | 2012-10-24 16:33:59 | [diff] [blame] | 899 | if (!reporting_active()) { |
holte | 6839585 | 2017-01-10 20:40:21 | [diff] [blame] | 900 | if (upload_scheduler_) { |
| 901 | upload_scheduler_->Stop(); |
| 902 | upload_scheduler_->UploadCancelled(); |
| 903 | } else { |
| 904 | scheduler_->Stop(); |
| 905 | scheduler_->UploadCancelled(); |
| 906 | } |
[email protected] | 410938e0 | 2012-10-24 16:33:59 | [diff] [blame] | 907 | return; |
| 908 | } |
holte | a6a3917 | 2015-03-25 19:57:20 | [diff] [blame] | 909 | if (!log_manager_.has_unsent_logs()) { |
| 910 | // Should only get here if serializing the log failed somehow. |
holte | 6839585 | 2017-01-10 20:40:21 | [diff] [blame] | 911 | if (upload_scheduler_) { |
| 912 | upload_scheduler_->Stop(); |
| 913 | // Reset backoff interval |
| 914 | upload_scheduler_->UploadFinished(true); |
| 915 | } else { |
| 916 | // Just tell the scheduler it was uploaded and wait for the next log |
| 917 | // interval. |
| 918 | scheduler_->UploadFinished(true, log_manager_.has_unsent_logs()); |
| 919 | } |
[email protected] | 2994826 | 2012-03-01 12:15:08 | [diff] [blame] | 920 | return; |
[email protected] | 2994826 | 2012-03-01 12:15:08 | [diff] [blame] | 921 | } |
holte | a6a3917 | 2015-03-25 19:57:20 | [diff] [blame] | 922 | if (!log_manager_.has_staged_log()) |
| 923 | log_manager_.StageNextLogForUpload(); |
gayane | 0b46091c | 2016-04-07 21:01:05 | [diff] [blame] | 924 | |
| 925 | // Proceed to stage the log for upload if log size satisfies cellular log |
| 926 | // upload constrains. |
gayane | 0417f978 | 2016-04-11 19:34:06 | [diff] [blame] | 927 | bool upload_canceled = false; |
| 928 | bool is_cellular_logic = client_->IsUMACellularUploadLogicEnabled(); |
gayane | a6c9bd1 | 2016-04-14 15:51:16 | [diff] [blame] | 929 | if (is_cellular_logic && data_use_tracker_ && |
gayane | 0b46091c | 2016-04-07 21:01:05 | [diff] [blame] | 930 | !data_use_tracker_->ShouldUploadLogOnCellular( |
| 931 | log_manager_.staged_log_hash().size())) { |
holte | 6839585 | 2017-01-10 20:40:21 | [diff] [blame] | 932 | if (upload_scheduler_) { |
| 933 | upload_scheduler_->UploadOverDataUsageCap(); |
| 934 | } else { |
| 935 | scheduler_->UploadCancelled(); |
| 936 | } |
gayane | 0417f978 | 2016-04-11 19:34:06 | [diff] [blame] | 937 | upload_canceled = true; |
gayane | 0b46091c | 2016-04-07 21:01:05 | [diff] [blame] | 938 | } else { |
| 939 | SendStagedLog(); |
| 940 | } |
gayane | 0417f978 | 2016-04-11 19:34:06 | [diff] [blame] | 941 | if (is_cellular_logic) { |
| 942 | UMA_HISTOGRAM_BOOLEAN("UMA.LogUpload.Canceled.CellularConstraint", |
| 943 | upload_canceled); |
| 944 | } |
[email protected] | 2994826 | 2012-03-01 12:15:08 | [diff] [blame] | 945 | } |
| 946 | |
lpromero | ca8cb6f | 2015-04-30 18:16:53 | [diff] [blame] | 947 | bool MetricsService::ProvidersHaveInitialStabilityMetrics() { |
| 948 | // Check whether any metrics provider has initial stability metrics. |
bcwhite | 02ca745 | 2016-11-29 21:54:07 | [diff] [blame] | 949 | // All providers are queried (rather than stopping after the first "true" |
| 950 | // response) in case they do any kind of setup work in preparation for |
| 951 | // the later call to RecordInitialHistogramSnapshots(). |
| 952 | bool has_stability_metrics = false; |
| 953 | for (MetricsProvider* provider : metrics_providers_) |
| 954 | has_stability_metrics |= provider->HasInitialStabilityMetrics(); |
siggi | c179dd06 | 2014-09-10 17:02:31 | [diff] [blame] | 955 | |
bcwhite | 02ca745 | 2016-11-29 21:54:07 | [diff] [blame] | 956 | return has_stability_metrics; |
siggi | c179dd06 | 2014-09-10 17:02:31 | [diff] [blame] | 957 | } |
| 958 | |
manzagop | ea99d195 | 2016-09-08 23:40:05 | [diff] [blame] | 959 | bool MetricsService::PrepareInitialStabilityLog( |
| 960 | const std::string& prefs_previous_version) { |
[email protected] | 80a8f31 | 2013-12-16 18:00:30 | [diff] [blame] | 961 | DCHECK_EQ(INITIALIZED, state_); |
[email protected] | 2994826 | 2012-03-01 12:15:08 | [diff] [blame] | 962 | |
dcheng | d99c42a | 2016-04-21 21:54:13 | [diff] [blame] | 963 | std::unique_ptr<MetricsLog> initial_stability_log( |
[email protected] | 09dee82d | 2014-05-22 14:00:53 | [diff] [blame] | 964 | CreateLog(MetricsLog::INITIAL_STABILITY_LOG)); |
[email protected] | 2a321de3 | 2014-05-10 19:59:06 | [diff] [blame] | 965 | |
| 966 | // Do not call NotifyOnDidCreateMetricsLog here because the stability |
| 967 | // log describes stats from the _previous_ session. |
manzagop | ea99d195 | 2016-09-08 23:40:05 | [diff] [blame] | 968 | std::string system_profile_app_version; |
| 969 | if (!initial_stability_log->LoadSavedEnvironmentFromPrefs( |
| 970 | &system_profile_app_version)) { |
Steven Holte | 2c294a3 | 2015-03-12 21:45:03 | [diff] [blame] | 971 | return false; |
manzagop | ea99d195 | 2016-09-08 23:40:05 | [diff] [blame] | 972 | } |
| 973 | if (system_profile_app_version != prefs_previous_version) |
| 974 | IncrementPrefValue(prefs::kStabilityVersionMismatchCount); |
[email protected] | 85791b0b | 2014-05-20 15:18:58 | [diff] [blame] | 975 | |
[email protected] | 80a8f31 | 2013-12-16 18:00:30 | [diff] [blame] | 976 | log_manager_.PauseCurrentLog(); |
dcheng | 5160635 | 2015-12-26 21:16:23 | [diff] [blame] | 977 | log_manager_.BeginLoggingWithLog(std::move(initial_stability_log)); |
[email protected] | 85791b0b | 2014-05-20 15:18:58 | [diff] [blame] | 978 | |
| 979 | // Note: Some stability providers may record stability stats via histograms, |
| 980 | // so this call has to be after BeginLoggingWithLog(). |
[email protected] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 981 | log_manager_.current_log()->RecordStabilityMetrics( |
| 982 | metrics_providers_.get(), base::TimeDelta(), base::TimeDelta()); |
[email protected] | c778687a | 2014-02-11 14:46:45 | [diff] [blame] | 983 | RecordCurrentStabilityHistograms(); |
[email protected] | 85791b0b | 2014-05-20 15:18:58 | [diff] [blame] | 984 | |
| 985 | // Note: RecordGeneralMetrics() intentionally not called since this log is for |
| 986 | // stability stats from a previous session only. |
| 987 | |
[email protected] | 80a8f31 | 2013-12-16 18:00:30 | [diff] [blame] | 988 | log_manager_.FinishCurrentLog(); |
| 989 | log_manager_.ResumePausedLog(); |
| 990 | |
| 991 | // Store unsent logs, including the stability log that was just saved, so |
| 992 | // that they're not lost in case of a crash before upload time. |
| 993 | log_manager_.PersistUnsentLogs(); |
| 994 | |
Steven Holte | 2c294a3 | 2015-03-12 21:45:03 | [diff] [blame] | 995 | return true; |
[email protected] | 80a8f31 | 2013-12-16 18:00:30 | [diff] [blame] | 996 | } |
| 997 | |
[email protected] | b58b8b2 | 2014-04-08 22:40:33 | [diff] [blame] | 998 | void MetricsService::PrepareInitialMetricsLog() { |
Steven Holte | 2c294a3 | 2015-03-12 21:45:03 | [diff] [blame] | 999 | DCHECK_EQ(INIT_TASK_DONE, state_); |
[email protected] | 0edf876 | 2013-11-21 18:33:30 | [diff] [blame] | 1000 | |
asvitkine | 88aa933 | 2014-09-29 23:29:17 | [diff] [blame] | 1001 | RecordCurrentEnvironment(initial_metrics_log_.get()); |
[email protected] | 076961c | 2014-03-12 22:23:56 | [diff] [blame] | 1002 | base::TimeDelta incremental_uptime; |
| 1003 | base::TimeDelta uptime; |
[email protected] | 24f81ca | 2014-05-26 15:59:34 | [diff] [blame] | 1004 | GetUptimes(local_state_, &incremental_uptime, &uptime); |
[email protected] | 2994826 | 2012-03-01 12:15:08 | [diff] [blame] | 1005 | |
| 1006 | // Histograms only get written to the current log, so make the new log current |
| 1007 | // before writing them. |
| 1008 | log_manager_.PauseCurrentLog(); |
dcheng | 5160635 | 2015-12-26 21:16:23 | [diff] [blame] | 1009 | log_manager_.BeginLoggingWithLog(std::move(initial_metrics_log_)); |
[email protected] | 85791b0b | 2014-05-20 15:18:58 | [diff] [blame] | 1010 | |
| 1011 | // Note: Some stability providers may record stability stats via histograms, |
| 1012 | // so this call has to be after BeginLoggingWithLog(). |
[email protected] | 94dce12 | 2014-07-16 04:20:12 | [diff] [blame] | 1013 | MetricsLog* current_log = log_manager_.current_log(); |
[email protected] | 85791b0b | 2014-05-20 15:18:58 | [diff] [blame] | 1014 | current_log->RecordStabilityMetrics(metrics_providers_.get(), |
| 1015 | base::TimeDelta(), base::TimeDelta()); |
[email protected] | 85791b0b | 2014-05-20 15:18:58 | [diff] [blame] | 1016 | current_log->RecordGeneralMetrics(metrics_providers_.get()); |
mariakhomenko | 19102898 | 2014-10-20 23:22:56 | [diff] [blame] | 1017 | RecordCurrentHistograms(); |
[email protected] | 85791b0b | 2014-05-20 15:18:58 | [diff] [blame] | 1018 | |
[email protected] | 2994826 | 2012-03-01 12:15:08 | [diff] [blame] | 1019 | log_manager_.FinishCurrentLog(); |
| 1020 | log_manager_.ResumePausedLog(); |
| 1021 | |
[email protected] | 94dce12 | 2014-07-16 04:20:12 | [diff] [blame] | 1022 | // Store unsent logs, including the initial log that was just saved, so |
| 1023 | // that they're not lost in case of a crash before upload time. |
| 1024 | log_manager_.PersistUnsentLogs(); |
holte | a6a3917 | 2015-03-25 19:57:20 | [diff] [blame] | 1025 | |
| 1026 | state_ = SENDING_LOGS; |
[email protected] | 2994826 | 2012-03-01 12:15:08 | [diff] [blame] | 1027 | } |
| 1028 | |
[email protected] | 2994826 | 2012-03-01 12:15:08 | [diff] [blame] | 1029 | void MetricsService::SendStagedLog() { |
| 1030 | DCHECK(log_manager_.has_staged_log()); |
[email protected] | 0d5a61a8 | 2014-05-31 22:28:34 | [diff] [blame] | 1031 | if (!log_manager_.has_staged_log()) |
| 1032 | return; |
[email protected] | 2994826 | 2012-03-01 12:15:08 | [diff] [blame] | 1033 | |
[email protected] | 0d5a61a8 | 2014-05-31 22:28:34 | [diff] [blame] | 1034 | DCHECK(!log_upload_in_progress_); |
| 1035 | log_upload_in_progress_ = true; |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1036 | |
[email protected] | 0d5a61a8 | 2014-05-31 22:28:34 | [diff] [blame] | 1037 | if (!log_uploader_) { |
| 1038 | log_uploader_ = client_->CreateUploader( |
holte | 567a16f2 | 2017-01-06 01:53:45 | [diff] [blame] | 1039 | client_->GetMetricsServerUrl(), |
| 1040 | metrics::kDefaultMetricsMimeType, |
[email protected] | 0d5a61a8 | 2014-05-31 22:28:34 | [diff] [blame] | 1041 | base::Bind(&MetricsService::OnLogUploadComplete, |
| 1042 | self_ptr_factory_.GetWeakPtr())); |
| 1043 | } |
| 1044 | |
| 1045 | const std::string hash = |
| 1046 | base::HexEncode(log_manager_.staged_log_hash().data(), |
| 1047 | log_manager_.staged_log_hash().size()); |
isherman | a672752 | 2015-08-06 22:28:49 | [diff] [blame] | 1048 | log_uploader_->UploadLog(log_manager_.staged_log(), hash); |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1049 | |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1050 | HandleIdleSinceLastTransmission(true); |
| 1051 | } |
| 1052 | |
[email protected] | cac7884 | 2008-11-27 01:02:20 | [diff] [blame] | 1053 | |
[email protected] | 0d5a61a8 | 2014-05-31 22:28:34 | [diff] [blame] | 1054 | void MetricsService::OnLogUploadComplete(int response_code) { |
Steven Holte | 2c294a3 | 2015-03-12 21:45:03 | [diff] [blame] | 1055 | DCHECK_EQ(SENDING_LOGS, state_); |
[email protected] | 0d5a61a8 | 2014-05-31 22:28:34 | [diff] [blame] | 1056 | DCHECK(log_upload_in_progress_); |
| 1057 | log_upload_in_progress_ = false; |
[email protected] | fe58acc2 | 2012-02-29 01:29:58 | [diff] [blame] | 1058 | |
[email protected] | dc61fe9 | 2012-06-12 00:13:50 | [diff] [blame] | 1059 | // Log a histogram to track response success vs. failure rates. |
[email protected] | e3eb0c4 | 2013-04-18 06:18:58 | [diff] [blame] | 1060 | UMA_HISTOGRAM_ENUMERATION("UMA.UploadResponseStatus.Protobuf", |
| 1061 | ResponseCodeToStatus(response_code), |
| 1062 | NUM_RESPONSE_STATUSES); |
[email protected] | fe58acc2 | 2012-02-29 01:29:58 | [diff] [blame] | 1063 | |
[email protected] | dc61fe9 | 2012-06-12 00:13:50 | [diff] [blame] | 1064 | bool upload_succeeded = response_code == 200; |
[email protected] | 7f7f196 | 2011-04-20 15:58:16 | [diff] [blame] | 1065 | |
[email protected] | 0eb34fee | 2009-01-21 08:04:38 | [diff] [blame] | 1066 | // Provide boolean for error recovery (allow us to ignore response_code). |
[email protected] | dc6f496 | 2009-02-13 01:25:50 | [diff] [blame] | 1067 | bool discard_log = false; |
[email protected] | 7f07db6 | 2014-05-15 01:12:45 | [diff] [blame] | 1068 | const size_t log_size = log_manager_.staged_log().length(); |
asvitkine | d80ebf6c | 2014-09-02 22:29:11 | [diff] [blame] | 1069 | if (upload_succeeded) { |
| 1070 | UMA_HISTOGRAM_COUNTS_10000("UMA.LogSize.OnSuccess", log_size / 1024); |
| 1071 | } else if (log_size > kUploadLogAvoidRetransmitSize) { |
[email protected] | dc61fe9 | 2012-06-12 00:13:50 | [diff] [blame] | 1072 | UMA_HISTOGRAM_COUNTS("UMA.Large Rejected Log was Discarded", |
| 1073 | static_cast<int>(log_size)); |
[email protected] | 0eb34fee | 2009-01-21 08:04:38 | [diff] [blame] | 1074 | discard_log = true; |
[email protected] | dc61fe9 | 2012-06-12 00:13:50 | [diff] [blame] | 1075 | } else if (response_code == 400) { |
[email protected] | 0eb34fee | 2009-01-21 08:04:38 | [diff] [blame] | 1076 | // Bad syntax. Retransmission won't work. |
[email protected] | 0eb34fee | 2009-01-21 08:04:38 | [diff] [blame] | 1077 | discard_log = true; |
[email protected] | 68475e60 | 2008-08-22 03:21:15 | [diff] [blame] | 1078 | } |
| 1079 | |
[email protected] | 94dce12 | 2014-07-16 04:20:12 | [diff] [blame] | 1080 | if (upload_succeeded || discard_log) { |
[email protected] | 5f3e164 | 2013-05-05 03:37:34 | [diff] [blame] | 1081 | log_manager_.DiscardStagedLog(); |
[email protected] | 94dce12 | 2014-07-16 04:20:12 | [diff] [blame] | 1082 | // Store the updated list to disk now that the removed log is uploaded. |
| 1083 | log_manager_.PersistUnsentLogs(); |
| 1084 | } |
[email protected] | dc61fe9 | 2012-06-12 00:13:50 | [diff] [blame] | 1085 | |
[email protected] | 7f7f196 | 2011-04-20 15:58:16 | [diff] [blame] | 1086 | // Error 400 indicates a problem with the log, not with the server, so |
| 1087 | // don't consider that a sign that the server is in trouble. |
[email protected] | dc61fe9 | 2012-06-12 00:13:50 | [diff] [blame] | 1088 | bool server_is_healthy = upload_succeeded || response_code == 400; |
holte | 6839585 | 2017-01-10 20:40:21 | [diff] [blame] | 1089 | if (upload_scheduler_) { |
| 1090 | if (!log_manager_.has_unsent_logs()) { |
| 1091 | upload_scheduler_->Stop(); |
| 1092 | } |
| 1093 | upload_scheduler_->UploadFinished(server_is_healthy); |
| 1094 | } else { |
| 1095 | scheduler_->UploadFinished(server_is_healthy, |
| 1096 | log_manager_.has_unsent_logs()); |
| 1097 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1098 | } |
| 1099 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 1100 | void MetricsService::IncrementPrefValue(const char* path) { |
[email protected] | 24f81ca | 2014-05-26 15:59:34 | [diff] [blame] | 1101 | int value = local_state_->GetInteger(path); |
| 1102 | local_state_->SetInteger(path, value + 1); |
[email protected] | e73c0197 | 2008-08-13 00:18:24 | [diff] [blame] | 1103 | } |
| 1104 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 1105 | void MetricsService::IncrementLongPrefsValue(const char* path) { |
avi | 2606292 | 2015-12-26 00:14:18 | [diff] [blame] | 1106 | int64_t value = local_state_->GetInt64(path); |
[email protected] | 24f81ca | 2014-05-26 15:59:34 | [diff] [blame] | 1107 | local_state_->SetInt64(path, value + 1); |
[email protected] | 0bb1a62 | 2009-03-04 03:22:32 | [diff] [blame] | 1108 | } |
| 1109 | |
[email protected] | c0c55e9 | 2011-09-10 18:47:30 | [diff] [blame] | 1110 | bool MetricsService::UmaMetricsProperlyShutdown() { |
| 1111 | CHECK(clean_shutdown_status_ == CLEANLY_SHUTDOWN || |
| 1112 | clean_shutdown_status_ == NEED_TO_SHUTDOWN); |
| 1113 | return clean_shutdown_status_ == CLEANLY_SHUTDOWN; |
| 1114 | } |
| 1115 | |
asvitkine | e0dbdbe | 2014-10-31 21:59:57 | [diff] [blame] | 1116 | void MetricsService::AddSyntheticTrialObserver( |
asvitkine | 9a27983 | 2015-12-18 02:35:50 | [diff] [blame] | 1117 | variations::SyntheticTrialObserver* observer) { |
asvitkine | e0dbdbe | 2014-10-31 21:59:57 | [diff] [blame] | 1118 | synthetic_trial_observer_list_.AddObserver(observer); |
| 1119 | if (!synthetic_trial_groups_.empty()) |
| 1120 | observer->OnSyntheticTrialsChanged(synthetic_trial_groups_); |
| 1121 | } |
| 1122 | |
| 1123 | void MetricsService::RemoveSyntheticTrialObserver( |
asvitkine | 9a27983 | 2015-12-18 02:35:50 | [diff] [blame] | 1124 | variations::SyntheticTrialObserver* observer) { |
asvitkine | e0dbdbe | 2014-10-31 21:59:57 | [diff] [blame] | 1125 | synthetic_trial_observer_list_.RemoveObserver(observer); |
| 1126 | } |
| 1127 | |
[email protected] | 6067756 | 2013-11-17 15:52:55 | [diff] [blame] | 1128 | void MetricsService::RegisterSyntheticFieldTrial( |
asvitkine | 9a27983 | 2015-12-18 02:35:50 | [diff] [blame] | 1129 | const variations::SyntheticTrialGroup& trial) { |
[email protected] | 6067756 | 2013-11-17 15:52:55 | [diff] [blame] | 1130 | for (size_t i = 0; i < synthetic_trial_groups_.size(); ++i) { |
| 1131 | if (synthetic_trial_groups_[i].id.name == trial.id.name) { |
| 1132 | if (synthetic_trial_groups_[i].id.group != trial.id.group) { |
| 1133 | synthetic_trial_groups_[i].id.group = trial.id.group; |
[email protected] | 7a5c0781 | 2014-02-26 11:45:41 | [diff] [blame] | 1134 | synthetic_trial_groups_[i].start_time = base::TimeTicks::Now(); |
asvitkine | e0dbdbe | 2014-10-31 21:59:57 | [diff] [blame] | 1135 | NotifySyntheticTrialObservers(); |
[email protected] | 6067756 | 2013-11-17 15:52:55 | [diff] [blame] | 1136 | } |
| 1137 | return; |
| 1138 | } |
| 1139 | } |
| 1140 | |
asvitkine | 9a27983 | 2015-12-18 02:35:50 | [diff] [blame] | 1141 | variations::SyntheticTrialGroup trial_group = trial; |
[email protected] | 7a5c0781 | 2014-02-26 11:45:41 | [diff] [blame] | 1142 | trial_group.start_time = base::TimeTicks::Now(); |
[email protected] | 6067756 | 2013-11-17 15:52:55 | [diff] [blame] | 1143 | synthetic_trial_groups_.push_back(trial_group); |
asvitkine | e0dbdbe | 2014-10-31 21:59:57 | [diff] [blame] | 1144 | NotifySyntheticTrialObservers(); |
[email protected] | 6067756 | 2013-11-17 15:52:55 | [diff] [blame] | 1145 | } |
| 1146 | |
asvitkine | 6a1c881 | 2016-08-01 22:37:01 | [diff] [blame] | 1147 | void MetricsService::RegisterSyntheticMultiGroupFieldTrial( |
| 1148 | uint32_t trial_name_hash, |
| 1149 | const std::vector<uint32_t>& group_name_hashes) { |
| 1150 | auto has_same_trial_name = |
| 1151 | [trial_name_hash](const variations::SyntheticTrialGroup& x) { |
| 1152 | return x.id.name == trial_name_hash; |
| 1153 | }; |
| 1154 | synthetic_trial_groups_.erase( |
| 1155 | std::remove_if(synthetic_trial_groups_.begin(), |
| 1156 | synthetic_trial_groups_.end(), has_same_trial_name), |
| 1157 | synthetic_trial_groups_.end()); |
| 1158 | |
| 1159 | if (group_name_hashes.empty()) |
| 1160 | return; |
| 1161 | |
| 1162 | variations::SyntheticTrialGroup trial_group(trial_name_hash, |
| 1163 | group_name_hashes[0]); |
| 1164 | trial_group.start_time = base::TimeTicks::Now(); |
| 1165 | for (uint32_t group_name_hash : group_name_hashes) { |
| 1166 | // Note: Adding the trial group will copy it, so this re-uses the same |
| 1167 | // |trial_group| struct for convenience (e.g. so start_time's all match). |
| 1168 | trial_group.id.group = group_name_hash; |
| 1169 | synthetic_trial_groups_.push_back(trial_group); |
| 1170 | } |
| 1171 | NotifySyntheticTrialObservers(); |
| 1172 | } |
| 1173 | |
nasko | 7d4c2cb | 2015-11-26 01:29:35 | [diff] [blame] | 1174 | void MetricsService::GetCurrentSyntheticFieldTrialsForTesting( |
| 1175 | std::vector<variations::ActiveGroupId>* synthetic_trials) { |
| 1176 | GetSyntheticFieldTrialsOlderThan(base::TimeTicks::Now(), synthetic_trials); |
| 1177 | } |
| 1178 | |
[email protected] | 85791b0b | 2014-05-20 15:18:58 | [diff] [blame] | 1179 | void MetricsService::RegisterMetricsProvider( |
dcheng | d99c42a | 2016-04-21 21:54:13 | [diff] [blame] | 1180 | std::unique_ptr<MetricsProvider> provider) { |
[email protected] | 85791b0b | 2014-05-20 15:18:58 | [diff] [blame] | 1181 | DCHECK_EQ(INITIALIZED, state_); |
dcheng | 5160635 | 2015-12-26 21:16:23 | [diff] [blame] | 1182 | metrics_providers_.push_back(std::move(provider)); |
[email protected] | 85791b0b | 2014-05-20 15:18:58 | [diff] [blame] | 1183 | } |
| 1184 | |
[email protected] | 61b0d48 | 2014-05-20 14:49:10 | [diff] [blame] | 1185 | void MetricsService::CheckForClonedInstall( |
| 1186 | scoped_refptr<base::SingleThreadTaskRunner> task_runner) { |
| 1187 | state_manager_->CheckForClonedInstall(task_runner); |
[email protected] | 99c892d | 2014-03-24 18:11:21 | [diff] [blame] | 1188 | } |
| 1189 | |
asvitkine | e0dbdbe | 2014-10-31 21:59:57 | [diff] [blame] | 1190 | void MetricsService::NotifySyntheticTrialObservers() { |
ericwilligers | a22488d4 | 2016-10-25 01:20:57 | [diff] [blame] | 1191 | for (variations::SyntheticTrialObserver& observer : |
| 1192 | synthetic_trial_observer_list_) { |
| 1193 | observer.OnSyntheticTrialsChanged(synthetic_trial_groups_); |
| 1194 | } |
asvitkine | e0dbdbe | 2014-10-31 21:59:57 | [diff] [blame] | 1195 | } |
| 1196 | |
nasko | 7d4c2cb | 2015-11-26 01:29:35 | [diff] [blame] | 1197 | void MetricsService::GetSyntheticFieldTrialsOlderThan( |
| 1198 | base::TimeTicks time, |
[email protected] | b3610d4 | 2014-05-19 18:07:23 | [diff] [blame] | 1199 | std::vector<variations::ActiveGroupId>* synthetic_trials) { |
[email protected] | 6067756 | 2013-11-17 15:52:55 | [diff] [blame] | 1200 | DCHECK(synthetic_trials); |
| 1201 | synthetic_trials->clear(); |
[email protected] | 6067756 | 2013-11-17 15:52:55 | [diff] [blame] | 1202 | for (size_t i = 0; i < synthetic_trial_groups_.size(); ++i) { |
nasko | 7d4c2cb | 2015-11-26 01:29:35 | [diff] [blame] | 1203 | if (synthetic_trial_groups_[i].start_time <= time) |
[email protected] | 6067756 | 2013-11-17 15:52:55 | [diff] [blame] | 1204 | synthetic_trials->push_back(synthetic_trial_groups_[i].id); |
| 1205 | } |
| 1206 | } |
| 1207 | |
dcheng | d99c42a | 2016-04-21 21:54:13 | [diff] [blame] | 1208 | std::unique_ptr<MetricsLog> MetricsService::CreateLog( |
| 1209 | MetricsLog::LogType log_type) { |
ricea | 85ec5795 | 2016-08-31 09:34:10 | [diff] [blame] | 1210 | return base::MakeUnique<MetricsLog>(state_manager_->client_id(), session_id_, |
| 1211 | log_type, client_, local_state_); |
[email protected] | 09dee82d | 2014-05-22 14:00:53 | [diff] [blame] | 1212 | } |
| 1213 | |
asvitkine | 88aa933 | 2014-09-29 23:29:17 | [diff] [blame] | 1214 | void MetricsService::RecordCurrentEnvironment(MetricsLog* log) { |
manzagop | a5d6688d | 2016-10-25 20:16:03 | [diff] [blame] | 1215 | DCHECK(client_); |
asvitkine | 88aa933 | 2014-09-29 23:29:17 | [diff] [blame] | 1216 | std::vector<variations::ActiveGroupId> synthetic_trials; |
nasko | 7d4c2cb | 2015-11-26 01:29:35 | [diff] [blame] | 1217 | GetSyntheticFieldTrialsOlderThan(log->creation_time(), &synthetic_trials); |
manzagop | a5d6688d | 2016-10-25 20:16:03 | [diff] [blame] | 1218 | std::string serialized_environment = log->RecordEnvironment( |
| 1219 | metrics_providers_.get(), synthetic_trials, GetInstallDate(), |
| 1220 | GetMetricsReportingEnabledDate()); |
| 1221 | client_->OnEnvironmentUpdate(&serialized_environment); |
asvitkine | 88aa933 | 2014-09-29 23:29:17 | [diff] [blame] | 1222 | } |
| 1223 | |
[email protected] | acc2ce551 | 2014-05-22 18:29:13 | [diff] [blame] | 1224 | void MetricsService::RecordCurrentHistograms() { |
| 1225 | DCHECK(log_manager_.current_log()); |
bcwhite | 9556e817 | 2016-06-10 19:55:04 | [diff] [blame] | 1226 | SCOPED_UMA_HISTOGRAM_TIMER("UMA.MetricsService.RecordCurrentHistograms.Time"); |
bcwhite | 05dc092 | 2016-06-03 04:59:44 | [diff] [blame] | 1227 | |
bcwhite | 34c6bbf | 2016-02-19 22:14:46 | [diff] [blame] | 1228 | // "true" to the begin() call indicates that StatisticsRecorder should include |
| 1229 | // histograms held in persistent storage. |
bcwhite | 8373bd3 | 2016-07-13 02:49:11 | [diff] [blame] | 1230 | histogram_snapshot_manager_.PrepareDeltas( |
bcwhite | 65e57d0 | 2016-05-13 14:39:40 | [diff] [blame] | 1231 | base::StatisticsRecorder::begin(true), base::StatisticsRecorder::end(), |
| 1232 | base::Histogram::kNoFlags, base::Histogram::kUmaTargetedHistogramFlag); |
bcwhite | 34c6bbf | 2016-02-19 22:14:46 | [diff] [blame] | 1233 | for (MetricsProvider* provider : metrics_providers_) |
| 1234 | provider->RecordHistogramSnapshots(&histogram_snapshot_manager_); |
[email protected] | acc2ce551 | 2014-05-22 18:29:13 | [diff] [blame] | 1235 | } |
| 1236 | |
| 1237 | void MetricsService::RecordCurrentStabilityHistograms() { |
| 1238 | DCHECK(log_manager_.current_log()); |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 1239 | // "true" indicates that StatisticsRecorder should include histograms in |
| 1240 | // persistent storage. |
bcwhite | 8373bd3 | 2016-07-13 02:49:11 | [diff] [blame] | 1241 | histogram_snapshot_manager_.PrepareDeltas( |
bcwhite | 5cb99eb | 2016-02-01 21:07:56 | [diff] [blame] | 1242 | base::StatisticsRecorder::begin(true), base::StatisticsRecorder::end(), |
[email protected] | acc2ce551 | 2014-05-22 18:29:13 | [diff] [blame] | 1243 | base::Histogram::kNoFlags, base::Histogram::kUmaStabilityHistogramFlag); |
bcwhite | 65e57d0 | 2016-05-13 14:39:40 | [diff] [blame] | 1244 | for (MetricsProvider* provider : metrics_providers_) |
| 1245 | provider->RecordInitialHistogramSnapshots(&histogram_snapshot_manager_); |
[email protected] | acc2ce551 | 2014-05-22 18:29:13 | [diff] [blame] | 1246 | } |
| 1247 | |
holte | 6d34116 | 2016-12-19 21:42:42 | [diff] [blame] | 1248 | void MetricsService::LogCleanShutdown(bool end_completed) { |
holte | a07de206 | 2017-01-21 03:43:15 | [diff] [blame] | 1249 | DCHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | c0c55e9 | 2011-09-10 18:47:30 | [diff] [blame] | 1250 | // Redundant setting to assure that we always reset this value at shutdown |
| 1251 | // (and that we don't use some alternate path, and not call LogCleanShutdown). |
| 1252 | clean_shutdown_status_ = CLEANLY_SHUTDOWN; |
manzagop | 14aff5d | 2016-11-23 17:27:00 | [diff] [blame] | 1253 | client_->OnLogCleanShutdown(); |
erikwright | 65b58df | 2014-09-12 00:05:28 | [diff] [blame] | 1254 | clean_exit_beacon_.WriteBeaconValue(true); |
holte | a27eadf | 2017-01-26 01:58:59 | [diff] [blame^] | 1255 | SetExecutionPhase(ExecutionPhase::SHUTDOWN_COMPLETE, local_state_); |
holte | 6d34116 | 2016-12-19 21:42:42 | [diff] [blame] | 1256 | local_state_->SetBoolean(prefs::kStabilitySessionEndCompleted, end_completed); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1257 | } |
asvitkine | cbd42073 | 2014-08-26 22:15:40 | [diff] [blame] | 1258 | |
| 1259 | } // namespace metrics |