[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 | |
[email protected] | 76869ff | 2013-01-15 16:13:47 | [diff] [blame] | 7 | #include <algorithm> |
[email protected] | 1eeb5e0 | 2010-07-20 23:02:11 | [diff] [blame] | 8 | #include <string> |
| 9 | #include <vector> |
| 10 | |
[email protected] | 0f2f779 | 2013-11-28 16:09:14 | [diff] [blame] | 11 | #include "base/base64.h" |
[email protected] | d1be67b | 2008-11-19 20:28:38 | [diff] [blame] | 12 | #include "base/basictypes.h" |
sebmarchand | 2f4ed50 | 2014-10-31 15:28:19 | [diff] [blame] | 13 | #include "base/build_time.h" |
[email protected] | 5c8f89f69 | 2013-07-18 11:13:28 | [diff] [blame] | 14 | #include "base/cpu.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 15 | #include "base/memory/scoped_ptr.h" |
[email protected] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 16 | #include "base/metrics/histogram.h" |
| 17 | #include "base/metrics/histogram_samples.h" |
[email protected] | 3853a4c | 2013-02-11 17:15:57 | [diff] [blame] | 18 | #include "base/prefs/pref_registry_simple.h" |
| 19 | #include "base/prefs/pref_service.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" |
[email protected] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 26 | #include "components/metrics/metrics_hashes.h" |
[email protected] | 91b1d91 | 2014-06-05 10:52:08 | [diff] [blame] | 27 | #include "components/metrics/metrics_pref_names.h" |
[email protected] | 85791b0b | 2014-05-20 15:18:58 | [diff] [blame] | 28 | #include "components/metrics/metrics_provider.h" |
[email protected] | 09dee82d | 2014-05-22 14:00:53 | [diff] [blame] | 29 | #include "components/metrics/metrics_service_client.h" |
[email protected] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 30 | #include "components/metrics/proto/histogram_event.pb.h" |
[email protected] | 064107e | 2014-05-02 00:59:06 | [diff] [blame] | 31 | #include "components/metrics/proto/system_profile.pb.h" |
[email protected] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 32 | #include "components/metrics/proto/user_action_event.pb.h" |
[email protected] | b3610d4 | 2014-05-19 18:07:23 | [diff] [blame] | 33 | #include "components/variations/active_field_trials.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 34 | |
[email protected] | 5106b3a | 2012-10-03 20:10:44 | [diff] [blame] | 35 | #if defined(OS_ANDROID) |
| 36 | #include "base/android/build_info.h" |
| 37 | #endif |
| 38 | |
[email protected] | d1be67b | 2008-11-19 20:28:38 | [diff] [blame] | 39 | #if defined(OS_WIN) |
[email protected] | 1bb25e0 | 2012-08-03 22:39:39 | [diff] [blame] | 40 | #include "base/win/metro.h" |
| 41 | |
| 42 | // http://blogs.msdn.com/oldnewthing/archive/2004/10/25/247180.aspx |
[email protected] | d1be67b | 2008-11-19 20:28:38 | [diff] [blame] | 43 | extern "C" IMAGE_DOS_HEADER __ImageBase; |
| 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] | 1df44b7 | 2012-01-19 05:20:34 | [diff] [blame] | 58 | // Returns the date at which the current metrics client ID was created as |
[email protected] | ca5ac4b | 2012-12-14 07:46:40 | [diff] [blame] | 59 | // a string containing seconds since the epoch, or "0" if none was found. |
[email protected] | cc5d7f4 | 2012-10-30 00:30:09 | [diff] [blame] | 60 | std::string GetMetricsEnabledDate(PrefService* pref) { |
[email protected] | 767c9d9 | 2012-03-02 16:04:34 | [diff] [blame] | 61 | if (!pref) { |
[email protected] | 1df44b7 | 2012-01-19 05:20:34 | [diff] [blame] | 62 | NOTREACHED(); |
| 63 | return "0"; |
| 64 | } |
[email protected] | 767c9d9 | 2012-03-02 16:04:34 | [diff] [blame] | 65 | |
asvitkine | 4c1d1ef | 2014-09-29 20:57:32 | [diff] [blame] | 66 | return pref->GetString(prefs::kMetricsReportingEnabledTimestamp); |
[email protected] | 1df44b7 | 2012-01-19 05:20:34 | [diff] [blame] | 67 | } |
| 68 | |
[email protected] | 0f2f779 | 2013-11-28 16:09:14 | [diff] [blame] | 69 | // Computes a SHA-1 hash of |data| and returns it as a hex string. |
| 70 | std::string ComputeSHA1(const std::string& data) { |
| 71 | const std::string sha1 = base::SHA1HashString(data); |
| 72 | return base::HexEncode(sha1.data(), sha1.size()); |
| 73 | } |
| 74 | |
[email protected] | 0c8b7ad | 2012-11-06 07:08:14 | [diff] [blame] | 75 | void WriteFieldTrials(const std::vector<ActiveGroupId>& field_trial_ids, |
[email protected] | 767c9d9 | 2012-03-02 16:04:34 | [diff] [blame] | 76 | SystemProfileProto* system_profile) { |
[email protected] | 0c8b7ad | 2012-11-06 07:08:14 | [diff] [blame] | 77 | for (std::vector<ActiveGroupId>::const_iterator it = |
[email protected] | ad2461c | 2012-04-27 21:11:03 | [diff] [blame] | 78 | field_trial_ids.begin(); it != field_trial_ids.end(); ++it) { |
[email protected] | 767c9d9 | 2012-03-02 16:04:34 | [diff] [blame] | 79 | SystemProfileProto::FieldTrial* field_trial = |
| 80 | system_profile->add_field_trial(); |
| 81 | field_trial->set_name_id(it->name); |
| 82 | field_trial->set_group_id(it->group); |
| 83 | } |
| 84 | } |
| 85 | |
[email protected] | 86573d1 | 2013-07-11 19:48:32 | [diff] [blame] | 86 | // Round a timestamp measured in seconds since epoch to one with a granularity |
| 87 | // of an hour. This can be used before uploaded potentially sensitive |
| 88 | // timestamps. |
| 89 | int64 RoundSecondsToHour(int64 time_in_seconds) { |
| 90 | return 3600 * (time_in_seconds / 3600); |
| 91 | } |
| 92 | |
[email protected] | 1df44b7 | 2012-01-19 05:20:34 | [diff] [blame] | 93 | } // namespace |
| 94 | |
[email protected] | 9eae403 | 2014-04-09 19:15:19 | [diff] [blame] | 95 | MetricsLog::MetricsLog(const std::string& client_id, |
| 96 | int session_id, |
[email protected] | 09dee82d | 2014-05-22 14:00:53 | [diff] [blame] | 97 | LogType log_type, |
asvitkine | 4c1d1ef | 2014-09-29 20:57:32 | [diff] [blame] | 98 | MetricsServiceClient* client, |
[email protected] | 24f81ca | 2014-05-26 15:59:34 | [diff] [blame] | 99 | PrefService* local_state) |
[email protected] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 100 | : closed_(false), |
| 101 | log_type_(log_type), |
[email protected] | 09dee82d | 2014-05-22 14:00:53 | [diff] [blame] | 102 | client_(client), |
[email protected] | 24f81ca | 2014-05-26 15:59:34 | [diff] [blame] | 103 | creation_time_(base::TimeTicks::Now()), |
| 104 | local_state_(local_state) { |
[email protected] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 105 | if (IsTestingID(client_id)) |
| 106 | uma_proto_.set_client_id(0); |
| 107 | else |
| 108 | uma_proto_.set_client_id(Hash(client_id)); |
| 109 | |
| 110 | uma_proto_.set_session_id(session_id); |
| 111 | |
asvitkine | 4c1d1ef | 2014-09-29 20:57:32 | [diff] [blame] | 112 | const int32 product = client_->GetProduct(); |
| 113 | // Only set the product if it differs from the default value. |
| 114 | if (product != uma_proto_.product()) |
| 115 | uma_proto_.set_product(product); |
| 116 | |
[email protected] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 117 | SystemProfileProto* system_profile = uma_proto_.mutable_system_profile(); |
| 118 | system_profile->set_build_timestamp(GetBuildTime()); |
| 119 | system_profile->set_app_version(client_->GetVersionString()); |
| 120 | system_profile->set_channel(client_->GetChannel()); |
[email protected] | afc03f0 | 2013-10-11 06:01:35 | [diff] [blame] | 121 | } |
[email protected] | 5ed7d457 | 2009-12-23 17:42:41 | [diff] [blame] | 122 | |
[email protected] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 123 | MetricsLog::~MetricsLog() { |
| 124 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 125 | |
[email protected] | 91b1d91 | 2014-06-05 10:52:08 | [diff] [blame] | 126 | // static |
| 127 | void MetricsLog::RegisterPrefs(PrefRegistrySimple* registry) { |
asvitkine | 4c1d1ef | 2014-09-29 20:57:32 | [diff] [blame] | 128 | registry->RegisterIntegerPref(prefs::kStabilityLaunchCount, 0); |
| 129 | registry->RegisterIntegerPref(prefs::kStabilityCrashCount, 0); |
| 130 | registry->RegisterIntegerPref(prefs::kStabilityIncompleteSessionEndCount, 0); |
| 131 | registry->RegisterIntegerPref(prefs::kStabilityBreakpadRegistrationFail, 0); |
[email protected] | 91b1d91 | 2014-06-05 10:52:08 | [diff] [blame] | 132 | registry->RegisterIntegerPref( |
asvitkine | 4c1d1ef | 2014-09-29 20:57:32 | [diff] [blame] | 133 | prefs::kStabilityBreakpadRegistrationSuccess, 0); |
| 134 | registry->RegisterIntegerPref(prefs::kStabilityDebuggerPresent, 0); |
| 135 | registry->RegisterIntegerPref(prefs::kStabilityDebuggerNotPresent, 0); |
| 136 | registry->RegisterStringPref(prefs::kStabilitySavedSystemProfile, |
[email protected] | 91b1d91 | 2014-06-05 10:52:08 | [diff] [blame] | 137 | std::string()); |
asvitkine | 4c1d1ef | 2014-09-29 20:57:32 | [diff] [blame] | 138 | registry->RegisterStringPref(prefs::kStabilitySavedSystemProfileHash, |
[email protected] | 91b1d91 | 2014-06-05 10:52:08 | [diff] [blame] | 139 | std::string()); |
| 140 | } |
| 141 | |
[email protected] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 142 | // static |
| 143 | uint64 MetricsLog::Hash(const std::string& value) { |
asvitkine | 4c1d1ef | 2014-09-29 20:57:32 | [diff] [blame] | 144 | uint64 hash = HashMetricName(value); |
[email protected] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 145 | |
| 146 | // The following log is VERY helpful when folks add some named histogram into |
| 147 | // the code, but forgot to update the descriptive list of histograms. When |
| 148 | // that happens, all we get to see (server side) is a hash of the histogram |
| 149 | // name. We can then use this logging to find out what histogram name was |
| 150 | // being hashed to a given MD5 value by just running the version of Chromium |
| 151 | // in question with --enable-logging. |
| 152 | DVLOG(1) << "Metrics: Hash numeric [" << value << "]=[" << hash << "]"; |
| 153 | |
| 154 | return hash; |
| 155 | } |
| 156 | |
| 157 | // static |
| 158 | int64 MetricsLog::GetBuildTime() { |
| 159 | static int64 integral_build_time = 0; |
sebmarchand | 2f4ed50 | 2014-10-31 15:28:19 | [diff] [blame] | 160 | if (!integral_build_time) |
| 161 | integral_build_time = static_cast<int64>(base::GetBuildTime().ToTimeT()); |
[email protected] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 162 | return integral_build_time; |
| 163 | } |
| 164 | |
| 165 | // static |
| 166 | int64 MetricsLog::GetCurrentTime() { |
| 167 | return (base::TimeTicks::Now() - base::TimeTicks()).InSeconds(); |
| 168 | } |
| 169 | |
| 170 | void MetricsLog::RecordUserAction(const std::string& key) { |
| 171 | DCHECK(!closed_); |
| 172 | |
| 173 | UserActionEventProto* user_action = uma_proto_.add_user_action_event(); |
| 174 | user_action->set_name_hash(Hash(key)); |
| 175 | user_action->set_time(GetCurrentTime()); |
| 176 | } |
| 177 | |
| 178 | void MetricsLog::RecordHistogramDelta(const std::string& histogram_name, |
| 179 | const base::HistogramSamples& snapshot) { |
| 180 | DCHECK(!closed_); |
| 181 | DCHECK_NE(0, snapshot.TotalCount()); |
| 182 | |
| 183 | // We will ignore the MAX_INT/infinite value in the last element of range[]. |
| 184 | |
| 185 | HistogramEventProto* histogram_proto = uma_proto_.add_histogram_event(); |
| 186 | histogram_proto->set_name_hash(Hash(histogram_name)); |
| 187 | histogram_proto->set_sum(snapshot.sum()); |
| 188 | |
| 189 | for (scoped_ptr<SampleCountIterator> it = snapshot.Iterator(); !it->Done(); |
| 190 | it->Next()) { |
| 191 | base::Histogram::Sample min; |
| 192 | base::Histogram::Sample max; |
| 193 | base::Histogram::Count count; |
| 194 | it->Get(&min, &max, &count); |
| 195 | HistogramEventProto::Bucket* bucket = histogram_proto->add_bucket(); |
| 196 | bucket->set_min(min); |
| 197 | bucket->set_max(max); |
| 198 | bucket->set_count(count); |
| 199 | } |
| 200 | |
| 201 | // Omit fields to save space (see rules in histogram_event.proto comments). |
| 202 | for (int i = 0; i < histogram_proto->bucket_size(); ++i) { |
| 203 | HistogramEventProto::Bucket* bucket = histogram_proto->mutable_bucket(i); |
| 204 | if (i + 1 < histogram_proto->bucket_size() && |
| 205 | bucket->max() == histogram_proto->bucket(i + 1).min()) { |
| 206 | bucket->clear_max(); |
| 207 | } else if (bucket->max() == bucket->min() + 1) { |
| 208 | bucket->clear_min(); |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | |
[email protected] | 85791b0b | 2014-05-20 15:18:58 | [diff] [blame] | 213 | void MetricsLog::RecordStabilityMetrics( |
asvitkine | 4c1d1ef | 2014-09-29 20:57:32 | [diff] [blame] | 214 | const std::vector<MetricsProvider*>& metrics_providers, |
[email protected] | 85791b0b | 2014-05-20 15:18:58 | [diff] [blame] | 215 | base::TimeDelta incremental_uptime, |
| 216 | base::TimeDelta uptime) { |
[email protected] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 217 | DCHECK(!closed_); |
[email protected] | 0f2f779 | 2013-11-28 16:09:14 | [diff] [blame] | 218 | DCHECK(HasEnvironment()); |
| 219 | DCHECK(!HasStabilityMetrics()); |
[email protected] | 0b33f80b | 2008-12-17 21:34:36 | [diff] [blame] | 220 | |
[email protected] | 24f81ca | 2014-05-26 15:59:34 | [diff] [blame] | 221 | PrefService* pref = local_state_; |
[email protected] | 0b33f80b | 2008-12-17 21:34:36 | [diff] [blame] | 222 | DCHECK(pref); |
| 223 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 224 | // Get stability attributes out of Local State, zeroing out stored values. |
| 225 | // NOTE: This could lead to some data loss if this report isn't successfully |
| 226 | // sent, but that's true for all the metrics. |
| 227 | |
[email protected] | 147bbc0b | 2009-01-06 19:37:40 | [diff] [blame] | 228 | WriteRequiredStabilityAttributes(pref); |
[email protected] | 0edf876 | 2013-11-21 18:33:30 | [diff] [blame] | 229 | |
| 230 | // Record recent delta for critical stability metrics. We can't wait for a |
| 231 | // restart to gather these, as that delay biases our observation away from |
| 232 | // users that run happily for a looooong time. We send increments with each |
| 233 | // uma log upload, just as we send histogram data. |
[email protected] | 076961c | 2014-03-12 22:23:56 | [diff] [blame] | 234 | WriteRealtimeStabilityAttributes(pref, incremental_uptime, uptime); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 235 | |
[email protected] | 48ff2c7f | 2014-05-23 09:57:45 | [diff] [blame] | 236 | SystemProfileProto* system_profile = uma_proto()->mutable_system_profile(); |
[email protected] | 85791b0b | 2014-05-20 15:18:58 | [diff] [blame] | 237 | for (size_t i = 0; i < metrics_providers.size(); ++i) |
[email protected] | 48ff2c7f | 2014-05-23 09:57:45 | [diff] [blame] | 238 | metrics_providers[i]->ProvideStabilityMetrics(system_profile); |
[email protected] | 85791b0b | 2014-05-20 15:18:58 | [diff] [blame] | 239 | |
[email protected] | 0edf876 | 2013-11-21 18:33:30 | [diff] [blame] | 240 | // Omit some stats unless this is the initial stability log. |
[email protected] | 9eae403 | 2014-04-09 19:15:19 | [diff] [blame] | 241 | if (log_type() != INITIAL_STABILITY_LOG) |
[email protected] | 0edf876 | 2013-11-21 18:33:30 | [diff] [blame] | 242 | return; |
| 243 | |
[email protected] | fe58acc2 | 2012-02-29 01:29:58 | [diff] [blame] | 244 | int incomplete_shutdown_count = |
asvitkine | 4c1d1ef | 2014-09-29 20:57:32 | [diff] [blame] | 245 | pref->GetInteger(prefs::kStabilityIncompleteSessionEndCount); |
| 246 | pref->SetInteger(prefs::kStabilityIncompleteSessionEndCount, 0); |
[email protected] | fe58acc2 | 2012-02-29 01:29:58 | [diff] [blame] | 247 | int breakpad_registration_success_count = |
asvitkine | 4c1d1ef | 2014-09-29 20:57:32 | [diff] [blame] | 248 | pref->GetInteger(prefs::kStabilityBreakpadRegistrationSuccess); |
| 249 | pref->SetInteger(prefs::kStabilityBreakpadRegistrationSuccess, 0); |
[email protected] | fe58acc2 | 2012-02-29 01:29:58 | [diff] [blame] | 250 | int breakpad_registration_failure_count = |
asvitkine | 4c1d1ef | 2014-09-29 20:57:32 | [diff] [blame] | 251 | pref->GetInteger(prefs::kStabilityBreakpadRegistrationFail); |
| 252 | pref->SetInteger(prefs::kStabilityBreakpadRegistrationFail, 0); |
[email protected] | fe58acc2 | 2012-02-29 01:29:58 | [diff] [blame] | 253 | int debugger_present_count = |
asvitkine | 4c1d1ef | 2014-09-29 20:57:32 | [diff] [blame] | 254 | pref->GetInteger(prefs::kStabilityDebuggerPresent); |
| 255 | pref->SetInteger(prefs::kStabilityDebuggerPresent, 0); |
[email protected] | fe58acc2 | 2012-02-29 01:29:58 | [diff] [blame] | 256 | int debugger_not_present_count = |
asvitkine | 4c1d1ef | 2014-09-29 20:57:32 | [diff] [blame] | 257 | pref->GetInteger(prefs::kStabilityDebuggerNotPresent); |
| 258 | pref->SetInteger(prefs::kStabilityDebuggerNotPresent, 0); |
[email protected] | b2a4812d | 2012-02-28 05:31:31 | [diff] [blame] | 259 | |
[email protected] | fe58acc2 | 2012-02-29 01:29:58 | [diff] [blame] | 260 | // TODO(jar): The following are all optional, so we *could* optimize them for |
| 261 | // values of zero (and not include them). |
[email protected] | 48ff2c7f | 2014-05-23 09:57:45 | [diff] [blame] | 262 | SystemProfileProto::Stability* stability = |
| 263 | system_profile->mutable_stability(); |
[email protected] | fe58acc2 | 2012-02-29 01:29:58 | [diff] [blame] | 264 | stability->set_incomplete_shutdown_count(incomplete_shutdown_count); |
| 265 | stability->set_breakpad_registration_success_count( |
| 266 | breakpad_registration_success_count); |
| 267 | stability->set_breakpad_registration_failure_count( |
| 268 | breakpad_registration_failure_count); |
| 269 | stability->set_debugger_present_count(debugger_present_count); |
| 270 | stability->set_debugger_not_present_count(debugger_not_present_count); |
[email protected] | 0edf876 | 2013-11-21 18:33:30 | [diff] [blame] | 271 | } |
[email protected] | fe58acc2 | 2012-02-29 01:29:58 | [diff] [blame] | 272 | |
[email protected] | 85791b0b | 2014-05-20 15:18:58 | [diff] [blame] | 273 | void MetricsLog::RecordGeneralMetrics( |
asvitkine | 4c1d1ef | 2014-09-29 20:57:32 | [diff] [blame] | 274 | const std::vector<MetricsProvider*>& metrics_providers) { |
[email protected] | 85791b0b | 2014-05-20 15:18:58 | [diff] [blame] | 275 | for (size_t i = 0; i < metrics_providers.size(); ++i) |
| 276 | metrics_providers[i]->ProvideGeneralMetrics(uma_proto()); |
| 277 | } |
| 278 | |
[email protected] | 0edf876 | 2013-11-21 18:33:30 | [diff] [blame] | 279 | void MetricsLog::GetFieldTrialIds( |
| 280 | std::vector<ActiveGroupId>* field_trial_ids) const { |
[email protected] | b3610d4 | 2014-05-19 18:07:23 | [diff] [blame] | 281 | variations::GetFieldTrialActiveGroupIds(field_trial_ids); |
[email protected] | 147bbc0b | 2009-01-06 19:37:40 | [diff] [blame] | 282 | } |
| 283 | |
[email protected] | 0f2f779 | 2013-11-28 16:09:14 | [diff] [blame] | 284 | bool MetricsLog::HasEnvironment() const { |
| 285 | return uma_proto()->system_profile().has_uma_enabled_date(); |
| 286 | } |
| 287 | |
| 288 | bool MetricsLog::HasStabilityMetrics() const { |
| 289 | return uma_proto()->system_profile().stability().has_launch_count(); |
| 290 | } |
| 291 | |
[email protected] | fe58acc2 | 2012-02-29 01:29:58 | [diff] [blame] | 292 | // The server refuses data that doesn't have certain values. crashcount and |
| 293 | // launchcount are currently "required" in the "stability" group. |
| 294 | // TODO(isherman): Stop writing these attributes specially once the migration to |
| 295 | // protobufs is complete. |
[email protected] | 147bbc0b | 2009-01-06 19:37:40 | [diff] [blame] | 296 | void MetricsLog::WriteRequiredStabilityAttributes(PrefService* pref) { |
asvitkine | 4c1d1ef | 2014-09-29 20:57:32 | [diff] [blame] | 297 | int launch_count = pref->GetInteger(prefs::kStabilityLaunchCount); |
| 298 | pref->SetInteger(prefs::kStabilityLaunchCount, 0); |
| 299 | int crash_count = pref->GetInteger(prefs::kStabilityCrashCount); |
| 300 | pref->SetInteger(prefs::kStabilityCrashCount, 0); |
[email protected] | fe58acc2 | 2012-02-29 01:29:58 | [diff] [blame] | 301 | |
[email protected] | fe58acc2 | 2012-02-29 01:29:58 | [diff] [blame] | 302 | SystemProfileProto::Stability* stability = |
[email protected] | 767c9d9 | 2012-03-02 16:04:34 | [diff] [blame] | 303 | uma_proto()->mutable_system_profile()->mutable_stability(); |
[email protected] | fe58acc2 | 2012-02-29 01:29:58 | [diff] [blame] | 304 | stability->set_launch_count(launch_count); |
| 305 | stability->set_crash_count(crash_count); |
[email protected] | 0b33f80b | 2008-12-17 21:34:36 | [diff] [blame] | 306 | } |
| 307 | |
[email protected] | c68a2b9b | 2013-10-09 18:16:36 | [diff] [blame] | 308 | void MetricsLog::WriteRealtimeStabilityAttributes( |
| 309 | PrefService* pref, |
[email protected] | 076961c | 2014-03-12 22:23:56 | [diff] [blame] | 310 | base::TimeDelta incremental_uptime, |
| 311 | base::TimeDelta uptime) { |
[email protected] | 0b33f80b | 2008-12-17 21:34:36 | [diff] [blame] | 312 | // Update the stats which are critical for real-time stability monitoring. |
| 313 | // 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] | 314 | // are aggregated (summed) server side. |
[email protected] | 0b33f80b | 2008-12-17 21:34:36 | [diff] [blame] | 315 | |
[email protected] | fe58acc2 | 2012-02-29 01:29:58 | [diff] [blame] | 316 | SystemProfileProto::Stability* stability = |
[email protected] | 767c9d9 | 2012-03-02 16:04:34 | [diff] [blame] | 317 | uma_proto()->mutable_system_profile()->mutable_stability(); |
[email protected] | 0b33f80b | 2008-12-17 21:34:36 | [diff] [blame] | 318 | |
[email protected] | 076961c | 2014-03-12 22:23:56 | [diff] [blame] | 319 | const uint64 incremental_uptime_sec = incremental_uptime.InSeconds(); |
| 320 | if (incremental_uptime_sec) |
| 321 | stability->set_incremental_uptime_sec(incremental_uptime_sec); |
| 322 | const uint64 uptime_sec = uptime.InSeconds(); |
[email protected] | c68a2b9b | 2013-10-09 18:16:36 | [diff] [blame] | 323 | if (uptime_sec) |
| 324 | stability->set_uptime_sec(uptime_sec); |
[email protected] | 0b33f80b | 2008-12-17 21:34:36 | [diff] [blame] | 325 | } |
| 326 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 327 | void MetricsLog::RecordEnvironment( |
asvitkine | 4c1d1ef | 2014-09-29 20:57:32 | [diff] [blame] | 328 | const std::vector<MetricsProvider*>& metrics_providers, |
[email protected] | 6580145 | 2014-07-09 05:42:41 | [diff] [blame] | 329 | const std::vector<variations::ActiveGroupId>& synthetic_trials, |
| 330 | int64 install_date) { |
[email protected] | 0f2f779 | 2013-11-28 16:09:14 | [diff] [blame] | 331 | DCHECK(!HasEnvironment()); |
| 332 | |
[email protected] | bc66d53 | 2012-03-23 01:57:05 | [diff] [blame] | 333 | SystemProfileProto* system_profile = uma_proto()->mutable_system_profile(); |
[email protected] | 24b9bb39 | 2013-01-29 20:29:29 | [diff] [blame] | 334 | |
| 335 | std::string brand_code; |
[email protected] | 09dee82d | 2014-05-22 14:00:53 | [diff] [blame] | 336 | if (client_->GetBrand(&brand_code)) |
[email protected] | 24b9bb39 | 2013-01-29 20:29:29 | [diff] [blame] | 337 | system_profile->set_brand_code(brand_code); |
| 338 | |
[email protected] | cc5d7f4 | 2012-10-30 00:30:09 | [diff] [blame] | 339 | int enabled_date; |
[email protected] | 24f81ca | 2014-05-26 15:59:34 | [diff] [blame] | 340 | bool success = |
| 341 | base::StringToInt(GetMetricsEnabledDate(local_state_), &enabled_date); |
[email protected] | bc66d53 | 2012-03-23 01:57:05 | [diff] [blame] | 342 | DCHECK(success); |
[email protected] | 86573d1 | 2013-07-11 19:48:32 | [diff] [blame] | 343 | |
| 344 | // Reduce granularity of the enabled_date field to nearest hour. |
| 345 | system_profile->set_uma_enabled_date(RoundSecondsToHour(enabled_date)); |
| 346 | |
[email protected] | 86573d1 | 2013-07-11 19:48:32 | [diff] [blame] | 347 | // Reduce granularity of the install_date field to nearest hour. |
| 348 | system_profile->set_install_date(RoundSecondsToHour(install_date)); |
[email protected] | bc66d53 | 2012-03-23 01:57:05 | [diff] [blame] | 349 | |
[email protected] | 09dee82d | 2014-05-22 14:00:53 | [diff] [blame] | 350 | system_profile->set_application_locale(client_->GetApplicationLocale()); |
[email protected] | bc66d53 | 2012-03-23 01:57:05 | [diff] [blame] | 351 | |
| 352 | SystemProfileProto::Hardware* hardware = system_profile->mutable_hardware(); |
[email protected] | 51994b2 | 2014-05-30 13:24:21 | [diff] [blame] | 353 | |
jeremy | ba4eca9 | 2014-11-06 18:47:10 | [diff] [blame^] | 354 | // HardwareModelName() will return an empty string on platforms where it's |
| 355 | // not implemented or if an error occured. |
| 356 | hardware->set_hardware_class(base::SysInfo::HardwareModelName()); |
[email protected] | 51994b2 | 2014-05-30 13:24:21 | [diff] [blame] | 357 | |
[email protected] | 0b6a4fb | 2012-10-16 01:58:21 | [diff] [blame] | 358 | hardware->set_cpu_architecture(base::SysInfo::OperatingSystemArchitecture()); |
[email protected] | bc66d53 | 2012-03-23 01:57:05 | [diff] [blame] | 359 | hardware->set_system_ram_mb(base::SysInfo::AmountOfPhysicalMemoryMB()); |
| 360 | #if defined(OS_WIN) |
| 361 | hardware->set_dll_base(reinterpret_cast<uint64>(&__ImageBase)); |
| 362 | #endif |
| 363 | |
| 364 | SystemProfileProto::OS* os = system_profile->mutable_os(); |
[email protected] | 1bb25e0 | 2012-08-03 22:39:39 | [diff] [blame] | 365 | std::string os_name = base::SysInfo::OperatingSystemName(); |
| 366 | #if defined(OS_WIN) |
| 367 | // TODO(mad): This only checks whether the main process is a Metro process at |
| 368 | // upload time; not whether the collected metrics were all gathered from |
| 369 | // Metro. This is ok as an approximation for now, since users will rarely be |
| 370 | // switching from Metro to Desktop mode; but we should re-evaluate whether we |
| 371 | // can distinguish metrics more cleanly in the future: http://crbug.com/140568 |
| 372 | if (base::win::IsMetroProcess()) |
| 373 | os_name += " (Metro)"; |
| 374 | #endif |
| 375 | os->set_name(os_name); |
[email protected] | bc66d53 | 2012-03-23 01:57:05 | [diff] [blame] | 376 | os->set_version(base::SysInfo::OperatingSystemVersion()); |
[email protected] | 5106b3a | 2012-10-03 20:10:44 | [diff] [blame] | 377 | #if defined(OS_ANDROID) |
| 378 | os->set_fingerprint( |
| 379 | base::android::BuildInfo::GetInstance()->android_build_fp()); |
| 380 | #endif |
[email protected] | bc66d53 | 2012-03-23 01:57:05 | [diff] [blame] | 381 | |
[email protected] | 5c8f89f69 | 2013-07-18 11:13:28 | [diff] [blame] | 382 | base::CPU cpu_info; |
| 383 | SystemProfileProto::Hardware::CPU* cpu = hardware->mutable_cpu(); |
| 384 | cpu->set_vendor_name(cpu_info.vendor_name()); |
| 385 | cpu->set_signature(cpu_info.signature()); |
| 386 | |
[email protected] | 0c8b7ad | 2012-11-06 07:08:14 | [diff] [blame] | 387 | std::vector<ActiveGroupId> field_trial_ids; |
[email protected] | 767c9d9 | 2012-03-02 16:04:34 | [diff] [blame] | 388 | GetFieldTrialIds(&field_trial_ids); |
| 389 | WriteFieldTrials(field_trial_ids, system_profile); |
[email protected] | 6067756 | 2013-11-17 15:52:55 | [diff] [blame] | 390 | WriteFieldTrials(synthetic_trials, system_profile); |
[email protected] | f65859e | 2013-02-04 20:00:25 | [diff] [blame] | 391 | |
[email protected] | 85791b0b | 2014-05-20 15:18:58 | [diff] [blame] | 392 | for (size_t i = 0; i < metrics_providers.size(); ++i) |
| 393 | metrics_providers[i]->ProvideSystemProfileMetrics(system_profile); |
| 394 | |
[email protected] | 0f2f779 | 2013-11-28 16:09:14 | [diff] [blame] | 395 | std::string serialied_system_profile; |
| 396 | std::string base64_system_profile; |
[email protected] | 33fca12 | 2013-12-11 01:48:50 | [diff] [blame] | 397 | if (system_profile->SerializeToString(&serialied_system_profile)) { |
| 398 | base::Base64Encode(serialied_system_profile, &base64_system_profile); |
[email protected] | 24f81ca | 2014-05-26 15:59:34 | [diff] [blame] | 399 | PrefService* local_state = local_state_; |
asvitkine | 4c1d1ef | 2014-09-29 20:57:32 | [diff] [blame] | 400 | local_state->SetString(prefs::kStabilitySavedSystemProfile, |
[email protected] | 0f2f779 | 2013-11-28 16:09:14 | [diff] [blame] | 401 | base64_system_profile); |
asvitkine | 4c1d1ef | 2014-09-29 20:57:32 | [diff] [blame] | 402 | local_state->SetString(prefs::kStabilitySavedSystemProfileHash, |
[email protected] | 0f2f779 | 2013-11-28 16:09:14 | [diff] [blame] | 403 | ComputeSHA1(serialied_system_profile)); |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | bool MetricsLog::LoadSavedEnvironmentFromPrefs() { |
[email protected] | 24f81ca | 2014-05-26 15:59:34 | [diff] [blame] | 408 | PrefService* local_state = local_state_; |
[email protected] | 0f2f779 | 2013-11-28 16:09:14 | [diff] [blame] | 409 | const std::string base64_system_profile = |
asvitkine | 4c1d1ef | 2014-09-29 20:57:32 | [diff] [blame] | 410 | local_state->GetString(prefs::kStabilitySavedSystemProfile); |
[email protected] | 0f2f779 | 2013-11-28 16:09:14 | [diff] [blame] | 411 | if (base64_system_profile.empty()) |
| 412 | return false; |
| 413 | |
| 414 | const std::string system_profile_hash = |
asvitkine | 4c1d1ef | 2014-09-29 20:57:32 | [diff] [blame] | 415 | local_state->GetString(prefs::kStabilitySavedSystemProfileHash); |
| 416 | local_state->ClearPref(prefs::kStabilitySavedSystemProfile); |
| 417 | local_state->ClearPref(prefs::kStabilitySavedSystemProfileHash); |
[email protected] | 0f2f779 | 2013-11-28 16:09:14 | [diff] [blame] | 418 | |
| 419 | SystemProfileProto* system_profile = uma_proto()->mutable_system_profile(); |
| 420 | std::string serialied_system_profile; |
| 421 | return base::Base64Decode(base64_system_profile, &serialied_system_profile) && |
| 422 | ComputeSHA1(serialied_system_profile) == system_profile_hash && |
| 423 | system_profile->ParseFromString(serialied_system_profile); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 424 | } |
| 425 | |
[email protected] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 426 | void MetricsLog::CloseLog() { |
| 427 | DCHECK(!closed_); |
| 428 | closed_ = true; |
| 429 | } |
| 430 | |
| 431 | void MetricsLog::GetEncodedLog(std::string* encoded_log) { |
| 432 | DCHECK(closed_); |
| 433 | uma_proto_.SerializeToString(encoded_log); |
| 434 | } |
asvitkine | cbd42073 | 2014-08-26 22:15:40 | [diff] [blame] | 435 | |
| 436 | } // namespace metrics |