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