blob: 835aca36a168a3026f831d4d015efa52ee326b6e [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#ifndef METRICS_DAEMON_H_
6#define METRICS_DAEMON_H_
7
Darin Petkov703ec972010-04-27 11:02:18 -07008#include <dbus/dbus.h>
Darin Petkov41e06232010-05-03 16:45:37 -07009#include <glib.h>
Darin Petkov65b01462010-04-14 13:32:20 -070010
Darin Petkovfc91b422010-05-12 13:05:45 -070011#include "metrics_library.h"
12
Darin Petkov2ccef012010-05-05 16:06:37 -070013#include <gtest/gtest_prod.h> // for FRIEND_TEST
Darin Petkovf27f0362010-06-04 13:14:19 -070014#include <base/time.h>
Darin Petkov2ccef012010-05-05 16:06:37 -070015
Darin Petkov65b01462010-04-14 13:32:20 -070016class MetricsDaemon {
17
18 public:
19 MetricsDaemon()
Darin Petkov2ccef012010-05-05 16:06:37 -070020 : daily_use_record_file_(NULL),
21 network_state_(kUnknownNetworkState),
Darin Petkov41e06232010-05-03 16:45:37 -070022 power_state_(kUnknownPowerState),
23 screensaver_state_(kUnknownScreenSaverState),
24 session_state_(kUnknownSessionState),
25 user_active_(false),
Darin Petkov41e06232010-05-03 16:45:37 -070026 daily_use_day_last_(0),
27 usemon_interval_(0),
28 usemon_source_(NULL) {}
Darin Petkov65b01462010-04-14 13:32:20 -070029 ~MetricsDaemon() {}
30
Darin Petkov11b8eb32010-05-18 11:00:59 -070031 // Initializes.
32 void Init(bool testing, MetricsLibraryInterface* metrics_lib);
33
Darin Petkov703ec972010-04-27 11:02:18 -070034 // Does all the work. If |run_as_daemon| is true, daemonizes by
Darin Petkov2ccef012010-05-05 16:06:37 -070035 // forking.
36 void Run(bool run_as_daemon);
Darin Petkov65b01462010-04-14 13:32:20 -070037
38 private:
Darin Petkov2ccef012010-05-05 16:06:37 -070039 friend class MetricsDaemonTest;
Darin Petkovfc91b422010-05-12 13:05:45 -070040 FRIEND_TEST(MetricsDaemonTest, LogDailyUseRecordBadFileLocation);
41 FRIEND_TEST(MetricsDaemonTest, LogDailyUseRecordOnLogin);
42 FRIEND_TEST(MetricsDaemonTest, LogDailyUseRecordRoundDown);
43 FRIEND_TEST(MetricsDaemonTest, LogDailyUseRecordRoundUp);
Darin Petkov2ccef012010-05-05 16:06:37 -070044 FRIEND_TEST(MetricsDaemonTest, LookupNetworkState);
45 FRIEND_TEST(MetricsDaemonTest, LookupPowerState);
46 FRIEND_TEST(MetricsDaemonTest, LookupScreenSaverState);
47 FRIEND_TEST(MetricsDaemonTest, LookupSessionState);
Darin Petkove579d662010-05-05 16:19:39 -070048 FRIEND_TEST(MetricsDaemonTest, MessageFilter);
Darin Petkovfc91b422010-05-12 13:05:45 -070049 FRIEND_TEST(MetricsDaemonTest, NetStateChangedSimpleDrop);
50 FRIEND_TEST(MetricsDaemonTest, NetStateChangedSuspend);
Darin Petkov2ccef012010-05-05 16:06:37 -070051 FRIEND_TEST(MetricsDaemonTest, PowerStateChanged);
Darin Petkov2ccef012010-05-05 16:06:37 -070052 FRIEND_TEST(MetricsDaemonTest, ScreenSaverStateChanged);
Darin Petkov11b8eb32010-05-18 11:00:59 -070053 FRIEND_TEST(MetricsDaemonTest, SendMetric);
Darin Petkov2ccef012010-05-05 16:06:37 -070054 FRIEND_TEST(MetricsDaemonTest, SessionStateChanged);
Darin Petkovfc91b422010-05-12 13:05:45 -070055 FRIEND_TEST(MetricsDaemonTest, SetUserActiveStateSendOnLogin);
56 FRIEND_TEST(MetricsDaemonTest, SetUserActiveStateSendOnMonitor);
Darin Petkovf27f0362010-06-04 13:14:19 -070057 FRIEND_TEST(MetricsDaemonTest, SetUserActiveStateTimeJump);
Darin Petkov2ccef012010-05-05 16:06:37 -070058
Darin Petkov703ec972010-04-27 11:02:18 -070059 // The network states (see network_states.h).
60 enum NetworkState {
61 kUnknownNetworkState = -1, // Initial/unknown network state.
Darin Petkov65b01462010-04-14 13:32:20 -070062#define STATE(name, capname) kNetworkState ## capname,
63#include "network_states.h"
64 kNumberNetworkStates
Darin Petkov703ec972010-04-27 11:02:18 -070065 };
Darin Petkov65b01462010-04-14 13:32:20 -070066
Darin Petkov703ec972010-04-27 11:02:18 -070067 // The power states (see power_states.h).
68 enum PowerState {
69 kUnknownPowerState = -1, // Initial/unknown power state.
70#define STATE(name, capname) kPowerState ## capname,
71#include "power_states.h"
72 kNumberPowerStates
73 };
Darin Petkov65b01462010-04-14 13:32:20 -070074
Darin Petkov41e06232010-05-03 16:45:37 -070075 // The screen-saver states (see screensaver_states.h).
76 enum ScreenSaverState {
77 kUnknownScreenSaverState = -1, // Initial/unknown screen-saver state.
78#define STATE(name, capname) kScreenSaverState ## capname,
79#include "screensaver_states.h"
80 kNumberScreenSaverStates
81 };
82
83 // The user session states (see session_states.h).
84 enum SessionState {
85 kUnknownSessionState = -1, // Initial/unknown user session state.
86#define STATE(name, capname) kSessionState ## capname,
87#include "session_states.h"
88 kNumberSessionStates
89 };
90
91 // Data record for aggregating daily usage.
92 class UseRecord {
93 public:
94 UseRecord() : day_(0), seconds_(0) {}
95 int day_;
96 int seconds_;
97 };
98
Darin Petkov2ccef012010-05-05 16:06:37 -070099 // Metric parameters.
100 static const char kMetricDailyUseTimeName[];
101 static const int kMetricDailyUseTimeMin;
102 static const int kMetricDailyUseTimeMax;
103 static const int kMetricDailyUseTimeBuckets;
104 static const char kMetricTimeToNetworkDropName[];
105 static const int kMetricTimeToNetworkDropMin;
106 static const int kMetricTimeToNetworkDropMax;
107 static const int kMetricTimeToNetworkDropBuckets;
108
Darin Petkov41e06232010-05-03 16:45:37 -0700109 // D-Bus message match strings.
110 static const char* kDBusMatches_[];
111
112 // Array of network states.
113 static const char* kNetworkStates_[kNumberNetworkStates];
114
115 // Array of power states.
116 static const char* kPowerStates_[kNumberPowerStates];
117
118 // Array of screen-saver states.
119 static const char* kScreenSaverStates_[kNumberScreenSaverStates];
120
121 // Array of user session states.
122 static const char* kSessionStates_[kNumberSessionStates];
123
Darin Petkov65b01462010-04-14 13:32:20 -0700124 // Creates the event loop and enters it.
125 void Loop();
126
Darin Petkov703ec972010-04-27 11:02:18 -0700127 // D-Bus filter callback.
128 static DBusHandlerResult MessageFilter(DBusConnection* connection,
129 DBusMessage* message,
130 void* user_data);
Darin Petkov65b01462010-04-14 13:32:20 -0700131
Darin Petkov703ec972010-04-27 11:02:18 -0700132 // Processes network state change.
Darin Petkovf27f0362010-06-04 13:14:19 -0700133 void NetStateChanged(const char* state_name, base::TimeTicks ticks);
Darin Petkov65b01462010-04-14 13:32:20 -0700134
Darin Petkov703ec972010-04-27 11:02:18 -0700135 // Given the state name, returns the state id.
136 NetworkState LookupNetworkState(const char* state_name);
Darin Petkov65b01462010-04-14 13:32:20 -0700137
Darin Petkov703ec972010-04-27 11:02:18 -0700138 // Processes power state change.
Darin Petkovf27f0362010-06-04 13:14:19 -0700139 void PowerStateChanged(const char* state_name, base::Time now);
Darin Petkov703ec972010-04-27 11:02:18 -0700140
141 // Given the state name, returns the state id.
142 PowerState LookupPowerState(const char* state_name);
Darin Petkov65b01462010-04-14 13:32:20 -0700143
Darin Petkov41e06232010-05-03 16:45:37 -0700144 // Processes screen-saver state change.
Darin Petkovf27f0362010-06-04 13:14:19 -0700145 void ScreenSaverStateChanged(const char* state_name, base::Time now);
Darin Petkov41e06232010-05-03 16:45:37 -0700146
147 // Given the state name, returns the state id.
148 ScreenSaverState LookupScreenSaverState(const char* state_name);
149
150 // Processes user session state change.
Darin Petkovf27f0362010-06-04 13:14:19 -0700151 void SessionStateChanged(const char* state_name, base::Time now);
Darin Petkov41e06232010-05-03 16:45:37 -0700152
153 // Given the state name, returns the state id.
154 SessionState LookupSessionState(const char* state_name);
155
156 // Updates the user-active state to |active| and logs the usage data
157 // since the last update. If the user has just become active,
158 // reschedule the daily use monitor for more frequent updates --
159 // this is followed by an exponential back-off (see UseMonitor).
Darin Petkovf27f0362010-06-04 13:14:19 -0700160 // While in active use, this method should be called at intervals no
161 // longer than kUseMonitorIntervalMax otherwise new use time will be
162 // discarded.
163 void SetUserActiveState(bool active, base::Time now);
Darin Petkov41e06232010-05-03 16:45:37 -0700164
165 // Updates the daily usage file, if necessary, by adding |seconds|
166 // of active use to the |day| since Epoch. If there's usage data for
167 // day in the past in the usage file, that data is sent to UMA and
168 // removed from the file. If there's already usage data for |day| in
169 // the usage file, the |seconds| are accumulated.
170 void LogDailyUseRecord(int day, int seconds);
171
172 // Callbacks for the daily use monitor. The daily use monitor uses
173 // LogDailyUseRecord to aggregate current usage data and send it to
174 // UMA, if necessary. It also reschedules itself using an
175 // exponentially bigger interval (up to a certain maximum) -- so
176 // usage is monitored less frequently with longer active use.
177 static gboolean UseMonitorStatic(gpointer data);
178 bool UseMonitor();
179
180 // Schedules or reschedules a daily use monitor for |interval|
181 // seconds from now. |backoff| mode is used by the use monitor to
182 // reschedule itself. If there's a monitor scheduled already and
183 // |backoff| is false, unschedules it first. Doesn't schedule a
184 // monitor for more than kUseMonitorIntervalMax seconds in the
185 // future (see metrics_daemon.cc). Returns true if a new use monitor
186 // was scheduled, false otherwise (note that if |backoff| is false a
187 // new use monitor will always be scheduled).
188 bool ScheduleUseMonitor(int interval, bool backoff);
189
190 // Unschedules a scheduled use monitor, if any.
191 void UnscheduleUseMonitor();
192
Darin Petkov11b8eb32010-05-18 11:00:59 -0700193 // Sends a regular (exponential) histogram sample to Chrome for
194 // transport to UMA. See MetricsLibrary::SendToUMA in
195 // metrics_library.h for a description of the arguments.
196 void SendMetric(const std::string& name, int sample,
197 int min, int max, int nbuckets);
Darin Petkov65b01462010-04-14 13:32:20 -0700198
Darin Petkov2ccef012010-05-05 16:06:37 -0700199 // Test mode.
Darin Petkov41e06232010-05-03 16:45:37 -0700200 bool testing_;
Darin Petkov65b01462010-04-14 13:32:20 -0700201
Darin Petkovfc91b422010-05-12 13:05:45 -0700202 // The metrics library handle.
203 MetricsLibraryInterface* metrics_lib_;
204
Darin Petkov2ccef012010-05-05 16:06:37 -0700205 const char* daily_use_record_file_;
206
Darin Petkov41e06232010-05-03 16:45:37 -0700207 // Current network state.
208 NetworkState network_state_;
Darin Petkov65b01462010-04-14 13:32:20 -0700209
Darin Petkovf27f0362010-06-04 13:14:19 -0700210 // Timestamps last network state update. This timestamp is used to
211 // sample the time from the network going online to going offline so
212 // TimeTicks ensures a monotonically increasing TimeDelta.
213 base::TimeTicks network_state_last_;
Darin Petkov65b01462010-04-14 13:32:20 -0700214
Darin Petkov41e06232010-05-03 16:45:37 -0700215 // Current power state.
216 PowerState power_state_;
217
218 // Current screen-saver state.
219 ScreenSaverState screensaver_state_;
220
221 // Current user session state.
222 SessionState session_state_;
223
224 // Is the user currently active: power is on, user session has
225 // started, screen is not locked.
226 bool user_active_;
227
Darin Petkovf27f0362010-06-04 13:14:19 -0700228 // Timestamps last user active update. Active use time is
229 // aggregated each day before sending to UMA so using time since the
230 // epoch as the timestamp.
231 base::Time user_active_last_;
Darin Petkov41e06232010-05-03 16:45:37 -0700232
Darin Petkovf27f0362010-06-04 13:14:19 -0700233 // Last stored daily use day (since the epoch).
Darin Petkov41e06232010-05-03 16:45:37 -0700234 int daily_use_day_last_;
235
236 // Sleep period until the next daily usage aggregation performed by
237 // the daily use monitor (see ScheduleUseMonitor).
238 int usemon_interval_;
239
240 // Scheduled daily use monitor source (see ScheduleUseMonitor).
241 GSource* usemon_source_;
Darin Petkov65b01462010-04-14 13:32:20 -0700242};
243
244#endif // METRICS_DAEMON_H_