metrics_log.cc revision 3345a6884c488ff3a535c2c9acdd33d74b37e311
1// Copyright (c) 2010 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.
4
5#include "chrome/browser/metrics/metrics_log.h"
6
7#include <string>
8#include <vector>
9
10#include "base/basictypes.h"
11#include "base/file_util.h"
12#include "base/perftimer.h"
13#include "base/scoped_ptr.h"
14#include "base/string_util.h"
15#include "base/sys_info.h"
16#include "base/third_party/nspr/prtime.h"
17#include "base/time.h"
18#include "base/utf_string_conversions.h"
19#include "chrome/browser/autocomplete/autocomplete.h"
20#include "chrome/browser/browser_process.h"
21#include "chrome/browser/gpu_process_host.h"
22#include "chrome/browser/prefs/pref_service.h"
23#include "chrome/common/chrome_version_info.h"
24#include "chrome/common/logging_chrome.h"
25#include "chrome/common/pref_names.h"
26#include "googleurl/src/gurl.h"
27
28#define OPEN_ELEMENT_FOR_SCOPE(name) ScopedElement scoped_element(this, name)
29
30// http://blogs.msdn.com/oldnewthing/archive/2004/10/25/247180.aspx
31#if defined(OS_WIN)
32extern "C" IMAGE_DOS_HEADER __ImageBase;
33#endif
34
35MetricsLog::MetricsLog(const std::string& client_id, int session_id)
36    : MetricsLogBase(client_id, session_id, MetricsLog::GetVersionString()) {}
37
38MetricsLog::~MetricsLog() {}
39
40// static
41void MetricsLog::RegisterPrefs(PrefService* local_state) {
42  local_state->RegisterListPref(prefs::kStabilityPluginStats);
43}
44
45int64 MetricsLog::GetIncrementalUptime(PrefService* pref) {
46  base::TimeTicks now = base::TimeTicks::Now();
47  static base::TimeTicks last_updated_time(now);
48  int64 incremental_time = (now - last_updated_time).InSeconds();
49  last_updated_time = now;
50
51  if (incremental_time > 0) {
52    int64 metrics_uptime = pref->GetInt64(prefs::kUninstallMetricsUptimeSec);
53    metrics_uptime += incremental_time;
54    pref->SetInt64(prefs::kUninstallMetricsUptimeSec, metrics_uptime);
55  }
56
57  return incremental_time;
58}
59
60std::string MetricsLog::GetInstallDate() const {
61  PrefService* pref = g_browser_process->local_state();
62  if (pref) {
63    return pref->GetString(prefs::kMetricsClientIDTimestamp);
64  } else {
65    NOTREACHED();
66    return "0";
67  }
68}
69
70// static
71std::string MetricsLog::GetVersionString() {
72  chrome::VersionInfo version_info;
73  if (!version_info.is_valid()) {
74    NOTREACHED() << "Unable to retrieve version info.";
75    return std::string();
76  }
77
78  std::string version = version_info.Version();
79  if (!version_extension_.empty())
80    version += version_extension_;
81  if (!version_info.IsOfficialBuild())
82    version.append("-devel");
83  return version;
84}
85
86MetricsLog* MetricsLog::AsMetricsLog() {
87  return this;
88}
89
90void MetricsLog::RecordIncrementalStabilityElements() {
91  DCHECK(!locked_);
92
93  PrefService* pref = g_browser_process->local_state();
94  DCHECK(pref);
95
96  OPEN_ELEMENT_FOR_SCOPE("profile");
97  WriteCommonEventAttributes();
98
99  WriteInstallElement();  // Supply appversion.
100
101  {
102    OPEN_ELEMENT_FOR_SCOPE("stability");  // Minimal set of stability elements.
103    WriteRequiredStabilityAttributes(pref);
104    WriteRealtimeStabilityAttributes(pref);
105
106    WritePluginStabilityElements(pref);
107  }
108}
109
110void MetricsLog::WriteStabilityElement() {
111  DCHECK(!locked_);
112
113  PrefService* pref = g_browser_process->local_state();
114  DCHECK(pref);
115
116  // Get stability attributes out of Local State, zeroing out stored values.
117  // NOTE: This could lead to some data loss if this report isn't successfully
118  //       sent, but that's true for all the metrics.
119
120  OPEN_ELEMENT_FOR_SCOPE("stability");
121  WriteRequiredStabilityAttributes(pref);
122  WriteRealtimeStabilityAttributes(pref);
123
124  // TODO(jar): The following are all optional, so we *could* optimize them for
125  // values of zero (and not include them).
126  WriteIntAttribute("incompleteshutdowncount",
127                    pref->GetInteger(
128                        prefs::kStabilityIncompleteSessionEndCount));
129  pref->SetInteger(prefs::kStabilityIncompleteSessionEndCount, 0);
130
131
132  WriteIntAttribute("breakpadregistrationok",
133      pref->GetInteger(prefs::kStabilityBreakpadRegistrationSuccess));
134  pref->SetInteger(prefs::kStabilityBreakpadRegistrationSuccess, 0);
135  WriteIntAttribute("breakpadregistrationfail",
136      pref->GetInteger(prefs::kStabilityBreakpadRegistrationFail));
137  pref->SetInteger(prefs::kStabilityBreakpadRegistrationFail, 0);
138  WriteIntAttribute("debuggerpresent",
139                   pref->GetInteger(prefs::kStabilityDebuggerPresent));
140  pref->SetInteger(prefs::kStabilityDebuggerPresent, 0);
141  WriteIntAttribute("debuggernotpresent",
142                   pref->GetInteger(prefs::kStabilityDebuggerNotPresent));
143  pref->SetInteger(prefs::kStabilityDebuggerNotPresent, 0);
144
145  WritePluginStabilityElements(pref);
146}
147
148void MetricsLog::WritePluginStabilityElements(PrefService* pref) {
149  // Now log plugin stability info.
150  const ListValue* plugin_stats_list = pref->GetList(
151      prefs::kStabilityPluginStats);
152  if (plugin_stats_list) {
153    OPEN_ELEMENT_FOR_SCOPE("plugins");
154    for (ListValue::const_iterator iter = plugin_stats_list->begin();
155         iter != plugin_stats_list->end(); ++iter) {
156      if (!(*iter)->IsType(Value::TYPE_DICTIONARY)) {
157        NOTREACHED();
158        continue;
159      }
160      DictionaryValue* plugin_dict = static_cast<DictionaryValue*>(*iter);
161
162      std::string plugin_name;
163      plugin_dict->GetString(prefs::kStabilityPluginName, &plugin_name);
164
165      OPEN_ELEMENT_FOR_SCOPE("pluginstability");
166      // Use "filename" instead of "name", otherwise we need to update the
167      // UMA servers.
168      WriteAttribute("filename", CreateBase64Hash(plugin_name));
169
170      int launches = 0;
171      plugin_dict->GetInteger(prefs::kStabilityPluginLaunches, &launches);
172      WriteIntAttribute("launchcount", launches);
173
174      int instances = 0;
175      plugin_dict->GetInteger(prefs::kStabilityPluginInstances, &instances);
176      WriteIntAttribute("instancecount", instances);
177
178      int crashes = 0;
179      plugin_dict->GetInteger(prefs::kStabilityPluginCrashes, &crashes);
180      WriteIntAttribute("crashcount", crashes);
181    }
182
183    pref->ClearPref(prefs::kStabilityPluginStats);
184  }
185}
186
187void MetricsLog::WriteRequiredStabilityAttributes(PrefService* pref) {
188  // The server refuses data that doesn't have certain values.  crashcount and
189  // launchcount are currently "required" in the "stability" group.
190  WriteIntAttribute("launchcount",
191                    pref->GetInteger(prefs::kStabilityLaunchCount));
192  pref->SetInteger(prefs::kStabilityLaunchCount, 0);
193  WriteIntAttribute("crashcount",
194                    pref->GetInteger(prefs::kStabilityCrashCount));
195  pref->SetInteger(prefs::kStabilityCrashCount, 0);
196}
197
198void MetricsLog::WriteRealtimeStabilityAttributes(PrefService* pref) {
199  // Update the stats which are critical for real-time stability monitoring.
200  // Since these are "optional," only list ones that are non-zero, as the counts
201  // are aggergated (summed) server side.
202
203  int count = pref->GetInteger(prefs::kStabilityPageLoadCount);
204  if (count) {
205    WriteIntAttribute("pageloadcount", count);
206    pref->SetInteger(prefs::kStabilityPageLoadCount, 0);
207  }
208
209  count = pref->GetInteger(prefs::kStabilityRendererCrashCount);
210  if (count) {
211    WriteIntAttribute("renderercrashcount", count);
212    pref->SetInteger(prefs::kStabilityRendererCrashCount, 0);
213  }
214
215  count = pref->GetInteger(prefs::kStabilityExtensionRendererCrashCount);
216  if (count) {
217    WriteIntAttribute("extensionrenderercrashcount", count);
218    pref->SetInteger(prefs::kStabilityExtensionRendererCrashCount, 0);
219  }
220
221  count = pref->GetInteger(prefs::kStabilityRendererHangCount);
222  if (count) {
223    WriteIntAttribute("rendererhangcount", count);
224    pref->SetInteger(prefs::kStabilityRendererHangCount, 0);
225  }
226
227  count = pref->GetInteger(prefs::kStabilityChildProcessCrashCount);
228  if (count) {
229    WriteIntAttribute("childprocesscrashcount", count);
230    pref->SetInteger(prefs::kStabilityChildProcessCrashCount, 0);
231  }
232
233  int64 recent_duration = GetIncrementalUptime(pref);
234  if (recent_duration)
235    WriteInt64Attribute("uptimesec", recent_duration);
236}
237
238void MetricsLog::WritePluginList(
239         const std::vector<WebPluginInfo>& plugin_list) {
240  DCHECK(!locked_);
241
242  OPEN_ELEMENT_FOR_SCOPE("plugins");
243
244  for (std::vector<WebPluginInfo>::const_iterator iter = plugin_list.begin();
245       iter != plugin_list.end(); ++iter) {
246    OPEN_ELEMENT_FOR_SCOPE("plugin");
247
248    // Plugin name and filename are hashed for the privacy of those
249    // testing unreleased new extensions.
250    WriteAttribute("name", CreateBase64Hash(UTF16ToUTF8(iter->name)));
251    WriteAttribute("filename",
252        CreateBase64Hash(WideToUTF8(iter->path.BaseName().ToWStringHack())));
253    WriteAttribute("version", UTF16ToUTF8(iter->version));
254  }
255}
256
257void MetricsLog::WriteInstallElement() {
258  OPEN_ELEMENT_FOR_SCOPE("install");
259  WriteAttribute("installdate", GetInstallDate());
260  WriteIntAttribute("buildid", 0);  // We're using appversion instead.
261}
262
263void MetricsLog::RecordEnvironment(
264         const std::vector<WebPluginInfo>& plugin_list,
265         const DictionaryValue* profile_metrics) {
266  DCHECK(!locked_);
267
268  PrefService* pref = g_browser_process->local_state();
269
270  OPEN_ELEMENT_FOR_SCOPE("profile");
271  WriteCommonEventAttributes();
272
273  WriteInstallElement();
274
275  WritePluginList(plugin_list);
276
277  WriteStabilityElement();
278
279  {
280    OPEN_ELEMENT_FOR_SCOPE("cpu");
281    WriteAttribute("arch", base::SysInfo::CPUArchitecture());
282  }
283
284  {
285    OPEN_ELEMENT_FOR_SCOPE("memory");
286    WriteIntAttribute("mb", base::SysInfo::AmountOfPhysicalMemoryMB());
287#if defined(OS_WIN)
288    WriteIntAttribute("dllbase", reinterpret_cast<int>(&__ImageBase));
289#endif
290  }
291
292  {
293    OPEN_ELEMENT_FOR_SCOPE("os");
294    WriteAttribute("name",
295                   base::SysInfo::OperatingSystemName());
296    WriteAttribute("version",
297                   base::SysInfo::OperatingSystemVersion());
298  }
299
300  {
301    OPEN_ELEMENT_FOR_SCOPE("gpu");
302    WriteIntAttribute("vendorid",
303                      GpuProcessHost::Get()->gpu_info().vendor_id());
304    WriteIntAttribute("deviceid",
305                      GpuProcessHost::Get()->gpu_info().device_id());
306  }
307
308  {
309    OPEN_ELEMENT_FOR_SCOPE("display");
310    int width = 0;
311    int height = 0;
312    base::SysInfo::GetPrimaryDisplayDimensions(&width, &height);
313    WriteIntAttribute("xsize", width);
314    WriteIntAttribute("ysize", height);
315    WriteIntAttribute("screens", base::SysInfo::DisplayCount());
316  }
317
318  {
319    OPEN_ELEMENT_FOR_SCOPE("bookmarks");
320    int num_bookmarks_on_bookmark_bar =
321        pref->GetInteger(prefs::kNumBookmarksOnBookmarkBar);
322    int num_folders_on_bookmark_bar =
323        pref->GetInteger(prefs::kNumFoldersOnBookmarkBar);
324    int num_bookmarks_in_other_bookmarks_folder =
325        pref->GetInteger(prefs::kNumBookmarksInOtherBookmarkFolder);
326    int num_folders_in_other_bookmarks_folder =
327        pref->GetInteger(prefs::kNumFoldersInOtherBookmarkFolder);
328    {
329      OPEN_ELEMENT_FOR_SCOPE("bookmarklocation");
330      WriteAttribute("name", "full-tree");
331      WriteIntAttribute("foldercount",
332          num_folders_on_bookmark_bar + num_folders_in_other_bookmarks_folder);
333      WriteIntAttribute("itemcount",
334          num_bookmarks_on_bookmark_bar +
335          num_bookmarks_in_other_bookmarks_folder);
336    }
337    {
338      OPEN_ELEMENT_FOR_SCOPE("bookmarklocation");
339      WriteAttribute("name", "toolbar");
340      WriteIntAttribute("foldercount", num_folders_on_bookmark_bar);
341      WriteIntAttribute("itemcount", num_bookmarks_on_bookmark_bar);
342    }
343  }
344
345  {
346    OPEN_ELEMENT_FOR_SCOPE("keywords");
347    WriteIntAttribute("count", pref->GetInteger(prefs::kNumKeywords));
348  }
349
350  if (profile_metrics)
351    WriteAllProfilesMetrics(*profile_metrics);
352}
353
354void MetricsLog::WriteAllProfilesMetrics(
355    const DictionaryValue& all_profiles_metrics) {
356  const std::string profile_prefix(prefs::kProfilePrefix);
357  for (DictionaryValue::key_iterator i = all_profiles_metrics.begin_keys();
358       i != all_profiles_metrics.end_keys(); ++i) {
359    const std::string& key_name = *i;
360    if (key_name.compare(0, profile_prefix.size(), profile_prefix) == 0) {
361      DictionaryValue* profile;
362      if (all_profiles_metrics.GetDictionaryWithoutPathExpansion(key_name,
363                                                                 &profile))
364        WriteProfileMetrics(key_name.substr(profile_prefix.size()), *profile);
365    }
366  }
367}
368
369void MetricsLog::WriteProfileMetrics(const std::string& profileidhash,
370                                     const DictionaryValue& profile_metrics) {
371  OPEN_ELEMENT_FOR_SCOPE("userprofile");
372  WriteAttribute("profileidhash", profileidhash);
373  for (DictionaryValue::key_iterator i = profile_metrics.begin_keys();
374       i != profile_metrics.end_keys(); ++i) {
375    Value* value;
376    if (profile_metrics.GetWithoutPathExpansion(*i, &value)) {
377      DCHECK(*i != "id");
378      switch (value->GetType()) {
379        case Value::TYPE_STRING: {
380          std::string string_value;
381          if (value->GetAsString(&string_value)) {
382            OPEN_ELEMENT_FOR_SCOPE("profileparam");
383            WriteAttribute("name", *i);
384            WriteAttribute("value", string_value);
385          }
386          break;
387        }
388
389        case Value::TYPE_BOOLEAN: {
390          bool bool_value;
391          if (value->GetAsBoolean(&bool_value)) {
392            OPEN_ELEMENT_FOR_SCOPE("profileparam");
393            WriteAttribute("name", *i);
394            WriteIntAttribute("value", bool_value ? 1 : 0);
395          }
396          break;
397        }
398
399        case Value::TYPE_INTEGER: {
400          int int_value;
401          if (value->GetAsInteger(&int_value)) {
402            OPEN_ELEMENT_FOR_SCOPE("profileparam");
403            WriteAttribute("name", *i);
404            WriteIntAttribute("value", int_value);
405          }
406          break;
407        }
408
409        default:
410          NOTREACHED();
411          break;
412      }
413    }
414  }
415}
416
417void MetricsLog::RecordOmniboxOpenedURL(const AutocompleteLog& log) {
418  DCHECK(!locked_);
419
420  OPEN_ELEMENT_FOR_SCOPE("uielement");
421  WriteAttribute("action", "autocomplete");
422  WriteAttribute("targetidhash", "");
423  // TODO(kochi): Properly track windows.
424  WriteIntAttribute("window", 0);
425  WriteCommonEventAttributes();
426
427  {
428    OPEN_ELEMENT_FOR_SCOPE("autocomplete");
429
430    WriteIntAttribute("typedlength", static_cast<int>(log.text.length()));
431    WriteIntAttribute("selectedindex", static_cast<int>(log.selected_index));
432    WriteIntAttribute("completedlength",
433                      static_cast<int>(log.inline_autocompleted_length));
434    const std::string input_type(
435        AutocompleteInput::TypeToString(log.input_type));
436    if (!input_type.empty())
437      WriteAttribute("inputtype", input_type);
438
439    for (AutocompleteResult::const_iterator i(log.result.begin());
440         i != log.result.end(); ++i) {
441      OPEN_ELEMENT_FOR_SCOPE("autocompleteitem");
442      if (i->provider)
443        WriteAttribute("provider", i->provider->name());
444      const std::string result_type(AutocompleteMatch::TypeToString(i->type));
445      if (!result_type.empty())
446        WriteAttribute("resulttype", result_type);
447      WriteIntAttribute("relevance", i->relevance);
448      WriteIntAttribute("isstarred", i->starred ? 1 : 0);
449    }
450  }
451
452  ++num_events_;
453}
454