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