OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Tests the MetricsService stat recording to make sure that the numbers are | 5 // Tests the MetricsService stat recording to make sure that the numbers are |
6 // what we expect. | 6 // what we expect. |
7 | 7 |
8 #include "components/metrics/metrics_service.h" | 8 #include "components/metrics/metrics_service.h" |
9 | 9 |
10 #include <string> | 10 #include <string> |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
51 ASSERT_EQ(1UL, buckets.size()); | 51 ASSERT_EQ(1UL, buckets.size()); |
52 EXPECT_EQ(1, buckets[0].count); | 52 EXPECT_EQ(1, buckets[0].count); |
53 int32_t exit_code = buckets[0].min; | 53 int32_t exit_code = buckets[0].min; |
54 EXPECT_TRUE(WIFSIGNALED(exit_code)); | 54 EXPECT_TRUE(WIFSIGNALED(exit_code)); |
55 EXPECT_EQ(signal, WTERMSIG(exit_code)); | 55 EXPECT_EQ(signal, WTERMSIG(exit_code)); |
56 } | 56 } |
57 | 57 |
58 } // namespace | 58 } // namespace |
59 #endif // OS_MACOSX || OS_LINUX | 59 #endif // OS_MACOSX || OS_LINUX |
60 | 60 |
| 61 // This test class verifies that metrics reporting works correctly for various |
| 62 // renderer behaviors such as page loads, recording crashed tabs, and browser |
| 63 // starts. It also verifies that if a renderer process crashes, the correct exit |
| 64 // code is recorded. |
| 65 // |
| 66 // TODO(isherman): We should also verify that |
| 67 // metrics::prefs::kStabilityExitedCleanly is set correctly after each of these |
| 68 // tests, but this preference isn't set until the browser exits... it's not |
| 69 // clear to me how to test that. |
61 class MetricsServiceBrowserTest : public InProcessBrowserTest { | 70 class MetricsServiceBrowserTest : public InProcessBrowserTest { |
62 public: | 71 public: |
63 void SetUpCommandLine(base::CommandLine* command_line) override { | 72 void SetUpCommandLine(base::CommandLine* command_line) override { |
64 // Enable the metrics service for testing (in recording-only mode). | 73 // Enable the metrics service for testing (in recording-only mode). |
65 command_line->AppendSwitch(switches::kMetricsRecordingOnly); | 74 command_line->AppendSwitch(switches::kMetricsRecordingOnly); |
66 } | 75 } |
67 | 76 |
| 77 // Open three tabs then navigate to |crashy_url| and wait for the renderer to |
| 78 // crash. |
| 79 void OpenTabsAndNavigateToCrashyUrl(const std::string& crashy_url) { |
| 80 // Opens three tabs. |
| 81 OpenThreeTabs(); |
| 82 |
| 83 // Kill the process for one of the tabs by navigating to |crashy_url|. |
| 84 content::RenderProcessHostWatcher observer( |
| 85 browser()->tab_strip_model()->GetActiveWebContents(), |
| 86 content::RenderProcessHostWatcher::WATCH_FOR_PROCESS_EXIT); |
| 87 // Opens one tab. |
| 88 ui_test_utils::NavigateToURL(browser(), GURL(crashy_url)); |
| 89 observer.Wait(); |
| 90 |
| 91 // The MetricsService listens for the same notification, so the |observer| |
| 92 // might finish waiting before the MetricsService has a chance to process |
| 93 // the notification. To avoid racing here, we repeatedly run the message |
| 94 // loop until the MetricsService catches up. This should happen "real soon |
| 95 // now", since the notification is posted to all observers essentially |
| 96 // simultaneously... so busy waiting here shouldn't be too bad. |
| 97 const PrefService* prefs = g_browser_process->local_state(); |
| 98 while (!prefs->GetInteger(metrics::prefs::kStabilityRendererCrashCount)) { |
| 99 content::RunAllPendingInMessageLoop(); |
| 100 } |
| 101 } |
| 102 |
68 // Open a couple of tabs of random content. | 103 // Open a couple of tabs of random content. |
69 void OpenTabs() { | 104 // |
| 105 // Calling this method causes three page load events: |
| 106 // 1. title2.html |
| 107 // 2. iframe.html |
| 108 // 3. title1.html (iframed by iframe.html) |
| 109 void OpenThreeTabs() { |
70 const int kBrowserTestFlags = | 110 const int kBrowserTestFlags = |
71 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB | | 111 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB | |
72 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION; | 112 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION; |
73 | 113 |
74 base::FilePath test_directory; | 114 base::FilePath test_directory; |
75 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_directory)); | 115 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_directory)); |
76 | 116 |
77 base::FilePath page1_path = test_directory.AppendASCII("title2.html"); | 117 base::FilePath page1_path = test_directory.AppendASCII("title2.html"); |
78 ui_test_utils::NavigateToURLWithDisposition( | 118 ui_test_utils::NavigateToURLWithDisposition( |
79 browser(), net::FilePathToFileURL(page1_path), | 119 browser(), net::FilePathToFileURL(page1_path), |
80 WindowOpenDisposition::NEW_FOREGROUND_TAB, kBrowserTestFlags); | 120 WindowOpenDisposition::NEW_FOREGROUND_TAB, kBrowserTestFlags); |
81 | 121 |
82 base::FilePath page2_path = test_directory.AppendASCII("iframe.html"); | 122 base::FilePath page2_path = test_directory.AppendASCII("iframe.html"); |
83 ui_test_utils::NavigateToURLWithDisposition( | 123 ui_test_utils::NavigateToURLWithDisposition( |
84 browser(), net::FilePathToFileURL(page2_path), | 124 browser(), net::FilePathToFileURL(page2_path), |
85 WindowOpenDisposition::NEW_FOREGROUND_TAB, kBrowserTestFlags); | 125 WindowOpenDisposition::NEW_FOREGROUND_TAB, kBrowserTestFlags); |
86 } | 126 } |
87 }; | 127 }; |
88 | 128 |
89 IN_PROC_BROWSER_TEST_F(MetricsServiceBrowserTest, CloseRenderersNormally) { | 129 IN_PROC_BROWSER_TEST_F(MetricsServiceBrowserTest, CloseRenderersNormally) { |
90 OpenTabs(); | 130 OpenThreeTabs(); |
91 | 131 |
92 // Verify that the expected stability metrics were recorded. | 132 // Verify that the expected stability metrics were recorded. |
93 const PrefService* prefs = g_browser_process->local_state(); | 133 const PrefService* prefs = g_browser_process->local_state(); |
94 EXPECT_EQ(1, prefs->GetInteger(metrics::prefs::kStabilityLaunchCount)); | 134 EXPECT_EQ(1, prefs->GetInteger(metrics::prefs::kStabilityLaunchCount)); |
95 EXPECT_EQ(3, prefs->GetInteger(metrics::prefs::kStabilityPageLoadCount)); | 135 EXPECT_EQ(3, prefs->GetInteger(metrics::prefs::kStabilityPageLoadCount)); |
96 EXPECT_EQ(0, prefs->GetInteger(metrics::prefs::kStabilityRendererCrashCount)); | 136 EXPECT_EQ(0, prefs->GetInteger(metrics::prefs::kStabilityRendererCrashCount)); |
97 // TODO(isherman): We should also verify that | |
98 // metrics::prefs::kStabilityExitedCleanly | |
99 // is set to true, but this preference isn't set until the browser | |
100 // exits... it's not clear to me how to test that. | |
101 } | 137 } |
102 | 138 |
103 // Flaky on Linux. See http://crbug.com/131094 | 139 // Flaky on Linux. See http://crbug.com/131094 |
104 // Child crashes fail the process on ASan (see crbug.com/411251, | 140 // Child crashes fail the process on ASan (see crbug.com/411251, |
105 // crbug.com/368525). | 141 // crbug.com/368525). |
106 #if defined(OS_LINUX) || defined(ADDRESS_SANITIZER) | 142 #if defined(OS_LINUX) || defined(ADDRESS_SANITIZER) |
107 #define MAYBE_CrashRenderers DISABLED_CrashRenderers | 143 #define MAYBE_CrashRenderers DISABLED_CrashRenderers |
| 144 #define MAYBE_CheckCrashRenderers DISABLED_CheckCrashRenderers |
108 #else | 145 #else |
109 #define MAYBE_CrashRenderers CrashRenderers | 146 #define MAYBE_CrashRenderers CrashRenderers |
| 147 #define MAYBE_CheckCrashRenderers CheckCrashRenderers |
110 #endif | 148 #endif |
| 149 |
111 IN_PROC_BROWSER_TEST_F(MetricsServiceBrowserTest, MAYBE_CrashRenderers) { | 150 IN_PROC_BROWSER_TEST_F(MetricsServiceBrowserTest, MAYBE_CrashRenderers) { |
112 base::HistogramTester histogram_tester; | 151 base::HistogramTester histogram_tester; |
113 OpenTabs(); | |
114 | 152 |
115 // Kill the process for one of the tabs. | 153 OpenTabsAndNavigateToCrashyUrl(content::kChromeUICrashURL); |
116 content::RenderProcessHostWatcher observer( | |
117 browser()->tab_strip_model()->GetActiveWebContents(), | |
118 content::RenderProcessHostWatcher::WATCH_FOR_PROCESS_EXIT); | |
119 ui_test_utils::NavigateToURL(browser(), GURL(content::kChromeUICrashURL)); | |
120 observer.Wait(); | |
121 | |
122 // The MetricsService listens for the same notification, so the |observer| | |
123 // might finish waiting before the MetricsService has a chance to process the | |
124 // notification. To avoid racing here, we repeatedly run the message loop | |
125 // until the MetricsService catches up. This should happen "real soon now", | |
126 // since the notification is posted to all observers essentially | |
127 // simultaneously... so busy waiting here shouldn't be too bad. | |
128 const PrefService* prefs = g_browser_process->local_state(); | |
129 while (!prefs->GetInteger(metrics::prefs::kStabilityRendererCrashCount)) { | |
130 content::RunAllPendingInMessageLoop(); | |
131 } | |
132 | 154 |
133 // Verify that the expected stability metrics were recorded. | 155 // Verify that the expected stability metrics were recorded. |
| 156 const PrefService* prefs = g_browser_process->local_state(); |
134 EXPECT_EQ(1, prefs->GetInteger(metrics::prefs::kStabilityLaunchCount)); | 157 EXPECT_EQ(1, prefs->GetInteger(metrics::prefs::kStabilityLaunchCount)); |
135 // Expect four page loads: | 158 // The three tabs from OpenTabs() and the one tab to open chrome://crash/. |
136 // 1. title2.html | |
137 // 2. iframe.html | |
138 // 3. title1.html (iframed by iframe.html) | |
139 // 4. chrome://crash | |
140 EXPECT_EQ(4, prefs->GetInteger(metrics::prefs::kStabilityPageLoadCount)); | 159 EXPECT_EQ(4, prefs->GetInteger(metrics::prefs::kStabilityPageLoadCount)); |
141 EXPECT_EQ(1, prefs->GetInteger(metrics::prefs::kStabilityRendererCrashCount)); | 160 EXPECT_EQ(1, prefs->GetInteger(metrics::prefs::kStabilityRendererCrashCount)); |
142 // TODO(isherman): We should also verify that | |
143 // metrics::prefs::kStabilityExitedCleanly | |
144 // is set to true, but this preference isn't set until the browser | |
145 // exits... it's not clear to me how to test that. | |
146 | 161 |
147 #if defined(OS_WIN) | 162 #if defined(OS_WIN) |
148 histogram_tester.ExpectUniqueSample( | 163 histogram_tester.ExpectUniqueSample( |
149 "CrashExitCodes.Renderer", | 164 "CrashExitCodes.Renderer", |
150 std::abs(static_cast<int32_t>(STATUS_ACCESS_VIOLATION)), 1); | 165 std::abs(static_cast<int32_t>(STATUS_ACCESS_VIOLATION)), 1); |
151 #elif defined(OS_MACOSX) || defined(OS_LINUX) | 166 #elif defined(OS_MACOSX) || defined(OS_LINUX) |
152 VerifyRendererExitCodeIsSignal(histogram_tester, SIGSEGV); | 167 VerifyRendererExitCodeIsSignal(histogram_tester, SIGSEGV); |
153 #endif | 168 #endif |
154 histogram_tester.ExpectUniqueSample("Tabs.SadTab.CrashCreated", 1, 1); | 169 histogram_tester.ExpectUniqueSample("Tabs.SadTab.CrashCreated", 1, 1); |
155 } | 170 } |
156 | 171 |
| 172 IN_PROC_BROWSER_TEST_F(MetricsServiceBrowserTest, MAYBE_CheckCrashRenderers) { |
| 173 base::HistogramTester histogram_tester; |
| 174 |
| 175 OpenTabsAndNavigateToCrashyUrl(content::kChromeUICheckCrashURL); |
| 176 |
| 177 // Verify that the expected stability metrics were recorded. |
| 178 const PrefService* prefs = g_browser_process->local_state(); |
| 179 EXPECT_EQ(1, prefs->GetInteger(metrics::prefs::kStabilityLaunchCount)); |
| 180 // The three tabs from OpenTabs() and the one tab to open |
| 181 // chrome://checkcrash/. |
| 182 EXPECT_EQ(4, prefs->GetInteger(metrics::prefs::kStabilityPageLoadCount)); |
| 183 EXPECT_EQ(1, prefs->GetInteger(metrics::prefs::kStabilityRendererCrashCount)); |
| 184 |
| 185 #if defined(OS_WIN) |
| 186 histogram_tester.ExpectUniqueSample( |
| 187 "CrashExitCodes.Renderer", |
| 188 std::abs(static_cast<int32_t>(STATUS_BREAKPOINT)), 1); |
| 189 #elif defined(OS_MACOSX) || defined(OS_LINUX) |
| 190 VerifyRendererExitCodeIsSignal(histogram_tester, SIGTRAP); |
| 191 #endif |
| 192 histogram_tester.ExpectUniqueSample("Tabs.SadTab.CrashCreated", 1, 1); |
| 193 } |
| 194 |
157 // OOM code only works on Windows. | 195 // OOM code only works on Windows. |
158 #if defined(OS_WIN) && !defined(ADDRESS_SANITIZER) | 196 #if defined(OS_WIN) && !defined(ADDRESS_SANITIZER) |
159 IN_PROC_BROWSER_TEST_F(MetricsServiceBrowserTest, OOMRenderers) { | 197 IN_PROC_BROWSER_TEST_F(MetricsServiceBrowserTest, OOMRenderers) { |
160 base::HistogramTester histogram_tester; | 198 base::HistogramTester histogram_tester; |
161 OpenTabs(); | |
162 | 199 |
163 // Kill the process for one of the tabs. | 200 OpenTabsAndNavigateToCrashyUrl(content::kChromeUIMemoryExhaustURL); |
164 content::RenderProcessHostWatcher observer( | |
165 browser()->tab_strip_model()->GetActiveWebContents(), | |
166 content::RenderProcessHostWatcher::WATCH_FOR_PROCESS_EXIT); | |
167 ui_test_utils::NavigateToURL(browser(), | |
168 GURL(content::kChromeUIMemoryExhaustURL)); | |
169 observer.Wait(); | |
170 | |
171 // The MetricsService listens for the same notification, so the |observer| | |
172 // might finish waiting before the MetricsService has a chance to process the | |
173 // notification. To avoid racing here, we repeatedly run the message loop | |
174 // until the MetricsService catches up. This should happen "real soon now", | |
175 // since the notification is posted to all observers essentially | |
176 // simultaneously... so busy waiting here shouldn't be too bad. | |
177 const PrefService* prefs = g_browser_process->local_state(); | |
178 while (!prefs->GetInteger(metrics::prefs::kStabilityRendererCrashCount)) { | |
179 content::RunAllPendingInMessageLoop(); | |
180 } | |
181 | 201 |
182 // Verify that the expected stability metrics were recorded. | 202 // Verify that the expected stability metrics were recorded. |
| 203 const PrefService* prefs = g_browser_process->local_state(); |
183 EXPECT_EQ(1, prefs->GetInteger(metrics::prefs::kStabilityLaunchCount)); | 204 EXPECT_EQ(1, prefs->GetInteger(metrics::prefs::kStabilityLaunchCount)); |
| 205 // The three tabs from OpenTabs() and the one tab to open |
| 206 // chrome://memory-exhaust/. |
184 EXPECT_EQ(4, prefs->GetInteger(metrics::prefs::kStabilityPageLoadCount)); | 207 EXPECT_EQ(4, prefs->GetInteger(metrics::prefs::kStabilityPageLoadCount)); |
185 EXPECT_EQ(1, prefs->GetInteger(metrics::prefs::kStabilityRendererCrashCount)); | 208 EXPECT_EQ(1, prefs->GetInteger(metrics::prefs::kStabilityRendererCrashCount)); |
186 | 209 |
187 // On 64-bit, the Job object should terminate the renderer on an OOM. | 210 // On 64-bit, the Job object should terminate the renderer on an OOM. |
188 #if defined(ARCH_CPU_64_BITS) | 211 #if defined(ARCH_CPU_64_BITS) |
189 const int expected_exit_code = sandbox::SBOX_FATAL_MEMORY_EXCEEDED; | 212 const int expected_exit_code = sandbox::SBOX_FATAL_MEMORY_EXCEEDED; |
190 #else | 213 #else |
191 const int expected_exit_code = base::win::kOomExceptionCode; | 214 const int expected_exit_code = base::win::kOomExceptionCode; |
192 #endif | 215 #endif |
193 | 216 |
194 // Exit codes are recorded after being passed through std::abs see | 217 // Exit codes are recorded after being passed through std::abs see |
195 // MapCrashExitCodeForHistogram. | 218 // MapCrashExitCodeForHistogram. |
196 histogram_tester.ExpectUniqueSample("CrashExitCodes.Renderer", | 219 histogram_tester.ExpectUniqueSample("CrashExitCodes.Renderer", |
197 std::abs(expected_exit_code), 1); | 220 std::abs(expected_exit_code), 1); |
198 | 221 |
199 histogram_tester.ExpectUniqueSample("Tabs.SadTab.OomCreated", 1, 1); | 222 histogram_tester.ExpectUniqueSample("Tabs.SadTab.OomCreated", 1, 1); |
200 } | 223 } |
201 #endif // OS_WIN && !ADDRESS_SANITIZER | 224 #endif // OS_WIN && !ADDRESS_SANITIZER |
OLD | NEW |