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