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 | |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 7 | #include <dbus/dbus-glib-lowlevel.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> |
| 11 | |
Darin Petkov | f1e85e4 | 2010-06-10 15:59:53 -0700 | [diff] [blame] | 12 | #include "counter.h" |
| 13 | |
Darin Petkov | f27f036 | 2010-06-04 13:14:19 -0700 | [diff] [blame] | 14 | using base::Time; |
| 15 | using base::TimeDelta; |
| 16 | using base::TimeTicks; |
Darin Petkov | 38d5cb0 | 2010-06-24 12:10:26 -0700 | [diff] [blame^] | 17 | using std::string; |
Darin Petkov | f27f036 | 2010-06-04 13:14:19 -0700 | [diff] [blame] | 18 | |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 19 | #define SAFE_MESSAGE(e) (e.message ? e.message : "unknown error") |
Darin Petkov | 1bb904e | 2010-06-16 15:58:06 -0700 | [diff] [blame] | 20 | #define DBUS_IFACE_CRASH_REPORTER "org.chromium.CrashReporter" |
Darin Petkov | e334840 | 2010-06-04 14:07:41 -0700 | [diff] [blame] | 21 | #define DBUS_IFACE_FLIMFLAM_MANAGER "org.chromium.flimflam.Manager" |
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 | |
Darin Petkov | 2ccef01 | 2010-05-05 16:06:37 -0700 | [diff] [blame] | 42 | // static metrics parameters. |
| 43 | const char MetricsDaemon::kMetricDailyUseTimeName[] = |
| 44 | "Logging.DailyUseTime"; |
| 45 | const int MetricsDaemon::kMetricDailyUseTimeMin = 1; |
| 46 | const int MetricsDaemon::kMetricDailyUseTimeMax = kMinutesPerDay; |
| 47 | const int MetricsDaemon::kMetricDailyUseTimeBuckets = 50; |
| 48 | |
Darin Petkov | 38d5cb0 | 2010-06-24 12:10:26 -0700 | [diff] [blame^] | 49 | const char MetricsDaemon::kMetricKernelCrashIntervalName[] = |
| 50 | "Logging.KernelCrashInterval"; |
| 51 | const int MetricsDaemon::kMetricKernelCrashIntervalMin = 1; |
| 52 | const int MetricsDaemon::kMetricKernelCrashIntervalMax = 4 * kSecondsPerWeek; |
| 53 | const int MetricsDaemon::kMetricKernelCrashIntervalBuckets = 50; |
| 54 | |
Darin Petkov | 2ccef01 | 2010-05-05 16:06:37 -0700 | [diff] [blame] | 55 | const char MetricsDaemon::kMetricTimeToNetworkDropName[] = |
| 56 | "Network.TimeToDrop"; |
| 57 | const int MetricsDaemon::kMetricTimeToNetworkDropMin = 1; |
| 58 | const int MetricsDaemon::kMetricTimeToNetworkDropMax = |
| 59 | 8 /* hours */ * kMinutesPerHour * kSecondsPerMinute; |
| 60 | const int MetricsDaemon::kMetricTimeToNetworkDropBuckets = 50; |
| 61 | |
Darin Petkov | 1bb904e | 2010-06-16 15:58:06 -0700 | [diff] [blame] | 62 | const char MetricsDaemon::kMetricUserCrashIntervalName[] = |
| 63 | "Logging.UserCrashInterval"; |
| 64 | const int MetricsDaemon::kMetricUserCrashIntervalMin = 1; |
| 65 | const int MetricsDaemon::kMetricUserCrashIntervalMax = 4 * kSecondsPerWeek; |
| 66 | const int MetricsDaemon::kMetricUserCrashIntervalBuckets = 50; |
| 67 | |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 68 | // static |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 69 | const char* MetricsDaemon::kDBusMatches_[] = { |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 70 | "type='signal'," |
Darin Petkov | 1bb904e | 2010-06-16 15:58:06 -0700 | [diff] [blame] | 71 | "interface='" DBUS_IFACE_CRASH_REPORTER "'," |
| 72 | "path='/'," |
| 73 | "member='UserCrash'", |
| 74 | |
| 75 | "type='signal'," |
Darin Petkov | e334840 | 2010-06-04 14:07:41 -0700 | [diff] [blame] | 76 | "sender='org.chromium.flimflam'," |
| 77 | "interface='" DBUS_IFACE_FLIMFLAM_MANAGER "'," |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 78 | "path='/'," |
| 79 | "member='StateChanged'", |
| 80 | |
| 81 | "type='signal'," |
| 82 | "interface='" DBUS_IFACE_POWER_MANAGER "'," |
Benson Leung | 53faeb0 | 2010-06-08 15:59:13 -0700 | [diff] [blame] | 83 | "path='/'", |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 84 | |
| 85 | "type='signal'," |
| 86 | "sender='org.chromium.SessionManager'," |
| 87 | "interface='" DBUS_IFACE_SESSION_MANAGER "'," |
| 88 | "path='/org/chromium/SessionManager'," |
| 89 | "member='SessionStateChanged'", |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 90 | }; |
| 91 | |
| 92 | // static |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 93 | const char* MetricsDaemon::kNetworkStates_[] = { |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 94 | #define STATE(name, capname) #name, |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 95 | #include "network_states.h" |
| 96 | }; |
| 97 | |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 98 | // static |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 99 | const char* MetricsDaemon::kPowerStates_[] = { |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 100 | #define STATE(name, capname) #name, |
| 101 | #include "power_states.h" |
| 102 | }; |
| 103 | |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 104 | // static |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 105 | const char* MetricsDaemon::kSessionStates_[] = { |
| 106 | #define STATE(name, capname) #name, |
| 107 | #include "session_states.h" |
| 108 | }; |
| 109 | |
Darin Petkov | 38d5cb0 | 2010-06-24 12:10:26 -0700 | [diff] [blame^] | 110 | // Invokes a remote method over D-Bus that takes no input arguments |
| 111 | // and returns a string result. The method call is issued with a 2 |
| 112 | // second blocking timeout. Returns an empty string on failure or |
| 113 | // timeout. |
| 114 | static string DBusGetString(DBusConnection* connection, |
| 115 | const string& destination, |
| 116 | const string& path, |
| 117 | const string& interface, |
| 118 | const string& method) { |
| 119 | DBusMessage* message = |
| 120 | dbus_message_new_method_call(destination.c_str(), |
| 121 | path.c_str(), |
| 122 | interface.c_str(), |
| 123 | method.c_str()); |
| 124 | if (!message) { |
| 125 | DLOG(WARNING) << "DBusGetString: unable to allocate a message"; |
| 126 | return ""; |
| 127 | } |
| 128 | |
| 129 | DBusError error; |
| 130 | dbus_error_init(&error); |
| 131 | const int kTimeout = 2000; // ms |
| 132 | DLOG(INFO) << "DBusGetString: dest=" << destination << " path=" << path |
| 133 | << " iface=" << interface << " method=" << method; |
| 134 | DBusMessage* reply = |
| 135 | dbus_connection_send_with_reply_and_block(connection, message, kTimeout, |
| 136 | &error); |
| 137 | dbus_message_unref(message); |
| 138 | if (dbus_error_is_set(&error) || !reply) { |
| 139 | DLOG(WARNING) << "DBusGetString: call failed"; |
| 140 | return ""; |
| 141 | } |
| 142 | DBusMessageIter iter; |
| 143 | dbus_message_iter_init(reply, &iter); |
| 144 | if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING) { |
| 145 | NOTREACHED(); |
| 146 | dbus_message_unref(reply); |
| 147 | return ""; |
| 148 | } |
| 149 | const char* c_result = ""; |
| 150 | dbus_message_iter_get_basic(&iter, &c_result); |
| 151 | string result = c_result; |
| 152 | DLOG(INFO) << "DBusGetString: result=" << result; |
| 153 | dbus_message_unref(reply); |
| 154 | return result; |
| 155 | } |
| 156 | |
Darin Petkov | f1e85e4 | 2010-06-10 15:59:53 -0700 | [diff] [blame] | 157 | MetricsDaemon::MetricsDaemon() |
| 158 | : network_state_(kUnknownNetworkState), |
| 159 | power_state_(kUnknownPowerState), |
| 160 | session_state_(kUnknownSessionState), |
| 161 | user_active_(false), |
| 162 | usemon_interval_(0), |
| 163 | usemon_source_(NULL) {} |
| 164 | |
| 165 | MetricsDaemon::~MetricsDaemon() {} |
| 166 | |
Darin Petkov | 2ccef01 | 2010-05-05 16:06:37 -0700 | [diff] [blame] | 167 | void MetricsDaemon::Run(bool run_as_daemon) { |
Darin Petkov | 38d5cb0 | 2010-06-24 12:10:26 -0700 | [diff] [blame^] | 168 | if (run_as_daemon && daemon(0, 0) != 0) |
| 169 | return; |
| 170 | |
| 171 | static const char kKernelCrashDetectedFile[] = "/tmp/kernel-crash-detected"; |
| 172 | CheckKernelCrash(kKernelCrashDetectedFile); |
| 173 | Loop(); |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Darin Petkov | fc91b42 | 2010-05-12 13:05:45 -0700 | [diff] [blame] | 176 | void MetricsDaemon::Init(bool testing, MetricsLibraryInterface* metrics_lib) { |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 177 | testing_ = testing; |
Darin Petkov | fc91b42 | 2010-05-12 13:05:45 -0700 | [diff] [blame] | 178 | DCHECK(metrics_lib != NULL); |
| 179 | metrics_lib_ = metrics_lib; |
Darin Petkov | 38d5cb0 | 2010-06-24 12:10:26 -0700 | [diff] [blame^] | 180 | |
| 181 | static const char kDailyUseRecordFile[] = "/var/log/metrics/daily-usage"; |
Darin Petkov | f1e85e4 | 2010-06-10 15:59:53 -0700 | [diff] [blame] | 182 | daily_use_.reset(new chromeos_metrics::TaggedCounter()); |
| 183 | daily_use_->Init(kDailyUseRecordFile, &DailyUseReporter, this); |
Darin Petkov | 38d5cb0 | 2010-06-24 12:10:26 -0700 | [diff] [blame^] | 184 | |
| 185 | static const char kUserCrashIntervalRecordFile[] = |
| 186 | "/var/log/metrics/user-crash-interval"; |
Darin Petkov | 1bb904e | 2010-06-16 15:58:06 -0700 | [diff] [blame] | 187 | user_crash_interval_.reset(new chromeos_metrics::TaggedCounter()); |
| 188 | user_crash_interval_->Init(kUserCrashIntervalRecordFile, |
| 189 | &UserCrashIntervalReporter, this); |
Darin Petkov | 2ccef01 | 2010-05-05 16:06:37 -0700 | [diff] [blame] | 190 | |
Darin Petkov | 38d5cb0 | 2010-06-24 12:10:26 -0700 | [diff] [blame^] | 191 | static const char kKernelCrashIntervalRecordFile[] = |
| 192 | "/var/log/metrics/kernel-crash-interval"; |
| 193 | kernel_crash_interval_.reset(new chromeos_metrics::TaggedCounter()); |
| 194 | kernel_crash_interval_->Init(kKernelCrashIntervalRecordFile, |
| 195 | &KernelCrashIntervalReporter, this); |
| 196 | |
Darin Petkov | 2ccef01 | 2010-05-05 16:06:37 -0700 | [diff] [blame] | 197 | // Don't setup D-Bus and GLib in test mode. |
| 198 | if (testing) |
| 199 | return; |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 200 | |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 201 | g_thread_init(NULL); |
| 202 | g_type_init(); |
| 203 | dbus_g_thread_init(); |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 204 | |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 205 | DBusError error; |
| 206 | dbus_error_init(&error); |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 207 | |
David James | 3b3add5 | 2010-06-04 15:01:19 -0700 | [diff] [blame] | 208 | DBusConnection* connection = dbus_bus_get(DBUS_BUS_SYSTEM, &error); |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 209 | LOG_IF(FATAL, dbus_error_is_set(&error)) << |
| 210 | "No D-Bus connection: " << SAFE_MESSAGE(error); |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 211 | |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 212 | dbus_connection_setup_with_g_main(connection, NULL); |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 213 | |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 214 | // Registers D-Bus matches for the signals we would like to catch. |
Darin Petkov | 1bb904e | 2010-06-16 15:58:06 -0700 | [diff] [blame] | 215 | for (unsigned int m = 0; m < arraysize(kDBusMatches_); m++) { |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 216 | const char* match = kDBusMatches_[m]; |
| 217 | DLOG(INFO) << "adding dbus match: " << match; |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 218 | dbus_bus_add_match(connection, match, &error); |
| 219 | LOG_IF(FATAL, dbus_error_is_set(&error)) << |
| 220 | "unable to add a match: " << SAFE_MESSAGE(error); |
| 221 | } |
| 222 | |
| 223 | // Adds the D-Bus filter routine to be called back whenever one of |
| 224 | // the registered D-Bus matches is successful. The daemon is not |
| 225 | // activated for D-Bus messages that don't match. |
| 226 | CHECK(dbus_connection_add_filter(connection, MessageFilter, this, NULL)); |
Darin Petkov | 38d5cb0 | 2010-06-24 12:10:26 -0700 | [diff] [blame^] | 227 | |
| 228 | // Initializes the current network state by retrieving it from flimflam. |
| 229 | string state_name = DBusGetString(connection, "org.chromium.flimflam", "/", |
| 230 | DBUS_IFACE_FLIMFLAM_MANAGER, "GetState"); |
| 231 | NetStateChanged(state_name.c_str(), TimeTicks::Now()); |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | void MetricsDaemon::Loop() { |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 235 | GMainLoop* loop = g_main_loop_new(NULL, false); |
| 236 | g_main_loop_run(loop); |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 237 | } |
| 238 | |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 239 | // static |
| 240 | DBusHandlerResult MetricsDaemon::MessageFilter(DBusConnection* connection, |
| 241 | DBusMessage* message, |
| 242 | void* user_data) { |
Darin Petkov | f27f036 | 2010-06-04 13:14:19 -0700 | [diff] [blame] | 243 | Time now = Time::Now(); |
| 244 | TimeTicks ticks = TimeTicks::Now(); |
| 245 | DLOG(INFO) << "message intercepted @ " << now.ToInternalValue(); |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 246 | |
| 247 | int message_type = dbus_message_get_type(message); |
| 248 | if (message_type != DBUS_MESSAGE_TYPE_SIGNAL) { |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 249 | DLOG(WARNING) << "unexpected message type " << message_type; |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 250 | return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
| 251 | } |
| 252 | |
| 253 | // Signal messages always have interfaces. |
| 254 | const char* interface = dbus_message_get_interface(message); |
| 255 | CHECK(interface != NULL); |
| 256 | |
| 257 | MetricsDaemon* daemon = static_cast<MetricsDaemon*>(user_data); |
| 258 | |
| 259 | DBusMessageIter iter; |
| 260 | dbus_message_iter_init(message, &iter); |
Darin Petkov | 1bb904e | 2010-06-16 15:58:06 -0700 | [diff] [blame] | 261 | if (strcmp(interface, DBUS_IFACE_CRASH_REPORTER) == 0) { |
| 262 | CHECK(strcmp(dbus_message_get_member(message), |
| 263 | "UserCrash") == 0); |
| 264 | daemon->ProcessUserCrash(); |
| 265 | } else if (strcmp(interface, DBUS_IFACE_FLIMFLAM_MANAGER) == 0) { |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 266 | CHECK(strcmp(dbus_message_get_member(message), |
| 267 | "StateChanged") == 0); |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 268 | |
David James | 3b3add5 | 2010-06-04 15:01:19 -0700 | [diff] [blame] | 269 | char* state_name; |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 270 | dbus_message_iter_get_basic(&iter, &state_name); |
Darin Petkov | f27f036 | 2010-06-04 13:14:19 -0700 | [diff] [blame] | 271 | daemon->NetStateChanged(state_name, ticks); |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 272 | } else if (strcmp(interface, DBUS_IFACE_POWER_MANAGER) == 0) { |
David James | 6bf6e25 | 2010-06-06 18:52:40 -0700 | [diff] [blame] | 273 | const char* member = dbus_message_get_member(message); |
| 274 | if (strcmp(member, "ScreenIsLocked") == 0) { |
| 275 | daemon->SetUserActiveState(false, now); |
| 276 | } else if (strcmp(member, "ScreenIsUnlocked") == 0) { |
| 277 | daemon->SetUserActiveState(true, now); |
| 278 | } else if (strcmp(member, "PowerStateChanged") == 0) { |
| 279 | char* state_name; |
| 280 | dbus_message_iter_get_basic(&iter, &state_name); |
| 281 | daemon->PowerStateChanged(state_name, now); |
| 282 | } |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 283 | } else if (strcmp(interface, DBUS_IFACE_SESSION_MANAGER) == 0) { |
| 284 | CHECK(strcmp(dbus_message_get_member(message), |
| 285 | "SessionStateChanged") == 0); |
| 286 | |
David James | 3b3add5 | 2010-06-04 15:01:19 -0700 | [diff] [blame] | 287 | char* state_name; |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 288 | dbus_message_iter_get_basic(&iter, &state_name); |
| 289 | daemon->SessionStateChanged(state_name, now); |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 290 | } else { |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 291 | DLOG(WARNING) << "unexpected interface: " << interface; |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 292 | return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
| 293 | } |
| 294 | |
| 295 | return DBUS_HANDLER_RESULT_HANDLED; |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 296 | } |
| 297 | |
Darin Petkov | f27f036 | 2010-06-04 13:14:19 -0700 | [diff] [blame] | 298 | void MetricsDaemon::NetStateChanged(const char* state_name, TimeTicks ticks) { |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 299 | DLOG(INFO) << "network state: " << state_name; |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 300 | |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 301 | NetworkState state = LookupNetworkState(state_name); |
| 302 | |
| 303 | // Logs the time in seconds between the network going online to |
Darin Petkov | 2ccef01 | 2010-05-05 16:06:37 -0700 | [diff] [blame] | 304 | // going offline (or, more precisely, going not online) in order to |
| 305 | // measure the mean time to network dropping. Going offline as part |
| 306 | // of suspend-to-RAM is not logged as network drop -- the assumption |
| 307 | // is that the message for suspend-to-RAM comes before the network |
| 308 | // offline message which seems to and should be the case. |
| 309 | if (state != kNetworkStateOnline && |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 310 | network_state_ == kNetworkStateOnline && |
| 311 | power_state_ != kPowerStateMem) { |
Darin Petkov | f27f036 | 2010-06-04 13:14:19 -0700 | [diff] [blame] | 312 | TimeDelta since_online = ticks - network_state_last_; |
| 313 | int online_time = static_cast<int>(since_online.InSeconds()); |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 314 | SendMetric(kMetricTimeToNetworkDropName, online_time, |
| 315 | kMetricTimeToNetworkDropMin, |
| 316 | kMetricTimeToNetworkDropMax, |
| 317 | kMetricTimeToNetworkDropBuckets); |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 318 | } |
| 319 | |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 320 | network_state_ = state; |
Darin Petkov | f27f036 | 2010-06-04 13:14:19 -0700 | [diff] [blame] | 321 | network_state_last_ = ticks; |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 322 | } |
| 323 | |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 324 | MetricsDaemon::NetworkState |
| 325 | MetricsDaemon::LookupNetworkState(const char* state_name) { |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 326 | for (int i = 0; i < kNumberNetworkStates; i++) { |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 327 | if (strcmp(state_name, kNetworkStates_[i]) == 0) { |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 328 | return static_cast<NetworkState>(i); |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 329 | } |
| 330 | } |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 331 | DLOG(WARNING) << "unknown network connection state: " << state_name; |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 332 | return kUnknownNetworkState; |
| 333 | } |
| 334 | |
Darin Petkov | f27f036 | 2010-06-04 13:14:19 -0700 | [diff] [blame] | 335 | void MetricsDaemon::PowerStateChanged(const char* state_name, Time now) { |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 336 | DLOG(INFO) << "power state: " << state_name; |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 337 | power_state_ = LookupPowerState(state_name); |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 338 | |
| 339 | if (power_state_ != kPowerStateOn) |
| 340 | SetUserActiveState(false, now); |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | MetricsDaemon::PowerState |
| 344 | MetricsDaemon::LookupPowerState(const char* state_name) { |
| 345 | for (int i = 0; i < kNumberPowerStates; i++) { |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 346 | if (strcmp(state_name, kPowerStates_[i]) == 0) { |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 347 | return static_cast<PowerState>(i); |
| 348 | } |
| 349 | } |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 350 | DLOG(WARNING) << "unknown power state: " << state_name; |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 351 | return kUnknownPowerState; |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 352 | } |
| 353 | |
Darin Petkov | f27f036 | 2010-06-04 13:14:19 -0700 | [diff] [blame] | 354 | void MetricsDaemon::SessionStateChanged(const char* state_name, Time now) { |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 355 | DLOG(INFO) << "user session state: " << state_name; |
| 356 | session_state_ = LookupSessionState(state_name); |
| 357 | SetUserActiveState(session_state_ == kSessionStateStarted, now); |
| 358 | } |
| 359 | |
| 360 | MetricsDaemon::SessionState |
| 361 | MetricsDaemon::LookupSessionState(const char* state_name) { |
| 362 | for (int i = 0; i < kNumberSessionStates; i++) { |
| 363 | if (strcmp(state_name, kSessionStates_[i]) == 0) { |
| 364 | return static_cast<SessionState>(i); |
| 365 | } |
| 366 | } |
| 367 | DLOG(WARNING) << "unknown user session state: " << state_name; |
| 368 | return kUnknownSessionState; |
| 369 | } |
| 370 | |
Darin Petkov | f27f036 | 2010-06-04 13:14:19 -0700 | [diff] [blame] | 371 | void MetricsDaemon::SetUserActiveState(bool active, Time now) { |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 372 | DLOG(INFO) << "user: " << (active ? "active" : "inactive"); |
| 373 | |
| 374 | // Calculates the seconds of active use since the last update and |
Darin Petkov | f27f036 | 2010-06-04 13:14:19 -0700 | [diff] [blame] | 375 | // the day since Epoch, and logs the usage data. Guards against the |
| 376 | // time jumping back and forth due to the user changing it by |
| 377 | // discarding the new use time. |
| 378 | int seconds = 0; |
| 379 | if (user_active_ && now > user_active_last_) { |
| 380 | TimeDelta since_active = now - user_active_last_; |
| 381 | if (since_active < TimeDelta::FromSeconds( |
| 382 | kUseMonitorIntervalMax + kSecondsPerMinute)) { |
| 383 | seconds = static_cast<int>(since_active.InSeconds()); |
| 384 | } |
| 385 | } |
| 386 | TimeDelta since_epoch = now - Time(); |
| 387 | int day = since_epoch.InDays(); |
Darin Petkov | f1e85e4 | 2010-06-10 15:59:53 -0700 | [diff] [blame] | 388 | daily_use_->Update(day, seconds); |
Darin Petkov | 1bb904e | 2010-06-16 15:58:06 -0700 | [diff] [blame] | 389 | user_crash_interval_->Update(0, seconds); |
Darin Petkov | 38d5cb0 | 2010-06-24 12:10:26 -0700 | [diff] [blame^] | 390 | kernel_crash_interval_->Update(0, seconds); |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 391 | |
| 392 | // Schedules a use monitor on inactive->active transitions and |
| 393 | // unschedules it on active->inactive transitions. |
| 394 | if (!user_active_ && active) |
| 395 | ScheduleUseMonitor(kUseMonitorIntervalInit, /* backoff */ false); |
| 396 | else if (user_active_ && !active) |
| 397 | UnscheduleUseMonitor(); |
| 398 | |
| 399 | // Remembers the current active state and the time of the last |
| 400 | // activity update. |
| 401 | user_active_ = active; |
| 402 | user_active_last_ = now; |
| 403 | } |
| 404 | |
Darin Petkov | 1bb904e | 2010-06-16 15:58:06 -0700 | [diff] [blame] | 405 | void MetricsDaemon::ProcessUserCrash() { |
| 406 | // Counts the active use time up to now. |
| 407 | SetUserActiveState(user_active_, Time::Now()); |
| 408 | |
| 409 | // Reports the active use time since the last crash and resets it. |
| 410 | user_crash_interval_->Flush(); |
| 411 | } |
| 412 | |
Darin Petkov | 38d5cb0 | 2010-06-24 12:10:26 -0700 | [diff] [blame^] | 413 | void MetricsDaemon::ProcessKernelCrash() { |
| 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 | kernel_crash_interval_->Flush(); |
| 419 | } |
| 420 | |
| 421 | void MetricsDaemon::CheckKernelCrash(const std::string& crash_file) { |
| 422 | FilePath crash_detected(crash_file); |
| 423 | if (!file_util::PathExists(crash_detected)) |
| 424 | return; |
| 425 | |
| 426 | ProcessKernelCrash(); |
| 427 | |
| 428 | // Deletes the crash-detected file so that the daemon doesn't report |
| 429 | // another kernel crash in case it's restarted. |
| 430 | file_util::Delete(crash_detected, |
| 431 | false); // recursive |
| 432 | } |
| 433 | |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 434 | // static |
| 435 | gboolean MetricsDaemon::UseMonitorStatic(gpointer data) { |
| 436 | return static_cast<MetricsDaemon*>(data)->UseMonitor() ? TRUE : FALSE; |
| 437 | } |
| 438 | |
| 439 | bool MetricsDaemon::UseMonitor() { |
Darin Petkov | f27f036 | 2010-06-04 13:14:19 -0700 | [diff] [blame] | 440 | SetUserActiveState(user_active_, Time::Now()); |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 441 | |
| 442 | // If a new monitor source/instance is scheduled, returns false to |
| 443 | // tell GLib to destroy this monitor source/instance. Returns true |
| 444 | // otherwise to keep calling back this monitor. |
| 445 | return !ScheduleUseMonitor(usemon_interval_ * 2, /* backoff */ true); |
| 446 | } |
| 447 | |
| 448 | bool MetricsDaemon::ScheduleUseMonitor(int interval, bool backoff) |
| 449 | { |
Darin Petkov | 2ccef01 | 2010-05-05 16:06:37 -0700 | [diff] [blame] | 450 | if (testing_) |
| 451 | return false; |
| 452 | |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 453 | // Caps the interval -- the bigger the interval, the more active use |
| 454 | // time will be potentially dropped on system shutdown. |
| 455 | if (interval > kUseMonitorIntervalMax) |
| 456 | interval = kUseMonitorIntervalMax; |
| 457 | |
| 458 | if (backoff) { |
| 459 | // Back-off mode is used by the use monitor to reschedule itself |
| 460 | // with exponential back-off in time. This mode doesn't create a |
| 461 | // new timeout source if the new interval is the same as the old |
| 462 | // one. Also, if a new timeout source is created, the old one is |
| 463 | // not destroyed explicitly here -- it will be destroyed by GLib |
| 464 | // when the monitor returns FALSE (see UseMonitor and |
| 465 | // UseMonitorStatic). |
| 466 | if (interval == usemon_interval_) |
| 467 | return false; |
| 468 | } else { |
| 469 | UnscheduleUseMonitor(); |
| 470 | } |
| 471 | |
| 472 | // Schedules a new use monitor for |interval| seconds from now. |
| 473 | DLOG(INFO) << "scheduling use monitor in " << interval << " seconds"; |
| 474 | usemon_source_ = g_timeout_source_new_seconds(interval); |
| 475 | g_source_set_callback(usemon_source_, UseMonitorStatic, this, |
| 476 | NULL); // No destroy notification. |
| 477 | g_source_attach(usemon_source_, |
| 478 | NULL); // Default context. |
| 479 | usemon_interval_ = interval; |
| 480 | return true; |
| 481 | } |
| 482 | |
| 483 | void MetricsDaemon::UnscheduleUseMonitor() { |
| 484 | // If there's a use monitor scheduled already, destroys it. |
| 485 | if (usemon_source_ == NULL) |
| 486 | return; |
| 487 | |
| 488 | DLOG(INFO) << "destroying use monitor"; |
| 489 | g_source_destroy(usemon_source_); |
| 490 | usemon_source_ = NULL; |
| 491 | usemon_interval_ = 0; |
| 492 | } |
| 493 | |
Darin Petkov | f1e85e4 | 2010-06-10 15:59:53 -0700 | [diff] [blame] | 494 | // static |
| 495 | void MetricsDaemon::DailyUseReporter(void* handle, int tag, int count) { |
Darin Petkov | 1bb904e | 2010-06-16 15:58:06 -0700 | [diff] [blame] | 496 | if (count <= 0) |
| 497 | return; |
| 498 | |
Darin Petkov | f1e85e4 | 2010-06-10 15:59:53 -0700 | [diff] [blame] | 499 | MetricsDaemon* daemon = static_cast<MetricsDaemon*>(handle); |
| 500 | int minutes = (count + kSecondsPerMinute / 2) / kSecondsPerMinute; |
| 501 | daemon->SendMetric(kMetricDailyUseTimeName, minutes, |
| 502 | kMetricDailyUseTimeMin, |
| 503 | kMetricDailyUseTimeMax, |
| 504 | kMetricDailyUseTimeBuckets); |
| 505 | } |
| 506 | |
Darin Petkov | 1bb904e | 2010-06-16 15:58:06 -0700 | [diff] [blame] | 507 | // static |
| 508 | void MetricsDaemon::UserCrashIntervalReporter(void* handle, |
| 509 | int tag, int count) { |
| 510 | MetricsDaemon* daemon = static_cast<MetricsDaemon*>(handle); |
| 511 | daemon->SendMetric(kMetricUserCrashIntervalName, count, |
| 512 | kMetricUserCrashIntervalMin, |
| 513 | kMetricUserCrashIntervalMax, |
| 514 | kMetricUserCrashIntervalBuckets); |
| 515 | } |
| 516 | |
Darin Petkov | 38d5cb0 | 2010-06-24 12:10:26 -0700 | [diff] [blame^] | 517 | // static |
| 518 | void MetricsDaemon::KernelCrashIntervalReporter(void* handle, |
| 519 | int tag, int count) { |
| 520 | MetricsDaemon* daemon = static_cast<MetricsDaemon*>(handle); |
| 521 | daemon->SendMetric(kMetricKernelCrashIntervalName, count, |
| 522 | kMetricKernelCrashIntervalMin, |
| 523 | kMetricKernelCrashIntervalMax, |
| 524 | kMetricKernelCrashIntervalBuckets); |
| 525 | } |
| 526 | |
| 527 | void MetricsDaemon::SendMetric(const string& name, int sample, |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 528 | int min, int max, int nbuckets) { |
Darin Petkov | fc91b42 | 2010-05-12 13:05:45 -0700 | [diff] [blame] | 529 | DLOG(INFO) << "received metric: " << name << " " << sample << " " |
| 530 | << min << " " << max << " " << nbuckets; |
| 531 | metrics_lib_->SendToUMA(name, sample, min, max, nbuckets); |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 532 | } |