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 |
| 20 | |
| 21 | #include <algorithm> |
| 22 | #include <climits> |
| 23 | #include <deque> |
Sanna Catherine de Treville Wager | d0dfe43 | 2017-06-22 15:09:38 -0700 | [diff] [blame] | 24 | #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> |
| 36 | #include <media/nbaio/NBLog.h> |
| 37 | #include <media/nbaio/PerformanceAnalysis.h> |
Sanna Catherine de Treville Wager | 8044808 | 2017-07-11 14:07:59 -0700 | [diff] [blame] | 38 | #include <media/nbaio/ReportPerformance.h> |
Sanna Catherine de Treville Wager | d0dfe43 | 2017-06-22 15:09:38 -0700 | [diff] [blame] | 39 | #include <utils/Log.h> |
| 40 | #include <utils/String8.h> |
| 41 | |
| 42 | #include <queue> |
| 43 | #include <utility> |
| 44 | |
| 45 | namespace android { |
| 46 | |
Sanna Catherine de Treville Wager | 8044808 | 2017-07-11 14:07:59 -0700 | [diff] [blame] | 47 | namespace ReportPerformance { |
| 48 | |
Sanna Catherine de Treville Wager | 41cad59 | 2017-06-29 14:57:59 -0700 | [diff] [blame] | 49 | PerformanceAnalysis::PerformanceAnalysis() { |
| 50 | // These variables will be (FIXME) learned from the data |
| 51 | kPeriodMs = 4; // typical buffer period (mode) |
| 52 | // average number of Ms spent processing buffer |
| 53 | kPeriodMsCPU = static_cast<int>(kPeriodMs * kRatio); |
| 54 | } |
| 55 | |
Sanna Catherine de Treville Wager | d0dfe43 | 2017-06-22 15:09:38 -0700 | [diff] [blame] | 56 | static int widthOf(int x) { |
| 57 | int width = 0; |
| 58 | while (x > 0) { |
| 59 | ++width; |
| 60 | x /= 10; |
| 61 | } |
| 62 | return width; |
| 63 | } |
| 64 | |
Sanna Catherine de Treville Wager | 8576894 | 2017-07-26 20:17:30 -0700 | [diff] [blame^] | 65 | |
| 66 | // Given a the most recent timestamp of a series of audio processing |
| 67 | // wakeup timestamps, |
| 68 | // buckets the time interval into a histogram, searches for |
Sanna Catherine de Treville Wager | a8a8a47 | 2017-07-11 09:41:25 -0700 | [diff] [blame] | 69 | // outliers, analyzes the outlier series for unexpectedly |
Sanna Catherine de Treville Wager | 8576894 | 2017-07-26 20:17:30 -0700 | [diff] [blame^] | 70 | // small or large values and stores these as peaks |
| 71 | void PerformanceAnalysis::logTsEntry(int64_t ts) { |
| 72 | // after a state change, start a new series and do not |
| 73 | // record time intervals in-between |
| 74 | if (mOutlierDistribution.mPrevTs == 0) { |
| 75 | mOutlierDistribution.mPrevTs = ts; |
Sanna Catherine de Treville Wager | a80649a | 2017-07-21 16:16:38 -0700 | [diff] [blame] | 76 | return; |
| 77 | } |
| 78 | |
Sanna Catherine de Treville Wager | 8576894 | 2017-07-26 20:17:30 -0700 | [diff] [blame^] | 79 | // Check whether the time interval between the current timestamp |
| 80 | // and the previous one is long enough to count as an outlier |
| 81 | const bool isOutlier = detectAndStoreOutlier(ts); |
| 82 | // If an outlier was found, check whether it was a peak |
| 83 | if (isOutlier) { |
| 84 | /*bool isPeak =*/ detectAndStorePeak( |
| 85 | mOutlierData[0].first, mOutlierData[0].second); |
| 86 | // TODO: decide whether to insert a new empty histogram if a peak |
| 87 | // 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] | 88 | // occurred at the current timestamp |
Sanna Catherine de Treville Wager | a80649a | 2017-07-21 16:16:38 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Sanna Catherine de Treville Wager | 8576894 | 2017-07-26 20:17:30 -0700 | [diff] [blame^] | 91 | // Insert a histogram to mHists if it is empty, or |
| 92 | // close the current histogram and insert a new empty one if |
| 93 | // if the current histogram has spanned its maximum time interval. |
| 94 | if (mHists.empty() || |
| 95 | deltaMs(mHists[0].first, ts) >= kMaxLength.HistTimespanMs) { |
| 96 | mHists.emplace_front(static_cast<uint64_t>(ts), std::map<int, int>()); |
| 97 | // When memory is full, delete oldest histogram |
| 98 | // TODO: use a circular buffer |
| 99 | if (mHists.size() >= kMaxLength.Hists) { |
| 100 | mHists.resize(kMaxLength.Hists); |
| 101 | } |
| 102 | } |
| 103 | // add current time intervals to histogram |
| 104 | ++mHists[0].second[deltaMs(mOutlierDistribution.mPrevTs, ts)]; |
| 105 | // update previous timestamp |
| 106 | mOutlierDistribution.mPrevTs = ts; |
Sanna Catherine de Treville Wager | a8a8a47 | 2017-07-11 09:41:25 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Sanna Catherine de Treville Wager | 8576894 | 2017-07-26 20:17:30 -0700 | [diff] [blame^] | 109 | |
Sanna Catherine de Treville Wager | a8a8a47 | 2017-07-11 09:41:25 -0700 | [diff] [blame] | 110 | // forces short-term histogram storage to avoid adding idle audio time interval |
| 111 | // to buffer period data |
Sanna Catherine de Treville Wager | 8044808 | 2017-07-11 14:07:59 -0700 | [diff] [blame] | 112 | void PerformanceAnalysis::handleStateChange() { |
Sanna Catherine de Treville Wager | 8576894 | 2017-07-26 20:17:30 -0700 | [diff] [blame^] | 113 | mOutlierDistribution.mPrevTs = 0; |
Sanna Catherine de Treville Wager | a8a8a47 | 2017-07-11 09:41:25 -0700 | [diff] [blame] | 114 | return; |
| 115 | } |
| 116 | |
Sanna Catherine de Treville Wager | 41cad59 | 2017-06-29 14:57:59 -0700 | [diff] [blame] | 117 | |
Sanna Catherine de Treville Wager | 0a3959e | 2017-07-25 16:08:17 -0700 | [diff] [blame] | 118 | // Checks whether the time interval between two outliers is far enough from |
| 119 | // a typical delta to be considered a peak. |
Sanna Catherine de Treville Wager | 41cad59 | 2017-06-29 14:57:59 -0700 | [diff] [blame] | 120 | // looks for changes in distribution (peaks), which can be either positive or negative. |
| 121 | // The function sets the mean to the starting value and sigma to 0, and updates |
| 122 | // them as long as no peak is detected. When a value is more than 'threshold' |
| 123 | // standard deviations from the mean, a peak is detected and the mean and sigma |
| 124 | // are set to the peak value and 0. |
Sanna Catherine de Treville Wager | 0a3959e | 2017-07-25 16:08:17 -0700 | [diff] [blame] | 125 | bool PerformanceAnalysis::detectAndStorePeak(outlierInterval diff, timestamp ts) { |
| 126 | bool isPeak = false; |
Sanna Catherine de Treville Wager | 41cad59 | 2017-06-29 14:57:59 -0700 | [diff] [blame] | 127 | if (mOutlierData.empty()) { |
Sanna Catherine de Treville Wager | 0a3959e | 2017-07-25 16:08:17 -0700 | [diff] [blame] | 128 | return false; |
Sanna Catherine de Treville Wager | 41cad59 | 2017-06-29 14:57:59 -0700 | [diff] [blame] | 129 | } |
Sanna Catherine de Treville Wager | 0a3959e | 2017-07-25 16:08:17 -0700 | [diff] [blame] | 130 | // Update mean of the distribution |
| 131 | // TypicalDiff is used to check whether a value is unusually large |
| 132 | // when we cannot use standard deviations from the mean because the sd is set to 0. |
| 133 | mOutlierDistribution.mTypicalDiff = (mOutlierDistribution.mTypicalDiff * |
| 134 | (mOutlierData.size() - 1) + diff) / mOutlierData.size(); |
Sanna Catherine de Treville Wager | 41cad59 | 2017-06-29 14:57:59 -0700 | [diff] [blame] | 135 | |
Sanna Catherine de Treville Wager | 0a3959e | 2017-07-25 16:08:17 -0700 | [diff] [blame] | 136 | // Initialize short-term mean at start of program |
| 137 | if (mOutlierDistribution.mMean == 0) { |
| 138 | mOutlierDistribution.mMean = static_cast<double>(diff); |
| 139 | } |
| 140 | // Update length of current sequence of outliers |
| 141 | mOutlierDistribution.mN++; |
Sanna Catherine de Treville Wager | 41cad59 | 2017-06-29 14:57:59 -0700 | [diff] [blame] | 142 | |
Sanna Catherine de Treville Wager | 0a3959e | 2017-07-25 16:08:17 -0700 | [diff] [blame] | 143 | // If statement checks whether a large deviation from the mean occurred. |
| 144 | // If the standard deviation has been reset to zero, the comparison is |
| 145 | // instead to the mean of the full mOutlierInterval sequence. |
| 146 | if ((fabs(static_cast<double>(diff) - mOutlierDistribution.mMean) < |
| 147 | mOutlierDistribution.kMaxDeviation * mOutlierDistribution.mSd) || |
| 148 | (mOutlierDistribution.mSd == 0 && |
| 149 | fabs(diff - mOutlierDistribution.mMean) < |
| 150 | mOutlierDistribution.mTypicalDiff)) { |
| 151 | // update the mean and sd using online algorithm |
| 152 | // https://en.wikipedia.org/wiki/ |
| 153 | // Algorithms_for_calculating_variance#Online_algorithm |
| 154 | mOutlierDistribution.mN++; |
| 155 | const double kDelta = diff - mOutlierDistribution.mMean; |
| 156 | mOutlierDistribution.mMean += kDelta / mOutlierDistribution.mN; |
| 157 | const double kDelta2 = diff - mOutlierDistribution.mMean; |
| 158 | mOutlierDistribution.mM2 += kDelta * kDelta2; |
| 159 | mOutlierDistribution.mSd = (mOutlierDistribution.mN < 2) ? 0 : |
Sanna Catherine de Treville Wager | 8576894 | 2017-07-26 20:17:30 -0700 | [diff] [blame^] | 160 | sqrt(mOutlierDistribution.mM2 / (mOutlierDistribution.mN - 1)); |
Sanna Catherine de Treville Wager | 0a3959e | 2017-07-25 16:08:17 -0700 | [diff] [blame] | 161 | } else { |
| 162 | // new value is far from the mean: |
| 163 | // store peak timestamp and reset mean, sd, and short-term sequence |
| 164 | isPeak = true; |
| 165 | mPeakTimestamps.emplace_front(ts); |
| 166 | // if mPeaks has reached capacity, delete oldest data |
| 167 | // Note: this means that mOutlierDistribution values do not exactly |
| 168 | // match the data we have in mPeakTimestamps, but this is not an issue |
| 169 | // in practice for estimating future peaks. |
| 170 | // TODO: turn this into a circular buffer |
| 171 | if (mPeakTimestamps.size() >= kMaxLength.Peaks) { |
| 172 | mPeakTimestamps.resize(kMaxLength.Peaks); |
Sanna Catherine de Treville Wager | 41cad59 | 2017-06-29 14:57:59 -0700 | [diff] [blame] | 173 | } |
Sanna Catherine de Treville Wager | 0a3959e | 2017-07-25 16:08:17 -0700 | [diff] [blame] | 174 | mOutlierDistribution.mMean = 0; |
| 175 | mOutlierDistribution.mSd = 0; |
| 176 | mOutlierDistribution.mN = 0; |
| 177 | mOutlierDistribution.mM2 = 0; |
Sanna Catherine de Treville Wager | 41cad59 | 2017-06-29 14:57:59 -0700 | [diff] [blame] | 178 | } |
Sanna Catherine de Treville Wager | 0a3959e | 2017-07-25 16:08:17 -0700 | [diff] [blame] | 179 | ALOGD("outlier distr %f %f", mOutlierDistribution.mMean, mOutlierDistribution.mSd); |
| 180 | return isPeak; |
Sanna Catherine de Treville Wager | 41cad59 | 2017-06-29 14:57:59 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Sanna Catherine de Treville Wager | 8576894 | 2017-07-26 20:17:30 -0700 | [diff] [blame^] | 183 | |
Sanna Catherine de Treville Wager | 0a3959e | 2017-07-25 16:08:17 -0700 | [diff] [blame] | 184 | // Determines whether the difference between a timestamp and the previous |
| 185 | // one is beyond a threshold. If yes, stores the timestamp as an outlier |
| 186 | // and writes to mOutlierdata in the following format: |
| 187 | // 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] | 188 | // e.g. timestamps (ms) 1, 4, 5, 16, 18, 28 will produce pairs (4, 5), (13, 18). |
Sanna Catherine de Treville Wager | 0a3959e | 2017-07-25 16:08:17 -0700 | [diff] [blame] | 189 | bool PerformanceAnalysis::detectAndStoreOutlier(const int64_t ts) { |
Sanna Catherine de Treville Wager | 0a3959e | 2017-07-25 16:08:17 -0700 | [diff] [blame] | 190 | bool isOutlier = false; |
Sanna Catherine de Treville Wager | 8576894 | 2017-07-26 20:17:30 -0700 | [diff] [blame^] | 191 | const int64_t diffMs = static_cast<int64_t>(deltaMs(mOutlierDistribution.mPrevTs, ts)); |
| 192 | if (diffMs >= static_cast<int64_t>(kOutlierMs)) { |
Sanna Catherine de Treville Wager | 0a3959e | 2017-07-25 16:08:17 -0700 | [diff] [blame] | 193 | isOutlier = true; |
Sanna Catherine de Treville Wager | 0a3959e | 2017-07-25 16:08:17 -0700 | [diff] [blame] | 194 | mOutlierData.emplace_front(mOutlierDistribution.mElapsed, |
Sanna Catherine de Treville Wager | 8576894 | 2017-07-26 20:17:30 -0700 | [diff] [blame^] | 195 | static_cast<uint64_t>(mOutlierDistribution.mPrevTs)); |
Sanna Catherine de Treville Wager | 0a3959e | 2017-07-25 16:08:17 -0700 | [diff] [blame] | 196 | // Remove oldest value if the vector is full |
| 197 | // TODO: turn this into a circular buffer |
| 198 | // TODO: make sure kShortHistSize is large enough that that data will never be lost |
| 199 | // before being written to file or to a FIFO |
| 200 | if (mOutlierData.size() >= kMaxLength.Outliers) { |
| 201 | mOutlierData.resize(kMaxLength.Outliers); |
Sanna Catherine de Treville Wager | 41cad59 | 2017-06-29 14:57:59 -0700 | [diff] [blame] | 202 | } |
Sanna Catherine de Treville Wager | 0a3959e | 2017-07-25 16:08:17 -0700 | [diff] [blame] | 203 | mOutlierDistribution.mElapsed = 0; |
Sanna Catherine de Treville Wager | 41cad59 | 2017-06-29 14:57:59 -0700 | [diff] [blame] | 204 | } |
Sanna Catherine de Treville Wager | 0a3959e | 2017-07-25 16:08:17 -0700 | [diff] [blame] | 205 | mOutlierDistribution.mElapsed += diffMs; |
Sanna Catherine de Treville Wager | 0a3959e | 2017-07-25 16:08:17 -0700 | [diff] [blame] | 206 | return isOutlier; |
Sanna Catherine de Treville Wager | 41cad59 | 2017-06-29 14:57:59 -0700 | [diff] [blame] | 207 | } |
| 208 | |
Sanna Catherine de Treville Wager | 8576894 | 2017-07-26 20:17:30 -0700 | [diff] [blame^] | 209 | |
Sanna Catherine de Treville Wager | 41cad59 | 2017-06-29 14:57:59 -0700 | [diff] [blame] | 210 | // TODO Make it return a std::string instead of modifying body --> is this still relevant? |
Sanna Catherine de Treville Wager | d0dfe43 | 2017-06-22 15:09:38 -0700 | [diff] [blame] | 211 | // TODO consider changing all ints to uint32_t or uint64_t |
Sanna Catherine de Treville Wager | 8044808 | 2017-07-11 14:07:59 -0700 | [diff] [blame] | 212 | // TODO: move this to ReportPerformance, probably make it a friend function of PerformanceAnalysis |
Sanna Catherine de Treville Wager | 23f89d3 | 2017-07-24 18:24:48 -0700 | [diff] [blame] | 213 | void PerformanceAnalysis::reportPerformance(String8 *body, int maxHeight) { |
Sanna Catherine de Treville Wager | a80649a | 2017-07-21 16:16:38 -0700 | [diff] [blame] | 214 | if (mHists.empty()) { |
| 215 | ALOGD("reportPerformance: mHists is empty"); |
Sanna Catherine de Treville Wager | d0dfe43 | 2017-06-22 15:09:38 -0700 | [diff] [blame] | 216 | return; |
| 217 | } |
Sanna Catherine de Treville Wager | 0a3959e | 2017-07-25 16:08:17 -0700 | [diff] [blame] | 218 | |
Sanna Catherine de Treville Wager | d0dfe43 | 2017-06-22 15:09:38 -0700 | [diff] [blame] | 219 | std::map<int, int> buckets; |
Sanna Catherine de Treville Wager | a80649a | 2017-07-21 16:16:38 -0700 | [diff] [blame] | 220 | for (const auto &shortHist: mHists) { |
Sanna Catherine de Treville Wager | d0dfe43 | 2017-06-22 15:09:38 -0700 | [diff] [blame] | 221 | for (const auto &countPair : shortHist.second) { |
| 222 | buckets[countPair.first] += countPair.second; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | // underscores and spaces length corresponds to maximum width of histogram |
| 227 | static const int kLen = 40; |
| 228 | std::string underscores(kLen, '_'); |
| 229 | std::string spaces(kLen, ' '); |
| 230 | |
| 231 | auto it = buckets.begin(); |
| 232 | int maxDelta = it->first; |
| 233 | int maxCount = it->second; |
| 234 | // Compute maximum values |
| 235 | while (++it != buckets.end()) { |
| 236 | if (it->first > maxDelta) { |
| 237 | maxDelta = it->first; |
| 238 | } |
| 239 | if (it->second > maxCount) { |
| 240 | maxCount = it->second; |
| 241 | } |
| 242 | } |
| 243 | int height = log2(maxCount) + 1; // maxCount > 0, safe to call log2 |
| 244 | const int leftPadding = widthOf(1 << height); |
| 245 | const int colWidth = std::max(std::max(widthOf(maxDelta) + 1, 3), leftPadding + 2); |
| 246 | int scalingFactor = 1; |
| 247 | // scale data if it exceeds maximum height |
| 248 | if (height > maxHeight) { |
| 249 | scalingFactor = (height + maxHeight) / maxHeight; |
| 250 | height /= scalingFactor; |
| 251 | } |
Sanna Catherine de Treville Wager | e486526 | 2017-07-14 16:24:15 -0700 | [diff] [blame] | 252 | // TODO: print reader (author) ID |
Sanna Catherine de Treville Wager | d0dfe43 | 2017-06-22 15:09:38 -0700 | [diff] [blame] | 253 | body->appendFormat("\n%*s", leftPadding + 11, "Occurrences"); |
| 254 | // write histogram label line with bucket values |
| 255 | body->appendFormat("\n%s", " "); |
| 256 | body->appendFormat("%*s", leftPadding, " "); |
| 257 | for (auto const &x : buckets) { |
| 258 | body->appendFormat("%*d", colWidth, x.second); |
| 259 | } |
| 260 | // write histogram ascii art |
| 261 | body->appendFormat("\n%s", " "); |
| 262 | for (int row = height * scalingFactor; row >= 0; row -= scalingFactor) { |
| 263 | const int value = 1 << row; |
| 264 | body->appendFormat("%.*s", leftPadding, spaces.c_str()); |
| 265 | for (auto const &x : buckets) { |
Sanna Catherine de Treville Wager | 716d2c6 | 2017-07-25 13:46:36 -0700 | [diff] [blame] | 266 | body->appendFormat("%.*s%s", colWidth - 1, |
| 267 | spaces.c_str(), x.second < value ? " " : "|"); |
Sanna Catherine de Treville Wager | d0dfe43 | 2017-06-22 15:09:38 -0700 | [diff] [blame] | 268 | } |
| 269 | body->appendFormat("\n%s", " "); |
| 270 | } |
| 271 | // print x-axis |
| 272 | const int columns = static_cast<int>(buckets.size()); |
| 273 | body->appendFormat("%*c", leftPadding, ' '); |
| 274 | body->appendFormat("%.*s", (columns + 1) * colWidth, underscores.c_str()); |
| 275 | body->appendFormat("\n%s", " "); |
| 276 | |
| 277 | // write footer with bucket labels |
| 278 | body->appendFormat("%*s", leftPadding, " "); |
| 279 | for (auto const &x : buckets) { |
| 280 | body->appendFormat("%*d", colWidth, x.first); |
| 281 | } |
| 282 | body->appendFormat("%.*s%s", colWidth, spaces.c_str(), "ms\n"); |
| 283 | |
Sanna Catherine de Treville Wager | 316f1fd | 2017-06-23 09:10:15 -0700 | [diff] [blame] | 284 | // Now report glitches |
| 285 | body->appendFormat("\ntime elapsed between glitches and glitch timestamps\n"); |
| 286 | for (const auto &outlier: mOutlierData) { |
| 287 | body->appendFormat("%lld: %lld\n", static_cast<long long>(outlier.first), |
| 288 | static_cast<long long>(outlier.second)); |
| 289 | } |
Sanna Catherine de Treville Wager | d0dfe43 | 2017-06-22 15:09:38 -0700 | [diff] [blame] | 290 | } |
| 291 | |
Sanna Catherine de Treville Wager | 8576894 | 2017-07-26 20:17:30 -0700 | [diff] [blame^] | 292 | |
Sanna Catherine de Treville Wager | f8c3428 | 2017-07-25 11:31:18 -0700 | [diff] [blame] | 293 | // TODO: decide whether to use this or whether it is overkill, and it is enough |
| 294 | // to only treat as glitches single wakeup call intervals which are too long. |
| 295 | // Ultimately, glitch detection will be directly on the audio signal. |
Sanna Catherine de Treville Wager | d0dfe43 | 2017-06-22 15:09:38 -0700 | [diff] [blame] | 296 | // Produces a log warning if the timing of recent buffer periods caused a glitch |
| 297 | // Computes sum of running window of three buffer periods |
| 298 | // Checks whether the buffer periods leave enough CPU time for the next one |
| 299 | // e.g. if a buffer period is expected to be 4 ms and a buffer requires 3 ms of CPU time, |
| 300 | // here are some glitch cases: |
| 301 | // 4 + 4 + 6 ; 5 + 4 + 5; 2 + 2 + 10 |
Sanna Catherine de Treville Wager | d0dfe43 | 2017-06-22 15:09:38 -0700 | [diff] [blame] | 302 | void PerformanceAnalysis::alertIfGlitch(const std::vector<int64_t> &samples) { |
| 303 | std::deque<int> periods(kNumBuff, kPeriodMs); |
| 304 | for (size_t i = 2; i < samples.size(); ++i) { // skip first time entry |
| 305 | periods.push_front(deltaMs(samples[i - 1], samples[i])); |
| 306 | periods.pop_back(); |
| 307 | // TODO: check that all glitch cases are covered |
| 308 | if (std::accumulate(periods.begin(), periods.end(), 0) > kNumBuff * kPeriodMs + |
| 309 | kPeriodMs - kPeriodMsCPU) { |
Sanna Catherine de Treville Wager | d0dfe43 | 2017-06-22 15:09:38 -0700 | [diff] [blame] | 310 | periods.assign(kNumBuff, kPeriodMs); |
| 311 | } |
| 312 | } |
| 313 | return; |
| 314 | } |
| 315 | |
Sanna Catherine de Treville Wager | cf6c75a | 2017-07-21 17:05:25 -0700 | [diff] [blame] | 316 | //------------------------------------------------------------------------------ |
| 317 | |
| 318 | // writes summary of performance into specified file descriptor |
Sanna Catherine de Treville Wager | 23f89d3 | 2017-07-24 18:24:48 -0700 | [diff] [blame] | 319 | void dump(int fd, int indent, PerformanceAnalysisMap &threadPerformanceAnalysis) { |
Sanna Catherine de Treville Wager | cf6c75a | 2017-07-21 17:05:25 -0700 | [diff] [blame] | 320 | String8 body; |
Sanna Catherine de Treville Wager | f8c3428 | 2017-07-25 11:31:18 -0700 | [diff] [blame] | 321 | const char* const kDirectory = "/data/misc/audioserver/"; |
Sanna Catherine de Treville Wager | cf6c75a | 2017-07-21 17:05:25 -0700 | [diff] [blame] | 322 | for (auto & thread : threadPerformanceAnalysis) { |
Sanna Catherine de Treville Wager | d096517 | 2017-07-24 13:42:44 -0700 | [diff] [blame] | 323 | for (auto & hash: thread.second) { |
Sanna Catherine de Treville Wager | 23f89d3 | 2017-07-24 18:24:48 -0700 | [diff] [blame] | 324 | PerformanceAnalysis& curr = hash.second; |
Sanna Catherine de Treville Wager | 23f89d3 | 2017-07-24 18:24:48 -0700 | [diff] [blame] | 325 | // write performance data to console |
| 326 | curr.reportPerformance(&body); |
Sanna Catherine de Treville Wager | 0a3959e | 2017-07-25 16:08:17 -0700 | [diff] [blame] | 327 | if (!body.isEmpty()) { |
| 328 | dumpLine(fd, indent, body); |
| 329 | body.clear(); |
| 330 | } |
Sanna Catherine de Treville Wager | 23f89d3 | 2017-07-24 18:24:48 -0700 | [diff] [blame] | 331 | // write to file |
Sanna Catherine de Treville Wager | f8c3428 | 2017-07-25 11:31:18 -0700 | [diff] [blame] | 332 | writeToFile(curr.mHists, curr.mOutlierData, curr.mPeakTimestamps, |
| 333 | kDirectory, false, thread.first, hash.first); |
Sanna Catherine de Treville Wager | d096517 | 2017-07-24 13:42:44 -0700 | [diff] [blame] | 334 | } |
Sanna Catherine de Treville Wager | cf6c75a | 2017-07-21 17:05:25 -0700 | [diff] [blame] | 335 | } |
Sanna Catherine de Treville Wager | cf6c75a | 2017-07-21 17:05:25 -0700 | [diff] [blame] | 336 | } |
| 337 | |
Sanna Catherine de Treville Wager | 8576894 | 2017-07-26 20:17:30 -0700 | [diff] [blame^] | 338 | |
Sanna Catherine de Treville Wager | cf6c75a | 2017-07-21 17:05:25 -0700 | [diff] [blame] | 339 | // Writes a string into specified file descriptor |
| 340 | void dumpLine(int fd, int indent, const String8 &body) { |
| 341 | dprintf(fd, "%.*s%s \n", indent, "", body.string()); |
| 342 | } |
| 343 | |
Sanna Catherine de Treville Wager | 8044808 | 2017-07-11 14:07:59 -0700 | [diff] [blame] | 344 | } // namespace ReportPerformance |
| 345 | |
Sanna Catherine de Treville Wager | d0dfe43 | 2017-06-22 15:09:38 -0700 | [diff] [blame] | 346 | } // namespace android |