blob: e0614452bcc9b0258c86a89dd76bf3d72b707440 [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,
Luigi Semenzato0f132bb2011-02-28 11:17:43 -080033 std::string 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);
Luigi Semenzato29c7ef92011-04-12 14:12:35 -070052 FRIEND_TEST(MetricsDaemonTest, ProcessMeminfo);
53 FRIEND_TEST(MetricsDaemonTest, ProcessMeminfo2);
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);
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -080058 FRIEND_TEST(MetricsDaemonTest, ReportDiskStats);
Ken Mixterccd84c02010-08-16 19:57:13 -070059 FRIEND_TEST(MetricsDaemonTest, ReportKernelCrashInterval);
60 FRIEND_TEST(MetricsDaemonTest, ReportUncleanShutdownInterval);
61 FRIEND_TEST(MetricsDaemonTest, ReportUserCrashInterval);
Darin Petkov2ccef012010-05-05 16:06:37 -070062 FRIEND_TEST(MetricsDaemonTest, ScreenSaverStateChanged);
Darin Petkov11b8eb32010-05-18 11:00:59 -070063 FRIEND_TEST(MetricsDaemonTest, SendMetric);
Darin Petkov2ccef012010-05-05 16:06:37 -070064 FRIEND_TEST(MetricsDaemonTest, SessionStateChanged);
Darin Petkovf1e85e42010-06-10 15:59:53 -070065 FRIEND_TEST(MetricsDaemonTest, SetUserActiveState);
Darin Petkovf27f0362010-06-04 13:14:19 -070066 FRIEND_TEST(MetricsDaemonTest, SetUserActiveStateTimeJump);
Darin Petkov2ccef012010-05-05 16:06:37 -070067
Darin Petkov703ec972010-04-27 11:02:18 -070068 // The power states (see power_states.h).
69 enum PowerState {
70 kUnknownPowerState = -1, // Initial/unknown power state.
71#define STATE(name, capname) kPowerState ## capname,
72#include "power_states.h"
73 kNumberPowerStates
74 };
Darin Petkov65b01462010-04-14 13:32:20 -070075
Darin Petkov41e06232010-05-03 16:45:37 -070076 // The user session states (see session_states.h).
77 enum SessionState {
78 kUnknownSessionState = -1, // Initial/unknown user session state.
79#define STATE(name, capname) kSessionState ## capname,
80#include "session_states.h"
81 kNumberSessionStates
82 };
83
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -080084 // State for disk stats collector callback.
85 enum DiskStatsState {
86 kDiskStatsShort, // short wait before short interval collection
87 kDiskStatsLong, // final wait before new collection
88 };
89
Darin Petkov41e06232010-05-03 16:45:37 -070090 // Data record for aggregating daily usage.
91 class UseRecord {
92 public:
93 UseRecord() : day_(0), seconds_(0) {}
94 int day_;
95 int seconds_;
96 };
97
Ken Mixter4c5daa42010-08-26 18:35:06 -070098 typedef std::map<std::string, chromeos_metrics::FrequencyCounter*>
99 FrequencyCounters;
100
Darin Petkov2ccef012010-05-05 16:06:37 -0700101 // Metric parameters.
Ken Mixterccd84c02010-08-16 19:57:13 -0700102 static const char kMetricAnyCrashesDailyName[];
Ken Mixter4c5daa42010-08-26 18:35:06 -0700103 static const char kMetricAnyCrashesWeeklyName[];
104 static const char kMetricCrashFrequencyBuckets;
105 static const char kMetricCrashFrequencyMax;
106 static const char kMetricCrashFrequencyMin;
Ken Mixterccd84c02010-08-16 19:57:13 -0700107 static const int kMetricCrashIntervalBuckets;
108 static const int kMetricCrashIntervalMax;
109 static const int kMetricCrashIntervalMin;
110 static const int kMetricDailyUseTimeBuckets;
111 static const int kMetricDailyUseTimeMax;
112 static const int kMetricDailyUseTimeMin;
Darin Petkov2ccef012010-05-05 16:06:37 -0700113 static const char kMetricDailyUseTimeName[];
Ken Mixterccd84c02010-08-16 19:57:13 -0700114 static const char kMetricKernelCrashesDailyName[];
Ken Mixter4c5daa42010-08-26 18:35:06 -0700115 static const char kMetricKernelCrashesWeeklyName[];
Darin Petkov38d5cb02010-06-24 12:10:26 -0700116 static const char kMetricKernelCrashIntervalName[];
Ken Mixter4c5daa42010-08-26 18:35:06 -0700117 static const char kMetricsPath[];
Ken Mixterccd84c02010-08-16 19:57:13 -0700118 static const char kMetricUncleanShutdownIntervalName[];
119 static const char kMetricUncleanShutdownsDailyName[];
Ken Mixter4c5daa42010-08-26 18:35:06 -0700120 static const char kMetricUncleanShutdownsWeeklyName[];
Ken Mixterccd84c02010-08-16 19:57:13 -0700121 static const char kMetricUserCrashesDailyName[];
Ken Mixter4c5daa42010-08-26 18:35:06 -0700122 static const char kMetricUserCrashesWeeklyName[];
Darin Petkov1bb904e2010-06-16 15:58:06 -0700123 static const char kMetricUserCrashIntervalName[];
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800124 static const char kMetricReadSectorsLongName[];
125 static const char kMetricReadSectorsShortName[];
126 static const char kMetricWriteSectorsLongName[];
127 static const char kMetricWriteSectorsShortName[];
128 static const int kMetricDiskStatsShortInterval;
129 static const int kMetricDiskStatsLongInterval;
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700130 static const int kMetricMeminfoInterval;
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800131 static const int kMetricSectorsIOMax;
132 static const int kMetricSectorsBuckets;
133 static const char kMetricsDiskStatsPath[];
Darin Petkov2ccef012010-05-05 16:06:37 -0700134
Darin Petkov41e06232010-05-03 16:45:37 -0700135 // D-Bus message match strings.
136 static const char* kDBusMatches_[];
137
Darin Petkov41e06232010-05-03 16:45:37 -0700138 // Array of power states.
139 static const char* kPowerStates_[kNumberPowerStates];
140
Darin Petkov41e06232010-05-03 16:45:37 -0700141 // Array of user session states.
142 static const char* kSessionStates_[kNumberSessionStates];
143
Ken Mixter4c5daa42010-08-26 18:35:06 -0700144 // Clears and deletes the data contained in frequency_counters_.
145 void DeleteFrequencyCounters();
146
147 // Configures the given crash interval reporter.
148 void ConfigureCrashIntervalReporter(
149 const char* histogram_name,
150 scoped_ptr<chromeos_metrics::TaggedCounterReporter>* reporter);
151
152 // Configures the given frequency counter reporter.
153 void ConfigureCrashFrequencyReporter(const char* histogram_name);
154
155 // Returns file path to persistent file for generating given histogram.
156 FilePath GetHistogramPath(const char* histogram_name);
157
Darin Petkov65b01462010-04-14 13:32:20 -0700158 // Creates the event loop and enters it.
159 void Loop();
160
Darin Petkov703ec972010-04-27 11:02:18 -0700161 // D-Bus filter callback.
162 static DBusHandlerResult MessageFilter(DBusConnection* connection,
163 DBusMessage* message,
164 void* user_data);
Darin Petkov65b01462010-04-14 13:32:20 -0700165
Darin Petkov703ec972010-04-27 11:02:18 -0700166 // Processes power state change.
Darin Petkovf27f0362010-06-04 13:14:19 -0700167 void PowerStateChanged(const char* state_name, base::Time now);
Darin Petkov703ec972010-04-27 11:02:18 -0700168
169 // Given the state name, returns the state id.
170 PowerState LookupPowerState(const char* state_name);
Darin Petkov65b01462010-04-14 13:32:20 -0700171
Darin Petkov41e06232010-05-03 16:45:37 -0700172 // Processes user session state change.
Darin Petkovf27f0362010-06-04 13:14:19 -0700173 void SessionStateChanged(const char* state_name, base::Time now);
Darin Petkov41e06232010-05-03 16:45:37 -0700174
175 // Given the state name, returns the state id.
176 SessionState LookupSessionState(const char* state_name);
177
178 // Updates the user-active state to |active| and logs the usage data
179 // since the last update. If the user has just become active,
180 // reschedule the daily use monitor for more frequent updates --
181 // this is followed by an exponential back-off (see UseMonitor).
Darin Petkovf27f0362010-06-04 13:14:19 -0700182 // While in active use, this method should be called at intervals no
183 // longer than kUseMonitorIntervalMax otherwise new use time will be
184 // discarded.
185 void SetUserActiveState(bool active, base::Time now);
Darin Petkov41e06232010-05-03 16:45:37 -0700186
187 // Updates the daily usage file, if necessary, by adding |seconds|
188 // of active use to the |day| since Epoch. If there's usage data for
189 // day in the past in the usage file, that data is sent to UMA and
190 // removed from the file. If there's already usage data for |day| in
191 // the usage file, the |seconds| are accumulated.
192 void LogDailyUseRecord(int day, int seconds);
193
Darin Petkov1bb904e2010-06-16 15:58:06 -0700194 // Updates the active use time and logs time between user-space
195 // process crashes.
196 void ProcessUserCrash();
197
Darin Petkov38d5cb02010-06-24 12:10:26 -0700198 // Updates the active use time and logs time between kernel crashes.
199 void ProcessKernelCrash();
200
Ken Mixterccd84c02010-08-16 19:57:13 -0700201 // Updates the active use time and logs time between unclean shutdowns.
202 void ProcessUncleanShutdown();
203
204 // Checks if a kernel crash has been detected and returns true if
205 // so. The method assumes that a kernel crash has happened if
206 // |crash_file| exists. It removes the file immediately if it
207 // exists, so it must not be called more than once.
208 bool CheckSystemCrash(const std::string& crash_file);
Darin Petkov38d5cb02010-06-24 12:10:26 -0700209
Darin Petkov41e06232010-05-03 16:45:37 -0700210 // Callbacks for the daily use monitor. The daily use monitor uses
211 // LogDailyUseRecord to aggregate current usage data and send it to
212 // UMA, if necessary. It also reschedules itself using an
213 // exponentially bigger interval (up to a certain maximum) -- so
214 // usage is monitored less frequently with longer active use.
215 static gboolean UseMonitorStatic(gpointer data);
216 bool UseMonitor();
217
218 // Schedules or reschedules a daily use monitor for |interval|
219 // seconds from now. |backoff| mode is used by the use monitor to
220 // reschedule itself. If there's a monitor scheduled already and
221 // |backoff| is false, unschedules it first. Doesn't schedule a
222 // monitor for more than kUseMonitorIntervalMax seconds in the
223 // future (see metrics_daemon.cc). Returns true if a new use monitor
224 // was scheduled, false otherwise (note that if |backoff| is false a
225 // new use monitor will always be scheduled).
226 bool ScheduleUseMonitor(int interval, bool backoff);
227
228 // Unschedules a scheduled use monitor, if any.
229 void UnscheduleUseMonitor();
230
Ken Mixter4c5daa42010-08-26 18:35:06 -0700231 // Report daily use through UMA.
232 static void ReportDailyUse(void* handle, int tag, int count);
233
Darin Petkov11b8eb32010-05-18 11:00:59 -0700234 // Sends a regular (exponential) histogram sample to Chrome for
235 // transport to UMA. See MetricsLibrary::SendToUMA in
236 // metrics_library.h for a description of the arguments.
237 void SendMetric(const std::string& name, int sample,
238 int min, int max, int nbuckets);
Darin Petkov65b01462010-04-14 13:32:20 -0700239
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700240 // Sends a linear histogram sample to Chrome for transport to UMA. See
241 // MetricsLibrary::SendToUMA in metrics_library.h for a description of the
242 // arguments.
243 void SendLinearMetric(const std::string& name, int sample,
244 int max, int nbuckets);
245
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800246 // Initializes disk stats reporting.
247 void DiskStatsReporterInit();
248
249 // Schedules a callback for the next disk stats collection.
250 void ScheduleDiskStatsCallback(int wait);
251
252 // Reads cumulative disk statistics from sysfs.
253 void DiskStatsReadStats(long int* read_sectors, long int* write_sectors);
254
255 // Reports disk statistics (static version for glib). Arguments are a glib
256 // artifact.
257 static gboolean DiskStatsCallbackStatic(void* handle);
258
259 // Reports disk statistics.
260 void DiskStatsCallback();
261
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700262 // Schedules meminfo collection callback.
263 void ScheduleMeminfoCallback(int wait);
264
265 // Reports memory statistics (static version for glib). Argument is a glib
266 // artifact.
267 static gboolean MeminfoCallbackStatic(void* handle);
268
269 // Reports memory statistics. Returns false on failure.
270 gboolean MeminfoCallback();
271
272 // Parses content of /proc/meminfo and sends fields of interest to UMA.
273 // Returns false on errors.
274 gboolean ProcessMeminfo(std::string meminfo);
275
Darin Petkov2ccef012010-05-05 16:06:37 -0700276 // Test mode.
Darin Petkov41e06232010-05-03 16:45:37 -0700277 bool testing_;
Darin Petkov65b01462010-04-14 13:32:20 -0700278
Darin Petkovfc91b422010-05-12 13:05:45 -0700279 // The metrics library handle.
280 MetricsLibraryInterface* metrics_lib_;
281
Darin Petkovf27f0362010-06-04 13:14:19 -0700282 // Timestamps last network state update. This timestamp is used to
283 // sample the time from the network going online to going offline so
284 // TimeTicks ensures a monotonically increasing TimeDelta.
285 base::TimeTicks network_state_last_;
Darin Petkov65b01462010-04-14 13:32:20 -0700286
Darin Petkov41e06232010-05-03 16:45:37 -0700287 // Current power state.
288 PowerState power_state_;
289
Darin Petkov41e06232010-05-03 16:45:37 -0700290 // Current user session state.
291 SessionState session_state_;
292
293 // Is the user currently active: power is on, user session has
294 // started, screen is not locked.
295 bool user_active_;
296
Darin Petkov1bb904e2010-06-16 15:58:06 -0700297 // Timestamps last user active update. Active use time is aggregated
298 // each day before sending to UMA so using time since the epoch as
299 // the timestamp.
Darin Petkovf27f0362010-06-04 13:14:19 -0700300 base::Time user_active_last_;
Darin Petkov41e06232010-05-03 16:45:37 -0700301
Darin Petkov1bb904e2010-06-16 15:58:06 -0700302 // Daily active use time in seconds.
Ken Mixter4c5daa42010-08-26 18:35:06 -0700303 scoped_ptr<chromeos_metrics::TaggedCounter> daily_use_;
Darin Petkov41e06232010-05-03 16:45:37 -0700304
Darin Petkov1bb904e2010-06-16 15:58:06 -0700305 // Active use time between user-space process crashes.
Ken Mixter4c5daa42010-08-26 18:35:06 -0700306 scoped_ptr<chromeos_metrics::TaggedCounterReporter> user_crash_interval_;
Darin Petkov1bb904e2010-06-16 15:58:06 -0700307
Darin Petkov38d5cb02010-06-24 12:10:26 -0700308 // Active use time between kernel crashes.
Ken Mixter4c5daa42010-08-26 18:35:06 -0700309 scoped_ptr<chromeos_metrics::TaggedCounterReporter> kernel_crash_interval_;
Darin Petkov38d5cb02010-06-24 12:10:26 -0700310
Ken Mixterccd84c02010-08-16 19:57:13 -0700311 // Active use time between unclean shutdowns crashes.
Ken Mixter4c5daa42010-08-26 18:35:06 -0700312 scoped_ptr<chromeos_metrics::TaggedCounterReporter>
Ken Mixterccd84c02010-08-16 19:57:13 -0700313 unclean_shutdown_interval_;
314
Ken Mixter4c5daa42010-08-26 18:35:06 -0700315 // Map of all frequency counters, to simplify flushing them.
316 FrequencyCounters frequency_counters_;
Ken Mixterccd84c02010-08-16 19:57:13 -0700317
Darin Petkov41e06232010-05-03 16:45:37 -0700318 // Sleep period until the next daily usage aggregation performed by
319 // the daily use monitor (see ScheduleUseMonitor).
320 int usemon_interval_;
321
322 // Scheduled daily use monitor source (see ScheduleUseMonitor).
323 GSource* usemon_source_;
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800324
325 // Contains the most recent disk stats.
326 long int read_sectors_;
327 long int write_sectors_;
328
329 DiskStatsState diskstats_state_;
Luigi Semenzato0f132bb2011-02-28 11:17:43 -0800330 std::string diskstats_path_;
Darin Petkov65b01462010-04-14 13:32:20 -0700331};
332
333#endif // METRICS_DAEMON_H_