blob: 8afc93e8dbed27fca398ca1abea641f022317073 [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
28namespace android {
29namespace scheduler {
30
31/**
32 * Class to encapsulate statistics about refresh rates that the display is using. When the power
33 * mode is set to HWC_POWER_MODE_NORMAL, SF is switching between refresh rates that are stored in
34 * the device's configs. Otherwise, we assume the HWC is running in power saving mode under the
35 * hood (eg. the device is in DOZE, or screen off mode).
36 */
37class RefreshRateStats {
38 static constexpr int64_t MS_PER_S = 1000;
39 static constexpr int64_t MS_PER_MIN = 60 * MS_PER_S;
40 static constexpr int64_t MS_PER_HOUR = 60 * MS_PER_MIN;
41 static constexpr int64_t MS_PER_DAY = 24 * MS_PER_HOUR;
42
43public:
Steven Thomas2bbaabe2019-08-28 16:08:35 -070044 RefreshRateStats(const RefreshRateConfigs& refreshRateConfigs, TimeStats& timeStats,
45 int currentConfigMode, int currentPowerMode)
46 : mRefreshRateConfigs(refreshRateConfigs),
47 mTimeStats(timeStats),
48 mCurrentConfigMode(currentConfigMode),
49 mCurrentPowerMode(currentPowerMode) {}
Ana Krulecb43429d2019-01-09 14:28:51 -080050
Steven Thomas2bbaabe2019-08-28 16:08:35 -070051 // Sets power mode.
Ana Krulecb43429d2019-01-09 14:28:51 -080052 void setPowerMode(int mode) {
53 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.
62 void setConfigMode(int mode) {
63 if (mCurrentConfigMode == mode) {
64 return;
65 }
66 flushTime();
67 mCurrentConfigMode = mode;
68 }
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.
81 for (const auto& [config, time] : mConfigModesTotalTime) {
82 totalTime[mRefreshRateConfigs.getRefreshRateFromConfigId(config).name] = 0;
Ana Krulecb43429d2019-01-09 14:28:51 -080083 }
Steven Thomas2bbaabe2019-08-28 16:08:35 -070084 for (const auto& [config, time] : mConfigModesTotalTime) {
85 totalTime[mRefreshRateConfigs.getRefreshRateFromConfigId(config).name] += time;
86 }
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;
112 if (mCurrentPowerMode == HWC_POWER_MODE_NORMAL) {
113 // 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;
118 fps = mRefreshRateConfigs.getRefreshRateFromConfigId(mCurrentConfigMode).fps;
119 } 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
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700142 int mCurrentConfigMode;
143 int32_t mCurrentPowerMode;
Ana Krulecb43429d2019-01-09 14:28:51 -0800144
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700145 std::unordered_map<int /* config */, int64_t /* duration in ms */> mConfigModesTotalTime;
146 int64_t mScreenOffTime = 0;
Ana Krulecb43429d2019-01-09 14:28:51 -0800147
Dominik Laskowski64536512019-03-28 09:53:04 -0700148 nsecs_t mPreviousRecordedTime = systemTime();
Ana Krulecb43429d2019-01-09 14:28:51 -0800149};
150
151} // namespace scheduler
Alec Mourifb571ea2019-01-24 18:42:10 -0800152} // namespace android