blob: ccbffc6f136ea931d4c2d35540cd46f8da4b94a4 [file] [log] [blame]
John Reck7075c792017-07-05 14:03:43 -07001/*
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
27namespace android {
28namespace uirenderer {
29
30enum JankType {
31 kMissedVsync = 0,
32 kHighInputLatency,
33 kSlowUI,
34 kSlowSync,
35 kSlowRT,
John Reck0e486472018-03-19 14:06:16 -070036 kMissedDeadline,
John Reck7075c792017-07-05 14:03:43 -070037
38 // must be last
39 NUM_BUCKETS,
40};
41
42// For testing
43class MockProfileData;
44
45// Try to keep as small as possible, should match ASHMEM_SIZE in
46// GraphicsStatsService.java
47class ProfileData {
48 PREVENT_COPY_AND_ASSIGN(ProfileData);
49
50public:
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 Iliev7203e1f2019-07-25 13:12:02 -040057 uint32_t findGPUPercentile(int percentile) const;
John Reck7075c792017-07-05 14:03:43 -070058
59 void reportFrame(int64_t duration);
Stan Iliev7203e1f2019-07-25 13:12:02 -040060 void reportGPUFrame(int64_t duration);
John Reck7075c792017-07-05 14:03:43 -070061 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 Iliev7203e1f2019-07-25 13:12:02 -040074 void histogramGPUForEach(const std::function<void(HistogramEntry)>& callback) const;
John Reck7075c792017-07-05 14:03:43 -070075
76 constexpr static int HistogramSize() {
John Reck1bcacfd2017-11-03 10:12:19 -070077 return std::tuple_size<decltype(ProfileData::mFrameCounts)>::value +
78 std::tuple_size<decltype(ProfileData::mSlowFrameCounts)>::value;
John Reck7075c792017-07-05 14:03:43 -070079 }
80
Stan Iliev7203e1f2019-07-25 13:12:02 -040081 constexpr static int GPUHistogramSize() {
82 return std::tuple_size<decltype(ProfileData::mGPUFrameCounts)>::value;
83 }
84
John Reck7075c792017-07-05 14:03:43 -070085 // Visible for testing
86 static uint32_t frameTimeForFrameCountIndex(uint32_t index);
87 static uint32_t frameTimeForSlowFrameCountIndex(uint32_t index);
Stan Iliev7203e1f2019-07-25 13:12:02 -040088 static uint32_t GPUFrameTimeForFrameCountIndex(uint32_t index);
John Reck7075c792017-07-05 14:03:43 -070089
90private:
91 // Open our guts up to unit tests
92 friend class MockProfileData;
93
John Reck1bcacfd2017-11-03 10:12:19 -070094 std::array<uint32_t, NUM_BUCKETS> mJankTypeCounts;
John Reck7075c792017-07-05 14:03:43 -070095 // 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 Iliev7203e1f2019-07-25 13:12:02 -040099 // 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 Reck7075c792017-07-05 14:03:43 -0700102
103 uint32_t mTotalFrameCount;
104 uint32_t mJankFrameCount;
105 nsecs_t mStatStartTime;
106};
107
108// For testing
109class MockProfileData : public ProfileData {
110public:
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 */