metrics_reporter_android.cc 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. //
  2. // Copyright (C) 2017 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. #include "update_engine/metrics_reporter_android.h"
  17. #include <memory>
  18. #include <string>
  19. #include <metricslogger/metrics_logger.h>
  20. #include "update_engine/common/constants.h"
  21. namespace {
  22. void LogHistogram(const std::string& metrics, int value) {
  23. android::metricslogger::LogHistogram(metrics, value);
  24. LOG(INFO) << "uploading " << value << " to histogram for metric " << metrics;
  25. }
  26. } // namespace
  27. namespace chromeos_update_engine {
  28. namespace metrics {
  29. // The histograms are defined in:
  30. // depot/google3/analysis/uma/configs/clearcut/TRON/histograms.xml
  31. constexpr char kMetricsUpdateEngineAttemptNumber[] =
  32. "ota_update_engine_attempt_number";
  33. constexpr char kMetricsUpdateEngineAttemptResult[] =
  34. "ota_update_engine_attempt_result";
  35. constexpr char kMetricsUpdateEngineAttemptDurationInMinutes[] =
  36. "ota_update_engine_attempt_fixed_duration_boottime_in_minutes";
  37. constexpr char kMetricsUpdateEngineAttemptDurationUptimeInMinutes[] =
  38. "ota_update_engine_attempt_duration_monotonic_in_minutes";
  39. constexpr char kMetricsUpdateEngineAttemptErrorCode[] =
  40. "ota_update_engine_attempt_error_code";
  41. constexpr char kMetricsUpdateEngineAttemptPayloadSizeMiB[] =
  42. "ota_update_engine_attempt_payload_size_mib";
  43. constexpr char kMetricsUpdateEngineAttemptPayloadType[] =
  44. "ota_update_engine_attempt_payload_type";
  45. constexpr char kMetricsUpdateEngineAttemptCurrentBytesDownloadedMiB[] =
  46. "ota_update_engine_attempt_fixed_current_bytes_downloaded_mib";
  47. constexpr char kMetricsUpdateEngineSuccessfulUpdateAttemptCount[] =
  48. "ota_update_engine_successful_update_attempt_count";
  49. constexpr char kMetricsUpdateEngineSuccessfulUpdateTotalDurationInMinutes[] =
  50. "ota_update_engine_successful_update_fixed_total_duration_in_minutes";
  51. constexpr char kMetricsUpdateEngineSuccessfulUpdatePayloadSizeMiB[] =
  52. "ota_update_engine_successful_update_payload_size_mib";
  53. constexpr char kMetricsUpdateEngineSuccessfulUpdatePayloadType[] =
  54. "ota_update_engine_successful_update_payload_type";
  55. constexpr char kMetricsUpdateEngineSuccessfulUpdateRebootCount[] =
  56. "ota_update_engine_successful_update_reboot_count";
  57. constexpr char kMetricsUpdateEngineSuccessfulUpdateTotalBytesDownloadedMiB[] =
  58. "ota_update_engine_successful_update_total_bytes_downloaded_mib";
  59. constexpr char
  60. kMetricsUpdateEngineSuccessfulUpdateDownloadOverheadPercentage[] =
  61. "ota_update_engine_successful_update_download_overhead_percentage";
  62. std::unique_ptr<MetricsReporterInterface> CreateMetricsReporter() {
  63. return std::make_unique<MetricsReporterAndroid>();
  64. }
  65. } // namespace metrics
  66. void MetricsReporterAndroid::ReportUpdateAttemptMetrics(
  67. SystemState* /* system_state */,
  68. int attempt_number,
  69. PayloadType payload_type,
  70. base::TimeDelta duration,
  71. base::TimeDelta duration_uptime,
  72. int64_t payload_size,
  73. metrics::AttemptResult attempt_result,
  74. ErrorCode error_code) {
  75. LogHistogram(metrics::kMetricsUpdateEngineAttemptNumber, attempt_number);
  76. LogHistogram(metrics::kMetricsUpdateEngineAttemptPayloadType,
  77. static_cast<int>(payload_type));
  78. LogHistogram(metrics::kMetricsUpdateEngineAttemptDurationInMinutes,
  79. duration.InMinutes());
  80. LogHistogram(metrics::kMetricsUpdateEngineAttemptDurationUptimeInMinutes,
  81. duration_uptime.InMinutes());
  82. int64_t payload_size_mib = payload_size / kNumBytesInOneMiB;
  83. LogHistogram(metrics::kMetricsUpdateEngineAttemptPayloadSizeMiB,
  84. payload_size_mib);
  85. LogHistogram(metrics::kMetricsUpdateEngineAttemptResult,
  86. static_cast<int>(attempt_result));
  87. LogHistogram(metrics::kMetricsUpdateEngineAttemptErrorCode,
  88. static_cast<int>(error_code));
  89. }
  90. void MetricsReporterAndroid::ReportUpdateAttemptDownloadMetrics(
  91. int64_t payload_bytes_downloaded,
  92. int64_t /* payload_download_speed_bps */,
  93. DownloadSource /* download_source */,
  94. metrics::DownloadErrorCode /* payload_download_error_code */,
  95. metrics::ConnectionType /* connection_type */) {
  96. LogHistogram(metrics::kMetricsUpdateEngineAttemptCurrentBytesDownloadedMiB,
  97. payload_bytes_downloaded / kNumBytesInOneMiB);
  98. }
  99. void MetricsReporterAndroid::ReportSuccessfulUpdateMetrics(
  100. int attempt_count,
  101. int /* updates_abandoned_count */,
  102. PayloadType payload_type,
  103. int64_t payload_size,
  104. int64_t num_bytes_downloaded[kNumDownloadSources],
  105. int download_overhead_percentage,
  106. base::TimeDelta total_duration,
  107. base::TimeDelta /* total_duration_uptime */,
  108. int reboot_count,
  109. int /* url_switch_count */) {
  110. LogHistogram(metrics::kMetricsUpdateEngineSuccessfulUpdateAttemptCount,
  111. attempt_count);
  112. LogHistogram(metrics::kMetricsUpdateEngineSuccessfulUpdatePayloadType,
  113. static_cast<int>(payload_type));
  114. int64_t payload_size_mib = payload_size / kNumBytesInOneMiB;
  115. LogHistogram(metrics::kMetricsUpdateEngineSuccessfulUpdatePayloadSizeMiB,
  116. payload_size_mib);
  117. int64_t total_bytes_downloaded = 0;
  118. for (size_t i = 0; i < kNumDownloadSources; i++) {
  119. total_bytes_downloaded += num_bytes_downloaded[i] / kNumBytesInOneMiB;
  120. }
  121. LogHistogram(
  122. metrics::kMetricsUpdateEngineSuccessfulUpdateTotalBytesDownloadedMiB,
  123. total_bytes_downloaded);
  124. LogHistogram(
  125. metrics::kMetricsUpdateEngineSuccessfulUpdateDownloadOverheadPercentage,
  126. download_overhead_percentage);
  127. LogHistogram(
  128. metrics::kMetricsUpdateEngineSuccessfulUpdateTotalDurationInMinutes,
  129. total_duration.InMinutes());
  130. LogHistogram(metrics::kMetricsUpdateEngineSuccessfulUpdateRebootCount,
  131. reboot_count);
  132. }
  133. void MetricsReporterAndroid::ReportAbnormallyTerminatedUpdateAttemptMetrics() {
  134. int attempt_result =
  135. static_cast<int>(metrics::AttemptResult::kAbnormalTermination);
  136. LogHistogram(metrics::kMetricsUpdateEngineAttemptResult, attempt_result);
  137. }
  138. }; // namespace chromeos_update_engine