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