blob: 22a30b982fa41c3b8299adbb9e6960fdee4e0451 [file] [log] [blame]
Sanna Catherine de Treville Wagerd0dfe432017-06-22 15:09:38 -07001/*
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
18#define LOG_TAG "PerformanceAnalysis"
19// #define LOG_NDEBUG 0
20
21#include <algorithm>
22#include <climits>
23#include <deque>
Sanna Catherine de Treville Wagerd0dfe432017-06-22 15:09:38 -070024#include <iostream>
25#include <math.h>
26#include <numeric>
27#include <vector>
28#include <stdarg.h>
29#include <stdint.h>
30#include <stdio.h>
31#include <string.h>
32#include <sys/prctl.h>
33#include <time.h>
34#include <new>
35#include <audio_utils/roundup.h>
Glenn Kasten8589ce72017-09-08 17:03:42 -070036#include <media/nblog/NBLog.h>
37#include <media/nblog/PerformanceAnalysis.h>
38#include <media/nblog/ReportPerformance.h>
Sanna Catherine de Treville Wagerd0dfe432017-06-22 15:09:38 -070039#include <utils/Log.h>
40#include <utils/String8.h>
41
42#include <queue>
43#include <utility>
44
45namespace android {
46
Sanna Catherine de Treville Wager80448082017-07-11 14:07:59 -070047namespace ReportPerformance {
48
Sanna Catherine de Treville Wager14316442017-08-11 10:45:29 -070049// Given an audio processing wakeup timestamp, buckets the time interval
50// since the previous timestamp into a histogram, searches for
Sanna Catherine de Treville Wagera8a8a472017-07-11 09:41:25 -070051// outliers, analyzes the outlier series for unexpectedly
Sanna Catherine de Treville Wager85768942017-07-26 20:17:30 -070052// small or large values and stores these as peaks
Sanna Catherine de Treville Wager6ad40ee2017-07-28 10:10:55 -070053void PerformanceAnalysis::logTsEntry(timestamp ts) {
Sanna Catherine de Treville Wager85768942017-07-26 20:17:30 -070054 // after a state change, start a new series and do not
55 // record time intervals in-between
Sanna Catherine de Treville Wager6ad40ee2017-07-28 10:10:55 -070056 if (mBufferPeriod.mPrevTs == 0) {
57 mBufferPeriod.mPrevTs = ts;
Sanna Catherine de Treville Wagera80649a2017-07-21 16:16:38 -070058 return;
59 }
60
Sanna Catherine de Treville Wager6ad40ee2017-07-28 10:10:55 -070061 // calculate time interval between current and previous timestamp
62 const msInterval diffMs = static_cast<msInterval>(
63 deltaMs(mBufferPeriod.mPrevTs, ts));
64
65 const int diffJiffy = deltaJiffy(mBufferPeriod.mPrevTs, ts);
66
Sanna Catherine de Treville Wager6ad40ee2017-07-28 10:10:55 -070067 // old versus new weight ratio when updating the buffer period mean
68 static constexpr double exponentialWeight = 0.999;
69 // update buffer period mean with exponential weighting
70 mBufferPeriod.mMean = (mBufferPeriod.mMean < 0) ? diffMs :
71 exponentialWeight * mBufferPeriod.mMean + (1.0 - exponentialWeight) * diffMs;
72 // set mOutlierFactor to a smaller value for the fastmixer thread
73 const int kFastMixerMax = 10;
74 // NormalMixer times vary much more than FastMixer times.
75 // TODO: mOutlierFactor values are set empirically based on what appears to be
76 // an outlier. Learn these values from the data.
Sanna Catherine de Treville Wager847b6e62017-08-03 11:35:51 -070077 mBufferPeriod.mOutlierFactor = mBufferPeriod.mMean < kFastMixerMax ? 1.8 : 2.0;
Sanna Catherine de Treville Wager6ad40ee2017-07-28 10:10:55 -070078 // set outlier threshold
79 mBufferPeriod.mOutlier = mBufferPeriod.mMean * mBufferPeriod.mOutlierFactor;
80
Sanna Catherine de Treville Wager85768942017-07-26 20:17:30 -070081 // Check whether the time interval between the current timestamp
82 // and the previous one is long enough to count as an outlier
Sanna Catherine de Treville Wager6ad40ee2017-07-28 10:10:55 -070083 const bool isOutlier = detectAndStoreOutlier(diffMs);
Sanna Catherine de Treville Wager85768942017-07-26 20:17:30 -070084 // If an outlier was found, check whether it was a peak
85 if (isOutlier) {
86 /*bool isPeak =*/ detectAndStorePeak(
87 mOutlierData[0].first, mOutlierData[0].second);
88 // TODO: decide whether to insert a new empty histogram if a peak
89 // TODO: remove isPeak if unused to avoid "unused variable" error
Sanna Catherine de Treville Wager0a3959e2017-07-25 16:08:17 -070090 // occurred at the current timestamp
Sanna Catherine de Treville Wagera80649a2017-07-21 16:16:38 -070091 }
92
Sanna Catherine de Treville Wager85768942017-07-26 20:17:30 -070093 // Insert a histogram to mHists if it is empty, or
94 // close the current histogram and insert a new empty one if
95 // if the current histogram has spanned its maximum time interval.
96 if (mHists.empty() ||
97 deltaMs(mHists[0].first, ts) >= kMaxLength.HistTimespanMs) {
Sanna Catherine de Treville Wager6ad40ee2017-07-28 10:10:55 -070098 mHists.emplace_front(ts, std::map<int, int>());
Sanna Catherine de Treville Wager85768942017-07-26 20:17:30 -070099 // When memory is full, delete oldest histogram
100 // TODO: use a circular buffer
101 if (mHists.size() >= kMaxLength.Hists) {
102 mHists.resize(kMaxLength.Hists);
103 }
104 }
105 // add current time intervals to histogram
Sanna Catherine de Treville Wager6ad40ee2017-07-28 10:10:55 -0700106 ++mHists[0].second[diffJiffy];
Sanna Catherine de Treville Wager85768942017-07-26 20:17:30 -0700107 // update previous timestamp
Sanna Catherine de Treville Wager6ad40ee2017-07-28 10:10:55 -0700108 mBufferPeriod.mPrevTs = ts;
Sanna Catherine de Treville Wagera8a8a472017-07-11 09:41:25 -0700109}
110
Sanna Catherine de Treville Wager85768942017-07-26 20:17:30 -0700111
Sanna Catherine de Treville Wagera8a8a472017-07-11 09:41:25 -0700112// forces short-term histogram storage to avoid adding idle audio time interval
113// to buffer period data
Sanna Catherine de Treville Wager80448082017-07-11 14:07:59 -0700114void PerformanceAnalysis::handleStateChange() {
Sanna Catherine de Treville Wager6ad40ee2017-07-28 10:10:55 -0700115 mBufferPeriod.mPrevTs = 0;
Sanna Catherine de Treville Wagera8a8a472017-07-11 09:41:25 -0700116 return;
117}
118
Sanna Catherine de Treville Wager41cad592017-06-29 14:57:59 -0700119
Sanna Catherine de Treville Wager0a3959e2017-07-25 16:08:17 -0700120// Checks whether the time interval between two outliers is far enough from
121// a typical delta to be considered a peak.
Sanna Catherine de Treville Wager41cad592017-06-29 14:57:59 -0700122// looks for changes in distribution (peaks), which can be either positive or negative.
123// The function sets the mean to the starting value and sigma to 0, and updates
124// them as long as no peak is detected. When a value is more than 'threshold'
125// standard deviations from the mean, a peak is detected and the mean and sigma
126// are set to the peak value and 0.
Sanna Catherine de Treville Wager6ad40ee2017-07-28 10:10:55 -0700127bool PerformanceAnalysis::detectAndStorePeak(msInterval diff, timestamp ts) {
Sanna Catherine de Treville Wager0a3959e2017-07-25 16:08:17 -0700128 bool isPeak = false;
Sanna Catherine de Treville Wager41cad592017-06-29 14:57:59 -0700129 if (mOutlierData.empty()) {
Sanna Catherine de Treville Wager0a3959e2017-07-25 16:08:17 -0700130 return false;
Sanna Catherine de Treville Wager41cad592017-06-29 14:57:59 -0700131 }
Sanna Catherine de Treville Wager0a3959e2017-07-25 16:08:17 -0700132 // Update mean of the distribution
133 // TypicalDiff is used to check whether a value is unusually large
134 // when we cannot use standard deviations from the mean because the sd is set to 0.
135 mOutlierDistribution.mTypicalDiff = (mOutlierDistribution.mTypicalDiff *
136 (mOutlierData.size() - 1) + diff) / mOutlierData.size();
Sanna Catherine de Treville Wager41cad592017-06-29 14:57:59 -0700137
Sanna Catherine de Treville Wager0a3959e2017-07-25 16:08:17 -0700138 // Initialize short-term mean at start of program
139 if (mOutlierDistribution.mMean == 0) {
Sanna Catherine de Treville Wager6ad40ee2017-07-28 10:10:55 -0700140 mOutlierDistribution.mMean = diff;
Sanna Catherine de Treville Wager0a3959e2017-07-25 16:08:17 -0700141 }
142 // Update length of current sequence of outliers
143 mOutlierDistribution.mN++;
Sanna Catherine de Treville Wager41cad592017-06-29 14:57:59 -0700144
Sanna Catherine de Treville Wager6ad40ee2017-07-28 10:10:55 -0700145 // Check whether a large deviation from the mean occurred.
Sanna Catherine de Treville Wager0a3959e2017-07-25 16:08:17 -0700146 // If the standard deviation has been reset to zero, the comparison is
147 // instead to the mean of the full mOutlierInterval sequence.
Sanna Catherine de Treville Wager6ad40ee2017-07-28 10:10:55 -0700148 if ((fabs(diff - mOutlierDistribution.mMean) <
Sanna Catherine de Treville Wager0a3959e2017-07-25 16:08:17 -0700149 mOutlierDistribution.kMaxDeviation * mOutlierDistribution.mSd) ||
150 (mOutlierDistribution.mSd == 0 &&
151 fabs(diff - mOutlierDistribution.mMean) <
152 mOutlierDistribution.mTypicalDiff)) {
153 // update the mean and sd using online algorithm
154 // https://en.wikipedia.org/wiki/
155 // Algorithms_for_calculating_variance#Online_algorithm
156 mOutlierDistribution.mN++;
157 const double kDelta = diff - mOutlierDistribution.mMean;
158 mOutlierDistribution.mMean += kDelta / mOutlierDistribution.mN;
159 const double kDelta2 = diff - mOutlierDistribution.mMean;
160 mOutlierDistribution.mM2 += kDelta * kDelta2;
161 mOutlierDistribution.mSd = (mOutlierDistribution.mN < 2) ? 0 :
Sanna Catherine de Treville Wager85768942017-07-26 20:17:30 -0700162 sqrt(mOutlierDistribution.mM2 / (mOutlierDistribution.mN - 1));
Sanna Catherine de Treville Wager0a3959e2017-07-25 16:08:17 -0700163 } else {
164 // new value is far from the mean:
165 // store peak timestamp and reset mean, sd, and short-term sequence
166 isPeak = true;
167 mPeakTimestamps.emplace_front(ts);
168 // if mPeaks has reached capacity, delete oldest data
169 // Note: this means that mOutlierDistribution values do not exactly
170 // match the data we have in mPeakTimestamps, but this is not an issue
171 // in practice for estimating future peaks.
172 // TODO: turn this into a circular buffer
173 if (mPeakTimestamps.size() >= kMaxLength.Peaks) {
174 mPeakTimestamps.resize(kMaxLength.Peaks);
Sanna Catherine de Treville Wager41cad592017-06-29 14:57:59 -0700175 }
Sanna Catherine de Treville Wager0a3959e2017-07-25 16:08:17 -0700176 mOutlierDistribution.mMean = 0;
177 mOutlierDistribution.mSd = 0;
178 mOutlierDistribution.mN = 0;
179 mOutlierDistribution.mM2 = 0;
Sanna Catherine de Treville Wager41cad592017-06-29 14:57:59 -0700180 }
Sanna Catherine de Treville Wager0a3959e2017-07-25 16:08:17 -0700181 return isPeak;
Sanna Catherine de Treville Wager41cad592017-06-29 14:57:59 -0700182}
183
Sanna Catherine de Treville Wager85768942017-07-26 20:17:30 -0700184
Sanna Catherine de Treville Wager0a3959e2017-07-25 16:08:17 -0700185// Determines whether the difference between a timestamp and the previous
186// one is beyond a threshold. If yes, stores the timestamp as an outlier
187// and writes to mOutlierdata in the following format:
188// Time elapsed since previous outlier: Timestamp of start of outlier
Sanna Catherine de Treville Wager41cad592017-06-29 14:57:59 -0700189// e.g. timestamps (ms) 1, 4, 5, 16, 18, 28 will produce pairs (4, 5), (13, 18).
Sanna Catherine de Treville Wager2a6a9452017-07-28 11:02:01 -0700190// TODO: learn what timestamp sequences correlate with glitches instead of
191// manually designing a heuristic.
Sanna Catherine de Treville Wager6ad40ee2017-07-28 10:10:55 -0700192bool PerformanceAnalysis::detectAndStoreOutlier(const msInterval diffMs) {
Sanna Catherine de Treville Wager0a3959e2017-07-25 16:08:17 -0700193 bool isOutlier = false;
Sanna Catherine de Treville Wager6ad40ee2017-07-28 10:10:55 -0700194 if (diffMs >= mBufferPeriod.mOutlier) {
Sanna Catherine de Treville Wager0a3959e2017-07-25 16:08:17 -0700195 isOutlier = true;
Sanna Catherine de Treville Wager6ad40ee2017-07-28 10:10:55 -0700196 mOutlierData.emplace_front(
197 mOutlierDistribution.mElapsed, mBufferPeriod.mPrevTs);
Sanna Catherine de Treville Wager0a3959e2017-07-25 16:08:17 -0700198 // Remove oldest value if the vector is full
199 // TODO: turn this into a circular buffer
200 // TODO: make sure kShortHistSize is large enough that that data will never be lost
201 // before being written to file or to a FIFO
202 if (mOutlierData.size() >= kMaxLength.Outliers) {
203 mOutlierData.resize(kMaxLength.Outliers);
Sanna Catherine de Treville Wager41cad592017-06-29 14:57:59 -0700204 }
Sanna Catherine de Treville Wager0a3959e2017-07-25 16:08:17 -0700205 mOutlierDistribution.mElapsed = 0;
Sanna Catherine de Treville Wager41cad592017-06-29 14:57:59 -0700206 }
Sanna Catherine de Treville Wager0a3959e2017-07-25 16:08:17 -0700207 mOutlierDistribution.mElapsed += diffMs;
Sanna Catherine de Treville Wager0a3959e2017-07-25 16:08:17 -0700208 return isOutlier;
Sanna Catherine de Treville Wager41cad592017-06-29 14:57:59 -0700209}
210
Sanna Catherine de Treville Wager2a6a9452017-07-28 11:02:01 -0700211static int widthOf(int x) {
212 int width = 0;
213 if (x < 0) {
214 width++;
215 x = x == INT_MIN ? INT_MAX : -x;
216 }
217 // assert (x >= 0)
218 do {
219 ++width;
220 x /= 10;
221 } while (x > 0);
222 return width;
223}
224
Sanna Catherine de Treville Wager6ad40ee2017-07-28 10:10:55 -0700225// computes the column width required for a specific histogram value
Sanna Catherine de Treville Wager2a6a9452017-07-28 11:02:01 -0700226inline int numberWidth(double number, int leftPadding) {
227 // Added values account for whitespaces needed around numbers, and for the
228 // dot and decimal digit not accounted for by widthOf
229 return std::max(std::max(widthOf(static_cast<int>(number)) + 3, 2), leftPadding + 1);
230}
231
232// rounds value to precision based on log-distance from mean
Ivan Lozanobb4b8b52017-11-30 15:43:19 -0800233__attribute__((no_sanitize("signed-integer-overflow")))
Sanna Catherine de Treville Wager2a6a9452017-07-28 11:02:01 -0700234inline double logRound(double x, double mean) {
Sanna Catherine de Treville Wager847b6e62017-08-03 11:35:51 -0700235 // Larger values decrease range of high resolution and prevent overflow
236 // of a histogram on the console.
237 // The following formula adjusts kBase based on the buffer period length.
238 // Different threads have buffer periods ranging from 2 to 40. The
239 // formula below maps buffer period 2 to kBase = ~1, 4 to ~2, 20 to ~3, 40 to ~4.
240 // TODO: tighten this for higher means, the data still overflows
241 const double kBase = log(mean) / log(2.2);
Sanna Catherine de Treville Wager2a6a9452017-07-28 11:02:01 -0700242 const double power = floor(
Sanna Catherine de Treville Wager847b6e62017-08-03 11:35:51 -0700243 log(abs(x - mean) / mean) / log(kBase)) + 2;
Sanna Catherine de Treville Wager2a6a9452017-07-28 11:02:01 -0700244 // do not round values close to the mean
245 if (power < 1) {
246 return x;
247 }
248 const int factor = static_cast<int>(pow(10, power));
249 return (static_cast<int>(x) * factor) / factor;
Sanna Catherine de Treville Wager6ad40ee2017-07-28 10:10:55 -0700250}
Sanna Catherine de Treville Wager85768942017-07-26 20:17:30 -0700251
Sanna Catherine de Treville Wager6ad40ee2017-07-28 10:10:55 -0700252// TODO Make it return a std::string instead of modifying body
Sanna Catherine de Treville Wager2a6a9452017-07-28 11:02:01 -0700253// TODO: move this to ReportPerformance, probably make it a friend function
254// of PerformanceAnalysis
255void PerformanceAnalysis::reportPerformance(String8 *body, int author, log_hash_t hash,
256 int maxHeight) {
Eric Tan8180b742018-07-10 15:23:29 -0700257 if (mHists.empty() || body == nullptr) {
Sanna Catherine de Treville Wagerd0dfe432017-06-22 15:09:38 -0700258 return;
259 }
Sanna Catherine de Treville Wager0a3959e2017-07-25 16:08:17 -0700260
Sanna Catherine de Treville Wager2a6a9452017-07-28 11:02:01 -0700261 // ms of active audio in displayed histogram
262 double elapsedMs = 0;
263 // starting timestamp of histogram
264 timestamp startingTs = mHists[0].first;
265
266 // histogram which stores .1 precision ms counts instead of Jiffy multiple counts
Sanna Catherine de Treville Wager2a6a9452017-07-28 11:02:01 -0700267 std::map<double, int> buckets;
Sanna Catherine de Treville Wagera80649a2017-07-21 16:16:38 -0700268 for (const auto &shortHist: mHists) {
Sanna Catherine de Treville Wagerd0dfe432017-06-22 15:09:38 -0700269 for (const auto &countPair : shortHist.second) {
Sanna Catherine de Treville Wager2a6a9452017-07-28 11:02:01 -0700270 const double ms = static_cast<double>(countPair.first) / kJiffyPerMs;
271 buckets[logRound(ms, mBufferPeriod.mMean)] += countPair.second;
Sanna Catherine de Treville Wager847b6e62017-08-03 11:35:51 -0700272 elapsedMs += ms * countPair.second;
Sanna Catherine de Treville Wagerd0dfe432017-06-22 15:09:38 -0700273 }
274 }
275
Eric Tan8180b742018-07-10 15:23:29 -0700276 static const int SIZE = 128;
277 char title[SIZE];
278 snprintf(title, sizeof(title), "\n%s %3.2f %s\n%s%d, %lld, %lld\n",
279 "Occurrences in", (elapsedMs / kMsPerSec), "seconds of audio:",
280 "Thread, hash, starting timestamp: ", author,
281 static_cast<long long>(hash), static_cast<long long>(startingTs));
282 static const char * const kLabel = "ms";
Sanna Catherine de Treville Wagerd0dfe432017-06-22 15:09:38 -0700283
284 auto it = buckets.begin();
Sanna Catherine de Treville Wager2a6a9452017-07-28 11:02:01 -0700285 double maxDelta = it->first;
Sanna Catherine de Treville Wagerd0dfe432017-06-22 15:09:38 -0700286 int maxCount = it->second;
287 // Compute maximum values
288 while (++it != buckets.end()) {
289 if (it->first > maxDelta) {
290 maxDelta = it->first;
291 }
292 if (it->second > maxCount) {
293 maxCount = it->second;
294 }
295 }
296 int height = log2(maxCount) + 1; // maxCount > 0, safe to call log2
297 const int leftPadding = widthOf(1 << height);
Sanna Catherine de Treville Wager6ad40ee2017-07-28 10:10:55 -0700298 const int bucketWidth = numberWidth(maxDelta, leftPadding);
Sanna Catherine de Treville Wagerd0dfe432017-06-22 15:09:38 -0700299 int scalingFactor = 1;
300 // scale data if it exceeds maximum height
301 if (height > maxHeight) {
302 scalingFactor = (height + maxHeight) / maxHeight;
303 height /= scalingFactor;
304 }
Eric Tan8180b742018-07-10 15:23:29 -0700305 body->appendFormat("%s", title);
Sanna Catherine de Treville Wagerd0dfe432017-06-22 15:09:38 -0700306 // write histogram label line with bucket values
307 body->appendFormat("\n%s", " ");
308 body->appendFormat("%*s", leftPadding, " ");
309 for (auto const &x : buckets) {
Sanna Catherine de Treville Wager2a6a9452017-07-28 11:02:01 -0700310 const int colWidth = numberWidth(x.first, leftPadding);
Sanna Catherine de Treville Wagerd0dfe432017-06-22 15:09:38 -0700311 body->appendFormat("%*d", colWidth, x.second);
312 }
313 // write histogram ascii art
Eric Tan8180b742018-07-10 15:23:29 -0700314 // underscores and spaces length corresponds to maximum width of histogram
315 static const int kLen = 200;
316 static const std::string underscores(kLen, '_');
317 static const std::string spaces(kLen, ' ');
318
Sanna Catherine de Treville Wagerd0dfe432017-06-22 15:09:38 -0700319 body->appendFormat("\n%s", " ");
320 for (int row = height * scalingFactor; row >= 0; row -= scalingFactor) {
321 const int value = 1 << row;
322 body->appendFormat("%.*s", leftPadding, spaces.c_str());
323 for (auto const &x : buckets) {
Sanna Catherine de Treville Wager2a6a9452017-07-28 11:02:01 -0700324 const int colWidth = numberWidth(x.first, leftPadding);
Sanna Catherine de Treville Wager6ad40ee2017-07-28 10:10:55 -0700325 body->appendFormat("%.*s%s", colWidth - 1,
326 spaces.c_str(), x.second < value ? " " : "|");
Sanna Catherine de Treville Wagerd0dfe432017-06-22 15:09:38 -0700327 }
328 body->appendFormat("\n%s", " ");
329 }
330 // print x-axis
331 const int columns = static_cast<int>(buckets.size());
332 body->appendFormat("%*c", leftPadding, ' ');
Sanna Catherine de Treville Wager6ad40ee2017-07-28 10:10:55 -0700333 body->appendFormat("%.*s", (columns + 1) * bucketWidth, underscores.c_str());
Sanna Catherine de Treville Wagerd0dfe432017-06-22 15:09:38 -0700334 body->appendFormat("\n%s", " ");
335
336 // write footer with bucket labels
337 body->appendFormat("%*s", leftPadding, " ");
338 for (auto const &x : buckets) {
Sanna Catherine de Treville Wager2a6a9452017-07-28 11:02:01 -0700339 const int colWidth = numberWidth(x.first, leftPadding);
340 body->appendFormat("%*.*f", colWidth, 1, x.first);
Sanna Catherine de Treville Wagerd0dfe432017-06-22 15:09:38 -0700341 }
Eric Tan8180b742018-07-10 15:23:29 -0700342 body->appendFormat("%.*s%s\n", bucketWidth, spaces.c_str(), kLabel);
Sanna Catherine de Treville Wagerd0dfe432017-06-22 15:09:38 -0700343
Sanna Catherine de Treville Wager316f1fd2017-06-23 09:10:15 -0700344 // Now report glitches
Sanna Catherine de Treville Wager2a6a9452017-07-28 11:02:01 -0700345 body->appendFormat("\ntime elapsed between glitches and glitch timestamps:\n");
Sanna Catherine de Treville Wager316f1fd2017-06-23 09:10:15 -0700346 for (const auto &outlier: mOutlierData) {
347 body->appendFormat("%lld: %lld\n", static_cast<long long>(outlier.first),
348 static_cast<long long>(outlier.second));
349 }
Sanna Catherine de Treville Wagerd0dfe432017-06-22 15:09:38 -0700350}
351
Sanna Catherine de Treville Wagercf6c75a2017-07-21 17:05:25 -0700352//------------------------------------------------------------------------------
353
354// writes summary of performance into specified file descriptor
Sanna Catherine de Treville Wager23f89d32017-07-24 18:24:48 -0700355void dump(int fd, int indent, PerformanceAnalysisMap &threadPerformanceAnalysis) {
Sanna Catherine de Treville Wagercf6c75a2017-07-21 17:05:25 -0700356 String8 body;
Sanna Catherine de Treville Wagerf8c34282017-07-25 11:31:18 -0700357 const char* const kDirectory = "/data/misc/audioserver/";
Sanna Catherine de Treville Wagercf6c75a2017-07-21 17:05:25 -0700358 for (auto & thread : threadPerformanceAnalysis) {
Sanna Catherine de Treville Wagerd0965172017-07-24 13:42:44 -0700359 for (auto & hash: thread.second) {
Sanna Catherine de Treville Wager23f89d32017-07-24 18:24:48 -0700360 PerformanceAnalysis& curr = hash.second;
Sanna Catherine de Treville Wager23f89d32017-07-24 18:24:48 -0700361 // write performance data to console
Sanna Catherine de Treville Wager2a6a9452017-07-28 11:02:01 -0700362 curr.reportPerformance(&body, thread.first, hash.first);
Sanna Catherine de Treville Wager0a3959e2017-07-25 16:08:17 -0700363 if (!body.isEmpty()) {
364 dumpLine(fd, indent, body);
365 body.clear();
366 }
Sanna Catherine de Treville Wager23f89d32017-07-24 18:24:48 -0700367 // write to file
Sanna Catherine de Treville Wagerf8c34282017-07-25 11:31:18 -0700368 writeToFile(curr.mHists, curr.mOutlierData, curr.mPeakTimestamps,
369 kDirectory, false, thread.first, hash.first);
Sanna Catherine de Treville Wagerd0965172017-07-24 13:42:44 -0700370 }
Sanna Catherine de Treville Wagercf6c75a2017-07-21 17:05:25 -0700371 }
Sanna Catherine de Treville Wagercf6c75a2017-07-21 17:05:25 -0700372}
373
Sanna Catherine de Treville Wager85768942017-07-26 20:17:30 -0700374
Sanna Catherine de Treville Wagercf6c75a2017-07-21 17:05:25 -0700375// Writes a string into specified file descriptor
376void dumpLine(int fd, int indent, const String8 &body) {
377 dprintf(fd, "%.*s%s \n", indent, "", body.string());
378}
379
Sanna Catherine de Treville Wager80448082017-07-11 14:07:59 -0700380} // namespace ReportPerformance
381
Sanna Catherine de Treville Wagerd0dfe432017-06-22 15:09:38 -0700382} // namespace android