File: ProfileData.cpp

package info (click to toggle)
android-platform-frameworks-base 1%3A14~beta1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 326,092 kB
  • sloc: java: 2,032,373; xml: 343,016; cpp: 304,181; python: 3,683; ansic: 2,090; sh: 1,871; makefile: 117; sed: 19
file content (230 lines) | stat: -rw-r--r-- 9,171 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "ProfileData.h"
#include "Properties.h"

#include <cinttypes>

namespace android {
namespace uirenderer {

static const char* JANK_TYPE_NAMES[] = {
        "Missed Vsync",        "High input latency",       "Slow UI thread",
        "Slow bitmap uploads", "Slow issue draw commands", "Frame deadline missed",
        "Frame deadline missed (legacy)"};

// The bucketing algorithm controls so to speak
// If a frame is <= to this it goes in bucket 0
static const uint32_t kBucketMinThreshold = 5;
// If a frame is > this, start counting in increments of 2ms
static const uint32_t kBucket2msIntervals = 32;
// If a frame is > this, start counting in increments of 4ms
static const uint32_t kBucket4msIntervals = 48;

// The interval of the slow frame histogram
static const uint32_t kSlowFrameBucketIntervalMs = 50;
// The start point of the slow frame bucket in ms
static const uint32_t kSlowFrameBucketStartMs = 150;

// This will be called every frame, performance sensitive
// Uses bit twiddling to avoid branching while achieving the packing desired
static uint32_t frameCountIndexForFrameTime(nsecs_t frameTime) {
    uint32_t index = static_cast<uint32_t>(ns2ms(frameTime));
    // If index > kBucketMinThreshold mask will be 0xFFFFFFFF as a result
    // of negating 1 (twos compliment, yaay) else mask will be 0
    uint32_t mask = -(index > kBucketMinThreshold);
    // If index > threshold, this will essentially perform:
    // amountAboveThreshold = index - threshold;
    // index = threshold + (amountAboveThreshold / 2)
    // However if index is <= this will do nothing. It will underflow, do
    // a right shift by 0 (no-op), then overflow back to the original value
    index = ((index - kBucket4msIntervals) >> (index > kBucket4msIntervals)) + kBucket4msIntervals;
    index = ((index - kBucket2msIntervals) >> (index > kBucket2msIntervals)) + kBucket2msIntervals;
    // If index was < minThreshold at the start of all this it's going to
    // be a pretty garbage value right now. However, mask is 0 so we'll end
    // up with the desired result of 0.
    index = (index - kBucketMinThreshold) & mask;
    return index;
}

// Only called when dumping stats, less performance sensitive
uint32_t ProfileData::frameTimeForFrameCountIndex(uint32_t index) {
    index = index + kBucketMinThreshold;
    if (index > kBucket2msIntervals) {
        index += (index - kBucket2msIntervals);
    }
    if (index > kBucket4msIntervals) {
        // This works because it was already doubled by the above if
        // 1 is added to shift slightly more towards the middle of the bucket
        index += (index - kBucket4msIntervals) + 1;
    }
    return index;
}

uint32_t ProfileData::frameTimeForSlowFrameCountIndex(uint32_t index) {
    return (index * kSlowFrameBucketIntervalMs) + kSlowFrameBucketStartMs;
}

void ProfileData::mergeWith(const ProfileData& other) {
    // Make sure we don't overflow Just In Case
    uint32_t divider = 0;
    if (mTotalFrameCount > (1 << 24)) {
        divider = 4;
    }
    for (size_t i = 0; i < other.mJankTypeCounts.size(); i++) {
        mJankTypeCounts[i] >>= divider;
        mJankTypeCounts[i] += other.mJankTypeCounts[i];
    }
    for (size_t i = 0; i < other.mFrameCounts.size(); i++) {
        mFrameCounts[i] >>= divider;
        mFrameCounts[i] += other.mFrameCounts[i];
    }
    mJankFrameCount >>= divider;
    mJankFrameCount += other.mJankFrameCount;
    mJankLegacyFrameCount >>= divider;
    mJankLegacyFrameCount += other.mJankLegacyFrameCount;
    mTotalFrameCount >>= divider;
    mTotalFrameCount += other.mTotalFrameCount;
    if (mStatStartTime > other.mStatStartTime || mStatStartTime == 0) {
        mStatStartTime = other.mStatStartTime;
    }
    for (size_t i = 0; i < other.mGPUFrameCounts.size(); i++) {
        mGPUFrameCounts[i] >>= divider;
        mGPUFrameCounts[i] += other.mGPUFrameCounts[i];
    }
    mPipelineType = other.mPipelineType;
}

void ProfileData::dump(int fd) const {
    dprintf(fd, "\nStats since: %" PRIu64 "ns", mStatStartTime);
    dprintf(fd, "\nTotal frames rendered: %u", mTotalFrameCount);
    dprintf(fd, "\nJanky frames: %u (%.2f%%)", mJankFrameCount,
            mTotalFrameCount == 0 ? 0.0f
                                  : (float)mJankFrameCount / (float)mTotalFrameCount * 100.0f);
    dprintf(fd, "\nJanky frames (legacy): %u (%.2f%%)", mJankLegacyFrameCount, mTotalFrameCount == 0
            ? 0.0f
            : (float)mJankLegacyFrameCount / (float)mTotalFrameCount * 100.0f);
    dprintf(fd, "\n50th percentile: %ums", findPercentile(50));
    dprintf(fd, "\n90th percentile: %ums", findPercentile(90));
    dprintf(fd, "\n95th percentile: %ums", findPercentile(95));
    dprintf(fd, "\n99th percentile: %ums", findPercentile(99));
    for (int i = 0; i < NUM_BUCKETS; i++) {
        dprintf(fd, "\nNumber %s: %u", JANK_TYPE_NAMES[i], mJankTypeCounts[i]);
    }
    dprintf(fd, "\nHISTOGRAM:");
    histogramForEach([fd](HistogramEntry entry) {
        dprintf(fd, " %ums=%u", entry.renderTimeMs, entry.frameCount);
    });
    dprintf(fd, "\n50th gpu percentile: %ums", findGPUPercentile(50));
    dprintf(fd, "\n90th gpu percentile: %ums", findGPUPercentile(90));
    dprintf(fd, "\n95th gpu percentile: %ums", findGPUPercentile(95));
    dprintf(fd, "\n99th gpu percentile: %ums", findGPUPercentile(99));
    dprintf(fd, "\nGPU HISTOGRAM:");
    histogramGPUForEach([fd](HistogramEntry entry) {
        dprintf(fd, " %ums=%u", entry.renderTimeMs, entry.frameCount);
    });
    dprintf(fd, "\n");
}

uint32_t ProfileData::findPercentile(int percentile) const {
    int pos = percentile * mTotalFrameCount / 100;
    int remaining = mTotalFrameCount - pos;
    for (int i = mSlowFrameCounts.size() - 1; i >= 0; i--) {
        remaining -= mSlowFrameCounts[i];
        if (remaining <= 0) {
            return (i * kSlowFrameBucketIntervalMs) + kSlowFrameBucketStartMs;
        }
    }
    for (int i = mFrameCounts.size() - 1; i >= 0; i--) {
        remaining -= mFrameCounts[i];
        if (remaining <= 0) {
            return frameTimeForFrameCountIndex(i);
        }
    }
    return 0;
}

void ProfileData::reset() {
    mJankTypeCounts.fill(0);
    mFrameCounts.fill(0);
    mGPUFrameCounts.fill(0);
    mSlowFrameCounts.fill(0);
    mTotalFrameCount = 0;
    mJankFrameCount = 0;
    mJankLegacyFrameCount = 0;
    mStatStartTime = systemTime(SYSTEM_TIME_MONOTONIC);
    mPipelineType = Properties::getRenderPipelineType();
}

void ProfileData::reportFrame(int64_t duration) {
    mTotalFrameCount++;
    uint32_t framebucket = frameCountIndexForFrameTime(duration);
    if (framebucket <= mFrameCounts.size()) {
        mFrameCounts[framebucket]++;
    } else {
        framebucket = (ns2ms(duration) - kSlowFrameBucketStartMs) / kSlowFrameBucketIntervalMs;
        framebucket = std::min(framebucket, static_cast<uint32_t>(mSlowFrameCounts.size() - 1));
        mSlowFrameCounts[framebucket]++;
    }
}

void ProfileData::histogramForEach(const std::function<void(HistogramEntry)>& callback) const {
    for (size_t i = 0; i < mFrameCounts.size(); i++) {
        callback(HistogramEntry{frameTimeForFrameCountIndex(i), mFrameCounts[i]});
    }
    for (size_t i = 0; i < mSlowFrameCounts.size(); i++) {
        callback(HistogramEntry{frameTimeForSlowFrameCountIndex(i), mSlowFrameCounts[i]});
    }
}

uint32_t ProfileData::findGPUPercentile(int percentile) const {
    uint32_t totalGPUFrameCount = 0;  // this is usually mTotalFrameCount - 3.
    for (int i = mGPUFrameCounts.size() - 1; i >= 0; i--) {
        totalGPUFrameCount += mGPUFrameCounts[i];
    }
    int pos = percentile * totalGPUFrameCount / 100;
    int remaining = totalGPUFrameCount - pos;
    for (int i = mGPUFrameCounts.size() - 1; i >= 0; i--) {
        remaining -= mGPUFrameCounts[i];
        if (remaining <= 0) {
            return GPUFrameTimeForFrameCountIndex(i);
        }
    }
    return 0;
}

uint32_t ProfileData::GPUFrameTimeForFrameCountIndex(uint32_t index) {
    return index != 25 ? index + 1 : 4950;
}

void ProfileData::reportGPUFrame(int64_t duration) {
    uint32_t index = static_cast<uint32_t>(ns2ms(duration));
    if (index > 25) {
        index = 25;
    }

    mGPUFrameCounts[index]++;
}

void ProfileData::histogramGPUForEach(const std::function<void(HistogramEntry)>& callback) const {
    for (size_t i = 0; i < mGPUFrameCounts.size(); i++) {
        callback(HistogramEntry{GPUFrameTimeForFrameCountIndex(i), mGPUFrameCounts[i]});
    }
}

} /* namespace uirenderer */
} /* namespace android */