Glenn Kasten | 97b5d0d | 2012-03-23 18:54:19 -0700 | [diff] [blame] | 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 | |
Elliott Hughes | ee49929 | 2014-05-21 17:55:51 -0700 | [diff] [blame^] | 20 | #include <linux/futex.h> |
| 21 | #include <sys/syscall.h> |
Glenn Kasten | 09474df | 2012-05-10 14:48:07 -0700 | [diff] [blame] | 22 | #include <utils/Debug.h> |
Glenn Kasten | 97b5d0d | 2012-03-23 18:54:19 -0700 | [diff] [blame] | 23 | #include <utils/Thread.h> |
Glenn Kasten | 97b5d0d | 2012-03-23 18:54:19 -0700 | [diff] [blame] | 24 | #include "StateQueue.h" |
| 25 | #include "FastMixerState.h" |
| 26 | |
| 27 | namespace android { |
| 28 | |
| 29 | typedef StateQueue<FastMixerState> FastMixerStateQueue; |
| 30 | |
| 31 | class FastMixer : public Thread { |
| 32 | |
| 33 | public: |
| 34 | FastMixer() : Thread(false /*canCallJava*/) { } |
| 35 | virtual ~FastMixer() { } |
| 36 | |
| 37 | FastMixerStateQueue* sq() { return &mSQ; } |
| 38 | |
| 39 | private: |
| 40 | virtual bool threadLoop(); |
| 41 | FastMixerStateQueue mSQ; |
| 42 | |
| 43 | }; // class FastMixer |
| 44 | |
Glenn Kasten | 09474df | 2012-05-10 14:48:07 -0700 | [diff] [blame] | 45 | // Describes the underrun status for a single "pull" attempt |
| 46 | enum FastTrackUnderrunStatus { |
| 47 | UNDERRUN_FULL, // framesReady() is full frame count, no underrun |
| 48 | UNDERRUN_PARTIAL, // framesReady() is non-zero but < full frame count, partial underrun |
| 49 | UNDERRUN_EMPTY, // framesReady() is zero, total underrun |
| 50 | }; |
| 51 | |
| 52 | // Underrun counters are not reset to zero for new tracks or if track generation changes. |
| 53 | // This packed representation is used to keep the information atomic. |
| 54 | union FastTrackUnderruns { |
| 55 | FastTrackUnderruns() { mAtomic = 0; |
| 56 | COMPILE_TIME_ASSERT_FUNCTION_SCOPE(sizeof(FastTrackUnderruns) == sizeof(uint32_t)); } |
| 57 | FastTrackUnderruns(const FastTrackUnderruns& copyFrom) : mAtomic(copyFrom.mAtomic) { } |
| 58 | FastTrackUnderruns& operator=(const FastTrackUnderruns& rhs) |
| 59 | { if (this != &rhs) mAtomic = rhs.mAtomic; return *this; } |
| 60 | struct { |
| 61 | #define UNDERRUN_BITS 10 |
| 62 | #define UNDERRUN_MASK ((1 << UNDERRUN_BITS) - 1) |
| 63 | uint32_t mFull : UNDERRUN_BITS; // framesReady() is full frame count |
| 64 | uint32_t mPartial : UNDERRUN_BITS; // framesReady() is non-zero but < full frame count |
| 65 | uint32_t mEmpty : UNDERRUN_BITS; // framesReady() is zero |
| 66 | FastTrackUnderrunStatus mMostRecent : 2; // status of most recent framesReady() |
| 67 | } mBitFields; |
| 68 | private: |
| 69 | uint32_t mAtomic; |
| 70 | }; |
| 71 | |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame] | 72 | // Represents the dump state of a fast track |
| 73 | struct FastTrackDump { |
Glenn Kasten | 1295bb4d | 2012-05-31 07:43:43 -0700 | [diff] [blame] | 74 | FastTrackDump() : mFramesReady(0) { } |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame] | 75 | /*virtual*/ ~FastTrackDump() { } |
Glenn Kasten | 09474df | 2012-05-10 14:48:07 -0700 | [diff] [blame] | 76 | FastTrackUnderruns mUnderruns; |
Glenn Kasten | 1295bb4d | 2012-05-31 07:43:43 -0700 | [diff] [blame] | 77 | size_t mFramesReady; // most recent value only; no long-term statistics kept |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame] | 78 | }; |
| 79 | |
Glenn Kasten | 97b5d0d | 2012-03-23 18:54:19 -0700 | [diff] [blame] | 80 | // The FastMixerDumpState keeps a cache of FastMixer statistics that can be logged by dumpsys. |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame] | 81 | // Each individual native word-sized field is accessed atomically. But the |
| 82 | // overall structure is non-atomic, that is there may be an inconsistency between fields. |
| 83 | // No barriers or locks are used for either writing or reading. |
| 84 | // Only POD types are permitted, and the contents shouldn't be trusted (i.e. do range checks). |
Glenn Kasten | 97b5d0d | 2012-03-23 18:54:19 -0700 | [diff] [blame] | 85 | // It has a different lifetime than the FastMixer, and so it can't be a member of FastMixer. |
| 86 | struct FastMixerDumpState { |
Glenn Kasten | 4182c4e | 2013-07-15 14:45:07 -0700 | [diff] [blame] | 87 | FastMixerDumpState( |
| 88 | #ifdef FAST_MIXER_STATISTICS |
| 89 | uint32_t samplingN = kSamplingNforLowRamDevice |
| 90 | #endif |
| 91 | ); |
Glenn Kasten | 97b5d0d | 2012-03-23 18:54:19 -0700 | [diff] [blame] | 92 | /*virtual*/ ~FastMixerDumpState(); |
| 93 | |
Glenn Kasten | 4182c4e | 2013-07-15 14:45:07 -0700 | [diff] [blame] | 94 | void dump(int fd) const; // should only be called on a stable copy, not the original |
Glenn Kasten | 97b5d0d | 2012-03-23 18:54:19 -0700 | [diff] [blame] | 95 | |
| 96 | FastMixerState::Command mCommand; // current command |
| 97 | uint32_t mWriteSequence; // incremented before and after each write() |
| 98 | uint32_t mFramesWritten; // total number of frames written successfully |
| 99 | uint32_t mNumTracks; // total number of active fast tracks |
| 100 | uint32_t mWriteErrors; // total number of write() errors |
| 101 | uint32_t mUnderruns; // total number of underruns |
| 102 | uint32_t mOverruns; // total number of overruns |
Glenn Kasten | 21e8c50 | 2012-04-12 09:39:42 -0700 | [diff] [blame] | 103 | uint32_t mSampleRate; |
| 104 | size_t mFrameCount; |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame] | 105 | struct timespec mMeasuredWarmupTs; // measured warmup time |
| 106 | uint32_t mWarmupCycles; // number of loop cycles required to warmup |
Glenn Kasten | 1295bb4d | 2012-05-31 07:43:43 -0700 | [diff] [blame] | 107 | uint32_t mTrackMask; // mask of active tracks |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame] | 108 | FastTrackDump mTracks[FastMixerState::kMaxFastTracks]; |
Glenn Kasten | 42d45cf | 2012-05-02 10:34:47 -0700 | [diff] [blame] | 109 | |
Glenn Kasten | 97b5d0d | 2012-03-23 18:54:19 -0700 | [diff] [blame] | 110 | #ifdef FAST_MIXER_STATISTICS |
Glenn Kasten | 42d45cf | 2012-05-02 10:34:47 -0700 | [diff] [blame] | 111 | // Recently collected samples of per-cycle monotonic time, thread CPU time, and CPU frequency. |
Glenn Kasten | 4182c4e | 2013-07-15 14:45:07 -0700 | [diff] [blame] | 112 | // kSamplingN is max size of sampling frame (statistics), and must be a power of 2 <= 0x8000. |
| 113 | // The sample arrays are virtually allocated based on this compile-time constant, |
| 114 | // but are only initialized and used based on the runtime parameter mSamplingN. |
Glenn Kasten | 0d35f78 | 2013-03-11 10:58:50 -0700 | [diff] [blame] | 115 | static const uint32_t kSamplingN = 0x8000; |
Glenn Kasten | 4182c4e | 2013-07-15 14:45:07 -0700 | [diff] [blame] | 116 | // Compile-time constant for a "low RAM device", must be a power of 2 <= kSamplingN. |
| 117 | // This value was chosen such that each array uses 1 small page (4 Kbytes). |
| 118 | static const uint32_t kSamplingNforLowRamDevice = 0x400; |
| 119 | // Corresponding runtime maximum size of sample arrays, must be a power of 2 <= kSamplingN. |
| 120 | uint32_t mSamplingN; |
Glenn Kasten | 42d45cf | 2012-05-02 10:34:47 -0700 | [diff] [blame] | 121 | // The bounds define the interval of valid samples, and are represented as follows: |
| 122 | // newest open (excluded) endpoint = lower 16 bits of bounds, modulo N |
| 123 | // oldest closed (included) endpoint = upper 16 bits of bounds, modulo N |
| 124 | // Number of valid samples is newest - oldest. |
| 125 | uint32_t mBounds; // bounds for mMonotonicNs, mThreadCpuNs, and mCpukHz |
| 126 | // The elements in the *Ns arrays are in units of nanoseconds <= 3999999999. |
| 127 | uint32_t mMonotonicNs[kSamplingN]; // delta monotonic (wall clock) time |
| 128 | uint32_t mLoadNs[kSamplingN]; // delta CPU load in time |
Glenn Kasten | 0a14c4c | 2012-06-13 14:58:49 -0700 | [diff] [blame] | 129 | #ifdef CPU_FREQUENCY_STATISTICS |
Glenn Kasten | 42d45cf | 2012-05-02 10:34:47 -0700 | [diff] [blame] | 130 | uint32_t mCpukHz[kSamplingN]; // absolute CPU clock frequency in kHz, bits 0-3 are CPU# |
Glenn Kasten | 97b5d0d | 2012-03-23 18:54:19 -0700 | [diff] [blame] | 131 | #endif |
Glenn Kasten | 4182c4e | 2013-07-15 14:45:07 -0700 | [diff] [blame] | 132 | // Increase sampling window after construction, must be a power of 2 <= kSamplingN |
| 133 | void increaseSamplingN(uint32_t samplingN); |
Glenn Kasten | 0a14c4c | 2012-06-13 14:58:49 -0700 | [diff] [blame] | 134 | #endif |
Glenn Kasten | 97b5d0d | 2012-03-23 18:54:19 -0700 | [diff] [blame] | 135 | }; |
| 136 | |
| 137 | } // namespace android |
| 138 | |
| 139 | #endif // ANDROID_AUDIO_FAST_MIXER_H |