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 std::string eventState;
148 if (data.state == StatsUtils::STATS_STATE_ACTIVATED) {
149 eventState = "LOCK";
150 } else {
151 eventState = "UNLOCK";
152 }
153 debugInfo.append("\n")
154 .append("Wakelock event: UID = ")
155 .append(ToString(data.uid))
156 .append(", PID = ")
157 .append(ToString(data.pid))
158 .append(", wakelock type = ")
159 .append(ToString(data.eventDataType))
160 .append(", wakelock name = ")
161 .append(data.eventDataName)
162 .append(", wakelock state = ")
163 .append(eventState)
164 .append(", boot time after boot = ")
165 .append(ToString(bootTimeMs))
166 .append("ms\n");
167 if (!data.eventDebugInfo.empty()) {
168 debugInfo.append("Additional debug info: ")
169 .append(data.eventDebugInfo);
170 }
171 }
172
HandleWorkschedulerInfo(StatsUtils::StatsData data,int64_t bootTimeMs,std::string & debugInfo)173 void BatteryStatsDetector::HandleWorkschedulerInfo(StatsUtils::StatsData data, int64_t bootTimeMs,
174 std::string& debugInfo)
175 {
176 debugInfo.append("WorkScheduler event: UID = ")
177 .append(ToString(data.uid))
178 .append(", PID = ")
179 .append(ToString(data.pid))
180 .append(", work type = ")
181 .append(ToString(data.eventDataType))
182 .append(", work interval = ")
183 .append(ToString(data.eventDataExtra))
184 .append(", work state = ")
185 .append(ToString(data.state))
186 .append(", boot time after boot = ")
187 .append(ToString(bootTimeMs))
188 .append("ms\n");
189 if (!data.eventDebugInfo.empty()) {
190 debugInfo.append("Additional debug info: ")
191 .append(data.eventDebugInfo);
192 }
193 }
194
HandlePhoneInfo(StatsUtils::StatsData data,int64_t bootTimeMs,std::string & debugInfo)195 void BatteryStatsDetector::HandlePhoneInfo(StatsUtils::StatsData data, int64_t bootTimeMs, std::string& debugInfo)
196 {
197 debugInfo.append("Phone event: Boot time after boot = ")
198 .append(ToString(bootTimeMs))
199 .append("ms\n");
200 if (!data.eventDebugInfo.empty()) {
201 debugInfo.append("Additional debug info: ")
202 .append(data.eventDebugInfo)
203 .append("\n");
204 }
205 }
206
HandleFlashlightInfo(StatsUtils::StatsData data,int64_t bootTimeMs,std::string & debugInfo)207 void BatteryStatsDetector::HandleFlashlightInfo(StatsUtils::StatsData data, int64_t bootTimeMs, std::string& debugInfo)
208 {
209 std::string eventState;
210 if (data.state == StatsUtils::STATS_STATE_ACTIVATED) {
211 eventState = "ON";
212 } else {
213 eventState = "OFF";
214 }
215 debugInfo.append("Flashlight event: UID = ")
216 .append(ToString(data.uid))
217 .append(", PID = ")
218 .append(ToString(data.pid))
219 .append(", flashlight state = ")
220 .append(eventState)
221 .append(", boot time after boot = ")
222 .append(ToString(bootTimeMs))
223 .append("ms\n");
224 }
225
HandleDistributedSchedulerInfo(StatsUtils::StatsData data,int64_t bootTimeMs,std::string & debugInfo)226 void BatteryStatsDetector::HandleDistributedSchedulerInfo(StatsUtils::StatsData data, int64_t bootTimeMs,
227 std::string& debugInfo)
228 {
229 debugInfo.append("Distributed schedule event")
230 .append(", boot time after boot = ")
231 .append(ToString(bootTimeMs))
232 .append("ms\n");
233 if (!data.eventDebugInfo.empty()) {
234 debugInfo.append("Additional debug info: ")
235 .append(data.eventDebugInfo)
236 .append("\n");
237 }
238 }
239
HandleDebugInfo(StatsUtils::StatsData data)240 void BatteryStatsDetector::HandleDebugInfo(StatsUtils::StatsData data)
241 {
242 int64_t bootTimeMs = StatsHelper::GetBootTimeMs();
243 auto core = DelayedStatsSpSingleton<BatteryStatsService>::GetInstance()->GetBatteryStatsCore();
244 std::string debugInfo;
245 switch (data.type) {
246 case StatsUtils::STATS_TYPE_THERMAL:
247 HandleThermalInfo(data, bootTimeMs, debugInfo);
248 break;
249 case StatsUtils::STATS_TYPE_BATTERY:
250 HandleBatteryInfo(data, bootTimeMs, debugInfo);
251 break;
252 case StatsUtils::STATS_TYPE_WORKSCHEDULER:
253 HandleWorkschedulerInfo(data, bootTimeMs, debugInfo);
254 break;
255 case StatsUtils::STATS_TYPE_WAKELOCK_HOLD:
256 HandleWakelockInfo(data, bootTimeMs, debugInfo);
257 break;
258 case StatsUtils::STATS_TYPE_DISPLAY:
259 case StatsUtils::STATS_TYPE_SCREEN_ON:
260 case StatsUtils::STATS_TYPE_SCREEN_BRIGHTNESS:
261 HandleDispalyInfo(data, bootTimeMs, debugInfo);
262 break;
263 case StatsUtils::STATS_TYPE_PHONE_ACTIVE:
264 case StatsUtils::STATS_TYPE_PHONE_DATA:
265 HandlePhoneInfo(data, bootTimeMs, debugInfo);
266 break;
267 case StatsUtils::STATS_TYPE_FLASHLIGHT_ON:
268 HandleFlashlightInfo(data, bootTimeMs, debugInfo);
269 break;
270 case StatsUtils::STATS_TYPE_DISTRIBUTEDSCHEDULER:
271 HandleDistributedSchedulerInfo(data, bootTimeMs, debugInfo);
272 break;
273 default:
274 STATS_HILOGD(COMP_SVC, "Invalid type");
275 break;
276 }
277 core->UpdateDebugInfo(debugInfo);
278 }
279 } // namespace PowerMgr
280 } // namespace OHOS
281