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 auto g_statsService = DelayedStatsSpSingleton<BatteryStatsService>::GetInstance();
27 }
28
AudioEntity()29 AudioEntity::AudioEntity()
30 {
31 consumptionType_ = BatteryStatsInfo::CONSUMPTION_TYPE_AUDIO;
32 }
33
GetActiveTimeMs(int32_t uid,StatsUtils::StatsType statsType,int16_t level)34 int64_t AudioEntity::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_AUDIO_ON: {
39 auto iter = audioTimerMap_.find(uid);
40 if (iter != audioTimerMap_.end()) {
41 activeTimeMs = iter->second->GetRunningTimeMs();
42 STATS_HILOGD(COMP_SVC, "Get audio 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 AudioEntity::Calculate(int32_t uid)
56 {
57 auto audioOnAverageMa = g_statsService->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