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