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