blob: 1ae191cea1cc535945604bcc4d62137e2fef2c5a [file] [log] [blame]
Glenn Kasten22340022014-04-07 12:04:41 -07001/*
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#ifndef ANDROID_AUDIO_FAST_MIXER_DUMP_STATE_H
18#define ANDROID_AUDIO_FAST_MIXER_DUMP_STATE_H
19
20#include "Configuration.h"
Glenn Kasten045ee7e2015-02-17 16:22:04 -080021#include "FastThreadDumpState.h"
Glenn Kasten22340022014-04-07 12:04:41 -070022
23namespace android {
24
25// Describes the underrun status for a single "pull" attempt
26enum FastTrackUnderrunStatus {
27 UNDERRUN_FULL, // framesReady() is full frame count, no underrun
28 UNDERRUN_PARTIAL, // framesReady() is non-zero but < full frame count, partial underrun
29 UNDERRUN_EMPTY, // framesReady() is zero, total underrun
30};
31
32// Underrun counters are not reset to zero for new tracks or if track generation changes.
33// This packed representation is used to keep the information atomic.
34union FastTrackUnderruns {
35 FastTrackUnderruns() { mAtomic = 0;
36 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(sizeof(FastTrackUnderruns) == sizeof(uint32_t)); }
37 FastTrackUnderruns(const FastTrackUnderruns& copyFrom) : mAtomic(copyFrom.mAtomic) { }
38 FastTrackUnderruns& operator=(const FastTrackUnderruns& rhs)
39 { if (this != &rhs) mAtomic = rhs.mAtomic; return *this; }
40 struct {
41#define UNDERRUN_BITS 10
42#define UNDERRUN_MASK ((1 << UNDERRUN_BITS) - 1)
43 uint32_t mFull : UNDERRUN_BITS; // framesReady() is full frame count
44 uint32_t mPartial : UNDERRUN_BITS; // framesReady() is non-zero but < full frame count
45 uint32_t mEmpty : UNDERRUN_BITS; // framesReady() is zero
46 FastTrackUnderrunStatus mMostRecent : 2; // status of most recent framesReady()
47 } mBitFields;
48private:
49 uint32_t mAtomic;
50};
51
52// Represents the dump state of a fast track
53struct FastTrackDump {
54 FastTrackDump() : mFramesReady(0) { }
55 /*virtual*/ ~FastTrackDump() { }
56 FastTrackUnderruns mUnderruns;
57 size_t mFramesReady; // most recent value only; no long-term statistics kept
58};
59
60// The FastMixerDumpState keeps a cache of FastMixer statistics that can be logged by dumpsys.
61// Each individual native word-sized field is accessed atomically. But the
62// overall structure is non-atomic, that is there may be an inconsistency between fields.
63// No barriers or locks are used for either writing or reading.
64// Only POD types are permitted, and the contents shouldn't be trusted (i.e. do range checks).
65// It has a different lifetime than the FastMixer, and so it can't be a member of FastMixer.
66struct FastMixerDumpState : FastThreadDumpState {
67 FastMixerDumpState(
68#ifdef FAST_MIXER_STATISTICS
69 uint32_t samplingN = kSamplingNforLowRamDevice
70#endif
71 );
72 /*virtual*/ ~FastMixerDumpState();
73
74 void dump(int fd) const; // should only be called on a stable copy, not the original
75
76 uint32_t mWriteSequence; // incremented before and after each write()
77 uint32_t mFramesWritten; // total number of frames written successfully
78 uint32_t mNumTracks; // total number of active fast tracks
79 uint32_t mWriteErrors; // total number of write() errors
80 uint32_t mSampleRate;
81 size_t mFrameCount;
82 uint32_t mTrackMask; // mask of active tracks
83 FastTrackDump mTracks[FastMixerState::kMaxFastTracks];
84
85#ifdef FAST_MIXER_STATISTICS
86 // Compile-time constant for a "low RAM device", must be a power of 2 <= kSamplingN.
87 // This value was chosen such that each array uses 1 small page (4 Kbytes).
88 static const uint32_t kSamplingNforLowRamDevice = 0x400;
89 // Increase sampling window after construction, must be a power of 2 <= kSamplingN
90 void increaseSamplingN(uint32_t samplingN);
91#endif
92};
93
94} // android
95
96#endif // ANDROID_AUDIO_FAST_MIXER_DUMP_STATE_H