[email protected] | 4dad9ad8 | 2009-11-25 20:47:52 | [diff] [blame] | 1 | // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 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] | 978df34 | 2009-11-24 06:21:53 | [diff] [blame] | 7 | #include "base/base64.h" |
[email protected] | d1be67b | 2008-11-19 20:28:38 | [diff] [blame] | 8 | #include "base/basictypes.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 9 | #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] | fadf97f | 2008-09-18 12:18:14 | [diff] [blame] | 14 | #include "base/sys_info.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 15 | #include "chrome/browser/autocomplete/autocomplete.h" |
| 16 | #include "chrome/browser/browser_process.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 17 | #include "chrome/common/logging_chrome.h" |
| 18 | #include "chrome/common/pref_names.h" |
| 19 | #include "chrome/common/pref_service.h" |
[email protected] | 46072d4 | 2008-07-28 14:49:35 | [diff] [blame] | 20 | #include "googleurl/src/gurl.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 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 | |
[email protected] | be6bf5e | 2009-06-16 13:14:08 | [diff] [blame] | 111 | 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] | f88ba61a | 2009-06-12 16:52:10 | [diff] [blame] | 119 | // 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] | be6bf5e | 2009-06-16 13:14:08 | [diff] [blame] | 126 | << reverse_uint64 << "]"; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 127 | return std::string(reinterpret_cast<char*>(digest.a), arraysize(digest.a)); |
| 128 | } |
| 129 | |
| 130 | std::string MetricsLog::CreateBase64Hash(const std::string& string) { |
| 131 | std::string encoded_digest; |
[email protected] | 978df34 | 2009-11-24 06:21:53 | [diff] [blame] | 132 | if (base::Base64Encode(CreateHash(string), &encoded_digest)) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 133 | DLOG(INFO) << "Metrics: Hash [" << encoded_digest << "]=[" << string << "]"; |
| 134 | return encoded_digest; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 135 | } |
[email protected] | a9bb6f69 | 2008-07-30 16:40:10 | [diff] [blame] | 136 | return std::string(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 137 | } |
| 138 | |
[email protected] | afe3a167 | 2009-11-17 19:04:12 | [diff] [blame] | 139 | void MetricsLog::RecordUserAction(const char* key) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 140 | DCHECK(!locked_); |
| 141 | |
[email protected] | afe3a167 | 2009-11-17 19:04:12 | [diff] [blame] | 142 | std::string command_hash = CreateBase64Hash(key); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 143 | if (command_hash.empty()) { |
| 144 | NOTREACHED() << "Unable generate encoded hash of command: " << key; |
| 145 | return; |
| 146 | } |
| 147 | |
[email protected] | ffaf78a | 2008-11-12 17:38:33 | [diff] [blame] | 148 | OPEN_ELEMENT_FOR_SCOPE("uielement"); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 149 | WriteAttribute("action", "command"); |
| 150 | WriteAttribute("targetidhash", command_hash); |
| 151 | |
| 152 | // TODO(jhughes): Properly track windows. |
| 153 | WriteIntAttribute("window", 0); |
| 154 | WriteCommonEventAttributes(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 155 | |
| 156 | ++num_events_; |
| 157 | } |
| 158 | |
| 159 | void 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] | ffaf78a | 2008-11-12 17:38:33 | [diff] [blame] | 166 | OPEN_ELEMENT_FOR_SCOPE("document"); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 167 | 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] | 0bfc29a | 2009-04-27 16:15:44 | [diff] [blame] | 196 | case PageTransition::KEYWORD: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 197 | 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 216 | |
| 217 | ++num_events_; |
| 218 | } |
| 219 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 220 | void MetricsLog::RecordWindowEvent(WindowEventType type, |
| 221 | int window_id, |
| 222 | int parent_id) { |
| 223 | DCHECK(!locked_); |
| 224 | |
[email protected] | ffaf78a | 2008-11-12 17:38:33 | [diff] [blame] | 225 | OPEN_ELEMENT_FOR_SCOPE("window"); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 226 | WriteAttribute("action", WindowEventTypeToString(type)); |
| 227 | WriteAttribute("windowid", IntToString(window_id)); |
| 228 | if (parent_id >= 0) |
| 229 | WriteAttribute("parent", IntToString(parent_id)); |
| 230 | WriteCommonEventAttributes(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 231 | |
| 232 | ++num_events_; |
| 233 | } |
| 234 | |
| 235 | std::string MetricsLog::GetCurrentTimeString() { |
| 236 | return Uint64ToString(Time::Now().ToTimeT()); |
| 237 | } |
| 238 | |
| 239 | // These are the attributes that are common to every event. |
| 240 | void MetricsLog::WriteCommonEventAttributes() { |
| 241 | WriteAttribute("session", session_id_); |
| 242 | WriteAttribute("time", GetCurrentTimeString()); |
| 243 | } |
| 244 | |
| 245 | void 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 | |
| 256 | void MetricsLog::WriteIntAttribute(const std::string& name, int value) { |
| 257 | WriteAttribute(name, IntToString(value)); |
| 258 | } |
| 259 | |
| 260 | void MetricsLog::WriteInt64Attribute(const std::string& name, int64 value) { |
| 261 | WriteAttribute(name, Int64ToString(value)); |
| 262 | } |
| 263 | |
[email protected] | ffaf78a | 2008-11-12 17:38:33 | [diff] [blame] | 264 | // static |
| 265 | const 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 279 | void 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 | |
| 287 | void MetricsLog::EndElement() { |
| 288 | DCHECK(!locked_); |
| 289 | |
| 290 | int result = xmlTextWriterEndElement(writer_); |
| 291 | DCHECK_GE(result, 0); |
| 292 | } |
| 293 | |
[email protected] | 541f7792 | 2009-02-23 21:14:38 | [diff] [blame] | 294 | // static |
| 295 | std::string MetricsLog::GetVersionString() { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 296 | 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 | |
| 310 | std::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] | 0b33f80b | 2008-12-17 21:34:36 | [diff] [blame] | 320 | void MetricsLog::RecordIncrementalStabilityElements() { |
| 321 | DCHECK(!locked_); |
| 322 | |
| 323 | PrefService* pref = g_browser_process->local_state(); |
| 324 | DCHECK(pref); |
| 325 | |
[email protected] | 147bbc0b | 2009-01-06 19:37:40 | [diff] [blame] | 326 | 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] | 0b33f80b | 2008-12-17 21:34:36 | [diff] [blame] | 338 | } |
| 339 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 340 | void 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] | ffaf78a | 2008-11-12 17:38:33 | [diff] [blame] | 350 | OPEN_ELEMENT_FOR_SCOPE("stability"); |
[email protected] | 147bbc0b | 2009-01-06 19:37:40 | [diff] [blame] | 351 | WriteRequiredStabilityAttributes(pref); |
| 352 | WriteRealtimeStabilityAttributes(pref); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 353 | |
[email protected] | 0b33f80b | 2008-12-17 21:34:36 | [diff] [blame] | 354 | // TODO(jar): The following are all optional, so we *could* optimize them for |
| 355 | // values of zero (and not include them). |
[email protected] | 8e674e4 | 2008-07-30 16:05:48 | [diff] [blame] | 356 | WriteIntAttribute("incompleteshutdowncount", |
| 357 | pref->GetInteger( |
| 358 | prefs::kStabilityIncompleteSessionEndCount)); |
| 359 | pref->SetInteger(prefs::kStabilityIncompleteSessionEndCount, 0); |
[email protected] | 0b33f80b | 2008-12-17 21:34:36 | [diff] [blame] | 360 | |
| 361 | |
[email protected] | e73c0197 | 2008-08-13 00:18:24 | [diff] [blame] | 362 | 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 374 | |
| 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] | 147bbc0b | 2009-01-06 19:37:40 | [diff] [blame] | 380 | WritePluginStabilityElements(pref); |
| 381 | } |
| 382 | |
| 383 | void MetricsLog::WritePluginStabilityElements(PrefService* pref) { |
| 384 | // Now log plugin stability info. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 385 | const ListValue* plugin_stats_list = pref->GetList( |
| 386 | prefs::kStabilityPluginStats); |
| 387 | if (plugin_stats_list) { |
[email protected] | ffaf78a | 2008-11-12 17:38:33 | [diff] [blame] | 388 | OPEN_ELEMENT_FOR_SCOPE("plugins"); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 389 | 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] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 397 | std::wstring plugin_name; |
| 398 | plugin_dict->GetString(prefs::kStabilityPluginName, &plugin_name); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 399 | |
[email protected] | ffaf78a | 2008-11-12 17:38:33 | [diff] [blame] | 400 | OPEN_ELEMENT_FOR_SCOPE("pluginstability"); |
[email protected] | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 401 | // Use "filename" instead of "name", otherwise we need to update the |
| 402 | // UMA servers. |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 403 | WriteAttribute("filename", CreateBase64Hash(WideToUTF8(plugin_name))); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 404 | |
| 405 | int launches = 0; |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 406 | plugin_dict->GetInteger(prefs::kStabilityPluginLaunches, &launches); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 407 | WriteIntAttribute("launchcount", launches); |
| 408 | |
| 409 | int instances = 0; |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 410 | plugin_dict->GetInteger(prefs::kStabilityPluginInstances, &instances); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 411 | WriteIntAttribute("instancecount", instances); |
| 412 | |
| 413 | int crashes = 0; |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 414 | plugin_dict->GetInteger(prefs::kStabilityPluginCrashes, &crashes); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 415 | WriteIntAttribute("crashcount", crashes); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | pref->ClearPref(prefs::kStabilityPluginStats); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 419 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 420 | } |
| 421 | |
[email protected] | 147bbc0b | 2009-01-06 19:37:40 | [diff] [blame] | 422 | void MetricsLog::WriteRequiredStabilityAttributes(PrefService* pref) { |
[email protected] | 0b33f80b | 2008-12-17 21:34:36 | [diff] [blame] | 423 | // 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] | 147bbc0b | 2009-01-06 19:37:40 | [diff] [blame] | 433 | void MetricsLog::WriteRealtimeStabilityAttributes(PrefService* pref) { |
[email protected] | 0b33f80b | 2008-12-17 21:34:36 | [diff] [blame] | 434 | // 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] | 1f08562 | 2009-12-04 05:33:45 | [diff] [blame^] | 450 | count = pref->GetInteger(prefs::kStabilityExtensionRendererCrashCount); |
| 451 | if (count) { |
| 452 | WriteIntAttribute("extensionrenderercrashcount", count); |
| 453 | pref->SetInteger(prefs::kStabilityExtensionRendererCrashCount, 0); |
| 454 | } |
| 455 | |
[email protected] | 0b33f80b | 2008-12-17 21:34:36 | [diff] [blame] | 456 | count = pref->GetInteger(prefs::kStabilityRendererHangCount); |
| 457 | if (count) { |
| 458 | WriteIntAttribute("rendererhangcount", count); |
| 459 | pref->SetInteger(prefs::kStabilityRendererHangCount, 0); |
| 460 | } |
[email protected] | 1f08562 | 2009-12-04 05:33:45 | [diff] [blame^] | 461 | |
| 462 | count = pref->GetInteger(prefs::kStabilityChildProcessCrashCount); |
| 463 | if (count) { |
| 464 | WriteIntAttribute("childprocesscrashcount", count); |
| 465 | pref->SetInteger(prefs::kStabilityChildProcessCrashCount, 0); |
| 466 | } |
[email protected] | 0b33f80b | 2008-12-17 21:34:36 | [diff] [blame] | 467 | } |
| 468 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 469 | void MetricsLog::WritePluginList( |
| 470 | const std::vector<WebPluginInfo>& plugin_list) { |
| 471 | DCHECK(!locked_); |
| 472 | |
[email protected] | ffaf78a | 2008-11-12 17:38:33 | [diff] [blame] | 473 | OPEN_ELEMENT_FOR_SCOPE("plugins"); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 474 | |
| 475 | for (std::vector<WebPluginInfo>::const_iterator iter = plugin_list.begin(); |
| 476 | iter != plugin_list.end(); ++iter) { |
[email protected] | ffaf78a | 2008-11-12 17:38:33 | [diff] [blame] | 477 | OPEN_ELEMENT_FOR_SCOPE("plugin"); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 478 | |
| 479 | // Plugin name and filename are hashed for the privacy of those |
| 480 | // testing unreleased new extensions. |
[email protected] | b734450 | 2009-01-12 19:43:44 | [diff] [blame] | 481 | WriteAttribute("name", CreateBase64Hash(WideToUTF8(iter->name))); |
[email protected] | 046344c | 2009-01-13 00:54:21 | [diff] [blame] | 482 | WriteAttribute("filename", |
| 483 | CreateBase64Hash(WideToUTF8(iter->path.BaseName().ToWStringHack()))); |
[email protected] | b734450 | 2009-01-12 19:43:44 | [diff] [blame] | 484 | WriteAttribute("version", WideToUTF8(iter->version)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 485 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 486 | } |
| 487 | |
[email protected] | 147bbc0b | 2009-01-06 19:37:40 | [diff] [blame] | 488 | void 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 495 | void 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] | ffaf78a | 2008-11-12 17:38:33 | [diff] [blame] | 502 | OPEN_ELEMENT_FOR_SCOPE("profile"); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 503 | WriteCommonEventAttributes(); |
| 504 | |
[email protected] | 147bbc0b | 2009-01-06 19:37:40 | [diff] [blame] | 505 | WriteInstallElement(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 506 | |
| 507 | WritePluginList(plugin_list); |
| 508 | |
| 509 | WriteStabilityElement(); |
| 510 | |
| 511 | { |
| 512 | OPEN_ELEMENT_FOR_SCOPE("cpu"); |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 513 | WriteAttribute("arch", base::SysInfo::CPUArchitecture()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 517 | OPEN_ELEMENT_FOR_SCOPE("memory"); |
[email protected] | ed6fc35 | 2008-09-18 12:44:40 | [diff] [blame] | 518 | WriteIntAttribute("mb", base::SysInfo::AmountOfPhysicalMemoryMB()); |
[email protected] | d1be67b | 2008-11-19 20:28:38 | [diff] [blame] | 519 | #if defined(OS_WIN) |
| 520 | WriteIntAttribute("dllbase", reinterpret_cast<int>(&__ImageBase)); |
| 521 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | { |
| 525 | OPEN_ELEMENT_FOR_SCOPE("os"); |
| 526 | WriteAttribute("name", |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 527 | base::SysInfo::OperatingSystemName()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 528 | WriteAttribute("version", |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 529 | base::SysInfo::OperatingSystemVersion()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | { |
| 533 | OPEN_ELEMENT_FOR_SCOPE("display"); |
| 534 | int width = 0; |
| 535 | int height = 0; |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 536 | base::SysInfo::GetPrimaryDisplayDimensions(&width, &height); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 537 | WriteIntAttribute("xsize", width); |
| 538 | WriteIntAttribute("ysize", height); |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 539 | WriteIntAttribute("screens", base::SysInfo::DisplayCount()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 540 | } |
| 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | void 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] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 583 | const std::wstring& key_name = *i; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 584 | if (key_name.compare(0, profile_prefix.size(), profile_prefix) == 0) { |
| 585 | DictionaryValue* profile; |
[email protected] | 4dad9ad8 | 2009-11-25 20:47:52 | [diff] [blame] | 586 | if (all_profiles_metrics.GetDictionaryWithoutPathExpansion(key_name, |
| 587 | &profile)) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 588 | WriteProfileMetrics(key_name.substr(profile_prefix.size()), *profile); |
| 589 | } |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | void 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] | 4dad9ad8 | 2009-11-25 20:47:52 | [diff] [blame] | 600 | if (profile_metrics.GetWithoutPathExpansion(*i, &value)) { |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 601 | DCHECK(*i != L"id"); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 602 | switch (value->GetType()) { |
| 603 | case Value::TYPE_STRING: { |
[email protected] | 5e324b7 | 2008-12-18 00:07:59 | [diff] [blame] | 604 | std::string string_value; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 605 | if (value->GetAsString(&string_value)) { |
| 606 | OPEN_ELEMENT_FOR_SCOPE("profileparam"); |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 607 | WriteAttribute("name", WideToUTF8(*i)); |
[email protected] | 5e324b7 | 2008-12-18 00:07:59 | [diff] [blame] | 608 | WriteAttribute("value", string_value); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 609 | } |
| 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] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 617 | WriteAttribute("name", WideToUTF8(*i)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 618 | 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] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 627 | WriteAttribute("name", WideToUTF8(*i)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 628 | WriteIntAttribute("value", int_value); |
| 629 | } |
| 630 | break; |
| 631 | } |
| 632 | |
| 633 | default: |
| 634 | NOTREACHED(); |
| 635 | break; |
| 636 | } |
| 637 | } |
| 638 | } |
| 639 | } |
| 640 | |
| 641 | void MetricsLog::RecordOmniboxOpenedURL(const AutocompleteLog& log) { |
| 642 | DCHECK(!locked_); |
| 643 | |
[email protected] | ffaf78a | 2008-11-12 17:38:33 | [diff] [blame] | 644 | OPEN_ELEMENT_FOR_SCOPE("uielement"); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 645 | WriteAttribute("action", "autocomplete"); |
| 646 | WriteAttribute("targetidhash", ""); |
| 647 | // TODO(kochi): Properly track windows. |
| 648 | WriteIntAttribute("window", 0); |
| 649 | WriteCommonEventAttributes(); |
| 650 | |
[email protected] | ffaf78a | 2008-11-12 17:38:33 | [diff] [blame] | 651 | { |
| 652 | OPEN_ELEMENT_FOR_SCOPE("autocomplete"); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 653 | |
[email protected] | ffaf78a | 2008-11-12 17:38:33 | [diff] [blame] | 654 | 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] | 381e299 | 2008-12-16 01:41:00 | [diff] [blame] | 658 | const std::string input_type( |
| 659 | AutocompleteInput::TypeToString(log.input_type)); |
| 660 | if (!input_type.empty()) |
| 661 | WriteAttribute("inputtype", input_type); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 662 | |
[email protected] | ffaf78a | 2008-11-12 17:38:33 | [diff] [blame] | 663 | 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] | 381e299 | 2008-12-16 01:41:00 | [diff] [blame] | 668 | const std::string result_type(AutocompleteMatch::TypeToString(i->type)); |
| 669 | if (!result_type.empty()) |
| 670 | WriteAttribute("resulttype", result_type); |
[email protected] | ffaf78a | 2008-11-12 17:38:33 | [diff] [blame] | 671 | WriteIntAttribute("relevance", i->relevance); |
| 672 | WriteIntAttribute("isstarred", i->starred ? 1 : 0); |
| 673 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 674 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 675 | |
| 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. |
| 683 | void 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 | } |