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 { 29 namespace 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 */ 37 class 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 43 public: RefreshRateStats(const RefreshRateConfigs & refreshRateConfigs,TimeStats & timeStats,int currentConfigMode,int currentPowerMode)44 RefreshRateStats(const RefreshRateConfigs& refreshRateConfigs, TimeStats& timeStats, 45 int currentConfigMode, int currentPowerMode) 46 : mRefreshRateConfigs(refreshRateConfigs), 47 mTimeStats(timeStats), 48 mCurrentConfigMode(currentConfigMode), 49 mCurrentPowerMode(currentPowerMode) {} 50 51 // Sets power mode. setPowerMode(int mode)52 void setPowerMode(int 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(int mode)62 void setConfigMode(int mode) { 63 if (mCurrentConfigMode == mode) { 64 return; 65 } 66 flushTime(); 67 mCurrentConfigMode = mode; 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& [config, time] : mConfigModesTotalTime) { 82 totalTime[mRefreshRateConfigs.getRefreshRateFromConfigId(config).name] = 0; 83 } 84 for (const auto& [config, time] : mConfigModesTotalTime) { 85 totalTime[mRefreshRateConfigs.getRefreshRateFromConfigId(config).name] += 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. doDump()93 std::string doDump() const { 94 std::ostringstream stream; 95 stream << "+ Refresh rate: running time in seconds\n"; 96 for (const auto& [name, time] : const_cast<RefreshRateStats*>(this)->getTotalTimes()) { 97 stream << name << ": " << getDateFormatFromMs(time) << '\n'; 98 } 99 return 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 == HWC_POWER_MODE_NORMAL) { 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 = mRefreshRateConfigs.getRefreshRateFromConfigId(mCurrentConfigMode).fps; 119 } else { 120 mScreenOffTime += timeElapsedMs; 121 } 122 mTimeStats.recordRefreshRate(fps, timeElapsed); 123 } 124 125 // Formats the time in milliseconds into easy to read format. getDateFormatFromMs(int64_t timeMs)126 static std::string getDateFormatFromMs(int64_t timeMs) { 127 auto [days, dayRemainderMs] = std::div(timeMs, MS_PER_DAY); 128 auto [hours, hourRemainderMs] = std::div(dayRemainderMs, MS_PER_HOUR); 129 auto [mins, minsRemainderMs] = std::div(hourRemainderMs, MS_PER_MIN); 130 auto [sec, secRemainderMs] = std::div(minsRemainderMs, MS_PER_S); 131 return base::StringPrintf("%" PRId64 "d%02" PRId64 ":%02" PRId64 ":%02" PRId64 132 ".%03" PRId64, 133 days, hours, mins, sec, secRemainderMs); 134 } 135 136 // Keeps information about refresh rate configs that device has. 137 const RefreshRateConfigs& mRefreshRateConfigs; 138 139 // Aggregate refresh rate statistics for telemetry. 140 TimeStats& mTimeStats; 141 142 int mCurrentConfigMode; 143 int32_t mCurrentPowerMode; 144 145 std::unordered_map<int /* config */, int64_t /* duration in ms */> mConfigModesTotalTime; 146 int64_t mScreenOffTime = 0; 147 148 nsecs_t mPreviousRecordedTime = systemTime(); 149 }; 150 151 } // namespace scheduler 152 } // namespace android 153