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/camera_entity.h"
17
18 #include "battery_stats_service.h"
19 #include "stats_log.h"
20
21 namespace OHOS {
22 namespace PowerMgr {
23 namespace {
24 auto g_statsService = DelayedStatsSpSingleton<BatteryStatsService>::GetInstance();
25 }
26
CameraEntity()27 CameraEntity::CameraEntity()
28 {
29 consumptionType_ = BatteryStatsInfo::CONSUMPTION_TYPE_CAMERA;
30 }
31
GetActiveTimeMs(int32_t uid,StatsUtils::StatsType statsType,int16_t level)32 int64_t CameraEntity::GetActiveTimeMs(int32_t uid, StatsUtils::StatsType statsType, int16_t level)
33 {
34 int64_t activeTimeMs = StatsUtils::DEFAULT_VALUE;
35 if (statsType == StatsUtils::STATS_TYPE_CAMERA_ON) {
36 for (auto& cameraIter : cameraTimerMap_) {
37 auto uidIter = cameraIter.second.find(uid);
38 if (uidIter != cameraIter.second.end()) {
39 activeTimeMs += uidIter->second->GetRunningTimeMs();
40 } else {
41 STATS_HILOGD(COMP_SVC, "Didn't find related timer for uid: %{public}d in camera id: %{private}s",
42 uid, cameraIter.first.c_str());
43 }
44 }
45 }
46 return activeTimeMs;
47 }
48
Calculate(int32_t uid)49 void CameraEntity::Calculate(int32_t uid)
50 {
51 auto cameraOnAverageMa = g_statsService->GetBatteryStatsParser()->GetAveragePowerMa(StatsUtils::CURRENT_CAMERA_ON);
52 auto cameraOnTimeMs = GetActiveTimeMs(uid, StatsUtils::STATS_TYPE_CAMERA_ON);
53 auto cameraOnPowerMah = cameraOnAverageMa * cameraOnTimeMs / StatsUtils::MS_IN_HOUR;
54 auto iter = cameraPowerMap_.find(uid);
55 if (iter != cameraPowerMap_.end()) {
56 STATS_HILOGD(COMP_SVC, "Update camera on power consumption: %{public}lfmAh for uid: %{public}d",
57 cameraOnAverageMa, uid);
58 iter->second = cameraOnPowerMah;
59 } else {
60 STATS_HILOGD(COMP_SVC, "Create camera on power consumption: %{public}lfmAh for uid: %{public}d",
61 cameraOnAverageMa, uid);
62 cameraPowerMap_.insert(std::pair<int32_t, double>(uid, cameraOnPowerMah));
63 }
64 }
65
GetEntityPowerMah(int32_t uidOrUserId)66 double CameraEntity::GetEntityPowerMah(int32_t uidOrUserId)
67 {
68 double power = StatsUtils::DEFAULT_VALUE;
69 auto iter = cameraPowerMap_.find(uidOrUserId);
70 if (iter != cameraPowerMap_.end()) {
71 power = iter->second;
72 STATS_HILOGD(COMP_SVC, "Get app camera power consumption: %{public}lfmAh for uid: %{public}d",
73 power, uidOrUserId);
74 } else {
75 STATS_HILOGD(COMP_SVC,
76 "No app camera power consumption related to uid: %{public}d was found, return 0", uidOrUserId);
77 }
78 return power;
79 }
80
GetStatsPowerMah(StatsUtils::StatsType statsType,int32_t uid)81 double CameraEntity::GetStatsPowerMah(StatsUtils::StatsType statsType, int32_t uid)
82 {
83 double power = StatsUtils::DEFAULT_VALUE;
84 if (statsType == StatsUtils::STATS_TYPE_CAMERA_ON) {
85 auto cameraOnIter = cameraPowerMap_.find(uid);
86 if (cameraOnIter != cameraPowerMap_.end()) {
87 power = cameraOnIter->second;
88 STATS_HILOGD(COMP_SVC, "Get camera on power consumption: %{public}lfmAh for uid: %{public}d",
89 power, uid);
90 } else {
91 STATS_HILOGD(COMP_SVC,
92 "No camera on power consumption related to uid: %{public}d was found, return 0", uid);
93 }
94 }
95 return power;
96 }
97
GetOrCreateTimer(const std::string & deviceId,int32_t uid,StatsUtils::StatsType statsType,int16_t level)98 std::shared_ptr<StatsHelper::ActiveTimer> CameraEntity::GetOrCreateTimer(const std::string& deviceId, int32_t uid,
99 StatsUtils::StatsType statsType, int16_t level)
100 {
101 std::shared_ptr<StatsHelper::ActiveTimer> cmrTimer = nullptr;
102 if (statsType != StatsUtils::STATS_TYPE_CAMERA_ON) {
103 return cmrTimer;
104 }
105
106 auto cameraIter = cameraTimerMap_.find(deviceId);
107 if (cameraIter != cameraTimerMap_.end()) {
108 auto uidIter = cameraIter->second.find(uid);
109 if (uidIter != cameraIter->second.end()) {
110 return uidIter->second;
111 }
112 cmrTimer = std::make_shared<StatsHelper::ActiveTimer>();
113 cameraIter->second.insert(std::pair<int32_t, std::shared_ptr<StatsHelper::ActiveTimer>>(uid, cmrTimer));
114 return cmrTimer;
115 }
116 cmrTimer = std::make_shared<StatsHelper::ActiveTimer>();
117 std::map<int32_t, std::shared_ptr<StatsHelper::ActiveTimer>> uidTimerMap;
118 uidTimerMap.insert(std::pair<int32_t, std::shared_ptr<StatsHelper::ActiveTimer>>(uid, cmrTimer));
119 cameraTimerMap_.insert(
120 std::pair<std::string, std::map<int32_t, std::shared_ptr<StatsHelper::ActiveTimer>>>(deviceId, uidTimerMap));
121 return cmrTimer;
122 }
123
Reset()124 void CameraEntity::Reset()
125 {
126 // Reset app Camera on total power consumption
127 for (auto& iter : cameraPowerMap_) {
128 iter.second = StatsUtils::DEFAULT_VALUE;
129 }
130
131 // Reset Camera on timer
132 for (auto& cameraIter : cameraTimerMap_) {
133 for (auto& uidIter : cameraIter.second) {
134 if (uidIter.second) {
135 uidIter.second->Reset();
136 }
137 }
138 }
139 }
140 } // namespace PowerMgr
141 } // namespace OHOS