Darin Petkov | 8032dd0 | 2011-05-09 16:33:19 -0700 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Luigi Semenzato | 4a6c942 | 2014-06-30 18:12:28 -0700 | [diff] [blame] | 5 | // For PRIu64 in inttypes.h, used by scanf. TODO(semenzato): replace |
| 6 | // with libchromeos methods. |
| 7 | #define __STDC_FORMAT_MACROS |
| 8 | |
| 9 | #include "metrics/metrics_daemon.h" |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 10 | |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 11 | #include <fcntl.h> |
Luigi Semenzato | 4a6c942 | 2014-06-30 18:12:28 -0700 | [diff] [blame] | 12 | #include <inttypes.h> |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 13 | #include <math.h> |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 14 | #include <string.h> |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 15 | #include <time.h> |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 16 | |
Luigi Semenzato | 859b3f0 | 2014-02-05 15:33:19 -0800 | [diff] [blame] | 17 | #include <base/files/file_path.h> |
Ben Chan | 51bf92a | 2014-09-05 08:21:06 -0700 | [diff] [blame] | 18 | #include <base/files/file_util.h> |
Luigi Semenzato | 859b3f0 | 2014-02-05 15:33:19 -0800 | [diff] [blame] | 19 | #include <base/hash.h> |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 20 | #include <base/logging.h> |
Ben Chan | 2e6543d | 2014-02-05 23:26:25 -0800 | [diff] [blame] | 21 | #include <base/strings/string_number_conversions.h> |
| 22 | #include <base/strings/string_split.h> |
| 23 | #include <base/strings/string_util.h> |
| 24 | #include <base/strings/stringprintf.h> |
Luigi Semenzato | 859b3f0 | 2014-02-05 15:33:19 -0800 | [diff] [blame] | 25 | #include <base/sys_info.h> |
Darin Petkov | 40f2573 | 2013-04-29 15:07:31 +0200 | [diff] [blame] | 26 | #include <chromeos/dbus/service_constants.h> |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 27 | #include <dbus/dbus-glib-lowlevel.h> |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 28 | #include "uploader/upload_service.h" |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 29 | |
Ben Chan | 2e6543d | 2014-02-05 23:26:25 -0800 | [diff] [blame] | 30 | using base::FilePath; |
| 31 | using base::StringPrintf; |
Darin Petkov | f27f036 | 2010-06-04 13:14:19 -0700 | [diff] [blame] | 32 | using base::Time; |
| 33 | using base::TimeDelta; |
| 34 | using base::TimeTicks; |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 35 | using chromeos_metrics::PersistentInteger; |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 36 | using std::map; |
Darin Petkov | 38d5cb0 | 2010-06-24 12:10:26 -0700 | [diff] [blame] | 37 | using std::string; |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 38 | using std::vector; |
| 39 | |
Daniel Erat | c83975a | 2014-04-04 08:53:44 -0700 | [diff] [blame] | 40 | namespace { |
Darin Petkov | f27f036 | 2010-06-04 13:14:19 -0700 | [diff] [blame] | 41 | |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 42 | #define SAFE_MESSAGE(e) (e.message ? e.message : "unknown error") |
Darin Petkov | 40f2573 | 2013-04-29 15:07:31 +0200 | [diff] [blame] | 43 | |
Daniel Erat | c83975a | 2014-04-04 08:53:44 -0700 | [diff] [blame] | 44 | const char kCrashReporterInterface[] = "org.chromium.CrashReporter"; |
| 45 | const char kCrashReporterUserCrashSignal[] = "UserCrash"; |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 46 | |
Daniel Erat | c83975a | 2014-04-04 08:53:44 -0700 | [diff] [blame] | 47 | const int kSecondsPerMinute = 60; |
| 48 | const int kMinutesPerHour = 60; |
| 49 | const int kHoursPerDay = 24; |
| 50 | const int kMinutesPerDay = kHoursPerDay * kMinutesPerHour; |
| 51 | const int kSecondsPerDay = kSecondsPerMinute * kMinutesPerDay; |
| 52 | const int kDaysPerWeek = 7; |
| 53 | const int kSecondsPerWeek = kSecondsPerDay * kDaysPerWeek; |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 54 | |
Daniel Erat | c83975a | 2014-04-04 08:53:44 -0700 | [diff] [blame] | 55 | // Interval between calls to UpdateStats(). |
| 56 | const guint kUpdateStatsIntervalMs = 300000; |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 57 | |
Luigi Semenzato | c5a9234 | 2014-02-14 15:05:51 -0800 | [diff] [blame] | 58 | const char kKernelCrashDetectedFile[] = "/var/run/kernel-crash-detected"; |
Daniel Erat | c83975a | 2014-04-04 08:53:44 -0700 | [diff] [blame] | 59 | const char kUncleanShutdownDetectedFile[] = |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 60 | "/var/run/unclean-shutdown-detected"; |
Ken Mixter | ccd84c0 | 2010-08-16 19:57:13 -0700 | [diff] [blame] | 61 | |
Daniel Erat | c83975a | 2014-04-04 08:53:44 -0700 | [diff] [blame] | 62 | } // namespace |
| 63 | |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 64 | // disk stats metrics |
| 65 | |
| 66 | // The {Read,Write}Sectors numbers are in sectors/second. |
| 67 | // A sector is usually 512 bytes. |
| 68 | |
| 69 | const char MetricsDaemon::kMetricReadSectorsLongName[] = |
| 70 | "Platform.ReadSectorsLong"; |
| 71 | const char MetricsDaemon::kMetricWriteSectorsLongName[] = |
| 72 | "Platform.WriteSectorsLong"; |
| 73 | const char MetricsDaemon::kMetricReadSectorsShortName[] = |
| 74 | "Platform.ReadSectorsShort"; |
| 75 | const char MetricsDaemon::kMetricWriteSectorsShortName[] = |
| 76 | "Platform.WriteSectorsShort"; |
| 77 | |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 78 | const int MetricsDaemon::kMetricStatsShortInterval = 1; // seconds |
| 79 | const int MetricsDaemon::kMetricStatsLongInterval = 30; // seconds |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 80 | |
Luigi Semenzato | 29c7ef9 | 2011-04-12 14:12:35 -0700 | [diff] [blame] | 81 | const int MetricsDaemon::kMetricMeminfoInterval = 30; // seconds |
| 82 | |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 83 | // Assume a max rate of 250Mb/s for reads (worse for writes) and 512 byte |
| 84 | // sectors. |
| 85 | const int MetricsDaemon::kMetricSectorsIOMax = 500000; // sectors/second |
| 86 | const int MetricsDaemon::kMetricSectorsBuckets = 50; // buckets |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 87 | // Page size is 4k, sector size is 0.5k. We're not interested in page fault |
| 88 | // rates that the disk cannot sustain. |
| 89 | const int MetricsDaemon::kMetricPageFaultsMax = kMetricSectorsIOMax / 8; |
| 90 | const int MetricsDaemon::kMetricPageFaultsBuckets = 50; |
| 91 | |
| 92 | // Major page faults, i.e. the ones that require data to be read from disk. |
| 93 | |
| 94 | const char MetricsDaemon::kMetricPageFaultsLongName[] = |
| 95 | "Platform.PageFaultsLong"; |
| 96 | const char MetricsDaemon::kMetricPageFaultsShortName[] = |
| 97 | "Platform.PageFaultsShort"; |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 98 | |
Sonny Rao | 4b8aebb | 2013-07-31 23:18:31 -0700 | [diff] [blame] | 99 | // Swap in and Swap out |
| 100 | |
| 101 | const char MetricsDaemon::kMetricSwapInLongName[] = |
| 102 | "Platform.SwapInLong"; |
| 103 | const char MetricsDaemon::kMetricSwapInShortName[] = |
| 104 | "Platform.SwapInShort"; |
| 105 | |
| 106 | const char MetricsDaemon::kMetricSwapOutLongName[] = |
| 107 | "Platform.SwapOutLong"; |
| 108 | const char MetricsDaemon::kMetricSwapOutShortName[] = |
| 109 | "Platform.SwapOutShort"; |
| 110 | |
Luigi Semenzato | ba0c65d | 2014-03-17 12:28:38 -0700 | [diff] [blame] | 111 | const char MetricsDaemon::kMetricsProcStatFileName[] = "/proc/stat"; |
| 112 | const int MetricsDaemon::kMetricsProcStatFirstLineItemsCount = 11; |
| 113 | |
Luigi Semenzato | fb3a821 | 2013-05-07 16:55:00 -0700 | [diff] [blame] | 114 | // Thermal CPU throttling. |
| 115 | |
| 116 | const char MetricsDaemon::kMetricScaledCpuFrequencyName[] = |
| 117 | "Platform.CpuFrequencyThermalScaling"; |
| 118 | |
Luigi Semenzato | 9636019 | 2014-06-04 10:53:35 -0700 | [diff] [blame] | 119 | // Zram sysfs entries. |
| 120 | |
| 121 | const char MetricsDaemon::kComprDataSizeName[] = "compr_data_size"; |
| 122 | const char MetricsDaemon::kOrigDataSizeName[] = "orig_data_size"; |
| 123 | const char MetricsDaemon::kZeroPagesName[] = "zero_pages"; |
| 124 | |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 125 | // Memory use stats collection intervals. We collect some memory use interval |
| 126 | // at these intervals after boot, and we stop collecting after the last one, |
| 127 | // with the assumption that in most cases the memory use won't change much |
| 128 | // after that. |
| 129 | static const int kMemuseIntervals[] = { |
| 130 | 1 * kSecondsPerMinute, // 1 minute mark |
| 131 | 4 * kSecondsPerMinute, // 5 minute mark |
| 132 | 25 * kSecondsPerMinute, // 0.5 hour mark |
| 133 | 120 * kSecondsPerMinute, // 2.5 hour mark |
| 134 | 600 * kSecondsPerMinute, // 12.5 hour mark |
| 135 | }; |
| 136 | |
Darin Petkov | f1e85e4 | 2010-06-10 15:59:53 -0700 | [diff] [blame] | 137 | MetricsDaemon::MetricsDaemon() |
Daniel Erat | c83975a | 2014-04-04 08:53:44 -0700 | [diff] [blame] | 138 | : update_stats_timeout_id_(-1), |
Luigi Semenzato | 0d9a9c9 | 2013-12-05 15:55:12 -0800 | [diff] [blame] | 139 | memuse_final_time_(0), |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 140 | memuse_interval_index_(0), |
| 141 | read_sectors_(0), |
| 142 | write_sectors_(0), |
Sonny Rao | 4b8aebb | 2013-07-31 23:18:31 -0700 | [diff] [blame] | 143 | vmstats_(), |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 144 | stats_state_(kStatsShort), |
Luigi Semenzato | ba0c65d | 2014-03-17 12:28:38 -0700 | [diff] [blame] | 145 | stats_initial_time_(0), |
| 146 | ticks_per_second_(0), |
| 147 | latest_cpu_use_ticks_(0) {} |
Darin Petkov | f1e85e4 | 2010-06-10 15:59:53 -0700 | [diff] [blame] | 148 | |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 149 | MetricsDaemon::~MetricsDaemon() { |
Daniel Erat | c83975a | 2014-04-04 08:53:44 -0700 | [diff] [blame] | 150 | if (update_stats_timeout_id_ > -1) |
| 151 | g_source_remove(update_stats_timeout_id_); |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 154 | double MetricsDaemon::GetActiveTime() { |
| 155 | struct timespec ts; |
| 156 | int r = clock_gettime(CLOCK_MONOTONIC, &ts); |
| 157 | if (r < 0) { |
| 158 | PLOG(WARNING) << "clock_gettime(CLOCK_MONOTONIC) failed"; |
| 159 | return 0; |
| 160 | } else { |
Luigi Semenzato | 4a6c942 | 2014-06-30 18:12:28 -0700 | [diff] [blame] | 161 | return ts.tv_sec + static_cast<double>(ts.tv_nsec) / (1000 * 1000 * 1000); |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | |
Darin Petkov | 2ccef01 | 2010-05-05 16:06:37 -0700 | [diff] [blame] | 165 | void MetricsDaemon::Run(bool run_as_daemon) { |
Darin Petkov | 38d5cb0 | 2010-06-24 12:10:26 -0700 | [diff] [blame] | 166 | if (run_as_daemon && daemon(0, 0) != 0) |
| 167 | return; |
| 168 | |
Ken Mixter | ccd84c0 | 2010-08-16 19:57:13 -0700 | [diff] [blame] | 169 | if (CheckSystemCrash(kKernelCrashDetectedFile)) { |
| 170 | ProcessKernelCrash(); |
| 171 | } |
| 172 | |
| 173 | if (CheckSystemCrash(kUncleanShutdownDetectedFile)) { |
| 174 | ProcessUncleanShutdown(); |
| 175 | } |
| 176 | |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 177 | // On OS version change, clear version stats (which are reported daily). |
Ben Chan | f05ab40 | 2014-08-07 00:54:59 -0700 | [diff] [blame] | 178 | int32_t version = GetOsVersionHash(); |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 179 | if (version_cycle_->Get() != version) { |
| 180 | version_cycle_->Set(version); |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 181 | kernel_crashes_version_count_->Set(0); |
Luigi Semenzato | e5883fa | 2014-04-18 17:00:35 -0700 | [diff] [blame] | 182 | version_cumulative_active_use_->Set(0); |
Luigi Semenzato | ba0c65d | 2014-03-17 12:28:38 -0700 | [diff] [blame] | 183 | version_cumulative_cpu_use_->Set(0); |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 184 | } |
| 185 | |
Darin Petkov | 38d5cb0 | 2010-06-24 12:10:26 -0700 | [diff] [blame] | 186 | Loop(); |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 189 | void MetricsDaemon::RunUploaderTest() { |
Bertrand SIMONNET | 71a62ef | 2014-10-07 11:26:25 -0700 | [diff] [blame] | 190 | upload_service_.reset(new UploadService(new SystemProfileCache(true, |
| 191 | config_root_), |
| 192 | server_)); |
Bertrand SIMONNET | cac74e1 | 2014-10-09 10:14:13 -0700 | [diff] [blame^] | 193 | upload_service_->Init(upload_interval_, metrics_file_); |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 194 | upload_service_->UploadEvent(); |
| 195 | } |
| 196 | |
Ben Chan | f05ab40 | 2014-08-07 00:54:59 -0700 | [diff] [blame] | 197 | uint32_t MetricsDaemon::GetOsVersionHash() { |
| 198 | static uint32_t cached_version_hash = 0; |
Luigi Semenzato | 859b3f0 | 2014-02-05 15:33:19 -0800 | [diff] [blame] | 199 | static bool version_hash_is_cached = false; |
| 200 | if (version_hash_is_cached) |
| 201 | return cached_version_hash; |
| 202 | version_hash_is_cached = true; |
| 203 | std::string version; |
| 204 | if (base::SysInfo::GetLsbReleaseValue("CHROMEOS_RELEASE_VERSION", &version)) { |
| 205 | cached_version_hash = base::Hash(version); |
| 206 | } else if (testing_) { |
| 207 | cached_version_hash = 42; // return any plausible value for the hash |
| 208 | } else { |
| 209 | LOG(FATAL) << "could not find CHROMEOS_RELEASE_VERSION"; |
| 210 | } |
| 211 | return cached_version_hash; |
| 212 | } |
| 213 | |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 214 | void MetricsDaemon::Init(bool testing, |
| 215 | bool uploader_active, |
| 216 | MetricsLibraryInterface* metrics_lib, |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 217 | const string& diskstats_path, |
Luigi Semenzato | fb3a821 | 2013-05-07 16:55:00 -0700 | [diff] [blame] | 218 | const string& vmstats_path, |
| 219 | const string& scaling_max_freq_path, |
Steve Fung | 67906c6 | 2014-10-06 15:15:30 -0700 | [diff] [blame] | 220 | const string& cpuinfo_max_freq_path, |
Bertrand SIMONNET | cac74e1 | 2014-10-09 10:14:13 -0700 | [diff] [blame^] | 221 | const base::TimeDelta& upload_interval, |
Steve Fung | 67906c6 | 2014-10-06 15:15:30 -0700 | [diff] [blame] | 222 | const string& server, |
Bertrand SIMONNET | 71a62ef | 2014-10-07 11:26:25 -0700 | [diff] [blame] | 223 | const string& metrics_file, |
| 224 | const string& config_root) { |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 225 | testing_ = testing; |
Bertrand SIMONNET | 71a62ef | 2014-10-07 11:26:25 -0700 | [diff] [blame] | 226 | config_root_ = config_root; |
Alex Vakulenko | 1459503 | 2014-08-28 14:59:56 -0700 | [diff] [blame] | 227 | DCHECK(metrics_lib != nullptr); |
Darin Petkov | fc91b42 | 2010-05-12 13:05:45 -0700 | [diff] [blame] | 228 | metrics_lib_ = metrics_lib; |
Darin Petkov | 38d5cb0 | 2010-06-24 12:10:26 -0700 | [diff] [blame] | 229 | |
Bertrand SIMONNET | cac74e1 | 2014-10-09 10:14:13 -0700 | [diff] [blame^] | 230 | upload_interval_ = upload_interval; |
Steve Fung | 67906c6 | 2014-10-06 15:15:30 -0700 | [diff] [blame] | 231 | server_ = server; |
| 232 | metrics_file_ = metrics_file; |
| 233 | |
Luigi Semenzato | ba0c65d | 2014-03-17 12:28:38 -0700 | [diff] [blame] | 234 | // Get ticks per second (HZ) on this system. |
| 235 | // Sysconf cannot fail, so no sanity checks are needed. |
| 236 | ticks_per_second_ = sysconf(_SC_CLK_TCK); |
| 237 | |
Luigi Semenzato | e5883fa | 2014-04-18 17:00:35 -0700 | [diff] [blame] | 238 | daily_active_use_.reset( |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 239 | new PersistentInteger("Logging.DailyUseTime")); |
Luigi Semenzato | e5883fa | 2014-04-18 17:00:35 -0700 | [diff] [blame] | 240 | version_cumulative_active_use_.reset( |
| 241 | new PersistentInteger("Logging.CumulativeDailyUseTime")); |
Luigi Semenzato | ba0c65d | 2014-03-17 12:28:38 -0700 | [diff] [blame] | 242 | version_cumulative_cpu_use_.reset( |
| 243 | new PersistentInteger("Logging.CumulativeCpuTime")); |
Darin Petkov | 38d5cb0 | 2010-06-24 12:10:26 -0700 | [diff] [blame] | 244 | |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 245 | kernel_crash_interval_.reset( |
| 246 | new PersistentInteger("Logging.KernelCrashInterval")); |
| 247 | unclean_shutdown_interval_.reset( |
| 248 | new PersistentInteger("Logging.UncleanShutdownInterval")); |
| 249 | user_crash_interval_.reset( |
| 250 | new PersistentInteger("Logging.UserCrashInterval")); |
Darin Petkov | 2ccef01 | 2010-05-05 16:06:37 -0700 | [diff] [blame] | 251 | |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 252 | any_crashes_daily_count_.reset( |
| 253 | new PersistentInteger("Logging.AnyCrashesDaily")); |
| 254 | any_crashes_weekly_count_.reset( |
| 255 | new PersistentInteger("Logging.AnyCrashesWeekly")); |
| 256 | user_crashes_daily_count_.reset( |
| 257 | new PersistentInteger("Logging.UserCrashesDaily")); |
| 258 | user_crashes_weekly_count_.reset( |
| 259 | new PersistentInteger("Logging.UserCrashesWeekly")); |
| 260 | kernel_crashes_daily_count_.reset( |
| 261 | new PersistentInteger("Logging.KernelCrashesDaily")); |
| 262 | kernel_crashes_weekly_count_.reset( |
| 263 | new PersistentInteger("Logging.KernelCrashesWeekly")); |
| 264 | kernel_crashes_version_count_.reset( |
| 265 | new PersistentInteger("Logging.KernelCrashesSinceUpdate")); |
| 266 | unclean_shutdowns_daily_count_.reset( |
| 267 | new PersistentInteger("Logging.UncleanShutdownsDaily")); |
| 268 | unclean_shutdowns_weekly_count_.reset( |
| 269 | new PersistentInteger("Logging.UncleanShutdownsWeekly")); |
Darin Petkov | 38d5cb0 | 2010-06-24 12:10:26 -0700 | [diff] [blame] | 270 | |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 271 | daily_cycle_.reset(new PersistentInteger("daily.cycle")); |
| 272 | weekly_cycle_.reset(new PersistentInteger("weekly.cycle")); |
| 273 | version_cycle_.reset(new PersistentInteger("version.cycle")); |
Luigi Semenzato | 859b3f0 | 2014-02-05 15:33:19 -0800 | [diff] [blame] | 274 | |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 275 | diskstats_path_ = diskstats_path; |
| 276 | vmstats_path_ = vmstats_path; |
Luigi Semenzato | fb3a821 | 2013-05-07 16:55:00 -0700 | [diff] [blame] | 277 | scaling_max_freq_path_ = scaling_max_freq_path; |
| 278 | cpuinfo_max_freq_path_ = cpuinfo_max_freq_path; |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 279 | StatsReporterInit(); |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 280 | |
Luigi Semenzato | 29c7ef9 | 2011-04-12 14:12:35 -0700 | [diff] [blame] | 281 | // Start collecting meminfo stats. |
| 282 | ScheduleMeminfoCallback(kMetricMeminfoInterval); |
Luigi Semenzato | 0d9a9c9 | 2013-12-05 15:55:12 -0800 | [diff] [blame] | 283 | memuse_final_time_ = GetActiveTime() + kMemuseIntervals[0]; |
| 284 | ScheduleMemuseCallback(kMemuseIntervals[0]); |
Luigi Semenzato | 29c7ef9 | 2011-04-12 14:12:35 -0700 | [diff] [blame] | 285 | |
Darin Petkov | 2ccef01 | 2010-05-05 16:06:37 -0700 | [diff] [blame] | 286 | // Don't setup D-Bus and GLib in test mode. |
| 287 | if (testing) |
| 288 | return; |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 289 | |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 290 | g_type_init(); |
Ben Chan | 6f59842 | 2013-06-22 06:29:36 -0700 | [diff] [blame] | 291 | dbus_threads_init_default(); |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 292 | |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 293 | DBusError error; |
| 294 | dbus_error_init(&error); |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 295 | |
David James | 3b3add5 | 2010-06-04 15:01:19 -0700 | [diff] [blame] | 296 | DBusConnection* connection = dbus_bus_get(DBUS_BUS_SYSTEM, &error); |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 297 | LOG_IF(FATAL, dbus_error_is_set(&error)) << |
| 298 | "No D-Bus connection: " << SAFE_MESSAGE(error); |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 299 | |
Alex Vakulenko | 1459503 | 2014-08-28 14:59:56 -0700 | [diff] [blame] | 300 | dbus_connection_setup_with_g_main(connection, nullptr); |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 301 | |
Darin Petkov | 40f2573 | 2013-04-29 15:07:31 +0200 | [diff] [blame] | 302 | vector<string> matches; |
| 303 | matches.push_back( |
Luigi Semenzato | 859b3f0 | 2014-02-05 15:33:19 -0800 | [diff] [blame] | 304 | base::StringPrintf("type='signal',interface='%s',path='/',member='%s'", |
| 305 | kCrashReporterInterface, |
| 306 | kCrashReporterUserCrashSignal)); |
Darin Petkov | 40f2573 | 2013-04-29 15:07:31 +0200 | [diff] [blame] | 307 | |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 308 | // Registers D-Bus matches for the signals we would like to catch. |
Darin Petkov | 40f2573 | 2013-04-29 15:07:31 +0200 | [diff] [blame] | 309 | for (vector<string>::const_iterator it = matches.begin(); |
| 310 | it != matches.end(); ++it) { |
| 311 | const char* match = it->c_str(); |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 312 | DLOG(INFO) << "adding dbus match: " << match; |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 313 | dbus_bus_add_match(connection, match, &error); |
| 314 | LOG_IF(FATAL, dbus_error_is_set(&error)) << |
| 315 | "unable to add a match: " << SAFE_MESSAGE(error); |
| 316 | } |
| 317 | |
| 318 | // Adds the D-Bus filter routine to be called back whenever one of |
| 319 | // the registered D-Bus matches is successful. The daemon is not |
| 320 | // activated for D-Bus messages that don't match. |
Alex Vakulenko | 1459503 | 2014-08-28 14:59:56 -0700 | [diff] [blame] | 321 | CHECK(dbus_connection_add_filter(connection, MessageFilter, this, nullptr)); |
Daniel Erat | c83975a | 2014-04-04 08:53:44 -0700 | [diff] [blame] | 322 | |
| 323 | update_stats_timeout_id_ = |
| 324 | g_timeout_add(kUpdateStatsIntervalMs, &HandleUpdateStatsTimeout, this); |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 325 | |
| 326 | if (uploader_active) { |
Nathan Bullock | 0be0f73 | 2014-09-11 10:29:17 -0400 | [diff] [blame] | 327 | LOG(INFO) << "uploader enabled"; |
Bertrand SIMONNET | 71a62ef | 2014-10-07 11:26:25 -0700 | [diff] [blame] | 328 | upload_service_.reset(new UploadService(new SystemProfileCache(), server_)); |
Bertrand SIMONNET | cac74e1 | 2014-10-09 10:14:13 -0700 | [diff] [blame^] | 329 | upload_service_->Init(upload_interval_, metrics_file_); |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 330 | } |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | void MetricsDaemon::Loop() { |
Alex Vakulenko | 1459503 | 2014-08-28 14:59:56 -0700 | [diff] [blame] | 334 | GMainLoop* loop = g_main_loop_new(nullptr, false); |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 335 | g_main_loop_run(loop); |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 336 | } |
| 337 | |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 338 | // static |
| 339 | DBusHandlerResult MetricsDaemon::MessageFilter(DBusConnection* connection, |
| 340 | DBusMessage* message, |
| 341 | void* user_data) { |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 342 | int message_type = dbus_message_get_type(message); |
| 343 | if (message_type != DBUS_MESSAGE_TYPE_SIGNAL) { |
Darin Petkov | 41e0623 | 2010-05-03 16:45:37 -0700 | [diff] [blame] | 344 | DLOG(WARNING) << "unexpected message type " << message_type; |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 345 | return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
| 346 | } |
| 347 | |
| 348 | // Signal messages always have interfaces. |
Daniel Erat | c83975a | 2014-04-04 08:53:44 -0700 | [diff] [blame] | 349 | const std::string interface(dbus_message_get_interface(message)); |
| 350 | const std::string member(dbus_message_get_member(message)); |
| 351 | DLOG(INFO) << "Got " << interface << "." << member << " D-Bus signal"; |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 352 | |
| 353 | MetricsDaemon* daemon = static_cast<MetricsDaemon*>(user_data); |
| 354 | |
| 355 | DBusMessageIter iter; |
| 356 | dbus_message_iter_init(message, &iter); |
Daniel Erat | c83975a | 2014-04-04 08:53:44 -0700 | [diff] [blame] | 357 | if (interface == kCrashReporterInterface) { |
| 358 | CHECK_EQ(member, kCrashReporterUserCrashSignal); |
Darin Petkov | 1bb904e | 2010-06-16 15:58:06 -0700 | [diff] [blame] | 359 | daemon->ProcessUserCrash(); |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 360 | } else { |
Daniel Erat | c83975a | 2014-04-04 08:53:44 -0700 | [diff] [blame] | 361 | // Ignore messages from the bus itself. |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame] | 362 | return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
| 363 | } |
| 364 | |
| 365 | return DBUS_HANDLER_RESULT_HANDLED; |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 366 | } |
| 367 | |
Luigi Semenzato | ba0c65d | 2014-03-17 12:28:38 -0700 | [diff] [blame] | 368 | // One might argue that parts of this should go into |
| 369 | // chromium/src/base/sys_info_chromeos.c instead, but put it here for now. |
| 370 | |
| 371 | TimeDelta MetricsDaemon::GetIncrementalCpuUse() { |
Luigi Semenzato | ba0c65d | 2014-03-17 12:28:38 -0700 | [diff] [blame] | 372 | FilePath proc_stat_path = FilePath(kMetricsProcStatFileName); |
| 373 | std::string proc_stat_string; |
| 374 | if (!base::ReadFileToString(proc_stat_path, &proc_stat_string)) { |
| 375 | LOG(WARNING) << "cannot open " << kMetricsProcStatFileName; |
| 376 | return TimeDelta(); |
| 377 | } |
| 378 | |
| 379 | std::vector<std::string> proc_stat_lines; |
| 380 | base::SplitString(proc_stat_string, '\n', &proc_stat_lines); |
| 381 | if (proc_stat_lines.empty()) { |
| 382 | LOG(WARNING) << "cannot parse " << kMetricsProcStatFileName |
| 383 | << ": " << proc_stat_string; |
| 384 | return TimeDelta(); |
| 385 | } |
| 386 | std::vector<std::string> proc_stat_totals; |
| 387 | base::SplitStringAlongWhitespace(proc_stat_lines[0], &proc_stat_totals); |
| 388 | |
Ben Chan | f05ab40 | 2014-08-07 00:54:59 -0700 | [diff] [blame] | 389 | uint64_t user_ticks, user_nice_ticks, system_ticks; |
Luigi Semenzato | ba0c65d | 2014-03-17 12:28:38 -0700 | [diff] [blame] | 390 | if (proc_stat_totals.size() != kMetricsProcStatFirstLineItemsCount || |
| 391 | proc_stat_totals[0] != "cpu" || |
| 392 | !base::StringToUint64(proc_stat_totals[1], &user_ticks) || |
| 393 | !base::StringToUint64(proc_stat_totals[2], &user_nice_ticks) || |
| 394 | !base::StringToUint64(proc_stat_totals[3], &system_ticks)) { |
| 395 | LOG(WARNING) << "cannot parse first line: " << proc_stat_lines[0]; |
| 396 | return TimeDelta(base::TimeDelta::FromSeconds(0)); |
| 397 | } |
| 398 | |
Ben Chan | f05ab40 | 2014-08-07 00:54:59 -0700 | [diff] [blame] | 399 | uint64_t total_cpu_use_ticks = user_ticks + user_nice_ticks + system_ticks; |
Luigi Semenzato | ba0c65d | 2014-03-17 12:28:38 -0700 | [diff] [blame] | 400 | |
| 401 | // Sanity check. |
| 402 | if (total_cpu_use_ticks < latest_cpu_use_ticks_) { |
| 403 | LOG(WARNING) << "CPU time decreasing from " << latest_cpu_use_ticks_ |
| 404 | << " to " << total_cpu_use_ticks; |
| 405 | return TimeDelta(); |
| 406 | } |
| 407 | |
Ben Chan | f05ab40 | 2014-08-07 00:54:59 -0700 | [diff] [blame] | 408 | uint64_t diff = total_cpu_use_ticks - latest_cpu_use_ticks_; |
Luigi Semenzato | ba0c65d | 2014-03-17 12:28:38 -0700 | [diff] [blame] | 409 | latest_cpu_use_ticks_ = total_cpu_use_ticks; |
| 410 | // Use microseconds to avoid significant truncations. |
| 411 | return base::TimeDelta::FromMicroseconds( |
| 412 | diff * 1000 * 1000 / ticks_per_second_); |
| 413 | } |
| 414 | |
Darin Petkov | 1bb904e | 2010-06-16 15:58:06 -0700 | [diff] [blame] | 415 | void MetricsDaemon::ProcessUserCrash() { |
Daniel Erat | c83975a | 2014-04-04 08:53:44 -0700 | [diff] [blame] | 416 | // Counts the active time up to now. |
| 417 | UpdateStats(TimeTicks::Now(), Time::Now()); |
Darin Petkov | 1bb904e | 2010-06-16 15:58:06 -0700 | [diff] [blame] | 418 | |
| 419 | // Reports the active use time since the last crash and resets it. |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 420 | SendCrashIntervalSample(user_crash_interval_); |
Ken Mixter | ccd84c0 | 2010-08-16 19:57:13 -0700 | [diff] [blame] | 421 | |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 422 | any_crashes_daily_count_->Add(1); |
| 423 | any_crashes_weekly_count_->Add(1); |
| 424 | user_crashes_daily_count_->Add(1); |
| 425 | user_crashes_weekly_count_->Add(1); |
Darin Petkov | 1bb904e | 2010-06-16 15:58:06 -0700 | [diff] [blame] | 426 | } |
| 427 | |
Darin Petkov | 38d5cb0 | 2010-06-24 12:10:26 -0700 | [diff] [blame] | 428 | void MetricsDaemon::ProcessKernelCrash() { |
Daniel Erat | c83975a | 2014-04-04 08:53:44 -0700 | [diff] [blame] | 429 | // Counts the active time up to now. |
| 430 | UpdateStats(TimeTicks::Now(), Time::Now()); |
Darin Petkov | 38d5cb0 | 2010-06-24 12:10:26 -0700 | [diff] [blame] | 431 | |
| 432 | // Reports the active use time since the last crash and resets it. |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 433 | SendCrashIntervalSample(kernel_crash_interval_); |
Ken Mixter | ccd84c0 | 2010-08-16 19:57:13 -0700 | [diff] [blame] | 434 | |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 435 | any_crashes_daily_count_->Add(1); |
| 436 | any_crashes_weekly_count_->Add(1); |
| 437 | kernel_crashes_daily_count_->Add(1); |
| 438 | kernel_crashes_weekly_count_->Add(1); |
Luigi Semenzato | 859b3f0 | 2014-02-05 15:33:19 -0800 | [diff] [blame] | 439 | |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 440 | kernel_crashes_version_count_->Add(1); |
Darin Petkov | 38d5cb0 | 2010-06-24 12:10:26 -0700 | [diff] [blame] | 441 | } |
| 442 | |
Ken Mixter | ccd84c0 | 2010-08-16 19:57:13 -0700 | [diff] [blame] | 443 | void MetricsDaemon::ProcessUncleanShutdown() { |
Daniel Erat | c83975a | 2014-04-04 08:53:44 -0700 | [diff] [blame] | 444 | // Counts the active time up to now. |
| 445 | UpdateStats(TimeTicks::Now(), Time::Now()); |
Ken Mixter | ccd84c0 | 2010-08-16 19:57:13 -0700 | [diff] [blame] | 446 | |
| 447 | // Reports the active use time since the last crash and resets it. |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 448 | SendCrashIntervalSample(unclean_shutdown_interval_); |
Ken Mixter | ccd84c0 | 2010-08-16 19:57:13 -0700 | [diff] [blame] | 449 | |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 450 | unclean_shutdowns_daily_count_->Add(1); |
| 451 | unclean_shutdowns_weekly_count_->Add(1); |
| 452 | any_crashes_daily_count_->Add(1); |
| 453 | any_crashes_weekly_count_->Add(1); |
Ken Mixter | ccd84c0 | 2010-08-16 19:57:13 -0700 | [diff] [blame] | 454 | } |
| 455 | |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 456 | bool MetricsDaemon::CheckSystemCrash(const string& crash_file) { |
Darin Petkov | 38d5cb0 | 2010-06-24 12:10:26 -0700 | [diff] [blame] | 457 | FilePath crash_detected(crash_file); |
Ben Chan | 2e6543d | 2014-02-05 23:26:25 -0800 | [diff] [blame] | 458 | if (!base::PathExists(crash_detected)) |
Ken Mixter | ccd84c0 | 2010-08-16 19:57:13 -0700 | [diff] [blame] | 459 | return false; |
Darin Petkov | 38d5cb0 | 2010-06-24 12:10:26 -0700 | [diff] [blame] | 460 | |
| 461 | // Deletes the crash-detected file so that the daemon doesn't report |
| 462 | // another kernel crash in case it's restarted. |
Luigi Semenzato | 859b3f0 | 2014-02-05 15:33:19 -0800 | [diff] [blame] | 463 | base::DeleteFile(crash_detected, false); // not recursive |
Ken Mixter | ccd84c0 | 2010-08-16 19:57:13 -0700 | [diff] [blame] | 464 | return true; |
Darin Petkov | 38d5cb0 | 2010-06-24 12:10:26 -0700 | [diff] [blame] | 465 | } |
| 466 | |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 467 | void MetricsDaemon::StatsReporterInit() { |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 468 | DiskStatsReadStats(&read_sectors_, &write_sectors_); |
Sonny Rao | 4b8aebb | 2013-07-31 23:18:31 -0700 | [diff] [blame] | 469 | VmStatsReadStats(&vmstats_); |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 470 | // The first time around just run the long stat, so we don't delay boot. |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 471 | stats_state_ = kStatsLong; |
| 472 | stats_initial_time_ = GetActiveTime(); |
| 473 | if (stats_initial_time_ < 0) { |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 474 | LOG(WARNING) << "not collecting disk stats"; |
| 475 | } else { |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 476 | ScheduleStatsCallback(kMetricStatsLongInterval); |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 477 | } |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 478 | } |
| 479 | |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 480 | void MetricsDaemon::ScheduleStatsCallback(int wait) { |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 481 | if (testing_) { |
| 482 | return; |
| 483 | } |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 484 | g_timeout_add_seconds(wait, StatsCallbackStatic, this); |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 485 | } |
| 486 | |
Ben Chan | f05ab40 | 2014-08-07 00:54:59 -0700 | [diff] [blame] | 487 | bool MetricsDaemon::DiskStatsReadStats(uint64_t* read_sectors, |
| 488 | uint64_t* write_sectors) { |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 489 | int nchars; |
| 490 | int nitems; |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 491 | bool success = false; |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 492 | char line[200]; |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 493 | if (diskstats_path_.empty()) { |
| 494 | return false; |
| 495 | } |
Luigi Semenzato | 0f132bb | 2011-02-28 11:17:43 -0800 | [diff] [blame] | 496 | int file = HANDLE_EINTR(open(diskstats_path_.c_str(), O_RDONLY)); |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 497 | if (file < 0) { |
| 498 | PLOG(WARNING) << "cannot open " << diskstats_path_; |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 499 | return false; |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 500 | } |
| 501 | nchars = HANDLE_EINTR(read(file, line, sizeof(line))); |
| 502 | if (nchars < 0) { |
| 503 | PLOG(WARNING) << "cannot read from " << diskstats_path_; |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 504 | return false; |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 505 | } else { |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 506 | LOG_IF(WARNING, nchars == sizeof(line)) |
| 507 | << "line too long in " << diskstats_path_; |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 508 | line[nchars] = '\0'; |
Nathan Bullock | dc1ef3c | 2014-09-17 09:24:10 -0400 | [diff] [blame] | 509 | nitems = sscanf(line, "%*d %*d %" PRIu64 " %*d %*d %*d %" PRIu64, |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 510 | read_sectors, write_sectors); |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 511 | if (nitems == 2) { |
| 512 | success = true; |
| 513 | } else { |
| 514 | LOG(WARNING) << "found " << nitems << " items in " |
| 515 | << diskstats_path_ << ", expected 2"; |
| 516 | } |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 517 | } |
Mike Frysinger | 3e8a851 | 2014-05-14 16:14:37 -0400 | [diff] [blame] | 518 | IGNORE_EINTR(close(file)); |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 519 | return success; |
| 520 | } |
| 521 | |
Sonny Rao | 4b8aebb | 2013-07-31 23:18:31 -0700 | [diff] [blame] | 522 | bool MetricsDaemon::VmStatsParseStats(const char* stats, |
| 523 | struct VmstatRecord* record) { |
| 524 | // a mapping of string name to field in VmstatRecord and whether we found it |
| 525 | struct mapping { |
| 526 | const string name; |
| 527 | uint64_t* value_p; |
| 528 | bool found; |
| 529 | } map[] = |
| 530 | { { .name = "pgmajfault", |
| 531 | .value_p = &record->page_faults_, |
| 532 | .found = false }, |
| 533 | { .name = "pswpin", |
| 534 | .value_p = &record->swap_in_, |
| 535 | .found = false }, |
| 536 | { .name = "pswpout", |
| 537 | .value_p = &record->swap_out_, |
| 538 | .found = false }, }; |
| 539 | |
Luigi Semenzato | fb3a821 | 2013-05-07 16:55:00 -0700 | [diff] [blame] | 540 | // Each line in the file has the form |
| 541 | // <ID> <VALUE> |
| 542 | // for instance: |
| 543 | // nr_free_pages 213427 |
Sonny Rao | 4b8aebb | 2013-07-31 23:18:31 -0700 | [diff] [blame] | 544 | vector<string> lines; |
| 545 | Tokenize(stats, "\n", &lines); |
| 546 | for (vector<string>::iterator it = lines.begin(); |
| 547 | it != lines.end(); ++it) { |
| 548 | vector<string> tokens; |
| 549 | base::SplitString(*it, ' ', &tokens); |
| 550 | if (tokens.size() == 2) { |
| 551 | for (unsigned int i = 0; i < sizeof(map)/sizeof(struct mapping); i++) { |
| 552 | if (!tokens[0].compare(map[i].name)) { |
| 553 | if (!base::StringToUint64(tokens[1], map[i].value_p)) |
| 554 | return false; |
| 555 | map[i].found = true; |
| 556 | } |
| 557 | } |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 558 | } else { |
Sonny Rao | 4b8aebb | 2013-07-31 23:18:31 -0700 | [diff] [blame] | 559 | LOG(WARNING) << "unexpected vmstat format"; |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 560 | } |
| 561 | } |
Sonny Rao | 4b8aebb | 2013-07-31 23:18:31 -0700 | [diff] [blame] | 562 | // make sure we got all the stats |
| 563 | for (unsigned i = 0; i < sizeof(map)/sizeof(struct mapping); i++) { |
| 564 | if (map[i].found == false) { |
| 565 | LOG(WARNING) << "vmstat missing " << map[i].name; |
| 566 | return false; |
| 567 | } |
| 568 | } |
| 569 | return true; |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 570 | } |
| 571 | |
Sonny Rao | 4b8aebb | 2013-07-31 23:18:31 -0700 | [diff] [blame] | 572 | bool MetricsDaemon::VmStatsReadStats(struct VmstatRecord* stats) { |
| 573 | string value_string; |
| 574 | FilePath* path = new FilePath(vmstats_path_); |
Ben Chan | 2e6543d | 2014-02-05 23:26:25 -0800 | [diff] [blame] | 575 | if (!base::ReadFileToString(*path, &value_string)) { |
Sonny Rao | 4b8aebb | 2013-07-31 23:18:31 -0700 | [diff] [blame] | 576 | delete path; |
| 577 | LOG(WARNING) << "cannot read " << vmstats_path_; |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 578 | return false; |
| 579 | } |
Sonny Rao | 4b8aebb | 2013-07-31 23:18:31 -0700 | [diff] [blame] | 580 | delete path; |
| 581 | return VmStatsParseStats(value_string.c_str(), stats); |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 582 | } |
| 583 | |
Luigi Semenzato | fb3a821 | 2013-05-07 16:55:00 -0700 | [diff] [blame] | 584 | bool MetricsDaemon::ReadFreqToInt(const string& sysfs_file_name, int* value) { |
Luigi Semenzato | d92d18c | 2013-06-04 13:24:21 -0700 | [diff] [blame] | 585 | const FilePath sysfs_path(sysfs_file_name); |
Luigi Semenzato | fb3a821 | 2013-05-07 16:55:00 -0700 | [diff] [blame] | 586 | string value_string; |
Ben Chan | 2e6543d | 2014-02-05 23:26:25 -0800 | [diff] [blame] | 587 | if (!base::ReadFileToString(sysfs_path, &value_string)) { |
Luigi Semenzato | fb3a821 | 2013-05-07 16:55:00 -0700 | [diff] [blame] | 588 | LOG(WARNING) << "cannot read " << sysfs_path.value().c_str(); |
| 589 | return false; |
| 590 | } |
Ben Chan | 2e6543d | 2014-02-05 23:26:25 -0800 | [diff] [blame] | 591 | if (!base::RemoveChars(value_string, "\n", &value_string)) { |
Luigi Semenzato | fb3a821 | 2013-05-07 16:55:00 -0700 | [diff] [blame] | 592 | LOG(WARNING) << "no newline in " << value_string; |
| 593 | // Continue even though the lack of newline is suspicious. |
| 594 | } |
| 595 | if (!base::StringToInt(value_string, value)) { |
| 596 | LOG(WARNING) << "cannot convert " << value_string << " to int"; |
| 597 | return false; |
| 598 | } |
| 599 | return true; |
| 600 | } |
| 601 | |
| 602 | void MetricsDaemon::SendCpuThrottleMetrics() { |
| 603 | // |max_freq| is 0 only the first time through. |
| 604 | static int max_freq = 0; |
| 605 | if (max_freq == -1) |
| 606 | // Give up, as sysfs did not report max_freq correctly. |
| 607 | return; |
| 608 | if (max_freq == 0 || testing_) { |
| 609 | // One-time initialization of max_freq. (Every time when testing.) |
| 610 | if (!ReadFreqToInt(cpuinfo_max_freq_path_, &max_freq)) { |
| 611 | max_freq = -1; |
| 612 | return; |
| 613 | } |
| 614 | if (max_freq == 0) { |
| 615 | LOG(WARNING) << "sysfs reports 0 max CPU frequency\n"; |
| 616 | max_freq = -1; |
| 617 | return; |
| 618 | } |
| 619 | if (max_freq % 10000 == 1000) { |
| 620 | // Special case: system has turbo mode, and max non-turbo frequency is |
| 621 | // max_freq - 1000. This relies on "normal" (non-turbo) frequencies |
| 622 | // being multiples of (at least) 10 MHz. Although there is no guarantee |
| 623 | // of this, it seems a fairly reasonable assumption. Otherwise we should |
| 624 | // read scaling_available_frequencies, sort the frequencies, compare the |
| 625 | // two highest ones, and check if they differ by 1000 (kHz) (and that's a |
| 626 | // hack too, no telling when it will change). |
| 627 | max_freq -= 1000; |
| 628 | } |
| 629 | } |
| 630 | int scaled_freq = 0; |
| 631 | if (!ReadFreqToInt(scaling_max_freq_path_, &scaled_freq)) |
| 632 | return; |
| 633 | // Frequencies are in kHz. If scaled_freq > max_freq, turbo is on, but |
| 634 | // scaled_freq is not the actual turbo frequency. We indicate this situation |
| 635 | // with a 101% value. |
| 636 | int percent = scaled_freq > max_freq ? 101 : scaled_freq / (max_freq / 100); |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 637 | SendLinearSample(kMetricScaledCpuFrequencyName, percent, 101, 102); |
Luigi Semenzato | fb3a821 | 2013-05-07 16:55:00 -0700 | [diff] [blame] | 638 | } |
| 639 | |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 640 | // static |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 641 | gboolean MetricsDaemon::StatsCallbackStatic(void* handle) { |
| 642 | (static_cast<MetricsDaemon*>(handle))->StatsCallback(); |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 643 | return false; // one-time callback |
| 644 | } |
| 645 | |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 646 | // Collects disk and vm stats alternating over a short and a long interval. |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 647 | |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 648 | void MetricsDaemon::StatsCallback() { |
Ben Chan | f05ab40 | 2014-08-07 00:54:59 -0700 | [diff] [blame] | 649 | uint64_t read_sectors_now, write_sectors_now; |
Sonny Rao | 4b8aebb | 2013-07-31 23:18:31 -0700 | [diff] [blame] | 650 | struct VmstatRecord vmstats_now; |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 651 | double time_now = GetActiveTime(); |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 652 | double delta_time = time_now - stats_initial_time_; |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 653 | if (testing_) { |
| 654 | // Fake the time when testing. |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 655 | delta_time = stats_state_ == kStatsShort ? |
| 656 | kMetricStatsShortInterval : kMetricStatsLongInterval; |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 657 | } |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 658 | bool diskstats_success = DiskStatsReadStats(&read_sectors_now, |
| 659 | &write_sectors_now); |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 660 | int delta_read = read_sectors_now - read_sectors_; |
| 661 | int delta_write = write_sectors_now - write_sectors_; |
| 662 | int read_sectors_per_second = delta_read / delta_time; |
| 663 | int write_sectors_per_second = delta_write / delta_time; |
Sonny Rao | 4b8aebb | 2013-07-31 23:18:31 -0700 | [diff] [blame] | 664 | bool vmstats_success = VmStatsReadStats(&vmstats_now); |
| 665 | uint64_t delta_faults = vmstats_now.page_faults_ - vmstats_.page_faults_; |
| 666 | uint64_t delta_swap_in = vmstats_now.swap_in_ - vmstats_.swap_in_; |
| 667 | uint64_t delta_swap_out = vmstats_now.swap_out_ - vmstats_.swap_out_; |
| 668 | uint64_t page_faults_per_second = delta_faults / delta_time; |
| 669 | uint64_t swap_in_per_second = delta_swap_in / delta_time; |
| 670 | uint64_t swap_out_per_second = delta_swap_out / delta_time; |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 671 | |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 672 | switch (stats_state_) { |
| 673 | case kStatsShort: |
| 674 | if (diskstats_success) { |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 675 | SendSample(kMetricReadSectorsShortName, |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 676 | read_sectors_per_second, |
| 677 | 1, |
| 678 | kMetricSectorsIOMax, |
| 679 | kMetricSectorsBuckets); |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 680 | SendSample(kMetricWriteSectorsShortName, |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 681 | write_sectors_per_second, |
| 682 | 1, |
| 683 | kMetricSectorsIOMax, |
| 684 | kMetricSectorsBuckets); |
| 685 | } |
| 686 | if (vmstats_success) { |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 687 | SendSample(kMetricPageFaultsShortName, |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 688 | page_faults_per_second, |
| 689 | 1, |
| 690 | kMetricPageFaultsMax, |
| 691 | kMetricPageFaultsBuckets); |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 692 | SendSample(kMetricSwapInShortName, |
Sonny Rao | 4b8aebb | 2013-07-31 23:18:31 -0700 | [diff] [blame] | 693 | swap_in_per_second, |
| 694 | 1, |
| 695 | kMetricPageFaultsMax, |
| 696 | kMetricPageFaultsBuckets); |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 697 | SendSample(kMetricSwapOutShortName, |
Sonny Rao | 4b8aebb | 2013-07-31 23:18:31 -0700 | [diff] [blame] | 698 | swap_out_per_second, |
| 699 | 1, |
| 700 | kMetricPageFaultsMax, |
| 701 | kMetricPageFaultsBuckets); |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 702 | } |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 703 | // Schedule long callback. |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 704 | stats_state_ = kStatsLong; |
| 705 | ScheduleStatsCallback(kMetricStatsLongInterval - |
| 706 | kMetricStatsShortInterval); |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 707 | break; |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 708 | case kStatsLong: |
| 709 | if (diskstats_success) { |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 710 | SendSample(kMetricReadSectorsLongName, |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 711 | read_sectors_per_second, |
| 712 | 1, |
| 713 | kMetricSectorsIOMax, |
| 714 | kMetricSectorsBuckets); |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 715 | SendSample(kMetricWriteSectorsLongName, |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 716 | write_sectors_per_second, |
| 717 | 1, |
| 718 | kMetricSectorsIOMax, |
| 719 | kMetricSectorsBuckets); |
| 720 | // Reset sector counters. |
| 721 | read_sectors_ = read_sectors_now; |
| 722 | write_sectors_ = write_sectors_now; |
| 723 | } |
| 724 | if (vmstats_success) { |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 725 | SendSample(kMetricPageFaultsLongName, |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 726 | page_faults_per_second, |
| 727 | 1, |
| 728 | kMetricPageFaultsMax, |
| 729 | kMetricPageFaultsBuckets); |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 730 | SendSample(kMetricSwapInLongName, |
Sonny Rao | 4b8aebb | 2013-07-31 23:18:31 -0700 | [diff] [blame] | 731 | swap_in_per_second, |
| 732 | 1, |
| 733 | kMetricPageFaultsMax, |
| 734 | kMetricPageFaultsBuckets); |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 735 | SendSample(kMetricSwapOutLongName, |
Sonny Rao | 4b8aebb | 2013-07-31 23:18:31 -0700 | [diff] [blame] | 736 | swap_out_per_second, |
| 737 | 1, |
| 738 | kMetricPageFaultsMax, |
| 739 | kMetricPageFaultsBuckets); |
| 740 | |
| 741 | vmstats_ = vmstats_now; |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 742 | } |
Luigi Semenzato | fb3a821 | 2013-05-07 16:55:00 -0700 | [diff] [blame] | 743 | SendCpuThrottleMetrics(); |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 744 | // Set start time for new cycle. |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 745 | stats_initial_time_ = time_now; |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 746 | // Schedule short callback. |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 747 | stats_state_ = kStatsShort; |
| 748 | ScheduleStatsCallback(kMetricStatsShortInterval); |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 749 | break; |
| 750 | default: |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 751 | LOG(FATAL) << "Invalid stats state"; |
Luigi Semenzato | c88e42d | 2011-02-17 10:21:16 -0800 | [diff] [blame] | 752 | } |
| 753 | } |
| 754 | |
Luigi Semenzato | 29c7ef9 | 2011-04-12 14:12:35 -0700 | [diff] [blame] | 755 | void MetricsDaemon::ScheduleMeminfoCallback(int wait) { |
| 756 | if (testing_) { |
| 757 | return; |
| 758 | } |
| 759 | g_timeout_add_seconds(wait, MeminfoCallbackStatic, this); |
| 760 | } |
| 761 | |
| 762 | // static |
| 763 | gboolean MetricsDaemon::MeminfoCallbackStatic(void* handle) { |
| 764 | return (static_cast<MetricsDaemon*>(handle))->MeminfoCallback(); |
| 765 | } |
| 766 | |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 767 | bool MetricsDaemon::MeminfoCallback() { |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 768 | string meminfo_raw; |
Luigi Semenzato | 29c7ef9 | 2011-04-12 14:12:35 -0700 | [diff] [blame] | 769 | const FilePath meminfo_path("/proc/meminfo"); |
Ben Chan | 2e6543d | 2014-02-05 23:26:25 -0800 | [diff] [blame] | 770 | if (!base::ReadFileToString(meminfo_path, &meminfo_raw)) { |
Luigi Semenzato | 29c7ef9 | 2011-04-12 14:12:35 -0700 | [diff] [blame] | 771 | LOG(WARNING) << "cannot read " << meminfo_path.value().c_str(); |
| 772 | return false; |
| 773 | } |
Luigi Semenzato | 9636019 | 2014-06-04 10:53:35 -0700 | [diff] [blame] | 774 | // Make both calls even if the first one fails. |
| 775 | bool success = ProcessMeminfo(meminfo_raw); |
| 776 | return ReportZram(base::FilePath(FILE_PATH_LITERAL("/sys/block/zram0"))) && |
| 777 | success; |
| 778 | } |
| 779 | |
| 780 | // static |
| 781 | bool MetricsDaemon::ReadFileToUint64(const base::FilePath& path, |
Ben Chan | f05ab40 | 2014-08-07 00:54:59 -0700 | [diff] [blame] | 782 | uint64_t* value) { |
Luigi Semenzato | 9636019 | 2014-06-04 10:53:35 -0700 | [diff] [blame] | 783 | std::string content; |
| 784 | if (!base::ReadFileToString(path, &content)) { |
| 785 | PLOG(WARNING) << "cannot read " << path.MaybeAsASCII(); |
| 786 | return false; |
| 787 | } |
Luigi Semenzato | 4a6c942 | 2014-06-30 18:12:28 -0700 | [diff] [blame] | 788 | // Remove final newline. |
| 789 | base::TrimWhitespaceASCII(content, base::TRIM_TRAILING, &content); |
Luigi Semenzato | 9636019 | 2014-06-04 10:53:35 -0700 | [diff] [blame] | 790 | if (!base::StringToUint64(content, value)) { |
| 791 | LOG(WARNING) << "invalid integer: " << content; |
| 792 | return false; |
| 793 | } |
| 794 | return true; |
| 795 | } |
| 796 | |
| 797 | bool MetricsDaemon::ReportZram(const base::FilePath& zram_dir) { |
| 798 | // Data sizes are in bytes. |zero_pages| is in number of pages. |
Ben Chan | f05ab40 | 2014-08-07 00:54:59 -0700 | [diff] [blame] | 799 | uint64_t compr_data_size, orig_data_size, zero_pages; |
Luigi Semenzato | 9636019 | 2014-06-04 10:53:35 -0700 | [diff] [blame] | 800 | const size_t page_size = 4096; |
| 801 | |
| 802 | if (!ReadFileToUint64(zram_dir.Append(kComprDataSizeName), |
| 803 | &compr_data_size) || |
| 804 | !ReadFileToUint64(zram_dir.Append(kOrigDataSizeName), &orig_data_size) || |
| 805 | !ReadFileToUint64(zram_dir.Append(kZeroPagesName), &zero_pages)) { |
| 806 | return false; |
| 807 | } |
| 808 | |
| 809 | // |orig_data_size| does not include zero-filled pages. |
| 810 | orig_data_size += zero_pages * page_size; |
| 811 | |
| 812 | const int compr_data_size_mb = compr_data_size >> 20; |
| 813 | const int savings_mb = (orig_data_size - compr_data_size) >> 20; |
| 814 | const int zero_ratio_percent = zero_pages * page_size * 100 / orig_data_size; |
| 815 | |
| 816 | // Report compressed size in megabytes. 100 MB or less has little impact. |
| 817 | SendSample("Platform.ZramCompressedSize", compr_data_size_mb, 100, 4000, 50); |
| 818 | SendSample("Platform.ZramSavings", savings_mb, 100, 4000, 50); |
| 819 | // The compression ratio is multiplied by 100 for better resolution. The |
| 820 | // ratios of interest are between 1 and 6 (100% and 600% as reported). We |
| 821 | // don't want samples when very little memory is being compressed. |
| 822 | if (compr_data_size_mb >= 1) { |
| 823 | SendSample("Platform.ZramCompressionRatioPercent", |
| 824 | orig_data_size * 100 / compr_data_size, 100, 600, 50); |
| 825 | } |
| 826 | // The values of interest for zero_pages are between 1MB and 1GB. The units |
| 827 | // are number of pages. |
| 828 | SendSample("Platform.ZramZeroPages", zero_pages, 256, 256 * 1024, 50); |
| 829 | SendSample("Platform.ZramZeroRatioPercent", zero_ratio_percent, 1, 50, 50); |
| 830 | |
| 831 | return true; |
Luigi Semenzato | 29c7ef9 | 2011-04-12 14:12:35 -0700 | [diff] [blame] | 832 | } |
| 833 | |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 834 | bool MetricsDaemon::ProcessMeminfo(const string& meminfo_raw) { |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 835 | static const MeminfoRecord fields_array[] = { |
Luigi Semenzato | 29c7ef9 | 2011-04-12 14:12:35 -0700 | [diff] [blame] | 836 | { "MemTotal", "MemTotal" }, // SPECIAL CASE: total system memory |
| 837 | { "MemFree", "MemFree" }, |
| 838 | { "Buffers", "Buffers" }, |
| 839 | { "Cached", "Cached" }, |
| 840 | // { "SwapCached", "SwapCached" }, |
| 841 | { "Active", "Active" }, |
| 842 | { "Inactive", "Inactive" }, |
| 843 | { "ActiveAnon", "Active(anon)" }, |
| 844 | { "InactiveAnon", "Inactive(anon)" }, |
| 845 | { "ActiveFile" , "Active(file)" }, |
| 846 | { "InactiveFile", "Inactive(file)" }, |
Luigi Semenzato | 942cbab | 2013-02-12 13:17:07 -0800 | [diff] [blame] | 847 | { "Unevictable", "Unevictable", kMeminfoOp_HistLog }, |
Luigi Semenzato | 29c7ef9 | 2011-04-12 14:12:35 -0700 | [diff] [blame] | 848 | // { "Mlocked", "Mlocked" }, |
Luigi Semenzato | 942cbab | 2013-02-12 13:17:07 -0800 | [diff] [blame] | 849 | { "SwapTotal", "SwapTotal", kMeminfoOp_SwapTotal }, |
| 850 | { "SwapFree", "SwapFree", kMeminfoOp_SwapFree }, |
Luigi Semenzato | 29c7ef9 | 2011-04-12 14:12:35 -0700 | [diff] [blame] | 851 | // { "Dirty", "Dirty" }, |
| 852 | // { "Writeback", "Writeback" }, |
| 853 | { "AnonPages", "AnonPages" }, |
| 854 | { "Mapped", "Mapped" }, |
Luigi Semenzato | 942cbab | 2013-02-12 13:17:07 -0800 | [diff] [blame] | 855 | { "Shmem", "Shmem", kMeminfoOp_HistLog }, |
| 856 | { "Slab", "Slab", kMeminfoOp_HistLog }, |
Luigi Semenzato | 29c7ef9 | 2011-04-12 14:12:35 -0700 | [diff] [blame] | 857 | // { "SReclaimable", "SReclaimable" }, |
| 858 | // { "SUnreclaim", "SUnreclaim" }, |
| 859 | }; |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 860 | vector<MeminfoRecord> fields(fields_array, |
| 861 | fields_array + arraysize(fields_array)); |
| 862 | if (!FillMeminfo(meminfo_raw, &fields)) { |
| 863 | return false; |
| 864 | } |
| 865 | int total_memory = fields[0].value; |
| 866 | if (total_memory == 0) { |
| 867 | // this "cannot happen" |
| 868 | LOG(WARNING) << "borked meminfo parser"; |
| 869 | return false; |
| 870 | } |
Luigi Semenzato | 942cbab | 2013-02-12 13:17:07 -0800 | [diff] [blame] | 871 | int swap_total = 0; |
| 872 | int swap_free = 0; |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 873 | // Send all fields retrieved, except total memory. |
| 874 | for (unsigned int i = 1; i < fields.size(); i++) { |
Luigi Semenzato | 859b3f0 | 2014-02-05 15:33:19 -0800 | [diff] [blame] | 875 | string metrics_name = base::StringPrintf("Platform.Meminfo%s", |
| 876 | fields[i].name); |
Luigi Semenzato | 3ccca06 | 2013-02-04 19:50:45 -0800 | [diff] [blame] | 877 | int percent; |
Luigi Semenzato | 942cbab | 2013-02-12 13:17:07 -0800 | [diff] [blame] | 878 | switch (fields[i].op) { |
| 879 | case kMeminfoOp_HistPercent: |
Luigi Semenzato | 3ccca06 | 2013-02-04 19:50:45 -0800 | [diff] [blame] | 880 | // report value as percent of total memory |
| 881 | percent = fields[i].value * 100 / total_memory; |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 882 | SendLinearSample(metrics_name, percent, 100, 101); |
Luigi Semenzato | 3ccca06 | 2013-02-04 19:50:45 -0800 | [diff] [blame] | 883 | break; |
Luigi Semenzato | 942cbab | 2013-02-12 13:17:07 -0800 | [diff] [blame] | 884 | case kMeminfoOp_HistLog: |
Luigi Semenzato | 3ccca06 | 2013-02-04 19:50:45 -0800 | [diff] [blame] | 885 | // report value in kbytes, log scale, 4Gb max |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 886 | SendSample(metrics_name, fields[i].value, 1, 4 * 1000 * 1000, 100); |
Luigi Semenzato | 3ccca06 | 2013-02-04 19:50:45 -0800 | [diff] [blame] | 887 | break; |
Luigi Semenzato | 942cbab | 2013-02-12 13:17:07 -0800 | [diff] [blame] | 888 | case kMeminfoOp_SwapTotal: |
| 889 | swap_total = fields[i].value; |
| 890 | case kMeminfoOp_SwapFree: |
| 891 | swap_free = fields[i].value; |
Luigi Semenzato | 3ccca06 | 2013-02-04 19:50:45 -0800 | [diff] [blame] | 892 | break; |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 893 | } |
| 894 | } |
Luigi Semenzato | 942cbab | 2013-02-12 13:17:07 -0800 | [diff] [blame] | 895 | if (swap_total > 0) { |
| 896 | int swap_used = swap_total - swap_free; |
| 897 | int swap_used_percent = swap_used * 100 / swap_total; |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 898 | SendSample("Platform.MeminfoSwapUsed", swap_used, 1, 8 * 1000 * 1000, 100); |
| 899 | SendLinearSample("Platform.MeminfoSwapUsedPercent", swap_used_percent, |
Luigi Semenzato | 942cbab | 2013-02-12 13:17:07 -0800 | [diff] [blame] | 900 | 100, 101); |
| 901 | } |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 902 | return true; |
| 903 | } |
| 904 | |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 905 | bool MetricsDaemon::FillMeminfo(const string& meminfo_raw, |
| 906 | vector<MeminfoRecord>* fields) { |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 907 | vector<string> lines; |
| 908 | unsigned int nlines = Tokenize(meminfo_raw, "\n", &lines); |
Luigi Semenzato | 29c7ef9 | 2011-04-12 14:12:35 -0700 | [diff] [blame] | 909 | |
| 910 | // Scan meminfo output and collect field values. Each field name has to |
| 911 | // match a meminfo entry (case insensitive) after removing non-alpha |
| 912 | // characters from the entry. |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 913 | unsigned int ifield = 0; |
| 914 | for (unsigned int iline = 0; |
| 915 | iline < nlines && ifield < fields->size(); |
| 916 | iline++) { |
| 917 | vector<string> tokens; |
Luigi Semenzato | 29c7ef9 | 2011-04-12 14:12:35 -0700 | [diff] [blame] | 918 | Tokenize(lines[iline], ": ", &tokens); |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 919 | if (strcmp((*fields)[ifield].match, tokens[0].c_str()) == 0) { |
| 920 | // Name matches. Parse value and save. |
Luigi Semenzato | 29c7ef9 | 2011-04-12 14:12:35 -0700 | [diff] [blame] | 921 | char* rest; |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 922 | (*fields)[ifield].value = |
| 923 | static_cast<int>(strtol(tokens[1].c_str(), &rest, 10)); |
Luigi Semenzato | 29c7ef9 | 2011-04-12 14:12:35 -0700 | [diff] [blame] | 924 | if (*rest != '\0') { |
| 925 | LOG(WARNING) << "missing meminfo value"; |
| 926 | return false; |
| 927 | } |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 928 | ifield++; |
Luigi Semenzato | 29c7ef9 | 2011-04-12 14:12:35 -0700 | [diff] [blame] | 929 | } |
Luigi Semenzato | 29c7ef9 | 2011-04-12 14:12:35 -0700 | [diff] [blame] | 930 | } |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 931 | if (ifield < fields->size()) { |
| 932 | // End of input reached while scanning. |
| 933 | LOG(WARNING) << "cannot find field " << (*fields)[ifield].match |
| 934 | << " and following"; |
| 935 | return false; |
| 936 | } |
| 937 | return true; |
| 938 | } |
| 939 | |
Luigi Semenzato | 0d9a9c9 | 2013-12-05 15:55:12 -0800 | [diff] [blame] | 940 | void MetricsDaemon::ScheduleMemuseCallback(double interval) { |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 941 | if (testing_) { |
| 942 | return; |
| 943 | } |
Luigi Semenzato | 0d9a9c9 | 2013-12-05 15:55:12 -0800 | [diff] [blame] | 944 | g_timeout_add_seconds(interval, MemuseCallbackStatic, this); |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 945 | } |
| 946 | |
| 947 | // static |
| 948 | gboolean MetricsDaemon::MemuseCallbackStatic(void* handle) { |
| 949 | MetricsDaemon* daemon = static_cast<MetricsDaemon*>(handle); |
| 950 | daemon->MemuseCallback(); |
| 951 | return false; |
| 952 | } |
| 953 | |
| 954 | void MetricsDaemon::MemuseCallback() { |
| 955 | // Since we only care about active time (i.e. uptime minus sleep time) but |
| 956 | // the callbacks are driven by real time (uptime), we check if we should |
| 957 | // reschedule this callback due to intervening sleep periods. |
| 958 | double now = GetActiveTime(); |
Luigi Semenzato | 0d9a9c9 | 2013-12-05 15:55:12 -0800 | [diff] [blame] | 959 | // Avoid intervals of less than one second. |
| 960 | double remaining_time = ceil(memuse_final_time_ - now); |
| 961 | if (remaining_time > 0) { |
| 962 | ScheduleMemuseCallback(remaining_time); |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 963 | } else { |
Luigi Semenzato | 0d9a9c9 | 2013-12-05 15:55:12 -0800 | [diff] [blame] | 964 | // Report stats and advance the measurement interval unless there are |
| 965 | // errors or we've completed the last interval. |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 966 | if (MemuseCallbackWork() && |
Luigi Semenzato | 0d9a9c9 | 2013-12-05 15:55:12 -0800 | [diff] [blame] | 967 | memuse_interval_index_ < arraysize(kMemuseIntervals)) { |
| 968 | double interval = kMemuseIntervals[memuse_interval_index_++]; |
| 969 | memuse_final_time_ = now + interval; |
| 970 | ScheduleMemuseCallback(interval); |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 971 | } |
| 972 | } |
| 973 | } |
| 974 | |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 975 | bool MetricsDaemon::MemuseCallbackWork() { |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 976 | string meminfo_raw; |
| 977 | const FilePath meminfo_path("/proc/meminfo"); |
Ben Chan | 2e6543d | 2014-02-05 23:26:25 -0800 | [diff] [blame] | 978 | if (!base::ReadFileToString(meminfo_path, &meminfo_raw)) { |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 979 | LOG(WARNING) << "cannot read " << meminfo_path.value().c_str(); |
| 980 | return false; |
| 981 | } |
| 982 | return ProcessMemuse(meminfo_raw); |
| 983 | } |
| 984 | |
Luigi Semenzato | 5bd764f | 2011-10-14 12:03:35 -0700 | [diff] [blame] | 985 | bool MetricsDaemon::ProcessMemuse(const string& meminfo_raw) { |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 986 | static const MeminfoRecord fields_array[] = { |
| 987 | { "MemTotal", "MemTotal" }, // SPECIAL CASE: total system memory |
| 988 | { "ActiveAnon", "Active(anon)" }, |
| 989 | { "InactiveAnon", "Inactive(anon)" }, |
| 990 | }; |
| 991 | vector<MeminfoRecord> fields(fields_array, |
| 992 | fields_array + arraysize(fields_array)); |
| 993 | if (!FillMeminfo(meminfo_raw, &fields)) { |
| 994 | return false; |
| 995 | } |
| 996 | int total = fields[0].value; |
| 997 | int active_anon = fields[1].value; |
| 998 | int inactive_anon = fields[2].value; |
| 999 | if (total == 0) { |
| 1000 | // this "cannot happen" |
| 1001 | LOG(WARNING) << "borked meminfo parser"; |
| 1002 | return false; |
| 1003 | } |
Luigi Semenzato | 859b3f0 | 2014-02-05 15:33:19 -0800 | [diff] [blame] | 1004 | string metrics_name = base::StringPrintf("Platform.MemuseAnon%d", |
| 1005 | memuse_interval_index_); |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 1006 | SendLinearSample(metrics_name, (active_anon + inactive_anon) * 100 / total, |
Luigi Semenzato | 8accd33 | 2011-05-17 16:37:18 -0700 | [diff] [blame] | 1007 | 100, 101); |
| 1008 | return true; |
Luigi Semenzato | 29c7ef9 | 2011-04-12 14:12:35 -0700 | [diff] [blame] | 1009 | } |
| 1010 | |
Luigi Semenzato | 5ef2e39 | 2014-04-15 15:15:02 -0700 | [diff] [blame] | 1011 | void MetricsDaemon::ReportDailyUse(int use_seconds) { |
| 1012 | if (use_seconds <= 0) |
Darin Petkov | 1bb904e | 2010-06-16 15:58:06 -0700 | [diff] [blame] | 1013 | return; |
| 1014 | |
Luigi Semenzato | 5ef2e39 | 2014-04-15 15:15:02 -0700 | [diff] [blame] | 1015 | int minutes = (use_seconds + kSecondsPerMinute / 2) / kSecondsPerMinute; |
| 1016 | SendSample("Logging.DailyUseTime", |
| 1017 | minutes, |
| 1018 | 1, |
| 1019 | kMinutesPerDay * 30 * 2, // cumulative---two months worth |
| 1020 | 50); |
Darin Petkov | f1e85e4 | 2010-06-10 15:59:53 -0700 | [diff] [blame] | 1021 | } |
| 1022 | |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 1023 | void MetricsDaemon::SendSample(const string& name, int sample, |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 1024 | int min, int max, int nbuckets) { |
Darin Petkov | fc91b42 | 2010-05-12 13:05:45 -0700 | [diff] [blame] | 1025 | metrics_lib_->SendToUMA(name, sample, min, max, nbuckets); |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 1026 | } |
Luigi Semenzato | 29c7ef9 | 2011-04-12 14:12:35 -0700 | [diff] [blame] | 1027 | |
Luigi Semenzato | e5883fa | 2014-04-18 17:00:35 -0700 | [diff] [blame] | 1028 | void MetricsDaemon::SendKernelCrashesCumulativeCountStats() { |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 1029 | // Report the number of crashes for this OS version, but don't clear the |
| 1030 | // counter. It is cleared elsewhere on version change. |
Ben Chan | f05ab40 | 2014-08-07 00:54:59 -0700 | [diff] [blame] | 1031 | int64_t crashes_count = kernel_crashes_version_count_->Get(); |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 1032 | SendSample(kernel_crashes_version_count_->Name(), |
Luigi Semenzato | ba0c65d | 2014-03-17 12:28:38 -0700 | [diff] [blame] | 1033 | crashes_count, |
| 1034 | 1, // value of first bucket |
| 1035 | 500, // value of last bucket |
| 1036 | 100); // number of buckets |
| 1037 | |
| 1038 | |
Ben Chan | f05ab40 | 2014-08-07 00:54:59 -0700 | [diff] [blame] | 1039 | int64_t cpu_use_ms = version_cumulative_cpu_use_->Get(); |
Luigi Semenzato | ba0c65d | 2014-03-17 12:28:38 -0700 | [diff] [blame] | 1040 | SendSample(version_cumulative_cpu_use_->Name(), |
| 1041 | cpu_use_ms / 1000, // stat is in seconds |
| 1042 | 1, // device may be used very little... |
| 1043 | 8 * 1000 * 1000, // ... or a lot (a little over 90 days) |
| 1044 | 100); |
| 1045 | |
| 1046 | // On the first run after an autoupdate, cpu_use_ms and active_use_seconds |
| 1047 | // can be zero. Avoid division by zero. |
| 1048 | if (cpu_use_ms > 0) { |
| 1049 | // Send the crash frequency since update in number of crashes per CPU year. |
| 1050 | SendSample("Logging.KernelCrashesPerCpuYear", |
| 1051 | crashes_count * kSecondsPerDay * 365 * 1000 / cpu_use_ms, |
| 1052 | 1, |
| 1053 | 1000 * 1000, // about one crash every 30s of CPU time |
| 1054 | 100); |
| 1055 | } |
| 1056 | |
Ben Chan | f05ab40 | 2014-08-07 00:54:59 -0700 | [diff] [blame] | 1057 | int64_t active_use_seconds = version_cumulative_active_use_->Get(); |
Luigi Semenzato | ba0c65d | 2014-03-17 12:28:38 -0700 | [diff] [blame] | 1058 | if (active_use_seconds > 0) { |
Luigi Semenzato | e5883fa | 2014-04-18 17:00:35 -0700 | [diff] [blame] | 1059 | SendSample(version_cumulative_active_use_->Name(), |
| 1060 | active_use_seconds / 1000, // stat is in seconds |
| 1061 | 1, // device may be used very little... |
| 1062 | 8 * 1000 * 1000, // ... or a lot (about 90 days) |
| 1063 | 100); |
Luigi Semenzato | ba0c65d | 2014-03-17 12:28:38 -0700 | [diff] [blame] | 1064 | // Same as above, but per year of active time. |
| 1065 | SendSample("Logging.KernelCrashesPerActiveYear", |
| 1066 | crashes_count * kSecondsPerDay * 365 / active_use_seconds, |
| 1067 | 1, |
| 1068 | 1000 * 1000, // about one crash every 30s of active time |
| 1069 | 100); |
| 1070 | } |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 1071 | } |
| 1072 | |
Luigi Semenzato | e5883fa | 2014-04-18 17:00:35 -0700 | [diff] [blame] | 1073 | void MetricsDaemon::SendDailyUseSample( |
| 1074 | const scoped_ptr<PersistentInteger>& use) { |
| 1075 | SendSample(use->Name(), |
| 1076 | use->GetAndClear(), |
| 1077 | 1, // value of first bucket |
| 1078 | kSecondsPerDay, // value of last bucket |
| 1079 | 50); // number of buckets |
| 1080 | } |
| 1081 | |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 1082 | void MetricsDaemon::SendCrashIntervalSample( |
| 1083 | const scoped_ptr<PersistentInteger>& interval) { |
| 1084 | SendSample(interval->Name(), |
| 1085 | interval->GetAndClear(), |
| 1086 | 1, // value of first bucket |
| 1087 | 4 * kSecondsPerWeek, // value of last bucket |
| 1088 | 50); // number of buckets |
| 1089 | } |
| 1090 | |
| 1091 | void MetricsDaemon::SendCrashFrequencySample( |
| 1092 | const scoped_ptr<PersistentInteger>& frequency) { |
| 1093 | SendSample(frequency->Name(), |
| 1094 | frequency->GetAndClear(), |
| 1095 | 1, // value of first bucket |
| 1096 | 100, // value of last bucket |
| 1097 | 50); // number of buckets |
| 1098 | } |
| 1099 | |
| 1100 | void MetricsDaemon::SendLinearSample(const string& name, int sample, |
Luigi Semenzato | 29c7ef9 | 2011-04-12 14:12:35 -0700 | [diff] [blame] | 1101 | int max, int nbuckets) { |
Luigi Semenzato | 29c7ef9 | 2011-04-12 14:12:35 -0700 | [diff] [blame] | 1102 | // TODO(semenzato): add a proper linear histogram to the Chrome external |
| 1103 | // metrics API. |
| 1104 | LOG_IF(FATAL, nbuckets != max + 1) << "unsupported histogram scale"; |
| 1105 | metrics_lib_->SendEnumToUMA(name, sample, max); |
| 1106 | } |
Daniel Erat | c83975a | 2014-04-04 08:53:44 -0700 | [diff] [blame] | 1107 | |
| 1108 | void MetricsDaemon::UpdateStats(TimeTicks now_ticks, |
| 1109 | Time now_wall_time) { |
| 1110 | const int elapsed_seconds = (now_ticks - last_update_stats_time_).InSeconds(); |
Luigi Semenzato | e5883fa | 2014-04-18 17:00:35 -0700 | [diff] [blame] | 1111 | daily_active_use_->Add(elapsed_seconds); |
| 1112 | version_cumulative_active_use_->Add(elapsed_seconds); |
Daniel Erat | c83975a | 2014-04-04 08:53:44 -0700 | [diff] [blame] | 1113 | user_crash_interval_->Add(elapsed_seconds); |
| 1114 | kernel_crash_interval_->Add(elapsed_seconds); |
| 1115 | version_cumulative_cpu_use_->Add(GetIncrementalCpuUse().InMilliseconds()); |
| 1116 | last_update_stats_time_ = now_ticks; |
| 1117 | |
| 1118 | const TimeDelta since_epoch = now_wall_time - Time::UnixEpoch(); |
| 1119 | const int day = since_epoch.InDays(); |
| 1120 | const int week = day / 7; |
| 1121 | |
| 1122 | if (daily_cycle_->Get() != day) { |
| 1123 | daily_cycle_->Set(day); |
Luigi Semenzato | e5883fa | 2014-04-18 17:00:35 -0700 | [diff] [blame] | 1124 | SendDailyUseSample(daily_active_use_); |
| 1125 | SendDailyUseSample(version_cumulative_active_use_); |
Daniel Erat | c83975a | 2014-04-04 08:53:44 -0700 | [diff] [blame] | 1126 | SendCrashFrequencySample(any_crashes_daily_count_); |
| 1127 | SendCrashFrequencySample(user_crashes_daily_count_); |
| 1128 | SendCrashFrequencySample(kernel_crashes_daily_count_); |
| 1129 | SendCrashFrequencySample(unclean_shutdowns_daily_count_); |
Luigi Semenzato | e5883fa | 2014-04-18 17:00:35 -0700 | [diff] [blame] | 1130 | SendKernelCrashesCumulativeCountStats(); |
Daniel Erat | c83975a | 2014-04-04 08:53:44 -0700 | [diff] [blame] | 1131 | } |
| 1132 | |
| 1133 | if (weekly_cycle_->Get() != week) { |
| 1134 | weekly_cycle_->Set(week); |
| 1135 | SendCrashFrequencySample(any_crashes_weekly_count_); |
| 1136 | SendCrashFrequencySample(user_crashes_weekly_count_); |
| 1137 | SendCrashFrequencySample(kernel_crashes_weekly_count_); |
| 1138 | SendCrashFrequencySample(unclean_shutdowns_weekly_count_); |
| 1139 | } |
| 1140 | } |
| 1141 | |
| 1142 | // static |
| 1143 | gboolean MetricsDaemon::HandleUpdateStatsTimeout(gpointer data) { |
| 1144 | static_cast<MetricsDaemon*>(data)->UpdateStats(TimeTicks::Now(), Time::Now()); |
| 1145 | return TRUE; |
| 1146 | } |