| Glenn Kasten | 045ee7e | 2015-02-17 16:22:04 -0800 | [diff] [blame] | 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 | |
| Andy Hung | f0859f3 | 2023-05-25 16:28:04 -0700 | [diff] [blame] | 17 | #pragma once |
| 18 | |
| 19 | #include <type_traits> |
| Glenn Kasten | 045ee7e | 2015-02-17 16:22:04 -0800 | [diff] [blame] | 20 | |
| 21 | #include "Configuration.h" |
| 22 | #include "FastThreadState.h" |
| 23 | |
| 24 | namespace android { |
| 25 | |
| Glenn Kasten | 8a0554c | 2015-03-03 12:17:06 -0800 | [diff] [blame] | 26 | // The FastThreadDumpState keeps a cache of FastThread statistics that can be logged by dumpsys. |
| 27 | // Each individual native word-sized field is accessed atomically. But the |
| 28 | // overall structure is non-atomic, that is there may be an inconsistency between fields. |
| 29 | // No barriers or locks are used for either writing or reading. |
| 30 | // Only POD types are permitted, and the contents shouldn't be trusted (i.e. do range checks). |
| 31 | // It has a different lifetime than the FastThread, and so it can't be a member of FastThread. |
| Glenn Kasten | 045ee7e | 2015-02-17 16:22:04 -0800 | [diff] [blame] | 32 | struct FastThreadDumpState { |
| 33 | FastThreadDumpState(); |
| Glenn Kasten | 045ee7e | 2015-02-17 16:22:04 -0800 | [diff] [blame] | 34 | |
| Andy Hung | 0530237 | 2023-05-26 16:24:28 -0700 | [diff] [blame] | 35 | FastThreadState::Command mCommand = FastThreadState::INITIAL; // current command |
| 36 | uint32_t mUnderruns = 0; // total number of underruns |
| 37 | uint32_t mOverruns = 0; // total number of overruns |
| 38 | struct timespec mMeasuredWarmupTs{}; // measured warmup time |
| 39 | uint32_t mWarmupCycles = 0; // number of loop cycles required to warmup |
| Glenn Kasten | 045ee7e | 2015-02-17 16:22:04 -0800 | [diff] [blame] | 40 | |
| Glenn Kasten | 214b406 | 2015-03-02 14:15:47 -0800 | [diff] [blame] | 41 | #ifdef FAST_THREAD_STATISTICS |
| Glenn Kasten | 045ee7e | 2015-02-17 16:22:04 -0800 | [diff] [blame] | 42 | // Recently collected samples of per-cycle monotonic time, thread CPU time, and CPU frequency. |
| 43 | // kSamplingN is max size of sampling frame (statistics), and must be a power of 2 <= 0x8000. |
| 44 | // The sample arrays are virtually allocated based on this compile-time constant, |
| 45 | // but are only initialized and used based on the runtime parameter mSamplingN. |
| 46 | static const uint32_t kSamplingN = 0x8000; |
| Glenn Kasten | fbdb2ac | 2015-03-02 14:47:19 -0800 | [diff] [blame] | 47 | // Compile-time constant for a "low RAM device", must be a power of 2 <= kSamplingN. |
| 48 | // This value was chosen such that each array uses 1 small page (4 Kbytes). |
| 49 | static const uint32_t kSamplingNforLowRamDevice = 0x400; |
| Glenn Kasten | 045ee7e | 2015-02-17 16:22:04 -0800 | [diff] [blame] | 50 | // Corresponding runtime maximum size of sample arrays, must be a power of 2 <= kSamplingN. |
| Andy Hung | 0530237 | 2023-05-26 16:24:28 -0700 | [diff] [blame] | 51 | uint32_t mSamplingN = 0; |
| Glenn Kasten | 045ee7e | 2015-02-17 16:22:04 -0800 | [diff] [blame] | 52 | // The bounds define the interval of valid samples, and are represented as follows: |
| 53 | // newest open (excluded) endpoint = lower 16 bits of bounds, modulo N |
| 54 | // oldest closed (included) endpoint = upper 16 bits of bounds, modulo N |
| 55 | // Number of valid samples is newest - oldest. |
| Andy Hung | 0530237 | 2023-05-26 16:24:28 -0700 | [diff] [blame] | 56 | uint32_t mBounds = 0; // bounds for mMonotonicNs, mThreadCpuNs, and mCpukHz |
| Glenn Kasten | 045ee7e | 2015-02-17 16:22:04 -0800 | [diff] [blame] | 57 | // The elements in the *Ns arrays are in units of nanoseconds <= 3999999999. |
| 58 | uint32_t mMonotonicNs[kSamplingN]; // delta monotonic (wall clock) time |
| 59 | uint32_t mLoadNs[kSamplingN]; // delta CPU load in time |
| 60 | #ifdef CPU_FREQUENCY_STATISTICS |
| 61 | uint32_t mCpukHz[kSamplingN]; // absolute CPU clock frequency in kHz, bits 0-3 are CPU# |
| 62 | #endif |
| Glenn Kasten | fbdb2ac | 2015-03-02 14:47:19 -0800 | [diff] [blame] | 63 | |
| 64 | // Increase sampling window after construction, must be a power of 2 <= kSamplingN |
| 65 | void increaseSamplingN(uint32_t samplingN); |
| Glenn Kasten | 045ee7e | 2015-02-17 16:22:04 -0800 | [diff] [blame] | 66 | #endif |
| 67 | |
| 68 | }; // struct FastThreadDumpState |
| 69 | |
| Andy Hung | f0859f3 | 2023-05-25 16:28:04 -0700 | [diff] [blame] | 70 | // No virtuals. |
| 71 | static_assert(!std::is_polymorphic_v<FastThreadDumpState>); |
| Glenn Kasten | 045ee7e | 2015-02-17 16:22:04 -0800 | [diff] [blame] | 72 | |
| Andy Hung | f0859f3 | 2023-05-25 16:28:04 -0700 | [diff] [blame] | 73 | } // namespace android |