blob: f0ba00a920055737e591c508f19c33f026b6b7ae [file] [log] [blame]
isherman@chromium.org1df44b72012-01-19 05:20:341// Copyright (c) 2012 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
ben@chromium.orgcd1adc22009-01-16 01:29:225#include "chrome/browser/metrics/metrics_log.h"
initial.commit09911bf2008-07-26 23:55:296
girard@chromium.org76869ff2013-01-15 16:13:477#include <algorithm>
thestig@chromium.org1eeb5e02010-07-20 23:02:118#include <string>
9#include <vector>
10
asvitkine@chromium.org0f2f7792013-11-28 16:09:1411#include "base/base64.h"
deanm@chromium.orgd1be67b2008-11-19 20:28:3812#include "base/basictypes.h"
keybuk@chromium.org611ae29a2013-04-29 21:32:1913#include "base/bind.h"
simonjam@chromium.org5c8f89f692013-07-18 11:13:2814#include "base/cpu.h"
levin@chromium.org3b63f8f42011-03-28 01:54:1515#include "base/memory/scoped_ptr.h"
joi@chromium.org3853a4c2013-02-11 17:15:5716#include "base/prefs/pref_registry_simple.h"
17#include "base/prefs/pref_service.h"
isherman@chromium.orged0fd002012-04-25 23:10:3418#include "base/profiler/alternate_timer.h"
asvitkine@chromium.org0f2f7792013-11-28 16:09:1419#include "base/sha1.h"
brettw@chromium.org3ea1b182013-02-08 22:38:4120#include "base/strings/string_number_conversions.h"
avi@chromium.orgf9b294362013-06-10 20:22:3121#include "base/strings/string_util.h"
avi@chromium.org112158af2013-06-07 23:46:1822#include "base/strings/utf_string_conversions.h"
deanm@chromium.orgfadf97f2008-09-18 12:18:1423#include "base/sys_info.h"
jar@chromium.org37f39e42010-01-06 17:35:1724#include "base/third_party/nspr/prtime.h"
avi@chromium.org84813472013-06-28 00:25:1925#include "base/time/time.h"
isherman@chromium.orged0fd002012-04-25 23:10:3426#include "base/tracked_objects.h"
initial.commit09911bf2008-07-26 23:55:2927#include "chrome/browser/browser_process.h"
initial.commit09911bf2008-07-26 23:55:2928#include "chrome/common/pref_names.h"
asvitkine@chromium.org85791b0b2014-05-20 15:18:5829#include "components/metrics/metrics_provider.h"
isherman@chromium.org09dee82d2014-05-22 14:00:5330#include "components/metrics/metrics_service_client.h"
bsimonnet@chromium.org064107e2014-05-02 00:59:0631#include "components/metrics/proto/profiler_event.pb.h"
32#include "components/metrics/proto/system_profile.pb.h"
yael.aharon@intel.comd5d383252013-07-04 14:44:3233#include "components/nacl/common/nacl_process_type.h"
asvitkine@chromium.orgb3610d42014-05-19 18:07:2334#include "components/variations/active_field_trials.h"
isherman@chromium.org1a838cc2012-04-24 22:06:4935#include "content/public/common/content_client.h"
tfarina@chromium.org761fa4702013-07-02 15:25:1536#include "url/gurl.h"
initial.commit09911bf2008-07-26 23:55:2937
dfalcantara@chromium.org5106b3a2012-10-03 20:10:4438#if defined(OS_ANDROID)
39#include "base/android/build_info.h"
40#endif
41
deanm@chromium.orgd1be67b2008-11-19 20:28:3842#if defined(OS_WIN)
isherman@chromium.org1bb25e02012-08-03 22:39:3943#include "base/win/metro.h"
44
45// http://blogs.msdn.com/oldnewthing/archive/2004/10/25/247180.aspx
deanm@chromium.orgd1be67b2008-11-19 20:28:3846extern "C" IMAGE_DOS_HEADER __ImageBase;
47#endif
48
bsimonnet@chromium.org064107e2014-05-02 00:59:0649using metrics::MetricsLogBase;
isherman@chromium.orged0fd002012-04-25 23:10:3450using metrics::ProfilerEventProto;
isherman@chromium.orgfe58acc22012-02-29 01:29:5851using metrics::SystemProfileProto;
isherman@chromium.orged0fd002012-04-25 23:10:3452using tracked_objects::ProcessDataSnapshot;
asvitkine@chromium.orgb3610d42014-05-19 18:07:2353typedef variations::ActiveGroupId ActiveGroupId;
jam@chromium.org79078df2012-02-16 01:22:3254
isherman@chromium.org1df44b72012-01-19 05:20:3455namespace {
56
57// Returns the date at which the current metrics client ID was created as
isherman@chromium.orgca5ac4b2012-12-14 07:46:4058// a string containing seconds since the epoch, or "0" if none was found.
isherman@chromium.orgcc5d7f42012-10-30 00:30:0959std::string GetMetricsEnabledDate(PrefService* pref) {
isherman@chromium.org767c9d92012-03-02 16:04:3460 if (!pref) {
isherman@chromium.org1df44b72012-01-19 05:20:3461 NOTREACHED();
62 return "0";
63 }
isherman@chromium.org767c9d92012-03-02 16:04:3464
asvitkine@chromium.org1f74e802014-03-29 03:08:3565 return pref->GetString(prefs::kMetricsReportingEnabledTimestamp);
isherman@chromium.org1df44b72012-01-19 05:20:3466}
67
isherman@chromium.orged0fd002012-04-25 23:10:3468ProfilerEventProto::TrackedObject::ProcessType AsProtobufProcessType(
jam@chromium.orgf3b357692013-03-22 05:16:1369 int process_type) {
isherman@chromium.orged0fd002012-04-25 23:10:3470 switch (process_type) {
71 case content::PROCESS_TYPE_BROWSER:
72 return ProfilerEventProto::TrackedObject::BROWSER;
73 case content::PROCESS_TYPE_RENDERER:
74 return ProfilerEventProto::TrackedObject::RENDERER;
75 case content::PROCESS_TYPE_PLUGIN:
76 return ProfilerEventProto::TrackedObject::PLUGIN;
77 case content::PROCESS_TYPE_WORKER:
78 return ProfilerEventProto::TrackedObject::WORKER;
isherman@chromium.orged0fd002012-04-25 23:10:3479 case content::PROCESS_TYPE_UTILITY:
80 return ProfilerEventProto::TrackedObject::UTILITY;
isherman@chromium.orged0fd002012-04-25 23:10:3481 case content::PROCESS_TYPE_ZYGOTE:
82 return ProfilerEventProto::TrackedObject::ZYGOTE;
83 case content::PROCESS_TYPE_SANDBOX_HELPER:
84 return ProfilerEventProto::TrackedObject::SANDBOX_HELPER;
isherman@chromium.orged0fd002012-04-25 23:10:3485 case content::PROCESS_TYPE_GPU:
86 return ProfilerEventProto::TrackedObject::GPU;
87 case content::PROCESS_TYPE_PPAPI_PLUGIN:
88 return ProfilerEventProto::TrackedObject::PPAPI_PLUGIN;
89 case content::PROCESS_TYPE_PPAPI_BROKER:
90 return ProfilerEventProto::TrackedObject::PPAPI_BROKER;
jam@chromium.orgf3b357692013-03-22 05:16:1391 case PROCESS_TYPE_NACL_LOADER:
92 return ProfilerEventProto::TrackedObject::NACL_LOADER;
93 case PROCESS_TYPE_NACL_BROKER:
94 return ProfilerEventProto::TrackedObject::NACL_BROKER;
isherman@chromium.orged0fd002012-04-25 23:10:3495 default:
96 NOTREACHED();
97 return ProfilerEventProto::TrackedObject::UNKNOWN;
98 }
99}
100
asvitkine@chromium.org0f2f7792013-11-28 16:09:14101// Computes a SHA-1 hash of |data| and returns it as a hex string.
102std::string ComputeSHA1(const std::string& data) {
103 const std::string sha1 = base::SHA1HashString(data);
104 return base::HexEncode(sha1.data(), sha1.size());
105}
106
asvitkine@chromium.org0c8b7ad2012-11-06 07:08:14107void WriteFieldTrials(const std::vector<ActiveGroupId>& field_trial_ids,
isherman@chromium.org767c9d92012-03-02 16:04:34108 SystemProfileProto* system_profile) {
asvitkine@chromium.org0c8b7ad2012-11-06 07:08:14109 for (std::vector<ActiveGroupId>::const_iterator it =
stevet@chromium.orgad2461c2012-04-27 21:11:03110 field_trial_ids.begin(); it != field_trial_ids.end(); ++it) {
isherman@chromium.org767c9d92012-03-02 16:04:34111 SystemProfileProto::FieldTrial* field_trial =
112 system_profile->add_field_trial();
113 field_trial->set_name_id(it->name);
114 field_trial->set_group_id(it->group);
115 }
116}
117
vadimt@chromium.org0018f722014-02-08 03:08:51118// Maps a thread name by replacing trailing sequence of digits with "*".
119// Examples:
120// 1. "BrowserBlockingWorker1/23857" => "BrowserBlockingWorker1/*"
121// 2. "Chrome_IOThread" => "Chrome_IOThread"
122std::string MapThreadName(const std::string& thread_name) {
123 size_t i = thread_name.length();
124
125 while (i > 0 && isdigit(thread_name[i - 1])) {
126 --i;
127 }
128
129 if (i == thread_name.length())
130 return thread_name;
131
132 return thread_name.substr(0, i) + '*';
133}
134
vadimt@chromium.org87b9bf12014-04-03 15:25:00135// Normalizes a source filename (which is platform- and build-method-dependent)
136// by extracting the last component of the full file name.
137// Example: "c:\b\build\slave\win\build\src\chrome\app\chrome_main.cc" =>
138// "chrome_main.cc".
139std::string NormalizeFileName(const std::string& file_name) {
140 const size_t offset = file_name.find_last_of("\\/");
141 return offset != std::string::npos ? file_name.substr(offset + 1) : file_name;
142}
143
isherman@chromium.orged0fd002012-04-25 23:10:34144void WriteProfilerData(const ProcessDataSnapshot& profiler_data,
jam@chromium.orgf3b357692013-03-22 05:16:13145 int process_type,
isherman@chromium.orged0fd002012-04-25 23:10:34146 ProfilerEventProto* performance_profile) {
147 for (std::vector<tracked_objects::TaskSnapshot>::const_iterator it =
148 profiler_data.tasks.begin();
149 it != profiler_data.tasks.end(); ++it) {
isherman@chromium.orged0fd002012-04-25 23:10:34150 const tracked_objects::DeathDataSnapshot& death_data = it->death_data;
151 ProfilerEventProto::TrackedObject* tracked_object =
152 performance_profile->add_tracked_object();
isherman@chromium.orgdb7fec832013-07-18 09:41:41153 tracked_object->set_birth_thread_name_hash(
vadimt@chromium.org0018f722014-02-08 03:08:51154 MetricsLogBase::Hash(MapThreadName(it->birth.thread_name)));
isherman@chromium.orgdb7fec832013-07-18 09:41:41155 tracked_object->set_exec_thread_name_hash(
vadimt@chromium.org0018f722014-02-08 03:08:51156 MetricsLogBase::Hash(MapThreadName(it->death_thread_name)));
isherman@chromium.orgdb7fec832013-07-18 09:41:41157 tracked_object->set_source_file_name_hash(
vadimt@chromium.org87b9bf12014-04-03 15:25:00158 MetricsLogBase::Hash(NormalizeFileName(it->birth.location.file_name)));
isherman@chromium.orgdb7fec832013-07-18 09:41:41159 tracked_object->set_source_function_name_hash(
160 MetricsLogBase::Hash(it->birth.location.function_name));
isherman@chromium.orged0fd002012-04-25 23:10:34161 tracked_object->set_source_line_number(it->birth.location.line_number);
162 tracked_object->set_exec_count(death_data.count);
163 tracked_object->set_exec_time_total(death_data.run_duration_sum);
164 tracked_object->set_exec_time_sampled(death_data.run_duration_sample);
165 tracked_object->set_queue_time_total(death_data.queue_duration_sum);
166 tracked_object->set_queue_time_sampled(death_data.queue_duration_sample);
167 tracked_object->set_process_type(AsProtobufProcessType(process_type));
168 tracked_object->set_process_id(profiler_data.process_id);
169 }
170}
171
rkaplow@chromium.org86573d12013-07-11 19:48:32172// Round a timestamp measured in seconds since epoch to one with a granularity
173// of an hour. This can be used before uploaded potentially sensitive
174// timestamps.
175int64 RoundSecondsToHour(int64 time_in_seconds) {
176 return 3600 * (time_in_seconds / 3600);
177}
178
isherman@chromium.org1df44b72012-01-19 05:20:34179} // namespace
180
asvitkine@chromium.org9eae4032014-04-09 19:15:19181MetricsLog::MetricsLog(const std::string& client_id,
182 int session_id,
isherman@chromium.org09dee82d2014-05-22 14:00:53183 LogType log_type,
blundell@chromium.org24f81ca2014-05-26 15:59:34184 metrics::MetricsServiceClient* client,
185 PrefService* local_state)
isherman@chromium.org09dee82d2014-05-22 14:00:53186 : MetricsLogBase(client_id,
187 session_id,
188 log_type,
189 client->GetVersionString()),
190 client_(client),
blundell@chromium.org24f81ca2014-05-26 15:59:34191 creation_time_(base::TimeTicks::Now()),
192 local_state_(local_state) {
isherman@chromium.org09dee82d2014-05-22 14:00:53193 uma_proto()->mutable_system_profile()->set_channel(client_->GetChannel());
tengs@chromium.orgafc03f02013-10-11 06:01:35194}
jar@chromium.org5ed7d4572009-12-23 17:42:41195
ananta@chromium.org1226abb2010-06-10 18:01:28196MetricsLog::~MetricsLog() {}
initial.commit09911bf2008-07-26 23:55:29197
asvitkine@chromium.org85791b0b2014-05-20 15:18:58198void MetricsLog::RecordStabilityMetrics(
199 const std::vector<metrics::MetricsProvider*>& metrics_providers,
200 base::TimeDelta incremental_uptime,
201 base::TimeDelta uptime) {
isherman@chromium.org767c9d92012-03-02 16:04:34202 DCHECK(!locked());
asvitkine@chromium.org0f2f7792013-11-28 16:09:14203 DCHECK(HasEnvironment());
204 DCHECK(!HasStabilityMetrics());
jar@google.com0b33f80b2008-12-17 21:34:36205
blundell@chromium.org24f81ca2014-05-26 15:59:34206 PrefService* pref = local_state_;
jar@google.com0b33f80b2008-12-17 21:34:36207 DCHECK(pref);
208
initial.commit09911bf2008-07-26 23:55:29209 // Get stability attributes out of Local State, zeroing out stored values.
210 // NOTE: This could lead to some data loss if this report isn't successfully
211 // sent, but that's true for all the metrics.
212
jar@google.com147bbc0b2009-01-06 19:37:40213 WriteRequiredStabilityAttributes(pref);
asvitkine@chromium.org0edf8762013-11-21 18:33:30214
215 // Record recent delta for critical stability metrics. We can't wait for a
216 // restart to gather these, as that delay biases our observation away from
217 // users that run happily for a looooong time. We send increments with each
218 // uma log upload, just as we send histogram data.
mpearson@chromium.org076961c2014-03-12 22:23:56219 WriteRealtimeStabilityAttributes(pref, incremental_uptime, uptime);
initial.commit09911bf2008-07-26 23:55:29220
asvitkine@chromium.org48ff2c7f2014-05-23 09:57:45221 SystemProfileProto* system_profile = uma_proto()->mutable_system_profile();
asvitkine@chromium.org85791b0b2014-05-20 15:18:58222 for (size_t i = 0; i < metrics_providers.size(); ++i)
asvitkine@chromium.org48ff2c7f2014-05-23 09:57:45223 metrics_providers[i]->ProvideStabilityMetrics(system_profile);
asvitkine@chromium.org85791b0b2014-05-20 15:18:58224
asvitkine@chromium.org0edf8762013-11-21 18:33:30225 // Omit some stats unless this is the initial stability log.
asvitkine@chromium.org9eae4032014-04-09 19:15:19226 if (log_type() != INITIAL_STABILITY_LOG)
asvitkine@chromium.org0edf8762013-11-21 18:33:30227 return;
228
isherman@chromium.orgfe58acc22012-02-29 01:29:58229 int incomplete_shutdown_count =
230 pref->GetInteger(prefs::kStabilityIncompleteSessionEndCount);
cpu@chromium.orge324f6b2012-02-28 05:43:37231 pref->SetInteger(prefs::kStabilityIncompleteSessionEndCount, 0);
isherman@chromium.orgfe58acc22012-02-29 01:29:58232 int breakpad_registration_success_count =
233 pref->GetInteger(prefs::kStabilityBreakpadRegistrationSuccess);
cpu@chromium.orge324f6b2012-02-28 05:43:37234 pref->SetInteger(prefs::kStabilityBreakpadRegistrationSuccess, 0);
isherman@chromium.orgfe58acc22012-02-29 01:29:58235 int breakpad_registration_failure_count =
236 pref->GetInteger(prefs::kStabilityBreakpadRegistrationFail);
cpu@chromium.orge324f6b2012-02-28 05:43:37237 pref->SetInteger(prefs::kStabilityBreakpadRegistrationFail, 0);
isherman@chromium.orgfe58acc22012-02-29 01:29:58238 int debugger_present_count =
239 pref->GetInteger(prefs::kStabilityDebuggerPresent);
cpu@chromium.orge324f6b2012-02-28 05:43:37240 pref->SetInteger(prefs::kStabilityDebuggerPresent, 0);
isherman@chromium.orgfe58acc22012-02-29 01:29:58241 int debugger_not_present_count =
242 pref->GetInteger(prefs::kStabilityDebuggerNotPresent);
cpu@chromium.orge324f6b2012-02-28 05:43:37243 pref->SetInteger(prefs::kStabilityDebuggerNotPresent, 0);
isherman@chromium.orgb2a4812d2012-02-28 05:31:31244
isherman@chromium.orgfe58acc22012-02-29 01:29:58245 // TODO(jar): The following are all optional, so we *could* optimize them for
246 // values of zero (and not include them).
asvitkine@chromium.org48ff2c7f2014-05-23 09:57:45247 SystemProfileProto::Stability* stability =
248 system_profile->mutable_stability();
isherman@chromium.orgfe58acc22012-02-29 01:29:58249 stability->set_incomplete_shutdown_count(incomplete_shutdown_count);
250 stability->set_breakpad_registration_success_count(
251 breakpad_registration_success_count);
252 stability->set_breakpad_registration_failure_count(
253 breakpad_registration_failure_count);
254 stability->set_debugger_present_count(debugger_present_count);
255 stability->set_debugger_not_present_count(debugger_not_present_count);
asvitkine@chromium.org0edf8762013-11-21 18:33:30256}
isherman@chromium.orgfe58acc22012-02-29 01:29:58257
asvitkine@chromium.org85791b0b2014-05-20 15:18:58258void MetricsLog::RecordGeneralMetrics(
259 const std::vector<metrics::MetricsProvider*>& metrics_providers) {
260 for (size_t i = 0; i < metrics_providers.size(); ++i)
261 metrics_providers[i]->ProvideGeneralMetrics(uma_proto());
262}
263
asvitkine@chromium.org0edf8762013-11-21 18:33:30264void MetricsLog::GetFieldTrialIds(
265 std::vector<ActiveGroupId>* field_trial_ids) const {
asvitkine@chromium.orgb3610d42014-05-19 18:07:23266 variations::GetFieldTrialActiveGroupIds(field_trial_ids);
jar@google.com147bbc0b2009-01-06 19:37:40267}
268
asvitkine@chromium.org0f2f7792013-11-28 16:09:14269bool MetricsLog::HasEnvironment() const {
270 return uma_proto()->system_profile().has_uma_enabled_date();
271}
272
273bool MetricsLog::HasStabilityMetrics() const {
274 return uma_proto()->system_profile().stability().has_launch_count();
275}
276
isherman@chromium.orgfe58acc22012-02-29 01:29:58277// The server refuses data that doesn't have certain values. crashcount and
278// launchcount are currently "required" in the "stability" group.
279// TODO(isherman): Stop writing these attributes specially once the migration to
280// protobufs is complete.
jar@google.com147bbc0b2009-01-06 19:37:40281void MetricsLog::WriteRequiredStabilityAttributes(PrefService* pref) {
isherman@chromium.orgfe58acc22012-02-29 01:29:58282 int launch_count = pref->GetInteger(prefs::kStabilityLaunchCount);
jar@google.com0b33f80b2008-12-17 21:34:36283 pref->SetInteger(prefs::kStabilityLaunchCount, 0);
isherman@chromium.orgfe58acc22012-02-29 01:29:58284 int crash_count = pref->GetInteger(prefs::kStabilityCrashCount);
jar@google.com0b33f80b2008-12-17 21:34:36285 pref->SetInteger(prefs::kStabilityCrashCount, 0);
isherman@chromium.orgfe58acc22012-02-29 01:29:58286
isherman@chromium.orgfe58acc22012-02-29 01:29:58287 SystemProfileProto::Stability* stability =
isherman@chromium.org767c9d92012-03-02 16:04:34288 uma_proto()->mutable_system_profile()->mutable_stability();
isherman@chromium.orgfe58acc22012-02-29 01:29:58289 stability->set_launch_count(launch_count);
290 stability->set_crash_count(crash_count);
jar@google.com0b33f80b2008-12-17 21:34:36291}
292
asvitkine@chromium.orgc68a2b9b2013-10-09 18:16:36293void MetricsLog::WriteRealtimeStabilityAttributes(
294 PrefService* pref,
mpearson@chromium.org076961c2014-03-12 22:23:56295 base::TimeDelta incremental_uptime,
296 base::TimeDelta uptime) {
jar@google.com0b33f80b2008-12-17 21:34:36297 // Update the stats which are critical for real-time stability monitoring.
298 // Since these are "optional," only list ones that are non-zero, as the counts
asvitkine@chromium.org250f7b662013-11-23 02:36:51299 // are aggregated (summed) server side.
jar@google.com0b33f80b2008-12-17 21:34:36300
isherman@chromium.orgfe58acc22012-02-29 01:29:58301 SystemProfileProto::Stability* stability =
isherman@chromium.org767c9d92012-03-02 16:04:34302 uma_proto()->mutable_system_profile()->mutable_stability();
jar@google.com0b33f80b2008-12-17 21:34:36303
mpearson@chromium.org076961c2014-03-12 22:23:56304 const uint64 incremental_uptime_sec = incremental_uptime.InSeconds();
305 if (incremental_uptime_sec)
306 stability->set_incremental_uptime_sec(incremental_uptime_sec);
307 const uint64 uptime_sec = uptime.InSeconds();
asvitkine@chromium.orgc68a2b9b2013-10-09 18:16:36308 if (uptime_sec)
309 stability->set_uptime_sec(uptime_sec);
jar@google.com0b33f80b2008-12-17 21:34:36310}
311
initial.commit09911bf2008-07-26 23:55:29312void MetricsLog::RecordEnvironment(
asvitkine@chromium.org85791b0b2014-05-20 15:18:58313 const std::vector<metrics::MetricsProvider*>& metrics_providers,
asvitkine@chromium.orgb3610d42014-05-19 18:07:23314 const std::vector<variations::ActiveGroupId>& synthetic_trials) {
asvitkine@chromium.org0f2f7792013-11-28 16:09:14315 DCHECK(!HasEnvironment());
316
isherman@chromium.orgbc66d532012-03-23 01:57:05317 SystemProfileProto* system_profile = uma_proto()->mutable_system_profile();
mpearson@chromium.org24b9bb392013-01-29 20:29:29318
319 std::string brand_code;
isherman@chromium.org09dee82d2014-05-22 14:00:53320 if (client_->GetBrand(&brand_code))
mpearson@chromium.org24b9bb392013-01-29 20:29:29321 system_profile->set_brand_code(brand_code);
322
isherman@chromium.orgcc5d7f42012-10-30 00:30:09323 int enabled_date;
blundell@chromium.org24f81ca2014-05-26 15:59:34324 bool success =
325 base::StringToInt(GetMetricsEnabledDate(local_state_), &enabled_date);
isherman@chromium.orgbc66d532012-03-23 01:57:05326 DCHECK(success);
rkaplow@chromium.org86573d12013-07-11 19:48:32327
328 // Reduce granularity of the enabled_date field to nearest hour.
329 system_profile->set_uma_enabled_date(RoundSecondsToHour(enabled_date));
330
blundell@chromium.org24f81ca2014-05-26 15:59:34331 int64 install_date = local_state_->GetInt64(prefs::kInstallDate);
rkaplow@chromium.org86573d12013-07-11 19:48:32332
333 // Reduce granularity of the install_date field to nearest hour.
334 system_profile->set_install_date(RoundSecondsToHour(install_date));
isherman@chromium.orgbc66d532012-03-23 01:57:05335
isherman@chromium.org09dee82d2014-05-22 14:00:53336 system_profile->set_application_locale(client_->GetApplicationLocale());
isherman@chromium.orgbc66d532012-03-23 01:57:05337
338 SystemProfileProto::Hardware* hardware = system_profile->mutable_hardware();
blundell@chromium.org51994b22014-05-30 13:24:21339
340 // By default, the hardware class is empty (i.e., unknown).
341 hardware->set_hardware_class(std::string());
342
thestig@chromium.org0b6a4fb2012-10-16 01:58:21343 hardware->set_cpu_architecture(base::SysInfo::OperatingSystemArchitecture());
isherman@chromium.orgbc66d532012-03-23 01:57:05344 hardware->set_system_ram_mb(base::SysInfo::AmountOfPhysicalMemoryMB());
345#if defined(OS_WIN)
346 hardware->set_dll_base(reinterpret_cast<uint64>(&__ImageBase));
347#endif
348
isherman@chromium.orgbc66d532012-03-23 01:57:05349 SystemProfileProto::OS* os = system_profile->mutable_os();
isherman@chromium.org1bb25e02012-08-03 22:39:39350 std::string os_name = base::SysInfo::OperatingSystemName();
351#if defined(OS_WIN)
352 // TODO(mad): This only checks whether the main process is a Metro process at
353 // upload time; not whether the collected metrics were all gathered from
354 // Metro. This is ok as an approximation for now, since users will rarely be
355 // switching from Metro to Desktop mode; but we should re-evaluate whether we
356 // can distinguish metrics more cleanly in the future: http://crbug.com/140568
357 if (base::win::IsMetroProcess())
358 os_name += " (Metro)";
359#endif
360 os->set_name(os_name);
isherman@chromium.orgbc66d532012-03-23 01:57:05361 os->set_version(base::SysInfo::OperatingSystemVersion());
dfalcantara@chromium.org5106b3a2012-10-03 20:10:44362#if defined(OS_ANDROID)
363 os->set_fingerprint(
364 base::android::BuildInfo::GetInstance()->android_build_fp());
365#endif
isherman@chromium.orgbc66d532012-03-23 01:57:05366
simonjam@chromium.org5c8f89f692013-07-18 11:13:28367 base::CPU cpu_info;
368 SystemProfileProto::Hardware::CPU* cpu = hardware->mutable_cpu();
369 cpu->set_vendor_name(cpu_info.vendor_name());
370 cpu->set_signature(cpu_info.signature());
371
asvitkine@chromium.org0c8b7ad2012-11-06 07:08:14372 std::vector<ActiveGroupId> field_trial_ids;
isherman@chromium.org767c9d92012-03-02 16:04:34373 GetFieldTrialIds(&field_trial_ids);
374 WriteFieldTrials(field_trial_ids, system_profile);
bengr@chromium.org60677562013-11-17 15:52:55375 WriteFieldTrials(synthetic_trials, system_profile);
asharif@chromium.orgf65859e2013-02-04 20:00:25376
asvitkine@chromium.org85791b0b2014-05-20 15:18:58377 for (size_t i = 0; i < metrics_providers.size(); ++i)
378 metrics_providers[i]->ProvideSystemProfileMetrics(system_profile);
379
asvitkine@chromium.org0f2f7792013-11-28 16:09:14380 std::string serialied_system_profile;
381 std::string base64_system_profile;
vadimt@chromium.org33fca122013-12-11 01:48:50382 if (system_profile->SerializeToString(&serialied_system_profile)) {
383 base::Base64Encode(serialied_system_profile, &base64_system_profile);
blundell@chromium.org24f81ca2014-05-26 15:59:34384 PrefService* local_state = local_state_;
asvitkine@chromium.org0f2f7792013-11-28 16:09:14385 local_state->SetString(prefs::kStabilitySavedSystemProfile,
386 base64_system_profile);
387 local_state->SetString(prefs::kStabilitySavedSystemProfileHash,
388 ComputeSHA1(serialied_system_profile));
389 }
390}
391
392bool MetricsLog::LoadSavedEnvironmentFromPrefs() {
blundell@chromium.org24f81ca2014-05-26 15:59:34393 PrefService* local_state = local_state_;
asvitkine@chromium.org0f2f7792013-11-28 16:09:14394 const std::string base64_system_profile =
395 local_state->GetString(prefs::kStabilitySavedSystemProfile);
396 if (base64_system_profile.empty())
397 return false;
398
399 const std::string system_profile_hash =
400 local_state->GetString(prefs::kStabilitySavedSystemProfileHash);
401 local_state->ClearPref(prefs::kStabilitySavedSystemProfile);
402 local_state->ClearPref(prefs::kStabilitySavedSystemProfileHash);
403
404 SystemProfileProto* system_profile = uma_proto()->mutable_system_profile();
405 std::string serialied_system_profile;
406 return base::Base64Decode(base64_system_profile, &serialied_system_profile) &&
407 ComputeSHA1(serialied_system_profile) == system_profile_hash &&
408 system_profile->ParseFromString(serialied_system_profile);
initial.commit09911bf2008-07-26 23:55:29409}
410
isherman@chromium.orged0fd002012-04-25 23:10:34411void MetricsLog::RecordProfilerData(
412 const tracked_objects::ProcessDataSnapshot& process_data,
jam@chromium.orgf3b357692013-03-22 05:16:13413 int process_type) {
isherman@chromium.orged0fd002012-04-25 23:10:34414 DCHECK(!locked());
415
isherman@chromium.org8f829682012-04-27 00:36:49416 if (tracked_objects::GetTimeSourceType() !=
417 tracked_objects::TIME_SOURCE_TYPE_WALL_TIME) {
isherman@chromium.orged0fd002012-04-25 23:10:34418 // We currently only support the default time source, wall clock time.
419 return;
420 }
421
422 ProfilerEventProto* profile;
423 if (!uma_proto()->profiler_event_size()) {
424 // For the first process's data, add a new field to the protocol buffer.
425 profile = uma_proto()->add_profiler_event();
426 profile->set_profile_type(ProfilerEventProto::STARTUP_PROFILE);
427 profile->set_time_source(ProfilerEventProto::WALL_CLOCK_TIME);
428 } else {
429 // For the remaining calls, re-use the existing field.
430 profile = uma_proto()->mutable_profiler_event(0);
431 }
432
433 WriteProfilerData(process_data, process_type, profile);
434}