blob: 30d3da8aecdf51270ea147ec72baeb4324c97d69 [file] [log] [blame]
Darin Petkov65b01462010-04-14 13:32:20 -07001// 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 Petkov65b01462010-04-14 13:32:20 -07006
Darin Petkov703ec972010-04-27 11:02:18 -07007#include <dbus/dbus-glib-lowlevel.h>
Darin Petkov65b01462010-04-14 13:32:20 -07008
Darin Petkov38d5cb02010-06-24 12:10:26 -07009#include <base/file_util.h>
Darin Petkov65b01462010-04-14 13:32:20 -070010#include <base/logging.h>
11
Darin Petkovf1e85e42010-06-10 15:59:53 -070012#include "counter.h"
13
Darin Petkovf27f0362010-06-04 13:14:19 -070014using base::Time;
15using base::TimeDelta;
16using base::TimeTicks;
Darin Petkov38d5cb02010-06-24 12:10:26 -070017using std::string;
Darin Petkovf27f0362010-06-04 13:14:19 -070018
Darin Petkov703ec972010-04-27 11:02:18 -070019#define SAFE_MESSAGE(e) (e.message ? e.message : "unknown error")
Darin Petkov1bb904e2010-06-16 15:58:06 -070020#define DBUS_IFACE_CRASH_REPORTER "org.chromium.CrashReporter"
Darin Petkove3348402010-06-04 14:07:41 -070021#define DBUS_IFACE_FLIMFLAM_MANAGER "org.chromium.flimflam.Manager"
David James6bf6e252010-06-06 18:52:40 -070022#define DBUS_IFACE_POWER_MANAGER "org.chromium.PowerManager"
Darin Petkov41e06232010-05-03 16:45:37 -070023#define DBUS_IFACE_SESSION_MANAGER "org.chromium.SessionManagerInterface"
24
Darin Petkov41e06232010-05-03 16:45:37 -070025static const int kSecondsPerMinute = 60;
26static const int kMinutesPerHour = 60;
27static const int kHoursPerDay = 24;
28static const int kMinutesPerDay = kHoursPerDay * kMinutesPerHour;
Darin Petkov1bb904e2010-06-16 15:58:06 -070029static const int kSecondsPerDay = kSecondsPerMinute * kMinutesPerDay;
30static const int kDaysPerWeek = 7;
31static const int kSecondsPerWeek = kSecondsPerDay * kDaysPerWeek;
Darin Petkov41e06232010-05-03 16:45:37 -070032
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.
39static const int kUseMonitorIntervalInit = 1 * kSecondsPerMinute;
40static const int kUseMonitorIntervalMax = 10 * kSecondsPerMinute;
Darin Petkov65b01462010-04-14 13:32:20 -070041
Darin Petkov2ccef012010-05-05 16:06:37 -070042// static metrics parameters.
43const char MetricsDaemon::kMetricDailyUseTimeName[] =
44 "Logging.DailyUseTime";
45const int MetricsDaemon::kMetricDailyUseTimeMin = 1;
46const int MetricsDaemon::kMetricDailyUseTimeMax = kMinutesPerDay;
47const int MetricsDaemon::kMetricDailyUseTimeBuckets = 50;
48
Darin Petkov38d5cb02010-06-24 12:10:26 -070049const char MetricsDaemon::kMetricKernelCrashIntervalName[] =
50 "Logging.KernelCrashInterval";
51const int MetricsDaemon::kMetricKernelCrashIntervalMin = 1;
52const int MetricsDaemon::kMetricKernelCrashIntervalMax = 4 * kSecondsPerWeek;
53const int MetricsDaemon::kMetricKernelCrashIntervalBuckets = 50;
54
Darin Petkov2ccef012010-05-05 16:06:37 -070055const char MetricsDaemon::kMetricTimeToNetworkDropName[] =
56 "Network.TimeToDrop";
57const int MetricsDaemon::kMetricTimeToNetworkDropMin = 1;
58const int MetricsDaemon::kMetricTimeToNetworkDropMax =
59 8 /* hours */ * kMinutesPerHour * kSecondsPerMinute;
60const int MetricsDaemon::kMetricTimeToNetworkDropBuckets = 50;
61
Darin Petkov1bb904e2010-06-16 15:58:06 -070062const char MetricsDaemon::kMetricUserCrashIntervalName[] =
63 "Logging.UserCrashInterval";
64const int MetricsDaemon::kMetricUserCrashIntervalMin = 1;
65const int MetricsDaemon::kMetricUserCrashIntervalMax = 4 * kSecondsPerWeek;
66const int MetricsDaemon::kMetricUserCrashIntervalBuckets = 50;
67
Darin Petkov703ec972010-04-27 11:02:18 -070068// static
Darin Petkov41e06232010-05-03 16:45:37 -070069const char* MetricsDaemon::kDBusMatches_[] = {
Darin Petkov703ec972010-04-27 11:02:18 -070070 "type='signal',"
Darin Petkov1bb904e2010-06-16 15:58:06 -070071 "interface='" DBUS_IFACE_CRASH_REPORTER "',"
72 "path='/',"
73 "member='UserCrash'",
74
75 "type='signal',"
Darin Petkove3348402010-06-04 14:07:41 -070076 "sender='org.chromium.flimflam',"
77 "interface='" DBUS_IFACE_FLIMFLAM_MANAGER "',"
Darin Petkov703ec972010-04-27 11:02:18 -070078 "path='/',"
79 "member='StateChanged'",
80
81 "type='signal',"
82 "interface='" DBUS_IFACE_POWER_MANAGER "',"
Benson Leung53faeb02010-06-08 15:59:13 -070083 "path='/'",
Darin Petkov41e06232010-05-03 16:45:37 -070084
85 "type='signal',"
86 "sender='org.chromium.SessionManager',"
87 "interface='" DBUS_IFACE_SESSION_MANAGER "',"
88 "path='/org/chromium/SessionManager',"
89 "member='SessionStateChanged'",
Darin Petkov703ec972010-04-27 11:02:18 -070090};
91
92// static
Darin Petkov41e06232010-05-03 16:45:37 -070093const char* MetricsDaemon::kNetworkStates_[] = {
Darin Petkov703ec972010-04-27 11:02:18 -070094#define STATE(name, capname) #name,
Darin Petkov65b01462010-04-14 13:32:20 -070095#include "network_states.h"
96};
97
Darin Petkov703ec972010-04-27 11:02:18 -070098// static
Darin Petkov41e06232010-05-03 16:45:37 -070099const char* MetricsDaemon::kPowerStates_[] = {
Darin Petkov703ec972010-04-27 11:02:18 -0700100#define STATE(name, capname) #name,
101#include "power_states.h"
102};
103
Darin Petkov41e06232010-05-03 16:45:37 -0700104// static
Darin Petkov41e06232010-05-03 16:45:37 -0700105const char* MetricsDaemon::kSessionStates_[] = {
106#define STATE(name, capname) #name,
107#include "session_states.h"
108};
109
Darin Petkov38d5cb02010-06-24 12:10:26 -0700110// 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.
114static 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 Petkovf1e85e42010-06-10 15:59:53 -0700157MetricsDaemon::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
165MetricsDaemon::~MetricsDaemon() {}
166
Darin Petkov2ccef012010-05-05 16:06:37 -0700167void MetricsDaemon::Run(bool run_as_daemon) {
Darin Petkov38d5cb02010-06-24 12:10:26 -0700168 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 Petkov65b01462010-04-14 13:32:20 -0700174}
175
Darin Petkovfc91b422010-05-12 13:05:45 -0700176void MetricsDaemon::Init(bool testing, MetricsLibraryInterface* metrics_lib) {
Darin Petkov65b01462010-04-14 13:32:20 -0700177 testing_ = testing;
Darin Petkovfc91b422010-05-12 13:05:45 -0700178 DCHECK(metrics_lib != NULL);
179 metrics_lib_ = metrics_lib;
Darin Petkov38d5cb02010-06-24 12:10:26 -0700180
181 static const char kDailyUseRecordFile[] = "/var/log/metrics/daily-usage";
Darin Petkovf1e85e42010-06-10 15:59:53 -0700182 daily_use_.reset(new chromeos_metrics::TaggedCounter());
183 daily_use_->Init(kDailyUseRecordFile, &DailyUseReporter, this);
Darin Petkov38d5cb02010-06-24 12:10:26 -0700184
185 static const char kUserCrashIntervalRecordFile[] =
186 "/var/log/metrics/user-crash-interval";
Darin Petkov1bb904e2010-06-16 15:58:06 -0700187 user_crash_interval_.reset(new chromeos_metrics::TaggedCounter());
188 user_crash_interval_->Init(kUserCrashIntervalRecordFile,
189 &UserCrashIntervalReporter, this);
Darin Petkov2ccef012010-05-05 16:06:37 -0700190
Darin Petkov38d5cb02010-06-24 12:10:26 -0700191 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 Petkov2ccef012010-05-05 16:06:37 -0700197 // Don't setup D-Bus and GLib in test mode.
198 if (testing)
199 return;
Darin Petkov65b01462010-04-14 13:32:20 -0700200
Darin Petkov703ec972010-04-27 11:02:18 -0700201 g_thread_init(NULL);
202 g_type_init();
203 dbus_g_thread_init();
Darin Petkov65b01462010-04-14 13:32:20 -0700204
Darin Petkov703ec972010-04-27 11:02:18 -0700205 DBusError error;
206 dbus_error_init(&error);
Darin Petkov65b01462010-04-14 13:32:20 -0700207
David James3b3add52010-06-04 15:01:19 -0700208 DBusConnection* connection = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
Darin Petkov703ec972010-04-27 11:02:18 -0700209 LOG_IF(FATAL, dbus_error_is_set(&error)) <<
210 "No D-Bus connection: " << SAFE_MESSAGE(error);
Darin Petkov65b01462010-04-14 13:32:20 -0700211
Darin Petkov703ec972010-04-27 11:02:18 -0700212 dbus_connection_setup_with_g_main(connection, NULL);
Darin Petkov65b01462010-04-14 13:32:20 -0700213
Darin Petkov703ec972010-04-27 11:02:18 -0700214 // Registers D-Bus matches for the signals we would like to catch.
Darin Petkov1bb904e2010-06-16 15:58:06 -0700215 for (unsigned int m = 0; m < arraysize(kDBusMatches_); m++) {
Darin Petkov41e06232010-05-03 16:45:37 -0700216 const char* match = kDBusMatches_[m];
217 DLOG(INFO) << "adding dbus match: " << match;
Darin Petkov703ec972010-04-27 11:02:18 -0700218 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 Petkov38d5cb02010-06-24 12:10:26 -0700227
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 Petkov65b01462010-04-14 13:32:20 -0700232}
233
234void MetricsDaemon::Loop() {
Darin Petkov703ec972010-04-27 11:02:18 -0700235 GMainLoop* loop = g_main_loop_new(NULL, false);
236 g_main_loop_run(loop);
Darin Petkov65b01462010-04-14 13:32:20 -0700237}
238
Darin Petkov703ec972010-04-27 11:02:18 -0700239// static
240DBusHandlerResult MetricsDaemon::MessageFilter(DBusConnection* connection,
241 DBusMessage* message,
242 void* user_data) {
Darin Petkovf27f0362010-06-04 13:14:19 -0700243 Time now = Time::Now();
244 TimeTicks ticks = TimeTicks::Now();
245 DLOG(INFO) << "message intercepted @ " << now.ToInternalValue();
Darin Petkov703ec972010-04-27 11:02:18 -0700246
247 int message_type = dbus_message_get_type(message);
248 if (message_type != DBUS_MESSAGE_TYPE_SIGNAL) {
Darin Petkov41e06232010-05-03 16:45:37 -0700249 DLOG(WARNING) << "unexpected message type " << message_type;
Darin Petkov703ec972010-04-27 11:02:18 -0700250 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 Petkov1bb904e2010-06-16 15:58:06 -0700261 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 Petkov41e06232010-05-03 16:45:37 -0700266 CHECK(strcmp(dbus_message_get_member(message),
267 "StateChanged") == 0);
Darin Petkov703ec972010-04-27 11:02:18 -0700268
David James3b3add52010-06-04 15:01:19 -0700269 char* state_name;
Darin Petkov703ec972010-04-27 11:02:18 -0700270 dbus_message_iter_get_basic(&iter, &state_name);
Darin Petkovf27f0362010-06-04 13:14:19 -0700271 daemon->NetStateChanged(state_name, ticks);
Darin Petkov703ec972010-04-27 11:02:18 -0700272 } else if (strcmp(interface, DBUS_IFACE_POWER_MANAGER) == 0) {
David James6bf6e252010-06-06 18:52:40 -0700273 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 Petkov41e06232010-05-03 16:45:37 -0700283 } else if (strcmp(interface, DBUS_IFACE_SESSION_MANAGER) == 0) {
284 CHECK(strcmp(dbus_message_get_member(message),
285 "SessionStateChanged") == 0);
286
David James3b3add52010-06-04 15:01:19 -0700287 char* state_name;
Darin Petkov41e06232010-05-03 16:45:37 -0700288 dbus_message_iter_get_basic(&iter, &state_name);
289 daemon->SessionStateChanged(state_name, now);
Darin Petkov703ec972010-04-27 11:02:18 -0700290 } else {
Darin Petkov41e06232010-05-03 16:45:37 -0700291 DLOG(WARNING) << "unexpected interface: " << interface;
Darin Petkov703ec972010-04-27 11:02:18 -0700292 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
293 }
294
295 return DBUS_HANDLER_RESULT_HANDLED;
Darin Petkov65b01462010-04-14 13:32:20 -0700296}
297
Darin Petkovf27f0362010-06-04 13:14:19 -0700298void MetricsDaemon::NetStateChanged(const char* state_name, TimeTicks ticks) {
Darin Petkov41e06232010-05-03 16:45:37 -0700299 DLOG(INFO) << "network state: " << state_name;
Darin Petkov703ec972010-04-27 11:02:18 -0700300
Darin Petkov703ec972010-04-27 11:02:18 -0700301 NetworkState state = LookupNetworkState(state_name);
302
303 // Logs the time in seconds between the network going online to
Darin Petkov2ccef012010-05-05 16:06:37 -0700304 // 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 Petkov703ec972010-04-27 11:02:18 -0700310 network_state_ == kNetworkStateOnline &&
311 power_state_ != kPowerStateMem) {
Darin Petkovf27f0362010-06-04 13:14:19 -0700312 TimeDelta since_online = ticks - network_state_last_;
313 int online_time = static_cast<int>(since_online.InSeconds());
Darin Petkov11b8eb32010-05-18 11:00:59 -0700314 SendMetric(kMetricTimeToNetworkDropName, online_time,
315 kMetricTimeToNetworkDropMin,
316 kMetricTimeToNetworkDropMax,
317 kMetricTimeToNetworkDropBuckets);
Darin Petkov65b01462010-04-14 13:32:20 -0700318 }
319
Darin Petkov703ec972010-04-27 11:02:18 -0700320 network_state_ = state;
Darin Petkovf27f0362010-06-04 13:14:19 -0700321 network_state_last_ = ticks;
Darin Petkov65b01462010-04-14 13:32:20 -0700322}
323
Darin Petkov703ec972010-04-27 11:02:18 -0700324MetricsDaemon::NetworkState
325MetricsDaemon::LookupNetworkState(const char* state_name) {
Darin Petkov65b01462010-04-14 13:32:20 -0700326 for (int i = 0; i < kNumberNetworkStates; i++) {
Darin Petkov41e06232010-05-03 16:45:37 -0700327 if (strcmp(state_name, kNetworkStates_[i]) == 0) {
Darin Petkov703ec972010-04-27 11:02:18 -0700328 return static_cast<NetworkState>(i);
Darin Petkov65b01462010-04-14 13:32:20 -0700329 }
330 }
Darin Petkov41e06232010-05-03 16:45:37 -0700331 DLOG(WARNING) << "unknown network connection state: " << state_name;
Darin Petkov703ec972010-04-27 11:02:18 -0700332 return kUnknownNetworkState;
333}
334
Darin Petkovf27f0362010-06-04 13:14:19 -0700335void MetricsDaemon::PowerStateChanged(const char* state_name, Time now) {
Darin Petkov41e06232010-05-03 16:45:37 -0700336 DLOG(INFO) << "power state: " << state_name;
Darin Petkov703ec972010-04-27 11:02:18 -0700337 power_state_ = LookupPowerState(state_name);
Darin Petkov41e06232010-05-03 16:45:37 -0700338
339 if (power_state_ != kPowerStateOn)
340 SetUserActiveState(false, now);
Darin Petkov703ec972010-04-27 11:02:18 -0700341}
342
343MetricsDaemon::PowerState
344MetricsDaemon::LookupPowerState(const char* state_name) {
345 for (int i = 0; i < kNumberPowerStates; i++) {
Darin Petkov41e06232010-05-03 16:45:37 -0700346 if (strcmp(state_name, kPowerStates_[i]) == 0) {
Darin Petkov703ec972010-04-27 11:02:18 -0700347 return static_cast<PowerState>(i);
348 }
349 }
Darin Petkov41e06232010-05-03 16:45:37 -0700350 DLOG(WARNING) << "unknown power state: " << state_name;
Darin Petkov703ec972010-04-27 11:02:18 -0700351 return kUnknownPowerState;
Darin Petkov65b01462010-04-14 13:32:20 -0700352}
353
Darin Petkovf27f0362010-06-04 13:14:19 -0700354void MetricsDaemon::SessionStateChanged(const char* state_name, Time now) {
Darin Petkov41e06232010-05-03 16:45:37 -0700355 DLOG(INFO) << "user session state: " << state_name;
356 session_state_ = LookupSessionState(state_name);
357 SetUserActiveState(session_state_ == kSessionStateStarted, now);
358}
359
360MetricsDaemon::SessionState
361MetricsDaemon::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 Petkovf27f0362010-06-04 13:14:19 -0700371void MetricsDaemon::SetUserActiveState(bool active, Time now) {
Darin Petkov41e06232010-05-03 16:45:37 -0700372 DLOG(INFO) << "user: " << (active ? "active" : "inactive");
373
374 // Calculates the seconds of active use since the last update and
Darin Petkovf27f0362010-06-04 13:14:19 -0700375 // 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 Petkovf1e85e42010-06-10 15:59:53 -0700388 daily_use_->Update(day, seconds);
Darin Petkov1bb904e2010-06-16 15:58:06 -0700389 user_crash_interval_->Update(0, seconds);
Darin Petkov38d5cb02010-06-24 12:10:26 -0700390 kernel_crash_interval_->Update(0, seconds);
Darin Petkov41e06232010-05-03 16:45:37 -0700391
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 Petkov1bb904e2010-06-16 15:58:06 -0700405void 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 Petkov38d5cb02010-06-24 12:10:26 -0700413void 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
421void 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 Petkov41e06232010-05-03 16:45:37 -0700434// static
435gboolean MetricsDaemon::UseMonitorStatic(gpointer data) {
436 return static_cast<MetricsDaemon*>(data)->UseMonitor() ? TRUE : FALSE;
437}
438
439bool MetricsDaemon::UseMonitor() {
Darin Petkovf27f0362010-06-04 13:14:19 -0700440 SetUserActiveState(user_active_, Time::Now());
Darin Petkov41e06232010-05-03 16:45:37 -0700441
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
448bool MetricsDaemon::ScheduleUseMonitor(int interval, bool backoff)
449{
Darin Petkov2ccef012010-05-05 16:06:37 -0700450 if (testing_)
451 return false;
452
Darin Petkov41e06232010-05-03 16:45:37 -0700453 // 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
483void 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 Petkovf1e85e42010-06-10 15:59:53 -0700494// static
495void MetricsDaemon::DailyUseReporter(void* handle, int tag, int count) {
Darin Petkov1bb904e2010-06-16 15:58:06 -0700496 if (count <= 0)
497 return;
498
Darin Petkovf1e85e42010-06-10 15:59:53 -0700499 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 Petkov1bb904e2010-06-16 15:58:06 -0700507// static
508void 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 Petkov38d5cb02010-06-24 12:10:26 -0700517// static
518void 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
527void MetricsDaemon::SendMetric(const string& name, int sample,
Darin Petkov11b8eb32010-05-18 11:00:59 -0700528 int min, int max, int nbuckets) {
Darin Petkovfc91b422010-05-12 13:05:45 -0700529 DLOG(INFO) << "received metric: " << name << " " << sample << " "
530 << min << " " << max << " " << nbuckets;
531 metrics_lib_->SendToUMA(name, sample, min, max, nbuckets);
Darin Petkov65b01462010-04-14 13:32:20 -0700532}