blob: 67e1b9c2ea9c4f050c02ca693d4766f8c0247c25 [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>
Dominik Laskowski80872bd2022-11-15 11:34:33 -050025#include <ftl/algorithm.h>
Dominik Laskowski6eab42d2021-09-13 14:34:13 -070026#include <ftl/small_map.h>
27#include <utils/Timers.h>
28
Dominik Laskowskif6b4ba62021-11-09 12:46:10 -080029#include <scheduler/Fps.h>
30
Dominik Laskowskib0054a22022-03-03 09:03:06 -080031#include "DisplayHardware/Hal.h"
Alec Mourifb571ea2019-01-24 18:42:10 -080032#include "TimeStats/TimeStats.h"
Ana Krulecb43429d2019-01-09 14:28:51 -080033
Ady Abraham2139f732019-11-13 18:56:40 -080034namespace android::scheduler {
Ana Krulecb43429d2019-01-09 14:28:51 -080035
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 */
42class 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 Laskowskib0054a22022-03-03 09:03:06 -080048 using PowerMode = android::hardware::graphics::composer::hal::PowerMode;
49
Ana Krulecb43429d2019-01-09 14:28:51 -080050public:
Dominik Laskowski6eab42d2021-09-13 14:34:13 -070051 // TODO(b/185535769): Inject clock to avoid sleeping in tests.
Dominik Laskowskib0054a22022-03-03 09:03:06 -080052 RefreshRateStats(TimeStats& timeStats, Fps currentRefreshRate, PowerMode currentPowerMode)
Marin Shalamanov68a94092020-11-24 17:48:00 +010053 : mTimeStats(timeStats),
54 mCurrentRefreshRate(currentRefreshRate),
Steven Thomas2bbaabe2019-08-28 16:08:35 -070055 mCurrentPowerMode(currentPowerMode) {}
Ana Krulecb43429d2019-01-09 14:28:51 -080056
Dominik Laskowskib0054a22022-03-03 09:03:06 -080057 void setPowerMode(PowerMode mode) {
Ana Krulecb43429d2019-01-09 14:28:51 -080058 if (mCurrentPowerMode == mode) {
59 return;
60 }
Ana Krulecb43429d2019-01-09 14:28:51 -080061 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 Shalamanov68a94092020-11-24 17:48:00 +010067 void setRefreshRate(Fps currRefreshRate) {
Dominik Laskowski6eab42d2021-09-13 14:34:13 -070068 if (isApproxEqual(mCurrentRefreshRate, currRefreshRate)) {
Ana Krulecb43429d2019-01-09 14:28:51 -080069 return;
70 }
Marin Shalamanov993ddf42021-05-26 16:54:40 +020071 mTimeStats.incrementRefreshRateSwitches();
Ana Krulecb43429d2019-01-09 14:28:51 -080072 flushTime();
Marin Shalamanov68a94092020-11-24 17:48:00 +010073 mCurrentRefreshRate = currRefreshRate;
Ana Krulecb43429d2019-01-09 14:28:51 -080074 }
75
Dominik Laskowski6eab42d2021-09-13 14:34:13 -070076 // 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 Krulecb43429d2019-01-09 14:28:51 -080080 // 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 Laskowski6eab42d2021-09-13 14:34:13 -070085 TotalTimes totalTimes = ftl::init::map("ScreenOff", mScreenOffTime);
Dominik Laskowski6eab42d2021-09-13 14:34:13 -070086
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 Laskowski80872bd2022-11-15 11:34:33 -050090 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 Laskowski6eab42d2021-09-13 14:34:13 -070098 totalTimes.emplace_or_replace(string, total.get() + time);
Ana Krulecb43429d2019-01-09 14:28:51 -080099 }
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700100
101 return totalTimes;
Ana Krulecb43429d2019-01-09 14:28:51 -0800102 }
103
104 // Traverses through the map of config modes and returns how long they've been running in easy
105 // to read format.
Dominik Laskowski98041832019-08-01 18:35:59 -0700106 void dump(std::string& result) const {
107 std::ostringstream stream("+ Refresh rate: running time in seconds\n");
108
Dominik Laskowski64536512019-03-28 09:53:04 -0700109 for (const auto& [name, time] : const_cast<RefreshRateStats*>(this)->getTotalTimes()) {
110 stream << name << ": " << getDateFormatFromMs(time) << '\n';
Ana Krulecb43429d2019-01-09 14:28:51 -0800111 }
Dominik Laskowski98041832019-08-01 18:35:59 -0700112 result.append(stream.str());
Ana Krulecb43429d2019-01-09 14:28:51 -0800113 }
114
115private:
Ana Krulecb43429d2019-01-09 14:28:51 -0800116 // Calculates the time that passed in ms between the last time we recorded time and the time
117 // this method was called.
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700118 void flushTime() {
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700119 const nsecs_t currentTime = systemTime();
120 const nsecs_t timeElapsed = currentTime - mPreviousRecordedTime;
Ana Krulecb43429d2019-01-09 14:28:51 -0800121 mPreviousRecordedTime = currentTime;
122
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700123 const auto duration = std::chrono::milliseconds{ns2ms(timeElapsed)};
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700124 uint32_t fps = 0;
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700125
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800126 if (mCurrentPowerMode == PowerMode::ON) {
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700127 // Normal power mode is counted under different config modes.
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700128 const auto total = std::as_const(mFpsTotalTimes)
129 .get(mCurrentRefreshRate)
Dominik Laskowski80872bd2022-11-15 11:34:33 -0500130 .or_else(ftl::static_ref<std::chrono::milliseconds>([] {
131 using namespace std::chrono_literals;
132 return 0ms;
133 }))
134 .value();
135
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700136 mFpsTotalTimes.emplace_or_replace(mCurrentRefreshRate, total.get() + duration);
137
Marin Shalamanov68a94092020-11-24 17:48:00 +0100138 fps = static_cast<uint32_t>(mCurrentRefreshRate.getIntValue());
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700139 } else {
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700140 mScreenOffTime += duration;
Alec Mourifb571ea2019-01-24 18:42:10 -0800141 }
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700142 mTimeStats.recordRefreshRate(fps, timeElapsed);
Ana Krulecb43429d2019-01-09 14:28:51 -0800143 }
144
145 // Formats the time in milliseconds into easy to read format.
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700146 static std::string getDateFormatFromMs(std::chrono::milliseconds time) {
147 auto [days, dayRemainderMs] = std::div(static_cast<int64_t>(time.count()), MS_PER_DAY);
Ana Krulecb43429d2019-01-09 14:28:51 -0800148 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 Mourifb571ea2019-01-24 18:42:10 -0800156 // Aggregate refresh rate statistics for telemetry.
Dominik Laskowski64536512019-03-28 09:53:04 -0700157 TimeStats& mTimeStats;
Alec Mourifb571ea2019-01-24 18:42:10 -0800158
Marin Shalamanov68a94092020-11-24 17:48:00 +0100159 Fps mCurrentRefreshRate;
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800160 PowerMode mCurrentPowerMode;
Ana Krulecb43429d2019-01-09 14:28:51 -0800161
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700162 ftl::SmallMap<Fps, std::chrono::milliseconds, 2, FpsApproxEqual> mFpsTotalTimes;
163 std::chrono::milliseconds mScreenOffTime = std::chrono::milliseconds::zero();
Ana Krulecb43429d2019-01-09 14:28:51 -0800164
Dominik Laskowski64536512019-03-28 09:53:04 -0700165 nsecs_t mPreviousRecordedTime = systemTime();
Ana Krulecb43429d2019-01-09 14:28:51 -0800166};
167
Ady Abraham2139f732019-11-13 18:56:40 -0800168} // namespace android::scheduler