blob: 6d8f81b1bed352b0fb1eb867e7a2e0f875c36bfd [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
Ben Chan2e6543d2014-02-05 23:26:25 -080012#include <base/files/file_path.h>
Chris Masone817016a2011-05-12 14:14:48 -070013#include <base/memory/scoped_ptr.h>
Ben Chan2e6543d2014-02-05 23:26:25 -080014#include <base/time/time.h>
Darin Petkovf1e85e42010-06-10 15:59:53 -070015#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;
Luigi Semenzato859b3f02014-02-05 15:33:19 -080021class VersionCounter;
Ken Mixter4c5daa42010-08-26 18:35:06 -070022class TaggedCounter;
23class TaggedCounterReporter;
Ken Mixterccd84c02010-08-16 19:57:13 -070024}
Darin Petkov2ccef012010-05-05 16:06:37 -070025
Darin Petkov65b01462010-04-14 13:32:20 -070026class MetricsDaemon {
27
28 public:
Darin Petkovf1e85e42010-06-10 15:59:53 -070029 MetricsDaemon();
30 ~MetricsDaemon();
Darin Petkov65b01462010-04-14 13:32:20 -070031
Darin Petkov11b8eb32010-05-18 11:00:59 -070032 // Initializes.
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -080033 void Init(bool testing, MetricsLibraryInterface* metrics_lib,
Luigi Semenzato5bd764f2011-10-14 12:03:35 -070034 const std::string& diskstats_path,
Luigi Semenzatofb3a8212013-05-07 16:55:00 -070035 const std::string& vmstats_path,
36 const std::string& cpuinfo_max_freq_path,
37 const std::string& scaling_max_freq_path);
Darin Petkov11b8eb32010-05-18 11:00:59 -070038
Darin Petkov703ec972010-04-27 11:02:18 -070039 // Does all the work. If |run_as_daemon| is true, daemonizes by
Darin Petkov2ccef012010-05-05 16:06:37 -070040 // forking.
41 void Run(bool run_as_daemon);
Darin Petkov65b01462010-04-14 13:32:20 -070042
43 private:
Darin Petkov2ccef012010-05-05 16:06:37 -070044 friend class MetricsDaemonTest;
Ken Mixterccd84c02010-08-16 19:57:13 -070045 FRIEND_TEST(MetricsDaemonTest, CheckSystemCrash);
Ken Mixter4c5daa42010-08-26 18:35:06 -070046 FRIEND_TEST(MetricsDaemonTest, ComputeEpochNoCurrent);
47 FRIEND_TEST(MetricsDaemonTest, ComputeEpochNoLast);
48 FRIEND_TEST(MetricsDaemonTest, GetHistogramPath);
49 FRIEND_TEST(MetricsDaemonTest, IsNewEpoch);
Darin Petkov2ccef012010-05-05 16:06:37 -070050 FRIEND_TEST(MetricsDaemonTest, LookupPowerState);
51 FRIEND_TEST(MetricsDaemonTest, LookupScreenSaverState);
52 FRIEND_TEST(MetricsDaemonTest, LookupSessionState);
Darin Petkove579d662010-05-05 16:19:39 -070053 FRIEND_TEST(MetricsDaemonTest, MessageFilter);
Luigi Semenzato5bd764f2011-10-14 12:03:35 -070054 FRIEND_TEST(MetricsDaemonTest, ParseVmStats);
Darin Petkov2ccef012010-05-05 16:06:37 -070055 FRIEND_TEST(MetricsDaemonTest, PowerStateChanged);
Darin Petkov38d5cb02010-06-24 12:10:26 -070056 FRIEND_TEST(MetricsDaemonTest, ProcessKernelCrash);
Luigi Semenzato29c7ef92011-04-12 14:12:35 -070057 FRIEND_TEST(MetricsDaemonTest, ProcessMeminfo);
58 FRIEND_TEST(MetricsDaemonTest, ProcessMeminfo2);
Ken Mixterccd84c02010-08-16 19:57:13 -070059 FRIEND_TEST(MetricsDaemonTest, ProcessUncleanShutdown);
Darin Petkov1bb904e2010-06-16 15:58:06 -070060 FRIEND_TEST(MetricsDaemonTest, ProcessUserCrash);
Ken Mixterccd84c02010-08-16 19:57:13 -070061 FRIEND_TEST(MetricsDaemonTest, ReportCrashesDailyFrequency);
Luigi Semenzatofb3a8212013-05-07 16:55:00 -070062 FRIEND_TEST(MetricsDaemonTest, ReadFreqToInt);
Ken Mixterccd84c02010-08-16 19:57:13 -070063 FRIEND_TEST(MetricsDaemonTest, ReportDailyUse);
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -080064 FRIEND_TEST(MetricsDaemonTest, ReportDiskStats);
Ken Mixterccd84c02010-08-16 19:57:13 -070065 FRIEND_TEST(MetricsDaemonTest, ReportKernelCrashInterval);
66 FRIEND_TEST(MetricsDaemonTest, ReportUncleanShutdownInterval);
67 FRIEND_TEST(MetricsDaemonTest, ReportUserCrashInterval);
Darin Petkov2ccef012010-05-05 16:06:37 -070068 FRIEND_TEST(MetricsDaemonTest, ScreenSaverStateChanged);
Darin Petkov11b8eb32010-05-18 11:00:59 -070069 FRIEND_TEST(MetricsDaemonTest, SendMetric);
Luigi Semenzatofb3a8212013-05-07 16:55:00 -070070 FRIEND_TEST(MetricsDaemonTest, SendCpuThrottleMetrics);
Darin Petkov2ccef012010-05-05 16:06:37 -070071 FRIEND_TEST(MetricsDaemonTest, SessionStateChanged);
Darin Petkovf1e85e42010-06-10 15:59:53 -070072 FRIEND_TEST(MetricsDaemonTest, SetUserActiveState);
Darin Petkovf27f0362010-06-04 13:14:19 -070073 FRIEND_TEST(MetricsDaemonTest, SetUserActiveStateTimeJump);
Darin Petkov2ccef012010-05-05 16:06:37 -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
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -080091 // State for disk stats collector callback.
Luigi Semenzato5bd764f2011-10-14 12:03:35 -070092 enum StatsState {
93 kStatsShort, // short wait before short interval collection
94 kStatsLong, // final wait before new collection
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -080095 };
96
Darin Petkov41e06232010-05-03 16:45:37 -070097 // Data record for aggregating daily usage.
98 class UseRecord {
99 public:
100 UseRecord() : day_(0), seconds_(0) {}
101 int day_;
102 int seconds_;
103 };
104
Luigi Semenzato3ccca062013-02-04 19:50:45 -0800105 // Type of scale to use for meminfo histograms. For most of them we use
106 // percent of total RAM, but for some we use absolute numbers, usually in
107 // megabytes, on a log scale from 0 to 4000, and 0 to 8000 for compressed
108 // swap (since it can be larger than total RAM).
Luigi Semenzato942cbab2013-02-12 13:17:07 -0800109 enum MeminfoOp {
110 kMeminfoOp_HistPercent = 0,
111 kMeminfoOp_HistLog,
112 kMeminfoOp_SwapTotal,
113 kMeminfoOp_SwapFree,
Luigi Semenzato3ccca062013-02-04 19:50:45 -0800114 };
115
Luigi Semenzato8accd332011-05-17 16:37:18 -0700116 // Record for retrieving and reporting values from /proc/meminfo.
117 struct MeminfoRecord {
Luigi Semenzato3ccca062013-02-04 19:50:45 -0800118 const char* name; // print name
119 const char* match; // string to match in output of /proc/meminfo
Luigi Semenzato942cbab2013-02-12 13:17:07 -0800120 MeminfoOp op; // histogram scale selector, or other operator
Luigi Semenzato3ccca062013-02-04 19:50:45 -0800121 int value; // value from /proc/meminfo
Luigi Semenzato8accd332011-05-17 16:37:18 -0700122 };
123
Sonny Rao4b8aebb2013-07-31 23:18:31 -0700124 // Record for retrieving and reporting values from /proc/vmstat
125 struct VmstatRecord {
126 uint64_t page_faults_; // major faults
127 uint64_t swap_in_; // pages swapped in
128 uint64_t swap_out_; // pages swapped out
129 };
130
Ken Mixter4c5daa42010-08-26 18:35:06 -0700131 typedef std::map<std::string, chromeos_metrics::FrequencyCounter*>
132 FrequencyCounters;
133
Darin Petkov2ccef012010-05-05 16:06:37 -0700134 // Metric parameters.
Ken Mixterccd84c02010-08-16 19:57:13 -0700135 static const char kMetricAnyCrashesDailyName[];
Ken Mixter4c5daa42010-08-26 18:35:06 -0700136 static const char kMetricAnyCrashesWeeklyName[];
Luigi Semenzato859b3f02014-02-05 15:33:19 -0800137 static const int kMetricCrashFrequencyBuckets;
138 static const int kMetricCrashFrequencyMax;
139 static const int kMetricCrashFrequencyMin;
Ken Mixterccd84c02010-08-16 19:57:13 -0700140 static const int kMetricCrashIntervalBuckets;
141 static const int kMetricCrashIntervalMax;
142 static const int kMetricCrashIntervalMin;
Luigi Semenzato859b3f02014-02-05 15:33:19 -0800143 static const int kMetricCumulativeCrashCountBuckets;
144 static const int kMetricCumulativeCrashCountMax;
145 static const int kMetricCumulativeCrashCountMin;
Ken Mixterccd84c02010-08-16 19:57:13 -0700146 static const int kMetricDailyUseTimeBuckets;
147 static const int kMetricDailyUseTimeMax;
148 static const int kMetricDailyUseTimeMin;
Darin Petkov2ccef012010-05-05 16:06:37 -0700149 static const char kMetricDailyUseTimeName[];
Ken Mixterccd84c02010-08-16 19:57:13 -0700150 static const char kMetricKernelCrashesDailyName[];
Ken Mixter4c5daa42010-08-26 18:35:06 -0700151 static const char kMetricKernelCrashesWeeklyName[];
Luigi Semenzato859b3f02014-02-05 15:33:19 -0800152 static const char kMetricKernelCrashesVersionName[];
Darin Petkov38d5cb02010-06-24 12:10:26 -0700153 static const char kMetricKernelCrashIntervalName[];
Ken Mixter4c5daa42010-08-26 18:35:06 -0700154 static const char kMetricsPath[];
Luigi Semenzato859b3f02014-02-05 15:33:19 -0800155 static const char kLsbReleasePath[];
Ken Mixterccd84c02010-08-16 19:57:13 -0700156 static const char kMetricUncleanShutdownIntervalName[];
157 static const char kMetricUncleanShutdownsDailyName[];
Ken Mixter4c5daa42010-08-26 18:35:06 -0700158 static const char kMetricUncleanShutdownsWeeklyName[];
Ken Mixterccd84c02010-08-16 19:57:13 -0700159 static const char kMetricUserCrashesDailyName[];
Ken Mixter4c5daa42010-08-26 18:35:06 -0700160 static const char kMetricUserCrashesWeeklyName[];
Darin Petkov1bb904e2010-06-16 15:58:06 -0700161 static const char kMetricUserCrashIntervalName[];
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800162 static const char kMetricReadSectorsLongName[];
163 static const char kMetricReadSectorsShortName[];
164 static const char kMetricWriteSectorsLongName[];
165 static const char kMetricWriteSectorsShortName[];
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700166 static const char kMetricPageFaultsShortName[];
167 static const char kMetricPageFaultsLongName[];
Sonny Rao4b8aebb2013-07-31 23:18:31 -0700168 static const char kMetricSwapInLongName[];
169 static const char kMetricSwapInShortName[];
170 static const char kMetricSwapOutLongName[];
171 static const char kMetricSwapOutShortName[];
Luigi Semenzatofb3a8212013-05-07 16:55:00 -0700172 static const char kMetricScaledCpuFrequencyName[];
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700173 static const int kMetricStatsShortInterval;
174 static const int kMetricStatsLongInterval;
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700175 static const int kMetricMeminfoInterval;
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800176 static const int kMetricSectorsIOMax;
177 static const int kMetricSectorsBuckets;
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700178 static const int kMetricPageFaultsMax;
179 static const int kMetricPageFaultsBuckets;
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800180 static const char kMetricsDiskStatsPath[];
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700181 static const char kMetricsVmStatsPath[];
Darin Petkov2ccef012010-05-05 16:06:37 -0700182
Darin Petkov41e06232010-05-03 16:45:37 -0700183 // Array of power states.
184 static const char* kPowerStates_[kNumberPowerStates];
185
Darin Petkov41e06232010-05-03 16:45:37 -0700186 // Array of user session states.
187 static const char* kSessionStates_[kNumberSessionStates];
188
Luigi Semenzato8accd332011-05-17 16:37:18 -0700189 // Returns the active time since boot (uptime minus sleep time) in seconds.
190 double GetActiveTime();
191
Ken Mixter4c5daa42010-08-26 18:35:06 -0700192 // Clears and deletes the data contained in frequency_counters_.
193 void DeleteFrequencyCounters();
194
195 // Configures the given crash interval reporter.
196 void ConfigureCrashIntervalReporter(
197 const char* histogram_name,
198 scoped_ptr<chromeos_metrics::TaggedCounterReporter>* reporter);
199
200 // Configures the given frequency counter reporter.
201 void ConfigureCrashFrequencyReporter(const char* histogram_name);
202
Luigi Semenzato859b3f02014-02-05 15:33:19 -0800203 // Configures the given version counter reporter.
204 void ConfigureCrashVersionReporter(const char* histogram_name);
205
Ken Mixter4c5daa42010-08-26 18:35:06 -0700206 // Returns file path to persistent file for generating given histogram.
Ben Chan2e6543d2014-02-05 23:26:25 -0800207 base::FilePath GetHistogramPath(const char* histogram_name);
Ken Mixter4c5daa42010-08-26 18:35:06 -0700208
Darin Petkov65b01462010-04-14 13:32:20 -0700209 // Creates the event loop and enters it.
210 void Loop();
211
Darin Petkov703ec972010-04-27 11:02:18 -0700212 // D-Bus filter callback.
213 static DBusHandlerResult MessageFilter(DBusConnection* connection,
214 DBusMessage* message,
215 void* user_data);
Darin Petkov65b01462010-04-14 13:32:20 -0700216
Darin Petkov703ec972010-04-27 11:02:18 -0700217 // Processes power state change.
Darin Petkovf27f0362010-06-04 13:14:19 -0700218 void PowerStateChanged(const char* state_name, base::Time now);
Darin Petkov703ec972010-04-27 11:02:18 -0700219
220 // Given the state name, returns the state id.
221 PowerState LookupPowerState(const char* state_name);
Darin Petkov65b01462010-04-14 13:32:20 -0700222
Darin Petkov41e06232010-05-03 16:45:37 -0700223 // Processes user session state change.
Darin Petkovf27f0362010-06-04 13:14:19 -0700224 void SessionStateChanged(const char* state_name, base::Time now);
Darin Petkov41e06232010-05-03 16:45:37 -0700225
226 // Given the state name, returns the state id.
227 SessionState LookupSessionState(const char* state_name);
228
229 // Updates the user-active state to |active| and logs the usage data
230 // since the last update. If the user has just become active,
231 // reschedule the daily use monitor for more frequent updates --
232 // this is followed by an exponential back-off (see UseMonitor).
Darin Petkovf27f0362010-06-04 13:14:19 -0700233 // While in active use, this method should be called at intervals no
234 // longer than kUseMonitorIntervalMax otherwise new use time will be
235 // discarded.
236 void SetUserActiveState(bool active, base::Time now);
Darin Petkov41e06232010-05-03 16:45:37 -0700237
238 // Updates the daily usage file, if necessary, by adding |seconds|
239 // of active use to the |day| since Epoch. If there's usage data for
240 // day in the past in the usage file, that data is sent to UMA and
241 // removed from the file. If there's already usage data for |day| in
242 // the usage file, the |seconds| are accumulated.
243 void LogDailyUseRecord(int day, int seconds);
244
Darin Petkov1bb904e2010-06-16 15:58:06 -0700245 // Updates the active use time and logs time between user-space
246 // process crashes.
247 void ProcessUserCrash();
248
Darin Petkov38d5cb02010-06-24 12:10:26 -0700249 // Updates the active use time and logs time between kernel crashes.
250 void ProcessKernelCrash();
251
Ken Mixterccd84c02010-08-16 19:57:13 -0700252 // Updates the active use time and logs time between unclean shutdowns.
253 void ProcessUncleanShutdown();
254
255 // Checks if a kernel crash has been detected and returns true if
256 // so. The method assumes that a kernel crash has happened if
257 // |crash_file| exists. It removes the file immediately if it
258 // exists, so it must not be called more than once.
259 bool CheckSystemCrash(const std::string& crash_file);
Darin Petkov38d5cb02010-06-24 12:10:26 -0700260
Darin Petkov41e06232010-05-03 16:45:37 -0700261 // Callbacks for the daily use monitor. The daily use monitor uses
262 // LogDailyUseRecord to aggregate current usage data and send it to
263 // UMA, if necessary. It also reschedules itself using an
264 // exponentially bigger interval (up to a certain maximum) -- so
265 // usage is monitored less frequently with longer active use.
266 static gboolean UseMonitorStatic(gpointer data);
267 bool UseMonitor();
268
269 // Schedules or reschedules a daily use monitor for |interval|
270 // seconds from now. |backoff| mode is used by the use monitor to
271 // reschedule itself. If there's a monitor scheduled already and
272 // |backoff| is false, unschedules it first. Doesn't schedule a
273 // monitor for more than kUseMonitorIntervalMax seconds in the
274 // future (see metrics_daemon.cc). Returns true if a new use monitor
275 // was scheduled, false otherwise (note that if |backoff| is false a
276 // new use monitor will always be scheduled).
277 bool ScheduleUseMonitor(int interval, bool backoff);
278
279 // Unschedules a scheduled use monitor, if any.
280 void UnscheduleUseMonitor();
281
Ken Mixter4c5daa42010-08-26 18:35:06 -0700282 // Report daily use through UMA.
Luigi Semenzato859b3f02014-02-05 15:33:19 -0800283 static void ReportDailyUse(void* handle, int count);
Ken Mixter4c5daa42010-08-26 18:35:06 -0700284
Darin Petkov11b8eb32010-05-18 11:00:59 -0700285 // Sends a regular (exponential) histogram sample to Chrome for
286 // transport to UMA. See MetricsLibrary::SendToUMA in
287 // metrics_library.h for a description of the arguments.
288 void SendMetric(const std::string& name, int sample,
289 int min, int max, int nbuckets);
Darin Petkov65b01462010-04-14 13:32:20 -0700290
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700291 // Sends a linear histogram sample to Chrome for transport to UMA. See
292 // MetricsLibrary::SendToUMA in metrics_library.h for a description of the
293 // arguments.
294 void SendLinearMetric(const std::string& name, int sample,
295 int max, int nbuckets);
296
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700297 // Initializes vm and disk stats reporting.
298 void StatsReporterInit();
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800299
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700300 // Schedules a callback for the next vm and disk stats collection.
301 void ScheduleStatsCallback(int wait);
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800302
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700303 // Reads cumulative disk statistics from sysfs. Returns true for success.
304 bool DiskStatsReadStats(long int* read_sectors, long int* write_sectors);
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800305
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700306 // Reads cumulative vm statistics from procfs. Returns true for success.
Sonny Rao4b8aebb2013-07-31 23:18:31 -0700307 bool VmStatsReadStats(struct VmstatRecord* stats);
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800308
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700309 // Parse cumulative vm statistics from a C string. Returns true for success.
Sonny Rao4b8aebb2013-07-31 23:18:31 -0700310 bool VmStatsParseStats(const char* stats, struct VmstatRecord* record);
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700311
312 // Reports disk and vm statistics (static version for glib). Arguments are a
313 // glib artifact.
314 static gboolean StatsCallbackStatic(void* handle);
315
316 // Reports disk and vm statistics.
317 void StatsCallback();
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800318
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700319 // Schedules meminfo collection callback.
320 void ScheduleMeminfoCallback(int wait);
321
322 // Reports memory statistics (static version for glib). Argument is a glib
323 // artifact.
324 static gboolean MeminfoCallbackStatic(void* handle);
325
326 // Reports memory statistics. Returns false on failure.
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700327 bool MeminfoCallback();
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700328
329 // Parses content of /proc/meminfo and sends fields of interest to UMA.
Luigi Semenzato8accd332011-05-17 16:37:18 -0700330 // Returns false on errors. |meminfo_raw| contains the content of
331 // /proc/meminfo.
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700332 bool ProcessMeminfo(const std::string& meminfo_raw);
Luigi Semenzato8accd332011-05-17 16:37:18 -0700333
334 // Parses meminfo data from |meminfo_raw|. |fields| is a vector containing
335 // the fields of interest. The order of the fields must be the same in which
336 // /proc/meminfo prints them. The result of parsing fields[i] is placed in
337 // fields[i].value.
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700338 bool FillMeminfo(const std::string& meminfo_raw,
339 std::vector<MeminfoRecord>* fields);
Luigi Semenzato8accd332011-05-17 16:37:18 -0700340
Luigi Semenzato0d9a9c92013-12-05 15:55:12 -0800341 // Schedule a memory use callback in |interval| seconds.
342 void ScheduleMemuseCallback(double interval);
Luigi Semenzato8accd332011-05-17 16:37:18 -0700343
344 // Static wrapper for MemuseCallback. Always returns false.
345 static gboolean MemuseCallbackStatic(void* handle);
346
347 // Calls MemuseCallbackWork, and possibly schedules next callback, if enough
348 // active time has passed. Otherwise reschedules itself to simulate active
349 // time callbacks (i.e. wall clock time minus sleep time).
350 void MemuseCallback();
351
352 // Reads /proc/meminfo and sends total anonymous memory usage to UMA.
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700353 bool MemuseCallbackWork();
Luigi Semenzato8accd332011-05-17 16:37:18 -0700354
Luigi Semenzatofb3a8212013-05-07 16:55:00 -0700355 // Parses meminfo data and sends it to UMA.
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700356 bool ProcessMemuse(const std::string& meminfo_raw);
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700357
Luigi Semenzatofb3a8212013-05-07 16:55:00 -0700358 // Sends stats for thermal CPU throttling.
359 void SendCpuThrottleMetrics();
360
361 // Reads an integer CPU frequency value from sysfs.
362 bool ReadFreqToInt(const std::string& sysfs_file_name, int* value);
363
Luigi Semenzato859b3f02014-02-05 15:33:19 -0800364 // Reads the current OS version from /etc/lsb-release and hashes it
365 // to a unsigned 32-bit int.
366 uint32 GetOsVersionHash();
367
Darin Petkov2ccef012010-05-05 16:06:37 -0700368 // Test mode.
Darin Petkov41e06232010-05-03 16:45:37 -0700369 bool testing_;
Darin Petkov65b01462010-04-14 13:32:20 -0700370
Darin Petkovfc91b422010-05-12 13:05:45 -0700371 // The metrics library handle.
372 MetricsLibraryInterface* metrics_lib_;
373
Darin Petkovf27f0362010-06-04 13:14:19 -0700374 // Timestamps last network state update. This timestamp is used to
375 // sample the time from the network going online to going offline so
376 // TimeTicks ensures a monotonically increasing TimeDelta.
377 base::TimeTicks network_state_last_;
Darin Petkov65b01462010-04-14 13:32:20 -0700378
Darin Petkov41e06232010-05-03 16:45:37 -0700379 // Current power state.
380 PowerState power_state_;
381
Darin Petkov41e06232010-05-03 16:45:37 -0700382 // Current user session state.
383 SessionState session_state_;
384
385 // Is the user currently active: power is on, user session has
386 // started, screen is not locked.
387 bool user_active_;
388
Darin Petkov1bb904e2010-06-16 15:58:06 -0700389 // Timestamps last user active update. Active use time is aggregated
390 // each day before sending to UMA so using time since the epoch as
391 // the timestamp.
Darin Petkovf27f0362010-06-04 13:14:19 -0700392 base::Time user_active_last_;
Darin Petkov41e06232010-05-03 16:45:37 -0700393
Darin Petkov1bb904e2010-06-16 15:58:06 -0700394 // Daily active use time in seconds.
Ken Mixter4c5daa42010-08-26 18:35:06 -0700395 scoped_ptr<chromeos_metrics::TaggedCounter> daily_use_;
Darin Petkov41e06232010-05-03 16:45:37 -0700396
Darin Petkov1bb904e2010-06-16 15:58:06 -0700397 // Active use time between user-space process crashes.
Ken Mixter4c5daa42010-08-26 18:35:06 -0700398 scoped_ptr<chromeos_metrics::TaggedCounterReporter> user_crash_interval_;
Darin Petkov1bb904e2010-06-16 15:58:06 -0700399
Darin Petkov38d5cb02010-06-24 12:10:26 -0700400 // Active use time between kernel crashes.
Ken Mixter4c5daa42010-08-26 18:35:06 -0700401 scoped_ptr<chromeos_metrics::TaggedCounterReporter> kernel_crash_interval_;
Darin Petkov38d5cb02010-06-24 12:10:26 -0700402
Ken Mixterccd84c02010-08-16 19:57:13 -0700403 // Active use time between unclean shutdowns crashes.
Ken Mixter4c5daa42010-08-26 18:35:06 -0700404 scoped_ptr<chromeos_metrics::TaggedCounterReporter>
Ken Mixterccd84c02010-08-16 19:57:13 -0700405 unclean_shutdown_interval_;
406
Ken Mixter4c5daa42010-08-26 18:35:06 -0700407 // Map of all frequency counters, to simplify flushing them.
408 FrequencyCounters frequency_counters_;
Ken Mixterccd84c02010-08-16 19:57:13 -0700409
Luigi Semenzato859b3f02014-02-05 15:33:19 -0800410 // This contains a cumulative number of kernel crashes since the latest
411 // version update.
412 chromeos_metrics::VersionCounter* kernel_crash_version_counter_;
413
Darin Petkov41e06232010-05-03 16:45:37 -0700414 // Sleep period until the next daily usage aggregation performed by
415 // the daily use monitor (see ScheduleUseMonitor).
416 int usemon_interval_;
417
418 // Scheduled daily use monitor source (see ScheduleUseMonitor).
419 GSource* usemon_source_;
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800420
Luigi Semenzato0d9a9c92013-12-05 15:55:12 -0800421 // End time of current memuse stat collection interval.
422 double memuse_final_time_;
Luigi Semenzato8accd332011-05-17 16:37:18 -0700423
424 // Selects the wait time for the next memory use callback.
425 unsigned int memuse_interval_index_;
426
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700427 // Contain the most recent disk and vm cumulative stats.
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800428 long int read_sectors_;
429 long int write_sectors_;
Sonny Rao4b8aebb2013-07-31 23:18:31 -0700430 struct VmstatRecord vmstats_;
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800431
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700432 StatsState stats_state_;
433 double stats_initial_time_;
434
Luigi Semenzato0f132bb2011-02-28 11:17:43 -0800435 std::string diskstats_path_;
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700436 std::string vmstats_path_;
Luigi Semenzatofb3a8212013-05-07 16:55:00 -0700437 std::string scaling_max_freq_path_;
438 std::string cpuinfo_max_freq_path_;
Darin Petkov65b01462010-04-14 13:32:20 -0700439};
440
441#endif // METRICS_DAEMON_H_