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 "cpu_time_reader.h"
17
18 #include <fstream>
19 #include "string_ex.h"
20
21 #include "battery_stats_service.h"
22 #include "stats_helper.h"
23 #include "stats_log.h"
24 #include "stats_utils.h"
25
26 namespace OHOS {
27 namespace PowerMgr {
28 namespace {
29 static const std::string UID_CPU_ACTIVE_TIME_FILE = "/proc/uid_concurrent_active_time";
30 static const std::string UID_CPU_CLUSTER_TIME_FILE = "/proc/uid_concurrent_policy_time";
31 static const std::string UID_CPU_FREQ_TIME_FILE = "/proc/uid_time_in_state";
32 static const std::string UID_CPU_TIME_FILE = "/proc/uid_cputime/show_uid_stat";
33 auto g_statsService = DelayedStatsSpSingleton<BatteryStatsService>::GetInstance();
34 } // namespace
Init()35 bool CpuTimeReader::Init()
36 {
37 if (!UpdateCpuTime()) {
38 STATS_HILOGW(COMP_SVC, "Update cpu time failed");
39 }
40 return true;
41 }
42
GetUidCpuActiveTimeMs(int32_t uid)43 int64_t CpuTimeReader::GetUidCpuActiveTimeMs(int32_t uid)
44 {
45 int64_t cpuActiveTime = 0;
46 auto iter = activeTimeMap_.find(uid);
47 if (iter != activeTimeMap_.end()) {
48 cpuActiveTime = iter->second;
49 STATS_HILOGD(COMP_SVC, "Get cpu active time: %{public}s for uid: %{public}d",
50 std::to_string(cpuActiveTime).c_str(), uid);
51 } else {
52 STATS_HILOGD(COMP_SVC, "No cpu active time found for uid: %{public}d, return 0", uid);
53 }
54 return cpuActiveTime;
55 }
56
DumpInfo(std::string & result,int32_t uid)57 void CpuTimeReader::DumpInfo(std::string& result, int32_t uid)
58 {
59 auto uidIter = lastUidTimeMap_.find(uid);
60 if (uidIter == lastUidTimeMap_.end()) {
61 STATS_HILOGE(COMP_SVC, "No related CPU info for uid: %{public}d", uid);
62 return;
63 }
64 std::string freqTime = "";
65 auto freqIter = lastFreqTimeMap_.find(uid);
66 if (freqIter != lastFreqTimeMap_.end()) {
67 for (auto timeIter = freqIter->second.begin(); timeIter != freqIter->second.end(); timeIter++) {
68 for (uint32_t i = 0; i < timeIter->second.size(); i++) {
69 freqTime.append(ToString(timeIter->second[i]))
70 .append(" ");
71 }
72 }
73 }
74 result.append("Total cpu time: userSpaceTime=")
75 .append(ToString(uidIter->second[0]))
76 .append("ms, systemSpaceTime=")
77 .append(ToString(uidIter->second[1]))
78 .append("ms\n")
79 .append("Total cpu time per freq: ")
80 .append(freqTime)
81 .append("\n");
82 }
83
GetUidCpuClusterTimeMs(int32_t uid,uint32_t cluster)84 int64_t CpuTimeReader::GetUidCpuClusterTimeMs(int32_t uid, uint32_t cluster)
85 {
86 int64_t cpuClusterTime = 0;
87 auto iter = clusterTimeMap_.find(uid);
88 if (iter != clusterTimeMap_.end()) {
89 auto cpuClusterTimeVector = iter->second;
90 if (cluster < cpuClusterTimeVector.size()) {
91 cpuClusterTime = cpuClusterTimeVector[cluster];
92 STATS_HILOGD(COMP_SVC, "Get cpu cluster time: %{public}s of cluster: %{public}d",
93 std::to_string(cpuClusterTime).c_str(), cluster);
94 } else {
95 STATS_HILOGD(COMP_SVC, "No cpu cluster time of cluster: %{public}d found, return 0", cluster);
96 }
97 } else {
98 STATS_HILOGD(COMP_SVC, "No cpu cluster time vector found for uid: %{public}d, return 0", uid);
99 }
100 return cpuClusterTime;
101 }
102
GetUidCpuFreqTimeMs(int32_t uid,uint32_t cluster,uint32_t speed)103 int64_t CpuTimeReader::GetUidCpuFreqTimeMs(int32_t uid, uint32_t cluster, uint32_t speed)
104 {
105 int64_t cpuFreqTime = 0;
106 auto uidIter = freqTimeMap_.find(uid);
107 if (uidIter != freqTimeMap_.end()) {
108 auto cpuFreqTimeMap = uidIter->second;
109 auto clusterIter = cpuFreqTimeMap.find(cluster);
110 if (clusterIter != cpuFreqTimeMap.end()) {
111 auto cpuFreqTimeVector = clusterIter->second;
112 if (speed < cpuFreqTimeVector.size()) {
113 cpuFreqTime = cpuFreqTimeVector[speed];
114 STATS_HILOGD(COMP_SVC, "Get cpu freq time: %{public}s of speed: %{public}d",
115 std::to_string(cpuFreqTime).c_str(), speed);
116 } else {
117 STATS_HILOGD(COMP_SVC, "No cpu freq time of speed: %{public}d found, return 0", speed);
118 }
119 } else {
120 STATS_HILOGD(COMP_SVC, "No cluster cpu freq time vector of cluster: %{public}d found, return 0",
121 cluster);
122 }
123 } else {
124 STATS_HILOGD(COMP_SVC, "No uid cpu freq time map found for uid: %{public}d, return 0", uid);
125 }
126 return cpuFreqTime;
127 }
128
GetUidCpuTimeMs(int32_t uid)129 std::vector<int64_t> CpuTimeReader::GetUidCpuTimeMs(int32_t uid)
130 {
131 std::vector<int64_t> cpuTimeVec;
132 auto iter = uidTimeMap_.find(uid);
133 if (iter != uidTimeMap_.end()) {
134 cpuTimeVec = iter->second;
135 STATS_HILOGD(COMP_SVC, "Get uid cpu time vector for uid: %{public}d, size: %{public}d", uid,
136 static_cast<int32_t>(cpuTimeVec.size()));
137 } else {
138 STATS_HILOGD(COMP_SVC, "No uid cpu time vector found for uid: %{public}d, return null", uid);
139 }
140 return cpuTimeVec;
141 }
142
UpdateCpuTime()143 bool CpuTimeReader::UpdateCpuTime()
144 {
145 bool result = true;
146 if (!ReadUidCpuClusterTime()) {
147 STATS_HILOGW(COMP_SVC, "Read uid cpu cluster time failed");
148 result = false;
149 }
150
151 if (!ReadUidCpuTime()) {
152 STATS_HILOGW(COMP_SVC, "Read uid cpu time failed");
153 result = false;
154 }
155
156 if (!ReadUidCpuActiveTime()) {
157 STATS_HILOGW(COMP_SVC, "Read uid cpu active time failed");
158 result = false;
159 }
160
161 if (!ReadUidCpuFreqTime()) {
162 STATS_HILOGW(COMP_SVC, "Read uid cpu freq time failed");
163 result = false;
164 }
165 return result;
166 }
167
ReadUidCpuActiveTimeImpl(std::string & line,int32_t uid)168 bool CpuTimeReader::ReadUidCpuActiveTimeImpl(std::string& line, int32_t uid)
169 {
170 int64_t timeMs = 0;
171 std::vector<std::string> splitedTime;
172 Split(line, ' ', splitedTime);
173 for (uint16_t i = 0; i < splitedTime.size(); i++) {
174 timeMs += stoll(splitedTime[i]) * 10; // Unit is 10ms
175 }
176
177 int64_t increment = 0;
178 if (timeMs > 0) {
179 auto iterLast = lastActiveTimeMap_.find(uid);
180 if (iterLast != lastActiveTimeMap_.end()) {
181 increment = timeMs - iterLast->second;
182 if (increment >= 0) {
183 iterLast->second = timeMs;
184 } else {
185 STATS_HILOGI(COMP_SVC, "Negative cpu active time increment");
186 return false;
187 }
188 } else {
189 lastActiveTimeMap_.insert(std::pair<int32_t, int64_t>(uid, timeMs));
190 increment = timeMs;
191 }
192 }
193
194 if (StatsHelper::IsOnBattery()) {
195 STATS_HILOGD(COMP_SVC, "Power supply is not connected. Add the increment");
196 auto iter = activeTimeMap_.find(uid);
197 if (iter != activeTimeMap_.end()) {
198 iter->second += increment;
199 } else {
200 activeTimeMap_.insert(std::pair<int32_t, int64_t>(uid, increment));
201 STATS_HILOGI(COMP_SVC, "Add active time: %{public}sms, uid: %{public}d",
202 std::to_string(increment).c_str(), uid);
203 }
204 }
205 return true;
206 }
207
ReadUidCpuActiveTime()208 bool CpuTimeReader::ReadUidCpuActiveTime()
209 {
210 std::ifstream input(UID_CPU_ACTIVE_TIME_FILE);
211 if (!input) {
212 STATS_HILOGW(COMP_SVC, "Open file failed");
213 return false;
214 }
215
216 std::string line;
217 const int32_t INDEX_0 = 0;
218 const int32_t INDEX_1 = 1;
219 while (getline(input, line)) {
220 int32_t uid = StatsUtils::INVALID_VALUE;
221 std::vector<std::string> splitedLine;
222 Split(line, ':', splitedLine);
223 if (splitedLine[INDEX_0] == "cpus") {
224 continue;
225 } else {
226 uid = stoi(splitedLine[INDEX_0]);
227 }
228
229 if (uid > StatsUtils::INVALID_VALUE) {
230 auto uidEntity =
231 g_statsService->GetBatteryStatsCore()->GetEntity(BatteryStatsInfo::CONSUMPTION_TYPE_APP);
232 if (uidEntity) {
233 uidEntity->UpdateUidMap(uid);
234 }
235 }
236
237 if (ReadUidCpuActiveTimeImpl(splitedLine[INDEX_1], uid)) {
238 continue;
239 } else {
240 return false;
241 }
242 }
243 return true;
244 }
245
ReadPolicy(std::vector<uint16_t> & clusters,std::string & line)246 void CpuTimeReader::ReadPolicy(std::vector<uint16_t>& clusters, std::string& line)
247 {
248 std::vector<std::string> splitedPolicy;
249 Split(line, ' ', splitedPolicy);
250 uint32_t step = 2;
251 for (uint32_t i = 0; i < splitedPolicy.size(); i += step) {
252 uint16_t coreNum = static_cast<uint16_t>(stoi(splitedPolicy[i + 1]));
253 clusters.push_back(coreNum);
254 clustersMap_.insert(std::pair<uint16_t, uint16_t>(i, coreNum));
255 }
256 }
257
ReadClusterTimeIncrement(std::vector<int64_t> & clusterTime,std::vector<int64_t> & increments,int32_t uid,std::vector<uint16_t> & clusters,std::string & timeLine)258 bool CpuTimeReader::ReadClusterTimeIncrement(std::vector<int64_t>& clusterTime, std::vector<int64_t>& increments,
259 int32_t uid, std::vector<uint16_t>& clusters, std::string& timeLine)
260 {
261 std::vector<std::string> splitedTime;
262 Split(timeLine, ' ', splitedTime);
263 uint16_t count = 0;
264 for (uint16_t i = 0; i < clusters.size(); i++) {
265 int64_t tempTimeMs = 0;
266 for (uint16_t j = 0; j < clusters[i]; j++) {
267 tempTimeMs += stoll(splitedTime[count++]) * 10; // Unit is 10ms
268 }
269 clusterTime.push_back(tempTimeMs);
270 }
271
272 auto iterLast = lastClusterTimeMap_.find(uid);
273 if (iterLast != lastClusterTimeMap_.end()) {
274 for (uint16_t i = 0; i < clusters.size(); i++) {
275 int64_t increment = clusterTime[i] - iterLast->second[i];
276 if (increment >= 0) {
277 iterLast->second[i] = clusterTime[i];
278 increments.push_back(increment);
279 } else {
280 STATS_HILOGD(COMP_SVC, "Negative cpu cluster time increment");
281 return false;
282 }
283 }
284 } else {
285 lastClusterTimeMap_.insert(std::pair<int32_t, std::vector<int64_t>>(uid, clusterTime));
286 increments = clusterTime;
287 STATS_HILOGI(COMP_SVC, "Add last cpu cluster time for uid: %{public}d", uid);
288 }
289 return true;
290 }
291
ReadUidCpuClusterTime()292 bool CpuTimeReader::ReadUidCpuClusterTime()
293 {
294 std::ifstream input(UID_CPU_CLUSTER_TIME_FILE);
295 if (!input) {
296 STATS_HILOGW(COMP_SVC, "Open file failed");
297 return false;
298 }
299 std::string line;
300 int32_t uid = -1;
301 std::vector<uint16_t> clusters;
302 std::vector<int64_t> clusterTime;
303 while (getline(input, line)) {
304 clusterTime.clear();
305 if (line.find("policy") != line.npos) {
306 ReadPolicy(clusters, line);
307 continue;
308 }
309
310 std::vector<std::string> splitedLine;
311 Split(line, ':', splitedLine);
312 uid = stoi(splitedLine[0]);
313 if (uid > StatsUtils::INVALID_VALUE) {
314 auto uidEntity = g_statsService->GetBatteryStatsCore()->GetEntity(BatteryStatsInfo::CONSUMPTION_TYPE_APP);
315 if (uidEntity) {
316 uidEntity->UpdateUidMap(uid);
317 }
318 }
319
320 std::vector<int64_t> increments;
321 if (!ReadClusterTimeIncrement(clusterTime, increments, uid, clusters, splitedLine[1])) {
322 return false;
323 }
324
325 if (StatsHelper::IsOnBattery()) {
326 STATS_HILOGD(COMP_SVC, "Power supply is not connected. Add the increment");
327 auto iter = clusterTimeMap_.find(uid);
328 if (iter != clusterTimeMap_.end()) {
329 for (uint16_t i = 0; i < clusters.size(); i++) {
330 iter->second[i] += increments[i];
331 }
332 } else {
333 clusterTimeMap_.insert(std::pair<int32_t, std::vector<int64_t>>(uid, increments));
334 STATS_HILOGI(COMP_SVC, "Add cpu cluster time for uid: %{public}d", uid);
335 }
336 }
337 }
338 return true;
339 }
340
ProcessFreqTime(std::map<uint32_t,std::vector<int64_t>> & map,std::map<uint32_t,std::vector<int64_t>> & increments,std::map<uint32_t,std::vector<int64_t>> & speedTime,int32_t index,int32_t uid)341 bool CpuTimeReader::ProcessFreqTime(std::map<uint32_t, std::vector<int64_t>>& map, std::map<uint32_t,
342 std::vector<int64_t>>& increments, std::map<uint32_t, std::vector<int64_t>>& speedTime, int32_t index, int32_t uid)
343 {
344 auto iterLastTemp = map.find(index);
345 if (iterLastTemp != map.end()) {
346 std::vector<int64_t> lastSpeedTimes = iterLastTemp->second;
347 std::vector<int64_t> newIncrementTimes;
348 newIncrementTimes.clear();
349 for (uint16_t j = 0; j < lastSpeedTimes.size(); j++) {
350 int64_t increment = speedTime.at(index)[j] - lastSpeedTimes[j];
351 if (increment >= 0) {
352 newIncrementTimes.push_back(increment);
353 increments.insert(std::pair<uint32_t, std::vector<int64_t>>(index, newIncrementTimes));
354 } else {
355 STATS_HILOGI(COMP_SVC, "Negative cpu freq time increment");
356 return false;
357 }
358 }
359 iterLastTemp->second = speedTime.at(index);
360 }
361 return true;
362 }
363
ReadFreqTimeIncrement(std::map<uint32_t,std::vector<int64_t>> & speedTime,std::map<uint32_t,std::vector<int64_t>> & increments,int32_t uid,std::vector<std::string> & splitedTime)364 bool CpuTimeReader::ReadFreqTimeIncrement(std::map<uint32_t, std::vector<int64_t>>& speedTime,
365 std::map<uint32_t, std::vector<int64_t>>& increments, int32_t uid, std::vector<std::string>& splitedTime)
366 {
367 auto parser = g_statsService->GetBatteryStatsParser();
368 uint16_t clusterNum = parser->GetClusterNum();
369 uint16_t count = 0;
370 for (uint16_t i = 0; i < clusterNum; i++) {
371 std::vector<int64_t> tempSpeedTimes;
372 tempSpeedTimes.clear();
373 for (uint16_t j = 0; j < parser->GetSpeedNum(i); j++) {
374 int64_t tempTimeMs = stoll(splitedTime[count++]) * 10; // Unit is 10ms
375 tempSpeedTimes.push_back(tempTimeMs);
376 }
377 speedTime.insert(std::pair<uint32_t, std::vector<int64_t>>(i, tempSpeedTimes));
378 }
379
380 auto iterLast = lastFreqTimeMap_.find(uid);
381 if (iterLast == lastFreqTimeMap_.end()) {
382 lastFreqTimeMap_.insert(std::pair<int32_t, std::map<uint32_t, std::vector<int64_t>>>(uid, speedTime));
383 increments = speedTime;
384 STATS_HILOGI(COMP_SVC, "Add last cpu freq time for uid: %{public}d", uid);
385 return true;
386 }
387 for (uint16_t i = 0; i < lastFreqTimeMap_.size(); i++) {
388 if (!ProcessFreqTime(iterLast->second, increments, speedTime, i, uid)) {
389 return false;
390 }
391 }
392 return true;
393 }
394
DistributeFreqTime(std::map<uint32_t,std::vector<int64_t>> & uidIncrements,std::map<uint32_t,std::vector<int64_t>> & increments)395 void CpuTimeReader::DistributeFreqTime(std::map<uint32_t, std::vector<int64_t>>& uidIncrements,
396 std::map<uint32_t, std::vector<int64_t>>& increments)
397 {
398 auto parser = g_statsService->GetBatteryStatsParser();
399 uint16_t clusterNum = parser->GetClusterNum();
400 if (wakelockCounts_ > 0) {
401 for (uint16_t i = 0; i < clusterNum; i++) {
402 uint16_t speedNum = parser->GetSpeedNum(i);
403 for (uint16_t j = 0; j < speedNum; j++) {
404 int32_t step = 2;
405 uidIncrements.at(i)[j] = increments.at(i)[j] / step;
406 }
407 }
408 // TO-DO, distribute half of cpu freq time to wakelock holders
409 }
410 }
411
AddFreqTimeToUid(std::map<uint32_t,std::vector<int64_t>> & uidIncrements,int32_t uid)412 void CpuTimeReader::AddFreqTimeToUid(std::map<uint32_t, std::vector<int64_t>>& uidIncrements, int32_t uid)
413 {
414 auto parser = g_statsService->GetBatteryStatsParser();
415 uint16_t clusterNum = parser->GetClusterNum();
416 auto iter = freqTimeMap_.find(uid);
417 if (iter != freqTimeMap_.end()) {
418 for (uint16_t i = 0; i < clusterNum; i++) {
419 uint16_t speedNum = parser->GetSpeedNum(i);
420 for (uint16_t j = 0; j < speedNum; j++) {
421 iter->second.at(i)[j] += uidIncrements.at(i)[j];
422 }
423 }
424 } else {
425 freqTimeMap_.insert(std::pair<int32_t, std::map<uint32_t, std::vector<int64_t>>>(uid, uidIncrements));
426 STATS_HILOGI(COMP_SVC, "Add cpu freq time for uid: %{public}d", uid);
427 }
428 }
429
ReadUidCpuFreqTime()430 bool CpuTimeReader::ReadUidCpuFreqTime()
431 {
432 std::ifstream input(UID_CPU_FREQ_TIME_FILE);
433 if (!input) {
434 STATS_HILOGW(COMP_SVC, "Open file failed");
435 return false;
436 }
437 std::string line;
438 int32_t uid = -1;
439 std::map<uint32_t, std::vector<int64_t>> speedTime;
440 while (getline(input, line)) {
441 speedTime.clear();
442 std::vector<std::string> splitedLine;
443 Split(line, ':', splitedLine);
444 if (splitedLine[0] == "uid") {
445 continue;
446 } else {
447 uid = stoi(splitedLine[0]);
448 }
449
450 if (uid > StatsUtils::INVALID_VALUE) {
451 auto uidEntity =
452 g_statsService->GetBatteryStatsCore()->GetEntity(BatteryStatsInfo::CONSUMPTION_TYPE_APP);
453 if (uidEntity) {
454 uidEntity->UpdateUidMap(uid);
455 }
456 }
457
458 std::vector<std::string> splitedTime;
459 Split(splitedLine[1], ' ', splitedTime);
460
461 std::map<uint32_t, std::vector<int64_t>> increments;
462 if (!ReadFreqTimeIncrement(speedTime, increments, uid, splitedTime)) {
463 return false;
464 }
465
466 std::map<uint32_t, std::vector<int64_t>> uidIncrements = increments;
467 DistributeFreqTime(uidIncrements, increments);
468
469 if (!StatsHelper::IsOnBattery()) {
470 STATS_HILOGD(COMP_SVC, "Power supply is connected, don't add the increment");
471 continue;
472 }
473 AddFreqTimeToUid(uidIncrements, uid);
474 }
475 return true;
476 }
477
ReadUidTimeIncrement(std::vector<int64_t> & cpuTime,std::vector<int64_t> & uidIncrements,int32_t uid,std::string & timeLine)478 bool CpuTimeReader::ReadUidTimeIncrement(std::vector<int64_t>& cpuTime, std::vector<int64_t>& uidIncrements,
479 int32_t uid, std::string& timeLine)
480 {
481 std::vector<std::string> splitedTime;
482 Split(timeLine, ' ', splitedTime);
483 for (uint16_t i = 0; i < splitedTime.size(); i++) {
484 int64_t tempTime = 0;
485 tempTime = stoll(splitedTime[i]);
486 cpuTime.push_back(tempTime);
487 }
488
489 std::vector<int64_t> increments;
490 auto iterLast = lastUidTimeMap_.find(uid);
491 if (iterLast != lastUidTimeMap_.end()) {
492 for (uint16_t i = 0; i < splitedTime.size(); i++) {
493 int64_t increment = 0;
494 increment = cpuTime[i] - iterLast->second[i];
495 if (increment >= 0) {
496 iterLast->second[i] = cpuTime[i];
497 increments.push_back(increment);
498 } else {
499 STATS_HILOGI(COMP_SVC, "Negative cpu time increment");
500 return false;
501 }
502 }
503 } else {
504 lastUidTimeMap_.insert(std::pair<int32_t, std::vector<int64_t>>(uid, cpuTime));
505 increments = cpuTime;
506 STATS_HILOGI(COMP_SVC, "Add last cpu time for uid: %{public}d", uid);
507 }
508
509 uidIncrements = increments;
510
511 if (wakelockCounts_ > 0) {
512 double weight = 0.5;
513 uidIncrements[0] = increments[0] / (StatsUtils::US_IN_MS * 1.0) * weight;
514 uidIncrements[1] = increments[1] / (StatsUtils::US_IN_MS * 1.0) * weight;
515 // TO-DO, distribute half of cpu time to wakelock holders
516 }
517 return true;
518 }
519
ReadUidCpuTime()520 bool CpuTimeReader::ReadUidCpuTime()
521 {
522 std::ifstream input(UID_CPU_TIME_FILE);
523 if (!input) {
524 STATS_HILOGW(COMP_SVC, "Open file failed");
525 return false;
526 }
527 std::string line;
528 std::vector<int64_t> cpuTime;
529 while (getline(input, line)) {
530 cpuTime.clear();
531 std::vector<std::string> splitedLine;
532 Split(line, ':', splitedLine);
533 int32_t uid = stoi(splitedLine[0]);
534 if (uid > StatsUtils::INVALID_VALUE) {
535 auto uidEntity =
536 g_statsService->GetBatteryStatsCore()->GetEntity(BatteryStatsInfo::CONSUMPTION_TYPE_APP);
537 if (uidEntity) {
538 uidEntity->UpdateUidMap(uid);
539 }
540 }
541
542 std::vector<int64_t> uidIncrements;
543 if (!ReadUidTimeIncrement(cpuTime, uidIncrements, uid, splitedLine[1])) {
544 return false;
545 }
546
547 if (StatsHelper::IsOnBattery()) {
548 STATS_HILOGD(COMP_SVC, "Power supply is not connected. Add the increment");
549 auto iter = uidTimeMap_.find(uid);
550 if (iter != uidTimeMap_.end()) {
551 for (uint16_t i = 0; i < uidIncrements.size(); i++) {
552 iter->second[i] = uidIncrements[i];
553 }
554 } else {
555 uidTimeMap_.insert(std::pair<int32_t, std::vector<int64_t>>(uid, uidIncrements));
556 STATS_HILOGI(COMP_SVC, "Add cpu time for uid: %{public}d", uid);
557 }
558 }
559 }
560 return true;
561 }
562
Split(std::string & origin,char delimiter,std::vector<std::string> & splited)563 void CpuTimeReader::Split(std::string &origin, char delimiter, std::vector<std::string> &splited)
564 {
565 size_t start;
566 size_t end = 0;
567
568 while ((start = origin.find_first_not_of(delimiter, end)) != std::string::npos) {
569 end = origin.find(delimiter, start);
570 splited.push_back(origin.substr(start, end - start));
571 }
572 }
573 } // namespace PowerMgr
574 } // namespace OHOS