blob: 1f097db08d0b616a91d4f177e3d65a2387b099c9 [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:
Dominik Laskowski64536512019-03-28 09:53:04 -070044 RefreshRateStats(const RefreshRateConfigs& refreshRateConfigs, TimeStats& timeStats)
45 : mRefreshRateConfigs(refreshRateConfigs), mTimeStats(timeStats) {}
Ana Krulecb43429d2019-01-09 14:28:51 -080046
47 // Sets power mode. We only collect the information when the power mode is not
48 // HWC_POWER_MODE_NORMAL. When power mode is HWC_POWER_MODE_NORMAL, we collect the stats based
49 // on config mode.
50 void setPowerMode(int mode) {
51 if (mCurrentPowerMode == mode) {
52 return;
53 }
54 // If power mode is normal, the time is going to be recorded under config modes.
55 if (mode == HWC_POWER_MODE_NORMAL) {
56 mCurrentPowerMode = mode;
57 return;
58 }
59 flushTime();
60 mCurrentPowerMode = mode;
61 }
62
63 // Sets config mode. If the mode has changed, it records how much time was spent in the previous
64 // mode.
65 void setConfigMode(int mode) {
66 if (mCurrentConfigMode == mode) {
67 return;
68 }
69 flushTime();
70 mCurrentConfigMode = mode;
71 }
72
73 // Returns a map between human readable refresh rate and number of seconds the device spent in
74 // that mode.
75 std::unordered_map<std::string, int64_t> getTotalTimes() {
76 // 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
81 std::unordered_map<std::string, int64_t> totalTime;
Dominik Laskowski64536512019-03-28 09:53:04 -070082 for (const auto& [type, config] : mRefreshRateConfigs.getRefreshRates()) {
Ana Krulec4593b692019-01-11 22:07:25 -080083 int64_t totalTimeForConfig = 0;
Alec Mouri0a1cc962019-03-14 12:33:02 -070084 if (!config) {
85 continue;
Ana Krulecb43429d2019-01-09 14:28:51 -080086 }
Alec Mouri0a1cc962019-03-14 12:33:02 -070087 if (mConfigModesTotalTime.find(config->configId) != mConfigModesTotalTime.end()) {
88 totalTimeForConfig = mConfigModesTotalTime.at(config->configId);
89 }
90 totalTime[config->name] = totalTimeForConfig;
Ana Krulecb43429d2019-01-09 14:28:51 -080091 }
92 return totalTime;
93 }
94
95 // Traverses through the map of config modes and returns how long they've been running in easy
96 // to read format.
Dominik Laskowski98041832019-08-01 18:35:59 -070097 void dump(std::string& result) const {
98 std::ostringstream stream("+ Refresh rate: running time in seconds\n");
99
Dominik Laskowski64536512019-03-28 09:53:04 -0700100 for (const auto& [name, time] : const_cast<RefreshRateStats*>(this)->getTotalTimes()) {
101 stream << name << ": " << getDateFormatFromMs(time) << '\n';
Ana Krulecb43429d2019-01-09 14:28:51 -0800102 }
Dominik Laskowski98041832019-08-01 18:35:59 -0700103 result.append(stream.str());
Ana Krulecb43429d2019-01-09 14:28:51 -0800104 }
105
106private:
107 void flushTime() {
108 // Normal power mode is counted under different config modes.
109 if (mCurrentPowerMode == HWC_POWER_MODE_NORMAL) {
110 flushTimeForMode(mCurrentConfigMode);
111 } else {
112 flushTimeForMode(SCREEN_OFF_CONFIG_ID);
113 }
114 }
115
116 // Calculates the time that passed in ms between the last time we recorded time and the time
117 // this method was called.
118 void flushTimeForMode(int mode) {
119 nsecs_t currentTime = systemTime();
Alec Mourifb571ea2019-01-24 18:42:10 -0800120 nsecs_t timeElapsed = currentTime - mPreviousRecordedTime;
121 int64_t timeElapsedMs = ns2ms(timeElapsed);
Ana Krulecb43429d2019-01-09 14:28:51 -0800122 mPreviousRecordedTime = currentTime;
123
124 mConfigModesTotalTime[mode] += timeElapsedMs;
Dominik Laskowski64536512019-03-28 09:53:04 -0700125 for (const auto& [type, config] : mRefreshRateConfigs.getRefreshRates()) {
Alec Mouri0a1cc962019-03-14 12:33:02 -0700126 if (!config) {
127 continue;
128 }
129 if (config->configId == mode) {
Dominik Laskowski64536512019-03-28 09:53:04 -0700130 mTimeStats.recordRefreshRate(config->fps, timeElapsed);
Alec Mourifb571ea2019-01-24 18:42:10 -0800131 }
132 }
Ana Krulecb43429d2019-01-09 14:28:51 -0800133 }
134
135 // Formats the time in milliseconds into easy to read format.
136 static std::string getDateFormatFromMs(int64_t timeMs) {
137 auto [days, dayRemainderMs] = std::div(timeMs, MS_PER_DAY);
138 auto [hours, hourRemainderMs] = std::div(dayRemainderMs, MS_PER_HOUR);
139 auto [mins, minsRemainderMs] = std::div(hourRemainderMs, MS_PER_MIN);
140 auto [sec, secRemainderMs] = std::div(minsRemainderMs, MS_PER_S);
141 return base::StringPrintf("%" PRId64 "d%02" PRId64 ":%02" PRId64 ":%02" PRId64
142 ".%03" PRId64,
143 days, hours, mins, sec, secRemainderMs);
144 }
145
146 // Keeps information about refresh rate configs that device has.
Dominik Laskowski64536512019-03-28 09:53:04 -0700147 const RefreshRateConfigs& mRefreshRateConfigs;
Ana Krulecb43429d2019-01-09 14:28:51 -0800148
Alec Mourifb571ea2019-01-24 18:42:10 -0800149 // Aggregate refresh rate statistics for telemetry.
Dominik Laskowski64536512019-03-28 09:53:04 -0700150 TimeStats& mTimeStats;
Alec Mourifb571ea2019-01-24 18:42:10 -0800151
Alec Mouri62c8bd12019-03-22 15:55:23 -0700152 int64_t mCurrentConfigMode = SCREEN_OFF_CONFIG_ID;
Ana Krulecb43429d2019-01-09 14:28:51 -0800153 int32_t mCurrentPowerMode = HWC_POWER_MODE_OFF;
154
155 std::unordered_map<int /* power mode */, int64_t /* duration in ms */> mConfigModesTotalTime;
156
Dominik Laskowski64536512019-03-28 09:53:04 -0700157 nsecs_t mPreviousRecordedTime = systemTime();
Ana Krulecb43429d2019-01-09 14:28:51 -0800158};
159
160} // namespace scheduler
Alec Mourifb571ea2019-01-24 18:42:10 -0800161} // namespace android