Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
| 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 | |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 7 | #include <string.h> |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 8 | |
Darin Petkov | 38d5cb0 | 2010-06-24 12:10:26 -0700 | [diff] [blame] | 9 | #include <base/file_util.h> |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 10 | #include <base/logging.h> |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 11 | #include <dbus/dbus-glib-lowlevel.h> |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 12 | |
Darin Petkov | f1e85e4 | 2010-06-10 15:59:53 -0700 | [diff] [blame] | 13 | #include "counter.h" |
| 14 | |
Darin Petkov | f27f036 | 2010-06-04 13:14:19 -0700 | [diff] [blame] | 15 | using base::Time; |
| 16 | using base::TimeDelta; |
| 17 | using base::TimeTicks; |
Darin Petkov | 38d5cb0 | 2010-06-24 12:10:26 -0700 | [diff] [blame] | 18 | using std::string; |
Darin Petkov | f27f036 | 2010-06-04 13:14:19 -0700 | [diff] [blame] | 19 | |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 20 | #define SAFE_MESSAGE(e) (e.message ? e.message : "unknown error") |
Darin Petkov | 1bb904e | 2010-06-16 15:58:06 -0700 | [diff] [blame] | 21 | #define DBUS_IFACE_CRASH_REPORTER "org.chromium.CrashReporter" |
David James | 6bf6e25 | 2010-06-06 18:52:40 -0700 | [diff] [blame] | 22 | #define DBUS_IFACE_POWER_MANAGER "org.chromium.PowerManager" |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 23 | #define DBUS_IFACE_SESSION_MANAGER "org.chromium.SessionManagerInterface" |
| 24 | |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 25 | static const int kSecondsPerMinute = 60; |
| 26 | static const int kMinutesPerHour = 60; |
| 27 | static const int kHoursPerDay = 24; |
| 28 | static const int kMinutesPerDay = kHoursPerDay * kMinutesPerHour; |
Darin Petkov | 1bb904e | 2010-06-16 15:58:06 -0700 | [diff] [blame] | 29 | static const int kSecondsPerDay = kSecondsPerMinute * kMinutesPerDay; |
| 30 | static const int kDaysPerWeek = 7; |
| 31 | static const int kSecondsPerWeek = kSecondsPerDay * kDaysPerWeek; |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 32 | |
| 33 | // The daily use monitor is scheduled to a 1-minute interval after |
| 34 | // initial user activity and then it's exponentially backed off to |
| 35 | // 10-minute intervals. Although not required, the back off is |
| 36 | // implemented because the histogram buckets are spaced exponentially |
| 37 | // anyway and to avoid too frequent metrics daemon process wake-ups |
| 38 | // and file I/O. |
| 39 | static const int kUseMonitorIntervalInit = 1 * kSecondsPerMinute; |
| 40 | static const int kUseMonitorIntervalMax = 10 * kSecondsPerMinute; |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 41 | |
Ken Mixter | ccd84c0 | 2010-08-16 19:57:13 -0700 | [diff] [blame] | 42 | const char kKernelCrashDetectedFile[] = "/tmp/kernel-crash-detected"; |
| 43 | static const char kUncleanShutdownDetectedFile[] = |
| 44 | "/tmp/unclean-shutdown-detected"; |
| 45 | |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 46 | // static metrics parameters |
Darin Petkov | 2ccef01 | 2010-05-05 16:06:37 -0700 | [diff] [blame] | 47 | const char MetricsDaemon::kMetricDailyUseTimeName[] = |
| 48 | "Logging.DailyUseTime"; |
| 49 | const int MetricsDaemon::kMetricDailyUseTimeMin = 1; |
| 50 | const int MetricsDaemon::kMetricDailyUseTimeMax = kMinutesPerDay; |
| 51 | const int MetricsDaemon::kMetricDailyUseTimeBuckets = 50; |
| 52 | |
Ken Mixter | ccd84c0 | 2010-08-16 19:57:13 -0700 | [diff] [blame] | 53 | // crash interval metrics |
| 54 | const char MetricsDaemon::kMetricKernelCrashIntervalName[] = |
| 55 | "Logging.KernelCrashInterval"; |
| 56 | const char MetricsDaemon::kMetricUncleanShutdownIntervalName[] = |
| 57 | "Logging.UncleanShutdownInterval"; |
Darin Petkov | 1bb904e | 2010-06-16 15:58:06 -0700 | [diff] [blame] | 58 | const char MetricsDaemon::kMetricUserCrashIntervalName[] = |
| 59 | "Logging.UserCrashInterval"; |
Ken Mixter | ccd84c0 | 2010-08-16 19:57:13 -0700 | [diff] [blame] | 60 | |
| 61 | const int MetricsDaemon::kMetricCrashIntervalMin = 1; |
| 62 | const int MetricsDaemon::kMetricCrashIntervalMax = |
| 63 | 4 * kSecondsPerWeek; |
| 64 | const int MetricsDaemon::kMetricCrashIntervalBuckets = 50; |
| 65 | |
| 66 | // crash frequency metrics |
| 67 | const char MetricsDaemon::kMetricAnyCrashesDailyName[] = |
| 68 | "Logging.AnyCrashesDaily"; |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 69 | const char MetricsDaemon::kMetricAnyCrashesWeeklyName[] = |
| 70 | "Logging.AnyCrashesWeekly"; |
Ken Mixter | ccd84c0 | 2010-08-16 19:57:13 -0700 | [diff] [blame] | 71 | const char MetricsDaemon::kMetricKernelCrashesDailyName[] = |
| 72 | "Logging.KernelCrashesDaily"; |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 73 | const char MetricsDaemon::kMetricKernelCrashesWeeklyName[] = |
| 74 | "Logging.KernelCrashesWeekly"; |
Ken Mixter | ccd84c0 | 2010-08-16 19:57:13 -0700 | [diff] [blame] | 75 | const char MetricsDaemon::kMetricUncleanShutdownsDailyName[] = |
| 76 | "Logging.UncleanShutdownsDaily"; |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 77 | const char MetricsDaemon::kMetricUncleanShutdownsWeeklyName[] = |
| 78 | "Logging.UncleanShutdownsWeekly"; |
Ken Mixter | ccd84c0 | 2010-08-16 19:57:13 -0700 | [diff] [blame] | 79 | const char MetricsDaemon::kMetricUserCrashesDailyName[] = |
| 80 | "Logging.UserCrashesDaily"; |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 81 | const char MetricsDaemon::kMetricUserCrashesWeeklyName[] = |
| 82 | "Logging.UserCrashesWeekly"; |
| 83 | const char MetricsDaemon::kMetricCrashFrequencyMin = 1; |
| 84 | const char MetricsDaemon::kMetricCrashFrequencyMax = 100; |
| 85 | const char MetricsDaemon::kMetricCrashFrequencyBuckets = 50; |
Ken Mixter | ccd84c0 | 2010-08-16 19:57:13 -0700 | [diff] [blame] | 86 | |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 87 | // persistent metrics path |
| 88 | const char MetricsDaemon::kMetricsPath[] = "/var/log/metrics"; |
Ken Mixter | ccd84c0 | 2010-08-16 19:57:13 -0700 | [diff] [blame] | 89 | |
Darin Petkov | 1bb904e | 2010-06-16 15:58:06 -0700 | [diff] [blame] | 90 | |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 91 | // static |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 92 | const char* MetricsDaemon::kDBusMatches_[] = { |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 93 | "type='signal'," |
Darin Petkov | 1bb904e | 2010-06-16 15:58:06 -0700 | [diff] [blame] | 94 | "interface='" DBUS_IFACE_CRASH_REPORTER "'," |
| 95 | "path='/'," |
| 96 | "member='UserCrash'", |
| 97 | |
| 98 | "type='signal'," |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 99 | "interface='" DBUS_IFACE_POWER_MANAGER "'," |
Benson Leung | 53faeb0 | 2010-06-08 15:59:13 -0700 | [diff] [blame] | 100 | "path='/'", |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 101 | |
| 102 | "type='signal'," |
| 103 | "sender='org.chromium.SessionManager'," |
| 104 | "interface='" DBUS_IFACE_SESSION_MANAGER "'," |
| 105 | "path='/org/chromium/SessionManager'," |
| 106 | "member='SessionStateChanged'", |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 107 | }; |
| 108 | |
| 109 | // static |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 110 | const char* MetricsDaemon::kPowerStates_[] = { |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 111 | #define STATE(name, capname) #name, |
| 112 | #include "power_states.h" |
| 113 | }; |
| 114 | |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 115 | // static |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 116 | const char* MetricsDaemon::kSessionStates_[] = { |
| 117 | #define STATE(name, capname) #name, |
| 118 | #include "session_states.h" |
| 119 | }; |
| 120 | |
Darin Petkov | f1e85e4 | 2010-06-10 15:59:53 -0700 | [diff] [blame] | 121 | MetricsDaemon::MetricsDaemon() |
Sam Leffler | 239b826 | 2010-08-30 08:56:58 -0700 | [diff] [blame^] | 122 | : power_state_(kUnknownPowerState), |
Darin Petkov | f1e85e4 | 2010-06-10 15:59:53 -0700 | [diff] [blame] | 123 | session_state_(kUnknownSessionState), |
| 124 | user_active_(false), |
| 125 | usemon_interval_(0), |
| 126 | usemon_source_(NULL) {} |
| 127 | |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 128 | MetricsDaemon::~MetricsDaemon() { |
| 129 | DeleteFrequencyCounters(); |
| 130 | } |
| 131 | |
| 132 | void MetricsDaemon::DeleteFrequencyCounters() { |
| 133 | for (FrequencyCounters::iterator i = frequency_counters_.begin(); |
| 134 | i != frequency_counters_.end(); ++i) { |
| 135 | delete i->second; |
| 136 | i->second = NULL; |
| 137 | } |
| 138 | } |
Darin Petkov | f1e85e4 | 2010-06-10 15:59:53 -0700 | [diff] [blame] | 139 | |
Darin Petkov | 2ccef01 | 2010-05-05 16:06:37 -0700 | [diff] [blame] | 140 | void MetricsDaemon::Run(bool run_as_daemon) { |
Darin Petkov | 38d5cb0 | 2010-06-24 12:10:26 -0700 | [diff] [blame] | 141 | if (run_as_daemon && daemon(0, 0) != 0) |
| 142 | return; |
| 143 | |
Ken Mixter | ccd84c0 | 2010-08-16 19:57:13 -0700 | [diff] [blame] | 144 | if (CheckSystemCrash(kKernelCrashDetectedFile)) { |
| 145 | ProcessKernelCrash(); |
| 146 | } |
| 147 | |
| 148 | if (CheckSystemCrash(kUncleanShutdownDetectedFile)) { |
| 149 | ProcessUncleanShutdown(); |
| 150 | } |
| 151 | |
Darin Petkov | 38d5cb0 | 2010-06-24 12:10:26 -0700 | [diff] [blame] | 152 | Loop(); |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 155 | FilePath MetricsDaemon::GetHistogramPath(const char* histogram_name) { |
| 156 | return FilePath(kMetricsPath).Append(histogram_name); |
| 157 | } |
| 158 | |
| 159 | void MetricsDaemon::ConfigureCrashIntervalReporter( |
| 160 | const char* histogram_name, |
| 161 | scoped_ptr<chromeos_metrics::TaggedCounterReporter>* reporter) { |
| 162 | reporter->reset(new chromeos_metrics::TaggedCounterReporter()); |
| 163 | FilePath file_path = GetHistogramPath(histogram_name); |
| 164 | (*reporter)->Init(file_path.value().c_str(), |
| 165 | histogram_name, |
| 166 | kMetricCrashIntervalMin, |
| 167 | kMetricCrashIntervalMax, |
| 168 | kMetricCrashIntervalBuckets); |
| 169 | } |
| 170 | |
| 171 | void MetricsDaemon::ConfigureCrashFrequencyReporter( |
| 172 | const char* histogram_name) { |
| 173 | scoped_ptr<chromeos_metrics::TaggedCounterReporter> reporter( |
| 174 | new chromeos_metrics::TaggedCounterReporter()); |
| 175 | FilePath file_path = GetHistogramPath(histogram_name); |
| 176 | reporter->Init(file_path.value().c_str(), |
| 177 | histogram_name, |
| 178 | kMetricCrashFrequencyMin, |
| 179 | kMetricCrashFrequencyMax, |
| 180 | kMetricCrashFrequencyBuckets); |
| 181 | scoped_ptr<chromeos_metrics::FrequencyCounter> new_counter( |
| 182 | new chromeos_metrics::FrequencyCounter()); |
| 183 | time_t cycle_duration = strstr(histogram_name, "Weekly") != NULL ? |
| 184 | chromeos_metrics::kSecondsPerWeek : |
| 185 | chromeos_metrics::kSecondsPerDay; |
| 186 | new_counter->Init( |
| 187 | static_cast<chromeos_metrics::TaggedCounterInterface*>( |
| 188 | reporter.release()), |
| 189 | cycle_duration); |
| 190 | frequency_counters_[histogram_name] = new_counter.release(); |
| 191 | } |
| 192 | |
Darin Petkov | fc91b42 | 2010-05-12 13:05:45 -0700 | [diff] [blame] | 193 | void MetricsDaemon::Init(bool testing, MetricsLibraryInterface* metrics_lib) { |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 194 | testing_ = testing; |
Darin Petkov | fc91b42 | 2010-05-12 13:05:45 -0700 | [diff] [blame] | 195 | DCHECK(metrics_lib != NULL); |
| 196 | metrics_lib_ = metrics_lib; |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 197 | chromeos_metrics::TaggedCounterReporter:: |
| 198 | SetMetricsLibraryInterface(metrics_lib); |
Darin Petkov | 38d5cb0 | 2010-06-24 12:10:26 -0700 | [diff] [blame] | 199 | |
| 200 | static const char kDailyUseRecordFile[] = "/var/log/metrics/daily-usage"; |
Darin Petkov | f1e85e4 | 2010-06-10 15:59:53 -0700 | [diff] [blame] | 201 | daily_use_.reset(new chromeos_metrics::TaggedCounter()); |
Ken Mixter | ccd84c0 | 2010-08-16 19:57:13 -0700 | [diff] [blame] | 202 | daily_use_->Init(kDailyUseRecordFile, &ReportDailyUse, this); |
Darin Petkov | 38d5cb0 | 2010-06-24 12:10:26 -0700 | [diff] [blame] | 203 | |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 204 | ConfigureCrashIntervalReporter(kMetricKernelCrashIntervalName, |
| 205 | &kernel_crash_interval_); |
| 206 | ConfigureCrashIntervalReporter(kMetricUncleanShutdownIntervalName, |
| 207 | &unclean_shutdown_interval_); |
| 208 | ConfigureCrashIntervalReporter(kMetricUserCrashIntervalName, |
| 209 | &user_crash_interval_); |
Darin Petkov | 2ccef01 | 2010-05-05 16:06:37 -0700 | [diff] [blame] | 210 | |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 211 | DeleteFrequencyCounters(); |
| 212 | ConfigureCrashFrequencyReporter(kMetricAnyCrashesDailyName); |
| 213 | ConfigureCrashFrequencyReporter(kMetricAnyCrashesDailyName); |
| 214 | ConfigureCrashFrequencyReporter(kMetricAnyCrashesWeeklyName); |
| 215 | ConfigureCrashFrequencyReporter(kMetricKernelCrashesDailyName); |
| 216 | ConfigureCrashFrequencyReporter(kMetricKernelCrashesWeeklyName); |
| 217 | ConfigureCrashFrequencyReporter(kMetricUncleanShutdownsDailyName); |
| 218 | ConfigureCrashFrequencyReporter(kMetricUncleanShutdownsWeeklyName); |
| 219 | ConfigureCrashFrequencyReporter(kMetricUserCrashesDailyName); |
| 220 | ConfigureCrashFrequencyReporter(kMetricUserCrashesWeeklyName); |
Darin Petkov | 38d5cb0 | 2010-06-24 12:10:26 -0700 | [diff] [blame] | 221 | |
Darin Petkov | 2ccef01 | 2010-05-05 16:06:37 -0700 | [diff] [blame] | 222 | // Don't setup D-Bus and GLib in test mode. |
| 223 | if (testing) |
| 224 | return; |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 225 | |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 226 | g_thread_init(NULL); |
| 227 | g_type_init(); |
| 228 | dbus_g_thread_init(); |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 229 | |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 230 | DBusError error; |
| 231 | dbus_error_init(&error); |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 232 | |
David James | 3b3add5 | 2010-06-04 15:01:19 -0700 | [diff] [blame] | 233 | DBusConnection* connection = dbus_bus_get(DBUS_BUS_SYSTEM, &error); |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 234 | LOG_IF(FATAL, dbus_error_is_set(&error)) << |
| 235 | "No D-Bus connection: " << SAFE_MESSAGE(error); |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 236 | |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 237 | dbus_connection_setup_with_g_main(connection, NULL); |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 238 | |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 239 | // Registers D-Bus matches for the signals we would like to catch. |
Darin Petkov | 1bb904e | 2010-06-16 15:58:06 -0700 | [diff] [blame] | 240 | for (unsigned int m = 0; m < arraysize(kDBusMatches_); m++) { |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 241 | const char* match = kDBusMatches_[m]; |
| 242 | DLOG(INFO) << "adding dbus match: " << match; |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 243 | dbus_bus_add_match(connection, match, &error); |
| 244 | LOG_IF(FATAL, dbus_error_is_set(&error)) << |
| 245 | "unable to add a match: " << SAFE_MESSAGE(error); |
| 246 | } |
| 247 | |
| 248 | // Adds the D-Bus filter routine to be called back whenever one of |
| 249 | // the registered D-Bus matches is successful. The daemon is not |
| 250 | // activated for D-Bus messages that don't match. |
| 251 | CHECK(dbus_connection_add_filter(connection, MessageFilter, this, NULL)); |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | void MetricsDaemon::Loop() { |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 255 | GMainLoop* loop = g_main_loop_new(NULL, false); |
| 256 | g_main_loop_run(loop); |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 257 | } |
| 258 | |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 259 | // static |
| 260 | DBusHandlerResult MetricsDaemon::MessageFilter(DBusConnection* connection, |
| 261 | DBusMessage* message, |
| 262 | void* user_data) { |
Darin Petkov | f27f036 | 2010-06-04 13:14:19 -0700 | [diff] [blame] | 263 | Time now = Time::Now(); |
| 264 | TimeTicks ticks = TimeTicks::Now(); |
| 265 | DLOG(INFO) << "message intercepted @ " << now.ToInternalValue(); |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 266 | |
| 267 | int message_type = dbus_message_get_type(message); |
| 268 | if (message_type != DBUS_MESSAGE_TYPE_SIGNAL) { |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 269 | DLOG(WARNING) << "unexpected message type " << message_type; |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 270 | return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
| 271 | } |
| 272 | |
| 273 | // Signal messages always have interfaces. |
| 274 | const char* interface = dbus_message_get_interface(message); |
| 275 | CHECK(interface != NULL); |
| 276 | |
| 277 | MetricsDaemon* daemon = static_cast<MetricsDaemon*>(user_data); |
| 278 | |
| 279 | DBusMessageIter iter; |
| 280 | dbus_message_iter_init(message, &iter); |
Darin Petkov | 1bb904e | 2010-06-16 15:58:06 -0700 | [diff] [blame] | 281 | if (strcmp(interface, DBUS_IFACE_CRASH_REPORTER) == 0) { |
| 282 | CHECK(strcmp(dbus_message_get_member(message), |
| 283 | "UserCrash") == 0); |
| 284 | daemon->ProcessUserCrash(); |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 285 | } else if (strcmp(interface, DBUS_IFACE_POWER_MANAGER) == 0) { |
David James | 6bf6e25 | 2010-06-06 18:52:40 -0700 | [diff] [blame] | 286 | const char* member = dbus_message_get_member(message); |
| 287 | if (strcmp(member, "ScreenIsLocked") == 0) { |
| 288 | daemon->SetUserActiveState(false, now); |
| 289 | } else if (strcmp(member, "ScreenIsUnlocked") == 0) { |
| 290 | daemon->SetUserActiveState(true, now); |
| 291 | } else if (strcmp(member, "PowerStateChanged") == 0) { |
| 292 | char* state_name; |
| 293 | dbus_message_iter_get_basic(&iter, &state_name); |
| 294 | daemon->PowerStateChanged(state_name, now); |
| 295 | } |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 296 | } else if (strcmp(interface, DBUS_IFACE_SESSION_MANAGER) == 0) { |
| 297 | CHECK(strcmp(dbus_message_get_member(message), |
| 298 | "SessionStateChanged") == 0); |
| 299 | |
David James | 3b3add5 | 2010-06-04 15:01:19 -0700 | [diff] [blame] | 300 | char* state_name; |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 301 | dbus_message_iter_get_basic(&iter, &state_name); |
| 302 | daemon->SessionStateChanged(state_name, now); |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 303 | } else { |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 304 | DLOG(WARNING) << "unexpected interface: " << interface; |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 305 | return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
| 306 | } |
| 307 | |
| 308 | return DBUS_HANDLER_RESULT_HANDLED; |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 309 | } |
| 310 | |
Darin Petkov | f27f036 | 2010-06-04 13:14:19 -0700 | [diff] [blame] | 311 | void MetricsDaemon::PowerStateChanged(const char* state_name, Time now) { |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 312 | DLOG(INFO) << "power state: " << state_name; |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 313 | power_state_ = LookupPowerState(state_name); |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 314 | |
| 315 | if (power_state_ != kPowerStateOn) |
| 316 | SetUserActiveState(false, now); |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | MetricsDaemon::PowerState |
| 320 | MetricsDaemon::LookupPowerState(const char* state_name) { |
| 321 | for (int i = 0; i < kNumberPowerStates; i++) { |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 322 | if (strcmp(state_name, kPowerStates_[i]) == 0) { |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 323 | return static_cast<PowerState>(i); |
| 324 | } |
| 325 | } |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 326 | DLOG(WARNING) << "unknown power state: " << state_name; |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 327 | return kUnknownPowerState; |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 328 | } |
| 329 | |
Darin Petkov | f27f036 | 2010-06-04 13:14:19 -0700 | [diff] [blame] | 330 | void MetricsDaemon::SessionStateChanged(const char* state_name, Time now) { |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 331 | DLOG(INFO) << "user session state: " << state_name; |
| 332 | session_state_ = LookupSessionState(state_name); |
| 333 | SetUserActiveState(session_state_ == kSessionStateStarted, now); |
| 334 | } |
| 335 | |
| 336 | MetricsDaemon::SessionState |
| 337 | MetricsDaemon::LookupSessionState(const char* state_name) { |
| 338 | for (int i = 0; i < kNumberSessionStates; i++) { |
| 339 | if (strcmp(state_name, kSessionStates_[i]) == 0) { |
| 340 | return static_cast<SessionState>(i); |
| 341 | } |
| 342 | } |
| 343 | DLOG(WARNING) << "unknown user session state: " << state_name; |
| 344 | return kUnknownSessionState; |
| 345 | } |
| 346 | |
Darin Petkov | f27f036 | 2010-06-04 13:14:19 -0700 | [diff] [blame] | 347 | void MetricsDaemon::SetUserActiveState(bool active, Time now) { |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 348 | DLOG(INFO) << "user: " << (active ? "active" : "inactive"); |
| 349 | |
| 350 | // Calculates the seconds of active use since the last update and |
Darin Petkov | f27f036 | 2010-06-04 13:14:19 -0700 | [diff] [blame] | 351 | // the day since Epoch, and logs the usage data. Guards against the |
| 352 | // time jumping back and forth due to the user changing it by |
| 353 | // discarding the new use time. |
| 354 | int seconds = 0; |
| 355 | if (user_active_ && now > user_active_last_) { |
| 356 | TimeDelta since_active = now - user_active_last_; |
| 357 | if (since_active < TimeDelta::FromSeconds( |
| 358 | kUseMonitorIntervalMax + kSecondsPerMinute)) { |
| 359 | seconds = static_cast<int>(since_active.InSeconds()); |
| 360 | } |
| 361 | } |
| 362 | TimeDelta since_epoch = now - Time(); |
| 363 | int day = since_epoch.InDays(); |
Darin Petkov | f1e85e4 | 2010-06-10 15:59:53 -0700 | [diff] [blame] | 364 | daily_use_->Update(day, seconds); |
Darin Petkov | 1bb904e | 2010-06-16 15:58:06 -0700 | [diff] [blame] | 365 | user_crash_interval_->Update(0, seconds); |
Darin Petkov | 38d5cb0 | 2010-06-24 12:10:26 -0700 | [diff] [blame] | 366 | kernel_crash_interval_->Update(0, seconds); |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 367 | |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 368 | // Flush finished cycles of all frequency counters. |
| 369 | for (FrequencyCounters::iterator i = frequency_counters_.begin(); |
| 370 | i != frequency_counters_.end(); ++i) { |
| 371 | i->second->FlushFinishedCycles(); |
| 372 | } |
| 373 | |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 374 | // Schedules a use monitor on inactive->active transitions and |
| 375 | // unschedules it on active->inactive transitions. |
| 376 | if (!user_active_ && active) |
| 377 | ScheduleUseMonitor(kUseMonitorIntervalInit, /* backoff */ false); |
| 378 | else if (user_active_ && !active) |
| 379 | UnscheduleUseMonitor(); |
| 380 | |
| 381 | // Remembers the current active state and the time of the last |
| 382 | // activity update. |
| 383 | user_active_ = active; |
| 384 | user_active_last_ = now; |
| 385 | } |
| 386 | |
Darin Petkov | 1bb904e | 2010-06-16 15:58:06 -0700 | [diff] [blame] | 387 | void MetricsDaemon::ProcessUserCrash() { |
| 388 | // Counts the active use time up to now. |
| 389 | SetUserActiveState(user_active_, Time::Now()); |
| 390 | |
| 391 | // Reports the active use time since the last crash and resets it. |
| 392 | user_crash_interval_->Flush(); |
Ken Mixter | ccd84c0 | 2010-08-16 19:57:13 -0700 | [diff] [blame] | 393 | |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 394 | frequency_counters_[kMetricUserCrashesDailyName]->Update(1); |
| 395 | frequency_counters_[kMetricUserCrashesWeeklyName]->Update(1); |
| 396 | frequency_counters_[kMetricAnyCrashesDailyName]->Update(1); |
| 397 | frequency_counters_[kMetricAnyCrashesWeeklyName]->Update(1); |
Darin Petkov | 1bb904e | 2010-06-16 15:58:06 -0700 | [diff] [blame] | 398 | } |
| 399 | |
Darin Petkov | 38d5cb0 | 2010-06-24 12:10:26 -0700 | [diff] [blame] | 400 | void MetricsDaemon::ProcessKernelCrash() { |
| 401 | // Counts the active use time up to now. |
| 402 | SetUserActiveState(user_active_, Time::Now()); |
| 403 | |
| 404 | // Reports the active use time since the last crash and resets it. |
| 405 | kernel_crash_interval_->Flush(); |
Ken Mixter | ccd84c0 | 2010-08-16 19:57:13 -0700 | [diff] [blame] | 406 | |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 407 | frequency_counters_[kMetricKernelCrashesDailyName]->Update(1); |
| 408 | frequency_counters_[kMetricKernelCrashesWeeklyName]->Update(1); |
| 409 | frequency_counters_[kMetricAnyCrashesDailyName]->Update(1); |
| 410 | frequency_counters_[kMetricAnyCrashesWeeklyName]->Update(1); |
Darin Petkov | 38d5cb0 | 2010-06-24 12:10:26 -0700 | [diff] [blame] | 411 | } |
| 412 | |
Ken Mixter | ccd84c0 | 2010-08-16 19:57:13 -0700 | [diff] [blame] | 413 | void MetricsDaemon::ProcessUncleanShutdown() { |
| 414 | // Counts the active use time up to now. |
| 415 | SetUserActiveState(user_active_, Time::Now()); |
| 416 | |
| 417 | // Reports the active use time since the last crash and resets it. |
| 418 | unclean_shutdown_interval_->Flush(); |
| 419 | |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 420 | frequency_counters_[kMetricUncleanShutdownsDailyName]->Update(1); |
| 421 | frequency_counters_[kMetricUncleanShutdownsWeeklyName]->Update(1); |
| 422 | frequency_counters_[kMetricAnyCrashesDailyName]->Update(1); |
| 423 | frequency_counters_[kMetricAnyCrashesWeeklyName]->Update(1); |
Ken Mixter | ccd84c0 | 2010-08-16 19:57:13 -0700 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | bool MetricsDaemon::CheckSystemCrash(const std::string& crash_file) { |
Darin Petkov | 38d5cb0 | 2010-06-24 12:10:26 -0700 | [diff] [blame] | 427 | FilePath crash_detected(crash_file); |
| 428 | if (!file_util::PathExists(crash_detected)) |
Ken Mixter | ccd84c0 | 2010-08-16 19:57:13 -0700 | [diff] [blame] | 429 | return false; |
Darin Petkov | 38d5cb0 | 2010-06-24 12:10:26 -0700 | [diff] [blame] | 430 | |
| 431 | // Deletes the crash-detected file so that the daemon doesn't report |
| 432 | // another kernel crash in case it's restarted. |
| 433 | file_util::Delete(crash_detected, |
| 434 | false); // recursive |
Ken Mixter | ccd84c0 | 2010-08-16 19:57:13 -0700 | [diff] [blame] | 435 | return true; |
Darin Petkov | 38d5cb0 | 2010-06-24 12:10:26 -0700 | [diff] [blame] | 436 | } |
| 437 | |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 438 | // static |
| 439 | gboolean MetricsDaemon::UseMonitorStatic(gpointer data) { |
| 440 | return static_cast<MetricsDaemon*>(data)->UseMonitor() ? TRUE : FALSE; |
| 441 | } |
| 442 | |
| 443 | bool MetricsDaemon::UseMonitor() { |
Darin Petkov | f27f036 | 2010-06-04 13:14:19 -0700 | [diff] [blame] | 444 | SetUserActiveState(user_active_, Time::Now()); |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 445 | |
| 446 | // If a new monitor source/instance is scheduled, returns false to |
| 447 | // tell GLib to destroy this monitor source/instance. Returns true |
| 448 | // otherwise to keep calling back this monitor. |
| 449 | return !ScheduleUseMonitor(usemon_interval_ * 2, /* backoff */ true); |
| 450 | } |
| 451 | |
| 452 | bool MetricsDaemon::ScheduleUseMonitor(int interval, bool backoff) |
| 453 | { |
Darin Petkov | 2ccef01 | 2010-05-05 16:06:37 -0700 | [diff] [blame] | 454 | if (testing_) |
| 455 | return false; |
| 456 | |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 457 | // Caps the interval -- the bigger the interval, the more active use |
| 458 | // time will be potentially dropped on system shutdown. |
| 459 | if (interval > kUseMonitorIntervalMax) |
| 460 | interval = kUseMonitorIntervalMax; |
| 461 | |
| 462 | if (backoff) { |
| 463 | // Back-off mode is used by the use monitor to reschedule itself |
| 464 | // with exponential back-off in time. This mode doesn't create a |
| 465 | // new timeout source if the new interval is the same as the old |
| 466 | // one. Also, if a new timeout source is created, the old one is |
| 467 | // not destroyed explicitly here -- it will be destroyed by GLib |
| 468 | // when the monitor returns FALSE (see UseMonitor and |
| 469 | // UseMonitorStatic). |
| 470 | if (interval == usemon_interval_) |
| 471 | return false; |
| 472 | } else { |
| 473 | UnscheduleUseMonitor(); |
| 474 | } |
| 475 | |
| 476 | // Schedules a new use monitor for |interval| seconds from now. |
| 477 | DLOG(INFO) << "scheduling use monitor in " << interval << " seconds"; |
| 478 | usemon_source_ = g_timeout_source_new_seconds(interval); |
| 479 | g_source_set_callback(usemon_source_, UseMonitorStatic, this, |
| 480 | NULL); // No destroy notification. |
| 481 | g_source_attach(usemon_source_, |
| 482 | NULL); // Default context. |
| 483 | usemon_interval_ = interval; |
| 484 | return true; |
| 485 | } |
| 486 | |
| 487 | void MetricsDaemon::UnscheduleUseMonitor() { |
| 488 | // If there's a use monitor scheduled already, destroys it. |
| 489 | if (usemon_source_ == NULL) |
| 490 | return; |
| 491 | |
| 492 | DLOG(INFO) << "destroying use monitor"; |
| 493 | g_source_destroy(usemon_source_); |
| 494 | usemon_source_ = NULL; |
| 495 | usemon_interval_ = 0; |
| 496 | } |
| 497 | |
Darin Petkov | f1e85e4 | 2010-06-10 15:59:53 -0700 | [diff] [blame] | 498 | // static |
Ken Mixter | ccd84c0 | 2010-08-16 19:57:13 -0700 | [diff] [blame] | 499 | void MetricsDaemon::ReportDailyUse(void* handle, int tag, int count) { |
Darin Petkov | 1bb904e | 2010-06-16 15:58:06 -0700 | [diff] [blame] | 500 | if (count <= 0) |
| 501 | return; |
| 502 | |
Darin Petkov | f1e85e4 | 2010-06-10 15:59:53 -0700 | [diff] [blame] | 503 | MetricsDaemon* daemon = static_cast<MetricsDaemon*>(handle); |
| 504 | int minutes = (count + kSecondsPerMinute / 2) / kSecondsPerMinute; |
| 505 | daemon->SendMetric(kMetricDailyUseTimeName, minutes, |
| 506 | kMetricDailyUseTimeMin, |
| 507 | kMetricDailyUseTimeMax, |
| 508 | kMetricDailyUseTimeBuckets); |
| 509 | } |
| 510 | |
Darin Petkov | 38d5cb0 | 2010-06-24 12:10:26 -0700 | [diff] [blame] | 511 | void MetricsDaemon::SendMetric(const string& name, int sample, |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 512 | int min, int max, int nbuckets) { |
Darin Petkov | fc91b42 | 2010-05-12 13:05:45 -0700 | [diff] [blame] | 513 | DLOG(INFO) << "received metric: " << name << " " << sample << " " |
| 514 | << min << " " << max << " " << nbuckets; |
| 515 | metrics_lib_->SendToUMA(name, sample, min, max, nbuckets); |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 516 | } |