| Glenn Kasten | 04333cd | 2015-02-17 16:23:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 "FastMixerDumpState" |
| 18 | //#define LOG_NDEBUG 0 |
| 19 | |
| 20 | #include "Configuration.h" |
| Glenn Kasten | 214b406 | 2015-03-02 14:15:47 -0800 | [diff] [blame] | 21 | #ifdef FAST_THREAD_STATISTICS |
| Eric Tan | 5b13ff8 | 2018-07-27 11:20:17 -0700 | [diff] [blame] | 22 | #include <audio_utils/Statistics.h> |
| Glenn Kasten | 04333cd | 2015-02-17 16:23:03 -0800 | [diff] [blame] | 23 | #ifdef CPU_FREQUENCY_STATISTICS |
| 24 | #include <cpustats/ThreadCpuUsage.h> |
| 25 | #endif |
| 26 | #endif |
| Glenn Kasten | 04333cd | 2015-02-17 16:23:03 -0800 | [diff] [blame] | 27 | #include <utils/Log.h> |
| 28 | #include "FastMixerDumpState.h" |
| 29 | |
| 30 | namespace android { |
| 31 | |
| Glenn Kasten | 04333cd | 2015-02-17 16:23:03 -0800 | [diff] [blame] | 32 | // helper function called by qsort() |
| 33 | static int compare_uint32_t(const void *pa, const void *pb) |
| 34 | { |
| Andy Hung | f0859f3 | 2023-05-25 16:28:04 -0700 | [diff] [blame] | 35 | const uint32_t a = *(const uint32_t *)pa; |
| 36 | const uint32_t b = *(const uint32_t *)pb; |
| Glenn Kasten | 04333cd | 2015-02-17 16:23:03 -0800 | [diff] [blame] | 37 | if (a < b) { |
| 38 | return -1; |
| 39 | } else if (a > b) { |
| 40 | return 1; |
| 41 | } else { |
| 42 | return 0; |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | void FastMixerDumpState::dump(int fd) const |
| 47 | { |
| 48 | if (mCommand == FastMixerState::INITIAL) { |
| 49 | dprintf(fd, " FastMixer not initialized\n"); |
| 50 | return; |
| 51 | } |
| Andy Hung | f0859f3 | 2023-05-25 16:28:04 -0700 | [diff] [blame] | 52 | const double measuredWarmupMs = (mMeasuredWarmupTs.tv_sec * 1000.0) + |
| Glenn Kasten | 04333cd | 2015-02-17 16:23:03 -0800 | [diff] [blame] | 53 | (mMeasuredWarmupTs.tv_nsec / 1000000.0); |
| Andy Hung | f0859f3 | 2023-05-25 16:28:04 -0700 | [diff] [blame] | 54 | const double mixPeriodSec = (double) mFrameCount / mSampleRate; |
| Glenn Kasten | 04333cd | 2015-02-17 16:23:03 -0800 | [diff] [blame] | 55 | dprintf(fd, " FastMixer command=%s writeSequence=%u framesWritten=%u\n" |
| 56 | " numTracks=%u writeErrors=%u underruns=%u overruns=%u\n" |
| 57 | " sampleRate=%u frameCount=%zu measuredWarmup=%.3g ms, warmupCycles=%u\n" |
| Andy Hung | f6ab58d | 2018-05-25 12:50:39 -0700 | [diff] [blame] | 58 | " mixPeriod=%.2f ms latency=%.2f ms\n", |
| Glenn Kasten | bcb1486 | 2015-03-05 17:11:21 -0800 | [diff] [blame] | 59 | FastMixerState::commandToString(mCommand), mWriteSequence, mFramesWritten, |
| 60 | mNumTracks, mWriteErrors, mUnderruns, mOverruns, |
| 61 | mSampleRate, mFrameCount, measuredWarmupMs, mWarmupCycles, |
| Andy Hung | f6ab58d | 2018-05-25 12:50:39 -0700 | [diff] [blame] | 62 | mixPeriodSec * 1e3, mLatencyMs); |
| Andy Hung | 2e2c0bb | 2018-06-11 19:13:11 -0700 | [diff] [blame] | 63 | dprintf(fd, " FastMixer Timestamp stats: %s\n", mTimestampVerifier.toString().c_str()); |
| Glenn Kasten | 214b406 | 2015-03-02 14:15:47 -0800 | [diff] [blame] | 64 | #ifdef FAST_THREAD_STATISTICS |
| Glenn Kasten | 04333cd | 2015-02-17 16:23:03 -0800 | [diff] [blame] | 65 | // find the interval of valid samples |
| Eric Tan | 7b65115 | 2018-07-13 10:17:19 -0700 | [diff] [blame] | 66 | const uint32_t bounds = mBounds; |
| 67 | const uint32_t newestOpen = bounds & 0xFFFF; |
| Glenn Kasten | 04333cd | 2015-02-17 16:23:03 -0800 | [diff] [blame] | 68 | uint32_t oldestClosed = bounds >> 16; |
| Ivan Lozano | e71027e | 2017-12-04 16:09:10 -0800 | [diff] [blame] | 69 | |
| 70 | //uint32_t n = (newestOpen - oldestClosed) & 0xFFFF; |
| 71 | uint32_t n; |
| 72 | __builtin_sub_overflow(newestOpen, oldestClosed, &n); |
| Eric Tan | 7b65115 | 2018-07-13 10:17:19 -0700 | [diff] [blame] | 73 | n &= 0xFFFF; |
| Ivan Lozano | e71027e | 2017-12-04 16:09:10 -0800 | [diff] [blame] | 74 | |
| Glenn Kasten | 04333cd | 2015-02-17 16:23:03 -0800 | [diff] [blame] | 75 | if (n > mSamplingN) { |
| 76 | ALOGE("too many samples %u", n); |
| 77 | n = mSamplingN; |
| 78 | } |
| 79 | // statistics for monotonic (wall clock) time, thread raw CPU load in time, CPU clock frequency, |
| 80 | // and adjusted CPU load in MHz normalized for CPU clock frequency |
| Andy Hung | 16698b8 | 2018-08-01 10:48:38 -0700 | [diff] [blame] | 81 | audio_utils::Statistics<double> wall, loadNs; |
| Glenn Kasten | 04333cd | 2015-02-17 16:23:03 -0800 | [diff] [blame] | 82 | #ifdef CPU_FREQUENCY_STATISTICS |
| Andy Hung | 16698b8 | 2018-08-01 10:48:38 -0700 | [diff] [blame] | 83 | audio_utils::Statistics<double> kHz, loadMHz; |
| Glenn Kasten | 04333cd | 2015-02-17 16:23:03 -0800 | [diff] [blame] | 84 | uint32_t previousCpukHz = 0; |
| 85 | #endif |
| 86 | // Assuming a normal distribution for cycle times, three standard deviations on either side of |
| 87 | // the mean account for 99.73% of the population. So if we take each tail to be 1/1000 of the |
| 88 | // sample set, we get 99.8% combined, or close to three standard deviations. |
| 89 | static const uint32_t kTailDenominator = 1000; |
| Andy Hung | f0859f3 | 2023-05-25 16:28:04 -0700 | [diff] [blame] | 90 | uint32_t *tail = n >= kTailDenominator ? new uint32_t[n] : nullptr; |
| Glenn Kasten | 04333cd | 2015-02-17 16:23:03 -0800 | [diff] [blame] | 91 | // loop over all the samples |
| 92 | for (uint32_t j = 0; j < n; ++j) { |
| Andy Hung | f0859f3 | 2023-05-25 16:28:04 -0700 | [diff] [blame] | 93 | const size_t i = oldestClosed++ & (mSamplingN - 1); |
| 94 | const uint32_t wallNs = mMonotonicNs[i]; |
| 95 | if (tail != nullptr) { |
| Glenn Kasten | 04333cd | 2015-02-17 16:23:03 -0800 | [diff] [blame] | 96 | tail[j] = wallNs; |
| 97 | } |
| Eric Tan | 5b13ff8 | 2018-07-27 11:20:17 -0700 | [diff] [blame] | 98 | wall.add(wallNs); |
| Andy Hung | f0859f3 | 2023-05-25 16:28:04 -0700 | [diff] [blame] | 99 | const uint32_t sampleLoadNs = mLoadNs[i]; |
| Eric Tan | 5b13ff8 | 2018-07-27 11:20:17 -0700 | [diff] [blame] | 100 | loadNs.add(sampleLoadNs); |
| Glenn Kasten | 04333cd | 2015-02-17 16:23:03 -0800 | [diff] [blame] | 101 | #ifdef CPU_FREQUENCY_STATISTICS |
| 102 | uint32_t sampleCpukHz = mCpukHz[i]; |
| 103 | // skip bad kHz samples |
| 104 | if ((sampleCpukHz & ~0xF) != 0) { |
| Eric Tan | 5b13ff8 | 2018-07-27 11:20:17 -0700 | [diff] [blame] | 105 | kHz.add(sampleCpukHz >> 4); |
| Glenn Kasten | 04333cd | 2015-02-17 16:23:03 -0800 | [diff] [blame] | 106 | if (sampleCpukHz == previousCpukHz) { |
| 107 | double megacycles = (double) sampleLoadNs * (double) (sampleCpukHz >> 4) * 1e-12; |
| 108 | double adjMHz = megacycles / mixPeriodSec; // _not_ wallNs * 1e9 |
| Eric Tan | 5b13ff8 | 2018-07-27 11:20:17 -0700 | [diff] [blame] | 109 | loadMHz.add(adjMHz); |
| Glenn Kasten | 04333cd | 2015-02-17 16:23:03 -0800 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | previousCpukHz = sampleCpukHz; |
| 113 | #endif |
| 114 | } |
| 115 | if (n) { |
| 116 | dprintf(fd, " Simple moving statistics over last %.1f seconds:\n", |
| Eric Tan | 5b13ff8 | 2018-07-27 11:20:17 -0700 | [diff] [blame] | 117 | wall.getN() * mixPeriodSec); |
| Glenn Kasten | 04333cd | 2015-02-17 16:23:03 -0800 | [diff] [blame] | 118 | dprintf(fd, " wall clock time in ms per mix cycle:\n" |
| 119 | " mean=%.2f min=%.2f max=%.2f stddev=%.2f\n", |
| Eric Tan | 5b13ff8 | 2018-07-27 11:20:17 -0700 | [diff] [blame] | 120 | wall.getMean()*1e-6, wall.getMin()*1e-6, wall.getMax()*1e-6, |
| 121 | wall.getStdDev()*1e-6); |
| Glenn Kasten | 04333cd | 2015-02-17 16:23:03 -0800 | [diff] [blame] | 122 | dprintf(fd, " raw CPU load in us per mix cycle:\n" |
| 123 | " mean=%.0f min=%.0f max=%.0f stddev=%.0f\n", |
| Eric Tan | 5b13ff8 | 2018-07-27 11:20:17 -0700 | [diff] [blame] | 124 | loadNs.getMean()*1e-3, loadNs.getMin()*1e-3, loadNs.getMax()*1e-3, |
| 125 | loadNs.getStdDev()*1e-3); |
| Glenn Kasten | 04333cd | 2015-02-17 16:23:03 -0800 | [diff] [blame] | 126 | } else { |
| 127 | dprintf(fd, " No FastMixer statistics available currently\n"); |
| 128 | } |
| 129 | #ifdef CPU_FREQUENCY_STATISTICS |
| 130 | dprintf(fd, " CPU clock frequency in MHz:\n" |
| 131 | " mean=%.0f min=%.0f max=%.0f stddev=%.0f\n", |
| Eric Tan | 5b13ff8 | 2018-07-27 11:20:17 -0700 | [diff] [blame] | 132 | kHz.getMean()*1e-3, kHz.getMin()*1e-3, kHz.getMax()*1e-3, kHz.getStdDev()*1e-3); |
| Glenn Kasten | 04333cd | 2015-02-17 16:23:03 -0800 | [diff] [blame] | 133 | dprintf(fd, " adjusted CPU load in MHz (i.e. normalized for CPU clock frequency):\n" |
| 134 | " mean=%.1f min=%.1f max=%.1f stddev=%.1f\n", |
| Eric Tan | 5b13ff8 | 2018-07-27 11:20:17 -0700 | [diff] [blame] | 135 | loadMHz.getMean(), loadMHz.getMin(), loadMHz.getMax(), loadMHz.getStdDev()); |
| Glenn Kasten | 04333cd | 2015-02-17 16:23:03 -0800 | [diff] [blame] | 136 | #endif |
| Andy Hung | f0859f3 | 2023-05-25 16:28:04 -0700 | [diff] [blame] | 137 | if (tail != nullptr) { |
| Glenn Kasten | 04333cd | 2015-02-17 16:23:03 -0800 | [diff] [blame] | 138 | qsort(tail, n, sizeof(uint32_t), compare_uint32_t); |
| 139 | // assume same number of tail samples on each side, left and right |
| Andy Hung | f0859f3 | 2023-05-25 16:28:04 -0700 | [diff] [blame] | 140 | const uint32_t count = n / kTailDenominator; |
| Andy Hung | 16698b8 | 2018-08-01 10:48:38 -0700 | [diff] [blame] | 141 | audio_utils::Statistics<double> left, right; |
| Glenn Kasten | 04333cd | 2015-02-17 16:23:03 -0800 | [diff] [blame] | 142 | for (uint32_t i = 0; i < count; ++i) { |
| Eric Tan | 5b13ff8 | 2018-07-27 11:20:17 -0700 | [diff] [blame] | 143 | left.add(tail[i]); |
| 144 | right.add(tail[n - (i + 1)]); |
| Glenn Kasten | 04333cd | 2015-02-17 16:23:03 -0800 | [diff] [blame] | 145 | } |
| 146 | dprintf(fd, " Distribution of mix cycle times in ms for the tails " |
| 147 | "(> ~3 stddev outliers):\n" |
| 148 | " left tail: mean=%.2f min=%.2f max=%.2f stddev=%.2f\n" |
| 149 | " right tail: mean=%.2f min=%.2f max=%.2f stddev=%.2f\n", |
| Eric Tan | 5b13ff8 | 2018-07-27 11:20:17 -0700 | [diff] [blame] | 150 | left.getMean()*1e-6, left.getMin()*1e-6, left.getMax()*1e-6, left.getStdDev()*1e-6, |
| 151 | right.getMean()*1e-6, right.getMin()*1e-6, right.getMax()*1e-6, |
| 152 | right.getStdDev()*1e-6); |
| Glenn Kasten | 04333cd | 2015-02-17 16:23:03 -0800 | [diff] [blame] | 153 | delete[] tail; |
| 154 | } |
| 155 | #endif |
| 156 | // The active track mask and track states are updated non-atomically. |
| 157 | // So if we relied on isActive to decide whether to display, |
| 158 | // then we might display an obsolete track or omit an active track. |
| 159 | // Instead we always display all tracks, with an indication |
| 160 | // of whether we think the track is active. |
| 161 | uint32_t trackMask = mTrackMask; |
| Glenn Kasten | dc2c50b | 2016-04-21 08:13:14 -0700 | [diff] [blame] | 162 | dprintf(fd, " Fast tracks: sMaxFastTracks=%u activeMask=%#x\n", |
| 163 | FastMixerState::sMaxFastTracks, trackMask); |
| Andy Hung | b54c854 | 2016-09-21 12:55:15 -0700 | [diff] [blame] | 164 | dprintf(fd, " Index Active Full Partial Empty Recent Ready Written\n"); |
| Glenn Kasten | dc2c50b | 2016-04-21 08:13:14 -0700 | [diff] [blame] | 165 | for (uint32_t i = 0; i < FastMixerState::sMaxFastTracks; ++i, trackMask >>= 1) { |
| Andy Hung | f0859f3 | 2023-05-25 16:28:04 -0700 | [diff] [blame] | 166 | const bool isActive = trackMask & 1; |
| Glenn Kasten | 04333cd | 2015-02-17 16:23:03 -0800 | [diff] [blame] | 167 | const FastTrackDump *ftDump = &mTracks[i]; |
| 168 | const FastTrackUnderruns& underruns = ftDump->mUnderruns; |
| 169 | const char *mostRecent; |
| 170 | switch (underruns.mBitFields.mMostRecent) { |
| 171 | case UNDERRUN_FULL: |
| 172 | mostRecent = "full"; |
| 173 | break; |
| 174 | case UNDERRUN_PARTIAL: |
| 175 | mostRecent = "partial"; |
| 176 | break; |
| 177 | case UNDERRUN_EMPTY: |
| 178 | mostRecent = "empty"; |
| 179 | break; |
| 180 | default: |
| 181 | mostRecent = "?"; |
| 182 | break; |
| 183 | } |
| Andy Hung | b54c854 | 2016-09-21 12:55:15 -0700 | [diff] [blame] | 184 | dprintf(fd, " %5u %6s %4u %7u %5u %7s %5zu %10lld\n", |
| 185 | i, isActive ? "yes" : "no", |
| Glenn Kasten | 04333cd | 2015-02-17 16:23:03 -0800 | [diff] [blame] | 186 | (underruns.mBitFields.mFull) & UNDERRUN_MASK, |
| 187 | (underruns.mBitFields.mPartial) & UNDERRUN_MASK, |
| 188 | (underruns.mBitFields.mEmpty) & UNDERRUN_MASK, |
| Andy Hung | b54c854 | 2016-09-21 12:55:15 -0700 | [diff] [blame] | 189 | mostRecent, ftDump->mFramesReady, |
| 190 | (long long)ftDump->mFramesWritten); |
| Glenn Kasten | 04333cd | 2015-02-17 16:23:03 -0800 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | |
| Andy Hung | 71ba4b3 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 194 | } // namespace android |