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 #include "chrome/browser/chromeos/boot_times_loader.h" | 5 #include "chrome/browser/chromeos/boot_times_loader.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
11 #include "base/file_util.h" | 11 #include "base/file_util.h" |
12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
13 #include "base/lazy_instance.h" | 13 #include "base/lazy_instance.h" |
14 #include "base/location.h" | 14 #include "base/location.h" |
15 #include "base/message_loop/message_loop.h" | 15 #include "base/message_loop/message_loop.h" |
16 #include "base/message_loop/message_loop_proxy.h" | 16 #include "base/message_loop/message_loop_proxy.h" |
17 #include "base/metrics/histogram.h" | 17 #include "base/metrics/histogram.h" |
18 #include "base/prefs/pref_service.h" | |
18 #include "base/strings/string_number_conversions.h" | 19 #include "base/strings/string_number_conversions.h" |
19 #include "base/strings/string_util.h" | 20 #include "base/strings/string_util.h" |
20 #include "base/strings/stringprintf.h" | 21 #include "base/strings/stringprintf.h" |
21 #include "base/threading/thread.h" | 22 #include "base/threading/thread.h" |
22 #include "base/threading/thread_restrictions.h" | 23 #include "base/threading/thread_restrictions.h" |
23 #include "base/time/time.h" | 24 #include "base/time/time.h" |
24 #include "chrome/browser/browser_process.h" | 25 #include "chrome/browser/browser_process.h" |
25 #include "chrome/browser/chrome_notification_types.h" | 26 #include "chrome/browser/chrome_notification_types.h" |
26 #include "chrome/browser/chromeos/login/auth/authentication_notification_details .h" | 27 #include "chrome/browser/chromeos/login/auth/authentication_notification_details .h" |
27 #include "chrome/browser/chromeos/login/users/user_manager.h" | 28 #include "chrome/browser/chromeos/login/users/user_manager.h" |
28 #include "chrome/browser/ui/browser.h" | 29 #include "chrome/browser/ui/browser.h" |
29 #include "chrome/browser/ui/browser_iterator.h" | 30 #include "chrome/browser/ui/browser_iterator.h" |
30 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 31 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
31 #include "chrome/common/chrome_switches.h" | 32 #include "chrome/common/chrome_switches.h" |
33 #include "chrome/common/pref_names.h" | |
32 #include "content/public/browser/browser_thread.h" | 34 #include "content/public/browser/browser_thread.h" |
33 #include "content/public/browser/navigation_controller.h" | 35 #include "content/public/browser/navigation_controller.h" |
34 #include "content/public/browser/notification_service.h" | 36 #include "content/public/browser/notification_service.h" |
35 #include "content/public/browser/render_widget_host_view.h" | 37 #include "content/public/browser/render_widget_host_view.h" |
36 #include "content/public/browser/web_contents.h" | 38 #include "content/public/browser/web_contents.h" |
37 | 39 |
38 using content::BrowserThread; | 40 using content::BrowserThread; |
39 using content::NavigationController; | 41 using content::NavigationController; |
40 using content::RenderWidgetHost; | 42 using content::RenderWidgetHost; |
41 using content::RenderWidgetHostView; | 43 using content::RenderWidgetHostView; |
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
258 // has already been terminated. | 260 // has already been terminated. |
259 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || | 261 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || |
260 !BrowserThread::IsMessageLoopValid(BrowserThread::UI)); | 262 !BrowserThread::IsMessageLoopValid(BrowserThread::UI)); |
261 | 263 |
262 WriteTimes(kLogoutTimes, | 264 WriteTimes(kLogoutTimes, |
263 (restart_requested_ ? kUmaRestart : kUmaLogout), | 265 (restart_requested_ ? kUmaRestart : kUmaLogout), |
264 kUmaLogoutPrefix, | 266 kUmaLogoutPrefix, |
265 logout_time_markers_); | 267 logout_time_markers_); |
266 } | 268 } |
267 | 269 |
270 // static | |
271 void BootTimesLoader::ClearLogoutStartedLastPreference() { | |
272 PrefService* local_state = g_browser_process->local_state(); | |
273 local_state->ClearPref(prefs::kLogoutStartedLast); | |
274 } | |
275 | |
276 void BootTimesLoader::OnBoot() { | |
277 PrefService* local_state = g_browser_process->local_state(); | |
278 const std::string logout_started_last_str = | |
279 local_state->GetString(prefs::kLogoutStartedLast); | |
280 if (logout_started_last_str.empty()) | |
281 return; | |
282 | |
283 // Note that kLogoutStartedLast is not cleared on format error to stay in | |
284 // logs in case of other fatal system errors. | |
285 | |
286 const Stats logout_started_last_stats = | |
287 Stats::DeserializeFromString(logout_started_last_str); | |
288 if (logout_started_last_stats.uptime.empty()) | |
289 return; | |
290 | |
291 // Extract double uptime seconds. | |
292 const std::string logout_started_last_uptime_str = | |
293 GetUptimeSeconds(logout_started_last_stats); | |
294 if (logout_started_last_uptime_str.empty()) | |
295 return; | |
296 | |
297 const std::string uptime_str = GetUptimeSeconds(GetCurrentStats()); | |
298 | |
299 double logout_started_last; | |
300 double uptime; | |
301 if (!base::StringToDouble(logout_started_last_uptime_str, | |
302 &logout_started_last) || | |
303 !base::StringToDouble(uptime_str, &uptime)) { | |
304 return; | |
305 } | |
306 | |
307 if (logout_started_last >= uptime) { | |
308 // Reboot happened. | |
309 ClearLogoutStartedLastPreference(); | |
310 return; | |
311 } | |
312 // Write /tmp/uptime-logout-started as well. | |
313 const char kLogoutStarted[] = "logout-started"; | |
314 RecordStatsWithCallback( | |
315 kLogoutStarted, | |
316 logout_started_last_stats, | |
317 base::Bind(&BootTimesLoader::ClearLogoutStartedLastPreference)); | |
318 } | |
319 | |
320 void BootTimesLoader::OnLogoutStarted(PrefService* state) { | |
321 const std::string uptime = GetCurrentStats().SerializeToString(); | |
322 if (!uptime.empty()) | |
323 state->SetString(prefs::kLogoutStartedLast, uptime); | |
324 } | |
325 | |
268 void BootTimesLoader::RecordStats(const std::string& name, const Stats& stats) { | 326 void BootTimesLoader::RecordStats(const std::string& name, const Stats& stats) { |
269 BrowserThread::PostTask( | 327 BrowserThread::PostTask( |
270 BrowserThread::FILE, FROM_HERE, | 328 BrowserThread::FILE, FROM_HERE, |
271 base::Bind(&RecordStatsDelayed, name, stats.uptime, stats.disk)); | 329 base::Bind(&RecordStatsDelayed, name, stats.uptime, stats.disk)); |
272 } | 330 } |
273 | 331 |
332 void BootTimesLoader::RecordStatsWithCallback(const std::string& name, | |
333 const Stats& stats, | |
334 const base::Closure& callback) { | |
335 BrowserThread::PostBlockingPoolTaskAndReply( | |
336 FROM_HERE, | |
337 base::Bind(&RecordStatsDelayed, name, stats.uptime, stats.disk), | |
338 callback); | |
339 } | |
340 | |
341 std::string BootTimesLoader::Stats::SerializeToString() const { | |
342 if (uptime.empty() || disk.empty()) | |
343 return std::string(); | |
344 | |
345 return uptime + '^' + disk; | |
Nikita (slow)
2014/05/30 07:07:03
nit: Please extract this divider as a constant.
Alexander Alekseev
2014/05/30 18:44:54
Done.
| |
346 } | |
347 | |
348 // static | |
349 BootTimesLoader::Stats BootTimesLoader::Stats::DeserializeFromString( | |
350 const std::string& value) { | |
351 Stats result; | |
352 | |
353 const size_t divider_at = value.find_first_of('^'); | |
354 if (divider_at != std::string::npos) { | |
355 result.uptime = value.substr(0, divider_at); | |
356 result.disk = value.substr(divider_at + 1); | |
357 } | |
358 return result; | |
359 } | |
360 | |
274 BootTimesLoader::Stats BootTimesLoader::GetCurrentStats() { | 361 BootTimesLoader::Stats BootTimesLoader::GetCurrentStats() { |
275 const base::FilePath kProcUptime(FPL("/proc/uptime")); | 362 const base::FilePath kProcUptime(FPL("/proc/uptime")); |
276 const base::FilePath kDiskStat(FPL("/sys/block/sda/stat")); | 363 const base::FilePath kDiskStat(FPL("/sys/block/sda/stat")); |
277 Stats stats; | 364 Stats stats; |
278 base::ThreadRestrictions::ScopedAllowIO allow_io; | 365 base::ThreadRestrictions::ScopedAllowIO allow_io; |
279 base::ReadFileToString(kProcUptime, &stats.uptime); | 366 base::ReadFileToString(kProcUptime, &stats.uptime); |
280 base::ReadFileToString(kDiskStat, &stats.disk); | 367 base::ReadFileToString(kDiskStat, &stats.disk); |
281 return stats; | 368 return stats; |
282 } | 369 } |
283 | 370 |
371 std::string BootTimesLoader::GetUptimeSeconds( | |
372 const BootTimesLoader::Stats& stats) { | |
373 std::string uptime = stats.uptime; | |
374 const size_t space_at = uptime.find_first_of(' '); | |
375 if (space_at == std::string::npos) | |
376 return std::string(); | |
377 | |
378 uptime.resize(space_at); | |
379 return uptime; | |
380 } | |
381 | |
284 void BootTimesLoader::RecordCurrentStats(const std::string& name) { | 382 void BootTimesLoader::RecordCurrentStats(const std::string& name) { |
285 RecordStats(name, GetCurrentStats()); | 383 RecordStats(name, GetCurrentStats()); |
286 } | 384 } |
287 | 385 |
288 void BootTimesLoader::SaveChromeMainStats() { | 386 void BootTimesLoader::SaveChromeMainStats() { |
289 chrome_main_stats_ = GetCurrentStats(); | 387 chrome_main_stats_ = GetCurrentStats(); |
290 } | 388 } |
291 | 389 |
292 void BootTimesLoader::RecordChromeMainStats() { | 390 void BootTimesLoader::RecordChromeMainStats() { |
293 RecordStats(kChromeMain, chrome_main_stats_); | 391 RecordStats(kChromeMain, chrome_main_stats_); |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
398 GetRenderWidgetHost(&web_contents->GetController()); | 496 GetRenderWidgetHost(&web_contents->GetController()); |
399 render_widget_hosts_loading_.erase(render_widget_host); | 497 render_widget_hosts_loading_.erase(render_widget_host); |
400 break; | 498 break; |
401 } | 499 } |
402 default: | 500 default: |
403 break; | 501 break; |
404 } | 502 } |
405 } | 503 } |
406 | 504 |
407 } // namespace chromeos | 505 } // namespace chromeos |
OLD | NEW |