Chromium Code Reviews| 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; |
| 42 using content::WebContents; | 44 using content::WebContents; |
| 43 | 45 |
| 44 namespace { | 46 namespace { |
| 45 | 47 |
| 48 const char kStatsDividerCharacter = '^'; | |
| 49 | |
| 46 RenderWidgetHost* GetRenderWidgetHost(NavigationController* tab) { | 50 RenderWidgetHost* GetRenderWidgetHost(NavigationController* tab) { |
| 47 WebContents* web_contents = tab->GetWebContents(); | 51 WebContents* web_contents = tab->GetWebContents(); |
| 48 if (web_contents) { | 52 if (web_contents) { |
| 49 RenderWidgetHostView* render_widget_host_view = | 53 RenderWidgetHostView* render_widget_host_view = |
| 50 web_contents->GetRenderWidgetHostView(); | 54 web_contents->GetRenderWidgetHostView(); |
| 51 if (render_widget_host_view) | 55 if (render_widget_host_view) |
| 52 return render_widget_host_view->GetRenderWidgetHost(); | 56 return render_widget_host_view->GetRenderWidgetHost(); |
| 53 } | 57 } |
| 54 return NULL; | 58 return NULL; |
| 55 } | 59 } |
| (...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 258 // has already been terminated. | 262 // has already been terminated. |
| 259 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || | 263 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || |
| 260 !BrowserThread::IsMessageLoopValid(BrowserThread::UI)); | 264 !BrowserThread::IsMessageLoopValid(BrowserThread::UI)); |
| 261 | 265 |
| 262 WriteTimes(kLogoutTimes, | 266 WriteTimes(kLogoutTimes, |
| 263 (restart_requested_ ? kUmaRestart : kUmaLogout), | 267 (restart_requested_ ? kUmaRestart : kUmaLogout), |
| 264 kUmaLogoutPrefix, | 268 kUmaLogoutPrefix, |
| 265 logout_time_markers_); | 269 logout_time_markers_); |
| 266 } | 270 } |
| 267 | 271 |
| 272 // static | |
| 273 void BootTimesLoader::ClearLogoutStartedLastPreference() { | |
| 274 PrefService* local_state = g_browser_process->local_state(); | |
| 275 local_state->ClearPref(prefs::kLogoutStartedLast); | |
| 276 } | |
| 277 | |
| 278 void BootTimesLoader::OnBoot() { | |
| 279 PrefService* local_state = g_browser_process->local_state(); | |
| 280 const std::string logout_started_last_str = | |
| 281 local_state->GetString(prefs::kLogoutStartedLast); | |
| 282 if (logout_started_last_str.empty()) | |
| 283 return; | |
| 284 | |
| 285 // Note that kLogoutStartedLast is not cleared on format error to stay in | |
| 286 // logs in case of other fatal system errors. | |
| 287 | |
| 288 const Stats logout_started_last_stats = | |
| 289 Stats::DeserializeFromString(logout_started_last_str); | |
| 290 if (logout_started_last_stats.uptime.empty()) | |
| 291 return; | |
| 292 | |
| 293 // Extract double uptime seconds. | |
| 294 const std::string logout_started_last_uptime_str = | |
| 295 GetUptimeSeconds(logout_started_last_stats); | |
| 296 if (logout_started_last_uptime_str.empty()) | |
| 297 return; | |
| 298 | |
| 299 const std::string uptime_str = GetUptimeSeconds(GetCurrentStats()); | |
| 300 | |
| 301 double logout_started_last; | |
| 302 double uptime; | |
| 303 if (!base::StringToDouble(logout_started_last_uptime_str, | |
| 304 &logout_started_last) || | |
| 305 !base::StringToDouble(uptime_str, &uptime)) { | |
| 306 return; | |
| 307 } | |
| 308 | |
| 309 if (logout_started_last >= uptime) { | |
| 310 // Reboot happened. | |
| 311 ClearLogoutStartedLastPreference(); | |
| 312 return; | |
| 313 } | |
| 314 // Write /tmp/uptime-logout-started as well. | |
| 315 const char kLogoutStarted[] = "logout-started"; | |
| 316 RecordStatsWithCallback( | |
| 317 kLogoutStarted, | |
| 318 logout_started_last_stats, | |
| 319 base::Bind(&BootTimesLoader::ClearLogoutStartedLastPreference)); | |
| 320 } | |
| 321 | |
| 322 void BootTimesLoader::OnLogoutStarted(PrefService* state) { | |
| 323 const std::string uptime = GetCurrentStats().SerializeToString(); | |
| 324 if (!uptime.empty()) | |
| 325 state->SetString(prefs::kLogoutStartedLast, uptime); | |
| 326 } | |
| 327 | |
| 268 void BootTimesLoader::RecordStats(const std::string& name, const Stats& stats) { | 328 void BootTimesLoader::RecordStats(const std::string& name, const Stats& stats) { |
| 269 BrowserThread::PostTask( | 329 BrowserThread::PostTask( |
| 270 BrowserThread::FILE, FROM_HERE, | 330 BrowserThread::FILE, FROM_HERE, |
| 271 base::Bind(&RecordStatsDelayed, name, stats.uptime, stats.disk)); | 331 base::Bind(&RecordStatsDelayed, name, stats.uptime, stats.disk)); |
| 272 } | 332 } |
| 273 | 333 |
| 334 void BootTimesLoader::RecordStatsWithCallback(const std::string& name, | |
| 335 const Stats& stats, | |
| 336 const base::Closure& callback) { | |
| 337 BrowserThread::PostBlockingPoolTaskAndReply( | |
| 338 FROM_HERE, | |
| 339 base::Bind(&RecordStatsDelayed, name, stats.uptime, stats.disk), | |
| 340 callback); | |
| 341 } | |
| 342 | |
| 343 std::string BootTimesLoader::Stats::SerializeToString() const { | |
| 344 if (uptime.empty() || disk.empty()) | |
| 345 return std::string(); | |
| 346 | |
| 347 return uptime + kStatsDividerCharacter + disk; | |
| 348 } | |
| 349 | |
| 350 // static | |
| 351 BootTimesLoader::Stats BootTimesLoader::Stats::DeserializeFromString( | |
| 352 const std::string& value) { | |
| 353 Stats result; | |
| 354 | |
| 355 const size_t divider_at = value.find_first_of(kStatsDividerCharacter); | |
| 356 if (divider_at != std::string::npos) { | |
| 357 result.uptime = value.substr(0, divider_at); | |
| 358 result.disk = value.substr(divider_at + 1); | |
| 359 } | |
|
stevenjb
2014/05/30 16:20:23
While on the one hand this is fairly trivial, if w
Alexander Alekseev
2014/05/30 21:12:45
Done.
| |
| 360 return result; | |
| 361 } | |
| 362 | |
| 274 BootTimesLoader::Stats BootTimesLoader::GetCurrentStats() { | 363 BootTimesLoader::Stats BootTimesLoader::GetCurrentStats() { |
| 275 const base::FilePath kProcUptime(FPL("/proc/uptime")); | 364 const base::FilePath kProcUptime(FPL("/proc/uptime")); |
| 276 const base::FilePath kDiskStat(FPL("/sys/block/sda/stat")); | 365 const base::FilePath kDiskStat(FPL("/sys/block/sda/stat")); |
| 277 Stats stats; | 366 Stats stats; |
| 278 base::ThreadRestrictions::ScopedAllowIO allow_io; | 367 base::ThreadRestrictions::ScopedAllowIO allow_io; |
| 279 base::ReadFileToString(kProcUptime, &stats.uptime); | 368 base::ReadFileToString(kProcUptime, &stats.uptime); |
| 280 base::ReadFileToString(kDiskStat, &stats.disk); | 369 base::ReadFileToString(kDiskStat, &stats.disk); |
| 281 return stats; | 370 return stats; |
| 282 } | 371 } |
| 283 | 372 |
| 373 std::string BootTimesLoader::GetUptimeSeconds( | |
| 374 const BootTimesLoader::Stats& stats) { | |
| 375 std::string uptime = stats.uptime; | |
| 376 const size_t space_at = uptime.find_first_of(' '); | |
|
stevenjb
2014/05/30 16:20:23
Where is this ' ' coming from?
Alexander Alekseev
2014/05/30 21:12:45
/proc/uptime has two fields: uptime in seconds and
| |
| 377 if (space_at == std::string::npos) | |
| 378 return std::string(); | |
| 379 | |
| 380 uptime.resize(space_at); | |
| 381 return uptime; | |
| 382 } | |
|
stevenjb
2014/05/30 16:20:23
This seems like either it should be a member of St
Alexander Alekseev
2014/05/30 21:12:45
There is a member of type Stats, so I decided to l
| |
| 383 | |
| 284 void BootTimesLoader::RecordCurrentStats(const std::string& name) { | 384 void BootTimesLoader::RecordCurrentStats(const std::string& name) { |
| 285 RecordStats(name, GetCurrentStats()); | 385 RecordStats(name, GetCurrentStats()); |
| 286 } | 386 } |
| 287 | 387 |
| 288 void BootTimesLoader::SaveChromeMainStats() { | 388 void BootTimesLoader::SaveChromeMainStats() { |
| 289 chrome_main_stats_ = GetCurrentStats(); | 389 chrome_main_stats_ = GetCurrentStats(); |
| 290 } | 390 } |
| 291 | 391 |
| 292 void BootTimesLoader::RecordChromeMainStats() { | 392 void BootTimesLoader::RecordChromeMainStats() { |
| 293 RecordStats(kChromeMain, chrome_main_stats_); | 393 RecordStats(kChromeMain, chrome_main_stats_); |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 398 GetRenderWidgetHost(&web_contents->GetController()); | 498 GetRenderWidgetHost(&web_contents->GetController()); |
| 399 render_widget_hosts_loading_.erase(render_widget_host); | 499 render_widget_hosts_loading_.erase(render_widget_host); |
| 400 break; | 500 break; |
| 401 } | 501 } |
| 402 default: | 502 default: |
| 403 break; | 503 break; |
| 404 } | 504 } |
| 405 } | 505 } |
| 406 | 506 |
| 407 } // namespace chromeos | 507 } // namespace chromeos |
| OLD | NEW |