John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | #pragma once |
| 18 | |
| 19 | #include "utils/Macros.h" |
| 20 | |
| 21 | #include <utils/Timers.h> |
| 22 | |
| 23 | #include <array> |
| 24 | #include <functional> |
| 25 | #include <tuple> |
| 26 | |
| 27 | namespace android { |
| 28 | namespace uirenderer { |
| 29 | |
| 30 | enum JankType { |
| 31 | kMissedVsync = 0, |
| 32 | kHighInputLatency, |
| 33 | kSlowUI, |
| 34 | kSlowSync, |
| 35 | kSlowRT, |
John Reck | 0e48647 | 2018-03-19 14:06:16 -0700 | [diff] [blame] | 36 | kMissedDeadline, |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 37 | |
| 38 | // must be last |
| 39 | NUM_BUCKETS, |
| 40 | }; |
| 41 | |
| 42 | // For testing |
| 43 | class MockProfileData; |
| 44 | |
| 45 | // Try to keep as small as possible, should match ASHMEM_SIZE in |
| 46 | // GraphicsStatsService.java |
| 47 | class ProfileData { |
| 48 | PREVENT_COPY_AND_ASSIGN(ProfileData); |
| 49 | |
| 50 | public: |
| 51 | ProfileData() { reset(); } |
| 52 | |
| 53 | void reset(); |
| 54 | void mergeWith(const ProfileData& other); |
| 55 | void dump(int fd) const; |
| 56 | uint32_t findPercentile(int percentile) const; |
Stan Iliev | 7203e1f | 2019-07-25 13:12:02 -0400 | [diff] [blame] | 57 | uint32_t findGPUPercentile(int percentile) const; |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 58 | |
| 59 | void reportFrame(int64_t duration); |
Stan Iliev | 7203e1f | 2019-07-25 13:12:02 -0400 | [diff] [blame] | 60 | void reportGPUFrame(int64_t duration); |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 61 | void reportJank() { mJankFrameCount++; } |
| 62 | void reportJankType(JankType type) { mJankTypeCounts[static_cast<int>(type)]++; } |
| 63 | |
| 64 | uint32_t totalFrameCount() const { return mTotalFrameCount; } |
| 65 | uint32_t jankFrameCount() const { return mJankFrameCount; } |
| 66 | nsecs_t statsStartTime() const { return mStatStartTime; } |
| 67 | uint32_t jankTypeCount(JankType type) const { return mJankTypeCounts[static_cast<int>(type)]; } |
| 68 | |
| 69 | struct HistogramEntry { |
| 70 | uint32_t renderTimeMs; |
| 71 | uint32_t frameCount; |
| 72 | }; |
| 73 | void histogramForEach(const std::function<void(HistogramEntry)>& callback) const; |
Stan Iliev | 7203e1f | 2019-07-25 13:12:02 -0400 | [diff] [blame] | 74 | void histogramGPUForEach(const std::function<void(HistogramEntry)>& callback) const; |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 75 | |
| 76 | constexpr static int HistogramSize() { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 77 | return std::tuple_size<decltype(ProfileData::mFrameCounts)>::value + |
| 78 | std::tuple_size<decltype(ProfileData::mSlowFrameCounts)>::value; |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Stan Iliev | 7203e1f | 2019-07-25 13:12:02 -0400 | [diff] [blame] | 81 | constexpr static int GPUHistogramSize() { |
| 82 | return std::tuple_size<decltype(ProfileData::mGPUFrameCounts)>::value; |
| 83 | } |
| 84 | |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 85 | // Visible for testing |
| 86 | static uint32_t frameTimeForFrameCountIndex(uint32_t index); |
| 87 | static uint32_t frameTimeForSlowFrameCountIndex(uint32_t index); |
Stan Iliev | 7203e1f | 2019-07-25 13:12:02 -0400 | [diff] [blame] | 88 | static uint32_t GPUFrameTimeForFrameCountIndex(uint32_t index); |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 89 | |
| 90 | private: |
| 91 | // Open our guts up to unit tests |
| 92 | friend class MockProfileData; |
| 93 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 94 | std::array<uint32_t, NUM_BUCKETS> mJankTypeCounts; |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 95 | // See comments on kBucket* constants for what this holds |
| 96 | std::array<uint32_t, 57> mFrameCounts; |
| 97 | // Holds a histogram of frame times in 50ms increments from 150ms to 5s |
| 98 | std::array<uint16_t, 97> mSlowFrameCounts; |
Stan Iliev | 7203e1f | 2019-07-25 13:12:02 -0400 | [diff] [blame] | 99 | // Holds a histogram of GPU draw times in 1ms increments. Frames longer than 25ms are placed in |
| 100 | // last bucket. |
| 101 | std::array<uint32_t, 26> mGPUFrameCounts; |
John Reck | 7075c79 | 2017-07-05 14:03:43 -0700 | [diff] [blame] | 102 | |
| 103 | uint32_t mTotalFrameCount; |
| 104 | uint32_t mJankFrameCount; |
| 105 | nsecs_t mStatStartTime; |
| 106 | }; |
| 107 | |
| 108 | // For testing |
| 109 | class MockProfileData : public ProfileData { |
| 110 | public: |
| 111 | std::array<uint32_t, NUM_BUCKETS>& editJankTypeCounts() { return mJankTypeCounts; } |
| 112 | std::array<uint32_t, 57>& editFrameCounts() { return mFrameCounts; } |
| 113 | std::array<uint16_t, 97>& editSlowFrameCounts() { return mSlowFrameCounts; } |
| 114 | uint32_t& editTotalFrameCount() { return mTotalFrameCount; } |
| 115 | uint32_t& editJankFrameCount() { return mJankFrameCount; } |
| 116 | nsecs_t& editStatStartTime() { return mStatStartTime; } |
| 117 | }; |
| 118 | |
| 119 | } /* namespace uirenderer */ |
| 120 | } /* namespace android */ |