Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 1 | // 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 Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 8 | #include <dbus/dbus.h> |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 9 | #include <glib.h> |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 10 | #include <map> |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 11 | |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 12 | #include <base/file_path.h> |
Chris Masone | 817016a | 2011-05-12 14:14:48 -0700 | [diff] [blame] | 13 | #include <base/memory/scoped_ptr.h> |
Darin Petkov | f1e85e4 | 2010-06-10 15:59:53 -0700 | [diff] [blame] | 14 | #include <base/time.h> |
| 15 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
| 16 | |
Darin Petkov | fc91b42 | 2010-05-12 13:05:45 -0700 | [diff] [blame] | 17 | #include "metrics_library.h" |
| 18 | |
Ken Mixter | ccd84c0 | 2010-08-16 19:57:13 -0700 | [diff] [blame] | 19 | namespace chromeos_metrics { |
| 20 | class FrequencyCounter; |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 21 | class TaggedCounter; |
| 22 | class TaggedCounterReporter; |
Ken Mixter | ccd84c0 | 2010-08-16 19:57:13 -0700 | [diff] [blame] | 23 | } |
Darin Petkov | 2ccef01 | 2010-05-05 16:06:37 -0700 | [diff] [blame] | 24 | |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 25 | class MetricsDaemon { |
| 26 | |
| 27 | public: |
Darin Petkov | f1e85e4 | 2010-06-10 15:59:53 -0700 | [diff] [blame] | 28 | MetricsDaemon(); |
| 29 | ~MetricsDaemon(); |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 30 | |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 31 | // Initializes. |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 32 | void Init(bool testing, MetricsLibraryInterface* metrics_lib, |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 33 | const std::string& diskstats_path, |
| 34 | const std::string& vmstats_path); |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 35 | |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 36 | // Does all the work. If |run_as_daemon| is true, daemonizes by |
Darin Petkov | 2ccef01 | 2010-05-05 16:06:37 -0700 | [diff] [blame] | 37 | // forking. |
| 38 | void Run(bool run_as_daemon); |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 39 | |
| 40 | private: |
Darin Petkov | 2ccef01 | 2010-05-05 16:06:37 -0700 | [diff] [blame] | 41 | friend class MetricsDaemonTest; |
Ken Mixter | ccd84c0 | 2010-08-16 19:57:13 -0700 | [diff] [blame] | 42 | FRIEND_TEST(MetricsDaemonTest, CheckSystemCrash); |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 43 | FRIEND_TEST(MetricsDaemonTest, ComputeEpochNoCurrent); |
| 44 | FRIEND_TEST(MetricsDaemonTest, ComputeEpochNoLast); |
| 45 | FRIEND_TEST(MetricsDaemonTest, GetHistogramPath); |
| 46 | FRIEND_TEST(MetricsDaemonTest, IsNewEpoch); |
Darin Petkov | 2ccef01 | 2010-05-05 16:06:37 -0700 | [diff] [blame] | 47 | FRIEND_TEST(MetricsDaemonTest, LookupPowerState); |
| 48 | FRIEND_TEST(MetricsDaemonTest, LookupScreenSaverState); |
| 49 | FRIEND_TEST(MetricsDaemonTest, LookupSessionState); |
Darin Petkov | e579d66 | 2010-05-05 16:19:39 -0700 | [diff] [blame] | 50 | FRIEND_TEST(MetricsDaemonTest, MessageFilter); |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 51 | FRIEND_TEST(MetricsDaemonTest, ParseVmStats); |
Darin Petkov | 2ccef01 | 2010-05-05 16:06:37 -0700 | [diff] [blame] | 52 | FRIEND_TEST(MetricsDaemonTest, PowerStateChanged); |
Darin Petkov | 38d5cb0 | 2010-06-24 12:10:26 -0700 | [diff] [blame] | 53 | FRIEND_TEST(MetricsDaemonTest, ProcessKernelCrash); |
Luigi Semenzato | 29c7ef9 | 2011-04-12 14:12:35 -0700 | [diff] [blame] | 54 | FRIEND_TEST(MetricsDaemonTest, ProcessMeminfo); |
| 55 | FRIEND_TEST(MetricsDaemonTest, ProcessMeminfo2); |
Ken Mixter | ccd84c0 | 2010-08-16 19:57:13 -0700 | [diff] [blame] | 56 | FRIEND_TEST(MetricsDaemonTest, ProcessUncleanShutdown); |
Darin Petkov | 1bb904e | 2010-06-16 15:58:06 -0700 | [diff] [blame] | 57 | FRIEND_TEST(MetricsDaemonTest, ProcessUserCrash); |
Ken Mixter | ccd84c0 | 2010-08-16 19:57:13 -0700 | [diff] [blame] | 58 | FRIEND_TEST(MetricsDaemonTest, ReportCrashesDailyFrequency); |
| 59 | FRIEND_TEST(MetricsDaemonTest, ReportDailyUse); |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 60 | FRIEND_TEST(MetricsDaemonTest, ReportDiskStats); |
Ken Mixter | ccd84c0 | 2010-08-16 19:57:13 -0700 | [diff] [blame] | 61 | FRIEND_TEST(MetricsDaemonTest, ReportKernelCrashInterval); |
| 62 | FRIEND_TEST(MetricsDaemonTest, ReportUncleanShutdownInterval); |
| 63 | FRIEND_TEST(MetricsDaemonTest, ReportUserCrashInterval); |
Darin Petkov | 2ccef01 | 2010-05-05 16:06:37 -0700 | [diff] [blame] | 64 | FRIEND_TEST(MetricsDaemonTest, ScreenSaverStateChanged); |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 65 | FRIEND_TEST(MetricsDaemonTest, SendMetric); |
Darin Petkov | 2ccef01 | 2010-05-05 16:06:37 -0700 | [diff] [blame] | 66 | FRIEND_TEST(MetricsDaemonTest, SessionStateChanged); |
Darin Petkov | f1e85e4 | 2010-06-10 15:59:53 -0700 | [diff] [blame] | 67 | FRIEND_TEST(MetricsDaemonTest, SetUserActiveState); |
Darin Petkov | f27f036 | 2010-06-04 13:14:19 -0700 | [diff] [blame] | 68 | FRIEND_TEST(MetricsDaemonTest, SetUserActiveStateTimeJump); |
Darin Petkov | 2ccef01 | 2010-05-05 16:06:37 -0700 | [diff] [blame] | 69 | |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 70 | // The power states (see power_states.h). |
| 71 | enum PowerState { |
| 72 | kUnknownPowerState = -1, // Initial/unknown power state. |
| 73 | #define STATE(name, capname) kPowerState ## capname, |
| 74 | #include "power_states.h" |
| 75 | kNumberPowerStates |
| 76 | }; |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 77 | |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 78 | // The user session states (see session_states.h). |
| 79 | enum SessionState { |
| 80 | kUnknownSessionState = -1, // Initial/unknown user session state. |
| 81 | #define STATE(name, capname) kSessionState ## capname, |
| 82 | #include "session_states.h" |
| 83 | kNumberSessionStates |
| 84 | }; |
| 85 | |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 86 | // State for disk stats collector callback. |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 87 | enum StatsState { |
| 88 | kStatsShort, // short wait before short interval collection |
| 89 | kStatsLong, // final wait before new collection |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 90 | }; |
| 91 | |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 92 | // Data record for aggregating daily usage. |
| 93 | class UseRecord { |
| 94 | public: |
| 95 | UseRecord() : day_(0), seconds_(0) {} |
| 96 | int day_; |
| 97 | int seconds_; |
| 98 | }; |
| 99 | |
Luigi Semenzato | 3ccca06 | 2013-02-04 19:50:45 -0800 | [diff] [blame^] | 100 | // Type of scale to use for meminfo histograms. For most of them we use |
| 101 | // percent of total RAM, but for some we use absolute numbers, usually in |
| 102 | // megabytes, on a log scale from 0 to 4000, and 0 to 8000 for compressed |
| 103 | // swap (since it can be larger than total RAM). |
| 104 | enum MeminfoScaleType { |
| 105 | kMeminfoScalePercent = 0, |
| 106 | kMeminfoScaleLog, |
| 107 | kMeminfoScaleLogLarge, |
| 108 | }; |
| 109 | |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 110 | // Record for retrieving and reporting values from /proc/meminfo. |
| 111 | struct MeminfoRecord { |
Luigi Semenzato | 3ccca06 | 2013-02-04 19:50:45 -0800 | [diff] [blame^] | 112 | const char* name; // print name |
| 113 | const char* match; // string to match in output of /proc/meminfo |
| 114 | MeminfoScaleType scale; // histogram scale selector |
| 115 | int value; // value from /proc/meminfo |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 116 | }; |
| 117 | |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 118 | typedef std::map<std::string, chromeos_metrics::FrequencyCounter*> |
| 119 | FrequencyCounters; |
| 120 | |
Darin Petkov | 2ccef01 | 2010-05-05 16:06:37 -0700 | [diff] [blame] | 121 | // Metric parameters. |
Ken Mixter | ccd84c0 | 2010-08-16 19:57:13 -0700 | [diff] [blame] | 122 | static const char kMetricAnyCrashesDailyName[]; |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 123 | static const char kMetricAnyCrashesWeeklyName[]; |
| 124 | static const char kMetricCrashFrequencyBuckets; |
| 125 | static const char kMetricCrashFrequencyMax; |
| 126 | static const char kMetricCrashFrequencyMin; |
Ken Mixter | ccd84c0 | 2010-08-16 19:57:13 -0700 | [diff] [blame] | 127 | static const int kMetricCrashIntervalBuckets; |
| 128 | static const int kMetricCrashIntervalMax; |
| 129 | static const int kMetricCrashIntervalMin; |
| 130 | static const int kMetricDailyUseTimeBuckets; |
| 131 | static const int kMetricDailyUseTimeMax; |
| 132 | static const int kMetricDailyUseTimeMin; |
Darin Petkov | 2ccef01 | 2010-05-05 16:06:37 -0700 | [diff] [blame] | 133 | static const char kMetricDailyUseTimeName[]; |
Ken Mixter | ccd84c0 | 2010-08-16 19:57:13 -0700 | [diff] [blame] | 134 | static const char kMetricKernelCrashesDailyName[]; |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 135 | static const char kMetricKernelCrashesWeeklyName[]; |
Darin Petkov | 38d5cb0 | 2010-06-24 12:10:26 -0700 | [diff] [blame] | 136 | static const char kMetricKernelCrashIntervalName[]; |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 137 | static const char kMetricsPath[]; |
Ken Mixter | ccd84c0 | 2010-08-16 19:57:13 -0700 | [diff] [blame] | 138 | static const char kMetricUncleanShutdownIntervalName[]; |
| 139 | static const char kMetricUncleanShutdownsDailyName[]; |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 140 | static const char kMetricUncleanShutdownsWeeklyName[]; |
Ken Mixter | ccd84c0 | 2010-08-16 19:57:13 -0700 | [diff] [blame] | 141 | static const char kMetricUserCrashesDailyName[]; |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 142 | static const char kMetricUserCrashesWeeklyName[]; |
Darin Petkov | 1bb904e | 2010-06-16 15:58:06 -0700 | [diff] [blame] | 143 | static const char kMetricUserCrashIntervalName[]; |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 144 | static const char kMetricReadSectorsLongName[]; |
| 145 | static const char kMetricReadSectorsShortName[]; |
| 146 | static const char kMetricWriteSectorsLongName[]; |
| 147 | static const char kMetricWriteSectorsShortName[]; |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 148 | static const char kMetricPageFaultsShortName[]; |
| 149 | static const char kMetricPageFaultsLongName[]; |
| 150 | static const int kMetricStatsShortInterval; |
| 151 | static const int kMetricStatsLongInterval; |
Luigi Semenzato | 29c7ef9 | 2011-04-12 14:12:35 -0700 | [diff] [blame] | 152 | static const int kMetricMeminfoInterval; |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 153 | static const int kMetricSectorsIOMax; |
| 154 | static const int kMetricSectorsBuckets; |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 155 | static const int kMetricPageFaultsMax; |
| 156 | static const int kMetricPageFaultsBuckets; |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 157 | static const char kMetricsDiskStatsPath[]; |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 158 | static const char kMetricsVmStatsPath[]; |
Darin Petkov | 2ccef01 | 2010-05-05 16:06:37 -0700 | [diff] [blame] | 159 | |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 160 | // D-Bus message match strings. |
| 161 | static const char* kDBusMatches_[]; |
| 162 | |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 163 | // Array of power states. |
| 164 | static const char* kPowerStates_[kNumberPowerStates]; |
| 165 | |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 166 | // Array of user session states. |
| 167 | static const char* kSessionStates_[kNumberSessionStates]; |
| 168 | |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 169 | // Returns the active time since boot (uptime minus sleep time) in seconds. |
| 170 | double GetActiveTime(); |
| 171 | |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 172 | // Clears and deletes the data contained in frequency_counters_. |
| 173 | void DeleteFrequencyCounters(); |
| 174 | |
| 175 | // Configures the given crash interval reporter. |
| 176 | void ConfigureCrashIntervalReporter( |
| 177 | const char* histogram_name, |
| 178 | scoped_ptr<chromeos_metrics::TaggedCounterReporter>* reporter); |
| 179 | |
| 180 | // Configures the given frequency counter reporter. |
| 181 | void ConfigureCrashFrequencyReporter(const char* histogram_name); |
| 182 | |
| 183 | // Returns file path to persistent file for generating given histogram. |
| 184 | FilePath GetHistogramPath(const char* histogram_name); |
| 185 | |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 186 | // Creates the event loop and enters it. |
| 187 | void Loop(); |
| 188 | |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 189 | // D-Bus filter callback. |
| 190 | static DBusHandlerResult MessageFilter(DBusConnection* connection, |
| 191 | DBusMessage* message, |
| 192 | void* user_data); |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 193 | |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 194 | // Processes power state change. |
Darin Petkov | f27f036 | 2010-06-04 13:14:19 -0700 | [diff] [blame] | 195 | void PowerStateChanged(const char* state_name, base::Time now); |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 196 | |
| 197 | // Given the state name, returns the state id. |
| 198 | PowerState LookupPowerState(const char* state_name); |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 199 | |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 200 | // Processes user session state change. |
Darin Petkov | f27f036 | 2010-06-04 13:14:19 -0700 | [diff] [blame] | 201 | void SessionStateChanged(const char* state_name, base::Time now); |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 202 | |
| 203 | // Given the state name, returns the state id. |
| 204 | SessionState LookupSessionState(const char* state_name); |
| 205 | |
| 206 | // Updates the user-active state to |active| and logs the usage data |
| 207 | // since the last update. If the user has just become active, |
| 208 | // reschedule the daily use monitor for more frequent updates -- |
| 209 | // this is followed by an exponential back-off (see UseMonitor). |
Darin Petkov | f27f036 | 2010-06-04 13:14:19 -0700 | [diff] [blame] | 210 | // While in active use, this method should be called at intervals no |
| 211 | // longer than kUseMonitorIntervalMax otherwise new use time will be |
| 212 | // discarded. |
| 213 | void SetUserActiveState(bool active, base::Time now); |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 214 | |
| 215 | // Updates the daily usage file, if necessary, by adding |seconds| |
| 216 | // of active use to the |day| since Epoch. If there's usage data for |
| 217 | // day in the past in the usage file, that data is sent to UMA and |
| 218 | // removed from the file. If there's already usage data for |day| in |
| 219 | // the usage file, the |seconds| are accumulated. |
| 220 | void LogDailyUseRecord(int day, int seconds); |
| 221 | |
Darin Petkov | 1bb904e | 2010-06-16 15:58:06 -0700 | [diff] [blame] | 222 | // Updates the active use time and logs time between user-space |
| 223 | // process crashes. |
| 224 | void ProcessUserCrash(); |
| 225 | |
Darin Petkov | 38d5cb0 | 2010-06-24 12:10:26 -0700 | [diff] [blame] | 226 | // Updates the active use time and logs time between kernel crashes. |
| 227 | void ProcessKernelCrash(); |
| 228 | |
Ken Mixter | ccd84c0 | 2010-08-16 19:57:13 -0700 | [diff] [blame] | 229 | // Updates the active use time and logs time between unclean shutdowns. |
| 230 | void ProcessUncleanShutdown(); |
| 231 | |
| 232 | // Checks if a kernel crash has been detected and returns true if |
| 233 | // so. The method assumes that a kernel crash has happened if |
| 234 | // |crash_file| exists. It removes the file immediately if it |
| 235 | // exists, so it must not be called more than once. |
| 236 | bool CheckSystemCrash(const std::string& crash_file); |
Darin Petkov | 38d5cb0 | 2010-06-24 12:10:26 -0700 | [diff] [blame] | 237 | |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 238 | // Callbacks for the daily use monitor. The daily use monitor uses |
| 239 | // LogDailyUseRecord to aggregate current usage data and send it to |
| 240 | // UMA, if necessary. It also reschedules itself using an |
| 241 | // exponentially bigger interval (up to a certain maximum) -- so |
| 242 | // usage is monitored less frequently with longer active use. |
| 243 | static gboolean UseMonitorStatic(gpointer data); |
| 244 | bool UseMonitor(); |
| 245 | |
| 246 | // Schedules or reschedules a daily use monitor for |interval| |
| 247 | // seconds from now. |backoff| mode is used by the use monitor to |
| 248 | // reschedule itself. If there's a monitor scheduled already and |
| 249 | // |backoff| is false, unschedules it first. Doesn't schedule a |
| 250 | // monitor for more than kUseMonitorIntervalMax seconds in the |
| 251 | // future (see metrics_daemon.cc). Returns true if a new use monitor |
| 252 | // was scheduled, false otherwise (note that if |backoff| is false a |
| 253 | // new use monitor will always be scheduled). |
| 254 | bool ScheduleUseMonitor(int interval, bool backoff); |
| 255 | |
| 256 | // Unschedules a scheduled use monitor, if any. |
| 257 | void UnscheduleUseMonitor(); |
| 258 | |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 259 | // Report daily use through UMA. |
| 260 | static void ReportDailyUse(void* handle, int tag, int count); |
| 261 | |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 262 | // Sends a regular (exponential) histogram sample to Chrome for |
| 263 | // transport to UMA. See MetricsLibrary::SendToUMA in |
| 264 | // metrics_library.h for a description of the arguments. |
| 265 | void SendMetric(const std::string& name, int sample, |
| 266 | int min, int max, int nbuckets); |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 267 | |
Luigi Semenzato | 29c7ef9 | 2011-04-12 14:12:35 -0700 | [diff] [blame] | 268 | // Sends a linear histogram sample to Chrome for transport to UMA. See |
| 269 | // MetricsLibrary::SendToUMA in metrics_library.h for a description of the |
| 270 | // arguments. |
| 271 | void SendLinearMetric(const std::string& name, int sample, |
| 272 | int max, int nbuckets); |
| 273 | |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 274 | // Initializes vm and disk stats reporting. |
| 275 | void StatsReporterInit(); |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 276 | |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 277 | // Schedules a callback for the next vm and disk stats collection. |
| 278 | void ScheduleStatsCallback(int wait); |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 279 | |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 280 | // Reads cumulative disk statistics from sysfs. Returns true for success. |
| 281 | bool DiskStatsReadStats(long int* read_sectors, long int* write_sectors); |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 282 | |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 283 | // Reads cumulative vm statistics from procfs. Returns true for success. |
| 284 | bool VmStatsReadStats(long int* page_faults); |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 285 | |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 286 | // Parse cumulative vm statistics from a C string. Returns true for success. |
| 287 | bool VmStatsParseStats(char* stats, long int* page_faults); |
| 288 | |
| 289 | // Reports disk and vm statistics (static version for glib). Arguments are a |
| 290 | // glib artifact. |
| 291 | static gboolean StatsCallbackStatic(void* handle); |
| 292 | |
| 293 | // Reports disk and vm statistics. |
| 294 | void StatsCallback(); |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 295 | |
Luigi Semenzato | 29c7ef9 | 2011-04-12 14:12:35 -0700 | [diff] [blame] | 296 | // Schedules meminfo collection callback. |
| 297 | void ScheduleMeminfoCallback(int wait); |
| 298 | |
| 299 | // Reports memory statistics (static version for glib). Argument is a glib |
| 300 | // artifact. |
| 301 | static gboolean MeminfoCallbackStatic(void* handle); |
| 302 | |
| 303 | // Reports memory statistics. Returns false on failure. |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 304 | bool MeminfoCallback(); |
Luigi Semenzato | 29c7ef9 | 2011-04-12 14:12:35 -0700 | [diff] [blame] | 305 | |
| 306 | // Parses content of /proc/meminfo and sends fields of interest to UMA. |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 307 | // Returns false on errors. |meminfo_raw| contains the content of |
| 308 | // /proc/meminfo. |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 309 | bool ProcessMeminfo(const std::string& meminfo_raw); |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 310 | |
| 311 | // Parses meminfo data from |meminfo_raw|. |fields| is a vector containing |
| 312 | // the fields of interest. The order of the fields must be the same in which |
| 313 | // /proc/meminfo prints them. The result of parsing fields[i] is placed in |
| 314 | // fields[i].value. |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 315 | bool FillMeminfo(const std::string& meminfo_raw, |
| 316 | std::vector<MeminfoRecord>* fields); |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 317 | |
| 318 | // Schedule a memory use callback. |new_callback| is true when this callback |
| 319 | // is scheduled for the first time. When |new_callback| is false, |
| 320 | // |time_elapsed| is the active (non-sleep) time that has passed between now |
| 321 | // and the original callback scheduling time. We use it to reschedule a |
| 322 | // callback that fired too early because we slept. |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 323 | void ScheduleMemuseCallback(bool new_callback, double time_elapsed); |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 324 | |
| 325 | // Static wrapper for MemuseCallback. Always returns false. |
| 326 | static gboolean MemuseCallbackStatic(void* handle); |
| 327 | |
| 328 | // Calls MemuseCallbackWork, and possibly schedules next callback, if enough |
| 329 | // active time has passed. Otherwise reschedules itself to simulate active |
| 330 | // time callbacks (i.e. wall clock time minus sleep time). |
| 331 | void MemuseCallback(); |
| 332 | |
| 333 | // Reads /proc/meminfo and sends total anonymous memory usage to UMA. |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 334 | bool MemuseCallbackWork(); |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 335 | |
| 336 | // Parse meminfo data and send to UMA. |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 337 | bool ProcessMemuse(const std::string& meminfo_raw); |
Luigi Semenzato | 29c7ef9 | 2011-04-12 14:12:35 -0700 | [diff] [blame] | 338 | |
Darin Petkov | 2ccef01 | 2010-05-05 16:06:37 -0700 | [diff] [blame] | 339 | // Test mode. |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 340 | bool testing_; |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 341 | |
Darin Petkov | fc91b42 | 2010-05-12 13:05:45 -0700 | [diff] [blame] | 342 | // The metrics library handle. |
| 343 | MetricsLibraryInterface* metrics_lib_; |
| 344 | |
Darin Petkov | f27f036 | 2010-06-04 13:14:19 -0700 | [diff] [blame] | 345 | // Timestamps last network state update. This timestamp is used to |
| 346 | // sample the time from the network going online to going offline so |
| 347 | // TimeTicks ensures a monotonically increasing TimeDelta. |
| 348 | base::TimeTicks network_state_last_; |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 349 | |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 350 | // Current power state. |
| 351 | PowerState power_state_; |
| 352 | |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 353 | // Current user session state. |
| 354 | SessionState session_state_; |
| 355 | |
| 356 | // Is the user currently active: power is on, user session has |
| 357 | // started, screen is not locked. |
| 358 | bool user_active_; |
| 359 | |
Darin Petkov | 1bb904e | 2010-06-16 15:58:06 -0700 | [diff] [blame] | 360 | // Timestamps last user active update. Active use time is aggregated |
| 361 | // each day before sending to UMA so using time since the epoch as |
| 362 | // the timestamp. |
Darin Petkov | f27f036 | 2010-06-04 13:14:19 -0700 | [diff] [blame] | 363 | base::Time user_active_last_; |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 364 | |
Darin Petkov | 1bb904e | 2010-06-16 15:58:06 -0700 | [diff] [blame] | 365 | // Daily active use time in seconds. |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 366 | scoped_ptr<chromeos_metrics::TaggedCounter> daily_use_; |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 367 | |
Darin Petkov | 1bb904e | 2010-06-16 15:58:06 -0700 | [diff] [blame] | 368 | // Active use time between user-space process crashes. |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 369 | scoped_ptr<chromeos_metrics::TaggedCounterReporter> user_crash_interval_; |
Darin Petkov | 1bb904e | 2010-06-16 15:58:06 -0700 | [diff] [blame] | 370 | |
Darin Petkov | 38d5cb0 | 2010-06-24 12:10:26 -0700 | [diff] [blame] | 371 | // Active use time between kernel crashes. |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 372 | scoped_ptr<chromeos_metrics::TaggedCounterReporter> kernel_crash_interval_; |
Darin Petkov | 38d5cb0 | 2010-06-24 12:10:26 -0700 | [diff] [blame] | 373 | |
Ken Mixter | ccd84c0 | 2010-08-16 19:57:13 -0700 | [diff] [blame] | 374 | // Active use time between unclean shutdowns crashes. |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 375 | scoped_ptr<chromeos_metrics::TaggedCounterReporter> |
Ken Mixter | ccd84c0 | 2010-08-16 19:57:13 -0700 | [diff] [blame] | 376 | unclean_shutdown_interval_; |
| 377 | |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 378 | // Map of all frequency counters, to simplify flushing them. |
| 379 | FrequencyCounters frequency_counters_; |
Ken Mixter | ccd84c0 | 2010-08-16 19:57:13 -0700 | [diff] [blame] | 380 | |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 381 | // Sleep period until the next daily usage aggregation performed by |
| 382 | // the daily use monitor (see ScheduleUseMonitor). |
| 383 | int usemon_interval_; |
| 384 | |
| 385 | // Scheduled daily use monitor source (see ScheduleUseMonitor). |
| 386 | GSource* usemon_source_; |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 387 | |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 388 | // Time of initial scheduling of memuse callback |
| 389 | double memuse_initial_time_; |
| 390 | |
| 391 | // Selects the wait time for the next memory use callback. |
| 392 | unsigned int memuse_interval_index_; |
| 393 | |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 394 | // Contain the most recent disk and vm cumulative stats. |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 395 | long int read_sectors_; |
| 396 | long int write_sectors_; |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 397 | long int page_faults_; |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 398 | |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 399 | StatsState stats_state_; |
| 400 | double stats_initial_time_; |
| 401 | |
Luigi Semenzato | 0f132bb | 2011-02-28 11:17:43 -0800 | [diff] [blame] | 402 | std::string diskstats_path_; |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 403 | std::string vmstats_path_; |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 404 | }; |
| 405 | |
| 406 | #endif // METRICS_DAEMON_H_ |