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 "battery_stats_detector.h"
17
18 #include <cinttypes>
19
20 #include "battery_stats_service.h"
21
22 namespace OHOS {
23 namespace PowerMgr {
HandleStatsChangedEvent(StatsUtils::StatsData data)24 void BatteryStatsDetector::HandleStatsChangedEvent(StatsUtils::StatsData data)
25 {
26 STATS_HILOGD(COMP_SVC,
27 "Handle type: %{public}s, state: %{public}d, level: %{public}d, uid: %{public}d, pid: %{public}d, " \
28 "eventDataName: %{public}s, eventDataType: %{public}d, eventDataExtra: %{public}d, " \
29 "time: %{public}" PRId64 ", traffic: %{public}" PRId64 ", deviceId: %{private}s",
30 StatsUtils::ConvertStatsType(data.type).c_str(),
31 data.state,
32 data.level,
33 data.uid,
34 data.pid,
35 data.eventDataName.c_str(),
36 data.eventDataType,
37 data.eventDataExtra,
38 data.time,
39 data.traffic,
40 data.deviceId.c_str());
41
42 auto bss = DelayedStatsSpSingleton<BatteryStatsService>::GetInstance();
43 if (bss == nullptr) {
44 STATS_HILOGE(COMP_SVC, "Get battery stats service failed");
45 return;
46 }
47 auto core = bss->GetBatteryStatsCore();
48 if (IsDurationRelated(data.type)) {
49 // Update related timer with reported time
50 // The traffic won't participate the power consumption calculation, just for dump info
51 core->UpdateStats(data.type, data.time, data.traffic, data.uid);
52 } else if (IsStateRelated(data.type)) {
53 // Update related timer based on state or level
54 core->UpdateStats(data.type, data.state, data.level, data.uid, data.deviceId);
55 }
56 HandleDebugInfo(data);
57 }
58
IsDurationRelated(StatsUtils::StatsType type)59 bool BatteryStatsDetector::IsDurationRelated(StatsUtils::StatsType type)
60 {
61 bool isMatch = false;
62 switch (type) {
63 case StatsUtils::STATS_TYPE_WIFI_SCAN:
64 case StatsUtils::STATS_TYPE_ALARM:
65 // Realated with duration
66 isMatch = true;
67 break;
68 default:
69 STATS_HILOGD(COMP_SVC, "No duration related type=%{public}d", static_cast<int32_t>(type));
70 break;
71 }
72 return isMatch;
73 }
74
IsStateRelated(StatsUtils::StatsType type)75 bool BatteryStatsDetector::IsStateRelated(StatsUtils::StatsType type)
76 {
77 bool isMatch = false;
78 switch (type) {
79 case StatsUtils::STATS_TYPE_SENSOR_GRAVITY_ON:
80 case StatsUtils::STATS_TYPE_SENSOR_PROXIMITY_ON:
81 case StatsUtils::STATS_TYPE_SCREEN_ON:
82 case StatsUtils::STATS_TYPE_SCREEN_BRIGHTNESS:
83 case StatsUtils::STATS_TYPE_BLUETOOTH_BR_ON:
84 case StatsUtils::STATS_TYPE_BLUETOOTH_BR_SCAN:
85 case StatsUtils::STATS_TYPE_BLUETOOTH_BLE_ON:
86 case StatsUtils::STATS_TYPE_BLUETOOTH_BLE_SCAN:
87 case StatsUtils::STATS_TYPE_WIFI_ON:
88 case StatsUtils::STATS_TYPE_PHONE_ACTIVE:
89 case StatsUtils::STATS_TYPE_PHONE_DATA:
90 case StatsUtils::STATS_TYPE_CAMERA_ON:
91 case StatsUtils::STATS_TYPE_CAMERA_FLASHLIGHT_ON:
92 case StatsUtils::STATS_TYPE_FLASHLIGHT_ON:
93 case StatsUtils::STATS_TYPE_GNSS_ON:
94 case StatsUtils::STATS_TYPE_AUDIO_ON:
95 case StatsUtils::STATS_TYPE_WAKELOCK_HOLD:
96 // Related with state
97 isMatch = true;
98 break;
99 default:
100 STATS_HILOGD(COMP_SVC, "No state related type=%{public}d", static_cast<int32_t>(type));
101 break;
102 }
103 return isMatch;
104 }
105
HandleThermalInfo(StatsUtils::StatsData data,int64_t bootTimeMs,std::string & debugInfo)106 void BatteryStatsDetector::HandleThermalInfo(StatsUtils::StatsData data, int64_t bootTimeMs, std::string& debugInfo)
107 {
108 debugInfo.append("Thermal event: Boot time after boot = ")
109 .append(ToString(bootTimeMs))
110 .append("ms\n");
111 if (!data.eventDebugInfo.empty()) {
112 debugInfo.append("Additional debug info: ")
113 .append(data.eventDebugInfo)
114 .append("\n");
115 }
116 }
117
HandleBatteryInfo(StatsUtils::StatsData data,int64_t bootTimeMs,std::string & debugInfo)118 void BatteryStatsDetector::HandleBatteryInfo(StatsUtils::StatsData data, int64_t bootTimeMs, std::string& debugInfo)
119 {
120 debugInfo.append("Battery event: Battery level = ")
121 .append(ToString(data.level))
122 .append(", Charger type = ")
123 .append(ToString(data.eventDataExtra))
124 .append(", boot time after boot = ")
125 .append(ToString(bootTimeMs))
126 .append("ms\n");
127 if (!data.eventDebugInfo.empty()) {
128 debugInfo.append("Additional debug info: ")
129 .append(data.eventDebugInfo);
130 }
131 }
132
HandleDispalyInfo(StatsUtils::StatsData data,int64_t bootTimeMs,std::string & debugInfo)133 void BatteryStatsDetector::HandleDispalyInfo(StatsUtils::StatsData data, int64_t bootTimeMs, std::string& debugInfo)
134 {
135 debugInfo.append("Dislpay event: Boot time after boot = ")
136 .append(ToString(bootTimeMs))
137 .append("ms\n");
138 if (!data.eventDebugInfo.empty()) {
139 debugInfo.append("Additional debug info: ")
140 .append(data.eventDebugInfo)
141 .append("\n");
142 }
143 }
144
HandleWakelockInfo(StatsUtils::StatsData data,int64_t bootTimeMs,std::string & debugInfo)145 void BatteryStatsDetector::HandleWakelockInfo(StatsUtils::StatsData data, int64_t bootTimeMs, std::string& debugInfo)
146 {
147 debugInfo.append("\n")
148 .append("Wakelock event: UID = ")
149 .append(ToString(data.uid))
150 .append(", PID = ")
151 .append(ToString(data.pid))
152 .append(", wakelock type = ")
153 .append(ToString(data.eventDataType))
154 .append(", wakelock name = ")
155 .append(data.eventDataName)
156 .append(", boot time after boot = ")
157 .append(ToString(bootTimeMs))
158 .append("ms\n");
159 if (!data.eventDebugInfo.empty()) {
160 debugInfo.append("Additional debug info: ")
161 .append(data.eventDebugInfo);
162 }
163 }
164
HandleWorkschedulerInfo(StatsUtils::StatsData data,int64_t bootTimeMs,std::string & debugInfo)165 void BatteryStatsDetector::HandleWorkschedulerInfo(StatsUtils::StatsData data, int64_t bootTimeMs,
166 std::string& debugInfo)
167 {
168 debugInfo.append("WorkScheduler event: UID = ")
169 .append(ToString(data.uid))
170 .append(", PID = ")
171 .append(ToString(data.pid))
172 .append(", work type = ")
173 .append(ToString(data.eventDataType))
174 .append(", work interval = ")
175 .append(ToString(data.eventDataExtra))
176 .append(", work state = ")
177 .append(ToString(data.state))
178 .append(", boot time after boot = ")
179 .append(ToString(bootTimeMs))
180 .append("ms\n");
181 if (!data.eventDebugInfo.empty()) {
182 debugInfo.append("Additional debug info: ")
183 .append(data.eventDebugInfo);
184 }
185 }
186
HandlePhoneInfo(StatsUtils::StatsData data,int64_t bootTimeMs,std::string & debugInfo)187 void BatteryStatsDetector::HandlePhoneInfo(StatsUtils::StatsData data, int64_t bootTimeMs, std::string& debugInfo)
188 {
189 debugInfo.append("Phone event: Boot time after boot = ")
190 .append(ToString(bootTimeMs))
191 .append("ms\n");
192 if (!data.eventDebugInfo.empty()) {
193 debugInfo.append("Additional debug info: ")
194 .append(data.eventDebugInfo)
195 .append("\n");
196 }
197 }
198
HandleFlashlightInfo(StatsUtils::StatsData data,int64_t bootTimeMs,std::string & debugInfo)199 void BatteryStatsDetector::HandleFlashlightInfo(StatsUtils::StatsData data, int64_t bootTimeMs, std::string& debugInfo)
200 {
201 std::string eventState;
202 if (data.state == StatsUtils::STATS_STATE_ACTIVATED) {
203 eventState = "ON";
204 } else {
205 eventState = "OFF";
206 }
207 debugInfo.append("Flashlight event: UID = ")
208 .append(ToString(data.uid))
209 .append(", PID = ")
210 .append(ToString(data.pid))
211 .append(", flashlight state = ")
212 .append(eventState)
213 .append(", boot time after boot = ")
214 .append(ToString(bootTimeMs))
215 .append("ms\n");
216 }
217
HandleDistributedSchedulerInfo(StatsUtils::StatsData data,int64_t bootTimeMs,std::string & debugInfo)218 void BatteryStatsDetector::HandleDistributedSchedulerInfo(StatsUtils::StatsData data, int64_t bootTimeMs,
219 std::string& debugInfo)
220 {
221 debugInfo.append("Distributed schedule event")
222 .append(", boot time after boot = ")
223 .append(ToString(bootTimeMs))
224 .append("ms\n");
225 if (!data.eventDebugInfo.empty()) {
226 debugInfo.append("Additional debug info: ")
227 .append(data.eventDebugInfo)
228 .append("\n");
229 }
230 }
231
HandleDebugInfo(StatsUtils::StatsData data)232 void BatteryStatsDetector::HandleDebugInfo(StatsUtils::StatsData data)
233 {
234 int64_t bootTimeMs = StatsHelper::GetBootTimeMs();
235 auto core = DelayedStatsSpSingleton<BatteryStatsService>::GetInstance()->GetBatteryStatsCore();
236 std::string debugInfo;
237 switch (data.type) {
238 case StatsUtils::STATS_TYPE_THERMAL:
239 HandleThermalInfo(data, bootTimeMs, debugInfo);
240 break;
241 case StatsUtils::STATS_TYPE_BATTERY:
242 HandleBatteryInfo(data, bootTimeMs, debugInfo);
243 break;
244 case StatsUtils::STATS_TYPE_WORKSCHEDULER:
245 HandleWorkschedulerInfo(data, bootTimeMs, debugInfo);
246 break;
247 case StatsUtils::STATS_TYPE_WAKELOCK_HOLD:
248 HandleWakelockInfo(data, bootTimeMs, debugInfo);
249 break;
250 case StatsUtils::STATS_TYPE_DISPLAY:
251 case StatsUtils::STATS_TYPE_SCREEN_ON:
252 case StatsUtils::STATS_TYPE_SCREEN_BRIGHTNESS:
253 HandleDispalyInfo(data, bootTimeMs, debugInfo);
254 break;
255 case StatsUtils::STATS_TYPE_PHONE_ACTIVE:
256 case StatsUtils::STATS_TYPE_PHONE_DATA:
257 HandlePhoneInfo(data, bootTimeMs, debugInfo);
258 break;
259 case StatsUtils::STATS_TYPE_FLASHLIGHT_ON:
260 HandleFlashlightInfo(data, bootTimeMs, debugInfo);
261 break;
262 case StatsUtils::STATS_TYPE_DISTRIBUTEDSCHEDULER:
263 HandleDistributedSchedulerInfo(data, bootTimeMs, debugInfo);
264 break;
265 default:
266 STATS_HILOGD(COMP_SVC, "Invalid type");
267 break;
268 }
269 core->UpdateDebugInfo(debugInfo);
270 }
271 } // namespace PowerMgr
272 } // namespace OHOS
273