blob: 437cafd0ec2e33af5e4de272dc84a422f4b25bb9 [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 Petkov38d5cb02010-06-24 12:10:26 -070034 FRIEND_TEST(MetricsDaemonTest, CheckKernelCrash);
Darin Petkovf1e85e42010-06-10 15:59:53 -070035 FRIEND_TEST(MetricsDaemonTest, DailyUseReporter);
Darin Petkov38d5cb02010-06-24 12:10:26 -070036 FRIEND_TEST(MetricsDaemonTest, KernelCrashIntervalReporter);
Darin Petkov2ccef012010-05-05 16:06:37 -070037 FRIEND_TEST(MetricsDaemonTest, LookupNetworkState);
38 FRIEND_TEST(MetricsDaemonTest, LookupPowerState);
39 FRIEND_TEST(MetricsDaemonTest, LookupScreenSaverState);
40 FRIEND_TEST(MetricsDaemonTest, LookupSessionState);
Darin Petkove579d662010-05-05 16:19:39 -070041 FRIEND_TEST(MetricsDaemonTest, MessageFilter);
Darin Petkovfc91b422010-05-12 13:05:45 -070042 FRIEND_TEST(MetricsDaemonTest, NetStateChangedSimpleDrop);
43 FRIEND_TEST(MetricsDaemonTest, NetStateChangedSuspend);
Darin Petkov2ccef012010-05-05 16:06:37 -070044 FRIEND_TEST(MetricsDaemonTest, PowerStateChanged);
Darin Petkov38d5cb02010-06-24 12:10:26 -070045 FRIEND_TEST(MetricsDaemonTest, ProcessKernelCrash);
Darin Petkov1bb904e2010-06-16 15:58:06 -070046 FRIEND_TEST(MetricsDaemonTest, ProcessUserCrash);
Darin Petkov2ccef012010-05-05 16:06:37 -070047 FRIEND_TEST(MetricsDaemonTest, ScreenSaverStateChanged);
Darin Petkov11b8eb32010-05-18 11:00:59 -070048 FRIEND_TEST(MetricsDaemonTest, SendMetric);
Darin Petkov2ccef012010-05-05 16:06:37 -070049 FRIEND_TEST(MetricsDaemonTest, SessionStateChanged);
Darin Petkovf1e85e42010-06-10 15:59:53 -070050 FRIEND_TEST(MetricsDaemonTest, SetUserActiveState);
Darin Petkovf27f0362010-06-04 13:14:19 -070051 FRIEND_TEST(MetricsDaemonTest, SetUserActiveStateTimeJump);
Darin Petkov1bb904e2010-06-16 15:58:06 -070052 FRIEND_TEST(MetricsDaemonTest, UserCrashIntervalReporter);
Darin Petkov2ccef012010-05-05 16:06:37 -070053
Darin Petkov703ec972010-04-27 11:02:18 -070054 // The network states (see network_states.h).
55 enum NetworkState {
56 kUnknownNetworkState = -1, // Initial/unknown network state.
Darin Petkov65b01462010-04-14 13:32:20 -070057#define STATE(name, capname) kNetworkState ## capname,
58#include "network_states.h"
59 kNumberNetworkStates
Darin Petkov703ec972010-04-27 11:02:18 -070060 };
Darin Petkov65b01462010-04-14 13:32:20 -070061
Darin Petkov703ec972010-04-27 11:02:18 -070062 // The power states (see power_states.h).
63 enum PowerState {
64 kUnknownPowerState = -1, // Initial/unknown power state.
65#define STATE(name, capname) kPowerState ## capname,
66#include "power_states.h"
67 kNumberPowerStates
68 };
Darin Petkov65b01462010-04-14 13:32:20 -070069
Darin Petkov41e06232010-05-03 16:45:37 -070070 // The user session states (see session_states.h).
71 enum SessionState {
72 kUnknownSessionState = -1, // Initial/unknown user session state.
73#define STATE(name, capname) kSessionState ## capname,
74#include "session_states.h"
75 kNumberSessionStates
76 };
77
78 // Data record for aggregating daily usage.
79 class UseRecord {
80 public:
81 UseRecord() : day_(0), seconds_(0) {}
82 int day_;
83 int seconds_;
84 };
85
Darin Petkov2ccef012010-05-05 16:06:37 -070086 // Metric parameters.
87 static const char kMetricDailyUseTimeName[];
88 static const int kMetricDailyUseTimeMin;
89 static const int kMetricDailyUseTimeMax;
90 static const int kMetricDailyUseTimeBuckets;
Darin Petkov38d5cb02010-06-24 12:10:26 -070091 static const char kMetricKernelCrashIntervalName[];
92 static const int kMetricKernelCrashIntervalMin;
93 static const int kMetricKernelCrashIntervalMax;
94 static const int kMetricKernelCrashIntervalBuckets;
Darin Petkov2ccef012010-05-05 16:06:37 -070095 static const char kMetricTimeToNetworkDropName[];
96 static const int kMetricTimeToNetworkDropMin;
97 static const int kMetricTimeToNetworkDropMax;
98 static const int kMetricTimeToNetworkDropBuckets;
Darin Petkov1bb904e2010-06-16 15:58:06 -070099 static const char kMetricUserCrashIntervalName[];
100 static const int kMetricUserCrashIntervalMin;
101 static const int kMetricUserCrashIntervalMax;
102 static const int kMetricUserCrashIntervalBuckets;
Darin Petkov2ccef012010-05-05 16:06:37 -0700103
Darin Petkov41e06232010-05-03 16:45:37 -0700104 // D-Bus message match strings.
105 static const char* kDBusMatches_[];
106
107 // Array of network states.
108 static const char* kNetworkStates_[kNumberNetworkStates];
109
110 // Array of power states.
111 static const char* kPowerStates_[kNumberPowerStates];
112
Darin Petkov41e06232010-05-03 16:45:37 -0700113 // Array of user session states.
114 static const char* kSessionStates_[kNumberSessionStates];
115
Darin Petkov65b01462010-04-14 13:32:20 -0700116 // Creates the event loop and enters it.
117 void Loop();
118
Darin Petkov703ec972010-04-27 11:02:18 -0700119 // D-Bus filter callback.
120 static DBusHandlerResult MessageFilter(DBusConnection* connection,
121 DBusMessage* message,
122 void* user_data);
Darin Petkov65b01462010-04-14 13:32:20 -0700123
Darin Petkov703ec972010-04-27 11:02:18 -0700124 // Processes network state change.
Darin Petkovf27f0362010-06-04 13:14:19 -0700125 void NetStateChanged(const char* state_name, base::TimeTicks ticks);
Darin Petkov65b01462010-04-14 13:32:20 -0700126
Darin Petkov703ec972010-04-27 11:02:18 -0700127 // Given the state name, returns the state id.
128 NetworkState LookupNetworkState(const char* state_name);
Darin Petkov65b01462010-04-14 13:32:20 -0700129
Darin Petkov703ec972010-04-27 11:02:18 -0700130 // Processes power state change.
Darin Petkovf27f0362010-06-04 13:14:19 -0700131 void PowerStateChanged(const char* state_name, base::Time now);
Darin Petkov703ec972010-04-27 11:02:18 -0700132
133 // Given the state name, returns the state id.
134 PowerState LookupPowerState(const char* state_name);
Darin Petkov65b01462010-04-14 13:32:20 -0700135
Darin Petkov41e06232010-05-03 16:45:37 -0700136 // Processes user session state change.
Darin Petkovf27f0362010-06-04 13:14:19 -0700137 void SessionStateChanged(const char* state_name, base::Time now);
Darin Petkov41e06232010-05-03 16:45:37 -0700138
139 // Given the state name, returns the state id.
140 SessionState LookupSessionState(const char* state_name);
141
142 // Updates the user-active state to |active| and logs the usage data
143 // since the last update. If the user has just become active,
144 // reschedule the daily use monitor for more frequent updates --
145 // this is followed by an exponential back-off (see UseMonitor).
Darin Petkovf27f0362010-06-04 13:14:19 -0700146 // While in active use, this method should be called at intervals no
147 // longer than kUseMonitorIntervalMax otherwise new use time will be
148 // discarded.
149 void SetUserActiveState(bool active, base::Time now);
Darin Petkov41e06232010-05-03 16:45:37 -0700150
151 // Updates the daily usage file, if necessary, by adding |seconds|
152 // of active use to the |day| since Epoch. If there's usage data for
153 // day in the past in the usage file, that data is sent to UMA and
154 // removed from the file. If there's already usage data for |day| in
155 // the usage file, the |seconds| are accumulated.
156 void LogDailyUseRecord(int day, int seconds);
157
Darin Petkov1bb904e2010-06-16 15:58:06 -0700158 // Updates the active use time and logs time between user-space
159 // process crashes.
160 void ProcessUserCrash();
161
Darin Petkov38d5cb02010-06-24 12:10:26 -0700162 // Updates the active use time and logs time between kernel crashes.
163 void ProcessKernelCrash();
164
165 // Checks if a kernel crash has been detected and processes if so.
166 // The method assumes that a kernel crash has happened if
167 // |crash_file| exists.
168 void CheckKernelCrash(const std::string& crash_file);
169
Darin Petkov41e06232010-05-03 16:45:37 -0700170 // Callbacks for the daily use monitor. The daily use monitor uses
171 // LogDailyUseRecord to aggregate current usage data and send it to
172 // UMA, if necessary. It also reschedules itself using an
173 // exponentially bigger interval (up to a certain maximum) -- so
174 // usage is monitored less frequently with longer active use.
175 static gboolean UseMonitorStatic(gpointer data);
176 bool UseMonitor();
177
178 // Schedules or reschedules a daily use monitor for |interval|
179 // seconds from now. |backoff| mode is used by the use monitor to
180 // reschedule itself. If there's a monitor scheduled already and
181 // |backoff| is false, unschedules it first. Doesn't schedule a
182 // monitor for more than kUseMonitorIntervalMax seconds in the
183 // future (see metrics_daemon.cc). Returns true if a new use monitor
184 // was scheduled, false otherwise (note that if |backoff| is false a
185 // new use monitor will always be scheduled).
186 bool ScheduleUseMonitor(int interval, bool backoff);
187
188 // Unschedules a scheduled use monitor, if any.
189 void UnscheduleUseMonitor();
190
Darin Petkov11b8eb32010-05-18 11:00:59 -0700191 // Sends a regular (exponential) histogram sample to Chrome for
192 // transport to UMA. See MetricsLibrary::SendToUMA in
193 // metrics_library.h for a description of the arguments.
194 void SendMetric(const std::string& name, int sample,
195 int min, int max, int nbuckets);
Darin Petkov65b01462010-04-14 13:32:20 -0700196
Darin Petkov1bb904e2010-06-16 15:58:06 -0700197 // TaggedCounter callback to process aggregated daily usage data and
198 // send to UMA.
Darin Petkovf1e85e42010-06-10 15:59:53 -0700199 static void DailyUseReporter(void* data, int tag, int count);
200
Darin Petkov1bb904e2010-06-16 15:58:06 -0700201 // TaggedCounter callback to process time between user-space process
202 // crashes and send to UMA.
203 static void UserCrashIntervalReporter(void* data, int tag, int count);
204
Darin Petkov38d5cb02010-06-24 12:10:26 -0700205 // TaggedCounter callback to process time between kernel crashes and
206 // send to UMA.
207 static void KernelCrashIntervalReporter(void* data, int tag, int count);
208
Darin Petkov2ccef012010-05-05 16:06:37 -0700209 // Test mode.
Darin Petkov41e06232010-05-03 16:45:37 -0700210 bool testing_;
Darin Petkov65b01462010-04-14 13:32:20 -0700211
Darin Petkovfc91b422010-05-12 13:05:45 -0700212 // The metrics library handle.
213 MetricsLibraryInterface* metrics_lib_;
214
Darin Petkov41e06232010-05-03 16:45:37 -0700215 // Current network state.
216 NetworkState network_state_;
Darin Petkov65b01462010-04-14 13:32:20 -0700217
Darin Petkovf27f0362010-06-04 13:14:19 -0700218 // Timestamps last network state update. This timestamp is used to
219 // sample the time from the network going online to going offline so
220 // TimeTicks ensures a monotonically increasing TimeDelta.
221 base::TimeTicks network_state_last_;
Darin Petkov65b01462010-04-14 13:32:20 -0700222
Darin Petkov41e06232010-05-03 16:45:37 -0700223 // Current power state.
224 PowerState power_state_;
225
Darin Petkov41e06232010-05-03 16:45:37 -0700226 // Current user session state.
227 SessionState session_state_;
228
229 // Is the user currently active: power is on, user session has
230 // started, screen is not locked.
231 bool user_active_;
232
Darin Petkov1bb904e2010-06-16 15:58:06 -0700233 // Timestamps last user active update. Active use time is aggregated
234 // each day before sending to UMA so using time since the epoch as
235 // the timestamp.
Darin Petkovf27f0362010-06-04 13:14:19 -0700236 base::Time user_active_last_;
Darin Petkov41e06232010-05-03 16:45:37 -0700237
Darin Petkov1bb904e2010-06-16 15:58:06 -0700238 // Daily active use time in seconds.
Darin Petkovf1e85e42010-06-10 15:59:53 -0700239 scoped_ptr<chromeos_metrics::TaggedCounterInterface> daily_use_;
Darin Petkov41e06232010-05-03 16:45:37 -0700240
Darin Petkov1bb904e2010-06-16 15:58:06 -0700241 // Active use time between user-space process crashes.
242 scoped_ptr<chromeos_metrics::TaggedCounterInterface> user_crash_interval_;
243
Darin Petkov38d5cb02010-06-24 12:10:26 -0700244 // Active use time between kernel crashes.
245 scoped_ptr<chromeos_metrics::TaggedCounterInterface> kernel_crash_interval_;
246
Darin Petkov41e06232010-05-03 16:45:37 -0700247 // Sleep period until the next daily usage aggregation performed by
248 // the daily use monitor (see ScheduleUseMonitor).
249 int usemon_interval_;
250
251 // Scheduled daily use monitor source (see ScheduleUseMonitor).
252 GSource* usemon_source_;
Darin Petkov65b01462010-04-14 13:32:20 -0700253};
254
255#endif // METRICS_DAEMON_H_