blob: dd613227de02984802e338a6115cf6cc6f7b8f6c [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.
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -080032 void Init(bool testing, MetricsLibraryInterface* metrics_lib,
33 const char* diskstats_path);
Darin Petkov11b8eb32010-05-18 11:00:59 -070034
Darin Petkov703ec972010-04-27 11:02:18 -070035 // Does all the work. If |run_as_daemon| is true, daemonizes by
Darin Petkov2ccef012010-05-05 16:06:37 -070036 // forking.
37 void Run(bool run_as_daemon);
Darin Petkov65b01462010-04-14 13:32:20 -070038
39 private:
Darin Petkov2ccef012010-05-05 16:06:37 -070040 friend class MetricsDaemonTest;
Ken Mixterccd84c02010-08-16 19:57:13 -070041 FRIEND_TEST(MetricsDaemonTest, CheckSystemCrash);
Ken Mixter4c5daa42010-08-26 18:35:06 -070042 FRIEND_TEST(MetricsDaemonTest, ComputeEpochNoCurrent);
43 FRIEND_TEST(MetricsDaemonTest, ComputeEpochNoLast);
44 FRIEND_TEST(MetricsDaemonTest, GetHistogramPath);
45 FRIEND_TEST(MetricsDaemonTest, IsNewEpoch);
Darin Petkov2ccef012010-05-05 16:06:37 -070046 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 Petkov2ccef012010-05-05 16:06:37 -070050 FRIEND_TEST(MetricsDaemonTest, PowerStateChanged);
Darin Petkov38d5cb02010-06-24 12:10:26 -070051 FRIEND_TEST(MetricsDaemonTest, ProcessKernelCrash);
Ken Mixterccd84c02010-08-16 19:57:13 -070052 FRIEND_TEST(MetricsDaemonTest, ProcessUncleanShutdown);
Darin Petkov1bb904e2010-06-16 15:58:06 -070053 FRIEND_TEST(MetricsDaemonTest, ProcessUserCrash);
Ken Mixterccd84c02010-08-16 19:57:13 -070054 FRIEND_TEST(MetricsDaemonTest, ReportCrashesDailyFrequency);
55 FRIEND_TEST(MetricsDaemonTest, ReportDailyUse);
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -080056 FRIEND_TEST(MetricsDaemonTest, ReportDiskStats);
Ken Mixterccd84c02010-08-16 19:57:13 -070057 FRIEND_TEST(MetricsDaemonTest, ReportKernelCrashInterval);
58 FRIEND_TEST(MetricsDaemonTest, ReportUncleanShutdownInterval);
59 FRIEND_TEST(MetricsDaemonTest, ReportUserCrashInterval);
Darin Petkov2ccef012010-05-05 16:06:37 -070060 FRIEND_TEST(MetricsDaemonTest, ScreenSaverStateChanged);
Darin Petkov11b8eb32010-05-18 11:00:59 -070061 FRIEND_TEST(MetricsDaemonTest, SendMetric);
Darin Petkov2ccef012010-05-05 16:06:37 -070062 FRIEND_TEST(MetricsDaemonTest, SessionStateChanged);
Darin Petkovf1e85e42010-06-10 15:59:53 -070063 FRIEND_TEST(MetricsDaemonTest, SetUserActiveState);
Darin Petkovf27f0362010-06-04 13:14:19 -070064 FRIEND_TEST(MetricsDaemonTest, SetUserActiveStateTimeJump);
Darin Petkov2ccef012010-05-05 16:06:37 -070065
Darin Petkov703ec972010-04-27 11:02:18 -070066 // The power states (see power_states.h).
67 enum PowerState {
68 kUnknownPowerState = -1, // Initial/unknown power state.
69#define STATE(name, capname) kPowerState ## capname,
70#include "power_states.h"
71 kNumberPowerStates
72 };
Darin Petkov65b01462010-04-14 13:32:20 -070073
Darin Petkov41e06232010-05-03 16:45:37 -070074 // The user session states (see session_states.h).
75 enum SessionState {
76 kUnknownSessionState = -1, // Initial/unknown user session state.
77#define STATE(name, capname) kSessionState ## capname,
78#include "session_states.h"
79 kNumberSessionStates
80 };
81
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -080082 // State for disk stats collector callback.
83 enum DiskStatsState {
84 kDiskStatsShort, // short wait before short interval collection
85 kDiskStatsLong, // final wait before new collection
86 };
87
Darin Petkov41e06232010-05-03 16:45:37 -070088 // Data record for aggregating daily usage.
89 class UseRecord {
90 public:
91 UseRecord() : day_(0), seconds_(0) {}
92 int day_;
93 int seconds_;
94 };
95
Ken Mixter4c5daa42010-08-26 18:35:06 -070096 typedef std::map<std::string, chromeos_metrics::FrequencyCounter*>
97 FrequencyCounters;
98
Darin Petkov2ccef012010-05-05 16:06:37 -070099 // Metric parameters.
Ken Mixterccd84c02010-08-16 19:57:13 -0700100 static const char kMetricAnyCrashesDailyName[];
Ken Mixter4c5daa42010-08-26 18:35:06 -0700101 static const char kMetricAnyCrashesWeeklyName[];
102 static const char kMetricCrashFrequencyBuckets;
103 static const char kMetricCrashFrequencyMax;
104 static const char kMetricCrashFrequencyMin;
Ken Mixterccd84c02010-08-16 19:57:13 -0700105 static const int kMetricCrashIntervalBuckets;
106 static const int kMetricCrashIntervalMax;
107 static const int kMetricCrashIntervalMin;
108 static const int kMetricDailyUseTimeBuckets;
109 static const int kMetricDailyUseTimeMax;
110 static const int kMetricDailyUseTimeMin;
Darin Petkov2ccef012010-05-05 16:06:37 -0700111 static const char kMetricDailyUseTimeName[];
Ken Mixterccd84c02010-08-16 19:57:13 -0700112 static const char kMetricKernelCrashesDailyName[];
Ken Mixter4c5daa42010-08-26 18:35:06 -0700113 static const char kMetricKernelCrashesWeeklyName[];
Darin Petkov38d5cb02010-06-24 12:10:26 -0700114 static const char kMetricKernelCrashIntervalName[];
Ken Mixter4c5daa42010-08-26 18:35:06 -0700115 static const char kMetricsPath[];
Ken Mixterccd84c02010-08-16 19:57:13 -0700116 static const char kMetricUncleanShutdownIntervalName[];
117 static const char kMetricUncleanShutdownsDailyName[];
Ken Mixter4c5daa42010-08-26 18:35:06 -0700118 static const char kMetricUncleanShutdownsWeeklyName[];
Ken Mixterccd84c02010-08-16 19:57:13 -0700119 static const char kMetricUserCrashesDailyName[];
Ken Mixter4c5daa42010-08-26 18:35:06 -0700120 static const char kMetricUserCrashesWeeklyName[];
Darin Petkov1bb904e2010-06-16 15:58:06 -0700121 static const char kMetricUserCrashIntervalName[];
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800122 static const char kMetricReadSectorsLongName[];
123 static const char kMetricReadSectorsShortName[];
124 static const char kMetricWriteSectorsLongName[];
125 static const char kMetricWriteSectorsShortName[];
126 static const int kMetricDiskStatsShortInterval;
127 static const int kMetricDiskStatsLongInterval;
128 static const int kMetricSectorsIOMax;
129 static const int kMetricSectorsBuckets;
130 static const char kMetricsDiskStatsPath[];
Darin Petkov2ccef012010-05-05 16:06:37 -0700131
Darin Petkov41e06232010-05-03 16:45:37 -0700132 // D-Bus message match strings.
133 static const char* kDBusMatches_[];
134
Darin Petkov41e06232010-05-03 16:45:37 -0700135 // Array of power states.
136 static const char* kPowerStates_[kNumberPowerStates];
137
Darin Petkov41e06232010-05-03 16:45:37 -0700138 // Array of user session states.
139 static const char* kSessionStates_[kNumberSessionStates];
140
Ken Mixter4c5daa42010-08-26 18:35:06 -0700141 // Clears and deletes the data contained in frequency_counters_.
142 void DeleteFrequencyCounters();
143
144 // Configures the given crash interval reporter.
145 void ConfigureCrashIntervalReporter(
146 const char* histogram_name,
147 scoped_ptr<chromeos_metrics::TaggedCounterReporter>* reporter);
148
149 // Configures the given frequency counter reporter.
150 void ConfigureCrashFrequencyReporter(const char* histogram_name);
151
152 // Returns file path to persistent file for generating given histogram.
153 FilePath GetHistogramPath(const char* histogram_name);
154
Darin Petkov65b01462010-04-14 13:32:20 -0700155 // Creates the event loop and enters it.
156 void Loop();
157
Darin Petkov703ec972010-04-27 11:02:18 -0700158 // D-Bus filter callback.
159 static DBusHandlerResult MessageFilter(DBusConnection* connection,
160 DBusMessage* message,
161 void* user_data);
Darin Petkov65b01462010-04-14 13:32:20 -0700162
Darin Petkov703ec972010-04-27 11:02:18 -0700163 // Processes power state change.
Darin Petkovf27f0362010-06-04 13:14:19 -0700164 void PowerStateChanged(const char* state_name, base::Time now);
Darin Petkov703ec972010-04-27 11:02:18 -0700165
166 // Given the state name, returns the state id.
167 PowerState LookupPowerState(const char* state_name);
Darin Petkov65b01462010-04-14 13:32:20 -0700168
Darin Petkov41e06232010-05-03 16:45:37 -0700169 // Processes user session state change.
Darin Petkovf27f0362010-06-04 13:14:19 -0700170 void SessionStateChanged(const char* state_name, base::Time now);
Darin Petkov41e06232010-05-03 16:45:37 -0700171
172 // Given the state name, returns the state id.
173 SessionState LookupSessionState(const char* state_name);
174
175 // Updates the user-active state to |active| and logs the usage data
176 // since the last update. If the user has just become active,
177 // reschedule the daily use monitor for more frequent updates --
178 // this is followed by an exponential back-off (see UseMonitor).
Darin Petkovf27f0362010-06-04 13:14:19 -0700179 // While in active use, this method should be called at intervals no
180 // longer than kUseMonitorIntervalMax otherwise new use time will be
181 // discarded.
182 void SetUserActiveState(bool active, base::Time now);
Darin Petkov41e06232010-05-03 16:45:37 -0700183
184 // Updates the daily usage file, if necessary, by adding |seconds|
185 // of active use to the |day| since Epoch. If there's usage data for
186 // day in the past in the usage file, that data is sent to UMA and
187 // removed from the file. If there's already usage data for |day| in
188 // the usage file, the |seconds| are accumulated.
189 void LogDailyUseRecord(int day, int seconds);
190
Darin Petkov1bb904e2010-06-16 15:58:06 -0700191 // Updates the active use time and logs time between user-space
192 // process crashes.
193 void ProcessUserCrash();
194
Darin Petkov38d5cb02010-06-24 12:10:26 -0700195 // Updates the active use time and logs time between kernel crashes.
196 void ProcessKernelCrash();
197
Ken Mixterccd84c02010-08-16 19:57:13 -0700198 // Updates the active use time and logs time between unclean shutdowns.
199 void ProcessUncleanShutdown();
200
201 // Checks if a kernel crash has been detected and returns true if
202 // so. The method assumes that a kernel crash has happened if
203 // |crash_file| exists. It removes the file immediately if it
204 // exists, so it must not be called more than once.
205 bool CheckSystemCrash(const std::string& crash_file);
Darin Petkov38d5cb02010-06-24 12:10:26 -0700206
Darin Petkov41e06232010-05-03 16:45:37 -0700207 // Callbacks for the daily use monitor. The daily use monitor uses
208 // LogDailyUseRecord to aggregate current usage data and send it to
209 // UMA, if necessary. It also reschedules itself using an
210 // exponentially bigger interval (up to a certain maximum) -- so
211 // usage is monitored less frequently with longer active use.
212 static gboolean UseMonitorStatic(gpointer data);
213 bool UseMonitor();
214
215 // Schedules or reschedules a daily use monitor for |interval|
216 // seconds from now. |backoff| mode is used by the use monitor to
217 // reschedule itself. If there's a monitor scheduled already and
218 // |backoff| is false, unschedules it first. Doesn't schedule a
219 // monitor for more than kUseMonitorIntervalMax seconds in the
220 // future (see metrics_daemon.cc). Returns true if a new use monitor
221 // was scheduled, false otherwise (note that if |backoff| is false a
222 // new use monitor will always be scheduled).
223 bool ScheduleUseMonitor(int interval, bool backoff);
224
225 // Unschedules a scheduled use monitor, if any.
226 void UnscheduleUseMonitor();
227
Ken Mixter4c5daa42010-08-26 18:35:06 -0700228 // Report daily use through UMA.
229 static void ReportDailyUse(void* handle, int tag, int count);
230
Darin Petkov11b8eb32010-05-18 11:00:59 -0700231 // Sends a regular (exponential) histogram sample to Chrome for
232 // transport to UMA. See MetricsLibrary::SendToUMA in
233 // metrics_library.h for a description of the arguments.
234 void SendMetric(const std::string& name, int sample,
235 int min, int max, int nbuckets);
Darin Petkov65b01462010-04-14 13:32:20 -0700236
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800237 // Initializes disk stats reporting.
238 void DiskStatsReporterInit();
239
240 // Schedules a callback for the next disk stats collection.
241 void ScheduleDiskStatsCallback(int wait);
242
243 // Reads cumulative disk statistics from sysfs.
244 void DiskStatsReadStats(long int* read_sectors, long int* write_sectors);
245
246 // Reports disk statistics (static version for glib). Arguments are a glib
247 // artifact.
248 static gboolean DiskStatsCallbackStatic(void* handle);
249
250 // Reports disk statistics.
251 void DiskStatsCallback();
252
Darin Petkov2ccef012010-05-05 16:06:37 -0700253 // Test mode.
Darin Petkov41e06232010-05-03 16:45:37 -0700254 bool testing_;
Darin Petkov65b01462010-04-14 13:32:20 -0700255
Darin Petkovfc91b422010-05-12 13:05:45 -0700256 // The metrics library handle.
257 MetricsLibraryInterface* metrics_lib_;
258
Darin Petkovf27f0362010-06-04 13:14:19 -0700259 // Timestamps last network state update. This timestamp is used to
260 // sample the time from the network going online to going offline so
261 // TimeTicks ensures a monotonically increasing TimeDelta.
262 base::TimeTicks network_state_last_;
Darin Petkov65b01462010-04-14 13:32:20 -0700263
Darin Petkov41e06232010-05-03 16:45:37 -0700264 // Current power state.
265 PowerState power_state_;
266
Darin Petkov41e06232010-05-03 16:45:37 -0700267 // Current user session state.
268 SessionState session_state_;
269
270 // Is the user currently active: power is on, user session has
271 // started, screen is not locked.
272 bool user_active_;
273
Darin Petkov1bb904e2010-06-16 15:58:06 -0700274 // Timestamps last user active update. Active use time is aggregated
275 // each day before sending to UMA so using time since the epoch as
276 // the timestamp.
Darin Petkovf27f0362010-06-04 13:14:19 -0700277 base::Time user_active_last_;
Darin Petkov41e06232010-05-03 16:45:37 -0700278
Darin Petkov1bb904e2010-06-16 15:58:06 -0700279 // Daily active use time in seconds.
Ken Mixter4c5daa42010-08-26 18:35:06 -0700280 scoped_ptr<chromeos_metrics::TaggedCounter> daily_use_;
Darin Petkov41e06232010-05-03 16:45:37 -0700281
Darin Petkov1bb904e2010-06-16 15:58:06 -0700282 // Active use time between user-space process crashes.
Ken Mixter4c5daa42010-08-26 18:35:06 -0700283 scoped_ptr<chromeos_metrics::TaggedCounterReporter> user_crash_interval_;
Darin Petkov1bb904e2010-06-16 15:58:06 -0700284
Darin Petkov38d5cb02010-06-24 12:10:26 -0700285 // Active use time between kernel crashes.
Ken Mixter4c5daa42010-08-26 18:35:06 -0700286 scoped_ptr<chromeos_metrics::TaggedCounterReporter> kernel_crash_interval_;
Darin Petkov38d5cb02010-06-24 12:10:26 -0700287
Ken Mixterccd84c02010-08-16 19:57:13 -0700288 // Active use time between unclean shutdowns crashes.
Ken Mixter4c5daa42010-08-26 18:35:06 -0700289 scoped_ptr<chromeos_metrics::TaggedCounterReporter>
Ken Mixterccd84c02010-08-16 19:57:13 -0700290 unclean_shutdown_interval_;
291
Ken Mixter4c5daa42010-08-26 18:35:06 -0700292 // Map of all frequency counters, to simplify flushing them.
293 FrequencyCounters frequency_counters_;
Ken Mixterccd84c02010-08-16 19:57:13 -0700294
Darin Petkov41e06232010-05-03 16:45:37 -0700295 // Sleep period until the next daily usage aggregation performed by
296 // the daily use monitor (see ScheduleUseMonitor).
297 int usemon_interval_;
298
299 // Scheduled daily use monitor source (see ScheduleUseMonitor).
300 GSource* usemon_source_;
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800301
302 // Contains the most recent disk stats.
303 long int read_sectors_;
304 long int write_sectors_;
305
306 DiskStatsState diskstats_state_;
307 const char* diskstats_path_;
Darin Petkov65b01462010-04-14 13:32:20 -0700308};
309
310#endif // METRICS_DAEMON_H_