blob: e3f6bf7e7963ee35f681f4ce90a8997802631bf6 [file] [log] [blame]
Darin Petkov8032dd02011-05-09 16:33:19 -07001// Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
Darin Petkov65b01462010-04-14 13:32:20 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "metrics_daemon.h"
Darin Petkov65b01462010-04-14 13:32:20 -07006
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -08007#include <fcntl.h>
Luigi Semenzato8accd332011-05-17 16:37:18 -07008#include <math.h>
Ken Mixter4c5daa42010-08-26 18:35:06 -07009#include <string.h>
Luigi Semenzato8accd332011-05-17 16:37:18 -070010#include <time.h>
Darin Petkov65b01462010-04-14 13:32:20 -070011
Luigi Semenzato859b3f02014-02-05 15:33:19 -080012#include <base/at_exit.h>
Darin Petkov38d5cb02010-06-24 12:10:26 -070013#include <base/file_util.h>
Luigi Semenzato859b3f02014-02-05 15:33:19 -080014#include <base/files/file_path.h>
15#include <base/hash.h>
Darin Petkov65b01462010-04-14 13:32:20 -070016#include <base/logging.h>
Ben Chan2e6543d2014-02-05 23:26:25 -080017#include <base/strings/string_number_conversions.h>
18#include <base/strings/string_split.h>
19#include <base/strings/string_util.h>
20#include <base/strings/stringprintf.h>
Luigi Semenzato859b3f02014-02-05 15:33:19 -080021#include <base/sys_info.h>
Darin Petkov40f25732013-04-29 15:07:31 +020022#include <chromeos/dbus/service_constants.h>
Ken Mixter4c5daa42010-08-26 18:35:06 -070023#include <dbus/dbus-glib-lowlevel.h>
Darin Petkov65b01462010-04-14 13:32:20 -070024
Ben Chan2e6543d2014-02-05 23:26:25 -080025using base::FilePath;
26using base::StringPrintf;
Darin Petkovf27f0362010-06-04 13:14:19 -070027using base::Time;
28using base::TimeDelta;
29using base::TimeTicks;
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080030using chromeos_metrics::PersistentInteger;
Luigi Semenzato8accd332011-05-17 16:37:18 -070031using std::map;
Darin Petkov38d5cb02010-06-24 12:10:26 -070032using std::string;
Luigi Semenzato8accd332011-05-17 16:37:18 -070033using std::vector;
34
Daniel Eratc83975a2014-04-04 08:53:44 -070035namespace {
Darin Petkovf27f0362010-06-04 13:14:19 -070036
Darin Petkov703ec972010-04-27 11:02:18 -070037#define SAFE_MESSAGE(e) (e.message ? e.message : "unknown error")
Darin Petkov40f25732013-04-29 15:07:31 +020038
Daniel Eratc83975a2014-04-04 08:53:44 -070039const char kCrashReporterInterface[] = "org.chromium.CrashReporter";
40const char kCrashReporterUserCrashSignal[] = "UserCrash";
Darin Petkov41e06232010-05-03 16:45:37 -070041
Daniel Eratc83975a2014-04-04 08:53:44 -070042const int kSecondsPerMinute = 60;
43const int kMinutesPerHour = 60;
44const int kHoursPerDay = 24;
45const int kMinutesPerDay = kHoursPerDay * kMinutesPerHour;
46const int kSecondsPerDay = kSecondsPerMinute * kMinutesPerDay;
47const int kDaysPerWeek = 7;
48const int kSecondsPerWeek = kSecondsPerDay * kDaysPerWeek;
Darin Petkov41e06232010-05-03 16:45:37 -070049
Daniel Eratc83975a2014-04-04 08:53:44 -070050// Interval between calls to UpdateStats().
51const guint kUpdateStatsIntervalMs = 300000;
Darin Petkov65b01462010-04-14 13:32:20 -070052
Luigi Semenzatoc5a92342014-02-14 15:05:51 -080053const char kKernelCrashDetectedFile[] = "/var/run/kernel-crash-detected";
Daniel Eratc83975a2014-04-04 08:53:44 -070054const char kUncleanShutdownDetectedFile[] =
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080055 "/var/run/unclean-shutdown-detected";
Ken Mixterccd84c02010-08-16 19:57:13 -070056
Daniel Eratc83975a2014-04-04 08:53:44 -070057} // namespace
58
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -080059// disk stats metrics
60
61// The {Read,Write}Sectors numbers are in sectors/second.
62// A sector is usually 512 bytes.
63
64const char MetricsDaemon::kMetricReadSectorsLongName[] =
65 "Platform.ReadSectorsLong";
66const char MetricsDaemon::kMetricWriteSectorsLongName[] =
67 "Platform.WriteSectorsLong";
68const char MetricsDaemon::kMetricReadSectorsShortName[] =
69 "Platform.ReadSectorsShort";
70const char MetricsDaemon::kMetricWriteSectorsShortName[] =
71 "Platform.WriteSectorsShort";
72
Luigi Semenzato5bd764f2011-10-14 12:03:35 -070073const int MetricsDaemon::kMetricStatsShortInterval = 1; // seconds
74const int MetricsDaemon::kMetricStatsLongInterval = 30; // seconds
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -080075
Luigi Semenzato29c7ef92011-04-12 14:12:35 -070076const int MetricsDaemon::kMetricMeminfoInterval = 30; // seconds
77
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -080078// Assume a max rate of 250Mb/s for reads (worse for writes) and 512 byte
79// sectors.
80const int MetricsDaemon::kMetricSectorsIOMax = 500000; // sectors/second
81const int MetricsDaemon::kMetricSectorsBuckets = 50; // buckets
Luigi Semenzato5bd764f2011-10-14 12:03:35 -070082// Page size is 4k, sector size is 0.5k. We're not interested in page fault
83// rates that the disk cannot sustain.
84const int MetricsDaemon::kMetricPageFaultsMax = kMetricSectorsIOMax / 8;
85const int MetricsDaemon::kMetricPageFaultsBuckets = 50;
86
87// Major page faults, i.e. the ones that require data to be read from disk.
88
89const char MetricsDaemon::kMetricPageFaultsLongName[] =
90 "Platform.PageFaultsLong";
91const char MetricsDaemon::kMetricPageFaultsShortName[] =
92 "Platform.PageFaultsShort";
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -080093
Sonny Rao4b8aebb2013-07-31 23:18:31 -070094// Swap in and Swap out
95
96const char MetricsDaemon::kMetricSwapInLongName[] =
97 "Platform.SwapInLong";
98const char MetricsDaemon::kMetricSwapInShortName[] =
99 "Platform.SwapInShort";
100
101const char MetricsDaemon::kMetricSwapOutLongName[] =
102 "Platform.SwapOutLong";
103const char MetricsDaemon::kMetricSwapOutShortName[] =
104 "Platform.SwapOutShort";
105
Luigi Semenzatoba0c65d2014-03-17 12:28:38 -0700106const char MetricsDaemon::kMetricsProcStatFileName[] = "/proc/stat";
107const int MetricsDaemon::kMetricsProcStatFirstLineItemsCount = 11;
108
Luigi Semenzatofb3a8212013-05-07 16:55:00 -0700109// Thermal CPU throttling.
110
111const char MetricsDaemon::kMetricScaledCpuFrequencyName[] =
112 "Platform.CpuFrequencyThermalScaling";
113
Luigi Semenzato8accd332011-05-17 16:37:18 -0700114// Memory use stats collection intervals. We collect some memory use interval
115// at these intervals after boot, and we stop collecting after the last one,
116// with the assumption that in most cases the memory use won't change much
117// after that.
118static const int kMemuseIntervals[] = {
119 1 * kSecondsPerMinute, // 1 minute mark
120 4 * kSecondsPerMinute, // 5 minute mark
121 25 * kSecondsPerMinute, // 0.5 hour mark
122 120 * kSecondsPerMinute, // 2.5 hour mark
123 600 * kSecondsPerMinute, // 12.5 hour mark
124};
125
Darin Petkovf1e85e42010-06-10 15:59:53 -0700126MetricsDaemon::MetricsDaemon()
Daniel Eratc83975a2014-04-04 08:53:44 -0700127 : update_stats_timeout_id_(-1),
Luigi Semenzato0d9a9c92013-12-05 15:55:12 -0800128 memuse_final_time_(0),
Luigi Semenzato8accd332011-05-17 16:37:18 -0700129 memuse_interval_index_(0),
130 read_sectors_(0),
131 write_sectors_(0),
Sonny Rao4b8aebb2013-07-31 23:18:31 -0700132 vmstats_(),
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700133 stats_state_(kStatsShort),
Luigi Semenzatoba0c65d2014-03-17 12:28:38 -0700134 stats_initial_time_(0),
135 ticks_per_second_(0),
136 latest_cpu_use_ticks_(0) {}
Darin Petkovf1e85e42010-06-10 15:59:53 -0700137
Ken Mixter4c5daa42010-08-26 18:35:06 -0700138MetricsDaemon::~MetricsDaemon() {
Daniel Eratc83975a2014-04-04 08:53:44 -0700139 if (update_stats_timeout_id_ > -1)
140 g_source_remove(update_stats_timeout_id_);
Ken Mixter4c5daa42010-08-26 18:35:06 -0700141}
142
Luigi Semenzato8accd332011-05-17 16:37:18 -0700143double MetricsDaemon::GetActiveTime() {
144 struct timespec ts;
145 int r = clock_gettime(CLOCK_MONOTONIC, &ts);
146 if (r < 0) {
147 PLOG(WARNING) << "clock_gettime(CLOCK_MONOTONIC) failed";
148 return 0;
149 } else {
150 return ts.tv_sec + ((double) ts.tv_nsec) / (1000 * 1000 * 1000);
151 }
152}
153
Darin Petkov2ccef012010-05-05 16:06:37 -0700154void MetricsDaemon::Run(bool run_as_daemon) {
Luigi Semenzato859b3f02014-02-05 15:33:19 -0800155 base::AtExitManager at_exit_manager;
156
Darin Petkov38d5cb02010-06-24 12:10:26 -0700157 if (run_as_daemon && daemon(0, 0) != 0)
158 return;
159
Ken Mixterccd84c02010-08-16 19:57:13 -0700160 if (CheckSystemCrash(kKernelCrashDetectedFile)) {
161 ProcessKernelCrash();
162 }
163
164 if (CheckSystemCrash(kUncleanShutdownDetectedFile)) {
165 ProcessUncleanShutdown();
166 }
167
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800168 // On OS version change, clear version stats (which are reported daily).
169 int32 version = GetOsVersionHash();
170 if (version_cycle_->Get() != version) {
171 version_cycle_->Set(version);
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800172 kernel_crashes_version_count_->Set(0);
Luigi Semenzatoe5883fa2014-04-18 17:00:35 -0700173 version_cumulative_active_use_->Set(0);
Luigi Semenzatoba0c65d2014-03-17 12:28:38 -0700174 version_cumulative_cpu_use_->Set(0);
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800175 }
176
Darin Petkov38d5cb02010-06-24 12:10:26 -0700177 Loop();
Darin Petkov65b01462010-04-14 13:32:20 -0700178}
179
Luigi Semenzato859b3f02014-02-05 15:33:19 -0800180uint32 MetricsDaemon::GetOsVersionHash() {
181 static uint32 cached_version_hash = 0;
182 static bool version_hash_is_cached = false;
183 if (version_hash_is_cached)
184 return cached_version_hash;
185 version_hash_is_cached = true;
186 std::string version;
187 if (base::SysInfo::GetLsbReleaseValue("CHROMEOS_RELEASE_VERSION", &version)) {
188 cached_version_hash = base::Hash(version);
189 } else if (testing_) {
190 cached_version_hash = 42; // return any plausible value for the hash
191 } else {
192 LOG(FATAL) << "could not find CHROMEOS_RELEASE_VERSION";
193 }
194 return cached_version_hash;
195}
196
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800197void MetricsDaemon::Init(bool testing, MetricsLibraryInterface* metrics_lib,
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700198 const string& diskstats_path,
Luigi Semenzatofb3a8212013-05-07 16:55:00 -0700199 const string& vmstats_path,
200 const string& scaling_max_freq_path,
Daniel Eratc83975a2014-04-04 08:53:44 -0700201 const string& cpuinfo_max_freq_path) {
Darin Petkov65b01462010-04-14 13:32:20 -0700202 testing_ = testing;
Darin Petkovfc91b422010-05-12 13:05:45 -0700203 DCHECK(metrics_lib != NULL);
204 metrics_lib_ = metrics_lib;
Darin Petkov38d5cb02010-06-24 12:10:26 -0700205
Luigi Semenzatoba0c65d2014-03-17 12:28:38 -0700206 // Get ticks per second (HZ) on this system.
207 // Sysconf cannot fail, so no sanity checks are needed.
208 ticks_per_second_ = sysconf(_SC_CLK_TCK);
209
Luigi Semenzatoe5883fa2014-04-18 17:00:35 -0700210 daily_active_use_.reset(
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800211 new PersistentInteger("Logging.DailyUseTime"));
Luigi Semenzatoe5883fa2014-04-18 17:00:35 -0700212 version_cumulative_active_use_.reset(
213 new PersistentInteger("Logging.CumulativeDailyUseTime"));
Luigi Semenzatoba0c65d2014-03-17 12:28:38 -0700214 version_cumulative_cpu_use_.reset(
215 new PersistentInteger("Logging.CumulativeCpuTime"));
Darin Petkov38d5cb02010-06-24 12:10:26 -0700216
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800217 kernel_crash_interval_.reset(
218 new PersistentInteger("Logging.KernelCrashInterval"));
219 unclean_shutdown_interval_.reset(
220 new PersistentInteger("Logging.UncleanShutdownInterval"));
221 user_crash_interval_.reset(
222 new PersistentInteger("Logging.UserCrashInterval"));
Darin Petkov2ccef012010-05-05 16:06:37 -0700223
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800224 any_crashes_daily_count_.reset(
225 new PersistentInteger("Logging.AnyCrashesDaily"));
226 any_crashes_weekly_count_.reset(
227 new PersistentInteger("Logging.AnyCrashesWeekly"));
228 user_crashes_daily_count_.reset(
229 new PersistentInteger("Logging.UserCrashesDaily"));
230 user_crashes_weekly_count_.reset(
231 new PersistentInteger("Logging.UserCrashesWeekly"));
232 kernel_crashes_daily_count_.reset(
233 new PersistentInteger("Logging.KernelCrashesDaily"));
234 kernel_crashes_weekly_count_.reset(
235 new PersistentInteger("Logging.KernelCrashesWeekly"));
236 kernel_crashes_version_count_.reset(
237 new PersistentInteger("Logging.KernelCrashesSinceUpdate"));
238 unclean_shutdowns_daily_count_.reset(
239 new PersistentInteger("Logging.UncleanShutdownsDaily"));
240 unclean_shutdowns_weekly_count_.reset(
241 new PersistentInteger("Logging.UncleanShutdownsWeekly"));
Darin Petkov38d5cb02010-06-24 12:10:26 -0700242
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800243 daily_cycle_.reset(new PersistentInteger("daily.cycle"));
244 weekly_cycle_.reset(new PersistentInteger("weekly.cycle"));
245 version_cycle_.reset(new PersistentInteger("version.cycle"));
Luigi Semenzato859b3f02014-02-05 15:33:19 -0800246
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700247 diskstats_path_ = diskstats_path;
248 vmstats_path_ = vmstats_path;
Luigi Semenzatofb3a8212013-05-07 16:55:00 -0700249 scaling_max_freq_path_ = scaling_max_freq_path;
250 cpuinfo_max_freq_path_ = cpuinfo_max_freq_path;
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700251 StatsReporterInit();
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800252
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700253 // Start collecting meminfo stats.
254 ScheduleMeminfoCallback(kMetricMeminfoInterval);
Luigi Semenzato0d9a9c92013-12-05 15:55:12 -0800255 memuse_final_time_ = GetActiveTime() + kMemuseIntervals[0];
256 ScheduleMemuseCallback(kMemuseIntervals[0]);
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700257
Darin Petkov2ccef012010-05-05 16:06:37 -0700258 // Don't setup D-Bus and GLib in test mode.
259 if (testing)
260 return;
Darin Petkov65b01462010-04-14 13:32:20 -0700261
Darin Petkov703ec972010-04-27 11:02:18 -0700262 g_type_init();
Ben Chan6f598422013-06-22 06:29:36 -0700263 dbus_threads_init_default();
Darin Petkov65b01462010-04-14 13:32:20 -0700264
Darin Petkov703ec972010-04-27 11:02:18 -0700265 DBusError error;
266 dbus_error_init(&error);
Darin Petkov65b01462010-04-14 13:32:20 -0700267
David James3b3add52010-06-04 15:01:19 -0700268 DBusConnection* connection = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
Darin Petkov703ec972010-04-27 11:02:18 -0700269 LOG_IF(FATAL, dbus_error_is_set(&error)) <<
270 "No D-Bus connection: " << SAFE_MESSAGE(error);
Darin Petkov65b01462010-04-14 13:32:20 -0700271
Darin Petkov703ec972010-04-27 11:02:18 -0700272 dbus_connection_setup_with_g_main(connection, NULL);
Darin Petkov65b01462010-04-14 13:32:20 -0700273
Darin Petkov40f25732013-04-29 15:07:31 +0200274 vector<string> matches;
275 matches.push_back(
Luigi Semenzato859b3f02014-02-05 15:33:19 -0800276 base::StringPrintf("type='signal',interface='%s',path='/',member='%s'",
277 kCrashReporterInterface,
278 kCrashReporterUserCrashSignal));
Darin Petkov40f25732013-04-29 15:07:31 +0200279
Darin Petkov703ec972010-04-27 11:02:18 -0700280 // Registers D-Bus matches for the signals we would like to catch.
Darin Petkov40f25732013-04-29 15:07:31 +0200281 for (vector<string>::const_iterator it = matches.begin();
282 it != matches.end(); ++it) {
283 const char* match = it->c_str();
Darin Petkov41e06232010-05-03 16:45:37 -0700284 DLOG(INFO) << "adding dbus match: " << match;
Darin Petkov703ec972010-04-27 11:02:18 -0700285 dbus_bus_add_match(connection, match, &error);
286 LOG_IF(FATAL, dbus_error_is_set(&error)) <<
287 "unable to add a match: " << SAFE_MESSAGE(error);
288 }
289
290 // Adds the D-Bus filter routine to be called back whenever one of
291 // the registered D-Bus matches is successful. The daemon is not
292 // activated for D-Bus messages that don't match.
293 CHECK(dbus_connection_add_filter(connection, MessageFilter, this, NULL));
Daniel Eratc83975a2014-04-04 08:53:44 -0700294
295 update_stats_timeout_id_ =
296 g_timeout_add(kUpdateStatsIntervalMs, &HandleUpdateStatsTimeout, this);
Darin Petkov65b01462010-04-14 13:32:20 -0700297}
298
299void MetricsDaemon::Loop() {
Darin Petkov703ec972010-04-27 11:02:18 -0700300 GMainLoop* loop = g_main_loop_new(NULL, false);
301 g_main_loop_run(loop);
Darin Petkov65b01462010-04-14 13:32:20 -0700302}
303
Darin Petkov703ec972010-04-27 11:02:18 -0700304// static
305DBusHandlerResult MetricsDaemon::MessageFilter(DBusConnection* connection,
306 DBusMessage* message,
307 void* user_data) {
Darin Petkov703ec972010-04-27 11:02:18 -0700308 int message_type = dbus_message_get_type(message);
309 if (message_type != DBUS_MESSAGE_TYPE_SIGNAL) {
Darin Petkov41e06232010-05-03 16:45:37 -0700310 DLOG(WARNING) << "unexpected message type " << message_type;
Darin Petkov703ec972010-04-27 11:02:18 -0700311 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
312 }
313
314 // Signal messages always have interfaces.
Daniel Eratc83975a2014-04-04 08:53:44 -0700315 const std::string interface(dbus_message_get_interface(message));
316 const std::string member(dbus_message_get_member(message));
317 DLOG(INFO) << "Got " << interface << "." << member << " D-Bus signal";
Darin Petkov703ec972010-04-27 11:02:18 -0700318
319 MetricsDaemon* daemon = static_cast<MetricsDaemon*>(user_data);
320
321 DBusMessageIter iter;
322 dbus_message_iter_init(message, &iter);
Daniel Eratc83975a2014-04-04 08:53:44 -0700323 if (interface == kCrashReporterInterface) {
324 CHECK_EQ(member, kCrashReporterUserCrashSignal);
Darin Petkov1bb904e2010-06-16 15:58:06 -0700325 daemon->ProcessUserCrash();
Darin Petkov703ec972010-04-27 11:02:18 -0700326 } else {
Daniel Eratc83975a2014-04-04 08:53:44 -0700327 // Ignore messages from the bus itself.
Darin Petkov703ec972010-04-27 11:02:18 -0700328 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
329 }
330
331 return DBUS_HANDLER_RESULT_HANDLED;
Darin Petkov65b01462010-04-14 13:32:20 -0700332}
333
Luigi Semenzatoba0c65d2014-03-17 12:28:38 -0700334// One might argue that parts of this should go into
335// chromium/src/base/sys_info_chromeos.c instead, but put it here for now.
336
337TimeDelta MetricsDaemon::GetIncrementalCpuUse() {
Luigi Semenzatoba0c65d2014-03-17 12:28:38 -0700338 FilePath proc_stat_path = FilePath(kMetricsProcStatFileName);
339 std::string proc_stat_string;
340 if (!base::ReadFileToString(proc_stat_path, &proc_stat_string)) {
341 LOG(WARNING) << "cannot open " << kMetricsProcStatFileName;
342 return TimeDelta();
343 }
344
345 std::vector<std::string> proc_stat_lines;
346 base::SplitString(proc_stat_string, '\n', &proc_stat_lines);
347 if (proc_stat_lines.empty()) {
348 LOG(WARNING) << "cannot parse " << kMetricsProcStatFileName
349 << ": " << proc_stat_string;
350 return TimeDelta();
351 }
352 std::vector<std::string> proc_stat_totals;
353 base::SplitStringAlongWhitespace(proc_stat_lines[0], &proc_stat_totals);
354
355 uint64 user_ticks, user_nice_ticks, system_ticks;
356 if (proc_stat_totals.size() != kMetricsProcStatFirstLineItemsCount ||
357 proc_stat_totals[0] != "cpu" ||
358 !base::StringToUint64(proc_stat_totals[1], &user_ticks) ||
359 !base::StringToUint64(proc_stat_totals[2], &user_nice_ticks) ||
360 !base::StringToUint64(proc_stat_totals[3], &system_ticks)) {
361 LOG(WARNING) << "cannot parse first line: " << proc_stat_lines[0];
362 return TimeDelta(base::TimeDelta::FromSeconds(0));
363 }
364
365 uint64 total_cpu_use_ticks = user_ticks + user_nice_ticks + system_ticks;
366
367 // Sanity check.
368 if (total_cpu_use_ticks < latest_cpu_use_ticks_) {
369 LOG(WARNING) << "CPU time decreasing from " << latest_cpu_use_ticks_
370 << " to " << total_cpu_use_ticks;
371 return TimeDelta();
372 }
373
374 uint64 diff = total_cpu_use_ticks - latest_cpu_use_ticks_;
375 latest_cpu_use_ticks_ = total_cpu_use_ticks;
376 // Use microseconds to avoid significant truncations.
377 return base::TimeDelta::FromMicroseconds(
378 diff * 1000 * 1000 / ticks_per_second_);
379}
380
Darin Petkov1bb904e2010-06-16 15:58:06 -0700381void MetricsDaemon::ProcessUserCrash() {
Daniel Eratc83975a2014-04-04 08:53:44 -0700382 // Counts the active time up to now.
383 UpdateStats(TimeTicks::Now(), Time::Now());
Darin Petkov1bb904e2010-06-16 15:58:06 -0700384
385 // Reports the active use time since the last crash and resets it.
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800386 SendCrashIntervalSample(user_crash_interval_);
Ken Mixterccd84c02010-08-16 19:57:13 -0700387
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800388 any_crashes_daily_count_->Add(1);
389 any_crashes_weekly_count_->Add(1);
390 user_crashes_daily_count_->Add(1);
391 user_crashes_weekly_count_->Add(1);
Darin Petkov1bb904e2010-06-16 15:58:06 -0700392}
393
Darin Petkov38d5cb02010-06-24 12:10:26 -0700394void MetricsDaemon::ProcessKernelCrash() {
Daniel Eratc83975a2014-04-04 08:53:44 -0700395 // Counts the active time up to now.
396 UpdateStats(TimeTicks::Now(), Time::Now());
Darin Petkov38d5cb02010-06-24 12:10:26 -0700397
398 // Reports the active use time since the last crash and resets it.
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800399 SendCrashIntervalSample(kernel_crash_interval_);
Ken Mixterccd84c02010-08-16 19:57:13 -0700400
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800401 any_crashes_daily_count_->Add(1);
402 any_crashes_weekly_count_->Add(1);
403 kernel_crashes_daily_count_->Add(1);
404 kernel_crashes_weekly_count_->Add(1);
Luigi Semenzato859b3f02014-02-05 15:33:19 -0800405
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800406 kernel_crashes_version_count_->Add(1);
Darin Petkov38d5cb02010-06-24 12:10:26 -0700407}
408
Ken Mixterccd84c02010-08-16 19:57:13 -0700409void MetricsDaemon::ProcessUncleanShutdown() {
Daniel Eratc83975a2014-04-04 08:53:44 -0700410 // Counts the active time up to now.
411 UpdateStats(TimeTicks::Now(), Time::Now());
Ken Mixterccd84c02010-08-16 19:57:13 -0700412
413 // Reports the active use time since the last crash and resets it.
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800414 SendCrashIntervalSample(unclean_shutdown_interval_);
Ken Mixterccd84c02010-08-16 19:57:13 -0700415
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800416 unclean_shutdowns_daily_count_->Add(1);
417 unclean_shutdowns_weekly_count_->Add(1);
418 any_crashes_daily_count_->Add(1);
419 any_crashes_weekly_count_->Add(1);
Ken Mixterccd84c02010-08-16 19:57:13 -0700420}
421
Luigi Semenzato8accd332011-05-17 16:37:18 -0700422bool MetricsDaemon::CheckSystemCrash(const string& crash_file) {
Darin Petkov38d5cb02010-06-24 12:10:26 -0700423 FilePath crash_detected(crash_file);
Ben Chan2e6543d2014-02-05 23:26:25 -0800424 if (!base::PathExists(crash_detected))
Ken Mixterccd84c02010-08-16 19:57:13 -0700425 return false;
Darin Petkov38d5cb02010-06-24 12:10:26 -0700426
427 // Deletes the crash-detected file so that the daemon doesn't report
428 // another kernel crash in case it's restarted.
Luigi Semenzato859b3f02014-02-05 15:33:19 -0800429 base::DeleteFile(crash_detected, false); // not recursive
Ken Mixterccd84c02010-08-16 19:57:13 -0700430 return true;
Darin Petkov38d5cb02010-06-24 12:10:26 -0700431}
432
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700433void MetricsDaemon::StatsReporterInit() {
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800434 DiskStatsReadStats(&read_sectors_, &write_sectors_);
Sonny Rao4b8aebb2013-07-31 23:18:31 -0700435 VmStatsReadStats(&vmstats_);
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800436 // The first time around just run the long stat, so we don't delay boot.
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700437 stats_state_ = kStatsLong;
438 stats_initial_time_ = GetActiveTime();
439 if (stats_initial_time_ < 0) {
Luigi Semenzato8accd332011-05-17 16:37:18 -0700440 LOG(WARNING) << "not collecting disk stats";
441 } else {
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700442 ScheduleStatsCallback(kMetricStatsLongInterval);
Luigi Semenzato8accd332011-05-17 16:37:18 -0700443 }
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800444}
445
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700446void MetricsDaemon::ScheduleStatsCallback(int wait) {
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800447 if (testing_) {
448 return;
449 }
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700450 g_timeout_add_seconds(wait, StatsCallbackStatic, this);
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800451}
452
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700453bool MetricsDaemon::DiskStatsReadStats(long int* read_sectors,
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800454 long int* write_sectors) {
455 int nchars;
456 int nitems;
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700457 bool success = false;
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800458 char line[200];
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700459 if (diskstats_path_.empty()) {
460 return false;
461 }
Luigi Semenzato0f132bb2011-02-28 11:17:43 -0800462 int file = HANDLE_EINTR(open(diskstats_path_.c_str(), O_RDONLY));
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800463 if (file < 0) {
464 PLOG(WARNING) << "cannot open " << diskstats_path_;
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700465 return false;
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800466 }
467 nchars = HANDLE_EINTR(read(file, line, sizeof(line)));
468 if (nchars < 0) {
469 PLOG(WARNING) << "cannot read from " << diskstats_path_;
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700470 return false;
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800471 } else {
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700472 LOG_IF(WARNING, nchars == sizeof(line))
473 << "line too long in " << diskstats_path_;
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800474 line[nchars] = '\0';
475 nitems = sscanf(line, "%*d %*d %ld %*d %*d %*d %ld",
476 read_sectors, write_sectors);
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700477 if (nitems == 2) {
478 success = true;
479 } else {
480 LOG(WARNING) << "found " << nitems << " items in "
481 << diskstats_path_ << ", expected 2";
482 }
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800483 }
Mike Frysinger3e8a8512014-05-14 16:14:37 -0400484 IGNORE_EINTR(close(file));
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700485 return success;
486}
487
Sonny Rao4b8aebb2013-07-31 23:18:31 -0700488bool MetricsDaemon::VmStatsParseStats(const char* stats,
489 struct VmstatRecord* record) {
490 // a mapping of string name to field in VmstatRecord and whether we found it
491 struct mapping {
492 const string name;
493 uint64_t* value_p;
494 bool found;
495 } map[] =
496 { { .name = "pgmajfault",
497 .value_p = &record->page_faults_,
498 .found = false },
499 { .name = "pswpin",
500 .value_p = &record->swap_in_,
501 .found = false },
502 { .name = "pswpout",
503 .value_p = &record->swap_out_,
504 .found = false }, };
505
Luigi Semenzatofb3a8212013-05-07 16:55:00 -0700506 // Each line in the file has the form
507 // <ID> <VALUE>
508 // for instance:
509 // nr_free_pages 213427
Sonny Rao4b8aebb2013-07-31 23:18:31 -0700510 vector<string> lines;
511 Tokenize(stats, "\n", &lines);
512 for (vector<string>::iterator it = lines.begin();
513 it != lines.end(); ++it) {
514 vector<string> tokens;
515 base::SplitString(*it, ' ', &tokens);
516 if (tokens.size() == 2) {
517 for (unsigned int i = 0; i < sizeof(map)/sizeof(struct mapping); i++) {
518 if (!tokens[0].compare(map[i].name)) {
519 if (!base::StringToUint64(tokens[1], map[i].value_p))
520 return false;
521 map[i].found = true;
522 }
523 }
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700524 } else {
Sonny Rao4b8aebb2013-07-31 23:18:31 -0700525 LOG(WARNING) << "unexpected vmstat format";
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700526 }
527 }
Sonny Rao4b8aebb2013-07-31 23:18:31 -0700528 // make sure we got all the stats
529 for (unsigned i = 0; i < sizeof(map)/sizeof(struct mapping); i++) {
530 if (map[i].found == false) {
531 LOG(WARNING) << "vmstat missing " << map[i].name;
532 return false;
533 }
534 }
535 return true;
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700536}
537
Sonny Rao4b8aebb2013-07-31 23:18:31 -0700538bool MetricsDaemon::VmStatsReadStats(struct VmstatRecord* stats) {
539 string value_string;
540 FilePath* path = new FilePath(vmstats_path_);
Ben Chan2e6543d2014-02-05 23:26:25 -0800541 if (!base::ReadFileToString(*path, &value_string)) {
Sonny Rao4b8aebb2013-07-31 23:18:31 -0700542 delete path;
543 LOG(WARNING) << "cannot read " << vmstats_path_;
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700544 return false;
545 }
Sonny Rao4b8aebb2013-07-31 23:18:31 -0700546 delete path;
547 return VmStatsParseStats(value_string.c_str(), stats);
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800548}
549
Luigi Semenzatofb3a8212013-05-07 16:55:00 -0700550bool MetricsDaemon::ReadFreqToInt(const string& sysfs_file_name, int* value) {
Luigi Semenzatod92d18c2013-06-04 13:24:21 -0700551 const FilePath sysfs_path(sysfs_file_name);
Luigi Semenzatofb3a8212013-05-07 16:55:00 -0700552 string value_string;
Ben Chan2e6543d2014-02-05 23:26:25 -0800553 if (!base::ReadFileToString(sysfs_path, &value_string)) {
Luigi Semenzatofb3a8212013-05-07 16:55:00 -0700554 LOG(WARNING) << "cannot read " << sysfs_path.value().c_str();
555 return false;
556 }
Ben Chan2e6543d2014-02-05 23:26:25 -0800557 if (!base::RemoveChars(value_string, "\n", &value_string)) {
Luigi Semenzatofb3a8212013-05-07 16:55:00 -0700558 LOG(WARNING) << "no newline in " << value_string;
559 // Continue even though the lack of newline is suspicious.
560 }
561 if (!base::StringToInt(value_string, value)) {
562 LOG(WARNING) << "cannot convert " << value_string << " to int";
563 return false;
564 }
565 return true;
566}
567
568void MetricsDaemon::SendCpuThrottleMetrics() {
569 // |max_freq| is 0 only the first time through.
570 static int max_freq = 0;
571 if (max_freq == -1)
572 // Give up, as sysfs did not report max_freq correctly.
573 return;
574 if (max_freq == 0 || testing_) {
575 // One-time initialization of max_freq. (Every time when testing.)
576 if (!ReadFreqToInt(cpuinfo_max_freq_path_, &max_freq)) {
577 max_freq = -1;
578 return;
579 }
580 if (max_freq == 0) {
581 LOG(WARNING) << "sysfs reports 0 max CPU frequency\n";
582 max_freq = -1;
583 return;
584 }
585 if (max_freq % 10000 == 1000) {
586 // Special case: system has turbo mode, and max non-turbo frequency is
587 // max_freq - 1000. This relies on "normal" (non-turbo) frequencies
588 // being multiples of (at least) 10 MHz. Although there is no guarantee
589 // of this, it seems a fairly reasonable assumption. Otherwise we should
590 // read scaling_available_frequencies, sort the frequencies, compare the
591 // two highest ones, and check if they differ by 1000 (kHz) (and that's a
592 // hack too, no telling when it will change).
593 max_freq -= 1000;
594 }
595 }
596 int scaled_freq = 0;
597 if (!ReadFreqToInt(scaling_max_freq_path_, &scaled_freq))
598 return;
599 // Frequencies are in kHz. If scaled_freq > max_freq, turbo is on, but
600 // scaled_freq is not the actual turbo frequency. We indicate this situation
601 // with a 101% value.
602 int percent = scaled_freq > max_freq ? 101 : scaled_freq / (max_freq / 100);
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800603 SendLinearSample(kMetricScaledCpuFrequencyName, percent, 101, 102);
Luigi Semenzatofb3a8212013-05-07 16:55:00 -0700604}
605
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800606// static
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700607gboolean MetricsDaemon::StatsCallbackStatic(void* handle) {
608 (static_cast<MetricsDaemon*>(handle))->StatsCallback();
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800609 return false; // one-time callback
610}
611
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700612// Collects disk and vm stats alternating over a short and a long interval.
Luigi Semenzato8accd332011-05-17 16:37:18 -0700613
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700614void MetricsDaemon::StatsCallback() {
Sonny Rao4b8aebb2013-07-31 23:18:31 -0700615 long int read_sectors_now, write_sectors_now;
616 struct VmstatRecord vmstats_now;
Luigi Semenzato8accd332011-05-17 16:37:18 -0700617 double time_now = GetActiveTime();
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700618 double delta_time = time_now - stats_initial_time_;
Luigi Semenzato8accd332011-05-17 16:37:18 -0700619 if (testing_) {
620 // Fake the time when testing.
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700621 delta_time = stats_state_ == kStatsShort ?
622 kMetricStatsShortInterval : kMetricStatsLongInterval;
Luigi Semenzato8accd332011-05-17 16:37:18 -0700623 }
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700624 bool diskstats_success = DiskStatsReadStats(&read_sectors_now,
625 &write_sectors_now);
Luigi Semenzato8accd332011-05-17 16:37:18 -0700626 int delta_read = read_sectors_now - read_sectors_;
627 int delta_write = write_sectors_now - write_sectors_;
628 int read_sectors_per_second = delta_read / delta_time;
629 int write_sectors_per_second = delta_write / delta_time;
Sonny Rao4b8aebb2013-07-31 23:18:31 -0700630 bool vmstats_success = VmStatsReadStats(&vmstats_now);
631 uint64_t delta_faults = vmstats_now.page_faults_ - vmstats_.page_faults_;
632 uint64_t delta_swap_in = vmstats_now.swap_in_ - vmstats_.swap_in_;
633 uint64_t delta_swap_out = vmstats_now.swap_out_ - vmstats_.swap_out_;
634 uint64_t page_faults_per_second = delta_faults / delta_time;
635 uint64_t swap_in_per_second = delta_swap_in / delta_time;
636 uint64_t swap_out_per_second = delta_swap_out / delta_time;
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800637
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700638 switch (stats_state_) {
639 case kStatsShort:
640 if (diskstats_success) {
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800641 SendSample(kMetricReadSectorsShortName,
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700642 read_sectors_per_second,
643 1,
644 kMetricSectorsIOMax,
645 kMetricSectorsBuckets);
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800646 SendSample(kMetricWriteSectorsShortName,
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700647 write_sectors_per_second,
648 1,
649 kMetricSectorsIOMax,
650 kMetricSectorsBuckets);
651 }
652 if (vmstats_success) {
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800653 SendSample(kMetricPageFaultsShortName,
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700654 page_faults_per_second,
655 1,
656 kMetricPageFaultsMax,
657 kMetricPageFaultsBuckets);
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800658 SendSample(kMetricSwapInShortName,
Sonny Rao4b8aebb2013-07-31 23:18:31 -0700659 swap_in_per_second,
660 1,
661 kMetricPageFaultsMax,
662 kMetricPageFaultsBuckets);
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800663 SendSample(kMetricSwapOutShortName,
Sonny Rao4b8aebb2013-07-31 23:18:31 -0700664 swap_out_per_second,
665 1,
666 kMetricPageFaultsMax,
667 kMetricPageFaultsBuckets);
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700668 }
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800669 // Schedule long callback.
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700670 stats_state_ = kStatsLong;
671 ScheduleStatsCallback(kMetricStatsLongInterval -
672 kMetricStatsShortInterval);
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800673 break;
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700674 case kStatsLong:
675 if (diskstats_success) {
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800676 SendSample(kMetricReadSectorsLongName,
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700677 read_sectors_per_second,
678 1,
679 kMetricSectorsIOMax,
680 kMetricSectorsBuckets);
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800681 SendSample(kMetricWriteSectorsLongName,
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700682 write_sectors_per_second,
683 1,
684 kMetricSectorsIOMax,
685 kMetricSectorsBuckets);
686 // Reset sector counters.
687 read_sectors_ = read_sectors_now;
688 write_sectors_ = write_sectors_now;
689 }
690 if (vmstats_success) {
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800691 SendSample(kMetricPageFaultsLongName,
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700692 page_faults_per_second,
693 1,
694 kMetricPageFaultsMax,
695 kMetricPageFaultsBuckets);
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800696 SendSample(kMetricSwapInLongName,
Sonny Rao4b8aebb2013-07-31 23:18:31 -0700697 swap_in_per_second,
698 1,
699 kMetricPageFaultsMax,
700 kMetricPageFaultsBuckets);
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800701 SendSample(kMetricSwapOutLongName,
Sonny Rao4b8aebb2013-07-31 23:18:31 -0700702 swap_out_per_second,
703 1,
704 kMetricPageFaultsMax,
705 kMetricPageFaultsBuckets);
706
707 vmstats_ = vmstats_now;
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700708 }
Luigi Semenzatofb3a8212013-05-07 16:55:00 -0700709 SendCpuThrottleMetrics();
Luigi Semenzato8accd332011-05-17 16:37:18 -0700710 // Set start time for new cycle.
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700711 stats_initial_time_ = time_now;
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800712 // Schedule short callback.
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700713 stats_state_ = kStatsShort;
714 ScheduleStatsCallback(kMetricStatsShortInterval);
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800715 break;
716 default:
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700717 LOG(FATAL) << "Invalid stats state";
Luigi Semenzatoc88e42d2011-02-17 10:21:16 -0800718 }
719}
720
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700721void MetricsDaemon::ScheduleMeminfoCallback(int wait) {
722 if (testing_) {
723 return;
724 }
725 g_timeout_add_seconds(wait, MeminfoCallbackStatic, this);
726}
727
728// static
729gboolean MetricsDaemon::MeminfoCallbackStatic(void* handle) {
730 return (static_cast<MetricsDaemon*>(handle))->MeminfoCallback();
731}
732
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700733bool MetricsDaemon::MeminfoCallback() {
Luigi Semenzato8accd332011-05-17 16:37:18 -0700734 string meminfo_raw;
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700735 const FilePath meminfo_path("/proc/meminfo");
Ben Chan2e6543d2014-02-05 23:26:25 -0800736 if (!base::ReadFileToString(meminfo_path, &meminfo_raw)) {
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700737 LOG(WARNING) << "cannot read " << meminfo_path.value().c_str();
738 return false;
739 }
Luigi Semenzato8accd332011-05-17 16:37:18 -0700740 return ProcessMeminfo(meminfo_raw);
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700741}
742
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700743bool MetricsDaemon::ProcessMeminfo(const string& meminfo_raw) {
Luigi Semenzato8accd332011-05-17 16:37:18 -0700744 static const MeminfoRecord fields_array[] = {
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700745 { "MemTotal", "MemTotal" }, // SPECIAL CASE: total system memory
746 { "MemFree", "MemFree" },
747 { "Buffers", "Buffers" },
748 { "Cached", "Cached" },
749 // { "SwapCached", "SwapCached" },
750 { "Active", "Active" },
751 { "Inactive", "Inactive" },
752 { "ActiveAnon", "Active(anon)" },
753 { "InactiveAnon", "Inactive(anon)" },
754 { "ActiveFile" , "Active(file)" },
755 { "InactiveFile", "Inactive(file)" },
Luigi Semenzato942cbab2013-02-12 13:17:07 -0800756 { "Unevictable", "Unevictable", kMeminfoOp_HistLog },
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700757 // { "Mlocked", "Mlocked" },
Luigi Semenzato942cbab2013-02-12 13:17:07 -0800758 { "SwapTotal", "SwapTotal", kMeminfoOp_SwapTotal },
759 { "SwapFree", "SwapFree", kMeminfoOp_SwapFree },
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700760 // { "Dirty", "Dirty" },
761 // { "Writeback", "Writeback" },
762 { "AnonPages", "AnonPages" },
763 { "Mapped", "Mapped" },
Luigi Semenzato942cbab2013-02-12 13:17:07 -0800764 { "Shmem", "Shmem", kMeminfoOp_HistLog },
765 { "Slab", "Slab", kMeminfoOp_HistLog },
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700766 // { "SReclaimable", "SReclaimable" },
767 // { "SUnreclaim", "SUnreclaim" },
768 };
Luigi Semenzato8accd332011-05-17 16:37:18 -0700769 vector<MeminfoRecord> fields(fields_array,
770 fields_array + arraysize(fields_array));
771 if (!FillMeminfo(meminfo_raw, &fields)) {
772 return false;
773 }
774 int total_memory = fields[0].value;
775 if (total_memory == 0) {
776 // this "cannot happen"
777 LOG(WARNING) << "borked meminfo parser";
778 return false;
779 }
Luigi Semenzato942cbab2013-02-12 13:17:07 -0800780 int swap_total = 0;
781 int swap_free = 0;
Luigi Semenzato8accd332011-05-17 16:37:18 -0700782 // Send all fields retrieved, except total memory.
783 for (unsigned int i = 1; i < fields.size(); i++) {
Luigi Semenzato859b3f02014-02-05 15:33:19 -0800784 string metrics_name = base::StringPrintf("Platform.Meminfo%s",
785 fields[i].name);
Luigi Semenzato3ccca062013-02-04 19:50:45 -0800786 int percent;
Luigi Semenzato942cbab2013-02-12 13:17:07 -0800787 switch (fields[i].op) {
788 case kMeminfoOp_HistPercent:
Luigi Semenzato3ccca062013-02-04 19:50:45 -0800789 // report value as percent of total memory
790 percent = fields[i].value * 100 / total_memory;
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800791 SendLinearSample(metrics_name, percent, 100, 101);
Luigi Semenzato3ccca062013-02-04 19:50:45 -0800792 break;
Luigi Semenzato942cbab2013-02-12 13:17:07 -0800793 case kMeminfoOp_HistLog:
Luigi Semenzato3ccca062013-02-04 19:50:45 -0800794 // report value in kbytes, log scale, 4Gb max
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800795 SendSample(metrics_name, fields[i].value, 1, 4 * 1000 * 1000, 100);
Luigi Semenzato3ccca062013-02-04 19:50:45 -0800796 break;
Luigi Semenzato942cbab2013-02-12 13:17:07 -0800797 case kMeminfoOp_SwapTotal:
798 swap_total = fields[i].value;
799 case kMeminfoOp_SwapFree:
800 swap_free = fields[i].value;
Luigi Semenzato3ccca062013-02-04 19:50:45 -0800801 break;
Luigi Semenzato8accd332011-05-17 16:37:18 -0700802 }
803 }
Luigi Semenzato942cbab2013-02-12 13:17:07 -0800804 if (swap_total > 0) {
805 int swap_used = swap_total - swap_free;
806 int swap_used_percent = swap_used * 100 / swap_total;
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800807 SendSample("Platform.MeminfoSwapUsed", swap_used, 1, 8 * 1000 * 1000, 100);
808 SendLinearSample("Platform.MeminfoSwapUsedPercent", swap_used_percent,
Luigi Semenzato942cbab2013-02-12 13:17:07 -0800809 100, 101);
810 }
Luigi Semenzato8accd332011-05-17 16:37:18 -0700811 return true;
812}
813
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700814bool MetricsDaemon::FillMeminfo(const string& meminfo_raw,
815 vector<MeminfoRecord>* fields) {
Luigi Semenzato8accd332011-05-17 16:37:18 -0700816 vector<string> lines;
817 unsigned int nlines = Tokenize(meminfo_raw, "\n", &lines);
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700818
819 // Scan meminfo output and collect field values. Each field name has to
820 // match a meminfo entry (case insensitive) after removing non-alpha
821 // characters from the entry.
Luigi Semenzato8accd332011-05-17 16:37:18 -0700822 unsigned int ifield = 0;
823 for (unsigned int iline = 0;
824 iline < nlines && ifield < fields->size();
825 iline++) {
826 vector<string> tokens;
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700827 Tokenize(lines[iline], ": ", &tokens);
Luigi Semenzato8accd332011-05-17 16:37:18 -0700828 if (strcmp((*fields)[ifield].match, tokens[0].c_str()) == 0) {
829 // Name matches. Parse value and save.
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700830 char* rest;
Luigi Semenzato8accd332011-05-17 16:37:18 -0700831 (*fields)[ifield].value =
832 static_cast<int>(strtol(tokens[1].c_str(), &rest, 10));
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700833 if (*rest != '\0') {
834 LOG(WARNING) << "missing meminfo value";
835 return false;
836 }
Luigi Semenzato8accd332011-05-17 16:37:18 -0700837 ifield++;
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700838 }
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700839 }
Luigi Semenzato8accd332011-05-17 16:37:18 -0700840 if (ifield < fields->size()) {
841 // End of input reached while scanning.
842 LOG(WARNING) << "cannot find field " << (*fields)[ifield].match
843 << " and following";
844 return false;
845 }
846 return true;
847}
848
Luigi Semenzato0d9a9c92013-12-05 15:55:12 -0800849void MetricsDaemon::ScheduleMemuseCallback(double interval) {
Luigi Semenzato8accd332011-05-17 16:37:18 -0700850 if (testing_) {
851 return;
852 }
Luigi Semenzato0d9a9c92013-12-05 15:55:12 -0800853 g_timeout_add_seconds(interval, MemuseCallbackStatic, this);
Luigi Semenzato8accd332011-05-17 16:37:18 -0700854}
855
856// static
857gboolean MetricsDaemon::MemuseCallbackStatic(void* handle) {
858 MetricsDaemon* daemon = static_cast<MetricsDaemon*>(handle);
859 daemon->MemuseCallback();
860 return false;
861}
862
863void MetricsDaemon::MemuseCallback() {
864 // Since we only care about active time (i.e. uptime minus sleep time) but
865 // the callbacks are driven by real time (uptime), we check if we should
866 // reschedule this callback due to intervening sleep periods.
867 double now = GetActiveTime();
Luigi Semenzato0d9a9c92013-12-05 15:55:12 -0800868 // Avoid intervals of less than one second.
869 double remaining_time = ceil(memuse_final_time_ - now);
870 if (remaining_time > 0) {
871 ScheduleMemuseCallback(remaining_time);
Luigi Semenzato8accd332011-05-17 16:37:18 -0700872 } else {
Luigi Semenzato0d9a9c92013-12-05 15:55:12 -0800873 // Report stats and advance the measurement interval unless there are
874 // errors or we've completed the last interval.
Luigi Semenzato8accd332011-05-17 16:37:18 -0700875 if (MemuseCallbackWork() &&
Luigi Semenzato0d9a9c92013-12-05 15:55:12 -0800876 memuse_interval_index_ < arraysize(kMemuseIntervals)) {
877 double interval = kMemuseIntervals[memuse_interval_index_++];
878 memuse_final_time_ = now + interval;
879 ScheduleMemuseCallback(interval);
Luigi Semenzato8accd332011-05-17 16:37:18 -0700880 }
881 }
882}
883
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700884bool MetricsDaemon::MemuseCallbackWork() {
Luigi Semenzato8accd332011-05-17 16:37:18 -0700885 string meminfo_raw;
886 const FilePath meminfo_path("/proc/meminfo");
Ben Chan2e6543d2014-02-05 23:26:25 -0800887 if (!base::ReadFileToString(meminfo_path, &meminfo_raw)) {
Luigi Semenzato8accd332011-05-17 16:37:18 -0700888 LOG(WARNING) << "cannot read " << meminfo_path.value().c_str();
889 return false;
890 }
891 return ProcessMemuse(meminfo_raw);
892}
893
Luigi Semenzato5bd764f2011-10-14 12:03:35 -0700894bool MetricsDaemon::ProcessMemuse(const string& meminfo_raw) {
Luigi Semenzato8accd332011-05-17 16:37:18 -0700895 static const MeminfoRecord fields_array[] = {
896 { "MemTotal", "MemTotal" }, // SPECIAL CASE: total system memory
897 { "ActiveAnon", "Active(anon)" },
898 { "InactiveAnon", "Inactive(anon)" },
899 };
900 vector<MeminfoRecord> fields(fields_array,
901 fields_array + arraysize(fields_array));
902 if (!FillMeminfo(meminfo_raw, &fields)) {
903 return false;
904 }
905 int total = fields[0].value;
906 int active_anon = fields[1].value;
907 int inactive_anon = fields[2].value;
908 if (total == 0) {
909 // this "cannot happen"
910 LOG(WARNING) << "borked meminfo parser";
911 return false;
912 }
Luigi Semenzato859b3f02014-02-05 15:33:19 -0800913 string metrics_name = base::StringPrintf("Platform.MemuseAnon%d",
914 memuse_interval_index_);
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800915 SendLinearSample(metrics_name, (active_anon + inactive_anon) * 100 / total,
Luigi Semenzato8accd332011-05-17 16:37:18 -0700916 100, 101);
917 return true;
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700918}
919
Luigi Semenzato5ef2e392014-04-15 15:15:02 -0700920void MetricsDaemon::ReportDailyUse(int use_seconds) {
921 if (use_seconds <= 0)
Darin Petkov1bb904e2010-06-16 15:58:06 -0700922 return;
923
Luigi Semenzato5ef2e392014-04-15 15:15:02 -0700924 int minutes = (use_seconds + kSecondsPerMinute / 2) / kSecondsPerMinute;
925 SendSample("Logging.DailyUseTime",
926 minutes,
927 1,
928 kMinutesPerDay * 30 * 2, // cumulative---two months worth
929 50);
Darin Petkovf1e85e42010-06-10 15:59:53 -0700930}
931
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800932void MetricsDaemon::SendSample(const string& name, int sample,
Darin Petkov11b8eb32010-05-18 11:00:59 -0700933 int min, int max, int nbuckets) {
Darin Petkovfc91b422010-05-12 13:05:45 -0700934 metrics_lib_->SendToUMA(name, sample, min, max, nbuckets);
Darin Petkov65b01462010-04-14 13:32:20 -0700935}
Luigi Semenzato29c7ef92011-04-12 14:12:35 -0700936
Luigi Semenzatoe5883fa2014-04-18 17:00:35 -0700937void MetricsDaemon::SendKernelCrashesCumulativeCountStats() {
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800938 // Report the number of crashes for this OS version, but don't clear the
939 // counter. It is cleared elsewhere on version change.
Luigi Semenzatoba0c65d2014-03-17 12:28:38 -0700940 int64 crashes_count = kernel_crashes_version_count_->Get();
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800941 SendSample(kernel_crashes_version_count_->Name(),
Luigi Semenzatoba0c65d2014-03-17 12:28:38 -0700942 crashes_count,
943 1, // value of first bucket
944 500, // value of last bucket
945 100); // number of buckets
946
947
948 int64 cpu_use_ms = version_cumulative_cpu_use_->Get();
949 SendSample(version_cumulative_cpu_use_->Name(),
950 cpu_use_ms / 1000, // stat is in seconds
951 1, // device may be used very little...
952 8 * 1000 * 1000, // ... or a lot (a little over 90 days)
953 100);
954
955 // On the first run after an autoupdate, cpu_use_ms and active_use_seconds
956 // can be zero. Avoid division by zero.
957 if (cpu_use_ms > 0) {
958 // Send the crash frequency since update in number of crashes per CPU year.
959 SendSample("Logging.KernelCrashesPerCpuYear",
960 crashes_count * kSecondsPerDay * 365 * 1000 / cpu_use_ms,
961 1,
962 1000 * 1000, // about one crash every 30s of CPU time
963 100);
964 }
965
Luigi Semenzatoe5883fa2014-04-18 17:00:35 -0700966 int64 active_use_seconds = version_cumulative_active_use_->Get();
Luigi Semenzatoba0c65d2014-03-17 12:28:38 -0700967 if (active_use_seconds > 0) {
Luigi Semenzatoe5883fa2014-04-18 17:00:35 -0700968 SendSample(version_cumulative_active_use_->Name(),
969 active_use_seconds / 1000, // stat is in seconds
970 1, // device may be used very little...
971 8 * 1000 * 1000, // ... or a lot (about 90 days)
972 100);
Luigi Semenzatoba0c65d2014-03-17 12:28:38 -0700973 // Same as above, but per year of active time.
974 SendSample("Logging.KernelCrashesPerActiveYear",
975 crashes_count * kSecondsPerDay * 365 / active_use_seconds,
976 1,
977 1000 * 1000, // about one crash every 30s of active time
978 100);
979 }
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800980}
981
Luigi Semenzatoe5883fa2014-04-18 17:00:35 -0700982void MetricsDaemon::SendDailyUseSample(
983 const scoped_ptr<PersistentInteger>& use) {
984 SendSample(use->Name(),
985 use->GetAndClear(),
986 1, // value of first bucket
987 kSecondsPerDay, // value of last bucket
988 50); // number of buckets
989}
990
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800991void MetricsDaemon::SendCrashIntervalSample(
992 const scoped_ptr<PersistentInteger>& interval) {
993 SendSample(interval->Name(),
994 interval->GetAndClear(),
995 1, // value of first bucket
996 4 * kSecondsPerWeek, // value of last bucket
997 50); // number of buckets
998}
999
1000void MetricsDaemon::SendCrashFrequencySample(
1001 const scoped_ptr<PersistentInteger>& frequency) {
1002 SendSample(frequency->Name(),
1003 frequency->GetAndClear(),
1004 1, // value of first bucket
1005 100, // value of last bucket
1006 50); // number of buckets
1007}
1008
1009void MetricsDaemon::SendLinearSample(const string& name, int sample,
Luigi Semenzato29c7ef92011-04-12 14:12:35 -07001010 int max, int nbuckets) {
Luigi Semenzato29c7ef92011-04-12 14:12:35 -07001011 // TODO(semenzato): add a proper linear histogram to the Chrome external
1012 // metrics API.
1013 LOG_IF(FATAL, nbuckets != max + 1) << "unsupported histogram scale";
1014 metrics_lib_->SendEnumToUMA(name, sample, max);
1015}
Daniel Eratc83975a2014-04-04 08:53:44 -07001016
1017void MetricsDaemon::UpdateStats(TimeTicks now_ticks,
1018 Time now_wall_time) {
1019 const int elapsed_seconds = (now_ticks - last_update_stats_time_).InSeconds();
Luigi Semenzatoe5883fa2014-04-18 17:00:35 -07001020 daily_active_use_->Add(elapsed_seconds);
1021 version_cumulative_active_use_->Add(elapsed_seconds);
Daniel Eratc83975a2014-04-04 08:53:44 -07001022 user_crash_interval_->Add(elapsed_seconds);
1023 kernel_crash_interval_->Add(elapsed_seconds);
1024 version_cumulative_cpu_use_->Add(GetIncrementalCpuUse().InMilliseconds());
1025 last_update_stats_time_ = now_ticks;
1026
1027 const TimeDelta since_epoch = now_wall_time - Time::UnixEpoch();
1028 const int day = since_epoch.InDays();
1029 const int week = day / 7;
1030
1031 if (daily_cycle_->Get() != day) {
1032 daily_cycle_->Set(day);
Luigi Semenzatoe5883fa2014-04-18 17:00:35 -07001033 SendDailyUseSample(daily_active_use_);
1034 SendDailyUseSample(version_cumulative_active_use_);
Daniel Eratc83975a2014-04-04 08:53:44 -07001035 SendCrashFrequencySample(any_crashes_daily_count_);
1036 SendCrashFrequencySample(user_crashes_daily_count_);
1037 SendCrashFrequencySample(kernel_crashes_daily_count_);
1038 SendCrashFrequencySample(unclean_shutdowns_daily_count_);
Luigi Semenzatoe5883fa2014-04-18 17:00:35 -07001039 SendKernelCrashesCumulativeCountStats();
Daniel Eratc83975a2014-04-04 08:53:44 -07001040 }
1041
1042 if (weekly_cycle_->Get() != week) {
1043 weekly_cycle_->Set(week);
1044 SendCrashFrequencySample(any_crashes_weekly_count_);
1045 SendCrashFrequencySample(user_crashes_weekly_count_);
1046 SendCrashFrequencySample(kernel_crashes_weekly_count_);
1047 SendCrashFrequencySample(unclean_shutdowns_weekly_count_);
1048 }
1049}
1050
1051// static
1052gboolean MetricsDaemon::HandleUpdateStatsTimeout(gpointer data) {
1053 static_cast<MetricsDaemon*>(data)->UpdateStats(TimeTicks::Now(), Time::Now());
1054 return TRUE;
1055}