Change file structure and data structure size

Histograms and related data structures can
store up to 10 hours worth of data. Fixed
histogram logging elapsed time count and
resolution

Test: dumpsys media.log

Change-Id: Ibb394273b7b1be4c791f9dbe64a32a7436714a9d
diff --git a/media/libnbaio/PerformanceAnalysis.cpp b/media/libnbaio/PerformanceAnalysis.cpp
index e5e444e..4e446a4 100644
--- a/media/libnbaio/PerformanceAnalysis.cpp
+++ b/media/libnbaio/PerformanceAnalysis.cpp
@@ -75,7 +75,8 @@
     // NormalMixer times vary much more than FastMixer times.
     // TODO: mOutlierFactor values are set empirically based on what appears to be
     // an outlier. Learn these values from the data.
-    mBufferPeriod.mOutlierFactor = mBufferPeriod.mMean < kFastMixerMax ? 1.8 : 2.5;
+    // TODO: the current settings are too high, change after next data collection is over
+    mBufferPeriod.mOutlierFactor = mBufferPeriod.mMean < kFastMixerMax ? 1.8 : 2.0;
     // set outlier threshold
     mBufferPeriod.mOutlier = mBufferPeriod.mMean * mBufferPeriod.mOutlierFactor;
 
@@ -232,10 +233,15 @@
 
 // rounds value to precision based on log-distance from mean
 inline double logRound(double x, double mean) {
-    // Larger values increase range of high resolution
-    constexpr double kBase = 2;
+    // Larger values decrease range of high resolution and prevent overflow
+    // of a histogram on the console.
+    // The following formula adjusts kBase based on the buffer period length.
+    // Different threads have buffer periods ranging from 2 to 40. The
+    // formula below maps buffer period 2 to kBase = ~1, 4 to ~2, 20 to ~3, 40 to ~4.
+    // TODO: tighten this for higher means, the data still overflows
+    const double kBase = log(mean) / log(2.2);
     const double power = floor(
-        log(abs(x - mean) / mean) / log(kBase)) + 1;
+        log(abs(x - mean) / mean) / log(kBase)) + 2;
     // do not round values close to the mean
     if (power < 1) {
         return x;
@@ -265,7 +271,7 @@
         for (const auto &countPair : shortHist.second) {
             const double ms = static_cast<double>(countPair.first) / kJiffyPerMs;
             buckets[logRound(ms, mBufferPeriod.mMean)] += countPair.second;
-            elapsedMs += ms;
+            elapsedMs += ms * countPair.second;
         }
     }
 
diff --git a/media/libnbaio/ReportPerformance.cpp b/media/libnbaio/ReportPerformance.cpp
index e64a6d3..efc1b84 100644
--- a/media/libnbaio/ReportPerformance.cpp
+++ b/media/libnbaio/ReportPerformance.cpp
@@ -37,6 +37,10 @@
 
 namespace ReportPerformance {
 
+
+// TODO: use a function like this to extract logic from writeToFile
+// https://stackoverflow.com/a/9279620
+
 // Writes outlier intervals, timestamps, and histograms spanning long time intervals to file.
 // TODO: write data in binary format
 void writeToFile(const std::deque<std::pair<timestamp, Histogram>> &hists,
@@ -80,13 +84,18 @@
     // each histogram is written as a line where the first value is the timestamp and
     // subsequent values are pairs of buckets and counts. Each value is separated
     // by a comma, and each histogram is separated by a newline.
-    for (const auto &hist : hists) {
-        hfs << hist.first << ", ";
-        for (const auto &bucket : hist.second) {
-            hfs << bucket.first / static_cast<double>(kJiffyPerMs)
-                    << ", " << bucket.second << ", ";
+    for (auto hist = hists.begin(); hist != hists.end(); ++hist) {
+        hfs << hist->first << ", ";
+        for (auto bucket = hist->second.begin(); bucket != hist->second.end(); ++bucket) {
+            hfs << bucket->first / static_cast<double>(kJiffyPerMs)
+                << ", " << bucket->second;
+            if (std::next(bucket) != end(hist->second)) {
+                hfs << ", ";
+            }
         }
-        hfs << "\n";
+        if (std::next(hist) != end(hists)) {
+            hfs << "\n";
+        }
     }
     hfs.close();
 
@@ -110,8 +119,11 @@
         return;
     }
     // peaks are simply timestamps separated by commas
-    for (const auto &peak : peakTimestamps) {
-        pfs << peak << ", ";
+    for (auto peak = peakTimestamps.begin(); peak != peakTimestamps.end(); ++peak) {
+        pfs << *peak;
+        if (std::next(peak) != end(peakTimestamps)) {
+            pfs << ", ";
+        }
     }
     pfs.close();
 }
diff --git a/media/libnbaio/include/media/nbaio/PerformanceAnalysis.h b/media/libnbaio/include/media/nbaio/PerformanceAnalysis.h
index a673151..fe73551 100644
--- a/media/libnbaio/include/media/nbaio/PerformanceAnalysis.h
+++ b/media/libnbaio/include/media/nbaio/PerformanceAnalysis.h
@@ -102,16 +102,16 @@
     } mBufferPeriod;
 
     // capacity allocated to data structures
-    // TODO: make these values longer when testing is finished
     struct MaxLength {
         size_t Hists; // number of histograms stored in memory
-        size_t TimeStamps; // histogram size
         size_t Outliers; // number of values stored in outlier array
         size_t Peaks; // number of values stored in peak array
         int HistTimespanMs; // maximum histogram timespan
     };
-    static constexpr MaxLength kMaxLength = {.Hists = 20, .TimeStamps = 1000,
-            .Outliers = 100, .Peaks = 100, .HistTimespanMs = 5 * kMsPerSec };
+    // These values allow for 10 hours of data allowing for a glitch and a peak
+    // as often as every 3 seconds
+    static constexpr MaxLength kMaxLength = {.Hists = 60, .Outliers = 12000,
+            .Peaks = 12000, .HistTimespanMs = 10 * kSecPerMin * kMsPerSec };
 
     // these variables ensure continuity while analyzing the timestamp
     // series one sample at a time.
diff --git a/media/libnbaio/include/media/nbaio/ReportPerformance.h b/media/libnbaio/include/media/nbaio/ReportPerformance.h
index 0d4a7b9..af12812 100644
--- a/media/libnbaio/include/media/nbaio/ReportPerformance.h
+++ b/media/libnbaio/include/media/nbaio/ReportPerformance.h
@@ -30,6 +30,7 @@
 namespace ReportPerformance {
 
 constexpr int kMsPerSec = 1000;
+constexpr int kSecPerMin = 60;
 
 constexpr int kJiffyPerMs = 10; // time unit for histogram as a multiple of milliseconds