license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 1 | // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | cd1adc2 | 2009-01-16 01:29:22 | [diff] [blame] | 5 | #include "chrome/browser/metrics/metrics_log.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 6 | |
[email protected] | d1be67b | 2008-11-19 20:28:38 | [diff] [blame] | 7 | #include "base/basictypes.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 8 | #include "base/file_util.h" |
| 9 | #include "base/file_version_info.h" |
| 10 | #include "base/md5.h" |
| 11 | #include "base/scoped_ptr.h" |
| 12 | #include "base/string_util.h" |
[email protected] | fadf97f | 2008-09-18 12:18:14 | [diff] [blame] | 13 | #include "base/sys_info.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 14 | #include "chrome/browser/autocomplete/autocomplete.h" |
| 15 | #include "chrome/browser/browser_process.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 16 | #include "chrome/common/logging_chrome.h" |
| 17 | #include "chrome/common/pref_names.h" |
| 18 | #include "chrome/common/pref_service.h" |
[email protected] | 46072d4 | 2008-07-28 14:49:35 | [diff] [blame] | 19 | #include "googleurl/src/gurl.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 20 | #include "net/base/base64.h" |
| 21 | |
| 22 | #define OPEN_ELEMENT_FOR_SCOPE(name) ScopedElement scoped_element(this, name) |
| 23 | |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 24 | using base::Time; |
| 25 | using base::TimeDelta; |
| 26 | |
[email protected] | d1be67b | 2008-11-19 20:28:38 | [diff] [blame] | 27 | // http://blogs.msdn.com/oldnewthing/archive/2004/10/25/247180.aspx |
| 28 | #if defined(OS_WIN) |
| 29 | extern "C" IMAGE_DOS_HEADER __ImageBase; |
| 30 | #endif |
| 31 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 32 | // libxml take xmlChar*, which is unsigned char* |
| 33 | inline const unsigned char* UnsignedChar(const char* input) { |
| 34 | return reinterpret_cast<const unsigned char*>(input); |
| 35 | } |
| 36 | |
| 37 | // static |
| 38 | void MetricsLog::RegisterPrefs(PrefService* local_state) { |
| 39 | local_state->RegisterListPref(prefs::kStabilityPluginStats); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | MetricsLog::MetricsLog(const std::string& client_id, int session_id) |
| 43 | : start_time_(Time::Now()), |
[email protected] | 29d02e5 | 2008-12-16 16:58:27 | [diff] [blame] | 44 | client_id_(client_id), |
| 45 | session_id_(IntToString(session_id)), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 46 | locked_(false), |
| 47 | buffer_(NULL), |
| 48 | writer_(NULL), |
[email protected] | 29d02e5 | 2008-12-16 16:58:27 | [diff] [blame] | 49 | num_events_(0) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 50 | |
| 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 | |
| 66 | MetricsLog::~MetricsLog() { |
| 67 | if (writer_) |
| 68 | xmlFreeTextWriter(writer_); |
| 69 | |
| 70 | if (buffer_) |
| 71 | xmlBufferFree(buffer_); |
| 72 | } |
| 73 | |
| 74 | void 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 | |
| 85 | int MetricsLog::GetEncodedLogSize() { |
| 86 | DCHECK(locked_); |
| 87 | return buffer_->use; |
| 88 | } |
| 89 | |
| 90 | bool 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 | |
| 99 | int MetricsLog::GetElapsedSeconds() { |
| 100 | return static_cast<int>((Time::Now() - start_time_).InSeconds()); |
| 101 | } |
| 102 | |
| 103 | std::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 | |
| 111 | unsigned char reverse[8]; // UMA only uses first 8 chars of hash. |
| 112 | DCHECK(arraysize(digest.a) >= arraysize(reverse)); |
[email protected] | 29d02e5 | 2008-12-16 16:58:27 | [diff] [blame] | 113 | for (size_t i = 0; i < arraysize(reverse); ++i) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 114 | reverse[i] = digest.a[arraysize(reverse) - i - 1]; |
| 115 | LOG(INFO) << "Metrics: Hash numeric [" << value << "]=[" |
| 116 | << *reinterpret_cast<const uint64*>(&reverse[0]) << "]"; |
| 117 | return std::string(reinterpret_cast<char*>(digest.a), arraysize(digest.a)); |
| 118 | } |
| 119 | |
| 120 | std::string MetricsLog::CreateBase64Hash(const std::string& string) { |
| 121 | std::string encoded_digest; |
[email protected] | a9bb6f69 | 2008-07-30 16:40:10 | [diff] [blame] | 122 | if (net::Base64Encode(CreateHash(string), &encoded_digest)) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 123 | DLOG(INFO) << "Metrics: Hash [" << encoded_digest << "]=[" << string << "]"; |
| 124 | return encoded_digest; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 125 | } |
[email protected] | a9bb6f69 | 2008-07-30 16:40:10 | [diff] [blame] | 126 | return std::string(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | void MetricsLog::RecordUserAction(const wchar_t* key) { |
| 130 | DCHECK(!locked_); |
| 131 | |
| 132 | std::string command_hash = CreateBase64Hash(WideToUTF8(key)); |
| 133 | if (command_hash.empty()) { |
| 134 | NOTREACHED() << "Unable generate encoded hash of command: " << key; |
| 135 | return; |
| 136 | } |
| 137 | |
[email protected] | ffaf78a | 2008-11-12 17:38:33 | [diff] [blame] | 138 | OPEN_ELEMENT_FOR_SCOPE("uielement"); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 139 | WriteAttribute("action", "command"); |
| 140 | WriteAttribute("targetidhash", command_hash); |
| 141 | |
| 142 | // TODO(jhughes): Properly track windows. |
| 143 | WriteIntAttribute("window", 0); |
| 144 | WriteCommonEventAttributes(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 145 | |
| 146 | ++num_events_; |
| 147 | } |
| 148 | |
| 149 | void MetricsLog::RecordLoadEvent(int window_id, |
| 150 | const GURL& url, |
| 151 | PageTransition::Type origin, |
| 152 | int session_index, |
| 153 | TimeDelta load_time) { |
| 154 | DCHECK(!locked_); |
| 155 | |
[email protected] | ffaf78a | 2008-11-12 17:38:33 | [diff] [blame] | 156 | OPEN_ELEMENT_FOR_SCOPE("document"); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 157 | WriteAttribute("action", "load"); |
| 158 | WriteIntAttribute("docid", session_index); |
| 159 | WriteIntAttribute("window", window_id); |
| 160 | WriteAttribute("loadtime", Int64ToString(load_time.InMilliseconds())); |
| 161 | |
| 162 | std::string origin_string; |
| 163 | |
| 164 | switch (PageTransition::StripQualifier(origin)) { |
| 165 | // TODO(jhughes): Some of these mappings aren't right... we need to add |
| 166 | // some values to the server's enum. |
| 167 | case PageTransition::LINK: |
| 168 | case PageTransition::MANUAL_SUBFRAME: |
| 169 | origin_string = "link"; |
| 170 | break; |
| 171 | |
| 172 | case PageTransition::TYPED: |
| 173 | origin_string = "typed"; |
| 174 | break; |
| 175 | |
| 176 | case PageTransition::AUTO_BOOKMARK: |
| 177 | origin_string = "bookmark"; |
| 178 | break; |
| 179 | |
| 180 | case PageTransition::AUTO_SUBFRAME: |
| 181 | case PageTransition::RELOAD: |
| 182 | origin_string = "refresh"; |
| 183 | break; |
| 184 | |
| 185 | case PageTransition::GENERATED: |
| 186 | origin_string = "global-history"; |
| 187 | break; |
| 188 | |
| 189 | case PageTransition::START_PAGE: |
| 190 | origin_string = "start-page"; |
| 191 | break; |
| 192 | |
| 193 | case PageTransition::FORM_SUBMIT: |
| 194 | origin_string = "form-submit"; |
| 195 | break; |
| 196 | |
| 197 | default: |
| 198 | NOTREACHED() << "Received an unknown page transition type: " << |
| 199 | PageTransition::StripQualifier(origin); |
| 200 | } |
| 201 | if (!origin_string.empty()) |
| 202 | WriteAttribute("origin", origin_string); |
| 203 | |
| 204 | WriteCommonEventAttributes(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 205 | |
| 206 | ++num_events_; |
| 207 | } |
| 208 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 209 | void MetricsLog::RecordWindowEvent(WindowEventType type, |
| 210 | int window_id, |
| 211 | int parent_id) { |
| 212 | DCHECK(!locked_); |
| 213 | |
[email protected] | ffaf78a | 2008-11-12 17:38:33 | [diff] [blame] | 214 | OPEN_ELEMENT_FOR_SCOPE("window"); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 215 | WriteAttribute("action", WindowEventTypeToString(type)); |
| 216 | WriteAttribute("windowid", IntToString(window_id)); |
| 217 | if (parent_id >= 0) |
| 218 | WriteAttribute("parent", IntToString(parent_id)); |
| 219 | WriteCommonEventAttributes(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 220 | |
| 221 | ++num_events_; |
| 222 | } |
| 223 | |
| 224 | std::string MetricsLog::GetCurrentTimeString() { |
| 225 | return Uint64ToString(Time::Now().ToTimeT()); |
| 226 | } |
| 227 | |
| 228 | // These are the attributes that are common to every event. |
| 229 | void MetricsLog::WriteCommonEventAttributes() { |
| 230 | WriteAttribute("session", session_id_); |
| 231 | WriteAttribute("time", GetCurrentTimeString()); |
| 232 | } |
| 233 | |
| 234 | void MetricsLog::WriteAttribute(const std::string& name, |
| 235 | const std::string& value) { |
| 236 | DCHECK(!locked_); |
| 237 | DCHECK(!name.empty()); |
| 238 | |
| 239 | int result = xmlTextWriterWriteAttribute(writer_, |
| 240 | UnsignedChar(name.c_str()), |
| 241 | UnsignedChar(value.c_str())); |
| 242 | DCHECK_GE(result, 0); |
| 243 | } |
| 244 | |
| 245 | void MetricsLog::WriteIntAttribute(const std::string& name, int value) { |
| 246 | WriteAttribute(name, IntToString(value)); |
| 247 | } |
| 248 | |
| 249 | void MetricsLog::WriteInt64Attribute(const std::string& name, int64 value) { |
| 250 | WriteAttribute(name, Int64ToString(value)); |
| 251 | } |
| 252 | |
[email protected] | ffaf78a | 2008-11-12 17:38:33 | [diff] [blame] | 253 | // static |
| 254 | const char* MetricsLog::WindowEventTypeToString(WindowEventType type) { |
| 255 | switch (type) { |
| 256 | case WINDOW_CREATE: return "create"; |
| 257 | case WINDOW_OPEN: return "open"; |
| 258 | case WINDOW_CLOSE: return "close"; |
| 259 | case WINDOW_DESTROY: return "destroy"; |
| 260 | |
| 261 | default: |
| 262 | NOTREACHED(); |
| 263 | return "unknown"; // Can't return NULL as this is used in a required |
| 264 | // attribute. |
| 265 | } |
| 266 | } |
| 267 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 268 | void MetricsLog::StartElement(const char* name) { |
| 269 | DCHECK(!locked_); |
| 270 | DCHECK(name); |
| 271 | |
| 272 | int result = xmlTextWriterStartElement(writer_, UnsignedChar(name)); |
| 273 | DCHECK_GE(result, 0); |
| 274 | } |
| 275 | |
| 276 | void MetricsLog::EndElement() { |
| 277 | DCHECK(!locked_); |
| 278 | |
| 279 | int result = xmlTextWriterEndElement(writer_); |
| 280 | DCHECK_GE(result, 0); |
| 281 | } |
| 282 | |
| 283 | std::string MetricsLog::GetVersionString() const { |
| 284 | scoped_ptr<FileVersionInfo> version_info( |
| 285 | FileVersionInfo::CreateFileVersionInfoForCurrentModule()); |
| 286 | if (version_info.get()) { |
| 287 | std::string version = WideToUTF8(version_info->product_version()); |
| 288 | if (!version_info->is_official_build()) |
| 289 | version.append("-devel"); |
| 290 | return version; |
| 291 | } else { |
| 292 | NOTREACHED() << "Unable to retrieve version string."; |
| 293 | } |
| 294 | |
| 295 | return std::string(); |
| 296 | } |
| 297 | |
| 298 | std::string MetricsLog::GetInstallDate() const { |
| 299 | PrefService* pref = g_browser_process->local_state(); |
| 300 | if (pref) { |
| 301 | return WideToUTF8(pref->GetString(prefs::kMetricsClientIDTimestamp)); |
| 302 | } else { |
| 303 | NOTREACHED(); |
| 304 | return "0"; |
| 305 | } |
| 306 | } |
| 307 | |
[email protected] | 0b33f80b | 2008-12-17 21:34:36 | [diff] [blame] | 308 | void MetricsLog::RecordIncrementalStabilityElements() { |
| 309 | DCHECK(!locked_); |
| 310 | |
| 311 | PrefService* pref = g_browser_process->local_state(); |
| 312 | DCHECK(pref); |
| 313 | |
[email protected] | 147bbc0b | 2009-01-06 19:37:40 | [diff] [blame] | 314 | OPEN_ELEMENT_FOR_SCOPE("profile"); |
| 315 | WriteCommonEventAttributes(); |
| 316 | |
| 317 | WriteInstallElement(); // Supply appversion. |
| 318 | |
| 319 | { |
| 320 | OPEN_ELEMENT_FOR_SCOPE("stability"); // Minimal set of stability elements. |
| 321 | WriteRequiredStabilityAttributes(pref); |
| 322 | WriteRealtimeStabilityAttributes(pref); |
| 323 | |
| 324 | WritePluginStabilityElements(pref); |
| 325 | } |
[email protected] | 0b33f80b | 2008-12-17 21:34:36 | [diff] [blame] | 326 | } |
| 327 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 328 | void MetricsLog::WriteStabilityElement() { |
| 329 | DCHECK(!locked_); |
| 330 | |
| 331 | PrefService* pref = g_browser_process->local_state(); |
| 332 | DCHECK(pref); |
| 333 | |
| 334 | // Get stability attributes out of Local State, zeroing out stored values. |
| 335 | // NOTE: This could lead to some data loss if this report isn't successfully |
| 336 | // sent, but that's true for all the metrics. |
| 337 | |
[email protected] | ffaf78a | 2008-11-12 17:38:33 | [diff] [blame] | 338 | OPEN_ELEMENT_FOR_SCOPE("stability"); |
[email protected] | 147bbc0b | 2009-01-06 19:37:40 | [diff] [blame] | 339 | WriteRequiredStabilityAttributes(pref); |
| 340 | WriteRealtimeStabilityAttributes(pref); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 341 | |
[email protected] | 0b33f80b | 2008-12-17 21:34:36 | [diff] [blame] | 342 | // TODO(jar): The following are all optional, so we *could* optimize them for |
| 343 | // values of zero (and not include them). |
[email protected] | 8e674e4 | 2008-07-30 16:05:48 | [diff] [blame] | 344 | WriteIntAttribute("incompleteshutdowncount", |
| 345 | pref->GetInteger( |
| 346 | prefs::kStabilityIncompleteSessionEndCount)); |
| 347 | pref->SetInteger(prefs::kStabilityIncompleteSessionEndCount, 0); |
[email protected] | 0b33f80b | 2008-12-17 21:34:36 | [diff] [blame] | 348 | |
| 349 | |
[email protected] | e73c0197 | 2008-08-13 00:18:24 | [diff] [blame] | 350 | WriteIntAttribute("breakpadregistrationok", |
| 351 | pref->GetInteger(prefs::kStabilityBreakpadRegistrationSuccess)); |
| 352 | pref->SetInteger(prefs::kStabilityBreakpadRegistrationSuccess, 0); |
| 353 | WriteIntAttribute("breakpadregistrationfail", |
| 354 | pref->GetInteger(prefs::kStabilityBreakpadRegistrationFail)); |
| 355 | pref->SetInteger(prefs::kStabilityBreakpadRegistrationFail, 0); |
| 356 | WriteIntAttribute("debuggerpresent", |
| 357 | pref->GetInteger(prefs::kStabilityDebuggerPresent)); |
| 358 | pref->SetInteger(prefs::kStabilityDebuggerPresent, 0); |
| 359 | WriteIntAttribute("debuggernotpresent", |
| 360 | pref->GetInteger(prefs::kStabilityDebuggerNotPresent)); |
| 361 | pref->SetInteger(prefs::kStabilityDebuggerNotPresent, 0); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 362 | |
| 363 | // Uptime is stored as a string, since there's no int64 in Value/JSON. |
| 364 | WriteAttribute("uptimesec", |
| 365 | WideToUTF8(pref->GetString(prefs::kStabilityUptimeSec))); |
| 366 | pref->SetString(prefs::kStabilityUptimeSec, L"0"); |
| 367 | |
[email protected] | 147bbc0b | 2009-01-06 19:37:40 | [diff] [blame] | 368 | WritePluginStabilityElements(pref); |
| 369 | } |
| 370 | |
| 371 | void MetricsLog::WritePluginStabilityElements(PrefService* pref) { |
| 372 | // Now log plugin stability info. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 373 | const ListValue* plugin_stats_list = pref->GetList( |
| 374 | prefs::kStabilityPluginStats); |
| 375 | if (plugin_stats_list) { |
[email protected] | ffaf78a | 2008-11-12 17:38:33 | [diff] [blame] | 376 | OPEN_ELEMENT_FOR_SCOPE("plugins"); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 377 | for (ListValue::const_iterator iter = plugin_stats_list->begin(); |
| 378 | iter != plugin_stats_list->end(); ++iter) { |
| 379 | if (!(*iter)->IsType(Value::TYPE_DICTIONARY)) { |
| 380 | NOTREACHED(); |
| 381 | continue; |
| 382 | } |
| 383 | DictionaryValue* plugin_dict = static_cast<DictionaryValue*>(*iter); |
| 384 | |
[email protected] | a27a938 | 2009-02-11 23:55:10 | [diff] [blame^] | 385 | std::wstring plugin_name; |
| 386 | plugin_dict->GetString(prefs::kStabilityPluginName, &plugin_name); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 387 | |
[email protected] | ffaf78a | 2008-11-12 17:38:33 | [diff] [blame] | 388 | OPEN_ELEMENT_FOR_SCOPE("pluginstability"); |
[email protected] | a27a938 | 2009-02-11 23:55:10 | [diff] [blame^] | 389 | // Use "filename" instead of "name", otherwise we need to update the |
| 390 | // UMA servers. |
| 391 | WriteAttribute("filename", CreateBase64Hash(WideToUTF8(plugin_name))); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 392 | |
| 393 | int launches = 0; |
| 394 | plugin_dict->GetInteger(prefs::kStabilityPluginLaunches, &launches); |
| 395 | WriteIntAttribute("launchcount", launches); |
| 396 | |
| 397 | int instances = 0; |
| 398 | plugin_dict->GetInteger(prefs::kStabilityPluginInstances, &instances); |
| 399 | WriteIntAttribute("instancecount", instances); |
| 400 | |
| 401 | int crashes = 0; |
| 402 | plugin_dict->GetInteger(prefs::kStabilityPluginCrashes, &crashes); |
| 403 | WriteIntAttribute("crashcount", crashes); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | pref->ClearPref(prefs::kStabilityPluginStats); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 407 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 408 | } |
| 409 | |
[email protected] | 147bbc0b | 2009-01-06 19:37:40 | [diff] [blame] | 410 | void MetricsLog::WriteRequiredStabilityAttributes(PrefService* pref) { |
[email protected] | 0b33f80b | 2008-12-17 21:34:36 | [diff] [blame] | 411 | // The server refuses data that doesn't have certain values. crashcount and |
| 412 | // launchcount are currently "required" in the "stability" group. |
| 413 | WriteIntAttribute("launchcount", |
| 414 | pref->GetInteger(prefs::kStabilityLaunchCount)); |
| 415 | pref->SetInteger(prefs::kStabilityLaunchCount, 0); |
| 416 | WriteIntAttribute("crashcount", |
| 417 | pref->GetInteger(prefs::kStabilityCrashCount)); |
| 418 | pref->SetInteger(prefs::kStabilityCrashCount, 0); |
| 419 | } |
| 420 | |
[email protected] | 147bbc0b | 2009-01-06 19:37:40 | [diff] [blame] | 421 | void MetricsLog::WriteRealtimeStabilityAttributes(PrefService* pref) { |
[email protected] | 0b33f80b | 2008-12-17 21:34:36 | [diff] [blame] | 422 | // Update the stats which are critical for real-time stability monitoring. |
| 423 | // Since these are "optional," only list ones that are non-zero, as the counts |
| 424 | // are aggergated (summed) server side. |
| 425 | |
| 426 | int count = pref->GetInteger(prefs::kStabilityPageLoadCount); |
| 427 | if (count) { |
| 428 | WriteIntAttribute("pageloadcount", count); |
| 429 | pref->SetInteger(prefs::kStabilityPageLoadCount, 0); |
| 430 | } |
| 431 | |
| 432 | count = pref->GetInteger(prefs::kStabilityRendererCrashCount); |
| 433 | if (count) { |
| 434 | WriteIntAttribute("renderercrashcount", count); |
| 435 | pref->SetInteger(prefs::kStabilityRendererCrashCount, 0); |
| 436 | } |
| 437 | |
| 438 | count = pref->GetInteger(prefs::kStabilityRendererHangCount); |
| 439 | if (count) { |
| 440 | WriteIntAttribute("rendererhangcount", count); |
| 441 | pref->SetInteger(prefs::kStabilityRendererHangCount, 0); |
| 442 | } |
| 443 | } |
| 444 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 445 | void MetricsLog::WritePluginList( |
| 446 | const std::vector<WebPluginInfo>& plugin_list) { |
| 447 | DCHECK(!locked_); |
| 448 | |
[email protected] | ffaf78a | 2008-11-12 17:38:33 | [diff] [blame] | 449 | OPEN_ELEMENT_FOR_SCOPE("plugins"); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 450 | |
| 451 | for (std::vector<WebPluginInfo>::const_iterator iter = plugin_list.begin(); |
| 452 | iter != plugin_list.end(); ++iter) { |
[email protected] | ffaf78a | 2008-11-12 17:38:33 | [diff] [blame] | 453 | OPEN_ELEMENT_FOR_SCOPE("plugin"); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 454 | |
| 455 | // Plugin name and filename are hashed for the privacy of those |
| 456 | // testing unreleased new extensions. |
[email protected] | b734450 | 2009-01-12 19:43:44 | [diff] [blame] | 457 | WriteAttribute("name", CreateBase64Hash(WideToUTF8(iter->name))); |
[email protected] | 046344c | 2009-01-13 00:54:21 | [diff] [blame] | 458 | WriteAttribute("filename", |
| 459 | CreateBase64Hash(WideToUTF8(iter->path.BaseName().ToWStringHack()))); |
[email protected] | b734450 | 2009-01-12 19:43:44 | [diff] [blame] | 460 | WriteAttribute("version", WideToUTF8(iter->version)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 461 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 462 | } |
| 463 | |
[email protected] | 147bbc0b | 2009-01-06 19:37:40 | [diff] [blame] | 464 | void MetricsLog::WriteInstallElement() { |
| 465 | OPEN_ELEMENT_FOR_SCOPE("install"); |
| 466 | WriteAttribute("installdate", GetInstallDate()); |
| 467 | WriteIntAttribute("buildid", 0); // We're using appversion instead. |
| 468 | WriteAttribute("appversion", GetVersionString()); |
| 469 | } |
| 470 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 471 | void MetricsLog::RecordEnvironment( |
| 472 | const std::vector<WebPluginInfo>& plugin_list, |
| 473 | const DictionaryValue* profile_metrics) { |
| 474 | DCHECK(!locked_); |
| 475 | |
| 476 | PrefService* pref = g_browser_process->local_state(); |
| 477 | |
[email protected] | ffaf78a | 2008-11-12 17:38:33 | [diff] [blame] | 478 | OPEN_ELEMENT_FOR_SCOPE("profile"); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 479 | WriteCommonEventAttributes(); |
| 480 | |
[email protected] | 147bbc0b | 2009-01-06 19:37:40 | [diff] [blame] | 481 | WriteInstallElement(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 482 | |
| 483 | WritePluginList(plugin_list); |
| 484 | |
| 485 | WriteStabilityElement(); |
| 486 | |
| 487 | { |
| 488 | OPEN_ELEMENT_FOR_SCOPE("cpu"); |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 489 | WriteAttribute("arch", base::SysInfo::CPUArchitecture()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | { |
| 493 | OPEN_ELEMENT_FOR_SCOPE("security"); |
| 494 | WriteIntAttribute("rendereronsboxdesktop", |
| 495 | pref->GetInteger(prefs::kSecurityRendererOnSboxDesktop)); |
| 496 | pref->SetInteger(prefs::kSecurityRendererOnSboxDesktop, 0); |
| 497 | |
| 498 | WriteIntAttribute("rendererondefaultdesktop", |
| 499 | pref->GetInteger(prefs::kSecurityRendererOnDefaultDesktop)); |
| 500 | pref->SetInteger(prefs::kSecurityRendererOnDefaultDesktop, 0); |
| 501 | } |
| 502 | |
| 503 | { |
| 504 | OPEN_ELEMENT_FOR_SCOPE("memory"); |
[email protected] | ed6fc35 | 2008-09-18 12:44:40 | [diff] [blame] | 505 | WriteIntAttribute("mb", base::SysInfo::AmountOfPhysicalMemoryMB()); |
[email protected] | d1be67b | 2008-11-19 20:28:38 | [diff] [blame] | 506 | #if defined(OS_WIN) |
| 507 | WriteIntAttribute("dllbase", reinterpret_cast<int>(&__ImageBase)); |
| 508 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | { |
| 512 | OPEN_ELEMENT_FOR_SCOPE("os"); |
| 513 | WriteAttribute("name", |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 514 | base::SysInfo::OperatingSystemName()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 515 | WriteAttribute("version", |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 516 | base::SysInfo::OperatingSystemVersion()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | { |
| 520 | OPEN_ELEMENT_FOR_SCOPE("display"); |
| 521 | int width = 0; |
| 522 | int height = 0; |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 523 | base::SysInfo::GetPrimaryDisplayDimensions(&width, &height); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 524 | WriteIntAttribute("xsize", width); |
| 525 | WriteIntAttribute("ysize", height); |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 526 | WriteIntAttribute("screens", base::SysInfo::DisplayCount()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | { |
| 530 | OPEN_ELEMENT_FOR_SCOPE("bookmarks"); |
| 531 | int num_bookmarks_on_bookmark_bar = |
| 532 | pref->GetInteger(prefs::kNumBookmarksOnBookmarkBar); |
| 533 | int num_folders_on_bookmark_bar = |
| 534 | pref->GetInteger(prefs::kNumFoldersOnBookmarkBar); |
| 535 | int num_bookmarks_in_other_bookmarks_folder = |
| 536 | pref->GetInteger(prefs::kNumBookmarksInOtherBookmarkFolder); |
| 537 | int num_folders_in_other_bookmarks_folder = |
| 538 | pref->GetInteger(prefs::kNumFoldersInOtherBookmarkFolder); |
| 539 | { |
| 540 | OPEN_ELEMENT_FOR_SCOPE("bookmarklocation"); |
| 541 | WriteAttribute("name", "full-tree"); |
| 542 | WriteIntAttribute("foldercount", |
| 543 | num_folders_on_bookmark_bar + num_folders_in_other_bookmarks_folder); |
| 544 | WriteIntAttribute("itemcount", |
| 545 | num_bookmarks_on_bookmark_bar + |
| 546 | num_bookmarks_in_other_bookmarks_folder); |
| 547 | } |
| 548 | { |
| 549 | OPEN_ELEMENT_FOR_SCOPE("bookmarklocation"); |
| 550 | WriteAttribute("name", "toolbar"); |
| 551 | WriteIntAttribute("foldercount", num_folders_on_bookmark_bar); |
| 552 | WriteIntAttribute("itemcount", num_bookmarks_on_bookmark_bar); |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | { |
| 557 | OPEN_ELEMENT_FOR_SCOPE("keywords"); |
| 558 | WriteIntAttribute("count", pref->GetInteger(prefs::kNumKeywords)); |
| 559 | } |
| 560 | |
| 561 | if (profile_metrics) |
| 562 | WriteAllProfilesMetrics(*profile_metrics); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | void MetricsLog::WriteAllProfilesMetrics( |
| 566 | const DictionaryValue& all_profiles_metrics) { |
| 567 | const std::wstring profile_prefix(prefs::kProfilePrefix); |
| 568 | for (DictionaryValue::key_iterator i = all_profiles_metrics.begin_keys(); |
| 569 | i != all_profiles_metrics.end_keys(); ++i) { |
| 570 | const std::wstring& key_name = *i; |
| 571 | if (key_name.compare(0, profile_prefix.size(), profile_prefix) == 0) { |
| 572 | DictionaryValue* profile; |
| 573 | if (all_profiles_metrics.GetDictionary(key_name, &profile)) |
| 574 | WriteProfileMetrics(key_name.substr(profile_prefix.size()), *profile); |
| 575 | } |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | void MetricsLog::WriteProfileMetrics(const std::wstring& profileidhash, |
| 580 | const DictionaryValue& profile_metrics) { |
| 581 | OPEN_ELEMENT_FOR_SCOPE("userprofile"); |
| 582 | WriteAttribute("profileidhash", WideToUTF8(profileidhash)); |
| 583 | for (DictionaryValue::key_iterator i = profile_metrics.begin_keys(); |
| 584 | i != profile_metrics.end_keys(); ++i) { |
| 585 | Value* value; |
| 586 | if (profile_metrics.Get(*i, &value)) { |
| 587 | DCHECK(*i != L"id"); |
| 588 | switch (value->GetType()) { |
| 589 | case Value::TYPE_STRING: { |
[email protected] | 5e324b7 | 2008-12-18 00:07:59 | [diff] [blame] | 590 | std::string string_value; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 591 | if (value->GetAsString(&string_value)) { |
| 592 | OPEN_ELEMENT_FOR_SCOPE("profileparam"); |
| 593 | WriteAttribute("name", WideToUTF8(*i)); |
[email protected] | 5e324b7 | 2008-12-18 00:07:59 | [diff] [blame] | 594 | WriteAttribute("value", string_value); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 595 | } |
| 596 | break; |
| 597 | } |
| 598 | |
| 599 | case Value::TYPE_BOOLEAN: { |
| 600 | bool bool_value; |
| 601 | if (value->GetAsBoolean(&bool_value)) { |
| 602 | OPEN_ELEMENT_FOR_SCOPE("profileparam"); |
| 603 | WriteAttribute("name", WideToUTF8(*i)); |
| 604 | WriteIntAttribute("value", bool_value ? 1 : 0); |
| 605 | } |
| 606 | break; |
| 607 | } |
| 608 | |
| 609 | case Value::TYPE_INTEGER: { |
| 610 | int int_value; |
| 611 | if (value->GetAsInteger(&int_value)) { |
| 612 | OPEN_ELEMENT_FOR_SCOPE("profileparam"); |
| 613 | WriteAttribute("name", WideToUTF8(*i)); |
| 614 | WriteIntAttribute("value", int_value); |
| 615 | } |
| 616 | break; |
| 617 | } |
| 618 | |
| 619 | default: |
| 620 | NOTREACHED(); |
| 621 | break; |
| 622 | } |
| 623 | } |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | void MetricsLog::RecordOmniboxOpenedURL(const AutocompleteLog& log) { |
| 628 | DCHECK(!locked_); |
| 629 | |
[email protected] | ffaf78a | 2008-11-12 17:38:33 | [diff] [blame] | 630 | OPEN_ELEMENT_FOR_SCOPE("uielement"); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 631 | WriteAttribute("action", "autocomplete"); |
| 632 | WriteAttribute("targetidhash", ""); |
| 633 | // TODO(kochi): Properly track windows. |
| 634 | WriteIntAttribute("window", 0); |
| 635 | WriteCommonEventAttributes(); |
| 636 | |
[email protected] | ffaf78a | 2008-11-12 17:38:33 | [diff] [blame] | 637 | { |
| 638 | OPEN_ELEMENT_FOR_SCOPE("autocomplete"); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 639 | |
[email protected] | ffaf78a | 2008-11-12 17:38:33 | [diff] [blame] | 640 | WriteIntAttribute("typedlength", static_cast<int>(log.text.length())); |
| 641 | WriteIntAttribute("selectedindex", static_cast<int>(log.selected_index)); |
| 642 | WriteIntAttribute("completedlength", |
| 643 | static_cast<int>(log.inline_autocompleted_length)); |
[email protected] | 381e299 | 2008-12-16 01:41:00 | [diff] [blame] | 644 | const std::string input_type( |
| 645 | AutocompleteInput::TypeToString(log.input_type)); |
| 646 | if (!input_type.empty()) |
| 647 | WriteAttribute("inputtype", input_type); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 648 | |
[email protected] | ffaf78a | 2008-11-12 17:38:33 | [diff] [blame] | 649 | for (AutocompleteResult::const_iterator i(log.result.begin()); |
| 650 | i != log.result.end(); ++i) { |
| 651 | OPEN_ELEMENT_FOR_SCOPE("autocompleteitem"); |
| 652 | if (i->provider) |
| 653 | WriteAttribute("provider", i->provider->name()); |
[email protected] | 381e299 | 2008-12-16 01:41:00 | [diff] [blame] | 654 | const std::string result_type(AutocompleteMatch::TypeToString(i->type)); |
| 655 | if (!result_type.empty()) |
| 656 | WriteAttribute("resulttype", result_type); |
[email protected] | ffaf78a | 2008-11-12 17:38:33 | [diff] [blame] | 657 | WriteIntAttribute("relevance", i->relevance); |
| 658 | WriteIntAttribute("isstarred", i->starred ? 1 : 0); |
| 659 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 660 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 661 | |
| 662 | ++num_events_; |
| 663 | } |
| 664 | |
| 665 | // TODO(JAR): A The following should really be part of the histogram class. |
| 666 | // Internal state is being needlessly exposed, and it would be hard to reuse |
| 667 | // this code. If we moved this into the Histogram class, then we could use |
| 668 | // the same infrastructure for logging StatsCounters, RatesCounters, etc. |
| 669 | void MetricsLog::RecordHistogramDelta(const Histogram& histogram, |
| 670 | const Histogram::SampleSet& snapshot) { |
| 671 | DCHECK(!locked_); |
| 672 | DCHECK(0 != snapshot.TotalCount()); |
| 673 | snapshot.CheckSize(histogram); |
| 674 | |
| 675 | // We will ignore the MAX_INT/infinite value in the last element of range[]. |
| 676 | |
| 677 | OPEN_ELEMENT_FOR_SCOPE("histogram"); |
| 678 | |
| 679 | WriteAttribute("name", CreateBase64Hash(histogram.histogram_name())); |
| 680 | |
| 681 | WriteInt64Attribute("sum", snapshot.sum()); |
| 682 | WriteInt64Attribute("sumsquares", snapshot.square_sum()); |
| 683 | |
| 684 | for (size_t i = 0; i < histogram.bucket_count(); i++) { |
| 685 | if (snapshot.counts(i)) { |
| 686 | OPEN_ELEMENT_FOR_SCOPE("histogrambucket"); |
| 687 | WriteIntAttribute("min", histogram.ranges(i)); |
| 688 | WriteIntAttribute("max", histogram.ranges(i + 1)); |
| 689 | WriteIntAttribute("count", snapshot.counts(i)); |
| 690 | } |
| 691 | } |
| 692 | } |