1 /*
2  * Copyright (c) 2021-2023 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/wakelock_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 
WakelockEntity()28 WakelockEntity::WakelockEntity()
29 {
30     consumptionType_ = BatteryStatsInfo::CONSUMPTION_TYPE_WAKELOCK;
31 }
32 
GetActiveTimeMs(int32_t uid,StatsUtils::StatsType statsType,int16_t level)33 int64_t WakelockEntity::GetActiveTimeMs(int32_t uid, StatsUtils::StatsType statsType, int16_t level)
34 {
35     int64_t activeTimeMs = StatsUtils::DEFAULT_VALUE;
36     if (statsType != StatsUtils::STATS_TYPE_WAKELOCK_HOLD) {
37         return activeTimeMs;
38     }
39 
40     auto iter = wakelockTimerMap_.find(uid);
41     if (iter != wakelockTimerMap_.end()) {
42         activeTimeMs = iter->second->GetRunningTimeMs();
43         STATS_HILOGD(COMP_SVC, "Get wakelock on time: %{public}" PRId64 "ms for uid: %{public}d", activeTimeMs, uid);
44         return activeTimeMs;
45     }
46     STATS_HILOGD(COMP_SVC, "Didn't find related timer for uid: %{public}d, return 0", uid);
47     return activeTimeMs;
48 }
49 
Calculate(int32_t uid)50 void WakelockEntity::Calculate(int32_t uid)
51 {
52     auto bss = BatteryStatsService::GetInstance();
53     auto wakelockOnAverageMa =
54         bss->GetBatteryStatsParser()->GetAveragePowerMa(StatsUtils::CURRENT_CPU_AWAKE);
55     auto wakelockOnTimeMs = GetActiveTimeMs(uid, StatsUtils::STATS_TYPE_WAKELOCK_HOLD);
56     auto wakelockOnPowerMah = wakelockOnAverageMa * wakelockOnTimeMs / StatsUtils::MS_IN_HOUR;
57     auto iter = wakelockPowerMap_.find(uid);
58     if (iter != wakelockPowerMap_.end()) {
59         STATS_HILOGD(COMP_SVC, "Update wakelock on power consumption: %{public}lfmAh for uid: %{public}d",
60             wakelockOnPowerMah, uid);
61         iter->second = wakelockOnPowerMah;
62     } else {
63         STATS_HILOGD(COMP_SVC, "Create wakelock on power consumption: %{public}lfmAh for uid: %{public}d",
64             wakelockOnPowerMah, uid);
65         wakelockPowerMap_.insert(std::pair<int32_t, double>(uid, wakelockOnPowerMah));
66     }
67 }
68 
GetEntityPowerMah(int32_t uidOrUserId)69 double WakelockEntity::GetEntityPowerMah(int32_t uidOrUserId)
70 {
71     double power = StatsUtils::DEFAULT_VALUE;
72     auto iter = wakelockPowerMap_.find(uidOrUserId);
73     if (iter != wakelockPowerMap_.end()) {
74         power = iter->second;
75         STATS_HILOGD(COMP_SVC, "Get app wakelock power consumption: %{public}lfmAh for uid: %{public}d",
76             power, uidOrUserId);
77     } else {
78         STATS_HILOGD(COMP_SVC,
79             "No app wakelock power consumption related to uid: %{public}d was found, return 0", uidOrUserId);
80     }
81     return power;
82 }
83 
GetStatsPowerMah(StatsUtils::StatsType statsType,int32_t uid)84 double WakelockEntity::GetStatsPowerMah(StatsUtils::StatsType statsType, int32_t uid)
85 {
86     double power = StatsUtils::DEFAULT_VALUE;
87     if (statsType == StatsUtils::STATS_TYPE_WAKELOCK_HOLD) {
88         auto wakelockOnIter = wakelockPowerMap_.find(uid);
89         if (wakelockOnIter != wakelockPowerMap_.end()) {
90             power = wakelockOnIter->second;
91             STATS_HILOGD(COMP_SVC, "Get wakelock on power consumption: %{public}lfmAh for uid: %{public}d",
92                 power, uid);
93         } else {
94             STATS_HILOGD(COMP_SVC,
95                 "No wakelock on power consumption related to uid: %{public}d was found, return 0", uid);
96         }
97     }
98     return power;
99 }
100 
GetOrCreateTimer(int32_t uid,StatsUtils::StatsType statsType,int16_t level)101 std::shared_ptr<StatsHelper::ActiveTimer> WakelockEntity::GetOrCreateTimer(int32_t uid, StatsUtils::StatsType statsType,
102     int16_t level)
103 {
104     if (statsType != StatsUtils::STATS_TYPE_WAKELOCK_HOLD) {
105         return nullptr;
106     }
107 
108     auto wakelockOnIter = wakelockTimerMap_.find(uid);
109     if (wakelockOnIter != wakelockTimerMap_.end()) {
110         STATS_HILOGD(COMP_SVC, "Get wakelock on timer for uid: %{public}d", uid);
111         return wakelockOnIter->second;
112     }
113     STATS_HILOGD(COMP_SVC, "Create wakelock on timer for uid: %{public}d", uid);
114     std::shared_ptr<StatsHelper::ActiveTimer> holdTimer = std::make_shared<StatsHelper::ActiveTimer>();
115     wakelockTimerMap_.insert(std::pair<int32_t, std::shared_ptr<StatsHelper::ActiveTimer>>(uid, holdTimer));
116     return holdTimer;
117 }
118 
Reset()119 void WakelockEntity::Reset()
120 {
121     // Reset app Wakelock on total power consumption
122     for (auto& iter : wakelockPowerMap_) {
123         iter.second = StatsUtils::DEFAULT_VALUE;
124     }
125 
126     STATS_HILOGI(COMP_SVC, "Reset Wakelock on timer.");
127     // Reset Wakelock on timer
128     for (auto& iter : wakelockTimerMap_) {
129         if (iter.second) {
130             iter.second->Reset();
131         }
132     }
133 }
134 } // namespace PowerMgr
135 } // namespace OHOS