• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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/alarm_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 
AlarmEntity()29 AlarmEntity::AlarmEntity()
30 {
31     consumptionType_ = BatteryStatsInfo::CONSUMPTION_TYPE_ALARM;
32 }
33 
GetConsumptionCount(StatsUtils::StatsType statsType,int32_t uid)34 int64_t AlarmEntity::GetConsumptionCount(StatsUtils::StatsType statsType, int32_t uid)
35 {
36     int64_t count = StatsUtils::DEFAULT_VALUE;
37     if (statsType == StatsUtils::STATS_TYPE_ALARM) {
38         auto almIter = alarmCounterMap_.find(uid);
39         if (almIter != alarmCounterMap_.end()) {
40             count = almIter->second->GetCount();
41             STATS_HILOGD(COMP_SVC, "Get alarm count: %{public}" PRId64 " for uid: %{public}d", count, uid);
42         }
43         STATS_HILOGD(COMP_SVC, "No alarm count related to uid: %{public}d was found, return 0", uid);
44     }
45     return count;
46 }
47 
Calculate(int32_t uid)48 void AlarmEntity::Calculate(int32_t uid)
49 {
50     auto alarmOnAverageMa = g_statsService->GetBatteryStatsParser()->GetAveragePowerMa(StatsUtils::CURRENT_ALARM_ON);
51     auto alarmOnCount = GetConsumptionCount(StatsUtils::STATS_TYPE_ALARM, uid);
52     auto alarmOnPowerMah = alarmOnAverageMa * alarmOnCount;
53     auto iter = alarmPowerMap_.find(uid);
54     if (iter != alarmPowerMap_.end()) {
55         STATS_HILOGD(COMP_SVC, "Update alarm on power consumption: %{public}lfmAh for uid: %{public}d",
56             alarmOnAverageMa, uid);
57         iter->second = alarmOnPowerMah;
58     } else {
59         STATS_HILOGD(COMP_SVC, "Create alarm on power consumption: %{public}lfmAh for uid: %{public}d",
60             alarmOnAverageMa, uid);
61         alarmPowerMap_.insert(std::pair<int32_t, double>(uid, alarmOnPowerMah));
62     }
63 }
64 
GetEntityPowerMah(int32_t uidOrUserId)65 double AlarmEntity::GetEntityPowerMah(int32_t uidOrUserId)
66 {
67     double power = StatsUtils::DEFAULT_VALUE;
68     auto iter = alarmPowerMap_.find(uidOrUserId);
69     if (iter != alarmPowerMap_.end()) {
70         power = iter->second;
71         STATS_HILOGD(COMP_SVC, "Get app alarm power consumption: %{public}lfmAh for uid: %{public}d",
72             power, uidOrUserId);
73     } else {
74         STATS_HILOGD(COMP_SVC,
75             "No app alarm power consumption related to uid: %{public}d was found, return 0", uidOrUserId);
76     }
77     return power;
78 }
79 
GetStatsPowerMah(StatsUtils::StatsType statsType,int32_t uid)80 double AlarmEntity::GetStatsPowerMah(StatsUtils::StatsType statsType, int32_t uid)
81 {
82     double power = StatsUtils::DEFAULT_VALUE;
83     if (statsType == StatsUtils::STATS_TYPE_ALARM) {
84         auto alarmOnIter = alarmPowerMap_.find(uid);
85         if (alarmOnIter != alarmPowerMap_.end()) {
86             power = alarmOnIter->second;
87             STATS_HILOGD(COMP_SVC, "Get alarm on power consumption: %{public}lfmAh for uid: %{public}d",
88                 power, uid);
89         } else {
90             STATS_HILOGD(COMP_SVC,
91                 "No alarm on power alarm related to uid: %{public}d was found, return 0", uid);
92         }
93     }
94     return power;
95 }
96 
GetOrCreateCounter(StatsUtils::StatsType statsType,int32_t uid)97 std::shared_ptr<StatsHelper::Counter> AlarmEntity::GetOrCreateCounter(StatsUtils::StatsType statsType, int32_t uid)
98 {
99     if (statsType != StatsUtils::STATS_TYPE_ALARM) {
100         return nullptr;
101     }
102 
103     auto alarmOnIter = alarmCounterMap_.find(uid);
104     if (alarmOnIter != alarmCounterMap_.end()) {
105         STATS_HILOGD(COMP_SVC, "Get alarm on counter for uid: %{public}d", uid);
106         return alarmOnIter->second;
107     }
108     STATS_HILOGD(COMP_SVC, "Create alarm on counter for uid: %{public}d", uid);
109     std::shared_ptr<StatsHelper::Counter> alarmConuter = std::make_shared<StatsHelper::Counter>();
110     alarmCounterMap_.insert(std::pair<int32_t, std::shared_ptr<StatsHelper::Counter>>(uid, alarmConuter));
111     return alarmConuter;
112 }
113 
Reset()114 void AlarmEntity::Reset()
115 {
116     // Reset app Alarm on total power consumption
117     for (auto& iter : alarmPowerMap_) {
118         iter.second = StatsUtils::DEFAULT_VALUE;
119     }
120 
121     // Reset Alarm on counter
122     for (auto& iter : alarmCounterMap_) {
123         if (iter.second) {
124             iter.second->Reset();
125         }
126     }
127 }
128 } // namespace PowerMgr
129 } // namespace OHOS