Fixed to compare pid, instead of using cpu time difference, when an app is re-launced. 27/103727/3
authorKunal <kunal.chawla@samsung.com>
Fri, 9 Dec 2016 08:14:00 +0000 (17:14 +0900)
committerSomin Kim <somin926.kim@samsung.com>
Mon, 12 Dec 2016 06:43:00 +0000 (22:43 -0800)
Change-Id: Ib2c72bd5634d2555ccdf517985c6711e4818325c
Signed-off-by: Kunal <kunal.chawla@samsung.com>
src/battery-stats/BatteryMonitor.cpp
src/battery-stats/BatteryStatisticsTypes.h
src/battery-stats/BatteryUsageAnalyzer.cpp
src/battery-stats/HeartDbReader.cpp

index 7098c9fa3cd995669ac0cf1581e376002dd79845..6e1687eac31623e3922433e45e80b079990e3859 100644 (file)
@@ -224,18 +224,21 @@ bool BatteryMonitor::__getLastCpuUsageTable(CpuUsageMap* lastCpuUsage)
        int timestamp;
        int stime;
        int utime;
+       int pid;
 
        while (k < lastCpuUsageLog.size()) {
                lastCpuUsageLog[k].get(NULL, BATTERY_APP_ID, &appId);
                lastCpuUsageLog[k].get(NULL, BATTERY_TIMESTAMP, &timestamp);
                lastCpuUsageLog[k].get(NULL, BATTERY_UTIME, &utime);
                lastCpuUsageLog[k].get(NULL, BATTERY_STIME, &stime);
+               lastCpuUsageLog[k].get(NULL, BATTERY_PID, &pid);
                k++;
 
                LastAppCpuUsageInfo lastAppCpuUsage;
                lastAppCpuUsage.timestamp = timestamp;
                lastAppCpuUsage.utime = utime;
                lastAppCpuUsage.stime = stime;
+               lastAppCpuUsage.pid = pid;
                (*lastCpuUsage)[appId] = lastAppCpuUsage;
        }
        return true;
@@ -361,10 +364,10 @@ bool BatteryMonitor::__insertLastCpuUsageLog(CpuUsageMap& usage)
        _D("Delete all rows from app last times table");
        __dbMgr.executeSync("DELETE FROM " BATTERY_LAST_CPU_USAGE_TABLE, NULL);
 
-       std::string query("INSERT INTO " BATTERY_LAST_CPU_USAGE_TABLE "(" BATTERY_APP_ID ", " BATTERY_UTIME ", " BATTERY_STIME "," BATTERY_TIMESTAMP ") VALUES");
+       std::string query("INSERT INTO " BATTERY_LAST_CPU_USAGE_TABLE "(" BATTERY_APP_ID ", " BATTERY_UTIME ", " BATTERY_STIME ", " BATTERY_TIMESTAMP ", " BATTERY_PID ") VALUES");
 
        std::string appId;
-       int stime, utime;
+       int stime, utime, pid;
        int timestamp;
 
        for (auto it = usage.begin(); it != usage.end(); it++) {
@@ -372,11 +375,13 @@ bool BatteryMonitor::__insertLastCpuUsageLog(CpuUsageMap& usage)
                timestamp = (it->second).timestamp;
                utime = (it->second).utime;
                stime = (it->second).stime;
+               pid = (it->second).pid;
 
                query += " ('" + appId
                                + "', " + std::to_string(utime)
                                + ", " + std::to_string(stime)
-                               + ", " + std::to_string(timestamp) + ")";
+                               + ", " + std::to_string(timestamp)
+                               + ", " + std::to_string(pid) + ")";
 
                query += ", ";
        }
index d22faa737ce21bc13307aeb8c3d1dedb892caa78..31622bcab1208d2468e19746db0e0e02edffd536 100644 (file)
@@ -44,7 +44,7 @@
 #define BATTERY_LAST_CPU_USAGE_TABLE "Temp_LastCpuUsagePerApp"
 #define BATTERY_LAST_CPU_USAGE_TABLE_COLUMNS \
        "AppId TEXT PRIMARY KEY, Utime INTEGER NOT NULL DEFAULT 0, " \
-       "Stime INTEGER NOT NULL DEFAULT 0, Timestamp INTEGER NOT NULL DEFAULT 0"
+       "Stime INTEGER NOT NULL DEFAULT 0, Timestamp INTEGER NOT NULL DEFAULT 0, Pid INTEGER NOT NULL DEFAULT 0"
 
 #define BATTERY_APP_ID "AppId"
 #define BATTERY_INDEX "idx"
@@ -61,6 +61,7 @@
 #define BATTERY_LAST_PERCENT_TIME "LastPercentTime"
 #define BATTERY_LAST_HEART_TIMESTAMP "LastHeartTimestamp"
 #define BATTERY_ROW_ID "RowId"
+#define BATTERY_PID "Pid"
 
 #define CURRENT_TIME (int)(time(0))
 
@@ -70,6 +71,7 @@ namespace ctx {
                int timestamp;
                int utime;
                int stime;
+               int pid;
        };
        typedef std::map<std::string, LastAppCpuUsageInfo> CpuUsageMap;
 }
index 66dd430ae10ed39b7823967a12510991811e6adb..9a01f02046e98fffac1952785a49ee5462ebc9b4 100644 (file)
@@ -48,6 +48,7 @@ bool BatteryUsageAnalyzer::calculateBatteryUsage(int lastTime, int currTime, std
        int stime;
        int utime;
        int idx;
+       int pid;
        int totalUtime = 0;
        int totalStime = 0;
 
@@ -57,6 +58,7 @@ bool BatteryUsageAnalyzer::calculateBatteryUsage(int lastTime, int currTime, std
                cpuUsageLog[i].get(NULL, BATTERY_UTIME, &utime);
                cpuUsageLog[i].get(NULL, BATTERY_STIME, &stime);
                cpuUsageLog[i].get(NULL, BATTERY_INDEX, &idx);
+               cpuUsageLog[i].get(NULL, BATTERY_PID, &pid);
 
                if (timestamp < lastTime) {
                        continue;
@@ -75,14 +77,16 @@ bool BatteryUsageAnalyzer::calculateBatteryUsage(int lastTime, int currTime, std
                int prevUtime = 0;
                int prevStime = 0;
                int prevTimestamp = 0;
+               int prevPid = 0;
 
                if (lastAppCpuUsage != lastCpuUsage.end()) {
                        prevTimestamp = (lastAppCpuUsage->second).timestamp;
                        prevUtime = (lastAppCpuUsage->second).utime;
                        prevStime = (lastAppCpuUsage->second).stime;
+                       prevPid = (lastAppCpuUsage->second).pid;
 
                        // If the app is killed and launched again
-                       if (prevStime > stime || prevUtime > utime) {
+                       if (pid != prevPid) {
                                prevUtime = 0;
                                prevStime = 0;
                                prevTimestamp = lastTime;
@@ -118,6 +122,7 @@ bool BatteryUsageAnalyzer::calculateBatteryUsage(int lastTime, int currTime, std
                lastAppUsage.timestamp = timestamp;
                lastAppUsage.utime = utime;
                lastAppUsage.stime = stime;
+               lastAppUsage.pid = pid;
                lastCpuUsage[appId] = lastAppUsage;
        }
        __normalizeBatteryUsage(usage, totalUtime, totalStime);
index 456d0d51df0068f3eb82c552bb3ae2c550423d6f..b19a5a05be52384bae4e08f4bde702fcaa6ae0cf 100644 (file)
@@ -132,4 +132,5 @@ void HeartDbReader::__convertCpuUsageLog(Json& row, Json* newRow)
        newRow->set(NULL, BATTERY_STIME, stime);
        newRow->set(NULL, BATTERY_TYPE, type);
        newRow->set(NULL, BATTERY_INDEX, index);
+       newRow->set(NULL, BATTERY_PID, pid);
 }