blob: 66d4a0305fd55b5f3175a4e91a15c303fef6b9bf [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,
Ady Abraham2139f732019-11-13 18:56:40 -080044 HwcConfigIndexType currentConfigId, int currentPowerMode)
Steven Thomas2bbaabe2019-08-28 16:08:35 -070045 : mRefreshRateConfigs(refreshRateConfigs),
46 mTimeStats(timeStats),
Ady Abraham2139f732019-11-13 18:56:40 -080047 mCurrentConfigMode(currentConfigId),
Steven Thomas2bbaabe2019-08-28 16:08:35 -070048 mCurrentPowerMode(currentPowerMode) {}
Ana Krulecb43429d2019-01-09 14:28:51 -080049
Steven Thomas2bbaabe2019-08-28 16:08:35 -070050 // Sets power mode.
Ana Krulecb43429d2019-01-09 14:28:51 -080051 void setPowerMode(int mode) {
52 if (mCurrentPowerMode == mode) {
53 return;
54 }
Ana Krulecb43429d2019-01-09 14:28:51 -080055 flushTime();
56 mCurrentPowerMode = mode;
57 }
58
59 // Sets config mode. If the mode has changed, it records how much time was spent in the previous
60 // mode.
Ady Abraham2139f732019-11-13 18:56:40 -080061 void setConfigMode(HwcConfigIndexType configId) {
62 if (mCurrentConfigMode == configId) {
Ana Krulecb43429d2019-01-09 14:28:51 -080063 return;
64 }
65 flushTime();
Ady Abraham2139f732019-11-13 18:56:40 -080066 mCurrentConfigMode = configId;
Ana Krulecb43429d2019-01-09 14:28:51 -080067 }
68
69 // Returns a map between human readable refresh rate and number of seconds the device spent in
70 // that mode.
71 std::unordered_map<std::string, int64_t> getTotalTimes() {
72 // If the power mode is on, then we are probably switching between the config modes. If
73 // it's not then the screen is probably off. Make sure to flush times before printing
74 // them.
75 flushTime();
76
77 std::unordered_map<std::string, int64_t> totalTime;
Steven Thomas2bbaabe2019-08-28 16:08:35 -070078 // Multiple configs may map to the same name, e.g. "60fps". Add the
79 // times for such configs together.
Ady Abraham2139f732019-11-13 18:56:40 -080080 for (const auto& [configId, time] : mConfigModesTotalTime) {
Ady Abrahamabc27602020-04-08 17:20:29 -070081 totalTime[mRefreshRateConfigs.getRefreshRateFromConfigId(configId).getName()] = 0;
Ana Krulecb43429d2019-01-09 14:28:51 -080082 }
Ady Abraham2139f732019-11-13 18:56:40 -080083 for (const auto& [configId, time] : mConfigModesTotalTime) {
Ady Abrahamabc27602020-04-08 17:20:29 -070084 totalTime[mRefreshRateConfigs.getRefreshRateFromConfigId(configId).getName()] += time;
Steven Thomas2bbaabe2019-08-28 16:08:35 -070085 }
86 totalTime["ScreenOff"] = mScreenOffTime;
Ana Krulecb43429d2019-01-09 14:28:51 -080087 return totalTime;
88 }
89
90 // Traverses through the map of config modes and returns how long they've been running in easy
91 // to read format.
Dominik Laskowski98041832019-08-01 18:35:59 -070092 void dump(std::string& result) const {
93 std::ostringstream stream("+ Refresh rate: running time in seconds\n");
94
Dominik Laskowski64536512019-03-28 09:53:04 -070095 for (const auto& [name, time] : const_cast<RefreshRateStats*>(this)->getTotalTimes()) {
96 stream << name << ": " << getDateFormatFromMs(time) << '\n';
Ana Krulecb43429d2019-01-09 14:28:51 -080097 }
Dominik Laskowski98041832019-08-01 18:35:59 -070098 result.append(stream.str());
Ana Krulecb43429d2019-01-09 14:28:51 -080099 }
100
101private:
Ana Krulecb43429d2019-01-09 14:28:51 -0800102 // Calculates the time that passed in ms between the last time we recorded time and the time
103 // this method was called.
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700104 void flushTime() {
Ana Krulecb43429d2019-01-09 14:28:51 -0800105 nsecs_t currentTime = systemTime();
Alec Mourifb571ea2019-01-24 18:42:10 -0800106 nsecs_t timeElapsed = currentTime - mPreviousRecordedTime;
107 int64_t timeElapsedMs = ns2ms(timeElapsed);
Ana Krulecb43429d2019-01-09 14:28:51 -0800108 mPreviousRecordedTime = currentTime;
109
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700110 uint32_t fps = 0;
111 if (mCurrentPowerMode == HWC_POWER_MODE_NORMAL) {
112 // Normal power mode is counted under different config modes.
113 if (mConfigModesTotalTime.find(mCurrentConfigMode) == mConfigModesTotalTime.end()) {
114 mConfigModesTotalTime[mCurrentConfigMode] = 0;
Alec Mouri0a1cc962019-03-14 12:33:02 -0700115 }
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700116 mConfigModesTotalTime[mCurrentConfigMode] += timeElapsedMs;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800117 fps = static_cast<uint32_t>(std::round(
Ady Abrahamabc27602020-04-08 17:20:29 -0700118 mRefreshRateConfigs.getRefreshRateFromConfigId(mCurrentConfigMode).getFps()));
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700119 } else {
120 mScreenOffTime += timeElapsedMs;
Alec Mourifb571ea2019-01-24 18:42:10 -0800121 }
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700122 mTimeStats.recordRefreshRate(fps, timeElapsed);
Ana Krulecb43429d2019-01-09 14:28:51 -0800123 }
124
125 // Formats the time in milliseconds into easy to read format.
126 static std::string getDateFormatFromMs(int64_t timeMs) {
127 auto [days, dayRemainderMs] = std::div(timeMs, MS_PER_DAY);
128 auto [hours, hourRemainderMs] = std::div(dayRemainderMs, MS_PER_HOUR);
129 auto [mins, minsRemainderMs] = std::div(hourRemainderMs, MS_PER_MIN);
130 auto [sec, secRemainderMs] = std::div(minsRemainderMs, MS_PER_S);
131 return base::StringPrintf("%" PRId64 "d%02" PRId64 ":%02" PRId64 ":%02" PRId64
132 ".%03" PRId64,
133 days, hours, mins, sec, secRemainderMs);
134 }
135
136 // Keeps information about refresh rate configs that device has.
Dominik Laskowski64536512019-03-28 09:53:04 -0700137 const RefreshRateConfigs& mRefreshRateConfigs;
Ana Krulecb43429d2019-01-09 14:28:51 -0800138
Alec Mourifb571ea2019-01-24 18:42:10 -0800139 // Aggregate refresh rate statistics for telemetry.
Dominik Laskowski64536512019-03-28 09:53:04 -0700140 TimeStats& mTimeStats;
Alec Mourifb571ea2019-01-24 18:42:10 -0800141
Ady Abraham2139f732019-11-13 18:56:40 -0800142 HwcConfigIndexType mCurrentConfigMode;
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700143 int32_t mCurrentPowerMode;
Ana Krulecb43429d2019-01-09 14:28:51 -0800144
Ady Abraham2139f732019-11-13 18:56:40 -0800145 std::unordered_map<HwcConfigIndexType /* configId */, int64_t /* duration in ms */>
146 mConfigModesTotalTime;
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700147 int64_t mScreenOffTime = 0;
Ana Krulecb43429d2019-01-09 14:28:51 -0800148
Dominik Laskowski64536512019-03-28 09:53:04 -0700149 nsecs_t mPreviousRecordedTime = systemTime();
Ana Krulecb43429d2019-01-09 14:28:51 -0800150};
151
Ady Abraham2139f732019-11-13 18:56:40 -0800152} // namespace android::scheduler