Rename the class Json to avoid symbol conflicts with Jsoncpp
[platform/core/context/context-provider.git] / src / battery-stats / BatteryUsageAnalyzer.cpp
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <map>
18 #include <Types.h>
19 #include "BatteryUsageAnalyzer.h"
20
21 using namespace ctx;
22
23 BatteryUsageAnalyzer::BatteryUsageAnalyzer()
24 {
25 }
26
27 BatteryUsageAnalyzer::~BatteryUsageAnalyzer()
28 {
29 }
30
31 void BatteryUsageAnalyzer::calculateBatteryUsage(std::vector<CtxJson1>& cpuLog, CpuUsageMap& recentCpuUsageMap, std::vector<CtxJson1>* usage)
32 {
33         std::string appId;
34         int timestamp;
35         int stime;
36         int utime;
37         int idx;
38         int pid;
39
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, &timestamp);
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);
47
48                 //If CPU table is reset, clear last cpu usage
49                 if (idx == 0) {
50                         recentCpuUsageMap.clear();
51                 }
52
53                 int prevTimestamp = timestamp;
54                 int prevUtime = 0;
55                 int prevStime = 0;
56
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;
64                         }
65                 }
66
67                 // Process duplicated logs
68                 if (utime - prevUtime == 0 && stime - prevStime == 0) {
69                         recentCpuUsageMap[appId].timestamp = timestamp;
70                         continue;
71                 }
72
73                 // Process invalid logs
74                 if (utime - prevUtime < 0 || stime - prevStime < 0)
75                         continue;
76
77                 if (prevTimestamp > timestamp)
78                         prevTimestamp = timestamp;
79
80                 CtxJson1 row;
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);
86
87                 usage->push_back(row);
88
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;
96         }
97 }