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