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