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 }
25 
IdleEntity()26 IdleEntity::IdleEntity()
27 {
28     consumptionType_ = BatteryStatsInfo::CONSUMPTION_TYPE_IDLE;
29     Reset();
30 }
31 
GetActiveTimeMs(StatsUtils::StatsType statsType,int16_t level)32 int64_t IdleEntity::GetActiveTimeMs(StatsUtils::StatsType statsType, int16_t level)
33 {
34     int64_t activeTimeMs = StatsUtils::DEFAULT_VALUE;
35     switch (statsType) {
36         case StatsUtils::STATS_TYPE_PHONE_IDLE:
37             activeTimeMs = StatsHelper::GetOnBatteryUpTimeMs();
38             break;
39         case StatsUtils::STATS_TYPE_CPU_SUSPEND:
40             activeTimeMs = StatsHelper::GetOnBatteryBootTimeMs();
41             break;
42         default:
43             break;
44     }
45     return activeTimeMs;
46 }
47 
Calculate(int32_t uid)48 void IdleEntity::Calculate(int32_t uid)
49 {
50     auto cpuSuspendPower = CalculateCpuSuspendPower();
51     auto cpuIdlePower = CalculateCpuIdlePower();
52     idleTotalPowerMah_ = cpuSuspendPower + cpuIdlePower;
53     totalPowerMah_ += idleTotalPowerMah_;
54     std::shared_ptr<BatteryStatsInfo> statsInfo = std::make_shared<BatteryStatsInfo>();
55     statsInfo->SetConsumptioType(BatteryStatsInfo::CONSUMPTION_TYPE_IDLE);
56     statsInfo->SetPower(idleTotalPowerMah_);
57     statsInfoList_.push_back(statsInfo);
58 
59     STATS_HILOGD(COMP_SVC, "Calculate idle total power consumption: %{public}lfmAh", idleTotalPowerMah_);
60 }
61 
CalculateCpuSuspendPower()62 double IdleEntity::CalculateCpuSuspendPower()
63 {
64     auto bss = BatteryStatsService::GetInstance();
65     auto cpuSuspendAverageMa =
66         bss->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 bss = BatteryStatsService::GetInstance();
77     auto cpuIdleAverageMa =
78         bss->GetBatteryStatsParser()->GetAveragePowerMa(StatsUtils::CURRENT_CPU_IDLE);
79     auto upOnBatteryTimeMs = GetActiveTimeMs(StatsUtils::STATS_TYPE_PHONE_IDLE);
80     auto cpuIdlePowerMah = cpuIdleAverageMa * upOnBatteryTimeMs / StatsUtils::MS_IN_HOUR;
81     cpuIdlePowerMah_ = cpuIdlePowerMah;
82     STATS_HILOGD(COMP_SVC, "Calculate cpu idle power consumption: %{public}lfmAh", cpuIdlePowerMah);
83     return cpuIdlePowerMah_;
84 }
85 
GetEntityPowerMah(int32_t uidOrUserId)86 double IdleEntity::GetEntityPowerMah(int32_t uidOrUserId)
87 {
88     return idleTotalPowerMah_;
89 }
90 
GetStatsPowerMah(StatsUtils::StatsType statsType,int32_t uid)91 double IdleEntity::GetStatsPowerMah(StatsUtils::StatsType statsType, int32_t uid)
92 {
93     double power = StatsUtils::DEFAULT_VALUE;
94     if (statsType == StatsUtils::STATS_TYPE_PHONE_IDLE) {
95         power = cpuIdlePowerMah_;
96         STATS_HILOGD(COMP_SVC, "Get cpu idle power consumption: %{public}lfmAh", power);
97     } else if (statsType == StatsUtils::STATS_TYPE_CPU_SUSPEND) {
98         power = cpuSuspendPowerMah_;
99         STATS_HILOGD(COMP_SVC, "Get cpu suspend power consumption: %{public}lfmAh", power);
100     }
101     return power;
102 }
103 
Reset()104 void IdleEntity::Reset()
105 {
106     // Reset Idle total power consumption
107     idleTotalPowerMah_ = StatsUtils::DEFAULT_VALUE;
108 
109     // Reset cpu idle power consumption
110     cpuIdlePowerMah_ = StatsUtils::DEFAULT_VALUE;
111 
112     // Reset cpu suspend power consumption
113     cpuSuspendPowerMah_ = StatsUtils::DEFAULT_VALUE;
114 }
115 
DumpInfo(std::string & result,int32_t uid)116 void IdleEntity::DumpInfo(std::string& result, int32_t uid)
117 {
118     int64_t upTime = GetActiveTimeMs(StatsUtils::STATS_TYPE_PHONE_IDLE);
119     int64_t bootTime = GetActiveTimeMs(StatsUtils::STATS_TYPE_CPU_SUSPEND);
120     result.append("Idle dump:\n")
121         .append("Phone boot time: ")
122         .append(ToString(bootTime))
123         .append("ms")
124         .append("\n")
125         .append("Phone up time: ")
126         .append(ToString(upTime))
127         .append("ms")
128         .append("\n");
129 }
130 } // namespace PowerMgr
131 } // namespace OHOS