From 6987b02636004b3d30f9605f0fa55d6aca108d93 Mon Sep 17 00:00:00 2001 From: Somin Kim Date: Thu, 19 Jan 2017 15:22:11 +0900 Subject: [PATCH] Add defence code for duration calculation when time manually changed Change-Id: I0f575e97cfc7fbd8f2e6e56a7f637898945f6073 Signed-off-by: Somin Kim --- src/app-stats/ActiveWindowMonitor.cpp | 19 ++++++++-- src/app-stats/ActiveWindowMonitor.h | 4 +++ src/app-stats/CMakeLists.txt | 1 + src/battery-stats/BatteryMonitor.cpp | 42 +++++++++++++++++----- src/battery-stats/BatteryMonitor.h | 3 ++ src/battery-stats/BatteryUsageAnalyzer.cpp | 8 +++++ src/battery-stats/CMakeLists.txt | 1 + 7 files changed, 68 insertions(+), 10 deletions(-) diff --git a/src/app-stats/ActiveWindowMonitor.cpp b/src/app-stats/ActiveWindowMonitor.cpp index 97a5b48..2f06201 100644 --- a/src/app-stats/ActiveWindowMonitor.cpp +++ b/src/app-stats/ActiveWindowMonitor.cpp @@ -78,14 +78,17 @@ public: ctx::AppUseMonitor::AppUseMonitor() : __signalId(-1), __lastCleanupTime(0), - __dbusWatcher(DBusType::SYSTEM) + __dbusWatcher(DBusType::SYSTEM), + __timeDiff(0) { __startLogging(); + vconf_notify_key_changed(VCONFKEY_SYSTEM_TIME_CHANGED, __timeChangeCb, this); } ctx::AppUseMonitor::~AppUseMonitor() { __stopLogging(); + vconf_ignore_key_changed(VCONFKEY_SYSTEM_TIME_CHANGED, __timeChangeCb); } bool ctx::AppUseMonitor::__startLogging() @@ -122,6 +125,7 @@ void ctx::AppUseMonitor::onSignal(const char* sender, const char* path, const ch } else if (STR_EQ(status, "bg")) { __finishRecord(appId); __removeExpired(); + __timeDiff = 0; } } @@ -154,7 +158,7 @@ void ctx::AppUseMonitor::__finishRecord(std::string appId) std::stringstream query; query << "UPDATE " APP_TABLE_USAGE_LOG \ - " SET " KEY_DURATION " = strftime('%s', 'now') - " KEY_UNIV_TIME \ + " SET " KEY_DURATION " = strftime('%s', 'now') - (" KEY_UNIV_TIME " + " << __timeDiff << ")" \ " WHERE " KEY_COL_ROW_ID " = (" \ "SELECT MAX(" KEY_COL_ROW_ID ") FROM " APP_TABLE_USAGE_LOG \ " WHERE " KEY_APP_ID " = '" << appId << "'" \ @@ -203,3 +207,14 @@ void ctx::AppUseMonitor::__removeExpired() KEY_UNIV_TIME " < strftime('%s', 'now') - " << LOG_RETENTION_PERIOD; __dbManager.execute(0, query.str().c_str(), NULL); } + +void ctx::AppUseMonitor::__timeChangeCb(keynode_t* node, void* userData) +{ + int timeDiff = vconf_keynode_get_int(node); + IF_FAIL_VOID(timeDiff != 0); + + _D("Time changed. Related timestamps will be modified."); + + AppUseMonitor* instance = static_cast(userData); + instance->__timeDiff = instance->__timeDiff + timeDiff; +} diff --git a/src/app-stats/ActiveWindowMonitor.h b/src/app-stats/ActiveWindowMonitor.h index 64f0d34..3170c5b 100644 --- a/src/app-stats/ActiveWindowMonitor.h +++ b/src/app-stats/ActiveWindowMonitor.h @@ -19,6 +19,7 @@ #include #include +#include #include #include @@ -30,6 +31,7 @@ namespace ctx { int __lastCleanupTime; DBusSignalWatcher __dbusWatcher; DatabaseManager __dbManager; + int __timeDiff; bool __startLogging(void); void __stopLogging(void); @@ -40,6 +42,8 @@ namespace ctx { void __removeExpired(); void onSignal(const char *sender, const char *path, const char *iface, const char *name, GVariant *param); + static void __timeChangeCb(keynode_t* node, void* userData); + public: AppUseMonitor(); ~AppUseMonitor(); diff --git a/src/app-stats/CMakeLists.txt b/src/app-stats/CMakeLists.txt index a11dec1..55de1df 100644 --- a/src/app-stats/CMakeLists.txt +++ b/src/app-stats/CMakeLists.txt @@ -6,6 +6,7 @@ SET(DEPS ${DEPS} capi-appfw-app-manager pkgmgr pkgmgr-info + vconf ) FILE(GLOB SRCS *.cpp) diff --git a/src/battery-stats/BatteryMonitor.cpp b/src/battery-stats/BatteryMonitor.cpp index 0bb5097..bee0c26 100644 --- a/src/battery-stats/BatteryMonitor.cpp +++ b/src/battery-stats/BatteryMonitor.cpp @@ -136,6 +136,24 @@ void BatteryMonitor::__chargerChangeCb(device_callback_e type, void* value, void } } +void BatteryMonitor::__timeChangeCb(keynode_t* node, void* userData) +{ + int timeDiff = vconf_keynode_get_int(node); + IF_FAIL_VOID(timeDiff != 0); + + _D("Time changed. Related timestamps will be modified."); + + BatteryMonitor* instance = static_cast(userData); + __needSync = true; + instance->__lastFullTime = instance->__lastFullTime + timeDiff; + instance->__lastHeartAccessTime = instance->__lastHeartAccessTime + timeDiff; + + bool ret = instance->__updateLastInfo(); + IF_FAIL_VOID_TAG(ret, _E, "Failed to update last reset time and last percent time"); + + __instance->__modifyLastCpuUsage(timeDiff); +} + gboolean BatteryMonitor::__timeoutCb(gpointer data) { BatteryMonitor* instance = static_cast(data); @@ -154,6 +172,9 @@ int BatteryMonitor::start() int error = device_add_callback(DEVICE_CALLBACK_BATTERY_CHARGING, __chargerChangeCb, this); IF_FAIL_RETURN_TAG(error == DEVICE_ERROR_NONE, ERR_OPERATION_FAILED, _E, "Failed to set battery charging change cb"); + error = vconf_notify_key_changed(VCONFKEY_SYSTEM_TIME_CHANGED, __timeChangeCb, this); + IF_FAIL_RETURN_TAG(error == VCONF_OK, ERR_OPERATION_FAILED, _E, "Failed to set time changed cb"); + _D("Start timer to request HEART data"); g_timeout_add(HEART_DB_QUERY_INTERVAL, __timeoutCb, this); @@ -170,6 +191,9 @@ int BatteryMonitor::stop() int error = device_remove_callback(DEVICE_CALLBACK_BATTERY_CHARGING, __chargerChangeCb); IF_FAIL_RETURN_TAG(error == DEVICE_ERROR_NONE, ERR_OPERATION_FAILED, _E, "Failed to remove callback for charger status"); + error = vconf_ignore_key_changed(VCONFKEY_SYSTEM_TIME_CHANGED, __timeChangeCb); + IF_FAIL_RETURN_TAG(error == VCONF_OK, ERR_OPERATION_FAILED, _E, "Failed to remove callback for time changed"); + return ERR_NONE; } @@ -206,13 +230,6 @@ bool BatteryMonitor::__getLastCpuUsageTable(CpuUsageMap* lastHeartCpuLog) bool BatteryMonitor::__processBatteryUsage() { - if (__lastHeartAccessTime > CURRENT_TIME) { - __lastHeartAccessTime = CURRENT_TIME - DEFAULT_TIME_DIFF; - __lastFullTime = CURRENT_TIME - DEFAULT_TIME_DIFF; - __updateLastInfo(); - __dbMgr.executeSync("DELETE FROM " BATTERY_LAST_CPU_USAGE_TABLE, NULL); - } - // Read from heart cpu table bool ret = __heartReader->dbOpen(); IF_FAIL_RETURN_TAG(ret, true, _E, "Failed to open heart db"); @@ -301,7 +318,7 @@ void BatteryMonitor::__removeExpiredLog() bool BatteryMonitor::__updateLastCpuUsageLog(CpuUsageMap& usage) { - _D("Delete all rows from app last times table"); + _D("Delete all rows from last cpu usage table"); __dbMgr.executeSync("DELETE FROM " BATTERY_LAST_CPU_USAGE_TABLE, NULL); IF_FAIL_RETURN_TAG(usage.size(), true, _W, "No data"); @@ -351,3 +368,12 @@ void BatteryMonitor::prepareData() __processBatteryUsage(); } + +void BatteryMonitor::__modifyLastCpuUsage(int timeDiff) +{ + char *query = sqlite3_mprintf("UPDATE Temp_LastCpuUsagePerApp SET Timestamp = Timestamp + (%d)", timeDiff); + __dbMgr.executeSync(query, NULL); + sqlite3_free(query); + + _D("Modified timestamp of LastCpuUsage"); +} diff --git a/src/battery-stats/BatteryMonitor.h b/src/battery-stats/BatteryMonitor.h index f87dc3b..bb6092a 100644 --- a/src/battery-stats/BatteryMonitor.h +++ b/src/battery-stats/BatteryMonitor.h @@ -18,6 +18,7 @@ #define _CONTEXT_BATTERY_MONITOR_H_ #include +#include #include #include "BatteryUsageAnalyzer.h" #include "HeartDbReader.h" @@ -47,8 +48,10 @@ namespace ctx { bool __updateLastCpuUsageLog(CpuUsageMap& usage); bool __getLastCpuUsageTable(CpuUsageMap* lastCpuUsage); void __removeExpiredLog(); + void __modifyLastCpuUsage(int timeDiff); static void __chargerChangeCb(device_callback_e type, void* value, void* userData); + static void __timeChangeCb(keynode_t* node, void* userData); static int __timeoutCb(void* data); static BatteryMonitor* __instance; diff --git a/src/battery-stats/BatteryUsageAnalyzer.cpp b/src/battery-stats/BatteryUsageAnalyzer.cpp index bb08d8b..cdc0d39 100644 --- a/src/battery-stats/BatteryUsageAnalyzer.cpp +++ b/src/battery-stats/BatteryUsageAnalyzer.cpp @@ -64,11 +64,19 @@ void BatteryUsageAnalyzer::calculateBatteryUsage(std::vector& cpuLog, CpuU } } + // Process duplicated logs if (utime - prevUtime == 0 && stime - prevStime == 0) { recentCpuUsageMap[appId].timestamp = timestamp; continue; } + // Process invalid logs + if (utime - prevUtime < 0 || stime - prevStime < 0) + continue; + + if (prevTimestamp > timestamp) + prevTimestamp = timestamp; + Json row; row.set(NULL, BATTERY_APP_ID, appId); row.set(NULL, BATTERY_START_TIME, prevTimestamp); diff --git a/src/battery-stats/CMakeLists.txt b/src/battery-stats/CMakeLists.txt index 00fbdf8..736984f 100644 --- a/src/battery-stats/CMakeLists.txt +++ b/src/battery-stats/CMakeLists.txt @@ -2,6 +2,7 @@ SET(target "${target_prefix}-battery-stats") SET(DEPS ${DEPS} capi-system-device + vconf ) FILE(GLOB SRCS *.cpp) -- 2.34.1