1 /*
2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "entities/idle_entity.h"
17
18 #include "battery_stats_service.h"
19 #include "stats_log.h"
20
21 namespace OHOS {
22 namespace PowerMgr {
23 namespace {
24 auto g_statsService = DelayedStatsSpSingleton<BatteryStatsService>::GetInstance();
25 }
26
IdleEntity()27 IdleEntity::IdleEntity()
28 {
29 consumptionType_ = BatteryStatsInfo::CONSUMPTION_TYPE_IDLE;
30 Reset();
31 }
32
GetActiveTimeMs(StatsUtils::StatsType statsType,int16_t level)33 int64_t IdleEntity::GetActiveTimeMs(StatsUtils::StatsType statsType, int16_t level)
34 {
35 int64_t activeTimeMs = StatsUtils::DEFAULT_VALUE;
36 switch (statsType) {
37 case StatsUtils::STATS_TYPE_PHONE_IDLE:
38 activeTimeMs = StatsHelper::GetOnBatteryUpTimeMs();
39 break;
40 case StatsUtils::STATS_TYPE_CPU_SUSPEND:
41 activeTimeMs = StatsHelper::GetOnBatteryBootTimeMs();
42 break;
43 default:
44 break;
45 }
46 return activeTimeMs;
47 }
48
Calculate(int32_t uid)49 void IdleEntity::Calculate(int32_t uid)
50 {
51 auto cpuSuspendPower = CalculateCpuSuspendPower();
52 auto cpuIdlePower = CalculateCpuIdlePower();
53 idleTotalPowerMah_ = cpuSuspendPower + cpuIdlePower;
54 totalPowerMah_ += idleTotalPowerMah_;
55 std::shared_ptr<BatteryStatsInfo> statsInfo = std::make_shared<BatteryStatsInfo>();
56 statsInfo->SetConsumptioType(BatteryStatsInfo::CONSUMPTION_TYPE_IDLE);
57 statsInfo->SetPower(idleTotalPowerMah_);
58 statsInfoList_.push_back(statsInfo);
59
60 STATS_HILOGD(COMP_SVC, "Calculate idle total power consumption: %{public}lfmAh", idleTotalPowerMah_);
61 }
62
CalculateCpuSuspendPower()63 double IdleEntity::CalculateCpuSuspendPower()
64 {
65 auto cpuSuspendAverageMa =
66 g_statsService->GetBatteryStatsParser()->GetAveragePowerMa(StatsUtils::CURRENT_CPU_SUSPEND);
67 auto bootOnBatteryTimeMs = GetActiveTimeMs(StatsUtils::STATS_TYPE_CPU_SUSPEND);
68 auto cpuSuspendPowerMah = cpuSuspendAverageMa * bootOnBatteryTimeMs / StatsUtils::MS_IN_HOUR;
69 cpuSuspendPowerMah_ = cpuSuspendPowerMah;
70 STATS_HILOGD(COMP_SVC, "Calculate cpu suspend power consumption: %{public}lfmAh", cpuSuspendPowerMah);
71 return cpuSuspendPowerMah_;
72 }
73
CalculateCpuIdlePower()74 double IdleEntity::CalculateCpuIdlePower()
75 {
76 auto cpuIdleAverageMa =
77 g_statsService->GetBatteryStatsParser()->GetAveragePowerMa(StatsUtils::CURRENT_CPU_IDLE);
78 auto upOnBatteryTimeMs = GetActiveTimeMs(StatsUtils::STATS_TYPE_PHONE_IDLE);
79 auto cpuIdlePowerMah = cpuIdleAverageMa * upOnBatteryTimeMs / StatsUtils::MS_IN_HOUR;
80 cpuIdlePowerMah_ = cpuIdlePowerMah;
81 STATS_HILOGD(COMP_SVC, "Calculate cpu idle power consumption: %{public}lfmAh", cpuIdlePowerMah);
82 return cpuIdlePowerMah_;
83 }
84
GetEntityPowerMah(int32_t uidOrUserId)85 double IdleEntity::GetEntityPowerMah(int32_t uidOrUserId)
86 {
87 return idleTotalPowerMah_;
88 }
89
GetStatsPowerMah(StatsUtils::StatsType statsType,int32_t uid)90 double IdleEntity::GetStatsPowerMah(StatsUtils::StatsType statsType, int32_t uid)
91 {
92 double power = StatsUtils::DEFAULT_VALUE;
93 if (statsType == StatsUtils::STATS_TYPE_PHONE_IDLE) {
94 power = cpuIdlePowerMah_;
95 STATS_HILOGD(COMP_SVC, "Get cpu idle power consumption: %{public}lfmAh", power);
96 } else if (statsType == StatsUtils::STATS_TYPE_CPU_SUSPEND) {
97 power = cpuSuspendPowerMah_;
98 STATS_HILOGD(COMP_SVC, "Get cpu suspend power consumption: %{public}lfmAh", power);
99 }
100 return power;
101 }
102
Reset()103 void IdleEntity::Reset()
104 {
105 // Reset Idle total power consumption
106 idleTotalPowerMah_ = StatsUtils::DEFAULT_VALUE;
107
108 // Reset cpu idle power consumption
109 cpuIdlePowerMah_ = StatsUtils::DEFAULT_VALUE;
110
111 // Reset cpu suspend power consumption
112 cpuSuspendPowerMah_ = StatsUtils::DEFAULT_VALUE;
113 }
114
DumpInfo(std::string & result,int32_t uid)115 void IdleEntity::DumpInfo(std::string& result, int32_t uid)
116 {
117 int64_t upTime = GetActiveTimeMs(StatsUtils::STATS_TYPE_PHONE_IDLE);
118 int64_t bootTime = GetActiveTimeMs(StatsUtils::STATS_TYPE_CPU_SUSPEND);
119 result.append("Idle dump:\n")
120 .append("Phone boot time: ")
121 .append(ToString(bootTime))
122 .append("ms")
123 .append("\n")
124 .append("Phone up time: ")
125 .append(ToString(upTime))
126 .append("ms")
127 .append("\n");
128 }
129 } // namespace PowerMgr
130 } // namespace OHOS