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