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 | |
| 20 | #include <utils/Thread.h> |
| 21 | extern "C" { |
| 22 | #include "../private/bionic_futex.h" |
| 23 | } |
| 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 | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame] | 45 | // Represents the dump state of a fast track |
| 46 | struct 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 | |
Glenn Kasten | 97b5d0d | 2012-03-23 18:54:19 -0700 | [diff] [blame] | 57 | // 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] | 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). |
Glenn Kasten | 97b5d0d | 2012-03-23 18:54:19 -0700 | [diff] [blame] | 62 | // It has a different lifetime than the FastMixer, and so it can't be a member of FastMixer. |
| 63 | struct FastMixerDumpState { |
| 64 | FastMixerDumpState(); |
| 65 | /*virtual*/ ~FastMixerDumpState(); |
| 66 | |
Glenn Kasten | 42d45cf | 2012-05-02 10:34:47 -0700 | [diff] [blame^] | 67 | void dump(int fd); // should only be called on a stable copy, not the original |
Glenn Kasten | 97b5d0d | 2012-03-23 18:54:19 -0700 | [diff] [blame] | 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 |
Glenn Kasten | 21e8c50 | 2012-04-12 09:39:42 -0700 | [diff] [blame] | 76 | uint32_t mSampleRate; |
| 77 | size_t mFrameCount; |
Glenn Kasten | 288ed21 | 2012-04-25 17:52:27 -0700 | [diff] [blame] | 78 | struct timespec mMeasuredWarmupTs; // measured warmup time |
| 79 | uint32_t mWarmupCycles; // number of loop cycles required to warmup |
| 80 | FastTrackDump mTracks[FastMixerState::kMaxFastTracks]; |
Glenn Kasten | 42d45cf | 2012-05-02 10:34:47 -0700 | [diff] [blame^] | 81 | |
Glenn Kasten | 97b5d0d | 2012-03-23 18:54:19 -0700 | [diff] [blame] | 82 | #ifdef FAST_MIXER_STATISTICS |
Glenn Kasten | 42d45cf | 2012-05-02 10:34:47 -0700 | [diff] [blame^] | 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# |
Glenn Kasten | 97b5d0d | 2012-03-23 18:54:19 -0700 | [diff] [blame] | 95 | #endif |
| 96 | }; |
| 97 | |
| 98 | } // namespace android |
| 99 | |
| 100 | #endif // ANDROID_AUDIO_FAST_MIXER_H |