blob: d9e7b371ff779cbdc3daeff4e4857acd0274c9bf [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
19#include <numeric>
20
21#include "Scheduler/RefreshRateConfigs.h"
22#include "Scheduler/SchedulerUtils.h"
Alec Mourifb571ea2019-01-24 18:42:10 -080023#include "TimeStats/TimeStats.h"
Ana Krulecb43429d2019-01-09 14:28:51 -080024
25#include "android-base/stringprintf.h"
26#include "utils/Timers.h"
27
Ady Abraham2139f732019-11-13 18:56:40 -080028namespace android::scheduler {
Ana Krulecb43429d2019-01-09 14:28:51 -080029
30/**
31 * Class to encapsulate statistics about refresh rates that the display is using. When the power
32 * mode is set to HWC_POWER_MODE_NORMAL, SF is switching between refresh rates that are stored in
33 * the device's configs. Otherwise, we assume the HWC is running in power saving mode under the
34 * hood (eg. the device is in DOZE, or screen off mode).
35 */
36class RefreshRateStats {
37 static constexpr int64_t MS_PER_S = 1000;
38 static constexpr int64_t MS_PER_MIN = 60 * MS_PER_S;
39 static constexpr int64_t MS_PER_HOUR = 60 * MS_PER_MIN;
40 static constexpr int64_t MS_PER_DAY = 24 * MS_PER_HOUR;
41
42public:
Steven Thomas2bbaabe2019-08-28 16:08:35 -070043 RefreshRateStats(const RefreshRateConfigs& refreshRateConfigs, TimeStats& timeStats,
Peiyong Lin65248e02020-04-18 21:15:07 -070044 HwcConfigIndexType currentConfigId,
45 android::hardware::graphics::composer::hal::PowerMode currentPowerMode)
Steven Thomas2bbaabe2019-08-28 16:08:35 -070046 : mRefreshRateConfigs(refreshRateConfigs),
47 mTimeStats(timeStats),
Ady Abraham2139f732019-11-13 18:56:40 -080048 mCurrentConfigMode(currentConfigId),
Steven Thomas2bbaabe2019-08-28 16:08:35 -070049 mCurrentPowerMode(currentPowerMode) {}
Ana Krulecb43429d2019-01-09 14:28:51 -080050
Steven Thomas2bbaabe2019-08-28 16:08:35 -070051 // Sets power mode.
Peiyong Lin65248e02020-04-18 21:15:07 -070052 void setPowerMode(android::hardware::graphics::composer::hal::PowerMode mode) {
Ana Krulecb43429d2019-01-09 14:28:51 -080053 if (mCurrentPowerMode == mode) {
54 return;
55 }
Ana Krulecb43429d2019-01-09 14:28:51 -080056 flushTime();
57 mCurrentPowerMode = mode;
58 }
59
60 // Sets config mode. If the mode has changed, it records how much time was spent in the previous
61 // mode.
Ady Abraham2139f732019-11-13 18:56:40 -080062 void setConfigMode(HwcConfigIndexType configId) {
63 if (mCurrentConfigMode == configId) {
Ana Krulecb43429d2019-01-09 14:28:51 -080064 return;
65 }
66 flushTime();
Ady Abraham2139f732019-11-13 18:56:40 -080067 mCurrentConfigMode = configId;
Ana Krulecb43429d2019-01-09 14:28:51 -080068 }
69
70 // Returns a map between human readable refresh rate and number of seconds the device spent in
71 // that mode.
72 std::unordered_map<std::string, int64_t> getTotalTimes() {
73 // If the power mode is on, then we are probably switching between the config modes. If
74 // it's not then the screen is probably off. Make sure to flush times before printing
75 // them.
76 flushTime();
77
78 std::unordered_map<std::string, int64_t> totalTime;
Steven Thomas2bbaabe2019-08-28 16:08:35 -070079 // Multiple configs may map to the same name, e.g. "60fps". Add the
80 // times for such configs together.
Ady Abraham2139f732019-11-13 18:56:40 -080081 for (const auto& [configId, time] : mConfigModesTotalTime) {
Ady Abrahamabc27602020-04-08 17:20:29 -070082 totalTime[mRefreshRateConfigs.getRefreshRateFromConfigId(configId).getName()] = 0;
Ana Krulecb43429d2019-01-09 14:28:51 -080083 }
Ady Abraham2139f732019-11-13 18:56:40 -080084 for (const auto& [configId, time] : mConfigModesTotalTime) {
Ady Abrahamabc27602020-04-08 17:20:29 -070085 totalTime[mRefreshRateConfigs.getRefreshRateFromConfigId(configId).getName()] += time;
Steven Thomas2bbaabe2019-08-28 16:08:35 -070086 }
87 totalTime["ScreenOff"] = mScreenOffTime;
Ana Krulecb43429d2019-01-09 14:28:51 -080088 return totalTime;
89 }
90
91 // Traverses through the map of config modes and returns how long they've been running in easy
92 // to read format.
Dominik Laskowski98041832019-08-01 18:35:59 -070093 void dump(std::string& result) const {
94 std::ostringstream stream("+ Refresh rate: running time in seconds\n");
95
Dominik Laskowski64536512019-03-28 09:53:04 -070096 for (const auto& [name, time] : const_cast<RefreshRateStats*>(this)->getTotalTimes()) {
97 stream << name << ": " << getDateFormatFromMs(time) << '\n';
Ana Krulecb43429d2019-01-09 14:28:51 -080098 }
Dominik Laskowski98041832019-08-01 18:35:59 -070099 result.append(stream.str());
Ana Krulecb43429d2019-01-09 14:28:51 -0800100 }
101
102private:
Ana Krulecb43429d2019-01-09 14:28:51 -0800103 // Calculates the time that passed in ms between the last time we recorded time and the time
104 // this method was called.
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700105 void flushTime() {
Ana Krulecb43429d2019-01-09 14:28:51 -0800106 nsecs_t currentTime = systemTime();
Alec Mourifb571ea2019-01-24 18:42:10 -0800107 nsecs_t timeElapsed = currentTime - mPreviousRecordedTime;
108 int64_t timeElapsedMs = ns2ms(timeElapsed);
Ana Krulecb43429d2019-01-09 14:28:51 -0800109 mPreviousRecordedTime = currentTime;
110
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700111 uint32_t fps = 0;
Peiyong Lin65248e02020-04-18 21:15:07 -0700112 if (mCurrentPowerMode == android::hardware::graphics::composer::hal::PowerMode::ON) {
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700113 // Normal power mode is counted under different config modes.
114 if (mConfigModesTotalTime.find(mCurrentConfigMode) == mConfigModesTotalTime.end()) {
115 mConfigModesTotalTime[mCurrentConfigMode] = 0;
Alec Mouri0a1cc962019-03-14 12:33:02 -0700116 }
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700117 mConfigModesTotalTime[mCurrentConfigMode] += timeElapsedMs;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800118 fps = static_cast<uint32_t>(std::round(
Ady Abrahamabc27602020-04-08 17:20:29 -0700119 mRefreshRateConfigs.getRefreshRateFromConfigId(mCurrentConfigMode).getFps()));
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700120 } else {
121 mScreenOffTime += timeElapsedMs;
Alec Mourifb571ea2019-01-24 18:42:10 -0800122 }
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700123 mTimeStats.recordRefreshRate(fps, timeElapsed);
Ana Krulecb43429d2019-01-09 14:28:51 -0800124 }
125
126 // Formats the time in milliseconds into easy to read format.
127 static std::string getDateFormatFromMs(int64_t timeMs) {
128 auto [days, dayRemainderMs] = std::div(timeMs, MS_PER_DAY);
129 auto [hours, hourRemainderMs] = std::div(dayRemainderMs, MS_PER_HOUR);
130 auto [mins, minsRemainderMs] = std::div(hourRemainderMs, MS_PER_MIN);
131 auto [sec, secRemainderMs] = std::div(minsRemainderMs, MS_PER_S);
132 return base::StringPrintf("%" PRId64 "d%02" PRId64 ":%02" PRId64 ":%02" PRId64
133 ".%03" PRId64,
134 days, hours, mins, sec, secRemainderMs);
135 }
136
137 // Keeps information about refresh rate configs that device has.
Dominik Laskowski64536512019-03-28 09:53:04 -0700138 const RefreshRateConfigs& mRefreshRateConfigs;
Ana Krulecb43429d2019-01-09 14:28:51 -0800139
Alec Mourifb571ea2019-01-24 18:42:10 -0800140 // Aggregate refresh rate statistics for telemetry.
Dominik Laskowski64536512019-03-28 09:53:04 -0700141 TimeStats& mTimeStats;
Alec Mourifb571ea2019-01-24 18:42:10 -0800142
Ady Abraham2139f732019-11-13 18:56:40 -0800143 HwcConfigIndexType mCurrentConfigMode;
Peiyong Lin65248e02020-04-18 21:15:07 -0700144 android::hardware::graphics::composer::hal::PowerMode mCurrentPowerMode;
Ana Krulecb43429d2019-01-09 14:28:51 -0800145
Ady Abraham2139f732019-11-13 18:56:40 -0800146 std::unordered_map<HwcConfigIndexType /* configId */, int64_t /* duration in ms */>
147 mConfigModesTotalTime;
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700148 int64_t mScreenOffTime = 0;
Ana Krulecb43429d2019-01-09 14:28:51 -0800149
Dominik Laskowski64536512019-03-28 09:53:04 -0700150 nsecs_t mPreviousRecordedTime = systemTime();
Ana Krulecb43429d2019-01-09 14:28:51 -0800151};
152
Ady Abraham2139f732019-11-13 18:56:40 -0800153} // namespace android::scheduler