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