summaryrefslogtreecommitdiff
path: root/services/surfaceflinger/TimeStats/timestatsproto/TimeStatsHelper.cpp
blob: ffb2f0921d8455eec41acd44e80306323c909088 (plain)
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
 * 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 "timestatsproto/TimeStatsHelper.h"

#include <android-base/stringprintf.h>
#include <inttypes.h>

#include <array>

#define HISTOGRAM_SIZE 85

using android::base::StringAppendF;
using android::base::StringPrintf;

namespace android {
namespace surfaceflinger {

// Time buckets for histogram, the calculated time deltas will be lower bounded
// to the buckets in this array.
static const std::array<int32_t, HISTOGRAM_SIZE> histogramConfig =
        {0,   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,  36,  38,  40,  42,  44,  46,  48,  50,  54,  58,  62,  66,  70,  74,  78,  82,
         86,  90,  94,  98,  102, 106, 110, 114, 118, 122, 126, 130, 134, 138, 142, 146, 150,
         200, 250, 300, 350, 400, 450, 500, 550, 600, 650, 700, 750, 800, 850, 900, 950, 1000};

void TimeStatsHelper::Histogram::insert(int32_t delta) {
    if (delta < 0) return;
    // std::lower_bound won't work on out of range values
    if (delta > histogramConfig[HISTOGRAM_SIZE - 1]) {
        hist[histogramConfig[HISTOGRAM_SIZE - 1]]++;
        return;
    }
    auto iter = std::lower_bound(histogramConfig.begin(), histogramConfig.end(), delta);
    hist[*iter]++;
}

int64_t TimeStatsHelper::Histogram::totalTime() const {
    int64_t ret = 0;
    for (const auto& ele : hist) {
        ret += ele.first * ele.second;
    }
    return ret;
}

float TimeStatsHelper::Histogram::averageTime() const {
    int64_t ret = 0;
    int64_t count = 0;
    for (const auto& ele : hist) {
        count += ele.second;
        ret += ele.first * ele.second;
    }
    return static_cast<float>(ret) / count;
}

std::string TimeStatsHelper::Histogram::toString() const {
    std::string result;
    for (int32_t i = 0; i < HISTOGRAM_SIZE; ++i) {
        int32_t bucket = histogramConfig[i];
        int32_t count = (hist.count(bucket) == 0) ? 0 : hist.at(bucket);
        StringAppendF(&result, "%dms=%d ", bucket, count);
    }
    result.back() = '\n';
    return result;
}

std::string TimeStatsHelper::JankPayload::toString() const {
    std::string result;
    StringAppendF(&result, "totalTimelineFrames = %d\n", totalFrames);
    StringAppendF(&result, "jankyFrames = %d\n", totalJankyFrames);
    StringAppendF(&result, "sfLongCpuJankyFrames = %d\n", totalSFLongCpu);
    StringAppendF(&result, "sfLongGpuJankyFrames = %d\n", totalSFLongGpu);
    StringAppendF(&result, "sfUnattributedJankyFrames = %d\n", totalSFUnattributed);
    StringAppendF(&result, "appUnattributedJankyFrames = %d\n", totalAppUnattributed);
    StringAppendF(&result, "sfSchedulingJankyFrames = %d\n", totalSFScheduling);
    StringAppendF(&result, "sfPredictionErrorJankyFrames = %d\n", totalSFPredictionError);
    StringAppendF(&result, "appBufferStuffingJankyFrames = %d\n", totalAppBufferStuffing);
    return result;
}

std::string TimeStatsHelper::SetFrameRateVote::toString(FrameRateCompatibility compatibility) {
    switch (compatibility) {
        case FrameRateCompatibility::Undefined:
            return "Undefined";
        case FrameRateCompatibility::Default:
            return "Default";
        case FrameRateCompatibility::ExactOrMultiple:
            return "ExactOrMultiple";
    }
}

std::string TimeStatsHelper::SetFrameRateVote::toString(Seamlessness seamlessness) {
    switch (seamlessness) {
        case Seamlessness::Undefined:
            return "Undefined";
        case Seamlessness::ShouldBeSeamless:
            return "ShouldBeSeamless";
        case Seamlessness::NotRequired:
            return "NotRequired";
    }
}

std::string TimeStatsHelper::SetFrameRateVote::toString() const {
    std::string result;
    StringAppendF(&result, "frameRate = %.2f\n", frameRate);
    StringAppendF(&result, "frameRateCompatibility = %s\n",
                  toString(frameRateCompatibility).c_str());
    StringAppendF(&result, "seamlessness = %s\n", toString(seamlessness).c_str());
    return result;
}

std::string TimeStatsHelper::TimeStatsLayer::toString(int32_t gameMode) const {
    switch (gameMode) {
        case TimeStatsHelper::GameModeUnsupported:
            return "GameModeUnsupported";
        case TimeStatsHelper::GameModeStandard:
            return "GameModeStandard";
        case TimeStatsHelper::GameModePerformance:
            return "GameModePerformance";
        case TimeStatsHelper::GameModeBattery:
            return "GameModeBattery";
        default:
            return "GameModeUnspecified";
    }
}
std::string TimeStatsHelper::TimeStatsLayer::toString() const {
    std::string result = "\n";
    StringAppendF(&result, "displayRefreshRate = %d fps\n", displayRefreshRateBucket);
    StringAppendF(&result, "renderRate = %d fps\n", renderRateBucket);
    StringAppendF(&result, "uid = %d\n", uid);
    StringAppendF(&result, "layerName = %s\n", layerName.c_str());
    StringAppendF(&result, "packageName = %s\n", packageName.c_str());
    StringAppendF(&result, "gameMode = %s\n", toString(gameMode).c_str());
    StringAppendF(&result, "totalFrames = %d\n", totalFrames);
    StringAppendF(&result, "droppedFrames = %d\n", droppedFrames);
    StringAppendF(&result, "lateAcquireFrames = %d\n", lateAcquireFrames);
    StringAppendF(&result, "badDesiredPresentFrames = %d\n", badDesiredPresentFrames);
    result.append("Jank payload for this layer:\n");
    result.append(jankPayload.toString());
    result.append("SetFrateRate vote for this layer:\n");
    result.append(setFrameRateVote.toString());
    const auto iter = deltas.find("present2present");
    if (iter != deltas.end()) {
        const float averageTime = iter->second.averageTime();
        const float averageFPS = averageTime < 1.0f ? 0.0f : 1000.0f / averageTime;
        StringAppendF(&result, "averageFPS = %.3f\n", averageFPS);
    }
    for (const auto& ele : deltas) {
        StringAppendF(&result, "%s histogram is as below:\n", ele.first.c_str());
        result.append(ele.second.toString());
    }

    return result;
}

std::string TimeStatsHelper::TimeStatsGlobal::toString(std::optional<uint32_t> maxLayers) const {
    std::string result = "SurfaceFlinger TimeStats:\n";
    result.append("Legacy stats are as follows:\n");
    StringAppendF(&result, "statsStart = %" PRId64 "\n", statsStartLegacy);
    StringAppendF(&result, "statsEnd = %" PRId64 "\n", statsEndLegacy);
    StringAppendF(&result, "totalFrames = %d\n", totalFramesLegacy);
    StringAppendF(&result, "missedFrames = %d\n", missedFramesLegacy);
    StringAppendF(&result, "clientCompositionFrames = %d\n", clientCompositionFramesLegacy);
    StringAppendF(&result, "clientCompositionReusedFrames = %d\n",
                  clientCompositionReusedFramesLegacy);
    StringAppendF(&result, "refreshRateSwitches = %d\n", refreshRateSwitchesLegacy);
    StringAppendF(&result, "compositionStrategyChanges = %d\n", compositionStrategyChangesLegacy);
    StringAppendF(&result, "displayOnTime = %" PRId64 " ms\n", displayOnTimeLegacy);
    StringAppendF(&result, "displayConfigStats is as below:\n");
    for (const auto& [fps, duration] : refreshRateStatsLegacy) {
        StringAppendF(&result, "%dfps = %ldms\n", fps, ns2ms(duration));
    }
    result.back() = '\n';
    StringAppendF(&result, "totalP2PTime = %" PRId64 " ms\n", presentToPresentLegacy.totalTime());
    StringAppendF(&result, "presentToPresent histogram is as below:\n");
    result.append(presentToPresentLegacy.toString());
    const float averageFrameDuration = frameDurationLegacy.averageTime();
    StringAppendF(&result, "averageFrameDuration = %.3f ms\n",
                  std::isnan(averageFrameDuration) ? 0.0f : averageFrameDuration);
    StringAppendF(&result, "frameDuration histogram is as below:\n");
    result.append(frameDurationLegacy.toString());
    const float averageRenderEngineTiming = renderEngineTimingLegacy.averageTime();
    StringAppendF(&result, "averageRenderEngineTiming = %.3f ms\n",
                  std::isnan(averageRenderEngineTiming) ? 0.0f : averageRenderEngineTiming);
    StringAppendF(&result, "renderEngineTiming histogram is as below:\n");
    result.append(renderEngineTimingLegacy.toString());

    result.append("\nGlobal aggregated jank payload (Timeline stats):");
    for (const auto& ele : stats) {
        result.append("\n");
        StringAppendF(&result, "displayRefreshRate = %d fps\n",
                      ele.second.key.displayRefreshRateBucket);
        StringAppendF(&result, "renderRate = %d fps\n", ele.second.key.renderRateBucket);
        result.append(ele.second.jankPayload.toString());
        StringAppendF(&result, "sfDeadlineMisses histogram is as below:\n");
        result.append(ele.second.displayDeadlineDeltas.toString());
        StringAppendF(&result, "sfPredictionErrors histogram is as below:\n");
        result.append(ele.second.displayPresentDeltas.toString());
    }

    const auto dumpStats = generateDumpStats(maxLayers);
    for (const auto& ele : dumpStats) {
        result.append(ele->toString());
    }

    return result;
}

SFTimeStatsLayerProto TimeStatsHelper::TimeStatsLayer::toProto() const {
    SFTimeStatsLayerProto layerProto;
    layerProto.set_layer_name(layerName);
    layerProto.set_package_name(packageName);
    layerProto.set_total_frames(totalFrames);
    layerProto.set_dropped_frames(droppedFrames);
    for (const auto& ele : deltas) {
        SFTimeStatsDeltaProto* deltaProto = layerProto.add_deltas();
        deltaProto->set_delta_name(ele.first);
        for (const auto& histEle : ele.second.hist) {
            SFTimeStatsHistogramBucketProto* histProto = deltaProto->add_histograms();
            histProto->set_time_millis(histEle.first);
            histProto->set_frame_count(histEle.second);
        }
    }
    return layerProto;
}

SFTimeStatsGlobalProto TimeStatsHelper::TimeStatsGlobal::toProto(
        std::optional<uint32_t> maxLayers) const {
    SFTimeStatsGlobalProto globalProto;
    globalProto.set_stats_start(statsStartLegacy);
    globalProto.set_stats_end(statsEndLegacy);
    globalProto.set_total_frames(totalFramesLegacy);
    globalProto.set_missed_frames(missedFramesLegacy);
    globalProto.set_client_composition_frames(clientCompositionFramesLegacy);
    globalProto.set_display_on_time(displayOnTimeLegacy);
    for (const auto& ele : refreshRateStatsLegacy) {
        SFTimeStatsDisplayConfigBucketProto* configBucketProto =
                globalProto.add_display_config_stats();
        SFTimeStatsDisplayConfigProto* configProto = configBucketProto->mutable_config();
        configProto->set_fps(ele.first);
        configBucketProto->set_duration_millis(ns2ms(ele.second));
    }
    for (const auto& histEle : presentToPresentLegacy.hist) {
        SFTimeStatsHistogramBucketProto* histProto = globalProto.add_present_to_present();
        histProto->set_time_millis(histEle.first);
        histProto->set_frame_count(histEle.second);
    }
    for (const auto& histEle : frameDurationLegacy.hist) {
        SFTimeStatsHistogramBucketProto* histProto = globalProto.add_frame_duration();
        histProto->set_time_millis(histEle.first);
        histProto->set_frame_count(histEle.second);
    }
    for (const auto& histEle : renderEngineTimingLegacy.hist) {
        SFTimeStatsHistogramBucketProto* histProto = globalProto.add_render_engine_timing();
        histProto->set_time_millis(histEle.first);
        histProto->set_frame_count(histEle.second);
    }
    const auto dumpStats = generateDumpStats(maxLayers);
    for (const auto& ele : dumpStats) {
        SFTimeStatsLayerProto* layerProto = globalProto.add_stats();
        layerProto->CopyFrom(ele->toProto());
    }
    return globalProto;
}

std::vector<TimeStatsHelper::TimeStatsLayer const*>
TimeStatsHelper::TimeStatsGlobal::generateDumpStats(std::optional<uint32_t> maxLayers) const {
    std::vector<TimeStatsLayer const*> dumpStats;

    int numLayers = 0;
    for (const auto& ele : stats) {
        numLayers += ele.second.stats.size();
    }

    dumpStats.reserve(numLayers);

    for (const auto& ele : stats) {
        for (const auto& layerEle : ele.second.stats) {
            dumpStats.push_back(&layerEle.second);
        }
    }

    std::sort(dumpStats.begin(), dumpStats.end(),
              [](TimeStatsHelper::TimeStatsLayer const* l,
                 TimeStatsHelper::TimeStatsLayer const* r) {
                  return l->totalFrames > r->totalFrames;
              });

    if (maxLayers && (*maxLayers < dumpStats.size())) {
        dumpStats.resize(*maxLayers);
    }
    return dumpStats;
}

} // namespace surfaceflinger
} // namespace android