blob: 50958b8d9a02bfb35c9e5dbb9473c129c649179e [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 Petkov1bb904e2010-06-16 15:58:06 -070043 FRIEND_TEST(MetricsDaemonTest, ProcessUserCrash);
Darin Petkov2ccef012010-05-05 16:06:37 -070044 FRIEND_TEST(MetricsDaemonTest, ScreenSaverStateChanged);
Darin Petkov11b8eb32010-05-18 11:00:59 -070045 FRIEND_TEST(MetricsDaemonTest, SendMetric);
Darin Petkov2ccef012010-05-05 16:06:37 -070046 FRIEND_TEST(MetricsDaemonTest, SessionStateChanged);
Darin Petkovf1e85e42010-06-10 15:59:53 -070047 FRIEND_TEST(MetricsDaemonTest, SetUserActiveState);
Darin Petkovf27f0362010-06-04 13:14:19 -070048 FRIEND_TEST(MetricsDaemonTest, SetUserActiveStateTimeJump);
Darin Petkov1bb904e2010-06-16 15:58:06 -070049 FRIEND_TEST(MetricsDaemonTest, UserCrashIntervalReporter);
Darin Petkov2ccef012010-05-05 16:06:37 -070050
Darin Petkov703ec972010-04-27 11:02:18 -070051 // The network states (see network_states.h).
52 enum NetworkState {
53 kUnknownNetworkState = -1, // Initial/unknown network state.
Darin Petkov65b01462010-04-14 13:32:20 -070054#define STATE(name, capname) kNetworkState ## capname,
55#include "network_states.h"
56 kNumberNetworkStates
Darin Petkov703ec972010-04-27 11:02:18 -070057 };
Darin Petkov65b01462010-04-14 13:32:20 -070058
Darin Petkov703ec972010-04-27 11:02:18 -070059 // The power states (see power_states.h).
60 enum PowerState {
61 kUnknownPowerState = -1, // Initial/unknown power state.
62#define STATE(name, capname) kPowerState ## capname,
63#include "power_states.h"
64 kNumberPowerStates
65 };
Darin Petkov65b01462010-04-14 13:32:20 -070066
Darin Petkov41e06232010-05-03 16:45:37 -070067 // The user session states (see session_states.h).
68 enum SessionState {
69 kUnknownSessionState = -1, // Initial/unknown user session state.
70#define STATE(name, capname) kSessionState ## capname,
71#include "session_states.h"
72 kNumberSessionStates
73 };
74
75 // Data record for aggregating daily usage.
76 class UseRecord {
77 public:
78 UseRecord() : day_(0), seconds_(0) {}
79 int day_;
80 int seconds_;
81 };
82
Darin Petkov2ccef012010-05-05 16:06:37 -070083 // Metric parameters.
84 static const char kMetricDailyUseTimeName[];
85 static const int kMetricDailyUseTimeMin;
86 static const int kMetricDailyUseTimeMax;
87 static const int kMetricDailyUseTimeBuckets;
88 static const char kMetricTimeToNetworkDropName[];
89 static const int kMetricTimeToNetworkDropMin;
90 static const int kMetricTimeToNetworkDropMax;
91 static const int kMetricTimeToNetworkDropBuckets;
Darin Petkov1bb904e2010-06-16 15:58:06 -070092 static const char kMetricUserCrashIntervalName[];
93 static const int kMetricUserCrashIntervalMin;
94 static const int kMetricUserCrashIntervalMax;
95 static const int kMetricUserCrashIntervalBuckets;
Darin Petkov2ccef012010-05-05 16:06:37 -070096
Darin Petkov41e06232010-05-03 16:45:37 -070097 // D-Bus message match strings.
98 static const char* kDBusMatches_[];
99
100 // Array of network states.
101 static const char* kNetworkStates_[kNumberNetworkStates];
102
103 // Array of power states.
104 static const char* kPowerStates_[kNumberPowerStates];
105
Darin Petkov41e06232010-05-03 16:45:37 -0700106 // Array of user session states.
107 static const char* kSessionStates_[kNumberSessionStates];
108
Darin Petkov65b01462010-04-14 13:32:20 -0700109 // Creates the event loop and enters it.
110 void Loop();
111
Darin Petkov703ec972010-04-27 11:02:18 -0700112 // D-Bus filter callback.
113 static DBusHandlerResult MessageFilter(DBusConnection* connection,
114 DBusMessage* message,
115 void* user_data);
Darin Petkov65b01462010-04-14 13:32:20 -0700116
Darin Petkov703ec972010-04-27 11:02:18 -0700117 // Processes network state change.
Darin Petkovf27f0362010-06-04 13:14:19 -0700118 void NetStateChanged(const char* state_name, base::TimeTicks ticks);
Darin Petkov65b01462010-04-14 13:32:20 -0700119
Darin Petkov703ec972010-04-27 11:02:18 -0700120 // Given the state name, returns the state id.
121 NetworkState LookupNetworkState(const char* state_name);
Darin Petkov65b01462010-04-14 13:32:20 -0700122
Darin Petkov703ec972010-04-27 11:02:18 -0700123 // Processes power state change.
Darin Petkovf27f0362010-06-04 13:14:19 -0700124 void PowerStateChanged(const char* state_name, base::Time now);
Darin Petkov703ec972010-04-27 11:02:18 -0700125
126 // Given the state name, returns the state id.
127 PowerState LookupPowerState(const char* state_name);
Darin Petkov65b01462010-04-14 13:32:20 -0700128
Darin Petkov41e06232010-05-03 16:45:37 -0700129 // Processes user session state change.
Darin Petkovf27f0362010-06-04 13:14:19 -0700130 void SessionStateChanged(const char* state_name, base::Time now);
Darin Petkov41e06232010-05-03 16:45:37 -0700131
132 // Given the state name, returns the state id.
133 SessionState LookupSessionState(const char* state_name);
134
135 // Updates the user-active state to |active| and logs the usage data
136 // since the last update. If the user has just become active,
137 // reschedule the daily use monitor for more frequent updates --
138 // this is followed by an exponential back-off (see UseMonitor).
Darin Petkovf27f0362010-06-04 13:14:19 -0700139 // While in active use, this method should be called at intervals no
140 // longer than kUseMonitorIntervalMax otherwise new use time will be
141 // discarded.
142 void SetUserActiveState(bool active, base::Time now);
Darin Petkov41e06232010-05-03 16:45:37 -0700143
144 // Updates the daily usage file, if necessary, by adding |seconds|
145 // of active use to the |day| since Epoch. If there's usage data for
146 // day in the past in the usage file, that data is sent to UMA and
147 // removed from the file. If there's already usage data for |day| in
148 // the usage file, the |seconds| are accumulated.
149 void LogDailyUseRecord(int day, int seconds);
150
Darin Petkov1bb904e2010-06-16 15:58:06 -0700151 // Updates the active use time and logs time between user-space
152 // process crashes.
153 void ProcessUserCrash();
154
Darin Petkov41e06232010-05-03 16:45:37 -0700155 // Callbacks for the daily use monitor. The daily use monitor uses
156 // LogDailyUseRecord to aggregate current usage data and send it to
157 // UMA, if necessary. It also reschedules itself using an
158 // exponentially bigger interval (up to a certain maximum) -- so
159 // usage is monitored less frequently with longer active use.
160 static gboolean UseMonitorStatic(gpointer data);
161 bool UseMonitor();
162
163 // Schedules or reschedules a daily use monitor for |interval|
164 // seconds from now. |backoff| mode is used by the use monitor to
165 // reschedule itself. If there's a monitor scheduled already and
166 // |backoff| is false, unschedules it first. Doesn't schedule a
167 // monitor for more than kUseMonitorIntervalMax seconds in the
168 // future (see metrics_daemon.cc). Returns true if a new use monitor
169 // was scheduled, false otherwise (note that if |backoff| is false a
170 // new use monitor will always be scheduled).
171 bool ScheduleUseMonitor(int interval, bool backoff);
172
173 // Unschedules a scheduled use monitor, if any.
174 void UnscheduleUseMonitor();
175
Darin Petkov11b8eb32010-05-18 11:00:59 -0700176 // Sends a regular (exponential) histogram sample to Chrome for
177 // transport to UMA. See MetricsLibrary::SendToUMA in
178 // metrics_library.h for a description of the arguments.
179 void SendMetric(const std::string& name, int sample,
180 int min, int max, int nbuckets);
Darin Petkov65b01462010-04-14 13:32:20 -0700181
Darin Petkov1bb904e2010-06-16 15:58:06 -0700182 // TaggedCounter callback to process aggregated daily usage data and
183 // send to UMA.
Darin Petkovf1e85e42010-06-10 15:59:53 -0700184 static void DailyUseReporter(void* data, int tag, int count);
185
Darin Petkov1bb904e2010-06-16 15:58:06 -0700186 // TaggedCounter callback to process time between user-space process
187 // crashes and send to UMA.
188 static void UserCrashIntervalReporter(void* data, int tag, int count);
189
Darin Petkov2ccef012010-05-05 16:06:37 -0700190 // Test mode.
Darin Petkov41e06232010-05-03 16:45:37 -0700191 bool testing_;
Darin Petkov65b01462010-04-14 13:32:20 -0700192
Darin Petkovfc91b422010-05-12 13:05:45 -0700193 // The metrics library handle.
194 MetricsLibraryInterface* metrics_lib_;
195
Darin Petkov41e06232010-05-03 16:45:37 -0700196 // Current network state.
197 NetworkState network_state_;
Darin Petkov65b01462010-04-14 13:32:20 -0700198
Darin Petkovf27f0362010-06-04 13:14:19 -0700199 // Timestamps last network state update. This timestamp is used to
200 // sample the time from the network going online to going offline so
201 // TimeTicks ensures a monotonically increasing TimeDelta.
202 base::TimeTicks network_state_last_;
Darin Petkov65b01462010-04-14 13:32:20 -0700203
Darin Petkov41e06232010-05-03 16:45:37 -0700204 // Current power state.
205 PowerState power_state_;
206
Darin Petkov41e06232010-05-03 16:45:37 -0700207 // Current user session state.
208 SessionState session_state_;
209
210 // Is the user currently active: power is on, user session has
211 // started, screen is not locked.
212 bool user_active_;
213
Darin Petkov1bb904e2010-06-16 15:58:06 -0700214 // Timestamps last user active update. Active use time is aggregated
215 // each day before sending to UMA so using time since the epoch as
216 // the timestamp.
Darin Petkovf27f0362010-06-04 13:14:19 -0700217 base::Time user_active_last_;
Darin Petkov41e06232010-05-03 16:45:37 -0700218
Darin Petkov1bb904e2010-06-16 15:58:06 -0700219 // Daily active use time in seconds.
Darin Petkovf1e85e42010-06-10 15:59:53 -0700220 scoped_ptr<chromeos_metrics::TaggedCounterInterface> daily_use_;
Darin Petkov41e06232010-05-03 16:45:37 -0700221
Darin Petkov1bb904e2010-06-16 15:58:06 -0700222 // Active use time between user-space process crashes.
223 scoped_ptr<chromeos_metrics::TaggedCounterInterface> user_crash_interval_;
224
Darin Petkov41e06232010-05-03 16:45:37 -0700225 // Sleep period until the next daily usage aggregation performed by
226 // the daily use monitor (see ScheduleUseMonitor).
227 int usemon_interval_;
228
229 // Scheduled daily use monitor source (see ScheduleUseMonitor).
230 GSource* usemon_source_;
Darin Petkov65b01462010-04-14 13:32:20 -0700231};
232
233#endif // METRICS_DAEMON_H_