blob: 87eaacb69109677352af418309dbc51703891534 [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
Glenn Kasten04333cd2015-02-17 16:23:03 -080022#include <cpustats/CentralTendencyStatistics.h>
23#ifdef CPU_FREQUENCY_STATISTICS
24#include <cpustats/ThreadCpuUsage.h>
25#endif
26#endif
27#include <utils/Debug.h>
28#include <utils/Log.h>
29#include "FastMixerDumpState.h"
30
31namespace android {
32
Glenn Kastenfbdb2ac2015-03-02 14:47:19 -080033FastMixerDumpState::FastMixerDumpState() : FastThreadDumpState(),
Glenn Kasten04333cd2015-02-17 16:23:03 -080034 mWriteSequence(0), mFramesWritten(0),
35 mNumTracks(0), mWriteErrors(0),
36 mSampleRate(0), mFrameCount(0),
37 mTrackMask(0)
38{
Glenn Kasten04333cd2015-02-17 16:23:03 -080039}
40
Glenn Kasten04333cd2015-02-17 16:23:03 -080041FastMixerDumpState::~FastMixerDumpState()
42{
43}
44
45// helper function called by qsort()
46static int compare_uint32_t(const void *pa, const void *pb)
47{
48 uint32_t a = *(const uint32_t *)pa;
49 uint32_t b = *(const uint32_t *)pb;
50 if (a < b) {
51 return -1;
52 } else if (a > b) {
53 return 1;
54 } else {
55 return 0;
56 }
57}
58
59void FastMixerDumpState::dump(int fd) const
60{
61 if (mCommand == FastMixerState::INITIAL) {
62 dprintf(fd, " FastMixer not initialized\n");
63 return;
64 }
65#define COMMAND_MAX 32
66 char string[COMMAND_MAX];
67 switch (mCommand) {
68 case FastMixerState::INITIAL:
69 strcpy(string, "INITIAL");
70 break;
71 case FastMixerState::HOT_IDLE:
72 strcpy(string, "HOT_IDLE");
73 break;
74 case FastMixerState::COLD_IDLE:
75 strcpy(string, "COLD_IDLE");
76 break;
77 case FastMixerState::EXIT:
78 strcpy(string, "EXIT");
79 break;
80 case FastMixerState::MIX:
81 strcpy(string, "MIX");
82 break;
83 case FastMixerState::WRITE:
84 strcpy(string, "WRITE");
85 break;
86 case FastMixerState::MIX_WRITE:
87 strcpy(string, "MIX_WRITE");
88 break;
89 default:
90 snprintf(string, COMMAND_MAX, "%d", mCommand);
91 break;
92 }
93 double measuredWarmupMs = (mMeasuredWarmupTs.tv_sec * 1000.0) +
94 (mMeasuredWarmupTs.tv_nsec / 1000000.0);
95 double mixPeriodSec = (double) mFrameCount / (double) mSampleRate;
96 dprintf(fd, " FastMixer command=%s writeSequence=%u framesWritten=%u\n"
97 " numTracks=%u writeErrors=%u underruns=%u overruns=%u\n"
98 " sampleRate=%u frameCount=%zu measuredWarmup=%.3g ms, warmupCycles=%u\n"
99 " mixPeriod=%.2f ms\n",
100 string, mWriteSequence, mFramesWritten,
101 mNumTracks, mWriteErrors, mUnderruns, mOverruns,
102 mSampleRate, mFrameCount, measuredWarmupMs, mWarmupCycles,
103 mixPeriodSec * 1e3);
Glenn Kasten214b4062015-03-02 14:15:47 -0800104#ifdef FAST_THREAD_STATISTICS
Glenn Kasten04333cd2015-02-17 16:23:03 -0800105 // find the interval of valid samples
106 uint32_t bounds = mBounds;
107 uint32_t newestOpen = bounds & 0xFFFF;
108 uint32_t oldestClosed = bounds >> 16;
109 uint32_t n = (newestOpen - oldestClosed) & 0xFFFF;
110 if (n > mSamplingN) {
111 ALOGE("too many samples %u", n);
112 n = mSamplingN;
113 }
114 // statistics for monotonic (wall clock) time, thread raw CPU load in time, CPU clock frequency,
115 // and adjusted CPU load in MHz normalized for CPU clock frequency
116 CentralTendencyStatistics wall, loadNs;
117#ifdef CPU_FREQUENCY_STATISTICS
118 CentralTendencyStatistics kHz, loadMHz;
119 uint32_t previousCpukHz = 0;
120#endif
121 // Assuming a normal distribution for cycle times, three standard deviations on either side of
122 // the mean account for 99.73% of the population. So if we take each tail to be 1/1000 of the
123 // sample set, we get 99.8% combined, or close to three standard deviations.
124 static const uint32_t kTailDenominator = 1000;
125 uint32_t *tail = n >= kTailDenominator ? new uint32_t[n] : NULL;
126 // loop over all the samples
127 for (uint32_t j = 0; j < n; ++j) {
128 size_t i = oldestClosed++ & (mSamplingN - 1);
129 uint32_t wallNs = mMonotonicNs[i];
130 if (tail != NULL) {
131 tail[j] = wallNs;
132 }
133 wall.sample(wallNs);
134 uint32_t sampleLoadNs = mLoadNs[i];
135 loadNs.sample(sampleLoadNs);
136#ifdef CPU_FREQUENCY_STATISTICS
137 uint32_t sampleCpukHz = mCpukHz[i];
138 // skip bad kHz samples
139 if ((sampleCpukHz & ~0xF) != 0) {
140 kHz.sample(sampleCpukHz >> 4);
141 if (sampleCpukHz == previousCpukHz) {
142 double megacycles = (double) sampleLoadNs * (double) (sampleCpukHz >> 4) * 1e-12;
143 double adjMHz = megacycles / mixPeriodSec; // _not_ wallNs * 1e9
144 loadMHz.sample(adjMHz);
145 }
146 }
147 previousCpukHz = sampleCpukHz;
148#endif
149 }
150 if (n) {
151 dprintf(fd, " Simple moving statistics over last %.1f seconds:\n",
152 wall.n() * mixPeriodSec);
153 dprintf(fd, " wall clock time in ms per mix cycle:\n"
154 " mean=%.2f min=%.2f max=%.2f stddev=%.2f\n",
155 wall.mean()*1e-6, wall.minimum()*1e-6, wall.maximum()*1e-6,
156 wall.stddev()*1e-6);
157 dprintf(fd, " raw CPU load in us per mix cycle:\n"
158 " mean=%.0f min=%.0f max=%.0f stddev=%.0f\n",
159 loadNs.mean()*1e-3, loadNs.minimum()*1e-3, loadNs.maximum()*1e-3,
160 loadNs.stddev()*1e-3);
161 } else {
162 dprintf(fd, " No FastMixer statistics available currently\n");
163 }
164#ifdef CPU_FREQUENCY_STATISTICS
165 dprintf(fd, " CPU clock frequency in MHz:\n"
166 " mean=%.0f min=%.0f max=%.0f stddev=%.0f\n",
167 kHz.mean()*1e-3, kHz.minimum()*1e-3, kHz.maximum()*1e-3, kHz.stddev()*1e-3);
168 dprintf(fd, " adjusted CPU load in MHz (i.e. normalized for CPU clock frequency):\n"
169 " mean=%.1f min=%.1f max=%.1f stddev=%.1f\n",
170 loadMHz.mean(), loadMHz.minimum(), loadMHz.maximum(), loadMHz.stddev());
171#endif
172 if (tail != NULL) {
173 qsort(tail, n, sizeof(uint32_t), compare_uint32_t);
174 // assume same number of tail samples on each side, left and right
175 uint32_t count = n / kTailDenominator;
176 CentralTendencyStatistics left, right;
177 for (uint32_t i = 0; i < count; ++i) {
178 left.sample(tail[i]);
179 right.sample(tail[n - (i + 1)]);
180 }
181 dprintf(fd, " Distribution of mix cycle times in ms for the tails "
182 "(> ~3 stddev outliers):\n"
183 " left tail: mean=%.2f min=%.2f max=%.2f stddev=%.2f\n"
184 " right tail: mean=%.2f min=%.2f max=%.2f stddev=%.2f\n",
185 left.mean()*1e-6, left.minimum()*1e-6, left.maximum()*1e-6, left.stddev()*1e-6,
186 right.mean()*1e-6, right.minimum()*1e-6, right.maximum()*1e-6,
187 right.stddev()*1e-6);
188 delete[] tail;
189 }
190#endif
191 // The active track mask and track states are updated non-atomically.
192 // So if we relied on isActive to decide whether to display,
193 // then we might display an obsolete track or omit an active track.
194 // Instead we always display all tracks, with an indication
195 // of whether we think the track is active.
196 uint32_t trackMask = mTrackMask;
197 dprintf(fd, " Fast tracks: kMaxFastTracks=%u activeMask=%#x\n",
198 FastMixerState::kMaxFastTracks, trackMask);
199 dprintf(fd, " Index Active Full Partial Empty Recent Ready\n");
200 for (uint32_t i = 0; i < FastMixerState::kMaxFastTracks; ++i, trackMask >>= 1) {
201 bool isActive = trackMask & 1;
202 const FastTrackDump *ftDump = &mTracks[i];
203 const FastTrackUnderruns& underruns = ftDump->mUnderruns;
204 const char *mostRecent;
205 switch (underruns.mBitFields.mMostRecent) {
206 case UNDERRUN_FULL:
207 mostRecent = "full";
208 break;
209 case UNDERRUN_PARTIAL:
210 mostRecent = "partial";
211 break;
212 case UNDERRUN_EMPTY:
213 mostRecent = "empty";
214 break;
215 default:
216 mostRecent = "?";
217 break;
218 }
219 dprintf(fd, " %5u %6s %4u %7u %5u %7s %5zu\n", i, isActive ? "yes" : "no",
220 (underruns.mBitFields.mFull) & UNDERRUN_MASK,
221 (underruns.mBitFields.mPartial) & UNDERRUN_MASK,
222 (underruns.mBitFields.mEmpty) & UNDERRUN_MASK,
223 mostRecent, ftDump->mFramesReady);
224 }
225}
226
227} // android