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 | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 8 | #include <sys/file.h> |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 9 | |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 10 | #include <base/eintr_wrapper.h> |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 11 | #include <base/logging.h> |
| 12 | |
Darin Petkov | f27f036 | 2010-06-04 13:14:19 -0700 | [diff] [blame] | 13 | using base::Time; |
| 14 | using base::TimeDelta; |
| 15 | using base::TimeTicks; |
| 16 | |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 17 | #define SAFE_MESSAGE(e) (e.message ? e.message : "unknown error") |
Darin Petkov | e334840 | 2010-06-04 14:07:41 -0700 | [diff] [blame] | 18 | #define DBUS_IFACE_FLIMFLAM_MANAGER "org.chromium.flimflam.Manager" |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 19 | #define DBUS_IFACE_POWER_MANAGER "org.chromium.Power.Manager" |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 20 | #define DBUS_IFACE_SCREENSAVER_MANAGER "org.chromium.ScreenSaver.Manager" |
| 21 | #define DBUS_IFACE_SESSION_MANAGER "org.chromium.SessionManagerInterface" |
| 22 | |
| 23 | // File to aggregate daily usage before sending to UMA. |
| 24 | // TODO(petkov): This file should probably live in a user-specific stateful |
| 25 | // location, e.g., /home/chronos/user. |
| 26 | static const char kDailyUseRecordFile[] = "/var/log/metrics/daily-usage"; |
| 27 | |
| 28 | static const int kSecondsPerMinute = 60; |
| 29 | static const int kMinutesPerHour = 60; |
| 30 | static const int kHoursPerDay = 24; |
| 31 | static const int kMinutesPerDay = kHoursPerDay * kMinutesPerHour; |
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 | |
| 49 | const char MetricsDaemon::kMetricTimeToNetworkDropName[] = |
| 50 | "Network.TimeToDrop"; |
| 51 | const int MetricsDaemon::kMetricTimeToNetworkDropMin = 1; |
| 52 | const int MetricsDaemon::kMetricTimeToNetworkDropMax = |
| 53 | 8 /* hours */ * kMinutesPerHour * kSecondsPerMinute; |
| 54 | const int MetricsDaemon::kMetricTimeToNetworkDropBuckets = 50; |
| 55 | |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 56 | // static |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 57 | const char* MetricsDaemon::kDBusMatches_[] = { |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 58 | "type='signal'," |
Darin Petkov | e334840 | 2010-06-04 14:07:41 -0700 | [diff] [blame] | 59 | "sender='org.chromium.flimflam'," |
| 60 | "interface='" DBUS_IFACE_FLIMFLAM_MANAGER "'," |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 61 | "path='/'," |
| 62 | "member='StateChanged'", |
| 63 | |
| 64 | "type='signal'," |
| 65 | "interface='" DBUS_IFACE_POWER_MANAGER "'," |
| 66 | "path='/'," |
| 67 | "member='PowerStateChanged'", |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 68 | |
| 69 | "type='signal'," |
| 70 | "interface='" DBUS_IFACE_SCREENSAVER_MANAGER "'," |
| 71 | "path='/'," |
| 72 | "member='LockStateChanged'", |
| 73 | |
| 74 | "type='signal'," |
| 75 | "sender='org.chromium.SessionManager'," |
| 76 | "interface='" DBUS_IFACE_SESSION_MANAGER "'," |
| 77 | "path='/org/chromium/SessionManager'," |
| 78 | "member='SessionStateChanged'", |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 79 | }; |
| 80 | |
| 81 | // static |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 82 | const char* MetricsDaemon::kNetworkStates_[] = { |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 83 | #define STATE(name, capname) #name, |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 84 | #include "network_states.h" |
| 85 | }; |
| 86 | |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 87 | // static |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 88 | const char* MetricsDaemon::kPowerStates_[] = { |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 89 | #define STATE(name, capname) #name, |
| 90 | #include "power_states.h" |
| 91 | }; |
| 92 | |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 93 | // static |
| 94 | const char* MetricsDaemon::kScreenSaverStates_[] = { |
| 95 | #define STATE(name, capname) #name, |
| 96 | #include "screensaver_states.h" |
| 97 | }; |
| 98 | |
| 99 | // static |
| 100 | const char* MetricsDaemon::kSessionStates_[] = { |
| 101 | #define STATE(name, capname) #name, |
| 102 | #include "session_states.h" |
| 103 | }; |
| 104 | |
Darin Petkov | 2ccef01 | 2010-05-05 16:06:37 -0700 | [diff] [blame] | 105 | void MetricsDaemon::Run(bool run_as_daemon) { |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 106 | if (!run_as_daemon || daemon(0, 0) == 0) { |
| 107 | Loop(); |
| 108 | } |
| 109 | } |
| 110 | |
Darin Petkov | fc91b42 | 2010-05-12 13:05:45 -0700 | [diff] [blame] | 111 | void MetricsDaemon::Init(bool testing, MetricsLibraryInterface* metrics_lib) { |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 112 | testing_ = testing; |
Darin Petkov | fc91b42 | 2010-05-12 13:05:45 -0700 | [diff] [blame] | 113 | DCHECK(metrics_lib != NULL); |
| 114 | metrics_lib_ = metrics_lib; |
Darin Petkov | 2ccef01 | 2010-05-05 16:06:37 -0700 | [diff] [blame] | 115 | daily_use_record_file_ = kDailyUseRecordFile; |
| 116 | |
| 117 | // Don't setup D-Bus and GLib in test mode. |
| 118 | if (testing) |
| 119 | return; |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 120 | |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 121 | g_thread_init(NULL); |
| 122 | g_type_init(); |
| 123 | dbus_g_thread_init(); |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 124 | |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 125 | DBusError error; |
| 126 | dbus_error_init(&error); |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 127 | |
David James | 3b3add5 | 2010-06-04 15:01:19 -0700 | [diff] [blame^] | 128 | DBusConnection* connection = dbus_bus_get(DBUS_BUS_SYSTEM, &error); |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 129 | LOG_IF(FATAL, dbus_error_is_set(&error)) << |
| 130 | "No D-Bus connection: " << SAFE_MESSAGE(error); |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 131 | |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 132 | dbus_connection_setup_with_g_main(connection, NULL); |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 133 | |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 134 | // Registers D-Bus matches for the signals we would like to catch. |
David James | 3b3add5 | 2010-06-04 15:01:19 -0700 | [diff] [blame^] | 135 | for (unsigned int m = 0; m < sizeof(kDBusMatches_) / sizeof(char*); m++) { |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 136 | const char* match = kDBusMatches_[m]; |
| 137 | DLOG(INFO) << "adding dbus match: " << match; |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 138 | dbus_bus_add_match(connection, match, &error); |
| 139 | LOG_IF(FATAL, dbus_error_is_set(&error)) << |
| 140 | "unable to add a match: " << SAFE_MESSAGE(error); |
| 141 | } |
| 142 | |
| 143 | // Adds the D-Bus filter routine to be called back whenever one of |
| 144 | // the registered D-Bus matches is successful. The daemon is not |
| 145 | // activated for D-Bus messages that don't match. |
| 146 | CHECK(dbus_connection_add_filter(connection, MessageFilter, this, NULL)); |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | void MetricsDaemon::Loop() { |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 150 | GMainLoop* loop = g_main_loop_new(NULL, false); |
| 151 | g_main_loop_run(loop); |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 154 | // static |
| 155 | DBusHandlerResult MetricsDaemon::MessageFilter(DBusConnection* connection, |
| 156 | DBusMessage* message, |
| 157 | void* user_data) { |
Darin Petkov | f27f036 | 2010-06-04 13:14:19 -0700 | [diff] [blame] | 158 | Time now = Time::Now(); |
| 159 | TimeTicks ticks = TimeTicks::Now(); |
| 160 | DLOG(INFO) << "message intercepted @ " << now.ToInternalValue(); |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 161 | |
| 162 | int message_type = dbus_message_get_type(message); |
| 163 | if (message_type != DBUS_MESSAGE_TYPE_SIGNAL) { |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 164 | DLOG(WARNING) << "unexpected message type " << message_type; |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 165 | return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
| 166 | } |
| 167 | |
| 168 | // Signal messages always have interfaces. |
| 169 | const char* interface = dbus_message_get_interface(message); |
| 170 | CHECK(interface != NULL); |
| 171 | |
| 172 | MetricsDaemon* daemon = static_cast<MetricsDaemon*>(user_data); |
| 173 | |
| 174 | DBusMessageIter iter; |
| 175 | dbus_message_iter_init(message, &iter); |
Darin Petkov | e334840 | 2010-06-04 14:07:41 -0700 | [diff] [blame] | 176 | if (strcmp(interface, DBUS_IFACE_FLIMFLAM_MANAGER) == 0) { |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 177 | CHECK(strcmp(dbus_message_get_member(message), |
| 178 | "StateChanged") == 0); |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 179 | |
David James | 3b3add5 | 2010-06-04 15:01:19 -0700 | [diff] [blame^] | 180 | char* state_name; |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 181 | dbus_message_iter_get_basic(&iter, &state_name); |
Darin Petkov | f27f036 | 2010-06-04 13:14:19 -0700 | [diff] [blame] | 182 | daemon->NetStateChanged(state_name, ticks); |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 183 | } else if (strcmp(interface, DBUS_IFACE_POWER_MANAGER) == 0) { |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 184 | CHECK(strcmp(dbus_message_get_member(message), |
| 185 | "PowerStateChanged") == 0); |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 186 | |
David James | 3b3add5 | 2010-06-04 15:01:19 -0700 | [diff] [blame^] | 187 | char* state_name; |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 188 | dbus_message_iter_get_basic(&iter, &state_name); |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 189 | daemon->PowerStateChanged(state_name, now); |
| 190 | } else if (strcmp(interface, DBUS_IFACE_SCREENSAVER_MANAGER) == 0) { |
| 191 | CHECK(strcmp(dbus_message_get_member(message), |
| 192 | "LockStateChanged") == 0); |
| 193 | |
David James | 3b3add5 | 2010-06-04 15:01:19 -0700 | [diff] [blame^] | 194 | char* state_name; |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 195 | dbus_message_iter_get_basic(&iter, &state_name); |
| 196 | daemon->ScreenSaverStateChanged(state_name, now); |
| 197 | } else if (strcmp(interface, DBUS_IFACE_SESSION_MANAGER) == 0) { |
| 198 | CHECK(strcmp(dbus_message_get_member(message), |
| 199 | "SessionStateChanged") == 0); |
| 200 | |
David James | 3b3add5 | 2010-06-04 15:01:19 -0700 | [diff] [blame^] | 201 | char* state_name; |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 202 | dbus_message_iter_get_basic(&iter, &state_name); |
| 203 | daemon->SessionStateChanged(state_name, now); |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 204 | } else { |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 205 | DLOG(WARNING) << "unexpected interface: " << interface; |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 206 | return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
| 207 | } |
| 208 | |
| 209 | return DBUS_HANDLER_RESULT_HANDLED; |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 210 | } |
| 211 | |
Darin Petkov | f27f036 | 2010-06-04 13:14:19 -0700 | [diff] [blame] | 212 | void MetricsDaemon::NetStateChanged(const char* state_name, TimeTicks ticks) { |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 213 | DLOG(INFO) << "network state: " << state_name; |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 214 | |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 215 | NetworkState state = LookupNetworkState(state_name); |
| 216 | |
| 217 | // Logs the time in seconds between the network going online to |
Darin Petkov | 2ccef01 | 2010-05-05 16:06:37 -0700 | [diff] [blame] | 218 | // going offline (or, more precisely, going not online) in order to |
| 219 | // measure the mean time to network dropping. Going offline as part |
| 220 | // of suspend-to-RAM is not logged as network drop -- the assumption |
| 221 | // is that the message for suspend-to-RAM comes before the network |
| 222 | // offline message which seems to and should be the case. |
| 223 | if (state != kNetworkStateOnline && |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 224 | network_state_ == kNetworkStateOnline && |
| 225 | power_state_ != kPowerStateMem) { |
Darin Petkov | f27f036 | 2010-06-04 13:14:19 -0700 | [diff] [blame] | 226 | TimeDelta since_online = ticks - network_state_last_; |
| 227 | int online_time = static_cast<int>(since_online.InSeconds()); |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 228 | SendMetric(kMetricTimeToNetworkDropName, online_time, |
| 229 | kMetricTimeToNetworkDropMin, |
| 230 | kMetricTimeToNetworkDropMax, |
| 231 | kMetricTimeToNetworkDropBuckets); |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 232 | } |
| 233 | |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 234 | network_state_ = state; |
Darin Petkov | f27f036 | 2010-06-04 13:14:19 -0700 | [diff] [blame] | 235 | network_state_last_ = ticks; |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 236 | } |
| 237 | |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 238 | MetricsDaemon::NetworkState |
| 239 | MetricsDaemon::LookupNetworkState(const char* state_name) { |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 240 | for (int i = 0; i < kNumberNetworkStates; i++) { |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 241 | if (strcmp(state_name, kNetworkStates_[i]) == 0) { |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 242 | return static_cast<NetworkState>(i); |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 243 | } |
| 244 | } |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 245 | DLOG(WARNING) << "unknown network connection state: " << state_name; |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 246 | return kUnknownNetworkState; |
| 247 | } |
| 248 | |
Darin Petkov | f27f036 | 2010-06-04 13:14:19 -0700 | [diff] [blame] | 249 | void MetricsDaemon::PowerStateChanged(const char* state_name, Time now) { |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 250 | DLOG(INFO) << "power state: " << state_name; |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 251 | power_state_ = LookupPowerState(state_name); |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 252 | |
| 253 | if (power_state_ != kPowerStateOn) |
| 254 | SetUserActiveState(false, now); |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | MetricsDaemon::PowerState |
| 258 | MetricsDaemon::LookupPowerState(const char* state_name) { |
| 259 | for (int i = 0; i < kNumberPowerStates; i++) { |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 260 | if (strcmp(state_name, kPowerStates_[i]) == 0) { |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 261 | return static_cast<PowerState>(i); |
| 262 | } |
| 263 | } |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 264 | DLOG(WARNING) << "unknown power state: " << state_name; |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 265 | return kUnknownPowerState; |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 266 | } |
| 267 | |
Darin Petkov | f27f036 | 2010-06-04 13:14:19 -0700 | [diff] [blame] | 268 | void MetricsDaemon::ScreenSaverStateChanged(const char* state_name, Time now) { |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 269 | DLOG(INFO) << "screen-saver state: " << state_name; |
| 270 | screensaver_state_ = LookupScreenSaverState(state_name); |
| 271 | SetUserActiveState(screensaver_state_ == kScreenSaverStateUnlocked, now); |
| 272 | } |
| 273 | |
| 274 | MetricsDaemon::ScreenSaverState |
| 275 | MetricsDaemon::LookupScreenSaverState(const char* state_name) { |
| 276 | for (int i = 0; i < kNumberScreenSaverStates; i++) { |
| 277 | if (strcmp(state_name, kScreenSaverStates_[i]) == 0) { |
| 278 | return static_cast<ScreenSaverState>(i); |
| 279 | } |
| 280 | } |
| 281 | DLOG(WARNING) << "unknown screen-saver state: " << state_name; |
| 282 | return kUnknownScreenSaverState; |
| 283 | } |
| 284 | |
Darin Petkov | f27f036 | 2010-06-04 13:14:19 -0700 | [diff] [blame] | 285 | void MetricsDaemon::SessionStateChanged(const char* state_name, Time now) { |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 286 | DLOG(INFO) << "user session state: " << state_name; |
| 287 | session_state_ = LookupSessionState(state_name); |
| 288 | SetUserActiveState(session_state_ == kSessionStateStarted, now); |
| 289 | } |
| 290 | |
| 291 | MetricsDaemon::SessionState |
| 292 | MetricsDaemon::LookupSessionState(const char* state_name) { |
| 293 | for (int i = 0; i < kNumberSessionStates; i++) { |
| 294 | if (strcmp(state_name, kSessionStates_[i]) == 0) { |
| 295 | return static_cast<SessionState>(i); |
| 296 | } |
| 297 | } |
| 298 | DLOG(WARNING) << "unknown user session state: " << state_name; |
| 299 | return kUnknownSessionState; |
| 300 | } |
| 301 | |
Darin Petkov | f27f036 | 2010-06-04 13:14:19 -0700 | [diff] [blame] | 302 | void MetricsDaemon::SetUserActiveState(bool active, Time now) { |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 303 | DLOG(INFO) << "user: " << (active ? "active" : "inactive"); |
| 304 | |
| 305 | // Calculates the seconds of active use since the last update and |
Darin Petkov | f27f036 | 2010-06-04 13:14:19 -0700 | [diff] [blame] | 306 | // the day since Epoch, and logs the usage data. Guards against the |
| 307 | // time jumping back and forth due to the user changing it by |
| 308 | // discarding the new use time. |
| 309 | int seconds = 0; |
| 310 | if (user_active_ && now > user_active_last_) { |
| 311 | TimeDelta since_active = now - user_active_last_; |
| 312 | if (since_active < TimeDelta::FromSeconds( |
| 313 | kUseMonitorIntervalMax + kSecondsPerMinute)) { |
| 314 | seconds = static_cast<int>(since_active.InSeconds()); |
| 315 | } |
| 316 | } |
| 317 | TimeDelta since_epoch = now - Time(); |
| 318 | int day = since_epoch.InDays(); |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 319 | LogDailyUseRecord(day, seconds); |
| 320 | |
| 321 | // Schedules a use monitor on inactive->active transitions and |
| 322 | // unschedules it on active->inactive transitions. |
| 323 | if (!user_active_ && active) |
| 324 | ScheduleUseMonitor(kUseMonitorIntervalInit, /* backoff */ false); |
| 325 | else if (user_active_ && !active) |
| 326 | UnscheduleUseMonitor(); |
| 327 | |
| 328 | // Remembers the current active state and the time of the last |
| 329 | // activity update. |
| 330 | user_active_ = active; |
| 331 | user_active_last_ = now; |
| 332 | } |
| 333 | |
| 334 | void MetricsDaemon::LogDailyUseRecord(int day, int seconds) { |
| 335 | // If there's no new active use today and the last record in the |
| 336 | // usage aggregation file is today, there's nothing to do. |
| 337 | if (seconds == 0 && day == daily_use_day_last_) |
| 338 | return; |
| 339 | |
| 340 | DLOG(INFO) << "day: " << day << " usage: " << seconds << " seconds"; |
Darin Petkov | 2ccef01 | 2010-05-05 16:06:37 -0700 | [diff] [blame] | 341 | int fd = HANDLE_EINTR(open(daily_use_record_file_, |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 342 | O_RDWR | O_CREAT, |
| 343 | S_IRUSR | S_IWUSR)); |
| 344 | if (fd < 0) { |
Darin Petkov | 2ccef01 | 2010-05-05 16:06:37 -0700 | [diff] [blame] | 345 | PLOG(WARNING) << "Unable to open the daily use file"; |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 346 | return; |
| 347 | } |
| 348 | |
| 349 | bool same_day = false; |
| 350 | UseRecord record; |
| 351 | if (HANDLE_EINTR(read(fd, &record, sizeof(record))) == sizeof(record)) { |
| 352 | if (record.day_ == day) { |
| 353 | // If there's an existing record for today, aggregates the usage |
| 354 | // time. |
| 355 | same_day = true; |
| 356 | record.seconds_ += seconds; |
| 357 | } else { |
| 358 | // If there's an existing record for a day in the past, rounds |
| 359 | // the usage to the nearest minute and sends it to UMA. |
| 360 | int minutes = |
| 361 | (record.seconds_ + kSecondsPerMinute / 2) / kSecondsPerMinute; |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 362 | SendMetric(kMetricDailyUseTimeName, minutes, |
| 363 | kMetricDailyUseTimeMin, |
| 364 | kMetricDailyUseTimeMax, |
| 365 | kMetricDailyUseTimeBuckets); |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 366 | |
| 367 | // Truncates the usage file to ensure that no duplicate usage is |
| 368 | // sent to UMA. |
Darin Petkov | 2ccef01 | 2010-05-05 16:06:37 -0700 | [diff] [blame] | 369 | PLOG_IF(WARNING, HANDLE_EINTR(ftruncate(fd, 0)) != 0); |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 370 | } |
| 371 | } |
| 372 | |
| 373 | // Updates the use record in the daily usage file if there's new |
| 374 | // usage today. |
| 375 | if (seconds > 0) { |
| 376 | if (!same_day) { |
| 377 | record.day_ = day; |
| 378 | record.seconds_ = seconds; |
| 379 | } |
| 380 | // else an already existing record for the same day will be |
| 381 | // overwritten with updated usage below. |
| 382 | |
Darin Petkov | 2ccef01 | 2010-05-05 16:06:37 -0700 | [diff] [blame] | 383 | PLOG_IF(WARNING, HANDLE_EINTR(lseek(fd, 0, SEEK_SET)) != 0); |
| 384 | PLOG_IF(WARNING, |
| 385 | HANDLE_EINTR(write(fd, &record, sizeof(record))) != |
| 386 | sizeof(record)); |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | HANDLE_EINTR(close(fd)); |
| 390 | |
| 391 | // Remembers the day of the use record in the usage aggregation file |
| 392 | // to reduce file I/O. This is not really useful now but potentially |
| 393 | // allows frequent LogDailyUseRecord calls with no unnecessary I/O |
| 394 | // overhead. |
| 395 | daily_use_day_last_ = day; |
| 396 | } |
| 397 | |
| 398 | // static |
| 399 | gboolean MetricsDaemon::UseMonitorStatic(gpointer data) { |
| 400 | return static_cast<MetricsDaemon*>(data)->UseMonitor() ? TRUE : FALSE; |
| 401 | } |
| 402 | |
| 403 | bool MetricsDaemon::UseMonitor() { |
Darin Petkov | f27f036 | 2010-06-04 13:14:19 -0700 | [diff] [blame] | 404 | SetUserActiveState(user_active_, Time::Now()); |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 405 | |
| 406 | // If a new monitor source/instance is scheduled, returns false to |
| 407 | // tell GLib to destroy this monitor source/instance. Returns true |
| 408 | // otherwise to keep calling back this monitor. |
| 409 | return !ScheduleUseMonitor(usemon_interval_ * 2, /* backoff */ true); |
| 410 | } |
| 411 | |
| 412 | bool MetricsDaemon::ScheduleUseMonitor(int interval, bool backoff) |
| 413 | { |
Darin Petkov | 2ccef01 | 2010-05-05 16:06:37 -0700 | [diff] [blame] | 414 | if (testing_) |
| 415 | return false; |
| 416 | |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 417 | // Caps the interval -- the bigger the interval, the more active use |
| 418 | // time will be potentially dropped on system shutdown. |
| 419 | if (interval > kUseMonitorIntervalMax) |
| 420 | interval = kUseMonitorIntervalMax; |
| 421 | |
| 422 | if (backoff) { |
| 423 | // Back-off mode is used by the use monitor to reschedule itself |
| 424 | // with exponential back-off in time. This mode doesn't create a |
| 425 | // new timeout source if the new interval is the same as the old |
| 426 | // one. Also, if a new timeout source is created, the old one is |
| 427 | // not destroyed explicitly here -- it will be destroyed by GLib |
| 428 | // when the monitor returns FALSE (see UseMonitor and |
| 429 | // UseMonitorStatic). |
| 430 | if (interval == usemon_interval_) |
| 431 | return false; |
| 432 | } else { |
| 433 | UnscheduleUseMonitor(); |
| 434 | } |
| 435 | |
| 436 | // Schedules a new use monitor for |interval| seconds from now. |
| 437 | DLOG(INFO) << "scheduling use monitor in " << interval << " seconds"; |
| 438 | usemon_source_ = g_timeout_source_new_seconds(interval); |
| 439 | g_source_set_callback(usemon_source_, UseMonitorStatic, this, |
| 440 | NULL); // No destroy notification. |
| 441 | g_source_attach(usemon_source_, |
| 442 | NULL); // Default context. |
| 443 | usemon_interval_ = interval; |
| 444 | return true; |
| 445 | } |
| 446 | |
| 447 | void MetricsDaemon::UnscheduleUseMonitor() { |
| 448 | // If there's a use monitor scheduled already, destroys it. |
| 449 | if (usemon_source_ == NULL) |
| 450 | return; |
| 451 | |
| 452 | DLOG(INFO) << "destroying use monitor"; |
| 453 | g_source_destroy(usemon_source_); |
| 454 | usemon_source_ = NULL; |
| 455 | usemon_interval_ = 0; |
| 456 | } |
| 457 | |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 458 | void MetricsDaemon::SendMetric(const std::string& name, int sample, |
| 459 | int min, int max, int nbuckets) { |
Darin Petkov | fc91b42 | 2010-05-12 13:05:45 -0700 | [diff] [blame] | 460 | DLOG(INFO) << "received metric: " << name << " " << sample << " " |
| 461 | << min << " " << max << " " << nbuckets; |
| 462 | metrics_lib_->SendToUMA(name, sample, min, max, nbuckets); |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 463 | } |