1 /*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #define LOG_TAG "ReportPerformance"
18
19 #include <fstream>
20 #include <iostream>
21 #include <queue>
22 #include <stdarg.h>
23 #include <stdint.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <sstream>
27 #include <sys/prctl.h>
28 #include <sys/time.h>
29 #include <utility>
30 #include <media/nblog/NBLog.h>
31 #include <media/nblog/PerformanceAnalysis.h>
32 #include <media/nblog/ReportPerformance.h>
33 #include <utils/Log.h>
34 #include <utils/String8.h>
35
36 namespace android {
37
38 namespace ReportPerformance {
39
40
41 // TODO: use a function like this to extract logic from writeToFile
42 // https://stackoverflow.com/a/9279620
43
44 // Writes outlier intervals, timestamps, and histograms spanning long time intervals to file.
45 // TODO: write data in binary format
writeToFile(const std::deque<std::pair<timestamp,Histogram>> & hists,const std::deque<std::pair<msInterval,timestamp>> & outlierData,const std::deque<timestamp> & peakTimestamps,const char * directory,bool append,int author,log_hash_t hash)46 void writeToFile(const std::deque<std::pair<timestamp, Histogram>> &hists,
47 const std::deque<std::pair<msInterval, timestamp>> &outlierData,
48 const std::deque<timestamp> &peakTimestamps,
49 const char * directory, bool append, int author, log_hash_t hash) {
50
51 // TODO: remove old files, implement rotating files as in AudioFlinger.cpp
52
53 if (outlierData.empty() && hists.empty() && peakTimestamps.empty()) {
54 ALOGW("No data, returning.");
55 return;
56 }
57
58 std::stringstream outlierName;
59 std::stringstream histogramName;
60 std::stringstream peakName;
61
62 // get current time
63 char currTime[16]; //YYYYMMDDHHMMSS + '\0' + one unused
64 struct timeval tv;
65 gettimeofday(&tv, NULL);
66 struct tm tm;
67 localtime_r(&tv.tv_sec, &tm);
68 strftime(currTime, sizeof(currTime), "%Y%m%d%H%M%S", &tm);
69
70 // generate file names
71 std::stringstream common;
72 common << author << "_" << hash << "_" << currTime << ".csv";
73
74 histogramName << directory << "histograms_" << common.str();
75 outlierName << directory << "outliers_" << common.str();
76 peakName << directory << "peaks_" << common.str();
77
78 std::ofstream hfs;
79 hfs.open(histogramName.str(), append ? std::ios::app : std::ios::trunc);
80 if (!hfs.is_open()) {
81 ALOGW("couldn't open file %s", histogramName.str().c_str());
82 return;
83 }
84 // each histogram is written as a line where the first value is the timestamp and
85 // subsequent values are pairs of buckets and counts. Each value is separated
86 // by a comma, and each histogram is separated by a newline.
87 for (auto hist = hists.begin(); hist != hists.end(); ++hist) {
88 hfs << hist->first << ", ";
89 for (auto bucket = hist->second.begin(); bucket != hist->second.end(); ++bucket) {
90 hfs << bucket->first / static_cast<double>(kJiffyPerMs)
91 << ", " << bucket->second;
92 if (std::next(bucket) != end(hist->second)) {
93 hfs << ", ";
94 }
95 }
96 if (std::next(hist) != end(hists)) {
97 hfs << "\n";
98 }
99 }
100 hfs.close();
101
102 std::ofstream ofs;
103 ofs.open(outlierName.str(), append ? std::ios::app : std::ios::trunc);
104 if (!ofs.is_open()) {
105 ALOGW("couldn't open file %s", outlierName.str().c_str());
106 return;
107 }
108 // outliers are written as pairs separated by newlines, where each
109 // pair's values are separated by a comma
110 for (const auto &outlier : outlierData) {
111 ofs << outlier.first << ", " << outlier.second << "\n";
112 }
113 ofs.close();
114
115 std::ofstream pfs;
116 pfs.open(peakName.str(), append ? std::ios::app : std::ios::trunc);
117 if (!pfs.is_open()) {
118 ALOGW("couldn't open file %s", peakName.str().c_str());
119 return;
120 }
121 // peaks are simply timestamps separated by commas
122 for (auto peak = peakTimestamps.begin(); peak != peakTimestamps.end(); ++peak) {
123 pfs << *peak;
124 if (std::next(peak) != end(peakTimestamps)) {
125 pfs << ", ";
126 }
127 }
128 pfs.close();
129 }
130
131 } // namespace ReportPerformance
132
133 } // namespace android
134