blob: a55fc4a54d4ff3ffbf58baa6648fbe01246e5a50 [file] [log] [blame]
[email protected]4dad9ad82009-11-25 20:47:521// Copyright (c) 2009 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]cd1adc22009-01-16 01:29:225#include "chrome/browser/metrics/metrics_log.h"
initial.commit09911bf2008-07-26 23:55:296
[email protected]978df342009-11-24 06:21:537#include "base/base64.h"
[email protected]d1be67b2008-11-19 20:28:388#include "base/basictypes.h"
initial.commit09911bf2008-07-26 23:55:299#include "base/file_util.h"
10#include "base/file_version_info.h"
11#include "base/md5.h"
12#include "base/scoped_ptr.h"
13#include "base/string_util.h"
[email protected]fadf97f2008-09-18 12:18:1414#include "base/sys_info.h"
initial.commit09911bf2008-07-26 23:55:2915#include "chrome/browser/autocomplete/autocomplete.h"
16#include "chrome/browser/browser_process.h"
initial.commit09911bf2008-07-26 23:55:2917#include "chrome/common/logging_chrome.h"
18#include "chrome/common/pref_names.h"
19#include "chrome/common/pref_service.h"
[email protected]46072d42008-07-28 14:49:3520#include "googleurl/src/gurl.h"
initial.commit09911bf2008-07-26 23:55:2921
22#define OPEN_ELEMENT_FOR_SCOPE(name) ScopedElement scoped_element(this, name)
23
[email protected]e1acf6f2008-10-27 20:43:3324using base::Time;
25using base::TimeDelta;
26
[email protected]d1be67b2008-11-19 20:28:3827// http://blogs.msdn.com/oldnewthing/archive/2004/10/25/247180.aspx
28#if defined(OS_WIN)
29extern "C" IMAGE_DOS_HEADER __ImageBase;
30#endif
31
initial.commit09911bf2008-07-26 23:55:2932// libxml take xmlChar*, which is unsigned char*
33inline const unsigned char* UnsignedChar(const char* input) {
34 return reinterpret_cast<const unsigned char*>(input);
35}
36
37// static
38void MetricsLog::RegisterPrefs(PrefService* local_state) {
39 local_state->RegisterListPref(prefs::kStabilityPluginStats);
initial.commit09911bf2008-07-26 23:55:2940}
41
42MetricsLog::MetricsLog(const std::string& client_id, int session_id)
43 : start_time_(Time::Now()),
[email protected]29d02e52008-12-16 16:58:2744 client_id_(client_id),
45 session_id_(IntToString(session_id)),
initial.commit09911bf2008-07-26 23:55:2946 locked_(false),
47 buffer_(NULL),
48 writer_(NULL),
[email protected]29d02e52008-12-16 16:58:2749 num_events_(0) {
initial.commit09911bf2008-07-26 23:55:2950
51 buffer_ = xmlBufferCreate();
52 DCHECK(buffer_);
53
54 writer_ = xmlNewTextWriterMemory(buffer_, 0);
55 DCHECK(writer_);
56
57 int result = xmlTextWriterSetIndent(writer_, 2);
58 DCHECK_EQ(0, result);
59
60 StartElement("log");
61 WriteAttribute("clientid", client_id_);
62
63 DCHECK_GE(result, 0);
64}
65
66MetricsLog::~MetricsLog() {
67 if (writer_)
68 xmlFreeTextWriter(writer_);
69
70 if (buffer_)
71 xmlBufferFree(buffer_);
72}
73
74void MetricsLog::CloseLog() {
75 DCHECK(!locked_);
76 locked_ = true;
77
78 int result = xmlTextWriterEndDocument(writer_);
79 DCHECK(result >= 0);
80
81 result = xmlTextWriterFlush(writer_);
82 DCHECK(result >= 0);
83}
84
85int MetricsLog::GetEncodedLogSize() {
86 DCHECK(locked_);
87 return buffer_->use;
88}
89
90bool MetricsLog::GetEncodedLog(char* buffer, int buffer_size) {
91 DCHECK(locked_);
92 if (buffer_size < GetEncodedLogSize())
93 return false;
94
95 memcpy(buffer, buffer_->content, GetEncodedLogSize());
96 return true;
97}
98
99int MetricsLog::GetElapsedSeconds() {
100 return static_cast<int>((Time::Now() - start_time_).InSeconds());
101}
102
103std::string MetricsLog::CreateHash(const std::string& value) {
104 MD5Context ctx;
105 MD5Init(&ctx);
106 MD5Update(&ctx, value.data(), value.length());
107
108 MD5Digest digest;
109 MD5Final(&digest, &ctx);
110
[email protected]be6bf5e2009-06-16 13:14:08111 uint64 reverse_uint64;
112 // UMA only uses first 8 chars of hash. We use the above uint64 instead
113 // of a unsigned char[8] so that we don't run into strict aliasing issues
114 // in the LOG statement below when trying to interpret reverse as a uint64.
115 unsigned char* reverse = reinterpret_cast<unsigned char *>(&reverse_uint64);
116 DCHECK(arraysize(digest.a) >= sizeof(reverse_uint64));
117 for (size_t i = 0; i < sizeof(reverse_uint64); ++i)
118 reverse[i] = digest.a[sizeof(reverse_uint64) - i - 1];
[email protected]f88ba61a2009-06-12 16:52:10119 // The following log is VERY helpful when folks add some named histogram into
120 // the code, but forgot to update the descriptive list of histograms. When
121 // that happens, all we get to see (server side) is a hash of the histogram
122 // name. We can then use this logging to find out what histogram name was
123 // being hashed to a given MD5 value by just running the version of Chromium
124 // in question with --enable-logging.
125 LOG(INFO) << "Metrics: Hash numeric [" << value << "]=["
[email protected]be6bf5e2009-06-16 13:14:08126 << reverse_uint64 << "]";
initial.commit09911bf2008-07-26 23:55:29127 return std::string(reinterpret_cast<char*>(digest.a), arraysize(digest.a));
128}
129
130std::string MetricsLog::CreateBase64Hash(const std::string& string) {
131 std::string encoded_digest;
[email protected]978df342009-11-24 06:21:53132 if (base::Base64Encode(CreateHash(string), &encoded_digest)) {
initial.commit09911bf2008-07-26 23:55:29133 DLOG(INFO) << "Metrics: Hash [" << encoded_digest << "]=[" << string << "]";
134 return encoded_digest;
initial.commit09911bf2008-07-26 23:55:29135 }
[email protected]a9bb6f692008-07-30 16:40:10136 return std::string();
initial.commit09911bf2008-07-26 23:55:29137}
138
[email protected]afe3a1672009-11-17 19:04:12139void MetricsLog::RecordUserAction(const char* key) {
initial.commit09911bf2008-07-26 23:55:29140 DCHECK(!locked_);
141
[email protected]afe3a1672009-11-17 19:04:12142 std::string command_hash = CreateBase64Hash(key);
initial.commit09911bf2008-07-26 23:55:29143 if (command_hash.empty()) {
144 NOTREACHED() << "Unable generate encoded hash of command: " << key;
145 return;
146 }
147
[email protected]ffaf78a2008-11-12 17:38:33148 OPEN_ELEMENT_FOR_SCOPE("uielement");
initial.commit09911bf2008-07-26 23:55:29149 WriteAttribute("action", "command");
150 WriteAttribute("targetidhash", command_hash);
151
152 // TODO(jhughes): Properly track windows.
153 WriteIntAttribute("window", 0);
154 WriteCommonEventAttributes();
initial.commit09911bf2008-07-26 23:55:29155
156 ++num_events_;
157}
158
159void MetricsLog::RecordLoadEvent(int window_id,
160 const GURL& url,
161 PageTransition::Type origin,
162 int session_index,
163 TimeDelta load_time) {
164 DCHECK(!locked_);
165
[email protected]ffaf78a2008-11-12 17:38:33166 OPEN_ELEMENT_FOR_SCOPE("document");
initial.commit09911bf2008-07-26 23:55:29167 WriteAttribute("action", "load");
168 WriteIntAttribute("docid", session_index);
169 WriteIntAttribute("window", window_id);
170 WriteAttribute("loadtime", Int64ToString(load_time.InMilliseconds()));
171
172 std::string origin_string;
173
174 switch (PageTransition::StripQualifier(origin)) {
175 // TODO(jhughes): Some of these mappings aren't right... we need to add
176 // some values to the server's enum.
177 case PageTransition::LINK:
178 case PageTransition::MANUAL_SUBFRAME:
179 origin_string = "link";
180 break;
181
182 case PageTransition::TYPED:
183 origin_string = "typed";
184 break;
185
186 case PageTransition::AUTO_BOOKMARK:
187 origin_string = "bookmark";
188 break;
189
190 case PageTransition::AUTO_SUBFRAME:
191 case PageTransition::RELOAD:
192 origin_string = "refresh";
193 break;
194
195 case PageTransition::GENERATED:
[email protected]0bfc29a2009-04-27 16:15:44196 case PageTransition::KEYWORD:
initial.commit09911bf2008-07-26 23:55:29197 origin_string = "global-history";
198 break;
199
200 case PageTransition::START_PAGE:
201 origin_string = "start-page";
202 break;
203
204 case PageTransition::FORM_SUBMIT:
205 origin_string = "form-submit";
206 break;
207
208 default:
209 NOTREACHED() << "Received an unknown page transition type: " <<
210 PageTransition::StripQualifier(origin);
211 }
212 if (!origin_string.empty())
213 WriteAttribute("origin", origin_string);
214
215 WriteCommonEventAttributes();
initial.commit09911bf2008-07-26 23:55:29216
217 ++num_events_;
218}
219
initial.commit09911bf2008-07-26 23:55:29220void MetricsLog::RecordWindowEvent(WindowEventType type,
221 int window_id,
222 int parent_id) {
223 DCHECK(!locked_);
224
[email protected]ffaf78a2008-11-12 17:38:33225 OPEN_ELEMENT_FOR_SCOPE("window");
initial.commit09911bf2008-07-26 23:55:29226 WriteAttribute("action", WindowEventTypeToString(type));
227 WriteAttribute("windowid", IntToString(window_id));
228 if (parent_id >= 0)
229 WriteAttribute("parent", IntToString(parent_id));
230 WriteCommonEventAttributes();
initial.commit09911bf2008-07-26 23:55:29231
232 ++num_events_;
233}
234
235std::string MetricsLog::GetCurrentTimeString() {
236 return Uint64ToString(Time::Now().ToTimeT());
237}
238
239// These are the attributes that are common to every event.
240void MetricsLog::WriteCommonEventAttributes() {
241 WriteAttribute("session", session_id_);
242 WriteAttribute("time", GetCurrentTimeString());
243}
244
245void MetricsLog::WriteAttribute(const std::string& name,
246 const std::string& value) {
247 DCHECK(!locked_);
248 DCHECK(!name.empty());
249
250 int result = xmlTextWriterWriteAttribute(writer_,
251 UnsignedChar(name.c_str()),
252 UnsignedChar(value.c_str()));
253 DCHECK_GE(result, 0);
254}
255
256void MetricsLog::WriteIntAttribute(const std::string& name, int value) {
257 WriteAttribute(name, IntToString(value));
258}
259
260void MetricsLog::WriteInt64Attribute(const std::string& name, int64 value) {
261 WriteAttribute(name, Int64ToString(value));
262}
263
[email protected]ffaf78a2008-11-12 17:38:33264// static
265const char* MetricsLog::WindowEventTypeToString(WindowEventType type) {
266 switch (type) {
267 case WINDOW_CREATE: return "create";
268 case WINDOW_OPEN: return "open";
269 case WINDOW_CLOSE: return "close";
270 case WINDOW_DESTROY: return "destroy";
271
272 default:
273 NOTREACHED();
274 return "unknown"; // Can't return NULL as this is used in a required
275 // attribute.
276 }
277}
278
initial.commit09911bf2008-07-26 23:55:29279void MetricsLog::StartElement(const char* name) {
280 DCHECK(!locked_);
281 DCHECK(name);
282
283 int result = xmlTextWriterStartElement(writer_, UnsignedChar(name));
284 DCHECK_GE(result, 0);
285}
286
287void MetricsLog::EndElement() {
288 DCHECK(!locked_);
289
290 int result = xmlTextWriterEndElement(writer_);
291 DCHECK_GE(result, 0);
292}
293
[email protected]541f77922009-02-23 21:14:38294// static
295std::string MetricsLog::GetVersionString() {
initial.commit09911bf2008-07-26 23:55:29296 scoped_ptr<FileVersionInfo> version_info(
297 FileVersionInfo::CreateFileVersionInfoForCurrentModule());
298 if (version_info.get()) {
299 std::string version = WideToUTF8(version_info->product_version());
300 if (!version_info->is_official_build())
301 version.append("-devel");
302 return version;
303 } else {
304 NOTREACHED() << "Unable to retrieve version string.";
305 }
306
307 return std::string();
308}
309
310std::string MetricsLog::GetInstallDate() const {
311 PrefService* pref = g_browser_process->local_state();
312 if (pref) {
313 return WideToUTF8(pref->GetString(prefs::kMetricsClientIDTimestamp));
314 } else {
315 NOTREACHED();
316 return "0";
317 }
318}
319
[email protected]0b33f80b2008-12-17 21:34:36320void MetricsLog::RecordIncrementalStabilityElements() {
321 DCHECK(!locked_);
322
323 PrefService* pref = g_browser_process->local_state();
324 DCHECK(pref);
325
[email protected]147bbc0b2009-01-06 19:37:40326 OPEN_ELEMENT_FOR_SCOPE("profile");
327 WriteCommonEventAttributes();
328
329 WriteInstallElement(); // Supply appversion.
330
331 {
332 OPEN_ELEMENT_FOR_SCOPE("stability"); // Minimal set of stability elements.
333 WriteRequiredStabilityAttributes(pref);
334 WriteRealtimeStabilityAttributes(pref);
335
336 WritePluginStabilityElements(pref);
337 }
[email protected]0b33f80b2008-12-17 21:34:36338}
339
initial.commit09911bf2008-07-26 23:55:29340void MetricsLog::WriteStabilityElement() {
341 DCHECK(!locked_);
342
343 PrefService* pref = g_browser_process->local_state();
344 DCHECK(pref);
345
346 // Get stability attributes out of Local State, zeroing out stored values.
347 // NOTE: This could lead to some data loss if this report isn't successfully
348 // sent, but that's true for all the metrics.
349
[email protected]ffaf78a2008-11-12 17:38:33350 OPEN_ELEMENT_FOR_SCOPE("stability");
[email protected]147bbc0b2009-01-06 19:37:40351 WriteRequiredStabilityAttributes(pref);
352 WriteRealtimeStabilityAttributes(pref);
initial.commit09911bf2008-07-26 23:55:29353
[email protected]0b33f80b2008-12-17 21:34:36354 // TODO(jar): The following are all optional, so we *could* optimize them for
355 // values of zero (and not include them).
[email protected]8e674e42008-07-30 16:05:48356 WriteIntAttribute("incompleteshutdowncount",
357 pref->GetInteger(
358 prefs::kStabilityIncompleteSessionEndCount));
359 pref->SetInteger(prefs::kStabilityIncompleteSessionEndCount, 0);
[email protected]0b33f80b2008-12-17 21:34:36360
361
[email protected]e73c01972008-08-13 00:18:24362 WriteIntAttribute("breakpadregistrationok",
363 pref->GetInteger(prefs::kStabilityBreakpadRegistrationSuccess));
364 pref->SetInteger(prefs::kStabilityBreakpadRegistrationSuccess, 0);
365 WriteIntAttribute("breakpadregistrationfail",
366 pref->GetInteger(prefs::kStabilityBreakpadRegistrationFail));
367 pref->SetInteger(prefs::kStabilityBreakpadRegistrationFail, 0);
368 WriteIntAttribute("debuggerpresent",
369 pref->GetInteger(prefs::kStabilityDebuggerPresent));
370 pref->SetInteger(prefs::kStabilityDebuggerPresent, 0);
371 WriteIntAttribute("debuggernotpresent",
372 pref->GetInteger(prefs::kStabilityDebuggerNotPresent));
373 pref->SetInteger(prefs::kStabilityDebuggerNotPresent, 0);
initial.commit09911bf2008-07-26 23:55:29374
375 // Uptime is stored as a string, since there's no int64 in Value/JSON.
376 WriteAttribute("uptimesec",
377 WideToUTF8(pref->GetString(prefs::kStabilityUptimeSec)));
378 pref->SetString(prefs::kStabilityUptimeSec, L"0");
379
[email protected]147bbc0b2009-01-06 19:37:40380 WritePluginStabilityElements(pref);
381}
382
383void MetricsLog::WritePluginStabilityElements(PrefService* pref) {
384 // Now log plugin stability info.
initial.commit09911bf2008-07-26 23:55:29385 const ListValue* plugin_stats_list = pref->GetList(
386 prefs::kStabilityPluginStats);
387 if (plugin_stats_list) {
[email protected]ffaf78a2008-11-12 17:38:33388 OPEN_ELEMENT_FOR_SCOPE("plugins");
initial.commit09911bf2008-07-26 23:55:29389 for (ListValue::const_iterator iter = plugin_stats_list->begin();
390 iter != plugin_stats_list->end(); ++iter) {
391 if (!(*iter)->IsType(Value::TYPE_DICTIONARY)) {
392 NOTREACHED();
393 continue;
394 }
395 DictionaryValue* plugin_dict = static_cast<DictionaryValue*>(*iter);
396
[email protected]8e50b602009-03-03 22:59:43397 std::wstring plugin_name;
398 plugin_dict->GetString(prefs::kStabilityPluginName, &plugin_name);
initial.commit09911bf2008-07-26 23:55:29399
[email protected]ffaf78a2008-11-12 17:38:33400 OPEN_ELEMENT_FOR_SCOPE("pluginstability");
[email protected]a27a9382009-02-11 23:55:10401 // Use "filename" instead of "name", otherwise we need to update the
402 // UMA servers.
[email protected]8e50b602009-03-03 22:59:43403 WriteAttribute("filename", CreateBase64Hash(WideToUTF8(plugin_name)));
initial.commit09911bf2008-07-26 23:55:29404
405 int launches = 0;
[email protected]8e50b602009-03-03 22:59:43406 plugin_dict->GetInteger(prefs::kStabilityPluginLaunches, &launches);
initial.commit09911bf2008-07-26 23:55:29407 WriteIntAttribute("launchcount", launches);
408
409 int instances = 0;
[email protected]8e50b602009-03-03 22:59:43410 plugin_dict->GetInteger(prefs::kStabilityPluginInstances, &instances);
initial.commit09911bf2008-07-26 23:55:29411 WriteIntAttribute("instancecount", instances);
412
413 int crashes = 0;
[email protected]8e50b602009-03-03 22:59:43414 plugin_dict->GetInteger(prefs::kStabilityPluginCrashes, &crashes);
initial.commit09911bf2008-07-26 23:55:29415 WriteIntAttribute("crashcount", crashes);
initial.commit09911bf2008-07-26 23:55:29416 }
417
418 pref->ClearPref(prefs::kStabilityPluginStats);
initial.commit09911bf2008-07-26 23:55:29419 }
initial.commit09911bf2008-07-26 23:55:29420}
421
[email protected]147bbc0b2009-01-06 19:37:40422void MetricsLog::WriteRequiredStabilityAttributes(PrefService* pref) {
[email protected]0b33f80b2008-12-17 21:34:36423 // The server refuses data that doesn't have certain values. crashcount and
424 // launchcount are currently "required" in the "stability" group.
425 WriteIntAttribute("launchcount",
426 pref->GetInteger(prefs::kStabilityLaunchCount));
427 pref->SetInteger(prefs::kStabilityLaunchCount, 0);
428 WriteIntAttribute("crashcount",
429 pref->GetInteger(prefs::kStabilityCrashCount));
430 pref->SetInteger(prefs::kStabilityCrashCount, 0);
431}
432
[email protected]147bbc0b2009-01-06 19:37:40433void MetricsLog::WriteRealtimeStabilityAttributes(PrefService* pref) {
[email protected]0b33f80b2008-12-17 21:34:36434 // Update the stats which are critical for real-time stability monitoring.
435 // Since these are "optional," only list ones that are non-zero, as the counts
436 // are aggergated (summed) server side.
437
438 int count = pref->GetInteger(prefs::kStabilityPageLoadCount);
439 if (count) {
440 WriteIntAttribute("pageloadcount", count);
441 pref->SetInteger(prefs::kStabilityPageLoadCount, 0);
442 }
443
444 count = pref->GetInteger(prefs::kStabilityRendererCrashCount);
445 if (count) {
446 WriteIntAttribute("renderercrashcount", count);
447 pref->SetInteger(prefs::kStabilityRendererCrashCount, 0);
448 }
449
[email protected]1f085622009-12-04 05:33:45450 count = pref->GetInteger(prefs::kStabilityExtensionRendererCrashCount);
451 if (count) {
452 WriteIntAttribute("extensionrenderercrashcount", count);
453 pref->SetInteger(prefs::kStabilityExtensionRendererCrashCount, 0);
454 }
455
[email protected]0b33f80b2008-12-17 21:34:36456 count = pref->GetInteger(prefs::kStabilityRendererHangCount);
457 if (count) {
458 WriteIntAttribute("rendererhangcount", count);
459 pref->SetInteger(prefs::kStabilityRendererHangCount, 0);
460 }
[email protected]1f085622009-12-04 05:33:45461
462 count = pref->GetInteger(prefs::kStabilityChildProcessCrashCount);
463 if (count) {
464 WriteIntAttribute("childprocesscrashcount", count);
465 pref->SetInteger(prefs::kStabilityChildProcessCrashCount, 0);
466 }
[email protected]0b33f80b2008-12-17 21:34:36467}
468
initial.commit09911bf2008-07-26 23:55:29469void MetricsLog::WritePluginList(
470 const std::vector<WebPluginInfo>& plugin_list) {
471 DCHECK(!locked_);
472
[email protected]ffaf78a2008-11-12 17:38:33473 OPEN_ELEMENT_FOR_SCOPE("plugins");
initial.commit09911bf2008-07-26 23:55:29474
475 for (std::vector<WebPluginInfo>::const_iterator iter = plugin_list.begin();
476 iter != plugin_list.end(); ++iter) {
[email protected]ffaf78a2008-11-12 17:38:33477 OPEN_ELEMENT_FOR_SCOPE("plugin");
initial.commit09911bf2008-07-26 23:55:29478
479 // Plugin name and filename are hashed for the privacy of those
480 // testing unreleased new extensions.
[email protected]b7344502009-01-12 19:43:44481 WriteAttribute("name", CreateBase64Hash(WideToUTF8(iter->name)));
[email protected]046344c2009-01-13 00:54:21482 WriteAttribute("filename",
483 CreateBase64Hash(WideToUTF8(iter->path.BaseName().ToWStringHack())));
[email protected]b7344502009-01-12 19:43:44484 WriteAttribute("version", WideToUTF8(iter->version));
initial.commit09911bf2008-07-26 23:55:29485 }
initial.commit09911bf2008-07-26 23:55:29486}
487
[email protected]147bbc0b2009-01-06 19:37:40488void MetricsLog::WriteInstallElement() {
489 OPEN_ELEMENT_FOR_SCOPE("install");
490 WriteAttribute("installdate", GetInstallDate());
491 WriteIntAttribute("buildid", 0); // We're using appversion instead.
492 WriteAttribute("appversion", GetVersionString());
493}
494
initial.commit09911bf2008-07-26 23:55:29495void MetricsLog::RecordEnvironment(
496 const std::vector<WebPluginInfo>& plugin_list,
497 const DictionaryValue* profile_metrics) {
498 DCHECK(!locked_);
499
500 PrefService* pref = g_browser_process->local_state();
501
[email protected]ffaf78a2008-11-12 17:38:33502 OPEN_ELEMENT_FOR_SCOPE("profile");
initial.commit09911bf2008-07-26 23:55:29503 WriteCommonEventAttributes();
504
[email protected]147bbc0b2009-01-06 19:37:40505 WriteInstallElement();
initial.commit09911bf2008-07-26 23:55:29506
507 WritePluginList(plugin_list);
508
509 WriteStabilityElement();
510
511 {
512 OPEN_ELEMENT_FOR_SCOPE("cpu");
[email protected]05f9b682008-09-29 22:18:01513 WriteAttribute("arch", base::SysInfo::CPUArchitecture());
initial.commit09911bf2008-07-26 23:55:29514 }
515
516 {
initial.commit09911bf2008-07-26 23:55:29517 OPEN_ELEMENT_FOR_SCOPE("memory");
[email protected]ed6fc352008-09-18 12:44:40518 WriteIntAttribute("mb", base::SysInfo::AmountOfPhysicalMemoryMB());
[email protected]d1be67b2008-11-19 20:28:38519#if defined(OS_WIN)
520 WriteIntAttribute("dllbase", reinterpret_cast<int>(&__ImageBase));
521#endif
initial.commit09911bf2008-07-26 23:55:29522 }
523
524 {
525 OPEN_ELEMENT_FOR_SCOPE("os");
526 WriteAttribute("name",
[email protected]05f9b682008-09-29 22:18:01527 base::SysInfo::OperatingSystemName());
initial.commit09911bf2008-07-26 23:55:29528 WriteAttribute("version",
[email protected]05f9b682008-09-29 22:18:01529 base::SysInfo::OperatingSystemVersion());
initial.commit09911bf2008-07-26 23:55:29530 }
531
532 {
533 OPEN_ELEMENT_FOR_SCOPE("display");
534 int width = 0;
535 int height = 0;
[email protected]05f9b682008-09-29 22:18:01536 base::SysInfo::GetPrimaryDisplayDimensions(&width, &height);
initial.commit09911bf2008-07-26 23:55:29537 WriteIntAttribute("xsize", width);
538 WriteIntAttribute("ysize", height);
[email protected]05f9b682008-09-29 22:18:01539 WriteIntAttribute("screens", base::SysInfo::DisplayCount());
initial.commit09911bf2008-07-26 23:55:29540 }
541
542 {
543 OPEN_ELEMENT_FOR_SCOPE("bookmarks");
544 int num_bookmarks_on_bookmark_bar =
545 pref->GetInteger(prefs::kNumBookmarksOnBookmarkBar);
546 int num_folders_on_bookmark_bar =
547 pref->GetInteger(prefs::kNumFoldersOnBookmarkBar);
548 int num_bookmarks_in_other_bookmarks_folder =
549 pref->GetInteger(prefs::kNumBookmarksInOtherBookmarkFolder);
550 int num_folders_in_other_bookmarks_folder =
551 pref->GetInteger(prefs::kNumFoldersInOtherBookmarkFolder);
552 {
553 OPEN_ELEMENT_FOR_SCOPE("bookmarklocation");
554 WriteAttribute("name", "full-tree");
555 WriteIntAttribute("foldercount",
556 num_folders_on_bookmark_bar + num_folders_in_other_bookmarks_folder);
557 WriteIntAttribute("itemcount",
558 num_bookmarks_on_bookmark_bar +
559 num_bookmarks_in_other_bookmarks_folder);
560 }
561 {
562 OPEN_ELEMENT_FOR_SCOPE("bookmarklocation");
563 WriteAttribute("name", "toolbar");
564 WriteIntAttribute("foldercount", num_folders_on_bookmark_bar);
565 WriteIntAttribute("itemcount", num_bookmarks_on_bookmark_bar);
566 }
567 }
568
569 {
570 OPEN_ELEMENT_FOR_SCOPE("keywords");
571 WriteIntAttribute("count", pref->GetInteger(prefs::kNumKeywords));
572 }
573
574 if (profile_metrics)
575 WriteAllProfilesMetrics(*profile_metrics);
initial.commit09911bf2008-07-26 23:55:29576}
577
578void MetricsLog::WriteAllProfilesMetrics(
579 const DictionaryValue& all_profiles_metrics) {
580 const std::wstring profile_prefix(prefs::kProfilePrefix);
581 for (DictionaryValue::key_iterator i = all_profiles_metrics.begin_keys();
582 i != all_profiles_metrics.end_keys(); ++i) {
[email protected]8e50b602009-03-03 22:59:43583 const std::wstring& key_name = *i;
initial.commit09911bf2008-07-26 23:55:29584 if (key_name.compare(0, profile_prefix.size(), profile_prefix) == 0) {
585 DictionaryValue* profile;
[email protected]4dad9ad82009-11-25 20:47:52586 if (all_profiles_metrics.GetDictionaryWithoutPathExpansion(key_name,
587 &profile))
initial.commit09911bf2008-07-26 23:55:29588 WriteProfileMetrics(key_name.substr(profile_prefix.size()), *profile);
589 }
590 }
591}
592
593void MetricsLog::WriteProfileMetrics(const std::wstring& profileidhash,
594 const DictionaryValue& profile_metrics) {
595 OPEN_ELEMENT_FOR_SCOPE("userprofile");
596 WriteAttribute("profileidhash", WideToUTF8(profileidhash));
597 for (DictionaryValue::key_iterator i = profile_metrics.begin_keys();
598 i != profile_metrics.end_keys(); ++i) {
599 Value* value;
[email protected]4dad9ad82009-11-25 20:47:52600 if (profile_metrics.GetWithoutPathExpansion(*i, &value)) {
[email protected]8e50b602009-03-03 22:59:43601 DCHECK(*i != L"id");
initial.commit09911bf2008-07-26 23:55:29602 switch (value->GetType()) {
603 case Value::TYPE_STRING: {
[email protected]5e324b72008-12-18 00:07:59604 std::string string_value;
initial.commit09911bf2008-07-26 23:55:29605 if (value->GetAsString(&string_value)) {
606 OPEN_ELEMENT_FOR_SCOPE("profileparam");
[email protected]8e50b602009-03-03 22:59:43607 WriteAttribute("name", WideToUTF8(*i));
[email protected]5e324b72008-12-18 00:07:59608 WriteAttribute("value", string_value);
initial.commit09911bf2008-07-26 23:55:29609 }
610 break;
611 }
612
613 case Value::TYPE_BOOLEAN: {
614 bool bool_value;
615 if (value->GetAsBoolean(&bool_value)) {
616 OPEN_ELEMENT_FOR_SCOPE("profileparam");
[email protected]8e50b602009-03-03 22:59:43617 WriteAttribute("name", WideToUTF8(*i));
initial.commit09911bf2008-07-26 23:55:29618 WriteIntAttribute("value", bool_value ? 1 : 0);
619 }
620 break;
621 }
622
623 case Value::TYPE_INTEGER: {
624 int int_value;
625 if (value->GetAsInteger(&int_value)) {
626 OPEN_ELEMENT_FOR_SCOPE("profileparam");
[email protected]8e50b602009-03-03 22:59:43627 WriteAttribute("name", WideToUTF8(*i));
initial.commit09911bf2008-07-26 23:55:29628 WriteIntAttribute("value", int_value);
629 }
630 break;
631 }
632
633 default:
634 NOTREACHED();
635 break;
636 }
637 }
638 }
639}
640
641void MetricsLog::RecordOmniboxOpenedURL(const AutocompleteLog& log) {
642 DCHECK(!locked_);
643
[email protected]ffaf78a2008-11-12 17:38:33644 OPEN_ELEMENT_FOR_SCOPE("uielement");
initial.commit09911bf2008-07-26 23:55:29645 WriteAttribute("action", "autocomplete");
646 WriteAttribute("targetidhash", "");
647 // TODO(kochi): Properly track windows.
648 WriteIntAttribute("window", 0);
649 WriteCommonEventAttributes();
650
[email protected]ffaf78a2008-11-12 17:38:33651 {
652 OPEN_ELEMENT_FOR_SCOPE("autocomplete");
initial.commit09911bf2008-07-26 23:55:29653
[email protected]ffaf78a2008-11-12 17:38:33654 WriteIntAttribute("typedlength", static_cast<int>(log.text.length()));
655 WriteIntAttribute("selectedindex", static_cast<int>(log.selected_index));
656 WriteIntAttribute("completedlength",
657 static_cast<int>(log.inline_autocompleted_length));
[email protected]381e2992008-12-16 01:41:00658 const std::string input_type(
659 AutocompleteInput::TypeToString(log.input_type));
660 if (!input_type.empty())
661 WriteAttribute("inputtype", input_type);
initial.commit09911bf2008-07-26 23:55:29662
[email protected]ffaf78a2008-11-12 17:38:33663 for (AutocompleteResult::const_iterator i(log.result.begin());
664 i != log.result.end(); ++i) {
665 OPEN_ELEMENT_FOR_SCOPE("autocompleteitem");
666 if (i->provider)
667 WriteAttribute("provider", i->provider->name());
[email protected]381e2992008-12-16 01:41:00668 const std::string result_type(AutocompleteMatch::TypeToString(i->type));
669 if (!result_type.empty())
670 WriteAttribute("resulttype", result_type);
[email protected]ffaf78a2008-11-12 17:38:33671 WriteIntAttribute("relevance", i->relevance);
672 WriteIntAttribute("isstarred", i->starred ? 1 : 0);
673 }
initial.commit09911bf2008-07-26 23:55:29674 }
initial.commit09911bf2008-07-26 23:55:29675
676 ++num_events_;
677}
678
679// TODO(JAR): A The following should really be part of the histogram class.
680// Internal state is being needlessly exposed, and it would be hard to reuse
681// this code. If we moved this into the Histogram class, then we could use
682// the same infrastructure for logging StatsCounters, RatesCounters, etc.
683void MetricsLog::RecordHistogramDelta(const Histogram& histogram,
684 const Histogram::SampleSet& snapshot) {
685 DCHECK(!locked_);
686 DCHECK(0 != snapshot.TotalCount());
687 snapshot.CheckSize(histogram);
688
689 // We will ignore the MAX_INT/infinite value in the last element of range[].
690
691 OPEN_ELEMENT_FOR_SCOPE("histogram");
692
693 WriteAttribute("name", CreateBase64Hash(histogram.histogram_name()));
694
695 WriteInt64Attribute("sum", snapshot.sum());
696 WriteInt64Attribute("sumsquares", snapshot.square_sum());
697
698 for (size_t i = 0; i < histogram.bucket_count(); i++) {
699 if (snapshot.counts(i)) {
700 OPEN_ELEMENT_FOR_SCOPE("histogrambucket");
701 WriteIntAttribute("min", histogram.ranges(i));
702 WriteIntAttribute("max", histogram.ranges(i + 1));
703 WriteIntAttribute("count", snapshot.counts(i));
704 }
705 }
706}