[email protected] | 91b1d91 | 2014-06-05 10:52:08 | [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 | |
[email protected] | 91b1d91 | 2014-06-05 10:52:08 | [diff] [blame] | 5 | #include "components/metrics/metrics_log.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 6 | |
avi | 2606292 | 2015-12-26 00:14:18 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | |
[email protected] | 76869ff | 2013-01-15 16:13:47 | [diff] [blame] | 9 | #include <algorithm> |
dcheng | d99c42a | 2016-04-21 21:54:13 | [diff] [blame] | 10 | #include <memory> |
[email protected] | 1eeb5e0 | 2010-07-20 23:02:11 | [diff] [blame] | 11 | #include <string> |
| 12 | #include <vector> |
| 13 | |
[email protected] | 0f2f779 | 2013-11-28 16:09:14 | [diff] [blame] | 14 | #include "base/base64.h" |
sebmarchand | 2f4ed50 | 2014-10-31 15:28:19 | [diff] [blame] | 15 | #include "base/build_time.h" |
[email protected] | 5c8f89f69 | 2013-07-18 11:13:28 | [diff] [blame] | 16 | #include "base/cpu.h" |
[email protected] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 17 | #include "base/metrics/histogram.h" |
| 18 | #include "base/metrics/histogram_samples.h" |
bcwhite | 502b229 | 2015-11-30 22:31:01 | [diff] [blame] | 19 | #include "base/metrics/metrics_hashes.h" |
[email protected] | 0f2f779 | 2013-11-28 16:09:14 | [diff] [blame] | 20 | #include "base/sha1.h" |
[email protected] | 3ea1b18 | 2013-02-08 22:38:41 | [diff] [blame] | 21 | #include "base/strings/string_number_conversions.h" |
[email protected] | f9b29436 | 2013-06-10 20:22:31 | [diff] [blame] | 22 | #include "base/strings/string_util.h" |
[email protected] | 112158af | 2013-06-07 23:46:18 | [diff] [blame] | 23 | #include "base/strings/utf_string_conversions.h" |
[email protected] | fadf97f | 2008-09-18 12:18:14 | [diff] [blame] | 24 | #include "base/sys_info.h" |
[email protected] | 8481347 | 2013-06-28 00:25:19 | [diff] [blame] | 25 | #include "base/time/time.h" |
avi | 2606292 | 2015-12-26 00:14:18 | [diff] [blame] | 26 | #include "build/build_config.h" |
rtenneti | 7254696 | 2014-12-15 21:41:59 | [diff] [blame] | 27 | #include "components/metrics/histogram_encoder.h" |
[email protected] | 91b1d91 | 2014-06-05 10:52:08 | [diff] [blame] | 28 | #include "components/metrics/metrics_pref_names.h" |
[email protected] | 85791b0b | 2014-05-20 15:18:58 | [diff] [blame] | 29 | #include "components/metrics/metrics_provider.h" |
[email protected] | 09dee82d | 2014-05-22 14:00:53 | [diff] [blame] | 30 | #include "components/metrics/metrics_service_client.h" |
[email protected] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 31 | #include "components/metrics/proto/histogram_event.pb.h" |
[email protected] | 064107e | 2014-05-02 00:59:06 | [diff] [blame] | 32 | #include "components/metrics/proto/system_profile.pb.h" |
[email protected] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 33 | #include "components/metrics/proto/user_action_event.pb.h" |
brettw | 06650868 | 2016-02-03 08:22:02 | [diff] [blame] | 34 | #include "components/prefs/pref_registry_simple.h" |
| 35 | #include "components/prefs/pref_service.h" |
[email protected] | b3610d4 | 2014-05-19 18:07:23 | [diff] [blame] | 36 | #include "components/variations/active_field_trials.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 37 | |
[email protected] | 5106b3a | 2012-10-03 20:10:44 | [diff] [blame] | 38 | #if defined(OS_ANDROID) |
| 39 | #include "base/android/build_info.h" |
| 40 | #endif |
| 41 | |
[email protected] | d1be67b | 2008-11-19 20:28:38 | [diff] [blame] | 42 | #if defined(OS_WIN) |
thakis | d62f5447 | 2016-04-04 02:21:10 | [diff] [blame] | 43 | #include "base/win/current_module.h" |
[email protected] | d1be67b | 2008-11-19 20:28:38 | [diff] [blame] | 44 | #endif |
| 45 | |
[email protected] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 46 | using base::SampleCountIterator; |
[email protected] | b3610d4 | 2014-05-19 18:07:23 | [diff] [blame] | 47 | typedef variations::ActiveGroupId ActiveGroupId; |
[email protected] | 79078df | 2012-02-16 01:22:32 | [diff] [blame] | 48 | |
asvitkine | cbd42073 | 2014-08-26 22:15:40 | [diff] [blame] | 49 | namespace metrics { |
| 50 | |
[email protected] | 1df44b7 | 2012-01-19 05:20:34 | [diff] [blame] | 51 | namespace { |
| 52 | |
[email protected] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 53 | // Any id less than 16 bytes is considered to be a testing id. |
| 54 | bool IsTestingID(const std::string& id) { |
| 55 | return id.size() < 16; |
| 56 | } |
| 57 | |
[email protected] | 0f2f779 | 2013-11-28 16:09:14 | [diff] [blame] | 58 | // Computes a SHA-1 hash of |data| and returns it as a hex string. |
| 59 | std::string ComputeSHA1(const std::string& data) { |
| 60 | const std::string sha1 = base::SHA1HashString(data); |
| 61 | return base::HexEncode(sha1.data(), sha1.size()); |
| 62 | } |
| 63 | |
[email protected] | 0c8b7ad | 2012-11-06 07:08:14 | [diff] [blame] | 64 | void WriteFieldTrials(const std::vector<ActiveGroupId>& field_trial_ids, |
[email protected] | 767c9d9 | 2012-03-02 16:04:34 | [diff] [blame] | 65 | SystemProfileProto* system_profile) { |
[email protected] | 0c8b7ad | 2012-11-06 07:08:14 | [diff] [blame] | 66 | for (std::vector<ActiveGroupId>::const_iterator it = |
[email protected] | ad2461c | 2012-04-27 21:11:03 | [diff] [blame] | 67 | field_trial_ids.begin(); it != field_trial_ids.end(); ++it) { |
[email protected] | 767c9d9 | 2012-03-02 16:04:34 | [diff] [blame] | 68 | SystemProfileProto::FieldTrial* field_trial = |
| 69 | system_profile->add_field_trial(); |
| 70 | field_trial->set_name_id(it->name); |
| 71 | field_trial->set_group_id(it->group); |
| 72 | } |
| 73 | } |
| 74 | |
[email protected] | 86573d1 | 2013-07-11 19:48:32 | [diff] [blame] | 75 | // Round a timestamp measured in seconds since epoch to one with a granularity |
| 76 | // of an hour. This can be used before uploaded potentially sensitive |
| 77 | // timestamps. |
avi | 2606292 | 2015-12-26 00:14:18 | [diff] [blame] | 78 | int64_t RoundSecondsToHour(int64_t time_in_seconds) { |
[email protected] | 86573d1 | 2013-07-11 19:48:32 | [diff] [blame] | 79 | return 3600 * (time_in_seconds / 3600); |
| 80 | } |
| 81 | |
[email protected] | 1df44b7 | 2012-01-19 05:20:34 | [diff] [blame] | 82 | } // namespace |
| 83 | |
[email protected] | 9eae403 | 2014-04-09 19:15:19 | [diff] [blame] | 84 | MetricsLog::MetricsLog(const std::string& client_id, |
| 85 | int session_id, |
[email protected] | 09dee82d | 2014-05-22 14:00:53 | [diff] [blame] | 86 | LogType log_type, |
asvitkine | 4c1d1ef | 2014-09-29 20:57:32 | [diff] [blame] | 87 | MetricsServiceClient* client, |
[email protected] | 24f81ca | 2014-05-26 15:59:34 | [diff] [blame] | 88 | PrefService* local_state) |
[email protected] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 89 | : closed_(false), |
| 90 | log_type_(log_type), |
[email protected] | 09dee82d | 2014-05-22 14:00:53 | [diff] [blame] | 91 | client_(client), |
[email protected] | 24f81ca | 2014-05-26 15:59:34 | [diff] [blame] | 92 | creation_time_(base::TimeTicks::Now()), |
| 93 | local_state_(local_state) { |
[email protected] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 94 | if (IsTestingID(client_id)) |
| 95 | uma_proto_.set_client_id(0); |
| 96 | else |
| 97 | uma_proto_.set_client_id(Hash(client_id)); |
| 98 | |
| 99 | uma_proto_.set_session_id(session_id); |
| 100 | |
avi | 2606292 | 2015-12-26 00:14:18 | [diff] [blame] | 101 | const int32_t product = client_->GetProduct(); |
asvitkine | 4c1d1ef | 2014-09-29 20:57:32 | [diff] [blame] | 102 | // Only set the product if it differs from the default value. |
| 103 | if (product != uma_proto_.product()) |
| 104 | uma_proto_.set_product(product); |
| 105 | |
[email protected] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 106 | SystemProfileProto* system_profile = uma_proto_.mutable_system_profile(); |
| 107 | system_profile->set_build_timestamp(GetBuildTime()); |
| 108 | system_profile->set_app_version(client_->GetVersionString()); |
| 109 | system_profile->set_channel(client_->GetChannel()); |
vadimt | 69053126 | 2015-01-07 15:35:22 | [diff] [blame] | 110 | #if defined(SYZYASAN) |
| 111 | system_profile->set_is_asan_build(true); |
| 112 | #endif |
[email protected] | afc03f0 | 2013-10-11 06:01:35 | [diff] [blame] | 113 | } |
[email protected] | 5ed7d457 | 2009-12-23 17:42:41 | [diff] [blame] | 114 | |
[email protected] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 115 | MetricsLog::~MetricsLog() { |
| 116 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 117 | |
[email protected] | 91b1d91 | 2014-06-05 10:52:08 | [diff] [blame] | 118 | // static |
| 119 | void MetricsLog::RegisterPrefs(PrefRegistrySimple* registry) { |
asvitkine | 4c1d1ef | 2014-09-29 20:57:32 | [diff] [blame] | 120 | registry->RegisterIntegerPref(prefs::kStabilityCrashCount, 0); |
| 121 | registry->RegisterIntegerPref(prefs::kStabilityIncompleteSessionEndCount, 0); |
manzagop | ea99d195 | 2016-09-08 23:40:05 | [diff] [blame^] | 122 | registry->RegisterIntegerPref(prefs::kStabilityLaunchCount, 0); |
asvitkine | 4c1d1ef | 2014-09-29 20:57:32 | [diff] [blame] | 123 | registry->RegisterIntegerPref(prefs::kStabilityBreakpadRegistrationFail, 0); |
[email protected] | 91b1d91 | 2014-06-05 10:52:08 | [diff] [blame] | 124 | registry->RegisterIntegerPref( |
asvitkine | 4c1d1ef | 2014-09-29 20:57:32 | [diff] [blame] | 125 | prefs::kStabilityBreakpadRegistrationSuccess, 0); |
| 126 | registry->RegisterIntegerPref(prefs::kStabilityDebuggerPresent, 0); |
| 127 | registry->RegisterIntegerPref(prefs::kStabilityDebuggerNotPresent, 0); |
| 128 | registry->RegisterStringPref(prefs::kStabilitySavedSystemProfile, |
[email protected] | 91b1d91 | 2014-06-05 10:52:08 | [diff] [blame] | 129 | std::string()); |
asvitkine | 4c1d1ef | 2014-09-29 20:57:32 | [diff] [blame] | 130 | registry->RegisterStringPref(prefs::kStabilitySavedSystemProfileHash, |
[email protected] | 91b1d91 | 2014-06-05 10:52:08 | [diff] [blame] | 131 | std::string()); |
manzagop | ea99d195 | 2016-09-08 23:40:05 | [diff] [blame^] | 132 | registry->RegisterIntegerPref(prefs::kStabilityDeferredCount, 0); |
| 133 | registry->RegisterIntegerPref(prefs::kStabilityDiscardCount, 0); |
| 134 | registry->RegisterIntegerPref(prefs::kStabilityVersionMismatchCount, 0); |
[email protected] | 91b1d91 | 2014-06-05 10:52:08 | [diff] [blame] | 135 | } |
| 136 | |
[email protected] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 137 | // static |
avi | 2606292 | 2015-12-26 00:14:18 | [diff] [blame] | 138 | uint64_t MetricsLog::Hash(const std::string& value) { |
| 139 | uint64_t hash = base::HashMetricName(value); |
[email protected] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 140 | |
| 141 | // The following log is VERY helpful when folks add some named histogram into |
| 142 | // the code, but forgot to update the descriptive list of histograms. When |
| 143 | // that happens, all we get to see (server side) is a hash of the histogram |
| 144 | // name. We can then use this logging to find out what histogram name was |
| 145 | // being hashed to a given MD5 value by just running the version of Chromium |
| 146 | // in question with --enable-logging. |
| 147 | DVLOG(1) << "Metrics: Hash numeric [" << value << "]=[" << hash << "]"; |
| 148 | |
| 149 | return hash; |
| 150 | } |
| 151 | |
| 152 | // static |
avi | 2606292 | 2015-12-26 00:14:18 | [diff] [blame] | 153 | int64_t MetricsLog::GetBuildTime() { |
| 154 | static int64_t integral_build_time = 0; |
sebmarchand | 2f4ed50 | 2014-10-31 15:28:19 | [diff] [blame] | 155 | if (!integral_build_time) |
avi | 2606292 | 2015-12-26 00:14:18 | [diff] [blame] | 156 | integral_build_time = static_cast<int64_t>(base::GetBuildTime().ToTimeT()); |
[email protected] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 157 | return integral_build_time; |
| 158 | } |
| 159 | |
| 160 | // static |
avi | 2606292 | 2015-12-26 00:14:18 | [diff] [blame] | 161 | int64_t MetricsLog::GetCurrentTime() { |
[email protected] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 162 | return (base::TimeTicks::Now() - base::TimeTicks()).InSeconds(); |
| 163 | } |
| 164 | |
| 165 | void MetricsLog::RecordUserAction(const std::string& key) { |
| 166 | DCHECK(!closed_); |
| 167 | |
| 168 | UserActionEventProto* user_action = uma_proto_.add_user_action_event(); |
| 169 | user_action->set_name_hash(Hash(key)); |
| 170 | user_action->set_time(GetCurrentTime()); |
| 171 | } |
| 172 | |
| 173 | void MetricsLog::RecordHistogramDelta(const std::string& histogram_name, |
| 174 | const base::HistogramSamples& snapshot) { |
| 175 | DCHECK(!closed_); |
rtenneti | 7254696 | 2014-12-15 21:41:59 | [diff] [blame] | 176 | EncodeHistogramDelta(histogram_name, snapshot, &uma_proto_); |
[email protected] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 177 | } |
| 178 | |
[email protected] | 85791b0b | 2014-05-20 15:18:58 | [diff] [blame] | 179 | void MetricsLog::RecordStabilityMetrics( |
asvitkine | 4c1d1ef | 2014-09-29 20:57:32 | [diff] [blame] | 180 | const std::vector<MetricsProvider*>& metrics_providers, |
[email protected] | 85791b0b | 2014-05-20 15:18:58 | [diff] [blame] | 181 | base::TimeDelta incremental_uptime, |
| 182 | base::TimeDelta uptime) { |
[email protected] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 183 | DCHECK(!closed_); |
[email protected] | 0f2f779 | 2013-11-28 16:09:14 | [diff] [blame] | 184 | DCHECK(HasEnvironment()); |
| 185 | DCHECK(!HasStabilityMetrics()); |
[email protected] | 0b33f80b | 2008-12-17 21:34:36 | [diff] [blame] | 186 | |
[email protected] | 24f81ca | 2014-05-26 15:59:34 | [diff] [blame] | 187 | PrefService* pref = local_state_; |
[email protected] | 0b33f80b | 2008-12-17 21:34:36 | [diff] [blame] | 188 | DCHECK(pref); |
| 189 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 190 | // Get stability attributes out of Local State, zeroing out stored values. |
| 191 | // NOTE: This could lead to some data loss if this report isn't successfully |
| 192 | // sent, but that's true for all the metrics. |
| 193 | |
[email protected] | 147bbc0b | 2009-01-06 19:37:40 | [diff] [blame] | 194 | WriteRequiredStabilityAttributes(pref); |
[email protected] | 0edf876 | 2013-11-21 18:33:30 | [diff] [blame] | 195 | |
| 196 | // Record recent delta for critical stability metrics. We can't wait for a |
| 197 | // restart to gather these, as that delay biases our observation away from |
| 198 | // users that run happily for a looooong time. We send increments with each |
| 199 | // uma log upload, just as we send histogram data. |
[email protected] | 076961c | 2014-03-12 22:23:56 | [diff] [blame] | 200 | WriteRealtimeStabilityAttributes(pref, incremental_uptime, uptime); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 201 | |
[email protected] | 48ff2c7f | 2014-05-23 09:57:45 | [diff] [blame] | 202 | SystemProfileProto* system_profile = uma_proto()->mutable_system_profile(); |
lpromero | ca8cb6f | 2015-04-30 18:16:53 | [diff] [blame] | 203 | for (size_t i = 0; i < metrics_providers.size(); ++i) { |
| 204 | if (log_type() == INITIAL_STABILITY_LOG) |
| 205 | metrics_providers[i]->ProvideInitialStabilityMetrics(system_profile); |
[email protected] | 48ff2c7f | 2014-05-23 09:57:45 | [diff] [blame] | 206 | metrics_providers[i]->ProvideStabilityMetrics(system_profile); |
lpromero | ca8cb6f | 2015-04-30 18:16:53 | [diff] [blame] | 207 | } |
[email protected] | 85791b0b | 2014-05-20 15:18:58 | [diff] [blame] | 208 | |
manzagop | 5415700 | 2016-09-01 02:43:59 | [diff] [blame] | 209 | SystemProfileProto::Stability* stability = |
| 210 | system_profile->mutable_stability(); |
[email protected] | 0edf876 | 2013-11-21 18:33:30 | [diff] [blame] | 211 | |
[email protected] | fe58acc2 | 2012-02-29 01:29:58 | [diff] [blame] | 212 | int incomplete_shutdown_count = |
asvitkine | 4c1d1ef | 2014-09-29 20:57:32 | [diff] [blame] | 213 | pref->GetInteger(prefs::kStabilityIncompleteSessionEndCount); |
manzagop | 5415700 | 2016-09-01 02:43:59 | [diff] [blame] | 214 | if (incomplete_shutdown_count) { |
| 215 | pref->SetInteger(prefs::kStabilityIncompleteSessionEndCount, 0); |
| 216 | stability->set_incomplete_shutdown_count(incomplete_shutdown_count); |
| 217 | } |
| 218 | |
[email protected] | fe58acc2 | 2012-02-29 01:29:58 | [diff] [blame] | 219 | int breakpad_registration_success_count = |
asvitkine | 4c1d1ef | 2014-09-29 20:57:32 | [diff] [blame] | 220 | pref->GetInteger(prefs::kStabilityBreakpadRegistrationSuccess); |
manzagop | 5415700 | 2016-09-01 02:43:59 | [diff] [blame] | 221 | if (breakpad_registration_success_count) { |
| 222 | pref->SetInteger(prefs::kStabilityBreakpadRegistrationSuccess, 0); |
| 223 | stability->set_breakpad_registration_success_count( |
| 224 | breakpad_registration_success_count); |
| 225 | } |
| 226 | |
[email protected] | fe58acc2 | 2012-02-29 01:29:58 | [diff] [blame] | 227 | int breakpad_registration_failure_count = |
asvitkine | 4c1d1ef | 2014-09-29 20:57:32 | [diff] [blame] | 228 | pref->GetInteger(prefs::kStabilityBreakpadRegistrationFail); |
manzagop | 5415700 | 2016-09-01 02:43:59 | [diff] [blame] | 229 | if (breakpad_registration_failure_count) { |
| 230 | pref->SetInteger(prefs::kStabilityBreakpadRegistrationFail, 0); |
| 231 | stability->set_breakpad_registration_failure_count( |
| 232 | breakpad_registration_failure_count); |
| 233 | } |
| 234 | |
[email protected] | fe58acc2 | 2012-02-29 01:29:58 | [diff] [blame] | 235 | int debugger_present_count = |
asvitkine | 4c1d1ef | 2014-09-29 20:57:32 | [diff] [blame] | 236 | pref->GetInteger(prefs::kStabilityDebuggerPresent); |
manzagop | 5415700 | 2016-09-01 02:43:59 | [diff] [blame] | 237 | if (debugger_present_count) { |
| 238 | pref->SetInteger(prefs::kStabilityDebuggerPresent, 0); |
| 239 | stability->set_debugger_present_count(debugger_present_count); |
| 240 | } |
| 241 | |
[email protected] | fe58acc2 | 2012-02-29 01:29:58 | [diff] [blame] | 242 | int debugger_not_present_count = |
asvitkine | 4c1d1ef | 2014-09-29 20:57:32 | [diff] [blame] | 243 | pref->GetInteger(prefs::kStabilityDebuggerNotPresent); |
manzagop | 5415700 | 2016-09-01 02:43:59 | [diff] [blame] | 244 | if (debugger_not_present_count) { |
| 245 | pref->SetInteger(prefs::kStabilityDebuggerNotPresent, 0); |
| 246 | stability->set_debugger_not_present_count(debugger_not_present_count); |
| 247 | } |
manzagop | ea99d195 | 2016-09-08 23:40:05 | [diff] [blame^] | 248 | |
| 249 | // Note: only logging the following histograms for non-zero values. |
| 250 | |
| 251 | int deferred_count = pref->GetInteger(prefs::kStabilityDeferredCount); |
| 252 | if (deferred_count) { |
| 253 | local_state_->SetInteger(prefs::kStabilityDeferredCount, 0); |
| 254 | UMA_STABILITY_HISTOGRAM_COUNTS_100( |
| 255 | "Stability.Internals.InitialStabilityLogDeferredCount", deferred_count); |
| 256 | } |
| 257 | |
| 258 | int discard_count = local_state_->GetInteger(prefs::kStabilityDiscardCount); |
| 259 | if (discard_count) { |
| 260 | local_state_->SetInteger(prefs::kStabilityDiscardCount, 0); |
| 261 | UMA_STABILITY_HISTOGRAM_COUNTS_100("Stability.Internals.DataDiscardCount", |
| 262 | discard_count); |
| 263 | } |
| 264 | |
| 265 | int version_mismatch_count = |
| 266 | local_state_->GetInteger(prefs::kStabilityVersionMismatchCount); |
| 267 | if (version_mismatch_count) { |
| 268 | local_state_->SetInteger(prefs::kStabilityVersionMismatchCount, 0); |
| 269 | UMA_STABILITY_HISTOGRAM_COUNTS_100( |
| 270 | "Stability.Internals.VersionMismatchCount", |
| 271 | version_mismatch_count); |
| 272 | } |
[email protected] | 0edf876 | 2013-11-21 18:33:30 | [diff] [blame] | 273 | } |
[email protected] | fe58acc2 | 2012-02-29 01:29:58 | [diff] [blame] | 274 | |
[email protected] | 85791b0b | 2014-05-20 15:18:58 | [diff] [blame] | 275 | void MetricsLog::RecordGeneralMetrics( |
asvitkine | 4c1d1ef | 2014-09-29 20:57:32 | [diff] [blame] | 276 | const std::vector<MetricsProvider*>& metrics_providers) { |
[email protected] | 85791b0b | 2014-05-20 15:18:58 | [diff] [blame] | 277 | for (size_t i = 0; i < metrics_providers.size(); ++i) |
| 278 | metrics_providers[i]->ProvideGeneralMetrics(uma_proto()); |
| 279 | } |
| 280 | |
[email protected] | 0edf876 | 2013-11-21 18:33:30 | [diff] [blame] | 281 | void MetricsLog::GetFieldTrialIds( |
| 282 | std::vector<ActiveGroupId>* field_trial_ids) const { |
[email protected] | b3610d4 | 2014-05-19 18:07:23 | [diff] [blame] | 283 | variations::GetFieldTrialActiveGroupIds(field_trial_ids); |
[email protected] | 147bbc0b | 2009-01-06 19:37:40 | [diff] [blame] | 284 | } |
| 285 | |
[email protected] | 0f2f779 | 2013-11-28 16:09:14 | [diff] [blame] | 286 | bool MetricsLog::HasEnvironment() const { |
| 287 | return uma_proto()->system_profile().has_uma_enabled_date(); |
| 288 | } |
| 289 | |
gayane | daaf3a0 | 2016-06-15 16:30:21 | [diff] [blame] | 290 | void MetricsLog::WriteMetricsEnableDefault(EnableMetricsDefault metrics_default, |
| 291 | SystemProfileProto* system_profile) { |
jwd | 421086f | 2016-03-21 14:40:42 | [diff] [blame] | 292 | if (client_->IsReportingPolicyManaged()) { |
| 293 | // If it's managed, then it must be reporting, otherwise we wouldn't be |
| 294 | // sending metrics. |
| 295 | system_profile->set_uma_default_state( |
| 296 | SystemProfileProto_UmaDefaultState_POLICY_FORCED_ENABLED); |
| 297 | return; |
| 298 | } |
| 299 | |
| 300 | switch (metrics_default) { |
gayane | daaf3a0 | 2016-06-15 16:30:21 | [diff] [blame] | 301 | case EnableMetricsDefault::DEFAULT_UNKNOWN: |
jwd | 421086f | 2016-03-21 14:40:42 | [diff] [blame] | 302 | // Don't set the field if it's unknown. |
| 303 | break; |
gayane | daaf3a0 | 2016-06-15 16:30:21 | [diff] [blame] | 304 | case EnableMetricsDefault::OPT_IN: |
jwd | 421086f | 2016-03-21 14:40:42 | [diff] [blame] | 305 | system_profile->set_uma_default_state( |
| 306 | SystemProfileProto_UmaDefaultState_OPT_IN); |
| 307 | break; |
gayane | daaf3a0 | 2016-06-15 16:30:21 | [diff] [blame] | 308 | case EnableMetricsDefault::OPT_OUT: |
jwd | 421086f | 2016-03-21 14:40:42 | [diff] [blame] | 309 | system_profile->set_uma_default_state( |
| 310 | SystemProfileProto_UmaDefaultState_OPT_OUT); |
| 311 | } |
| 312 | } |
| 313 | |
[email protected] | 0f2f779 | 2013-11-28 16:09:14 | [diff] [blame] | 314 | bool MetricsLog::HasStabilityMetrics() const { |
| 315 | return uma_proto()->system_profile().stability().has_launch_count(); |
| 316 | } |
| 317 | |
[email protected] | fe58acc2 | 2012-02-29 01:29:58 | [diff] [blame] | 318 | // The server refuses data that doesn't have certain values. crashcount and |
| 319 | // launchcount are currently "required" in the "stability" group. |
| 320 | // TODO(isherman): Stop writing these attributes specially once the migration to |
| 321 | // protobufs is complete. |
[email protected] | 147bbc0b | 2009-01-06 19:37:40 | [diff] [blame] | 322 | void MetricsLog::WriteRequiredStabilityAttributes(PrefService* pref) { |
asvitkine | 4c1d1ef | 2014-09-29 20:57:32 | [diff] [blame] | 323 | int launch_count = pref->GetInteger(prefs::kStabilityLaunchCount); |
manzagop | 5415700 | 2016-09-01 02:43:59 | [diff] [blame] | 324 | if (launch_count) |
| 325 | pref->SetInteger(prefs::kStabilityLaunchCount, 0); |
asvitkine | 4c1d1ef | 2014-09-29 20:57:32 | [diff] [blame] | 326 | int crash_count = pref->GetInteger(prefs::kStabilityCrashCount); |
manzagop | 5415700 | 2016-09-01 02:43:59 | [diff] [blame] | 327 | if (crash_count) |
| 328 | pref->SetInteger(prefs::kStabilityCrashCount, 0); |
[email protected] | fe58acc2 | 2012-02-29 01:29:58 | [diff] [blame] | 329 | |
[email protected] | fe58acc2 | 2012-02-29 01:29:58 | [diff] [blame] | 330 | SystemProfileProto::Stability* stability = |
[email protected] | 767c9d9 | 2012-03-02 16:04:34 | [diff] [blame] | 331 | uma_proto()->mutable_system_profile()->mutable_stability(); |
[email protected] | fe58acc2 | 2012-02-29 01:29:58 | [diff] [blame] | 332 | stability->set_launch_count(launch_count); |
| 333 | stability->set_crash_count(crash_count); |
[email protected] | 0b33f80b | 2008-12-17 21:34:36 | [diff] [blame] | 334 | } |
| 335 | |
[email protected] | c68a2b9b | 2013-10-09 18:16:36 | [diff] [blame] | 336 | void MetricsLog::WriteRealtimeStabilityAttributes( |
| 337 | PrefService* pref, |
[email protected] | 076961c | 2014-03-12 22:23:56 | [diff] [blame] | 338 | base::TimeDelta incremental_uptime, |
| 339 | base::TimeDelta uptime) { |
[email protected] | 0b33f80b | 2008-12-17 21:34:36 | [diff] [blame] | 340 | // Update the stats which are critical for real-time stability monitoring. |
| 341 | // Since these are "optional," only list ones that are non-zero, as the counts |
[email protected] | 250f7b66 | 2013-11-23 02:36:51 | [diff] [blame] | 342 | // are aggregated (summed) server side. |
[email protected] | 0b33f80b | 2008-12-17 21:34:36 | [diff] [blame] | 343 | |
[email protected] | fe58acc2 | 2012-02-29 01:29:58 | [diff] [blame] | 344 | SystemProfileProto::Stability* stability = |
[email protected] | 767c9d9 | 2012-03-02 16:04:34 | [diff] [blame] | 345 | uma_proto()->mutable_system_profile()->mutable_stability(); |
[email protected] | 0b33f80b | 2008-12-17 21:34:36 | [diff] [blame] | 346 | |
avi | 2606292 | 2015-12-26 00:14:18 | [diff] [blame] | 347 | const uint64_t incremental_uptime_sec = incremental_uptime.InSeconds(); |
[email protected] | 076961c | 2014-03-12 22:23:56 | [diff] [blame] | 348 | if (incremental_uptime_sec) |
| 349 | stability->set_incremental_uptime_sec(incremental_uptime_sec); |
avi | 2606292 | 2015-12-26 00:14:18 | [diff] [blame] | 350 | const uint64_t uptime_sec = uptime.InSeconds(); |
[email protected] | c68a2b9b | 2013-10-09 18:16:36 | [diff] [blame] | 351 | if (uptime_sec) |
| 352 | stability->set_uptime_sec(uptime_sec); |
[email protected] | 0b33f80b | 2008-12-17 21:34:36 | [diff] [blame] | 353 | } |
| 354 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 355 | void MetricsLog::RecordEnvironment( |
asvitkine | 4c1d1ef | 2014-09-29 20:57:32 | [diff] [blame] | 356 | const std::vector<MetricsProvider*>& metrics_providers, |
[email protected] | 6580145 | 2014-07-09 05:42:41 | [diff] [blame] | 357 | const std::vector<variations::ActiveGroupId>& synthetic_trials, |
avi | 2606292 | 2015-12-26 00:14:18 | [diff] [blame] | 358 | int64_t install_date, |
| 359 | int64_t metrics_reporting_enabled_date) { |
[email protected] | 0f2f779 | 2013-11-28 16:09:14 | [diff] [blame] | 360 | DCHECK(!HasEnvironment()); |
| 361 | |
[email protected] | bc66d53 | 2012-03-23 01:57:05 | [diff] [blame] | 362 | SystemProfileProto* system_profile = uma_proto()->mutable_system_profile(); |
[email protected] | 24b9bb39 | 2013-01-29 20:29:29 | [diff] [blame] | 363 | |
gayane | daaf3a0 | 2016-06-15 16:30:21 | [diff] [blame] | 364 | WriteMetricsEnableDefault(client_->GetMetricsReportingDefaultState(), |
| 365 | system_profile); |
jwd | 421086f | 2016-03-21 14:40:42 | [diff] [blame] | 366 | |
[email protected] | 24b9bb39 | 2013-01-29 20:29:29 | [diff] [blame] | 367 | std::string brand_code; |
[email protected] | 09dee82d | 2014-05-22 14:00:53 | [diff] [blame] | 368 | if (client_->GetBrand(&brand_code)) |
[email protected] | 24b9bb39 | 2013-01-29 20:29:29 | [diff] [blame] | 369 | system_profile->set_brand_code(brand_code); |
| 370 | |
[email protected] | 86573d1 | 2013-07-11 19:48:32 | [diff] [blame] | 371 | // Reduce granularity of the enabled_date field to nearest hour. |
olivierrobin | c3dfc5b | 2015-04-07 19:12:00 | [diff] [blame] | 372 | system_profile->set_uma_enabled_date( |
| 373 | RoundSecondsToHour(metrics_reporting_enabled_date)); |
[email protected] | 86573d1 | 2013-07-11 19:48:32 | [diff] [blame] | 374 | |
[email protected] | 86573d1 | 2013-07-11 19:48:32 | [diff] [blame] | 375 | // Reduce granularity of the install_date field to nearest hour. |
| 376 | system_profile->set_install_date(RoundSecondsToHour(install_date)); |
[email protected] | bc66d53 | 2012-03-23 01:57:05 | [diff] [blame] | 377 | |
[email protected] | 09dee82d | 2014-05-22 14:00:53 | [diff] [blame] | 378 | system_profile->set_application_locale(client_->GetApplicationLocale()); |
[email protected] | bc66d53 | 2012-03-23 01:57:05 | [diff] [blame] | 379 | |
| 380 | SystemProfileProto::Hardware* hardware = system_profile->mutable_hardware(); |
[email protected] | 51994b2 | 2014-05-30 13:24:21 | [diff] [blame] | 381 | |
jeremy | ba4eca9 | 2014-11-06 18:47:10 | [diff] [blame] | 382 | // HardwareModelName() will return an empty string on platforms where it's |
| 383 | // not implemented or if an error occured. |
| 384 | hardware->set_hardware_class(base::SysInfo::HardwareModelName()); |
[email protected] | 51994b2 | 2014-05-30 13:24:21 | [diff] [blame] | 385 | |
[email protected] | 0b6a4fb | 2012-10-16 01:58:21 | [diff] [blame] | 386 | hardware->set_cpu_architecture(base::SysInfo::OperatingSystemArchitecture()); |
[email protected] | bc66d53 | 2012-03-23 01:57:05 | [diff] [blame] | 387 | hardware->set_system_ram_mb(base::SysInfo::AmountOfPhysicalMemoryMB()); |
| 388 | #if defined(OS_WIN) |
thakis | d62f5447 | 2016-04-04 02:21:10 | [diff] [blame] | 389 | hardware->set_dll_base(reinterpret_cast<uint64_t>(CURRENT_MODULE())); |
[email protected] | bc66d53 | 2012-03-23 01:57:05 | [diff] [blame] | 390 | #endif |
| 391 | |
jessicag | d599415 | 2016-05-20 21:27:31 | [diff] [blame] | 392 | SystemProfileProto::OS* os = system_profile->mutable_os(); |
jessicag | ab7a08b | 2016-03-11 22:34:49 | [diff] [blame] | 393 | #if defined(OVERRIDE_OS_NAME_TO_BLIMP) |
| 394 | os->set_name("Blimp"); |
| 395 | #else |
[email protected] | 1bb25e0 | 2012-08-03 22:39:39 | [diff] [blame] | 396 | std::string os_name = base::SysInfo::OperatingSystemName(); |
[email protected] | 1bb25e0 | 2012-08-03 22:39:39 | [diff] [blame] | 397 | os->set_name(os_name); |
jessicag | ab7a08b | 2016-03-11 22:34:49 | [diff] [blame] | 398 | #endif |
| 399 | |
[email protected] | bc66d53 | 2012-03-23 01:57:05 | [diff] [blame] | 400 | os->set_version(base::SysInfo::OperatingSystemVersion()); |
[email protected] | 5106b3a | 2012-10-03 20:10:44 | [diff] [blame] | 401 | #if defined(OS_ANDROID) |
| 402 | os->set_fingerprint( |
| 403 | base::android::BuildInfo::GetInstance()->android_build_fp()); |
| 404 | #endif |
[email protected] | bc66d53 | 2012-03-23 01:57:05 | [diff] [blame] | 405 | |
[email protected] | 5c8f89f69 | 2013-07-18 11:13:28 | [diff] [blame] | 406 | base::CPU cpu_info; |
| 407 | SystemProfileProto::Hardware::CPU* cpu = hardware->mutable_cpu(); |
| 408 | cpu->set_vendor_name(cpu_info.vendor_name()); |
| 409 | cpu->set_signature(cpu_info.signature()); |
gayane | f550f50f | 2015-03-11 00:44:59 | [diff] [blame] | 410 | cpu->set_num_cores(base::SysInfo::NumberOfProcessors()); |
[email protected] | 5c8f89f69 | 2013-07-18 11:13:28 | [diff] [blame] | 411 | |
[email protected] | 0c8b7ad | 2012-11-06 07:08:14 | [diff] [blame] | 412 | std::vector<ActiveGroupId> field_trial_ids; |
[email protected] | 767c9d9 | 2012-03-02 16:04:34 | [diff] [blame] | 413 | GetFieldTrialIds(&field_trial_ids); |
| 414 | WriteFieldTrials(field_trial_ids, system_profile); |
[email protected] | 6067756 | 2013-11-17 15:52:55 | [diff] [blame] | 415 | WriteFieldTrials(synthetic_trials, system_profile); |
[email protected] | f65859e | 2013-02-04 20:00:25 | [diff] [blame] | 416 | |
[email protected] | 85791b0b | 2014-05-20 15:18:58 | [diff] [blame] | 417 | for (size_t i = 0; i < metrics_providers.size(); ++i) |
| 418 | metrics_providers[i]->ProvideSystemProfileMetrics(system_profile); |
| 419 | |
manzagop | 5415700 | 2016-09-01 02:43:59 | [diff] [blame] | 420 | std::string serialized_system_profile; |
[email protected] | 0f2f779 | 2013-11-28 16:09:14 | [diff] [blame] | 421 | std::string base64_system_profile; |
manzagop | 5415700 | 2016-09-01 02:43:59 | [diff] [blame] | 422 | if (system_profile->SerializeToString(&serialized_system_profile)) { |
| 423 | base::Base64Encode(serialized_system_profile, &base64_system_profile); |
[email protected] | 24f81ca | 2014-05-26 15:59:34 | [diff] [blame] | 424 | PrefService* local_state = local_state_; |
asvitkine | 4c1d1ef | 2014-09-29 20:57:32 | [diff] [blame] | 425 | local_state->SetString(prefs::kStabilitySavedSystemProfile, |
[email protected] | 0f2f779 | 2013-11-28 16:09:14 | [diff] [blame] | 426 | base64_system_profile); |
asvitkine | 4c1d1ef | 2014-09-29 20:57:32 | [diff] [blame] | 427 | local_state->SetString(prefs::kStabilitySavedSystemProfileHash, |
manzagop | 5415700 | 2016-09-01 02:43:59 | [diff] [blame] | 428 | ComputeSHA1(serialized_system_profile)); |
[email protected] | 0f2f779 | 2013-11-28 16:09:14 | [diff] [blame] | 429 | } |
| 430 | } |
| 431 | |
manzagop | ea99d195 | 2016-09-08 23:40:05 | [diff] [blame^] | 432 | bool MetricsLog::LoadSavedEnvironmentFromPrefs(std::string* app_version) { |
| 433 | DCHECK(app_version); |
| 434 | app_version->clear(); |
| 435 | |
[email protected] | 24f81ca | 2014-05-26 15:59:34 | [diff] [blame] | 436 | PrefService* local_state = local_state_; |
[email protected] | 0f2f779 | 2013-11-28 16:09:14 | [diff] [blame] | 437 | const std::string base64_system_profile = |
asvitkine | 4c1d1ef | 2014-09-29 20:57:32 | [diff] [blame] | 438 | local_state->GetString(prefs::kStabilitySavedSystemProfile); |
[email protected] | 0f2f779 | 2013-11-28 16:09:14 | [diff] [blame] | 439 | if (base64_system_profile.empty()) |
| 440 | return false; |
| 441 | |
| 442 | const std::string system_profile_hash = |
asvitkine | 4c1d1ef | 2014-09-29 20:57:32 | [diff] [blame] | 443 | local_state->GetString(prefs::kStabilitySavedSystemProfileHash); |
| 444 | local_state->ClearPref(prefs::kStabilitySavedSystemProfile); |
| 445 | local_state->ClearPref(prefs::kStabilitySavedSystemProfileHash); |
[email protected] | 0f2f779 | 2013-11-28 16:09:14 | [diff] [blame] | 446 | |
| 447 | SystemProfileProto* system_profile = uma_proto()->mutable_system_profile(); |
manzagop | 5415700 | 2016-09-01 02:43:59 | [diff] [blame] | 448 | std::string serialized_system_profile; |
manzagop | ea99d195 | 2016-09-08 23:40:05 | [diff] [blame^] | 449 | |
| 450 | bool success = |
| 451 | base::Base64Decode(base64_system_profile, &serialized_system_profile) && |
| 452 | ComputeSHA1(serialized_system_profile) == system_profile_hash && |
| 453 | system_profile->ParseFromString(serialized_system_profile); |
| 454 | if (success) |
| 455 | *app_version = system_profile->app_version(); |
| 456 | return success; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 457 | } |
| 458 | |
[email protected] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 459 | void MetricsLog::CloseLog() { |
| 460 | DCHECK(!closed_); |
| 461 | closed_ = true; |
| 462 | } |
| 463 | |
| 464 | void MetricsLog::GetEncodedLog(std::string* encoded_log) { |
| 465 | DCHECK(closed_); |
| 466 | uma_proto_.SerializeToString(encoded_log); |
| 467 | } |
asvitkine | cbd42073 | 2014-08-26 22:15:40 | [diff] [blame] | 468 | |
| 469 | } // namespace metrics |