blob: ee805962003093a9aeb32c892631299e54ccd0e1 [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 Petkovf1e85e42010-06-10 15:59:53 -070011#include <base/scoped_ptr.h>
12#include <base/time.h>
13#include <gtest/gtest_prod.h> // for FRIEND_TEST
14
Darin Petkovfc91b422010-05-12 13:05:45 -070015#include "metrics_library.h"
16
Darin Petkovf1e85e42010-06-10 15:59:53 -070017namespace chromeos_metrics { class TaggedCounterInterface; }
Darin Petkov2ccef012010-05-05 16:06:37 -070018
Darin Petkov65b01462010-04-14 13:32:20 -070019class MetricsDaemon {
20
21 public:
Darin Petkovf1e85e42010-06-10 15:59:53 -070022 MetricsDaemon();
23 ~MetricsDaemon();
Darin Petkov65b01462010-04-14 13:32:20 -070024
Darin Petkov11b8eb32010-05-18 11:00:59 -070025 // Initializes.
26 void Init(bool testing, MetricsLibraryInterface* metrics_lib);
27
Darin Petkov703ec972010-04-27 11:02:18 -070028 // Does all the work. If |run_as_daemon| is true, daemonizes by
Darin Petkov2ccef012010-05-05 16:06:37 -070029 // forking.
30 void Run(bool run_as_daemon);
Darin Petkov65b01462010-04-14 13:32:20 -070031
32 private:
Darin Petkov2ccef012010-05-05 16:06:37 -070033 friend class MetricsDaemonTest;
Darin Petkovf1e85e42010-06-10 15:59:53 -070034 FRIEND_TEST(MetricsDaemonTest, DailyUseReporter);
Darin Petkov2ccef012010-05-05 16:06:37 -070035 FRIEND_TEST(MetricsDaemonTest, LookupNetworkState);
36 FRIEND_TEST(MetricsDaemonTest, LookupPowerState);
37 FRIEND_TEST(MetricsDaemonTest, LookupScreenSaverState);
38 FRIEND_TEST(MetricsDaemonTest, LookupSessionState);
Darin Petkove579d662010-05-05 16:19:39 -070039 FRIEND_TEST(MetricsDaemonTest, MessageFilter);
Darin Petkovfc91b422010-05-12 13:05:45 -070040 FRIEND_TEST(MetricsDaemonTest, NetStateChangedSimpleDrop);
41 FRIEND_TEST(MetricsDaemonTest, NetStateChangedSuspend);
Darin Petkov2ccef012010-05-05 16:06:37 -070042 FRIEND_TEST(MetricsDaemonTest, PowerStateChanged);
Darin Petkov2ccef012010-05-05 16:06:37 -070043 FRIEND_TEST(MetricsDaemonTest, ScreenSaverStateChanged);
Darin Petkov11b8eb32010-05-18 11:00:59 -070044 FRIEND_TEST(MetricsDaemonTest, SendMetric);
Darin Petkov2ccef012010-05-05 16:06:37 -070045 FRIEND_TEST(MetricsDaemonTest, SessionStateChanged);
Darin Petkovf1e85e42010-06-10 15:59:53 -070046 FRIEND_TEST(MetricsDaemonTest, SetUserActiveState);
Darin Petkovf27f0362010-06-04 13:14:19 -070047 FRIEND_TEST(MetricsDaemonTest, SetUserActiveStateTimeJump);
Darin Petkov2ccef012010-05-05 16:06:37 -070048
Darin Petkov703ec972010-04-27 11:02:18 -070049 // The network states (see network_states.h).
50 enum NetworkState {
51 kUnknownNetworkState = -1, // Initial/unknown network state.
Darin Petkov65b01462010-04-14 13:32:20 -070052#define STATE(name, capname) kNetworkState ## capname,
53#include "network_states.h"
54 kNumberNetworkStates
Darin Petkov703ec972010-04-27 11:02:18 -070055 };
Darin Petkov65b01462010-04-14 13:32:20 -070056
Darin Petkov703ec972010-04-27 11:02:18 -070057 // The power states (see power_states.h).
58 enum PowerState {
59 kUnknownPowerState = -1, // Initial/unknown power state.
60#define STATE(name, capname) kPowerState ## capname,
61#include "power_states.h"
62 kNumberPowerStates
63 };
Darin Petkov65b01462010-04-14 13:32:20 -070064
Darin Petkov41e06232010-05-03 16:45:37 -070065 // The user session states (see session_states.h).
66 enum SessionState {
67 kUnknownSessionState = -1, // Initial/unknown user session state.
68#define STATE(name, capname) kSessionState ## capname,
69#include "session_states.h"
70 kNumberSessionStates
71 };
72
73 // Data record for aggregating daily usage.
74 class UseRecord {
75 public:
76 UseRecord() : day_(0), seconds_(0) {}
77 int day_;
78 int seconds_;
79 };
80
Darin Petkov2ccef012010-05-05 16:06:37 -070081 // Metric parameters.
82 static const char kMetricDailyUseTimeName[];
83 static const int kMetricDailyUseTimeMin;
84 static const int kMetricDailyUseTimeMax;
85 static const int kMetricDailyUseTimeBuckets;
86 static const char kMetricTimeToNetworkDropName[];
87 static const int kMetricTimeToNetworkDropMin;
88 static const int kMetricTimeToNetworkDropMax;
89 static const int kMetricTimeToNetworkDropBuckets;
90
Darin Petkov41e06232010-05-03 16:45:37 -070091 // D-Bus message match strings.
92 static const char* kDBusMatches_[];
93
94 // Array of network states.
95 static const char* kNetworkStates_[kNumberNetworkStates];
96
97 // Array of power states.
98 static const char* kPowerStates_[kNumberPowerStates];
99
Darin Petkov41e06232010-05-03 16:45:37 -0700100 // Array of user session states.
101 static const char* kSessionStates_[kNumberSessionStates];
102
Darin Petkov65b01462010-04-14 13:32:20 -0700103 // Creates the event loop and enters it.
104 void Loop();
105
Darin Petkov703ec972010-04-27 11:02:18 -0700106 // D-Bus filter callback.
107 static DBusHandlerResult MessageFilter(DBusConnection* connection,
108 DBusMessage* message,
109 void* user_data);
Darin Petkov65b01462010-04-14 13:32:20 -0700110
Darin Petkov703ec972010-04-27 11:02:18 -0700111 // Processes network state change.
Darin Petkovf27f0362010-06-04 13:14:19 -0700112 void NetStateChanged(const char* state_name, base::TimeTicks ticks);
Darin Petkov65b01462010-04-14 13:32:20 -0700113
Darin Petkov703ec972010-04-27 11:02:18 -0700114 // Given the state name, returns the state id.
115 NetworkState LookupNetworkState(const char* state_name);
Darin Petkov65b01462010-04-14 13:32:20 -0700116
Darin Petkov703ec972010-04-27 11:02:18 -0700117 // Processes power state change.
Darin Petkovf27f0362010-06-04 13:14:19 -0700118 void PowerStateChanged(const char* state_name, base::Time now);
Darin Petkov703ec972010-04-27 11:02:18 -0700119
120 // Given the state name, returns the state id.
121 PowerState LookupPowerState(const char* state_name);
Darin Petkov65b01462010-04-14 13:32:20 -0700122
Darin Petkov41e06232010-05-03 16:45:37 -0700123 // Processes user session state change.
Darin Petkovf27f0362010-06-04 13:14:19 -0700124 void SessionStateChanged(const char* state_name, base::Time now);
Darin Petkov41e06232010-05-03 16:45:37 -0700125
126 // Given the state name, returns the state id.
127 SessionState LookupSessionState(const char* state_name);
128
129 // Updates the user-active state to |active| and logs the usage data
130 // since the last update. If the user has just become active,
131 // reschedule the daily use monitor for more frequent updates --
132 // this is followed by an exponential back-off (see UseMonitor).
Darin Petkovf27f0362010-06-04 13:14:19 -0700133 // While in active use, this method should be called at intervals no
134 // longer than kUseMonitorIntervalMax otherwise new use time will be
135 // discarded.
136 void SetUserActiveState(bool active, base::Time now);
Darin Petkov41e06232010-05-03 16:45:37 -0700137
138 // Updates the daily usage file, if necessary, by adding |seconds|
139 // of active use to the |day| since Epoch. If there's usage data for
140 // day in the past in the usage file, that data is sent to UMA and
141 // removed from the file. If there's already usage data for |day| in
142 // the usage file, the |seconds| are accumulated.
143 void LogDailyUseRecord(int day, int seconds);
144
145 // Callbacks for the daily use monitor. The daily use monitor uses
146 // LogDailyUseRecord to aggregate current usage data and send it to
147 // UMA, if necessary. It also reschedules itself using an
148 // exponentially bigger interval (up to a certain maximum) -- so
149 // usage is monitored less frequently with longer active use.
150 static gboolean UseMonitorStatic(gpointer data);
151 bool UseMonitor();
152
153 // Schedules or reschedules a daily use monitor for |interval|
154 // seconds from now. |backoff| mode is used by the use monitor to
155 // reschedule itself. If there's a monitor scheduled already and
156 // |backoff| is false, unschedules it first. Doesn't schedule a
157 // monitor for more than kUseMonitorIntervalMax seconds in the
158 // future (see metrics_daemon.cc). Returns true if a new use monitor
159 // was scheduled, false otherwise (note that if |backoff| is false a
160 // new use monitor will always be scheduled).
161 bool ScheduleUseMonitor(int interval, bool backoff);
162
163 // Unschedules a scheduled use monitor, if any.
164 void UnscheduleUseMonitor();
165
Darin Petkov11b8eb32010-05-18 11:00:59 -0700166 // Sends a regular (exponential) histogram sample to Chrome for
167 // transport to UMA. See MetricsLibrary::SendToUMA in
168 // metrics_library.h for a description of the arguments.
169 void SendMetric(const std::string& name, int sample,
170 int min, int max, int nbuckets);
Darin Petkov65b01462010-04-14 13:32:20 -0700171
Darin Petkovf1e85e42010-06-10 15:59:53 -0700172 static void DailyUseReporter(void* data, int tag, int count);
173
Darin Petkov2ccef012010-05-05 16:06:37 -0700174 // Test mode.
Darin Petkov41e06232010-05-03 16:45:37 -0700175 bool testing_;
Darin Petkov65b01462010-04-14 13:32:20 -0700176
Darin Petkovfc91b422010-05-12 13:05:45 -0700177 // The metrics library handle.
178 MetricsLibraryInterface* metrics_lib_;
179
Darin Petkov41e06232010-05-03 16:45:37 -0700180 // Current network state.
181 NetworkState network_state_;
Darin Petkov65b01462010-04-14 13:32:20 -0700182
Darin Petkovf27f0362010-06-04 13:14:19 -0700183 // Timestamps last network state update. This timestamp is used to
184 // sample the time from the network going online to going offline so
185 // TimeTicks ensures a monotonically increasing TimeDelta.
186 base::TimeTicks network_state_last_;
Darin Petkov65b01462010-04-14 13:32:20 -0700187
Darin Petkov41e06232010-05-03 16:45:37 -0700188 // Current power state.
189 PowerState power_state_;
190
Darin Petkov41e06232010-05-03 16:45:37 -0700191 // Current user session state.
192 SessionState session_state_;
193
194 // Is the user currently active: power is on, user session has
195 // started, screen is not locked.
196 bool user_active_;
197
Darin Petkovf27f0362010-06-04 13:14:19 -0700198 // Timestamps last user active update. Active use time is
199 // aggregated each day before sending to UMA so using time since the
200 // epoch as the timestamp.
201 base::Time user_active_last_;
Darin Petkov41e06232010-05-03 16:45:37 -0700202
Darin Petkovf1e85e42010-06-10 15:59:53 -0700203 scoped_ptr<chromeos_metrics::TaggedCounterInterface> daily_use_;
Darin Petkov41e06232010-05-03 16:45:37 -0700204
205 // Sleep period until the next daily usage aggregation performed by
206 // the daily use monitor (see ScheduleUseMonitor).
207 int usemon_interval_;
208
209 // Scheduled daily use monitor source (see ScheduleUseMonitor).
210 GSource* usemon_source_;
Darin Petkov65b01462010-04-14 13:32:20 -0700211};
212
213#endif // METRICS_DAEMON_H_