metrics_daemon: restore correct meaning of Logging.DailyUse.

Maintain separate persistent values for DailyActiveUse and
CumulativeActiveUse.  The latter is used to compute crash rates
relative to active use.

BUG=chromium:364746
TEST=manual

Change-Id: I4d0485016eb49b30a77169ba1cedc1ffbff8810e
Reviewed-on: https://chromium-review.googlesource.com/195698
Reviewed-by: Daniel Erat <derat@chromium.org>
Reviewed-by: Luigi Semenzato <semenzato@chromium.org>
Tested-by: Luigi Semenzato <semenzato@chromium.org>
Commit-Queue: Luigi Semenzato <semenzato@chromium.org>
diff --git a/metrics/metrics_daemon.cc b/metrics/metrics_daemon.cc
index d1eb25e..97c099c 100644
--- a/metrics/metrics_daemon.cc
+++ b/metrics/metrics_daemon.cc
@@ -170,6 +170,7 @@
   if (version_cycle_->Get() != version) {
     version_cycle_->Set(version);
     kernel_crashes_version_count_->Set(0);
+    version_cumulative_active_use_->Set(0);
     version_cumulative_cpu_use_->Set(0);
   }
 
@@ -206,8 +207,10 @@
   // Sysconf cannot fail, so no sanity checks are needed.
   ticks_per_second_ = sysconf(_SC_CLK_TCK);
 
-  daily_use_.reset(
+  daily_active_use_.reset(
       new PersistentInteger("Logging.DailyUseTime"));
+  version_cumulative_active_use_.reset(
+      new PersistentInteger("Logging.CumulativeDailyUseTime"));
   version_cumulative_cpu_use_.reset(
       new PersistentInteger("Logging.CumulativeCpuTime"));
 
@@ -931,8 +934,7 @@
   metrics_lib_->SendToUMA(name, sample, min, max, nbuckets);
 }
 
-void MetricsDaemon::SendKernelCrashesCumulativeCountStats(
-    int64 active_use_seconds) {
+void MetricsDaemon::SendKernelCrashesCumulativeCountStats() {
   // Report the number of crashes for this OS version, but don't clear the
   // counter.  It is cleared elsewhere on version change.
   int64 crashes_count = kernel_crashes_version_count_->Get();
@@ -961,7 +963,13 @@
                100);
   }
 
+  int64 active_use_seconds = version_cumulative_active_use_->Get();
   if (active_use_seconds > 0) {
+    SendSample(version_cumulative_active_use_->Name(),
+               active_use_seconds / 1000,  // stat is in seconds
+               1,                          // device may be used very little...
+               8 * 1000 * 1000,            // ... or a lot (about 90 days)
+               100);
     // Same as above, but per year of active time.
     SendSample("Logging.KernelCrashesPerActiveYear",
                crashes_count * kSecondsPerDay * 365 / active_use_seconds,
@@ -971,6 +979,15 @@
   }
 }
 
+void MetricsDaemon::SendDailyUseSample(
+    const scoped_ptr<PersistentInteger>& use) {
+  SendSample(use->Name(),
+             use->GetAndClear(),
+             1,                        // value of first bucket
+             kSecondsPerDay,           // value of last bucket
+             50);                      // number of buckets
+}
+
 void MetricsDaemon::SendCrashIntervalSample(
     const scoped_ptr<PersistentInteger>& interval) {
   SendSample(interval->Name(),
@@ -1000,7 +1017,8 @@
 void MetricsDaemon::UpdateStats(TimeTicks now_ticks,
                                 Time now_wall_time) {
   const int elapsed_seconds = (now_ticks - last_update_stats_time_).InSeconds();
-  daily_use_->Add(elapsed_seconds);
+  daily_active_use_->Add(elapsed_seconds);
+  version_cumulative_active_use_->Add(elapsed_seconds);
   user_crash_interval_->Add(elapsed_seconds);
   kernel_crash_interval_->Add(elapsed_seconds);
   version_cumulative_cpu_use_->Add(GetIncrementalCpuUse().InMilliseconds());
@@ -1012,11 +1030,13 @@
 
   if (daily_cycle_->Get() != day) {
     daily_cycle_->Set(day);
+    SendDailyUseSample(daily_active_use_);
+    SendDailyUseSample(version_cumulative_active_use_);
     SendCrashFrequencySample(any_crashes_daily_count_);
     SendCrashFrequencySample(user_crashes_daily_count_);
     SendCrashFrequencySample(kernel_crashes_daily_count_);
     SendCrashFrequencySample(unclean_shutdowns_daily_count_);
-    SendKernelCrashesCumulativeCountStats(daily_use_->Get());
+    SendKernelCrashesCumulativeCountStats();
   }
 
   if (weekly_cycle_->Get() != week) {
diff --git a/metrics/metrics_daemon.h b/metrics/metrics_daemon.h
index 929706a..81e5386 100644
--- a/metrics/metrics_daemon.h
+++ b/metrics/metrics_daemon.h
@@ -175,12 +175,16 @@
 
   // Sends various cumulative kernel crash-related stats, for instance the
   // total number of kernel crashes since the last version update.
-  void SendKernelCrashesCumulativeCountStats(int64 active_time_seconds);
+  void SendKernelCrashesCumulativeCountStats();
 
   // Returns the total (system-wide) CPU usage between the time of the most
   // recent call to this function and now.
   base::TimeDelta GetIncrementalCpuUse();
 
+  // Sends a sample representing the number of seconds of active use
+  // for a 24-hour period.
+  void SendDailyUseSample(const scoped_ptr<PersistentInteger>& use);
+
   // Sends a sample representing a time interval between two crashes of the
   // same type.
   void SendCrashIntervalSample(const scoped_ptr<PersistentInteger>& interval);
@@ -316,7 +320,10 @@
   scoped_ptr<PersistentInteger> weekly_cycle_;
   scoped_ptr<PersistentInteger> version_cycle_;
 
-  scoped_ptr<PersistentInteger> daily_use_;
+  // Active use accumulated in a day.
+  scoped_ptr<PersistentInteger> daily_active_use_;
+  // Active use accumulated since the latest version update.
+  scoped_ptr<PersistentInteger> version_cumulative_active_use_;
 
   // The CPU time accumulator.  This contains the CPU time, in milliseconds,
   // used by the system since the most recent OS version update.
diff --git a/metrics/metrics_daemon_test.cc b/metrics/metrics_daemon_test.cc
index 4217d51..264d5cd 100644
--- a/metrics/metrics_daemon_test.cc
+++ b/metrics/metrics_daemon_test.cc
@@ -65,9 +65,9 @@
     base::CreateDirectory(FilePath(kTestDir));
 
     // Replace original persistent values with mock ones.
-    daily_use_mock_ =
+    daily_active_use_mock_ =
         new StrictMock<PersistentIntegerMock>("1.mock");
-    daemon_.daily_use_.reset(daily_use_mock_);
+    daemon_.daily_active_use_.reset(daily_active_use_mock_);
 
     kernel_crash_interval_mock_ =
         new StrictMock<PersistentIntegerMock>("2.mock");
@@ -92,7 +92,7 @@
   // Adds active use aggregation counters update expectations that the
   // specified count will be added.
   void ExpectActiveUseUpdate(int count) {
-    EXPECT_CALL(*daily_use_mock_, Add(count))
+    EXPECT_CALL(*daily_active_use_mock_, Add(count))
         .Times(1)
         .RetiresOnSaturation();
     EXPECT_CALL(*kernel_crash_interval_mock_, Add(count))
@@ -105,7 +105,7 @@
 
   // As above, but ignore values of counter updates.
   void IgnoreActiveUseUpdate() {
-    EXPECT_CALL(*daily_use_mock_, Add(_))
+    EXPECT_CALL(*daily_active_use_mock_, Add(_))
         .Times(1)
         .RetiresOnSaturation();
     EXPECT_CALL(*kernel_crash_interval_mock_, Add(_))
@@ -182,7 +182,7 @@
   // Mocks. They are strict mock so that all unexpected
   // calls are marked as failures.
   StrictMock<MetricsLibraryMock> metrics_lib_;
-  StrictMock<PersistentIntegerMock>* daily_use_mock_;
+  StrictMock<PersistentIntegerMock>* daily_active_use_mock_;
   StrictMock<PersistentIntegerMock>* kernel_crash_interval_mock_;
   StrictMock<PersistentIntegerMock>* user_crash_interval_mock_;
   StrictMock<PersistentIntegerMock>* unclean_shutdown_interval_mock_;