2 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include "BatteryUsageAnalyzer.h"
23 BatteryUsageAnalyzer::BatteryUsageAnalyzer()
27 BatteryUsageAnalyzer::~BatteryUsageAnalyzer()
31 void BatteryUsageAnalyzer::calculateBatteryUsage(std::vector<CtxJson1>& cpuLog, CpuUsageMap& recentCpuUsageMap, std::vector<CtxJson1>* usage)
40 for (unsigned int i = 0; i < cpuLog.size(); i++) {
41 cpuLog[i].get(NULL, BATTERY_APP_ID, &appId);
42 cpuLog[i].get(NULL, BATTERY_TIMESTAMP, ×tamp);
43 cpuLog[i].get(NULL, BATTERY_UTIME, &utime);
44 cpuLog[i].get(NULL, BATTERY_STIME, &stime);
45 cpuLog[i].get(NULL, BATTERY_INDEX, &idx);
46 cpuLog[i].get(NULL, BATTERY_PID, &pid);
48 //If CPU table is reset, clear last cpu usage
50 recentCpuUsageMap.clear();
53 int prevTimestamp = timestamp;
57 auto lastAppCpuUsage = recentCpuUsageMap.find(appId);
58 if (lastAppCpuUsage != recentCpuUsageMap.end()) {
59 // If the last info of the app exists
60 if ((lastAppCpuUsage->second).pid == pid) {
61 prevTimestamp = (lastAppCpuUsage->second).timestamp;
62 prevUtime = (lastAppCpuUsage->second).utime;
63 prevStime = (lastAppCpuUsage->second).stime;
67 // Process duplicated logs
68 if (utime - prevUtime == 0 && stime - prevStime == 0) {
69 recentCpuUsageMap[appId].timestamp = timestamp;
73 // Process invalid logs
74 if (utime - prevUtime < 0 || stime - prevStime < 0)
77 if (prevTimestamp > timestamp)
78 prevTimestamp = timestamp;
81 row.set(NULL, BATTERY_APP_ID, appId);
82 row.set(NULL, BATTERY_START_TIME, prevTimestamp);
83 row.set(NULL, BATTERY_END_TIME, timestamp);
84 row.set(NULL, BATTERY_UTIME, utime - prevUtime);
85 row.set(NULL, BATTERY_STIME, stime - prevStime);
87 usage->push_back(row);
89 // Store recent cpu usage info of the app
90 CpuLogInfo currentAppInfo;
91 currentAppInfo.timestamp = timestamp;
92 currentAppInfo.utime = utime;
93 currentAppInfo.stime = stime;
94 currentAppInfo.pid = pid;
95 recentCpuUsageMap[appId] = currentAppInfo;