blob: 23ebb061e46a52c2c671e14f0a847e75dd2ad965 [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>
Ana Krulecb43429d2019-01-09 14:28:51 -080020#include <numeric>
21
Dominik Laskowski6eab42d2021-09-13 14:34:13 -070022#include <android-base/stringprintf.h>
23#include <ftl/small_map.h>
24#include <utils/Timers.h>
25
Dominik Laskowskif6b4ba62021-11-09 12:46:10 -080026#include <scheduler/Fps.h>
27
Ana Krulecb43429d2019-01-09 14:28:51 -080028#include "Scheduler/SchedulerUtils.h"
Alec Mourifb571ea2019-01-24 18:42:10 -080029#include "TimeStats/TimeStats.h"
Ana Krulecb43429d2019-01-09 14:28:51 -080030
Ady Abraham2139f732019-11-13 18:56:40 -080031namespace android::scheduler {
Ana Krulecb43429d2019-01-09 14:28:51 -080032
33/**
34 * Class to encapsulate statistics about refresh rates that the display is using. When the power
35 * mode is set to HWC_POWER_MODE_NORMAL, SF is switching between refresh rates that are stored in
36 * the device's configs. Otherwise, we assume the HWC is running in power saving mode under the
37 * hood (eg. the device is in DOZE, or screen off mode).
38 */
39class RefreshRateStats {
40 static constexpr int64_t MS_PER_S = 1000;
41 static constexpr int64_t MS_PER_MIN = 60 * MS_PER_S;
42 static constexpr int64_t MS_PER_HOUR = 60 * MS_PER_MIN;
43 static constexpr int64_t MS_PER_DAY = 24 * MS_PER_HOUR;
44
45public:
Dominik Laskowski6eab42d2021-09-13 14:34:13 -070046 // TODO(b/185535769): Inject clock to avoid sleeping in tests.
Marin Shalamanov68a94092020-11-24 17:48:00 +010047 RefreshRateStats(TimeStats& timeStats, Fps currentRefreshRate,
Peiyong Lin65248e02020-04-18 21:15:07 -070048 android::hardware::graphics::composer::hal::PowerMode currentPowerMode)
Marin Shalamanov68a94092020-11-24 17:48:00 +010049 : mTimeStats(timeStats),
50 mCurrentRefreshRate(currentRefreshRate),
Steven Thomas2bbaabe2019-08-28 16:08:35 -070051 mCurrentPowerMode(currentPowerMode) {}
Ana Krulecb43429d2019-01-09 14:28:51 -080052
Steven Thomas2bbaabe2019-08-28 16:08:35 -070053 // Sets power mode.
Peiyong Lin65248e02020-04-18 21:15:07 -070054 void setPowerMode(android::hardware::graphics::composer::hal::PowerMode mode) {
Ana Krulecb43429d2019-01-09 14:28:51 -080055 if (mCurrentPowerMode == mode) {
56 return;
57 }
Ana Krulecb43429d2019-01-09 14:28:51 -080058 flushTime();
59 mCurrentPowerMode = mode;
60 }
61
62 // Sets config mode. If the mode has changed, it records how much time was spent in the previous
63 // mode.
Marin Shalamanov68a94092020-11-24 17:48:00 +010064 void setRefreshRate(Fps currRefreshRate) {
Dominik Laskowski6eab42d2021-09-13 14:34:13 -070065 if (isApproxEqual(mCurrentRefreshRate, currRefreshRate)) {
Ana Krulecb43429d2019-01-09 14:28:51 -080066 return;
67 }
Marin Shalamanov993ddf42021-05-26 16:54:40 +020068 mTimeStats.incrementRefreshRateSwitches();
Ana Krulecb43429d2019-01-09 14:28:51 -080069 flushTime();
Marin Shalamanov68a94092020-11-24 17:48:00 +010070 mCurrentRefreshRate = currRefreshRate;
Ana Krulecb43429d2019-01-09 14:28:51 -080071 }
72
Dominik Laskowski6eab42d2021-09-13 14:34:13 -070073 // Maps stringified refresh rate to total time spent in that mode.
74 using TotalTimes = ftl::SmallMap<std::string, std::chrono::milliseconds, 3>;
75
76 TotalTimes getTotalTimes() {
Ana Krulecb43429d2019-01-09 14:28:51 -080077 // If the power mode is on, then we are probably switching between the config modes. If
78 // it's not then the screen is probably off. Make sure to flush times before printing
79 // them.
80 flushTime();
81
Dominik Laskowski6eab42d2021-09-13 14:34:13 -070082 TotalTimes totalTimes = ftl::init::map("ScreenOff", mScreenOffTime);
83 const auto zero = std::chrono::milliseconds::zero();
84
85 // Sum the times for modes that map to the same name, e.g. "60 Hz".
86 for (const auto& [fps, time] : mFpsTotalTimes) {
87 const auto string = to_string(fps);
88 const auto total = std::as_const(totalTimes).get(string).value_or(std::cref(zero));
89 totalTimes.emplace_or_replace(string, total.get() + time);
Ana Krulecb43429d2019-01-09 14:28:51 -080090 }
Dominik Laskowski6eab42d2021-09-13 14:34:13 -070091
92 return totalTimes;
Ana Krulecb43429d2019-01-09 14:28:51 -080093 }
94
95 // Traverses through the map of config modes and returns how long they've been running in easy
96 // to read format.
Dominik Laskowski98041832019-08-01 18:35:59 -070097 void dump(std::string& result) const {
98 std::ostringstream stream("+ Refresh rate: running time in seconds\n");
99
Dominik Laskowski64536512019-03-28 09:53:04 -0700100 for (const auto& [name, time] : const_cast<RefreshRateStats*>(this)->getTotalTimes()) {
101 stream << name << ": " << getDateFormatFromMs(time) << '\n';
Ana Krulecb43429d2019-01-09 14:28:51 -0800102 }
Dominik Laskowski98041832019-08-01 18:35:59 -0700103 result.append(stream.str());
Ana Krulecb43429d2019-01-09 14:28:51 -0800104 }
105
106private:
Ana Krulecb43429d2019-01-09 14:28:51 -0800107 // Calculates the time that passed in ms between the last time we recorded time and the time
108 // this method was called.
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700109 void flushTime() {
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700110 const nsecs_t currentTime = systemTime();
111 const nsecs_t timeElapsed = currentTime - mPreviousRecordedTime;
Ana Krulecb43429d2019-01-09 14:28:51 -0800112 mPreviousRecordedTime = currentTime;
113
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700114 const auto duration = std::chrono::milliseconds{ns2ms(timeElapsed)};
115 const auto zero = std::chrono::milliseconds::zero();
116
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700117 uint32_t fps = 0;
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700118
Peiyong Lin65248e02020-04-18 21:15:07 -0700119 if (mCurrentPowerMode == android::hardware::graphics::composer::hal::PowerMode::ON) {
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700120 // Normal power mode is counted under different config modes.
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700121 const auto total = std::as_const(mFpsTotalTimes)
122 .get(mCurrentRefreshRate)
123 .value_or(std::cref(zero));
124 mFpsTotalTimes.emplace_or_replace(mCurrentRefreshRate, total.get() + duration);
125
Marin Shalamanov68a94092020-11-24 17:48:00 +0100126 fps = static_cast<uint32_t>(mCurrentRefreshRate.getIntValue());
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700127 } else {
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700128 mScreenOffTime += duration;
Alec Mourifb571ea2019-01-24 18:42:10 -0800129 }
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700130 mTimeStats.recordRefreshRate(fps, timeElapsed);
Ana Krulecb43429d2019-01-09 14:28:51 -0800131 }
132
133 // Formats the time in milliseconds into easy to read format.
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700134 static std::string getDateFormatFromMs(std::chrono::milliseconds time) {
135 auto [days, dayRemainderMs] = std::div(static_cast<int64_t>(time.count()), MS_PER_DAY);
Ana Krulecb43429d2019-01-09 14:28:51 -0800136 auto [hours, hourRemainderMs] = std::div(dayRemainderMs, MS_PER_HOUR);
137 auto [mins, minsRemainderMs] = std::div(hourRemainderMs, MS_PER_MIN);
138 auto [sec, secRemainderMs] = std::div(minsRemainderMs, MS_PER_S);
139 return base::StringPrintf("%" PRId64 "d%02" PRId64 ":%02" PRId64 ":%02" PRId64
140 ".%03" PRId64,
141 days, hours, mins, sec, secRemainderMs);
142 }
143
Alec Mourifb571ea2019-01-24 18:42:10 -0800144 // Aggregate refresh rate statistics for telemetry.
Dominik Laskowski64536512019-03-28 09:53:04 -0700145 TimeStats& mTimeStats;
Alec Mourifb571ea2019-01-24 18:42:10 -0800146
Marin Shalamanov68a94092020-11-24 17:48:00 +0100147 Fps mCurrentRefreshRate;
Peiyong Lin65248e02020-04-18 21:15:07 -0700148 android::hardware::graphics::composer::hal::PowerMode mCurrentPowerMode;
Ana Krulecb43429d2019-01-09 14:28:51 -0800149
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700150 ftl::SmallMap<Fps, std::chrono::milliseconds, 2, FpsApproxEqual> mFpsTotalTimes;
151 std::chrono::milliseconds mScreenOffTime = std::chrono::milliseconds::zero();
Ana Krulecb43429d2019-01-09 14:28:51 -0800152
Dominik Laskowski64536512019-03-28 09:53:04 -0700153 nsecs_t mPreviousRecordedTime = systemTime();
Ana Krulecb43429d2019-01-09 14:28:51 -0800154};
155
Ady Abraham2139f732019-11-13 18:56:40 -0800156} // namespace android::scheduler