Bertrand SIMONNET | 52e5b99 | 2015-08-10 15:18:00 -0700 | [diff] [blame] | 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 | */ |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 16 | |
Bertrand SIMONNET | 608e428 | 2015-11-12 17:52:17 -0800 | [diff] [blame] | 17 | #include "metrics_collector.h" |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 18 | |
Steve Fung | e86591e | 2014-12-01 13:38:21 -0800 | [diff] [blame] | 19 | #include <sysexits.h> |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 20 | #include <time.h> |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 21 | |
Bertrand SIMONNET | 4b915ae | 2015-07-28 15:38:14 -0700 | [diff] [blame] | 22 | #include <base/bind.h> |
Luigi Semenzato | 859b3f0 | 2014-02-05 15:33:19 -0800 | [diff] [blame] | 23 | #include <base/files/file_path.h> |
Ben Chan | 51bf92a | 2014-09-05 08:21:06 -0700 | [diff] [blame] | 24 | #include <base/files/file_util.h> |
Luigi Semenzato | 859b3f0 | 2014-02-05 15:33:19 -0800 | [diff] [blame] | 25 | #include <base/hash.h> |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 26 | #include <base/logging.h> |
Ben Chan | 2e6543d | 2014-02-05 23:26:25 -0800 | [diff] [blame] | 27 | #include <base/strings/string_number_conversions.h> |
| 28 | #include <base/strings/string_split.h> |
| 29 | #include <base/strings/string_util.h> |
| 30 | #include <base/strings/stringprintf.h> |
Bertrand SIMONNET | eb697ab | 2015-10-14 13:26:42 -0700 | [diff] [blame] | 31 | #include <brillo/osrelease_reader.h> |
Steve Fung | e86591e | 2014-12-01 13:38:21 -0800 | [diff] [blame] | 32 | #include <dbus/dbus.h> |
| 33 | #include <dbus/message.h> |
Bertrand SIMONNET | bae5dcc | 2015-08-04 14:12:10 -0700 | [diff] [blame] | 34 | |
| 35 | #include "constants.h" |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 36 | |
Ben Chan | 2e6543d | 2014-02-05 23:26:25 -0800 | [diff] [blame] | 37 | using base::FilePath; |
| 38 | using base::StringPrintf; |
Darin Petkov | f27f036 | 2010-06-04 13:14:19 -0700 | [diff] [blame] | 39 | using base::Time; |
| 40 | using base::TimeDelta; |
| 41 | using base::TimeTicks; |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 42 | using chromeos_metrics::PersistentInteger; |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 43 | using std::map; |
Darin Petkov | 38d5cb0 | 2010-06-24 12:10:26 -0700 | [diff] [blame] | 44 | using std::string; |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 45 | using std::vector; |
| 46 | |
Daniel Erat | c83975a | 2014-04-04 08:53:44 -0700 | [diff] [blame] | 47 | namespace { |
Darin Petkov | f27f036 | 2010-06-04 13:14:19 -0700 | [diff] [blame] | 48 | |
Daniel Erat | c83975a | 2014-04-04 08:53:44 -0700 | [diff] [blame] | 49 | const char kCrashReporterInterface[] = "org.chromium.CrashReporter"; |
| 50 | const char kCrashReporterUserCrashSignal[] = "UserCrash"; |
Steve Fung | e86591e | 2014-12-01 13:38:21 -0800 | [diff] [blame] | 51 | const char kCrashReporterMatchRule[] = |
| 52 | "type='signal',interface='%s',path='/',member='%s'"; |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 53 | |
Daniel Erat | c83975a | 2014-04-04 08:53:44 -0700 | [diff] [blame] | 54 | const int kSecondsPerMinute = 60; |
| 55 | const int kMinutesPerHour = 60; |
| 56 | const int kHoursPerDay = 24; |
| 57 | const int kMinutesPerDay = kHoursPerDay * kMinutesPerHour; |
| 58 | const int kSecondsPerDay = kSecondsPerMinute * kMinutesPerDay; |
| 59 | const int kDaysPerWeek = 7; |
| 60 | const int kSecondsPerWeek = kSecondsPerDay * kDaysPerWeek; |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 61 | |
Daniel Erat | c83975a | 2014-04-04 08:53:44 -0700 | [diff] [blame] | 62 | // Interval between calls to UpdateStats(). |
Steve Fung | e86591e | 2014-12-01 13:38:21 -0800 | [diff] [blame] | 63 | const uint32_t kUpdateStatsIntervalMs = 300000; |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 64 | |
Luigi Semenzato | c5a9234 | 2014-02-14 15:05:51 -0800 | [diff] [blame] | 65 | const char kKernelCrashDetectedFile[] = "/var/run/kernel-crash-detected"; |
Daniel Erat | c83975a | 2014-04-04 08:53:44 -0700 | [diff] [blame] | 66 | const char kUncleanShutdownDetectedFile[] = |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 67 | "/var/run/unclean-shutdown-detected"; |
Ken Mixter | ccd84c0 | 2010-08-16 19:57:13 -0700 | [diff] [blame] | 68 | |
Bertrand SIMONNET | ebbe35c | 2015-09-08 10:13:35 -0700 | [diff] [blame] | 69 | const int kMetricMeminfoInterval = 30; // seconds |
| 70 | |
Bertrand SIMONNET | 675a10c | 2015-08-25 14:11:43 -0700 | [diff] [blame] | 71 | const char kMeminfoFileName[] = "/proc/meminfo"; |
Bertrand SIMONNET | 7a96405 | 2015-09-29 11:07:24 -0700 | [diff] [blame] | 72 | const char kVmStatFileName[] = "/proc/vmstat"; |
Luigi Semenzato | ba0c65d | 2014-03-17 12:28:38 -0700 | [diff] [blame] | 73 | |
Bertrand SIMONNET | 675a10c | 2015-08-25 14:11:43 -0700 | [diff] [blame] | 74 | } // namespace |
| 75 | |
Luigi Semenzato | 9636019 | 2014-06-04 10:53:35 -0700 | [diff] [blame] | 76 | // Zram sysfs entries. |
| 77 | |
Bertrand SIMONNET | 608e428 | 2015-11-12 17:52:17 -0800 | [diff] [blame] | 78 | const char MetricsCollector::kComprDataSizeName[] = "compr_data_size"; |
| 79 | const char MetricsCollector::kOrigDataSizeName[] = "orig_data_size"; |
| 80 | const char MetricsCollector::kZeroPagesName[] = "zero_pages"; |
Luigi Semenzato | 9636019 | 2014-06-04 10:53:35 -0700 | [diff] [blame] | 81 | |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 82 | // Memory use stats collection intervals. We collect some memory use interval |
| 83 | // at these intervals after boot, and we stop collecting after the last one, |
| 84 | // with the assumption that in most cases the memory use won't change much |
| 85 | // after that. |
| 86 | static const int kMemuseIntervals[] = { |
| 87 | 1 * kSecondsPerMinute, // 1 minute mark |
| 88 | 4 * kSecondsPerMinute, // 5 minute mark |
| 89 | 25 * kSecondsPerMinute, // 0.5 hour mark |
| 90 | 120 * kSecondsPerMinute, // 2.5 hour mark |
| 91 | 600 * kSecondsPerMinute, // 12.5 hour mark |
| 92 | }; |
| 93 | |
Bertrand SIMONNET | 608e428 | 2015-11-12 17:52:17 -0800 | [diff] [blame] | 94 | MetricsCollector::MetricsCollector() |
Steve Fung | e86591e | 2014-12-01 13:38:21 -0800 | [diff] [blame] | 95 | : memuse_final_time_(0), |
Bertrand SIMONNET | 0ada2ca | 2015-11-02 14:08:44 -0800 | [diff] [blame] | 96 | memuse_interval_index_(0) {} |
Darin Petkov | f1e85e4 | 2010-06-10 15:59:53 -0700 | [diff] [blame] | 97 | |
Bertrand SIMONNET | 608e428 | 2015-11-12 17:52:17 -0800 | [diff] [blame] | 98 | MetricsCollector::~MetricsCollector() { |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Bertrand SIMONNET | 7a96405 | 2015-09-29 11:07:24 -0700 | [diff] [blame] | 101 | // static |
Bertrand SIMONNET | 608e428 | 2015-11-12 17:52:17 -0800 | [diff] [blame] | 102 | double MetricsCollector::GetActiveTime() { |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 103 | struct timespec ts; |
| 104 | int r = clock_gettime(CLOCK_MONOTONIC, &ts); |
| 105 | if (r < 0) { |
| 106 | PLOG(WARNING) << "clock_gettime(CLOCK_MONOTONIC) failed"; |
| 107 | return 0; |
| 108 | } else { |
Luigi Semenzato | 4a6c942 | 2014-06-30 18:12:28 -0700 | [diff] [blame] | 109 | return ts.tv_sec + static_cast<double>(ts.tv_nsec) / (1000 * 1000 * 1000); |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | |
Bertrand SIMONNET | 608e428 | 2015-11-12 17:52:17 -0800 | [diff] [blame] | 113 | int MetricsCollector::Run() { |
Ken Mixter | ccd84c0 | 2010-08-16 19:57:13 -0700 | [diff] [blame] | 114 | if (CheckSystemCrash(kKernelCrashDetectedFile)) { |
| 115 | ProcessKernelCrash(); |
| 116 | } |
| 117 | |
| 118 | if (CheckSystemCrash(kUncleanShutdownDetectedFile)) { |
| 119 | ProcessUncleanShutdown(); |
| 120 | } |
| 121 | |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 122 | // On OS version change, clear version stats (which are reported daily). |
Ben Chan | f05ab40 | 2014-08-07 00:54:59 -0700 | [diff] [blame] | 123 | int32_t version = GetOsVersionHash(); |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 124 | if (version_cycle_->Get() != version) { |
| 125 | version_cycle_->Set(version); |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 126 | kernel_crashes_version_count_->Set(0); |
Luigi Semenzato | e5883fa | 2014-04-18 17:00:35 -0700 | [diff] [blame] | 127 | version_cumulative_active_use_->Set(0); |
Luigi Semenzato | ba0c65d | 2014-03-17 12:28:38 -0700 | [diff] [blame] | 128 | version_cumulative_cpu_use_->Set(0); |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 129 | } |
| 130 | |
Alex Vakulenko | 74dc624 | 2015-10-13 09:23:34 -0700 | [diff] [blame] | 131 | return brillo::DBusDaemon::Run(); |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Bertrand SIMONNET | 608e428 | 2015-11-12 17:52:17 -0800 | [diff] [blame] | 134 | uint32_t MetricsCollector::GetOsVersionHash() { |
Bertrand SIMONNET | eb697ab | 2015-10-14 13:26:42 -0700 | [diff] [blame] | 135 | brillo::OsReleaseReader reader; |
| 136 | reader.Load(); |
| 137 | string version; |
| 138 | if (!reader.GetString(metrics::kProductVersion, &version)) { |
| 139 | LOG(ERROR) << "failed to read the product version."; |
| 140 | version = metrics::kDefaultVersion; |
Luigi Semenzato | 859b3f0 | 2014-02-05 15:33:19 -0800 | [diff] [blame] | 141 | } |
Bertrand SIMONNET | eb697ab | 2015-10-14 13:26:42 -0700 | [diff] [blame] | 142 | |
| 143 | uint32_t version_hash = base::Hash(version); |
| 144 | if (testing_) { |
| 145 | version_hash = 42; // return any plausible value for the hash |
| 146 | } |
| 147 | return version_hash; |
Luigi Semenzato | 859b3f0 | 2014-02-05 15:33:19 -0800 | [diff] [blame] | 148 | } |
| 149 | |
Bertrand SIMONNET | a7bc1c1 | 2015-11-25 13:29:48 -0800 | [diff] [blame] | 150 | void MetricsCollector::Init(bool testing, MetricsLibraryInterface* metrics_lib, |
| 151 | const string& diskstats_path, |
Bertrand SIMONNET | 9d3a4ae | 2015-11-25 13:49:12 -0800 | [diff] [blame] | 152 | const base::FilePath& private_metrics_directory, |
| 153 | const base::FilePath& shared_metrics_directory) { |
Bertrand SIMONNET | 675a10c | 2015-08-25 14:11:43 -0700 | [diff] [blame] | 154 | CHECK(metrics_lib); |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 155 | testing_ = testing; |
Bertrand SIMONNET | 9d3a4ae | 2015-11-25 13:49:12 -0800 | [diff] [blame] | 156 | shared_metrics_directory_ = shared_metrics_directory; |
Darin Petkov | fc91b42 | 2010-05-12 13:05:45 -0700 | [diff] [blame] | 157 | metrics_lib_ = metrics_lib; |
Darin Petkov | 38d5cb0 | 2010-06-24 12:10:26 -0700 | [diff] [blame] | 158 | |
Bertrand SIMONNET | 9d3a4ae | 2015-11-25 13:49:12 -0800 | [diff] [blame] | 159 | daily_active_use_.reset(new PersistentInteger("Platform.UseTime.PerDay", |
| 160 | private_metrics_directory)); |
| 161 | version_cumulative_active_use_.reset(new PersistentInteger( |
| 162 | "Platform.CumulativeUseTime", private_metrics_directory)); |
| 163 | version_cumulative_cpu_use_.reset(new PersistentInteger( |
| 164 | "Platform.CumulativeCpuTime", private_metrics_directory)); |
Darin Petkov | 38d5cb0 | 2010-06-24 12:10:26 -0700 | [diff] [blame] | 165 | |
Bertrand SIMONNET | a7bc1c1 | 2015-11-25 13:29:48 -0800 | [diff] [blame] | 166 | kernel_crash_interval_.reset(new PersistentInteger( |
Bertrand SIMONNET | 9d3a4ae | 2015-11-25 13:49:12 -0800 | [diff] [blame] | 167 | "Platform.KernelCrashInterval", private_metrics_directory)); |
Bertrand SIMONNET | a7bc1c1 | 2015-11-25 13:29:48 -0800 | [diff] [blame] | 168 | unclean_shutdown_interval_.reset(new PersistentInteger( |
Bertrand SIMONNET | 9d3a4ae | 2015-11-25 13:49:12 -0800 | [diff] [blame] | 169 | "Platform.UncleanShutdownInterval", private_metrics_directory)); |
| 170 | user_crash_interval_.reset(new PersistentInteger("Platform.UserCrashInterval", |
| 171 | private_metrics_directory)); |
Darin Petkov | 2ccef01 | 2010-05-05 16:06:37 -0700 | [diff] [blame] | 172 | |
Bertrand SIMONNET | 9d3a4ae | 2015-11-25 13:49:12 -0800 | [diff] [blame] | 173 | any_crashes_daily_count_.reset(new PersistentInteger( |
| 174 | "Platform.AnyCrashes.PerDay", private_metrics_directory)); |
| 175 | any_crashes_weekly_count_.reset(new PersistentInteger( |
| 176 | "Platform.AnyCrashes.PerWeek", private_metrics_directory)); |
| 177 | user_crashes_daily_count_.reset(new PersistentInteger( |
| 178 | "Platform.UserCrashes.PerDay", private_metrics_directory)); |
Bertrand SIMONNET | a7bc1c1 | 2015-11-25 13:29:48 -0800 | [diff] [blame] | 179 | user_crashes_weekly_count_.reset(new PersistentInteger( |
Bertrand SIMONNET | 9d3a4ae | 2015-11-25 13:49:12 -0800 | [diff] [blame] | 180 | "Platform.UserCrashes.PerWeek", private_metrics_directory)); |
Bertrand SIMONNET | a7bc1c1 | 2015-11-25 13:29:48 -0800 | [diff] [blame] | 181 | kernel_crashes_daily_count_.reset(new PersistentInteger( |
Bertrand SIMONNET | 9d3a4ae | 2015-11-25 13:49:12 -0800 | [diff] [blame] | 182 | "Platform.KernelCrashes.PerDay", private_metrics_directory)); |
Bertrand SIMONNET | a7bc1c1 | 2015-11-25 13:29:48 -0800 | [diff] [blame] | 183 | kernel_crashes_weekly_count_.reset(new PersistentInteger( |
Bertrand SIMONNET | 9d3a4ae | 2015-11-25 13:49:12 -0800 | [diff] [blame] | 184 | "Platform.KernelCrashes.PerWeek", private_metrics_directory)); |
Bertrand SIMONNET | a7bc1c1 | 2015-11-25 13:29:48 -0800 | [diff] [blame] | 185 | kernel_crashes_version_count_.reset(new PersistentInteger( |
Bertrand SIMONNET | 9d3a4ae | 2015-11-25 13:49:12 -0800 | [diff] [blame] | 186 | "Platform.KernelCrashesSinceUpdate", private_metrics_directory)); |
Bertrand SIMONNET | a7bc1c1 | 2015-11-25 13:29:48 -0800 | [diff] [blame] | 187 | unclean_shutdowns_daily_count_.reset(new PersistentInteger( |
Bertrand SIMONNET | 9d3a4ae | 2015-11-25 13:49:12 -0800 | [diff] [blame] | 188 | "Platform.UncleanShutdown.PerDay", private_metrics_directory)); |
Bertrand SIMONNET | a7bc1c1 | 2015-11-25 13:29:48 -0800 | [diff] [blame] | 189 | unclean_shutdowns_weekly_count_.reset(new PersistentInteger( |
Bertrand SIMONNET | 9d3a4ae | 2015-11-25 13:49:12 -0800 | [diff] [blame] | 190 | "Platform.UncleanShutdowns.PerWeek", private_metrics_directory)); |
Darin Petkov | 38d5cb0 | 2010-06-24 12:10:26 -0700 | [diff] [blame] | 191 | |
Bertrand SIMONNET | 9d3a4ae | 2015-11-25 13:49:12 -0800 | [diff] [blame] | 192 | daily_cycle_.reset( |
| 193 | new PersistentInteger("daily.cycle", private_metrics_directory)); |
Bertrand SIMONNET | a7bc1c1 | 2015-11-25 13:29:48 -0800 | [diff] [blame] | 194 | weekly_cycle_.reset( |
Bertrand SIMONNET | 9d3a4ae | 2015-11-25 13:49:12 -0800 | [diff] [blame] | 195 | new PersistentInteger("weekly.cycle", private_metrics_directory)); |
Bertrand SIMONNET | a7bc1c1 | 2015-11-25 13:29:48 -0800 | [diff] [blame] | 196 | version_cycle_.reset( |
Bertrand SIMONNET | 9d3a4ae | 2015-11-25 13:49:12 -0800 | [diff] [blame] | 197 | new PersistentInteger("version.cycle", private_metrics_directory)); |
Luigi Semenzato | 859b3f0 | 2014-02-05 15:33:19 -0800 | [diff] [blame] | 198 | |
Bertrand SIMONNET | 5658dc5 | 2015-09-18 13:38:10 -0700 | [diff] [blame] | 199 | disk_usage_collector_.reset(new DiskUsageCollector(metrics_lib_)); |
Bertrand SIMONNET | 7a96405 | 2015-09-29 11:07:24 -0700 | [diff] [blame] | 200 | averaged_stats_collector_.reset( |
| 201 | new AveragedStatisticsCollector(metrics_lib_, diskstats_path, |
| 202 | kVmStatFileName)); |
Bertrand SIMONNET | 0ada2ca | 2015-11-02 14:08:44 -0800 | [diff] [blame] | 203 | cpu_usage_collector_.reset(new CpuUsageCollector(metrics_lib_)); |
Steve Fung | e86591e | 2014-12-01 13:38:21 -0800 | [diff] [blame] | 204 | } |
| 205 | |
Bertrand SIMONNET | 608e428 | 2015-11-12 17:52:17 -0800 | [diff] [blame] | 206 | int MetricsCollector::OnInit() { |
| 207 | int return_code = brillo::DBusDaemon::OnInit(); |
Steve Fung | e86591e | 2014-12-01 13:38:21 -0800 | [diff] [blame] | 208 | if (return_code != EX_OK) |
| 209 | return return_code; |
| 210 | |
Bertrand SIMONNET | ebbe35c | 2015-09-08 10:13:35 -0700 | [diff] [blame] | 211 | StatsReporterInit(); |
| 212 | |
| 213 | // Start collecting meminfo stats. |
| 214 | ScheduleMeminfoCallback(kMetricMeminfoInterval); |
| 215 | memuse_final_time_ = GetActiveTime() + kMemuseIntervals[0]; |
| 216 | ScheduleMemuseCallback(kMemuseIntervals[0]); |
| 217 | |
Steve Fung | e86591e | 2014-12-01 13:38:21 -0800 | [diff] [blame] | 218 | if (testing_) |
| 219 | return EX_OK; |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 220 | |
Bertrand SIMONNET | 608e428 | 2015-11-12 17:52:17 -0800 | [diff] [blame] | 221 | bus_->AssertOnDBusThread(); |
| 222 | CHECK(bus_->SetUpAsyncOperations()); |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 223 | |
Bertrand SIMONNET | 608e428 | 2015-11-12 17:52:17 -0800 | [diff] [blame] | 224 | if (bus_->is_connected()) { |
Steve Fung | e86591e | 2014-12-01 13:38:21 -0800 | [diff] [blame] | 225 | const std::string match_rule = |
| 226 | base::StringPrintf(kCrashReporterMatchRule, |
| 227 | kCrashReporterInterface, |
| 228 | kCrashReporterUserCrashSignal); |
| 229 | |
Bertrand SIMONNET | 608e428 | 2015-11-12 17:52:17 -0800 | [diff] [blame] | 230 | bus_->AddFilterFunction(&MetricsCollector::MessageFilter, this); |
| 231 | |
| 232 | DBusError error; |
| 233 | dbus_error_init(&error); |
| 234 | bus_->AddMatch(match_rule, &error); |
| 235 | |
| 236 | if (dbus_error_is_set(&error)) { |
| 237 | LOG(ERROR) << "Failed to add match rule \"" << match_rule << "\". Got " |
| 238 | << error.name << ": " << error.message; |
| 239 | return EX_SOFTWARE; |
| 240 | } |
| 241 | } else { |
| 242 | LOG(ERROR) << "DBus isn't connected."; |
| 243 | return EX_UNAVAILABLE; |
| 244 | } |
| 245 | |
| 246 | device_ = weaved::Device::CreateInstance( |
| 247 | bus_, |
| 248 | base::Bind(&MetricsCollector::UpdateWeaveState, base::Unretained(this))); |
| 249 | device_->AddCommandHandler( |
| 250 | "_metrics._enableAnalyticsReporting", |
| 251 | base::Bind(&MetricsCollector::OnEnableMetrics, base::Unretained(this))); |
| 252 | device_->AddCommandHandler( |
| 253 | "_metrics._disableAnalyticsReporting", |
| 254 | base::Bind(&MetricsCollector::OnDisableMetrics, base::Unretained(this))); |
| 255 | |
| 256 | latest_cpu_use_microseconds_ = cpu_usage_collector_->GetCumulativeCpuUse(); |
| 257 | base::MessageLoop::current()->PostDelayedTask(FROM_HERE, |
| 258 | base::Bind(&MetricsCollector::HandleUpdateStatsTimeout, |
| 259 | base::Unretained(this)), |
| 260 | base::TimeDelta::FromMilliseconds(kUpdateStatsIntervalMs)); |
| 261 | |
| 262 | return EX_OK; |
| 263 | } |
| 264 | |
| 265 | void MetricsCollector::OnShutdown(int* return_code) { |
| 266 | if (!testing_ && bus_->is_connected()) { |
| 267 | const std::string match_rule = |
| 268 | base::StringPrintf(kCrashReporterMatchRule, |
| 269 | kCrashReporterInterface, |
| 270 | kCrashReporterUserCrashSignal); |
| 271 | |
| 272 | bus_->RemoveFilterFunction(&MetricsCollector::MessageFilter, this); |
Steve Fung | e86591e | 2014-12-01 13:38:21 -0800 | [diff] [blame] | 273 | |
| 274 | DBusError error; |
| 275 | dbus_error_init(&error); |
| 276 | bus_->RemoveMatch(match_rule, &error); |
| 277 | |
| 278 | if (dbus_error_is_set(&error)) { |
| 279 | LOG(ERROR) << "Failed to remove match rule \"" << match_rule << "\". Got " |
| 280 | << error.name << ": " << error.message; |
| 281 | } |
| 282 | } |
Alex Vakulenko | 74dc624 | 2015-10-13 09:23:34 -0700 | [diff] [blame] | 283 | brillo::DBusDaemon::OnShutdown(return_code); |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 284 | } |
| 285 | |
Bertrand SIMONNET | 608e428 | 2015-11-12 17:52:17 -0800 | [diff] [blame] | 286 | void MetricsCollector::OnEnableMetrics( |
| 287 | const std::weak_ptr<weaved::Command>& cmd) { |
Alex Vakulenko | 82b02de | 2015-10-09 20:07:47 -0700 | [diff] [blame] | 288 | auto command = cmd.lock(); |
| 289 | if (!command) |
Bertrand SIMONNET | 59890e2 | 2015-10-02 16:45:18 -0700 | [diff] [blame] | 290 | return; |
Bertrand SIMONNET | 59890e2 | 2015-10-02 16:45:18 -0700 | [diff] [blame] | 291 | |
Bertrand SIMONNET | 9d3a4ae | 2015-11-25 13:49:12 -0800 | [diff] [blame] | 292 | if (base::WriteFile( |
| 293 | shared_metrics_directory_.Append(metrics::kConsentFileName), "", 0) != |
| 294 | 0) { |
Bertrand SIMONNET | 59890e2 | 2015-10-02 16:45:18 -0700 | [diff] [blame] | 295 | PLOG(ERROR) << "Could not create the consent file."; |
Alex Vakulenko | 35f8963 | 2015-10-09 08:18:35 -0700 | [diff] [blame] | 296 | command->Abort("metrics_error", "Could not create the consent file", |
| 297 | nullptr); |
Bertrand SIMONNET | 59890e2 | 2015-10-02 16:45:18 -0700 | [diff] [blame] | 298 | return; |
| 299 | } |
| 300 | |
Alex Vakulenko | 82b02de | 2015-10-09 20:07:47 -0700 | [diff] [blame] | 301 | UpdateWeaveState(); |
Alex Vakulenko | 35f8963 | 2015-10-09 08:18:35 -0700 | [diff] [blame] | 302 | command->Complete({}, nullptr); |
Bertrand SIMONNET | 59890e2 | 2015-10-02 16:45:18 -0700 | [diff] [blame] | 303 | } |
| 304 | |
Bertrand SIMONNET | 608e428 | 2015-11-12 17:52:17 -0800 | [diff] [blame] | 305 | void MetricsCollector::OnDisableMetrics( |
Alex Vakulenko | 82b02de | 2015-10-09 20:07:47 -0700 | [diff] [blame] | 306 | const std::weak_ptr<weaved::Command>& cmd) { |
| 307 | auto command = cmd.lock(); |
| 308 | if (!command) |
| 309 | return; |
| 310 | |
Bertrand SIMONNET | 9d3a4ae | 2015-11-25 13:49:12 -0800 | [diff] [blame] | 311 | if (!base::DeleteFile( |
| 312 | shared_metrics_directory_.Append(metrics::kConsentFileName), false)) { |
Alex Vakulenko | 35f8963 | 2015-10-09 08:18:35 -0700 | [diff] [blame] | 313 | PLOG(ERROR) << "Could not delete the consent file."; |
| 314 | command->Abort("metrics_error", "Could not delete the consent file", |
| 315 | nullptr); |
Bertrand SIMONNET | 59890e2 | 2015-10-02 16:45:18 -0700 | [diff] [blame] | 316 | return; |
| 317 | } |
| 318 | |
Alex Vakulenko | 82b02de | 2015-10-09 20:07:47 -0700 | [diff] [blame] | 319 | UpdateWeaveState(); |
Alex Vakulenko | 35f8963 | 2015-10-09 08:18:35 -0700 | [diff] [blame] | 320 | command->Complete({}, nullptr); |
Bertrand SIMONNET | 59890e2 | 2015-10-02 16:45:18 -0700 | [diff] [blame] | 321 | } |
| 322 | |
Bertrand SIMONNET | 608e428 | 2015-11-12 17:52:17 -0800 | [diff] [blame] | 323 | void MetricsCollector::UpdateWeaveState() { |
Alex Vakulenko | 82b02de | 2015-10-09 20:07:47 -0700 | [diff] [blame] | 324 | if (!device_) |
| 325 | return; |
Bertrand SIMONNET | 59890e2 | 2015-10-02 16:45:18 -0700 | [diff] [blame] | 326 | |
Alex Vakulenko | 74dc624 | 2015-10-13 09:23:34 -0700 | [diff] [blame] | 327 | brillo::VariantDictionary state_change{ |
Bertrand SIMONNET | 59890e2 | 2015-10-02 16:45:18 -0700 | [diff] [blame] | 328 | { "_metrics._AnalyticsReportingState", |
| 329 | metrics_lib_->AreMetricsEnabled() ? "enabled" : "disabled" } |
| 330 | }; |
| 331 | |
Alex Vakulenko | 82b02de | 2015-10-09 20:07:47 -0700 | [diff] [blame] | 332 | if (!device_->SetStateProperties(state_change, nullptr)) { |
Bertrand SIMONNET | 59890e2 | 2015-10-02 16:45:18 -0700 | [diff] [blame] | 333 | LOG(ERROR) << "failed to update weave's state"; |
| 334 | } |
| 335 | } |
| 336 | |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 337 | // static |
Bertrand SIMONNET | 608e428 | 2015-11-12 17:52:17 -0800 | [diff] [blame] | 338 | DBusHandlerResult MetricsCollector::MessageFilter(DBusConnection* connection, |
| 339 | DBusMessage* message, |
| 340 | void* user_data) { |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 341 | int message_type = dbus_message_get_type(message); |
| 342 | if (message_type != DBUS_MESSAGE_TYPE_SIGNAL) { |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 343 | DLOG(WARNING) << "unexpected message type " << message_type; |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 344 | return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
| 345 | } |
| 346 | |
| 347 | // Signal messages always have interfaces. |
Daniel Erat | c83975a | 2014-04-04 08:53:44 -0700 | [diff] [blame] | 348 | const std::string interface(dbus_message_get_interface(message)); |
| 349 | const std::string member(dbus_message_get_member(message)); |
| 350 | DLOG(INFO) << "Got " << interface << "." << member << " D-Bus signal"; |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 351 | |
Bertrand SIMONNET | 608e428 | 2015-11-12 17:52:17 -0800 | [diff] [blame] | 352 | MetricsCollector* daemon = static_cast<MetricsCollector*>(user_data); |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 353 | |
| 354 | DBusMessageIter iter; |
| 355 | dbus_message_iter_init(message, &iter); |
Daniel Erat | c83975a | 2014-04-04 08:53:44 -0700 | [diff] [blame] | 356 | if (interface == kCrashReporterInterface) { |
| 357 | CHECK_EQ(member, kCrashReporterUserCrashSignal); |
Darin Petkov | 1bb904e | 2010-06-16 15:58:06 -0700 | [diff] [blame] | 358 | daemon->ProcessUserCrash(); |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 359 | } else { |
Daniel Erat | c83975a | 2014-04-04 08:53:44 -0700 | [diff] [blame] | 360 | // Ignore messages from the bus itself. |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 361 | return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
| 362 | } |
| 363 | |
| 364 | return DBUS_HANDLER_RESULT_HANDLED; |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 365 | } |
| 366 | |
Bertrand SIMONNET | 608e428 | 2015-11-12 17:52:17 -0800 | [diff] [blame] | 367 | void MetricsCollector::ProcessUserCrash() { |
Daniel Erat | c83975a | 2014-04-04 08:53:44 -0700 | [diff] [blame] | 368 | // Counts the active time up to now. |
| 369 | UpdateStats(TimeTicks::Now(), Time::Now()); |
Darin Petkov | 1bb904e | 2010-06-16 15:58:06 -0700 | [diff] [blame] | 370 | |
| 371 | // Reports the active use time since the last crash and resets it. |
Bertrand SIMONNET | 2d03783 | 2015-09-16 17:11:51 -0700 | [diff] [blame] | 372 | SendAndResetCrashIntervalSample(user_crash_interval_); |
Ken Mixter | ccd84c0 | 2010-08-16 19:57:13 -0700 | [diff] [blame] | 373 | |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 374 | any_crashes_daily_count_->Add(1); |
| 375 | any_crashes_weekly_count_->Add(1); |
| 376 | user_crashes_daily_count_->Add(1); |
| 377 | user_crashes_weekly_count_->Add(1); |
Darin Petkov | 1bb904e | 2010-06-16 15:58:06 -0700 | [diff] [blame] | 378 | } |
| 379 | |
Bertrand SIMONNET | 608e428 | 2015-11-12 17:52:17 -0800 | [diff] [blame] | 380 | void MetricsCollector::ProcessKernelCrash() { |
Daniel Erat | c83975a | 2014-04-04 08:53:44 -0700 | [diff] [blame] | 381 | // Counts the active time up to now. |
| 382 | UpdateStats(TimeTicks::Now(), Time::Now()); |
Darin Petkov | 38d5cb0 | 2010-06-24 12:10:26 -0700 | [diff] [blame] | 383 | |
| 384 | // Reports the active use time since the last crash and resets it. |
Bertrand SIMONNET | 2d03783 | 2015-09-16 17:11:51 -0700 | [diff] [blame] | 385 | SendAndResetCrashIntervalSample(kernel_crash_interval_); |
Ken Mixter | ccd84c0 | 2010-08-16 19:57:13 -0700 | [diff] [blame] | 386 | |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 387 | any_crashes_daily_count_->Add(1); |
| 388 | any_crashes_weekly_count_->Add(1); |
| 389 | kernel_crashes_daily_count_->Add(1); |
| 390 | kernel_crashes_weekly_count_->Add(1); |
Luigi Semenzato | 859b3f0 | 2014-02-05 15:33:19 -0800 | [diff] [blame] | 391 | |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 392 | kernel_crashes_version_count_->Add(1); |
Darin Petkov | 38d5cb0 | 2010-06-24 12:10:26 -0700 | [diff] [blame] | 393 | } |
| 394 | |
Bertrand SIMONNET | 608e428 | 2015-11-12 17:52:17 -0800 | [diff] [blame] | 395 | void MetricsCollector::ProcessUncleanShutdown() { |
Daniel Erat | c83975a | 2014-04-04 08:53:44 -0700 | [diff] [blame] | 396 | // Counts the active time up to now. |
| 397 | UpdateStats(TimeTicks::Now(), Time::Now()); |
Ken Mixter | ccd84c0 | 2010-08-16 19:57:13 -0700 | [diff] [blame] | 398 | |
| 399 | // Reports the active use time since the last crash and resets it. |
Bertrand SIMONNET | 2d03783 | 2015-09-16 17:11:51 -0700 | [diff] [blame] | 400 | SendAndResetCrashIntervalSample(unclean_shutdown_interval_); |
Ken Mixter | ccd84c0 | 2010-08-16 19:57:13 -0700 | [diff] [blame] | 401 | |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 402 | unclean_shutdowns_daily_count_->Add(1); |
| 403 | unclean_shutdowns_weekly_count_->Add(1); |
| 404 | any_crashes_daily_count_->Add(1); |
| 405 | any_crashes_weekly_count_->Add(1); |
Ken Mixter | ccd84c0 | 2010-08-16 19:57:13 -0700 | [diff] [blame] | 406 | } |
| 407 | |
Bertrand SIMONNET | 608e428 | 2015-11-12 17:52:17 -0800 | [diff] [blame] | 408 | bool MetricsCollector::CheckSystemCrash(const string& crash_file) { |
Darin Petkov | 38d5cb0 | 2010-06-24 12:10:26 -0700 | [diff] [blame] | 409 | FilePath crash_detected(crash_file); |
Ben Chan | 2e6543d | 2014-02-05 23:26:25 -0800 | [diff] [blame] | 410 | if (!base::PathExists(crash_detected)) |
Ken Mixter | ccd84c0 | 2010-08-16 19:57:13 -0700 | [diff] [blame] | 411 | return false; |
Darin Petkov | 38d5cb0 | 2010-06-24 12:10:26 -0700 | [diff] [blame] | 412 | |
| 413 | // Deletes the crash-detected file so that the daemon doesn't report |
| 414 | // another kernel crash in case it's restarted. |
Luigi Semenzato | 859b3f0 | 2014-02-05 15:33:19 -0800 | [diff] [blame] | 415 | base::DeleteFile(crash_detected, false); // not recursive |
Ken Mixter | ccd84c0 | 2010-08-16 19:57:13 -0700 | [diff] [blame] | 416 | return true; |
Darin Petkov | 38d5cb0 | 2010-06-24 12:10:26 -0700 | [diff] [blame] | 417 | } |
| 418 | |
Bertrand SIMONNET | 608e428 | 2015-11-12 17:52:17 -0800 | [diff] [blame] | 419 | void MetricsCollector::StatsReporterInit() { |
Bertrand SIMONNET | 5658dc5 | 2015-09-18 13:38:10 -0700 | [diff] [blame] | 420 | disk_usage_collector_->Schedule(); |
Bertrand SIMONNET | 7a96405 | 2015-09-29 11:07:24 -0700 | [diff] [blame] | 421 | |
Bertrand SIMONNET | 0ada2ca | 2015-11-02 14:08:44 -0800 | [diff] [blame] | 422 | cpu_usage_collector_->Init(); |
| 423 | cpu_usage_collector_->Schedule(); |
| 424 | |
Bertrand SIMONNET | 7a96405 | 2015-09-29 11:07:24 -0700 | [diff] [blame] | 425 | // Don't start a collection cycle during the first run to avoid delaying the |
| 426 | // boot. |
| 427 | averaged_stats_collector_->ScheduleWait(); |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 428 | } |
| 429 | |
Bertrand SIMONNET | 608e428 | 2015-11-12 17:52:17 -0800 | [diff] [blame] | 430 | void MetricsCollector::ScheduleMeminfoCallback(int wait) { |
Luigi Semenzato | 29c7ef9 | 2011-04-12 14:12:35 -0700 | [diff] [blame] | 431 | if (testing_) { |
| 432 | return; |
| 433 | } |
Steve Fung | e86591e | 2014-12-01 13:38:21 -0800 | [diff] [blame] | 434 | base::TimeDelta waitDelta = base::TimeDelta::FromSeconds(wait); |
| 435 | base::MessageLoop::current()->PostDelayedTask(FROM_HERE, |
Bertrand SIMONNET | 608e428 | 2015-11-12 17:52:17 -0800 | [diff] [blame] | 436 | base::Bind(&MetricsCollector::MeminfoCallback, base::Unretained(this), |
Steve Fung | 8ab89c5 | 2015-01-05 13:48:30 -0800 | [diff] [blame] | 437 | waitDelta), |
Steve Fung | e86591e | 2014-12-01 13:38:21 -0800 | [diff] [blame] | 438 | waitDelta); |
Luigi Semenzato | 29c7ef9 | 2011-04-12 14:12:35 -0700 | [diff] [blame] | 439 | } |
| 440 | |
Bertrand SIMONNET | 608e428 | 2015-11-12 17:52:17 -0800 | [diff] [blame] | 441 | void MetricsCollector::MeminfoCallback(base::TimeDelta wait) { |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 442 | string meminfo_raw; |
Bertrand SIMONNET | 675a10c | 2015-08-25 14:11:43 -0700 | [diff] [blame] | 443 | const FilePath meminfo_path(kMeminfoFileName); |
Ben Chan | 2e6543d | 2014-02-05 23:26:25 -0800 | [diff] [blame] | 444 | if (!base::ReadFileToString(meminfo_path, &meminfo_raw)) { |
Luigi Semenzato | 29c7ef9 | 2011-04-12 14:12:35 -0700 | [diff] [blame] | 445 | LOG(WARNING) << "cannot read " << meminfo_path.value().c_str(); |
Steve Fung | e86591e | 2014-12-01 13:38:21 -0800 | [diff] [blame] | 446 | return; |
Luigi Semenzato | 29c7ef9 | 2011-04-12 14:12:35 -0700 | [diff] [blame] | 447 | } |
Luigi Semenzato | 9636019 | 2014-06-04 10:53:35 -0700 | [diff] [blame] | 448 | // Make both calls even if the first one fails. |
Bertrand SIMONNET | ebbe35c | 2015-09-08 10:13:35 -0700 | [diff] [blame] | 449 | if (ProcessMeminfo(meminfo_raw)) { |
Steve Fung | e86591e | 2014-12-01 13:38:21 -0800 | [diff] [blame] | 450 | base::MessageLoop::current()->PostDelayedTask(FROM_HERE, |
Bertrand SIMONNET | 608e428 | 2015-11-12 17:52:17 -0800 | [diff] [blame] | 451 | base::Bind(&MetricsCollector::MeminfoCallback, base::Unretained(this), |
Steve Fung | 8ab89c5 | 2015-01-05 13:48:30 -0800 | [diff] [blame] | 452 | wait), |
Steve Fung | e86591e | 2014-12-01 13:38:21 -0800 | [diff] [blame] | 453 | wait); |
| 454 | } |
Luigi Semenzato | 9636019 | 2014-06-04 10:53:35 -0700 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | // static |
Bertrand SIMONNET | 608e428 | 2015-11-12 17:52:17 -0800 | [diff] [blame] | 458 | bool MetricsCollector::ReadFileToUint64(const base::FilePath& path, |
| 459 | uint64_t* value) { |
Luigi Semenzato | 9636019 | 2014-06-04 10:53:35 -0700 | [diff] [blame] | 460 | std::string content; |
| 461 | if (!base::ReadFileToString(path, &content)) { |
| 462 | PLOG(WARNING) << "cannot read " << path.MaybeAsASCII(); |
| 463 | return false; |
| 464 | } |
Luigi Semenzato | 4a6c942 | 2014-06-30 18:12:28 -0700 | [diff] [blame] | 465 | // Remove final newline. |
| 466 | base::TrimWhitespaceASCII(content, base::TRIM_TRAILING, &content); |
Luigi Semenzato | 9636019 | 2014-06-04 10:53:35 -0700 | [diff] [blame] | 467 | if (!base::StringToUint64(content, value)) { |
| 468 | LOG(WARNING) << "invalid integer: " << content; |
| 469 | return false; |
| 470 | } |
| 471 | return true; |
| 472 | } |
| 473 | |
Bertrand SIMONNET | 608e428 | 2015-11-12 17:52:17 -0800 | [diff] [blame] | 474 | bool MetricsCollector::ReportZram(const base::FilePath& zram_dir) { |
Luigi Semenzato | 9636019 | 2014-06-04 10:53:35 -0700 | [diff] [blame] | 475 | // Data sizes are in bytes. |zero_pages| is in number of pages. |
Ben Chan | f05ab40 | 2014-08-07 00:54:59 -0700 | [diff] [blame] | 476 | uint64_t compr_data_size, orig_data_size, zero_pages; |
Luigi Semenzato | 9636019 | 2014-06-04 10:53:35 -0700 | [diff] [blame] | 477 | const size_t page_size = 4096; |
| 478 | |
| 479 | if (!ReadFileToUint64(zram_dir.Append(kComprDataSizeName), |
| 480 | &compr_data_size) || |
| 481 | !ReadFileToUint64(zram_dir.Append(kOrigDataSizeName), &orig_data_size) || |
| 482 | !ReadFileToUint64(zram_dir.Append(kZeroPagesName), &zero_pages)) { |
| 483 | return false; |
| 484 | } |
| 485 | |
| 486 | // |orig_data_size| does not include zero-filled pages. |
| 487 | orig_data_size += zero_pages * page_size; |
| 488 | |
| 489 | const int compr_data_size_mb = compr_data_size >> 20; |
| 490 | const int savings_mb = (orig_data_size - compr_data_size) >> 20; |
| 491 | const int zero_ratio_percent = zero_pages * page_size * 100 / orig_data_size; |
| 492 | |
| 493 | // Report compressed size in megabytes. 100 MB or less has little impact. |
| 494 | SendSample("Platform.ZramCompressedSize", compr_data_size_mb, 100, 4000, 50); |
| 495 | SendSample("Platform.ZramSavings", savings_mb, 100, 4000, 50); |
| 496 | // The compression ratio is multiplied by 100 for better resolution. The |
| 497 | // ratios of interest are between 1 and 6 (100% and 600% as reported). We |
| 498 | // don't want samples when very little memory is being compressed. |
| 499 | if (compr_data_size_mb >= 1) { |
| 500 | SendSample("Platform.ZramCompressionRatioPercent", |
| 501 | orig_data_size * 100 / compr_data_size, 100, 600, 50); |
| 502 | } |
| 503 | // The values of interest for zero_pages are between 1MB and 1GB. The units |
| 504 | // are number of pages. |
| 505 | SendSample("Platform.ZramZeroPages", zero_pages, 256, 256 * 1024, 50); |
| 506 | SendSample("Platform.ZramZeroRatioPercent", zero_ratio_percent, 1, 50, 50); |
| 507 | |
| 508 | return true; |
Luigi Semenzato | 29c7ef9 | 2011-04-12 14:12:35 -0700 | [diff] [blame] | 509 | } |
| 510 | |
Bertrand SIMONNET | 608e428 | 2015-11-12 17:52:17 -0800 | [diff] [blame] | 511 | bool MetricsCollector::ProcessMeminfo(const string& meminfo_raw) { |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 512 | static const MeminfoRecord fields_array[] = { |
Luigi Semenzato | 29c7ef9 | 2011-04-12 14:12:35 -0700 | [diff] [blame] | 513 | { "MemTotal", "MemTotal" }, // SPECIAL CASE: total system memory |
| 514 | { "MemFree", "MemFree" }, |
| 515 | { "Buffers", "Buffers" }, |
| 516 | { "Cached", "Cached" }, |
| 517 | // { "SwapCached", "SwapCached" }, |
| 518 | { "Active", "Active" }, |
| 519 | { "Inactive", "Inactive" }, |
| 520 | { "ActiveAnon", "Active(anon)" }, |
| 521 | { "InactiveAnon", "Inactive(anon)" }, |
| 522 | { "ActiveFile" , "Active(file)" }, |
| 523 | { "InactiveFile", "Inactive(file)" }, |
Luigi Semenzato | 942cbab | 2013-02-12 13:17:07 -0800 | [diff] [blame] | 524 | { "Unevictable", "Unevictable", kMeminfoOp_HistLog }, |
Luigi Semenzato | 29c7ef9 | 2011-04-12 14:12:35 -0700 | [diff] [blame] | 525 | // { "Mlocked", "Mlocked" }, |
Luigi Semenzato | 942cbab | 2013-02-12 13:17:07 -0800 | [diff] [blame] | 526 | { "SwapTotal", "SwapTotal", kMeminfoOp_SwapTotal }, |
| 527 | { "SwapFree", "SwapFree", kMeminfoOp_SwapFree }, |
Luigi Semenzato | 29c7ef9 | 2011-04-12 14:12:35 -0700 | [diff] [blame] | 528 | // { "Dirty", "Dirty" }, |
| 529 | // { "Writeback", "Writeback" }, |
| 530 | { "AnonPages", "AnonPages" }, |
| 531 | { "Mapped", "Mapped" }, |
Luigi Semenzato | 942cbab | 2013-02-12 13:17:07 -0800 | [diff] [blame] | 532 | { "Shmem", "Shmem", kMeminfoOp_HistLog }, |
| 533 | { "Slab", "Slab", kMeminfoOp_HistLog }, |
Luigi Semenzato | 29c7ef9 | 2011-04-12 14:12:35 -0700 | [diff] [blame] | 534 | // { "SReclaimable", "SReclaimable" }, |
| 535 | // { "SUnreclaim", "SUnreclaim" }, |
| 536 | }; |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 537 | vector<MeminfoRecord> fields(fields_array, |
| 538 | fields_array + arraysize(fields_array)); |
| 539 | if (!FillMeminfo(meminfo_raw, &fields)) { |
| 540 | return false; |
| 541 | } |
| 542 | int total_memory = fields[0].value; |
| 543 | if (total_memory == 0) { |
| 544 | // this "cannot happen" |
| 545 | LOG(WARNING) << "borked meminfo parser"; |
| 546 | return false; |
| 547 | } |
Luigi Semenzato | 942cbab | 2013-02-12 13:17:07 -0800 | [diff] [blame] | 548 | int swap_total = 0; |
| 549 | int swap_free = 0; |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 550 | // Send all fields retrieved, except total memory. |
| 551 | for (unsigned int i = 1; i < fields.size(); i++) { |
Luigi Semenzato | 859b3f0 | 2014-02-05 15:33:19 -0800 | [diff] [blame] | 552 | string metrics_name = base::StringPrintf("Platform.Meminfo%s", |
| 553 | fields[i].name); |
Luigi Semenzato | 3ccca06 | 2013-02-04 19:50:45 -0800 | [diff] [blame] | 554 | int percent; |
Luigi Semenzato | 942cbab | 2013-02-12 13:17:07 -0800 | [diff] [blame] | 555 | switch (fields[i].op) { |
| 556 | case kMeminfoOp_HistPercent: |
Luigi Semenzato | 3ccca06 | 2013-02-04 19:50:45 -0800 | [diff] [blame] | 557 | // report value as percent of total memory |
| 558 | percent = fields[i].value * 100 / total_memory; |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 559 | SendLinearSample(metrics_name, percent, 100, 101); |
Luigi Semenzato | 3ccca06 | 2013-02-04 19:50:45 -0800 | [diff] [blame] | 560 | break; |
Luigi Semenzato | 942cbab | 2013-02-12 13:17:07 -0800 | [diff] [blame] | 561 | case kMeminfoOp_HistLog: |
Luigi Semenzato | 3ccca06 | 2013-02-04 19:50:45 -0800 | [diff] [blame] | 562 | // report value in kbytes, log scale, 4Gb max |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 563 | SendSample(metrics_name, fields[i].value, 1, 4 * 1000 * 1000, 100); |
Luigi Semenzato | 3ccca06 | 2013-02-04 19:50:45 -0800 | [diff] [blame] | 564 | break; |
Luigi Semenzato | 942cbab | 2013-02-12 13:17:07 -0800 | [diff] [blame] | 565 | case kMeminfoOp_SwapTotal: |
| 566 | swap_total = fields[i].value; |
| 567 | case kMeminfoOp_SwapFree: |
| 568 | swap_free = fields[i].value; |
Luigi Semenzato | 3ccca06 | 2013-02-04 19:50:45 -0800 | [diff] [blame] | 569 | break; |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 570 | } |
| 571 | } |
Luigi Semenzato | 942cbab | 2013-02-12 13:17:07 -0800 | [diff] [blame] | 572 | if (swap_total > 0) { |
| 573 | int swap_used = swap_total - swap_free; |
| 574 | int swap_used_percent = swap_used * 100 / swap_total; |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 575 | SendSample("Platform.MeminfoSwapUsed", swap_used, 1, 8 * 1000 * 1000, 100); |
Bertrand SIMONNET | 008fb7e | 2015-09-21 16:48:01 -0700 | [diff] [blame] | 576 | SendLinearSample("Platform.MeminfoSwapUsed.Percent", swap_used_percent, |
Luigi Semenzato | 942cbab | 2013-02-12 13:17:07 -0800 | [diff] [blame] | 577 | 100, 101); |
| 578 | } |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 579 | return true; |
| 580 | } |
| 581 | |
Bertrand SIMONNET | 608e428 | 2015-11-12 17:52:17 -0800 | [diff] [blame] | 582 | bool MetricsCollector::FillMeminfo(const string& meminfo_raw, |
| 583 | vector<MeminfoRecord>* fields) { |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 584 | vector<string> lines; |
| 585 | unsigned int nlines = Tokenize(meminfo_raw, "\n", &lines); |
Luigi Semenzato | 29c7ef9 | 2011-04-12 14:12:35 -0700 | [diff] [blame] | 586 | |
| 587 | // Scan meminfo output and collect field values. Each field name has to |
| 588 | // match a meminfo entry (case insensitive) after removing non-alpha |
| 589 | // characters from the entry. |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 590 | unsigned int ifield = 0; |
| 591 | for (unsigned int iline = 0; |
| 592 | iline < nlines && ifield < fields->size(); |
| 593 | iline++) { |
| 594 | vector<string> tokens; |
Luigi Semenzato | 29c7ef9 | 2011-04-12 14:12:35 -0700 | [diff] [blame] | 595 | Tokenize(lines[iline], ": ", &tokens); |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 596 | if (strcmp((*fields)[ifield].match, tokens[0].c_str()) == 0) { |
| 597 | // Name matches. Parse value and save. |
Bertrand SIMONNET | 675a10c | 2015-08-25 14:11:43 -0700 | [diff] [blame] | 598 | if (!base::StringToInt(tokens[1], &(*fields)[ifield].value)) { |
| 599 | LOG(WARNING) << "Cound not convert " << tokens[1] << " to int"; |
Luigi Semenzato | 29c7ef9 | 2011-04-12 14:12:35 -0700 | [diff] [blame] | 600 | return false; |
| 601 | } |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 602 | ifield++; |
Luigi Semenzato | 29c7ef9 | 2011-04-12 14:12:35 -0700 | [diff] [blame] | 603 | } |
Luigi Semenzato | 29c7ef9 | 2011-04-12 14:12:35 -0700 | [diff] [blame] | 604 | } |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 605 | if (ifield < fields->size()) { |
| 606 | // End of input reached while scanning. |
| 607 | LOG(WARNING) << "cannot find field " << (*fields)[ifield].match |
| 608 | << " and following"; |
| 609 | return false; |
| 610 | } |
| 611 | return true; |
| 612 | } |
| 613 | |
Bertrand SIMONNET | 608e428 | 2015-11-12 17:52:17 -0800 | [diff] [blame] | 614 | void MetricsCollector::ScheduleMemuseCallback(double interval) { |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 615 | if (testing_) { |
| 616 | return; |
| 617 | } |
Steve Fung | e86591e | 2014-12-01 13:38:21 -0800 | [diff] [blame] | 618 | base::MessageLoop::current()->PostDelayedTask(FROM_HERE, |
Bertrand SIMONNET | 608e428 | 2015-11-12 17:52:17 -0800 | [diff] [blame] | 619 | base::Bind(&MetricsCollector::MemuseCallback, base::Unretained(this)), |
Steve Fung | e86591e | 2014-12-01 13:38:21 -0800 | [diff] [blame] | 620 | base::TimeDelta::FromSeconds(interval)); |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 621 | } |
| 622 | |
Bertrand SIMONNET | 608e428 | 2015-11-12 17:52:17 -0800 | [diff] [blame] | 623 | void MetricsCollector::MemuseCallback() { |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 624 | // Since we only care about active time (i.e. uptime minus sleep time) but |
| 625 | // the callbacks are driven by real time (uptime), we check if we should |
| 626 | // reschedule this callback due to intervening sleep periods. |
| 627 | double now = GetActiveTime(); |
Luigi Semenzato | 0d9a9c9 | 2013-12-05 15:55:12 -0800 | [diff] [blame] | 628 | // Avoid intervals of less than one second. |
| 629 | double remaining_time = ceil(memuse_final_time_ - now); |
| 630 | if (remaining_time > 0) { |
| 631 | ScheduleMemuseCallback(remaining_time); |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 632 | } else { |
Luigi Semenzato | 0d9a9c9 | 2013-12-05 15:55:12 -0800 | [diff] [blame] | 633 | // Report stats and advance the measurement interval unless there are |
| 634 | // errors or we've completed the last interval. |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 635 | if (MemuseCallbackWork() && |
Luigi Semenzato | 0d9a9c9 | 2013-12-05 15:55:12 -0800 | [diff] [blame] | 636 | memuse_interval_index_ < arraysize(kMemuseIntervals)) { |
| 637 | double interval = kMemuseIntervals[memuse_interval_index_++]; |
| 638 | memuse_final_time_ = now + interval; |
| 639 | ScheduleMemuseCallback(interval); |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 640 | } |
| 641 | } |
| 642 | } |
| 643 | |
Bertrand SIMONNET | 608e428 | 2015-11-12 17:52:17 -0800 | [diff] [blame] | 644 | bool MetricsCollector::MemuseCallbackWork() { |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 645 | string meminfo_raw; |
Bertrand SIMONNET | 675a10c | 2015-08-25 14:11:43 -0700 | [diff] [blame] | 646 | const FilePath meminfo_path(kMeminfoFileName); |
Ben Chan | 2e6543d | 2014-02-05 23:26:25 -0800 | [diff] [blame] | 647 | if (!base::ReadFileToString(meminfo_path, &meminfo_raw)) { |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 648 | LOG(WARNING) << "cannot read " << meminfo_path.value().c_str(); |
| 649 | return false; |
| 650 | } |
| 651 | return ProcessMemuse(meminfo_raw); |
| 652 | } |
| 653 | |
Bertrand SIMONNET | 608e428 | 2015-11-12 17:52:17 -0800 | [diff] [blame] | 654 | bool MetricsCollector::ProcessMemuse(const string& meminfo_raw) { |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 655 | static const MeminfoRecord fields_array[] = { |
| 656 | { "MemTotal", "MemTotal" }, // SPECIAL CASE: total system memory |
| 657 | { "ActiveAnon", "Active(anon)" }, |
| 658 | { "InactiveAnon", "Inactive(anon)" }, |
| 659 | }; |
| 660 | vector<MeminfoRecord> fields(fields_array, |
| 661 | fields_array + arraysize(fields_array)); |
| 662 | if (!FillMeminfo(meminfo_raw, &fields)) { |
| 663 | return false; |
| 664 | } |
| 665 | int total = fields[0].value; |
| 666 | int active_anon = fields[1].value; |
| 667 | int inactive_anon = fields[2].value; |
| 668 | if (total == 0) { |
| 669 | // this "cannot happen" |
| 670 | LOG(WARNING) << "borked meminfo parser"; |
| 671 | return false; |
| 672 | } |
Luigi Semenzato | 859b3f0 | 2014-02-05 15:33:19 -0800 | [diff] [blame] | 673 | string metrics_name = base::StringPrintf("Platform.MemuseAnon%d", |
| 674 | memuse_interval_index_); |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 675 | SendLinearSample(metrics_name, (active_anon + inactive_anon) * 100 / total, |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 676 | 100, 101); |
| 677 | return true; |
Luigi Semenzato | 29c7ef9 | 2011-04-12 14:12:35 -0700 | [diff] [blame] | 678 | } |
| 679 | |
Bertrand SIMONNET | 608e428 | 2015-11-12 17:52:17 -0800 | [diff] [blame] | 680 | void MetricsCollector::SendSample(const string& name, int sample, |
| 681 | int min, int max, int nbuckets) { |
Darin Petkov | fc91b42 | 2010-05-12 13:05:45 -0700 | [diff] [blame] | 682 | metrics_lib_->SendToUMA(name, sample, min, max, nbuckets); |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 683 | } |
Luigi Semenzato | 29c7ef9 | 2011-04-12 14:12:35 -0700 | [diff] [blame] | 684 | |
Bertrand SIMONNET | 608e428 | 2015-11-12 17:52:17 -0800 | [diff] [blame] | 685 | void MetricsCollector::SendKernelCrashesCumulativeCountStats() { |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 686 | // Report the number of crashes for this OS version, but don't clear the |
| 687 | // counter. It is cleared elsewhere on version change. |
Ben Chan | f05ab40 | 2014-08-07 00:54:59 -0700 | [diff] [blame] | 688 | int64_t crashes_count = kernel_crashes_version_count_->Get(); |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 689 | SendSample(kernel_crashes_version_count_->Name(), |
Luigi Semenzato | ba0c65d | 2014-03-17 12:28:38 -0700 | [diff] [blame] | 690 | crashes_count, |
| 691 | 1, // value of first bucket |
| 692 | 500, // value of last bucket |
| 693 | 100); // number of buckets |
| 694 | |
| 695 | |
Ben Chan | f05ab40 | 2014-08-07 00:54:59 -0700 | [diff] [blame] | 696 | int64_t cpu_use_ms = version_cumulative_cpu_use_->Get(); |
Luigi Semenzato | ba0c65d | 2014-03-17 12:28:38 -0700 | [diff] [blame] | 697 | SendSample(version_cumulative_cpu_use_->Name(), |
| 698 | cpu_use_ms / 1000, // stat is in seconds |
| 699 | 1, // device may be used very little... |
| 700 | 8 * 1000 * 1000, // ... or a lot (a little over 90 days) |
| 701 | 100); |
| 702 | |
| 703 | // On the first run after an autoupdate, cpu_use_ms and active_use_seconds |
| 704 | // can be zero. Avoid division by zero. |
| 705 | if (cpu_use_ms > 0) { |
| 706 | // Send the crash frequency since update in number of crashes per CPU year. |
| 707 | SendSample("Logging.KernelCrashesPerCpuYear", |
| 708 | crashes_count * kSecondsPerDay * 365 * 1000 / cpu_use_ms, |
| 709 | 1, |
| 710 | 1000 * 1000, // about one crash every 30s of CPU time |
| 711 | 100); |
| 712 | } |
| 713 | |
Ben Chan | f05ab40 | 2014-08-07 00:54:59 -0700 | [diff] [blame] | 714 | int64_t active_use_seconds = version_cumulative_active_use_->Get(); |
Luigi Semenzato | ba0c65d | 2014-03-17 12:28:38 -0700 | [diff] [blame] | 715 | if (active_use_seconds > 0) { |
Luigi Semenzato | e5883fa | 2014-04-18 17:00:35 -0700 | [diff] [blame] | 716 | SendSample(version_cumulative_active_use_->Name(), |
Bertrand SIMONNET | 2d03783 | 2015-09-16 17:11:51 -0700 | [diff] [blame] | 717 | active_use_seconds, |
Luigi Semenzato | e5883fa | 2014-04-18 17:00:35 -0700 | [diff] [blame] | 718 | 1, // device may be used very little... |
| 719 | 8 * 1000 * 1000, // ... or a lot (about 90 days) |
| 720 | 100); |
Luigi Semenzato | ba0c65d | 2014-03-17 12:28:38 -0700 | [diff] [blame] | 721 | // Same as above, but per year of active time. |
| 722 | SendSample("Logging.KernelCrashesPerActiveYear", |
| 723 | crashes_count * kSecondsPerDay * 365 / active_use_seconds, |
| 724 | 1, |
| 725 | 1000 * 1000, // about one crash every 30s of active time |
| 726 | 100); |
| 727 | } |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 728 | } |
| 729 | |
Bertrand SIMONNET | 608e428 | 2015-11-12 17:52:17 -0800 | [diff] [blame] | 730 | void MetricsCollector::SendAndResetDailyUseSample( |
Luigi Semenzato | e5883fa | 2014-04-18 17:00:35 -0700 | [diff] [blame] | 731 | const scoped_ptr<PersistentInteger>& use) { |
| 732 | SendSample(use->Name(), |
| 733 | use->GetAndClear(), |
| 734 | 1, // value of first bucket |
| 735 | kSecondsPerDay, // value of last bucket |
| 736 | 50); // number of buckets |
| 737 | } |
| 738 | |
Bertrand SIMONNET | 608e428 | 2015-11-12 17:52:17 -0800 | [diff] [blame] | 739 | void MetricsCollector::SendAndResetCrashIntervalSample( |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 740 | const scoped_ptr<PersistentInteger>& interval) { |
| 741 | SendSample(interval->Name(), |
| 742 | interval->GetAndClear(), |
| 743 | 1, // value of first bucket |
| 744 | 4 * kSecondsPerWeek, // value of last bucket |
| 745 | 50); // number of buckets |
| 746 | } |
| 747 | |
Bertrand SIMONNET | 608e428 | 2015-11-12 17:52:17 -0800 | [diff] [blame] | 748 | void MetricsCollector::SendAndResetCrashFrequencySample( |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 749 | const scoped_ptr<PersistentInteger>& frequency) { |
| 750 | SendSample(frequency->Name(), |
| 751 | frequency->GetAndClear(), |
| 752 | 1, // value of first bucket |
| 753 | 100, // value of last bucket |
| 754 | 50); // number of buckets |
| 755 | } |
| 756 | |
Bertrand SIMONNET | 608e428 | 2015-11-12 17:52:17 -0800 | [diff] [blame] | 757 | void MetricsCollector::SendLinearSample(const string& name, int sample, |
| 758 | int max, int nbuckets) { |
Luigi Semenzato | 29c7ef9 | 2011-04-12 14:12:35 -0700 | [diff] [blame] | 759 | // TODO(semenzato): add a proper linear histogram to the Chrome external |
| 760 | // metrics API. |
| 761 | LOG_IF(FATAL, nbuckets != max + 1) << "unsupported histogram scale"; |
| 762 | metrics_lib_->SendEnumToUMA(name, sample, max); |
| 763 | } |
Daniel Erat | c83975a | 2014-04-04 08:53:44 -0700 | [diff] [blame] | 764 | |
Bertrand SIMONNET | 608e428 | 2015-11-12 17:52:17 -0800 | [diff] [blame] | 765 | void MetricsCollector::UpdateStats(TimeTicks now_ticks, |
| 766 | Time now_wall_time) { |
Daniel Erat | c83975a | 2014-04-04 08:53:44 -0700 | [diff] [blame] | 767 | const int elapsed_seconds = (now_ticks - last_update_stats_time_).InSeconds(); |
Luigi Semenzato | e5883fa | 2014-04-18 17:00:35 -0700 | [diff] [blame] | 768 | daily_active_use_->Add(elapsed_seconds); |
| 769 | version_cumulative_active_use_->Add(elapsed_seconds); |
Daniel Erat | c83975a | 2014-04-04 08:53:44 -0700 | [diff] [blame] | 770 | user_crash_interval_->Add(elapsed_seconds); |
| 771 | kernel_crash_interval_->Add(elapsed_seconds); |
Bertrand SIMONNET | 0ada2ca | 2015-11-02 14:08:44 -0800 | [diff] [blame] | 772 | TimeDelta cpu_use = cpu_usage_collector_->GetCumulativeCpuUse(); |
| 773 | version_cumulative_cpu_use_->Add( |
| 774 | (cpu_use - latest_cpu_use_microseconds_).InMilliseconds()); |
| 775 | latest_cpu_use_microseconds_ = cpu_use; |
Daniel Erat | c83975a | 2014-04-04 08:53:44 -0700 | [diff] [blame] | 776 | last_update_stats_time_ = now_ticks; |
| 777 | |
| 778 | const TimeDelta since_epoch = now_wall_time - Time::UnixEpoch(); |
| 779 | const int day = since_epoch.InDays(); |
| 780 | const int week = day / 7; |
| 781 | |
| 782 | if (daily_cycle_->Get() != day) { |
| 783 | daily_cycle_->Set(day); |
Bertrand SIMONNET | 2d03783 | 2015-09-16 17:11:51 -0700 | [diff] [blame] | 784 | SendAndResetDailyUseSample(daily_active_use_); |
| 785 | SendAndResetCrashFrequencySample(any_crashes_daily_count_); |
| 786 | SendAndResetCrashFrequencySample(user_crashes_daily_count_); |
| 787 | SendAndResetCrashFrequencySample(kernel_crashes_daily_count_); |
| 788 | SendAndResetCrashFrequencySample(unclean_shutdowns_daily_count_); |
Luigi Semenzato | e5883fa | 2014-04-18 17:00:35 -0700 | [diff] [blame] | 789 | SendKernelCrashesCumulativeCountStats(); |
Daniel Erat | c83975a | 2014-04-04 08:53:44 -0700 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | if (weekly_cycle_->Get() != week) { |
| 793 | weekly_cycle_->Set(week); |
Bertrand SIMONNET | 2d03783 | 2015-09-16 17:11:51 -0700 | [diff] [blame] | 794 | SendAndResetCrashFrequencySample(any_crashes_weekly_count_); |
| 795 | SendAndResetCrashFrequencySample(user_crashes_weekly_count_); |
| 796 | SendAndResetCrashFrequencySample(kernel_crashes_weekly_count_); |
| 797 | SendAndResetCrashFrequencySample(unclean_shutdowns_weekly_count_); |
Daniel Erat | c83975a | 2014-04-04 08:53:44 -0700 | [diff] [blame] | 798 | } |
| 799 | } |
| 800 | |
Bertrand SIMONNET | 608e428 | 2015-11-12 17:52:17 -0800 | [diff] [blame] | 801 | void MetricsCollector::HandleUpdateStatsTimeout() { |
Steve Fung | e86591e | 2014-12-01 13:38:21 -0800 | [diff] [blame] | 802 | UpdateStats(TimeTicks::Now(), Time::Now()); |
| 803 | base::MessageLoop::current()->PostDelayedTask(FROM_HERE, |
Bertrand SIMONNET | 608e428 | 2015-11-12 17:52:17 -0800 | [diff] [blame] | 804 | base::Bind(&MetricsCollector::HandleUpdateStatsTimeout, |
Steve Fung | e86591e | 2014-12-01 13:38:21 -0800 | [diff] [blame] | 805 | base::Unretained(this)), |
| 806 | base::TimeDelta::FromMilliseconds(kUpdateStatsIntervalMs)); |
Daniel Erat | c83975a | 2014-04-04 08:53:44 -0700 | [diff] [blame] | 807 | } |