From e736528949fa2e42b169afaf303d924d2ba9009f Mon Sep 17 00:00:00 2001 From: Kunal Date: Thu, 29 Sep 2016 15:53:56 +0900 Subject: [PATCH] Battery Stats Algorithm modified to include CPU Usage of both background and foreground app events Applied code review comments and coding rules, and bug fixes Change-Id: I264d5b8350da9b6322b5fcbccb265130ffa9b86d Signed-off-by: Kunal --- src/battery-stats/BatteryMonitor.cpp | 130 +++++++++++++++++---- src/battery-stats/BatteryMonitor.h | 4 +- src/battery-stats/BatteryStatisticsTypes.h | 29 ++++- src/battery-stats/BatteryUsageAnalyzer.cpp | 109 +++++++++++------ src/battery-stats/BatteryUsageAnalyzer.h | 5 +- src/battery-stats/HeartDbReader.cpp | 32 ++--- src/battery-stats/HeartDbReader.h | 6 +- 7 files changed, 232 insertions(+), 83 deletions(-) diff --git a/src/battery-stats/BatteryMonitor.cpp b/src/battery-stats/BatteryMonitor.cpp index 6c9ba5d..4352019 100644 --- a/src/battery-stats/BatteryMonitor.cpp +++ b/src/battery-stats/BatteryMonitor.cpp @@ -20,7 +20,6 @@ #include #include #include -#include "BatteryStatisticsTypes.h" #include "HeartDbReader.h" #include "BatteryMonitor.h" @@ -31,6 +30,7 @@ using namespace ctx; static int __lastResetTime = 0; static int __lastPercent = -1; static int __lastPercentTime = 0; +static int __lastHeartTimestamp = 0; static bool __isCharging = 0; static std::vector __batteryTimeInfoVec; @@ -55,6 +55,10 @@ void BatteryMonitor::__initialize() "CREATE TABLE IF NOT EXISTS " BATTERY_LAST_INFO_TABLE \ " (" BATTERY_LAST_INFO_TABLE_COLUMNS ")", &records); + __dbMgr.executeSync( + "CREATE TABLE IF NOT EXISTS " BATTERY_LAST_CPU_USAGE_TABLE \ + " (" BATTERY_LAST_CPU_USAGE_TABLE_COLUMNS ")", &records); + __dbMgr.createTableSync(BATTERY_TEMP_TIME_INFO, BATTERY_TEMP_TIME_INFO_COLUMNS, NULL); } @@ -72,11 +76,12 @@ bool BatteryMonitor::__loadLastInfo() records[0].get(NULL, BATTERY_LAST_RESET_TIME, &__lastResetTime); records[0].get(NULL, BATTERY_LAST_PERCENT, &__lastPercent); records[0].get(NULL, BATTERY_LAST_PERCENT_TIME, &__lastPercentTime); + records[0].get(NULL, BATTERY_LAST_HEART_TIMESTAMP, &__lastHeartTimestamp); } records.clear(); __batteryTimeInfoVec.clear(); - ret = __dbMgr.executeSync("SELECT * FROM " BATTERY_TEMP_TIME_INFO , &__batteryTimeInfoVec); + ret = __dbMgr.executeSync("SELECT * FROM " BATTERY_TEMP_TIME_INFO , &__batteryTimeInfoVec); ret = __dbMgr.executeSync("DELETE FROM " BATTERY_TEMP_TIME_INFO, &records); @@ -97,8 +102,8 @@ bool BatteryMonitor::__updateLastInfo() char *query = sqlite3_mprintf( "INSERT OR REPLACE INTO " BATTERY_LAST_INFO_TABLE " (" \ BATTERY_ROW_ID ", " BATTERY_LAST_RESET_TIME ", " \ - BATTERY_LAST_PERCENT ", " BATTERY_LAST_PERCENT_TIME ") VALUES (%s, %d, %d, %d)", - DEFAULT_ROW_ID_STR, __lastResetTime, __lastPercent, __lastPercentTime); + BATTERY_LAST_PERCENT ", " BATTERY_LAST_PERCENT_TIME ", " BATTERY_LAST_HEART_TIMESTAMP ") VALUES (%s, %d, %d, %d, %d)", + DEFAULT_ROW_ID_STR, __lastResetTime, __lastPercent, __lastPercentTime, __lastHeartTimestamp); std::vector records; bool ret = __dbMgr.executeSync(query, &records); @@ -181,6 +186,36 @@ int BatteryMonitor::stop() return ERR_NONE; } +bool BatteryMonitor::__getLastCpuUsageTable(CpuUsageMap* lastCpuUsage) +{ + std::vector lastCpuUsageLog; + bool ret = __dbMgr.executeSync( + "SELECT * FROM " BATTERY_LAST_CPU_USAGE_TABLE, + &lastCpuUsageLog); + IF_FAIL_RETURN_TAG(ret, false, _E, "Failed to load last Cpu Times of Apps"); + + unsigned int k = 0; + std::string appId; + int timestamp; + int stime; + int utime; + + while (k < lastCpuUsageLog.size()) { + lastCpuUsageLog[k].get(NULL, BATTERY_APP_ID, &appId); + lastCpuUsageLog[k].get(NULL, BATTERY_TIMESTAMP, ×tamp); + lastCpuUsageLog[k].get(NULL, BATTERY_UTIME, &utime); + lastCpuUsageLog[k].get(NULL, BATTERY_STIME, &stime); + k++; + + LastAppCpuUsageInfo lastAppCpuUsage; + lastAppCpuUsage.timestamp = timestamp; + lastAppCpuUsage.utime = utime; + lastAppCpuUsage.stime = stime; + (*lastCpuUsage)[appId] = lastAppCpuUsage; + } + return true; +} + bool BatteryMonitor::processBatteryUsage() { IF_FAIL_RETURN_TAG(__batteryTimeInfoVec.size() > 0, true, _D, "All per-app battery usages are already calculated"); @@ -195,16 +230,17 @@ bool BatteryMonitor::processBatteryUsage() bool ret = heartReader.open(); IF_FAIL_RETURN_TAG(ret, false, _E, "Failed to open heart db"); - std::vector appLog; - ret = heartReader.readAppLaunchLog(totalStartTime, totalEndTime, &appLog); + std::vector cpuUsageLog; + ret = heartReader.readCpuUsageLog(totalStartTime, totalEndTime, &cpuUsageLog); heartReader.close(); IF_FAIL_RETURN_TAG(ret, false, _E, "Cannot read from heart cpu table"); - IF_FAIL_RETURN_TAG(appLog.size() > 0, true, _W, "Heart cpu data is not prepared yet (%d ~ %d)", totalStartTime, totalEndTime); - _D("Read %d rows from heart cpu table from %d to %d", appLog.size(), totalStartTime, totalEndTime); + IF_FAIL_RETURN_TAG(cpuUsageLog.size() > 0, true, _W, "Heart cpu data is not prepared yet (%d ~ %d)", totalStartTime, totalEndTime); + _D("Read %d rows from heart cpu table from %d to %d", cpuUsageLog.size(), totalStartTime, totalEndTime); - // Get the last timestamp of HEART cpu data - int lastHeartTimestamp; - appLog.back().get(NULL, BATTERY_TIMESTAMP, &lastHeartTimestamp); + // Get the last timestamp of HEART cpu data and maximum app times tables in cache + cpuUsageLog.back().get(NULL, BATTERY_TIMESTAMP, &__lastHeartTimestamp); + CpuUsageMap lastCpuUsage; + ret = __getLastCpuUsageTable(&lastCpuUsage); unsigned int i; for (i = 0; i < __batteryTimeInfoVec.size(); i++) { @@ -215,14 +251,14 @@ bool BatteryMonitor::processBatteryUsage() row.get(NULL, BATTERY_START_TIME, &startTime); row.get(NULL, BATTERY_END_TIME, &endTime); - if (endTime > lastHeartTimestamp) { + if (endTime > __lastHeartTimestamp) { _W("[%d] Heart cpu data is not prepared yet (%d ~ %d)", i, startTime, endTime); break; } // Calculate per app battery usage std::vector usage; - ret = __analyzer.calculateBatteryUsage(startTime, endTime, appLog, &usage); + ret = __analyzer.calculateBatteryUsage(startTime, endTime, cpuUsageLog, lastCpuUsage, &usage); if (!ret) { _E("[%d] Failed to calculate battery usage (%d ~ %d)", i, startTime, endTime); continue; @@ -230,12 +266,20 @@ bool BatteryMonitor::processBatteryUsage() _D("[%d] Battery usage per app calculated (%d ~ %d)", i, startTime, endTime); // Insert battery usage - ret = __insertUsageLog(usage); + ret = __insertBatteryUsageLog(usage); if (!ret) { _E("Failed to insert per app battery usage"); } } + // Insert log of last times of apps + if (i != 0) { + ret = __insertLastCpuUsageLog(lastCpuUsage); + if (!ret) { + _E("Failed to insert last Cpu Usage of apps"); + } + } + // Remove completed time info _D("Total %d time intervals, %d intervals are calculated", __batteryTimeInfoVec.size(), i); __batteryTimeInfoVec.erase(__batteryTimeInfoVec.begin(), __batteryTimeInfoVec.begin() + i); @@ -243,14 +287,17 @@ bool BatteryMonitor::processBatteryUsage() return true; } -bool BatteryMonitor::__insertUsageLog(std::vector& usage) +bool BatteryMonitor::__insertBatteryUsageLog(std::vector& usage) { IF_FAIL_RETURN_TAG(usage.size(), true, _W, "No data"); - std::string query = std::string("INSERT INTO " BATTERY_USAGE_TABLE \ - "(" BATTERY_APP_ID ", " BATTERY_START_TIME ", " BATTERY_END_TIME "," BATTERY_AMOUNT ") VALUES"); + std::string query("INSERT INTO " BATTERY_USAGE_TABLE \ + "(" BATTERY_APP_ID ", " BATTERY_START_TIME ", " \ + BATTERY_END_TIME ", " BATTERY_UTIME ", " \ + BATTERY_STIME ", " BATTERY_AMOUNT ") VALUES"); std::string appId; int startTime; + int stime, utime; int endTime; double amount; @@ -258,19 +305,54 @@ bool BatteryMonitor::__insertUsageLog(std::vector& usage) usage[i].get(NULL, BATTERY_APP_ID, &appId); usage[i].get(NULL, BATTERY_START_TIME, &startTime); usage[i].get(NULL, BATTERY_END_TIME, &endTime); + usage[i].get(NULL, BATTERY_UTIME, &utime); + usage[i].get(NULL, BATTERY_STIME, &stime); usage[i].get(NULL, BATTERY_AMOUNT, &amount); - query += std::string(" ('" + appId + "', " + std::to_string(startTime) + ", " + - std::to_string(endTime) + ", " + std::to_string((int) (amount * 10000)) + ")"); + query += " ('" + appId + "', " + + std::to_string(startTime) + ", " + + std::to_string(endTime) + ", " + + std::to_string(utime) + ", " + + std::to_string(stime) + ", " + + std::to_string((int) (amount * 10000)) + ")"; - if (i != usage.size() - 1 ) { - query += ", "; - } + query += ", "; } + query = query.substr(0, query.size() - 2); _D("Insert %d rows of per app battery usage", usage.size()); - std::vector records; - return __dbMgr.executeSync(query.c_str(), &records); + return __dbMgr.executeSync(query.c_str(), NULL); +} + +bool BatteryMonitor::__insertLastCpuUsageLog(CpuUsageMap& usage) +{ + IF_FAIL_RETURN_TAG(usage.size(), true, _W, "No data"); + _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 appId; + int stime, utime; + int timestamp; + + for (auto it = usage.begin(); it != usage.end(); it++) { + appId = it->first; + timestamp = (it->second).timestamp; + utime = (it->second).utime; + stime = (it->second).stime; + + query += " ('" + appId + + "', " + std::to_string(utime) + + ", " + std::to_string(stime) + + ", " + std::to_string(timestamp) + ")"; + + query += ", "; + } + + _D("Insert %d rows in app last times table", usage.size()); + query = query.substr(0, query.size() - 2); + return __dbMgr.executeSync(query.c_str(), NULL); } int BatteryMonitor::getLastResetTime() diff --git a/src/battery-stats/BatteryMonitor.h b/src/battery-stats/BatteryMonitor.h index 361a08f..7ec34f8 100644 --- a/src/battery-stats/BatteryMonitor.h +++ b/src/battery-stats/BatteryMonitor.h @@ -43,7 +43,9 @@ namespace ctx { static void __batteryChangeCb(device_callback_e type, void* value, void* userData); static void __chargerChangeCb(device_callback_e type, void* value, void* userData); - bool __insertUsageLog(std::vector& usage); + bool __insertBatteryUsageLog(std::vector& usage); + bool __insertLastCpuUsageLog(CpuUsageMap& usage); + bool __getLastCpuUsageTable(CpuUsageMap* lastCpuUsage); DatabaseManager __dbMgr; BatteryUsageAnalyzer __analyzer; diff --git a/src/battery-stats/BatteryStatisticsTypes.h b/src/battery-stats/BatteryStatisticsTypes.h index dc43678..d22faa7 100644 --- a/src/battery-stats/BatteryStatisticsTypes.h +++ b/src/battery-stats/BatteryStatisticsTypes.h @@ -18,39 +18,62 @@ #define _CONTEXT_STATS_BATTERY_TYPES_H_ #include +#include #define BATTERY_HISTORY_PRIV PRIV_APP_HISTORY #define BATTERY_USAGE_TABLE "Log_BatteryUsagePerApp" #define BATTERY_USAGE_TABLE_COLUMNS \ "AppId TEXT NOT NULL, StartTime INTEGER NOT NULL DEFAULT 0, " \ - "EndTime INTEGER NOT NULL DEFAULT 0, Amount INTEGER NOT NULL DEFAULT 0" + "EndTime INTEGER NOT NULL DEFAULT 0, Amount INTEGER NOT NULL DEFAULT 0, " \ + "Utime INTEGER NOT NULL DEFAULT 0, Stime INTEGER NOT NULL DEFAULT 0" #define BATTERY_LAST_INFO_TABLE "Battery_LastInfo" #define BATTERY_LAST_INFO_TABLE_COLUMNS \ "RowId INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, " \ "LastResetTime UNSIGNED INT NOT NULL DEFAULT 0, " \ - "LastPercent UNSIGNED INT NOT NULL DEFAULT 0," \ - "LastPercentTime UNSIGNED INT NOT NULL DEFAULT 0" + "LastPercent UNSIGNED INT NOT NULL DEFAULT 0, " \ + "LastPercentTime UNSIGNED INT NOT NULL DEFAULT 0, " \ + "LastHeartTimestamp UNSIGNED INT NOT NULL DEFAULT 0" #define BATTERY_TEMP_TIME_INFO "Temp_BatteryTimeInfo" #define BATTERY_TEMP_TIME_INFO_COLUMNS \ "Amount INTEGER NOT NULL DEFAULT 0, StartTime INTEGER NOT NULL DEFAULT 0, " \ "EndTime INTEGER NOT NULL DEFAULT 0" +#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" + #define BATTERY_APP_ID "AppId" +#define BATTERY_INDEX "idx" #define BATTERY_TIMESTAMP "Timestamp" #define BATTERY_TYPE "Type" +#define BATTERY_DATA "Data" #define BATTERY_START_TIME "StartTime" #define BATTERY_END_TIME "EndTime" +#define BATTERY_UTIME "Utime" +#define BATTERY_STIME "Stime" #define BATTERY_AMOUNT "Amount" #define BATTERY_LAST_RESET_TIME "LastResetTime" #define BATTERY_LAST_PERCENT "LastPercent" #define BATTERY_LAST_PERCENT_TIME "LastPercentTime" +#define BATTERY_LAST_HEART_TIMESTAMP "LastHeartTimestamp" #define BATTERY_ROW_ID "RowId" #define CURRENT_TIME (int)(time(0)) +/* Typedefs */ +namespace ctx { + struct LastAppCpuUsageInfo { + int timestamp; + int utime; + int stime; + }; + typedef std::map CpuUsageMap; +} + enum AppLaunchType { TYPE_SERVICE = 0, TYPE_FOREGROUND, diff --git a/src/battery-stats/BatteryUsageAnalyzer.cpp b/src/battery-stats/BatteryUsageAnalyzer.cpp index e02b7be..73f1b2c 100644 --- a/src/battery-stats/BatteryUsageAnalyzer.cpp +++ b/src/battery-stats/BatteryUsageAnalyzer.cpp @@ -16,66 +16,103 @@ #include #include -#include "BatteryStatisticsTypes.h" #include "BatteryUsageAnalyzer.h" using namespace ctx; -bool BatteryUsageAnalyzer::calculateBatteryUsage(int lastTime, int currTime, std::vector& appLog, std::vector* usage) +void BatteryUsageAnalyzer::__normalizeBatteryUsage(std::vector* usage, int totalUtime, int totalStime) +{ + if (totalStime + totalUtime == 0) { + return; + } + + int utime; + int stime; + double batteryUsage; + + for (unsigned int i= 0; i < usage->size(); i++) { + (*usage)[i].get(NULL, BATTERY_UTIME, &utime); + (*usage)[i].get(NULL, BATTERY_STIME, &stime); + batteryUsage = ((double)(utime + stime)) / ((double)(totalUtime + totalStime)); + (*usage)[i].set(NULL, BATTERY_AMOUNT, batteryUsage); + } +} + +bool BatteryUsageAnalyzer::calculateBatteryUsage(int lastTime, int currTime, std::vector& cpuUsageLog, CpuUsageMap& lastCpuUsage, std::vector* usage) { int timeInterval = currTime - lastTime; IF_FAIL_RETURN(timeInterval > 0, false); - std::map lastAppTimeMap; std::string appId; int timestamp; - int type; + int stime; + int utime; + int idx; + int totalUtime = 0; + int totalStime = 0; - for (unsigned int i = 0; i < appLog.size(); i++) { - appLog[i].get(NULL, BATTERY_APP_ID, &appId); - appLog[i].get(NULL, BATTERY_TIMESTAMP, ×tamp); - appLog[i].get(NULL, BATTERY_TYPE, &type); + for (unsigned int i = 0; i < cpuUsageLog.size(); i++) { + cpuUsageLog[i].get(NULL, BATTERY_APP_ID, &appId); + cpuUsageLog[i].get(NULL, BATTERY_TIMESTAMP, ×tamp); + cpuUsageLog[i].get(NULL, BATTERY_UTIME, &utime); + cpuUsageLog[i].get(NULL, BATTERY_STIME, &stime); + cpuUsageLog[i].get(NULL, BATTERY_INDEX, &idx); if (timestamp < lastTime) { continue; } - if (timestamp > currTime) { break; } - if (type == TYPE_FOREGROUND) { // Mark timestamp of foreground - lastAppTimeMap[appId] = timestamp; - } else if (type == TYPE_BACKGROUND) { // Foreground duration calculation (Foreground->Background) + //If CPU table is reset, clear last app times and make lastTime as the timestamp + if (idx == 0) { + lastCpuUsage.clear(); + lastTime = timestamp; + } + + auto lastAppCpuUsage = lastCpuUsage.find(appId); + int prevUtime = 0; + int prevStime = 0; + int prevTimestamp = 0; + + if (lastAppCpuUsage != lastCpuUsage.end()) { + prevTimestamp = (lastAppCpuUsage->second).timestamp; + prevUtime = (lastAppCpuUsage->second).utime; + prevStime = (lastAppCpuUsage->second).stime; + Json row; - if (lastAppTimeMap.find(appId) == lastAppTimeMap.end() || lastAppTimeMap[appId] == 0) { - __createBatteryUsage(appId, lastTime, timestamp, timeInterval, &row); - } else { - __createBatteryUsage(appId, lastAppTimeMap[appId], timestamp, timeInterval, &row); - lastAppTimeMap[appId] = 0; - } + row.set(NULL, BATTERY_APP_ID, appId); + row.set(NULL, BATTERY_START_TIME, prevTimestamp); + row.set(NULL, BATTERY_END_TIME, timestamp); + row.set(NULL, BATTERY_AMOUNT, 0); + row.set(NULL, BATTERY_UTIME, utime - prevUtime); + row.set(NULL, BATTERY_STIME, stime - prevStime); + + totalUtime += utime - prevUtime; + totalStime += stime - prevStime; usage->push_back(row); - } - } - // Not closed app - for (auto it = lastAppTimeMap.begin(); it != lastAppTimeMap.end(); it++) { - if (it->second == 0) { - continue; + } else { + Json row; + row.set(NULL, BATTERY_APP_ID, appId); + row.set(NULL, BATTERY_START_TIME, lastTime); + row.set(NULL, BATTERY_END_TIME, timestamp); + row.set(NULL, BATTERY_AMOUNT, 0); + row.set(NULL, BATTERY_UTIME, utime); + row.set(NULL, BATTERY_STIME, stime); + + totalStime += stime; + totalUtime += utime; + usage->push_back(row); } - Json row; - __createBatteryUsage(it->first, it->second, currTime, timeInterval, &row); - usage->push_back(row); + LastAppCpuUsageInfo lastAppUsage; + lastAppUsage.timestamp = timestamp; + lastAppUsage.utime = utime; + lastAppUsage.stime = stime; + lastCpuUsage[appId] = lastAppUsage; } - + __normalizeBatteryUsage(usage, totalUtime, totalStime); return true; } - -void BatteryUsageAnalyzer::__createBatteryUsage(std::string appId, int startTime, int endTime, int timeInterval, Json* record) -{ - record->set(NULL, BATTERY_APP_ID, appId); - record->set(NULL, BATTERY_START_TIME, startTime); - record->set(NULL, BATTERY_END_TIME, endTime); - record->set(NULL, BATTERY_AMOUNT, (double)(endTime - startTime) / (double)timeInterval); -} diff --git a/src/battery-stats/BatteryUsageAnalyzer.h b/src/battery-stats/BatteryUsageAnalyzer.h index 982ca59..f5c8333 100644 --- a/src/battery-stats/BatteryUsageAnalyzer.h +++ b/src/battery-stats/BatteryUsageAnalyzer.h @@ -19,6 +19,7 @@ #include #include +#include "BatteryStatisticsTypes.h" namespace ctx { @@ -28,10 +29,10 @@ namespace ctx { BatteryUsageAnalyzer() {}; ~BatteryUsageAnalyzer() {}; - bool calculateBatteryUsage(int lastTime, int currTime, std::vector& appLog, std::vector* usage); + bool calculateBatteryUsage(int lastTime, int currTime, std::vector& cpuUsageLog, CpuUsageMap& lastCpuUsage, std::vector* usage); private: - void __createBatteryUsage(std::string appId, int startTime, int endTime, int timeInterval, Json* record); + void __normalizeBatteryUsage(std::vector* usage, int totalUtime, int totalStime); }; diff --git a/src/battery-stats/HeartDbReader.cpp b/src/battery-stats/HeartDbReader.cpp index 5b65b33..456d0d5 100644 --- a/src/battery-stats/HeartDbReader.cpp +++ b/src/battery-stats/HeartDbReader.cpp @@ -14,7 +14,7 @@ * limitations under the License. */ -//#include +#include #include #include #include "BatteryStatisticsTypes.h" @@ -24,6 +24,7 @@ #define HEART_APP_ID "appid" #define HEART_TIME "time" #define HEART_DATA "data" +#define HEART_INDEX "idx" #define HEART_DB_PATH tzplatform_mkpath(TZ_USER_DB, ".resourced-heart-default.db") @@ -59,16 +60,16 @@ void HeartDbReader::close() __heartDb = NULL; } -bool HeartDbReader::readAppLaunchLog(int startTime, int endTime, std::vector* appLog) +bool HeartDbReader::readCpuUsageLog(int startTime, int endTime, std::vector* cpuUsageLog) { IF_FAIL_RETURN(__heartDb, false); char* sql = sqlite3_mprintf( - "SELECT " HEART_APP_ID ", " HEART_TIME ", " HEART_DATA " FROM " HEART_CPU_TABLE \ + "SELECT " HEART_APP_ID ", " HEART_TIME ", " HEART_DATA ", " HEART_INDEX " FROM " HEART_CPU_TABLE \ " WHERE " HEART_TIME " >= %d AND " HEART_TIME " < %d", startTime, endTime); char* err = NULL; - int ret = sqlite3_exec(__heartDb, sql, __appLaunchLogCb, appLog, &err); + int ret = sqlite3_exec(__heartDb, sql, __cpuUsageLogCb, cpuUsageLog, &err); sqlite3_free(sql); if (ret != SQLITE_OK) { _E("Failed to read heart DB: Error(%s)", err); @@ -79,7 +80,7 @@ bool HeartDbReader::readAppLaunchLog(int startTime, int endTime, std::vectorpush_back(newRow); } return 0; } -void HeartDbReader::__convertAppLaunchLog(Json& row, Json* newRow) +void HeartDbReader::__convertCpuUsageLog(Json& row, Json* newRow) { std::string appId; std::string timestamp; std::string data; -/* int utime; + int utime; int stime; int pid; int type; -*/ + int index; + row.get(NULL, HEART_APP_ID, &appId); row.get(NULL, HEART_TIME, ×tamp); row.get(NULL, HEART_DATA, &data); -/* - std::stringstream buf = data; // TODO use utim & stime + row.get(NULL, HEART_INDEX, &index); + + std::stringstream buf(data); buf >> utime >> stime >> pid >> type; -*/ - std::string type = data.substr(data.length() - 2, 1); newRow->set(NULL, BATTERY_APP_ID, appId); newRow->set(NULL, BATTERY_TIMESTAMP, atoi(timestamp.c_str())); - newRow->set(NULL, BATTERY_TYPE, atoi(type.c_str())); + newRow->set(NULL, BATTERY_UTIME, utime); + newRow->set(NULL, BATTERY_STIME, stime); + newRow->set(NULL, BATTERY_TYPE, type); + newRow->set(NULL, BATTERY_INDEX, index); } diff --git a/src/battery-stats/HeartDbReader.h b/src/battery-stats/HeartDbReader.h index f355233..4c72902 100644 --- a/src/battery-stats/HeartDbReader.h +++ b/src/battery-stats/HeartDbReader.h @@ -32,11 +32,11 @@ namespace ctx { bool open(); void close(); - bool readAppLaunchLog(int startTime, int endTime, std::vector* appLog); + bool readCpuUsageLog(int startTime, int endTime, std::vector* cpuUsageLog); private: - static int __appLaunchLogCb(void *userData, int dim, char **value, char **column); - static void __convertAppLaunchLog(Json& row, Json* newRow); + static int __cpuUsageLogCb(void *userData, int dim, char **value, char **column); + static void __convertCpuUsageLog(Json& row, Json* newRow); sqlite3 *__heartDb; }; -- 2.34.1