• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     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     if (statsType != StatsUtils::STATS_TYPE_WAKELOCK_HOLD) {
38         return activeTimeMs;
39     }
40 
41     auto iter = wakelockTimerMap_.find(uid);
42     if (iter != wakelockTimerMap_.end()) {
43         activeTimeMs = iter->second->GetRunningTimeMs();
44         STATS_HILOGD(COMP_SVC, "Get wakelock on time: %{public}" PRId64 "ms for uid: %{public}d", activeTimeMs, uid);
45         return activeTimeMs;
46     }
47     STATS_HILOGD(COMP_SVC, "Didn't find related timer for uid: %{public}d, return 0", uid);
48     return activeTimeMs;
49 }
50 
Calculate(int32_t uid)51 void WakelockEntity::Calculate(int32_t uid)
52 {
53     auto wakelockOnAverageMa =
54         g_statsService->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     // Reset Wakelock on timer
127     for (auto& iter : wakelockTimerMap_) {
128         if (iter.second) {
129             iter.second->Reset();
130         }
131     }
132 }
133 } // namespace PowerMgr
134 } // namespace OHOS