1 // Copyright (c) 2013 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.
5 #include "chrome/browser/metrics/chrome_browser_main_extra_parts_metrics.h"
10 #include "base/command_line.h"
12 #include "base/metrics/histogram.h"
13 #include "base/metrics/sparse_histogram.h"
14 #include "base/sys_info.h"
15 #include "base/threading/sequenced_worker_pool.h"
16 #include "base/time/time.h"
17 #include "chrome/browser/about_flags.h"
18 #include "chrome/browser/browser_process.h"
19 #include "chrome/browser/chrome_browser_main.h"
20 #include "chrome/browser/mac/bluetooth_utility.h"
21 #include "chrome/browser/pref_service_flags_storage.h"
22 #include "chrome/browser/shell_integration.h"
23 #include "content/public/browser/browser_thread.h"
24 #include "ui/base/touch/touch_device.h"
25 #include "ui/base/ui_base_switches.h"
26 #include "ui/events/event_switches.h"
27 #include "ui/gfx/screen.h"
29 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
30 #include <gnu/libc-version.h>
32 #include "base/version.h"
34 #include "ui/base/x/x11_util.h"
36 #endif // defined(OS_LINUX) && !defined(OS_CHROMEOS)
39 #include "chrome/installer/util/google_update_settings.h"
40 #endif // defined(OS_WIN)
44 enum UMALinuxGlibcVersion
{
45 UMA_LINUX_GLIBC_NOT_PARSEABLE
,
46 UMA_LINUX_GLIBC_UNKNOWN
,
48 // To log newer versions, just update tools/metrics/histograms/histograms.xml.
51 enum UMALinuxWindowManager
{
52 UMA_LINUX_WINDOW_MANAGER_OTHER
,
53 UMA_LINUX_WINDOW_MANAGER_BLACKBOX
,
54 UMA_LINUX_WINDOW_MANAGER_CHROME_OS
, // Deprecated.
55 UMA_LINUX_WINDOW_MANAGER_COMPIZ
,
56 UMA_LINUX_WINDOW_MANAGER_ENLIGHTENMENT
,
57 UMA_LINUX_WINDOW_MANAGER_ICE_WM
,
58 UMA_LINUX_WINDOW_MANAGER_KWIN
,
59 UMA_LINUX_WINDOW_MANAGER_METACITY
,
60 UMA_LINUX_WINDOW_MANAGER_MUFFIN
,
61 UMA_LINUX_WINDOW_MANAGER_MUTTER
,
62 UMA_LINUX_WINDOW_MANAGER_OPENBOX
,
63 UMA_LINUX_WINDOW_MANAGER_XFWM4
,
64 UMA_LINUX_WINDOW_MANAGER_AWESOME
,
65 UMA_LINUX_WINDOW_MANAGER_I3
,
66 UMA_LINUX_WINDOW_MANAGER_ION3
,
67 UMA_LINUX_WINDOW_MANAGER_MATCHBOX
,
68 UMA_LINUX_WINDOW_MANAGER_NOTION
,
69 UMA_LINUX_WINDOW_MANAGER_QTILE
,
70 UMA_LINUX_WINDOW_MANAGER_RATPOISON
,
71 UMA_LINUX_WINDOW_MANAGER_STUMPWM
,
72 UMA_LINUX_WINDOW_MANAGER_WMII
,
73 UMA_LINUX_WINDOW_MANAGER_FLUXBOX
,
74 // NOTE: Append new window managers to the list above this line (i.e. don't
75 // renumber) and update LinuxWindowManagerName in
76 // tools/metrics/histograms/histograms.xml accordingly.
77 UMA_LINUX_WINDOW_MANAGER_COUNT
80 enum UMATouchEventsState
{
81 UMA_TOUCH_EVENTS_ENABLED
,
82 UMA_TOUCH_EVENTS_AUTO_ENABLED
,
83 UMA_TOUCH_EVENTS_AUTO_DISABLED
,
84 UMA_TOUCH_EVENTS_DISABLED
,
85 // NOTE: Add states only immediately above this line. Make sure to
86 // update the enum list in tools/metrics/histograms/histograms.xml
88 UMA_TOUCH_EVENTS_STATE_COUNT
91 void RecordMicroArchitectureStats() {
92 #if defined(ARCH_CPU_X86_FAMILY)
94 base::CPU::IntelMicroArchitecture arch
= cpu
.GetIntelMicroArchitecture();
95 UMA_HISTOGRAM_ENUMERATION("Platform.IntelMaxMicroArchitecture", arch
,
96 base::CPU::MAX_INTEL_MICRO_ARCHITECTURE
);
97 #endif // defined(ARCH_CPU_X86_FAMILY)
98 UMA_HISTOGRAM_SPARSE_SLOWLY("Platform.LogicalCpuCount",
99 base::SysInfo::NumberOfProcessors());
102 // Called on the blocking pool some time after startup to avoid slowing down
103 // startup with metrics that aren't trivial to compute.
104 void RecordStartupMetricsOnBlockingPool() {
106 GoogleUpdateSettings::RecordChromeUpdatePolicyHistograms();
107 #endif // defined(OS_WIN)
109 #if defined(OS_MACOSX) && !defined(OS_IOS)
110 bluetooth_utility::BluetoothAvailability availability
=
111 bluetooth_utility::GetBluetoothAvailability();
112 UMA_HISTOGRAM_ENUMERATION("OSX.BluetoothAvailability",
114 bluetooth_utility::BLUETOOTH_AVAILABILITY_COUNT
);
115 #endif // defined(OS_MACOSX) && !defined(OS_IOS)
118 void RecordLinuxGlibcVersion() {
119 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
120 Version
version(gnu_get_libc_version());
122 UMALinuxGlibcVersion glibc_version_result
= UMA_LINUX_GLIBC_NOT_PARSEABLE
;
123 if (version
.IsValid() && version
.components().size() == 2) {
124 glibc_version_result
= UMA_LINUX_GLIBC_UNKNOWN
;
125 uint32_t glibc_major_version
= version
.components()[0];
126 uint32_t glibc_minor_version
= version
.components()[1];
127 if (glibc_major_version
== 2) {
128 // A constant to translate glibc 2.x minor versions to their
129 // equivalent UMALinuxGlibcVersion values.
130 const int kGlibcMinorVersionTranslationOffset
= 11 - UMA_LINUX_GLIBC_2_11
;
131 uint32_t translated_glibc_minor_version
=
132 glibc_minor_version
- kGlibcMinorVersionTranslationOffset
;
133 if (translated_glibc_minor_version
>= UMA_LINUX_GLIBC_2_11
) {
134 glibc_version_result
=
135 static_cast<UMALinuxGlibcVersion
>(translated_glibc_minor_version
);
139 UMA_HISTOGRAM_SPARSE_SLOWLY("Linux.GlibcVersion", glibc_version_result
);
143 #if defined(USE_X11) && !defined(OS_CHROMEOS)
144 UMALinuxWindowManager
GetLinuxWindowManager() {
145 switch (ui::GuessWindowManager()) {
147 return UMA_LINUX_WINDOW_MANAGER_OTHER
;
149 return UMA_LINUX_WINDOW_MANAGER_AWESOME
;
150 case ui::WM_BLACKBOX
:
151 return UMA_LINUX_WINDOW_MANAGER_BLACKBOX
;
153 return UMA_LINUX_WINDOW_MANAGER_COMPIZ
;
154 case ui::WM_ENLIGHTENMENT
:
155 return UMA_LINUX_WINDOW_MANAGER_ENLIGHTENMENT
;
157 return UMA_LINUX_WINDOW_MANAGER_FLUXBOX
;
159 return UMA_LINUX_WINDOW_MANAGER_I3
;
161 return UMA_LINUX_WINDOW_MANAGER_ICE_WM
;
163 return UMA_LINUX_WINDOW_MANAGER_ION3
;
165 return UMA_LINUX_WINDOW_MANAGER_KWIN
;
166 case ui::WM_MATCHBOX
:
167 return UMA_LINUX_WINDOW_MANAGER_MATCHBOX
;
168 case ui::WM_METACITY
:
169 return UMA_LINUX_WINDOW_MANAGER_METACITY
;
171 return UMA_LINUX_WINDOW_MANAGER_MUFFIN
;
173 return UMA_LINUX_WINDOW_MANAGER_MUTTER
;
175 return UMA_LINUX_WINDOW_MANAGER_NOTION
;
177 return UMA_LINUX_WINDOW_MANAGER_OPENBOX
;
179 return UMA_LINUX_WINDOW_MANAGER_QTILE
;
180 case ui::WM_RATPOISON
:
181 return UMA_LINUX_WINDOW_MANAGER_RATPOISON
;
183 return UMA_LINUX_WINDOW_MANAGER_STUMPWM
;
185 return UMA_LINUX_WINDOW_MANAGER_WMII
;
187 return UMA_LINUX_WINDOW_MANAGER_XFWM4
;
189 return UMA_LINUX_WINDOW_MANAGER_OTHER
;
193 void RecordTouchEventState() {
194 const base::CommandLine
& command_line
=
195 *base::CommandLine::ForCurrentProcess();
196 const std::string touch_enabled_switch
=
197 command_line
.HasSwitch(switches::kTouchEvents
) ?
198 command_line
.GetSwitchValueASCII(switches::kTouchEvents
) :
199 switches::kTouchEventsAuto
;
201 UMATouchEventsState state
;
202 if (touch_enabled_switch
.empty() ||
203 touch_enabled_switch
== switches::kTouchEventsEnabled
) {
204 state
= UMA_TOUCH_EVENTS_ENABLED
;
205 } else if (touch_enabled_switch
== switches::kTouchEventsAuto
) {
206 state
= ui::IsTouchDevicePresent() ?
207 UMA_TOUCH_EVENTS_AUTO_ENABLED
: UMA_TOUCH_EVENTS_AUTO_DISABLED
;
208 } else if (touch_enabled_switch
== switches::kTouchEventsDisabled
) {
209 state
= UMA_TOUCH_EVENTS_DISABLED
;
215 UMA_HISTOGRAM_ENUMERATION("Touchscreen.TouchEventsEnabled", state
,
216 UMA_TOUCH_EVENTS_STATE_COUNT
);
221 ChromeBrowserMainExtraPartsMetrics::ChromeBrowserMainExtraPartsMetrics()
222 : display_count_(0), is_screen_observer_(false) {
225 ChromeBrowserMainExtraPartsMetrics::~ChromeBrowserMainExtraPartsMetrics() {
226 if (is_screen_observer_
)
227 gfx::Screen::GetNativeScreen()->RemoveObserver(this);
230 void ChromeBrowserMainExtraPartsMetrics::PreProfileInit() {
231 RecordMicroArchitectureStats();
234 void ChromeBrowserMainExtraPartsMetrics::PreBrowserStart() {
235 about_flags::PrefServiceFlagsStorage
flags_storage_(
236 g_browser_process
->local_state());
237 about_flags::RecordUMAStatistics(&flags_storage_
);
240 void ChromeBrowserMainExtraPartsMetrics::PostBrowserStart() {
241 RecordLinuxGlibcVersion();
242 #if defined(USE_X11) && !defined(OS_CHROMEOS)
243 UMA_HISTOGRAM_ENUMERATION("Linux.WindowManager",
244 GetLinuxWindowManager(),
245 UMA_LINUX_WINDOW_MANAGER_COUNT
);
247 RecordTouchEventState();
249 #if defined(OS_MACOSX) && !defined(OS_IOS)
251 #endif // defined(OS_MACOSX) && !defined(OS_IOS)
253 const int kStartupMetricsGatheringDelaySeconds
= 45;
254 content::BrowserThread::GetBlockingPool()->PostDelayedTask(
256 base::Bind(&RecordStartupMetricsOnBlockingPool
),
257 base::TimeDelta::FromSeconds(kStartupMetricsGatheringDelaySeconds
));
259 display_count_
= gfx::Screen::GetNativeScreen()->GetNumDisplays();
260 UMA_HISTOGRAM_COUNTS_100("Hardware.Display.Count.OnStartup", display_count_
);
261 gfx::Screen::GetNativeScreen()->AddObserver(this);
262 is_screen_observer_
= true;
264 #if !defined(OS_ANDROID)
265 first_web_contents_profiler_
=
266 FirstWebContentsProfiler::CreateProfilerForFirstWebContents(this).Pass();
267 #endif // !defined(OS_ANDROID)
270 void ChromeBrowserMainExtraPartsMetrics::OnDisplayAdded(
271 const gfx::Display
& new_display
) {
272 EmitDisplaysChangedMetric();
275 void ChromeBrowserMainExtraPartsMetrics::OnDisplayRemoved(
276 const gfx::Display
& old_display
) {
277 EmitDisplaysChangedMetric();
280 void ChromeBrowserMainExtraPartsMetrics::OnDisplayMetricsChanged(
281 const gfx::Display
& display
,
282 uint32_t changed_metrics
) {
285 void ChromeBrowserMainExtraPartsMetrics::ProfilerFinishedCollectingMetrics() {
286 first_web_contents_profiler_
.reset();
289 void ChromeBrowserMainExtraPartsMetrics::EmitDisplaysChangedMetric() {
290 int display_count
= gfx::Screen::GetNativeScreen()->GetNumDisplays();
291 if (display_count
!= display_count_
) {
292 display_count_
= display_count
;
293 UMA_HISTOGRAM_COUNTS_100("Hardware.Display.Count.OnChange", display_count_
);
299 void AddMetricsExtraParts(ChromeBrowserMainParts
* main_parts
) {
300 main_parts
->AddParts(new ChromeBrowserMainExtraPartsMetrics());
303 } // namespace chrome