blob: ea4771fc7bf643ab59cbe059b5ac8b55c4cac574 [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),
Darin Petkov41e06232010-05-03 16:45:37 -070023 session_state_(kUnknownSessionState),
24 user_active_(false),
Darin Petkov41e06232010-05-03 16:45:37 -070025 daily_use_day_last_(0),
26 usemon_interval_(0),
27 usemon_source_(NULL) {}
Darin Petkov65b01462010-04-14 13:32:20 -070028 ~MetricsDaemon() {}
29
Darin Petkov11b8eb32010-05-18 11:00:59 -070030 // Initializes.
31 void Init(bool testing, MetricsLibraryInterface* metrics_lib);
32
Darin Petkov703ec972010-04-27 11:02:18 -070033 // Does all the work. If |run_as_daemon| is true, daemonizes by
Darin Petkov2ccef012010-05-05 16:06:37 -070034 // forking.
35 void Run(bool run_as_daemon);
Darin Petkov65b01462010-04-14 13:32:20 -070036
37 private:
Darin Petkov2ccef012010-05-05 16:06:37 -070038 friend class MetricsDaemonTest;
Darin Petkovfc91b422010-05-12 13:05:45 -070039 FRIEND_TEST(MetricsDaemonTest, LogDailyUseRecordBadFileLocation);
40 FRIEND_TEST(MetricsDaemonTest, LogDailyUseRecordOnLogin);
41 FRIEND_TEST(MetricsDaemonTest, LogDailyUseRecordRoundDown);
42 FRIEND_TEST(MetricsDaemonTest, LogDailyUseRecordRoundUp);
Darin Petkov2ccef012010-05-05 16:06:37 -070043 FRIEND_TEST(MetricsDaemonTest, LookupNetworkState);
44 FRIEND_TEST(MetricsDaemonTest, LookupPowerState);
45 FRIEND_TEST(MetricsDaemonTest, LookupScreenSaverState);
46 FRIEND_TEST(MetricsDaemonTest, LookupSessionState);
Darin Petkove579d662010-05-05 16:19:39 -070047 FRIEND_TEST(MetricsDaemonTest, MessageFilter);
Darin Petkovfc91b422010-05-12 13:05:45 -070048 FRIEND_TEST(MetricsDaemonTest, NetStateChangedSimpleDrop);
49 FRIEND_TEST(MetricsDaemonTest, NetStateChangedSuspend);
Darin Petkov2ccef012010-05-05 16:06:37 -070050 FRIEND_TEST(MetricsDaemonTest, PowerStateChanged);
Darin Petkov2ccef012010-05-05 16:06:37 -070051 FRIEND_TEST(MetricsDaemonTest, ScreenSaverStateChanged);
Darin Petkov11b8eb32010-05-18 11:00:59 -070052 FRIEND_TEST(MetricsDaemonTest, SendMetric);
Darin Petkov2ccef012010-05-05 16:06:37 -070053 FRIEND_TEST(MetricsDaemonTest, SessionStateChanged);
Darin Petkovfc91b422010-05-12 13:05:45 -070054 FRIEND_TEST(MetricsDaemonTest, SetUserActiveStateSendOnLogin);
55 FRIEND_TEST(MetricsDaemonTest, SetUserActiveStateSendOnMonitor);
Darin Petkovf27f0362010-06-04 13:14:19 -070056 FRIEND_TEST(MetricsDaemonTest, SetUserActiveStateTimeJump);
Darin Petkov2ccef012010-05-05 16:06:37 -070057
Darin Petkov703ec972010-04-27 11:02:18 -070058 // The network states (see network_states.h).
59 enum NetworkState {
60 kUnknownNetworkState = -1, // Initial/unknown network state.
Darin Petkov65b01462010-04-14 13:32:20 -070061#define STATE(name, capname) kNetworkState ## capname,
62#include "network_states.h"
63 kNumberNetworkStates
Darin Petkov703ec972010-04-27 11:02:18 -070064 };
Darin Petkov65b01462010-04-14 13:32:20 -070065
Darin Petkov703ec972010-04-27 11:02:18 -070066 // The power states (see power_states.h).
67 enum PowerState {
68 kUnknownPowerState = -1, // Initial/unknown power state.
69#define STATE(name, capname) kPowerState ## capname,
70#include "power_states.h"
71 kNumberPowerStates
72 };
Darin Petkov65b01462010-04-14 13:32:20 -070073
Darin Petkov41e06232010-05-03 16:45:37 -070074 // The user session states (see session_states.h).
75 enum SessionState {
76 kUnknownSessionState = -1, // Initial/unknown user session state.
77#define STATE(name, capname) kSessionState ## capname,
78#include "session_states.h"
79 kNumberSessionStates
80 };
81
82 // Data record for aggregating daily usage.
83 class UseRecord {
84 public:
85 UseRecord() : day_(0), seconds_(0) {}
86 int day_;
87 int seconds_;
88 };
89
Darin Petkov2ccef012010-05-05 16:06:37 -070090 // Metric parameters.
91 static const char kMetricDailyUseTimeName[];
92 static const int kMetricDailyUseTimeMin;
93 static const int kMetricDailyUseTimeMax;
94 static const int kMetricDailyUseTimeBuckets;
95 static const char kMetricTimeToNetworkDropName[];
96 static const int kMetricTimeToNetworkDropMin;
97 static const int kMetricTimeToNetworkDropMax;
98 static const int kMetricTimeToNetworkDropBuckets;
99
Darin Petkov41e06232010-05-03 16:45:37 -0700100 // D-Bus message match strings.
101 static const char* kDBusMatches_[];
102
103 // Array of network states.
104 static const char* kNetworkStates_[kNumberNetworkStates];
105
106 // Array of power states.
107 static const char* kPowerStates_[kNumberPowerStates];
108
Darin Petkov41e06232010-05-03 16:45:37 -0700109 // Array of user session states.
110 static const char* kSessionStates_[kNumberSessionStates];
111
Darin Petkov65b01462010-04-14 13:32:20 -0700112 // Creates the event loop and enters it.
113 void Loop();
114
Darin Petkov703ec972010-04-27 11:02:18 -0700115 // D-Bus filter callback.
116 static DBusHandlerResult MessageFilter(DBusConnection* connection,
117 DBusMessage* message,
118 void* user_data);
Darin Petkov65b01462010-04-14 13:32:20 -0700119
Darin Petkov703ec972010-04-27 11:02:18 -0700120 // Processes network state change.
Darin Petkovf27f0362010-06-04 13:14:19 -0700121 void NetStateChanged(const char* state_name, base::TimeTicks ticks);
Darin Petkov65b01462010-04-14 13:32:20 -0700122
Darin Petkov703ec972010-04-27 11:02:18 -0700123 // Given the state name, returns the state id.
124 NetworkState LookupNetworkState(const char* state_name);
Darin Petkov65b01462010-04-14 13:32:20 -0700125
Darin Petkov703ec972010-04-27 11:02:18 -0700126 // Processes power state change.
Darin Petkovf27f0362010-06-04 13:14:19 -0700127 void PowerStateChanged(const char* state_name, base::Time now);
Darin Petkov703ec972010-04-27 11:02:18 -0700128
129 // Given the state name, returns the state id.
130 PowerState LookupPowerState(const char* state_name);
Darin Petkov65b01462010-04-14 13:32:20 -0700131
Darin Petkov41e06232010-05-03 16:45:37 -0700132 // Processes user session state change.
Darin Petkovf27f0362010-06-04 13:14:19 -0700133 void SessionStateChanged(const char* state_name, base::Time now);
Darin Petkov41e06232010-05-03 16:45:37 -0700134
135 // Given the state name, returns the state id.
136 SessionState LookupSessionState(const char* state_name);
137
138 // Updates the user-active state to |active| and logs the usage data
139 // since the last update. If the user has just become active,
140 // reschedule the daily use monitor for more frequent updates --
141 // this is followed by an exponential back-off (see UseMonitor).
Darin Petkovf27f0362010-06-04 13:14:19 -0700142 // While in active use, this method should be called at intervals no
143 // longer than kUseMonitorIntervalMax otherwise new use time will be
144 // discarded.
145 void SetUserActiveState(bool active, base::Time now);
Darin Petkov41e06232010-05-03 16:45:37 -0700146
147 // Updates the daily usage file, if necessary, by adding |seconds|
148 // of active use to the |day| since Epoch. If there's usage data for
149 // day in the past in the usage file, that data is sent to UMA and
150 // removed from the file. If there's already usage data for |day| in
151 // the usage file, the |seconds| are accumulated.
152 void LogDailyUseRecord(int day, int seconds);
153
154 // Callbacks for the daily use monitor. The daily use monitor uses
155 // LogDailyUseRecord to aggregate current usage data and send it to
156 // UMA, if necessary. It also reschedules itself using an
157 // exponentially bigger interval (up to a certain maximum) -- so
158 // usage is monitored less frequently with longer active use.
159 static gboolean UseMonitorStatic(gpointer data);
160 bool UseMonitor();
161
162 // Schedules or reschedules a daily use monitor for |interval|
163 // seconds from now. |backoff| mode is used by the use monitor to
164 // reschedule itself. If there's a monitor scheduled already and
165 // |backoff| is false, unschedules it first. Doesn't schedule a
166 // monitor for more than kUseMonitorIntervalMax seconds in the
167 // future (see metrics_daemon.cc). Returns true if a new use monitor
168 // was scheduled, false otherwise (note that if |backoff| is false a
169 // new use monitor will always be scheduled).
170 bool ScheduleUseMonitor(int interval, bool backoff);
171
172 // Unschedules a scheduled use monitor, if any.
173 void UnscheduleUseMonitor();
174
Darin Petkov11b8eb32010-05-18 11:00:59 -0700175 // Sends a regular (exponential) histogram sample to Chrome for
176 // transport to UMA. See MetricsLibrary::SendToUMA in
177 // metrics_library.h for a description of the arguments.
178 void SendMetric(const std::string& name, int sample,
179 int min, int max, int nbuckets);
Darin Petkov65b01462010-04-14 13:32:20 -0700180
Darin Petkov2ccef012010-05-05 16:06:37 -0700181 // Test mode.
Darin Petkov41e06232010-05-03 16:45:37 -0700182 bool testing_;
Darin Petkov65b01462010-04-14 13:32:20 -0700183
Darin Petkovfc91b422010-05-12 13:05:45 -0700184 // The metrics library handle.
185 MetricsLibraryInterface* metrics_lib_;
186
Darin Petkov2ccef012010-05-05 16:06:37 -0700187 const char* daily_use_record_file_;
188
Darin Petkov41e06232010-05-03 16:45:37 -0700189 // Current network state.
190 NetworkState network_state_;
Darin Petkov65b01462010-04-14 13:32:20 -0700191
Darin Petkovf27f0362010-06-04 13:14:19 -0700192 // Timestamps last network state update. This timestamp is used to
193 // sample the time from the network going online to going offline so
194 // TimeTicks ensures a monotonically increasing TimeDelta.
195 base::TimeTicks network_state_last_;
Darin Petkov65b01462010-04-14 13:32:20 -0700196
Darin Petkov41e06232010-05-03 16:45:37 -0700197 // Current power state.
198 PowerState power_state_;
199
Darin Petkov41e06232010-05-03 16:45:37 -0700200 // Current user session state.
201 SessionState session_state_;
202
203 // Is the user currently active: power is on, user session has
204 // started, screen is not locked.
205 bool user_active_;
206
Darin Petkovf27f0362010-06-04 13:14:19 -0700207 // Timestamps last user active update. Active use time is
208 // aggregated each day before sending to UMA so using time since the
209 // epoch as the timestamp.
210 base::Time user_active_last_;
Darin Petkov41e06232010-05-03 16:45:37 -0700211
Darin Petkovf27f0362010-06-04 13:14:19 -0700212 // Last stored daily use day (since the epoch).
Darin Petkov41e06232010-05-03 16:45:37 -0700213 int daily_use_day_last_;
214
215 // Sleep period until the next daily usage aggregation performed by
216 // the daily use monitor (see ScheduleUseMonitor).
217 int usemon_interval_;
218
219 // Scheduled daily use monitor source (see ScheduleUseMonitor).
220 GSource* usemon_source_;
Darin Petkov65b01462010-04-14 13:32:20 -0700221};
222
223#endif // METRICS_DAEMON_H_