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