1ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck/*
2ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck * Copyright (C) 2015 The Android Open Source Project
3ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck *
4ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck * Licensed under the Apache License, Version 2.0 (the "License");
5ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck * you may not use this file except in compliance with the License.
6ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck * You may obtain a copy of the License at
7ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck *
8ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck *      http://www.apache.org/licenses/LICENSE-2.0
9ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck *
10ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck * Unless required by applicable law or agreed to in writing, software
11ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck * distributed under the License is distributed on an "AS IS" BASIS,
12ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck * See the License for the specific language governing permissions and
14ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck * limitations under the License.
15ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck */
16ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck#ifndef JANKTRACKER_H_
17ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck#define JANKTRACKER_H_
18ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck
19ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck#include "FrameInfo.h"
20ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck#include "renderthread/TimeLord.h"
21ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck#include "utils/RingBuffer.h"
22ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck
23edc524c90506d80e0fc5fb67e8de7b8f3ef53439John Reck#include <cutils/compiler.h>
242d5b8d73929a38b019c6b6276d4a19542b990f0cJohn Reck#include <ui/DisplayInfo.h>
25edc524c90506d80e0fc5fb67e8de7b8f3ef53439John Reck
26b36016c65f1d1b5846dba0349aab491dbd3a746aJohn Reck#include <array>
27ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck#include <memory>
28ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck
29ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Recknamespace android {
30ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Recknamespace uirenderer {
31ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck
32ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reckenum JankType {
33ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck    kMissedVsync = 0,
34ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck    kHighInputLatency,
35ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck    kSlowUI,
36ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck    kSlowSync,
37ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck    kSlowRT,
38ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck
39ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck    // must be last
40ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck    NUM_BUCKETS,
41ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck};
42ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck
43edc524c90506d80e0fc5fb67e8de7b8f3ef53439John Reck// Try to keep as small as possible, should match ASHMEM_SIZE in
44edc524c90506d80e0fc5fb67e8de7b8f3ef53439John Reck// GraphicsStatsService.java
45edc524c90506d80e0fc5fb67e8de7b8f3ef53439John Reckstruct ProfileData {
46edc524c90506d80e0fc5fb67e8de7b8f3ef53439John Reck    std::array<uint32_t, NUM_BUCKETS> jankTypeCounts;
47edc524c90506d80e0fc5fb67e8de7b8f3ef53439John Reck    // See comments on kBucket* constants for what this holds
48660108075e61d7b7e6c138000890011510d5b079John Reck    std::array<uint32_t, 57> frameCounts;
49660108075e61d7b7e6c138000890011510d5b079John Reck    // Holds a histogram of frame times in 50ms increments from 150ms to 5s
50660108075e61d7b7e6c138000890011510d5b079John Reck    std::array<uint16_t, 97> slowFrameCounts;
51edc524c90506d80e0fc5fb67e8de7b8f3ef53439John Reck
52edc524c90506d80e0fc5fb67e8de7b8f3ef53439John Reck    uint32_t totalFrameCount;
53edc524c90506d80e0fc5fb67e8de7b8f3ef53439John Reck    uint32_t jankFrameCount;
54379f264bb62ace2cf2053d4765307234bf66552fJohn Reck    nsecs_t statStartTime;
55ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck};
56ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck
57df1742ed47da1e9b61afeae16fa448d5302a8aa0John Reckenum class JankTrackerType {
58df1742ed47da1e9b61afeae16fa448d5302a8aa0John Reck    // The default, means there's no description set
59df1742ed47da1e9b61afeae16fa448d5302a8aa0John Reck    Generic,
60df1742ed47da1e9b61afeae16fa448d5302a8aa0John Reck    // The profile data represents a package
61df1742ed47da1e9b61afeae16fa448d5302a8aa0John Reck    Package,
62df1742ed47da1e9b61afeae16fa448d5302a8aa0John Reck    // The profile data is for a specific window
63df1742ed47da1e9b61afeae16fa448d5302a8aa0John Reck    Window,
64df1742ed47da1e9b61afeae16fa448d5302a8aa0John Reck};
65df1742ed47da1e9b61afeae16fa448d5302a8aa0John Reck
66df1742ed47da1e9b61afeae16fa448d5302a8aa0John Reck// Metadata about the ProfileData being collected
67df1742ed47da1e9b61afeae16fa448d5302a8aa0John Reckstruct ProfileDataDescription {
68df1742ed47da1e9b61afeae16fa448d5302a8aa0John Reck    JankTrackerType type;
69df1742ed47da1e9b61afeae16fa448d5302a8aa0John Reck    std::string name;
70df1742ed47da1e9b61afeae16fa448d5302a8aa0John Reck};
71df1742ed47da1e9b61afeae16fa448d5302a8aa0John Reck
72ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck// TODO: Replace DrawProfiler with this
73ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reckclass JankTracker {
74ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reckpublic:
75fb5c675b7e1fee074f19cf1866b5dda0785dbe64John Reck    explicit JankTracker(const DisplayInfo& displayInfo);
76edc524c90506d80e0fc5fb67e8de7b8f3ef53439John Reck    ~JankTracker();
77ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck
78df1742ed47da1e9b61afeae16fa448d5302a8aa0John Reck    void setDescription(JankTrackerType type, const std::string&& name) {
79df1742ed47da1e9b61afeae16fa448d5302a8aa0John Reck        mDescription.type = type;
80df1742ed47da1e9b61afeae16fa448d5302a8aa0John Reck        mDescription.name = name;
81df1742ed47da1e9b61afeae16fa448d5302a8aa0John Reck    }
82df1742ed47da1e9b61afeae16fa448d5302a8aa0John Reck
83ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck    void addFrame(const FrameInfo& frame);
84ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck
85df1742ed47da1e9b61afeae16fa448d5302a8aa0John Reck    void dump(int fd) { dumpData(fd, &mDescription, mData); }
86ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck    void reset();
87ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck
88df1742ed47da1e9b61afeae16fa448d5302a8aa0John Reck    void rotateStorage();
89edc524c90506d80e0fc5fb67e8de7b8f3ef53439John Reck    void switchStorageToAshmem(int ashmemfd);
90edc524c90506d80e0fc5fb67e8de7b8f3ef53439John Reck
91edc524c90506d80e0fc5fb67e8de7b8f3ef53439John Reck    uint32_t findPercentile(int p) { return findPercentile(mData, p); }
92df1742ed47da1e9b61afeae16fa448d5302a8aa0John Reck    static int32_t frameTimeForFrameCountIndex(uint32_t index);
93df1742ed47da1e9b61afeae16fa448d5302a8aa0John Reck    static int32_t frameTimeForSlowFrameCountIndex(uint32_t index);
94edc524c90506d80e0fc5fb67e8de7b8f3ef53439John Reck
95ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reckprivate:
96edc524c90506d80e0fc5fb67e8de7b8f3ef53439John Reck    void freeData();
97edc524c90506d80e0fc5fb67e8de7b8f3ef53439John Reck    void setFrameInterval(nsecs_t frameIntervalNanos);
98e70c5754d01f2ab0ff47ea3eabaa88aca5ed2a36John Reck
99edc524c90506d80e0fc5fb67e8de7b8f3ef53439John Reck    static uint32_t findPercentile(const ProfileData* data, int p);
100df1742ed47da1e9b61afeae16fa448d5302a8aa0John Reck    static void dumpData(int fd, const ProfileDataDescription* description, const ProfileData* data);
101ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck
102edc524c90506d80e0fc5fb67e8de7b8f3ef53439John Reck    std::array<int64_t, NUM_BUCKETS> mThresholds;
103ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck    int64_t mFrameInterval;
1042d5b8d73929a38b019c6b6276d4a19542b990f0cJohn Reck    // The amount of time we will erase from the total duration to account
1052d5b8d73929a38b019c6b6276d4a19542b990f0cJohn Reck    // for SF vsync offsets with HWC2 blocking dequeueBuffers.
1062d5b8d73929a38b019c6b6276d4a19542b990f0cJohn Reck    // (Vsync + mDequeueBlockTolerance) is the point at which we expect
1072d5b8d73929a38b019c6b6276d4a19542b990f0cJohn Reck    // SF to have released the buffer normally, so we will forgive up to that
1082d5b8d73929a38b019c6b6276d4a19542b990f0cJohn Reck    // point in time by comparing to (IssueDrawCommandsStart + DequeueDuration)
1092d5b8d73929a38b019c6b6276d4a19542b990f0cJohn Reck    // This is only used if we are in pipelined mode and are using HWC2,
1102d5b8d73929a38b019c6b6276d4a19542b990f0cJohn Reck    // otherwise it's 0.
1112d5b8d73929a38b019c6b6276d4a19542b990f0cJohn Reck    nsecs_t mDequeueTimeForgiveness = 0;
112edc524c90506d80e0fc5fb67e8de7b8f3ef53439John Reck    ProfileData* mData;
113edc524c90506d80e0fc5fb67e8de7b8f3ef53439John Reck    bool mIsMapped = false;
114df1742ed47da1e9b61afeae16fa448d5302a8aa0John Reck    ProfileDataDescription mDescription;
115ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck};
116ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck
117ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck} /* namespace uirenderer */
118ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck} /* namespace android */
119ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck
120ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck#endif /* JANKTRACKER_H_ */
121