metrics_utils.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. //
  2. // Copyright (C) 2015 The Android Open Source Project
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. #ifndef UPDATE_ENGINE_METRICS_UTILS_H_
  17. #define UPDATE_ENGINE_METRICS_UTILS_H_
  18. #include <string>
  19. #include <base/time/time.h>
  20. #include "update_engine/common/clock_interface.h"
  21. #include "update_engine/common/error_code.h"
  22. #include "update_engine/common/prefs_interface.h"
  23. #include "update_engine/connection_utils.h"
  24. #include "update_engine/metrics_constants.h"
  25. #include "update_engine/metrics_reporter_interface.h"
  26. namespace chromeos_update_engine {
  27. class SystemState;
  28. namespace metrics_utils {
  29. // Transforms a ErrorCode value into a metrics::DownloadErrorCode.
  30. // This obviously only works for errors related to downloading so if |code|
  31. // is e.g. |ErrorCode::kFilesystemCopierError| then
  32. // |kDownloadErrorCodeInputMalformed| is returned.
  33. metrics::DownloadErrorCode GetDownloadErrorCode(ErrorCode code);
  34. // Transforms a ErrorCode value into a metrics::AttemptResult.
  35. //
  36. // If metrics::AttemptResult::kPayloadDownloadError is returned, you
  37. // can use utils::GetDownloadError() to get more detail.
  38. metrics::AttemptResult GetAttemptResult(ErrorCode code);
  39. // Calculates the internet connection type given |type| and |tethering|.
  40. metrics::ConnectionType GetConnectionType(ConnectionType type,
  41. ConnectionTethering tethering);
  42. // This function returns the duration on the wallclock since the last
  43. // time it was called for the same |state_variable_key| value.
  44. //
  45. // If the function returns |true|, the duration (always non-negative)
  46. // is returned in |out_duration|. If the function returns |false|
  47. // something went wrong or there was no previous measurement.
  48. bool WallclockDurationHelper(SystemState* system_state,
  49. const std::string& state_variable_key,
  50. base::TimeDelta* out_duration);
  51. // This function returns the duration on the monotonic clock since the
  52. // last time it was called for the same |storage| pointer.
  53. //
  54. // You should pass a pointer to a 64-bit integer in |storage| which
  55. // should be initialized to 0.
  56. //
  57. // If the function returns |true|, the duration (always non-negative)
  58. // is returned in |out_duration|. If the function returns |false|
  59. // something went wrong or there was no previous measurement.
  60. bool MonotonicDurationHelper(SystemState* system_state,
  61. int64_t* storage,
  62. base::TimeDelta* out_duration);
  63. // Returns the persisted value from prefs for the given key. It also
  64. // validates that the value returned is non-negative.
  65. int64_t GetPersistedValue(const std::string& key, PrefsInterface* prefs);
  66. // Persists the reboot count of the update attempt to |kPrefsNumReboots|.
  67. void SetNumReboots(int64_t num_reboots, PrefsInterface* prefs);
  68. // Persists the payload attempt number to |kPrefsPayloadAttemptNumber|.
  69. void SetPayloadAttemptNumber(int64_t payload_attempt_number,
  70. PrefsInterface* prefs);
  71. // Persists the finished time of an update to the |kPrefsSystemUpdatedMarker|.
  72. void SetSystemUpdatedMarker(ClockInterface* clock, PrefsInterface* prefs);
  73. // Persists the start monotonic time of an update to
  74. // |kPrefsUpdateTimestampStart|.
  75. void SetUpdateTimestampStart(const base::Time& update_start_time,
  76. PrefsInterface* prefs);
  77. // Persists the start boot time of an update to
  78. // |kPrefsUpdateBootTimestampStart|.
  79. void SetUpdateBootTimestampStart(const base::Time& update_start_boot_time,
  80. PrefsInterface* prefs);
  81. // Called at program startup if the device booted into a new update.
  82. // The |time_to_reboot| parameter contains the (monotonic-clock) duration
  83. // from when the update successfully completed (the value in
  84. // |kPrefsSystemUpdatedMarker|) until the device was booted into the update
  85. // (current monotonic-clock time).
  86. bool LoadAndReportTimeToReboot(MetricsReporterInterface* metrics_reporter,
  87. PrefsInterface* prefs,
  88. ClockInterface* clock);
  89. } // namespace metrics_utils
  90. } // namespace chromeos_update_engine
  91. #endif // UPDATE_ENGINE_METRICS_UTILS_H_