blob: 80f4665150a9003499521d76a0e8330cad2dd951 [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
Marin Shalamanov68a94092020-11-24 17:48:00 +010021#include "Fps.h"
Ana Krulecb43429d2019-01-09 14:28:51 -080022#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:
Marin Shalamanov68a94092020-11-24 17:48:00 +010043 RefreshRateStats(TimeStats& timeStats, Fps currentRefreshRate,
Peiyong Lin65248e02020-04-18 21:15:07 -070044 android::hardware::graphics::composer::hal::PowerMode currentPowerMode)
Marin Shalamanov68a94092020-11-24 17:48:00 +010045 : mTimeStats(timeStats),
46 mCurrentRefreshRate(currentRefreshRate),
Steven Thomas2bbaabe2019-08-28 16:08:35 -070047 mCurrentPowerMode(currentPowerMode) {}
Ana Krulecb43429d2019-01-09 14:28:51 -080048
Steven Thomas2bbaabe2019-08-28 16:08:35 -070049 // Sets power mode.
Peiyong Lin65248e02020-04-18 21:15:07 -070050 void setPowerMode(android::hardware::graphics::composer::hal::PowerMode mode) {
Ana Krulecb43429d2019-01-09 14:28:51 -080051 if (mCurrentPowerMode == mode) {
52 return;
53 }
Ana Krulecb43429d2019-01-09 14:28:51 -080054 flushTime();
55 mCurrentPowerMode = mode;
56 }
57
58 // Sets config mode. If the mode has changed, it records how much time was spent in the previous
59 // mode.
Marin Shalamanov68a94092020-11-24 17:48:00 +010060 void setRefreshRate(Fps currRefreshRate) {
61 if (mCurrentRefreshRate.equalsWithMargin(currRefreshRate)) {
Ana Krulecb43429d2019-01-09 14:28:51 -080062 return;
63 }
64 flushTime();
Marin Shalamanov68a94092020-11-24 17:48:00 +010065 mCurrentRefreshRate = currRefreshRate;
Ana Krulecb43429d2019-01-09 14:28:51 -080066 }
67
68 // Returns a map between human readable refresh rate and number of seconds the device spent in
69 // that mode.
70 std::unordered_map<std::string, int64_t> getTotalTimes() {
71 // If the power mode is on, then we are probably switching between the config modes. If
72 // it's not then the screen is probably off. Make sure to flush times before printing
73 // them.
74 flushTime();
75
76 std::unordered_map<std::string, int64_t> totalTime;
Steven Thomas2bbaabe2019-08-28 16:08:35 -070077 // Multiple configs may map to the same name, e.g. "60fps". Add the
78 // times for such configs together.
Ady Abraham2139f732019-11-13 18:56:40 -080079 for (const auto& [configId, time] : mConfigModesTotalTime) {
Marin Shalamanov68a94092020-11-24 17:48:00 +010080 totalTime[to_string(configId)] = 0;
Ana Krulecb43429d2019-01-09 14:28:51 -080081 }
Ady Abraham2139f732019-11-13 18:56:40 -080082 for (const auto& [configId, time] : mConfigModesTotalTime) {
Marin Shalamanov68a94092020-11-24 17:48:00 +010083 totalTime[to_string(configId)] += time;
Steven Thomas2bbaabe2019-08-28 16:08:35 -070084 }
85 totalTime["ScreenOff"] = mScreenOffTime;
Ana Krulecb43429d2019-01-09 14:28:51 -080086 return totalTime;
87 }
88
89 // Traverses through the map of config modes and returns how long they've been running in easy
90 // to read format.
Dominik Laskowski98041832019-08-01 18:35:59 -070091 void dump(std::string& result) const {
92 std::ostringstream stream("+ Refresh rate: running time in seconds\n");
93
Dominik Laskowski64536512019-03-28 09:53:04 -070094 for (const auto& [name, time] : const_cast<RefreshRateStats*>(this)->getTotalTimes()) {
95 stream << name << ": " << getDateFormatFromMs(time) << '\n';
Ana Krulecb43429d2019-01-09 14:28:51 -080096 }
Dominik Laskowski98041832019-08-01 18:35:59 -070097 result.append(stream.str());
Ana Krulecb43429d2019-01-09 14:28:51 -080098 }
99
100private:
Ana Krulecb43429d2019-01-09 14:28:51 -0800101 // Calculates the time that passed in ms between the last time we recorded time and the time
102 // this method was called.
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700103 void flushTime() {
Ana Krulecb43429d2019-01-09 14:28:51 -0800104 nsecs_t currentTime = systemTime();
Alec Mourifb571ea2019-01-24 18:42:10 -0800105 nsecs_t timeElapsed = currentTime - mPreviousRecordedTime;
106 int64_t timeElapsedMs = ns2ms(timeElapsed);
Ana Krulecb43429d2019-01-09 14:28:51 -0800107 mPreviousRecordedTime = currentTime;
108
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700109 uint32_t fps = 0;
Peiyong Lin65248e02020-04-18 21:15:07 -0700110 if (mCurrentPowerMode == android::hardware::graphics::composer::hal::PowerMode::ON) {
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700111 // Normal power mode is counted under different config modes.
Marin Shalamanov68a94092020-11-24 17:48:00 +0100112 if (mConfigModesTotalTime.find(mCurrentRefreshRate) == mConfigModesTotalTime.end()) {
113 mConfigModesTotalTime[mCurrentRefreshRate] = 0;
Alec Mouri0a1cc962019-03-14 12:33:02 -0700114 }
Marin Shalamanov68a94092020-11-24 17:48:00 +0100115 mConfigModesTotalTime[mCurrentRefreshRate] += timeElapsedMs;
116 fps = static_cast<uint32_t>(mCurrentRefreshRate.getIntValue());
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700117 } else {
118 mScreenOffTime += timeElapsedMs;
Alec Mourifb571ea2019-01-24 18:42:10 -0800119 }
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700120 mTimeStats.recordRefreshRate(fps, timeElapsed);
Ana Krulecb43429d2019-01-09 14:28:51 -0800121 }
122
123 // Formats the time in milliseconds into easy to read format.
124 static std::string getDateFormatFromMs(int64_t timeMs) {
125 auto [days, dayRemainderMs] = std::div(timeMs, MS_PER_DAY);
126 auto [hours, hourRemainderMs] = std::div(dayRemainderMs, MS_PER_HOUR);
127 auto [mins, minsRemainderMs] = std::div(hourRemainderMs, MS_PER_MIN);
128 auto [sec, secRemainderMs] = std::div(minsRemainderMs, MS_PER_S);
129 return base::StringPrintf("%" PRId64 "d%02" PRId64 ":%02" PRId64 ":%02" PRId64
130 ".%03" PRId64,
131 days, hours, mins, sec, secRemainderMs);
132 }
133
Alec Mourifb571ea2019-01-24 18:42:10 -0800134 // Aggregate refresh rate statistics for telemetry.
Dominik Laskowski64536512019-03-28 09:53:04 -0700135 TimeStats& mTimeStats;
Alec Mourifb571ea2019-01-24 18:42:10 -0800136
Marin Shalamanov68a94092020-11-24 17:48:00 +0100137 Fps mCurrentRefreshRate;
Peiyong Lin65248e02020-04-18 21:15:07 -0700138 android::hardware::graphics::composer::hal::PowerMode mCurrentPowerMode;
Ana Krulecb43429d2019-01-09 14:28:51 -0800139
Marin Shalamanov68a94092020-11-24 17:48:00 +0100140 std::unordered_map<Fps, int64_t /* duration in ms */, std::hash<Fps>, Fps::EqualsInBuckets>
Ady Abraham2139f732019-11-13 18:56:40 -0800141 mConfigModesTotalTime;
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700142 int64_t mScreenOffTime = 0;
Ana Krulecb43429d2019-01-09 14:28:51 -0800143
Dominik Laskowski64536512019-03-28 09:53:04 -0700144 nsecs_t mPreviousRecordedTime = systemTime();
Ana Krulecb43429d2019-01-09 14:28:51 -0800145};
146
Ady Abraham2139f732019-11-13 18:56:40 -0800147} // namespace android::scheduler