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