1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/metrics/metrics_log.h"
#include <algorithm>
#include <string>
#include <vector>
#include "base/base64.h"
#include "base/basictypes.h"
#include "base/bind.h"
#include "base/cpu.h"
#include "base/memory/scoped_ptr.h"
#include "base/prefs/pref_registry_simple.h"
#include "base/prefs/pref_service.h"
#include "base/profiler/alternate_timer.h"
#include "base/sha1.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/sys_info.h"
#include "base/third_party/nspr/prtime.h"
#include "base/time/time.h"
#include "base/tracked_objects.h"
#include "chrome/browser/browser_process.h"
#include "chrome/common/pref_names.h"
#include "components/metrics/metrics_provider.h"
#include "components/metrics/metrics_service_client.h"
#include "components/metrics/proto/profiler_event.pb.h"
#include "components/metrics/proto/system_profile.pb.h"
#include "components/nacl/common/nacl_process_type.h"
#include "components/variations/active_field_trials.h"
#include "content/public/common/content_client.h"
#include "url/gurl.h"
#if defined(OS_ANDROID)
#include "base/android/build_info.h"
#endif
#if defined(OS_WIN)
#include "base/win/metro.h"
// http://blogs.msdn.com/oldnewthing/archive/2004/10/25/247180.aspx
extern "C" IMAGE_DOS_HEADER __ImageBase;
#endif
#if defined(OS_CHROMEOS)
#include "chrome/browser/metrics/metrics_log_chromeos.h"
#endif // OS_CHROMEOS
using metrics::MetricsLogBase;
using metrics::ProfilerEventProto;
using metrics::SystemProfileProto;
using tracked_objects::ProcessDataSnapshot;
typedef variations::ActiveGroupId ActiveGroupId;
namespace {
// Returns the date at which the current metrics client ID was created as
// a string containing seconds since the epoch, or "0" if none was found.
std::string GetMetricsEnabledDate(PrefService* pref) {
if (!pref) {
NOTREACHED();
return "0";
}
return pref->GetString(prefs::kMetricsReportingEnabledTimestamp);
}
ProfilerEventProto::TrackedObject::ProcessType AsProtobufProcessType(
int process_type) {
switch (process_type) {
case content::PROCESS_TYPE_BROWSER:
return ProfilerEventProto::TrackedObject::BROWSER;
case content::PROCESS_TYPE_RENDERER:
return ProfilerEventProto::TrackedObject::RENDERER;
case content::PROCESS_TYPE_PLUGIN:
return ProfilerEventProto::TrackedObject::PLUGIN;
case content::PROCESS_TYPE_WORKER:
return ProfilerEventProto::TrackedObject::WORKER;
case content::PROCESS_TYPE_UTILITY:
return ProfilerEventProto::TrackedObject::UTILITY;
case content::PROCESS_TYPE_ZYGOTE:
return ProfilerEventProto::TrackedObject::ZYGOTE;
case content::PROCESS_TYPE_SANDBOX_HELPER:
return ProfilerEventProto::TrackedObject::SANDBOX_HELPER;
case content::PROCESS_TYPE_GPU:
return ProfilerEventProto::TrackedObject::GPU;
case content::PROCESS_TYPE_PPAPI_PLUGIN:
return ProfilerEventProto::TrackedObject::PPAPI_PLUGIN;
case content::PROCESS_TYPE_PPAPI_BROKER:
return ProfilerEventProto::TrackedObject::PPAPI_BROKER;
case PROCESS_TYPE_NACL_LOADER:
return ProfilerEventProto::TrackedObject::NACL_LOADER;
case PROCESS_TYPE_NACL_BROKER:
return ProfilerEventProto::TrackedObject::NACL_BROKER;
default:
NOTREACHED();
return ProfilerEventProto::TrackedObject::UNKNOWN;
}
}
// Computes a SHA-1 hash of |data| and returns it as a hex string.
std::string ComputeSHA1(const std::string& data) {
const std::string sha1 = base::SHA1HashString(data);
return base::HexEncode(sha1.data(), sha1.size());
}
void WriteFieldTrials(const std::vector<ActiveGroupId>& field_trial_ids,
SystemProfileProto* system_profile) {
for (std::vector<ActiveGroupId>::const_iterator it =
field_trial_ids.begin(); it != field_trial_ids.end(); ++it) {
SystemProfileProto::FieldTrial* field_trial =
system_profile->add_field_trial();
field_trial->set_name_id(it->name);
field_trial->set_group_id(it->group);
}
}
// Maps a thread name by replacing trailing sequence of digits with "*".
// Examples:
// 1. "BrowserBlockingWorker1/23857" => "BrowserBlockingWorker1/*"
// 2. "Chrome_IOThread" => "Chrome_IOThread"
std::string MapThreadName(const std::string& thread_name) {
size_t i = thread_name.length();
while (i > 0 && isdigit(thread_name[i - 1])) {
--i;
}
if (i == thread_name.length())
return thread_name;
return thread_name.substr(0, i) + '*';
}
// Normalizes a source filename (which is platform- and build-method-dependent)
// by extracting the last component of the full file name.
// Example: "c:\b\build\slave\win\build\src\chrome\app\chrome_main.cc" =>
// "chrome_main.cc".
std::string NormalizeFileName(const std::string& file_name) {
const size_t offset = file_name.find_last_of("\\/");
return offset != std::string::npos ? file_name.substr(offset + 1) : file_name;
}
void WriteProfilerData(const ProcessDataSnapshot& profiler_data,
int process_type,
ProfilerEventProto* performance_profile) {
for (std::vector<tracked_objects::TaskSnapshot>::const_iterator it =
profiler_data.tasks.begin();
it != profiler_data.tasks.end(); ++it) {
const tracked_objects::DeathDataSnapshot& death_data = it->death_data;
ProfilerEventProto::TrackedObject* tracked_object =
performance_profile->add_tracked_object();
tracked_object->set_birth_thread_name_hash(
MetricsLogBase::Hash(MapThreadName(it->birth.thread_name)));
tracked_object->set_exec_thread_name_hash(
MetricsLogBase::Hash(MapThreadName(it->death_thread_name)));
tracked_object->set_source_file_name_hash(
MetricsLogBase::Hash(NormalizeFileName(it->birth.location.file_name)));
tracked_object->set_source_function_name_hash(
MetricsLogBase::Hash(it->birth.location.function_name));
tracked_object->set_source_line_number(it->birth.location.line_number);
tracked_object->set_exec_count(death_data.count);
tracked_object->set_exec_time_total(death_data.run_duration_sum);
tracked_object->set_exec_time_sampled(death_data.run_duration_sample);
tracked_object->set_queue_time_total(death_data.queue_duration_sum);
tracked_object->set_queue_time_sampled(death_data.queue_duration_sample);
tracked_object->set_process_type(AsProtobufProcessType(process_type));
tracked_object->set_process_id(profiler_data.process_id);
}
}
// Round a timestamp measured in seconds since epoch to one with a granularity
// of an hour. This can be used before uploaded potentially sensitive
// timestamps.
int64 RoundSecondsToHour(int64 time_in_seconds) {
return 3600 * (time_in_seconds / 3600);
}
} // namespace
MetricsLog::MetricsLog(const std::string& client_id,
int session_id,
LogType log_type,
metrics::MetricsServiceClient* client)
: MetricsLogBase(client_id,
session_id,
log_type,
client->GetVersionString()),
client_(client),
creation_time_(base::TimeTicks::Now()) {
uma_proto()->mutable_system_profile()->set_channel(client_->GetChannel());
#if defined(OS_CHROMEOS)
metrics_log_chromeos_.reset(new MetricsLogChromeOS(uma_proto()));
#endif // OS_CHROMEOS
}
MetricsLog::~MetricsLog() {}
void MetricsLog::RecordStabilityMetrics(
const std::vector<metrics::MetricsProvider*>& metrics_providers,
base::TimeDelta incremental_uptime,
base::TimeDelta uptime) {
DCHECK(!locked());
DCHECK(HasEnvironment());
DCHECK(!HasStabilityMetrics());
PrefService* pref = GetPrefService();
DCHECK(pref);
// Get stability attributes out of Local State, zeroing out stored values.
// NOTE: This could lead to some data loss if this report isn't successfully
// sent, but that's true for all the metrics.
WriteRequiredStabilityAttributes(pref);
// Record recent delta for critical stability metrics. We can't wait for a
// restart to gather these, as that delay biases our observation away from
// users that run happily for a looooong time. We send increments with each
// uma log upload, just as we send histogram data.
WriteRealtimeStabilityAttributes(pref, incremental_uptime, uptime);
SystemProfileProto* system_profile = uma_proto()->mutable_system_profile();
for (size_t i = 0; i < metrics_providers.size(); ++i)
metrics_providers[i]->ProvideStabilityMetrics(system_profile);
// Omit some stats unless this is the initial stability log.
if (log_type() != INITIAL_STABILITY_LOG)
return;
int incomplete_shutdown_count =
pref->GetInteger(prefs::kStabilityIncompleteSessionEndCount);
pref->SetInteger(prefs::kStabilityIncompleteSessionEndCount, 0);
int breakpad_registration_success_count =
pref->GetInteger(prefs::kStabilityBreakpadRegistrationSuccess);
pref->SetInteger(prefs::kStabilityBreakpadRegistrationSuccess, 0);
int breakpad_registration_failure_count =
pref->GetInteger(prefs::kStabilityBreakpadRegistrationFail);
pref->SetInteger(prefs::kStabilityBreakpadRegistrationFail, 0);
int debugger_present_count =
pref->GetInteger(prefs::kStabilityDebuggerPresent);
pref->SetInteger(prefs::kStabilityDebuggerPresent, 0);
int debugger_not_present_count =
pref->GetInteger(prefs::kStabilityDebuggerNotPresent);
pref->SetInteger(prefs::kStabilityDebuggerNotPresent, 0);
// TODO(jar): The following are all optional, so we *could* optimize them for
// values of zero (and not include them).
SystemProfileProto::Stability* stability =
system_profile->mutable_stability();
stability->set_incomplete_shutdown_count(incomplete_shutdown_count);
stability->set_breakpad_registration_success_count(
breakpad_registration_success_count);
stability->set_breakpad_registration_failure_count(
breakpad_registration_failure_count);
stability->set_debugger_present_count(debugger_present_count);
stability->set_debugger_not_present_count(debugger_not_present_count);
}
void MetricsLog::RecordGeneralMetrics(
const std::vector<metrics::MetricsProvider*>& metrics_providers) {
for (size_t i = 0; i < metrics_providers.size(); ++i)
metrics_providers[i]->ProvideGeneralMetrics(uma_proto());
}
PrefService* MetricsLog::GetPrefService() {
return g_browser_process->local_state();
}
void MetricsLog::GetFieldTrialIds(
std::vector<ActiveGroupId>* field_trial_ids) const {
variations::GetFieldTrialActiveGroupIds(field_trial_ids);
}
bool MetricsLog::HasEnvironment() const {
return uma_proto()->system_profile().has_uma_enabled_date();
}
bool MetricsLog::HasStabilityMetrics() const {
return uma_proto()->system_profile().stability().has_launch_count();
}
// The server refuses data that doesn't have certain values. crashcount and
// launchcount are currently "required" in the "stability" group.
// TODO(isherman): Stop writing these attributes specially once the migration to
// protobufs is complete.
void MetricsLog::WriteRequiredStabilityAttributes(PrefService* pref) {
int launch_count = pref->GetInteger(prefs::kStabilityLaunchCount);
pref->SetInteger(prefs::kStabilityLaunchCount, 0);
int crash_count = pref->GetInteger(prefs::kStabilityCrashCount);
pref->SetInteger(prefs::kStabilityCrashCount, 0);
SystemProfileProto::Stability* stability =
uma_proto()->mutable_system_profile()->mutable_stability();
stability->set_launch_count(launch_count);
stability->set_crash_count(crash_count);
}
void MetricsLog::WriteRealtimeStabilityAttributes(
PrefService* pref,
base::TimeDelta incremental_uptime,
base::TimeDelta uptime) {
// Update the stats which are critical for real-time stability monitoring.
// Since these are "optional," only list ones that are non-zero, as the counts
// are aggregated (summed) server side.
SystemProfileProto::Stability* stability =
uma_proto()->mutable_system_profile()->mutable_stability();
int count = pref->GetInteger(prefs::kStabilityChildProcessCrashCount);
if (count) {
stability->set_child_process_crash_count(count);
pref->SetInteger(prefs::kStabilityChildProcessCrashCount, 0);
}
#if defined(OS_CHROMEOS)
metrics_log_chromeos_->WriteRealtimeStabilityAttributes(pref);
#endif // OS_CHROMEOS
const uint64 incremental_uptime_sec = incremental_uptime.InSeconds();
if (incremental_uptime_sec)
stability->set_incremental_uptime_sec(incremental_uptime_sec);
const uint64 uptime_sec = uptime.InSeconds();
if (uptime_sec)
stability->set_uptime_sec(uptime_sec);
}
void MetricsLog::RecordEnvironment(
const std::vector<metrics::MetricsProvider*>& metrics_providers,
const std::vector<variations::ActiveGroupId>& synthetic_trials) {
DCHECK(!HasEnvironment());
SystemProfileProto* system_profile = uma_proto()->mutable_system_profile();
std::string brand_code;
if (client_->GetBrand(&brand_code))
system_profile->set_brand_code(brand_code);
int enabled_date;
bool success = base::StringToInt(GetMetricsEnabledDate(GetPrefService()),
&enabled_date);
DCHECK(success);
// Reduce granularity of the enabled_date field to nearest hour.
system_profile->set_uma_enabled_date(RoundSecondsToHour(enabled_date));
int64 install_date = GetPrefService()->GetInt64(prefs::kInstallDate);
// Reduce granularity of the install_date field to nearest hour.
system_profile->set_install_date(RoundSecondsToHour(install_date));
system_profile->set_application_locale(client_->GetApplicationLocale());
SystemProfileProto::Hardware* hardware = system_profile->mutable_hardware();
hardware->set_cpu_architecture(base::SysInfo::OperatingSystemArchitecture());
hardware->set_system_ram_mb(base::SysInfo::AmountOfPhysicalMemoryMB());
#if defined(OS_WIN)
hardware->set_dll_base(reinterpret_cast<uint64>(&__ImageBase));
#endif
SystemProfileProto::OS* os = system_profile->mutable_os();
std::string os_name = base::SysInfo::OperatingSystemName();
#if defined(OS_WIN)
// TODO(mad): This only checks whether the main process is a Metro process at
// upload time; not whether the collected metrics were all gathered from
// Metro. This is ok as an approximation for now, since users will rarely be
// switching from Metro to Desktop mode; but we should re-evaluate whether we
// can distinguish metrics more cleanly in the future: http://crbug.com/140568
if (base::win::IsMetroProcess())
os_name += " (Metro)";
#endif
os->set_name(os_name);
os->set_version(base::SysInfo::OperatingSystemVersion());
#if defined(OS_ANDROID)
os->set_fingerprint(
base::android::BuildInfo::GetInstance()->android_build_fp());
#endif
base::CPU cpu_info;
SystemProfileProto::Hardware::CPU* cpu = hardware->mutable_cpu();
cpu->set_vendor_name(cpu_info.vendor_name());
cpu->set_signature(cpu_info.signature());
std::vector<ActiveGroupId> field_trial_ids;
GetFieldTrialIds(&field_trial_ids);
WriteFieldTrials(field_trial_ids, system_profile);
WriteFieldTrials(synthetic_trials, system_profile);
#if defined(OS_CHROMEOS)
metrics_log_chromeos_->LogChromeOSMetrics();
#endif // OS_CHROMEOS
for (size_t i = 0; i < metrics_providers.size(); ++i)
metrics_providers[i]->ProvideSystemProfileMetrics(system_profile);
std::string serialied_system_profile;
std::string base64_system_profile;
if (system_profile->SerializeToString(&serialied_system_profile)) {
base::Base64Encode(serialied_system_profile, &base64_system_profile);
PrefService* local_state = GetPrefService();
local_state->SetString(prefs::kStabilitySavedSystemProfile,
base64_system_profile);
local_state->SetString(prefs::kStabilitySavedSystemProfileHash,
ComputeSHA1(serialied_system_profile));
}
}
bool MetricsLog::LoadSavedEnvironmentFromPrefs() {
PrefService* local_state = GetPrefService();
const std::string base64_system_profile =
local_state->GetString(prefs::kStabilitySavedSystemProfile);
if (base64_system_profile.empty())
return false;
const std::string system_profile_hash =
local_state->GetString(prefs::kStabilitySavedSystemProfileHash);
local_state->ClearPref(prefs::kStabilitySavedSystemProfile);
local_state->ClearPref(prefs::kStabilitySavedSystemProfileHash);
SystemProfileProto* system_profile = uma_proto()->mutable_system_profile();
std::string serialied_system_profile;
return base::Base64Decode(base64_system_profile, &serialied_system_profile) &&
ComputeSHA1(serialied_system_profile) == system_profile_hash &&
system_profile->ParseFromString(serialied_system_profile);
}
void MetricsLog::RecordProfilerData(
const tracked_objects::ProcessDataSnapshot& process_data,
int process_type) {
DCHECK(!locked());
if (tracked_objects::GetTimeSourceType() !=
tracked_objects::TIME_SOURCE_TYPE_WALL_TIME) {
// We currently only support the default time source, wall clock time.
return;
}
ProfilerEventProto* profile;
if (!uma_proto()->profiler_event_size()) {
// For the first process's data, add a new field to the protocol buffer.
profile = uma_proto()->add_profiler_event();
profile->set_profile_type(ProfilerEventProto::STARTUP_PROFILE);
profile->set_time_source(ProfilerEventProto::WALL_CLOCK_TIME);
} else {
// For the remaining calls, re-use the existing field.
profile = uma_proto()->mutable_profiler_event(0);
}
WriteProfilerData(process_data, process_type, profile);
}
|