blob: 30659bd1986792f56199dae8dd7f65fe2d1d3085 [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
Bertrand SIMONNET608e4282015-11-12 17:52:17 -080017#ifndef METRICS_METRICS_COLLECTOR_H_
18#define METRICS_METRICS_COLLECTOR_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>
Bertrand SIMONNET6c9fbb92015-12-21 14:56:40 -080023#include <memory>
Luigi Semenzato4a6c9422014-06-30 18:12:28 -070024#include <string>
25#include <vector>
Darin Petkov65b01462010-04-14 13:32:20 -070026
Ben Chan2e6543d2014-02-05 23:26:25 -080027#include <base/files/file_path.h>
Alex Vakulenko53ca76f2015-12-29 15:02:00 -080028#include <base/memory/weak_ptr.h>
Ben Chan2e6543d2014-02-05 23:26:25 -080029#include <base/time/time.h>
Alex Vakulenko53ca76f2015-12-29 15:02:00 -080030#include <brillo/binder_watcher.h>
Alex Vakulenkoeca14e32016-01-28 14:39:27 -080031#include <brillo/daemons/daemon.h>
Alex Vakulenko82b02de2015-10-09 20:07:47 -070032#include <libweaved/command.h>
Alex Vakulenko53ca76f2015-12-29 15:02:00 -080033#include <libweaved/service.h>
Darin Petkovf1e85e42010-06-10 15:59:53 -070034#include <gtest/gtest_prod.h> // for FRIEND_TEST
35
Bertrand SIMONNET7a964052015-09-29 11:07:24 -070036#include "collectors/averaged_statistics_collector.h"
Bertrand SIMONNET0ada2ca2015-11-02 14:08:44 -080037#include "collectors/cpu_usage_collector.h"
Bertrand SIMONNET5658dc52015-09-18 13:38:10 -070038#include "collectors/disk_usage_collector.h"
Luigi Semenzato4a6c9422014-06-30 18:12:28 -070039#include "metrics/metrics_library.h"
Bertrand SIMONNET4b915ae2015-07-28 15:38:14 -070040#include "persistent_integer.h"
Darin Petkovfc91b422010-05-12 13:05:45 -070041
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080042using chromeos_metrics::PersistentInteger;
Bertrand SIMONNET6c9fbb92015-12-21 14:56:40 -080043using std::unique_ptr;
Darin Petkov2ccef012010-05-05 16:06:37 -070044
Alex Vakulenkoeca14e32016-01-28 14:39:27 -080045class MetricsCollector : public brillo::Daemon {
Darin Petkov65b01462010-04-14 13:32:20 -070046 public:
Bertrand SIMONNET608e4282015-11-12 17:52:17 -080047 MetricsCollector();
48 ~MetricsCollector();
Darin Petkov65b01462010-04-14 13:32:20 -070049
Steve Funge86591e2014-12-01 13:38:21 -080050 // Initializes metrics class variables.
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -070051 void Init(bool testing,
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -070052 MetricsLibraryInterface* metrics_lib,
Bertrand SIMONNETebbe35c2015-09-08 10:13:35 -070053 const std::string& diskstats_path,
Bertrand SIMONNET9d3a4ae2015-11-25 13:49:12 -080054 const base::FilePath& private_metrics_directory,
55 const base::FilePath& shared_metrics_directory);
Darin Petkov11b8eb32010-05-18 11:00:59 -070056
Alex Vakulenkoeca14e32016-01-28 14:39:27 -080057 // Initializes the daemon.
Steve Funge86591e2014-12-01 13:38:21 -080058 int OnInit() override;
59
Steve Funge86591e2014-12-01 13:38:21 -080060 // Does all the work.
61 int Run() override;
Darin Petkov65b01462010-04-14 13:32:20 -070062
Bertrand SIMONNET7a964052015-09-29 11:07:24 -070063 // Returns the active time since boot (uptime minus sleep time) in seconds.
64 static double GetActiveTime();
65
Todd Poynor694553d2015-12-03 11:05:45 -080066 // Updates the active use time and logs time between user-space
67 // process crashes. Called via MetricsCollectorServiceTrampoline.
68 void ProcessUserCrash();
69
Luigi Semenzato96360192014-06-04 10:53:35 -070070 protected:
71 // Used also by the unit tests.
72 static const char kComprDataSizeName[];
73 static const char kOrigDataSizeName[];
74 static const char kZeroPagesName[];
75
Darin Petkov65b01462010-04-14 13:32:20 -070076 private:
Bertrand SIMONNET608e4282015-11-12 17:52:17 -080077 friend class MetricsCollectorTest;
78 FRIEND_TEST(MetricsCollectorTest, CheckSystemCrash);
79 FRIEND_TEST(MetricsCollectorTest, ComputeEpochNoCurrent);
80 FRIEND_TEST(MetricsCollectorTest, ComputeEpochNoLast);
81 FRIEND_TEST(MetricsCollectorTest, GetHistogramPath);
82 FRIEND_TEST(MetricsCollectorTest, IsNewEpoch);
83 FRIEND_TEST(MetricsCollectorTest, MessageFilter);
84 FRIEND_TEST(MetricsCollectorTest, ProcessKernelCrash);
85 FRIEND_TEST(MetricsCollectorTest, ProcessMeminfo);
86 FRIEND_TEST(MetricsCollectorTest, ProcessMeminfo2);
87 FRIEND_TEST(MetricsCollectorTest, ProcessUncleanShutdown);
88 FRIEND_TEST(MetricsCollectorTest, ProcessUserCrash);
89 FRIEND_TEST(MetricsCollectorTest, ReportCrashesDailyFrequency);
90 FRIEND_TEST(MetricsCollectorTest, ReportKernelCrashInterval);
91 FRIEND_TEST(MetricsCollectorTest, ReportUncleanShutdownInterval);
92 FRIEND_TEST(MetricsCollectorTest, ReportUserCrashInterval);
93 FRIEND_TEST(MetricsCollectorTest, SendSample);
94 FRIEND_TEST(MetricsCollectorTest, SendZramMetrics);
Darin Petkov41e06232010-05-03 16:45:37 -070095
Luigi Semenzato3ccca062013-02-04 19:50:45 -080096 // 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 Semenzato942cbab2013-02-12 13:17:07 -0800100 enum MeminfoOp {
101 kMeminfoOp_HistPercent = 0,
102 kMeminfoOp_HistLog,
103 kMeminfoOp_SwapTotal,
104 kMeminfoOp_SwapFree,
Luigi Semenzato3ccca062013-02-04 19:50:45 -0800105 };
106
Luigi Semenzato8accd332011-05-17 16:37:18 -0700107 // Record for retrieving and reporting values from /proc/meminfo.
108 struct MeminfoRecord {
Luigi Semenzato3ccca062013-02-04 19:50:45 -0800109 const char* name; // print name
110 const char* match; // string to match in output of /proc/meminfo
Luigi Semenzato942cbab2013-02-12 13:17:07 -0800111 MeminfoOp op; // histogram scale selector, or other operator
Luigi Semenzato3ccca062013-02-04 19:50:45 -0800112 int value; // value from /proc/meminfo
Luigi Semenzato8accd332011-05-17 16:37:18 -0700113 };
114
Bertrand SIMONNET59890e22015-10-02 16:45:18 -0700115 // Enables metrics reporting.
Alex Vakulenko53ca76f2015-12-29 15:02:00 -0800116 void OnEnableMetrics(std::unique_ptr<weaved::Command> command);
Bertrand SIMONNET59890e22015-10-02 16:45:18 -0700117
118 // Disables metrics reporting.
Alex Vakulenko53ca76f2015-12-29 15:02:00 -0800119 void OnDisableMetrics(std::unique_ptr<weaved::Command> command);
Bertrand SIMONNET59890e22015-10-02 16:45:18 -0700120
121 // Updates the weave device state.
Alex Vakulenko82b02de2015-10-09 20:07:47 -0700122 void UpdateWeaveState();
Bertrand SIMONNET59890e22015-10-02 16:45:18 -0700123
Darin Petkov38d5cb02010-06-24 12:10:26 -0700124 // Updates the active use time and logs time between kernel crashes.
125 void ProcessKernelCrash();
126
Ken Mixterccd84c02010-08-16 19:57:13 -0700127 // Updates the active use time and logs time between unclean shutdowns.
128 void ProcessUncleanShutdown();
129
130 // Checks if a kernel crash has been detected and returns true if
131 // so. The method assumes that a kernel crash has happened if
132 // |crash_file| exists. It removes the file immediately if it
133 // exists, so it must not be called more than once.
134 bool CheckSystemCrash(const std::string& crash_file);
Darin Petkov38d5cb02010-06-24 12:10:26 -0700135
Darin Petkov11b8eb32010-05-18 11:00:59 -0700136 // Sends a regular (exponential) histogram sample to Chrome for
137 // transport to UMA. See MetricsLibrary::SendToUMA in
138 // metrics_library.h for a description of the arguments.
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800139 void SendSample(const std::string& name, int sample,
Darin Petkov11b8eb32010-05-18 11:00:59 -0700140 int min, int max, int nbuckets);
Darin Petkov65b01462010-04-14 13:32:20 -0700141
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700142 // Sends a linear histogram sample to Chrome for transport to UMA. See
143 // MetricsLibrary::SendToUMA in metrics_library.h for a description of the
144 // arguments.
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800145 void SendLinearSample(const std::string& name, int sample,
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700146 int max, int nbuckets);
147
Luigi Semenzatoba0c65d2014-03-17 12:28:38 -0700148 // Sends various cumulative kernel crash-related stats, for instance the
149 // total number of kernel crashes since the last version update.
Luigi Semenzatoe5883fa2014-04-18 17:00:35 -0700150 void SendKernelCrashesCumulativeCountStats();
Luigi Semenzatoba0c65d2014-03-17 12:28:38 -0700151
Luigi Semenzatoe5883fa2014-04-18 17:00:35 -0700152 // Sends a sample representing the number of seconds of active use
Bertrand SIMONNET2d037832015-09-16 17:11:51 -0700153 // for a 24-hour period and reset |use|.
Bertrand SIMONNET6c9fbb92015-12-21 14:56:40 -0800154 void SendAndResetDailyUseSample(const unique_ptr<PersistentInteger>& use);
Luigi Semenzatoe5883fa2014-04-18 17:00:35 -0700155
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800156 // Sends a sample representing a time interval between two crashes of the
Bertrand SIMONNET2d037832015-09-16 17:11:51 -0700157 // same type and reset |interval|.
158 void SendAndResetCrashIntervalSample(
Bertrand SIMONNET6c9fbb92015-12-21 14:56:40 -0800159 const unique_ptr<PersistentInteger>& interval);
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800160
Bertrand SIMONNET2d037832015-09-16 17:11:51 -0700161 // Sends a sample representing a frequency of crashes of some type and reset
162 // |frequency|.
163 void SendAndResetCrashFrequencySample(
Bertrand SIMONNET6c9fbb92015-12-21 14:56:40 -0800164 const unique_ptr<PersistentInteger>& frequency);
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800165
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700166 // Initializes vm and disk stats reporting.
167 void StatsReporterInit();
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800168
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700169 // Schedules meminfo collection callback.
170 void ScheduleMeminfoCallback(int wait);
171
Steve Funge86591e2014-12-01 13:38:21 -0800172 // Reports memory statistics. Reschedules callback on success.
173 void MeminfoCallback(base::TimeDelta wait);
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700174
175 // Parses content of /proc/meminfo and sends fields of interest to UMA.
Luigi Semenzato8accd332011-05-17 16:37:18 -0700176 // Returns false on errors. |meminfo_raw| contains the content of
177 // /proc/meminfo.
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700178 bool ProcessMeminfo(const std::string& meminfo_raw);
Luigi Semenzato8accd332011-05-17 16:37:18 -0700179
180 // Parses meminfo data from |meminfo_raw|. |fields| is a vector containing
181 // the fields of interest. The order of the fields must be the same in which
182 // /proc/meminfo prints them. The result of parsing fields[i] is placed in
183 // fields[i].value.
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700184 bool FillMeminfo(const std::string& meminfo_raw,
185 std::vector<MeminfoRecord>* fields);
Luigi Semenzato8accd332011-05-17 16:37:18 -0700186
Luigi Semenzato0d9a9c92013-12-05 15:55:12 -0800187 // Schedule a memory use callback in |interval| seconds.
188 void ScheduleMemuseCallback(double interval);
Luigi Semenzato8accd332011-05-17 16:37:18 -0700189
Luigi Semenzato8accd332011-05-17 16:37:18 -0700190 // Calls MemuseCallbackWork, and possibly schedules next callback, if enough
191 // active time has passed. Otherwise reschedules itself to simulate active
192 // time callbacks (i.e. wall clock time minus sleep time).
193 void MemuseCallback();
194
195 // Reads /proc/meminfo and sends total anonymous memory usage to UMA.
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700196 bool MemuseCallbackWork();
Luigi Semenzato8accd332011-05-17 16:37:18 -0700197
Luigi Semenzatofb3a8212013-05-07 16:55:00 -0700198 // Parses meminfo data and sends it to UMA.
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700199 bool ProcessMemuse(const std::string& meminfo_raw);
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700200
Luigi Semenzato859b3f02014-02-05 15:33:19 -0800201 // Reads the current OS version from /etc/lsb-release and hashes it
202 // to a unsigned 32-bit int.
Ben Chanf05ab402014-08-07 00:54:59 -0700203 uint32_t GetOsVersionHash();
Luigi Semenzato859b3f02014-02-05 15:33:19 -0800204
Daniel Eratc83975a2014-04-04 08:53:44 -0700205 // Updates stats, additionally sending them to UMA if enough time has elapsed
206 // since the last report.
207 void UpdateStats(base::TimeTicks now_ticks, base::Time now_wall_time);
208
209 // Invoked periodically by |update_stats_timeout_id_| to call UpdateStats().
Steve Funge86591e2014-12-01 13:38:21 -0800210 void HandleUpdateStatsTimeout();
Daniel Eratc83975a2014-04-04 08:53:44 -0700211
Luigi Semenzato96360192014-06-04 10:53:35 -0700212 // Reports zram statistics.
213 bool ReportZram(const base::FilePath& zram_dir);
214
Ben Chanf05ab402014-08-07 00:54:59 -0700215 // Reads a string from a file and converts it to uint64_t.
216 static bool ReadFileToUint64(const base::FilePath& path, uint64_t* value);
Luigi Semenzato96360192014-06-04 10:53:35 -0700217
Alex Vakulenko53ca76f2015-12-29 15:02:00 -0800218 // Callback invoked when a connection to weaved's service is established
219 // over Binder interface.
220 void OnWeaveServiceConnected(const std::weak_ptr<weaved::Service>& service);
221
Luigi Semenzato96360192014-06-04 10:53:35 -0700222 // VARIABLES
223
Darin Petkov2ccef012010-05-05 16:06:37 -0700224 // Test mode.
Darin Petkov41e06232010-05-03 16:45:37 -0700225 bool testing_;
Darin Petkov65b01462010-04-14 13:32:20 -0700226
Bertrand SIMONNET9d3a4ae2015-11-25 13:49:12 -0800227 // Publicly readable metrics directory.
228 base::FilePath shared_metrics_directory_;
Bertrand SIMONNET71a62ef2014-10-07 11:26:25 -0700229
Darin Petkovfc91b422010-05-12 13:05:45 -0700230 // The metrics library handle.
231 MetricsLibraryInterface* metrics_lib_;
232
Daniel Eratc83975a2014-04-04 08:53:44 -0700233 // The last time that UpdateStats() was called.
234 base::TimeTicks last_update_stats_time_;
Darin Petkov41e06232010-05-03 16:45:37 -0700235
Luigi Semenzato0d9a9c92013-12-05 15:55:12 -0800236 // End time of current memuse stat collection interval.
237 double memuse_final_time_;
Luigi Semenzato8accd332011-05-17 16:37:18 -0700238
239 // Selects the wait time for the next memory use callback.
240 unsigned int memuse_interval_index_;
241
Luigi Semenzatoba0c65d2014-03-17 12:28:38 -0700242 // Used internally by GetIncrementalCpuUse() to return the CPU utilization
243 // between calls.
Bertrand SIMONNET0ada2ca2015-11-02 14:08:44 -0800244 base::TimeDelta latest_cpu_use_microseconds_;
Luigi Semenzatoba0c65d2014-03-17 12:28:38 -0700245
246 // Persistent values and accumulators for crash statistics.
Bertrand SIMONNET6c9fbb92015-12-21 14:56:40 -0800247 unique_ptr<PersistentInteger> daily_cycle_;
248 unique_ptr<PersistentInteger> weekly_cycle_;
249 unique_ptr<PersistentInteger> version_cycle_;
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800250
Luigi Semenzatoe5883fa2014-04-18 17:00:35 -0700251 // Active use accumulated in a day.
Bertrand SIMONNET6c9fbb92015-12-21 14:56:40 -0800252 unique_ptr<PersistentInteger> daily_active_use_;
Luigi Semenzatoe5883fa2014-04-18 17:00:35 -0700253 // Active use accumulated since the latest version update.
Bertrand SIMONNET6c9fbb92015-12-21 14:56:40 -0800254 unique_ptr<PersistentInteger> version_cumulative_active_use_;
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800255
Luigi Semenzatoba0c65d2014-03-17 12:28:38 -0700256 // The CPU time accumulator. This contains the CPU time, in milliseconds,
257 // used by the system since the most recent OS version update.
Bertrand SIMONNET6c9fbb92015-12-21 14:56:40 -0800258 unique_ptr<PersistentInteger> version_cumulative_cpu_use_;
Luigi Semenzatoba0c65d2014-03-17 12:28:38 -0700259
Bertrand SIMONNET6c9fbb92015-12-21 14:56:40 -0800260 unique_ptr<PersistentInteger> user_crash_interval_;
261 unique_ptr<PersistentInteger> kernel_crash_interval_;
262 unique_ptr<PersistentInteger> unclean_shutdown_interval_;
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800263
Bertrand SIMONNET6c9fbb92015-12-21 14:56:40 -0800264 unique_ptr<PersistentInteger> any_crashes_daily_count_;
265 unique_ptr<PersistentInteger> any_crashes_weekly_count_;
266 unique_ptr<PersistentInteger> user_crashes_daily_count_;
267 unique_ptr<PersistentInteger> user_crashes_weekly_count_;
268 unique_ptr<PersistentInteger> kernel_crashes_daily_count_;
269 unique_ptr<PersistentInteger> kernel_crashes_weekly_count_;
270 unique_ptr<PersistentInteger> kernel_crashes_version_count_;
271 unique_ptr<PersistentInteger> unclean_shutdowns_daily_count_;
272 unique_ptr<PersistentInteger> unclean_shutdowns_weekly_count_;
Bertrand SIMONNET0ada2ca2015-11-02 14:08:44 -0800273
Bertrand SIMONNET6c9fbb92015-12-21 14:56:40 -0800274 unique_ptr<CpuUsageCollector> cpu_usage_collector_;
275 unique_ptr<DiskUsageCollector> disk_usage_collector_;
276 unique_ptr<AveragedStatisticsCollector> averaged_stats_collector_;
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800277
Alex Vakulenko53ca76f2015-12-29 15:02:00 -0800278 unique_ptr<weaved::Service::Subscription> weave_service_subscription_;
279 std::weak_ptr<weaved::Service> service_;
280
281 base::WeakPtrFactory<MetricsCollector> weak_ptr_factory_{this};
Darin Petkov65b01462010-04-14 13:32:20 -0700282};
283
Bertrand SIMONNET608e4282015-11-12 17:52:17 -0800284#endif // METRICS_METRICS_COLLECTOR_H_