Sanna Catherine de Treville Wager | d0dfe43 | 2017-06-22 15:09:38 -0700 | [diff] [blame] | 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 | |
| 18 | #define LOG_TAG "PerformanceAnalysis" |
| 19 | // #define LOG_NDEBUG 0 |
Eric Tan | e98dd6f | 2018-08-22 18:23:50 -0700 | [diff] [blame] | 20 | // #define WRITE_TO_FILE |
Sanna Catherine de Treville Wager | d0dfe43 | 2017-06-22 15:09:38 -0700 | [diff] [blame] | 21 | |
| 22 | #include <algorithm> |
| 23 | #include <climits> |
| 24 | #include <deque> |
Sanna Catherine de Treville Wager | d0dfe43 | 2017-06-22 15:09:38 -0700 | [diff] [blame] | 25 | #include <math.h> |
| 26 | #include <numeric> |
Eric Tan | e98dd6f | 2018-08-22 18:23:50 -0700 | [diff] [blame] | 27 | #include <sstream> |
| 28 | #include <string> |
Sanna Catherine de Treville Wager | d0dfe43 | 2017-06-22 15:09:38 -0700 | [diff] [blame] | 29 | #include <vector> |
| 30 | #include <stdarg.h> |
| 31 | #include <stdint.h> |
| 32 | #include <stdio.h> |
| 33 | #include <string.h> |
| 34 | #include <sys/prctl.h> |
| 35 | #include <time.h> |
| 36 | #include <new> |
Eric Tan | 39ec8d6 | 2018-07-24 09:49:29 -0700 | [diff] [blame] | 37 | #include <audio_utils/LogPlot.h> |
Sanna Catherine de Treville Wager | d0dfe43 | 2017-06-22 15:09:38 -0700 | [diff] [blame] | 38 | #include <audio_utils/roundup.h> |
Glenn Kasten | 8589ce7 | 2017-09-08 17:03:42 -0700 | [diff] [blame] | 39 | #include <media/nblog/NBLog.h> |
| 40 | #include <media/nblog/PerformanceAnalysis.h> |
| 41 | #include <media/nblog/ReportPerformance.h> |
Sanna Catherine de Treville Wager | d0dfe43 | 2017-06-22 15:09:38 -0700 | [diff] [blame] | 42 | #include <utils/Log.h> |
| 43 | #include <utils/String8.h> |
Eric Tan | fefe316 | 2018-09-07 10:09:11 -0700 | [diff] [blame] | 44 | #include <utils/Timers.h> |
Sanna Catherine de Treville Wager | d0dfe43 | 2017-06-22 15:09:38 -0700 | [diff] [blame] | 45 | |
| 46 | #include <queue> |
| 47 | #include <utility> |
| 48 | |
| 49 | namespace android { |
Eric Tan | d6eee71 | 2018-09-07 10:58:19 -0700 | [diff] [blame] | 50 | namespace ReportPerformance { |
| 51 | |
Eric Tan | e98dd6f | 2018-08-22 18:23:50 -0700 | [diff] [blame] | 52 | void Histogram::add(double value) |
| 53 | { |
| 54 | // TODO Handle domain and range error exceptions? |
| 55 | const int binIndex = lround((value - mLow) / mBinSize); |
| 56 | if (binIndex < 0) { |
| 57 | mLowCount++; |
| 58 | } else if (binIndex >= mNumBins) { |
| 59 | mHighCount++; |
| 60 | } else { |
| 61 | mBins[binIndex]++; |
| 62 | } |
| 63 | mTotalCount++; |
| 64 | } |
| 65 | |
| 66 | void Histogram::clear() |
| 67 | { |
| 68 | std::fill(mBins.begin(), mBins.end(), 0); |
| 69 | mLowCount = 0; |
| 70 | mHighCount = 0; |
| 71 | mTotalCount = 0; |
| 72 | } |
| 73 | |
| 74 | uint64_t Histogram::totalCount() const |
| 75 | { |
| 76 | return mTotalCount; |
| 77 | } |
| 78 | |
Eric Tan | fefe316 | 2018-09-07 10:09:11 -0700 | [diff] [blame] | 79 | std::string Histogram::toString() const { |
Eric Tan | e98dd6f | 2018-08-22 18:23:50 -0700 | [diff] [blame] | 80 | std::stringstream ss; |
| 81 | static constexpr char kDivider = '|'; |
Eric Tan | 24ee2da | 2018-09-13 10:53:17 -0700 | [diff] [blame] | 82 | ss << kVersion << "," << mBinSize << "," << mNumBins << "," << mLow << ",{"; |
Eric Tan | e98dd6f | 2018-08-22 18:23:50 -0700 | [diff] [blame] | 83 | bool first = true; |
| 84 | if (mLowCount != 0) { |
| 85 | ss << "-1" << kDivider << mLowCount; |
| 86 | first = false; |
| 87 | } |
| 88 | for (size_t i = 0; i < mNumBins; i++) { |
| 89 | if (mBins[i] != 0) { |
| 90 | if (!first) { |
| 91 | ss << ","; |
| 92 | } |
| 93 | ss << i << kDivider << mBins[i]; |
| 94 | first = false; |
| 95 | } |
| 96 | } |
| 97 | if (mHighCount != 0) { |
| 98 | if (!first) { |
| 99 | ss << ","; |
| 100 | } |
| 101 | ss << mNumBins << kDivider << mHighCount; |
| 102 | first = false; |
| 103 | } |
| 104 | ss << "}"; |
| 105 | |
| 106 | return ss.str(); |
| 107 | } |
| 108 | |
Eric Tan | e98dd6f | 2018-08-22 18:23:50 -0700 | [diff] [blame] | 109 | //------------------------------------------------------------------------------ |
| 110 | |
Sanna Catherine de Treville Wager | 1431644 | 2017-08-11 10:45:29 -0700 | [diff] [blame] | 111 | // Given an audio processing wakeup timestamp, buckets the time interval |
| 112 | // since the previous timestamp into a histogram, searches for |
Sanna Catherine de Treville Wager | a8a8a47 | 2017-07-11 09:41:25 -0700 | [diff] [blame] | 113 | // outliers, analyzes the outlier series for unexpectedly |
Sanna Catherine de Treville Wager | 8576894 | 2017-07-26 20:17:30 -0700 | [diff] [blame] | 114 | // small or large values and stores these as peaks |
Sanna Catherine de Treville Wager | 6ad40ee | 2017-07-28 10:10:55 -0700 | [diff] [blame] | 115 | void PerformanceAnalysis::logTsEntry(timestamp ts) { |
Sanna Catherine de Treville Wager | 8576894 | 2017-07-26 20:17:30 -0700 | [diff] [blame] | 116 | // after a state change, start a new series and do not |
| 117 | // record time intervals in-between |
Sanna Catherine de Treville Wager | 6ad40ee | 2017-07-28 10:10:55 -0700 | [diff] [blame] | 118 | if (mBufferPeriod.mPrevTs == 0) { |
| 119 | mBufferPeriod.mPrevTs = ts; |
Sanna Catherine de Treville Wager | a80649a | 2017-07-21 16:16:38 -0700 | [diff] [blame] | 120 | return; |
| 121 | } |
| 122 | |
Sanna Catherine de Treville Wager | 6ad40ee | 2017-07-28 10:10:55 -0700 | [diff] [blame] | 123 | // calculate time interval between current and previous timestamp |
| 124 | const msInterval diffMs = static_cast<msInterval>( |
| 125 | deltaMs(mBufferPeriod.mPrevTs, ts)); |
| 126 | |
| 127 | const int diffJiffy = deltaJiffy(mBufferPeriod.mPrevTs, ts); |
| 128 | |
Sanna Catherine de Treville Wager | 6ad40ee | 2017-07-28 10:10:55 -0700 | [diff] [blame] | 129 | // old versus new weight ratio when updating the buffer period mean |
| 130 | static constexpr double exponentialWeight = 0.999; |
| 131 | // update buffer period mean with exponential weighting |
| 132 | mBufferPeriod.mMean = (mBufferPeriod.mMean < 0) ? diffMs : |
| 133 | exponentialWeight * mBufferPeriod.mMean + (1.0 - exponentialWeight) * diffMs; |
| 134 | // set mOutlierFactor to a smaller value for the fastmixer thread |
| 135 | const int kFastMixerMax = 10; |
| 136 | // NormalMixer times vary much more than FastMixer times. |
| 137 | // TODO: mOutlierFactor values are set empirically based on what appears to be |
| 138 | // an outlier. Learn these values from the data. |
Sanna Catherine de Treville Wager | 847b6e6 | 2017-08-03 11:35:51 -0700 | [diff] [blame] | 139 | mBufferPeriod.mOutlierFactor = mBufferPeriod.mMean < kFastMixerMax ? 1.8 : 2.0; |
Sanna Catherine de Treville Wager | 6ad40ee | 2017-07-28 10:10:55 -0700 | [diff] [blame] | 140 | // set outlier threshold |
| 141 | mBufferPeriod.mOutlier = mBufferPeriod.mMean * mBufferPeriod.mOutlierFactor; |
| 142 | |
Sanna Catherine de Treville Wager | 8576894 | 2017-07-26 20:17:30 -0700 | [diff] [blame] | 143 | // Check whether the time interval between the current timestamp |
| 144 | // and the previous one is long enough to count as an outlier |
Sanna Catherine de Treville Wager | 6ad40ee | 2017-07-28 10:10:55 -0700 | [diff] [blame] | 145 | const bool isOutlier = detectAndStoreOutlier(diffMs); |
Sanna Catherine de Treville Wager | 8576894 | 2017-07-26 20:17:30 -0700 | [diff] [blame] | 146 | // If an outlier was found, check whether it was a peak |
| 147 | if (isOutlier) { |
| 148 | /*bool isPeak =*/ detectAndStorePeak( |
| 149 | mOutlierData[0].first, mOutlierData[0].second); |
| 150 | // TODO: decide whether to insert a new empty histogram if a peak |
| 151 | // TODO: remove isPeak if unused to avoid "unused variable" error |
Sanna Catherine de Treville Wager | 0a3959e | 2017-07-25 16:08:17 -0700 | [diff] [blame] | 152 | // occurred at the current timestamp |
Sanna Catherine de Treville Wager | a80649a | 2017-07-21 16:16:38 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Sanna Catherine de Treville Wager | 8576894 | 2017-07-26 20:17:30 -0700 | [diff] [blame] | 155 | // Insert a histogram to mHists if it is empty, or |
| 156 | // close the current histogram and insert a new empty one if |
| 157 | // if the current histogram has spanned its maximum time interval. |
| 158 | if (mHists.empty() || |
| 159 | deltaMs(mHists[0].first, ts) >= kMaxLength.HistTimespanMs) { |
Sanna Catherine de Treville Wager | 6ad40ee | 2017-07-28 10:10:55 -0700 | [diff] [blame] | 160 | mHists.emplace_front(ts, std::map<int, int>()); |
Sanna Catherine de Treville Wager | 8576894 | 2017-07-26 20:17:30 -0700 | [diff] [blame] | 161 | // When memory is full, delete oldest histogram |
| 162 | // TODO: use a circular buffer |
| 163 | if (mHists.size() >= kMaxLength.Hists) { |
| 164 | mHists.resize(kMaxLength.Hists); |
| 165 | } |
| 166 | } |
| 167 | // add current time intervals to histogram |
Sanna Catherine de Treville Wager | 6ad40ee | 2017-07-28 10:10:55 -0700 | [diff] [blame] | 168 | ++mHists[0].second[diffJiffy]; |
Sanna Catherine de Treville Wager | 8576894 | 2017-07-26 20:17:30 -0700 | [diff] [blame] | 169 | // update previous timestamp |
Sanna Catherine de Treville Wager | 6ad40ee | 2017-07-28 10:10:55 -0700 | [diff] [blame] | 170 | mBufferPeriod.mPrevTs = ts; |
Sanna Catherine de Treville Wager | a8a8a47 | 2017-07-11 09:41:25 -0700 | [diff] [blame] | 171 | } |
| 172 | |
Sanna Catherine de Treville Wager | 8576894 | 2017-07-26 20:17:30 -0700 | [diff] [blame] | 173 | |
Sanna Catherine de Treville Wager | a8a8a47 | 2017-07-11 09:41:25 -0700 | [diff] [blame] | 174 | // forces short-term histogram storage to avoid adding idle audio time interval |
| 175 | // to buffer period data |
Sanna Catherine de Treville Wager | 8044808 | 2017-07-11 14:07:59 -0700 | [diff] [blame] | 176 | void PerformanceAnalysis::handleStateChange() { |
Sanna Catherine de Treville Wager | 6ad40ee | 2017-07-28 10:10:55 -0700 | [diff] [blame] | 177 | mBufferPeriod.mPrevTs = 0; |
Sanna Catherine de Treville Wager | a8a8a47 | 2017-07-11 09:41:25 -0700 | [diff] [blame] | 178 | return; |
| 179 | } |
| 180 | |
Sanna Catherine de Treville Wager | 41cad59 | 2017-06-29 14:57:59 -0700 | [diff] [blame] | 181 | |
Sanna Catherine de Treville Wager | 0a3959e | 2017-07-25 16:08:17 -0700 | [diff] [blame] | 182 | // Checks whether the time interval between two outliers is far enough from |
| 183 | // a typical delta to be considered a peak. |
Sanna Catherine de Treville Wager | 41cad59 | 2017-06-29 14:57:59 -0700 | [diff] [blame] | 184 | // looks for changes in distribution (peaks), which can be either positive or negative. |
| 185 | // The function sets the mean to the starting value and sigma to 0, and updates |
| 186 | // them as long as no peak is detected. When a value is more than 'threshold' |
| 187 | // standard deviations from the mean, a peak is detected and the mean and sigma |
| 188 | // are set to the peak value and 0. |
Sanna Catherine de Treville Wager | 6ad40ee | 2017-07-28 10:10:55 -0700 | [diff] [blame] | 189 | bool PerformanceAnalysis::detectAndStorePeak(msInterval diff, timestamp ts) { |
Sanna Catherine de Treville Wager | 0a3959e | 2017-07-25 16:08:17 -0700 | [diff] [blame] | 190 | bool isPeak = false; |
Sanna Catherine de Treville Wager | 41cad59 | 2017-06-29 14:57:59 -0700 | [diff] [blame] | 191 | if (mOutlierData.empty()) { |
Sanna Catherine de Treville Wager | 0a3959e | 2017-07-25 16:08:17 -0700 | [diff] [blame] | 192 | return false; |
Sanna Catherine de Treville Wager | 41cad59 | 2017-06-29 14:57:59 -0700 | [diff] [blame] | 193 | } |
Sanna Catherine de Treville Wager | 0a3959e | 2017-07-25 16:08:17 -0700 | [diff] [blame] | 194 | // Update mean of the distribution |
| 195 | // TypicalDiff is used to check whether a value is unusually large |
| 196 | // when we cannot use standard deviations from the mean because the sd is set to 0. |
| 197 | mOutlierDistribution.mTypicalDiff = (mOutlierDistribution.mTypicalDiff * |
| 198 | (mOutlierData.size() - 1) + diff) / mOutlierData.size(); |
Sanna Catherine de Treville Wager | 41cad59 | 2017-06-29 14:57:59 -0700 | [diff] [blame] | 199 | |
Sanna Catherine de Treville Wager | 0a3959e | 2017-07-25 16:08:17 -0700 | [diff] [blame] | 200 | // Initialize short-term mean at start of program |
| 201 | if (mOutlierDistribution.mMean == 0) { |
Sanna Catherine de Treville Wager | 6ad40ee | 2017-07-28 10:10:55 -0700 | [diff] [blame] | 202 | mOutlierDistribution.mMean = diff; |
Sanna Catherine de Treville Wager | 0a3959e | 2017-07-25 16:08:17 -0700 | [diff] [blame] | 203 | } |
| 204 | // Update length of current sequence of outliers |
| 205 | mOutlierDistribution.mN++; |
Sanna Catherine de Treville Wager | 41cad59 | 2017-06-29 14:57:59 -0700 | [diff] [blame] | 206 | |
Sanna Catherine de Treville Wager | 6ad40ee | 2017-07-28 10:10:55 -0700 | [diff] [blame] | 207 | // Check whether a large deviation from the mean occurred. |
Sanna Catherine de Treville Wager | 0a3959e | 2017-07-25 16:08:17 -0700 | [diff] [blame] | 208 | // If the standard deviation has been reset to zero, the comparison is |
| 209 | // instead to the mean of the full mOutlierInterval sequence. |
Sanna Catherine de Treville Wager | 6ad40ee | 2017-07-28 10:10:55 -0700 | [diff] [blame] | 210 | if ((fabs(diff - mOutlierDistribution.mMean) < |
Sanna Catherine de Treville Wager | 0a3959e | 2017-07-25 16:08:17 -0700 | [diff] [blame] | 211 | mOutlierDistribution.kMaxDeviation * mOutlierDistribution.mSd) || |
| 212 | (mOutlierDistribution.mSd == 0 && |
| 213 | fabs(diff - mOutlierDistribution.mMean) < |
| 214 | mOutlierDistribution.mTypicalDiff)) { |
| 215 | // update the mean and sd using online algorithm |
| 216 | // https://en.wikipedia.org/wiki/ |
| 217 | // Algorithms_for_calculating_variance#Online_algorithm |
| 218 | mOutlierDistribution.mN++; |
| 219 | const double kDelta = diff - mOutlierDistribution.mMean; |
| 220 | mOutlierDistribution.mMean += kDelta / mOutlierDistribution.mN; |
| 221 | const double kDelta2 = diff - mOutlierDistribution.mMean; |
| 222 | mOutlierDistribution.mM2 += kDelta * kDelta2; |
| 223 | mOutlierDistribution.mSd = (mOutlierDistribution.mN < 2) ? 0 : |
Sanna Catherine de Treville Wager | 8576894 | 2017-07-26 20:17:30 -0700 | [diff] [blame] | 224 | sqrt(mOutlierDistribution.mM2 / (mOutlierDistribution.mN - 1)); |
Sanna Catherine de Treville Wager | 0a3959e | 2017-07-25 16:08:17 -0700 | [diff] [blame] | 225 | } else { |
| 226 | // new value is far from the mean: |
| 227 | // store peak timestamp and reset mean, sd, and short-term sequence |
| 228 | isPeak = true; |
| 229 | mPeakTimestamps.emplace_front(ts); |
| 230 | // if mPeaks has reached capacity, delete oldest data |
| 231 | // Note: this means that mOutlierDistribution values do not exactly |
| 232 | // match the data we have in mPeakTimestamps, but this is not an issue |
| 233 | // in practice for estimating future peaks. |
| 234 | // TODO: turn this into a circular buffer |
| 235 | if (mPeakTimestamps.size() >= kMaxLength.Peaks) { |
| 236 | mPeakTimestamps.resize(kMaxLength.Peaks); |
Sanna Catherine de Treville Wager | 41cad59 | 2017-06-29 14:57:59 -0700 | [diff] [blame] | 237 | } |
Sanna Catherine de Treville Wager | 0a3959e | 2017-07-25 16:08:17 -0700 | [diff] [blame] | 238 | mOutlierDistribution.mMean = 0; |
| 239 | mOutlierDistribution.mSd = 0; |
| 240 | mOutlierDistribution.mN = 0; |
| 241 | mOutlierDistribution.mM2 = 0; |
Sanna Catherine de Treville Wager | 41cad59 | 2017-06-29 14:57:59 -0700 | [diff] [blame] | 242 | } |
Sanna Catherine de Treville Wager | 0a3959e | 2017-07-25 16:08:17 -0700 | [diff] [blame] | 243 | return isPeak; |
Sanna Catherine de Treville Wager | 41cad59 | 2017-06-29 14:57:59 -0700 | [diff] [blame] | 244 | } |
| 245 | |
Sanna Catherine de Treville Wager | 8576894 | 2017-07-26 20:17:30 -0700 | [diff] [blame] | 246 | |
Sanna Catherine de Treville Wager | 0a3959e | 2017-07-25 16:08:17 -0700 | [diff] [blame] | 247 | // Determines whether the difference between a timestamp and the previous |
| 248 | // one is beyond a threshold. If yes, stores the timestamp as an outlier |
| 249 | // and writes to mOutlierdata in the following format: |
| 250 | // Time elapsed since previous outlier: Timestamp of start of outlier |
Sanna Catherine de Treville Wager | 41cad59 | 2017-06-29 14:57:59 -0700 | [diff] [blame] | 251 | // e.g. timestamps (ms) 1, 4, 5, 16, 18, 28 will produce pairs (4, 5), (13, 18). |
Sanna Catherine de Treville Wager | 2a6a945 | 2017-07-28 11:02:01 -0700 | [diff] [blame] | 252 | // TODO: learn what timestamp sequences correlate with glitches instead of |
| 253 | // manually designing a heuristic. |
Sanna Catherine de Treville Wager | 6ad40ee | 2017-07-28 10:10:55 -0700 | [diff] [blame] | 254 | bool PerformanceAnalysis::detectAndStoreOutlier(const msInterval diffMs) { |
Sanna Catherine de Treville Wager | 0a3959e | 2017-07-25 16:08:17 -0700 | [diff] [blame] | 255 | bool isOutlier = false; |
Sanna Catherine de Treville Wager | 6ad40ee | 2017-07-28 10:10:55 -0700 | [diff] [blame] | 256 | if (diffMs >= mBufferPeriod.mOutlier) { |
Sanna Catherine de Treville Wager | 0a3959e | 2017-07-25 16:08:17 -0700 | [diff] [blame] | 257 | isOutlier = true; |
Sanna Catherine de Treville Wager | 6ad40ee | 2017-07-28 10:10:55 -0700 | [diff] [blame] | 258 | mOutlierData.emplace_front( |
| 259 | mOutlierDistribution.mElapsed, mBufferPeriod.mPrevTs); |
Sanna Catherine de Treville Wager | 0a3959e | 2017-07-25 16:08:17 -0700 | [diff] [blame] | 260 | // Remove oldest value if the vector is full |
| 261 | // TODO: turn this into a circular buffer |
| 262 | // TODO: make sure kShortHistSize is large enough that that data will never be lost |
| 263 | // before being written to file or to a FIFO |
| 264 | if (mOutlierData.size() >= kMaxLength.Outliers) { |
| 265 | mOutlierData.resize(kMaxLength.Outliers); |
Sanna Catherine de Treville Wager | 41cad59 | 2017-06-29 14:57:59 -0700 | [diff] [blame] | 266 | } |
Sanna Catherine de Treville Wager | 0a3959e | 2017-07-25 16:08:17 -0700 | [diff] [blame] | 267 | mOutlierDistribution.mElapsed = 0; |
Sanna Catherine de Treville Wager | 41cad59 | 2017-06-29 14:57:59 -0700 | [diff] [blame] | 268 | } |
Sanna Catherine de Treville Wager | 0a3959e | 2017-07-25 16:08:17 -0700 | [diff] [blame] | 269 | mOutlierDistribution.mElapsed += diffMs; |
Sanna Catherine de Treville Wager | 0a3959e | 2017-07-25 16:08:17 -0700 | [diff] [blame] | 270 | return isOutlier; |
Sanna Catherine de Treville Wager | 41cad59 | 2017-06-29 14:57:59 -0700 | [diff] [blame] | 271 | } |
| 272 | |
Sanna Catherine de Treville Wager | 2a6a945 | 2017-07-28 11:02:01 -0700 | [diff] [blame] | 273 | // rounds value to precision based on log-distance from mean |
Ivan Lozano | bb4b8b5 | 2017-11-30 15:43:19 -0800 | [diff] [blame] | 274 | __attribute__((no_sanitize("signed-integer-overflow"))) |
Sanna Catherine de Treville Wager | 2a6a945 | 2017-07-28 11:02:01 -0700 | [diff] [blame] | 275 | inline double logRound(double x, double mean) { |
Sanna Catherine de Treville Wager | 847b6e6 | 2017-08-03 11:35:51 -0700 | [diff] [blame] | 276 | // Larger values decrease range of high resolution and prevent overflow |
| 277 | // of a histogram on the console. |
| 278 | // The following formula adjusts kBase based on the buffer period length. |
| 279 | // Different threads have buffer periods ranging from 2 to 40. The |
| 280 | // formula below maps buffer period 2 to kBase = ~1, 4 to ~2, 20 to ~3, 40 to ~4. |
| 281 | // TODO: tighten this for higher means, the data still overflows |
| 282 | const double kBase = log(mean) / log(2.2); |
Sanna Catherine de Treville Wager | 2a6a945 | 2017-07-28 11:02:01 -0700 | [diff] [blame] | 283 | const double power = floor( |
Sanna Catherine de Treville Wager | 847b6e6 | 2017-08-03 11:35:51 -0700 | [diff] [blame] | 284 | log(abs(x - mean) / mean) / log(kBase)) + 2; |
Sanna Catherine de Treville Wager | 2a6a945 | 2017-07-28 11:02:01 -0700 | [diff] [blame] | 285 | // do not round values close to the mean |
| 286 | if (power < 1) { |
| 287 | return x; |
| 288 | } |
| 289 | const int factor = static_cast<int>(pow(10, power)); |
| 290 | return (static_cast<int>(x) * factor) / factor; |
Sanna Catherine de Treville Wager | 6ad40ee | 2017-07-28 10:10:55 -0700 | [diff] [blame] | 291 | } |
Sanna Catherine de Treville Wager | 8576894 | 2017-07-26 20:17:30 -0700 | [diff] [blame] | 292 | |
Sanna Catherine de Treville Wager | 6ad40ee | 2017-07-28 10:10:55 -0700 | [diff] [blame] | 293 | // TODO Make it return a std::string instead of modifying body |
Sanna Catherine de Treville Wager | 2a6a945 | 2017-07-28 11:02:01 -0700 | [diff] [blame] | 294 | // TODO: move this to ReportPerformance, probably make it a friend function |
| 295 | // of PerformanceAnalysis |
| 296 | void PerformanceAnalysis::reportPerformance(String8 *body, int author, log_hash_t hash, |
| 297 | int maxHeight) { |
Eric Tan | 8180b74 | 2018-07-10 15:23:29 -0700 | [diff] [blame] | 298 | if (mHists.empty() || body == nullptr) { |
Sanna Catherine de Treville Wager | d0dfe43 | 2017-06-22 15:09:38 -0700 | [diff] [blame] | 299 | return; |
| 300 | } |
Sanna Catherine de Treville Wager | 0a3959e | 2017-07-25 16:08:17 -0700 | [diff] [blame] | 301 | |
Sanna Catherine de Treville Wager | 2a6a945 | 2017-07-28 11:02:01 -0700 | [diff] [blame] | 302 | // ms of active audio in displayed histogram |
| 303 | double elapsedMs = 0; |
| 304 | // starting timestamp of histogram |
| 305 | timestamp startingTs = mHists[0].first; |
| 306 | |
| 307 | // histogram which stores .1 precision ms counts instead of Jiffy multiple counts |
Sanna Catherine de Treville Wager | 2a6a945 | 2017-07-28 11:02:01 -0700 | [diff] [blame] | 308 | std::map<double, int> buckets; |
Sanna Catherine de Treville Wager | a80649a | 2017-07-21 16:16:38 -0700 | [diff] [blame] | 309 | for (const auto &shortHist: mHists) { |
Sanna Catherine de Treville Wager | d0dfe43 | 2017-06-22 15:09:38 -0700 | [diff] [blame] | 310 | for (const auto &countPair : shortHist.second) { |
Sanna Catherine de Treville Wager | 2a6a945 | 2017-07-28 11:02:01 -0700 | [diff] [blame] | 311 | const double ms = static_cast<double>(countPair.first) / kJiffyPerMs; |
| 312 | buckets[logRound(ms, mBufferPeriod.mMean)] += countPair.second; |
Sanna Catherine de Treville Wager | 847b6e6 | 2017-08-03 11:35:51 -0700 | [diff] [blame] | 313 | elapsedMs += ms * countPair.second; |
Sanna Catherine de Treville Wager | d0dfe43 | 2017-06-22 15:09:38 -0700 | [diff] [blame] | 314 | } |
| 315 | } |
| 316 | |
Eric Tan | 8180b74 | 2018-07-10 15:23:29 -0700 | [diff] [blame] | 317 | static const int SIZE = 128; |
| 318 | char title[SIZE]; |
| 319 | snprintf(title, sizeof(title), "\n%s %3.2f %s\n%s%d, %lld, %lld\n", |
| 320 | "Occurrences in", (elapsedMs / kMsPerSec), "seconds of audio:", |
| 321 | "Thread, hash, starting timestamp: ", author, |
| 322 | static_cast<long long>(hash), static_cast<long long>(startingTs)); |
| 323 | static const char * const kLabel = "ms"; |
Sanna Catherine de Treville Wager | d0dfe43 | 2017-06-22 15:09:38 -0700 | [diff] [blame] | 324 | |
Eric Tan | 39ec8d6 | 2018-07-24 09:49:29 -0700 | [diff] [blame] | 325 | body->appendFormat("%s", |
| 326 | audio_utils_plot_histogram(buckets, title, kLabel, maxHeight).c_str()); |
Sanna Catherine de Treville Wager | d0dfe43 | 2017-06-22 15:09:38 -0700 | [diff] [blame] | 327 | |
Sanna Catherine de Treville Wager | 316f1fd | 2017-06-23 09:10:15 -0700 | [diff] [blame] | 328 | // Now report glitches |
Sanna Catherine de Treville Wager | 2a6a945 | 2017-07-28 11:02:01 -0700 | [diff] [blame] | 329 | body->appendFormat("\ntime elapsed between glitches and glitch timestamps:\n"); |
Sanna Catherine de Treville Wager | 316f1fd | 2017-06-23 09:10:15 -0700 | [diff] [blame] | 330 | for (const auto &outlier: mOutlierData) { |
| 331 | body->appendFormat("%lld: %lld\n", static_cast<long long>(outlier.first), |
| 332 | static_cast<long long>(outlier.second)); |
| 333 | } |
Sanna Catherine de Treville Wager | d0dfe43 | 2017-06-22 15:09:38 -0700 | [diff] [blame] | 334 | } |
| 335 | |
Sanna Catherine de Treville Wager | cf6c75a | 2017-07-21 17:05:25 -0700 | [diff] [blame] | 336 | //------------------------------------------------------------------------------ |
| 337 | |
| 338 | // writes summary of performance into specified file descriptor |
Sanna Catherine de Treville Wager | 23f89d3 | 2017-07-24 18:24:48 -0700 | [diff] [blame] | 339 | void dump(int fd, int indent, PerformanceAnalysisMap &threadPerformanceAnalysis) { |
Sanna Catherine de Treville Wager | cf6c75a | 2017-07-21 17:05:25 -0700 | [diff] [blame] | 340 | String8 body; |
Eric Tan | e98dd6f | 2018-08-22 18:23:50 -0700 | [diff] [blame] | 341 | #ifdef WRITE_TO_FILE |
Sanna Catherine de Treville Wager | f8c3428 | 2017-07-25 11:31:18 -0700 | [diff] [blame] | 342 | const char* const kDirectory = "/data/misc/audioserver/"; |
Eric Tan | e98dd6f | 2018-08-22 18:23:50 -0700 | [diff] [blame] | 343 | #endif |
Sanna Catherine de Treville Wager | cf6c75a | 2017-07-21 17:05:25 -0700 | [diff] [blame] | 344 | for (auto & thread : threadPerformanceAnalysis) { |
Sanna Catherine de Treville Wager | d096517 | 2017-07-24 13:42:44 -0700 | [diff] [blame] | 345 | for (auto & hash: thread.second) { |
Sanna Catherine de Treville Wager | 23f89d3 | 2017-07-24 18:24:48 -0700 | [diff] [blame] | 346 | PerformanceAnalysis& curr = hash.second; |
Sanna Catherine de Treville Wager | 23f89d3 | 2017-07-24 18:24:48 -0700 | [diff] [blame] | 347 | // write performance data to console |
Sanna Catherine de Treville Wager | 2a6a945 | 2017-07-28 11:02:01 -0700 | [diff] [blame] | 348 | curr.reportPerformance(&body, thread.first, hash.first); |
Sanna Catherine de Treville Wager | 0a3959e | 2017-07-25 16:08:17 -0700 | [diff] [blame] | 349 | if (!body.isEmpty()) { |
| 350 | dumpLine(fd, indent, body); |
| 351 | body.clear(); |
| 352 | } |
Eric Tan | e98dd6f | 2018-08-22 18:23:50 -0700 | [diff] [blame] | 353 | #ifdef WRITE_TO_FILE |
| 354 | // write to file. Enable by uncommenting macro at top of file. |
Sanna Catherine de Treville Wager | f8c3428 | 2017-07-25 11:31:18 -0700 | [diff] [blame] | 355 | writeToFile(curr.mHists, curr.mOutlierData, curr.mPeakTimestamps, |
| 356 | kDirectory, false, thread.first, hash.first); |
Eric Tan | e98dd6f | 2018-08-22 18:23:50 -0700 | [diff] [blame] | 357 | #endif |
Sanna Catherine de Treville Wager | d096517 | 2017-07-24 13:42:44 -0700 | [diff] [blame] | 358 | } |
Sanna Catherine de Treville Wager | cf6c75a | 2017-07-21 17:05:25 -0700 | [diff] [blame] | 359 | } |
Sanna Catherine de Treville Wager | cf6c75a | 2017-07-21 17:05:25 -0700 | [diff] [blame] | 360 | } |
| 361 | |
Sanna Catherine de Treville Wager | 8576894 | 2017-07-26 20:17:30 -0700 | [diff] [blame] | 362 | |
Sanna Catherine de Treville Wager | cf6c75a | 2017-07-21 17:05:25 -0700 | [diff] [blame] | 363 | // Writes a string into specified file descriptor |
| 364 | void dumpLine(int fd, int indent, const String8 &body) { |
| 365 | dprintf(fd, "%.*s%s \n", indent, "", body.string()); |
| 366 | } |
| 367 | |
Sanna Catherine de Treville Wager | 8044808 | 2017-07-11 14:07:59 -0700 | [diff] [blame] | 368 | } // namespace ReportPerformance |
Sanna Catherine de Treville Wager | d0dfe43 | 2017-06-22 15:09:38 -0700 | [diff] [blame] | 369 | } // namespace android |