blob: f12b02eeea44aaae83b18878e108fb3cbfd2beb3 [file] [log] [blame]
Bertrand SIMONNET52e5b992015-08-10 15:18:00 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Darin Petkov65b01462010-04-14 13:32:20 -070016
Luigi Semenzato4a6c9422014-06-30 18:12:28 -070017#ifndef METRICS_METRICS_DAEMON_H_
18#define METRICS_METRICS_DAEMON_H_
Darin Petkov65b01462010-04-14 13:32:20 -070019
Ben Chanf05ab402014-08-07 00:54:59 -070020#include <stdint.h>
21
Ken Mixter4c5daa42010-08-26 18:35:06 -070022#include <map>
Luigi Semenzato4a6c9422014-06-30 18:12:28 -070023#include <string>
24#include <vector>
Darin Petkov65b01462010-04-14 13:32:20 -070025
Ben Chan2e6543d2014-02-05 23:26:25 -080026#include <base/files/file_path.h>
Chris Masone817016a2011-05-12 14:14:48 -070027#include <base/memory/scoped_ptr.h>
Ben Chan2e6543d2014-02-05 23:26:25 -080028#include <base/time/time.h>
Alex Vakulenko74dc6242015-10-13 09:23:34 -070029#include <brillo/daemons/dbus_daemon.h>
Alex Vakulenko82b02de2015-10-09 20:07:47 -070030#include <libweaved/command.h>
31#include <libweaved/device.h>
Darin Petkovf1e85e42010-06-10 15:59:53 -070032#include <gtest/gtest_prod.h> // for FRIEND_TEST
33
Bertrand SIMONNET7a964052015-09-29 11:07:24 -070034#include "collectors/averaged_statistics_collector.h"
Bertrand SIMONNET0ada2ca2015-11-02 14:08:44 -080035#include "collectors/cpu_usage_collector.h"
Bertrand SIMONNET5658dc52015-09-18 13:38:10 -070036#include "collectors/disk_usage_collector.h"
Luigi Semenzato4a6c9422014-06-30 18:12:28 -070037#include "metrics/metrics_library.h"
Bertrand SIMONNET4b915ae2015-07-28 15:38:14 -070038#include "persistent_integer.h"
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -070039#include "uploader/upload_service.h"
Darin Petkovfc91b422010-05-12 13:05:45 -070040
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080041using chromeos_metrics::PersistentInteger;
Darin Petkov2ccef012010-05-05 16:06:37 -070042
Alex Vakulenko74dc6242015-10-13 09:23:34 -070043class MetricsDaemon : public brillo::DBusDaemon {
Darin Petkov65b01462010-04-14 13:32:20 -070044 public:
Darin Petkovf1e85e42010-06-10 15:59:53 -070045 MetricsDaemon();
46 ~MetricsDaemon();
Darin Petkov65b01462010-04-14 13:32:20 -070047
Steve Funge86591e2014-12-01 13:38:21 -080048 // Initializes metrics class variables.
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -070049 void Init(bool testing,
50 bool uploader_active,
Bertrand SIMONNETfec4d2c2015-08-05 16:04:14 -070051 bool dbus_enabled,
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -070052 MetricsLibraryInterface* metrics_lib,
Bertrand SIMONNETebbe35c2015-09-08 10:13:35 -070053 const std::string& diskstats_path,
Luigi Semenzatofb3a8212013-05-07 16:55:00 -070054 const std::string& cpuinfo_max_freq_path,
Steve Fung67906c62014-10-06 15:15:30 -070055 const std::string& scaling_max_freq_path,
Bertrand SIMONNETcac74e12014-10-09 10:14:13 -070056 const base::TimeDelta& upload_interval,
Steve Fung67906c62014-10-06 15:15:30 -070057 const std::string& server,
Bertrand SIMONNET2765d0a2015-09-09 10:38:20 -070058 const base::FilePath& metrics_directory);
Darin Petkov11b8eb32010-05-18 11:00:59 -070059
Steve Funge86591e2014-12-01 13:38:21 -080060 // Initializes DBus and MessageLoop variables before running the MessageLoop.
61 int OnInit() override;
62
63 // Clean up data set up in OnInit before shutting down message loop.
64 void OnShutdown(int* return_code) override;
65
66 // Does all the work.
67 int Run() override;
Darin Petkov65b01462010-04-14 13:32:20 -070068
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -070069 // Triggers an upload event and exit. (Used to test UploadService)
70 void RunUploaderTest();
71
Bertrand SIMONNET7a964052015-09-29 11:07:24 -070072 // Returns the active time since boot (uptime minus sleep time) in seconds.
73 static double GetActiveTime();
74
Luigi Semenzato96360192014-06-04 10:53:35 -070075 protected:
76 // Used also by the unit tests.
77 static const char kComprDataSizeName[];
78 static const char kOrigDataSizeName[];
79 static const char kZeroPagesName[];
80
Darin Petkov65b01462010-04-14 13:32:20 -070081 private:
Darin Petkov2ccef012010-05-05 16:06:37 -070082 friend class MetricsDaemonTest;
Ken Mixterccd84c02010-08-16 19:57:13 -070083 FRIEND_TEST(MetricsDaemonTest, CheckSystemCrash);
Ken Mixter4c5daa42010-08-26 18:35:06 -070084 FRIEND_TEST(MetricsDaemonTest, ComputeEpochNoCurrent);
85 FRIEND_TEST(MetricsDaemonTest, ComputeEpochNoLast);
86 FRIEND_TEST(MetricsDaemonTest, GetHistogramPath);
87 FRIEND_TEST(MetricsDaemonTest, IsNewEpoch);
Darin Petkove579d662010-05-05 16:19:39 -070088 FRIEND_TEST(MetricsDaemonTest, MessageFilter);
Darin Petkov38d5cb02010-06-24 12:10:26 -070089 FRIEND_TEST(MetricsDaemonTest, ProcessKernelCrash);
Luigi Semenzato29c7ef92011-04-12 14:12:35 -070090 FRIEND_TEST(MetricsDaemonTest, ProcessMeminfo);
91 FRIEND_TEST(MetricsDaemonTest, ProcessMeminfo2);
Ken Mixterccd84c02010-08-16 19:57:13 -070092 FRIEND_TEST(MetricsDaemonTest, ProcessUncleanShutdown);
Darin Petkov1bb904e2010-06-16 15:58:06 -070093 FRIEND_TEST(MetricsDaemonTest, ProcessUserCrash);
Ken Mixterccd84c02010-08-16 19:57:13 -070094 FRIEND_TEST(MetricsDaemonTest, ReportCrashesDailyFrequency);
Luigi Semenzatofb3a8212013-05-07 16:55:00 -070095 FRIEND_TEST(MetricsDaemonTest, ReadFreqToInt);
Ken Mixterccd84c02010-08-16 19:57:13 -070096 FRIEND_TEST(MetricsDaemonTest, ReportKernelCrashInterval);
97 FRIEND_TEST(MetricsDaemonTest, ReportUncleanShutdownInterval);
98 FRIEND_TEST(MetricsDaemonTest, ReportUserCrashInterval);
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080099 FRIEND_TEST(MetricsDaemonTest, SendSample);
Luigi Semenzatofb3a8212013-05-07 16:55:00 -0700100 FRIEND_TEST(MetricsDaemonTest, SendCpuThrottleMetrics);
Luigi Semenzato96360192014-06-04 10:53:35 -0700101 FRIEND_TEST(MetricsDaemonTest, SendZramMetrics);
Darin Petkov41e06232010-05-03 16:45:37 -0700102
Luigi Semenzato3ccca062013-02-04 19:50:45 -0800103 // Type of scale to use for meminfo histograms. For most of them we use
104 // percent of total RAM, but for some we use absolute numbers, usually in
105 // megabytes, on a log scale from 0 to 4000, and 0 to 8000 for compressed
106 // swap (since it can be larger than total RAM).
Luigi Semenzato942cbab2013-02-12 13:17:07 -0800107 enum MeminfoOp {
108 kMeminfoOp_HistPercent = 0,
109 kMeminfoOp_HistLog,
110 kMeminfoOp_SwapTotal,
111 kMeminfoOp_SwapFree,
Luigi Semenzato3ccca062013-02-04 19:50:45 -0800112 };
113
Luigi Semenzato8accd332011-05-17 16:37:18 -0700114 // Record for retrieving and reporting values from /proc/meminfo.
115 struct MeminfoRecord {
Luigi Semenzato3ccca062013-02-04 19:50:45 -0800116 const char* name; // print name
117 const char* match; // string to match in output of /proc/meminfo
Luigi Semenzato942cbab2013-02-12 13:17:07 -0800118 MeminfoOp op; // histogram scale selector, or other operator
Luigi Semenzato3ccca062013-02-04 19:50:45 -0800119 int value; // value from /proc/meminfo
Luigi Semenzato8accd332011-05-17 16:37:18 -0700120 };
121
Darin Petkov703ec972010-04-27 11:02:18 -0700122 // D-Bus filter callback.
123 static DBusHandlerResult MessageFilter(DBusConnection* connection,
124 DBusMessage* message,
125 void* user_data);
Darin Petkov65b01462010-04-14 13:32:20 -0700126
Bertrand SIMONNET59890e22015-10-02 16:45:18 -0700127 // Enables metrics reporting.
Alex Vakulenko82b02de2015-10-09 20:07:47 -0700128 void OnEnableMetrics(const std::weak_ptr<weaved::Command>& cmd);
Bertrand SIMONNET59890e22015-10-02 16:45:18 -0700129
130 // Disables metrics reporting.
Alex Vakulenko82b02de2015-10-09 20:07:47 -0700131 void OnDisableMetrics(const std::weak_ptr<weaved::Command>& cmd);
Bertrand SIMONNET59890e22015-10-02 16:45:18 -0700132
133 // Updates the weave device state.
Alex Vakulenko82b02de2015-10-09 20:07:47 -0700134 void UpdateWeaveState();
Bertrand SIMONNET59890e22015-10-02 16:45:18 -0700135
Darin Petkov1bb904e2010-06-16 15:58:06 -0700136 // Updates the active use time and logs time between user-space
137 // process crashes.
138 void ProcessUserCrash();
139
Darin Petkov38d5cb02010-06-24 12:10:26 -0700140 // Updates the active use time and logs time between kernel crashes.
141 void ProcessKernelCrash();
142
Ken Mixterccd84c02010-08-16 19:57:13 -0700143 // Updates the active use time and logs time between unclean shutdowns.
144 void ProcessUncleanShutdown();
145
146 // Checks if a kernel crash has been detected and returns true if
147 // so. The method assumes that a kernel crash has happened if
148 // |crash_file| exists. It removes the file immediately if it
149 // exists, so it must not be called more than once.
150 bool CheckSystemCrash(const std::string& crash_file);
Darin Petkov38d5cb02010-06-24 12:10:26 -0700151
Darin Petkov11b8eb32010-05-18 11:00:59 -0700152 // Sends a regular (exponential) histogram sample to Chrome for
153 // transport to UMA. See MetricsLibrary::SendToUMA in
154 // metrics_library.h for a description of the arguments.
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800155 void SendSample(const std::string& name, int sample,
Darin Petkov11b8eb32010-05-18 11:00:59 -0700156 int min, int max, int nbuckets);
Darin Petkov65b01462010-04-14 13:32:20 -0700157
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700158 // Sends a linear histogram sample to Chrome for transport to UMA. See
159 // MetricsLibrary::SendToUMA in metrics_library.h for a description of the
160 // arguments.
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800161 void SendLinearSample(const std::string& name, int sample,
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700162 int max, int nbuckets);
163
Luigi Semenzatoba0c65d2014-03-17 12:28:38 -0700164 // Sends various cumulative kernel crash-related stats, for instance the
165 // total number of kernel crashes since the last version update.
Luigi Semenzatoe5883fa2014-04-18 17:00:35 -0700166 void SendKernelCrashesCumulativeCountStats();
Luigi Semenzatoba0c65d2014-03-17 12:28:38 -0700167
Luigi Semenzatoe5883fa2014-04-18 17:00:35 -0700168 // Sends a sample representing the number of seconds of active use
Bertrand SIMONNET2d037832015-09-16 17:11:51 -0700169 // for a 24-hour period and reset |use|.
170 void SendAndResetDailyUseSample(
171 const scoped_ptr<PersistentInteger>& use);
Luigi Semenzatoe5883fa2014-04-18 17:00:35 -0700172
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800173 // Sends a sample representing a time interval between two crashes of the
Bertrand SIMONNET2d037832015-09-16 17:11:51 -0700174 // same type and reset |interval|.
175 void SendAndResetCrashIntervalSample(
176 const scoped_ptr<PersistentInteger>& interval);
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800177
Bertrand SIMONNET2d037832015-09-16 17:11:51 -0700178 // Sends a sample representing a frequency of crashes of some type and reset
179 // |frequency|.
180 void SendAndResetCrashFrequencySample(
181 const scoped_ptr<PersistentInteger>& frequency);
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800182
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700183 // Initializes vm and disk stats reporting.
184 void StatsReporterInit();
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800185
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700186 // Schedules meminfo collection callback.
187 void ScheduleMeminfoCallback(int wait);
188
Steve Funge86591e2014-12-01 13:38:21 -0800189 // Reports memory statistics. Reschedules callback on success.
190 void MeminfoCallback(base::TimeDelta wait);
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700191
192 // Parses content of /proc/meminfo and sends fields of interest to UMA.
Luigi Semenzato8accd332011-05-17 16:37:18 -0700193 // Returns false on errors. |meminfo_raw| contains the content of
194 // /proc/meminfo.
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700195 bool ProcessMeminfo(const std::string& meminfo_raw);
Luigi Semenzato8accd332011-05-17 16:37:18 -0700196
197 // Parses meminfo data from |meminfo_raw|. |fields| is a vector containing
198 // the fields of interest. The order of the fields must be the same in which
199 // /proc/meminfo prints them. The result of parsing fields[i] is placed in
200 // fields[i].value.
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700201 bool FillMeminfo(const std::string& meminfo_raw,
202 std::vector<MeminfoRecord>* fields);
Luigi Semenzato8accd332011-05-17 16:37:18 -0700203
Luigi Semenzato0d9a9c92013-12-05 15:55:12 -0800204 // Schedule a memory use callback in |interval| seconds.
205 void ScheduleMemuseCallback(double interval);
Luigi Semenzato8accd332011-05-17 16:37:18 -0700206
Luigi Semenzato8accd332011-05-17 16:37:18 -0700207 // Calls MemuseCallbackWork, and possibly schedules next callback, if enough
208 // active time has passed. Otherwise reschedules itself to simulate active
209 // time callbacks (i.e. wall clock time minus sleep time).
210 void MemuseCallback();
211
212 // Reads /proc/meminfo and sends total anonymous memory usage to UMA.
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700213 bool MemuseCallbackWork();
Luigi Semenzato8accd332011-05-17 16:37:18 -0700214
Luigi Semenzatofb3a8212013-05-07 16:55:00 -0700215 // Parses meminfo data and sends it to UMA.
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700216 bool ProcessMemuse(const std::string& meminfo_raw);
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700217
Luigi Semenzatofb3a8212013-05-07 16:55:00 -0700218 // Sends stats for thermal CPU throttling.
219 void SendCpuThrottleMetrics();
220
221 // Reads an integer CPU frequency value from sysfs.
222 bool ReadFreqToInt(const std::string& sysfs_file_name, int* value);
223
Luigi Semenzato859b3f02014-02-05 15:33:19 -0800224 // Reads the current OS version from /etc/lsb-release and hashes it
225 // to a unsigned 32-bit int.
Ben Chanf05ab402014-08-07 00:54:59 -0700226 uint32_t GetOsVersionHash();
Luigi Semenzato859b3f02014-02-05 15:33:19 -0800227
Daniel Eratc83975a2014-04-04 08:53:44 -0700228 // Updates stats, additionally sending them to UMA if enough time has elapsed
229 // since the last report.
230 void UpdateStats(base::TimeTicks now_ticks, base::Time now_wall_time);
231
232 // Invoked periodically by |update_stats_timeout_id_| to call UpdateStats().
Steve Funge86591e2014-12-01 13:38:21 -0800233 void HandleUpdateStatsTimeout();
Daniel Eratc83975a2014-04-04 08:53:44 -0700234
Luigi Semenzato96360192014-06-04 10:53:35 -0700235 // Reports zram statistics.
236 bool ReportZram(const base::FilePath& zram_dir);
237
Ben Chanf05ab402014-08-07 00:54:59 -0700238 // Reads a string from a file and converts it to uint64_t.
239 static bool ReadFileToUint64(const base::FilePath& path, uint64_t* value);
Luigi Semenzato96360192014-06-04 10:53:35 -0700240
241 // VARIABLES
242
Darin Petkov2ccef012010-05-05 16:06:37 -0700243 // Test mode.
Darin Petkov41e06232010-05-03 16:45:37 -0700244 bool testing_;
Darin Petkov65b01462010-04-14 13:32:20 -0700245
Steve Funge86591e2014-12-01 13:38:21 -0800246 // Whether the uploader is enabled or disabled.
247 bool uploader_active_;
248
Bertrand SIMONNETfec4d2c2015-08-05 16:04:14 -0700249 // Whether or not dbus should be used.
250 // If disabled, we will not collect the frequency of crashes.
251 bool dbus_enabled_;
252
Bertrand SIMONNET71a62ef2014-10-07 11:26:25 -0700253 // Root of the configuration files to use.
Bertrand SIMONNET2765d0a2015-09-09 10:38:20 -0700254 base::FilePath metrics_directory_;
Bertrand SIMONNET71a62ef2014-10-07 11:26:25 -0700255
Darin Petkovfc91b422010-05-12 13:05:45 -0700256 // The metrics library handle.
257 MetricsLibraryInterface* metrics_lib_;
258
Daniel Eratc83975a2014-04-04 08:53:44 -0700259 // The last time that UpdateStats() was called.
260 base::TimeTicks last_update_stats_time_;
Darin Petkov41e06232010-05-03 16:45:37 -0700261
Luigi Semenzato0d9a9c92013-12-05 15:55:12 -0800262 // End time of current memuse stat collection interval.
263 double memuse_final_time_;
Luigi Semenzato8accd332011-05-17 16:37:18 -0700264
265 // Selects the wait time for the next memory use callback.
266 unsigned int memuse_interval_index_;
267
Luigi Semenzatoba0c65d2014-03-17 12:28:38 -0700268 // Used internally by GetIncrementalCpuUse() to return the CPU utilization
269 // between calls.
Bertrand SIMONNET0ada2ca2015-11-02 14:08:44 -0800270 base::TimeDelta latest_cpu_use_microseconds_;
Luigi Semenzatoba0c65d2014-03-17 12:28:38 -0700271
272 // Persistent values and accumulators for crash statistics.
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800273 scoped_ptr<PersistentInteger> daily_cycle_;
274 scoped_ptr<PersistentInteger> weekly_cycle_;
275 scoped_ptr<PersistentInteger> version_cycle_;
276
Luigi Semenzatoe5883fa2014-04-18 17:00:35 -0700277 // Active use accumulated in a day.
278 scoped_ptr<PersistentInteger> daily_active_use_;
279 // Active use accumulated since the latest version update.
280 scoped_ptr<PersistentInteger> version_cumulative_active_use_;
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800281
Luigi Semenzatoba0c65d2014-03-17 12:28:38 -0700282 // The CPU time accumulator. This contains the CPU time, in milliseconds,
283 // used by the system since the most recent OS version update.
284 scoped_ptr<PersistentInteger> version_cumulative_cpu_use_;
285
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800286 scoped_ptr<PersistentInteger> user_crash_interval_;
287 scoped_ptr<PersistentInteger> kernel_crash_interval_;
288 scoped_ptr<PersistentInteger> unclean_shutdown_interval_;
289
290 scoped_ptr<PersistentInteger> any_crashes_daily_count_;
291 scoped_ptr<PersistentInteger> any_crashes_weekly_count_;
292 scoped_ptr<PersistentInteger> user_crashes_daily_count_;
293 scoped_ptr<PersistentInteger> user_crashes_weekly_count_;
294 scoped_ptr<PersistentInteger> kernel_crashes_daily_count_;
295 scoped_ptr<PersistentInteger> kernel_crashes_weekly_count_;
296 scoped_ptr<PersistentInteger> kernel_crashes_version_count_;
297 scoped_ptr<PersistentInteger> unclean_shutdowns_daily_count_;
298 scoped_ptr<PersistentInteger> unclean_shutdowns_weekly_count_;
Bertrand SIMONNET0ada2ca2015-11-02 14:08:44 -0800299
300 scoped_ptr<CpuUsageCollector> cpu_usage_collector_;
Bertrand SIMONNET5658dc52015-09-18 13:38:10 -0700301 scoped_ptr<DiskUsageCollector> disk_usage_collector_;
Bertrand SIMONNET7a964052015-09-29 11:07:24 -0700302 scoped_ptr<AveragedStatisticsCollector> averaged_stats_collector_;
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800303
Luigi Semenzatofb3a8212013-05-07 16:55:00 -0700304 std::string scaling_max_freq_path_;
305 std::string cpuinfo_max_freq_path_;
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -0700306
Bertrand SIMONNETcac74e12014-10-09 10:14:13 -0700307 base::TimeDelta upload_interval_;
Steve Fung67906c62014-10-06 15:15:30 -0700308 std::string server_;
Steve Fung67906c62014-10-06 15:15:30 -0700309
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -0700310 scoped_ptr<UploadService> upload_service_;
Alex Vakulenko82b02de2015-10-09 20:07:47 -0700311 std::unique_ptr<weaved::Device> device_;
Darin Petkov65b01462010-04-14 13:32:20 -0700312};
313
Luigi Semenzato4a6c9422014-06-30 18:12:28 -0700314#endif // METRICS_METRICS_DAEMON_H_