blob: 80aa96fd633ebaebb0c451535306cfeca5c90627 [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
Marin Shalamanov68a94092020-11-24 17:48:00 +010026#include "Fps.h"
Ana Krulecb43429d2019-01-09 14:28:51 -080027#include "Scheduler/SchedulerUtils.h"
Alec Mourifb571ea2019-01-24 18:42:10 -080028#include "TimeStats/TimeStats.h"
Ana Krulecb43429d2019-01-09 14:28:51 -080029
Ady Abraham2139f732019-11-13 18:56:40 -080030namespace android::scheduler {
Ana Krulecb43429d2019-01-09 14:28:51 -080031
32/**
33 * Class to encapsulate statistics about refresh rates that the display is using. When the power
34 * mode is set to HWC_POWER_MODE_NORMAL, SF is switching between refresh rates that are stored in
35 * the device's configs. Otherwise, we assume the HWC is running in power saving mode under the
36 * hood (eg. the device is in DOZE, or screen off mode).
37 */
38class RefreshRateStats {
39 static constexpr int64_t MS_PER_S = 1000;
40 static constexpr int64_t MS_PER_MIN = 60 * MS_PER_S;
41 static constexpr int64_t MS_PER_HOUR = 60 * MS_PER_MIN;
42 static constexpr int64_t MS_PER_DAY = 24 * MS_PER_HOUR;
43
44public:
Dominik Laskowski6eab42d2021-09-13 14:34:13 -070045 // TODO(b/185535769): Inject clock to avoid sleeping in tests.
Marin Shalamanov68a94092020-11-24 17:48:00 +010046 RefreshRateStats(TimeStats& timeStats, Fps currentRefreshRate,
Peiyong Lin65248e02020-04-18 21:15:07 -070047 android::hardware::graphics::composer::hal::PowerMode currentPowerMode)
Marin Shalamanov68a94092020-11-24 17:48:00 +010048 : mTimeStats(timeStats),
49 mCurrentRefreshRate(currentRefreshRate),
Steven Thomas2bbaabe2019-08-28 16:08:35 -070050 mCurrentPowerMode(currentPowerMode) {}
Ana Krulecb43429d2019-01-09 14:28:51 -080051
Steven Thomas2bbaabe2019-08-28 16:08:35 -070052 // Sets power mode.
Peiyong Lin65248e02020-04-18 21:15:07 -070053 void setPowerMode(android::hardware::graphics::composer::hal::PowerMode mode) {
Ana Krulecb43429d2019-01-09 14:28:51 -080054 if (mCurrentPowerMode == mode) {
55 return;
56 }
Ana Krulecb43429d2019-01-09 14:28:51 -080057 flushTime();
58 mCurrentPowerMode = mode;
59 }
60
61 // Sets config mode. If the mode has changed, it records how much time was spent in the previous
62 // mode.
Marin Shalamanov68a94092020-11-24 17:48:00 +010063 void setRefreshRate(Fps currRefreshRate) {
Dominik Laskowski6eab42d2021-09-13 14:34:13 -070064 if (isApproxEqual(mCurrentRefreshRate, currRefreshRate)) {
Ana Krulecb43429d2019-01-09 14:28:51 -080065 return;
66 }
Marin Shalamanov993ddf42021-05-26 16:54:40 +020067 mTimeStats.incrementRefreshRateSwitches();
Ana Krulecb43429d2019-01-09 14:28:51 -080068 flushTime();
Marin Shalamanov68a94092020-11-24 17:48:00 +010069 mCurrentRefreshRate = currRefreshRate;
Ana Krulecb43429d2019-01-09 14:28:51 -080070 }
71
Dominik Laskowski6eab42d2021-09-13 14:34:13 -070072 // Maps stringified refresh rate to total time spent in that mode.
73 using TotalTimes = ftl::SmallMap<std::string, std::chrono::milliseconds, 3>;
74
75 TotalTimes getTotalTimes() {
Ana Krulecb43429d2019-01-09 14:28:51 -080076 // If the power mode is on, then we are probably switching between the config modes. If
77 // it's not then the screen is probably off. Make sure to flush times before printing
78 // them.
79 flushTime();
80
Dominik Laskowski6eab42d2021-09-13 14:34:13 -070081 TotalTimes totalTimes = ftl::init::map("ScreenOff", mScreenOffTime);
82 const auto zero = std::chrono::milliseconds::zero();
83
84 // Sum the times for modes that map to the same name, e.g. "60 Hz".
85 for (const auto& [fps, time] : mFpsTotalTimes) {
86 const auto string = to_string(fps);
87 const auto total = std::as_const(totalTimes).get(string).value_or(std::cref(zero));
88 totalTimes.emplace_or_replace(string, total.get() + time);
Ana Krulecb43429d2019-01-09 14:28:51 -080089 }
Dominik Laskowski6eab42d2021-09-13 14:34:13 -070090
91 return totalTimes;
Ana Krulecb43429d2019-01-09 14:28:51 -080092 }
93
94 // Traverses through the map of config modes and returns how long they've been running in easy
95 // to read format.
Dominik Laskowski98041832019-08-01 18:35:59 -070096 void dump(std::string& result) const {
97 std::ostringstream stream("+ Refresh rate: running time in seconds\n");
98
Dominik Laskowski64536512019-03-28 09:53:04 -070099 for (const auto& [name, time] : const_cast<RefreshRateStats*>(this)->getTotalTimes()) {
100 stream << name << ": " << getDateFormatFromMs(time) << '\n';
Ana Krulecb43429d2019-01-09 14:28:51 -0800101 }
Dominik Laskowski98041832019-08-01 18:35:59 -0700102 result.append(stream.str());
Ana Krulecb43429d2019-01-09 14:28:51 -0800103 }
104
105private:
Ana Krulecb43429d2019-01-09 14:28:51 -0800106 // Calculates the time that passed in ms between the last time we recorded time and the time
107 // this method was called.
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700108 void flushTime() {
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700109 const nsecs_t currentTime = systemTime();
110 const nsecs_t timeElapsed = currentTime - mPreviousRecordedTime;
Ana Krulecb43429d2019-01-09 14:28:51 -0800111 mPreviousRecordedTime = currentTime;
112
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700113 const auto duration = std::chrono::milliseconds{ns2ms(timeElapsed)};
114 const auto zero = std::chrono::milliseconds::zero();
115
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700116 uint32_t fps = 0;
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700117
Peiyong Lin65248e02020-04-18 21:15:07 -0700118 if (mCurrentPowerMode == android::hardware::graphics::composer::hal::PowerMode::ON) {
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700119 // Normal power mode is counted under different config modes.
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700120 const auto total = std::as_const(mFpsTotalTimes)
121 .get(mCurrentRefreshRate)
122 .value_or(std::cref(zero));
123 mFpsTotalTimes.emplace_or_replace(mCurrentRefreshRate, total.get() + duration);
124
Marin Shalamanov68a94092020-11-24 17:48:00 +0100125 fps = static_cast<uint32_t>(mCurrentRefreshRate.getIntValue());
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700126 } else {
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700127 mScreenOffTime += duration;
Alec Mourifb571ea2019-01-24 18:42:10 -0800128 }
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700129 mTimeStats.recordRefreshRate(fps, timeElapsed);
Ana Krulecb43429d2019-01-09 14:28:51 -0800130 }
131
132 // Formats the time in milliseconds into easy to read format.
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700133 static std::string getDateFormatFromMs(std::chrono::milliseconds time) {
134 auto [days, dayRemainderMs] = std::div(static_cast<int64_t>(time.count()), MS_PER_DAY);
Ana Krulecb43429d2019-01-09 14:28:51 -0800135 auto [hours, hourRemainderMs] = std::div(dayRemainderMs, MS_PER_HOUR);
136 auto [mins, minsRemainderMs] = std::div(hourRemainderMs, MS_PER_MIN);
137 auto [sec, secRemainderMs] = std::div(minsRemainderMs, MS_PER_S);
138 return base::StringPrintf("%" PRId64 "d%02" PRId64 ":%02" PRId64 ":%02" PRId64
139 ".%03" PRId64,
140 days, hours, mins, sec, secRemainderMs);
141 }
142
Alec Mourifb571ea2019-01-24 18:42:10 -0800143 // Aggregate refresh rate statistics for telemetry.
Dominik Laskowski64536512019-03-28 09:53:04 -0700144 TimeStats& mTimeStats;
Alec Mourifb571ea2019-01-24 18:42:10 -0800145
Marin Shalamanov68a94092020-11-24 17:48:00 +0100146 Fps mCurrentRefreshRate;
Peiyong Lin65248e02020-04-18 21:15:07 -0700147 android::hardware::graphics::composer::hal::PowerMode mCurrentPowerMode;
Ana Krulecb43429d2019-01-09 14:28:51 -0800148
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700149 ftl::SmallMap<Fps, std::chrono::milliseconds, 2, FpsApproxEqual> mFpsTotalTimes;
150 std::chrono::milliseconds mScreenOffTime = std::chrono::milliseconds::zero();
Ana Krulecb43429d2019-01-09 14:28:51 -0800151
Dominik Laskowski64536512019-03-28 09:53:04 -0700152 nsecs_t mPreviousRecordedTime = systemTime();
Ana Krulecb43429d2019-01-09 14:28:51 -0800153};
154
Ady Abraham2139f732019-11-13 18:56:40 -0800155} // namespace android::scheduler