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