Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 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 | |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 19 | #include <chrono> |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 20 | #include <cinttypes> |
| 21 | #include <cstdlib> |
| 22 | #include <string> |
Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 23 | |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 24 | #include <android-base/stringprintf.h> |
Dominik Laskowski | 80872bd | 2022-11-15 11:34:33 -0500 | [diff] [blame] | 25 | #include <ftl/algorithm.h> |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 26 | #include <ftl/small_map.h> |
| 27 | #include <utils/Timers.h> |
| 28 | |
Dominik Laskowski | f6b4ba6 | 2021-11-09 12:46:10 -0800 | [diff] [blame] | 29 | #include <scheduler/Fps.h> |
| 30 | |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 31 | #include "DisplayHardware/Hal.h" |
Alec Mouri | fb571ea | 2019-01-24 18:42:10 -0800 | [diff] [blame] | 32 | #include "TimeStats/TimeStats.h" |
Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 33 | |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 34 | namespace android::scheduler { |
Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 35 | |
| 36 | /** |
| 37 | * Class to encapsulate statistics about refresh rates that the display is using. When the power |
| 38 | * mode is set to HWC_POWER_MODE_NORMAL, SF is switching between refresh rates that are stored in |
| 39 | * the device's configs. Otherwise, we assume the HWC is running in power saving mode under the |
| 40 | * hood (eg. the device is in DOZE, or screen off mode). |
| 41 | */ |
| 42 | class RefreshRateStats { |
| 43 | static constexpr int64_t MS_PER_S = 1000; |
| 44 | static constexpr int64_t MS_PER_MIN = 60 * MS_PER_S; |
| 45 | static constexpr int64_t MS_PER_HOUR = 60 * MS_PER_MIN; |
| 46 | static constexpr int64_t MS_PER_DAY = 24 * MS_PER_HOUR; |
| 47 | |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 48 | using PowerMode = android::hardware::graphics::composer::hal::PowerMode; |
| 49 | |
Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 50 | public: |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 51 | // TODO(b/185535769): Inject clock to avoid sleeping in tests. |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 52 | RefreshRateStats(TimeStats& timeStats, Fps currentRefreshRate, PowerMode currentPowerMode) |
Marin Shalamanov | 68a9409 | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 53 | : mTimeStats(timeStats), |
| 54 | mCurrentRefreshRate(currentRefreshRate), |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 55 | mCurrentPowerMode(currentPowerMode) {} |
Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 56 | |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 57 | void setPowerMode(PowerMode mode) { |
Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 58 | if (mCurrentPowerMode == mode) { |
| 59 | return; |
| 60 | } |
Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 61 | flushTime(); |
| 62 | mCurrentPowerMode = mode; |
| 63 | } |
| 64 | |
| 65 | // Sets config mode. If the mode has changed, it records how much time was spent in the previous |
| 66 | // mode. |
Marin Shalamanov | 68a9409 | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 67 | void setRefreshRate(Fps currRefreshRate) { |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 68 | if (isApproxEqual(mCurrentRefreshRate, currRefreshRate)) { |
Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 69 | return; |
| 70 | } |
Marin Shalamanov | 993ddf4 | 2021-05-26 16:54:40 +0200 | [diff] [blame] | 71 | mTimeStats.incrementRefreshRateSwitches(); |
Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 72 | flushTime(); |
Marin Shalamanov | 68a9409 | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 73 | mCurrentRefreshRate = currRefreshRate; |
Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 74 | } |
| 75 | |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 76 | // Maps stringified refresh rate to total time spent in that mode. |
| 77 | using TotalTimes = ftl::SmallMap<std::string, std::chrono::milliseconds, 3>; |
| 78 | |
| 79 | TotalTimes getTotalTimes() { |
Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 80 | // If the power mode is on, then we are probably switching between the config modes. If |
| 81 | // it's not then the screen is probably off. Make sure to flush times before printing |
| 82 | // them. |
| 83 | flushTime(); |
| 84 | |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 85 | TotalTimes totalTimes = ftl::init::map("ScreenOff", mScreenOffTime); |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 86 | |
| 87 | // Sum the times for modes that map to the same name, e.g. "60 Hz". |
| 88 | for (const auto& [fps, time] : mFpsTotalTimes) { |
| 89 | const auto string = to_string(fps); |
Dominik Laskowski | 80872bd | 2022-11-15 11:34:33 -0500 | [diff] [blame] | 90 | const auto total = std::as_const(totalTimes) |
| 91 | .get(string) |
| 92 | .or_else(ftl::static_ref<std::chrono::milliseconds>([] { |
| 93 | using namespace std::chrono_literals; |
| 94 | return 0ms; |
| 95 | })) |
| 96 | .value(); |
| 97 | |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 98 | totalTimes.emplace_or_replace(string, total.get() + time); |
Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 99 | } |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 100 | |
| 101 | return totalTimes; |
Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | // Traverses through the map of config modes and returns how long they've been running in easy |
| 105 | // to read format. |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 106 | void dump(std::string& result) const { |
| 107 | std::ostringstream stream("+ Refresh rate: running time in seconds\n"); |
| 108 | |
Dominik Laskowski | 6453651 | 2019-03-28 09:53:04 -0700 | [diff] [blame] | 109 | for (const auto& [name, time] : const_cast<RefreshRateStats*>(this)->getTotalTimes()) { |
| 110 | stream << name << ": " << getDateFormatFromMs(time) << '\n'; |
Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 111 | } |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 112 | result.append(stream.str()); |
Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | private: |
Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 116 | // Calculates the time that passed in ms between the last time we recorded time and the time |
| 117 | // this method was called. |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 118 | void flushTime() { |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 119 | const nsecs_t currentTime = systemTime(); |
| 120 | const nsecs_t timeElapsed = currentTime - mPreviousRecordedTime; |
Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 121 | mPreviousRecordedTime = currentTime; |
| 122 | |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 123 | const auto duration = std::chrono::milliseconds{ns2ms(timeElapsed)}; |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 124 | uint32_t fps = 0; |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 125 | |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 126 | if (mCurrentPowerMode == PowerMode::ON) { |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 127 | // Normal power mode is counted under different config modes. |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 128 | const auto total = std::as_const(mFpsTotalTimes) |
| 129 | .get(mCurrentRefreshRate) |
Dominik Laskowski | 80872bd | 2022-11-15 11:34:33 -0500 | [diff] [blame] | 130 | .or_else(ftl::static_ref<std::chrono::milliseconds>([] { |
| 131 | using namespace std::chrono_literals; |
| 132 | return 0ms; |
| 133 | })) |
| 134 | .value(); |
| 135 | |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 136 | mFpsTotalTimes.emplace_or_replace(mCurrentRefreshRate, total.get() + duration); |
| 137 | |
Marin Shalamanov | 68a9409 | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 138 | fps = static_cast<uint32_t>(mCurrentRefreshRate.getIntValue()); |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 139 | } else { |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 140 | mScreenOffTime += duration; |
Alec Mouri | fb571ea | 2019-01-24 18:42:10 -0800 | [diff] [blame] | 141 | } |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 142 | mTimeStats.recordRefreshRate(fps, timeElapsed); |
Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | // Formats the time in milliseconds into easy to read format. |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 146 | static std::string getDateFormatFromMs(std::chrono::milliseconds time) { |
| 147 | auto [days, dayRemainderMs] = std::div(static_cast<int64_t>(time.count()), MS_PER_DAY); |
Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 148 | auto [hours, hourRemainderMs] = std::div(dayRemainderMs, MS_PER_HOUR); |
| 149 | auto [mins, minsRemainderMs] = std::div(hourRemainderMs, MS_PER_MIN); |
| 150 | auto [sec, secRemainderMs] = std::div(minsRemainderMs, MS_PER_S); |
| 151 | return base::StringPrintf("%" PRId64 "d%02" PRId64 ":%02" PRId64 ":%02" PRId64 |
| 152 | ".%03" PRId64, |
| 153 | days, hours, mins, sec, secRemainderMs); |
| 154 | } |
| 155 | |
Alec Mouri | fb571ea | 2019-01-24 18:42:10 -0800 | [diff] [blame] | 156 | // Aggregate refresh rate statistics for telemetry. |
Dominik Laskowski | 6453651 | 2019-03-28 09:53:04 -0700 | [diff] [blame] | 157 | TimeStats& mTimeStats; |
Alec Mouri | fb571ea | 2019-01-24 18:42:10 -0800 | [diff] [blame] | 158 | |
Marin Shalamanov | 68a9409 | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 159 | Fps mCurrentRefreshRate; |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 160 | PowerMode mCurrentPowerMode; |
Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 161 | |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 162 | ftl::SmallMap<Fps, std::chrono::milliseconds, 2, FpsApproxEqual> mFpsTotalTimes; |
| 163 | std::chrono::milliseconds mScreenOffTime = std::chrono::milliseconds::zero(); |
Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 164 | |
Dominik Laskowski | 6453651 | 2019-03-28 09:53:04 -0700 | [diff] [blame] | 165 | nsecs_t mPreviousRecordedTime = systemTime(); |
Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 166 | }; |
| 167 | |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 168 | } // namespace android::scheduler |