login_event_recorder.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // Copyright 2014 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. #ifndef CHROMEOS_METRICS_LOGIN_EVENT_RECORDER_H_
  5. #define CHROMEOS_METRICS_LOGIN_EVENT_RECORDER_H_
  6. #include <vector>
  7. #include "base/callback.h"
  8. #include "base/component_export.h"
  9. #include "base/memory/ref_counted.h"
  10. #include "base/time/time.h"
  11. #include "third_party/abseil-cpp/absl/types/optional.h"
  12. namespace base {
  13. class SequencedTaskRunner;
  14. }
  15. namespace chromeos {
  16. // LoginEventRecorder is a utility class used to save the bootimes of Chrome OS.
  17. class COMPONENT_EXPORT(CHROMEOS_METRICS) LoginEventRecorder {
  18. public:
  19. class TimeMarker {
  20. public:
  21. TimeMarker(const char* name,
  22. absl::optional<std::string> url,
  23. bool send_to_uma,
  24. bool write_to_file);
  25. TimeMarker(const TimeMarker& other);
  26. ~TimeMarker();
  27. const char* name() const { return name_; }
  28. base::Time time() const { return time_; }
  29. const absl::optional<std::string>& url() const { return url_; }
  30. bool send_to_uma() const { return send_to_uma_; }
  31. bool write_to_file() const { return write_to_file_; }
  32. // comparator for sorting
  33. bool operator<(const TimeMarker& other) const {
  34. return time_ < other.time_;
  35. }
  36. private:
  37. friend class std::vector<TimeMarker>;
  38. const char* name_;
  39. base::Time time_ = base::Time::NowFromSystemTime();
  40. absl::optional<std::string> url_;
  41. bool send_to_uma_;
  42. bool write_to_file_;
  43. };
  44. class Stats {
  45. public:
  46. // Initializes stats with current /proc values.
  47. static Stats GetCurrentStats();
  48. // Returns JSON representation.
  49. std::string SerializeToString() const;
  50. // Creates new object from JSON representation.
  51. static Stats DeserializeFromString(const std::string& value);
  52. const std::string& uptime() const { return uptime_; }
  53. const std::string& disk() const { return disk_; }
  54. // Writes "uptime in seconds" to result. (This is first field in uptime_.)
  55. // Returns true on successful conversion.
  56. bool UptimeDouble(double* result) const;
  57. // Stores stats to 'type-name' file with the given |name|.
  58. // I.e. '/tmp/uptime-logout-started' and '/tmp/disk-logout-started' for
  59. // name='logout-started'.
  60. //
  61. // When |write_flag_file| is true, also creates 'stats-name.written' flag
  62. // file to signal that stats were appended.
  63. // I.e. '/tmp/stats-logout-started.written' for name='logout-started'.
  64. void RecordStats(const std::string& name, bool write_flag_file) const;
  65. void RecordStatsWithCallback(const std::string& name,
  66. bool write_flag_file,
  67. base::OnceClosure callback) const;
  68. private:
  69. // Runs asynchronously when RecordStats(WithCallback) is called.
  70. void RecordStatsAsync(const std::string& name, bool write_flag_file) const;
  71. std::string uptime_;
  72. std::string disk_;
  73. };
  74. LoginEventRecorder();
  75. LoginEventRecorder(const LoginEventRecorder&) = delete;
  76. LoginEventRecorder& operator=(const LoginEventRecorder&) = delete;
  77. virtual ~LoginEventRecorder();
  78. static LoginEventRecorder* Get();
  79. // Add a time marker for login. A timeline will be dumped to
  80. // /tmp/login-times-sent after login is done. If |send_to_uma| is true
  81. // the time between this marker and the last will be sent to UMA with
  82. // the identifier BootTime.|marker_name|.
  83. void AddLoginTimeMarker(const char* marker_name,
  84. bool send_to_uma,
  85. bool write_to_file = true);
  86. void AddLoginTimeMarkerWithURL(const char* marker_name,
  87. absl::optional<std::string> url,
  88. bool send_to_uma,
  89. bool write_to_file = true);
  90. // Add a time marker for logout. A timeline will be dumped to
  91. // /tmp/logout-times-sent after logout is done. If |send_to_uma| is true
  92. // the time between this marker and the last will be sent to UMA with
  93. // the identifier ShutdownTime.|marker_name|.
  94. void AddLogoutTimeMarker(const char* marker_name, bool send_to_uma);
  95. // Record events for successful authentication.
  96. void RecordAuthenticationSuccess();
  97. // Record events for authentication failure.
  98. void RecordAuthenticationFailure();
  99. void ClearLoginTimeMarkers();
  100. // Records current uptime and disk usage for metrics use.
  101. // Posts task to file thread.
  102. // name will be used as part of file names in /tmp.
  103. // Existing stats files will not be overwritten.
  104. void RecordCurrentStats(const std::string& name);
  105. // Schedule writing login times to logs.
  106. void ScheduleWriteLoginTimes(const std::string base_name,
  107. const std::string uma_name,
  108. const std::string uma_prefix);
  109. // Immediately execute the task to write login times.
  110. void RunScheduledWriteLoginTimes();
  111. // Write logout times to logs.
  112. void WriteLogoutTimes(const std::string base_name,
  113. const std::string uma_name,
  114. const std::string uma_prefix);
  115. // Stores a copy of the events to be retrieved by tests.
  116. // Also all future events will have stored copies for testing.
  117. void PrepareEventCollectionForTesting();
  118. // Returns list of all events collected.
  119. const std::vector<TimeMarker>& GetCollectedLoginEventsForTesting();
  120. private:
  121. void AddMarker(std::vector<TimeMarker>* vector, TimeMarker&& marker);
  122. void WriteLoginTimesDelayed();
  123. std::vector<TimeMarker> login_time_markers_;
  124. std::vector<TimeMarker> logout_time_markers_;
  125. base::OnceCallback<void(std::vector<TimeMarker>)> callback_;
  126. scoped_refptr<base::SequencedTaskRunner> task_runner_;
  127. // This list is never cleared. It has copy of all the login events that
  128. // login_time_markers_ had when PrepareEventCollectionForTesting() was
  129. // called and all login avents since that moment.
  130. absl::optional<std::vector<TimeMarker>> login_time_markers_for_testing_;
  131. base::WeakPtrFactory<LoginEventRecorder> weak_ptr_factory_{this};
  132. };
  133. } // namespace chromeos
  134. // TODO(https://crbug.com/1164001): remove after the migration is finished.
  135. namespace ash {
  136. using ::chromeos::LoginEventRecorder;
  137. }
  138. #endif // CHROMEOS_METRICS_LOGIN_EVENT_RECORDER_H_