blob: ed65bc607d7ed7865d4c6751be8c46aaf49df13b [file] [log] [blame]
Ana Krulecb43429d2019-01-09 14:28:51 -08001/*
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 Laskowski6eab42d2021-09-13 14:34:13 -070019#include <chrono>
Dominik Laskowskib0054a22022-03-03 09:03:06 -080020#include <cinttypes>
21#include <cstdlib>
22#include <string>
Ana Krulecb43429d2019-01-09 14:28:51 -080023
Dominik Laskowski6eab42d2021-09-13 14:34:13 -070024#include <android-base/stringprintf.h>
25#include <ftl/small_map.h>
26#include <utils/Timers.h>
27
Dominik Laskowskif6b4ba62021-11-09 12:46:10 -080028#include <scheduler/Fps.h>
29
Dominik Laskowskib0054a22022-03-03 09:03:06 -080030#include "DisplayHardware/Hal.h"
Alec Mourifb571ea2019-01-24 18:42:10 -080031#include "TimeStats/TimeStats.h"
Ana Krulecb43429d2019-01-09 14:28:51 -080032
Ady Abraham2139f732019-11-13 18:56:40 -080033namespace android::scheduler {
Ana Krulecb43429d2019-01-09 14:28:51 -080034
35/**
36 * Class to encapsulate statistics about refresh rates that the display is using. When the power
37 * mode is set to HWC_POWER_MODE_NORMAL, SF is switching between refresh rates that are stored in
38 * the device's configs. Otherwise, we assume the HWC is running in power saving mode under the
39 * hood (eg. the device is in DOZE, or screen off mode).
40 */
41class RefreshRateStats {
42 static constexpr int64_t MS_PER_S = 1000;
43 static constexpr int64_t MS_PER_MIN = 60 * MS_PER_S;
44 static constexpr int64_t MS_PER_HOUR = 60 * MS_PER_MIN;
45 static constexpr int64_t MS_PER_DAY = 24 * MS_PER_HOUR;
46
Dominik Laskowskib0054a22022-03-03 09:03:06 -080047 using PowerMode = android::hardware::graphics::composer::hal::PowerMode;
48
Ana Krulecb43429d2019-01-09 14:28:51 -080049public:
Dominik Laskowski6eab42d2021-09-13 14:34:13 -070050 // TODO(b/185535769): Inject clock to avoid sleeping in tests.
Dominik Laskowskib0054a22022-03-03 09:03:06 -080051 RefreshRateStats(TimeStats& timeStats, Fps currentRefreshRate, PowerMode currentPowerMode)
Marin Shalamanov68a94092020-11-24 17:48:00 +010052 : mTimeStats(timeStats),
53 mCurrentRefreshRate(currentRefreshRate),
Steven Thomas2bbaabe2019-08-28 16:08:35 -070054 mCurrentPowerMode(currentPowerMode) {}
Ana Krulecb43429d2019-01-09 14:28:51 -080055
Dominik Laskowskib0054a22022-03-03 09:03:06 -080056 void setPowerMode(PowerMode mode) {
Ana Krulecb43429d2019-01-09 14:28:51 -080057 if (mCurrentPowerMode == mode) {
58 return;
59 }
Ana Krulecb43429d2019-01-09 14:28:51 -080060 flushTime();
61 mCurrentPowerMode = mode;
62 }
63
64 // Sets config mode. If the mode has changed, it records how much time was spent in the previous
65 // mode.
Marin Shalamanov68a94092020-11-24 17:48:00 +010066 void setRefreshRate(Fps currRefreshRate) {
Dominik Laskowski6eab42d2021-09-13 14:34:13 -070067 if (isApproxEqual(mCurrentRefreshRate, currRefreshRate)) {
Ana Krulecb43429d2019-01-09 14:28:51 -080068 return;
69 }
Marin Shalamanov993ddf42021-05-26 16:54:40 +020070 mTimeStats.incrementRefreshRateSwitches();
Ana Krulecb43429d2019-01-09 14:28:51 -080071 flushTime();
Marin Shalamanov68a94092020-11-24 17:48:00 +010072 mCurrentRefreshRate = currRefreshRate;
Ana Krulecb43429d2019-01-09 14:28:51 -080073 }
74
Dominik Laskowski6eab42d2021-09-13 14:34:13 -070075 // Maps stringified refresh rate to total time spent in that mode.
76 using TotalTimes = ftl::SmallMap<std::string, std::chrono::milliseconds, 3>;
77
78 TotalTimes getTotalTimes() {
Ana Krulecb43429d2019-01-09 14:28:51 -080079 // If the power mode is on, then we are probably switching between the config modes. If
80 // it's not then the screen is probably off. Make sure to flush times before printing
81 // them.
82 flushTime();
83
Dominik Laskowski6eab42d2021-09-13 14:34:13 -070084 TotalTimes totalTimes = ftl::init::map("ScreenOff", mScreenOffTime);
85 const auto zero = std::chrono::milliseconds::zero();
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);
90 const auto total = std::as_const(totalTimes).get(string).value_or(std::cref(zero));
91 totalTimes.emplace_or_replace(string, total.get() + time);
Ana Krulecb43429d2019-01-09 14:28:51 -080092 }
Dominik Laskowski6eab42d2021-09-13 14:34:13 -070093
94 return totalTimes;
Ana Krulecb43429d2019-01-09 14:28:51 -080095 }
96
97 // Traverses through the map of config modes and returns how long they've been running in easy
98 // to read format.
Dominik Laskowski98041832019-08-01 18:35:59 -070099 void dump(std::string& result) const {
100 std::ostringstream stream("+ Refresh rate: running time in seconds\n");
101
Dominik Laskowski64536512019-03-28 09:53:04 -0700102 for (const auto& [name, time] : const_cast<RefreshRateStats*>(this)->getTotalTimes()) {
103 stream << name << ": " << getDateFormatFromMs(time) << '\n';
Ana Krulecb43429d2019-01-09 14:28:51 -0800104 }
Dominik Laskowski98041832019-08-01 18:35:59 -0700105 result.append(stream.str());
Ana Krulecb43429d2019-01-09 14:28:51 -0800106 }
107
108private:
Ana Krulecb43429d2019-01-09 14:28:51 -0800109 // Calculates the time that passed in ms between the last time we recorded time and the time
110 // this method was called.
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700111 void flushTime() {
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700112 const nsecs_t currentTime = systemTime();
113 const nsecs_t timeElapsed = currentTime - mPreviousRecordedTime;
Ana Krulecb43429d2019-01-09 14:28:51 -0800114 mPreviousRecordedTime = currentTime;
115
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700116 const auto duration = std::chrono::milliseconds{ns2ms(timeElapsed)};
117 const auto zero = std::chrono::milliseconds::zero();
118
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700119 uint32_t fps = 0;
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700120
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800121 if (mCurrentPowerMode == PowerMode::ON) {
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700122 // Normal power mode is counted under different config modes.
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700123 const auto total = std::as_const(mFpsTotalTimes)
124 .get(mCurrentRefreshRate)
125 .value_or(std::cref(zero));
126 mFpsTotalTimes.emplace_or_replace(mCurrentRefreshRate, total.get() + duration);
127
Marin Shalamanov68a94092020-11-24 17:48:00 +0100128 fps = static_cast<uint32_t>(mCurrentRefreshRate.getIntValue());
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700129 } else {
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700130 mScreenOffTime += duration;
Alec Mourifb571ea2019-01-24 18:42:10 -0800131 }
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700132 mTimeStats.recordRefreshRate(fps, timeElapsed);
Ana Krulecb43429d2019-01-09 14:28:51 -0800133 }
134
135 // Formats the time in milliseconds into easy to read format.
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700136 static std::string getDateFormatFromMs(std::chrono::milliseconds time) {
137 auto [days, dayRemainderMs] = std::div(static_cast<int64_t>(time.count()), MS_PER_DAY);
Ana Krulecb43429d2019-01-09 14:28:51 -0800138 auto [hours, hourRemainderMs] = std::div(dayRemainderMs, MS_PER_HOUR);
139 auto [mins, minsRemainderMs] = std::div(hourRemainderMs, MS_PER_MIN);
140 auto [sec, secRemainderMs] = std::div(minsRemainderMs, MS_PER_S);
141 return base::StringPrintf("%" PRId64 "d%02" PRId64 ":%02" PRId64 ":%02" PRId64
142 ".%03" PRId64,
143 days, hours, mins, sec, secRemainderMs);
144 }
145
Alec Mourifb571ea2019-01-24 18:42:10 -0800146 // Aggregate refresh rate statistics for telemetry.
Dominik Laskowski64536512019-03-28 09:53:04 -0700147 TimeStats& mTimeStats;
Alec Mourifb571ea2019-01-24 18:42:10 -0800148
Marin Shalamanov68a94092020-11-24 17:48:00 +0100149 Fps mCurrentRefreshRate;
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800150 PowerMode mCurrentPowerMode;
Ana Krulecb43429d2019-01-09 14:28:51 -0800151
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700152 ftl::SmallMap<Fps, std::chrono::milliseconds, 2, FpsApproxEqual> mFpsTotalTimes;
153 std::chrono::milliseconds mScreenOffTime = std::chrono::milliseconds::zero();
Ana Krulecb43429d2019-01-09 14:28:51 -0800154
Dominik Laskowski64536512019-03-28 09:53:04 -0700155 nsecs_t mPreviousRecordedTime = systemTime();
Ana Krulecb43429d2019-01-09 14:28:51 -0800156};
157
Ady Abraham2139f732019-11-13 18:56:40 -0800158} // namespace android::scheduler