Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "metrics_daemon.h" | 5 #include "metrics_daemon.h" |
| 6 | 6 |
| 7 #include <fcntl.h> | |
| 7 #include <string.h> | 8 #include <string.h> |
| 8 | 9 |
| 9 #include <base/file_util.h> | 10 #include <base/file_util.h> |
| 10 #include <base/logging.h> | 11 #include <base/logging.h> |
| 11 #include <dbus/dbus-glib-lowlevel.h> | 12 #include <dbus/dbus-glib-lowlevel.h> |
| 12 | 13 |
| 13 #include "counter.h" | 14 #include "counter.h" |
| 14 | 15 |
| 15 using base::Time; | 16 using base::Time; |
| 16 using base::TimeDelta; | 17 using base::TimeDelta; |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 77 const char MetricsDaemon::kMetricUncleanShutdownsWeeklyName[] = | 78 const char MetricsDaemon::kMetricUncleanShutdownsWeeklyName[] = |
| 78 "Logging.UncleanShutdownsWeekly"; | 79 "Logging.UncleanShutdownsWeekly"; |
| 79 const char MetricsDaemon::kMetricUserCrashesDailyName[] = | 80 const char MetricsDaemon::kMetricUserCrashesDailyName[] = |
| 80 "Logging.UserCrashesDaily"; | 81 "Logging.UserCrashesDaily"; |
| 81 const char MetricsDaemon::kMetricUserCrashesWeeklyName[] = | 82 const char MetricsDaemon::kMetricUserCrashesWeeklyName[] = |
| 82 "Logging.UserCrashesWeekly"; | 83 "Logging.UserCrashesWeekly"; |
| 83 const char MetricsDaemon::kMetricCrashFrequencyMin = 1; | 84 const char MetricsDaemon::kMetricCrashFrequencyMin = 1; |
| 84 const char MetricsDaemon::kMetricCrashFrequencyMax = 100; | 85 const char MetricsDaemon::kMetricCrashFrequencyMax = 100; |
| 85 const char MetricsDaemon::kMetricCrashFrequencyBuckets = 50; | 86 const char MetricsDaemon::kMetricCrashFrequencyBuckets = 50; |
| 86 | 87 |
| 88 // disk stats metrics | |
| 89 | |
| 90 // The {Read,Write}Sectors numbers are in sectors/second. | |
| 91 // A sector is usually 512 bytes. | |
| 92 | |
| 93 const char MetricsDaemon::kMetricReadSectorsLongName[] = | |
| 94 "Filesystem.ReadSectorsLong"; | |
| 95 const char MetricsDaemon::kMetricWriteSectorsLongName[] = | |
| 96 "Filesystem.WriteSectorsLong"; | |
| 97 const char MetricsDaemon::kMetricReadSectorsShortName[] = | |
| 98 "Filesystem.ReadSectorsShort"; | |
| 99 const char MetricsDaemon::kMetricWriteSectorsShortName[] = | |
| 100 "Filesystem.WriteSectorsShort"; | |
| 101 | |
| 102 const int MetricsDaemon::kMetricDiskStatsShortInterval = 1; // seconds | |
| 103 const int MetricsDaemon::kMetricDiskStatsLongInterval = 30; // seconds | |
| 104 | |
| 105 // Assume a max rate of 250Mb/s for reads (worse for writes) and 512 byte | |
| 106 // sectors. | |
| 107 const int MetricsDaemon::kMetricSectorsIOMax = 500000; // sectors/second | |
| 108 const int MetricsDaemon::kMetricSectorsBuckets = 50; // buckets | |
| 109 | |
| 87 // persistent metrics path | 110 // persistent metrics path |
| 88 const char MetricsDaemon::kMetricsPath[] = "/var/log/metrics"; | 111 const char MetricsDaemon::kMetricsPath[] = "/var/log/metrics"; |
| 89 | 112 |
| 90 | 113 |
| 91 // static | 114 // static |
| 92 const char* MetricsDaemon::kDBusMatches_[] = { | 115 const char* MetricsDaemon::kDBusMatches_[] = { |
| 93 "type='signal'," | 116 "type='signal'," |
| 94 "interface='" DBUS_IFACE_CRASH_REPORTER "'," | 117 "interface='" DBUS_IFACE_CRASH_REPORTER "'," |
| 95 "path='/'," | 118 "path='/'," |
| 96 "member='UserCrash'", | 119 "member='UserCrash'", |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 111 #define STATE(name, capname) #name, | 134 #define STATE(name, capname) #name, |
| 112 #include "power_states.h" | 135 #include "power_states.h" |
| 113 }; | 136 }; |
| 114 | 137 |
| 115 // static | 138 // static |
| 116 const char* MetricsDaemon::kSessionStates_[] = { | 139 const char* MetricsDaemon::kSessionStates_[] = { |
| 117 #define STATE(name, capname) #name, | 140 #define STATE(name, capname) #name, |
| 118 #include "session_states.h" | 141 #include "session_states.h" |
| 119 }; | 142 }; |
| 120 | 143 |
| 121 MetricsDaemon::MetricsDaemon() | 144 MetricsDaemon::MetricsDaemon() |
|
petkov
2011/02/14 23:39:16
initialize diskstats_path_ to NULL here.
Luigi Semenzato
2011/02/15 17:45:47
Thanks.
| |
| 122 : power_state_(kUnknownPowerState), | 145 : power_state_(kUnknownPowerState), |
| 123 session_state_(kUnknownSessionState), | 146 session_state_(kUnknownSessionState), |
| 124 user_active_(false), | 147 user_active_(false), |
| 125 usemon_interval_(0), | 148 usemon_interval_(0), |
| 126 usemon_source_(NULL) {} | 149 usemon_source_(NULL) {} |
| 127 | 150 |
| 128 MetricsDaemon::~MetricsDaemon() { | 151 MetricsDaemon::~MetricsDaemon() { |
| 129 DeleteFrequencyCounters(); | 152 DeleteFrequencyCounters(); |
| 130 } | 153 } |
| 131 | 154 |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 183 time_t cycle_duration = strstr(histogram_name, "Weekly") != NULL ? | 206 time_t cycle_duration = strstr(histogram_name, "Weekly") != NULL ? |
| 184 chromeos_metrics::kSecondsPerWeek : | 207 chromeos_metrics::kSecondsPerWeek : |
| 185 chromeos_metrics::kSecondsPerDay; | 208 chromeos_metrics::kSecondsPerDay; |
| 186 new_counter->Init( | 209 new_counter->Init( |
| 187 static_cast<chromeos_metrics::TaggedCounterInterface*>( | 210 static_cast<chromeos_metrics::TaggedCounterInterface*>( |
| 188 reporter.release()), | 211 reporter.release()), |
| 189 cycle_duration); | 212 cycle_duration); |
| 190 frequency_counters_[histogram_name] = new_counter.release(); | 213 frequency_counters_[histogram_name] = new_counter.release(); |
| 191 } | 214 } |
| 192 | 215 |
| 193 void MetricsDaemon::Init(bool testing, MetricsLibraryInterface* metrics_lib) { | 216 void MetricsDaemon::Init(bool testing, MetricsLibraryInterface* metrics_lib, |
| 217 const char* diskstats_path) { | |
| 194 testing_ = testing; | 218 testing_ = testing; |
| 195 DCHECK(metrics_lib != NULL); | 219 DCHECK(metrics_lib != NULL); |
| 196 metrics_lib_ = metrics_lib; | 220 metrics_lib_ = metrics_lib; |
| 197 chromeos_metrics::TaggedCounterReporter:: | 221 chromeos_metrics::TaggedCounterReporter:: |
| 198 SetMetricsLibraryInterface(metrics_lib); | 222 SetMetricsLibraryInterface(metrics_lib); |
| 199 | 223 |
| 200 static const char kDailyUseRecordFile[] = "/var/log/metrics/daily-usage"; | 224 static const char kDailyUseRecordFile[] = "/var/log/metrics/daily-usage"; |
| 201 daily_use_.reset(new chromeos_metrics::TaggedCounter()); | 225 daily_use_.reset(new chromeos_metrics::TaggedCounter()); |
| 202 daily_use_->Init(kDailyUseRecordFile, &ReportDailyUse, this); | 226 daily_use_->Init(kDailyUseRecordFile, &ReportDailyUse, this); |
| 203 | 227 |
| 204 ConfigureCrashIntervalReporter(kMetricKernelCrashIntervalName, | 228 ConfigureCrashIntervalReporter(kMetricKernelCrashIntervalName, |
| 205 &kernel_crash_interval_); | 229 &kernel_crash_interval_); |
| 206 ConfigureCrashIntervalReporter(kMetricUncleanShutdownIntervalName, | 230 ConfigureCrashIntervalReporter(kMetricUncleanShutdownIntervalName, |
| 207 &unclean_shutdown_interval_); | 231 &unclean_shutdown_interval_); |
| 208 ConfigureCrashIntervalReporter(kMetricUserCrashIntervalName, | 232 ConfigureCrashIntervalReporter(kMetricUserCrashIntervalName, |
| 209 &user_crash_interval_); | 233 &user_crash_interval_); |
| 210 | 234 |
| 211 DeleteFrequencyCounters(); | 235 DeleteFrequencyCounters(); |
| 212 ConfigureCrashFrequencyReporter(kMetricAnyCrashesDailyName); | 236 ConfigureCrashFrequencyReporter(kMetricAnyCrashesDailyName); |
| 213 ConfigureCrashFrequencyReporter(kMetricAnyCrashesWeeklyName); | 237 ConfigureCrashFrequencyReporter(kMetricAnyCrashesWeeklyName); |
| 214 ConfigureCrashFrequencyReporter(kMetricKernelCrashesDailyName); | 238 ConfigureCrashFrequencyReporter(kMetricKernelCrashesDailyName); |
| 215 ConfigureCrashFrequencyReporter(kMetricKernelCrashesWeeklyName); | 239 ConfigureCrashFrequencyReporter(kMetricKernelCrashesWeeklyName); |
| 216 ConfigureCrashFrequencyReporter(kMetricUncleanShutdownsDailyName); | 240 ConfigureCrashFrequencyReporter(kMetricUncleanShutdownsDailyName); |
| 217 ConfigureCrashFrequencyReporter(kMetricUncleanShutdownsWeeklyName); | 241 ConfigureCrashFrequencyReporter(kMetricUncleanShutdownsWeeklyName); |
| 218 ConfigureCrashFrequencyReporter(kMetricUserCrashesDailyName); | 242 ConfigureCrashFrequencyReporter(kMetricUserCrashesDailyName); |
| 219 ConfigureCrashFrequencyReporter(kMetricUserCrashesWeeklyName); | 243 ConfigureCrashFrequencyReporter(kMetricUserCrashesWeeklyName); |
| 220 | 244 |
| 245 diskstats_path_ = diskstats_path; | |
| 246 DiskStatsReporterInit(); | |
| 247 | |
| 221 // Don't setup D-Bus and GLib in test mode. | 248 // Don't setup D-Bus and GLib in test mode. |
| 222 if (testing) | 249 if (testing) |
| 223 return; | 250 return; |
| 224 | 251 |
| 225 g_thread_init(NULL); | 252 g_thread_init(NULL); |
| 226 g_type_init(); | 253 g_type_init(); |
| 227 dbus_g_thread_init(); | 254 dbus_g_thread_init(); |
| 228 | 255 |
| 229 DBusError error; | 256 DBusError error; |
| 230 dbus_error_init(&error); | 257 dbus_error_init(&error); |
| (...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 487 // If there's a use monitor scheduled already, destroys it. | 514 // If there's a use monitor scheduled already, destroys it. |
| 488 if (usemon_source_ == NULL) | 515 if (usemon_source_ == NULL) |
| 489 return; | 516 return; |
| 490 | 517 |
| 491 DLOG(INFO) << "destroying use monitor"; | 518 DLOG(INFO) << "destroying use monitor"; |
| 492 g_source_destroy(usemon_source_); | 519 g_source_destroy(usemon_source_); |
| 493 usemon_source_ = NULL; | 520 usemon_source_ = NULL; |
| 494 usemon_interval_ = 0; | 521 usemon_interval_ = 0; |
| 495 } | 522 } |
| 496 | 523 |
| 524 void MetricsDaemon::DiskStatsReporterInit() { | |
| 525 DiskStatsReadStats(&read_sectors_, &write_sectors_); | |
| 526 // The first time around just run the long stat, so we don't delay boot. | |
| 527 diskstats_state_ = kDiskStatsLong; | |
| 528 ScheduleDiskStatsCallback(kMetricDiskStatsLongInterval); | |
| 529 } | |
| 530 | |
| 531 void MetricsDaemon::ScheduleDiskStatsCallback(int wait) { | |
| 532 if (testing_) { | |
| 533 return; | |
| 534 } | |
| 535 g_timeout_add_seconds(wait, DiskStatsCallbackStatic, this); | |
| 536 } | |
| 537 | |
| 538 void MetricsDaemon::DiskStatsReadStats(long int* read_sectors, | |
| 539 long int* write_sectors) { | |
| 540 int nchars; | |
| 541 int nitems; | |
| 542 char line[200]; | |
| 543 int file = HANDLE_EINTR(open(diskstats_path_, O_RDONLY)); | |
| 544 if (file < 0) { | |
| 545 PLOG(WARNING) << "cannot open " << diskstats_path_; | |
| 546 return; | |
| 547 } | |
| 548 nchars = HANDLE_EINTR(read(file, line, sizeof(line))); | |
| 549 if (nchars < 0) { | |
| 550 PLOG(WARNING) << "cannot read from " << diskstats_path_; | |
| 551 } else { | |
| 552 LOG_IF(WARNING, nchars == sizeof(line)) << "line too long in " | |
| 553 << diskstats_path_; | |
| 554 line[nchars] = '\0'; | |
| 555 nitems = sscanf(line, "%*d %*d %ld %*d %*d %*d %ld", | |
| 556 read_sectors, write_sectors); | |
| 557 LOG_IF(WARNING, nitems != 2) << "found " << nitems << " items in " | |
| 558 << diskstats_path_ << ", expected 2"; | |
| 559 } | |
| 560 HANDLE_EINTR(close(file)); | |
| 561 } | |
| 562 | |
| 563 // static | |
| 564 gboolean MetricsDaemon::DiskStatsCallbackStatic(void* handle) { | |
| 565 (static_cast<MetricsDaemon*>(handle))->DiskStatsCallback(); | |
| 566 return false; // one-time callback | |
| 567 } | |
| 568 | |
| 569 void MetricsDaemon::DiskStatsCallback() { | |
| 570 long int read_sectors_now, write_sectors_now; | |
| 571 DiskStatsReadStats(&read_sectors_now, &write_sectors_now); | |
| 572 | |
| 573 switch (diskstats_state_) { | |
| 574 case kDiskStatsShort: | |
| 575 SendMetric(kMetricReadSectorsShortName, | |
| 576 (int) (read_sectors_now - read_sectors_) / | |
| 577 kMetricDiskStatsShortInterval, | |
| 578 0, | |
|
petkov
2011/02/14 23:39:16
did you double check that it's OK for this to be 0
Luigi Semenzato
2011/02/15 17:45:47
No, it should be 1. Fixing.
| |
| 579 kMetricSectorsIOMax, | |
| 580 kMetricSectorsBuckets); | |
| 581 SendMetric(kMetricWriteSectorsShortName, | |
| 582 (int) (write_sectors_now - write_sectors_) / | |
| 583 kMetricDiskStatsShortInterval, | |
| 584 0, | |
| 585 kMetricSectorsIOMax, | |
| 586 kMetricSectorsBuckets); | |
| 587 // Schedule long callback. | |
| 588 diskstats_state_ = kDiskStatsLong; | |
| 589 ScheduleDiskStatsCallback(kMetricDiskStatsLongInterval - | |
| 590 kMetricDiskStatsShortInterval); | |
| 591 break; | |
| 592 case kDiskStatsLong: | |
| 593 SendMetric(kMetricReadSectorsLongName, | |
| 594 (int) (read_sectors_now - read_sectors_) / | |
| 595 kMetricDiskStatsLongInterval, | |
| 596 1, | |
| 597 kMetricSectorsIOMax, | |
| 598 kMetricSectorsBuckets); | |
| 599 SendMetric(kMetricWriteSectorsLongName, | |
| 600 (int) (write_sectors_now - write_sectors_) / | |
| 601 kMetricDiskStatsLongInterval, | |
| 602 1, | |
| 603 kMetricSectorsIOMax, | |
| 604 kMetricSectorsBuckets); | |
| 605 // Reset sector counters | |
| 606 read_sectors_ = read_sectors_now; | |
| 607 write_sectors_ = write_sectors_now; | |
| 608 // Schedule short callback. | |
| 609 diskstats_state_ = kDiskStatsShort; | |
| 610 ScheduleDiskStatsCallback(kMetricDiskStatsShortInterval); | |
| 611 break; | |
| 612 default: | |
| 613 LOG(FATAL) << "Invalid disk stats state"; | |
| 614 } | |
| 615 } | |
| 616 | |
| 497 // static | 617 // static |
| 498 void MetricsDaemon::ReportDailyUse(void* handle, int tag, int count) { | 618 void MetricsDaemon::ReportDailyUse(void* handle, int tag, int count) { |
| 499 if (count <= 0) | 619 if (count <= 0) |
| 500 return; | 620 return; |
| 501 | 621 |
| 502 MetricsDaemon* daemon = static_cast<MetricsDaemon*>(handle); | 622 MetricsDaemon* daemon = static_cast<MetricsDaemon*>(handle); |
| 503 int minutes = (count + kSecondsPerMinute / 2) / kSecondsPerMinute; | 623 int minutes = (count + kSecondsPerMinute / 2) / kSecondsPerMinute; |
| 504 daemon->SendMetric(kMetricDailyUseTimeName, minutes, | 624 daemon->SendMetric(kMetricDailyUseTimeName, minutes, |
| 505 kMetricDailyUseTimeMin, | 625 kMetricDailyUseTimeMin, |
| 506 kMetricDailyUseTimeMax, | 626 kMetricDailyUseTimeMax, |
| 507 kMetricDailyUseTimeBuckets); | 627 kMetricDailyUseTimeBuckets); |
| 508 } | 628 } |
| 509 | 629 |
| 510 void MetricsDaemon::SendMetric(const string& name, int sample, | 630 void MetricsDaemon::SendMetric(const string& name, int sample, |
| 511 int min, int max, int nbuckets) { | 631 int min, int max, int nbuckets) { |
| 512 DLOG(INFO) << "received metric: " << name << " " << sample << " " | 632 DLOG(INFO) << "received metric: " << name << " " << sample << " " |
| 513 << min << " " << max << " " << nbuckets; | 633 << min << " " << max << " " << nbuckets; |
| 514 metrics_lib_->SendToUMA(name, sample, min, max, nbuckets); | 634 metrics_lib_->SendToUMA(name, sample, min, max, nbuckets); |
| 515 } | 635 } |
| OLD | NEW |