Write peaks to file. Minor cleanup

Test: dumpsys media.log
Change-Id: Ie696cd904306997142f050cf842290aeacfb6636
diff --git a/media/libnbaio/PerformanceAnalysis.cpp b/media/libnbaio/PerformanceAnalysis.cpp
index d1dff47..5746222 100644
--- a/media/libnbaio/PerformanceAnalysis.cpp
+++ b/media/libnbaio/PerformanceAnalysis.cpp
@@ -219,20 +219,6 @@
     }
 }
 
-// FIXME: delete this temporary test code, recycled for various new functions
-void PerformanceAnalysis::testFunction() {
-    // produces values (4: 5000000), (13: 18000000)
-    // ns timestamps of buffer periods
-    const std::vector<int64_t>kTempTestData = {1000000, 4000000, 5000000,
-                                               16000000, 18000000, 28000000};
-    PerformanceAnalysis::storeOutlierData(kTempTestData);
-    for (const auto &outlier: mOutlierData) {
-        ALOGE("PerformanceAnalysis test %lld: %lld",
-              static_cast<long long>(outlier.first), static_cast<long long>(outlier.second));
-    }
-    detectPeaks();
-}
-
 // TODO Make it return a std::string instead of modifying body --> is this still relevant?
 // TODO consider changing all ints to uint32_t or uint64_t
 // TODO: move this to ReportPerformance, probably make it a friend function of PerformanceAnalysis
@@ -319,15 +305,15 @@
 
 }
 
-
+// TODO: decide whether to use this or whether it is overkill, and it is enough
+// to only treat as glitches single wakeup call intervals which are too long.
+// Ultimately, glitch detection will be directly on the audio signal.
 // Produces a log warning if the timing of recent buffer periods caused a glitch
 // Computes sum of running window of three buffer periods
 // Checks whether the buffer periods leave enough CPU time for the next one
 // e.g. if a buffer period is expected to be 4 ms and a buffer requires 3 ms of CPU time,
 // here are some glitch cases:
 // 4 + 4 + 6 ; 5 + 4 + 5; 2 + 2 + 10
-// TODO: develop this code to track changes in histogram distribution in addition
-// to / instead of glitches.
 void PerformanceAnalysis::alertIfGlitch(const std::vector<int64_t> &samples) {
     std::deque<int> periods(kNumBuff, kPeriodMs);
     for (size_t i = 2; i < samples.size(); ++i) { // skip first time entry
@@ -348,7 +334,7 @@
 // writes summary of performance into specified file descriptor
 void dump(int fd, int indent, PerformanceAnalysisMap &threadPerformanceAnalysis) {
     String8 body;
-    const char* const kName = "/data/misc/audioserver/";
+    const char* const kDirectory = "/data/misc/audioserver/";
     for (auto & thread : threadPerformanceAnalysis) {
         for (auto & hash: thread.second) {
             PerformanceAnalysis& curr = hash.second;
@@ -356,8 +342,8 @@
             // write performance data to console
             curr.reportPerformance(&body);
             // write to file
-            writeToFile(curr.mOutlierData, curr.mHists, kName, false,
-                        thread.first, hash.first);
+            writeToFile(curr.mHists, curr.mOutlierData, curr.mPeakTimestamps,
+                        kDirectory, false, thread.first, hash.first);
         }
     }
     if (!body.isEmpty()) {
diff --git a/media/libnbaio/ReportPerformance.cpp b/media/libnbaio/ReportPerformance.cpp
index 87c223b..fa2b9a0 100644
--- a/media/libnbaio/ReportPerformance.cpp
+++ b/media/libnbaio/ReportPerformance.cpp
@@ -38,10 +38,10 @@
 
 // Writes outlier intervals, timestamps, and histograms spanning long time intervals to a file.
 // TODO: format the data efficiently and write different types of data to different files
-void writeToFile(const std::deque<std::pair<outlierInterval, timestamp>> &outlierData,
-                                    const std::deque<std::pair<timestamp, Histogram>> &hists,
-                                    const char * kDirectory,
-                                    bool append, int author, log_hash_t hash) {
+void writeToFile(const std::deque<std::pair<timestamp, Histogram>> &hists,
+                 const std::deque<std::pair<outlierInterval, timestamp>> &outlierData,
+                 const std::deque<timestamp> &peakTimestamps,
+                 const char * directory, bool append, int author, log_hash_t hash) {
     if (outlierData.empty() || hists.empty()) {
         ALOGW("No data, returning.");
         return;
@@ -49,24 +49,14 @@
 
     std::stringstream outlierName;
     std::stringstream histogramName;
+    std::stringstream peakName;
 
-    outlierName << kDirectory << "outliers_" << author << "_" << hash;
-    histogramName << kDirectory << "histograms_" << author << "_" << hash;
-
-    std::ofstream ofs;
-    ofs.open(outlierName.str().c_str(), append ? std::ios::app : std::ios::trunc);
-    if (!ofs.is_open()) {
-        ALOGW("couldn't open file %s", outlierName.str().c_str());
-        return;
-    }
-    ofs << "Outlier data: interval and timestamp\n";
-    for (const auto &outlier : outlierData) {
-        ofs << outlier.first << ": " << outlier.second << "\n";
-    }
-    ofs.close();
+    histogramName << directory << "histograms_" << author << "_" << hash;
+    outlierName << directory << "outliers_" << author << "_" << hash;
+    peakName << directory << "peaks_" << author << "_" << hash;
 
     std::ofstream hfs;
-    hfs.open(histogramName.str().c_str(), append ? std::ios::app : std::ios::trunc);
+    hfs.open(histogramName.str(), append ? std::ios::app : std::ios::trunc);
     if (!hfs.is_open()) {
         ALOGW("couldn't open file %s", histogramName.str().c_str());
         return;
@@ -82,6 +72,30 @@
         hfs << "\n"; // separate histograms with a newline
     }
     hfs.close();
+
+    std::ofstream ofs;
+    ofs.open(outlierName.str(), append ? std::ios::app : std::ios::trunc);
+    if (!ofs.is_open()) {
+        ALOGW("couldn't open file %s", outlierName.str().c_str());
+        return;
+    }
+    ofs << "Outlier data: interval and timestamp\n";
+    for (const auto &outlier : outlierData) {
+        ofs << outlier.first << ": " << outlier.second << "\n";
+    }
+    ofs.close();
+
+    std::ofstream pfs;
+    pfs.open(peakName.str(), append ? std::ios::app : std::ios::trunc);
+    if (!pfs.is_open()) {
+        ALOGW("couldn't open file %s", peakName.str().c_str());
+        return;
+    }
+    pfs << "Peak data: timestamp\n";
+    for (const auto &peak : peakTimestamps) {
+        pfs << peak << "\n";
+    }
+    pfs.close();
 }
 
 } // namespace ReportPerformance
diff --git a/media/libnbaio/include/media/nbaio/PerformanceAnalysis.h b/media/libnbaio/include/media/nbaio/PerformanceAnalysis.h
index da68821..dc6a989 100644
--- a/media/libnbaio/include/media/nbaio/PerformanceAnalysis.h
+++ b/media/libnbaio/include/media/nbaio/PerformanceAnalysis.h
@@ -79,11 +79,10 @@
     // FIXME: move this data visualization to a separate class. Model/view/controller
     void reportPerformance(String8 *body, int maxHeight = 10);
 
-    // TODO: delete this. temp for testing
-    void testFunction();
-
-    // This function used to detect glitches in a time series
-    // TODO incorporate this into the analysis (currently unused)
+    // This function detects glitches in a time series.
+    // TODO: decide whether to use this or whether it is overkill, and it is enough
+    // to only treat as glitches single wakeup call intervals which are too long.
+    // Ultimately, glitch detection will be directly on the audio signal.
     void alertIfGlitch(const std::vector<timestamp_raw> &samples);
 
 private:
diff --git a/media/libnbaio/include/media/nbaio/ReportPerformance.h b/media/libnbaio/include/media/nbaio/ReportPerformance.h
index afbbb75..bfab470 100644
--- a/media/libnbaio/include/media/nbaio/ReportPerformance.h
+++ b/media/libnbaio/include/media/nbaio/ReportPerformance.h
@@ -56,9 +56,10 @@
 
 // Writes outlier intervals, timestamps, and histograms spanning long time
 // intervals to a file.
-void writeToFile(const std::deque<std::pair<outlierInterval, timestamp>> &outlierData,
-                 const std::deque<std::pair<timestamp, Histogram>> &hists,
-                 const char * kName, bool append, int author, log_hash_t hash);
+void writeToFile(const std::deque<std::pair<timestamp, Histogram>> &hists,
+                 const std::deque<std::pair<outlierInterval, timestamp>> &outlierData,
+                 const std::deque<timestamp> &peakTimestamps,
+                 const char * kDirectory, bool append, int author, log_hash_t hash);
 
 } // namespace ReportPerformance