blob: 3824762e217d71c3d5d49fd925ab43207a558366 [file] [log] [blame]
[email protected]91b1d912014-06-05 10:52:081// Copyright 2014 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]91b1d912014-06-05 10:52:085#include "components/metrics/metrics_log.h"
initial.commit09911bf2008-07-26 23:55:296
avi26062922015-12-26 00:14:187#include <stddef.h>
8
[email protected]76869ff2013-01-15 16:13:479#include <algorithm>
dchengd99c42a2016-04-21 21:54:1310#include <memory>
[email protected]1eeb5e02010-07-20 23:02:1111#include <string>
12#include <vector>
13
[email protected]0f2f7792013-11-28 16:09:1414#include "base/base64.h"
sebmarchand2f4ed502014-10-31 15:28:1915#include "base/build_time.h"
[email protected]5c8f89f692013-07-18 11:13:2816#include "base/cpu.h"
[email protected]bfb77b52014-06-07 01:54:0117#include "base/metrics/histogram.h"
18#include "base/metrics/histogram_samples.h"
bcwhite502b2292015-11-30 22:31:0119#include "base/metrics/metrics_hashes.h"
[email protected]0f2f7792013-11-28 16:09:1420#include "base/sha1.h"
[email protected]3ea1b182013-02-08 22:38:4121#include "base/strings/string_number_conversions.h"
[email protected]f9b294362013-06-10 20:22:3122#include "base/strings/string_util.h"
[email protected]112158af2013-06-07 23:46:1823#include "base/strings/utf_string_conversions.h"
[email protected]fadf97f2008-09-18 12:18:1424#include "base/sys_info.h"
[email protected]84813472013-06-28 00:25:1925#include "base/time/time.h"
avi26062922015-12-26 00:14:1826#include "build/build_config.h"
rtenneti72546962014-12-15 21:41:5927#include "components/metrics/histogram_encoder.h"
[email protected]91b1d912014-06-05 10:52:0828#include "components/metrics/metrics_pref_names.h"
[email protected]85791b0b2014-05-20 15:18:5829#include "components/metrics/metrics_provider.h"
[email protected]09dee82d2014-05-22 14:00:5330#include "components/metrics/metrics_service_client.h"
[email protected]bfb77b52014-06-07 01:54:0131#include "components/metrics/proto/histogram_event.pb.h"
[email protected]064107e2014-05-02 00:59:0632#include "components/metrics/proto/system_profile.pb.h"
[email protected]bfb77b52014-06-07 01:54:0133#include "components/metrics/proto/user_action_event.pb.h"
brettw066508682016-02-03 08:22:0234#include "components/prefs/pref_registry_simple.h"
35#include "components/prefs/pref_service.h"
[email protected]b3610d42014-05-19 18:07:2336#include "components/variations/active_field_trials.h"
initial.commit09911bf2008-07-26 23:55:2937
[email protected]5106b3a2012-10-03 20:10:4438#if defined(OS_ANDROID)
39#include "base/android/build_info.h"
40#endif
41
[email protected]d1be67b2008-11-19 20:28:3842#if defined(OS_WIN)
thakisd62f54472016-04-04 02:21:1043#include "base/win/current_module.h"
[email protected]d1be67b2008-11-19 20:28:3844#endif
45
[email protected]bfb77b52014-06-07 01:54:0146using base::SampleCountIterator;
[email protected]b3610d42014-05-19 18:07:2347typedef variations::ActiveGroupId ActiveGroupId;
[email protected]79078df2012-02-16 01:22:3248
asvitkinecbd420732014-08-26 22:15:4049namespace metrics {
50
[email protected]1df44b72012-01-19 05:20:3451namespace {
52
[email protected]bfb77b52014-06-07 01:54:0153// Any id less than 16 bytes is considered to be a testing id.
54bool IsTestingID(const std::string& id) {
55 return id.size() < 16;
56}
57
[email protected]0f2f7792013-11-28 16:09:1458// Computes a SHA-1 hash of |data| and returns it as a hex string.
59std::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]0c8b7ad2012-11-06 07:08:1464void WriteFieldTrials(const std::vector<ActiveGroupId>& field_trial_ids,
[email protected]767c9d92012-03-02 16:04:3465 SystemProfileProto* system_profile) {
[email protected]0c8b7ad2012-11-06 07:08:1466 for (std::vector<ActiveGroupId>::const_iterator it =
[email protected]ad2461c2012-04-27 21:11:0367 field_trial_ids.begin(); it != field_trial_ids.end(); ++it) {
[email protected]767c9d92012-03-02 16:04:3468 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]86573d12013-07-11 19:48:3275// 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.
avi26062922015-12-26 00:14:1878int64_t RoundSecondsToHour(int64_t time_in_seconds) {
[email protected]86573d12013-07-11 19:48:3279 return 3600 * (time_in_seconds / 3600);
80}
81
[email protected]1df44b72012-01-19 05:20:3482} // namespace
83
[email protected]9eae4032014-04-09 19:15:1984MetricsLog::MetricsLog(const std::string& client_id,
85 int session_id,
[email protected]09dee82d2014-05-22 14:00:5386 LogType log_type,
asvitkine4c1d1ef2014-09-29 20:57:3287 MetricsServiceClient* client,
[email protected]24f81ca2014-05-26 15:59:3488 PrefService* local_state)
[email protected]bfb77b52014-06-07 01:54:0189 : closed_(false),
90 log_type_(log_type),
[email protected]09dee82d2014-05-22 14:00:5391 client_(client),
[email protected]24f81ca2014-05-26 15:59:3492 creation_time_(base::TimeTicks::Now()),
93 local_state_(local_state) {
[email protected]bfb77b52014-06-07 01:54:0194 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
avi26062922015-12-26 00:14:18101 const int32_t product = client_->GetProduct();
asvitkine4c1d1ef2014-09-29 20:57:32102 // 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]bfb77b52014-06-07 01:54:01106 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());
vadimt690531262015-01-07 15:35:22110#if defined(SYZYASAN)
111 system_profile->set_is_asan_build(true);
112#endif
[email protected]afc03f02013-10-11 06:01:35113}
[email protected]5ed7d4572009-12-23 17:42:41114
[email protected]bfb77b52014-06-07 01:54:01115MetricsLog::~MetricsLog() {
116}
initial.commit09911bf2008-07-26 23:55:29117
[email protected]91b1d912014-06-05 10:52:08118// static
119void MetricsLog::RegisterPrefs(PrefRegistrySimple* registry) {
asvitkine4c1d1ef2014-09-29 20:57:32120 registry->RegisterIntegerPref(prefs::kStabilityCrashCount, 0);
121 registry->RegisterIntegerPref(prefs::kStabilityIncompleteSessionEndCount, 0);
manzagopea99d1952016-09-08 23:40:05122 registry->RegisterIntegerPref(prefs::kStabilityLaunchCount, 0);
asvitkine4c1d1ef2014-09-29 20:57:32123 registry->RegisterIntegerPref(prefs::kStabilityBreakpadRegistrationFail, 0);
[email protected]91b1d912014-06-05 10:52:08124 registry->RegisterIntegerPref(
asvitkine4c1d1ef2014-09-29 20:57:32125 prefs::kStabilityBreakpadRegistrationSuccess, 0);
126 registry->RegisterIntegerPref(prefs::kStabilityDebuggerPresent, 0);
127 registry->RegisterIntegerPref(prefs::kStabilityDebuggerNotPresent, 0);
128 registry->RegisterStringPref(prefs::kStabilitySavedSystemProfile,
[email protected]91b1d912014-06-05 10:52:08129 std::string());
asvitkine4c1d1ef2014-09-29 20:57:32130 registry->RegisterStringPref(prefs::kStabilitySavedSystemProfileHash,
[email protected]91b1d912014-06-05 10:52:08131 std::string());
manzagopea99d1952016-09-08 23:40:05132 registry->RegisterIntegerPref(prefs::kStabilityDeferredCount, 0);
133 registry->RegisterIntegerPref(prefs::kStabilityDiscardCount, 0);
134 registry->RegisterIntegerPref(prefs::kStabilityVersionMismatchCount, 0);
[email protected]91b1d912014-06-05 10:52:08135}
136
[email protected]bfb77b52014-06-07 01:54:01137// static
avi26062922015-12-26 00:14:18138uint64_t MetricsLog::Hash(const std::string& value) {
139 uint64_t hash = base::HashMetricName(value);
[email protected]bfb77b52014-06-07 01:54:01140
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
avi26062922015-12-26 00:14:18153int64_t MetricsLog::GetBuildTime() {
154 static int64_t integral_build_time = 0;
sebmarchand2f4ed502014-10-31 15:28:19155 if (!integral_build_time)
avi26062922015-12-26 00:14:18156 integral_build_time = static_cast<int64_t>(base::GetBuildTime().ToTimeT());
[email protected]bfb77b52014-06-07 01:54:01157 return integral_build_time;
158}
159
160// static
avi26062922015-12-26 00:14:18161int64_t MetricsLog::GetCurrentTime() {
[email protected]bfb77b52014-06-07 01:54:01162 return (base::TimeTicks::Now() - base::TimeTicks()).InSeconds();
163}
164
165void 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
173void MetricsLog::RecordHistogramDelta(const std::string& histogram_name,
174 const base::HistogramSamples& snapshot) {
175 DCHECK(!closed_);
rtenneti72546962014-12-15 21:41:59176 EncodeHistogramDelta(histogram_name, snapshot, &uma_proto_);
[email protected]bfb77b52014-06-07 01:54:01177}
178
[email protected]85791b0b2014-05-20 15:18:58179void MetricsLog::RecordStabilityMetrics(
asvitkine4c1d1ef2014-09-29 20:57:32180 const std::vector<MetricsProvider*>& metrics_providers,
[email protected]85791b0b2014-05-20 15:18:58181 base::TimeDelta incremental_uptime,
182 base::TimeDelta uptime) {
[email protected]bfb77b52014-06-07 01:54:01183 DCHECK(!closed_);
[email protected]0f2f7792013-11-28 16:09:14184 DCHECK(HasEnvironment());
185 DCHECK(!HasStabilityMetrics());
[email protected]0b33f80b2008-12-17 21:34:36186
[email protected]24f81ca2014-05-26 15:59:34187 PrefService* pref = local_state_;
[email protected]0b33f80b2008-12-17 21:34:36188 DCHECK(pref);
189
initial.commit09911bf2008-07-26 23:55:29190 // 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]147bbc0b2009-01-06 19:37:40194 WriteRequiredStabilityAttributes(pref);
[email protected]0edf8762013-11-21 18:33:30195
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]076961c2014-03-12 22:23:56200 WriteRealtimeStabilityAttributes(pref, incremental_uptime, uptime);
initial.commit09911bf2008-07-26 23:55:29201
[email protected]48ff2c7f2014-05-23 09:57:45202 SystemProfileProto* system_profile = uma_proto()->mutable_system_profile();
lpromeroca8cb6f2015-04-30 18:16:53203 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]48ff2c7f2014-05-23 09:57:45206 metrics_providers[i]->ProvideStabilityMetrics(system_profile);
lpromeroca8cb6f2015-04-30 18:16:53207 }
[email protected]85791b0b2014-05-20 15:18:58208
manzagop54157002016-09-01 02:43:59209 SystemProfileProto::Stability* stability =
210 system_profile->mutable_stability();
[email protected]0edf8762013-11-21 18:33:30211
[email protected]fe58acc22012-02-29 01:29:58212 int incomplete_shutdown_count =
asvitkine4c1d1ef2014-09-29 20:57:32213 pref->GetInteger(prefs::kStabilityIncompleteSessionEndCount);
manzagop54157002016-09-01 02:43:59214 if (incomplete_shutdown_count) {
215 pref->SetInteger(prefs::kStabilityIncompleteSessionEndCount, 0);
216 stability->set_incomplete_shutdown_count(incomplete_shutdown_count);
217 }
218
[email protected]fe58acc22012-02-29 01:29:58219 int breakpad_registration_success_count =
asvitkine4c1d1ef2014-09-29 20:57:32220 pref->GetInteger(prefs::kStabilityBreakpadRegistrationSuccess);
manzagop54157002016-09-01 02:43:59221 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]fe58acc22012-02-29 01:29:58227 int breakpad_registration_failure_count =
asvitkine4c1d1ef2014-09-29 20:57:32228 pref->GetInteger(prefs::kStabilityBreakpadRegistrationFail);
manzagop54157002016-09-01 02:43:59229 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]fe58acc22012-02-29 01:29:58235 int debugger_present_count =
asvitkine4c1d1ef2014-09-29 20:57:32236 pref->GetInteger(prefs::kStabilityDebuggerPresent);
manzagop54157002016-09-01 02:43:59237 if (debugger_present_count) {
238 pref->SetInteger(prefs::kStabilityDebuggerPresent, 0);
239 stability->set_debugger_present_count(debugger_present_count);
240 }
241
[email protected]fe58acc22012-02-29 01:29:58242 int debugger_not_present_count =
asvitkine4c1d1ef2014-09-29 20:57:32243 pref->GetInteger(prefs::kStabilityDebuggerNotPresent);
manzagop54157002016-09-01 02:43:59244 if (debugger_not_present_count) {
245 pref->SetInteger(prefs::kStabilityDebuggerNotPresent, 0);
246 stability->set_debugger_not_present_count(debugger_not_present_count);
247 }
manzagopea99d1952016-09-08 23:40:05248
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]0edf8762013-11-21 18:33:30273}
[email protected]fe58acc22012-02-29 01:29:58274
[email protected]85791b0b2014-05-20 15:18:58275void MetricsLog::RecordGeneralMetrics(
asvitkine4c1d1ef2014-09-29 20:57:32276 const std::vector<MetricsProvider*>& metrics_providers) {
[email protected]85791b0b2014-05-20 15:18:58277 for (size_t i = 0; i < metrics_providers.size(); ++i)
278 metrics_providers[i]->ProvideGeneralMetrics(uma_proto());
279}
280
[email protected]0edf8762013-11-21 18:33:30281void MetricsLog::GetFieldTrialIds(
282 std::vector<ActiveGroupId>* field_trial_ids) const {
[email protected]b3610d42014-05-19 18:07:23283 variations::GetFieldTrialActiveGroupIds(field_trial_ids);
[email protected]147bbc0b2009-01-06 19:37:40284}
285
[email protected]0f2f7792013-11-28 16:09:14286bool MetricsLog::HasEnvironment() const {
287 return uma_proto()->system_profile().has_uma_enabled_date();
288}
289
gayanedaaf3a02016-06-15 16:30:21290void MetricsLog::WriteMetricsEnableDefault(EnableMetricsDefault metrics_default,
291 SystemProfileProto* system_profile) {
jwd421086f2016-03-21 14:40:42292 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) {
gayanedaaf3a02016-06-15 16:30:21301 case EnableMetricsDefault::DEFAULT_UNKNOWN:
jwd421086f2016-03-21 14:40:42302 // Don't set the field if it's unknown.
303 break;
gayanedaaf3a02016-06-15 16:30:21304 case EnableMetricsDefault::OPT_IN:
jwd421086f2016-03-21 14:40:42305 system_profile->set_uma_default_state(
306 SystemProfileProto_UmaDefaultState_OPT_IN);
307 break;
gayanedaaf3a02016-06-15 16:30:21308 case EnableMetricsDefault::OPT_OUT:
jwd421086f2016-03-21 14:40:42309 system_profile->set_uma_default_state(
310 SystemProfileProto_UmaDefaultState_OPT_OUT);
311 }
312}
313
[email protected]0f2f7792013-11-28 16:09:14314bool MetricsLog::HasStabilityMetrics() const {
315 return uma_proto()->system_profile().stability().has_launch_count();
316}
317
[email protected]fe58acc22012-02-29 01:29:58318// 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]147bbc0b2009-01-06 19:37:40322void MetricsLog::WriteRequiredStabilityAttributes(PrefService* pref) {
asvitkine4c1d1ef2014-09-29 20:57:32323 int launch_count = pref->GetInteger(prefs::kStabilityLaunchCount);
manzagop54157002016-09-01 02:43:59324 if (launch_count)
325 pref->SetInteger(prefs::kStabilityLaunchCount, 0);
asvitkine4c1d1ef2014-09-29 20:57:32326 int crash_count = pref->GetInteger(prefs::kStabilityCrashCount);
manzagop54157002016-09-01 02:43:59327 if (crash_count)
328 pref->SetInteger(prefs::kStabilityCrashCount, 0);
[email protected]fe58acc22012-02-29 01:29:58329
[email protected]fe58acc22012-02-29 01:29:58330 SystemProfileProto::Stability* stability =
[email protected]767c9d92012-03-02 16:04:34331 uma_proto()->mutable_system_profile()->mutable_stability();
[email protected]fe58acc22012-02-29 01:29:58332 stability->set_launch_count(launch_count);
333 stability->set_crash_count(crash_count);
[email protected]0b33f80b2008-12-17 21:34:36334}
335
[email protected]c68a2b9b2013-10-09 18:16:36336void MetricsLog::WriteRealtimeStabilityAttributes(
337 PrefService* pref,
[email protected]076961c2014-03-12 22:23:56338 base::TimeDelta incremental_uptime,
339 base::TimeDelta uptime) {
[email protected]0b33f80b2008-12-17 21:34:36340 // 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]250f7b662013-11-23 02:36:51342 // are aggregated (summed) server side.
[email protected]0b33f80b2008-12-17 21:34:36343
[email protected]fe58acc22012-02-29 01:29:58344 SystemProfileProto::Stability* stability =
[email protected]767c9d92012-03-02 16:04:34345 uma_proto()->mutable_system_profile()->mutable_stability();
[email protected]0b33f80b2008-12-17 21:34:36346
avi26062922015-12-26 00:14:18347 const uint64_t incremental_uptime_sec = incremental_uptime.InSeconds();
[email protected]076961c2014-03-12 22:23:56348 if (incremental_uptime_sec)
349 stability->set_incremental_uptime_sec(incremental_uptime_sec);
avi26062922015-12-26 00:14:18350 const uint64_t uptime_sec = uptime.InSeconds();
[email protected]c68a2b9b2013-10-09 18:16:36351 if (uptime_sec)
352 stability->set_uptime_sec(uptime_sec);
[email protected]0b33f80b2008-12-17 21:34:36353}
354
initial.commit09911bf2008-07-26 23:55:29355void MetricsLog::RecordEnvironment(
asvitkine4c1d1ef2014-09-29 20:57:32356 const std::vector<MetricsProvider*>& metrics_providers,
[email protected]65801452014-07-09 05:42:41357 const std::vector<variations::ActiveGroupId>& synthetic_trials,
avi26062922015-12-26 00:14:18358 int64_t install_date,
359 int64_t metrics_reporting_enabled_date) {
[email protected]0f2f7792013-11-28 16:09:14360 DCHECK(!HasEnvironment());
361
[email protected]bc66d532012-03-23 01:57:05362 SystemProfileProto* system_profile = uma_proto()->mutable_system_profile();
[email protected]24b9bb392013-01-29 20:29:29363
gayanedaaf3a02016-06-15 16:30:21364 WriteMetricsEnableDefault(client_->GetMetricsReportingDefaultState(),
365 system_profile);
jwd421086f2016-03-21 14:40:42366
[email protected]24b9bb392013-01-29 20:29:29367 std::string brand_code;
[email protected]09dee82d2014-05-22 14:00:53368 if (client_->GetBrand(&brand_code))
[email protected]24b9bb392013-01-29 20:29:29369 system_profile->set_brand_code(brand_code);
370
[email protected]86573d12013-07-11 19:48:32371 // Reduce granularity of the enabled_date field to nearest hour.
olivierrobinc3dfc5b2015-04-07 19:12:00372 system_profile->set_uma_enabled_date(
373 RoundSecondsToHour(metrics_reporting_enabled_date));
[email protected]86573d12013-07-11 19:48:32374
[email protected]86573d12013-07-11 19:48:32375 // Reduce granularity of the install_date field to nearest hour.
376 system_profile->set_install_date(RoundSecondsToHour(install_date));
[email protected]bc66d532012-03-23 01:57:05377
[email protected]09dee82d2014-05-22 14:00:53378 system_profile->set_application_locale(client_->GetApplicationLocale());
[email protected]bc66d532012-03-23 01:57:05379
380 SystemProfileProto::Hardware* hardware = system_profile->mutable_hardware();
[email protected]51994b22014-05-30 13:24:21381
jeremyba4eca92014-11-06 18:47:10382 // 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]51994b22014-05-30 13:24:21385
[email protected]0b6a4fb2012-10-16 01:58:21386 hardware->set_cpu_architecture(base::SysInfo::OperatingSystemArchitecture());
[email protected]bc66d532012-03-23 01:57:05387 hardware->set_system_ram_mb(base::SysInfo::AmountOfPhysicalMemoryMB());
388#if defined(OS_WIN)
thakisd62f54472016-04-04 02:21:10389 hardware->set_dll_base(reinterpret_cast<uint64_t>(CURRENT_MODULE()));
[email protected]bc66d532012-03-23 01:57:05390#endif
391
jessicagd5994152016-05-20 21:27:31392 SystemProfileProto::OS* os = system_profile->mutable_os();
jessicagab7a08b2016-03-11 22:34:49393#if defined(OVERRIDE_OS_NAME_TO_BLIMP)
394 os->set_name("Blimp");
395#else
[email protected]1bb25e02012-08-03 22:39:39396 std::string os_name = base::SysInfo::OperatingSystemName();
[email protected]1bb25e02012-08-03 22:39:39397 os->set_name(os_name);
jessicagab7a08b2016-03-11 22:34:49398#endif
399
[email protected]bc66d532012-03-23 01:57:05400 os->set_version(base::SysInfo::OperatingSystemVersion());
[email protected]5106b3a2012-10-03 20:10:44401#if defined(OS_ANDROID)
402 os->set_fingerprint(
403 base::android::BuildInfo::GetInstance()->android_build_fp());
404#endif
[email protected]bc66d532012-03-23 01:57:05405
[email protected]5c8f89f692013-07-18 11:13:28406 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());
gayanef550f50f2015-03-11 00:44:59410 cpu->set_num_cores(base::SysInfo::NumberOfProcessors());
[email protected]5c8f89f692013-07-18 11:13:28411
[email protected]0c8b7ad2012-11-06 07:08:14412 std::vector<ActiveGroupId> field_trial_ids;
[email protected]767c9d92012-03-02 16:04:34413 GetFieldTrialIds(&field_trial_ids);
414 WriteFieldTrials(field_trial_ids, system_profile);
[email protected]60677562013-11-17 15:52:55415 WriteFieldTrials(synthetic_trials, system_profile);
[email protected]f65859e2013-02-04 20:00:25416
[email protected]85791b0b2014-05-20 15:18:58417 for (size_t i = 0; i < metrics_providers.size(); ++i)
418 metrics_providers[i]->ProvideSystemProfileMetrics(system_profile);
419
manzagop54157002016-09-01 02:43:59420 std::string serialized_system_profile;
[email protected]0f2f7792013-11-28 16:09:14421 std::string base64_system_profile;
manzagop54157002016-09-01 02:43:59422 if (system_profile->SerializeToString(&serialized_system_profile)) {
423 base::Base64Encode(serialized_system_profile, &base64_system_profile);
[email protected]24f81ca2014-05-26 15:59:34424 PrefService* local_state = local_state_;
asvitkine4c1d1ef2014-09-29 20:57:32425 local_state->SetString(prefs::kStabilitySavedSystemProfile,
[email protected]0f2f7792013-11-28 16:09:14426 base64_system_profile);
asvitkine4c1d1ef2014-09-29 20:57:32427 local_state->SetString(prefs::kStabilitySavedSystemProfileHash,
manzagop54157002016-09-01 02:43:59428 ComputeSHA1(serialized_system_profile));
[email protected]0f2f7792013-11-28 16:09:14429 }
430}
431
manzagopea99d1952016-09-08 23:40:05432bool MetricsLog::LoadSavedEnvironmentFromPrefs(std::string* app_version) {
433 DCHECK(app_version);
434 app_version->clear();
435
[email protected]24f81ca2014-05-26 15:59:34436 PrefService* local_state = local_state_;
[email protected]0f2f7792013-11-28 16:09:14437 const std::string base64_system_profile =
asvitkine4c1d1ef2014-09-29 20:57:32438 local_state->GetString(prefs::kStabilitySavedSystemProfile);
[email protected]0f2f7792013-11-28 16:09:14439 if (base64_system_profile.empty())
440 return false;
441
442 const std::string system_profile_hash =
asvitkine4c1d1ef2014-09-29 20:57:32443 local_state->GetString(prefs::kStabilitySavedSystemProfileHash);
444 local_state->ClearPref(prefs::kStabilitySavedSystemProfile);
445 local_state->ClearPref(prefs::kStabilitySavedSystemProfileHash);
[email protected]0f2f7792013-11-28 16:09:14446
447 SystemProfileProto* system_profile = uma_proto()->mutable_system_profile();
manzagop54157002016-09-01 02:43:59448 std::string serialized_system_profile;
manzagopea99d1952016-09-08 23:40:05449
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.commit09911bf2008-07-26 23:55:29457}
458
[email protected]bfb77b52014-06-07 01:54:01459void MetricsLog::CloseLog() {
460 DCHECK(!closed_);
461 closed_ = true;
462}
463
464void MetricsLog::GetEncodedLog(std::string* encoded_log) {
465 DCHECK(closed_);
466 uma_proto_.SerializeToString(encoded_log);
467}
asvitkinecbd420732014-08-26 22:15:40468
469} // namespace metrics