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