1/*
2 * Copyright (C) 2015 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#ifndef JANKTRACKER_H_
17#define JANKTRACKER_H_
18
19#include "FrameInfo.h"
20#include "renderthread/TimeLord.h"
21#include "utils/RingBuffer.h"
22
23#include <cutils/compiler.h>
24
25#include <array>
26#include <memory>
27
28namespace android {
29namespace uirenderer {
30
31enum JankType {
32    kMissedVsync = 0,
33    kHighInputLatency,
34    kSlowUI,
35    kSlowSync,
36    kSlowRT,
37
38    // must be last
39    NUM_BUCKETS,
40};
41
42// Try to keep as small as possible, should match ASHMEM_SIZE in
43// GraphicsStatsService.java
44struct ProfileData {
45    std::array<uint32_t, NUM_BUCKETS> jankTypeCounts;
46    // See comments on kBucket* constants for what this holds
47    std::array<uint32_t, 55> frameCounts;
48
49    uint32_t totalFrameCount;
50    uint32_t jankFrameCount;
51    nsecs_t statStartTime;
52};
53
54// TODO: Replace DrawProfiler with this
55class JankTracker {
56public:
57    JankTracker(nsecs_t frameIntervalNanos);
58    ~JankTracker();
59
60    void addFrame(const FrameInfo& frame);
61
62    void dump(int fd) { dumpData(mData, fd); }
63    void reset();
64
65    void switchStorageToAshmem(int ashmemfd);
66
67    uint32_t findPercentile(int p) { return findPercentile(mData, p); }
68
69    ANDROID_API static void dumpBuffer(const void* buffer, size_t bufsize, int fd);
70
71private:
72    void freeData();
73    void setFrameInterval(nsecs_t frameIntervalNanos);
74
75    static uint32_t findPercentile(const ProfileData* data, int p);
76    static void dumpData(const ProfileData* data, int fd);
77
78    std::array<int64_t, NUM_BUCKETS> mThresholds;
79    int64_t mFrameInterval;
80    ProfileData* mData;
81    bool mIsMapped = false;
82};
83
84} /* namespace uirenderer */
85} /* namespace android */
86
87#endif /* JANKTRACKER_H_ */
88