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 | |
Yiwei Zhang | 3a226d2 | 2018-10-16 09:23:03 -0700 | [diff] [blame] | 22 | #include <hardware/hwcomposer_defs.h> |
| 23 | |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 24 | #include <ui/FenceTime.h> |
| 25 | |
| 26 | #include <utils/String16.h> |
| 27 | #include <utils/String8.h> |
| 28 | #include <utils/Vector.h> |
| 29 | |
Yiwei Zhang | c5f2c45 | 2018-05-08 16:31:56 -0700 | [diff] [blame] | 30 | #include <deque> |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 31 | #include <mutex> |
Yiwei Zhang | 8a4015c | 2018-05-08 16:03:47 -0700 | [diff] [blame] | 32 | #include <optional> |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 33 | #include <unordered_map> |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 34 | |
| 35 | using namespace android::surfaceflinger; |
| 36 | |
| 37 | namespace android { |
| 38 | class String8; |
| 39 | |
| 40 | class TimeStats { |
| 41 | // TODO(zzyiwei): Bound the timeStatsTracker with weighted LRU |
| 42 | // static const size_t MAX_NUM_LAYER_RECORDS = 200; |
| 43 | static const size_t MAX_NUM_TIME_RECORDS = 64; |
| 44 | |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 45 | struct FrameTime { |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 46 | uint64_t frameNumber = 0; |
| 47 | nsecs_t postTime = 0; |
| 48 | nsecs_t latchTime = 0; |
| 49 | nsecs_t acquireTime = 0; |
| 50 | nsecs_t desiredTime = 0; |
| 51 | nsecs_t presentTime = 0; |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | struct TimeRecord { |
| 55 | bool ready = false; |
| 56 | FrameTime frameTime; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 57 | std::shared_ptr<FenceTime> acquireFence; |
| 58 | std::shared_ptr<FenceTime> presentFence; |
| 59 | }; |
| 60 | |
| 61 | struct LayerRecord { |
| 62 | // This is the index in timeRecords, at which the timestamps for that |
| 63 | // specific frame are still not fully received. This is not waiting for |
| 64 | // fences to signal, but rather waiting to receive those fences/timestamps. |
| 65 | int32_t waitData = -1; |
Yiwei Zhang | eaeea06 | 2018-06-28 14:46:51 -0700 | [diff] [blame] | 66 | uint32_t droppedFrames = 0; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 67 | TimeRecord prevTimeRecord; |
Yiwei Zhang | c5f2c45 | 2018-05-08 16:31:56 -0700 | [diff] [blame] | 68 | std::deque<TimeRecord> timeRecords; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 69 | }; |
| 70 | |
Yiwei Zhang | 3a226d2 | 2018-10-16 09:23:03 -0700 | [diff] [blame] | 71 | struct PowerTime { |
| 72 | int32_t powerMode = HWC_POWER_MODE_OFF; |
| 73 | nsecs_t prevTime = 0; |
| 74 | }; |
| 75 | |
Yiwei Zhang | ce6ebc0 | 2018-10-20 12:42:38 -0700 | [diff] [blame^] | 76 | struct GlobalRecord { |
| 77 | nsecs_t prevPresentTime = 0; |
| 78 | std::deque<std::shared_ptr<FenceTime>> presentFences; |
| 79 | }; |
| 80 | |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 81 | public: |
| 82 | static TimeStats& getInstance(); |
| 83 | void parseArgs(bool asProto, const Vector<String16>& args, size_t& index, String8& result); |
| 84 | void incrementTotalFrames(); |
Yiwei Zhang | 621f9d4 | 2018-05-07 10:40:55 -0700 | [diff] [blame] | 85 | void incrementMissedFrames(); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 86 | void incrementClientCompositionFrames(); |
| 87 | |
| 88 | void setPostTime(const std::string& layerName, uint64_t frameNumber, nsecs_t postTime); |
| 89 | void setLatchTime(const std::string& layerName, uint64_t frameNumber, nsecs_t latchTime); |
| 90 | void setDesiredTime(const std::string& layerName, uint64_t frameNumber, nsecs_t desiredTime); |
| 91 | void setAcquireTime(const std::string& layerName, uint64_t frameNumber, nsecs_t acquireTime); |
| 92 | void setAcquireFence(const std::string& layerName, uint64_t frameNumber, |
| 93 | const std::shared_ptr<FenceTime>& acquireFence); |
| 94 | void setPresentTime(const std::string& layerName, uint64_t frameNumber, nsecs_t presentTime); |
| 95 | void setPresentFence(const std::string& layerName, uint64_t frameNumber, |
| 96 | const std::shared_ptr<FenceTime>& presentFence); |
Yiwei Zhang | eaeea06 | 2018-06-28 14:46:51 -0700 | [diff] [blame] | 97 | // On producer disconnect with BufferQueue. |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 98 | void onDisconnect(const std::string& layerName); |
Yiwei Zhang | dc22404 | 2018-10-18 15:34:00 -0700 | [diff] [blame] | 99 | // On layer tear down. |
| 100 | void onDestroy(const std::string& layerName); |
Yiwei Zhang | eaeea06 | 2018-06-28 14:46:51 -0700 | [diff] [blame] | 101 | // When SF is cleaning up the queue, clear the LayerRecord as well. |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 102 | void clearLayerRecord(const std::string& layerName); |
Yiwei Zhang | eaeea06 | 2018-06-28 14:46:51 -0700 | [diff] [blame] | 103 | // If SF skips or rejects a buffer, remove the corresponding TimeRecord. |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 104 | void removeTimeRecord(const std::string& layerName, uint64_t frameNumber); |
| 105 | |
Yiwei Zhang | 3a226d2 | 2018-10-16 09:23:03 -0700 | [diff] [blame] | 106 | void setPowerMode(int32_t powerMode); |
Yiwei Zhang | ce6ebc0 | 2018-10-20 12:42:38 -0700 | [diff] [blame^] | 107 | void setPresentFenceGlobal(const std::shared_ptr<FenceTime>& presentFence); |
Yiwei Zhang | 3a226d2 | 2018-10-16 09:23:03 -0700 | [diff] [blame] | 108 | |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 109 | private: |
| 110 | TimeStats() = default; |
| 111 | |
| 112 | bool recordReadyLocked(const std::string& layerName, TimeRecord* timeRecord); |
| 113 | void flushAvailableRecordsToStatsLocked(const std::string& layerName); |
Yiwei Zhang | 3a226d2 | 2018-10-16 09:23:03 -0700 | [diff] [blame] | 114 | void flushPowerTimeLocked(); |
Yiwei Zhang | ce6ebc0 | 2018-10-20 12:42:38 -0700 | [diff] [blame^] | 115 | void flushAvailableGlobalRecordsToStatsLocked(); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 116 | |
| 117 | void enable(); |
| 118 | void disable(); |
| 119 | void clear(); |
| 120 | bool isEnabled(); |
Yiwei Zhang | 8a4015c | 2018-05-08 16:03:47 -0700 | [diff] [blame] | 121 | void dump(bool asProto, std::optional<uint32_t> maxLayers, String8& result); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 122 | |
| 123 | std::atomic<bool> mEnabled = false; |
| 124 | std::mutex mMutex; |
Yiwei Zhang | dc22404 | 2018-10-18 15:34:00 -0700 | [diff] [blame] | 125 | TimeStatsHelper::TimeStatsGlobal mTimeStats; |
| 126 | std::unordered_map<std::string, LayerRecord> mTimeStatsTracker; |
Yiwei Zhang | 3a226d2 | 2018-10-16 09:23:03 -0700 | [diff] [blame] | 127 | PowerTime mPowerTime; |
Yiwei Zhang | ce6ebc0 | 2018-10-20 12:42:38 -0700 | [diff] [blame^] | 128 | GlobalRecord mGlobalRecord; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 129 | }; |
| 130 | |
| 131 | } // namespace android |