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