blob: 24910812c34e17413a0c212ec48d8498a9ce7885 [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:
Ady Abraham1902d072019-03-01 17:18:59 -080044 explicit RefreshRateStats(const std::shared_ptr<RefreshRateConfigs>& refreshRateConfigs,
45 const std::shared_ptr<TimeStats>& timeStats)
46 : mRefreshRateConfigs(refreshRateConfigs),
Alec Mourifb571ea2019-01-24 18:42:10 -080047 mTimeStats(timeStats),
Ana Krulecb43429d2019-01-09 14:28:51 -080048 mPreviousRecordedTime(systemTime()) {}
49 ~RefreshRateStats() = default;
50
51 // Sets power mode. We only collect the information when the power mode is not
52 // HWC_POWER_MODE_NORMAL. When power mode is HWC_POWER_MODE_NORMAL, we collect the stats based
53 // on config mode.
54 void setPowerMode(int mode) {
55 if (mCurrentPowerMode == mode) {
56 return;
57 }
58 // If power mode is normal, the time is going to be recorded under config modes.
59 if (mode == HWC_POWER_MODE_NORMAL) {
60 mCurrentPowerMode = mode;
61 return;
62 }
63 flushTime();
64 mCurrentPowerMode = mode;
65 }
66
67 // Sets config mode. If the mode has changed, it records how much time was spent in the previous
68 // mode.
69 void setConfigMode(int mode) {
70 if (mCurrentConfigMode == mode) {
71 return;
72 }
73 flushTime();
74 mCurrentConfigMode = mode;
75 }
76
77 // Returns a map between human readable refresh rate and number of seconds the device spent in
78 // that mode.
79 std::unordered_map<std::string, int64_t> getTotalTimes() {
80 // If the power mode is on, then we are probably switching between the config modes. If
81 // it's not then the screen is probably off. Make sure to flush times before printing
82 // them.
83 flushTime();
84
85 std::unordered_map<std::string, int64_t> totalTime;
Ady Abraham1902d072019-03-01 17:18:59 -080086 for (auto [type, config] : mRefreshRateConfigs->getRefreshRates()) {
Ana Krulec4593b692019-01-11 22:07:25 -080087 int64_t totalTimeForConfig = 0;
Ana Krulecb43429d2019-01-09 14:28:51 -080088 if (mConfigModesTotalTime.find(config.configId) != mConfigModesTotalTime.end()) {
Ana Krulec4593b692019-01-11 22:07:25 -080089 totalTimeForConfig = mConfigModesTotalTime.at(config.configId);
Ana Krulecb43429d2019-01-09 14:28:51 -080090 }
Ana Krulec4593b692019-01-11 22:07:25 -080091 totalTime[config.name] = totalTimeForConfig;
Ana Krulecb43429d2019-01-09 14:28:51 -080092 }
93 return totalTime;
94 }
95
96 // Traverses through the map of config modes and returns how long they've been running in easy
97 // to read format.
98 std::string doDump() {
99 std::ostringstream stream;
100 stream << "+ Refresh rate: running time in seconds\n";
101 for (auto stats : getTotalTimes()) {
102 stream << stats.first.c_str() << ": " << getDateFormatFromMs(stats.second) << "\n";
103 }
104 return stream.str();
105 }
106
107private:
108 void flushTime() {
109 // Normal power mode is counted under different config modes.
110 if (mCurrentPowerMode == HWC_POWER_MODE_NORMAL) {
111 flushTimeForMode(mCurrentConfigMode);
112 } else {
113 flushTimeForMode(SCREEN_OFF_CONFIG_ID);
114 }
115 }
116
117 // Calculates the time that passed in ms between the last time we recorded time and the time
118 // this method was called.
119 void flushTimeForMode(int mode) {
120 nsecs_t currentTime = systemTime();
Alec Mourifb571ea2019-01-24 18:42:10 -0800121 nsecs_t timeElapsed = currentTime - mPreviousRecordedTime;
122 int64_t timeElapsedMs = ns2ms(timeElapsed);
Ana Krulecb43429d2019-01-09 14:28:51 -0800123 mPreviousRecordedTime = currentTime;
124
125 mConfigModesTotalTime[mode] += timeElapsedMs;
Ady Abraham1902d072019-03-01 17:18:59 -0800126 for (const auto& [type, config] : mRefreshRateConfigs->getRefreshRates()) {
Alec Mourifb571ea2019-01-24 18:42:10 -0800127 if (config.configId == mode) {
128 mTimeStats->recordRefreshRate(config.fps, timeElapsed);
129 }
130 }
Ana Krulecb43429d2019-01-09 14:28:51 -0800131 }
132
133 // Formats the time in milliseconds into easy to read format.
134 static std::string getDateFormatFromMs(int64_t timeMs) {
135 auto [days, dayRemainderMs] = std::div(timeMs, MS_PER_DAY);
136 auto [hours, hourRemainderMs] = std::div(dayRemainderMs, MS_PER_HOUR);
137 auto [mins, minsRemainderMs] = std::div(hourRemainderMs, MS_PER_MIN);
138 auto [sec, secRemainderMs] = std::div(minsRemainderMs, MS_PER_S);
139 return base::StringPrintf("%" PRId64 "d%02" PRId64 ":%02" PRId64 ":%02" PRId64
140 ".%03" PRId64,
141 days, hours, mins, sec, secRemainderMs);
142 }
143
144 // Keeps information about refresh rate configs that device has.
Ady Abraham1902d072019-03-01 17:18:59 -0800145 std::shared_ptr<RefreshRateConfigs> mRefreshRateConfigs;
Ana Krulecb43429d2019-01-09 14:28:51 -0800146
Alec Mourifb571ea2019-01-24 18:42:10 -0800147 // Aggregate refresh rate statistics for telemetry.
148 std::shared_ptr<TimeStats> mTimeStats;
149
Ana Krulecb43429d2019-01-09 14:28:51 -0800150 int64_t mCurrentConfigMode = 0;
151 int32_t mCurrentPowerMode = HWC_POWER_MODE_OFF;
152
153 std::unordered_map<int /* power mode */, int64_t /* duration in ms */> mConfigModesTotalTime;
154
155 nsecs_t mPreviousRecordedTime;
156};
157
158} // namespace scheduler
Alec Mourifb571ea2019-01-24 18:42:10 -0800159} // namespace android