login_unlock_throughput_recorder.cc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Copyright 2020 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. #include "ash/metrics/login_unlock_throughput_recorder.h"
  5. #include "ash/public/cpp/metrics_util.h"
  6. #include "ash/session/session_controller_impl.h"
  7. #include "ash/shell.h"
  8. #include "ash/wm/tablet_mode/tablet_mode_controller.h"
  9. #include "base/bind.h"
  10. #include "base/metrics/histogram_functions.h"
  11. #include "chromeos/login/login_state/login_state.h"
  12. #include "chromeos/metrics/login_event_recorder.h"
  13. #include "ui/aura/window.h"
  14. #include "ui/aura/window_tree_host.h"
  15. #include "ui/compositor/total_animation_throughput_reporter.h"
  16. namespace ash {
  17. namespace {
  18. std::string GetDeviceModeSuffix() {
  19. return Shell::Get()->tablet_mode_controller()->InTabletMode()
  20. ? "TabletMode"
  21. : "ClamshellMode";
  22. }
  23. void RecordMetrics(const base::TimeTicks& start,
  24. const cc::FrameSequenceMetrics::CustomReportData& data,
  25. const char* smoothness_name,
  26. const char* jank_name,
  27. const char* duration_name) {
  28. DCHECK(data.frames_expected);
  29. // Report could happen during Shell shutdown. Early out in that case.
  30. if (!Shell::HasInstance() || !Shell::Get()->tablet_mode_controller())
  31. return;
  32. int duration_ms = (base::TimeTicks::Now() - start).InMilliseconds();
  33. int smoothness, jank;
  34. smoothness = metrics_util::CalculateSmoothness(data);
  35. jank = metrics_util::CalculateJank(data);
  36. std::string suffix = GetDeviceModeSuffix();
  37. base::UmaHistogramPercentage(smoothness_name + suffix, smoothness);
  38. base::UmaHistogramPercentage(jank_name + suffix, jank);
  39. // TODO(crbug.com/1143898): Deprecate this metrics once the login/unlock
  40. // performance issue is resolved.
  41. base::UmaHistogramCustomTimes(duration_name + suffix,
  42. base::Milliseconds(duration_ms),
  43. base::Milliseconds(100), base::Seconds(5), 50);
  44. }
  45. void ReportLogin(base::TimeTicks start,
  46. const cc::FrameSequenceMetrics::CustomReportData& data) {
  47. if (!data.frames_expected) {
  48. LOG(WARNING) << "Zero frames expected in login animation throughput data";
  49. return;
  50. }
  51. chromeos::LoginEventRecorder::Get()->AddLoginTimeMarker(
  52. "LoginAnimationEnd",
  53. /*send_to_uma=*/false,
  54. /*write_to_file=*/false);
  55. chromeos::LoginEventRecorder::Get()->RunScheduledWriteLoginTimes();
  56. RecordMetrics(start, data, "Ash.LoginAnimation.Smoothness.",
  57. "Ash.LoginAnimation.Jank.", "Ash.LoginAnimation.Duration.");
  58. }
  59. void ReportUnlock(base::TimeTicks start,
  60. const cc::FrameSequenceMetrics::CustomReportData& data) {
  61. if (!data.frames_expected) {
  62. LOG(WARNING) << "Zero frames expected in unlock animation throughput data";
  63. return;
  64. }
  65. RecordMetrics(start, data, "Ash.UnlockAnimation.Smoothness.",
  66. "Ash.UnlockAnimation.Jank.", "Ash.UnlockAnimation.Duration.");
  67. }
  68. } // namespace
  69. LoginUnlockThroughputRecorder::LoginUnlockThroughputRecorder() {
  70. Shell::Get()->session_controller()->AddObserver(this);
  71. chromeos::LoginState::Get()->AddObserver(this);
  72. }
  73. LoginUnlockThroughputRecorder::~LoginUnlockThroughputRecorder() {
  74. Shell::Get()->session_controller()->RemoveObserver(this);
  75. chromeos::LoginState::Get()->RemoveObserver(this);
  76. }
  77. void LoginUnlockThroughputRecorder::OnLockStateChanged(bool locked) {
  78. auto logged_in_user = chromeos::LoginState::Get()->GetLoggedInUserType();
  79. if (!locked &&
  80. (logged_in_user == chromeos::LoginState::LOGGED_IN_USER_OWNER ||
  81. logged_in_user == chromeos::LoginState::LOGGED_IN_USER_REGULAR)) {
  82. auto* primary_root = Shell::GetPrimaryRootWindow();
  83. new ui::TotalAnimationThroughputReporter(
  84. primary_root->GetHost()->compositor(),
  85. base::BindOnce(&ReportUnlock, base::TimeTicks::Now()),
  86. /*self_destruct=*/true);
  87. }
  88. }
  89. void LoginUnlockThroughputRecorder::LoggedInStateChanged() {
  90. auto* login_state = chromeos::LoginState::Get();
  91. auto logged_in_user = login_state->GetLoggedInUserType();
  92. if (login_state->IsUserLoggedIn() &&
  93. (logged_in_user == chromeos::LoginState::LOGGED_IN_USER_OWNER ||
  94. logged_in_user == chromeos::LoginState::LOGGED_IN_USER_REGULAR)) {
  95. ui_recorder_.OnUserLoggedIn();
  96. auto* primary_root = Shell::GetPrimaryRootWindow();
  97. new ui::TotalAnimationThroughputReporter(
  98. primary_root->GetHost()->compositor(),
  99. base::BindOnce(&LoginUnlockThroughputRecorder::OnLoginAnimationFinish,
  100. weak_ptr_factory_.GetWeakPtr(), base::TimeTicks::Now()),
  101. /*self_destruct=*/true);
  102. }
  103. }
  104. void LoginUnlockThroughputRecorder::OnLoginAnimationFinish(
  105. base::TimeTicks start,
  106. const cc::FrameSequenceMetrics::CustomReportData& data) {
  107. ui_recorder_.OnPostLoginAnimationFinish();
  108. ReportLogin(start, data);
  109. }
  110. } // namespace ash