Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 1 | /* |
| 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 Zhang | c5f2c45 | 2018-05-08 16:31:56 -0700 | [diff] [blame] | 28 | #include <deque> |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 29 | #include <mutex> |
Yiwei Zhang | 8a4015c | 2018-05-08 16:03:47 -0700 | [diff] [blame] | 30 | #include <optional> |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 31 | #include <unordered_map> |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 32 | |
| 33 | using namespace android::surfaceflinger; |
| 34 | |
| 35 | namespace android { |
| 36 | class String8; |
| 37 | |
| 38 | class TimeStats { |
| 39 | // TODO(zzyiwei): Bound the timeStatsTracker with weighted LRU |
| 40 | // static const size_t MAX_NUM_LAYER_RECORDS = 200; |
| 41 | static const size_t MAX_NUM_TIME_RECORDS = 64; |
| 42 | |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame^] | 43 | struct FrameTime { |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 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; |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame^] | 50 | }; |
| 51 | |
| 52 | struct TimeRecord { |
| 53 | bool ready = false; |
| 54 | FrameTime frameTime; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 55 | std::shared_ptr<FenceTime> acquireFence; |
| 56 | std::shared_ptr<FenceTime> presentFence; |
| 57 | }; |
| 58 | |
| 59 | struct LayerRecord { |
| 60 | // This is the index in timeRecords, at which the timestamps for that |
| 61 | // specific frame are still not fully received. This is not waiting for |
| 62 | // fences to signal, but rather waiting to receive those fences/timestamps. |
| 63 | int32_t waitData = -1; |
| 64 | TimeRecord prevTimeRecord; |
Yiwei Zhang | c5f2c45 | 2018-05-08 16:31:56 -0700 | [diff] [blame] | 65 | std::deque<TimeRecord> timeRecords; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | public: |
| 69 | static TimeStats& getInstance(); |
| 70 | void parseArgs(bool asProto, const Vector<String16>& args, size_t& index, String8& result); |
| 71 | void incrementTotalFrames(); |
Yiwei Zhang | 621f9d4 | 2018-05-07 10:40:55 -0700 | [diff] [blame] | 72 | void incrementMissedFrames(); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 73 | void incrementClientCompositionFrames(); |
| 74 | |
| 75 | void setPostTime(const std::string& layerName, uint64_t frameNumber, nsecs_t postTime); |
| 76 | void setLatchTime(const std::string& layerName, uint64_t frameNumber, nsecs_t latchTime); |
| 77 | void setDesiredTime(const std::string& layerName, uint64_t frameNumber, nsecs_t desiredTime); |
| 78 | void setAcquireTime(const std::string& layerName, uint64_t frameNumber, nsecs_t acquireTime); |
| 79 | void setAcquireFence(const std::string& layerName, uint64_t frameNumber, |
| 80 | const std::shared_ptr<FenceTime>& acquireFence); |
| 81 | void setPresentTime(const std::string& layerName, uint64_t frameNumber, nsecs_t presentTime); |
| 82 | void setPresentFence(const std::string& layerName, uint64_t frameNumber, |
| 83 | const std::shared_ptr<FenceTime>& presentFence); |
| 84 | void onDisconnect(const std::string& layerName); |
| 85 | void clearLayerRecord(const std::string& layerName); |
| 86 | void removeTimeRecord(const std::string& layerName, uint64_t frameNumber); |
| 87 | |
| 88 | private: |
| 89 | TimeStats() = default; |
| 90 | |
| 91 | bool recordReadyLocked(const std::string& layerName, TimeRecord* timeRecord); |
| 92 | void flushAvailableRecordsToStatsLocked(const std::string& layerName); |
| 93 | |
| 94 | void enable(); |
| 95 | void disable(); |
| 96 | void clear(); |
| 97 | bool isEnabled(); |
Yiwei Zhang | 8a4015c | 2018-05-08 16:03:47 -0700 | [diff] [blame] | 98 | void dump(bool asProto, std::optional<uint32_t> maxLayers, String8& result); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 99 | |
| 100 | std::atomic<bool> mEnabled = false; |
| 101 | std::mutex mMutex; |
| 102 | TimeStatsHelper::TimeStatsGlobal timeStats; |
| 103 | std::unordered_map<std::string, LayerRecord> timeStatsTracker; |
| 104 | }; |
| 105 | |
| 106 | } // namespace android |