blob: 2abfbfb122f124302aa69b13ef821138a305fccc [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
Eric Tan1882f162018-08-02 18:05:39 -070027#include <json/json.h>
Eric Tan7b651152018-07-13 10:17:19 -070028#include <string>
Glenn Kasten04333cd2015-02-17 16:23:03 -080029#include <utils/Debug.h>
30#include <utils/Log.h>
31#include "FastMixerDumpState.h"
32
33namespace android {
34
Glenn Kastenfbdb2ac2015-03-02 14:47:19 -080035FastMixerDumpState::FastMixerDumpState() : FastThreadDumpState(),
Glenn Kasten04333cd2015-02-17 16:23:03 -080036 mWriteSequence(0), mFramesWritten(0),
37 mNumTracks(0), mWriteErrors(0),
38 mSampleRate(0), mFrameCount(0),
39 mTrackMask(0)
40{
Glenn Kasten04333cd2015-02-17 16:23:03 -080041}
42
Glenn Kasten04333cd2015-02-17 16:23:03 -080043FastMixerDumpState::~FastMixerDumpState()
44{
45}
46
47// helper function called by qsort()
48static int compare_uint32_t(const void *pa, const void *pb)
49{
50 uint32_t a = *(const uint32_t *)pa;
51 uint32_t b = *(const uint32_t *)pb;
52 if (a < b) {
53 return -1;
54 } else if (a > b) {
55 return 1;
56 } else {
57 return 0;
58 }
59}
60
61void FastMixerDumpState::dump(int fd) const
62{
63 if (mCommand == FastMixerState::INITIAL) {
64 dprintf(fd, " FastMixer not initialized\n");
65 return;
66 }
Glenn Kasten04333cd2015-02-17 16:23:03 -080067 double measuredWarmupMs = (mMeasuredWarmupTs.tv_sec * 1000.0) +
68 (mMeasuredWarmupTs.tv_nsec / 1000000.0);
Glenn Kasten17c9c992015-03-02 15:53:01 -080069 double mixPeriodSec = (double) mFrameCount / mSampleRate;
Glenn Kasten04333cd2015-02-17 16:23:03 -080070 dprintf(fd, " FastMixer command=%s writeSequence=%u framesWritten=%u\n"
71 " numTracks=%u writeErrors=%u underruns=%u overruns=%u\n"
72 " sampleRate=%u frameCount=%zu measuredWarmup=%.3g ms, warmupCycles=%u\n"
Andy Hungf6ab58d2018-05-25 12:50:39 -070073 " mixPeriod=%.2f ms latency=%.2f ms\n",
Glenn Kastenbcb14862015-03-05 17:11:21 -080074 FastMixerState::commandToString(mCommand), mWriteSequence, mFramesWritten,
75 mNumTracks, mWriteErrors, mUnderruns, mOverruns,
76 mSampleRate, mFrameCount, measuredWarmupMs, mWarmupCycles,
Andy Hungf6ab58d2018-05-25 12:50:39 -070077 mixPeriodSec * 1e3, mLatencyMs);
Andy Hung2e2c0bb2018-06-11 19:13:11 -070078 dprintf(fd, " FastMixer Timestamp stats: %s\n", mTimestampVerifier.toString().c_str());
Glenn Kasten214b4062015-03-02 14:15:47 -080079#ifdef FAST_THREAD_STATISTICS
Glenn Kasten04333cd2015-02-17 16:23:03 -080080 // find the interval of valid samples
Eric Tan7b651152018-07-13 10:17:19 -070081 const uint32_t bounds = mBounds;
82 const uint32_t newestOpen = bounds & 0xFFFF;
Glenn Kasten04333cd2015-02-17 16:23:03 -080083 uint32_t oldestClosed = bounds >> 16;
Ivan Lozanoe71027e2017-12-04 16:09:10 -080084
85 //uint32_t n = (newestOpen - oldestClosed) & 0xFFFF;
86 uint32_t n;
87 __builtin_sub_overflow(newestOpen, oldestClosed, &n);
Eric Tan7b651152018-07-13 10:17:19 -070088 n &= 0xFFFF;
Ivan Lozanoe71027e2017-12-04 16:09:10 -080089
Glenn Kasten04333cd2015-02-17 16:23:03 -080090 if (n > mSamplingN) {
91 ALOGE("too many samples %u", n);
92 n = mSamplingN;
93 }
94 // statistics for monotonic (wall clock) time, thread raw CPU load in time, CPU clock frequency,
95 // and adjusted CPU load in MHz normalized for CPU clock frequency
Andy Hung16698b82018-08-01 10:48:38 -070096 audio_utils::Statistics<double> wall, loadNs;
Glenn Kasten04333cd2015-02-17 16:23:03 -080097#ifdef CPU_FREQUENCY_STATISTICS
Andy Hung16698b82018-08-01 10:48:38 -070098 audio_utils::Statistics<double> kHz, loadMHz;
Glenn Kasten04333cd2015-02-17 16:23:03 -080099 uint32_t previousCpukHz = 0;
100#endif
101 // Assuming a normal distribution for cycle times, three standard deviations on either side of
102 // the mean account for 99.73% of the population. So if we take each tail to be 1/1000 of the
103 // sample set, we get 99.8% combined, or close to three standard deviations.
104 static const uint32_t kTailDenominator = 1000;
105 uint32_t *tail = n >= kTailDenominator ? new uint32_t[n] : NULL;
106 // loop over all the samples
107 for (uint32_t j = 0; j < n; ++j) {
108 size_t i = oldestClosed++ & (mSamplingN - 1);
109 uint32_t wallNs = mMonotonicNs[i];
110 if (tail != NULL) {
111 tail[j] = wallNs;
112 }
Eric Tan5b13ff82018-07-27 11:20:17 -0700113 wall.add(wallNs);
Glenn Kasten04333cd2015-02-17 16:23:03 -0800114 uint32_t sampleLoadNs = mLoadNs[i];
Eric Tan5b13ff82018-07-27 11:20:17 -0700115 loadNs.add(sampleLoadNs);
Glenn Kasten04333cd2015-02-17 16:23:03 -0800116#ifdef CPU_FREQUENCY_STATISTICS
117 uint32_t sampleCpukHz = mCpukHz[i];
118 // skip bad kHz samples
119 if ((sampleCpukHz & ~0xF) != 0) {
Eric Tan5b13ff82018-07-27 11:20:17 -0700120 kHz.add(sampleCpukHz >> 4);
Glenn Kasten04333cd2015-02-17 16:23:03 -0800121 if (sampleCpukHz == previousCpukHz) {
122 double megacycles = (double) sampleLoadNs * (double) (sampleCpukHz >> 4) * 1e-12;
123 double adjMHz = megacycles / mixPeriodSec; // _not_ wallNs * 1e9
Eric Tan5b13ff82018-07-27 11:20:17 -0700124 loadMHz.add(adjMHz);
Glenn Kasten04333cd2015-02-17 16:23:03 -0800125 }
126 }
127 previousCpukHz = sampleCpukHz;
128#endif
129 }
130 if (n) {
131 dprintf(fd, " Simple moving statistics over last %.1f seconds:\n",
Eric Tan5b13ff82018-07-27 11:20:17 -0700132 wall.getN() * mixPeriodSec);
Glenn Kasten04333cd2015-02-17 16:23:03 -0800133 dprintf(fd, " wall clock time in ms per mix cycle:\n"
134 " mean=%.2f min=%.2f max=%.2f stddev=%.2f\n",
Eric Tan5b13ff82018-07-27 11:20:17 -0700135 wall.getMean()*1e-6, wall.getMin()*1e-6, wall.getMax()*1e-6,
136 wall.getStdDev()*1e-6);
Glenn Kasten04333cd2015-02-17 16:23:03 -0800137 dprintf(fd, " raw CPU load in us per mix cycle:\n"
138 " mean=%.0f min=%.0f max=%.0f stddev=%.0f\n",
Eric Tan5b13ff82018-07-27 11:20:17 -0700139 loadNs.getMean()*1e-3, loadNs.getMin()*1e-3, loadNs.getMax()*1e-3,
140 loadNs.getStdDev()*1e-3);
Glenn Kasten04333cd2015-02-17 16:23:03 -0800141 } else {
142 dprintf(fd, " No FastMixer statistics available currently\n");
143 }
144#ifdef CPU_FREQUENCY_STATISTICS
145 dprintf(fd, " CPU clock frequency in MHz:\n"
146 " mean=%.0f min=%.0f max=%.0f stddev=%.0f\n",
Eric Tan5b13ff82018-07-27 11:20:17 -0700147 kHz.getMean()*1e-3, kHz.getMin()*1e-3, kHz.getMax()*1e-3, kHz.getStdDev()*1e-3);
Glenn Kasten04333cd2015-02-17 16:23:03 -0800148 dprintf(fd, " adjusted CPU load in MHz (i.e. normalized for CPU clock frequency):\n"
149 " mean=%.1f min=%.1f max=%.1f stddev=%.1f\n",
Eric Tan5b13ff82018-07-27 11:20:17 -0700150 loadMHz.getMean(), loadMHz.getMin(), loadMHz.getMax(), loadMHz.getStdDev());
Glenn Kasten04333cd2015-02-17 16:23:03 -0800151#endif
152 if (tail != NULL) {
153 qsort(tail, n, sizeof(uint32_t), compare_uint32_t);
154 // assume same number of tail samples on each side, left and right
155 uint32_t count = n / kTailDenominator;
Andy Hung16698b82018-08-01 10:48:38 -0700156 audio_utils::Statistics<double> left, right;
Glenn Kasten04333cd2015-02-17 16:23:03 -0800157 for (uint32_t i = 0; i < count; ++i) {
Eric Tan5b13ff82018-07-27 11:20:17 -0700158 left.add(tail[i]);
159 right.add(tail[n - (i + 1)]);
Glenn Kasten04333cd2015-02-17 16:23:03 -0800160 }
161 dprintf(fd, " Distribution of mix cycle times in ms for the tails "
162 "(> ~3 stddev outliers):\n"
163 " left tail: mean=%.2f min=%.2f max=%.2f stddev=%.2f\n"
164 " right tail: mean=%.2f min=%.2f max=%.2f stddev=%.2f\n",
Eric Tan5b13ff82018-07-27 11:20:17 -0700165 left.getMean()*1e-6, left.getMin()*1e-6, left.getMax()*1e-6, left.getStdDev()*1e-6,
166 right.getMean()*1e-6, right.getMin()*1e-6, right.getMax()*1e-6,
167 right.getStdDev()*1e-6);
Glenn Kasten04333cd2015-02-17 16:23:03 -0800168 delete[] tail;
169 }
170#endif
171 // The active track mask and track states are updated non-atomically.
172 // So if we relied on isActive to decide whether to display,
173 // then we might display an obsolete track or omit an active track.
174 // Instead we always display all tracks, with an indication
175 // of whether we think the track is active.
176 uint32_t trackMask = mTrackMask;
Glenn Kastendc2c50b2016-04-21 08:13:14 -0700177 dprintf(fd, " Fast tracks: sMaxFastTracks=%u activeMask=%#x\n",
178 FastMixerState::sMaxFastTracks, trackMask);
Andy Hungb54c8542016-09-21 12:55:15 -0700179 dprintf(fd, " Index Active Full Partial Empty Recent Ready Written\n");
Glenn Kastendc2c50b2016-04-21 08:13:14 -0700180 for (uint32_t i = 0; i < FastMixerState::sMaxFastTracks; ++i, trackMask >>= 1) {
Glenn Kasten04333cd2015-02-17 16:23:03 -0800181 bool isActive = trackMask & 1;
182 const FastTrackDump *ftDump = &mTracks[i];
183 const FastTrackUnderruns& underruns = ftDump->mUnderruns;
184 const char *mostRecent;
185 switch (underruns.mBitFields.mMostRecent) {
186 case UNDERRUN_FULL:
187 mostRecent = "full";
188 break;
189 case UNDERRUN_PARTIAL:
190 mostRecent = "partial";
191 break;
192 case UNDERRUN_EMPTY:
193 mostRecent = "empty";
194 break;
195 default:
196 mostRecent = "?";
197 break;
198 }
Andy Hungb54c8542016-09-21 12:55:15 -0700199 dprintf(fd, " %5u %6s %4u %7u %5u %7s %5zu %10lld\n",
200 i, isActive ? "yes" : "no",
Glenn Kasten04333cd2015-02-17 16:23:03 -0800201 (underruns.mBitFields.mFull) & UNDERRUN_MASK,
202 (underruns.mBitFields.mPartial) & UNDERRUN_MASK,
203 (underruns.mBitFields.mEmpty) & UNDERRUN_MASK,
Andy Hungb54c8542016-09-21 12:55:15 -0700204 mostRecent, ftDump->mFramesReady,
205 (long long)ftDump->mFramesWritten);
Glenn Kasten04333cd2015-02-17 16:23:03 -0800206 }
207}
208
Eric Tan1882f162018-08-02 18:05:39 -0700209Json::Value FastMixerDumpState::getJsonDump() const
Eric Tan7b651152018-07-13 10:17:19 -0700210{
Eric Tan1882f162018-08-02 18:05:39 -0700211 Json::Value root(Json::objectValue);
Eric Tan7b651152018-07-13 10:17:19 -0700212 if (mCommand == FastMixerState::INITIAL) {
Eric Tan1882f162018-08-02 18:05:39 -0700213 root["status"] = "uninitialized";
214 return root;
Eric Tan7b651152018-07-13 10:17:19 -0700215 }
Eric Tan7b651152018-07-13 10:17:19 -0700216#ifdef FAST_THREAD_STATISTICS
217 // find the interval of valid samples
218 const uint32_t bounds = mBounds;
219 const uint32_t newestOpen = bounds & 0xFFFF;
220 uint32_t oldestClosed = bounds >> 16;
221
222 //uint32_t n = (newestOpen - oldestClosed) & 0xFFFF;
223 uint32_t n;
224 __builtin_sub_overflow(newestOpen, oldestClosed, &n);
225 n &= 0xFFFF;
226
227 if (n > mSamplingN) {
228 ALOGE("too many samples %u", n);
229 n = mSamplingN;
230 }
231 // statistics for monotonic (wall clock) time, thread raw CPU load in time, CPU clock frequency,
232 // and adjusted CPU load in MHz normalized for CPU clock frequency
Eric Tan1882f162018-08-02 18:05:39 -0700233 Json::Value jsonWall(Json::arrayValue);
234 Json::Value jsonLoadNs(Json::arrayValue);
Eric Tan7b651152018-07-13 10:17:19 -0700235 // loop over all the samples
236 for (uint32_t j = 0; j < n; ++j) {
237 size_t i = oldestClosed++ & (mSamplingN - 1);
238 uint32_t wallNs = mMonotonicNs[i];
Eric Tan1882f162018-08-02 18:05:39 -0700239 jsonWall.append(wallNs);
Eric Tan7b651152018-07-13 10:17:19 -0700240 uint32_t sampleLoadNs = mLoadNs[i];
Eric Tan1882f162018-08-02 18:05:39 -0700241 jsonLoadNs.append(sampleLoadNs);
Eric Tan7b651152018-07-13 10:17:19 -0700242 }
Eric Tan7b651152018-07-13 10:17:19 -0700243 if (n) {
Eric Tan1882f162018-08-02 18:05:39 -0700244 root["wall_clock_time_ns"] = jsonWall;
245 root["raw_cpu_load_ns"] = jsonLoadNs;
246 root["status"] = "ok";
Eric Tan7b651152018-07-13 10:17:19 -0700247 } else {
Eric Tan1882f162018-08-02 18:05:39 -0700248 root["status"] = "unavailable";
Eric Tan7b651152018-07-13 10:17:19 -0700249 }
250#endif
Eric Tan1882f162018-08-02 18:05:39 -0700251 return root;
Eric Tan7b651152018-07-13 10:17:19 -0700252}
253
Glenn Kasten04333cd2015-02-17 16:23:03 -0800254} // android