blob: 6b78cee91b4112af202766051458058bc7b90fc3 [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;
Alec Mouri0a1cc962019-03-14 12:33:02 -070088 if (!config) {
89 continue;
Ana Krulecb43429d2019-01-09 14:28:51 -080090 }
Alec Mouri0a1cc962019-03-14 12:33:02 -070091 if (mConfigModesTotalTime.find(config->configId) != mConfigModesTotalTime.end()) {
92 totalTimeForConfig = mConfigModesTotalTime.at(config->configId);
93 }
94 totalTime[config->name] = totalTimeForConfig;
Ana Krulecb43429d2019-01-09 14:28:51 -080095 }
96 return totalTime;
97 }
98
99 // Traverses through the map of config modes and returns how long they've been running in easy
100 // to read format.
101 std::string doDump() {
102 std::ostringstream stream;
103 stream << "+ Refresh rate: running time in seconds\n";
104 for (auto stats : getTotalTimes()) {
105 stream << stats.first.c_str() << ": " << getDateFormatFromMs(stats.second) << "\n";
106 }
107 return stream.str();
108 }
109
110private:
111 void flushTime() {
112 // Normal power mode is counted under different config modes.
113 if (mCurrentPowerMode == HWC_POWER_MODE_NORMAL) {
114 flushTimeForMode(mCurrentConfigMode);
115 } else {
116 flushTimeForMode(SCREEN_OFF_CONFIG_ID);
117 }
118 }
119
120 // Calculates the time that passed in ms between the last time we recorded time and the time
121 // this method was called.
122 void flushTimeForMode(int mode) {
123 nsecs_t currentTime = systemTime();
Alec Mourifb571ea2019-01-24 18:42:10 -0800124 nsecs_t timeElapsed = currentTime - mPreviousRecordedTime;
125 int64_t timeElapsedMs = ns2ms(timeElapsed);
Ana Krulecb43429d2019-01-09 14:28:51 -0800126 mPreviousRecordedTime = currentTime;
127
128 mConfigModesTotalTime[mode] += timeElapsedMs;
Ady Abraham1902d072019-03-01 17:18:59 -0800129 for (const auto& [type, config] : mRefreshRateConfigs->getRefreshRates()) {
Alec Mouri0a1cc962019-03-14 12:33:02 -0700130 if (!config) {
131 continue;
132 }
133 if (config->configId == mode) {
134 mTimeStats->recordRefreshRate(config->fps, timeElapsed);
Alec Mourifb571ea2019-01-24 18:42:10 -0800135 }
136 }
Ana Krulecb43429d2019-01-09 14:28:51 -0800137 }
138
139 // Formats the time in milliseconds into easy to read format.
140 static std::string getDateFormatFromMs(int64_t timeMs) {
141 auto [days, dayRemainderMs] = std::div(timeMs, MS_PER_DAY);
142 auto [hours, hourRemainderMs] = std::div(dayRemainderMs, MS_PER_HOUR);
143 auto [mins, minsRemainderMs] = std::div(hourRemainderMs, MS_PER_MIN);
144 auto [sec, secRemainderMs] = std::div(minsRemainderMs, MS_PER_S);
145 return base::StringPrintf("%" PRId64 "d%02" PRId64 ":%02" PRId64 ":%02" PRId64
146 ".%03" PRId64,
147 days, hours, mins, sec, secRemainderMs);
148 }
149
150 // Keeps information about refresh rate configs that device has.
Ady Abraham1902d072019-03-01 17:18:59 -0800151 std::shared_ptr<RefreshRateConfigs> mRefreshRateConfigs;
Ana Krulecb43429d2019-01-09 14:28:51 -0800152
Alec Mourifb571ea2019-01-24 18:42:10 -0800153 // Aggregate refresh rate statistics for telemetry.
154 std::shared_ptr<TimeStats> mTimeStats;
155
Ana Krulecb43429d2019-01-09 14:28:51 -0800156 int64_t mCurrentConfigMode = 0;
157 int32_t mCurrentPowerMode = HWC_POWER_MODE_OFF;
158
159 std::unordered_map<int /* power mode */, int64_t /* duration in ms */> mConfigModesTotalTime;
160
161 nsecs_t mPreviousRecordedTime;
162};
163
164} // namespace scheduler
Alec Mourifb571ea2019-01-24 18:42:10 -0800165} // namespace android