• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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"
23 #include "TimeStats/TimeStats.h"
24 
25 #include "android-base/stringprintf.h"
26 #include "utils/Timers.h"
27 
28 namespace android::scheduler {
29 
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  */
36 class 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 
42 public:
RefreshRateStats(const RefreshRateConfigs & refreshRateConfigs,TimeStats & timeStats,HwcConfigIndexType currentConfigId,android::hardware::graphics::composer::hal::PowerMode currentPowerMode)43     RefreshRateStats(const RefreshRateConfigs& refreshRateConfigs, TimeStats& timeStats,
44                      HwcConfigIndexType currentConfigId,
45                      android::hardware::graphics::composer::hal::PowerMode currentPowerMode)
46           : mRefreshRateConfigs(refreshRateConfigs),
47             mTimeStats(timeStats),
48             mCurrentConfigMode(currentConfigId),
49             mCurrentPowerMode(currentPowerMode) {}
50 
51     // Sets power mode.
setPowerMode(android::hardware::graphics::composer::hal::PowerMode mode)52     void setPowerMode(android::hardware::graphics::composer::hal::PowerMode mode) {
53         if (mCurrentPowerMode == mode) {
54             return;
55         }
56         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.
setConfigMode(HwcConfigIndexType configId)62     void setConfigMode(HwcConfigIndexType configId) {
63         if (mCurrentConfigMode == configId) {
64             return;
65         }
66         flushTime();
67         mCurrentConfigMode = configId;
68     }
69 
70     // Returns a map between human readable refresh rate and number of seconds the device spent in
71     // that mode.
getTotalTimes()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;
79         // Multiple configs may map to the same name, e.g. "60fps". Add the
80         // times for such configs together.
81         for (const auto& [configId, time] : mConfigModesTotalTime) {
82             totalTime[mRefreshRateConfigs.getRefreshRateFromConfigId(configId).getName()] = 0;
83         }
84         for (const auto& [configId, time] : mConfigModesTotalTime) {
85             totalTime[mRefreshRateConfigs.getRefreshRateFromConfigId(configId).getName()] += time;
86         }
87         totalTime["ScreenOff"] = mScreenOffTime;
88         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.
dump(std::string & result)93     void dump(std::string& result) const {
94         std::ostringstream stream("+  Refresh rate: running time in seconds\n");
95 
96         for (const auto& [name, time] : const_cast<RefreshRateStats*>(this)->getTotalTimes()) {
97             stream << name << ": " << getDateFormatFromMs(time) << '\n';
98         }
99         result.append(stream.str());
100     }
101 
102 private:
103     // Calculates the time that passed in ms between the last time we recorded time and the time
104     // this method was called.
flushTime()105     void flushTime() {
106         nsecs_t currentTime = systemTime();
107         nsecs_t timeElapsed = currentTime - mPreviousRecordedTime;
108         int64_t timeElapsedMs = ns2ms(timeElapsed);
109         mPreviousRecordedTime = currentTime;
110 
111         uint32_t fps = 0;
112         if (mCurrentPowerMode == android::hardware::graphics::composer::hal::PowerMode::ON) {
113             // Normal power mode is counted under different config modes.
114             if (mConfigModesTotalTime.find(mCurrentConfigMode) == mConfigModesTotalTime.end()) {
115                 mConfigModesTotalTime[mCurrentConfigMode] = 0;
116             }
117             mConfigModesTotalTime[mCurrentConfigMode] += timeElapsedMs;
118             fps = static_cast<uint32_t>(std::round(
119                     mRefreshRateConfigs.getRefreshRateFromConfigId(mCurrentConfigMode).getFps()));
120         } else {
121             mScreenOffTime += timeElapsedMs;
122         }
123         mTimeStats.recordRefreshRate(fps, timeElapsed);
124     }
125 
126     // Formats the time in milliseconds into easy to read format.
getDateFormatFromMs(int64_t timeMs)127     static std::string getDateFormatFromMs(int64_t timeMs) {
128         auto [days, dayRemainderMs] = std::div(timeMs, MS_PER_DAY);
129         auto [hours, hourRemainderMs] = std::div(dayRemainderMs, MS_PER_HOUR);
130         auto [mins, minsRemainderMs] = std::div(hourRemainderMs, MS_PER_MIN);
131         auto [sec, secRemainderMs] = std::div(minsRemainderMs, MS_PER_S);
132         return base::StringPrintf("%" PRId64 "d%02" PRId64 ":%02" PRId64 ":%02" PRId64
133                                   ".%03" PRId64,
134                                   days, hours, mins, sec, secRemainderMs);
135     }
136 
137     // Keeps information about refresh rate configs that device has.
138     const RefreshRateConfigs& mRefreshRateConfigs;
139 
140     // Aggregate refresh rate statistics for telemetry.
141     TimeStats& mTimeStats;
142 
143     HwcConfigIndexType mCurrentConfigMode;
144     android::hardware::graphics::composer::hal::PowerMode mCurrentPowerMode;
145 
146     std::unordered_map<HwcConfigIndexType /* configId */, int64_t /* duration in ms */>
147             mConfigModesTotalTime;
148     int64_t mScreenOffTime = 0;
149 
150     nsecs_t mPreviousRecordedTime = systemTime();
151 };
152 
153 } // namespace android::scheduler
154