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