blob: 5855cee5db06760b2be28e890c806a51da822974 [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 SIMONNET4b915ae2015-07-28 15:38:14 -070017#include "metrics_daemon.h"
Darin Petkov65b01462010-04-14 13:32:20 -070018
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -080019#include <fcntl.h>
Luigi Semenzato4a6c9422014-06-30 18:12:28 -070020#include <inttypes.h>
Luigi Semenzato8accd332011-05-17 16:37:18 -070021#include <math.h>
Ken Mixter4c5daa42010-08-26 18:35:06 -070022#include <string.h>
Steve Funge86591e2014-12-01 13:38:21 -080023#include <sysexits.h>
Luigi Semenzato8accd332011-05-17 16:37:18 -070024#include <time.h>
Darin Petkov65b01462010-04-14 13:32:20 -070025
Bertrand SIMONNET4b915ae2015-07-28 15:38:14 -070026#include <base/bind.h>
Luigi Semenzato859b3f02014-02-05 15:33:19 -080027#include <base/files/file_path.h>
Ben Chan51bf92a2014-09-05 08:21:06 -070028#include <base/files/file_util.h>
Luigi Semenzato859b3f02014-02-05 15:33:19 -080029#include <base/hash.h>
Darin Petkov65b01462010-04-14 13:32:20 -070030#include <base/logging.h>
Ben Chan2e6543d2014-02-05 23:26:25 -080031#include <base/strings/string_number_conversions.h>
32#include <base/strings/string_split.h>
33#include <base/strings/string_util.h>
34#include <base/strings/stringprintf.h>
Bertrand SIMONNET26993622015-08-20 14:08:41 -070035#include <cutils/properties.h>
Steve Funge86591e2014-12-01 13:38:21 -080036#include <dbus/dbus.h>
37#include <dbus/message.h>
Bertrand SIMONNETbae5dcc2015-08-04 14:12:10 -070038
39#include "constants.h"
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -070040#include "uploader/upload_service.h"
Darin Petkov65b01462010-04-14 13:32:20 -070041
Ben Chan2e6543d2014-02-05 23:26:25 -080042using base::FilePath;
43using base::StringPrintf;
Darin Petkovf27f0362010-06-04 13:14:19 -070044using base::Time;
45using base::TimeDelta;
46using base::TimeTicks;
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080047using chromeos_metrics::PersistentInteger;
Luigi Semenzato8accd332011-05-17 16:37:18 -070048using std::map;
Darin Petkov38d5cb02010-06-24 12:10:26 -070049using std::string;
Luigi Semenzato8accd332011-05-17 16:37:18 -070050using std::vector;
51
Daniel Eratc83975a2014-04-04 08:53:44 -070052namespace {
Darin Petkovf27f0362010-06-04 13:14:19 -070053
Darin Petkov703ec972010-04-27 11:02:18 -070054#define SAFE_MESSAGE(e) (e.message ? e.message : "unknown error")
Darin Petkov40f25732013-04-29 15:07:31 +020055
Daniel Eratc83975a2014-04-04 08:53:44 -070056const char kCrashReporterInterface[] = "org.chromium.CrashReporter";
57const char kCrashReporterUserCrashSignal[] = "UserCrash";
Steve Funge86591e2014-12-01 13:38:21 -080058const char kCrashReporterMatchRule[] =
59 "type='signal',interface='%s',path='/',member='%s'";
Darin Petkov41e06232010-05-03 16:45:37 -070060
Daniel Eratc83975a2014-04-04 08:53:44 -070061const int kSecondsPerMinute = 60;
62const int kMinutesPerHour = 60;
63const int kHoursPerDay = 24;
64const int kMinutesPerDay = kHoursPerDay * kMinutesPerHour;
65const int kSecondsPerDay = kSecondsPerMinute * kMinutesPerDay;
66const int kDaysPerWeek = 7;
67const int kSecondsPerWeek = kSecondsPerDay * kDaysPerWeek;
Darin Petkov41e06232010-05-03 16:45:37 -070068
Daniel Eratc83975a2014-04-04 08:53:44 -070069// Interval between calls to UpdateStats().
Steve Funge86591e2014-12-01 13:38:21 -080070const uint32_t kUpdateStatsIntervalMs = 300000;
Darin Petkov65b01462010-04-14 13:32:20 -070071
Luigi Semenzatoc5a92342014-02-14 15:05:51 -080072const char kKernelCrashDetectedFile[] = "/var/run/kernel-crash-detected";
Daniel Eratc83975a2014-04-04 08:53:44 -070073const char kUncleanShutdownDetectedFile[] =
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080074 "/var/run/unclean-shutdown-detected";
Ken Mixterccd84c02010-08-16 19:57:13 -070075
Daniel Eratc83975a2014-04-04 08:53:44 -070076} // namespace
77
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -080078// disk stats metrics
79
80// The {Read,Write}Sectors numbers are in sectors/second.
81// A sector is usually 512 bytes.
82
83const char MetricsDaemon::kMetricReadSectorsLongName[] =
84 "Platform.ReadSectorsLong";
85const char MetricsDaemon::kMetricWriteSectorsLongName[] =
86 "Platform.WriteSectorsLong";
87const char MetricsDaemon::kMetricReadSectorsShortName[] =
88 "Platform.ReadSectorsShort";
89const char MetricsDaemon::kMetricWriteSectorsShortName[] =
90 "Platform.WriteSectorsShort";
91
Luigi Semenzato5bd764f2011-10-14 12:03:35 -070092const int MetricsDaemon::kMetricStatsShortInterval = 1; // seconds
93const int MetricsDaemon::kMetricStatsLongInterval = 30; // seconds
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -080094
Luigi Semenzato29c7ef92011-04-12 14:12:35 -070095const int MetricsDaemon::kMetricMeminfoInterval = 30; // seconds
96
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -080097// Assume a max rate of 250Mb/s for reads (worse for writes) and 512 byte
98// sectors.
99const int MetricsDaemon::kMetricSectorsIOMax = 500000; // sectors/second
100const int MetricsDaemon::kMetricSectorsBuckets = 50; // buckets
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700101// Page size is 4k, sector size is 0.5k. We're not interested in page fault
102// rates that the disk cannot sustain.
103const int MetricsDaemon::kMetricPageFaultsMax = kMetricSectorsIOMax / 8;
104const int MetricsDaemon::kMetricPageFaultsBuckets = 50;
105
106// Major page faults, i.e. the ones that require data to be read from disk.
107
108const char MetricsDaemon::kMetricPageFaultsLongName[] =
109 "Platform.PageFaultsLong";
110const char MetricsDaemon::kMetricPageFaultsShortName[] =
111 "Platform.PageFaultsShort";
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800112
Sonny Rao4b8aebb2013-07-31 23:18:31 -0700113// Swap in and Swap out
114
115const char MetricsDaemon::kMetricSwapInLongName[] =
116 "Platform.SwapInLong";
117const char MetricsDaemon::kMetricSwapInShortName[] =
118 "Platform.SwapInShort";
119
120const char MetricsDaemon::kMetricSwapOutLongName[] =
121 "Platform.SwapOutLong";
122const char MetricsDaemon::kMetricSwapOutShortName[] =
123 "Platform.SwapOutShort";
124
Luigi Semenzatoba0c65d2014-03-17 12:28:38 -0700125const char MetricsDaemon::kMetricsProcStatFileName[] = "/proc/stat";
126const int MetricsDaemon::kMetricsProcStatFirstLineItemsCount = 11;
127
Luigi Semenzatofb3a8212013-05-07 16:55:00 -0700128// Thermal CPU throttling.
129
130const char MetricsDaemon::kMetricScaledCpuFrequencyName[] =
131 "Platform.CpuFrequencyThermalScaling";
132
Luigi Semenzato96360192014-06-04 10:53:35 -0700133// Zram sysfs entries.
134
135const char MetricsDaemon::kComprDataSizeName[] = "compr_data_size";
136const char MetricsDaemon::kOrigDataSizeName[] = "orig_data_size";
137const char MetricsDaemon::kZeroPagesName[] = "zero_pages";
138
Luigi Semenzato8accd332011-05-17 16:37:18 -0700139// Memory use stats collection intervals. We collect some memory use interval
140// at these intervals after boot, and we stop collecting after the last one,
141// with the assumption that in most cases the memory use won't change much
142// after that.
143static const int kMemuseIntervals[] = {
144 1 * kSecondsPerMinute, // 1 minute mark
145 4 * kSecondsPerMinute, // 5 minute mark
146 25 * kSecondsPerMinute, // 0.5 hour mark
147 120 * kSecondsPerMinute, // 2.5 hour mark
148 600 * kSecondsPerMinute, // 12.5 hour mark
149};
150
Darin Petkovf1e85e42010-06-10 15:59:53 -0700151MetricsDaemon::MetricsDaemon()
Steve Funge86591e2014-12-01 13:38:21 -0800152 : memuse_final_time_(0),
Luigi Semenzato8accd332011-05-17 16:37:18 -0700153 memuse_interval_index_(0),
154 read_sectors_(0),
155 write_sectors_(0),
Sonny Rao4b8aebb2013-07-31 23:18:31 -0700156 vmstats_(),
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700157 stats_state_(kStatsShort),
Luigi Semenzatoba0c65d2014-03-17 12:28:38 -0700158 stats_initial_time_(0),
159 ticks_per_second_(0),
160 latest_cpu_use_ticks_(0) {}
Darin Petkovf1e85e42010-06-10 15:59:53 -0700161
Ken Mixter4c5daa42010-08-26 18:35:06 -0700162MetricsDaemon::~MetricsDaemon() {
Ken Mixter4c5daa42010-08-26 18:35:06 -0700163}
164
Luigi Semenzato8accd332011-05-17 16:37:18 -0700165double MetricsDaemon::GetActiveTime() {
166 struct timespec ts;
167 int r = clock_gettime(CLOCK_MONOTONIC, &ts);
168 if (r < 0) {
169 PLOG(WARNING) << "clock_gettime(CLOCK_MONOTONIC) failed";
170 return 0;
171 } else {
Luigi Semenzato4a6c9422014-06-30 18:12:28 -0700172 return ts.tv_sec + static_cast<double>(ts.tv_nsec) / (1000 * 1000 * 1000);
Luigi Semenzato8accd332011-05-17 16:37:18 -0700173 }
174}
175
Steve Funge86591e2014-12-01 13:38:21 -0800176int MetricsDaemon::Run() {
Ken Mixterccd84c02010-08-16 19:57:13 -0700177 if (CheckSystemCrash(kKernelCrashDetectedFile)) {
178 ProcessKernelCrash();
179 }
180
181 if (CheckSystemCrash(kUncleanShutdownDetectedFile)) {
182 ProcessUncleanShutdown();
183 }
184
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800185 // On OS version change, clear version stats (which are reported daily).
Ben Chanf05ab402014-08-07 00:54:59 -0700186 int32_t version = GetOsVersionHash();
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800187 if (version_cycle_->Get() != version) {
188 version_cycle_->Set(version);
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800189 kernel_crashes_version_count_->Set(0);
Luigi Semenzatoe5883fa2014-04-18 17:00:35 -0700190 version_cumulative_active_use_->Set(0);
Luigi Semenzatoba0c65d2014-03-17 12:28:38 -0700191 version_cumulative_cpu_use_->Set(0);
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800192 }
193
Steve Funge86591e2014-12-01 13:38:21 -0800194 return chromeos::DBusDaemon::Run();
Darin Petkov65b01462010-04-14 13:32:20 -0700195}
196
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -0700197void MetricsDaemon::RunUploaderTest() {
Bertrand SIMONNET71a62ef2014-10-07 11:26:25 -0700198 upload_service_.reset(new UploadService(new SystemProfileCache(true,
199 config_root_),
Bertrand SIMONNETe4fa61e2015-02-18 09:38:55 -0800200 metrics_lib_,
Bertrand SIMONNET71a62ef2014-10-07 11:26:25 -0700201 server_));
Bertrand SIMONNETcac74e12014-10-09 10:14:13 -0700202 upload_service_->Init(upload_interval_, metrics_file_);
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -0700203 upload_service_->UploadEvent();
204}
205
Ben Chanf05ab402014-08-07 00:54:59 -0700206uint32_t MetricsDaemon::GetOsVersionHash() {
207 static uint32_t cached_version_hash = 0;
Luigi Semenzato859b3f02014-02-05 15:33:19 -0800208 static bool version_hash_is_cached = false;
209 if (version_hash_is_cached)
210 return cached_version_hash;
211 version_hash_is_cached = true;
Bertrand SIMONNET26993622015-08-20 14:08:41 -0700212
213 char version[PROPERTY_VALUE_MAX];
Bertrand SIMONNETbae5dcc2015-08-04 14:12:10 -0700214 // The version might not be set for development devices. In this case, use the
215 // zero version.
Bertrand SIMONNET26993622015-08-20 14:08:41 -0700216 property_get(metrics::kProductVersionProperty, version,
217 metrics::kDefaultVersion);
218
Bertrand SIMONNETbae5dcc2015-08-04 14:12:10 -0700219 cached_version_hash = base::Hash(version);
220 if (testing_) {
Luigi Semenzato859b3f02014-02-05 15:33:19 -0800221 cached_version_hash = 42; // return any plausible value for the hash
Luigi Semenzato859b3f02014-02-05 15:33:19 -0800222 }
223 return cached_version_hash;
224}
225
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -0700226void MetricsDaemon::Init(bool testing,
227 bool uploader_active,
Bertrand SIMONNETfec4d2c2015-08-05 16:04:14 -0700228 bool dbus_enabled,
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -0700229 MetricsLibraryInterface* metrics_lib,
Luigi Semenzatofb3a8212013-05-07 16:55:00 -0700230 const string& vmstats_path,
231 const string& scaling_max_freq_path,
Steve Fung67906c62014-10-06 15:15:30 -0700232 const string& cpuinfo_max_freq_path,
Bertrand SIMONNETcac74e12014-10-09 10:14:13 -0700233 const base::TimeDelta& upload_interval,
Steve Fung67906c62014-10-06 15:15:30 -0700234 const string& server,
Bertrand SIMONNET71a62ef2014-10-07 11:26:25 -0700235 const string& metrics_file,
236 const string& config_root) {
Darin Petkov65b01462010-04-14 13:32:20 -0700237 testing_ = testing;
Steve Funge86591e2014-12-01 13:38:21 -0800238 uploader_active_ = uploader_active;
Bertrand SIMONNETfec4d2c2015-08-05 16:04:14 -0700239 dbus_enabled_ = dbus_enabled;
Bertrand SIMONNET71a62ef2014-10-07 11:26:25 -0700240 config_root_ = config_root;
Alex Vakulenko14595032014-08-28 14:59:56 -0700241 DCHECK(metrics_lib != nullptr);
Darin Petkovfc91b422010-05-12 13:05:45 -0700242 metrics_lib_ = metrics_lib;
Darin Petkov38d5cb02010-06-24 12:10:26 -0700243
Bertrand SIMONNETcac74e12014-10-09 10:14:13 -0700244 upload_interval_ = upload_interval;
Steve Fung67906c62014-10-06 15:15:30 -0700245 server_ = server;
246 metrics_file_ = metrics_file;
247
Luigi Semenzatoba0c65d2014-03-17 12:28:38 -0700248 // Get ticks per second (HZ) on this system.
249 // Sysconf cannot fail, so no sanity checks are needed.
250 ticks_per_second_ = sysconf(_SC_CLK_TCK);
251
Luigi Semenzatoe5883fa2014-04-18 17:00:35 -0700252 daily_active_use_.reset(
Luigi Semenzatodc865892015-07-09 08:28:08 -0700253 new PersistentInteger("Platform.DailyUseTime"));
Luigi Semenzatoe5883fa2014-04-18 17:00:35 -0700254 version_cumulative_active_use_.reset(
Luigi Semenzatodc865892015-07-09 08:28:08 -0700255 new PersistentInteger("Platform.CumulativeDailyUseTime"));
Luigi Semenzatoba0c65d2014-03-17 12:28:38 -0700256 version_cumulative_cpu_use_.reset(
Luigi Semenzatodc865892015-07-09 08:28:08 -0700257 new PersistentInteger("Platform.CumulativeCpuTime"));
Darin Petkov38d5cb02010-06-24 12:10:26 -0700258
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800259 kernel_crash_interval_.reset(
Luigi Semenzatodc865892015-07-09 08:28:08 -0700260 new PersistentInteger("Platform.KernelCrashInterval"));
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800261 unclean_shutdown_interval_.reset(
Luigi Semenzatodc865892015-07-09 08:28:08 -0700262 new PersistentInteger("Platform.UncleanShutdownInterval"));
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800263 user_crash_interval_.reset(
Luigi Semenzatodc865892015-07-09 08:28:08 -0700264 new PersistentInteger("Platform.UserCrashInterval"));
Darin Petkov2ccef012010-05-05 16:06:37 -0700265
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800266 any_crashes_daily_count_.reset(
Luigi Semenzatodc865892015-07-09 08:28:08 -0700267 new PersistentInteger("Platform.AnyCrashesDaily"));
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800268 any_crashes_weekly_count_.reset(
Luigi Semenzatodc865892015-07-09 08:28:08 -0700269 new PersistentInteger("Platform.AnyCrashesWeekly"));
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800270 user_crashes_daily_count_.reset(
Luigi Semenzatodc865892015-07-09 08:28:08 -0700271 new PersistentInteger("Platform.UserCrashesDaily"));
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800272 user_crashes_weekly_count_.reset(
Luigi Semenzatodc865892015-07-09 08:28:08 -0700273 new PersistentInteger("Platform.UserCrashesWeekly"));
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800274 kernel_crashes_daily_count_.reset(
Luigi Semenzatodc865892015-07-09 08:28:08 -0700275 new PersistentInteger("Platform.KernelCrashesDaily"));
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800276 kernel_crashes_weekly_count_.reset(
Luigi Semenzatodc865892015-07-09 08:28:08 -0700277 new PersistentInteger("Platform.KernelCrashesWeekly"));
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800278 kernel_crashes_version_count_.reset(
Luigi Semenzatodc865892015-07-09 08:28:08 -0700279 new PersistentInteger("Platform.KernelCrashesSinceUpdate"));
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800280 unclean_shutdowns_daily_count_.reset(
Luigi Semenzatodc865892015-07-09 08:28:08 -0700281 new PersistentInteger("Platform.UncleanShutdownsDaily"));
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800282 unclean_shutdowns_weekly_count_.reset(
Luigi Semenzatodc865892015-07-09 08:28:08 -0700283 new PersistentInteger("Platform.UncleanShutdownsWeekly"));
Darin Petkov38d5cb02010-06-24 12:10:26 -0700284
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800285 daily_cycle_.reset(new PersistentInteger("daily.cycle"));
286 weekly_cycle_.reset(new PersistentInteger("weekly.cycle"));
287 version_cycle_.reset(new PersistentInteger("version.cycle"));
Luigi Semenzato859b3f02014-02-05 15:33:19 -0800288
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700289 vmstats_path_ = vmstats_path;
Luigi Semenzatofb3a8212013-05-07 16:55:00 -0700290 scaling_max_freq_path_ = scaling_max_freq_path;
291 cpuinfo_max_freq_path_ = cpuinfo_max_freq_path;
Steve Funge86591e2014-12-01 13:38:21 -0800292}
293
294int MetricsDaemon::OnInit() {
Bertrand SIMONNETfec4d2c2015-08-05 16:04:14 -0700295 int return_code = dbus_enabled_ ? chromeos::DBusDaemon::OnInit() :
296 chromeos::Daemon::OnInit();
Steve Funge86591e2014-12-01 13:38:21 -0800297 if (return_code != EX_OK)
298 return return_code;
299
Steve Funge86591e2014-12-01 13:38:21 -0800300 if (testing_)
301 return EX_OK;
Darin Petkov65b01462010-04-14 13:32:20 -0700302
Bertrand SIMONNETfec4d2c2015-08-05 16:04:14 -0700303 if (dbus_enabled_) {
304 bus_->AssertOnDBusThread();
305 CHECK(bus_->SetUpAsyncOperations());
Darin Petkov65b01462010-04-14 13:32:20 -0700306
Bertrand SIMONNETfec4d2c2015-08-05 16:04:14 -0700307 if (bus_->is_connected()) {
308 const std::string match_rule =
309 base::StringPrintf(kCrashReporterMatchRule,
310 kCrashReporterInterface,
311 kCrashReporterUserCrashSignal);
Darin Petkov65b01462010-04-14 13:32:20 -0700312
Bertrand SIMONNETfec4d2c2015-08-05 16:04:14 -0700313 bus_->AddFilterFunction(&MetricsDaemon::MessageFilter, this);
Darin Petkov65b01462010-04-14 13:32:20 -0700314
Bertrand SIMONNETfec4d2c2015-08-05 16:04:14 -0700315 DBusError error;
316 dbus_error_init(&error);
317 bus_->AddMatch(match_rule, &error);
Darin Petkov65b01462010-04-14 13:32:20 -0700318
Bertrand SIMONNETfec4d2c2015-08-05 16:04:14 -0700319 if (dbus_error_is_set(&error)) {
320 LOG(ERROR) << "Failed to add match rule \"" << match_rule << "\". Got "
321 << error.name << ": " << error.message;
322 return EX_SOFTWARE;
323 }
324 } else {
325 LOG(ERROR) << "DBus isn't connected.";
326 return EX_UNAVAILABLE;
Steve Funge86591e2014-12-01 13:38:21 -0800327 }
Darin Petkov703ec972010-04-27 11:02:18 -0700328 }
329
Steve Funge86591e2014-12-01 13:38:21 -0800330 if (uploader_active_) {
Bertrand SIMONNETbae5dcc2015-08-04 14:12:10 -0700331 upload_service_.reset(
332 new UploadService(new SystemProfileCache(), metrics_lib_, server_));
333 upload_service_->Init(upload_interval_, metrics_file_);
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -0700334 }
Steve Funge86591e2014-12-01 13:38:21 -0800335
336 return EX_OK;
Darin Petkov65b01462010-04-14 13:32:20 -0700337}
338
Steve Funge86591e2014-12-01 13:38:21 -0800339void MetricsDaemon::OnShutdown(int* return_code) {
Bertrand SIMONNETfec4d2c2015-08-05 16:04:14 -0700340 if (!testing_ && dbus_enabled_ && bus_->is_connected()) {
Steve Funge86591e2014-12-01 13:38:21 -0800341 const std::string match_rule =
342 base::StringPrintf(kCrashReporterMatchRule,
343 kCrashReporterInterface,
344 kCrashReporterUserCrashSignal);
345
346 bus_->RemoveFilterFunction(&MetricsDaemon::MessageFilter, this);
347
348 DBusError error;
349 dbus_error_init(&error);
350 bus_->RemoveMatch(match_rule, &error);
351
352 if (dbus_error_is_set(&error)) {
353 LOG(ERROR) << "Failed to remove match rule \"" << match_rule << "\". Got "
354 << error.name << ": " << error.message;
355 }
356 }
357 chromeos::DBusDaemon::OnShutdown(return_code);
Darin Petkov65b01462010-04-14 13:32:20 -0700358}
359
Darin Petkov703ec972010-04-27 11:02:18 -0700360// static
361DBusHandlerResult MetricsDaemon::MessageFilter(DBusConnection* connection,
362 DBusMessage* message,
363 void* user_data) {
Darin Petkov703ec972010-04-27 11:02:18 -0700364 int message_type = dbus_message_get_type(message);
365 if (message_type != DBUS_MESSAGE_TYPE_SIGNAL) {
Darin Petkov41e06232010-05-03 16:45:37 -0700366 DLOG(WARNING) << "unexpected message type " << message_type;
Darin Petkov703ec972010-04-27 11:02:18 -0700367 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
368 }
369
370 // Signal messages always have interfaces.
Daniel Eratc83975a2014-04-04 08:53:44 -0700371 const std::string interface(dbus_message_get_interface(message));
372 const std::string member(dbus_message_get_member(message));
373 DLOG(INFO) << "Got " << interface << "." << member << " D-Bus signal";
Darin Petkov703ec972010-04-27 11:02:18 -0700374
375 MetricsDaemon* daemon = static_cast<MetricsDaemon*>(user_data);
376
377 DBusMessageIter iter;
378 dbus_message_iter_init(message, &iter);
Daniel Eratc83975a2014-04-04 08:53:44 -0700379 if (interface == kCrashReporterInterface) {
380 CHECK_EQ(member, kCrashReporterUserCrashSignal);
Darin Petkov1bb904e2010-06-16 15:58:06 -0700381 daemon->ProcessUserCrash();
Darin Petkov703ec972010-04-27 11:02:18 -0700382 } else {
Daniel Eratc83975a2014-04-04 08:53:44 -0700383 // Ignore messages from the bus itself.
Darin Petkov703ec972010-04-27 11:02:18 -0700384 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
385 }
386
387 return DBUS_HANDLER_RESULT_HANDLED;
Darin Petkov65b01462010-04-14 13:32:20 -0700388}
389
Luigi Semenzatoba0c65d2014-03-17 12:28:38 -0700390// One might argue that parts of this should go into
391// chromium/src/base/sys_info_chromeos.c instead, but put it here for now.
392
393TimeDelta MetricsDaemon::GetIncrementalCpuUse() {
Luigi Semenzatoba0c65d2014-03-17 12:28:38 -0700394 FilePath proc_stat_path = FilePath(kMetricsProcStatFileName);
395 std::string proc_stat_string;
396 if (!base::ReadFileToString(proc_stat_path, &proc_stat_string)) {
397 LOG(WARNING) << "cannot open " << kMetricsProcStatFileName;
398 return TimeDelta();
399 }
400
401 std::vector<std::string> proc_stat_lines;
402 base::SplitString(proc_stat_string, '\n', &proc_stat_lines);
403 if (proc_stat_lines.empty()) {
404 LOG(WARNING) << "cannot parse " << kMetricsProcStatFileName
405 << ": " << proc_stat_string;
406 return TimeDelta();
407 }
408 std::vector<std::string> proc_stat_totals;
409 base::SplitStringAlongWhitespace(proc_stat_lines[0], &proc_stat_totals);
410
Ben Chanf05ab402014-08-07 00:54:59 -0700411 uint64_t user_ticks, user_nice_ticks, system_ticks;
Luigi Semenzatoba0c65d2014-03-17 12:28:38 -0700412 if (proc_stat_totals.size() != kMetricsProcStatFirstLineItemsCount ||
413 proc_stat_totals[0] != "cpu" ||
414 !base::StringToUint64(proc_stat_totals[1], &user_ticks) ||
415 !base::StringToUint64(proc_stat_totals[2], &user_nice_ticks) ||
416 !base::StringToUint64(proc_stat_totals[3], &system_ticks)) {
417 LOG(WARNING) << "cannot parse first line: " << proc_stat_lines[0];
418 return TimeDelta(base::TimeDelta::FromSeconds(0));
419 }
420
Ben Chanf05ab402014-08-07 00:54:59 -0700421 uint64_t total_cpu_use_ticks = user_ticks + user_nice_ticks + system_ticks;
Luigi Semenzatoba0c65d2014-03-17 12:28:38 -0700422
423 // Sanity check.
424 if (total_cpu_use_ticks < latest_cpu_use_ticks_) {
425 LOG(WARNING) << "CPU time decreasing from " << latest_cpu_use_ticks_
426 << " to " << total_cpu_use_ticks;
427 return TimeDelta();
428 }
429
Ben Chanf05ab402014-08-07 00:54:59 -0700430 uint64_t diff = total_cpu_use_ticks - latest_cpu_use_ticks_;
Luigi Semenzatoba0c65d2014-03-17 12:28:38 -0700431 latest_cpu_use_ticks_ = total_cpu_use_ticks;
432 // Use microseconds to avoid significant truncations.
433 return base::TimeDelta::FromMicroseconds(
434 diff * 1000 * 1000 / ticks_per_second_);
435}
436
Darin Petkov1bb904e2010-06-16 15:58:06 -0700437void MetricsDaemon::ProcessUserCrash() {
Daniel Eratc83975a2014-04-04 08:53:44 -0700438 // Counts the active time up to now.
439 UpdateStats(TimeTicks::Now(), Time::Now());
Darin Petkov1bb904e2010-06-16 15:58:06 -0700440
441 // Reports the active use time since the last crash and resets it.
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800442 SendCrashIntervalSample(user_crash_interval_);
Ken Mixterccd84c02010-08-16 19:57:13 -0700443
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800444 any_crashes_daily_count_->Add(1);
445 any_crashes_weekly_count_->Add(1);
446 user_crashes_daily_count_->Add(1);
447 user_crashes_weekly_count_->Add(1);
Darin Petkov1bb904e2010-06-16 15:58:06 -0700448}
449
Darin Petkov38d5cb02010-06-24 12:10:26 -0700450void MetricsDaemon::ProcessKernelCrash() {
Daniel Eratc83975a2014-04-04 08:53:44 -0700451 // Counts the active time up to now.
452 UpdateStats(TimeTicks::Now(), Time::Now());
Darin Petkov38d5cb02010-06-24 12:10:26 -0700453
454 // Reports the active use time since the last crash and resets it.
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800455 SendCrashIntervalSample(kernel_crash_interval_);
Ken Mixterccd84c02010-08-16 19:57:13 -0700456
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800457 any_crashes_daily_count_->Add(1);
458 any_crashes_weekly_count_->Add(1);
459 kernel_crashes_daily_count_->Add(1);
460 kernel_crashes_weekly_count_->Add(1);
Luigi Semenzato859b3f02014-02-05 15:33:19 -0800461
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800462 kernel_crashes_version_count_->Add(1);
Darin Petkov38d5cb02010-06-24 12:10:26 -0700463}
464
Ken Mixterccd84c02010-08-16 19:57:13 -0700465void MetricsDaemon::ProcessUncleanShutdown() {
Daniel Eratc83975a2014-04-04 08:53:44 -0700466 // Counts the active time up to now.
467 UpdateStats(TimeTicks::Now(), Time::Now());
Ken Mixterccd84c02010-08-16 19:57:13 -0700468
469 // Reports the active use time since the last crash and resets it.
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800470 SendCrashIntervalSample(unclean_shutdown_interval_);
Ken Mixterccd84c02010-08-16 19:57:13 -0700471
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800472 unclean_shutdowns_daily_count_->Add(1);
473 unclean_shutdowns_weekly_count_->Add(1);
474 any_crashes_daily_count_->Add(1);
475 any_crashes_weekly_count_->Add(1);
Ken Mixterccd84c02010-08-16 19:57:13 -0700476}
477
Luigi Semenzato8accd332011-05-17 16:37:18 -0700478bool MetricsDaemon::CheckSystemCrash(const string& crash_file) {
Darin Petkov38d5cb02010-06-24 12:10:26 -0700479 FilePath crash_detected(crash_file);
Ben Chan2e6543d2014-02-05 23:26:25 -0800480 if (!base::PathExists(crash_detected))
Ken Mixterccd84c02010-08-16 19:57:13 -0700481 return false;
Darin Petkov38d5cb02010-06-24 12:10:26 -0700482
483 // Deletes the crash-detected file so that the daemon doesn't report
484 // another kernel crash in case it's restarted.
Luigi Semenzato859b3f02014-02-05 15:33:19 -0800485 base::DeleteFile(crash_detected, false); // not recursive
Ken Mixterccd84c02010-08-16 19:57:13 -0700486 return true;
Darin Petkov38d5cb02010-06-24 12:10:26 -0700487}
488
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700489void MetricsDaemon::StatsReporterInit() {
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800490 DiskStatsReadStats(&read_sectors_, &write_sectors_);
Sonny Rao4b8aebb2013-07-31 23:18:31 -0700491 VmStatsReadStats(&vmstats_);
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800492 // The first time around just run the long stat, so we don't delay boot.
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700493 stats_state_ = kStatsLong;
494 stats_initial_time_ = GetActiveTime();
495 if (stats_initial_time_ < 0) {
Luigi Semenzato8accd332011-05-17 16:37:18 -0700496 LOG(WARNING) << "not collecting disk stats";
497 } else {
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700498 ScheduleStatsCallback(kMetricStatsLongInterval);
Luigi Semenzato8accd332011-05-17 16:37:18 -0700499 }
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800500}
501
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700502void MetricsDaemon::ScheduleStatsCallback(int wait) {
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800503 if (testing_) {
504 return;
505 }
Steve Funge86591e2014-12-01 13:38:21 -0800506 base::MessageLoop::current()->PostDelayedTask(FROM_HERE,
507 base::Bind(&MetricsDaemon::StatsCallback, base::Unretained(this)),
508 base::TimeDelta::FromSeconds(wait));
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800509}
510
Sonny Rao4b8aebb2013-07-31 23:18:31 -0700511bool MetricsDaemon::VmStatsParseStats(const char* stats,
512 struct VmstatRecord* record) {
513 // a mapping of string name to field in VmstatRecord and whether we found it
514 struct mapping {
515 const string name;
516 uint64_t* value_p;
517 bool found;
518 } map[] =
519 { { .name = "pgmajfault",
520 .value_p = &record->page_faults_,
521 .found = false },
522 { .name = "pswpin",
523 .value_p = &record->swap_in_,
524 .found = false },
525 { .name = "pswpout",
526 .value_p = &record->swap_out_,
527 .found = false }, };
528
Luigi Semenzatofb3a8212013-05-07 16:55:00 -0700529 // Each line in the file has the form
530 // <ID> <VALUE>
531 // for instance:
532 // nr_free_pages 213427
Sonny Rao4b8aebb2013-07-31 23:18:31 -0700533 vector<string> lines;
534 Tokenize(stats, "\n", &lines);
535 for (vector<string>::iterator it = lines.begin();
536 it != lines.end(); ++it) {
537 vector<string> tokens;
538 base::SplitString(*it, ' ', &tokens);
539 if (tokens.size() == 2) {
540 for (unsigned int i = 0; i < sizeof(map)/sizeof(struct mapping); i++) {
541 if (!tokens[0].compare(map[i].name)) {
542 if (!base::StringToUint64(tokens[1], map[i].value_p))
543 return false;
544 map[i].found = true;
545 }
546 }
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700547 } else {
Sonny Rao4b8aebb2013-07-31 23:18:31 -0700548 LOG(WARNING) << "unexpected vmstat format";
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700549 }
550 }
Sonny Rao4b8aebb2013-07-31 23:18:31 -0700551 // make sure we got all the stats
552 for (unsigned i = 0; i < sizeof(map)/sizeof(struct mapping); i++) {
553 if (map[i].found == false) {
554 LOG(WARNING) << "vmstat missing " << map[i].name;
555 return false;
556 }
557 }
558 return true;
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700559}
560
Sonny Rao4b8aebb2013-07-31 23:18:31 -0700561bool MetricsDaemon::VmStatsReadStats(struct VmstatRecord* stats) {
562 string value_string;
563 FilePath* path = new FilePath(vmstats_path_);
Ben Chan2e6543d2014-02-05 23:26:25 -0800564 if (!base::ReadFileToString(*path, &value_string)) {
Sonny Rao4b8aebb2013-07-31 23:18:31 -0700565 delete path;
566 LOG(WARNING) << "cannot read " << vmstats_path_;
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700567 return false;
568 }
Sonny Rao4b8aebb2013-07-31 23:18:31 -0700569 delete path;
570 return VmStatsParseStats(value_string.c_str(), stats);
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800571}
572
Luigi Semenzatofb3a8212013-05-07 16:55:00 -0700573bool MetricsDaemon::ReadFreqToInt(const string& sysfs_file_name, int* value) {
Luigi Semenzatod92d18c2013-06-04 13:24:21 -0700574 const FilePath sysfs_path(sysfs_file_name);
Luigi Semenzatofb3a8212013-05-07 16:55:00 -0700575 string value_string;
Ben Chan2e6543d2014-02-05 23:26:25 -0800576 if (!base::ReadFileToString(sysfs_path, &value_string)) {
Luigi Semenzatofb3a8212013-05-07 16:55:00 -0700577 LOG(WARNING) << "cannot read " << sysfs_path.value().c_str();
578 return false;
579 }
Ben Chan2e6543d2014-02-05 23:26:25 -0800580 if (!base::RemoveChars(value_string, "\n", &value_string)) {
Luigi Semenzatofb3a8212013-05-07 16:55:00 -0700581 LOG(WARNING) << "no newline in " << value_string;
582 // Continue even though the lack of newline is suspicious.
583 }
584 if (!base::StringToInt(value_string, value)) {
585 LOG(WARNING) << "cannot convert " << value_string << " to int";
586 return false;
587 }
588 return true;
589}
590
591void MetricsDaemon::SendCpuThrottleMetrics() {
592 // |max_freq| is 0 only the first time through.
593 static int max_freq = 0;
594 if (max_freq == -1)
595 // Give up, as sysfs did not report max_freq correctly.
596 return;
597 if (max_freq == 0 || testing_) {
598 // One-time initialization of max_freq. (Every time when testing.)
599 if (!ReadFreqToInt(cpuinfo_max_freq_path_, &max_freq)) {
600 max_freq = -1;
601 return;
602 }
603 if (max_freq == 0) {
604 LOG(WARNING) << "sysfs reports 0 max CPU frequency\n";
605 max_freq = -1;
606 return;
607 }
608 if (max_freq % 10000 == 1000) {
609 // Special case: system has turbo mode, and max non-turbo frequency is
610 // max_freq - 1000. This relies on "normal" (non-turbo) frequencies
611 // being multiples of (at least) 10 MHz. Although there is no guarantee
612 // of this, it seems a fairly reasonable assumption. Otherwise we should
613 // read scaling_available_frequencies, sort the frequencies, compare the
614 // two highest ones, and check if they differ by 1000 (kHz) (and that's a
615 // hack too, no telling when it will change).
616 max_freq -= 1000;
617 }
618 }
619 int scaled_freq = 0;
620 if (!ReadFreqToInt(scaling_max_freq_path_, &scaled_freq))
621 return;
622 // Frequencies are in kHz. If scaled_freq > max_freq, turbo is on, but
623 // scaled_freq is not the actual turbo frequency. We indicate this situation
624 // with a 101% value.
625 int percent = scaled_freq > max_freq ? 101 : scaled_freq / (max_freq / 100);
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800626 SendLinearSample(kMetricScaledCpuFrequencyName, percent, 101, 102);
Luigi Semenzatofb3a8212013-05-07 16:55:00 -0700627}
628
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700629// Collects disk and vm stats alternating over a short and a long interval.
Luigi Semenzato8accd332011-05-17 16:37:18 -0700630
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700631void MetricsDaemon::StatsCallback() {
Ben Chanf05ab402014-08-07 00:54:59 -0700632 uint64_t read_sectors_now, write_sectors_now;
Sonny Rao4b8aebb2013-07-31 23:18:31 -0700633 struct VmstatRecord vmstats_now;
Luigi Semenzato8accd332011-05-17 16:37:18 -0700634 double time_now = GetActiveTime();
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700635 double delta_time = time_now - stats_initial_time_;
Luigi Semenzato8accd332011-05-17 16:37:18 -0700636 if (testing_) {
637 // Fake the time when testing.
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700638 delta_time = stats_state_ == kStatsShort ?
639 kMetricStatsShortInterval : kMetricStatsLongInterval;
Luigi Semenzato8accd332011-05-17 16:37:18 -0700640 }
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700641 bool diskstats_success = DiskStatsReadStats(&read_sectors_now,
642 &write_sectors_now);
Luigi Semenzato8accd332011-05-17 16:37:18 -0700643 int delta_read = read_sectors_now - read_sectors_;
644 int delta_write = write_sectors_now - write_sectors_;
645 int read_sectors_per_second = delta_read / delta_time;
646 int write_sectors_per_second = delta_write / delta_time;
Sonny Rao4b8aebb2013-07-31 23:18:31 -0700647 bool vmstats_success = VmStatsReadStats(&vmstats_now);
648 uint64_t delta_faults = vmstats_now.page_faults_ - vmstats_.page_faults_;
649 uint64_t delta_swap_in = vmstats_now.swap_in_ - vmstats_.swap_in_;
650 uint64_t delta_swap_out = vmstats_now.swap_out_ - vmstats_.swap_out_;
651 uint64_t page_faults_per_second = delta_faults / delta_time;
652 uint64_t swap_in_per_second = delta_swap_in / delta_time;
653 uint64_t swap_out_per_second = delta_swap_out / delta_time;
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800654
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700655 switch (stats_state_) {
656 case kStatsShort:
657 if (diskstats_success) {
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800658 SendSample(kMetricReadSectorsShortName,
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700659 read_sectors_per_second,
660 1,
661 kMetricSectorsIOMax,
662 kMetricSectorsBuckets);
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800663 SendSample(kMetricWriteSectorsShortName,
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700664 write_sectors_per_second,
665 1,
666 kMetricSectorsIOMax,
667 kMetricSectorsBuckets);
668 }
669 if (vmstats_success) {
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800670 SendSample(kMetricPageFaultsShortName,
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700671 page_faults_per_second,
672 1,
673 kMetricPageFaultsMax,
674 kMetricPageFaultsBuckets);
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800675 SendSample(kMetricSwapInShortName,
Sonny Rao4b8aebb2013-07-31 23:18:31 -0700676 swap_in_per_second,
677 1,
678 kMetricPageFaultsMax,
679 kMetricPageFaultsBuckets);
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800680 SendSample(kMetricSwapOutShortName,
Sonny Rao4b8aebb2013-07-31 23:18:31 -0700681 swap_out_per_second,
682 1,
683 kMetricPageFaultsMax,
684 kMetricPageFaultsBuckets);
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700685 }
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800686 // Schedule long callback.
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700687 stats_state_ = kStatsLong;
688 ScheduleStatsCallback(kMetricStatsLongInterval -
689 kMetricStatsShortInterval);
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800690 break;
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700691 case kStatsLong:
692 if (diskstats_success) {
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800693 SendSample(kMetricReadSectorsLongName,
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700694 read_sectors_per_second,
695 1,
696 kMetricSectorsIOMax,
697 kMetricSectorsBuckets);
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800698 SendSample(kMetricWriteSectorsLongName,
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700699 write_sectors_per_second,
700 1,
701 kMetricSectorsIOMax,
702 kMetricSectorsBuckets);
703 // Reset sector counters.
704 read_sectors_ = read_sectors_now;
705 write_sectors_ = write_sectors_now;
706 }
707 if (vmstats_success) {
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800708 SendSample(kMetricPageFaultsLongName,
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700709 page_faults_per_second,
710 1,
711 kMetricPageFaultsMax,
712 kMetricPageFaultsBuckets);
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800713 SendSample(kMetricSwapInLongName,
Sonny Rao4b8aebb2013-07-31 23:18:31 -0700714 swap_in_per_second,
715 1,
716 kMetricPageFaultsMax,
717 kMetricPageFaultsBuckets);
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800718 SendSample(kMetricSwapOutLongName,
Sonny Rao4b8aebb2013-07-31 23:18:31 -0700719 swap_out_per_second,
720 1,
721 kMetricPageFaultsMax,
722 kMetricPageFaultsBuckets);
723
724 vmstats_ = vmstats_now;
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700725 }
Luigi Semenzatofb3a8212013-05-07 16:55:00 -0700726 SendCpuThrottleMetrics();
Luigi Semenzato8accd332011-05-17 16:37:18 -0700727 // Set start time for new cycle.
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700728 stats_initial_time_ = time_now;
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800729 // Schedule short callback.
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700730 stats_state_ = kStatsShort;
731 ScheduleStatsCallback(kMetricStatsShortInterval);
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800732 break;
733 default:
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700734 LOG(FATAL) << "Invalid stats state";
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800735 }
736}
737
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700738void MetricsDaemon::ScheduleMeminfoCallback(int wait) {
739 if (testing_) {
740 return;
741 }
Steve Funge86591e2014-12-01 13:38:21 -0800742 base::TimeDelta waitDelta = base::TimeDelta::FromSeconds(wait);
743 base::MessageLoop::current()->PostDelayedTask(FROM_HERE,
744 base::Bind(&MetricsDaemon::MeminfoCallback, base::Unretained(this),
Steve Fung8ab89c52015-01-05 13:48:30 -0800745 waitDelta),
Steve Funge86591e2014-12-01 13:38:21 -0800746 waitDelta);
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700747}
748
Steve Funge86591e2014-12-01 13:38:21 -0800749void MetricsDaemon::MeminfoCallback(base::TimeDelta wait) {
Luigi Semenzato8accd332011-05-17 16:37:18 -0700750 string meminfo_raw;
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700751 const FilePath meminfo_path("/proc/meminfo");
Ben Chan2e6543d2014-02-05 23:26:25 -0800752 if (!base::ReadFileToString(meminfo_path, &meminfo_raw)) {
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700753 LOG(WARNING) << "cannot read " << meminfo_path.value().c_str();
Steve Funge86591e2014-12-01 13:38:21 -0800754 return;
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700755 }
Luigi Semenzato96360192014-06-04 10:53:35 -0700756 // Make both calls even if the first one fails.
757 bool success = ProcessMeminfo(meminfo_raw);
Steve Funge86591e2014-12-01 13:38:21 -0800758 bool reschedule =
759 ReportZram(base::FilePath(FILE_PATH_LITERAL("/sys/block/zram0"))) &&
Luigi Semenzato96360192014-06-04 10:53:35 -0700760 success;
Steve Funge86591e2014-12-01 13:38:21 -0800761 if (reschedule) {
762 base::MessageLoop::current()->PostDelayedTask(FROM_HERE,
763 base::Bind(&MetricsDaemon::MeminfoCallback, base::Unretained(this),
Steve Fung8ab89c52015-01-05 13:48:30 -0800764 wait),
Steve Funge86591e2014-12-01 13:38:21 -0800765 wait);
766 }
Luigi Semenzato96360192014-06-04 10:53:35 -0700767}
768
769// static
770bool MetricsDaemon::ReadFileToUint64(const base::FilePath& path,
Ben Chanf05ab402014-08-07 00:54:59 -0700771 uint64_t* value) {
Luigi Semenzato96360192014-06-04 10:53:35 -0700772 std::string content;
773 if (!base::ReadFileToString(path, &content)) {
774 PLOG(WARNING) << "cannot read " << path.MaybeAsASCII();
775 return false;
776 }
Luigi Semenzato4a6c9422014-06-30 18:12:28 -0700777 // Remove final newline.
778 base::TrimWhitespaceASCII(content, base::TRIM_TRAILING, &content);
Luigi Semenzato96360192014-06-04 10:53:35 -0700779 if (!base::StringToUint64(content, value)) {
780 LOG(WARNING) << "invalid integer: " << content;
781 return false;
782 }
783 return true;
784}
785
786bool MetricsDaemon::ReportZram(const base::FilePath& zram_dir) {
787 // Data sizes are in bytes. |zero_pages| is in number of pages.
Ben Chanf05ab402014-08-07 00:54:59 -0700788 uint64_t compr_data_size, orig_data_size, zero_pages;
Luigi Semenzato96360192014-06-04 10:53:35 -0700789 const size_t page_size = 4096;
790
791 if (!ReadFileToUint64(zram_dir.Append(kComprDataSizeName),
792 &compr_data_size) ||
793 !ReadFileToUint64(zram_dir.Append(kOrigDataSizeName), &orig_data_size) ||
794 !ReadFileToUint64(zram_dir.Append(kZeroPagesName), &zero_pages)) {
795 return false;
796 }
797
798 // |orig_data_size| does not include zero-filled pages.
799 orig_data_size += zero_pages * page_size;
800
801 const int compr_data_size_mb = compr_data_size >> 20;
802 const int savings_mb = (orig_data_size - compr_data_size) >> 20;
803 const int zero_ratio_percent = zero_pages * page_size * 100 / orig_data_size;
804
805 // Report compressed size in megabytes. 100 MB or less has little impact.
806 SendSample("Platform.ZramCompressedSize", compr_data_size_mb, 100, 4000, 50);
807 SendSample("Platform.ZramSavings", savings_mb, 100, 4000, 50);
808 // The compression ratio is multiplied by 100 for better resolution. The
809 // ratios of interest are between 1 and 6 (100% and 600% as reported). We
810 // don't want samples when very little memory is being compressed.
811 if (compr_data_size_mb >= 1) {
812 SendSample("Platform.ZramCompressionRatioPercent",
813 orig_data_size * 100 / compr_data_size, 100, 600, 50);
814 }
815 // The values of interest for zero_pages are between 1MB and 1GB. The units
816 // are number of pages.
817 SendSample("Platform.ZramZeroPages", zero_pages, 256, 256 * 1024, 50);
818 SendSample("Platform.ZramZeroRatioPercent", zero_ratio_percent, 1, 50, 50);
819
820 return true;
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700821}
822
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700823bool MetricsDaemon::ProcessMeminfo(const string& meminfo_raw) {
Luigi Semenzato8accd332011-05-17 16:37:18 -0700824 static const MeminfoRecord fields_array[] = {
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700825 { "MemTotal", "MemTotal" }, // SPECIAL CASE: total system memory
826 { "MemFree", "MemFree" },
827 { "Buffers", "Buffers" },
828 { "Cached", "Cached" },
829 // { "SwapCached", "SwapCached" },
830 { "Active", "Active" },
831 { "Inactive", "Inactive" },
832 { "ActiveAnon", "Active(anon)" },
833 { "InactiveAnon", "Inactive(anon)" },
834 { "ActiveFile" , "Active(file)" },
835 { "InactiveFile", "Inactive(file)" },
Luigi Semenzato942cbab2013-02-12 13:17:07 -0800836 { "Unevictable", "Unevictable", kMeminfoOp_HistLog },
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700837 // { "Mlocked", "Mlocked" },
Luigi Semenzato942cbab2013-02-12 13:17:07 -0800838 { "SwapTotal", "SwapTotal", kMeminfoOp_SwapTotal },
839 { "SwapFree", "SwapFree", kMeminfoOp_SwapFree },
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700840 // { "Dirty", "Dirty" },
841 // { "Writeback", "Writeback" },
842 { "AnonPages", "AnonPages" },
843 { "Mapped", "Mapped" },
Luigi Semenzato942cbab2013-02-12 13:17:07 -0800844 { "Shmem", "Shmem", kMeminfoOp_HistLog },
845 { "Slab", "Slab", kMeminfoOp_HistLog },
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700846 // { "SReclaimable", "SReclaimable" },
847 // { "SUnreclaim", "SUnreclaim" },
848 };
Luigi Semenzato8accd332011-05-17 16:37:18 -0700849 vector<MeminfoRecord> fields(fields_array,
850 fields_array + arraysize(fields_array));
851 if (!FillMeminfo(meminfo_raw, &fields)) {
852 return false;
853 }
854 int total_memory = fields[0].value;
855 if (total_memory == 0) {
856 // this "cannot happen"
857 LOG(WARNING) << "borked meminfo parser";
858 return false;
859 }
Luigi Semenzato942cbab2013-02-12 13:17:07 -0800860 int swap_total = 0;
861 int swap_free = 0;
Luigi Semenzato8accd332011-05-17 16:37:18 -0700862 // Send all fields retrieved, except total memory.
863 for (unsigned int i = 1; i < fields.size(); i++) {
Luigi Semenzato859b3f02014-02-05 15:33:19 -0800864 string metrics_name = base::StringPrintf("Platform.Meminfo%s",
865 fields[i].name);
Luigi Semenzato3ccca062013-02-04 19:50:45 -0800866 int percent;
Luigi Semenzato942cbab2013-02-12 13:17:07 -0800867 switch (fields[i].op) {
868 case kMeminfoOp_HistPercent:
Luigi Semenzato3ccca062013-02-04 19:50:45 -0800869 // report value as percent of total memory
870 percent = fields[i].value * 100 / total_memory;
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800871 SendLinearSample(metrics_name, percent, 100, 101);
Luigi Semenzato3ccca062013-02-04 19:50:45 -0800872 break;
Luigi Semenzato942cbab2013-02-12 13:17:07 -0800873 case kMeminfoOp_HistLog:
Luigi Semenzato3ccca062013-02-04 19:50:45 -0800874 // report value in kbytes, log scale, 4Gb max
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800875 SendSample(metrics_name, fields[i].value, 1, 4 * 1000 * 1000, 100);
Luigi Semenzato3ccca062013-02-04 19:50:45 -0800876 break;
Luigi Semenzato942cbab2013-02-12 13:17:07 -0800877 case kMeminfoOp_SwapTotal:
878 swap_total = fields[i].value;
879 case kMeminfoOp_SwapFree:
880 swap_free = fields[i].value;
Luigi Semenzato3ccca062013-02-04 19:50:45 -0800881 break;
Luigi Semenzato8accd332011-05-17 16:37:18 -0700882 }
883 }
Luigi Semenzato942cbab2013-02-12 13:17:07 -0800884 if (swap_total > 0) {
885 int swap_used = swap_total - swap_free;
886 int swap_used_percent = swap_used * 100 / swap_total;
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800887 SendSample("Platform.MeminfoSwapUsed", swap_used, 1, 8 * 1000 * 1000, 100);
888 SendLinearSample("Platform.MeminfoSwapUsedPercent", swap_used_percent,
Luigi Semenzato942cbab2013-02-12 13:17:07 -0800889 100, 101);
890 }
Luigi Semenzato8accd332011-05-17 16:37:18 -0700891 return true;
892}
893
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700894bool MetricsDaemon::FillMeminfo(const string& meminfo_raw,
895 vector<MeminfoRecord>* fields) {
Luigi Semenzato8accd332011-05-17 16:37:18 -0700896 vector<string> lines;
897 unsigned int nlines = Tokenize(meminfo_raw, "\n", &lines);
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700898
899 // Scan meminfo output and collect field values. Each field name has to
900 // match a meminfo entry (case insensitive) after removing non-alpha
901 // characters from the entry.
Luigi Semenzato8accd332011-05-17 16:37:18 -0700902 unsigned int ifield = 0;
903 for (unsigned int iline = 0;
904 iline < nlines && ifield < fields->size();
905 iline++) {
906 vector<string> tokens;
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700907 Tokenize(lines[iline], ": ", &tokens);
Luigi Semenzato8accd332011-05-17 16:37:18 -0700908 if (strcmp((*fields)[ifield].match, tokens[0].c_str()) == 0) {
909 // Name matches. Parse value and save.
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700910 char* rest;
Luigi Semenzato8accd332011-05-17 16:37:18 -0700911 (*fields)[ifield].value =
912 static_cast<int>(strtol(tokens[1].c_str(), &rest, 10));
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700913 if (*rest != '\0') {
914 LOG(WARNING) << "missing meminfo value";
915 return false;
916 }
Luigi Semenzato8accd332011-05-17 16:37:18 -0700917 ifield++;
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700918 }
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700919 }
Luigi Semenzato8accd332011-05-17 16:37:18 -0700920 if (ifield < fields->size()) {
921 // End of input reached while scanning.
922 LOG(WARNING) << "cannot find field " << (*fields)[ifield].match
923 << " and following";
924 return false;
925 }
926 return true;
927}
928
Luigi Semenzato0d9a9c92013-12-05 15:55:12 -0800929void MetricsDaemon::ScheduleMemuseCallback(double interval) {
Luigi Semenzato8accd332011-05-17 16:37:18 -0700930 if (testing_) {
931 return;
932 }
Steve Funge86591e2014-12-01 13:38:21 -0800933 base::MessageLoop::current()->PostDelayedTask(FROM_HERE,
934 base::Bind(&MetricsDaemon::MemuseCallback, base::Unretained(this)),
935 base::TimeDelta::FromSeconds(interval));
Luigi Semenzato8accd332011-05-17 16:37:18 -0700936}
937
938void MetricsDaemon::MemuseCallback() {
939 // Since we only care about active time (i.e. uptime minus sleep time) but
940 // the callbacks are driven by real time (uptime), we check if we should
941 // reschedule this callback due to intervening sleep periods.
942 double now = GetActiveTime();
Luigi Semenzato0d9a9c92013-12-05 15:55:12 -0800943 // Avoid intervals of less than one second.
944 double remaining_time = ceil(memuse_final_time_ - now);
945 if (remaining_time > 0) {
946 ScheduleMemuseCallback(remaining_time);
Luigi Semenzato8accd332011-05-17 16:37:18 -0700947 } else {
Luigi Semenzato0d9a9c92013-12-05 15:55:12 -0800948 // Report stats and advance the measurement interval unless there are
949 // errors or we've completed the last interval.
Luigi Semenzato8accd332011-05-17 16:37:18 -0700950 if (MemuseCallbackWork() &&
Luigi Semenzato0d9a9c92013-12-05 15:55:12 -0800951 memuse_interval_index_ < arraysize(kMemuseIntervals)) {
952 double interval = kMemuseIntervals[memuse_interval_index_++];
953 memuse_final_time_ = now + interval;
954 ScheduleMemuseCallback(interval);
Luigi Semenzato8accd332011-05-17 16:37:18 -0700955 }
956 }
957}
958
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700959bool MetricsDaemon::MemuseCallbackWork() {
Luigi Semenzato8accd332011-05-17 16:37:18 -0700960 string meminfo_raw;
961 const FilePath meminfo_path("/proc/meminfo");
Ben Chan2e6543d2014-02-05 23:26:25 -0800962 if (!base::ReadFileToString(meminfo_path, &meminfo_raw)) {
Luigi Semenzato8accd332011-05-17 16:37:18 -0700963 LOG(WARNING) << "cannot read " << meminfo_path.value().c_str();
964 return false;
965 }
966 return ProcessMemuse(meminfo_raw);
967}
968
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700969bool MetricsDaemon::ProcessMemuse(const string& meminfo_raw) {
Luigi Semenzato8accd332011-05-17 16:37:18 -0700970 static const MeminfoRecord fields_array[] = {
971 { "MemTotal", "MemTotal" }, // SPECIAL CASE: total system memory
972 { "ActiveAnon", "Active(anon)" },
973 { "InactiveAnon", "Inactive(anon)" },
974 };
975 vector<MeminfoRecord> fields(fields_array,
976 fields_array + arraysize(fields_array));
977 if (!FillMeminfo(meminfo_raw, &fields)) {
978 return false;
979 }
980 int total = fields[0].value;
981 int active_anon = fields[1].value;
982 int inactive_anon = fields[2].value;
983 if (total == 0) {
984 // this "cannot happen"
985 LOG(WARNING) << "borked meminfo parser";
986 return false;
987 }
Luigi Semenzato859b3f02014-02-05 15:33:19 -0800988 string metrics_name = base::StringPrintf("Platform.MemuseAnon%d",
989 memuse_interval_index_);
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800990 SendLinearSample(metrics_name, (active_anon + inactive_anon) * 100 / total,
Luigi Semenzato8accd332011-05-17 16:37:18 -0700991 100, 101);
992 return true;
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700993}
994
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800995void MetricsDaemon::SendSample(const string& name, int sample,
Darin Petkov11b8eb32010-05-18 11:00:59 -0700996 int min, int max, int nbuckets) {
Darin Petkovfc91b422010-05-12 13:05:45 -0700997 metrics_lib_->SendToUMA(name, sample, min, max, nbuckets);
Darin Petkov65b01462010-04-14 13:32:20 -0700998}
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700999
Luigi Semenzatoe5883fa2014-04-18 17:00:35 -07001000void MetricsDaemon::SendKernelCrashesCumulativeCountStats() {
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -08001001 // Report the number of crashes for this OS version, but don't clear the
1002 // counter. It is cleared elsewhere on version change.
Ben Chanf05ab402014-08-07 00:54:59 -07001003 int64_t crashes_count = kernel_crashes_version_count_->Get();
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -08001004 SendSample(kernel_crashes_version_count_->Name(),
Luigi Semenzatoba0c65d2014-03-17 12:28:38 -07001005 crashes_count,
1006 1, // value of first bucket
1007 500, // value of last bucket
1008 100); // number of buckets
1009
1010
Ben Chanf05ab402014-08-07 00:54:59 -07001011 int64_t cpu_use_ms = version_cumulative_cpu_use_->Get();
Luigi Semenzatoba0c65d2014-03-17 12:28:38 -07001012 SendSample(version_cumulative_cpu_use_->Name(),
1013 cpu_use_ms / 1000, // stat is in seconds
1014 1, // device may be used very little...
1015 8 * 1000 * 1000, // ... or a lot (a little over 90 days)
1016 100);
1017
1018 // On the first run after an autoupdate, cpu_use_ms and active_use_seconds
1019 // can be zero. Avoid division by zero.
1020 if (cpu_use_ms > 0) {
1021 // Send the crash frequency since update in number of crashes per CPU year.
1022 SendSample("Logging.KernelCrashesPerCpuYear",
1023 crashes_count * kSecondsPerDay * 365 * 1000 / cpu_use_ms,
1024 1,
1025 1000 * 1000, // about one crash every 30s of CPU time
1026 100);
1027 }
1028
Ben Chanf05ab402014-08-07 00:54:59 -07001029 int64_t active_use_seconds = version_cumulative_active_use_->Get();
Luigi Semenzatoba0c65d2014-03-17 12:28:38 -07001030 if (active_use_seconds > 0) {
Luigi Semenzatoe5883fa2014-04-18 17:00:35 -07001031 SendSample(version_cumulative_active_use_->Name(),
1032 active_use_seconds / 1000, // stat is in seconds
1033 1, // device may be used very little...
1034 8 * 1000 * 1000, // ... or a lot (about 90 days)
1035 100);
Luigi Semenzatoba0c65d2014-03-17 12:28:38 -07001036 // Same as above, but per year of active time.
1037 SendSample("Logging.KernelCrashesPerActiveYear",
1038 crashes_count * kSecondsPerDay * 365 / active_use_seconds,
1039 1,
1040 1000 * 1000, // about one crash every 30s of active time
1041 100);
1042 }
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -08001043}
1044
Luigi Semenzatoe5883fa2014-04-18 17:00:35 -07001045void MetricsDaemon::SendDailyUseSample(
1046 const scoped_ptr<PersistentInteger>& use) {
1047 SendSample(use->Name(),
1048 use->GetAndClear(),
1049 1, // value of first bucket
1050 kSecondsPerDay, // value of last bucket
1051 50); // number of buckets
1052}
1053
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -08001054void MetricsDaemon::SendCrashIntervalSample(
1055 const scoped_ptr<PersistentInteger>& interval) {
1056 SendSample(interval->Name(),
1057 interval->GetAndClear(),
1058 1, // value of first bucket
1059 4 * kSecondsPerWeek, // value of last bucket
1060 50); // number of buckets
1061}
1062
1063void MetricsDaemon::SendCrashFrequencySample(
1064 const scoped_ptr<PersistentInteger>& frequency) {
1065 SendSample(frequency->Name(),
1066 frequency->GetAndClear(),
1067 1, // value of first bucket
1068 100, // value of last bucket
1069 50); // number of buckets
1070}
1071
1072void MetricsDaemon::SendLinearSample(const string& name, int sample,
Luigi Semenzato29c7ef92011-04-12 14:12:35 -07001073 int max, int nbuckets) {
Luigi Semenzato29c7ef92011-04-12 14:12:35 -07001074 // TODO(semenzato): add a proper linear histogram to the Chrome external
1075 // metrics API.
1076 LOG_IF(FATAL, nbuckets != max + 1) << "unsupported histogram scale";
1077 metrics_lib_->SendEnumToUMA(name, sample, max);
1078}
Daniel Eratc83975a2014-04-04 08:53:44 -07001079
1080void MetricsDaemon::UpdateStats(TimeTicks now_ticks,
1081 Time now_wall_time) {
1082 const int elapsed_seconds = (now_ticks - last_update_stats_time_).InSeconds();
Luigi Semenzatoe5883fa2014-04-18 17:00:35 -07001083 daily_active_use_->Add(elapsed_seconds);
1084 version_cumulative_active_use_->Add(elapsed_seconds);
Daniel Eratc83975a2014-04-04 08:53:44 -07001085 user_crash_interval_->Add(elapsed_seconds);
1086 kernel_crash_interval_->Add(elapsed_seconds);
1087 version_cumulative_cpu_use_->Add(GetIncrementalCpuUse().InMilliseconds());
1088 last_update_stats_time_ = now_ticks;
1089
1090 const TimeDelta since_epoch = now_wall_time - Time::UnixEpoch();
1091 const int day = since_epoch.InDays();
1092 const int week = day / 7;
1093
1094 if (daily_cycle_->Get() != day) {
1095 daily_cycle_->Set(day);
Luigi Semenzatoe5883fa2014-04-18 17:00:35 -07001096 SendDailyUseSample(daily_active_use_);
1097 SendDailyUseSample(version_cumulative_active_use_);
Daniel Eratc83975a2014-04-04 08:53:44 -07001098 SendCrashFrequencySample(any_crashes_daily_count_);
1099 SendCrashFrequencySample(user_crashes_daily_count_);
1100 SendCrashFrequencySample(kernel_crashes_daily_count_);
1101 SendCrashFrequencySample(unclean_shutdowns_daily_count_);
Luigi Semenzatoe5883fa2014-04-18 17:00:35 -07001102 SendKernelCrashesCumulativeCountStats();
Daniel Eratc83975a2014-04-04 08:53:44 -07001103 }
1104
1105 if (weekly_cycle_->Get() != week) {
1106 weekly_cycle_->Set(week);
1107 SendCrashFrequencySample(any_crashes_weekly_count_);
1108 SendCrashFrequencySample(user_crashes_weekly_count_);
1109 SendCrashFrequencySample(kernel_crashes_weekly_count_);
1110 SendCrashFrequencySample(unclean_shutdowns_weekly_count_);
1111 }
1112}
1113
Steve Funge86591e2014-12-01 13:38:21 -08001114void MetricsDaemon::HandleUpdateStatsTimeout() {
1115 UpdateStats(TimeTicks::Now(), Time::Now());
1116 base::MessageLoop::current()->PostDelayedTask(FROM_HERE,
1117 base::Bind(&MetricsDaemon::HandleUpdateStatsTimeout,
1118 base::Unretained(this)),
1119 base::TimeDelta::FromMilliseconds(kUpdateStatsIntervalMs));
Daniel Eratc83975a2014-04-04 08:53:44 -07001120}