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