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