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_THREAD_STATE_H
18 #define ANDROID_AUDIO_FAST_THREAD_STATE_H
19 
20 #include "Configuration.h"
21 #include <stdint.h>
22 #include <media/nbaio/NBLog.h>
23 
24 namespace android {
25 
26 struct FastThreadDumpState;
27 
28 // Represents a single state of a FastThread
29 struct FastThreadState {
30                 FastThreadState();
31     /*virtual*/ ~FastThreadState();
32 
33     typedef uint32_t Command;
34     static const Command
35         INITIAL = 0,            // used only for the initial state
36         HOT_IDLE = 1,           // do nothing
37         COLD_IDLE = 2,          // wait for the futex
38         IDLE = 3,               // either HOT_IDLE or COLD_IDLE
39         EXIT = 4;               // exit from thread
40         // additional values defined per subclass
41     Command     mCommand;       // current command
42     int32_t*    mColdFutexAddr; // for COLD_IDLE only, pointer to the associated futex
43     unsigned    mColdGen;       // increment when COLD_IDLE is requested so it's only performed once
44 
45     // This might be a one-time configuration rather than per-state
46     FastThreadDumpState* mDumpState; // if non-NULL, then update dump state periodically
47     NBLog::Writer* mNBLogWriter; // non-blocking logger
48 
49 };  // struct FastThreadState
50 
51 
52 // FIXME extract common part of comment at FastMixerDumpState
53 struct FastThreadDumpState {
54     FastThreadDumpState();
55     /*virtual*/ ~FastThreadDumpState();
56 
57     FastThreadState::Command mCommand;   // current command
58     uint32_t mUnderruns;        // total number of underruns
59     uint32_t mOverruns;         // total number of overruns
60     struct timespec mMeasuredWarmupTs;  // measured warmup time
61     uint32_t mWarmupCycles;     // number of loop cycles required to warmup
62 
63 #ifdef FAST_MIXER_STATISTICS
64     // Recently collected samples of per-cycle monotonic time, thread CPU time, and CPU frequency.
65     // kSamplingN is max size of sampling frame (statistics), and must be a power of 2 <= 0x8000.
66     // The sample arrays are virtually allocated based on this compile-time constant,
67     // but are only initialized and used based on the runtime parameter mSamplingN.
68     static const uint32_t kSamplingN = 0x8000;
69     // Corresponding runtime maximum size of sample arrays, must be a power of 2 <= kSamplingN.
70     uint32_t mSamplingN;
71     // The bounds define the interval of valid samples, and are represented as follows:
72     //      newest open (excluded) endpoint   = lower 16 bits of bounds, modulo N
73     //      oldest closed (included) endpoint = upper 16 bits of bounds, modulo N
74     // Number of valid samples is newest - oldest.
75     uint32_t mBounds;                   // bounds for mMonotonicNs, mThreadCpuNs, and mCpukHz
76     // The elements in the *Ns arrays are in units of nanoseconds <= 3999999999.
77     uint32_t mMonotonicNs[kSamplingN];  // delta monotonic (wall clock) time
78     uint32_t mLoadNs[kSamplingN];       // delta CPU load in time
79 #ifdef CPU_FREQUENCY_STATISTICS
80     uint32_t mCpukHz[kSamplingN];       // absolute CPU clock frequency in kHz, bits 0-3 are CPU#
81 #endif
82 #endif
83 
84 };  // struct FastThreadDumpState
85 
86 }   // android
87 
88 #endif  // ANDROID_AUDIO_FAST_THREAD_STATE_H
89