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/sensor_entity.h"
17
18 #include <cinttypes>
19
20 #include "battery_stats_service.h"
21 #include "stats_log.h"
22
23 namespace OHOS {
24 namespace PowerMgr {
25 namespace {
26 }
27
SensorEntity()28 SensorEntity::SensorEntity()
29 {
30 consumptionType_ = BatteryStatsInfo::CONSUMPTION_TYPE_SENSOR;
31 }
32
GetActiveTimeMs(int32_t uid,StatsUtils::StatsType statsType,int16_t level)33 int64_t SensorEntity::GetActiveTimeMs(int32_t uid, StatsUtils::StatsType statsType, int16_t level)
34 {
35 int64_t activeTimeMs = StatsUtils::DEFAULT_VALUE;
36 switch (statsType) {
37 case StatsUtils::STATS_TYPE_SENSOR_GRAVITY_ON: {
38 auto gravityIter = gravityTimerMap_.find(uid);
39 if (gravityIter != gravityTimerMap_.end()) {
40 activeTimeMs = gravityIter->second->GetRunningTimeMs();
41 STATS_HILOGD(COMP_SVC, "Get gravity on time: %{public}" PRId64 "ms for uid: %{public}d",
42 activeTimeMs, uid);
43 break;
44 }
45 STATS_HILOGD(COMP_SVC, "Didn't find related timer for uid: %{public}d, return 0", uid);
46 break;
47 }
48 case StatsUtils::STATS_TYPE_SENSOR_PROXIMITY_ON: {
49 auto proximityIter = proximityTimerMap_.find(uid);
50 if (proximityIter != proximityTimerMap_.end()) {
51 activeTimeMs = proximityIter->second->GetRunningTimeMs();
52 STATS_HILOGD(COMP_SVC, "Get proximity on time: %{public}" PRId64 "ms for uid: %{public}d",
53 activeTimeMs, uid);
54 break;
55 }
56 STATS_HILOGD(COMP_SVC, "Didn't find related timer for uid: %{public}d, return 0", uid);
57 break;
58 }
59 default:
60 break;
61 }
62 return activeTimeMs;
63 }
64
CalculateGravity(int32_t uid)65 double SensorEntity::CalculateGravity(int32_t uid)
66 {
67 auto bss = BatteryStatsService::GetInstance();
68 auto gravityOnAverageMa =
69 bss->GetBatteryStatsParser()->GetAveragePowerMa(StatsUtils::CURRENT_SENSOR_GRAVITY);
70 auto gravityOnTimeMs = GetActiveTimeMs(uid, StatsUtils::STATS_TYPE_SENSOR_GRAVITY_ON);
71 auto gravityOnPowerMah = gravityOnAverageMa * gravityOnTimeMs / StatsUtils::MS_IN_HOUR;
72 auto gravityIter = gravityPowerMap_.find(uid);
73 if (gravityIter != gravityPowerMap_.end()) {
74 STATS_HILOGD(COMP_SVC, "Update gravity on power consumption: %{public}lfmAh for uid: %{public}d",
75 gravityOnAverageMa, uid);
76 gravityIter->second = gravityOnPowerMah;
77 } else {
78 STATS_HILOGD(COMP_SVC, "Create gravity on power consumption: %{public}lfmAh for uid: %{public}d",
79 gravityOnAverageMa, uid);
80 gravityPowerMap_.insert(std::pair<int32_t, double>(uid, gravityOnPowerMah));
81 }
82 return gravityOnPowerMah;
83 }
84
CalculateProximity(int32_t uid)85 double SensorEntity::CalculateProximity(int32_t uid)
86 {
87 auto bss = BatteryStatsService::GetInstance();
88 auto proximityOnAverageMa =
89 bss->GetBatteryStatsParser()->GetAveragePowerMa(StatsUtils::CURRENT_SENSOR_PROXIMITY);
90 auto proximityOnTimeMs = GetActiveTimeMs(uid, StatsUtils::STATS_TYPE_SENSOR_PROXIMITY_ON);
91 auto proximityOnPowerMah = proximityOnAverageMa * proximityOnTimeMs / StatsUtils::MS_IN_HOUR;
92 auto proximityIter = proximityPowerMap_.find(uid);
93 if (proximityIter != proximityPowerMap_.end()) {
94 STATS_HILOGD(COMP_SVC, "Update proximity on power consumption: %{public}lfmAh for uid: %{public}d",
95 proximityOnAverageMa, uid);
96 proximityIter->second = proximityOnPowerMah;
97 } else {
98 STATS_HILOGD(COMP_SVC, "Create proximity on power consumption: %{public}lfmAh for uid: %{public}d",
99 proximityOnAverageMa, uid);
100 proximityPowerMap_.insert(std::pair<int32_t, double>(uid, proximityOnPowerMah));
101 }
102 return proximityOnPowerMah;
103 }
104
Calculate(int32_t uid)105 void SensorEntity::Calculate(int32_t uid)
106 {
107 auto gravityOnPowerMah = CalculateGravity(uid);
108 auto proximityOnPowerMah = CalculateProximity(uid);
109
110 double sensorTotalPowerMah = gravityOnPowerMah + proximityOnPowerMah;
111 auto sensorIter = sensorTotalPowerMap_.find(uid);
112 if (sensorIter != sensorTotalPowerMap_.end()) {
113 STATS_HILOGD(COMP_SVC, "Update sensor total power consumption: %{public}lfmAh for uid: %{public}d",
114 sensorTotalPowerMah, uid);
115 sensorIter->second = sensorTotalPowerMah;
116 } else {
117 STATS_HILOGD(COMP_SVC, "Create sensor total power consumption: %{public}lfmAh for uid: %{public}d",
118 sensorTotalPowerMah, uid);
119 sensorTotalPowerMap_.insert(std::pair<int32_t, double>(uid, sensorTotalPowerMah));
120 }
121 }
122
GetEntityPowerMah(int32_t uidOrUserId)123 double SensorEntity::GetEntityPowerMah(int32_t uidOrUserId)
124 {
125 double power = StatsUtils::DEFAULT_VALUE;
126 auto iter = sensorTotalPowerMap_.find(uidOrUserId);
127 if (iter != sensorTotalPowerMap_.end()) {
128 power = iter->second;
129 STATS_HILOGD(COMP_SVC, "Get app sensor power consumption: %{public}lfmAh for uid: %{public}d",
130 power, uidOrUserId);
131 } else {
132 STATS_HILOGD(COMP_SVC,
133 "No app sensor power consumption related to uid: %{public}d was found, return 0", uidOrUserId);
134 }
135 return power;
136 }
137
GetStatsPowerMah(StatsUtils::StatsType statsType,int32_t uid)138 double SensorEntity::GetStatsPowerMah(StatsUtils::StatsType statsType, int32_t uid)
139 {
140 double power = StatsUtils::DEFAULT_VALUE;
141 if (statsType == StatsUtils::STATS_TYPE_SENSOR_GRAVITY_ON) {
142 auto gravityOnIter = gravityPowerMap_.find(uid);
143 if (gravityOnIter != gravityPowerMap_.end()) {
144 power = gravityOnIter->second;
145 STATS_HILOGD(COMP_SVC, "Get gravity on power consumption: %{public}lfmAh for uid: %{public}d",
146 power, uid);
147 } else {
148 STATS_HILOGD(COMP_SVC,
149 "No gravity on power consumption related to uid: %{public}d was found, return 0", uid);
150 }
151 } else if (statsType == StatsUtils::STATS_TYPE_SENSOR_PROXIMITY_ON) {
152 auto proximityOnIter = proximityPowerMap_.find(uid);
153 if (proximityOnIter != proximityPowerMap_.end()) {
154 power = proximityOnIter->second;
155 STATS_HILOGD(COMP_SVC, "Get proximity on power consumption: %{public}lfmAh for uid: %{public}d",
156 power, uid);
157 } else {
158 STATS_HILOGD(COMP_SVC,
159 "No proximity on power consumption related to uid: %{public}d was found, return 0", uid);
160 }
161 }
162 return power;
163 }
164
GetOrCreateTimer(int32_t uid,StatsUtils::StatsType statsType,int16_t level)165 std::shared_ptr<StatsHelper::ActiveTimer> SensorEntity::GetOrCreateTimer(int32_t uid, StatsUtils::StatsType statsType,
166 int16_t level)
167 {
168 std::shared_ptr<StatsHelper::ActiveTimer> timer = nullptr;
169 switch (statsType) {
170 case StatsUtils::STATS_TYPE_SENSOR_GRAVITY_ON: {
171 auto gravityOnIter = gravityTimerMap_.find(uid);
172 if (gravityOnIter != gravityTimerMap_.end()) {
173 STATS_HILOGD(COMP_SVC, "Get gravity on timer for uid: %{public}d", uid);
174 timer = gravityOnIter->second;
175 break;
176 }
177 STATS_HILOGD(COMP_SVC, "Create gravity on timer for uid: %{public}d", uid);
178 std::shared_ptr<StatsHelper::ActiveTimer> gravityTimer = std::make_shared<StatsHelper::ActiveTimer>();
179 gravityTimerMap_.insert(
180 std::pair<int32_t, std::shared_ptr<StatsHelper::ActiveTimer>>(uid, gravityTimer));
181 timer = gravityTimer;
182 break;
183 }
184 case StatsUtils::STATS_TYPE_SENSOR_PROXIMITY_ON: {
185 auto proximityOnIter = proximityTimerMap_.find(uid);
186 if (proximityOnIter != proximityTimerMap_.end()) {
187 STATS_HILOGD(COMP_SVC, "Get proximity on timer for uid: %{public}d", uid);
188 timer = proximityOnIter->second;
189 break;
190 }
191 STATS_HILOGD(COMP_SVC, "Create proximity on timer for uid: %{public}d", uid);
192 std::shared_ptr<StatsHelper::ActiveTimer> proximityTimer = std::make_shared<StatsHelper::ActiveTimer>();
193 proximityTimerMap_.insert(
194 std::pair<int32_t, std::shared_ptr<StatsHelper::ActiveTimer>>(uid, proximityTimer));
195 timer = proximityTimer;
196 break;
197 }
198 default:
199 STATS_HILOGW(COMP_SVC, "Create active timer failed");
200 break;
201 }
202 return timer;
203 }
204
Reset()205 void SensorEntity::Reset()
206 {
207 // Reset app sensor total power consumption
208 for (auto& iter : sensorTotalPowerMap_) {
209 iter.second = StatsUtils::DEFAULT_VALUE;
210 }
211
212 // Reset app gravity on total power consumption
213 for (auto& iter : gravityPowerMap_) {
214 iter.second = StatsUtils::DEFAULT_VALUE;
215 }
216
217 // Reset app proximity on total power consumption
218 for (auto& iter : proximityPowerMap_) {
219 iter.second = StatsUtils::DEFAULT_VALUE;
220 }
221
222 // Reset gravity on timer
223 for (auto& iter : gravityTimerMap_) {
224 if (iter.second) {
225 iter.second->Reset();
226 }
227 }
228
229 // Reset proximity on timer
230 for (auto& iter : proximityTimerMap_) {
231 if (iter.second) {
232 iter.second->Reset();
233 }
234 }
235 }
236 } // namespace PowerMgr
237 } // namespace OHOS