blob: 5f2e7868a9aed95793ea023f1863f2503326ccde [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, LookupPowerState);
46 FRIEND_TEST(MetricsDaemonTest, LookupScreenSaverState);
47 FRIEND_TEST(MetricsDaemonTest, LookupSessionState);
Darin Petkove579d662010-05-05 16:19:39 -070048 FRIEND_TEST(MetricsDaemonTest, MessageFilter);
Darin Petkov2ccef012010-05-05 16:06:37 -070049 FRIEND_TEST(MetricsDaemonTest, PowerStateChanged);
Darin Petkov38d5cb02010-06-24 12:10:26 -070050 FRIEND_TEST(MetricsDaemonTest, ProcessKernelCrash);
Ken Mixterccd84c02010-08-16 19:57:13 -070051 FRIEND_TEST(MetricsDaemonTest, ProcessUncleanShutdown);
Darin Petkov1bb904e2010-06-16 15:58:06 -070052 FRIEND_TEST(MetricsDaemonTest, ProcessUserCrash);
Ken Mixterccd84c02010-08-16 19:57:13 -070053 FRIEND_TEST(MetricsDaemonTest, ReportCrashesDailyFrequency);
54 FRIEND_TEST(MetricsDaemonTest, ReportDailyUse);
55 FRIEND_TEST(MetricsDaemonTest, ReportKernelCrashInterval);
56 FRIEND_TEST(MetricsDaemonTest, ReportUncleanShutdownInterval);
57 FRIEND_TEST(MetricsDaemonTest, ReportUserCrashInterval);
Darin Petkov2ccef012010-05-05 16:06:37 -070058 FRIEND_TEST(MetricsDaemonTest, ScreenSaverStateChanged);
Darin Petkov11b8eb32010-05-18 11:00:59 -070059 FRIEND_TEST(MetricsDaemonTest, SendMetric);
Darin Petkov2ccef012010-05-05 16:06:37 -070060 FRIEND_TEST(MetricsDaemonTest, SessionStateChanged);
Darin Petkovf1e85e42010-06-10 15:59:53 -070061 FRIEND_TEST(MetricsDaemonTest, SetUserActiveState);
Darin Petkovf27f0362010-06-04 13:14:19 -070062 FRIEND_TEST(MetricsDaemonTest, SetUserActiveStateTimeJump);
Darin Petkov2ccef012010-05-05 16:06:37 -070063
Darin Petkov703ec972010-04-27 11:02:18 -070064 // The power states (see power_states.h).
65 enum PowerState {
66 kUnknownPowerState = -1, // Initial/unknown power state.
67#define STATE(name, capname) kPowerState ## capname,
68#include "power_states.h"
69 kNumberPowerStates
70 };
Darin Petkov65b01462010-04-14 13:32:20 -070071
Darin Petkov41e06232010-05-03 16:45:37 -070072 // The user session states (see session_states.h).
73 enum SessionState {
74 kUnknownSessionState = -1, // Initial/unknown user session state.
75#define STATE(name, capname) kSessionState ## capname,
76#include "session_states.h"
77 kNumberSessionStates
78 };
79
80 // Data record for aggregating daily usage.
81 class UseRecord {
82 public:
83 UseRecord() : day_(0), seconds_(0) {}
84 int day_;
85 int seconds_;
86 };
87
Ken Mixter4c5daa42010-08-26 18:35:06 -070088 typedef std::map<std::string, chromeos_metrics::FrequencyCounter*>
89 FrequencyCounters;
90
Darin Petkov2ccef012010-05-05 16:06:37 -070091 // Metric parameters.
Ken Mixterccd84c02010-08-16 19:57:13 -070092 static const char kMetricAnyCrashesDailyName[];
Ken Mixter4c5daa42010-08-26 18:35:06 -070093 static const char kMetricAnyCrashesWeeklyName[];
94 static const char kMetricCrashFrequencyBuckets;
95 static const char kMetricCrashFrequencyMax;
96 static const char kMetricCrashFrequencyMin;
Ken Mixterccd84c02010-08-16 19:57:13 -070097 static const int kMetricCrashIntervalBuckets;
98 static const int kMetricCrashIntervalMax;
99 static const int kMetricCrashIntervalMin;
100 static const int kMetricDailyUseTimeBuckets;
101 static const int kMetricDailyUseTimeMax;
102 static const int kMetricDailyUseTimeMin;
Darin Petkov2ccef012010-05-05 16:06:37 -0700103 static const char kMetricDailyUseTimeName[];
Ken Mixterccd84c02010-08-16 19:57:13 -0700104 static const char kMetricKernelCrashesDailyName[];
Ken Mixter4c5daa42010-08-26 18:35:06 -0700105 static const char kMetricKernelCrashesWeeklyName[];
Darin Petkov38d5cb02010-06-24 12:10:26 -0700106 static const char kMetricKernelCrashIntervalName[];
Ken Mixter4c5daa42010-08-26 18:35:06 -0700107 static const char kMetricsPath[];
Ken Mixterccd84c02010-08-16 19:57:13 -0700108 static const char kMetricUncleanShutdownIntervalName[];
109 static const char kMetricUncleanShutdownsDailyName[];
Ken Mixter4c5daa42010-08-26 18:35:06 -0700110 static const char kMetricUncleanShutdownsWeeklyName[];
Ken Mixterccd84c02010-08-16 19:57:13 -0700111 static const char kMetricUserCrashesDailyName[];
Ken Mixter4c5daa42010-08-26 18:35:06 -0700112 static const char kMetricUserCrashesWeeklyName[];
Darin Petkov1bb904e2010-06-16 15:58:06 -0700113 static const char kMetricUserCrashIntervalName[];
Darin Petkov2ccef012010-05-05 16:06:37 -0700114
Darin Petkov41e06232010-05-03 16:45:37 -0700115 // D-Bus message match strings.
116 static const char* kDBusMatches_[];
117
Darin Petkov41e06232010-05-03 16:45:37 -0700118 // Array of power states.
119 static const char* kPowerStates_[kNumberPowerStates];
120
Darin Petkov41e06232010-05-03 16:45:37 -0700121 // Array of user session states.
122 static const char* kSessionStates_[kNumberSessionStates];
123
Ken Mixter4c5daa42010-08-26 18:35:06 -0700124 // Clears and deletes the data contained in frequency_counters_.
125 void DeleteFrequencyCounters();
126
127 // Configures the given crash interval reporter.
128 void ConfigureCrashIntervalReporter(
129 const char* histogram_name,
130 scoped_ptr<chromeos_metrics::TaggedCounterReporter>* reporter);
131
132 // Configures the given frequency counter reporter.
133 void ConfigureCrashFrequencyReporter(const char* histogram_name);
134
135 // Returns file path to persistent file for generating given histogram.
136 FilePath GetHistogramPath(const char* histogram_name);
137
Darin Petkov65b01462010-04-14 13:32:20 -0700138 // Creates the event loop and enters it.
139 void Loop();
140
Darin Petkov703ec972010-04-27 11:02:18 -0700141 // D-Bus filter callback.
142 static DBusHandlerResult MessageFilter(DBusConnection* connection,
143 DBusMessage* message,
144 void* user_data);
Darin Petkov65b01462010-04-14 13:32:20 -0700145
Darin Petkov703ec972010-04-27 11:02:18 -0700146 // Processes power state change.
Darin Petkovf27f0362010-06-04 13:14:19 -0700147 void PowerStateChanged(const char* state_name, base::Time now);
Darin Petkov703ec972010-04-27 11:02:18 -0700148
149 // Given the state name, returns the state id.
150 PowerState LookupPowerState(const char* state_name);
Darin Petkov65b01462010-04-14 13:32:20 -0700151
Darin Petkov41e06232010-05-03 16:45:37 -0700152 // Processes user session state change.
Darin Petkovf27f0362010-06-04 13:14:19 -0700153 void SessionStateChanged(const char* state_name, base::Time now);
Darin Petkov41e06232010-05-03 16:45:37 -0700154
155 // Given the state name, returns the state id.
156 SessionState LookupSessionState(const char* state_name);
157
158 // Updates the user-active state to |active| and logs the usage data
159 // since the last update. If the user has just become active,
160 // reschedule the daily use monitor for more frequent updates --
161 // this is followed by an exponential back-off (see UseMonitor).
Darin Petkovf27f0362010-06-04 13:14:19 -0700162 // While in active use, this method should be called at intervals no
163 // longer than kUseMonitorIntervalMax otherwise new use time will be
164 // discarded.
165 void SetUserActiveState(bool active, base::Time now);
Darin Petkov41e06232010-05-03 16:45:37 -0700166
167 // Updates the daily usage file, if necessary, by adding |seconds|
168 // of active use to the |day| since Epoch. If there's usage data for
169 // day in the past in the usage file, that data is sent to UMA and
170 // removed from the file. If there's already usage data for |day| in
171 // the usage file, the |seconds| are accumulated.
172 void LogDailyUseRecord(int day, int seconds);
173
Darin Petkov1bb904e2010-06-16 15:58:06 -0700174 // Updates the active use time and logs time between user-space
175 // process crashes.
176 void ProcessUserCrash();
177
Darin Petkov38d5cb02010-06-24 12:10:26 -0700178 // Updates the active use time and logs time between kernel crashes.
179 void ProcessKernelCrash();
180
Ken Mixterccd84c02010-08-16 19:57:13 -0700181 // Updates the active use time and logs time between unclean shutdowns.
182 void ProcessUncleanShutdown();
183
184 // Checks if a kernel crash has been detected and returns true if
185 // so. The method assumes that a kernel crash has happened if
186 // |crash_file| exists. It removes the file immediately if it
187 // exists, so it must not be called more than once.
188 bool CheckSystemCrash(const std::string& crash_file);
Darin Petkov38d5cb02010-06-24 12:10:26 -0700189
Darin Petkov41e06232010-05-03 16:45:37 -0700190 // Callbacks for the daily use monitor. The daily use monitor uses
191 // LogDailyUseRecord to aggregate current usage data and send it to
192 // UMA, if necessary. It also reschedules itself using an
193 // exponentially bigger interval (up to a certain maximum) -- so
194 // usage is monitored less frequently with longer active use.
195 static gboolean UseMonitorStatic(gpointer data);
196 bool UseMonitor();
197
198 // Schedules or reschedules a daily use monitor for |interval|
199 // seconds from now. |backoff| mode is used by the use monitor to
200 // reschedule itself. If there's a monitor scheduled already and
201 // |backoff| is false, unschedules it first. Doesn't schedule a
202 // monitor for more than kUseMonitorIntervalMax seconds in the
203 // future (see metrics_daemon.cc). Returns true if a new use monitor
204 // was scheduled, false otherwise (note that if |backoff| is false a
205 // new use monitor will always be scheduled).
206 bool ScheduleUseMonitor(int interval, bool backoff);
207
208 // Unschedules a scheduled use monitor, if any.
209 void UnscheduleUseMonitor();
210
Ken Mixter4c5daa42010-08-26 18:35:06 -0700211 // Report daily use through UMA.
212 static void ReportDailyUse(void* handle, int tag, int count);
213
Darin Petkov11b8eb32010-05-18 11:00:59 -0700214 // Sends a regular (exponential) histogram sample to Chrome for
215 // transport to UMA. See MetricsLibrary::SendToUMA in
216 // metrics_library.h for a description of the arguments.
217 void SendMetric(const std::string& name, int sample,
218 int min, int max, int nbuckets);
Darin Petkov65b01462010-04-14 13:32:20 -0700219
Darin Petkov2ccef012010-05-05 16:06:37 -0700220 // Test mode.
Darin Petkov41e06232010-05-03 16:45:37 -0700221 bool testing_;
Darin Petkov65b01462010-04-14 13:32:20 -0700222
Darin Petkovfc91b422010-05-12 13:05:45 -0700223 // The metrics library handle.
224 MetricsLibraryInterface* metrics_lib_;
225
Darin Petkovf27f0362010-06-04 13:14:19 -0700226 // Timestamps last network state update. This timestamp is used to
227 // sample the time from the network going online to going offline so
228 // TimeTicks ensures a monotonically increasing TimeDelta.
229 base::TimeTicks network_state_last_;
Darin Petkov65b01462010-04-14 13:32:20 -0700230
Darin Petkov41e06232010-05-03 16:45:37 -0700231 // Current power state.
232 PowerState power_state_;
233
Darin Petkov41e06232010-05-03 16:45:37 -0700234 // Current user session state.
235 SessionState session_state_;
236
237 // Is the user currently active: power is on, user session has
238 // started, screen is not locked.
239 bool user_active_;
240
Darin Petkov1bb904e2010-06-16 15:58:06 -0700241 // Timestamps last user active update. Active use time is aggregated
242 // each day before sending to UMA so using time since the epoch as
243 // the timestamp.
Darin Petkovf27f0362010-06-04 13:14:19 -0700244 base::Time user_active_last_;
Darin Petkov41e06232010-05-03 16:45:37 -0700245
Darin Petkov1bb904e2010-06-16 15:58:06 -0700246 // Daily active use time in seconds.
Ken Mixter4c5daa42010-08-26 18:35:06 -0700247 scoped_ptr<chromeos_metrics::TaggedCounter> daily_use_;
Darin Petkov41e06232010-05-03 16:45:37 -0700248
Darin Petkov1bb904e2010-06-16 15:58:06 -0700249 // Active use time between user-space process crashes.
Ken Mixter4c5daa42010-08-26 18:35:06 -0700250 scoped_ptr<chromeos_metrics::TaggedCounterReporter> user_crash_interval_;
Darin Petkov1bb904e2010-06-16 15:58:06 -0700251
Darin Petkov38d5cb02010-06-24 12:10:26 -0700252 // Active use time between kernel crashes.
Ken Mixter4c5daa42010-08-26 18:35:06 -0700253 scoped_ptr<chromeos_metrics::TaggedCounterReporter> kernel_crash_interval_;
Darin Petkov38d5cb02010-06-24 12:10:26 -0700254
Ken Mixterccd84c02010-08-16 19:57:13 -0700255 // Active use time between unclean shutdowns crashes.
Ken Mixter4c5daa42010-08-26 18:35:06 -0700256 scoped_ptr<chromeos_metrics::TaggedCounterReporter>
Ken Mixterccd84c02010-08-16 19:57:13 -0700257 unclean_shutdown_interval_;
258
Ken Mixter4c5daa42010-08-26 18:35:06 -0700259 // Map of all frequency counters, to simplify flushing them.
260 FrequencyCounters frequency_counters_;
Ken Mixterccd84c02010-08-16 19:57:13 -0700261
Darin Petkov41e06232010-05-03 16:45:37 -0700262 // Sleep period until the next daily usage aggregation performed by
263 // the daily use monitor (see ScheduleUseMonitor).
264 int usemon_interval_;
265
266 // Scheduled daily use monitor source (see ScheduleUseMonitor).
267 GSource* usemon_source_;
Darin Petkov65b01462010-04-14 13:32:20 -0700268};
269
270#endif // METRICS_DAEMON_H_