blob: 04ad68631d5249aef47ba98f116a1567803cc10d [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 Petkov41e06232010-05-03 16:45:37 -07008#include <sys/file.h>
Darin Petkov65b01462010-04-14 13:32:20 -07009
Darin Petkov41e06232010-05-03 16:45:37 -070010#include <base/eintr_wrapper.h>
Darin Petkov65b01462010-04-14 13:32:20 -070011#include <base/logging.h>
12
Darin Petkovf27f0362010-06-04 13:14:19 -070013using base::Time;
14using base::TimeDelta;
15using base::TimeTicks;
16
Darin Petkov703ec972010-04-27 11:02:18 -070017#define SAFE_MESSAGE(e) (e.message ? e.message : "unknown error")
Darin Petkove3348402010-06-04 14:07:41 -070018#define DBUS_IFACE_FLIMFLAM_MANAGER "org.chromium.flimflam.Manager"
Darin Petkov703ec972010-04-27 11:02:18 -070019#define DBUS_IFACE_POWER_MANAGER "org.chromium.Power.Manager"
Darin Petkov41e06232010-05-03 16:45:37 -070020#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.
26static const char kDailyUseRecordFile[] = "/var/log/metrics/daily-usage";
27
28static const int kSecondsPerMinute = 60;
29static const int kMinutesPerHour = 60;
30static const int kHoursPerDay = 24;
31static const int kMinutesPerDay = kHoursPerDay * kMinutesPerHour;
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
49const char MetricsDaemon::kMetricTimeToNetworkDropName[] =
50 "Network.TimeToDrop";
51const int MetricsDaemon::kMetricTimeToNetworkDropMin = 1;
52const int MetricsDaemon::kMetricTimeToNetworkDropMax =
53 8 /* hours */ * kMinutesPerHour * kSecondsPerMinute;
54const int MetricsDaemon::kMetricTimeToNetworkDropBuckets = 50;
55
Darin Petkov703ec972010-04-27 11:02:18 -070056// static
Darin Petkov41e06232010-05-03 16:45:37 -070057const char* MetricsDaemon::kDBusMatches_[] = {
Darin Petkov703ec972010-04-27 11:02:18 -070058 "type='signal',"
Darin Petkove3348402010-06-04 14:07:41 -070059 "sender='org.chromium.flimflam',"
60 "interface='" DBUS_IFACE_FLIMFLAM_MANAGER "',"
Darin Petkov703ec972010-04-27 11:02:18 -070061 "path='/',"
62 "member='StateChanged'",
63
64 "type='signal',"
65 "interface='" DBUS_IFACE_POWER_MANAGER "',"
66 "path='/',"
67 "member='PowerStateChanged'",
Darin Petkov41e06232010-05-03 16:45:37 -070068
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 Petkov703ec972010-04-27 11:02:18 -070079};
80
81// static
Darin Petkov41e06232010-05-03 16:45:37 -070082const char* MetricsDaemon::kNetworkStates_[] = {
Darin Petkov703ec972010-04-27 11:02:18 -070083#define STATE(name, capname) #name,
Darin Petkov65b01462010-04-14 13:32:20 -070084#include "network_states.h"
85};
86
Darin Petkov703ec972010-04-27 11:02:18 -070087// static
Darin Petkov41e06232010-05-03 16:45:37 -070088const char* MetricsDaemon::kPowerStates_[] = {
Darin Petkov703ec972010-04-27 11:02:18 -070089#define STATE(name, capname) #name,
90#include "power_states.h"
91};
92
Darin Petkov41e06232010-05-03 16:45:37 -070093// static
94const char* MetricsDaemon::kScreenSaverStates_[] = {
95#define STATE(name, capname) #name,
96#include "screensaver_states.h"
97};
98
99// static
100const char* MetricsDaemon::kSessionStates_[] = {
101#define STATE(name, capname) #name,
102#include "session_states.h"
103};
104
Darin Petkov2ccef012010-05-05 16:06:37 -0700105void MetricsDaemon::Run(bool run_as_daemon) {
Darin Petkov65b01462010-04-14 13:32:20 -0700106 if (!run_as_daemon || daemon(0, 0) == 0) {
107 Loop();
108 }
109}
110
Darin Petkovfc91b422010-05-12 13:05:45 -0700111void MetricsDaemon::Init(bool testing, MetricsLibraryInterface* metrics_lib) {
Darin Petkov65b01462010-04-14 13:32:20 -0700112 testing_ = testing;
Darin Petkovfc91b422010-05-12 13:05:45 -0700113 DCHECK(metrics_lib != NULL);
114 metrics_lib_ = metrics_lib;
Darin Petkov2ccef012010-05-05 16:06:37 -0700115 daily_use_record_file_ = kDailyUseRecordFile;
116
117 // Don't setup D-Bus and GLib in test mode.
118 if (testing)
119 return;
Darin Petkov65b01462010-04-14 13:32:20 -0700120
Darin Petkov703ec972010-04-27 11:02:18 -0700121 g_thread_init(NULL);
122 g_type_init();
123 dbus_g_thread_init();
Darin Petkov65b01462010-04-14 13:32:20 -0700124
Darin Petkov703ec972010-04-27 11:02:18 -0700125 DBusError error;
126 dbus_error_init(&error);
Darin Petkov65b01462010-04-14 13:32:20 -0700127
David James3b3add52010-06-04 15:01:19 -0700128 DBusConnection* connection = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
Darin Petkov703ec972010-04-27 11:02:18 -0700129 LOG_IF(FATAL, dbus_error_is_set(&error)) <<
130 "No D-Bus connection: " << SAFE_MESSAGE(error);
Darin Petkov65b01462010-04-14 13:32:20 -0700131
Darin Petkov703ec972010-04-27 11:02:18 -0700132 dbus_connection_setup_with_g_main(connection, NULL);
Darin Petkov65b01462010-04-14 13:32:20 -0700133
Darin Petkov703ec972010-04-27 11:02:18 -0700134 // Registers D-Bus matches for the signals we would like to catch.
David James3b3add52010-06-04 15:01:19 -0700135 for (unsigned int m = 0; m < sizeof(kDBusMatches_) / sizeof(char*); m++) {
Darin Petkov41e06232010-05-03 16:45:37 -0700136 const char* match = kDBusMatches_[m];
137 DLOG(INFO) << "adding dbus match: " << match;
Darin Petkov703ec972010-04-27 11:02:18 -0700138 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 Petkov65b01462010-04-14 13:32:20 -0700147}
148
149void MetricsDaemon::Loop() {
Darin Petkov703ec972010-04-27 11:02:18 -0700150 GMainLoop* loop = g_main_loop_new(NULL, false);
151 g_main_loop_run(loop);
Darin Petkov65b01462010-04-14 13:32:20 -0700152}
153
Darin Petkov703ec972010-04-27 11:02:18 -0700154// static
155DBusHandlerResult MetricsDaemon::MessageFilter(DBusConnection* connection,
156 DBusMessage* message,
157 void* user_data) {
Darin Petkovf27f0362010-06-04 13:14:19 -0700158 Time now = Time::Now();
159 TimeTicks ticks = TimeTicks::Now();
160 DLOG(INFO) << "message intercepted @ " << now.ToInternalValue();
Darin Petkov703ec972010-04-27 11:02:18 -0700161
162 int message_type = dbus_message_get_type(message);
163 if (message_type != DBUS_MESSAGE_TYPE_SIGNAL) {
Darin Petkov41e06232010-05-03 16:45:37 -0700164 DLOG(WARNING) << "unexpected message type " << message_type;
Darin Petkov703ec972010-04-27 11:02:18 -0700165 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 Petkove3348402010-06-04 14:07:41 -0700176 if (strcmp(interface, DBUS_IFACE_FLIMFLAM_MANAGER) == 0) {
Darin Petkov41e06232010-05-03 16:45:37 -0700177 CHECK(strcmp(dbus_message_get_member(message),
178 "StateChanged") == 0);
Darin Petkov703ec972010-04-27 11:02:18 -0700179
David James3b3add52010-06-04 15:01:19 -0700180 char* state_name;
Darin Petkov703ec972010-04-27 11:02:18 -0700181 dbus_message_iter_get_basic(&iter, &state_name);
Darin Petkovf27f0362010-06-04 13:14:19 -0700182 daemon->NetStateChanged(state_name, ticks);
Darin Petkov703ec972010-04-27 11:02:18 -0700183 } else if (strcmp(interface, DBUS_IFACE_POWER_MANAGER) == 0) {
Darin Petkov41e06232010-05-03 16:45:37 -0700184 CHECK(strcmp(dbus_message_get_member(message),
185 "PowerStateChanged") == 0);
Darin Petkov703ec972010-04-27 11:02:18 -0700186
David James3b3add52010-06-04 15:01:19 -0700187 char* state_name;
Darin Petkov703ec972010-04-27 11:02:18 -0700188 dbus_message_iter_get_basic(&iter, &state_name);
Darin Petkov41e06232010-05-03 16:45:37 -0700189 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 James3b3add52010-06-04 15:01:19 -0700194 char* state_name;
Darin Petkov41e06232010-05-03 16:45:37 -0700195 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 James3b3add52010-06-04 15:01:19 -0700201 char* state_name;
Darin Petkov41e06232010-05-03 16:45:37 -0700202 dbus_message_iter_get_basic(&iter, &state_name);
203 daemon->SessionStateChanged(state_name, now);
Darin Petkov703ec972010-04-27 11:02:18 -0700204 } else {
Darin Petkov41e06232010-05-03 16:45:37 -0700205 DLOG(WARNING) << "unexpected interface: " << interface;
Darin Petkov703ec972010-04-27 11:02:18 -0700206 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
207 }
208
209 return DBUS_HANDLER_RESULT_HANDLED;
Darin Petkov65b01462010-04-14 13:32:20 -0700210}
211
Darin Petkovf27f0362010-06-04 13:14:19 -0700212void MetricsDaemon::NetStateChanged(const char* state_name, TimeTicks ticks) {
Darin Petkov41e06232010-05-03 16:45:37 -0700213 DLOG(INFO) << "network state: " << state_name;
Darin Petkov703ec972010-04-27 11:02:18 -0700214
Darin Petkov703ec972010-04-27 11:02:18 -0700215 NetworkState state = LookupNetworkState(state_name);
216
217 // Logs the time in seconds between the network going online to
Darin Petkov2ccef012010-05-05 16:06:37 -0700218 // 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 Petkov703ec972010-04-27 11:02:18 -0700224 network_state_ == kNetworkStateOnline &&
225 power_state_ != kPowerStateMem) {
Darin Petkovf27f0362010-06-04 13:14:19 -0700226 TimeDelta since_online = ticks - network_state_last_;
227 int online_time = static_cast<int>(since_online.InSeconds());
Darin Petkov11b8eb32010-05-18 11:00:59 -0700228 SendMetric(kMetricTimeToNetworkDropName, online_time,
229 kMetricTimeToNetworkDropMin,
230 kMetricTimeToNetworkDropMax,
231 kMetricTimeToNetworkDropBuckets);
Darin Petkov65b01462010-04-14 13:32:20 -0700232 }
233
Darin Petkov703ec972010-04-27 11:02:18 -0700234 network_state_ = state;
Darin Petkovf27f0362010-06-04 13:14:19 -0700235 network_state_last_ = ticks;
Darin Petkov65b01462010-04-14 13:32:20 -0700236}
237
Darin Petkov703ec972010-04-27 11:02:18 -0700238MetricsDaemon::NetworkState
239MetricsDaemon::LookupNetworkState(const char* state_name) {
Darin Petkov65b01462010-04-14 13:32:20 -0700240 for (int i = 0; i < kNumberNetworkStates; i++) {
Darin Petkov41e06232010-05-03 16:45:37 -0700241 if (strcmp(state_name, kNetworkStates_[i]) == 0) {
Darin Petkov703ec972010-04-27 11:02:18 -0700242 return static_cast<NetworkState>(i);
Darin Petkov65b01462010-04-14 13:32:20 -0700243 }
244 }
Darin Petkov41e06232010-05-03 16:45:37 -0700245 DLOG(WARNING) << "unknown network connection state: " << state_name;
Darin Petkov703ec972010-04-27 11:02:18 -0700246 return kUnknownNetworkState;
247}
248
Darin Petkovf27f0362010-06-04 13:14:19 -0700249void MetricsDaemon::PowerStateChanged(const char* state_name, Time now) {
Darin Petkov41e06232010-05-03 16:45:37 -0700250 DLOG(INFO) << "power state: " << state_name;
Darin Petkov703ec972010-04-27 11:02:18 -0700251 power_state_ = LookupPowerState(state_name);
Darin Petkov41e06232010-05-03 16:45:37 -0700252
253 if (power_state_ != kPowerStateOn)
254 SetUserActiveState(false, now);
Darin Petkov703ec972010-04-27 11:02:18 -0700255}
256
257MetricsDaemon::PowerState
258MetricsDaemon::LookupPowerState(const char* state_name) {
259 for (int i = 0; i < kNumberPowerStates; i++) {
Darin Petkov41e06232010-05-03 16:45:37 -0700260 if (strcmp(state_name, kPowerStates_[i]) == 0) {
Darin Petkov703ec972010-04-27 11:02:18 -0700261 return static_cast<PowerState>(i);
262 }
263 }
Darin Petkov41e06232010-05-03 16:45:37 -0700264 DLOG(WARNING) << "unknown power state: " << state_name;
Darin Petkov703ec972010-04-27 11:02:18 -0700265 return kUnknownPowerState;
Darin Petkov65b01462010-04-14 13:32:20 -0700266}
267
Darin Petkovf27f0362010-06-04 13:14:19 -0700268void MetricsDaemon::ScreenSaverStateChanged(const char* state_name, Time now) {
Darin Petkov41e06232010-05-03 16:45:37 -0700269 DLOG(INFO) << "screen-saver state: " << state_name;
270 screensaver_state_ = LookupScreenSaverState(state_name);
271 SetUserActiveState(screensaver_state_ == kScreenSaverStateUnlocked, now);
272}
273
274MetricsDaemon::ScreenSaverState
275MetricsDaemon::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 Petkovf27f0362010-06-04 13:14:19 -0700285void MetricsDaemon::SessionStateChanged(const char* state_name, Time now) {
Darin Petkov41e06232010-05-03 16:45:37 -0700286 DLOG(INFO) << "user session state: " << state_name;
287 session_state_ = LookupSessionState(state_name);
288 SetUserActiveState(session_state_ == kSessionStateStarted, now);
289}
290
291MetricsDaemon::SessionState
292MetricsDaemon::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 Petkovf27f0362010-06-04 13:14:19 -0700302void MetricsDaemon::SetUserActiveState(bool active, Time now) {
Darin Petkov41e06232010-05-03 16:45:37 -0700303 DLOG(INFO) << "user: " << (active ? "active" : "inactive");
304
305 // Calculates the seconds of active use since the last update and
Darin Petkovf27f0362010-06-04 13:14:19 -0700306 // 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 Petkov41e06232010-05-03 16:45:37 -0700319 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
334void 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 Petkov2ccef012010-05-05 16:06:37 -0700341 int fd = HANDLE_EINTR(open(daily_use_record_file_,
Darin Petkov41e06232010-05-03 16:45:37 -0700342 O_RDWR | O_CREAT,
343 S_IRUSR | S_IWUSR));
344 if (fd < 0) {
Darin Petkov2ccef012010-05-05 16:06:37 -0700345 PLOG(WARNING) << "Unable to open the daily use file";
Darin Petkov41e06232010-05-03 16:45:37 -0700346 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 Petkov11b8eb32010-05-18 11:00:59 -0700362 SendMetric(kMetricDailyUseTimeName, minutes,
363 kMetricDailyUseTimeMin,
364 kMetricDailyUseTimeMax,
365 kMetricDailyUseTimeBuckets);
Darin Petkov41e06232010-05-03 16:45:37 -0700366
367 // Truncates the usage file to ensure that no duplicate usage is
368 // sent to UMA.
Darin Petkov2ccef012010-05-05 16:06:37 -0700369 PLOG_IF(WARNING, HANDLE_EINTR(ftruncate(fd, 0)) != 0);
Darin Petkov41e06232010-05-03 16:45:37 -0700370 }
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 Petkov2ccef012010-05-05 16:06:37 -0700383 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 Petkov41e06232010-05-03 16:45:37 -0700387 }
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
399gboolean MetricsDaemon::UseMonitorStatic(gpointer data) {
400 return static_cast<MetricsDaemon*>(data)->UseMonitor() ? TRUE : FALSE;
401}
402
403bool MetricsDaemon::UseMonitor() {
Darin Petkovf27f0362010-06-04 13:14:19 -0700404 SetUserActiveState(user_active_, Time::Now());
Darin Petkov41e06232010-05-03 16:45:37 -0700405
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
412bool MetricsDaemon::ScheduleUseMonitor(int interval, bool backoff)
413{
Darin Petkov2ccef012010-05-05 16:06:37 -0700414 if (testing_)
415 return false;
416
Darin Petkov41e06232010-05-03 16:45:37 -0700417 // 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
447void 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 Petkov11b8eb32010-05-18 11:00:59 -0700458void MetricsDaemon::SendMetric(const std::string& name, int sample,
459 int min, int max, int nbuckets) {
Darin Petkovfc91b422010-05-12 13:05:45 -0700460 DLOG(INFO) << "received metric: " << name << " " << sample << " "
461 << min << " " << max << " " << nbuckets;
462 metrics_lib_->SendToUMA(name, sample, min, max, nbuckets);
Darin Petkov65b01462010-04-14 13:32:20 -0700463}