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