blob: 4f79dd62bb76b4c62b5329ca9ff5a91f4e502fd1 [file] [log] [blame]
Glenn Kasten04333cd2015-02-17 16:23:03 -08001/*
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 Kasten214b4062015-03-02 14:15:47 -080021#ifdef FAST_THREAD_STATISTICS
Eric Tan5b13ff82018-07-27 11:20:17 -070022#include <audio_utils/Statistics.h>
Glenn Kasten04333cd2015-02-17 16:23:03 -080023#ifdef CPU_FREQUENCY_STATISTICS
24#include <cpustats/ThreadCpuUsage.h>
25#endif
26#endif
Glenn Kasten04333cd2015-02-17 16:23:03 -080027#include <utils/Log.h>
28#include "FastMixerDumpState.h"
29
30namespace android {
31
Glenn Kasten04333cd2015-02-17 16:23:03 -080032// helper function called by qsort()
33static int compare_uint32_t(const void *pa, const void *pb)
34{
Andy Hungf0859f32023-05-25 16:28:04 -070035 const uint32_t a = *(const uint32_t *)pa;
36 const uint32_t b = *(const uint32_t *)pb;
Glenn Kasten04333cd2015-02-17 16:23:03 -080037 if (a < b) {
38 return -1;
39 } else if (a > b) {
40 return 1;
41 } else {
42 return 0;
43 }
44}
45
46void FastMixerDumpState::dump(int fd) const
47{
48 if (mCommand == FastMixerState::INITIAL) {
49 dprintf(fd, " FastMixer not initialized\n");
50 return;
51 }
Andy Hungf0859f32023-05-25 16:28:04 -070052 const double measuredWarmupMs = (mMeasuredWarmupTs.tv_sec * 1000.0) +
Glenn Kasten04333cd2015-02-17 16:23:03 -080053 (mMeasuredWarmupTs.tv_nsec / 1000000.0);
Andy Hungf0859f32023-05-25 16:28:04 -070054 const double mixPeriodSec = (double) mFrameCount / mSampleRate;
Glenn Kasten04333cd2015-02-17 16:23:03 -080055 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 Hungf6ab58d2018-05-25 12:50:39 -070058 " mixPeriod=%.2f ms latency=%.2f ms\n",
Glenn Kastenbcb14862015-03-05 17:11:21 -080059 FastMixerState::commandToString(mCommand), mWriteSequence, mFramesWritten,
60 mNumTracks, mWriteErrors, mUnderruns, mOverruns,
61 mSampleRate, mFrameCount, measuredWarmupMs, mWarmupCycles,
Andy Hungf6ab58d2018-05-25 12:50:39 -070062 mixPeriodSec * 1e3, mLatencyMs);
Andy Hung2e2c0bb2018-06-11 19:13:11 -070063 dprintf(fd, " FastMixer Timestamp stats: %s\n", mTimestampVerifier.toString().c_str());
Glenn Kasten214b4062015-03-02 14:15:47 -080064#ifdef FAST_THREAD_STATISTICS
Glenn Kasten04333cd2015-02-17 16:23:03 -080065 // find the interval of valid samples
Eric Tan7b651152018-07-13 10:17:19 -070066 const uint32_t bounds = mBounds;
67 const uint32_t newestOpen = bounds & 0xFFFF;
Glenn Kasten04333cd2015-02-17 16:23:03 -080068 uint32_t oldestClosed = bounds >> 16;
Ivan Lozanoe71027e2017-12-04 16:09:10 -080069
70 //uint32_t n = (newestOpen - oldestClosed) & 0xFFFF;
71 uint32_t n;
72 __builtin_sub_overflow(newestOpen, oldestClosed, &n);
Eric Tan7b651152018-07-13 10:17:19 -070073 n &= 0xFFFF;
Ivan Lozanoe71027e2017-12-04 16:09:10 -080074
Glenn Kasten04333cd2015-02-17 16:23:03 -080075 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 Hung16698b82018-08-01 10:48:38 -070081 audio_utils::Statistics<double> wall, loadNs;
Glenn Kasten04333cd2015-02-17 16:23:03 -080082#ifdef CPU_FREQUENCY_STATISTICS
Andy Hung16698b82018-08-01 10:48:38 -070083 audio_utils::Statistics<double> kHz, loadMHz;
Glenn Kasten04333cd2015-02-17 16:23:03 -080084 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 Hungf0859f32023-05-25 16:28:04 -070090 uint32_t *tail = n >= kTailDenominator ? new uint32_t[n] : nullptr;
Glenn Kasten04333cd2015-02-17 16:23:03 -080091 // loop over all the samples
92 for (uint32_t j = 0; j < n; ++j) {
Andy Hungf0859f32023-05-25 16:28:04 -070093 const size_t i = oldestClosed++ & (mSamplingN - 1);
94 const uint32_t wallNs = mMonotonicNs[i];
95 if (tail != nullptr) {
Glenn Kasten04333cd2015-02-17 16:23:03 -080096 tail[j] = wallNs;
97 }
Eric Tan5b13ff82018-07-27 11:20:17 -070098 wall.add(wallNs);
Andy Hungf0859f32023-05-25 16:28:04 -070099 const uint32_t sampleLoadNs = mLoadNs[i];
Eric Tan5b13ff82018-07-27 11:20:17 -0700100 loadNs.add(sampleLoadNs);
Glenn Kasten04333cd2015-02-17 16:23:03 -0800101#ifdef CPU_FREQUENCY_STATISTICS
102 uint32_t sampleCpukHz = mCpukHz[i];
103 // skip bad kHz samples
104 if ((sampleCpukHz & ~0xF) != 0) {
Eric Tan5b13ff82018-07-27 11:20:17 -0700105 kHz.add(sampleCpukHz >> 4);
Glenn Kasten04333cd2015-02-17 16:23:03 -0800106 if (sampleCpukHz == previousCpukHz) {
107 double megacycles = (double) sampleLoadNs * (double) (sampleCpukHz >> 4) * 1e-12;
108 double adjMHz = megacycles / mixPeriodSec; // _not_ wallNs * 1e9
Eric Tan5b13ff82018-07-27 11:20:17 -0700109 loadMHz.add(adjMHz);
Glenn Kasten04333cd2015-02-17 16:23:03 -0800110 }
111 }
112 previousCpukHz = sampleCpukHz;
113#endif
114 }
115 if (n) {
116 dprintf(fd, " Simple moving statistics over last %.1f seconds:\n",
Eric Tan5b13ff82018-07-27 11:20:17 -0700117 wall.getN() * mixPeriodSec);
Glenn Kasten04333cd2015-02-17 16:23:03 -0800118 dprintf(fd, " wall clock time in ms per mix cycle:\n"
119 " mean=%.2f min=%.2f max=%.2f stddev=%.2f\n",
Eric Tan5b13ff82018-07-27 11:20:17 -0700120 wall.getMean()*1e-6, wall.getMin()*1e-6, wall.getMax()*1e-6,
121 wall.getStdDev()*1e-6);
Glenn Kasten04333cd2015-02-17 16:23:03 -0800122 dprintf(fd, " raw CPU load in us per mix cycle:\n"
123 " mean=%.0f min=%.0f max=%.0f stddev=%.0f\n",
Eric Tan5b13ff82018-07-27 11:20:17 -0700124 loadNs.getMean()*1e-3, loadNs.getMin()*1e-3, loadNs.getMax()*1e-3,
125 loadNs.getStdDev()*1e-3);
Glenn Kasten04333cd2015-02-17 16:23:03 -0800126 } 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 Tan5b13ff82018-07-27 11:20:17 -0700132 kHz.getMean()*1e-3, kHz.getMin()*1e-3, kHz.getMax()*1e-3, kHz.getStdDev()*1e-3);
Glenn Kasten04333cd2015-02-17 16:23:03 -0800133 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 Tan5b13ff82018-07-27 11:20:17 -0700135 loadMHz.getMean(), loadMHz.getMin(), loadMHz.getMax(), loadMHz.getStdDev());
Glenn Kasten04333cd2015-02-17 16:23:03 -0800136#endif
Andy Hungf0859f32023-05-25 16:28:04 -0700137 if (tail != nullptr) {
Glenn Kasten04333cd2015-02-17 16:23:03 -0800138 qsort(tail, n, sizeof(uint32_t), compare_uint32_t);
139 // assume same number of tail samples on each side, left and right
Andy Hungf0859f32023-05-25 16:28:04 -0700140 const uint32_t count = n / kTailDenominator;
Andy Hung16698b82018-08-01 10:48:38 -0700141 audio_utils::Statistics<double> left, right;
Glenn Kasten04333cd2015-02-17 16:23:03 -0800142 for (uint32_t i = 0; i < count; ++i) {
Eric Tan5b13ff82018-07-27 11:20:17 -0700143 left.add(tail[i]);
144 right.add(tail[n - (i + 1)]);
Glenn Kasten04333cd2015-02-17 16:23:03 -0800145 }
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 Tan5b13ff82018-07-27 11:20:17 -0700150 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 Kasten04333cd2015-02-17 16:23:03 -0800153 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 Kastendc2c50b2016-04-21 08:13:14 -0700162 dprintf(fd, " Fast tracks: sMaxFastTracks=%u activeMask=%#x\n",
163 FastMixerState::sMaxFastTracks, trackMask);
Andy Hungb54c8542016-09-21 12:55:15 -0700164 dprintf(fd, " Index Active Full Partial Empty Recent Ready Written\n");
Glenn Kastendc2c50b2016-04-21 08:13:14 -0700165 for (uint32_t i = 0; i < FastMixerState::sMaxFastTracks; ++i, trackMask >>= 1) {
Andy Hungf0859f32023-05-25 16:28:04 -0700166 const bool isActive = trackMask & 1;
Glenn Kasten04333cd2015-02-17 16:23:03 -0800167 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 Hungb54c8542016-09-21 12:55:15 -0700184 dprintf(fd, " %5u %6s %4u %7u %5u %7s %5zu %10lld\n",
185 i, isActive ? "yes" : "no",
Glenn Kasten04333cd2015-02-17 16:23:03 -0800186 (underruns.mBitFields.mFull) & UNDERRUN_MASK,
187 (underruns.mBitFields.mPartial) & UNDERRUN_MASK,
188 (underruns.mBitFields.mEmpty) & UNDERRUN_MASK,
Andy Hungb54c8542016-09-21 12:55:15 -0700189 mostRecent, ftDump->mFramesReady,
190 (long long)ftDump->mFramesWritten);
Glenn Kasten04333cd2015-02-17 16:23:03 -0800191 }
192}
193
Andy Hung71ba4b32022-10-06 12:09:49 -0700194} // namespace android