FastMixer.h revision 42d45cfd0c3d62357a6549c62f535e4d4fe08d91
1/*
2 * Copyright (C) 2012 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_H
18#define ANDROID_AUDIO_FAST_MIXER_H
19
20#include <utils/Thread.h>
21extern "C" {
22#include "../private/bionic_futex.h"
23}
24#include "StateQueue.h"
25#include "FastMixerState.h"
26
27namespace android {
28
29typedef StateQueue<FastMixerState> FastMixerStateQueue;
30
31class FastMixer : public Thread {
32
33public:
34            FastMixer() : Thread(false /*canCallJava*/) { }
35    virtual ~FastMixer() { }
36
37            FastMixerStateQueue* sq() { return &mSQ; }
38
39private:
40    virtual bool                threadLoop();
41            FastMixerStateQueue mSQ;
42
43};  // class FastMixer
44
45// Represents the dump state of a fast track
46struct FastTrackDump {
47    FastTrackDump() : mUnderruns(0) { }
48    /*virtual*/ ~FastTrackDump() { }
49    uint32_t mUnderruns;        // Underrun status, represented as follows:
50                                //   bit 0 == 0 means not currently in underrun
51                                //   bit 0 == 1 means currently in underrun
52                                //   bits 1 to 31 == total number of underruns
53                                // Not reset to zero for new tracks or if track generation changes.
54                                // This representation is used to keep the information atomic.
55};
56
57// The FastMixerDumpState keeps a cache of FastMixer statistics that can be logged by dumpsys.
58// Each individual native word-sized field is accessed atomically.  But the
59// overall structure is non-atomic, that is there may be an inconsistency between fields.
60// No barriers or locks are used for either writing or reading.
61// Only POD types are permitted, and the contents shouldn't be trusted (i.e. do range checks).
62// It has a different lifetime than the FastMixer, and so it can't be a member of FastMixer.
63struct FastMixerDumpState {
64    FastMixerDumpState();
65    /*virtual*/ ~FastMixerDumpState();
66
67    void dump(int fd);          // should only be called on a stable copy, not the original
68
69    FastMixerState::Command mCommand;   // current command
70    uint32_t mWriteSequence;    // incremented before and after each write()
71    uint32_t mFramesWritten;    // total number of frames written successfully
72    uint32_t mNumTracks;        // total number of active fast tracks
73    uint32_t mWriteErrors;      // total number of write() errors
74    uint32_t mUnderruns;        // total number of underruns
75    uint32_t mOverruns;         // total number of overruns
76    uint32_t mSampleRate;
77    size_t   mFrameCount;
78    struct timespec mMeasuredWarmupTs;  // measured warmup time
79    uint32_t mWarmupCycles;     // number of loop cycles required to warmup
80    FastTrackDump   mTracks[FastMixerState::kMaxFastTracks];
81
82#ifdef FAST_MIXER_STATISTICS
83    // Recently collected samples of per-cycle monotonic time, thread CPU time, and CPU frequency.
84    // kSamplingN is the size of the sampling frame, and must be a power of 2 <= 0x8000.
85    static const uint32_t kSamplingN = 0x1000;
86    // The bounds define the interval of valid samples, and are represented as follows:
87    //      newest open (excluded) endpoint   = lower 16 bits of bounds, modulo N
88    //      oldest closed (included) endpoint = upper 16 bits of bounds, modulo N
89    // Number of valid samples is newest - oldest.
90    uint32_t mBounds;                   // bounds for mMonotonicNs, mThreadCpuNs, and mCpukHz
91    // The elements in the *Ns arrays are in units of nanoseconds <= 3999999999.
92    uint32_t mMonotonicNs[kSamplingN];  // delta monotonic (wall clock) time
93    uint32_t mLoadNs[kSamplingN];       // delta CPU load in time
94    uint32_t mCpukHz[kSamplingN];       // absolute CPU clock frequency in kHz, bits 0-3 are CPU#
95#endif
96};
97
98}   // namespace android
99
100#endif  // ANDROID_AUDIO_FAST_MIXER_H
101