blob: d400bf0f0e602baac79e28bfb8be15d5210a59ba [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>
Ken Mixter4c5daa42010-08-26 18:35:06 -070010#include <map>
Darin Petkov65b01462010-04-14 13:32:20 -070011
Ken Mixter4c5daa42010-08-26 18:35:06 -070012#include <base/file_path.h>
Darin Petkovf1e85e42010-06-10 15:59:53 -070013#include <base/scoped_ptr.h>
14#include <base/time.h>
15#include <gtest/gtest_prod.h> // for FRIEND_TEST
16
Darin Petkovfc91b422010-05-12 13:05:45 -070017#include "metrics_library.h"
18
Ken Mixterccd84c02010-08-16 19:57:13 -070019namespace chromeos_metrics {
20class FrequencyCounter;
Ken Mixter4c5daa42010-08-26 18:35:06 -070021class TaggedCounter;
22class TaggedCounterReporter;
Ken Mixterccd84c02010-08-16 19:57:13 -070023}
Darin Petkov2ccef012010-05-05 16:06:37 -070024
Darin Petkov65b01462010-04-14 13:32:20 -070025class MetricsDaemon {
26
27 public:
Darin Petkovf1e85e42010-06-10 15:59:53 -070028 MetricsDaemon();
29 ~MetricsDaemon();
Darin Petkov65b01462010-04-14 13:32:20 -070030
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;
Ken Mixterccd84c02010-08-16 19:57:13 -070040 FRIEND_TEST(MetricsDaemonTest, CheckSystemCrash);
Ken Mixter4c5daa42010-08-26 18:35:06 -070041 FRIEND_TEST(MetricsDaemonTest, ComputeEpochNoCurrent);
42 FRIEND_TEST(MetricsDaemonTest, ComputeEpochNoLast);
43 FRIEND_TEST(MetricsDaemonTest, GetHistogramPath);
44 FRIEND_TEST(MetricsDaemonTest, IsNewEpoch);
Darin Petkov2ccef012010-05-05 16:06:37 -070045 FRIEND_TEST(MetricsDaemonTest, LookupNetworkState);
46 FRIEND_TEST(MetricsDaemonTest, LookupPowerState);
47 FRIEND_TEST(MetricsDaemonTest, LookupScreenSaverState);
48 FRIEND_TEST(MetricsDaemonTest, LookupSessionState);
Darin Petkove579d662010-05-05 16:19:39 -070049 FRIEND_TEST(MetricsDaemonTest, MessageFilter);
Darin Petkovfc91b422010-05-12 13:05:45 -070050 FRIEND_TEST(MetricsDaemonTest, NetStateChangedSimpleDrop);
51 FRIEND_TEST(MetricsDaemonTest, NetStateChangedSuspend);
Darin Petkov2ccef012010-05-05 16:06:37 -070052 FRIEND_TEST(MetricsDaemonTest, PowerStateChanged);
Darin Petkov38d5cb02010-06-24 12:10:26 -070053 FRIEND_TEST(MetricsDaemonTest, ProcessKernelCrash);
Ken Mixterccd84c02010-08-16 19:57:13 -070054 FRIEND_TEST(MetricsDaemonTest, ProcessUncleanShutdown);
Darin Petkov1bb904e2010-06-16 15:58:06 -070055 FRIEND_TEST(MetricsDaemonTest, ProcessUserCrash);
Ken Mixterccd84c02010-08-16 19:57:13 -070056 FRIEND_TEST(MetricsDaemonTest, ReportCrashesDailyFrequency);
57 FRIEND_TEST(MetricsDaemonTest, ReportDailyUse);
58 FRIEND_TEST(MetricsDaemonTest, ReportKernelCrashInterval);
59 FRIEND_TEST(MetricsDaemonTest, ReportUncleanShutdownInterval);
60 FRIEND_TEST(MetricsDaemonTest, ReportUserCrashInterval);
Darin Petkov2ccef012010-05-05 16:06:37 -070061 FRIEND_TEST(MetricsDaemonTest, ScreenSaverStateChanged);
Darin Petkov11b8eb32010-05-18 11:00:59 -070062 FRIEND_TEST(MetricsDaemonTest, SendMetric);
Darin Petkov2ccef012010-05-05 16:06:37 -070063 FRIEND_TEST(MetricsDaemonTest, SessionStateChanged);
Darin Petkovf1e85e42010-06-10 15:59:53 -070064 FRIEND_TEST(MetricsDaemonTest, SetUserActiveState);
Darin Petkovf27f0362010-06-04 13:14:19 -070065 FRIEND_TEST(MetricsDaemonTest, SetUserActiveStateTimeJump);
Darin Petkov2ccef012010-05-05 16:06:37 -070066
Darin Petkov703ec972010-04-27 11:02:18 -070067 // The network states (see network_states.h).
68 enum NetworkState {
69 kUnknownNetworkState = -1, // Initial/unknown network state.
Darin Petkov65b01462010-04-14 13:32:20 -070070#define STATE(name, capname) kNetworkState ## capname,
71#include "network_states.h"
72 kNumberNetworkStates
Darin Petkov703ec972010-04-27 11:02:18 -070073 };
Darin Petkov65b01462010-04-14 13:32:20 -070074
Darin Petkov703ec972010-04-27 11:02:18 -070075 // The power states (see power_states.h).
76 enum PowerState {
77 kUnknownPowerState = -1, // Initial/unknown power state.
78#define STATE(name, capname) kPowerState ## capname,
79#include "power_states.h"
80 kNumberPowerStates
81 };
Darin Petkov65b01462010-04-14 13:32:20 -070082
Darin Petkov41e06232010-05-03 16:45:37 -070083 // 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
Ken Mixter4c5daa42010-08-26 18:35:06 -070099 typedef std::map<std::string, chromeos_metrics::FrequencyCounter*>
100 FrequencyCounters;
101
Darin Petkov2ccef012010-05-05 16:06:37 -0700102 // Metric parameters.
Ken Mixterccd84c02010-08-16 19:57:13 -0700103 static const char kMetricAnyCrashesDailyName[];
Ken Mixter4c5daa42010-08-26 18:35:06 -0700104 static const char kMetricAnyCrashesWeeklyName[];
105 static const char kMetricCrashFrequencyBuckets;
106 static const char kMetricCrashFrequencyMax;
107 static const char kMetricCrashFrequencyMin;
Ken Mixterccd84c02010-08-16 19:57:13 -0700108 static const int kMetricCrashIntervalBuckets;
109 static const int kMetricCrashIntervalMax;
110 static const int kMetricCrashIntervalMin;
111 static const int kMetricDailyUseTimeBuckets;
112 static const int kMetricDailyUseTimeMax;
113 static const int kMetricDailyUseTimeMin;
Darin Petkov2ccef012010-05-05 16:06:37 -0700114 static const char kMetricDailyUseTimeName[];
Ken Mixterccd84c02010-08-16 19:57:13 -0700115 static const char kMetricKernelCrashesDailyName[];
Ken Mixter4c5daa42010-08-26 18:35:06 -0700116 static const char kMetricKernelCrashesWeeklyName[];
Darin Petkov38d5cb02010-06-24 12:10:26 -0700117 static const char kMetricKernelCrashIntervalName[];
Ken Mixter4c5daa42010-08-26 18:35:06 -0700118 static const char kMetricsPath[];
Ken Mixterccd84c02010-08-16 19:57:13 -0700119 static const int kMetricTimeToNetworkDropBuckets;
120 static const int kMetricTimeToNetworkDropMax;
121 static const int kMetricTimeToNetworkDropMin;
Darin Petkov2ccef012010-05-05 16:06:37 -0700122 static const char kMetricTimeToNetworkDropName[];
Ken Mixterccd84c02010-08-16 19:57:13 -0700123 static const char kMetricUncleanShutdownIntervalName[];
124 static const char kMetricUncleanShutdownsDailyName[];
Ken Mixter4c5daa42010-08-26 18:35:06 -0700125 static const char kMetricUncleanShutdownsWeeklyName[];
Ken Mixterccd84c02010-08-16 19:57:13 -0700126 static const char kMetricUserCrashesDailyName[];
Ken Mixter4c5daa42010-08-26 18:35:06 -0700127 static const char kMetricUserCrashesWeeklyName[];
Darin Petkov1bb904e2010-06-16 15:58:06 -0700128 static const char kMetricUserCrashIntervalName[];
Darin Petkov2ccef012010-05-05 16:06:37 -0700129
Darin Petkov41e06232010-05-03 16:45:37 -0700130 // D-Bus message match strings.
131 static const char* kDBusMatches_[];
132
133 // Array of network states.
134 static const char* kNetworkStates_[kNumberNetworkStates];
135
136 // Array of power states.
137 static const char* kPowerStates_[kNumberPowerStates];
138
Darin Petkov41e06232010-05-03 16:45:37 -0700139 // Array of user session states.
140 static const char* kSessionStates_[kNumberSessionStates];
141
Ken Mixter4c5daa42010-08-26 18:35:06 -0700142 // Clears and deletes the data contained in frequency_counters_.
143 void DeleteFrequencyCounters();
144
145 // Configures the given crash interval reporter.
146 void ConfigureCrashIntervalReporter(
147 const char* histogram_name,
148 scoped_ptr<chromeos_metrics::TaggedCounterReporter>* reporter);
149
150 // Configures the given frequency counter reporter.
151 void ConfigureCrashFrequencyReporter(const char* histogram_name);
152
153 // Returns file path to persistent file for generating given histogram.
154 FilePath GetHistogramPath(const char* histogram_name);
155
Darin Petkov65b01462010-04-14 13:32:20 -0700156 // Creates the event loop and enters it.
157 void Loop();
158
Darin Petkov703ec972010-04-27 11:02:18 -0700159 // D-Bus filter callback.
160 static DBusHandlerResult MessageFilter(DBusConnection* connection,
161 DBusMessage* message,
162 void* user_data);
Darin Petkov65b01462010-04-14 13:32:20 -0700163
Darin Petkov703ec972010-04-27 11:02:18 -0700164 // Processes network state change.
Darin Petkovf27f0362010-06-04 13:14:19 -0700165 void NetStateChanged(const char* state_name, base::TimeTicks ticks);
Darin Petkov65b01462010-04-14 13:32:20 -0700166
Darin Petkov703ec972010-04-27 11:02:18 -0700167 // Given the state name, returns the state id.
168 NetworkState LookupNetworkState(const char* state_name);
Darin Petkov65b01462010-04-14 13:32:20 -0700169
Darin Petkov703ec972010-04-27 11:02:18 -0700170 // Processes power state change.
Darin Petkovf27f0362010-06-04 13:14:19 -0700171 void PowerStateChanged(const char* state_name, base::Time now);
Darin Petkov703ec972010-04-27 11:02:18 -0700172
173 // Given the state name, returns the state id.
174 PowerState LookupPowerState(const char* state_name);
Darin Petkov65b01462010-04-14 13:32:20 -0700175
Darin Petkov41e06232010-05-03 16:45:37 -0700176 // Processes user session state change.
Darin Petkovf27f0362010-06-04 13:14:19 -0700177 void SessionStateChanged(const char* state_name, base::Time now);
Darin Petkov41e06232010-05-03 16:45:37 -0700178
179 // Given the state name, returns the state id.
180 SessionState LookupSessionState(const char* state_name);
181
182 // Updates the user-active state to |active| and logs the usage data
183 // since the last update. If the user has just become active,
184 // reschedule the daily use monitor for more frequent updates --
185 // this is followed by an exponential back-off (see UseMonitor).
Darin Petkovf27f0362010-06-04 13:14:19 -0700186 // While in active use, this method should be called at intervals no
187 // longer than kUseMonitorIntervalMax otherwise new use time will be
188 // discarded.
189 void SetUserActiveState(bool active, base::Time now);
Darin Petkov41e06232010-05-03 16:45:37 -0700190
191 // Updates the daily usage file, if necessary, by adding |seconds|
192 // of active use to the |day| since Epoch. If there's usage data for
193 // day in the past in the usage file, that data is sent to UMA and
194 // removed from the file. If there's already usage data for |day| in
195 // the usage file, the |seconds| are accumulated.
196 void LogDailyUseRecord(int day, int seconds);
197
Darin Petkov1bb904e2010-06-16 15:58:06 -0700198 // Updates the active use time and logs time between user-space
199 // process crashes.
200 void ProcessUserCrash();
201
Darin Petkov38d5cb02010-06-24 12:10:26 -0700202 // Updates the active use time and logs time between kernel crashes.
203 void ProcessKernelCrash();
204
Ken Mixterccd84c02010-08-16 19:57:13 -0700205 // Updates the active use time and logs time between unclean shutdowns.
206 void ProcessUncleanShutdown();
207
208 // Checks if a kernel crash has been detected and returns true if
209 // so. The method assumes that a kernel crash has happened if
210 // |crash_file| exists. It removes the file immediately if it
211 // exists, so it must not be called more than once.
212 bool CheckSystemCrash(const std::string& crash_file);
Darin Petkov38d5cb02010-06-24 12:10:26 -0700213
Darin Petkov41e06232010-05-03 16:45:37 -0700214 // Callbacks for the daily use monitor. The daily use monitor uses
215 // LogDailyUseRecord to aggregate current usage data and send it to
216 // UMA, if necessary. It also reschedules itself using an
217 // exponentially bigger interval (up to a certain maximum) -- so
218 // usage is monitored less frequently with longer active use.
219 static gboolean UseMonitorStatic(gpointer data);
220 bool UseMonitor();
221
222 // Schedules or reschedules a daily use monitor for |interval|
223 // seconds from now. |backoff| mode is used by the use monitor to
224 // reschedule itself. If there's a monitor scheduled already and
225 // |backoff| is false, unschedules it first. Doesn't schedule a
226 // monitor for more than kUseMonitorIntervalMax seconds in the
227 // future (see metrics_daemon.cc). Returns true if a new use monitor
228 // was scheduled, false otherwise (note that if |backoff| is false a
229 // new use monitor will always be scheduled).
230 bool ScheduleUseMonitor(int interval, bool backoff);
231
232 // Unschedules a scheduled use monitor, if any.
233 void UnscheduleUseMonitor();
234
Ken Mixter4c5daa42010-08-26 18:35:06 -0700235 // Report daily use through UMA.
236 static void ReportDailyUse(void* handle, int tag, int count);
237
Darin Petkov11b8eb32010-05-18 11:00:59 -0700238 // Sends a regular (exponential) histogram sample to Chrome for
239 // transport to UMA. See MetricsLibrary::SendToUMA in
240 // metrics_library.h for a description of the arguments.
241 void SendMetric(const std::string& name, int sample,
242 int min, int max, int nbuckets);
Darin Petkov65b01462010-04-14 13:32:20 -0700243
Darin Petkov2ccef012010-05-05 16:06:37 -0700244 // Test mode.
Darin Petkov41e06232010-05-03 16:45:37 -0700245 bool testing_;
Darin Petkov65b01462010-04-14 13:32:20 -0700246
Darin Petkovfc91b422010-05-12 13:05:45 -0700247 // The metrics library handle.
248 MetricsLibraryInterface* metrics_lib_;
249
Darin Petkov41e06232010-05-03 16:45:37 -0700250 // Current network state.
251 NetworkState network_state_;
Darin Petkov65b01462010-04-14 13:32:20 -0700252
Darin Petkovf27f0362010-06-04 13:14:19 -0700253 // Timestamps last network state update. This timestamp is used to
254 // sample the time from the network going online to going offline so
255 // TimeTicks ensures a monotonically increasing TimeDelta.
256 base::TimeTicks network_state_last_;
Darin Petkov65b01462010-04-14 13:32:20 -0700257
Darin Petkov41e06232010-05-03 16:45:37 -0700258 // Current power state.
259 PowerState power_state_;
260
Darin Petkov41e06232010-05-03 16:45:37 -0700261 // Current user session state.
262 SessionState session_state_;
263
264 // Is the user currently active: power is on, user session has
265 // started, screen is not locked.
266 bool user_active_;
267
Darin Petkov1bb904e2010-06-16 15:58:06 -0700268 // Timestamps last user active update. Active use time is aggregated
269 // each day before sending to UMA so using time since the epoch as
270 // the timestamp.
Darin Petkovf27f0362010-06-04 13:14:19 -0700271 base::Time user_active_last_;
Darin Petkov41e06232010-05-03 16:45:37 -0700272
Darin Petkov1bb904e2010-06-16 15:58:06 -0700273 // Daily active use time in seconds.
Ken Mixter4c5daa42010-08-26 18:35:06 -0700274 scoped_ptr<chromeos_metrics::TaggedCounter> daily_use_;
Darin Petkov41e06232010-05-03 16:45:37 -0700275
Darin Petkov1bb904e2010-06-16 15:58:06 -0700276 // Active use time between user-space process crashes.
Ken Mixter4c5daa42010-08-26 18:35:06 -0700277 scoped_ptr<chromeos_metrics::TaggedCounterReporter> user_crash_interval_;
Darin Petkov1bb904e2010-06-16 15:58:06 -0700278
Darin Petkov38d5cb02010-06-24 12:10:26 -0700279 // Active use time between kernel crashes.
Ken Mixter4c5daa42010-08-26 18:35:06 -0700280 scoped_ptr<chromeos_metrics::TaggedCounterReporter> kernel_crash_interval_;
Darin Petkov38d5cb02010-06-24 12:10:26 -0700281
Ken Mixterccd84c02010-08-16 19:57:13 -0700282 // Active use time between unclean shutdowns crashes.
Ken Mixter4c5daa42010-08-26 18:35:06 -0700283 scoped_ptr<chromeos_metrics::TaggedCounterReporter>
Ken Mixterccd84c02010-08-16 19:57:13 -0700284 unclean_shutdown_interval_;
285
Ken Mixter4c5daa42010-08-26 18:35:06 -0700286 // Map of all frequency counters, to simplify flushing them.
287 FrequencyCounters frequency_counters_;
Ken Mixterccd84c02010-08-16 19:57:13 -0700288
Darin Petkov41e06232010-05-03 16:45:37 -0700289 // Sleep period until the next daily usage aggregation performed by
290 // the daily use monitor (see ScheduleUseMonitor).
291 int usemon_interval_;
292
293 // Scheduled daily use monitor source (see ScheduleUseMonitor).
294 GSource* usemon_source_;
Darin Petkov65b01462010-04-14 13:32:20 -0700295};
296
297#endif // METRICS_DAEMON_H_