blob: 8399c5dfca7bd96208639cf81d35bd5937360423 [file] [log] [blame]
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001/*
2 * Copyright 2018 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 <timestatsproto/TimeStatsHelper.h>
20#include <timestatsproto/TimeStatsProtoHeader.h>
21
22#include <ui/FenceTime.h>
23
24#include <utils/String16.h>
25#include <utils/String8.h>
26#include <utils/Vector.h>
27
Yiwei Zhangc5f2c452018-05-08 16:31:56 -070028#include <deque>
Yiwei Zhang0102ad22018-05-02 17:37:17 -070029#include <mutex>
30#include <unordered_map>
Yiwei Zhang0102ad22018-05-02 17:37:17 -070031
32using namespace android::surfaceflinger;
33
34namespace android {
35class String8;
36
37class TimeStats {
38 // TODO(zzyiwei): Bound the timeStatsTracker with weighted LRU
39 // static const size_t MAX_NUM_LAYER_RECORDS = 200;
40 static const size_t MAX_NUM_TIME_RECORDS = 64;
41
42 struct TimeRecord {
43 bool ready = false;
44 uint64_t frameNumber = 0;
45 nsecs_t postTime = 0;
46 nsecs_t latchTime = 0;
47 nsecs_t acquireTime = 0;
48 nsecs_t desiredTime = 0;
49 nsecs_t presentTime = 0;
50 std::shared_ptr<FenceTime> acquireFence;
51 std::shared_ptr<FenceTime> presentFence;
52 };
53
54 struct LayerRecord {
55 // This is the index in timeRecords, at which the timestamps for that
56 // specific frame are still not fully received. This is not waiting for
57 // fences to signal, but rather waiting to receive those fences/timestamps.
58 int32_t waitData = -1;
59 TimeRecord prevTimeRecord;
Yiwei Zhangc5f2c452018-05-08 16:31:56 -070060 std::deque<TimeRecord> timeRecords;
Yiwei Zhang0102ad22018-05-02 17:37:17 -070061 };
62
63public:
64 static TimeStats& getInstance();
65 void parseArgs(bool asProto, const Vector<String16>& args, size_t& index, String8& result);
66 void incrementTotalFrames();
Yiwei Zhang621f9d42018-05-07 10:40:55 -070067 void incrementMissedFrames();
Yiwei Zhang0102ad22018-05-02 17:37:17 -070068 void incrementClientCompositionFrames();
69
70 void setPostTime(const std::string& layerName, uint64_t frameNumber, nsecs_t postTime);
71 void setLatchTime(const std::string& layerName, uint64_t frameNumber, nsecs_t latchTime);
72 void setDesiredTime(const std::string& layerName, uint64_t frameNumber, nsecs_t desiredTime);
73 void setAcquireTime(const std::string& layerName, uint64_t frameNumber, nsecs_t acquireTime);
74 void setAcquireFence(const std::string& layerName, uint64_t frameNumber,
75 const std::shared_ptr<FenceTime>& acquireFence);
76 void setPresentTime(const std::string& layerName, uint64_t frameNumber, nsecs_t presentTime);
77 void setPresentFence(const std::string& layerName, uint64_t frameNumber,
78 const std::shared_ptr<FenceTime>& presentFence);
79 void onDisconnect(const std::string& layerName);
80 void clearLayerRecord(const std::string& layerName);
81 void removeTimeRecord(const std::string& layerName, uint64_t frameNumber);
82
83private:
84 TimeStats() = default;
85
86 bool recordReadyLocked(const std::string& layerName, TimeRecord* timeRecord);
87 void flushAvailableRecordsToStatsLocked(const std::string& layerName);
88
89 void enable();
90 void disable();
91 void clear();
92 bool isEnabled();
93 void dump(bool asProto, uint32_t maxLayer, String8& result);
94 void dumpAsTextLocked(String8& result);
95 void dumpAsProtoLocked(String8& result);
96
97 std::atomic<bool> mEnabled = false;
98 std::mutex mMutex;
99 TimeStatsHelper::TimeStatsGlobal timeStats;
100 std::unordered_map<std::string, LayerRecord> timeStatsTracker;
101};
102
103} // namespace android